blob: 2c2fba2ca00cd27559c56788ff0af31d85079508 [file] [log] [blame]
Tim Hall79d07d22020-04-27 18:20:16 +01001/*
2 * Copyright (c) 2020 Arm Limited. All rights reserved.
3 *
4 * SPDX-License-Identifier: Apache-2.0
5 *
6 * Licensed under the Apache License, Version 2.0 (the License); you may
7 * not use this file except in compliance with the License.
8 * You may obtain a copy of the License at
9 *
10 * www.apache.org/licenses/LICENSE-2.0
11 *
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an AS IS BASIS, WITHOUT
14 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
17 */
18
19#define PY_SSIZE_T_CLEAN
20#include <Python.h>
21
22#include "mlw_decode.h"
23#include "mlw_encode.h"
24
25/* C extension wrapper for mlw_encode
26 *
27 * This method is exposed directly in python with the arguments with a
28 * prototype of the form:
29 *
30 * output = mlw_codec.encode(input, verbose=0)
31 *
32 * input: [int]
33 * verbose: int
34 * output: bytearray
35 */
36
37static PyObject *
38method_encode (PyObject *self, PyObject *args)
39{
40 /* Object to hold the input integer list. */
41 PyObject *input_list_object;
42
43 /* Object to hold the input verbosity integer, the verbose argument
44 * is optional so defaulted to 0.
45 */
46 int verbose = 0;
47
48 /* Arguments to the method are delivered as a tuple, unpack the
49 * tuple to get the individual arguments, note the second is
50 * optional.
51 */
52 if (!PyArg_ParseTuple(args, "O|i", &input_list_object, &verbose))
53 return NULL;
54
55 /* Unpack the length of the input integer list. */
Louis Verhaard60232142021-01-22 14:11:15 +010056 Py_ssize_t input_length = PyObject_Length (input_list_object);
57 if (input_length < 0) {
58 return NULL;
59 }
Tim Hall79d07d22020-04-27 18:20:16 +010060
61 /* We need to marshall the integer list into an input buffer
62 * suitable for mlw_encode, use a temporary heap allocated buffer
63 * for that purpose.
64 */
65 int16_t *input_buffer = (int16_t *) malloc(sizeof(int16_t *) * input_length);
66 if (input_buffer == NULL)
67 return PyErr_NoMemory();
68
69 /* Unpack the input integer list into the temporary buffer.
70 */
71 for (int i = 0; i < input_length; i++)
72 {
73 PyObject *item;
74 item = PyList_GetItem(input_list_object, i);
Louis Verhaard60232142021-01-22 14:11:15 +010075 long value = PyLong_AsLong(item);
76 if (value < -255 || value > 255) {
77 PyErr_SetString(PyExc_ValueError, "Input value out of bounds");
78 return NULL;
79 }
80 input_buffer[i] = value;
Tim Hall79d07d22020-04-27 18:20:16 +010081 }
Louis Verhaard60232142021-01-22 14:11:15 +010082 if (PyErr_Occurred() != NULL) {
83 PyErr_SetString(PyExc_ValueError, "Invalid input");
84 return NULL;
85 }
Tim Hall79d07d22020-04-27 18:20:16 +010086
87 /* We don't know the output length required, we guess worst case,
88 * the mlw_encode call will do a resize (downwards) anyway.
89 */
Louis Verhaard60232142021-01-22 14:11:15 +010090 uint8_t *output_buffer = (uint8_t *) malloc(input_length);
Tim Hall79d07d22020-04-27 18:20:16 +010091 if (output_buffer == NULL)
92 return PyErr_NoMemory();
93
94 int output_length = mlw_encode(input_buffer, input_length, &output_buffer, verbose);
95
96 PyObject *output_byte_array = PyByteArray_FromStringAndSize ((char *) output_buffer, output_length);
97
98 /* Discard the temporary input and output buffers. */
99 free (input_buffer);
100 free (output_buffer);
101
102 return output_byte_array;
103}
104
105/* C extension wrapper for mlw_decode
106 *
107 * This method is exposed directly in python with the arguments with a
108 * prototype of the form:
109 *
110 * output = mlw_codec.decode(input, verbose=0)
111 *
112 * input: bytearray
113 * verbose: int
114 * output: [int]
115 */
116
117static PyObject *
118method_decode(PyObject *self, PyObject *args)
119{
120 /* Object to hold the input bytearray. */
121 PyObject *input_bytearray_object;
122
123 /* Object to hold the input verbosity integer, the verbose argument
124 * is optional so defaulted to 0.
125 */
126 int verbose = 0;
127
128 /* Arguments to the method are delivered as a tuple, unpack the
129 * tuple to get the individual arguments, note the second is
130 * optional.
131 */
132 if (!PyArg_ParseTuple(args, "Y|i", &input_bytearray_object, &verbose))
133 return NULL;
134
135 /* Unpack the input buffer and length from the bytearray object. */
136 uint8_t *input_buffer = (uint8_t *) PyByteArray_AsString(input_bytearray_object);
Louis Verhaard60232142021-01-22 14:11:15 +0100137 Py_ssize_t input_length = PyByteArray_Size(input_bytearray_object);
Tim Hall79d07d22020-04-27 18:20:16 +0100138
139 /* We don't know the output length required, we guess, but the guess
140 * will be too small, the mlw_decode call will do a resize (upwards)
141 * anyway.
142 */
Louis Verhaard60232142021-01-22 14:11:15 +0100143 int16_t *output_buffer = (int16_t *) malloc (input_length);
Tim Hall79d07d22020-04-27 18:20:16 +0100144 if (output_buffer == NULL)
145 return PyErr_NoMemory();
146
147 int output_length = mlw_decode (input_buffer, input_length, &output_buffer, verbose);
148
149 /* Construct a new integer list and marshall the output buffer
150 * contents into the list. */
151 PyObject *output_list = PyList_New(output_length);
152 for (int i = 0; i <output_length; i++)
153 PyList_SetItem (output_list, i, PyLong_FromLong (output_buffer[i]));
154
155 free (output_buffer);
156
157 return output_list;
158}
159
160/* mlw_codec method descriptors.
161 */
162
163static PyMethodDef mlw_methods[] = {
164 {"decode", method_decode, METH_VARARGS, "Python interface for decode"},
165 {"encode", method_encode, METH_VARARGS, "Python interface for encode"},
166 {NULL, NULL, 0, NULL}
167};
168
169/* mlw_codec module descriptor.
170 */
171
172static struct PyModuleDef mlw_codecmodule = {
173 PyModuleDef_HEAD_INIT,
174 "mlw_codec",
175 "Python interface for the mlw encoder",
176 -1,
177 mlw_methods
178};
179
180PyMODINIT_FUNC PyInit_mlw_codec(void) {
181 return PyModule_Create(&mlw_codecmodule);
182}