|
| 1 | +// Copyright 2022 The go-python Authors. All rights reserved. |
| 2 | +// Use of this source code is governed by a BSD-style |
| 3 | +// license that can be found in the LICENSE file. |
| 4 | + |
| 5 | +// Package binascii provides the implementation of the python's 'binascii' module. |
| 6 | +package binascii |
| 7 | + |
| 8 | +import ( |
| 9 | + "encoding/base64" |
| 10 | + "encoding/hex" |
| 11 | + "errors" |
| 12 | + "hash/crc32" |
| 13 | + |
| 14 | + "github.com/go-python/gpython/py" |
| 15 | +) |
| 16 | + |
| 17 | +var ( |
| 18 | + Incomplete = py.ExceptionType.NewType("binascii.Incomplete", "", nil, nil) |
| 19 | + Error = py.ValueError.NewType("binascii.Error", "", nil, nil) |
| 20 | +) |
| 21 | + |
| 22 | +func init() { |
| 23 | + py.RegisterModule(&py.ModuleImpl{ |
| 24 | + Info: py.ModuleInfo{ |
| 25 | + Name: "binascii", |
| 26 | + Doc: "Conversion between binary data and ASCII", |
| 27 | + }, |
| 28 | + Methods: []*py.Method{ |
| 29 | + py.MustNewMethod("a2b_base64", a2b_base64, 0, "Decode a line of base64 data."), |
| 30 | + py.MustNewMethod("b2a_base64", b2a_base64, 0, "Base64-code line of data."), |
| 31 | + py.MustNewMethod("a2b_hex", a2b_hex, 0, a2b_hex_doc), |
| 32 | + py.MustNewMethod("b2a_hex", b2a_hex, 0, b2a_hex_doc), |
| 33 | + py.MustNewMethod("crc32", crc32_, 0, "Compute CRC-32 incrementally."), |
| 34 | + py.MustNewMethod("unhexlify", a2b_hex, 0, unhexlify_doc), |
| 35 | + py.MustNewMethod("hexlify", b2a_hex, 0, hexlify_doc), |
| 36 | + }, |
| 37 | + Globals: py.StringDict{ |
| 38 | + "Incomplete": Incomplete, |
| 39 | + "Error": Error, |
| 40 | + }, |
| 41 | + }) |
| 42 | +} |
| 43 | + |
| 44 | +func b2a_base64(self py.Object, args py.Tuple, kwargs py.StringDict) (py.Object, error) { |
| 45 | + var ( |
| 46 | + pydata py.Object |
| 47 | + pynewl py.Object = py.True |
| 48 | + ) |
| 49 | + err := py.ParseTupleAndKeywords(args, kwargs, "y*|p:binascii.b2a_base64", []string{"data", "newline"}, &pydata, &pynewl) |
| 50 | + if err != nil { |
| 51 | + return nil, err |
| 52 | + } |
| 53 | + |
| 54 | + var ( |
| 55 | + buf = []byte(pydata.(py.Bytes)) |
| 56 | + newline = bool(pynewl.(py.Bool)) |
| 57 | + ) |
| 58 | + |
| 59 | + out := base64.StdEncoding.EncodeToString(buf) |
| 60 | + if newline { |
| 61 | + out += "\n" |
| 62 | + } |
| 63 | + return py.Bytes(out), nil |
| 64 | +} |
| 65 | + |
| 66 | +func a2b_base64(self py.Object, args py.Tuple) (py.Object, error) { |
| 67 | + var pydata py.Object |
| 68 | + err := py.ParseTuple(args, "s:binascii.a2b_base64", &pydata) |
| 69 | + if err != nil { |
| 70 | + return nil, err |
| 71 | + } |
| 72 | + |
| 73 | + out, err := base64.StdEncoding.DecodeString(string(pydata.(py.String))) |
| 74 | + if err != nil { |
| 75 | + return nil, py.ExceptionNewf(Error, "could not decode base64 data: %+v", err) |
| 76 | + } |
| 77 | + |
| 78 | + return py.Bytes(out), nil |
| 79 | +} |
| 80 | + |
| 81 | +func crc32_(self py.Object, args py.Tuple) (py.Object, error) { |
| 82 | + var ( |
| 83 | + pydata py.Object |
| 84 | + pycrc py.Object = py.Int(0) |
| 85 | + ) |
| 86 | + |
| 87 | + err := py.ParseTuple(args, "y*|i:binascii.crc32", &pydata, &pycrc) |
| 88 | + if err != nil { |
| 89 | + return nil, err |
| 90 | + } |
| 91 | + |
| 92 | + crc := crc32.Update(uint32(pycrc.(py.Int)), crc32.IEEETable, []byte(pydata.(py.Bytes))) |
| 93 | + return py.Int(crc), nil |
| 94 | + |
| 95 | +} |
| 96 | + |
| 97 | +const a2b_hex_doc = `Binary data of hexadecimal representation. |
| 98 | +
|
| 99 | +hexstr must contain an even number of hex digits (upper or lower case). |
| 100 | +This function is also available as "unhexlify()".` |
| 101 | + |
| 102 | +func a2b_hex(self py.Object, args py.Tuple) (py.Object, error) { |
| 103 | + var ( |
| 104 | + hexErr hex.InvalidByteError |
| 105 | + pydata py.Object |
| 106 | + src string |
| 107 | + ) |
| 108 | + err := py.ParseTuple(args, "s*:binascii.a2b_hex", &pydata) |
| 109 | + if err != nil { |
| 110 | + return nil, err |
| 111 | + } |
| 112 | + |
| 113 | + switch v := pydata.(type) { |
| 114 | + case py.String: |
| 115 | + src = string(v) |
| 116 | + case py.Bytes: |
| 117 | + src = string(v) |
| 118 | + } |
| 119 | + |
| 120 | + o, err := hex.DecodeString(src) |
| 121 | + if err != nil { |
| 122 | + switch { |
| 123 | + case errors.Is(err, hex.ErrLength): |
| 124 | + return nil, py.ExceptionNewf(Error, "Odd-length string") |
| 125 | + case errors.As(err, &hexErr): |
| 126 | + return nil, py.ExceptionNewf(Error, "Non-hexadecimal digit found") |
| 127 | + default: |
| 128 | + return nil, py.ExceptionNewf(Error, "could not decode hex data: %+v", err) |
| 129 | + } |
| 130 | + } |
| 131 | + |
| 132 | + return py.Bytes(o), nil |
| 133 | +} |
| 134 | + |
| 135 | +const b2a_hex_doc = `Hexadecimal representation of binary data. |
| 136 | +
|
| 137 | +The return value is a bytes object. This function is also |
| 138 | +available as "hexlify()".` |
| 139 | + |
| 140 | +func b2a_hex(self py.Object, args py.Tuple) (py.Object, error) { |
| 141 | + var pydata py.Object |
| 142 | + err := py.ParseTuple(args, "y*:binascii.b2a_hex", &pydata) |
| 143 | + if err != nil { |
| 144 | + return nil, err |
| 145 | + } |
| 146 | + |
| 147 | + o := hex.EncodeToString([]byte(pydata.(py.Bytes))) |
| 148 | + return py.Bytes(o), nil |
| 149 | +} |
| 150 | + |
| 151 | +const unhexlify_doc = `Binary data of hexadecimal representation. |
| 152 | +
|
| 153 | +hexstr must contain an even number of hex digits (upper or lower case).` |
| 154 | + |
| 155 | +const hexlify_doc = `Hexadecimal representation of binary data. |
| 156 | +
|
| 157 | +The return value is a bytes object. This function is also |
| 158 | +available as "b2a_hex()".` |
0 commit comments