| 1 |
#include <stdio.h> |
| 2 |
#include <string.h> |
| 3 |
#include <assert.h> |
| 4 |
|
| 5 |
#include <asn_internal.h> |
| 6 |
#include <ber_tlv_tag.h> |
| 7 |
|
| 8 |
struct tag_control_s { |
| 9 |
int taglen; |
| 10 |
uint8_t tagbuf[8]; |
| 11 |
|
| 12 |
int correctly_decodable; |
| 13 |
|
| 14 |
int tclass; |
| 15 |
ber_tlv_tag_t tvalue; |
| 16 |
int constr; |
| 17 |
} control[] = { |
| 18 |
{ 1, { 0x2 << 6 }, 1, ASN_TAG_CLASS_CONTEXT, 0, 0 }, |
| 19 |
{ 1, { 0x2 << 6 | 32 | 1 }, 1, ASN_TAG_CLASS_CONTEXT, 1, 1 }, |
| 20 |
{ 1, { 0x3 << 6 | 30 }, 1, ASN_TAG_CLASS_PRIVATE, 30, 0 }, |
| 21 |
{ 1, { 29 }, 1, ASN_TAG_CLASS_UNIVERSAL, 29, 0 }, |
| 22 |
{ 1, { 0xbf, 31 }, 0, ASN_TAG_CLASS_CONTEXT, 31, 1 }, |
| 23 |
{ 2, { 0xbf, 31 }, 1, ASN_TAG_CLASS_CONTEXT, 31, 1 }, |
| 24 |
{ 2, { 0xbf, 83 }, 1, ASN_TAG_CLASS_CONTEXT, 83, 1 }, |
| 25 |
{ 2, { 0xbf, 127 }, 1, ASN_TAG_CLASS_CONTEXT, 127, 1 }, |
| 26 |
{ 2, { 0xbf, 129 }, 0, ASN_TAG_CLASS_CONTEXT, 127, 1 }, |
| 27 |
{ 3, { 0xbf, 129, 0 }, 1, ASN_TAG_CLASS_CONTEXT, 128, 1 }, |
| 28 |
{ 3, { 0xbf, 129, 1 }, 1, ASN_TAG_CLASS_CONTEXT, 129, 1 }, |
| 29 |
{ 3, { 0xbf, 130, 0 }, 1, ASN_TAG_CLASS_CONTEXT, 256, 1 }, |
| 30 |
{ 3, { 0xbf, 130, 1 }, 1, ASN_TAG_CLASS_CONTEXT, 257, 1 }, |
| 31 |
{ 3, { 0xbf, 130, 0x81 }, 0, 0, 0, 0 }, |
| 32 |
{ 4, { 0xbf, 130, 0x81, 2 }, 1, ASN_TAG_CLASS_CONTEXT, 32898, 1 }, |
| 33 |
{ 4, { 0xbf, 130, 0x81, 0x82 }, 0, ASN_TAG_CLASS_CONTEXT, 32898, 1 }, |
| 34 |
{ 5, { 0x1f, 130, 0x81, 0x82, 1 }, 1, 0, 4210945, 0 }, |
| 35 |
{ 5, { 0x1f, 130, 0x81, 0x82, 2 }, 1, 0, 4210946, 0 }, |
| 36 |
{ 5, { 0x1f, 0xff, 0x81, 0x82, 2 }, 1, 0, 266354946, 0 }, |
| 37 |
{ 6, { 0x1f, 0xff, 0xff, 0x82, 0x80, 1 }, -1, 0, 266354946, 0 }, |
| 38 |
{ 7, { 0x1f, 0x8E, 0x87, 0xAA, 0x95, 0x99, 3}, -1, 0, 4000000000UL, 0 }, |
| 39 |
}; |
| 40 |
|
| 41 |
|
| 42 |
static void check_decode(struct tag_control_s *ctrl); |
| 43 |
static void check_encode(struct tag_control_s *ctrl); |
| 44 |
|
| 45 |
int |
| 46 |
main() { |
| 47 |
size_t i; |
| 48 |
|
| 49 |
for(i = 0; i < sizeof(control) / sizeof(control[0]); i++) { |
| 50 |
check_decode(&control[i]); |
| 51 |
check_encode(&control[i]); |
| 52 |
} |
| 53 |
|
| 54 |
return 0; |
| 55 |
} |
| 56 |
|
| 57 |
static void |
| 58 |
check_decode(struct tag_control_s *ctrl) { |
| 59 |
ber_tlv_tag_t tag = 123; |
| 60 |
ber_tlv_tag_t tag1 = 124; |
| 61 |
ber_tlv_tag_t tag2 = 125; |
| 62 |
ssize_t size; |
| 63 |
|
| 64 |
if(ctrl->correctly_decodable < 1) { |
| 65 |
size = ber_fetch_tag(ctrl->tagbuf, ctrl->taglen, &tag1); |
| 66 |
assert(size == ctrl->correctly_decodable); |
| 67 |
return; |
| 68 |
} |
| 69 |
|
| 70 |
printf("Expecting "); |
| 71 |
tag = (ctrl->tvalue << 2) | ctrl->tclass; |
| 72 |
ber_tlv_tag_fwrite(tag, stdout); |
| 73 |
printf(", got "); |
| 74 |
|
| 75 |
size = ber_fetch_tag(ctrl->tagbuf, 0, &tag1); |
| 76 |
assert(size == 0); |
| 77 |
|
| 78 |
size = ber_fetch_tag(ctrl->tagbuf, ctrl->taglen, &tag1); |
| 79 |
assert(size == ctrl->taglen); |
| 80 |
|
| 81 |
size = ber_fetch_tag(ctrl->tagbuf, ctrl->taglen + 10, &tag2); |
| 82 |
assert(size == ctrl->taglen); |
| 83 |
|
| 84 |
ber_tlv_tag_fwrite(tag1, stdout); |
| 85 |
printf("\n"); |
| 86 |
|
| 87 |
assert(tag1 == tag2); |
| 88 |
assert(tag == tag1); |
| 89 |
|
| 90 |
assert(ctrl->constr == BER_TLV_CONSTRUCTED(ctrl->tagbuf)); |
| 91 |
} |
| 92 |
|
| 93 |
|
| 94 |
|
| 95 |
static void |
| 96 |
check_encode(struct tag_control_s *ctrl) { |
| 97 |
uint8_t buf[16]; |
| 98 |
ber_tlv_tag_t tag; |
| 99 |
int Filler = 0xDA; |
| 100 |
ssize_t size; |
| 101 |
ssize_t i; |
| 102 |
|
| 103 |
tag = ctrl->tvalue << 2 | ctrl->tclass; |
| 104 |
|
| 105 |
|
| 106 |
|
| 107 |
|
| 108 |
for(i = 0; i < (int)sizeof(buf); i++) { |
| 109 |
int j; |
| 110 |
|
| 111 |
memset(buf, Filler, sizeof(buf)); |
| 112 |
|
| 113 |
size = ber_tlv_tag_serialize(tag, buf, i); |
| 114 |
assert(size < (int)sizeof(buf)); |
| 115 |
|
| 116 |
if(size <= i) { |
| 117 |
for(j = 0; j < size; j++) assert(buf[j] != Filler); |
| 118 |
} else { |
| 119 |
j = i; |
| 120 |
} |
| 121 |
for(; j < (int)sizeof(buf); j++) assert(buf[j] == Filler); |
| 122 |
} |
| 123 |
|
| 124 |
memset(buf, Filler, sizeof(buf)); |
| 125 |
|
| 126 |
size = ber_tlv_tag_serialize(tag, buf, sizeof(buf)); |
| 127 |
assert(size < (int)sizeof(buf)); |
| 128 |
|
| 129 |
for(i = 0; i < size; i++) assert(buf[i] != Filler); |
| 130 |
for(; i < (int)sizeof(buf); i++) assert(buf[i] == Filler); |
| 131 |
|
| 132 |
if(ctrl->correctly_decodable == 1) { |
| 133 |
assert(size == ctrl->taglen); |
| 134 |
} |
| 135 |
if(ctrl->constr) *buf |= 0x20; |
| 136 |
|
| 137 |
ber_tlv_tag_fwrite(tag, stdout); |
| 138 |
|
| 139 |
printf(":"); |
| 140 |
|
| 141 |
for(i = 0; i < size; i++) { |
| 142 |
printf(" %02x", buf[i]); |
| 143 |
if(ctrl->correctly_decodable == 1) { |
| 144 |
assert(ctrl->tagbuf[i] == buf[i]); |
| 145 |
} |
| 146 |
} |
| 147 |
printf("\n"); |
| 148 |
} |
| 149 |
|