Add the framework needed for BEJ encoding
This change adds the framework needed for BEJ encoding. Inorder to keep
the patch smaller, I only added the basic framework needed to understand
how the encoding is done. Rest of the functionality and testing will
be added in the later patches.
Signed-off-by: Kasun Athukorala <kasunath@google.com>
Change-Id: If2bd315fe5a3e6c7afff2af1750434042517790b
diff --git a/src/bej_common.c b/src/bej_common.c
index 1d3be07..0e1cf10 100644
--- a/src/bej_common.c
+++ b/src/bej_common.c
@@ -23,3 +23,22 @@
// In nnint, first byte indicate how many bytes are there.
return *nnint + sizeof(uint8_t);
}
+
+uint8_t bejNnintEncodingSizeOfUInt(uint64_t val)
+{
+ uint8_t bytes = 0;
+ do
+ {
+ // Even if the value is 0, we need a byte for that.
+ ++bytes;
+ val = val >> 8;
+ } while (val != 0);
+ // Need 1 byte to add the nnint length.
+ return bytes + 1;
+}
+
+uint8_t bejNnintLengthFieldOfUInt(uint64_t val)
+{
+ // From the size of the encoded value, we need 1 byte for the length field.
+ return bejNnintEncodingSizeOfUInt(val) - 1;
+}