SIM-115: update simulator to use latest E2SM KPM version 3
[sim/e2-interface.git] / e2sim / asn1c / OCTET_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 <OCTET_STRING.h>
8
9 int
10 OCTET_STRING_print(const asn_TYPE_descriptor_t *td, const void *sptr,
11                    int ilevel, asn_app_consume_bytes_f *cb, void *app_key) {
12     const char * const h2c = "0123456789ABCDEF";
13     const OCTET_STRING_t *st = (const OCTET_STRING_t *)sptr;
14     char scratch[16 * 3 + 4];
15     char *p = scratch;
16     uint8_t *buf;
17     uint8_t *end;
18     size_t i;
19
20     (void)td;  /* Unused argument */
21
22     if(!st || (!st->buf && st->size))
23         return (cb("<absent>", 8, app_key) < 0) ? -1 : 0;
24
25     /*
26      * Dump the contents of the buffer in hexadecimal.
27      */
28     buf = st->buf;
29     end = (buf == NULL)? NULL : buf + st->size;
30     for(i = 0; buf < end; buf++, i++) {
31         if(!(i % 16) && (i || st->size > 16)) {
32             if(cb(scratch, p - scratch, app_key) < 0)
33                 return -1;
34             _i_INDENT(1);
35             p = scratch;
36         }
37         *p++ = h2c[(*buf >> 4) & 0x0F];
38         *p++ = h2c[*buf & 0x0F];
39         *p++ = 0x20;
40     }
41
42     if(p > scratch) {
43         p--;  /* Remove the tail space */
44         if(cb(scratch, p - scratch, app_key) < 0)
45             return -1;
46     }
47
48     return 0;
49 }
50
51 int
52 OCTET_STRING_print_utf8(const asn_TYPE_descriptor_t *td, const void *sptr,
53                         int ilevel, asn_app_consume_bytes_f *cb,
54                         void *app_key) {
55     const OCTET_STRING_t *st = (const OCTET_STRING_t *)sptr;
56
57     (void)td;  /* Unused argument */
58     (void)ilevel;  /* Unused argument */
59
60     if(st && (st->buf || !st->size)) {
61         return (cb(st->buf, st->size, app_key) < 0) ? -1 : 0;
62     } else {
63         return (cb("<absent>", 8, app_key) < 0) ? -1 : 0;
64     }
65 }