From 0fb0b055e54b8e045f72794c3a441ef8e859d9ad Mon Sep 17 00:00:00 2001 From: Robert Schmidt Date: Sat, 4 May 2024 11:43:11 +0200 Subject: [PATCH] NGAP: Correct encoding of AMF Set ID and Pointer Taking the example of the AMF Set ID, the previous version of the macro used another macro to encode the number, then marked the last 6 bits in the bit string (of length 16/2 bytes) as unused. This is wrong, because, assuming AMF Set ID=3, it basically truncates the number by encoding all 16 bits and cutting off the last 6, like this: ------------------- encode 16 bits 0000 0000 0000 0011 ------------ take these 10 bits So what remains is these bit positions of the number: (16,15,14,13,12,11,10,9,8,7) -- the last 6 bits are cut (and the first 6 are either 0 or garbage, because they don't exist). Instead, manually encode bits (10,9,8,7,6,5,4,3) in the first byte, and (2,1) in the second byte of the bit string, like so: ------------ encode 10 bits 0000 0000 1100 0000 ------------ take these 10 bits Do the same with the pointer. The AMF Region ID above is correct, as it is exactly 8 bits. --- openair3/UTILS/conversions.h | 26 +++++++++++++++----------- 1 file changed, 15 insertions(+), 11 deletions(-) diff --git a/openair3/UTILS/conversions.h b/openair3/UTILS/conversions.h index 2620d106a0..588edb1461 100644 --- a/openair3/UTILS/conversions.h +++ b/openair3/UTILS/conversions.h @@ -190,18 +190,22 @@ do { \ (aSN)->bits_unused = 0; \ } while(0) -#define AMF_SETID_TO_BIT_STRING(x, aSN) \ - do { \ - INT16_TO_OCTET_STRING(x, aSN); \ - (aSN)->bits_unused = 6; \ -} while(0) - -#define AMF_POINTER_TO_BIT_STRING(x, aSN) \ - do { \ - INT8_TO_OCTET_STRING(x, aSN); \ - (aSN)->bits_unused = 2; \ -} while(0) +#define AMF_SETID_TO_BIT_STRING(x, aSN) \ + do { \ + (aSN)->buf = calloc(2, sizeof(uint8_t)); \ + (aSN)->buf[0] = ((x) >> 2) & 0xff; \ + (aSN)->buf[1] = ((x) & 0x03) << 6; \ + (aSN)->size = 2; \ + (aSN)->bits_unused = 6; \ + } while (0) +#define AMF_POINTER_TO_BIT_STRING(x, aSN) \ + do { \ + (aSN)->buf = calloc(1, sizeof(uint8_t)); \ + (aSN)->buf[0] = ((x) & 0x3f) << 2; \ + (aSN)->size = 1; \ + (aSN)->bits_unused = 2; \ + } while (0) #define ENCRALG_TO_BIT_STRING(encralg, bitstring) \ do { \