SIM-115: update simulator to use latest E2SM KPM version 3
[sim/e2-interface.git] / e2sim / asn1c / BIT_STRING_print.c
1 /*
2  * Copyright (c) 2017 Lev Walkin <vlm@lionet.info>.
3  * All rights reserved.
4  * Redistribution and modifications are permitted subject to BSD license.
5  */
6 #include <asn_internal.h>
7 #include <BIT_STRING.h>
8
9 /*
10  * BIT STRING specific contents printer.
11  */
12 int
13 BIT_STRING_print(const asn_TYPE_descriptor_t *td, const void *sptr, int ilevel,
14                  asn_app_consume_bytes_f *cb, void *app_key) {
15     const char * const h2c = "0123456789ABCDEF";
16     char scratch[64];
17     const BIT_STRING_t *st = (const BIT_STRING_t *)sptr;
18     uint8_t *buf;
19     uint8_t *end;
20     char *p = scratch;
21
22     (void)td;  /* Unused argument */
23
24     if(!st || !st->buf)
25         return (cb("<absent>", 8, app_key) < 0) ? -1 : 0;
26
27     ilevel++;
28     buf = st->buf;
29     end = buf + st->size;
30
31     /*
32      * Hexadecimal dump.
33      */
34     for(; buf < end; buf++) {
35         if((buf - st->buf) % 16 == 0 && (st->size > 16)
36                 && buf != st->buf) {
37             _i_INDENT(1);
38             /* Dump the string */
39             if(cb(scratch, p - scratch, app_key) < 0) return -1;
40             p = scratch;
41         }
42         *p++ = h2c[*buf >> 4];
43         *p++ = h2c[*buf & 0x0F];
44         *p++ = 0x20;
45     }
46
47     if(p > scratch) {
48         p--;  /* Eat the tailing space */
49
50         if((st->size > 16)) {
51             _i_INDENT(1);
52         }
53
54         /* Dump the incomplete 16-bytes row */
55         if(cb(scratch, p - scratch, app_key) < 0)
56             return -1;
57     }
58
59     if(st->bits_unused) {
60         int ret = snprintf(scratch, sizeof(scratch), " (%d bit%s unused)",
61                            st->bits_unused, st->bits_unused == 1 ? "" : "s");
62         assert(ret > 0 && ret < (ssize_t)sizeof(scratch));
63         if(ret > 0 && ret < (ssize_t)sizeof(scratch)
64            && cb(scratch, ret, app_key) < 0)
65             return -1;
66     }
67
68     return 0;
69 }