Fixed OER length encoding/decoding and added test
This commit is contained in:
@ -62,19 +62,19 @@ uint oer_get_length(OER_Stream *oer, uint *value) {
|
|||||||
|
|
||||||
if (len & 0x80) {
|
if (len & 0x80) {
|
||||||
len &= 0x7f;
|
len &= 0x7f;
|
||||||
size += len;
|
|
||||||
while (len--) {
|
while (len--) {
|
||||||
val <<= 8;
|
val <<= 8;
|
||||||
val |= *oer->ptr++;
|
val |= *oer->ptr++;
|
||||||
|
size++;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
val = len & 0x7f;
|
val = len & 0x7f;
|
||||||
|
|
||||||
if (value)
|
if (value)
|
||||||
*value = val;
|
*value = val;
|
||||||
oer->consumed += (size*8);
|
oer->consumed += (size*8);
|
||||||
return val;
|
return size;
|
||||||
}
|
}
|
||||||
|
|
||||||
uint64_t oer_get_value(OER_Stream *oer, uint size)
|
uint64_t oer_get_value(OER_Stream *oer, uint size)
|
||||||
|
|||||||
@ -57,7 +57,7 @@ uint oer_put_length(OER_Stream *oer, uint value)
|
|||||||
if (!oer || !oer->ptr)
|
if (!oer || !oer->ptr)
|
||||||
return 0;
|
return 0;
|
||||||
|
|
||||||
int size = 1;
|
uint size = 1;
|
||||||
if (value > 0x7f) {
|
if (value > 0x7f) {
|
||||||
*oer->ptr++ = 0x82;
|
*oer->ptr++ = 0x82;
|
||||||
*oer->ptr++ = value >> 8;
|
*oer->ptr++ = value >> 8;
|
||||||
|
|||||||
@ -78,7 +78,9 @@ uint oer_get_extension(OER_Stream *oer, OER_Extension *ext, uint noptions, uint
|
|||||||
if (ellipses) {
|
if (ellipses) {
|
||||||
ext->ellipses = (ext->value >> noptions) & 1;
|
ext->ellipses = (ext->value >> noptions) & 1;
|
||||||
|
|
||||||
ext->length = oer_get_length(oer, NULL);
|
uint length;
|
||||||
|
oer_get_length(oer, &length);
|
||||||
|
ext->length = length;
|
||||||
|
|
||||||
if (_asn1_debug_on_)
|
if (_asn1_debug_on_)
|
||||||
printf("\tEXT_LEN = %d\n", ext->length);
|
printf("\tEXT_LEN = %d\n", ext->length);
|
||||||
@ -100,8 +102,9 @@ uint oer_get_ext_length(OER_Stream *oer, OER_Extension *ext)
|
|||||||
if (!ext->ellipses)
|
if (!ext->ellipses)
|
||||||
return 0;
|
return 0;
|
||||||
|
|
||||||
uint len;
|
uint length;
|
||||||
ext->length = oer_get_length(oer, &len);
|
oer_get_length(oer, &length);
|
||||||
|
ext->length = length;
|
||||||
|
|
||||||
return ext->length;
|
return ext->length;
|
||||||
}
|
}
|
||||||
|
|||||||
Binary file not shown.
@ -210,6 +210,28 @@ void bitstream_test(void)
|
|||||||
printf("\tFAILED\n");
|
printf("\tFAILED\n");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void length_test(void)
|
||||||
|
{
|
||||||
|
memset(buffer, 0, sizeof(buffer));
|
||||||
|
|
||||||
|
OER_Stream len_enc, len_dec;
|
||||||
|
oer_init_stream(&len_enc, buffer, sizeof(buffer));
|
||||||
|
oer_init_stream(&len_dec, buffer, sizeof(buffer));
|
||||||
|
|
||||||
|
printf("Length test:\n");
|
||||||
|
int n = oer_put_length(&len_enc, 749);
|
||||||
|
buffer_print(&len_enc, buffer);
|
||||||
|
|
||||||
|
uint value = 0;
|
||||||
|
int x = oer_get_length(&len_dec, &value);
|
||||||
|
printf("\t%d -> %d\n", n, x);
|
||||||
|
|
||||||
|
if (n == x && value == 749)
|
||||||
|
printf("\tPASSED\n");
|
||||||
|
else
|
||||||
|
printf("\tFAILED\n");
|
||||||
|
}
|
||||||
|
|
||||||
int main(int argc, char **argv)
|
int main(int argc, char **argv)
|
||||||
{
|
{
|
||||||
asn1_set_debug(2);
|
asn1_set_debug(2);
|
||||||
@ -229,5 +251,8 @@ int main(int argc, char **argv)
|
|||||||
// bitstream test
|
// bitstream test
|
||||||
bitstream_test();
|
bitstream_test();
|
||||||
|
|
||||||
|
// length test
|
||||||
|
length_test();
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user