Debian Codegraph @

addons/ooh323c/src/perutil.c

raw symbols SCIP
1 /*
2 * Copyright (C) 1997-2005 by Objective Systems, Inc.
3 *
4 * This software is furnished under an open source license and may be
5 * used and copied only in accordance with the terms of this license.
6 * The text of the license may generally be found in the root
7 * directory of this installation in the COPYING file. It
8 * can also be viewed online at the following URL:
9 *
10 * http://www.obj-sys.com/open/license.html
11 *
12 * Any redistributions of this file including modified versions must
13 * maintain this copyright notice.
14 *
15 *****************************************************************************/
16
17 #include "asterisk.h"
18 #include "asterisk/lock.h"
19
20 #include "ooasn1.h"
21 #include "ooper.h"
22
23 ASN1BOOL isExtendableSize (Asn1SizeCnst* pSizeList);
24 static ASN1BOOL isFixedSize (Asn1SizeCnst* pSizeList);
25
26 ASN1BOOL alignCharStr
27 (OOCTXT* pctxt, ASN1UINT len, ASN1UINT nbits, Asn1SizeCnst* pSize)
28 {
29 if (TRUE) {
30 ASN1UINT lower, upper;
31 ASN1BOOL doAlign = (len > 0), extendable;
32
33 pSize = checkSize (pSize, len, &extendable);
34
35 if (0 != pSize) {
36 lower = pSize->lower;
37 upper = pSize->upper;
38 }
39 else {
40 lower = 0;
41 upper = ASN1UINT_MAX;
42 }
43
44 if (!extendable && upper < 65536) {
45 ASN1UINT bitRange = upper * nbits;
46 if (upper == lower) {
47 /* X.691, clause 26.5.6 */
48 if (bitRange <= 16) doAlign = FALSE;
49 }
50 else {
51 /* X.691, clause 26.5.7 */
52 if (bitRange < 16) doAlign = FALSE;
53 }
54 }
55
56 return doAlign;
57 }
58 else
59 return FALSE;
60 }
61
62 int bitAndOctetStringAlignmentTest (Asn1SizeCnst* pSizeList,
63 ASN1UINT itemCount,
64 ASN1BOOL bitStrFlag,
65 ASN1BOOL* pAlignFlag)
66 {
67 ASN1UINT threshold = (bitStrFlag) ? 16 : 2;
68
69 if (pSizeList == 0 || itemCount > threshold)
70 *pAlignFlag = TRUE;
71 else if (isFixedSize(pSizeList))
72 *pAlignFlag = FALSE;
73 else {
74
75 /* Variable length case: check size.. no alignment required if */
76 /* lower == upper and not extended.. */
77
78 ASN1BOOL extended;
79 Asn1SizeCnst* pSize = checkSize (pSizeList, itemCount, &extended);
80
81 if (pSize != 0)
82 *pAlignFlag = ((pSize->upper != pSize->lower) || pSize->extended);
83 else {
84 /* Note: we never should get here because constraint */
85 /* violation should have been caught when length was encoded */
86 /* or decoded.. */
87 return (ASN_E_CONSVIO);
88 }
89 }
90
91 return (ASN_OK);
92 }
93
94 Asn1SizeCnst* checkSize (Asn1SizeCnst* pSizeList,
95 ASN1UINT value,
96 ASN1BOOL* pExtendable)
97 {
98 Asn1SizeCnst* lpSize = pSizeList;
99 *pExtendable = isExtendableSize (lpSize);
100
101 while (lpSize) {
102 if (value >= lpSize->lower && value <= lpSize->upper) {
103 return (lpSize);
104 }
105 else lpSize = lpSize->next;
106 }
107
108 return 0;
109 }
110
111 int getPERMsgLen (OOCTXT* pctxt)
112 {
113 return (pctxt->buffer.bitOffset == 8) ?
114 pctxt->buffer.byteIndex : pctxt->buffer.byteIndex + 1;
115 }
116
117 int addSizeConstraint (OOCTXT* pctxt, Asn1SizeCnst* pSize)
118 {
119 Asn1SizeCnst* lpSize;
120 int stat = ASN_OK;
121
122 /* If constraint does not already exist, add it */
123
124 if (!pctxt->pSizeConstraint) {
125 pctxt->pSizeConstraint = pSize;
126 }
127
128 /* Otherwise, check to make sure given constraint is larger than */
129 /* the existing constraint.. */
130
131 else {
132 lpSize = pSize;
133 while (lpSize) {
134 if (pctxt->pSizeConstraint->lower <= lpSize->lower ||
135 pctxt->pSizeConstraint->upper >= lpSize->upper)
136 {
137 /* Set the extension flag to the value of the size */
138 /* constraint structure that the item falls within.. */
139
140 /* pctxt->pSizeConstraint->extended = lpSize->extended; */
141
142 break;
143 }
144 lpSize = lpSize->next;
145 }
146
147 if (!lpSize)
148 stat = ASN_E_CONSVIO;
149 }
150
151 return stat;
152 }
153
154 Asn1SizeCnst* getSizeConstraint (OOCTXT* pctxt, ASN1BOOL extbit)
155 {
156 Asn1SizeCnst* lpSize = pctxt->pSizeConstraint;
157
158 while (lpSize) {
159 if (lpSize->extended == extbit)
160 return lpSize;
161 else
162 lpSize = lpSize->next;
163 }
164
165 return NULL;
166 }
167
168 int checkSizeConstraint(OOCTXT* pctxt, int size)
169 {
170 Asn1SizeCnst* pSize;
171 ASN1UINT upper;
172 ASN1BOOL extbit;
173 int stat;
174
175 /* If size constraint is present and extendable, decode extension */
176 /* bit.. */
177
178 if (isExtendableSize(pctxt->pSizeConstraint)) {
179 stat = DE_BIT (pctxt, &extbit);
180 if (stat != ASN_OK) return LOG_ASN1ERR (pctxt, stat);
181 }
182 else extbit = 0;
183
184 /* Now use the value of the extension bit to select the proper */
185 /* size constraint range specification.. */
186
187 pSize = getSizeConstraint (pctxt, extbit);
188
189 upper = (pSize) ? pSize->upper : ASN1UINT_MAX;
190
191 if (upper < (ASN1UINT)size) {
192 return LOG_ASN1ERR (pctxt, ASN_E_CONSVIO);
193 }
194
195 return ASN_OK;
196 }
197
198 ASN1UINT getUIntBitCount (ASN1UINT value)
199 {
200 /* Binary search - decision tree (5 tests, rarely 6) */
201 return
202 ((value < 1<<15) ?
203 ((value < 1<<7) ?
204 ((value < 1<<3) ?
205 ((value < 1<<1) ? ((value < 1<<0) ? 0 : 1) :
206 ((value < 1<<2) ? 2 : 3)) :
207 ((value < 1<<5) ? ((value < 1<<4) ? 4 : 5) :
208 ((value < 1<<6) ? 6 : 7))) :
209 ((value < 1<<11) ?
210 ((value < 1<<9) ? ((value < 1<<8) ? 8 : 9) :
211 ((value < 1<<10) ? 10 : 11)) :
212 ((value < 1<<13) ? ((value < 1<<12) ? 12 : 13) :
213 ((value < 1<<14) ? 14 : 15)))) :
214 ((value < 1<<23) ?
215 ((value < 1<<19) ?
216 ((value < 1<<17) ? ((value < 1<<16) ? 16 : 17) :
217 ((value < 1<<18) ? 18 : 19)) :
218 ((value < 1<<21) ? ((value < 1<<20) ? 20 : 21) :
219 ((value < 1<<22) ? 22 : 23))) :
220 ((value < 1<<27) ?
221 ((value < 1<<25) ? ((value < 1<<24) ? 24 : 25) :
222 ((value < 1<<26) ? 26 : 27)) :
223 ((value < 1<<29) ? ((value < 1<<28) ? 28 : 29) :
224 ((value < 1<<30) ? 30 :
225 ((value < 1UL<<31) ? 31 : 32))))));
226 }
227
228 void init16BitCharSet (Asn116BitCharSet* pCharSet, ASN116BITCHAR first,
229 ASN116BITCHAR last, ASN1UINT abits, ASN1UINT ubits)
230 {
231 pCharSet->charSet.nchars = 0;
232 pCharSet->charSet.data = 0;
233 pCharSet->firstChar = first;
234 pCharSet->lastChar = last;
235 pCharSet->unalignedBits = ubits;
236 pCharSet->alignedBits = abits;
237 }
238
239 ASN1BOOL isExtendableSize (Asn1SizeCnst* pSizeList)
240 {
241 Asn1SizeCnst* lpSize = pSizeList;
242 while (lpSize) {
243 if (lpSize->extended)
244 return TRUE;
245 else
246 lpSize = lpSize->next;
247 }
248 return FALSE;
249 }
250
251 static ASN1BOOL isFixedSize (Asn1SizeCnst* pSizeList)
252 {
253 Asn1SizeCnst* lpSize = pSizeList;
254 if (lpSize && !lpSize->extended && !lpSize->next) {
255 return (ASN1BOOL) (lpSize->lower == lpSize->upper);
256 }
257 return FALSE;
258 }
259
260 void set16BitCharSet
261 (OOCTXT* pctxt, Asn116BitCharSet* pCharSet, Asn116BitCharSet* pAlphabet)
262 {
263 /* Permitted alphabet range can either be specified as a range of */
264 /* characters or as a discrete set.. */
265
266 if (pAlphabet->charSet.data) {
267 int nocts = pAlphabet->charSet.nchars * 2;
268 pCharSet->charSet.nchars = pAlphabet->charSet.nchars;
269
270 pCharSet->charSet.data =
271 (ASN116BITCHAR*) ASN1MALLOC (pctxt, nocts);
272
273 if (pCharSet->charSet.data != NULL)
274 memcpy (pCharSet->charSet.data, pAlphabet->charSet.data, nocts);
275 }
276 else {
277 pCharSet->firstChar = pAlphabet->firstChar;
278 pCharSet->lastChar = pAlphabet->lastChar;
279 pCharSet->charSet.nchars = pCharSet->lastChar - pCharSet->firstChar;
280 }
281
282 pCharSet->unalignedBits = getUIntBitCount (pCharSet->charSet.nchars);
283
284 pCharSet->alignedBits = 1;
285 while (pCharSet->unalignedBits > pCharSet->alignedBits)
286 pCharSet->alignedBits <<= 1;
287
288 }