Fixed OER length encoding/decoding and added test
This commit is contained in:
@ -62,10 +62,10 @@ uint oer_get_length(OER_Stream *oer, uint *value) {
|
||||
|
||||
if (len & 0x80) {
|
||||
len &= 0x7f;
|
||||
size += len;
|
||||
while (len--) {
|
||||
val <<= 8;
|
||||
val |= *oer->ptr++;
|
||||
size++;
|
||||
}
|
||||
}
|
||||
else
|
||||
@ -74,7 +74,7 @@ uint oer_get_length(OER_Stream *oer, uint *value) {
|
||||
if (value)
|
||||
*value = val;
|
||||
oer->consumed += (size*8);
|
||||
return val;
|
||||
return 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)
|
||||
return 0;
|
||||
|
||||
int size = 1;
|
||||
uint size = 1;
|
||||
if (value > 0x7f) {
|
||||
*oer->ptr++ = 0x82;
|
||||
*oer->ptr++ = value >> 8;
|
||||
|
||||
@ -78,7 +78,9 @@ uint oer_get_extension(OER_Stream *oer, OER_Extension *ext, uint noptions, uint
|
||||
if (ellipses) {
|
||||
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_)
|
||||
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)
|
||||
return 0;
|
||||
|
||||
uint len;
|
||||
ext->length = oer_get_length(oer, &len);
|
||||
uint length;
|
||||
oer_get_length(oer, &length);
|
||||
ext->length = length;
|
||||
|
||||
return ext->length;
|
||||
}
|
||||
|
||||
Binary file not shown.
@ -210,6 +210,28 @@ void bitstream_test(void)
|
||||
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)
|
||||
{
|
||||
asn1_set_debug(2);
|
||||
@ -229,5 +251,8 @@ int main(int argc, char **argv)
|
||||
// bitstream test
|
||||
bitstream_test();
|
||||
|
||||
// length test
|
||||
length_test();
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user