diff options
Diffstat (limited to 'skeletons/BMPString.c')
-rw-r--r-- | skeletons/BMPString.c | 65 |
1 files changed, 65 insertions, 0 deletions
diff --git a/skeletons/BMPString.c b/skeletons/BMPString.c new file mode 100644 index 00000000..596974ea --- /dev/null +++ b/skeletons/BMPString.c @@ -0,0 +1,65 @@ +/*- + * Copyright (c) 2003 Lev Walkin <vlm@lionet.info>. All rights reserved. + * Redistribution and modifications are permitted subject to BSD license. + */ +#include <BMPString.h> + +/* + * BMPString basic type description. + */ +static ber_tlv_tag_t asn1_DEF_BMPString_tags[] = { + (ASN_TAG_CLASS_UNIVERSAL | (30 << 2)) +}; +asn1_TYPE_descriptor_t asn1_DEF_BMPString = { + "BMPString", + asn_generic_no_constraint, /* No constraint by default */ + OCTET_STRING_decode_ber, /* Implemented in terms of OCTET STRING */ + OCTET_STRING_encode_der, /* Implemented in terms of OCTET STRING */ + BMPString_print, + OCTET_STRING_free, /* -//- */ + 0, /* Use generic outmost tag fetcher */ + asn1_DEF_BMPString_tags, + sizeof(asn1_DEF_BMPString_tags) + / sizeof(asn1_DEF_BMPString_tags[0]), + 1, /* Single UNIVERSAL tag may be implicitly overriden */ + -1, /* Both ways are fine */ +}; + +/* + * BMPString specific contents printer. + */ +int +BMPString_print(asn1_TYPE_descriptor_t *td, const void *sptr, int ilevel, + asn_app_consume_bytes_f *cb, void *app_key) { + const BMPString_t *st = sptr; + uint16_t *wchar; + uint16_t *wend; + char scratch[128]; /* Scratchpad buffer */ + char *p; + + if(!st || !st->buf) return cb("<absent>", 8, app_key); + + wchar = (uint16_t *)st->buf; + wend = (uint16_t *)(st->buf + st->size); + for(p = scratch; wchar < wend; wchar++) { + uint16_t wc = (((uint8_t *)wchar)[0] << 8) + | ((uint8_t *)wchar)[1]; /* 2 bytes */ + if(sizeof(scratch) - (p - scratch) < 3) { + if(cb(scratch, p - scratch, app_key)) + return -1; + p = scratch; + } + if(wc < 0x80) { + *p++ = (char)wc; + } else if(wc < 0x800) { + *p++ = 0xc0 | ((wc >> 6)); + *p++ = 0x80 | ((wc & 0x3f)); + } else { + *p++ = 0xe0 | ((wc >> 12)); + *p++ = 0x80 | ((wc >> 6) & 0x3f); + *p++ = 0x80 | ((wc & 0x3f)); + } + } + + return cb(scratch, p - scratch, app_key); +} |