Debian Codegraph @

addons/ooh323c/src/encode.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 #include <stdlib.h>
20
21 #include "ooasn1.h"
22
23 static int encode16BitConstrainedString
24 (OOCTXT* pctxt, Asn116BitCharString value, Asn116BitCharSet* pCharSet);
25
26 static int encode2sCompBinInt (OOCTXT* pctxt, ASN1INT value);
27 static int encodeNonNegBinInt (OOCTXT* pctxt, ASN1UINT value);
28 static int encodeUnconsLength (OOCTXT* pctxt, ASN1UINT value);
29 static int getIdentByteCount (ASN1UINT ident);
30
31 int encodeBitsFromOctet (OOCTXT* pctxt, ASN1OCTET value, ASN1UINT nbits);
32 int encodeGetMsgBitCnt (OOCTXT* pctxt);
33 int encodeIdent (OOCTXT* pctxt, ASN1UINT ident);
34
35
36 int encodeBit (OOCTXT* pctxt, ASN1BOOL value)
37 {
38 int stat = ASN_OK;
39
40 /* If start of new byte, init to zero */
41
42 if (pctxt->buffer.bitOffset == 8) {
43 pctxt->buffer.data[pctxt->buffer.byteIndex] = 0;
44 }
45
46 /* Adjust bit offset and determine if at end of current byte */
47
48 if (--pctxt->buffer.bitOffset < 0) {
49 if (++pctxt->buffer.byteIndex >= pctxt->buffer.size) {
50 if ((stat = encodeExpandBuffer (pctxt, 1)) != ASN_OK) {
51 return stat;
52 }
53 }
54 pctxt->buffer.data[pctxt->buffer.byteIndex] = 0;
55 pctxt->buffer.bitOffset = 7;
56 }
57
58 /* Set single-bit value */
59
60 if (value) {
61 pctxt->buffer.data[pctxt->buffer.byteIndex] |=
62 ( 1 << pctxt->buffer.bitOffset );
63 }
64
65 /* If last bit in octet, set offsets to start new byte (ED, 9/7/01) */
66
67 if (pctxt->buffer.bitOffset == 0) {
68 pctxt->buffer.bitOffset = 8;
69 pctxt->buffer.byteIndex++;
70 pctxt->buffer.data[pctxt->buffer.byteIndex] = 0;
71 }
72
73 return stat;
74 }
75
76 int encodeBits (OOCTXT* pctxt, ASN1UINT value, ASN1UINT nbits)
77 {
78 int nbytes = (nbits + 7)/ 8, stat = ASN_OK;
79
80 if (nbits == 0) return stat;
81
82 /* If start of new byte, init to zero */
83
84 if (pctxt->buffer.bitOffset == 8) {
85 pctxt->buffer.data[pctxt->buffer.byteIndex] = 0;
86 }
87
88 /* Mask off unused bits from the front of the value */
89
90 if (nbits < (sizeof(ASN1UINT) * 8))
91 value &= ((1 << nbits) - 1);
92
93 /* If bits will fit in current byte, set them and return */
94
95 if (nbits < (unsigned)pctxt->buffer.bitOffset) {
96 pctxt->buffer.bitOffset -= nbits;
97 pctxt->buffer.data[pctxt->buffer.byteIndex] |=
98 ( value << pctxt->buffer.bitOffset );
99 return stat;
100 }
101
102 /* Check buffer space and allocate more memory if necessary */
103
104 stat = encodeCheckBuffer (pctxt, nbytes);
105 if (stat != ASN_OK) return LOG_ASN1ERR (pctxt, stat);
106
107 /* Set bits in remainder of the current byte and then loop */
108 /* to set bits in subsequent bytes.. */
109
110 nbits -= pctxt->buffer.bitOffset;
111 pctxt->buffer.data[pctxt->buffer.byteIndex++] |=
112 (ASN1OCTET)( value >> nbits );
113 pctxt->buffer.data[pctxt->buffer.byteIndex] = 0;
114
115 while (nbits >= 8) {
116 nbits -= 8;
117 pctxt->buffer.data[pctxt->buffer.byteIndex++] =
118 (ASN1OCTET)( value >> nbits );
119 pctxt->buffer.data[pctxt->buffer.byteIndex] = 0;
120 }
121
122 /* copy final partial byte */
123
124 pctxt->buffer.bitOffset = 8 - nbits;
125 if (nbits > 0) {
126 pctxt->buffer.data[pctxt->buffer.byteIndex] =
127 (ASN1OCTET)((value & ((1 << nbits)-1)) << pctxt->buffer.bitOffset);
128 }
129 else
130 pctxt->buffer.data[pctxt->buffer.byteIndex] = 0;
131
132 return stat;
133 }
134
135 int encodeBitsFromOctet (OOCTXT* pctxt, ASN1OCTET value, ASN1UINT nbits)
136 {
137 int lshift = pctxt->buffer.bitOffset;
138 int rshift = 8 - pctxt->buffer.bitOffset;
139 int stat = ASN_OK;
140 ASN1OCTET mask = 0x0;
141
142 if (nbits == 0) return ASN_OK;
143
144 /* Mask off unused bits from the end of the value */
145
146 if (nbits < 8) {
147 switch (nbits) {
148 case 1: mask = 0x80; break;
149 case 2: mask = 0xC0; break;
150 case 3: mask = 0xE0; break;
151 case 4: mask = 0xF0; break;
152 case 5: mask = 0xF8; break;
153 case 6: mask = 0xFC; break;
154 case 7: mask = 0xFE; break;
155 default:;
156 }
157 value &= mask;
158 }
159
160 /* If we are on a byte boundary, we can do a direct assignment */
161
162 if (pctxt->buffer.bitOffset == 8) {
163 pctxt->buffer.data[pctxt->buffer.byteIndex] = value;
164 if (nbits == 8) {
165 pctxt->buffer.byteIndex++;
166 pctxt->buffer.data[pctxt->buffer.byteIndex] = 0;
167 }
168 else
169 pctxt->buffer.bitOffset -= nbits;
170 }
171
172 /* Otherwise, need to set some bits in the first octet and */
173 /* possibly some bits in the following octet.. */
174
175 else {
176 pctxt->buffer.data[pctxt->buffer.byteIndex] |=
177 (ASN1OCTET)(value >> rshift);
178
179 pctxt->buffer.bitOffset -= nbits;
180
181 if (pctxt->buffer.bitOffset < 0) {
182 pctxt->buffer.byteIndex++;
183 pctxt->buffer.data[pctxt->buffer.byteIndex] =
184 (ASN1OCTET)(value << lshift);
185 pctxt->buffer.bitOffset += 8;
186 }
187 }
188
189 return stat;
190 }
191
192 int encodeBitString (OOCTXT* pctxt, ASN1UINT numbits, const ASN1OCTET* data)
193 {
194 int enclen, octidx = 0, stat;
195 Asn1SizeCnst* pSizeList = pctxt->pSizeConstraint;
196
197 for (;;) {
198 if ((enclen = encodeLength (pctxt, numbits)) < 0) {
199 return LOG_ASN1ERR (pctxt, enclen);
200 }
201
202 if (enclen > 0) {
203 ASN1BOOL doAlign;
204
205 stat = bitAndOctetStringAlignmentTest
206 (pSizeList, numbits, TRUE, &doAlign);
207 if (stat != ASN_OK) return LOG_ASN1ERR (pctxt, stat);
208
209 if (doAlign) {
210 stat = encodeByteAlign (pctxt);
211 if (stat != ASN_OK) return LOG_ASN1ERR (pctxt, stat);
212 }
213
214 stat = encodeOctets (pctxt, &data[octidx], enclen);
215 if (stat != ASN_OK) return LOG_ASN1ERR (pctxt, stat);
216 }
217
218 if (enclen < (int)numbits) {
219 numbits -= enclen;
220 octidx += (enclen/8);
221 }
222 else break;
223 }
224
225 return ASN_OK;
226 }
227
228 int encodeBMPString
229 (OOCTXT* pctxt, ASN1BMPString value, Asn116BitCharSet* permCharSet)
230 {
231 Asn116BitCharSet charSet;
232 int stat;
233
234 /* Set character set */
235
236 init16BitCharSet (&charSet, BMP_FIRST, BMP_LAST, BMP_ABITS, BMP_UBITS);
237
238 if (permCharSet) {
239 set16BitCharSet (pctxt, &charSet, permCharSet);
240 }
241
242 /* Encode constrained string */
243
244 stat = encode16BitConstrainedString (pctxt, value, &charSet);
245 if (stat != ASN_OK) return LOG_ASN1ERR (pctxt, stat);
246
247 return stat;
248 }
249
250 int encodeByteAlign (OOCTXT* pctxt)
251 {
252 if (pctxt->buffer.bitOffset != 8) {
253 if ((pctxt->buffer.byteIndex + 1) >= pctxt->buffer.size) {
254 int stat = encodeExpandBuffer (pctxt, 1);
255 if (stat != ASN_OK) return (stat);
256 }
257 pctxt->buffer.byteIndex++;
258 pctxt->buffer.bitOffset = 8;
259 pctxt->buffer.data[pctxt->buffer.byteIndex] = 0;
260 }
261
262 return ASN_OK;
263 }
264
265 int encodeCheckBuffer (OOCTXT* pctxt, ASN1UINT nbytes)
266 {
267 int stat = ASN_OK;
268
269 /* Add one to required bytes because increment logic will always */
270 /* init the byte at the incremented index to zero.. */
271
272 if ( ( pctxt->buffer.byteIndex + nbytes + 1 ) >= pctxt->buffer.size ) {
273 if ((stat = encodeExpandBuffer (pctxt, nbytes+1)) != ASN_OK) {
274 return LOG_ASN1ERR (pctxt, stat);
275 }
276 }
277
278 return (stat);
279 }
280
281 int encodeConsInteger
282 (OOCTXT* pctxt, ASN1INT value, ASN1INT lower, ASN1INT upper)
283 {
284 ASN1UINT range_value;
285 ASN1UINT adjusted_value;
286 int stat;
287
288 /* Check value against given range */
289
290 if (value < lower || value > upper) {
291 return ASN_E_CONSVIO;
292 }
293
294 /* Adjust range value based on lower/upper signed values and */
295 /* other possible conflicts.. */
296
297 if ((upper > 0 && lower >= 0) || (upper <= 0 && lower < 0)) {
298 range_value = upper - lower;
299 adjusted_value = value - lower;
300 }
301 else {
302 range_value = upper + abs(lower);
303 adjusted_value = value + abs(lower);
304 }
305
306 if (range_value != ASN1UINT_MAX) { range_value += 1; }
307
308 if (range_value == 0 || lower > upper)
309 stat = ASN_E_RANGERR;
310 else if (lower != upper) {
311 stat = encodeConsWholeNumber (pctxt, adjusted_value, range_value);
312 }
313 else
314 stat = ASN_OK;
315
316 return stat;
317 }
318
319 int encodeConsUnsigned
320 (OOCTXT* pctxt, ASN1UINT value, ASN1UINT lower, ASN1UINT upper)
321 {
322 ASN1UINT range_value;
323 ASN1UINT adjusted_value;
324 int stat;
325
326 /* Check for special case: if lower is 0 and upper is ASN1UINT_MAX, */
327 /* set range to ASN1UINT_MAX; otherwise to upper - lower + 1 */
328
329 range_value = (lower == 0 && upper == ASN1UINT_MAX) ?
330 ASN1UINT_MAX : upper - lower + 1;
331
332 adjusted_value = value - lower;
333
334 if (lower != upper) {
335 stat = encodeConsWholeNumber (pctxt, adjusted_value, range_value);
336 }
337 else
338 stat = ASN_OK;
339
340 return stat;
341 }
342
343 int encodeConsWholeNumber
344 (OOCTXT* pctxt, ASN1UINT adjusted_value, ASN1UINT range_value)
345 {
346 ASN1UINT nocts, range_bitcnt = getUIntBitCount (range_value - 1);
347 int stat;
348
349 if (adjusted_value >= range_value && range_value != ASN1UINT_MAX) {
350 return LOG_ASN1ERR (pctxt, ASN_E_RANGERR);
351 }
352
353 /* If range is <= 255, bit-field case (10.5.7a) */
354
355 if (range_value <= 255) {
356 return encodeBits (pctxt, adjusted_value, range_bitcnt);
357 }
358
359 /* If range is exactly 256, one-octet case (10.5.7b) */
360
361 else if (range_value == 256) {
362 stat = encodeByteAlign (pctxt);
363 if (stat != ASN_OK) return LOG_ASN1ERR (pctxt, stat);
364
365 return encodeBits (pctxt, adjusted_value, 8);
366 }
367
368 /* If range > 256 and <= 64k (65536), two-octet case (10.5.7c) */
369
370 else if (range_value <= 65536) {
371 stat = encodeByteAlign (pctxt);
372 if (stat != ASN_OK) return LOG_ASN1ERR (pctxt, stat);
373
374 return encodeBits (pctxt, adjusted_value, 16);
375 }
376
377 /* If range > 64k, indefinite-length case (10.5.7d) */
378
379 else {
380 /* Encode length determinant as a constrained whole number. */
381 /* Constraint is 1 to max number of bytes needed to hold */
382 /* the target integer value.. */
383
384 if (adjusted_value < 256) nocts = 1;
385 else if (adjusted_value < 65536) nocts = 2;
386 else if (adjusted_value < 0x1000000) nocts = 3;
387 else nocts = 4;
388
389 stat = encodeBits (pctxt, nocts - 1, 2);
390 if (stat != ASN_OK) return LOG_ASN1ERR (pctxt, stat);
391
392 stat = encodeByteAlign (pctxt);
393 if (stat != ASN_OK) return LOG_ASN1ERR (pctxt, stat);
394
395 return encodeNonNegBinInt (pctxt, adjusted_value);
396 }
397 }
398
399 int encodeConstrainedStringEx (OOCTXT* pctxt,
400 const char* string,
401 const char* charSet,
402 ASN1UINT abits, /* aligned char bits */
403 ASN1UINT ubits, /* unaligned char bits */
404 ASN1UINT canSetBits)
405 {
406 ASN1UINT i, len = strlen(string);
407 int stat;
408 /* note: need to save size constraint for use in alignCharStr */
409 /* because it will be cleared in encodeLength from the context.. */
410 Asn1SizeCnst* psize = pctxt->pSizeConstraint;
411
412 /* Encode length */
413
414 stat = encodeLength (pctxt, len);
415 if (stat < 0) return LOG_ASN1ERR (pctxt, stat);
416
417 /* Byte align */
418
419 if (alignCharStr (pctxt, len, abits, psize)) {
420 stat = encodeByteAlign (pctxt);
421 if (stat != ASN_OK) return LOG_ASN1ERR (pctxt, stat);
422 }
423
424 /* Encode data */
425
426 if (abits >= canSetBits && canSetBits > 4) {
427 for (i = 0; i < len; i++) {
428 if ((stat = encodeBits (pctxt, string[i], abits)) != ASN_OK)
429 return LOG_ASN1ERR (pctxt, stat);
430 }
431 }
432 else if (0 != charSet) {
433 ASN1UINT nchars = strlen(charSet), pos;
434 const char* ptr;
435 for (i = 0; i < len; i++) {
436 ptr = memchr (charSet, string[i], nchars);
437
438 if (0 == ptr)
439 return LOG_ASN1ERR (pctxt, ASN_E_CONSVIO);
440 else
441 pos = ptr - charSet;
442
443 if ((stat = encodeBits (pctxt, pos, abits)) != ASN_OK)
444 return LOG_ASN1ERR (pctxt, stat);
445 }
446 }
447 else return LOG_ASN1ERR (pctxt, ASN_E_INVPARAM);
448
449 return stat;
450 }
451
452 int encodeExpandBuffer (OOCTXT* pctxt, ASN1UINT nbytes)
453 {
454 if (pctxt->buffer.dynamic)
455 {
456 /* If dynamic encoding is enabled, expand the current buffer to */
457 /* allow encoding to continue. */
458
459 pctxt->buffer.size += ASN1MAX (ASN_K_ENCBUFSIZ, nbytes);
460
461 pctxt->buffer.data = (ASN1OCTET*) memHeapRealloc
462 (&pctxt->pMsgMemHeap, pctxt->buffer.data, pctxt->buffer.size);
463
464 if (!pctxt->buffer.data) return (ASN_E_NOMEM);
465
466 return (ASN_OK);
467 }
468
469 return (ASN_E_BUFOVFLW);
470 }
471
472 int encodeGetMsgBitCnt (OOCTXT* pctxt)
473 {
474 int numBitsInLastByte = 8 - pctxt->buffer.bitOffset;
475 return ((pctxt->buffer.byteIndex * 8) + numBitsInLastByte);
476 }
477
478 ASN1OCTET* encodeGetMsgPtr (OOCTXT* pctxt, int* pLength)
479 {
480 if (pLength) *pLength = getPERMsgLen (pctxt);
481 return pctxt->buffer.data;
482 }
483
484 int encodeIdent (OOCTXT* pctxt, ASN1UINT ident)
485 {
486 ASN1UINT mask;
487 int nshifts = 0, stat;
488
489 if (ident !=0) {
490 ASN1UINT lv;
491 nshifts = getIdentByteCount (ident);
492 while (nshifts > 0) {
493 mask = ((ASN1UINT)0x7f) << (7 * (nshifts - 1));
494 nshifts--;
495 lv = (ASN1UINT)((ident & mask) >> (nshifts * 7));
496 if (nshifts != 0) { lv |= 0x80; }
497 if ((stat = encodeBits (pctxt, lv, 8)) != ASN_OK)
498 return LOG_ASN1ERR (pctxt, stat);
499 }
500 }
501 else {
502 /* encode a single zero byte */
503 if ((stat = encodeBits (pctxt, 0, 8)) != ASN_OK)
504 return LOG_ASN1ERR (pctxt, stat);
505 }
506
507 return ASN_OK;
508 }
509
510 int encodeLength (OOCTXT* pctxt, ASN1UINT value)
511 {
512 ASN1BOOL extendable;
513 Asn1SizeCnst* pSize =
514 checkSize (pctxt->pSizeConstraint, value, &extendable);
515 ASN1UINT lower = (pSize) ? pSize->lower : 0;
516 ASN1UINT upper = (pSize) ? pSize->upper : ASN1UINT_MAX;
517 int enclen, stat;
518
519 /* If size constraints exist and the given length did not fall */
520 /* within the range of any of them, signal constraint violation */
521 /* error.. */
522
523 if (pctxt->pSizeConstraint && !pSize)
524 return LOG_ASN1ERR (pctxt, ASN_E_CONSVIO);
525
526 /* Reset the size constraint in the context block structure */
527
528 pctxt->pSizeConstraint = 0;
529
530 /* If size constraint is present and extendable, encode extension */
531 /* bit.. */
532
533 if (extendable) {
534 stat = (pSize) ?
535 encodeBit (pctxt, pSize->extended) : encodeBit (pctxt, 1);
536
537 if (stat != ASN_OK) return (stat);
538 }
539
540 /* If upper limit is less than 64k, constrained case */
541
542 if (upper < 65536) {
543 stat = (lower == upper) ? ASN_OK :
544 encodeConsWholeNumber (pctxt, value - lower, upper - lower + 1);
545 enclen = (stat == ASN_OK) ? value : stat;
546 }
547 else {
548 /* unconstrained case or Constrained with upper bound >= 64K*/
549 enclen = encodeUnconsLength (pctxt, value);
550 }
551
552 return enclen;
553
554 }
555
556 int encodeObjectIdentifier (OOCTXT* pctxt, ASN1OBJID* pvalue)
557 {
558 int len, stat;
559 ASN1UINT temp;
560 register int numids, i;
561
562 /* Calculate length in bytes and encode */
563
564 len = 1; /* 1st 2 arcs require 1 byte */
565 numids = pvalue->numids;
566 for (i = 2; i < numids; i++) {
567 len += getIdentByteCount (pvalue->subid[i]);
568 }
569
570 /* PER encode length */
571
572 if ((stat = encodeLength (pctxt, (ASN1UINT)len)) < 0) {
573 return LOG_ASN1ERR (pctxt, stat);
574 }
575
576 /* Validate given object ID by applying ASN.1 rules */
577
578 if (0 == pvalue) return LOG_ASN1ERR (pctxt, ASN_E_INVOBJID);
579 if (numids < 2) return LOG_ASN1ERR (pctxt, ASN_E_INVOBJID);
580 if (pvalue->subid[0] > 2) return LOG_ASN1ERR (pctxt, ASN_E_INVOBJID);
581 if (pvalue->subid[0] != 2 && pvalue->subid[1] > 39)
582 return LOG_ASN1ERR (pctxt, ASN_E_INVOBJID);
583
584 /* Passed checks, encode object identifier */
585
586 /* Munge first two sub ID's and encode */
587
588 temp = ((pvalue->subid[0] * 40) + pvalue->subid[1]);
589 if ((stat = encodeIdent (pctxt, temp)) != ASN_OK)
590 return LOG_ASN1ERR (pctxt, stat);
591
592 /* Encode the remainder of the OID value */
593
594 for (i = 2; i < numids; i++) {
595 if ((stat = encodeIdent (pctxt, pvalue->subid[i])) != ASN_OK)
596 return LOG_ASN1ERR (pctxt, stat);
597 }
598
599 return ASN_OK;
600 }
601
602 int encodebitsFromOctet (OOCTXT* pctxt, ASN1OCTET value, ASN1UINT nbits)
603 {
604 int lshift = pctxt->buffer.bitOffset;
605 int rshift = 8 - pctxt->buffer.bitOffset;
606 int stat = ASN_OK;
607 ASN1OCTET mask = 0x0;
608
609 if (nbits == 0) return ASN_OK;
610
611 /* Mask off unused bits from the end of the value */
612
613 if (nbits < 8) {
614 switch (nbits) {
615 case 1: mask = 0x80; break;
616 case 2: mask = 0xC0; break;
617 case 3: mask = 0xE0; break;
618 case 4: mask = 0xF0; break;
619 case 5: mask = 0xF8; break;
620 case 6: mask = 0xFC; break;
621 case 7: mask = 0xFE; break;
622 default:;
623 }
624 value &= mask;
625 }
626
627 /* If we are on a byte boundary, we can do a direct assignment */
628
629 if (pctxt->buffer.bitOffset == 8) {
630 pctxt->buffer.data[pctxt->buffer.byteIndex] = value;
631 if (nbits == 8) {
632 pctxt->buffer.byteIndex++;
633 pctxt->buffer.data[pctxt->buffer.byteIndex] = 0;
634 }
635 else
636 pctxt->buffer.bitOffset -= nbits;
637 }
638
639 /* Otherwise, need to set some bits in the first octet and */
640 /* possibly some bits in the following octet.. */
641
642 else {
643 pctxt->buffer.data[pctxt->buffer.byteIndex] |=
644 (ASN1OCTET)(value >> rshift);
645
646 pctxt->buffer.bitOffset -= nbits;
647
648 if (pctxt->buffer.bitOffset < 0) {
649 pctxt->buffer.byteIndex++;
650 pctxt->buffer.data[pctxt->buffer.byteIndex] =
651 (ASN1OCTET)(value << lshift);
652 pctxt->buffer.bitOffset += 8;
653 }
654 }
655
656 return stat;
657 }
658
659 int encodeOctets (OOCTXT* pctxt, const ASN1OCTET* pvalue, ASN1UINT nbits)
660 {
661 int i = 0, stat;
662 int numFullOcts = nbits / 8;
663
664 if (nbits == 0) return 0;
665
666 /* Check buffer space and allocate more memory if necessary */
667
668 stat = encodeCheckBuffer (pctxt, numFullOcts + 1);
669 if (stat != ASN_OK) return LOG_ASN1ERR (pctxt, stat);
670
671 if (numFullOcts > 0) {
672
673 /* If the current bit offset is 8 (i.e. we don't have a */
674 /* byte started), can copy the string directly to the */
675 /* encode buffer.. */
676
677 if (pctxt->buffer.bitOffset == 8) {
678 memcpy (&pctxt->buffer.data[pctxt->buffer.byteIndex], pvalue,
679 numFullOcts);
680 pctxt->buffer.byteIndex += numFullOcts;
681 pctxt->buffer.data[pctxt->buffer.byteIndex] = 0;
682 i = numFullOcts;
683 }
684
685 /* Else, copy bits */
686
687 else {
688 for (i = 0; i < numFullOcts; i++) {
689 stat = encodeBitsFromOctet (pctxt, pvalue[i], 8);
690 if (stat != ASN_OK) return stat;
691 }
692 }
693 }
694
695 /* Move remaining bits from the last octet to the output buffer */
696
697 if (nbits % 8 != 0) {
698 stat = encodeBitsFromOctet (pctxt, pvalue[i], nbits % 8);
699 }
700
701 return stat;
702 }
703
704 int encodeOctetString (OOCTXT* pctxt, ASN1UINT numocts, const ASN1OCTET* data)
705 {
706 int enclen, octidx = 0, stat;
707 Asn1SizeCnst* pSizeList = pctxt->pSizeConstraint;
708
709 for (;;) {
710 if ((enclen = encodeLength (pctxt, numocts)) < 0) {
711 return LOG_ASN1ERR (pctxt, enclen);
712 }
713
714 if (enclen > 0) {
715 ASN1BOOL doAlign;
716
717 stat = bitAndOctetStringAlignmentTest
718 (pSizeList, numocts, FALSE, &doAlign);
719 if (stat != ASN_OK) return LOG_ASN1ERR (pctxt, stat);
720
721 if (doAlign) {
722 stat = encodeByteAlign (pctxt);
723 if (stat != ASN_OK) return LOG_ASN1ERR (pctxt, stat);
724 }
725
726 stat = encodeOctets (pctxt, &data[octidx], enclen * 8);
727 if (stat != ASN_OK) return LOG_ASN1ERR (pctxt, stat);
728 }
729
730 if (enclen < (int)numocts) {
731 numocts -= enclen;
732 octidx += enclen;
733 }
734 else break;
735 }
736
737 return ASN_OK;
738 }
739
740 int encodeOpenType (OOCTXT* pctxt, ASN1UINT numocts, const ASN1OCTET* data)
741 {
742 int enclen, octidx = 0, stat;
743 ASN1OCTET zeroByte = 0x00;
744 ASN1OpenType openType;
745
746 /* If open type contains length zero, add a single zero byte (10.1) */
747
748 if (numocts == 0) {
749 openType.numocts = 1;
750 openType.data = &zeroByte;
751 }
752 else {
753 openType.numocts = numocts;
754 openType.data = data;
755 }
756
757 /* Encode the open type */
758
759 for (;;) {
760 if ((enclen = encodeLength (pctxt, openType.numocts)) < 0) {
761 return LOG_ASN1ERR (pctxt, enclen);
762 }
763
764 stat = encodeByteAlign (pctxt);
765 if (stat != ASN_OK) return LOG_ASN1ERR (pctxt, stat);
766
767 stat = encodeOctets (pctxt, &openType.data[octidx], enclen * 8);
768 if (stat != ASN_OK) return LOG_ASN1ERR (pctxt, stat);
769
770 if (enclen < (int)openType.numocts) {
771 openType.numocts -= enclen;
772 octidx += enclen;
773 }
774 else break;
775 }
776
777 return ASN_OK;
778 }
779
780 int encodeOpenTypeExt (OOCTXT* pctxt, DList* pElemList)
781 {
782 DListNode* pnode;
783 ASN1OpenType* pOpenType;
784 int stat;
785
786 if (0 != pElemList) {
787 pnode = pElemList->head;
788 while (0 != pnode) {
789 if (0 != pnode->data) {
790 pOpenType = (ASN1OpenType*)pnode->data;
791
792 if (pOpenType->numocts > 0) {
793 stat = encodeByteAlign (pctxt);
794 if (stat != ASN_OK) return LOG_ASN1ERR (pctxt, stat);
795
796 stat = encodeOpenType
797 (pctxt, pOpenType->numocts, pOpenType->data);
798
799 if (stat != ASN_OK) return LOG_ASN1ERR (pctxt, stat);
800 }
801 }
802 pnode = pnode->next;
803 }
804 }
805
806 return ASN_OK;
807 }
808
809 int encodeOpenTypeExtBits (OOCTXT* pctxt, DList* pElemList)
810 {
811 DListNode* pnode;
812 int stat;
813
814 if (0 != pElemList) {
815 pnode = pElemList->head;
816
817 while (0 != pnode) {
818 stat = encodeBit (pctxt, (ASN1BOOL)(0 != pnode->data));
819 if (stat != ASN_OK) return LOG_ASN1ERR (pctxt, stat);
820
821 pnode = pnode->next;
822 }
823 }
824
825 return ASN_OK;
826 }
827
828 int encodeSemiConsInteger (OOCTXT* pctxt, ASN1INT value, ASN1INT lower)
829 {
830 int nbytes, stat;
831 int shift = ((sizeof(value) - 1) * 8) - 1;
832 ASN1UINT tempValue;
833
834 if (lower > ASN1INT_MIN)
835 value -= lower;
836
837 /* Calculate signed number value length */
838
839 for ( ; shift > 0; shift -= 8) {
840 tempValue = (value >> shift) & 0x1ff;
841 if (tempValue == 0 || tempValue == 0x1ff) continue;
842 else break;
843 }
844
845 nbytes = (shift + 9) / 8;
846
847 /* Encode length */
848
849 if ((stat = encodeLength (pctxt, nbytes)) < 0) {
850 return stat;
851 }
852
853 if ((stat = encodeByteAlign (pctxt)) != ASN_OK)
854 return stat;
855
856 /* Encode signed value */
857
858 stat = encode2sCompBinInt (pctxt, value);
859
860 return stat;
861 }
862
863 int encodeSemiConsUnsigned (OOCTXT* pctxt, ASN1UINT value, ASN1UINT lower)
864 {
865 int nbytes, stat;
866 int shift = ((sizeof(value) - 1) * 8) - 1;
867 ASN1UINT mask = 1UL << ((sizeof(value) * 8) - 1);
868 ASN1UINT tempValue;
869
870 value -= lower;
871
872 /* Calculate unsigned number value length */
873
874 for ( ; shift > 0; shift -= 8) {
875 tempValue = (value >> shift) & 0x1ff;
876
877 if (tempValue == 0) continue;
878 else break;
879 }
880
881 nbytes = (shift + 9) / 8;
882
883 /* If MS bit in unsigned number is set, add an extra zero byte */
884
885 if ((value & mask) != 0) nbytes++;
886
887 /* Encode length */
888
889 if ((stat = encodeLength (pctxt, nbytes)) < 0) {
890 return stat;
891 }
892
893 if ((stat = encodeByteAlign (pctxt)) != ASN_OK)
894 return stat;
895
896 /* Encode additional zero byte if necessary */
897
898 if (nbytes > sizeof(value)) {
899 stat = encodebitsFromOctet (pctxt, 0, 8);
900 if (stat != ASN_OK) return (stat);
901 }
902
903 /* Encode unsigned value */
904
905 stat = encodeNonNegBinInt (pctxt, value);
906
907 return stat;
908 }
909
910 int encodeSmallNonNegWholeNumber (OOCTXT* pctxt, ASN1UINT value)
911 {
912 int stat;
913
914 if (value < 64) {
915 stat = encodeBits (pctxt, value, 7);
916 }
917 else {
918 ASN1UINT len;
919
920 /* Encode a one-byte length determinant value */
921 if (value < 256) len = 1;
922 else if (value < 65536) len = 2;
923 else if (value < 0x1000000) len = 3;
924 else len = 4;
925
926 stat = encodeBits (pctxt, len, 8);
927
928 /* Byte-align and encode the value */
929 if (stat == ASN_OK) {
930 if ((stat = encodeByteAlign (pctxt)) == ASN_OK) {
931 stat = encodeBits (pctxt, value, len*8);
932 }
933 }
934 }
935
936 return stat;
937 }
938
939 int encodeVarWidthCharString (OOCTXT* pctxt, const char* value)
940 {
941 int stat;
942 ASN1UINT len = strlen (value);
943 /* note: need to save size constraint for use in alignCharStr */
944 /* because it will be cleared in encodeLength from the context.. */
945 Asn1SizeCnst* psize = pctxt->pSizeConstraint;
946
947 /* Encode length */
948
949 stat = encodeLength (pctxt, len);
950 if (stat < 0) return LOG_ASN1ERR (pctxt, stat);
951
952 /* Byte align */
953
954 if (alignCharStr (pctxt, len, 8, psize)) {
955 stat = encodeByteAlign (pctxt);
956 if (stat != ASN_OK) return LOG_ASN1ERR (pctxt, stat);
957 }
958
959 /* Encode data */
960
961 stat = encodeOctets (pctxt, (const ASN1OCTET*)value, len * 8);
962 if (stat != ASN_OK) return LOG_ASN1ERR (pctxt, stat);
963
964 return ASN_OK;
965 }
966
967 static int encode16BitConstrainedString
968 (OOCTXT* pctxt, Asn116BitCharString value, Asn116BitCharSet* pCharSet)
969 {
970 ASN1UINT i, pos;
971 ASN1UINT nbits = pCharSet->alignedBits;
972 int stat;
973
974 /* Encode length */
975
976 stat = encodeLength (pctxt, value.nchars);
977 if (stat < 0) return LOG_ASN1ERR (pctxt, stat);
978
979 /* Byte align */
980
981 stat = encodeByteAlign (pctxt);
982 if (stat != ASN_OK) return LOG_ASN1ERR (pctxt, stat);
983
984 /* Encode data */
985
986 for (i = 0; i < value.nchars; i++) {
987 if (pCharSet->charSet.data == 0) {
988 stat = encodeBits
989 (pctxt, value.data[i] - pCharSet->firstChar, nbits);
990 if (stat != ASN_OK) return LOG_ASN1ERR (pctxt, stat);
991 }
992 else {
993 for (pos = 0; pos < pCharSet->charSet.nchars; pos++) {
994 if (value.data[i] == pCharSet->charSet.data[pos]) {
995 stat = encodeBits (pctxt, pos, nbits);
996 if (stat != ASN_OK) return LOG_ASN1ERR (pctxt, stat);
997 break;
998 }
999 }
1000 }
1001 }
1002
1003 return stat;
1004 }
1005
1006 int encode2sCompBinInt (OOCTXT* pctxt, ASN1INT value)
1007 {
1008 /* 10.4.6 A minimum octet 2's-complement-binary-integer encoding */
1009 /* of the whole number has a field width that is a multiple of 8 */
1010 /* bits and also satisfies the condition that the leading 9 bits */
1011 /* field shall not be all zeros and shall not be all ones. */
1012
1013 /* first encode integer value into a local buffer */
1014
1015 ASN1OCTET lbuf[8], lb;
1016 ASN1INT i = sizeof(lbuf), temp = value;
1017
1018 memset (lbuf, 0, sizeof(lbuf));
1019 do {
1020 lb = temp % 256;
1021 temp /= 256;
1022 if (temp < 0 && lb != 0) temp--; /* two's complement adjustment */
1023 lbuf[--i] = lb;
1024 } while (temp != 0 && temp != -1);
1025
1026 /* If the value is positive and bit 8 of the leading byte is set, */
1027 /* copy a zero byte to the contents to signal a positive number.. */
1028
1029 if (value > 0 && (lb & 0x80) != 0) {
1030 i--;
1031 }
1032
1033 /* If the value is negative and bit 8 of the leading byte is clear, */
1034 /* copy a -1 byte (0xFF) to the contents to signal a negative */
1035 /* number.. */
1036
1037 else if (value < 0 && ((lb & 0x80) == 0)) {
1038 lbuf[--i] = 0xff;
1039 }
1040
1041 /* Add the data to the encode buffer */
1042
1043 return encodeOctets (pctxt, &lbuf[i], (sizeof(lbuf) - i) * 8);
1044 }
1045
1046 static int encodeNonNegBinInt (OOCTXT* pctxt, ASN1UINT value)
1047 {
1048 /* 10.3.6 A minimum octet non-negative binary integer encoding of */
1049 /* the whole number (which does not predetermine the number of */
1050 /* octets to be used for the encoding) has a field which is a */
1051 /* multiple of 8 bits and also satisfies the condition that the */
1052 /* leading eight bits of the field shall not be zero unless the */
1053 /* field is precisely 8 bits long. */
1054
1055 ASN1UINT bitcnt = (value == 0) ? 1 : getUIntBitCount (value);
1056
1057 /* round-up to nearest 8-bit boundary */
1058
1059 bitcnt = (bitcnt + 7) & (~7);
1060
1061 /* encode bits */
1062
1063 return encodeBits (pctxt, value, bitcnt);
1064 }
1065
1066 static int encodeUnconsLength (OOCTXT* pctxt, ASN1UINT value)
1067 {
1068 int enclen, stat;
1069
1070 stat = encodeByteAlign (pctxt);
1071 if (stat != ASN_OK) return LOG_ASN1ERR (pctxt, stat);
1072
1073 /* 1 octet case */
1074 if (value < 128) {
1075 stat = encodeBits (pctxt, value, 8);
1076 enclen = (stat == ASN_OK) ? value : stat;
1077 }
1078 /* 2 octet case */
1079 else if (value < 16384) {
1080 if ((stat = encodeBit (pctxt, 1)) == ASN_OK)
1081 stat = encodeBits (pctxt, value, 15);
1082 enclen = (stat == ASN_OK) ? value : stat;
1083 }
1084 /* fragmentation case */
1085 else {
1086 int multiplier = ASN1MIN (value/16384, 4);
1087 encodeBit (pctxt, 1); /* set bit 8 of first octet */
1088 encodeBit (pctxt, 1); /* set bit 7 of first octet */
1089 stat = encodeBits (pctxt, multiplier, 6);
1090 enclen = (stat == ASN_OK) ? 16384 * multiplier : stat;
1091 }
1092
1093 return enclen;
1094 }
1095
1096 static int getIdentByteCount (ASN1UINT ident)
1097 {
1098 if (ident < (1u << 7)) { /* 7 */
1099 return 1;
1100 }
1101 else if (ident < (1u << 14)) { /* 14 */
1102 return 2;
1103 }
1104 else if (ident < (1u << 21)) { /* 21 */
1105 return 3;
1106 }
1107 else if (ident < (1u << 28)) { /* 28 */
1108 return 4;
1109 }
1110 return 5;
1111 }