Files
Cnomicon/src/libasn1/oer_get_ext.c

147 lines
4.1 KiB
C

/*****************************************************************************
* Copyright (C) 2004-2021
* ProbeStar Technical Systems, LLC
* All Rights Reserved. Proprietary and Confidential.
*============================================================================
*****************************************************************************/
/*****************************************************************************
* includes
*****************************************************************************/
#include "embtypes.h"
#include "asn1oer.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 oer_get_extension(OER_Stream *oer, OER_Extension *ext, uint noptions, uint ellipses)
{
if (!oer || !ext)
return 0;
ellipses &= 1;
if ((noptions+ellipses)==0) {
oer->error = 1;
return 0;
}
uint64_t value = oer_get_value(oer, ellipses+noptions);
uint nbits = ellipses + noptions;
uint shift = (64 - nbits) % 8;
if (_asn1_debug_on_ == 2)
printf("\tEXT_VALUE = %lx (%ld) >> %d\n", value, value, shift);
value >>= shift;
// mask
uint64_t mask = (0xffffffffffffffffULL >> (64 - nbits));
ext->value = value & mask;
if (ellipses) {
ext->ellipses = (ext->value >> noptions) & 1;
uint length;
oer_get_length(oer, &length);
ext->length = length;
if (_asn1_debug_on_)
printf("\tEXT_LEN = %d\n", ext->length);
// otherwise no ...
}
else {
ext->length = 0;
}
if (noptions) {
ext->options = noptions;
}
return ellipses + noptions;
}
uint oer_get_ext_length(OER_Stream *oer, OER_Extension *ext)
{
if (!ext->ellipses)
return 0;
uint length;
oer_get_length(oer, &length);
ext->length = length;
return ext->length;
}
// if ellipses extension available
uint oer_get_ext_ellipses(OER_Extension *ext)
{
if (ext)
return ext->ellipses;
return 0;
}
// from the front (high->low bits)
uint oer_get_ext_optional(OER_Extension *ext, uint bit)
{
if (!ext || (bit>=ext->options))
return 0;
return (ext->value & 1<<(ext->options-bit));
}
uint oer_get_ext_skip(OER_Stream *oer, OER_Extension *ext)
{
if (!oer && !ext && !ext->length)
return 0;
int bits = ext->length;
// copy in
int sbit = oer->bit; // bit order in stream (not byte)
int ebit = (sbit+bits) % 8; // after last bit
// copy out
oer->ptr += (sbit + bits) / 8;
oer->bit = ebit;
oer->consumed += bits;
return bits;
}