|
| 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 | + "hash/crc32" |
| 11 | + |
| 12 | + "github.com/go-python/gpython/py" |
| 13 | +) |
| 14 | + |
| 15 | +var ( |
| 16 | + Incomplete = py.ExceptionType.NewType("binascii.Incomplete", "", nil, nil) |
| 17 | + Error = py.ValueError.NewType("binascii.Error", "", nil, nil) |
| 18 | +) |
| 19 | + |
| 20 | +func init() { |
| 21 | + py.RegisterModule(&py.ModuleImpl{ |
| 22 | + Info: py.ModuleInfo{ |
| 23 | + Name: "binascii", |
| 24 | + Doc: "Conversion between binary data and ASCII", |
| 25 | + }, |
| 26 | + Methods: []*py.Method{ |
| 27 | + py.MustNewMethod("a2b_base64", a2b_base64, 0, "Decode a line of base64 data."), |
| 28 | + py.MustNewMethod("b2a_base64", b2a_base64, 0, "Base64-code line of data."), |
| 29 | + py.MustNewMethod("crc32", crc32_, 0, "Compute CRC-32 incrementally."), |
| 30 | + }, |
| 31 | + Globals: py.StringDict{ |
| 32 | + "Incomplete": Incomplete, |
| 33 | + "Error": Error, |
| 34 | + }, |
| 35 | + }) |
| 36 | +} |
| 37 | + |
| 38 | +func b2a_base64(self py.Object, args py.Tuple, kwargs py.StringDict) (py.Object, error) { |
| 39 | + var ( |
| 40 | + pydata py.Object |
| 41 | + pynewl py.Object = py.True |
| 42 | + ) |
| 43 | + err := py.ParseTupleAndKeywords(args, kwargs, "y*|p:binascii.b2a_base64", []string{"data", "newline"}, &pydata, &pynewl) |
| 44 | + if err != nil { |
| 45 | + return nil, err |
| 46 | + } |
| 47 | + |
| 48 | + var ( |
| 49 | + buf = []byte(pydata.(py.Bytes)) |
| 50 | + newline = bool(pynewl.(py.Bool)) |
| 51 | + ) |
| 52 | + |
| 53 | + out := base64.StdEncoding.EncodeToString(buf) |
| 54 | + if newline { |
| 55 | + out += "\n" |
| 56 | + } |
| 57 | + return py.Bytes(out), nil |
| 58 | +} |
| 59 | + |
| 60 | +func a2b_base64(self py.Object, args py.Tuple) (py.Object, error) { |
| 61 | + var pydata py.Object |
| 62 | + err := py.ParseTuple(args, "s:binascii.a2b_base64", &pydata) |
| 63 | + if err != nil { |
| 64 | + return nil, err |
| 65 | + } |
| 66 | + |
| 67 | + out, err := base64.StdEncoding.DecodeString(string(pydata.(py.String))) |
| 68 | + if err != nil { |
| 69 | + return nil, py.ExceptionNewf(Error, "could not decode base64 data: %+v", err) |
| 70 | + } |
| 71 | + |
| 72 | + return py.Bytes(out), nil |
| 73 | +} |
| 74 | + |
| 75 | +func crc32_(self py.Object, args py.Tuple) (py.Object, error) { |
| 76 | + var ( |
| 77 | + pydata py.Object |
| 78 | + pycrc py.Object = py.Int(0) |
| 79 | + ) |
| 80 | + |
| 81 | + err := py.ParseTuple(args, "y*|i:binascii.crc32", &pydata, &pycrc) |
| 82 | + if err != nil { |
| 83 | + return nil, err |
| 84 | + } |
| 85 | + |
| 86 | + crc := crc32.Update(uint32(pycrc.(py.Int)), crc32.IEEETable, []byte(pydata.(py.Bytes))) |
| 87 | + return py.Int(crc), nil |
| 88 | + |
| 89 | +} |
0 commit comments