143 lines
3.9 KiB
C
143 lines
3.9 KiB
C
/*****************************************************************************
|
|
* Copyright (C) 2004-2021
|
|
* ProbeStar Technical Systems, LLC
|
|
* All Rights Reserved. Proprietary and Confidential.
|
|
*============================================================================
|
|
*****************************************************************************/
|
|
|
|
/*****************************************************************************
|
|
* includes
|
|
*****************************************************************************/
|
|
|
|
#include "embtypes.h"
|
|
#include "asn1uper.h"
|
|
|
|
/*****************************************************************************
|
|
* defines
|
|
*****************************************************************************/
|
|
|
|
/*****************************************************************************
|
|
* macros
|
|
*****************************************************************************/
|
|
|
|
#define min(A,B) ((A)<(B)?(A):(B))
|
|
#define max(A,B) ((A)>(B)?(A):(B))
|
|
|
|
/*****************************************************************************
|
|
* structs & typedefs
|
|
*****************************************************************************/
|
|
|
|
/*****************************************************************************
|
|
* global constants
|
|
*****************************************************************************/
|
|
|
|
/*****************************************************************************
|
|
* global variables
|
|
*****************************************************************************/
|
|
|
|
/*****************************************************************************
|
|
* static constants
|
|
*****************************************************************************/
|
|
|
|
/*****************************************************************************
|
|
* static variables
|
|
*****************************************************************************/
|
|
|
|
/*****************************************************************************
|
|
* static prototypes
|
|
*****************************************************************************/
|
|
|
|
/*****************************************************************************
|
|
* C functions
|
|
*****************************************************************************/
|
|
|
|
// grab extension bits, check for extension defined by ellipses
|
|
uint uper_get_extension(UPER_Stream *uper, UPER_Extension *ext, uint noptions, uint ellipses)
|
|
{
|
|
if (!uper || !ext)
|
|
return 0;
|
|
|
|
uper_init_extension(ext);
|
|
|
|
ellipses &= 1;
|
|
if ((noptions+ellipses)==0) {
|
|
uper->error = 1;
|
|
return 0;
|
|
}
|
|
|
|
// check for extensions
|
|
if (ellipses) {
|
|
ext->ellipses = uper_get_bits_stream(uper, 1);
|
|
}
|
|
|
|
// check for options
|
|
if (noptions) {
|
|
ext->value = uper_get_bits_stream(uper, noptions);
|
|
ext->options = noptions;
|
|
}
|
|
|
|
if (ext->ellipses) {
|
|
uper_get_ext_length(uper, ext);
|
|
|
|
if (_asn1_debug_on_)
|
|
printf("\tEXT_LEN = %d\n", ext->length);
|
|
// otherwise no ...
|
|
}
|
|
|
|
return ellipses + noptions;
|
|
}
|
|
|
|
uint uper_get_ext_length(UPER_Stream *uper, UPER_Extension *ext)
|
|
{
|
|
if (!ext->ellipses)
|
|
return 0;
|
|
|
|
uint len = uper_get_bits_stream(uper, 8);
|
|
|
|
if (len & 0x80) {
|
|
len = (len & 0x7f) << 8;
|
|
len |= uper_get_bits_stream(uper, 8);
|
|
}
|
|
else
|
|
len &= 0x7f;
|
|
ext->length = len;
|
|
|
|
return len;
|
|
}
|
|
|
|
// if ellipses extension available
|
|
uint uper_get_ext_ellipses(UPER_Extension *ext)
|
|
{
|
|
if (ext)
|
|
return ext->ellipses;
|
|
return 0;
|
|
}
|
|
|
|
// from the front (high->low bits)
|
|
uint uper_get_ext_optional(UPER_Extension *ext, uint bit)
|
|
{
|
|
if (!ext || (bit>=ext->options))
|
|
return 0;
|
|
|
|
return (ext->value & 1<<(ext->options-bit));
|
|
}
|
|
|
|
uint uper_get_ext_skip(UPER_Stream *uper, UPER_Extension *ext)
|
|
{
|
|
if (!uper && !ext && !ext->length)
|
|
return 0;
|
|
|
|
int bits = ext->length;
|
|
|
|
// copy in
|
|
int sbit = uper->bit; // bit order in stream (not byte)
|
|
int ebit = (sbit+bits) % 8; // after last bit
|
|
|
|
// copy out
|
|
uper->ptr += (sbit + bits) / 8;
|
|
uper->bit = ebit;
|
|
uper->consumed += bits;
|
|
|
|
return bits;
|
|
}
|