Moving in e2sim originally from it/test/simulators
[sim/e2-interface.git] / e2sim / ASN1c / ber_tlv_tag.c
1 /*****************************************************************************
2 #                                                                            *
3 # Copyright 2019 AT&T Intellectual Property                                  *
4 #                                                                            *
5 # Licensed under the Apache License, Version 2.0 (the "License");            *
6 # you may not use this file except in compliance with the License.           *
7 # You may obtain a copy of the License at                                    *
8 #                                                                            *
9 #      http://www.apache.org/licenses/LICENSE-2.0                            *
10 #                                                                            *
11 # Unless required by applicable law or agreed to in writing, software        *
12 # distributed under the License is distributed on an "AS IS" BASIS,          *
13 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.   *
14 # See the License for the specific language governing permissions and        *
15 # limitations under the License.                                             *
16 #                                                                            *
17 ******************************************************************************/
18
19 /*-
20  * Copyright (c) 2003, 2004 Lev Walkin <vlm@lionet.info>. All rights reserved.
21  * Redistribution and modifications are permitted subject to BSD license.
22  */
23 #include <asn_internal.h>
24 #include <ber_tlv_tag.h>
25 #include <errno.h>
26
27 ssize_t
28 ber_fetch_tag(const void *ptr, size_t size, ber_tlv_tag_t *tag_r) {
29         ber_tlv_tag_t val;
30         ber_tlv_tag_t tclass;
31         size_t skipped;
32
33         if(size == 0)
34                 return 0;
35
36         val = *(const uint8_t *)ptr;
37         tclass = (val >> 6);
38         if((val &= 0x1F) != 0x1F) {
39                 /*
40                  * Simple form: everything encoded in a single octet.
41                  * Tag Class is encoded using two least significant bits.
42                  */
43                 *tag_r = (val << 2) | tclass;
44                 return 1;
45         }
46
47         /*
48          * Each octet contains 7 bits of useful information.
49          * The MSB is 0 if it is the last octet of the tag.
50          */
51         for(val = 0, ptr = ((const char *)ptr) + 1, skipped = 2;
52                         skipped <= size;
53                                 ptr = ((const char *)ptr) + 1, skipped++) {
54                 unsigned int oct = *(const uint8_t *)ptr;
55                 if(oct & 0x80) {
56                         val = (val << 7) | (oct & 0x7F);
57                         /*
58                          * Make sure there are at least 9 bits spare
59                          * at the MS side of a value.
60                          */
61                         if(val >> ((8 * sizeof(val)) - 9)) {
62                                 /*
63                                  * We would not be able to accomodate
64                                  * any more tag bits.
65                                  */
66                                 return -1;
67                         }
68                 } else {
69                         val = (val << 7) | oct;
70                         *tag_r = (val << 2) | tclass;
71                         return skipped;
72                 }
73         }
74
75         return 0;       /* Want more */
76 }
77
78
79 ssize_t
80 ber_tlv_tag_fwrite(ber_tlv_tag_t tag, FILE *f) {
81         char buf[sizeof("[APPLICATION ]") + 32];
82         ssize_t ret;
83
84         ret = ber_tlv_tag_snprint(tag, buf, sizeof(buf));
85         if(ret >= (ssize_t)sizeof(buf) || ret < 2) {
86                 errno = EPERM;
87                 return -1;
88         }
89
90         return fwrite(buf, 1, ret, f);
91 }
92
93 ssize_t
94 ber_tlv_tag_snprint(ber_tlv_tag_t tag, char *buf, size_t size) {
95         const char *type = 0;
96         int ret;
97
98         switch(tag & 0x3) {
99         case ASN_TAG_CLASS_UNIVERSAL:   type = "UNIVERSAL ";    break;
100         case ASN_TAG_CLASS_APPLICATION: type = "APPLICATION ";  break;
101         case ASN_TAG_CLASS_CONTEXT:     type = "";              break;
102         case ASN_TAG_CLASS_PRIVATE:     type = "PRIVATE ";      break;
103         }
104
105         ret = snprintf(buf, size, "[%s%u]", type, ((unsigned)tag) >> 2);
106         if(ret <= 0 && size) buf[0] = '\0';     /* against broken libc's */
107
108         return ret;
109 }
110
111 char *
112 ber_tlv_tag_string(ber_tlv_tag_t tag) {
113         static char buf[sizeof("[APPLICATION ]") + 32];
114
115         (void)ber_tlv_tag_snprint(tag, buf, sizeof(buf));
116
117         return buf;
118 }
119
120
121 size_t
122 ber_tlv_tag_serialize(ber_tlv_tag_t tag, void *bufp, size_t size) {
123         int tclass = BER_TAG_CLASS(tag);
124         ber_tlv_tag_t tval = BER_TAG_VALUE(tag);
125         uint8_t *buf = (uint8_t *)bufp;
126         uint8_t *end;
127         size_t required_size;
128         size_t i;
129
130         if(tval <= 30) {
131                 /* Encoded in 1 octet */
132                 if(size) buf[0] = (tclass << 6) | tval;
133                 return 1;
134         } else if(size) {
135                 *buf++ = (tclass << 6) | 0x1F;
136                 size--;
137         }
138
139         /*
140          * Compute the size of the subsequent bytes.
141          */
142         for(required_size = 1, i = 7; i < 8 * sizeof(tval); i += 7) {
143                 if(tval >> i)
144                         required_size++;
145                 else
146                         break;
147         }
148
149         if(size < required_size)
150                 return required_size + 1;
151
152         /*
153          * Fill in the buffer, space permitting.
154          */
155         end = buf + required_size - 1;
156         for(i -= 7; buf < end; i -= 7, buf++)
157                 *buf = 0x80 | ((tval >> i) & 0x7F);
158         *buf = (tval & 0x7F);   /* Last octet without high bit */
159
160         return required_size + 1;
161 }
162