SIM-115: update simulator to use latest E2SM KPM version 3
[sim/e2-interface.git] / e2sim / asn1c / BIT_STRING_jer.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 static const char *_bit_pattern[16] = {
10     "0000", "0001", "0010", "0011", "0100", "0101", "0110", "0111",
11     "1000", "1001", "1010", "1011", "1100", "1101", "1110", "1111"
12 };
13
14 asn_enc_rval_t
15 BIT_STRING_encode_jer(const asn_TYPE_descriptor_t *td, const void *sptr,
16                       int ilevel, enum jer_encoder_flags_e flags,
17                       asn_app_consume_bytes_f *cb, void *app_key) {
18     asn_enc_rval_t er = {0, 0, 0};
19     char scratch[128];
20     char *p = scratch;
21     char *scend = scratch + (sizeof(scratch) - 10);
22     const BIT_STRING_t *st = (const BIT_STRING_t *)sptr;
23     int xcan = 0;
24     uint8_t *buf;
25     uint8_t *end;
26
27     if(!st || !st->buf)
28         ASN__ENCODE_FAILED;
29
30     er.encoded = 0;
31
32     buf = st->buf;
33     end = buf + st->size - 1;  /* Last byte is special */
34
35     /*
36      * Binary dump
37      */
38     for(; buf < end; buf++) {
39         int v = *buf;
40         int nline = xcan?0:(((buf - st->buf) % 8) == 0);
41         if(p >= scend || nline) {
42             ASN__CALLBACK(scratch, p - scratch);
43             p = scratch;
44             if(nline) ASN__TEXT_INDENT(1, ilevel);
45         }
46         memcpy(p + 0, _bit_pattern[v >> 4], 4);
47         memcpy(p + 4, _bit_pattern[v & 0x0f], 4);
48         p += 8;
49     }
50
51     if(!xcan && ((buf - st->buf) % 8) == 0)
52         ASN__TEXT_INDENT(1, ilevel);
53     ASN__CALLBACK(scratch, p - scratch);
54     p = scratch;
55
56     if(buf == end) {
57         int v = *buf;
58         int ubits = st->bits_unused;
59         int i;
60         for(i = 7; i >= ubits; i--)
61             *p++ = (v & (1 << i)) ? 0x31 : 0x30;
62         ASN__CALLBACK(scratch, p - scratch);
63     }
64
65     if(!xcan) ASN__TEXT_INDENT(1, ilevel - 1);
66
67     ASN__ENCODED_OK(er);
68 cb_failed:
69     ASN__ENCODE_FAILED;
70 }