summaryrefslogtreecommitdiffhomepage
path: root/lib/struct.c
AgeCommit message (Collapse)Author
2023-08-23docs: add struct module documentationJo-Philipp Wich
Add full documentation coverage for the struct module by utilizing large parts of the Python struct module documentation for the format string description. Signed-off-by: Jo-Philipp Wich <jo@mein.io>
2023-05-27struct: remove state->lenFelix Fietkau
It is unused and I couldn't find any purpose for it Signed-off-by: Felix Fietkau <nbd@nbd.name>
2022-06-08struct: add optional offset argument to `unpack()`Jo-Philipp Wich
Extend the `unpack()` function to take an optional, second offset parameter which is useful to skip an initial portion of the input data without having to encode pad bytes into the format string. Signed-off-by: Jo-Philipp Wich <jo@mein.io>
2022-06-04struct: fix packing `*` format after other repeated formatsJo-Philipp Wich
When packing a format such as `!6C*`, the `*` format was not included into the result buffer due to improper tracking of the function argument offset. Solve this issue by taking field repetitions into account when tracking the argument offset while calculating the size of dynamic arguments. Signed-off-by: Jo-Philipp Wich <jo@mein.io>
2022-01-24struct: implement `*` format, fix invalid memory accessesJo-Philipp Wich
Implement a new `*` format which acts like `s` on unpack but accepts input records which are shorter than the specified length, e.g. the following call will yield "abc" while an equivalent "10s" format would fail: unpack("2*", "abc") // [ "ab" ] unpack("10*", "abc") // [ "abc" ] unpack("10s", "abc") // null The `*` format is primarily useful to extract the remainder of a variable length record without having to encode the specific length of the record directly into the format string. When packing records, the `*` format takes at most as many bytes as specified in the format string repeat count. If the input string is shorter than the given repeat count, only as many bytes as present in the input string are taken. A bare `*` without any repeat count will take all bytes from the given input string: pack("2*", "abc") // "ab" pack("10*", "abc") // "abc" pack("*", "abc") // "abc" pack("10s", "abc") // "abc\0\0\0\0\0\0\0" Additionally prevent invalid memory accesses when unpacking a buffer shorter than the length expected by the format string. Signed-off-by: Jo-Philipp Wich <jo@mein.io>
2022-01-04struct: reuse double packing routines from coreJo-Philipp Wich
Use uc_pack_double() and uc_unpack_double() from core to avoid unnecessary code duplication. Signed-off-by: Jo-Philipp Wich <jo@mein.io>
2021-11-01treewide: fix typo in exported function names and typesJo-Philipp Wich
Fix instances of misspelled "resource". This commit breaks the exported libucode ABI. Signed-off-by: Jo-Philipp Wich <jo@mein.io>
2021-11-01struct: fix PowerPC specific compiler pragma nameJo-Philipp Wich
The "align" pragma was accidentally renamed while refactoring the original module code. Fixes: 402f603 ("lib: introduce struct library") Signed-off-by: Jo-Philipp Wich <jo@mein.io>
2021-10-31lib: introduce struct libraryJo-Philipp Wich
Introduce a new "struct" library which is a port of the Python 3.10 struct module with a reduced set of API functions. It supports the same format patterns and conversions while providing the following methods: struct = require('struct'); buf = struct.pack("fmt", args...); values = struct.unpack("fmt", buf); struct_inst = struct.new("fmt"); buf = struct_inst.pack(args...); values = struct_inst.unpack(buf); Signed-off-by: Jo-Philipp Wich <jo@mein.io>