blob: 89b92c4886acf4689e4aeb9e4e0e226685da5190 [file] [log] [blame]
rjw6c1fd8f2022-11-30 14:33:01 +08001/*******************************************************************************
2* Copyright Statement:
3* --------------------
4* This software is protected by Copyright and the information contained
5* herein is confidential. The software may not be copied and the information
6* contained herein may not be used or disclosed except with the written
7* permission of MediaTek Inc. (C) 2011
8*
9* BY OPENING THIS FILE, BUYER HEREBY UNEQUIVOCALLY ACKNOWLEDGES AND AGREES
10* THAT THE SOFTWARE/FIRMWARE AND ITS DOCUMENTATIONS ("MEDIATEK SOFTWARE")
11* RECEIVED FROM MEDIATEK AND/OR ITS REPRESENTATIVES ARE PROVIDED TO BUYER ON
12* AN "AS-IS" BASIS ONLY. MEDIATEK EXPRESSLY DISCLAIMS ANY AND ALL WARRANTIES,
13* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE IMPLIED WARRANTIES OF
14* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE OR NONINFRINGEMENT.
15* NEITHER DOES MEDIATEK PROVIDE ANY WARRANTY WHATSOEVER WITH RESPECT TO THE
16* SOFTWARE OF ANY THIRD PARTY WHICH MAY BE USED BY, INCORPORATED IN, OR
17* SUPPLIED WITH THE MEDIATEK SOFTWARE, AND BUYER AGREES TO LOOK ONLY TO SUCH
18* THIRD PARTY FOR ANY WARRANTY CLAIM RELATING THERETO. MEDIATEK SHALL ALSO
19* NOT BE RESPONSIBLE FOR ANY MEDIATEK SOFTWARE RELEASES MADE TO BUYER'S
20* SPECIFICATION OR TO CONFORM TO A PARTICULAR STANDARD OR OPEN FORUM.
21*
22* BUYER'S SOLE AND EXCLUSIVE REMEDY AND MEDIATEK'S ENTIRE AND CUMULATIVE
23* LIABILITY WITH RESPECT TO THE MEDIATEK SOFTWARE RELEASED HEREUNDER WILL BE,
24* AT MEDIATEK'S OPTION, TO REVISE OR REPLACE THE MEDIATEK SOFTWARE AT ISSUE,
25* OR REFUND ANY SOFTWARE LICENSE FEES OR SERVICE CHARGE PAID BY BUYER TO
26* MEDIATEK FOR SUCH MEDIATEK SOFTWARE AT ISSUE.
27*
28* THE TRANSACTION CONTEMPLATED HEREUNDER SHALL BE CONSTRUED IN ACCORDANCE
29* WITH THE LAWS OF THE STATE OF CALIFORNIA, USA, EXCLUDING ITS CONFLICT OF
30* LAWS PRINCIPLES. ANY DISPUTES, CONTROVERSIES OR CLAIMS ARISING THEREOF AND
31* RELATED THERETO SHALL BE SETTLED BY ARBITRATION IN SAN FRANCISCO, CA, UNDER
32* THE RULES OF THE INTERNATIONAL CHAMBER OF COMMERCE (ICC).
33*
34*****************************************************************************/
35
36/****************************************************************************
37 *
38 * COMPONENT: ASN
39 * MODULE: ASN_COMMON
40 * DESCRIPTION: Auto generated by MTK ASN.1 Compiler
41 *
42 ****************************************************************************/
43/*****************************************************************************
44 * removed!
45 *
46 * removed!
47 * removed!
48 * removed!
49 * removed!
50 *
51 * removed!
52 * removed!
53 * removed!
54 * removed!
55 *
56 * removed!
57 * removed!
58 * removed!
59 *
60 * removed!
61 * removed!
62 * removed!
63 *
64 * removed!
65 * removed!
66 * removed!
67 *
68 * removed!
69 * removed!
70 * removed!
71 * removed!
72 *
73 * removed!
74 * removed!
75 * removed!
76 *
77 * removed!
78 * removed!
79 * removed!
80 * removed!
81 * removed!
82 * removed!
83 * removed!
84 * removed!
85 * removed!
86 * removed!
87 * removed!
88 *
89 * removed!
90 * removed!
91 * removed!
92 *
93 * removed!
94 * removed!
95 * removed!
96 *
97 ****************************************************************************/
98
99#include <setjmp.h>
100#include <assert.h>
101#include "kal_public_api.h"
102#include "asn_common.h"
103#include "asn_memory.h"
104
105#ifdef MCD_DLL_EXPORT
106#include <stdarg.h>
107
108#if defined(__COSIM_BYPASS_DRV__)
109void MDM_ASSERT(kal_uint32 e1, kal_uint32 e2, kal_uint32 e3)
110{
111 while(1);/* YY Hsieh */
112}
113#endif
114
115void AsnFreeString( char *string )
116{
117 if( string )
118 {
119 asnMemFree( (void **)&string );
120 }
121}
122
123void InitAsnBuf( AsnContext *pContext )
124{
125 pContext->printBufSize = ASNBUF_BLOCKSIZE;
126 pContext->pMemAllocFunc=NULL;
127 pContext->pMemFreeFunc =NULL;
128 asnMemAlloc(pContext,(void**)&pContext->printBufStart, pContext->printBufSize);
129
130 pContext->printBufNext = pContext->printBufStart;
131}
132
133void AsnPrint( AsnContext *pContext, char *fmt, ... )
134{
135 va_list va;
136 char *new_buf;
137
138 if( (unsigned)(pContext->printBufNext - pContext->printBufStart) > pContext->printBufSize - ASNBUF_RESERVE )
139 {
140 /* Buffer usage has crossed the watermark - need to allocate another
141 buffer one block size bigger */
142 pContext->printBufSize += ASNBUF_BLOCKSIZE;
143
144 asnMemAlloc(pContext,(void**)&new_buf, pContext->printBufSize );
145
146 /* Copy everything from the old buffer to the new buffer */
147 memcpy( new_buf, pContext->printBufStart, pContext->printBufNext - pContext->printBufStart );
148 pContext->printBufNext = pContext->printBufNext - pContext->printBufStart + new_buf;
149
150 /* Free the old buffer */
151 if( pContext->printBufStart )
152 asnMemFree( (void**)&pContext->printBufStart );
153 pContext->printBufStart = new_buf;
154 }
155
156 va_start( va, fmt );
157
158 pContext->printBufNext += vsprintf( pContext->printBufNext, fmt, va );
159
160 va_end( va );
161}
162
163void AsnPrintNull( AsnContext *pContext )
164{
165 AsnPrint( pContext, "NULL" );
166}
167
168void AsnPrintInteger( AsnContext *pContext, S32 value )
169{
170 AsnPrint( pContext, "%d", value );
171}
172
173void AsnPrintBoolean( AsnContext *pContext, Bool boolean )
174{
175 if( boolean == TRUE )
176 {
177 AsnPrint( pContext, "TRUE" );
178 }
179 else
180 {
181 AsnPrint( pContext, "FALSE" );
182 }
183}
184
185void AsnPrintOctetString( AsnContext *pContext, U8 *buffer, U32 length )
186{
187 unsigned int i;
188
189 AsnPrint( pContext, "'" );
190 for( i = 0; i < length; i++ )
191 {
192 AsnPrint( pContext, "%02X", buffer[i] );
193 }
194 AsnPrint( pContext, "'H" );
195}
196
197void AsnPrintBitString( AsnContext *pContext, U8 *buffer, U32 length )
198{
199 unsigned int i;
200 U8 mask[8] = { 0x80, 0x40, 0x20, 0x10, 0x08, 0x04, 0x02, 0x01 };
201
202 AsnPrint( pContext, "'" );
203 for( i = 0; i < length; i++ )
204 {
205 if( buffer[ i/8 ] & mask[ i%8 ] )
206 AsnPrint( pContext, "1" );
207 else
208 AsnPrint( pContext, "0" );
209 }
210 AsnPrint( pContext, "'B" );
211}
212
213void AsnPrintCharacterString( AsnContext *pContext, char *string )
214{
215 AsnPrint( pContext, "\"" );
216 AsnPrint( pContext, "%s", string );
217 AsnPrint( pContext, "\"" );
218}
219
220void AsnPrintOneByteString( AsnContext *pContext, U8 *buffer, U32 length )
221{
222 unsigned int i;
223 AsnPrint( pContext, "'" );
224 for( i = 0; i < length; i++ )
225 {
226 AsnPrint( pContext, "%c", buffer[i] );
227 }
228 AsnPrint( pContext, "'" );
229}
230
231void AsnPrintVisibleString( AsnContext *pContext, asn_VisibleString* pType )
232{
233 AsnPrintOneByteString( pContext, pType->value, pType->valueLen );
234}
235
236void AsnPrintIA5String( AsnContext *pContext, asn_IA5String* pType )
237{
238 AsnPrintOneByteString( pContext, pType->value, pType->valueLen );
239}
240#endif //MCD_DLL_EXPORT
241
242
243U16 GetUperLengthDeterminant( AsnContext *pContext )
244{
245 U16 length = 0;
246 if( ! getShortBits( pContext, 1 ) )
247 {
248 /* 7 bit length */
249 length = (U16)getShortBits( pContext, 7 );
250 }
251 else
252 if( !getShortBits( pContext, 1 ) )
253 {
254 /* 14 bit length */
255 length = (U16)getShortBits( pContext, 14 );
256 }
257 else
258 {
259 /* Unsupported length format */
260 UA1_ERROR( 1 );
261 }
262
263 return length;
264}
265
266void PutUperLengthDeterminant( AsnContext *pContext, U16 length )
267{
268 if( length < 128 )
269 {
270 /* 8 bit length determinant */
271 putShortBits( pContext, 8, length );
272 }
273 else
274 if( length < 16384 )
275 {
276 /* 16 bit length determinant */
277 putShortBits( pContext, 16, length | 0x8000 );
278 }
279 else
280 {
281 /* Unsupported length */
282 UA1_ERROR( 2 );
283 }
284}
285
286void AsnFreeDecoded( void *pType, PAsnMemFreeCallBack pFreeFunc)
287{
288 AsnDecodeFree( ( AllocRecord *)pType - 1, pFreeFunc);
289}
290
291void AsnFreeEncoded( U8 *pEncoded, PAsnMemFreeCallBack pFreeFunc)
292{
293 AsnEncodeFree( pEncoded, pFreeFunc);
294}
295
296void AsnRootDecodeAlloc( AsnContext *pContext, void **ppType, int size )
297{
298 if( !(*ppType) )
299 {
300#if defined(__UMTS_RAT__) || defined(__LTE_RAT__)
301 int i;
302#endif
303 size += sizeof( AllocRecord );
304 asnMemAlloc(pContext, (void**) &pContext->decodeAlloc, size );
305 pContext->decodeAlloc->ppEnd = &(pContext->decodeAlloc->pMemBlock[0]) + sizeof( pContext->decodeAlloc->pMemBlock ) / sizeof( pContext->decodeAlloc->pMemBlock[0] );
306 pContext->decodeAlloc->ppNext = &(pContext->decodeAlloc->pMemBlock[0]);
307 *ppType = pContext->decodeAlloc + 1;
308
309#if defined(__UMTS_RAT__) || defined(__LTE_RAT__)
310 /*nick for block reset*/
311 pContext->decodeAlloc->blocknum = 0;
312 for ( i = 0 ; i < ASN_BLOCK_FREE_NUM ;i ++) {
313 pContext->decodeAlloc->start[i] = NULL;
314 pContext->decodeAlloc->count[i] = 0;
315 }
316#endif
317 }
318}
319
320void AsnDecodeAlloc( AsnContext *pContext, void **ppMem, int size )
321{
322 if( !pContext->decodeAlloc )
323 {
324 /* Attempt to allocate memory when decoding to a user supplied buffer */
325 UA1_ERROR( 3 );
326 }
327
328 if( pContext->decodeAlloc->ppNext >= pContext->decodeAlloc->ppEnd )
329 {
330 /* Limit of runtime allocated decode memory blocks reached */
331 //printf("Limit of runtime allocated decode memory blocks reached");
332 UA1_ERROR( 4 );
333 }
334 asnMemAlloc(pContext, ppMem, size );
335 *pContext->decodeAlloc->ppNext++ = *ppMem;
336}
337
338void AsnDecodeFree( AllocRecord *decodeAlloc, PAsnMemFreeCallBack pFreeFunc)
339{
340 void **ppFree;
341 if( decodeAlloc )
342 {
343 for( ppFree = decodeAlloc->pMemBlock; ppFree < decodeAlloc->ppNext; ppFree++ )
344 {
345 if (pFreeFunc == NULL)
346 {
347 asnMemFree( ppFree );
348 }
349 else
350 {
351 pFreeFunc( ppFree );
352 }
353 }
354
355 if (pFreeFunc == NULL)
356 {
357 asnMemFree((void**)&decodeAlloc);
358 }
359 else
360 {
361 pFreeFunc((void**)&decodeAlloc);
362 }
363 }
364}
365
366#if defined(__UMTS_RAT__) || defined(__LTE_RAT__)
367/* nick integrate from old asn */
368
369void AsnDecodeFreeWithCallback( AllocRecord *decodeAlloc, PAsnMemFreeCallBack pFreeFunc)
370{
371 void **ppFree;
372 if( decodeAlloc )
373 {
374 for( ppFree = decodeAlloc->pMemBlock; ppFree < decodeAlloc->ppNext; ppFree++ )
375 {
376 if (pFreeFunc == NULL)
377 {
378 asnMemFree( ppFree );
379 }
380 else
381 {
382 pFreeFunc( ppFree );
383 }
384 }
385
386 if (pFreeFunc == NULL)
387 {
388 asnMemFree((void**)&decodeAlloc);
389 }
390 else
391 {
392 pFreeFunc((void**)&decodeAlloc);
393 }
394 }
395}
396
397
398
399/*nick for block free */
400static void AsnDecodeFreeWithBlock( AllocRecord *decodeAlloc, PAsnMemFreeCallBack pFreeFunc)
401{
402void **ppFree;
403
404 if( decodeAlloc )
405 {
406 for( ppFree = decodeAlloc->pMemBlock; ppFree < decodeAlloc->ppNext; ppFree++ )
407 {
408 int i = 0;
409 int found = 0;
410 for (i=0; i <ASN_BLOCK_FREE_NUM ; i++) {
411
412 if (!decodeAlloc->start[i]) break;
413
414 if (decodeAlloc->start[i] &&
415 decodeAlloc->count[i] &&
416 *ppFree == decodeAlloc->start[i] ) {
417
418 ppFree += ( decodeAlloc->count[i] -1 );
419 found = 1;
420 break;
421 }
422 }
423 if (found ) continue;
424
425
426 if (pFreeFunc == NULL)
427 {
428 asnMemFree((void**)ppFree );
429 }
430 else
431 {
432 pFreeFunc((void**)ppFree );
433 }
434 }
435
436 if (pFreeFunc == NULL)
437 {
438 asnMemFree((void**)&decodeAlloc);
439 }
440 else
441 {
442 pFreeFunc((void**)&decodeAlloc);
443 }
444
445 }
446}
447
448static void AsnDecodeFreeSetBlock( AllocRecord *decodeAlloc, PAsnMemFreeCallBack pFreeFunc, void *start, int count )
449{
450//void **ppFree;
451
452 if( decodeAlloc )
453 {
454 /*EXT_ASSERT(start && count);*/
455 /*EXT_ASSERT(decodeAlloc->blocknum < ASN_BLOCK_FREE_NUM);*/
456 if (1) { /* no constrain here */
457 decodeAlloc->start[decodeAlloc->blocknum] = start;
458 decodeAlloc->count[decodeAlloc->blocknum] = count;
459 decodeAlloc->blocknum++;
460 }
461 if (pFreeFunc == NULL) {
462 /* do nothing*/
463 } else {
464 /* do nothing*/
465 }
466 }
467}
468
469void AsnFreeDecodedWithBlock( void *pType , PAsnMemFreeCallBack pFreeFunc)
470{
471 AsnDecodeFreeWithBlock( (AllocRecord *)pType - 1, pFreeFunc );
472}
473
474void AsnFreeDecodedSetBlock( void *pType, PAsnMemFreeCallBack pFreeFunc, void *start, int count)
475{
476 AsnDecodeFreeSetBlock( (AllocRecord *)pType - 1, pFreeFunc, start, count);
477}
478#endif
479
480void AsnEncodeAlloc( AsnContext *pContext, U8 **ppMem, U32 *pEncodedLength )
481{
482 U32 size;
483
484 if (NULL == ppMem)
485 {
486 // Encode length test
487 pContext->pEncoded = NULL;
488 }
489 else
490 {
491 if( !(*ppMem) )
492 {
493 /* Fixed size buffer allocated for encoding, you can change this size by modify the batch command */
494 size = 4000;
495 asnMemAlloc(pContext, (void**)&pContext->encodeAlloc, size );
496 *ppMem = (U8*)pContext->encodeAlloc;
497 }
498 NOT_USED( pEncodedLength );
499 }
500}
501
502void AsnEncodeFree( void *pFree, PAsnMemFreeCallBack pFreeFunc)
503{
504 if( pFree )
505 {
506 if (pFreeFunc == NULL)
507 {
508 asnMemFree( &pFree );
509 }
510 else
511 {
512 pFreeFunc( &pFree );
513 }
514 }
515}
516
517#if defined(__UMTS_RAT__) || defined(__LTE_RAT__)
518/* nick add it*/
519void AsnEncodeFreeWithCallback( void *pFree, PAsnMemFreeCallBack pFreeFunc)
520{
521 if( pFree )
522 {
523 if (pFreeFunc == NULL)
524 {
525 asnMemFree( &pFree );
526 }
527 else
528 {
529 pFreeFunc( &pFree );
530 }
531 }
532}
533#endif
534
535void AsnError( AsnContext *pContext, U32 errorCode )
536{
537 pContext->result = errorCode;
538 longjmp( pContext->env, 1 );
539}
540
541extern S32 GetAlphabetIndex(ASN_OneByteAlphabet *pAlphabet, char *pChar)
542{
543 U32 uIndex, left, right;
544 left = 0;
545 right = pAlphabet->valueLen - 1;
546
547 while(left <= right)
548 {
549 uIndex = (left + right) / 2;
550 if(*(pAlphabet->value+uIndex) == *pChar)
551 {
552 return uIndex;
553 }
554 else if(*(pAlphabet->value+uIndex) > *pChar)
555 {
556 right = uIndex - 1;
557 }
558 else
559 {
560 left = uIndex + 1;
561 }
562 }
563 return -1;
564}
565
566extern U32 GetNumberOctetLength(U32 Data)
567{
568 if (Data <= 0xFF) {
569 return 1;
570 }
571 if (Data > 0xFF &&
572 Data <= 0xFFFF) {
573 return 2;
574 }
575 if (Data > 0xFFFF &&
576 Data <= 0xFFFFFF) {
577 return 3;
578 }
579 else if (Data > 0xFFFFFF &&
580 Data <= 0xFFFFFFFF) {
581 return 4;
582 }
583 else{
584 return 0;
585 }
586}
587
588extern Bool OIDCompare(OID oidA, U32 length, U32 *pValue)
589{
590 if (oidA.valueLen != length)
591 {
592 return (Bool)FALSE;
593 }
594 if (0 == asnMemCmp(oidA.value, pValue, length * sizeof(U32)))
595 {
596 return (Bool)TRUE;
597 }
598 else
599 {
600 return (Bool)FALSE;
601 }
602}
603
604static struct {
605 U8 *pByte;
606}SkipedEncodeByte;
607
608extern void SkipEncodeByte(AsnContext *pContext)
609{
610 SkipedEncodeByte.pByte = pContext->pEncoded;
611 pContext->pEncoded += 1;
612}
613
614extern EncodeBeginPoint BeginTestEncodeLen(AsnContext *pContext)
615{
616 if (NULL != pContext->pEncoded)
617 {
618 return (EncodeBeginPoint) pContext->pEncoded;
619 }
620 else
621 {
622 return (EncodeBeginPoint)pContext->shiftRegister;
623 }
624}
625
626extern U32 EndTestEncodeLen(AsnContext *pContext, EncodeBeginPoint BeginPoint)
627{
628 S32 len;
629 ASSERT(BeginPoint != 0);
630
631 if (NULL == pContext->pEncoded)
632 {
633 len = (U32)pContext->shiftRegister - (U32)BeginPoint;
634 }
635 else
636 {
637 len = (U32)pContext->pEncoded - (U32)BeginPoint;
638 }
639
640 len = len > 0 ? len : -len ;
641 return len;
642}
643
644extern U32 testGetShortBits( AsnContext *pContext, U32 numBits)
645{
646 U32 result;
647 AsnContext TempContext = *pContext;
648 result = getShortBits(&TempContext, numBits);
649
650 return result;
651}
652
653extern void PutShortSkipedEncodeByte(AsnContext *pContext, U32 uByte, U32 Data, U32 uAfterSize)
654{
655 AsnContext tempContext;
656
657 if (uAfterSize != 0)
658 {
659 ASSERT(1 >= uByte);
660 }
661
662 if (1 < uByte)
663 {
664 asnMemMove(SkipedEncodeByte.pByte+uByte, SkipedEncodeByte.pByte + 1, uAfterSize);
665 }
666
667 tempContext = *pContext;
668 pContext->pEncoded = SkipedEncodeByte.pByte;
669 putShortBits(pContext, uByte * 8, Data);
670
671 *pContext = tempContext;
672}
673
674
675#define MAX_BITS_FOR_SHORT (24)
676#define BIT_MASK_ARRAY_LENGTH (65)
677
678#if defined (__GNUC__)
679
680static const U64 lsbMask[BIT_MASK_ARRAY_LENGTH]=
681{
6820X0000000000000000ULL,
6830X0000000000000001ULL, 0X0000000000000003ULL, 0X0000000000000007ULL, 0X000000000000000FULL,
6840X000000000000001FULL, 0X000000000000003FULL, 0X000000000000007FULL, 0X00000000000000FFULL,
6850X00000000000001FFULL, 0X00000000000003FFULL, 0X00000000000007FFULL, 0X0000000000000FFFULL,
6860X0000000000001FFFULL, 0X0000000000003FFFULL, 0X0000000000007FFFULL, 0X000000000000FFFFULL,
6870X000000000001FFFFULL, 0X000000000003FFFFULL, 0X000000000007FFFFULL, 0X00000000000FFFFFULL,
6880X00000000001FFFFFULL, 0X00000000003FFFFFULL, 0X00000000007FFFFFULL, 0X0000000000FFFFFFULL,
6890X0000000001FFFFFFULL, 0X0000000003FFFFFFULL, 0X0000000007FFFFFFULL, 0X000000000FFFFFFFULL,
6900X000000001FFFFFFFULL, 0X000000003FFFFFFFULL, 0X000000007FFFFFFFULL, 0X00000000FFFFFFFFULL,
6910X00000001FFFFFFFFULL, 0X00000003FFFFFFFFULL, 0X00000007FFFFFFFFULL, 0X0000000FFFFFFFFFULL,
6920X0000001FFFFFFFFFULL, 0X0000003FFFFFFFFFULL, 0X0000007FFFFFFFFFULL, 0X000000FFFFFFFFFFULL,
6930X000001FFFFFFFFFFULL, 0X000003FFFFFFFFFFULL, 0X000007FFFFFFFFFFULL, 0X00000FFFFFFFFFFFULL,
6940X00001FFFFFFFFFFFULL, 0X00003FFFFFFFFFFFULL, 0X00007FFFFFFFFFFFULL, 0X0000FFFFFFFFFFFFULL,
6950X0001FFFFFFFFFFFFULL, 0X0003FFFFFFFFFFFFULL, 0X0007FFFFFFFFFFFFULL, 0X000FFFFFFFFFFFFFULL,
6960X001FFFFFFFFFFFFFULL, 0X003FFFFFFFFFFFFFULL, 0X007FFFFFFFFFFFFFULL, 0X00FFFFFFFFFFFFFFULL,
6970X01FFFFFFFFFFFFFFULL, 0X03FFFFFFFFFFFFFFULL, 0X07FFFFFFFFFFFFFFULL, 0X0FFFFFFFFFFFFFFFULL,
6980X1FFFFFFFFFFFFFFFULL, 0X3FFFFFFFFFFFFFFFULL, 0X7FFFFFFFFFFFFFFFULL, 0XFFFFFFFFFFFFFFFFULL
699};
700
701#else
702
703static const U64 lsbMask[BIT_MASK_ARRAY_LENGTH]=
704{
7050x0000000000000000,
7060x0000000000000001, 0x0000000000000003, 0x0000000000000007, 0x000000000000000f,
7070x000000000000001f, 0x000000000000003f, 0x000000000000007f, 0x00000000000000ff,
7080x00000000000001ff, 0x00000000000003ff, 0x00000000000007ff, 0x0000000000000fff,
7090x0000000000001fff, 0x0000000000003fff, 0x0000000000007fff, 0x000000000000ffff,
7100x000000000001ffff, 0x000000000003ffff, 0x000000000007ffff, 0x00000000000fffff,
7110x00000000001fffff, 0x00000000003fffff, 0x00000000007fffff, 0x0000000000ffffff,
7120x0000000001ffffff, 0x0000000003ffffff, 0x0000000007ffffff, 0x000000000fffffff,
7130x000000001fffffff, 0x000000003fffffff, 0x000000007fffffff, 0x00000000ffffffff,
7140x00000001ffffffff, 0x00000003ffffffff, 0x00000007ffffffff, 0x0000000fffffffff,
7150x0000001fffffffff, 0x0000003fffffffff, 0x0000007fffffffff, 0x000000ffffffffff,
7160x000001ffffffffff, 0x000003ffffffffff, 0x000007ffffffffff, 0x00000fffffffffff,
7170x00001fffffffffff, 0x00003fffffffffff, 0x00007fffffffffff, 0x0000ffffffffffff,
7180x0001ffffffffffff, 0x0003ffffffffffff, 0x0007ffffffffffff, 0x000fffffffffffff,
7190x001fffffffffffff, 0x003fffffffffffff, 0x007fffffffffffff, 0x00ffffffffffffff,
7200x01ffffffffffffff, 0x03ffffffffffffff, 0x07ffffffffffffff, 0x0fffffffffffffff,
7210x1fffffffffffffff, 0x3fffffffffffffff, 0x7fffffffffffffff, 0xffffffffffffffff
722};
723#endif //defined (__GNUC__)
724
725extern void initFifo( AsnContext *pContext, U8 *buffer, U32 bufferLength )
726{
727 pContext->pEncoded = buffer;
728 pContext->pEncodedEnd = buffer + bufferLength;
729 pContext->shiftRegister = pContext->shiftRegisterLength = 0;
730 pContext->result = 0;
731}
732
733extern void flushFifo( AsnContext *pContext )
734{
735 if( pContext->shiftRegisterLength > 0 )
736 {
737 if (NULL == pContext->pEncoded)
738 {
739 pContext->shiftRegister++;
740 }
741 else
742 {
743 *pContext->pEncoded++ = (U8)(pContext->shiftRegister >> 24);
744 }
745 }
746}
747
748extern U32 getShortBits( AsnContext *pContext, U32 numBits )
749{
750 U32 retVal;
751 ASSERT( numBits <= MAX_BITS_FOR_SHORT );
752
753 while ( numBits > pContext->shiftRegisterLength )
754 {
755 if( pContext->pEncoded >= pContext->pEncodedEnd )
756 {
757 UA1_ERROR( 5 );
758 }
759
760 pContext->shiftRegister |= *pContext->pEncoded++ << (24 - pContext->shiftRegisterLength);
761 pContext->shiftRegisterLength += 8;
762 }
763
764 retVal = pContext->shiftRegister >> (32 - numBits);
765 pContext->shiftRegister <<= numBits;
766 pContext->shiftRegisterLength -= numBits;
767
768 return( retVal );
769}
770
771extern U32 getBits( AsnContext *pContext, U32 numBits )
772{
773 U32 retVal;
774 if ( numBits > 32)
775 {
776 UA1_ERROR( 7 );
777 }
778 else if ( numBits <= MAX_BITS_FOR_SHORT )
779 {
780 retVal = (U32)getShortBits( pContext, numBits );
781 }
782 else
783 {
784 retVal = (U32) getShortBits( pContext, MAX_BITS_FOR_SHORT );
785 numBits -= MAX_BITS_FOR_SHORT;
786 /* ASN.1 is big endian so first n bits go at top of word */
787 retVal <<= numBits;
788 retVal |= (U32)getShortBits( pContext, numBits );
789 }
790 return( retVal );
791}
792
793extern void getLongBits( AsnContext *pContext, U32 numBits, U8 *outputBuffer )
794{
795 while ( numBits >= 8 )
796 {
797 if( pContext->pEncoded >= pContext->pEncodedEnd )
798 {
799 UA1_ERROR( 6 );
800 }
801 pContext->shiftRegister |= *pContext->pEncoded++ << (24 - pContext->shiftRegisterLength);
802 *outputBuffer++ = (U8)(pContext->shiftRegister >> 24);
803 pContext->shiftRegister <<= 8;
804 numBits-= 8;
805 }
806
807 if ( numBits )
808 {
809 *outputBuffer = (U8)(getShortBits( pContext, numBits ) << (8 - numBits));
810 }
811}
812
813extern U64 getInt64( AsnContext *pContext, U32 numBits )
814{
815 U64 retVal;
816 ASSERT( numBits <= 64 );
817 if ( numBits <= 32 )
818 {
819 retVal = (U64)getBits( pContext, numBits );
820 }
821 else
822 {
823 retVal = (U64) getBits( pContext, 32 );
824 numBits -= 32;
825 /* ASN.1 is big endian so first n bits go at top of word */
826 retVal <<= numBits;
827 retVal |= (U64)getBits( pContext, numBits );
828 }
829 return( retVal );
830}
831
832extern void putShortBits( AsnContext *pContext, U32 numBits, U32 data )
833{
834 ASSERT( numBits <= MAX_BITS_FOR_SHORT );
835 ASSERT( 0 == (data & ~lsbMask[numBits]) );
836
837 if (NULL != pContext->pEncoded)
838 {
839 pContext->shiftRegister |= data << (32 - pContext->shiftRegisterLength - numBits);
840 }
841
842 pContext->shiftRegisterLength += numBits;
843
844 while ( pContext->shiftRegisterLength >= 8 )
845 {
846 if (NULL == pContext->pEncoded)
847 {
848 pContext->shiftRegister++;
849 }
850 else
851 {
852 if (pContext->pEncoded >= pContext->pEncodedEnd)
853 {
854 UA1_ERROR(15);
855 }
856
857 *pContext->pEncoded++ = (U8)(pContext->shiftRegister >> 24);
858 pContext->shiftRegister <<= 8;
859 }
860
861 pContext->shiftRegisterLength -= 8;
862 }
863}
864
865extern void putBits( AsnContext *pContext, U32 numBits, U32 data )
866{
867 ASSERT( numBits <= 32 );
868 if( numBits > MAX_BITS_FOR_SHORT )
869 {
870 /* Insert top 24 bits */
871 putShortBits( pContext, MAX_BITS_FOR_SHORT, data >> (numBits - MAX_BITS_FOR_SHORT) );
872 /* reduce amount to fit, and then mask off bits inserted */
873 numBits -= MAX_BITS_FOR_SHORT;
874 data &= lsbMask[numBits];
875 }
876 putShortBits( pContext, numBits, data );
877}
878
879extern void putInt64( AsnContext *pContext, U32 numBits, U64 data )
880{
881 ASSERT( numBits <= 64 );
882 if( numBits > 32 )
883 {
884 /* Insert top 32 bits */
885 putBits( pContext, 32, (U32)(data >> (numBits - 32)) );
886 /* reduce amount to fit, and then mask off bits inserted */
887 numBits -= 32;
888 data &= lsbMask[numBits];
889 }
890 putBits( pContext, numBits, (U32)(data) );
891}
892
893extern void putLongBits( AsnContext *pContext, U32 numBits, U8 *data )
894{
895 while ( numBits >= 8 )
896 {
897 if (NULL == pContext->pEncoded)
898 {
899 pContext->shiftRegister++;
900 }
901 else
902 {
903 if (pContext->pEncoded >= pContext->pEncodedEnd)
904 {
905 UA1_ERROR(15);
906 }
907
908 pContext->shiftRegister |= *data++ << ( 24 - pContext->shiftRegisterLength );
909 *pContext->pEncoded++ = (U8)(pContext->shiftRegister >> 24);
910 pContext->shiftRegister <<= 8;
911 }
912
913 numBits -= 8;
914 }
915
916 if ( numBits && data )
917 {
918 putShortBits( pContext, numBits, *data >> (8 - numBits) );
919 }
920}
921