Skip to content

Commit 36555f6

Browse files
authored
CBOR bindings (#559)
1 parent 58f0a16 commit 36555f6

File tree

9 files changed

+3533
-6
lines changed

9 files changed

+3533
-6
lines changed

.gitignore

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -386,12 +386,7 @@ poetry.toml
386386
pyrightconfig.json
387387

388388
### VisualStudioCode ###
389-
.vscode/*
390-
!.vscode/settings.json
391-
!.vscode/tasks.json
392-
!.vscode/launch.json
393-
!.vscode/extensions.json
394-
!.vscode/*.code-snippets
389+
.vscode
395390

396391
# Local History for Visual Studio Code
397392
.history/

awscrt/cbor.py

Lines changed: 573 additions & 0 deletions
Large diffs are not rendered by default.

source/cbor.c

Lines changed: 785 additions & 0 deletions
Large diffs are not rendered by default.

source/cbor.h

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
#ifndef AWS_CRT_PYTHON_CBOR_H
2+
#define AWS_CRT_PYTHON_CBOR_H
3+
/**
4+
* Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
5+
* SPDX-License-Identifier: Apache-2.0.
6+
*/
7+
#include "module.h"
8+
9+
/*******************************************************************************
10+
* ENCODE
11+
******************************************************************************/
12+
13+
PyObject *aws_py_cbor_encoder_new(PyObject *self, PyObject *args);
14+
PyObject *aws_py_cbor_encoder_get_encoded_data(PyObject *self, PyObject *args);
15+
16+
PyObject *aws_py_cbor_encoder_write_uint(PyObject *self, PyObject *args);
17+
PyObject *aws_py_cbor_encoder_write_negint(PyObject *self, PyObject *args);
18+
PyObject *aws_py_cbor_encoder_write_float(PyObject *self, PyObject *args);
19+
PyObject *aws_py_cbor_encoder_write_bytes(PyObject *self, PyObject *args);
20+
PyObject *aws_py_cbor_encoder_write_text(PyObject *self, PyObject *args);
21+
PyObject *aws_py_cbor_encoder_write_array_start(PyObject *self, PyObject *args);
22+
PyObject *aws_py_cbor_encoder_write_map_start(PyObject *self, PyObject *args);
23+
PyObject *aws_py_cbor_encoder_write_tag(PyObject *self, PyObject *args);
24+
PyObject *aws_py_cbor_encoder_write_bool(PyObject *self, PyObject *args);
25+
26+
/* Encode the types without value needed. The arg is the type to encode. */
27+
PyObject *aws_py_cbor_encoder_write_simple_types(PyObject *self, PyObject *args);
28+
29+
PyObject *aws_py_cbor_encoder_write_py_list(PyObject *self, PyObject *args);
30+
PyObject *aws_py_cbor_encoder_write_py_dict(PyObject *self, PyObject *args);
31+
PyObject *aws_py_cbor_encoder_write_data_item(PyObject *self, PyObject *args);
32+
33+
/*******************************************************************************
34+
* DECODE
35+
******************************************************************************/
36+
37+
PyObject *aws_py_cbor_decoder_new(PyObject *self, PyObject *args);
38+
PyObject *aws_py_cbor_decoder_peek_type(PyObject *self, PyObject *args);
39+
PyObject *aws_py_cbor_decoder_get_remaining_bytes_len(PyObject *self, PyObject *args);
40+
PyObject *aws_py_cbor_decoder_consume_next_element(PyObject *self, PyObject *args);
41+
PyObject *aws_py_cbor_decoder_consume_next_data_item(PyObject *self, PyObject *args);
42+
PyObject *aws_py_cbor_decoder_pop_next_unsigned_int(PyObject *self, PyObject *args);
43+
PyObject *aws_py_cbor_decoder_pop_next_negative_int(PyObject *self, PyObject *args);
44+
PyObject *aws_py_cbor_decoder_pop_next_float(PyObject *self, PyObject *args);
45+
PyObject *aws_py_cbor_decoder_pop_next_boolean(PyObject *self, PyObject *args);
46+
PyObject *aws_py_cbor_decoder_pop_next_bytes(PyObject *self, PyObject *args);
47+
PyObject *aws_py_cbor_decoder_pop_next_text(PyObject *self, PyObject *args);
48+
PyObject *aws_py_cbor_decoder_pop_next_array_start(PyObject *self, PyObject *args);
49+
PyObject *aws_py_cbor_decoder_pop_next_map_start(PyObject *self, PyObject *args);
50+
PyObject *aws_py_cbor_decoder_pop_next_tag(PyObject *self, PyObject *args);
51+
52+
PyObject *aws_py_cbor_decoder_pop_next_py_list(PyObject *self, PyObject *args);
53+
PyObject *aws_py_cbor_decoder_pop_next_py_dict(PyObject *self, PyObject *args);
54+
PyObject *aws_py_cbor_decoder_pop_next_data_item(PyObject *self, PyObject *args);
55+
56+
#endif /* AWS_CRT_PYTHON_CBOR_H */

source/module.c

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
#include "module.h"
66

77
#include "auth.h"
8+
#include "cbor.h"
89
#include "checksums.h"
910
#include "common.h"
1011
#include "crypto.h"
@@ -117,6 +118,14 @@ PyObject *PyUnicode_FromAwsString(const struct aws_string *aws_str) {
117118
return PyUnicode_FromStringAndSize(aws_string_c_str(aws_str), aws_str->len);
118119
}
119120

121+
PyObject *PyBytes_FromAwsByteCursor(const struct aws_byte_cursor *cursor) {
122+
if (cursor->len > PY_SSIZE_T_MAX) {
123+
PyErr_SetString(PyExc_OverflowError, "Cursor exceeds PY_SSIZE_T_MAX");
124+
return NULL;
125+
}
126+
return PyBytes_FromStringAndSize((const char *)cursor->ptr, (Py_ssize_t)cursor->len);
127+
}
128+
120129
uint32_t PyObject_GetAttrAsUint32(PyObject *o, const char *class_name, const char *attr_name) {
121130
uint32_t result = UINT32_MAX;
122131

@@ -899,6 +908,41 @@ static PyMethodDef s_module_methods[] = {
899908
AWS_PY_METHOD_DEF(websocket_increment_read_window, METH_VARARGS),
900909
AWS_PY_METHOD_DEF(websocket_create_handshake_request, METH_VARARGS),
901910

911+
/* CBOR Encode */
912+
AWS_PY_METHOD_DEF(cbor_encoder_new, METH_VARARGS),
913+
AWS_PY_METHOD_DEF(cbor_encoder_get_encoded_data, METH_VARARGS),
914+
AWS_PY_METHOD_DEF(cbor_encoder_write_uint, METH_VARARGS),
915+
AWS_PY_METHOD_DEF(cbor_encoder_write_negint, METH_VARARGS),
916+
AWS_PY_METHOD_DEF(cbor_encoder_write_float, METH_VARARGS),
917+
AWS_PY_METHOD_DEF(cbor_encoder_write_bytes, METH_VARARGS),
918+
AWS_PY_METHOD_DEF(cbor_encoder_write_text, METH_VARARGS),
919+
AWS_PY_METHOD_DEF(cbor_encoder_write_array_start, METH_VARARGS),
920+
AWS_PY_METHOD_DEF(cbor_encoder_write_map_start, METH_VARARGS),
921+
AWS_PY_METHOD_DEF(cbor_encoder_write_tag, METH_VARARGS),
922+
AWS_PY_METHOD_DEF(cbor_encoder_write_bool, METH_VARARGS),
923+
AWS_PY_METHOD_DEF(cbor_encoder_write_simple_types, METH_VARARGS),
924+
AWS_PY_METHOD_DEF(cbor_encoder_write_py_list, METH_VARARGS),
925+
AWS_PY_METHOD_DEF(cbor_encoder_write_py_dict, METH_VARARGS),
926+
AWS_PY_METHOD_DEF(cbor_encoder_write_data_item, METH_VARARGS),
927+
928+
/* CBOR Decode */
929+
AWS_PY_METHOD_DEF(cbor_decoder_new, METH_VARARGS),
930+
AWS_PY_METHOD_DEF(cbor_decoder_peek_type, METH_VARARGS),
931+
AWS_PY_METHOD_DEF(cbor_decoder_get_remaining_bytes_len, METH_VARARGS),
932+
AWS_PY_METHOD_DEF(cbor_decoder_consume_next_element, METH_VARARGS),
933+
AWS_PY_METHOD_DEF(cbor_decoder_consume_next_data_item, METH_VARARGS),
934+
AWS_PY_METHOD_DEF(cbor_decoder_pop_next_unsigned_int, METH_VARARGS),
935+
AWS_PY_METHOD_DEF(cbor_decoder_pop_next_negative_int, METH_VARARGS),
936+
AWS_PY_METHOD_DEF(cbor_decoder_pop_next_float, METH_VARARGS),
937+
AWS_PY_METHOD_DEF(cbor_decoder_pop_next_boolean, METH_VARARGS),
938+
AWS_PY_METHOD_DEF(cbor_decoder_pop_next_bytes, METH_VARARGS),
939+
AWS_PY_METHOD_DEF(cbor_decoder_pop_next_text, METH_VARARGS),
940+
AWS_PY_METHOD_DEF(cbor_decoder_pop_next_array_start, METH_VARARGS),
941+
AWS_PY_METHOD_DEF(cbor_decoder_pop_next_map_start, METH_VARARGS),
942+
AWS_PY_METHOD_DEF(cbor_decoder_pop_next_tag, METH_VARARGS),
943+
AWS_PY_METHOD_DEF(cbor_decoder_pop_next_py_list, METH_VARARGS),
944+
AWS_PY_METHOD_DEF(cbor_decoder_pop_next_py_dict, METH_VARARGS),
945+
AWS_PY_METHOD_DEF(cbor_decoder_pop_next_data_item, METH_VARARGS),
902946
{NULL, NULL, 0, NULL},
903947
};
904948

source/module.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ enum aws_crt_python_errors {
3030
/* AWS Specific Helpers */
3131
PyObject *PyUnicode_FromAwsByteCursor(const struct aws_byte_cursor *cursor);
3232
PyObject *PyUnicode_FromAwsString(const struct aws_string *aws_str);
33+
PyObject *PyBytes_FromAwsByteCursor(const struct aws_byte_cursor *cursor);
3334

3435
/* Return the named attribute, converted to the specified type.
3536
* If conversion cannot occur a python exception is set (check PyExc_Occurred()) */

0 commit comments

Comments
 (0)