6 #include <OCTET_STRING.h>
7 #include <BIT_STRING.h>
9 enum encoding_type { HEX, BINARY, UTF8 };
11 #define check(t, tag, buf, verify) check_impl(__LINE__, t, tag, buf, verify)
14 check_impl(int lineno, enum encoding_type type, char *tagname, char *xmlbuf, char *verify) {
15 size_t xmllen = strlen(xmlbuf);
16 size_t verlen = verify ? strlen(verify) : 0;
17 asn_TYPE_descriptor_t *td = &asn_DEF_OCTET_STRING;
18 OCTET_STRING_t *st = 0;
19 OCTET_STRING_t **stp = &st;
21 xer_type_decoder_f *decoder = 0;
25 decoder = OCTET_STRING_decode_xer_hex;
28 td = &asn_DEF_BIT_STRING;
29 decoder = OCTET_STRING_decode_xer_binary;
32 decoder = OCTET_STRING_decode_xer_utf8;
36 rc = decoder(0, td, (void **)stp, tagname, xmlbuf, xmllen);
37 printf("%03d: [%s] => [%s]:%zu vs [%s]:%zu, code %d\n",
39 st ? (const char *)st->buf : "", st ? st->size : 0,
40 verify ? verify : "", verlen, rc.code);
43 assert(rc.code == RC_OK);
46 assert(st->size == verlen);
47 assert(!memcmp(st->buf, verify, verlen));
49 assert(rc.code != RC_OK);
52 ASN_STRUCT_FREE(*td, st);
55 static char buf[1024];
58 write_buf(const void *buffer, size_t size, void *key) {
60 assert(*off + size < sizeof(buf));
61 memcpy(buf + *off, buffer, size);
67 encode(char *orig, char *encoded) {
72 memset(&os, 0, sizeof(os));
74 OCTET_STRING_fromString(&os, orig);
76 er = OCTET_STRING_encode_xer_utf8(&asn_DEF_OCTET_STRING, &os,
77 0, 0, write_buf, &written);
78 assert(er.encoded >= 0);
79 buf[er.encoded] = '\0';
80 printf("Orig: [%s], encoded: [%s], check [%s]\n",
82 assert(strcmp(buf, encoded) == 0);
83 ASN_STRUCT_RESET(asn_DEF_OCTET_STRING, &os);
89 check(HEX, 0, "<OCTET_STRING>41424</OCTET_STRING>",
92 check(HEX, 0, "<!--comment--><OCTET_STRING>\n"
93 "<!--comment-->41424</OCTET_STRING>",
96 check(HEX, 0, "<OCTET_STRING blah blah> 4 1 4 2 4 5 44 </OCTET_STRING>",
100 check(HEX, "z", "<z><!-- < -->40</z>", "@");
101 check(HEX, "z", "<z><!-- <-->40</z>", "@");
102 check(HEX, "z", "<z><!-- -->>40</z>", 0);
103 check(HEX, "z", "<z><!-- <some <sometag>-->40</z>", "@");
104 check(HEX, "z", "<z><!-- <some <sometag-->>40</z>", 0);
106 check(HEX, "z", "ignored<z>40</z>stuff", "@");
108 check(HEX, "tag", "<tag>4</tag>", "@");
109 check(HEX, "a-z", "<a-z>7 375 73 6c6 9<!--/-->6 b</a-z>", "suslik");
111 /* This one has a comment in a not-yet-supported place */
112 /* check(HEX, "a-z", "<a-z>73 75 73 6c 6<!--/-->9 6b</a-z>",
115 check(BINARY, "tag", "<tag/>", "");
116 check(BINARY, "tag", "<tag>blah</tag>", 0);
117 check(BINARY, "tag", "<tag>01000001</tag>", "A");
118 check(BINARY, "tag", "<tag>01000<!--blah--> 00 101 00001</tag>", "AB");
120 check(UTF8, 0, "<OCTET_STRING>one, two, three</OCTET_STRING>",
123 check(UTF8, "z", "<z></z>", "");
124 check(UTF8, "z", "<z z z><&></z z z>", "<&>");
125 check(UTF8, "z", "<z z z>a<b&c>d</z z z>", "a<b&c>d");
126 check(UTF8, "z", "<z z z>a<</z z z>", "a<");
127 check(UTF8, "z", "<z z z>a&sdfsdfsdf;b</z z z>", "a&sdfsdfsdf;b");
128 check(UTF8, "z", "<z z z>a b</z z z>", "a b");
129 check(UTF8, "z", "<z z z>a b</z z z>", "a b");
130 check(UTF8, "z", "<z>a繃b</z>", "a\347\271\203b");
131 check(UTF8, "z", "<z>a俄|</z>", "a\xe4\xbf\x84|");
132 /* Last unicode point */
133 check(UTF8, "z", "<z>a|</z>", "a\xf4\x8f\xbf\xbf|");
134 check(UTF8, "z", "<z>a|</z>", "a\xf4\x8f\xbf\xbf|");
135 /* One past the last unicode point */
136 check(UTF8, "z", "<z>a�|</z>", "a�|");
137 check(UTF8, "z", "<z>a�|</z>", "a�|");
138 check(UTF8, "z", "<z>a�b</z>", "a�b");
139 check(UTF8, "z", "<z>a�b</z>", "a�b");
140 check(UTF8, "z", "<z>aĬ</z>", "aĬ");
141 check(UTF8, "z", "<z>a&#-300;</z>", "a&#-300;");
142 check(UTF8, "z", "<z>a<ff/>b</z>", "a\014b");
143 check(UTF8, "z", "<z>a<soh/>b</z>", "a\001b");
144 check(UTF8, "z", "<z>a<bel/></z>", "a\007");
148 encode("a\nb", "a\nb");
149 encode("a\bc", "a<bs/>c");
150 encode("ab\01c\ndef\r\n", "ab<soh/>c\ndef\r\n");