From f195b2cd798c08096110d33b2aa582bc2c17366e Mon Sep 17 00:00:00 2001 From: v0-e Date: Mon, 24 Jul 2023 01:38:05 +0100 Subject: [PATCH 1/2] JER BIT STRING quoted hex --- skeletons/BIT_STRING_jer.c | 26 ++++++++++---------------- 1 file changed, 10 insertions(+), 16 deletions(-) diff --git a/skeletons/BIT_STRING_jer.c b/skeletons/BIT_STRING_jer.c index 69c320d2f..73765e360 100644 --- a/skeletons/BIT_STRING_jer.c +++ b/skeletons/BIT_STRING_jer.c @@ -6,11 +6,6 @@ #include #include -static const char *_bit_pattern[16] = { - "0000", "0001", "0010", "0011", "0100", "0101", "0110", "0111", - "1000", "1001", "1010", "1011", "1100", "1101", "1110", "1111" -}; - asn_enc_rval_t BIT_STRING_encode_jer(const asn_TYPE_descriptor_t *td, const void *sptr, int ilevel, enum jer_encoder_flags_e flags, @@ -18,9 +13,9 @@ BIT_STRING_encode_jer(const asn_TYPE_descriptor_t *td, const void *sptr, asn_enc_rval_t er = {0, 0, 0}; char scratch[128]; char *p = scratch; - char *scend = scratch + (sizeof(scratch) - 10); + char *scend = scratch + (sizeof(scratch) - 4); const BIT_STRING_t *st = (const BIT_STRING_t *)sptr; - int xcan = 0; + int xcan = 1; uint8_t *buf; uint8_t *end; @@ -35,6 +30,7 @@ BIT_STRING_encode_jer(const asn_TYPE_descriptor_t *td, const void *sptr, /* * Binary dump */ + *p++ = '"'; for(; buf < end; buf++) { int v = *buf; int nline = xcan?0:(((buf - st->buf) % 8) == 0); @@ -43,9 +39,7 @@ BIT_STRING_encode_jer(const asn_TYPE_descriptor_t *td, const void *sptr, p = scratch; if(nline) ASN__TEXT_INDENT(1, ilevel); } - memcpy(p + 0, _bit_pattern[v >> 4], 4); - memcpy(p + 4, _bit_pattern[v & 0x0f], 4); - p += 8; + p += sprintf(p, "%02x", v); } if(!xcan && ((buf - st->buf) % 8) == 0) @@ -54,15 +48,15 @@ BIT_STRING_encode_jer(const asn_TYPE_descriptor_t *td, const void *sptr, p = scratch; if(buf == end) { - int v = *buf; + uint8_t v = *buf; int ubits = st->bits_unused; - int i; - for(i = 7; i >= ubits; i--) - *p++ = (v & (1 << i)) ? 0x31 : 0x30; + p += sprintf(p, "%02x", v & (0xff << ubits)); ASN__CALLBACK(scratch, p - scratch); + p = scratch; } - - if(!xcan) ASN__TEXT_INDENT(1, ilevel - 1); + *p++ = '"'; + ASN__CALLBACK(scratch, p - scratch); + ASN__TEXT_INDENT(1, ilevel - 1); ASN__ENCODED_OK(er); cb_failed: From d866a05915868963f84591e319ad0ad13ddc9ef8 Mon Sep 17 00:00:00 2001 From: v0-e Date: Thu, 7 Sep 2023 10:20:58 +0100 Subject: [PATCH 2/2] BIT STRING JER hex encoding from sprintf to lookup --- skeletons/BIT_STRING_jer.c | 26 +++++++++++--------------- 1 file changed, 11 insertions(+), 15 deletions(-) diff --git a/skeletons/BIT_STRING_jer.c b/skeletons/BIT_STRING_jer.c index 73765e360..f66238dee 100644 --- a/skeletons/BIT_STRING_jer.c +++ b/skeletons/BIT_STRING_jer.c @@ -11,11 +11,10 @@ BIT_STRING_encode_jer(const asn_TYPE_descriptor_t *td, const void *sptr, int ilevel, enum jer_encoder_flags_e flags, asn_app_consume_bytes_f *cb, void *app_key) { asn_enc_rval_t er = {0, 0, 0}; - char scratch[128]; + const char * const h2c = "0123456789ABCDEF"; + char scratch[16 * 3 + 4]; char *p = scratch; - char *scend = scratch + (sizeof(scratch) - 4); const BIT_STRING_t *st = (const BIT_STRING_t *)sptr; - int xcan = 1; uint8_t *buf; uint8_t *end; @@ -28,29 +27,26 @@ BIT_STRING_encode_jer(const asn_TYPE_descriptor_t *td, const void *sptr, end = buf + st->size - 1; /* Last byte is special */ /* - * Binary dump + * Hex dump */ *p++ = '"'; - for(; buf < end; buf++) { - int v = *buf; - int nline = xcan?0:(((buf - st->buf) % 8) == 0); - if(p >= scend || nline) { - ASN__CALLBACK(scratch, p - scratch); + for(int i = 0; buf < end; buf++, i++) { + if(!(i % 16) && (i || st->size > 16)) { + ASN__CALLBACK(scratch, p-scratch); p = scratch; - if(nline) ASN__TEXT_INDENT(1, ilevel); } - p += sprintf(p, "%02x", v); + *p++ = h2c[*buf >> 4]; + *p++ = h2c[*buf & 0x0F]; } - if(!xcan && ((buf - st->buf) % 8) == 0) - ASN__TEXT_INDENT(1, ilevel); ASN__CALLBACK(scratch, p - scratch); p = scratch; if(buf == end) { - uint8_t v = *buf; int ubits = st->bits_unused; - p += sprintf(p, "%02x", v & (0xff << ubits)); + uint8_t v = *buf & (0xff << ubits); + *p++ = h2c[v >> 4]; + *p++ = h2c[v & 0x0F]; ASN__CALLBACK(scratch, p - scratch); p = scratch; }