blob: 93584c8e012e24d4461a9fdfa174e2ebad969561 [file] [log] [blame]
Lev Walkinf15320b2004-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 */
Lev Walkind9bd7752004-06-05 08:17:50 +000026 0 /* No specifics */
Lev Walkinf15320b2004-06-03 03:38:44 +000027};
28
29/*
30 * BMPString specific contents printer.
31 */
32int
33BMPString_print(asn1_TYPE_descriptor_t *td, const void *sptr, int ilevel,
34 asn_app_consume_bytes_f *cb, void *app_key) {
35 const BMPString_t *st = sptr;
36 uint16_t *wchar;
37 uint16_t *wend;
38 char scratch[128]; /* Scratchpad buffer */
39 char *p;
40
Lev Walkind9bd7752004-06-05 08:17:50 +000041 (void)td; /* Unused argument */
42 (void)ilevel; /* Unused argument */
43
Lev Walkinf15320b2004-06-03 03:38:44 +000044 if(!st || !st->buf) return cb("<absent>", 8, app_key);
45
46 wchar = (uint16_t *)st->buf;
47 wend = (uint16_t *)(st->buf + st->size);
48 for(p = scratch; wchar < wend; wchar++) {
49 uint16_t wc = (((uint8_t *)wchar)[0] << 8)
50 | ((uint8_t *)wchar)[1]; /* 2 bytes */
51 if(sizeof(scratch) - (p - scratch) < 3) {
52 if(cb(scratch, p - scratch, app_key))
53 return -1;
54 p = scratch;
55 }
56 if(wc < 0x80) {
57 *p++ = (char)wc;
58 } else if(wc < 0x800) {
59 *p++ = 0xc0 | ((wc >> 6));
60 *p++ = 0x80 | ((wc & 0x3f));
61 } else {
62 *p++ = 0xe0 | ((wc >> 12));
63 *p++ = 0x80 | ((wc >> 6) & 0x3f);
64 *p++ = 0x80 | ((wc & 0x3f));
65 }
66 }
67
68 return cb(scratch, p - scratch, app_key);
69}