blob: 596974eafe09f7c7da090df70d1fb3545979543d [file] [log] [blame]
vlmfa67ddc2004-06-03 03:38:44 +00001/*-
2 * Copyright (c) 2003 Lev Walkin <vlm@lionet.info>. All rights reserved.
3 * Redistribution and modifications are permitted subject to BSD license.
4 */
5#include <BMPString.h>
6
7/*
8 * BMPString basic type description.
9 */
10static ber_tlv_tag_t asn1_DEF_BMPString_tags[] = {
11 (ASN_TAG_CLASS_UNIVERSAL | (30 << 2))
12};
13asn1_TYPE_descriptor_t asn1_DEF_BMPString = {
14 "BMPString",
15 asn_generic_no_constraint, /* No constraint by default */
16 OCTET_STRING_decode_ber, /* Implemented in terms of OCTET STRING */
17 OCTET_STRING_encode_der, /* Implemented in terms of OCTET STRING */
18 BMPString_print,
19 OCTET_STRING_free, /* -//- */
20 0, /* Use generic outmost tag fetcher */
21 asn1_DEF_BMPString_tags,
22 sizeof(asn1_DEF_BMPString_tags)
23 / sizeof(asn1_DEF_BMPString_tags[0]),
24 1, /* Single UNIVERSAL tag may be implicitly overriden */
25 -1, /* Both ways are fine */
26};
27
28/*
29 * BMPString specific contents printer.
30 */
31int
32BMPString_print(asn1_TYPE_descriptor_t *td, const void *sptr, int ilevel,
33 asn_app_consume_bytes_f *cb, void *app_key) {
34 const BMPString_t *st = sptr;
35 uint16_t *wchar;
36 uint16_t *wend;
37 char scratch[128]; /* Scratchpad buffer */
38 char *p;
39
40 if(!st || !st->buf) return cb("<absent>", 8, app_key);
41
42 wchar = (uint16_t *)st->buf;
43 wend = (uint16_t *)(st->buf + st->size);
44 for(p = scratch; wchar < wend; wchar++) {
45 uint16_t wc = (((uint8_t *)wchar)[0] << 8)
46 | ((uint8_t *)wchar)[1]; /* 2 bytes */
47 if(sizeof(scratch) - (p - scratch) < 3) {
48 if(cb(scratch, p - scratch, app_key))
49 return -1;
50 p = scratch;
51 }
52 if(wc < 0x80) {
53 *p++ = (char)wc;
54 } else if(wc < 0x800) {
55 *p++ = 0xc0 | ((wc >> 6));
56 *p++ = 0x80 | ((wc & 0x3f));
57 } else {
58 *p++ = 0xe0 | ((wc >> 12));
59 *p++ = 0x80 | ((wc >> 6) & 0x3f);
60 *p++ = 0x80 | ((wc & 0x3f));
61 }
62 }
63
64 return cb(scratch, p - scratch, app_key);
65}