#include #include #include #include #include #include enum encoding_type { HEX, BINARY, UTF8 }; static void check(enum encoding_type type, char *tagname, char *xmlbuf, char *verify) { int xmllen = strlen(xmlbuf); int verlen = verify ? strlen(verify) : 0; asn_TYPE_descriptor_t *td = &asn_DEF_OCTET_STRING; OCTET_STRING_t *st = 0; OCTET_STRING_t **stp = &st; asn_dec_rval_t rc; xer_type_decoder_f *decoder = 0; switch(type) { case HEX: decoder = OCTET_STRING_decode_xer_hex; break; case BINARY: td = &asn_DEF_BIT_STRING; decoder = OCTET_STRING_decode_xer_binary; break; case UTF8: decoder = OCTET_STRING_decode_xer_utf8; break; } rc = decoder(0, td, (void **)stp, tagname, xmlbuf, xmllen); printf("[%s] => [%s]:%d vs [%s]:%d, code %d\n", xmlbuf, st ? (const char *)st->buf : "", st ? st->size : 0, verify ? verify : "", verlen, rc.code); if(verify) { assert(rc.code == RC_OK); assert(st); assert(st->buf); assert(st->size == verlen); assert(!memcmp(st->buf, verify, verlen)); } else { assert(rc.code != RC_OK); } } static char buf[1024]; static int write_buf(const void *buffer, size_t size, void *key) { size_t *off = key; assert(*off + size < sizeof(buf)); memcpy(buf + *off, buffer, size); *off += size; return 0; } static void encode(char *orig, char *encoded) { OCTET_STRING_t os; size_t written = 0; asn_enc_rval_t er; memset(&os, 0, sizeof(os)); OCTET_STRING_fromString(&os, orig); er = OCTET_STRING_encode_xer_utf8(&asn_DEF_OCTET_STRING, &os, 0, 0, write_buf, &written); assert(er.encoded >= 0); buf[er.encoded] = '\0'; printf("Orig: [%s], encoded: [%s], check [%s]\n", orig, buf, encoded); assert(strcmp(buf, encoded) == 0); } int main() { check(HEX, 0, "41424", "AB@"); check(HEX, 0, "\n" "41424", "AB@"); check(HEX, 0, " 4 1 4 2 4 5 44 ", "ABED"); /* Some hard cases */ check(HEX, "z", "40", "@"); check(HEX, "z", "40", "@"); check(HEX, "z", ">40", 0); check(HEX, "z", "40", "@"); check(HEX, "z", ">40", 0); check(HEX, "z", "ignored40stuff", "@"); check(HEX, "tag", "4", "@"); check(HEX, "a-z", "7 375 73 6c6 96 b", "suslik"); /* This one has a comment in a not-yet-supported place */ /* check(HEX, "a-z", "73 75 73 6c 69 6b", "suslik"); */ check(BINARY, "tag", "", ""); check(BINARY, "tag", "blah", 0); check(BINARY, "tag", "01000001", "A"); check(BINARY, "tag", "01000 00 101 00001", "AB"); check(UTF8, 0, "one, two, three", "one, two, three"); check(UTF8, "z", "", ""); check(UTF8, "z", "<&>", "<&>"); check(UTF8, "z", "a<b&c>d", "ad"); check(UTF8, "z", "a<", "a<"); check(UTF8, "z", "a&sdfsdfsdf;b", "a&sdfsdfsdf;b"); check(UTF8, "z", "a b", "a b"); check(UTF8, "z", "a b", "a b"); check(UTF8, "z", "a繃b", "a\347\271\203b"); check(UTF8, "z", "a�b", "a�b"); check(UTF8, "z", "a�b", "a�b"); check(UTF8, "z", "", "aĬ"); check(UTF8, "z", "a&#-300;", "a&#-300;"); check(UTF8, "z", "ab", "a\014b"); check(UTF8, "z", "ab", "a\001b"); check(UTF8, "z", "a", "a\007"); encode("", ""); encode("a", "a"); encode("a\nb", "a\nb"); encode("a\bc", "ac"); encode("ab\01c\ndef\r\n", "abc\ndef\r\n"); return 0; }