Skip to content

CBOR bindings #559

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 32 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
32 commits
Select commit Hold shift + click to select a range
93c6148
WIP
TingDaoK Apr 12, 2024
22dee0e
Merge branch 'main' into cbor-main
TingDaoK Apr 12, 2024
4c5f80f
renaming stuff
TingDaoK Apr 15, 2024
721b617
Merge branch 'cbor-main' of github.com:awslabs/aws-crt-python into cb…
TingDaoK Apr 15, 2024
36ca2b5
get next -> pop next
TingDaoK Apr 15, 2024
9e32058
new line at the end
TingDaoK Apr 15, 2024
1f19817
basic impl
TingDaoK Apr 19, 2024
0e3370d
WIP
TingDaoK Apr 19, 2024
da13a7f
decode 2
TingDaoK Apr 22, 2024
eefd3f0
encoding rewrite
TingDaoK Apr 22, 2024
9623e51
add empty str and empty bytes
TingDaoK Apr 22, 2024
d1dd6ad
reordering
TingDaoK Apr 22, 2024
f887b94
Merge branch 'cbor-main' of github.com:awslabs/aws-crt-python into cb…
TingDaoK Apr 22, 2024
f4a5730
add more supports
TingDaoK Apr 22, 2024
7a67099
clean up the impl
TingDaoK Apr 22, 2024
15cee91
adjust based on the change from C
TingDaoK Apr 24, 2024
78379e3
apply the latest change
TingDaoK Jun 19, 2024
e839c94
Merge branch 'main' into cbor-remove-py-impl
TingDaoK Jun 19, 2024
3cd840b
pick up the fix from C, and just ignore the whole vscode dir
TingDaoK Jun 19, 2024
9366b75
use tag
TingDaoK Jun 21, 2024
26fe591
Merge branch 'main' into cbor-remove-py-impl
TingDaoK Jan 10, 2025
9e74ae4
amazon Q is great
TingDaoK Jan 13, 2025
95b4ac3
add some doc
TingDaoK Jan 13, 2025
b036836
let's move to arm64 codebuild
TingDaoK Jan 13, 2025
70dd7df
actually the image matrix did explain it well enough
TingDaoK Jan 13, 2025
61bc058
Merge branch 'main' into cbor-remove-py-impl
TingDaoK Mar 5, 2025
f5c8732
WIP
TingDaoK May 1, 2025
b3a6c56
more
TingDaoK May 1, 2025
1c2faad
Merge branch 'main' into cbor-remove-py-impl
TingDaoK May 7, 2025
15b9bc5
submodules
TingDaoK May 7, 2025
13061c9
API and documentation
TingDaoK May 9, 2025
9498164
add fix and they updated the test case
TingDaoK May 9, 2025
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 1 addition & 6 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -386,12 +386,7 @@ poetry.toml
pyrightconfig.json

### VisualStudioCode ###
.vscode/*
!.vscode/settings.json
!.vscode/tasks.json
!.vscode/launch.json
!.vscode/extensions.json
!.vscode/*.code-snippets
.vscode

# Local History for Visual Studio Code
.history/
Expand Down
404 changes: 404 additions & 0 deletions awscrt/cbor.py

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion crt/aws-c-cal
775 changes: 775 additions & 0 deletions source/cbor.c

Large diffs are not rendered by default.

56 changes: 56 additions & 0 deletions source/cbor.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
#ifndef AWS_CRT_PYTHON_CBOR_H
#define AWS_CRT_PYTHON_CBOR_H
/**
* Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
* SPDX-License-Identifier: Apache-2.0.
*/
#include "module.h"

/*******************************************************************************
* ENCODE
******************************************************************************/

PyObject *aws_py_cbor_encoder_new(PyObject *self, PyObject *args);
PyObject *aws_py_cbor_encoder_get_encoded_data(PyObject *self, PyObject *args);

PyObject *aws_py_cbor_encoder_write_unsigned_int(PyObject *self, PyObject *args);
PyObject *aws_py_cbor_encoder_write_negative_int(PyObject *self, PyObject *args);
PyObject *aws_py_cbor_encoder_write_float(PyObject *self, PyObject *args);
PyObject *aws_py_cbor_encoder_write_bytes(PyObject *self, PyObject *args);
PyObject *aws_py_cbor_encoder_write_text(PyObject *self, PyObject *args);
PyObject *aws_py_cbor_encoder_write_array_start(PyObject *self, PyObject *args);
PyObject *aws_py_cbor_encoder_write_map_start(PyObject *self, PyObject *args);
PyObject *aws_py_cbor_encoder_write_tag(PyObject *self, PyObject *args);
PyObject *aws_py_cbor_encoder_write_bool(PyObject *self, PyObject *args);

/* Encode the types without value needed. The arg is the type to encode. */
PyObject *aws_py_cbor_encoder_write_simple_types(PyObject *self, PyObject *args);

PyObject *aws_py_cbor_encoder_write_py_list(PyObject *self, PyObject *args);
PyObject *aws_py_cbor_encoder_write_py_dict(PyObject *self, PyObject *args);
PyObject *aws_py_cbor_encoder_write_data_item(PyObject *self, PyObject *args);

/*******************************************************************************
* DECODE
******************************************************************************/

PyObject *aws_py_cbor_decoder_new(PyObject *self, PyObject *args);
PyObject *aws_py_cbor_decoder_peek_type(PyObject *self, PyObject *args);
PyObject *aws_py_cbor_decoder_get_remaining_bytes_len(PyObject *self, PyObject *args);
PyObject *aws_py_cbor_decoder_consume_next_element(PyObject *self, PyObject *args);
PyObject *aws_py_cbor_decoder_consume_next_data_item(PyObject *self, PyObject *args);
PyObject *aws_py_cbor_decoder_pop_next_unsigned_int(PyObject *self, PyObject *args);
PyObject *aws_py_cbor_decoder_pop_next_negative_int(PyObject *self, PyObject *args);
PyObject *aws_py_cbor_decoder_pop_next_float(PyObject *self, PyObject *args);
PyObject *aws_py_cbor_decoder_pop_next_bool(PyObject *self, PyObject *args);
PyObject *aws_py_cbor_decoder_pop_next_bytes(PyObject *self, PyObject *args);
PyObject *aws_py_cbor_decoder_pop_next_text(PyObject *self, PyObject *args);
PyObject *aws_py_cbor_decoder_pop_next_array_start(PyObject *self, PyObject *args);
PyObject *aws_py_cbor_decoder_pop_next_map_start(PyObject *self, PyObject *args);
PyObject *aws_py_cbor_decoder_pop_next_tag_val(PyObject *self, PyObject *args);

PyObject *aws_py_cbor_decoder_pop_next_py_list(PyObject *self, PyObject *args);
PyObject *aws_py_cbor_decoder_pop_next_py_dict(PyObject *self, PyObject *args);
PyObject *aws_py_cbor_decoder_pop_next_data_item(PyObject *self, PyObject *args);

#endif /* AWS_CRT_PYTHON_CBOR_H */
44 changes: 44 additions & 0 deletions source/module.c
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
#include "module.h"

#include "auth.h"
#include "cbor.h"
#include "checksums.h"
#include "common.h"
#include "crypto.h"
Expand Down Expand Up @@ -117,6 +118,14 @@ PyObject *PyUnicode_FromAwsString(const struct aws_string *aws_str) {
return PyUnicode_FromStringAndSize(aws_string_c_str(aws_str), aws_str->len);
}

PyObject *PyBytes_FromAwsByteCursor(const struct aws_byte_cursor *cursor) {
if (cursor->len > PY_SSIZE_T_MAX) {
PyErr_SetString(PyExc_OverflowError, "Cursor exceeds PY_SSIZE_T_MAX");
return NULL;
}
return PyBytes_FromStringAndSize((const char *)cursor->ptr, (Py_ssize_t)cursor->len);
}

uint32_t PyObject_GetAttrAsUint32(PyObject *o, const char *class_name, const char *attr_name) {
uint32_t result = UINT32_MAX;

Expand Down Expand Up @@ -897,6 +906,41 @@ static PyMethodDef s_module_methods[] = {
AWS_PY_METHOD_DEF(websocket_increment_read_window, METH_VARARGS),
AWS_PY_METHOD_DEF(websocket_create_handshake_request, METH_VARARGS),

/* CBOR Encode */
AWS_PY_METHOD_DEF(cbor_encoder_new, METH_VARARGS),
AWS_PY_METHOD_DEF(cbor_encoder_get_encoded_data, METH_VARARGS),
AWS_PY_METHOD_DEF(cbor_encoder_write_unsigned_int, METH_VARARGS),
AWS_PY_METHOD_DEF(cbor_encoder_write_negative_int, METH_VARARGS),
AWS_PY_METHOD_DEF(cbor_encoder_write_float, METH_VARARGS),
AWS_PY_METHOD_DEF(cbor_encoder_write_bytes, METH_VARARGS),
AWS_PY_METHOD_DEF(cbor_encoder_write_text, METH_VARARGS),
AWS_PY_METHOD_DEF(cbor_encoder_write_array_start, METH_VARARGS),
AWS_PY_METHOD_DEF(cbor_encoder_write_map_start, METH_VARARGS),
AWS_PY_METHOD_DEF(cbor_encoder_write_tag, METH_VARARGS),
AWS_PY_METHOD_DEF(cbor_encoder_write_bool, METH_VARARGS),
AWS_PY_METHOD_DEF(cbor_encoder_write_simple_types, METH_VARARGS),
AWS_PY_METHOD_DEF(cbor_encoder_write_py_list, METH_VARARGS),
AWS_PY_METHOD_DEF(cbor_encoder_write_py_dict, METH_VARARGS),
AWS_PY_METHOD_DEF(cbor_encoder_write_data_item, METH_VARARGS),

/* CBOR Decode */
AWS_PY_METHOD_DEF(cbor_decoder_new, METH_VARARGS),
AWS_PY_METHOD_DEF(cbor_decoder_peek_type, METH_VARARGS),
AWS_PY_METHOD_DEF(cbor_decoder_get_remaining_bytes_len, METH_VARARGS),
AWS_PY_METHOD_DEF(cbor_decoder_consume_next_element, METH_VARARGS),
AWS_PY_METHOD_DEF(cbor_decoder_consume_next_data_item, METH_VARARGS),
AWS_PY_METHOD_DEF(cbor_decoder_pop_next_unsigned_int, METH_VARARGS),
AWS_PY_METHOD_DEF(cbor_decoder_pop_next_negative_int, METH_VARARGS),
AWS_PY_METHOD_DEF(cbor_decoder_pop_next_float, METH_VARARGS),
AWS_PY_METHOD_DEF(cbor_decoder_pop_next_bool, METH_VARARGS),
AWS_PY_METHOD_DEF(cbor_decoder_pop_next_bytes, METH_VARARGS),
AWS_PY_METHOD_DEF(cbor_decoder_pop_next_text, METH_VARARGS),
AWS_PY_METHOD_DEF(cbor_decoder_pop_next_array_start, METH_VARARGS),
AWS_PY_METHOD_DEF(cbor_decoder_pop_next_map_start, METH_VARARGS),
AWS_PY_METHOD_DEF(cbor_decoder_pop_next_tag_val, METH_VARARGS),
AWS_PY_METHOD_DEF(cbor_decoder_pop_next_py_list, METH_VARARGS),
AWS_PY_METHOD_DEF(cbor_decoder_pop_next_py_dict, METH_VARARGS),
AWS_PY_METHOD_DEF(cbor_decoder_pop_next_data_item, METH_VARARGS),
{NULL, NULL, 0, NULL},
};

Expand Down
1 change: 1 addition & 0 deletions source/module.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ enum aws_crt_python_errors {
/* AWS Specific Helpers */
PyObject *PyUnicode_FromAwsByteCursor(const struct aws_byte_cursor *cursor);
PyObject *PyUnicode_FromAwsString(const struct aws_string *aws_str);
PyObject *PyBytes_FromAwsByteCursor(const struct aws_byte_cursor *cursor);

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