Initial commit of files
This commit is contained in:
65
src/libasn1/CMakeLists.txt
Normal file
65
src/libasn1/CMakeLists.txt
Normal file
@ -0,0 +1,65 @@
|
||||
cmake_minimum_required(VERSION 3.7)
|
||||
set(NAME asn1)
|
||||
set(TARGET lib${NAME})
|
||||
project(Cnomicon-${TARGET})
|
||||
set(CMAKE_INSTALL_PREFIX ..)
|
||||
set(LIBRARY ${TARGET})
|
||||
|
||||
# list of header files
|
||||
set(LIBH
|
||||
asn1common.h
|
||||
asn1oer.h
|
||||
asn1uper.h
|
||||
)
|
||||
|
||||
# list of source files
|
||||
set(LIBSRC
|
||||
asn1_debug.c
|
||||
oer_stream.c oer_decode.c oer_encode.c
|
||||
oer_get_bits.c oer_put_bits.c
|
||||
oer_get_ext.c oer_put_ext.c
|
||||
oer_get_octet.c oer_put_octet.c
|
||||
uper_stream.c uper_decode.c uper_encode.c
|
||||
uper_get_bits.c uper_put_bits.c
|
||||
uper_get_ext.c uper_put_ext.c
|
||||
uper_get_octet.c uper_put_octet.c
|
||||
)
|
||||
|
||||
# includes
|
||||
include_directories(
|
||||
../include
|
||||
)
|
||||
link_directories(../lib)
|
||||
|
||||
link_libraries(
|
||||
json-c.a
|
||||
pcap.a
|
||||
m
|
||||
)
|
||||
|
||||
# executables
|
||||
#add_executable(test_${NAME}_uper test_${NAME}_uper.c)
|
||||
|
||||
# includes
|
||||
include_directories(../include)
|
||||
|
||||
set(CMAKE_ARCHIVE_OUTPUT_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}/../lib)
|
||||
set(CMAKE_LIBRARY_OUTPUT_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}/../lib)
|
||||
|
||||
# this is the "object library" target: compiles the sources only once
|
||||
add_library(${LIBRARY} OBJECT ${LIBSRC})
|
||||
|
||||
# shared libraries need PIC
|
||||
set_property(TARGET ${LIBRARY} PROPERTY POSITION_INDEPENDENT_CODE 1)
|
||||
|
||||
# shared and static libraries built from the same object files
|
||||
add_library(${LIBRARY}-shared SHARED $<TARGET_OBJECTS:${LIBRARY}>)
|
||||
add_library(${LIBRARY}-static STATIC $<TARGET_OBJECTS:${LIBRARY}>)
|
||||
|
||||
# output file names
|
||||
set_target_properties(${LIBRARY}-shared PROPERTIES OUTPUT_NAME ${NAME})
|
||||
set_target_properties(${LIBRARY}-static PROPERTIES OUTPUT_NAME ${NAME})
|
||||
|
||||
# installations
|
||||
install (FILES ${LIBH} DESTINATION include)
|
||||
#install (FILES lib${NAME}.a lib${NAME}.so DESTINATION lib)
|
||||
18
src/libasn1/TODO
Normal file
18
src/libasn1/TODO
Normal file
@ -0,0 +1,18 @@
|
||||
|
||||
ASN.1 ToDo
|
||||
|
||||
Implementations added as needed
|
||||
|
||||
BER/DER
|
||||
As needed
|
||||
|
||||
OER
|
||||
Enumerated
|
||||
Real types
|
||||
|
||||
UPER
|
||||
Enumerated
|
||||
Real types
|
||||
|
||||
JSON/XER
|
||||
dunno
|
||||
56
src/libasn1/asn1_debug.c
Normal file
56
src/libasn1/asn1_debug.c
Normal file
@ -0,0 +1,56 @@
|
||||
/*****************************************************************************
|
||||
* Copyright (C) 2004-2021
|
||||
* ProbeStar Technical Systems, LLC
|
||||
* All Rights Reserved. Proprietary and Confidential.
|
||||
*============================================================================
|
||||
*****************************************************************************/
|
||||
|
||||
/*****************************************************************************
|
||||
* includes
|
||||
*****************************************************************************/
|
||||
|
||||
#include "embtypes.h"
|
||||
#include "asn1uper.h"
|
||||
|
||||
/*****************************************************************************
|
||||
* defines
|
||||
*****************************************************************************/
|
||||
|
||||
/*****************************************************************************
|
||||
* macros
|
||||
*****************************************************************************/
|
||||
|
||||
/*****************************************************************************
|
||||
* structs & typedefs
|
||||
*****************************************************************************/
|
||||
|
||||
/*****************************************************************************
|
||||
* global constants
|
||||
*****************************************************************************/
|
||||
|
||||
/*****************************************************************************
|
||||
* global variables
|
||||
*****************************************************************************/
|
||||
|
||||
/*****************************************************************************
|
||||
* static constants
|
||||
*****************************************************************************/
|
||||
|
||||
/*****************************************************************************
|
||||
* static variables
|
||||
*****************************************************************************/
|
||||
|
||||
uint8_t _asn1_debug_on_ = 0;
|
||||
|
||||
/*****************************************************************************
|
||||
* static prototypes
|
||||
*****************************************************************************/
|
||||
|
||||
/*****************************************************************************
|
||||
* C functions
|
||||
*****************************************************************************/
|
||||
|
||||
void asn1_set_debug(uint8_t flag)
|
||||
{
|
||||
_asn1_debug_on_ = flag;
|
||||
}
|
||||
77
src/libasn1/asn1common.h
Normal file
77
src/libasn1/asn1common.h
Normal file
@ -0,0 +1,77 @@
|
||||
#ifndef __ASN1COMMON_INCLUDE__
|
||||
#define __ASN1COMMON_INCLUDE__
|
||||
|
||||
/******************************************************************************
|
||||
* includes
|
||||
*****************************************************************************/
|
||||
|
||||
#include <stdio.h>
|
||||
#include <stdint.h>
|
||||
#include <sys/types.h>
|
||||
#include <memory.h>
|
||||
|
||||
#include "embtypes.h"
|
||||
|
||||
/******************************************************************************
|
||||
* defines
|
||||
*****************************************************************************/
|
||||
|
||||
#define ASN1_UPER_ALIGNED 0
|
||||
#define ASN1_APER_ALIGNED 1
|
||||
#define ASN1_OER_ALIGNED 2
|
||||
|
||||
#define asn1_get_error(S) ((S)->error)
|
||||
|
||||
#define asn1_get_bits_consumed(O) ((O)->consumed)
|
||||
#define asn1_get_bytes_consumed(O) ((O)->consumed?(((O)->consumed-1)/8+1):0)
|
||||
|
||||
#define asn1_init_extension(E) memset((E),0,sizeof(ASN1_Extension))
|
||||
#define asn1_init_bitstream(E) memset((E),0,sizeof(ASN1_BitStream))
|
||||
|
||||
/******************************************************************************
|
||||
* structs & typedefs
|
||||
*****************************************************************************/
|
||||
|
||||
typedef struct _asn1_stream {
|
||||
uint8_t *buf; // pointer to first byte
|
||||
uint8_t *ptr; // pointer to current byte
|
||||
uint8_t alloc; // set if alloc'd
|
||||
uint8_t align; // alignment
|
||||
uint8_t error; // set if error
|
||||
uint16_t bit; // current bit in byte
|
||||
uint16_t max; // maximum bits
|
||||
uint16_t consumed;// bits consumed
|
||||
} ASN1_Stream;
|
||||
|
||||
typedef struct _asn1_bitstream {
|
||||
uint64_t value; // value
|
||||
uint16_t max; // maximum bits
|
||||
} ASN1_BitStream;
|
||||
|
||||
typedef struct _asn1_extension {
|
||||
uint64_t value; // extension+option bits
|
||||
uint8_t ellipses; // 0 or 1
|
||||
uint8_t options; // # options
|
||||
uint16_t length; // length of extension
|
||||
} ASN1_Extension;
|
||||
|
||||
/******************************************************************************
|
||||
* function prototypes
|
||||
*****************************************************************************/
|
||||
/* export C functions to C++ */
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
void asn1_set_debug(uint8_t flag);
|
||||
extern uint8_t _asn1_debug_on_;
|
||||
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
/******************************************************************************
|
||||
* Macros
|
||||
* ***************************************************************************/
|
||||
|
||||
#endif
|
||||
111
src/libasn1/asn1oer.h
Normal file
111
src/libasn1/asn1oer.h
Normal file
@ -0,0 +1,111 @@
|
||||
#ifndef __ASN1OER_INCLUDE__
|
||||
#define __ASN1OER_INCLUDE__
|
||||
|
||||
/******************************************************************************
|
||||
* includes
|
||||
*****************************************************************************/
|
||||
|
||||
#include <stdio.h>
|
||||
#include <stdint.h>
|
||||
#include <sys/types.h>
|
||||
#include <memory.h>
|
||||
|
||||
#include "embtypes.h"
|
||||
#include "asn1common.h"
|
||||
|
||||
/******************************************************************************
|
||||
* defines
|
||||
*****************************************************************************/
|
||||
|
||||
#define oer_get_error(O) ((O)->error)
|
||||
#define oer_get_bytes_consumed(O) asn1_get_bytes_consumed(O)
|
||||
|
||||
#define oer_init_extension(E) asn1_init_extension(E)
|
||||
#define oer_init_bitstream(E) asn1_init_bitstream(E)
|
||||
|
||||
#define oer_get_signed_value(U,B,O) (((int64_t)uper_get_bits_stream(U,B))-O)
|
||||
#define oer_get_unsigned_value(U,B) uper_get_bits_stream(U,B)
|
||||
|
||||
#define oer_get_char_value(O) ((int8_t)(oer_get_value(O,1)))
|
||||
#define oer_get_int8_value(O) ((int8_t)(oer_get_value(O,1)))
|
||||
#define oer_get_byte_value(O) ((uint8_t)(oer_get_value(O,1)))
|
||||
#define oer_get_uint8_value(O) ((uint8_t)(oer_get_value(O,1)))
|
||||
|
||||
#define oer_get_short_value(O) ((int16_t)(oer_get_value(O,2)))
|
||||
#define oer_get_int16_value(O) ((int16_t)(oer_get_value(O,2)))
|
||||
#define oer_get_word_value(O) ((uint16_t)(oer_get_value(O,2)))
|
||||
#define oer_get_uint16_value(O) ((uint16_t)(oer_get_value(O,2)))
|
||||
|
||||
#define oer_get_long_value(O) ((int32_t)(oer_get_value(O,4)))
|
||||
#define oer_get_int32_value(O) ((int32_t)(oer_get_value(O,4)))
|
||||
#define oer_get_dword_value(O) ((uint32_t)(oer_get_value(O,4)))
|
||||
#define oer_get_uint32_value(O) ((uint32_t)(oer_get_value(O,4)))
|
||||
|
||||
#define oer_get_llong_value(O) ((int64_t)oer_get_value(O,8))
|
||||
#define oer_get_int64_value(O) ((int64_t)oer_get_value(O,8))
|
||||
#define oer_get_qword_value(O) ((uint64_t)oer_get_value(O,8))
|
||||
#define oer_get_uint64_value(O) ((uint64_t)oer_get_value(O,8))
|
||||
|
||||
#define oer_get_string(O,B,N,X) oer_get_octet_stream(O,B,N,X)
|
||||
#define oer_put_string(O,B,L,N,X) oer_put_octet_stream(O,B,L,N,X)
|
||||
|
||||
#define OER_Stream ASN1_Stream
|
||||
#define OER_Extension ASN1_Extension
|
||||
#define OER_BitStream ASN1_BitStream
|
||||
|
||||
/******************************************************************************
|
||||
* structs & typedefs
|
||||
*****************************************************************************/
|
||||
|
||||
/******************************************************************************
|
||||
* function prototypes
|
||||
*****************************************************************************/
|
||||
/* export C functions to C++ */
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
void oer_init_stream(OER_Stream *strm, uint8_t *ptr, uint max);
|
||||
OER_Stream *uper_alloc_stream(uint8_t *ptr, uint max);
|
||||
void oer_free_stream(OER_Stream *oer);
|
||||
|
||||
// DECODE
|
||||
uint oer_get_length(OER_Stream *oer, uint *value);
|
||||
uint64_t oer_get_value(OER_Stream *oer, uint size);
|
||||
uint oer_get_data(OER_Stream *oer, uint8_t *data, uint max);
|
||||
|
||||
// ENCODE
|
||||
uint oer_put_length(OER_Stream *oer, uint value);
|
||||
void oer_put_value(OER_Stream *oer, uint64_t value, uint size);
|
||||
void oer_put_data(OER_Stream *oer, uint8_t *data, uint length);
|
||||
|
||||
// EXTENSIONS
|
||||
uint oer_get_extension(OER_Stream *uper, OER_Extension *ext, uint nbits, uint ellipses);
|
||||
uint oer_get_ext_ellipses(OER_Extension *ext);
|
||||
uint oer_get_ext_optional(OER_Extension *ext, uint bit);
|
||||
uint oer_get_ext_length(OER_Stream *uper, OER_Extension *ext);
|
||||
uint oer_get_ext_skip(OER_Stream *uper, OER_Extension *ext);
|
||||
void oer_put_extension(OER_Stream *uper, OER_Extension *ext);
|
||||
void oer_set_ext_ellipses(OER_Extension *ext);
|
||||
void oer_set_ext_length(OER_Extension *ext, uint len);
|
||||
void oer_set_ext_optional(OER_Extension *ext, uint option, uint val, uint nbits);
|
||||
|
||||
// OCTET
|
||||
uint oer_get_octet_stream(OER_Stream *ptr, uint8_t *buf, uint min, uint max);
|
||||
void oer_put_octet_stream(OER_Stream *ptr, uint8_t *buf, uint len, uint min, uint max);
|
||||
|
||||
// BIT STREAM
|
||||
uint64_t oer_get_bitstream(OER_Stream *ptr, OER_BitStream *bs, uint max);
|
||||
uint64_t oer_get_bit_bitstream(OER_BitStream *bs, uint bit);
|
||||
void oer_put_bitstream(OER_Stream *ptr, OER_BitStream *bs);
|
||||
void oer_set_bit_bitstream(OER_BitStream *bs, uint bit, uint max);
|
||||
void oer_set_bits_bitstream(OER_BitStream *bs, uint64_t value, uint max);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
/******************************************************************************
|
||||
* Macros
|
||||
* ***************************************************************************/
|
||||
|
||||
#endif
|
||||
118
src/libasn1/asn1uper.h
Normal file
118
src/libasn1/asn1uper.h
Normal file
@ -0,0 +1,118 @@
|
||||
#ifndef __ASN1UPER_INCLUDE__
|
||||
#define __ASN1UPER_INCLUDE__
|
||||
|
||||
/******************************************************************************
|
||||
* includes
|
||||
*****************************************************************************/
|
||||
|
||||
#include <stdio.h>
|
||||
#include <stdint.h>
|
||||
#include <sys/types.h>
|
||||
#include <memory.h>
|
||||
|
||||
#include "embtypes.h"
|
||||
#include "asn1common.h"
|
||||
|
||||
/******************************************************************************
|
||||
* defines
|
||||
*****************************************************************************/
|
||||
|
||||
#define uper_get_error(S) ((S)->error)
|
||||
#define uper_get_bits_consumed(U) asn1_get_bits_consumed(U)
|
||||
#define uper_get_bytes_consumed(U) asn1_get_bytes_consumed(U)
|
||||
|
||||
#define uper_get_bits_length(U,S) (uper_get_bits_stream(U,S)+1)
|
||||
#define uper_put_bits_length(U,L,S) uper_put_bits_stream(U,(L)-1,S)
|
||||
|
||||
#define uper_init_extension(E) asn1_init_extension(E)
|
||||
#define uper_init_bitstream(E) asn1_init_bitstream(E)
|
||||
|
||||
#define uper_get_signed_value(U,B,O) (((int64_t)uper_get_bits_stream(U,B))-O)
|
||||
#define uper_get_unsigned_value(U,B) uper_get_bits_stream(U,B)
|
||||
|
||||
#define uper_get_char_value(O) ((int8_t)(uper_get_value(O,1)))
|
||||
#define uper_get_int8_value(O) ((int8_t)(uper_get_value(O,1)))
|
||||
#define uper_get_byte_value(O) ((uint8_t)(uper_get_value(O,1)))
|
||||
#define uper_get_uint8_value(O) ((uint8_t)(uper_get_value(O,1)))
|
||||
|
||||
#define uper_get_short_value(O) ((int16_t)(uper_get_value(O,2)))
|
||||
#define uper_get_int16_value(O) ((int16_t)(uper_get_value(O,2)))
|
||||
#define uper_get_word_value(O) ((uint16_t)(uper_get_value(O,2)))
|
||||
#define uper_get_uint16_value(O) ((uint16_t)(uper_get_value(O,2)))
|
||||
|
||||
#define uper_get_long_value(O) ((int32_t)(uper_get_value(O,4)))
|
||||
#define uper_get_int32_value(O) ((int32_t)(uper_get_value(O,4)))
|
||||
#define uper_get_dword_value(O) ((uint32_t)(uper_get_value(O,4)))
|
||||
#define uper_get_uint32_value(O) ((uint32_t)(uper_get_value(O,4)))
|
||||
|
||||
#define uper_get_llong_value(O) ((int64_t)uper_get_value(O,8))
|
||||
#define uper_get_int64_value(O) ((int64_t)uper_get_value(O,8))
|
||||
#define uper_get_qword_value(O) ((uint64_t)uper_get_value(O,8))
|
||||
#define uper_get_uint64_value(O) ((uint64_t)uper_get_value(O,8))
|
||||
|
||||
#define uper_get_string(O,B,N,X) uper_get_octet_stream(O,B,N,X)
|
||||
#define uper_put_string(O,B,L,N,X) uper_put_octet_stream(O,B,L,N,X)
|
||||
|
||||
#define UPER_Stream ASN1_Stream
|
||||
#define UPER_Extension ASN1_Extension
|
||||
#define UPER_BitStream ASN1_BitStream
|
||||
|
||||
/******************************************************************************
|
||||
* structs & typedefs
|
||||
*****************************************************************************/
|
||||
|
||||
/******************************************************************************
|
||||
* function prototypes
|
||||
*****************************************************************************/
|
||||
/* export C functions to C++ */
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
void uper_init_stream(UPER_Stream *strm, uint8_t *ptr, uint max);
|
||||
UPER_Stream *uper_alloc_stream(uint8_t *ptr, uint max);
|
||||
void uper_free_stream(UPER_Stream *oer);
|
||||
|
||||
// DECODE
|
||||
uint uper_get_bit_direct(const uint8_t *pkt, uint bit);
|
||||
uint64_t uper_get_bits_direct(const uint8_t *pkt, uint bit, uint bits);
|
||||
uint64_t uper_get_bits_stream(UPER_Stream *ptr, uint bits);
|
||||
uint64_t uper_get_bits_stream_reverse(UPER_Stream *ptr, uint nbits);
|
||||
uint uper_get_bytes_stream(UPER_Stream *uper, uint8_t *buf, uint nbytes);
|
||||
|
||||
// ENCODE
|
||||
void uper_put_bit_direct(uint8_t *pkt, uint val, uint bit);
|
||||
void uper_put_bits_direct(uint8_t *pkt, uint64_t value, uint bit, uint bits);
|
||||
void uper_put_bits_stream(UPER_Stream *uper, uint64_t value, uint bits);
|
||||
void uper_put_bytes_stream(UPER_Stream *uper, uint8_t *buf, uint nbytes);
|
||||
|
||||
// EXTENSIONS
|
||||
uint uper_get_extension(UPER_Stream *uper, UPER_Extension *ext, uint nbits, uint ellipses);
|
||||
uint uper_get_ext_ellipses(UPER_Extension *ext);
|
||||
uint uper_get_ext_optional(UPER_Extension *ext, uint bit);
|
||||
uint uper_get_ext_length(UPER_Stream *uper, UPER_Extension *ext);
|
||||
uint uper_get_ext_skip(UPER_Stream *uper, UPER_Extension *ext);
|
||||
void uper_put_extension(UPER_Stream *uper, UPER_Extension *ext);
|
||||
void uper_set_ext_ellipses(UPER_Extension *ext);
|
||||
void uper_set_ext_length(UPER_Extension *ext, uint len);
|
||||
void uper_set_ext_optional(UPER_Extension *ext, uint option, uint val, uint nbits);
|
||||
|
||||
// OCTET
|
||||
uint uper_get_octet_stream(UPER_Stream *ptr, uint8_t *buf, uint min, uint max);
|
||||
void uper_put_octet_stream(UPER_Stream *uper, uint8_t *buf, uint len, uint min, uint max);
|
||||
|
||||
// BIT STREAM
|
||||
uint64_t uper_get_bitstream(UPER_Stream *uper, UPER_BitStream *bs, uint max);
|
||||
uint64_t uper_get_bit_bitstream(UPER_BitStream *bs, uint bit);
|
||||
void uper_put_bitstream(UPER_Stream *uper, UPER_BitStream *bs);
|
||||
void uper_set_bit_bitstream(UPER_BitStream *bs, uint bit, uint max);
|
||||
void uper_set_bits_bitstream(UPER_BitStream *bs, uint64_t value, uint max);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
/******************************************************************************
|
||||
* Macros
|
||||
* ***************************************************************************/
|
||||
|
||||
#endif
|
||||
25
src/libasn1/makefile
Normal file
25
src/libasn1/makefile
Normal file
@ -0,0 +1,25 @@
|
||||
|
||||
ASN=asn1_debug.c
|
||||
OERS=oer_stream.c oer_decode.c oer_encode.c
|
||||
OERE=oer_put_ext.c oer_put_octet.c oer_put_bits.c
|
||||
OERD=oer_get_ext.c oer_get_octet.c oer_get_bits.c
|
||||
OER=$(ASN) $(OERS) $(OERE) $(OERD)
|
||||
UPERS=uper_stream.c uper_encode.c uper_decode.c
|
||||
UPERE=uper_put_ext.c uper_put_octet.c uper_put_bits.c
|
||||
UPERD=uper_get_ext.c uper_get_octet.c uper_get_bits.c
|
||||
UPER=$(ASN) $(UPERS) $(UPERE) $(UPERD)
|
||||
|
||||
OPTS=-g -Wall -I../include
|
||||
|
||||
TESTS=test_asn1_uper test_asn1_oer
|
||||
|
||||
all : $(TESTS)
|
||||
|
||||
test_asn1_uper : test_asn1_uper.c $(ASN) $(UPER) asn1common.h asn1uper.h
|
||||
$(CC) -o $@ $(OPTS) test_asn1_uper.c $(UPER)
|
||||
|
||||
test_asn1_oer : test_asn1_oer.c $(ASN) $(OER) asn1common.h asn1oer.h
|
||||
$(CC) -o $@ $(OPTS) test_asn1_oer.c $(OER)
|
||||
|
||||
clean : $(TESTS)
|
||||
-rm -f $(TESTS)
|
||||
116
src/libasn1/oer_decode.c
Normal file
116
src/libasn1/oer_decode.c
Normal file
@ -0,0 +1,116 @@
|
||||
/*****************************************************************************
|
||||
* 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
|
||||
*****************************************************************************/
|
||||
|
||||
// OER length
|
||||
uint oer_get_length(OER_Stream *oer, uint *value) {
|
||||
if (!oer)
|
||||
return 0;
|
||||
|
||||
int len = *oer->ptr++;
|
||||
int val = len & 0x7f;
|
||||
int size = 1;
|
||||
|
||||
// if length > 127
|
||||
if (len & 0x80) {
|
||||
// slurp
|
||||
val <<= 8;
|
||||
val |= *oer->ptr++;
|
||||
size = 2;
|
||||
}
|
||||
|
||||
if (value)
|
||||
*value = val;
|
||||
size *= 8;
|
||||
oer->consumed += size;
|
||||
return val;
|
||||
}
|
||||
|
||||
uint64_t oer_get_value(OER_Stream *oer, uint size)
|
||||
{
|
||||
if (!oer || !size)
|
||||
return 0;
|
||||
|
||||
if (oer->consumed + size >= oer->max) {
|
||||
oer->error = 1;
|
||||
return 0;
|
||||
}
|
||||
|
||||
// fix size to byte sized
|
||||
uint n = ((size-1)/8)+1;
|
||||
|
||||
uint64_t value = 0;
|
||||
|
||||
while (n--) {
|
||||
value <<= 8;
|
||||
value |= *oer->ptr++;
|
||||
}
|
||||
|
||||
// bit size
|
||||
oer->consumed += size;
|
||||
|
||||
return value;
|
||||
}
|
||||
|
||||
uint oer_get_data(OER_Stream *oer, uint8_t *data, uint length)
|
||||
{
|
||||
if (!oer || !oer->ptr || !data || !length)
|
||||
return 0;
|
||||
|
||||
oer->consumed += (length*8);
|
||||
uint len = length;
|
||||
while (len--) {
|
||||
*data++ = *oer->ptr++;
|
||||
}
|
||||
|
||||
return length;
|
||||
}
|
||||
102
src/libasn1/oer_encode.c
Normal file
102
src/libasn1/oer_encode.c
Normal file
@ -0,0 +1,102 @@
|
||||
/*****************************************************************************
|
||||
* 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
|
||||
*****************************************************************************/
|
||||
|
||||
// OER length
|
||||
uint oer_put_length(OER_Stream *oer, uint value)
|
||||
{
|
||||
if (!oer || !oer->ptr)
|
||||
return 0;
|
||||
|
||||
int size = 8;
|
||||
if (value > 0x7f) {
|
||||
*oer->ptr++ = (value >> 8) | 0x80;
|
||||
size = 16;
|
||||
}
|
||||
*oer->ptr++ = value & 0xff;
|
||||
|
||||
oer->consumed += size;
|
||||
return size;
|
||||
}
|
||||
|
||||
void oer_put_value(OER_Stream *oer, uint64_t value, uint size)
|
||||
{
|
||||
if (!oer || !oer->ptr || !size)
|
||||
return;
|
||||
|
||||
if (oer->consumed + size >= oer->max) {
|
||||
oer->error = 1;
|
||||
return;
|
||||
}
|
||||
|
||||
uint n = ((size-1)/8)+1;
|
||||
uint shift = (n-1) * 8;
|
||||
|
||||
while (n--) {
|
||||
*oer->ptr++ = (uint8_t)(value >> shift);
|
||||
shift -= 8;
|
||||
}
|
||||
|
||||
// bit size
|
||||
oer->consumed += size;
|
||||
}
|
||||
|
||||
void oer_put_data(OER_Stream *oer, uint8_t *data, uint length)
|
||||
{
|
||||
if (!oer || !oer->ptr || !data || !length)
|
||||
return;
|
||||
|
||||
oer->consumed += (length*8);
|
||||
while (length--) {
|
||||
*oer->ptr++ = *data++;
|
||||
}
|
||||
}
|
||||
71
src/libasn1/oer_get_bits.c
Normal file
71
src/libasn1/oer_get_bits.c
Normal file
@ -0,0 +1,71 @@
|
||||
/*****************************************************************************
|
||||
* 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
|
||||
*****************************************************************************/
|
||||
|
||||
uint64_t oer_get_bitstream(OER_Stream *oer, OER_BitStream *bs, uint max)
|
||||
{
|
||||
if (!oer || !bs || !max)
|
||||
return 0;
|
||||
|
||||
bs->value = oer_get_value(oer, max);
|
||||
bs->max = max;
|
||||
return max;
|
||||
}
|
||||
|
||||
// from the front (high->low bits)
|
||||
uint64_t oer_get_bit_bstream(OER_BitStream *bs, uint bit)
|
||||
{
|
||||
if (!bs || (bit>=bs->max))
|
||||
return 0;
|
||||
|
||||
return (bs->value & 1<<(bs->max-bit-1));
|
||||
}
|
||||
143
src/libasn1/oer_get_ext.c
Normal file
143
src/libasn1/oer_get_ext.c
Normal file
@ -0,0 +1,143 @@
|
||||
/*****************************************************************************
|
||||
* 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;
|
||||
|
||||
ext->length = oer_get_length(oer, NULL);
|
||||
|
||||
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 len;
|
||||
ext->length = oer_get_length(oer, &len);
|
||||
|
||||
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;
|
||||
}
|
||||
79
src/libasn1/oer_get_octet.c
Normal file
79
src/libasn1/oer_get_octet.c
Normal file
@ -0,0 +1,79 @@
|
||||
/*****************************************************************************
|
||||
* 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
|
||||
*****************************************************************************/
|
||||
|
||||
uint oer_get_octet_stream(OER_Stream *oer, uint8_t *buf, uint min, uint max)
|
||||
{
|
||||
if (!oer || !buf || !max)
|
||||
return 0;
|
||||
|
||||
// # bits
|
||||
uint num = 0;
|
||||
uint m = max;
|
||||
while (m) {
|
||||
m >>= 1;
|
||||
num++;
|
||||
}
|
||||
|
||||
uint len = max;
|
||||
|
||||
// # bytes
|
||||
if (min < max) {
|
||||
oer_get_length(oer, &len);
|
||||
}
|
||||
|
||||
if (_asn1_debug_on_)
|
||||
printf("\tGET_OCTET = bits=%d, len/max=%d/%d\n", num, len, max);
|
||||
|
||||
// slurp bytes
|
||||
return oer_get_data(oer, buf, len);
|
||||
}
|
||||
80
src/libasn1/oer_put_bits.c
Normal file
80
src/libasn1/oer_put_bits.c
Normal file
@ -0,0 +1,80 @@
|
||||
/*****************************************************************************
|
||||
* Copyright (C) 2004-2021
|
||||
* ProbeStar Technical Systems, LLC
|
||||
* All Rights Reserved. Proprietary and Confidential.
|
||||
*============================================================================
|
||||
*****************************************************************************/
|
||||
|
||||
/*****************************************************************************
|
||||
* includes
|
||||
*****************************************************************************/
|
||||
|
||||
#include "embtypes.h"
|
||||
#include "asn1oer.h"
|
||||
|
||||
/*****************************************************************************
|
||||
* defines
|
||||
*****************************************************************************/
|
||||
|
||||
/*****************************************************************************
|
||||
* macros
|
||||
*****************************************************************************/
|
||||
|
||||
/*****************************************************************************
|
||||
* structs & typedefs
|
||||
*****************************************************************************/
|
||||
|
||||
/*****************************************************************************
|
||||
* global constants
|
||||
*****************************************************************************/
|
||||
|
||||
/*****************************************************************************
|
||||
* global variables
|
||||
*****************************************************************************/
|
||||
|
||||
/*****************************************************************************
|
||||
* static constants
|
||||
*****************************************************************************/
|
||||
|
||||
/*****************************************************************************
|
||||
* static variables
|
||||
*****************************************************************************/
|
||||
|
||||
/*****************************************************************************
|
||||
* static prototypes
|
||||
*****************************************************************************/
|
||||
|
||||
/*****************************************************************************
|
||||
* C functions
|
||||
*****************************************************************************/
|
||||
|
||||
void oer_put_bitstream(OER_Stream *oer, OER_BitStream *bs)
|
||||
{
|
||||
if (!oer || !bs)
|
||||
return;
|
||||
|
||||
// limit 64 bits
|
||||
oer_put_value(oer, bs->value, bs->max);
|
||||
}
|
||||
|
||||
// from the front (high->low bits)
|
||||
void oer_set_bit_bitstream(OER_BitStream *bs, uint bit, uint max)
|
||||
{
|
||||
if (!bs || !max)
|
||||
return;
|
||||
bs->max = max;
|
||||
|
||||
if (bit)
|
||||
bs->value |= 1<<(bs->max-bit-1);
|
||||
else
|
||||
bs->value &= ~(1<<(bs->max-bit-1));
|
||||
}
|
||||
|
||||
void oer_set_bits_bitstream(OER_BitStream *bs, uint64_t value, uint max)
|
||||
{
|
||||
if (!bs)
|
||||
return;
|
||||
|
||||
bs->value = value;
|
||||
bs->max = max;
|
||||
}
|
||||
118
src/libasn1/oer_put_ext.c
Normal file
118
src/libasn1/oer_put_ext.c
Normal file
@ -0,0 +1,118 @@
|
||||
/*****************************************************************************
|
||||
* 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
|
||||
void oer_put_extension(OER_Stream *oer, OER_Extension *ext)
|
||||
{
|
||||
if (!oer || !ext)
|
||||
return;
|
||||
|
||||
if (ext->ellipses) {
|
||||
ext->value |= 1 << ext->options;
|
||||
}
|
||||
else {
|
||||
ext->length = 0;
|
||||
}
|
||||
|
||||
uint64_t value = ext->value;
|
||||
uint nbits = ext->ellipses + ext->options;
|
||||
uint shift = (64 - nbits) % 8;
|
||||
if (_asn1_debug_on_ == 2)
|
||||
printf("\tEXT_VALUE = %lx (%ld) << %d\n", value, value, shift);
|
||||
value <<= shift;
|
||||
|
||||
oer_put_value(oer, value, nbits);
|
||||
|
||||
if (ext->length) {
|
||||
uint len = ext->length & 0x7fff;
|
||||
if (_asn1_debug_on_)
|
||||
printf("\tEXT_LEN = %d\n", ext->length);
|
||||
oer_put_length(oer, len);
|
||||
}
|
||||
}
|
||||
|
||||
void oer_set_ext_length(OER_Extension *ext, uint len)
|
||||
{
|
||||
if (ext && len && len < 32768) {
|
||||
ext->ellipses = 1;
|
||||
ext->length = len & 0x7fff;
|
||||
}
|
||||
}
|
||||
|
||||
// if ellipses extension available
|
||||
void oer_set_ext_ellipses(OER_Extension *ext)
|
||||
{
|
||||
if (ext) {
|
||||
ext->ellipses = 1;
|
||||
ext->length = 0;
|
||||
}
|
||||
}
|
||||
|
||||
// from the front (high->low bits)
|
||||
void oer_set_ext_optional(OER_Extension *ext, uint option, uint val, uint nbits)
|
||||
{
|
||||
if (!ext)
|
||||
return;
|
||||
|
||||
if (!ext->options)
|
||||
ext->options = nbits;
|
||||
|
||||
// check
|
||||
if (option >= ext->options)
|
||||
return;
|
||||
|
||||
if (val)
|
||||
ext->value |= 1<<(ext->options-option-1);
|
||||
else
|
||||
ext->value &= ~(1<<(ext->options-option-1));
|
||||
}
|
||||
77
src/libasn1/oer_put_octet.c
Normal file
77
src/libasn1/oer_put_octet.c
Normal file
@ -0,0 +1,77 @@
|
||||
/*****************************************************************************
|
||||
* 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
|
||||
*****************************************************************************/
|
||||
|
||||
void oer_put_octet_stream(OER_Stream *oer, uint8_t *buf, uint len, uint min, uint max)
|
||||
{
|
||||
if (!oer || !buf || !max)
|
||||
return;
|
||||
|
||||
// # bits
|
||||
uint num = 0;
|
||||
uint m = max;
|
||||
while (m) {
|
||||
m >>= 1;
|
||||
num++;
|
||||
}
|
||||
|
||||
if (_asn1_debug_on_)
|
||||
printf("\tGET_OCTET = bits=%d, len/max=%d/%d\n", num, len, max);
|
||||
|
||||
// # bytes
|
||||
if (min < max) {
|
||||
oer_put_length(oer, len);
|
||||
}
|
||||
|
||||
// slurp bytes
|
||||
oer_put_data(oer, buf, len);
|
||||
}
|
||||
88
src/libasn1/oer_stream.c
Normal file
88
src/libasn1/oer_stream.c
Normal file
@ -0,0 +1,88 @@
|
||||
/*****************************************************************************
|
||||
* Copyright (C) 2004-2021
|
||||
* ProbeStar Technical Systems, LLC
|
||||
* All Rights Reserved. Proprietary and Confidential.
|
||||
*============================================================================
|
||||
*****************************************************************************/
|
||||
|
||||
/*****************************************************************************
|
||||
* includes
|
||||
*****************************************************************************/
|
||||
|
||||
#include <stdlib.h>
|
||||
|
||||
#include "embtypes.h"
|
||||
#include "asn1oer.h"
|
||||
|
||||
/*****************************************************************************
|
||||
* defines
|
||||
*****************************************************************************/
|
||||
|
||||
/*****************************************************************************
|
||||
* macros
|
||||
*****************************************************************************/
|
||||
|
||||
/*****************************************************************************
|
||||
* structs & typedefs
|
||||
*****************************************************************************/
|
||||
|
||||
/*****************************************************************************
|
||||
* global constants
|
||||
*****************************************************************************/
|
||||
|
||||
/*****************************************************************************
|
||||
* global variables
|
||||
*****************************************************************************/
|
||||
|
||||
/*****************************************************************************
|
||||
* static constants
|
||||
*****************************************************************************/
|
||||
|
||||
/*****************************************************************************
|
||||
* static variables
|
||||
*****************************************************************************/
|
||||
|
||||
/*****************************************************************************
|
||||
* static prototypes
|
||||
*****************************************************************************/
|
||||
|
||||
/*****************************************************************************
|
||||
* C functions
|
||||
*****************************************************************************/
|
||||
|
||||
void oer_init_stream(OER_Stream *strm, uint8_t *ptr, uint max)
|
||||
{
|
||||
if (strm && ptr && max) {
|
||||
strm->buf = ptr;
|
||||
strm->ptr = ptr;
|
||||
strm->align = ASN1_OER_ALIGNED;
|
||||
strm->bit = 0;
|
||||
strm->max = max;
|
||||
strm->consumed = 0;
|
||||
strm->error = 0;
|
||||
}
|
||||
}
|
||||
|
||||
OER_Stream *oer_alloc_stream(uint8_t *ptr, uint max)
|
||||
{
|
||||
OER_Stream *oer = (OER_Stream*)calloc(1, sizeof(OER_Stream));
|
||||
|
||||
if (oer) {
|
||||
if (!ptr && max) {
|
||||
ptr = (uint8_t*)calloc(max/8+1,1);
|
||||
oer->alloc = 1;
|
||||
}
|
||||
oer_init_stream(oer, ptr, max);
|
||||
}
|
||||
|
||||
return oer;
|
||||
}
|
||||
|
||||
void oer_free_stream(OER_Stream *oer)
|
||||
{
|
||||
if (oer) {
|
||||
if (oer->buf && oer->alloc)
|
||||
free(oer->buf);
|
||||
free(oer);
|
||||
}
|
||||
}
|
||||
BIN
src/libasn1/test_asn1_oer
Executable file
BIN
src/libasn1/test_asn1_oer
Executable file
Binary file not shown.
233
src/libasn1/test_asn1_oer.c
Normal file
233
src/libasn1/test_asn1_oer.c
Normal file
@ -0,0 +1,233 @@
|
||||
|
||||
#include "asn1oer.h"
|
||||
|
||||
|
||||
static uint8_t buffer[1500];
|
||||
|
||||
#define NUM_OPTIONS 5
|
||||
|
||||
void buffer_print(OER_Stream *strm, uint8_t *buf)
|
||||
{
|
||||
int n = ((strm->consumed-1) / 8) +1;
|
||||
printf("\tBUFFER[%d] = (", n);
|
||||
for (int i=0; i<n; i++)
|
||||
printf("%02x", buffer[i]);
|
||||
printf(")\n");
|
||||
}
|
||||
|
||||
void stream_print(OER_Stream *strm)
|
||||
{
|
||||
printf("Stream status: bit=%u, consumed=%u / max=%u\n", strm->bit, strm->consumed, strm->max);
|
||||
}
|
||||
|
||||
void opt_ext_print(OER_Extension *ext)
|
||||
{
|
||||
printf("Extension status: value=%lu (%ld) (0x%lx), options=%u, %s\n", ext->value, ext->value, ext->value, ext->options, ext->ellipses?"...":"");
|
||||
}
|
||||
|
||||
void opt_test(void)
|
||||
{
|
||||
memset(buffer, 0, sizeof(buffer));
|
||||
|
||||
OER_Stream strm_enc, strm_dec;
|
||||
oer_init_stream(&strm_enc, buffer, sizeof(buffer));
|
||||
oer_init_stream(&strm_dec, buffer, sizeof(buffer));
|
||||
|
||||
OER_Extension ext_enc, ext_dec;
|
||||
oer_init_extension(&ext_enc);
|
||||
oer_init_extension(&ext_dec);
|
||||
|
||||
printf("Options test:\n");
|
||||
|
||||
// encode
|
||||
oer_set_ext_optional(&ext_enc, 2, 1, 5);
|
||||
oer_put_extension(&strm_enc, &ext_enc);
|
||||
|
||||
printf("\tENCODED ");
|
||||
opt_ext_print(&ext_enc);
|
||||
printf("\tENCODED ");
|
||||
stream_print(&strm_enc);
|
||||
printf("\tENCODED");
|
||||
buffer_print(&strm_enc, buffer);
|
||||
|
||||
// decode
|
||||
oer_get_extension(&strm_dec, &ext_dec, NUM_OPTIONS, 0);
|
||||
|
||||
printf("\tDECODED ");
|
||||
opt_ext_print(&ext_dec);
|
||||
printf("\tDECODED ");
|
||||
stream_print(&strm_dec);
|
||||
|
||||
if (ext_enc.options == ext_dec.options && ext_enc.ellipses == ext_dec.ellipses && ext_enc.length == ext_dec.length && ext_enc.value == ext_dec.value)
|
||||
printf("\tPASSED\n");
|
||||
else
|
||||
printf("\tFAILED\n");
|
||||
}
|
||||
|
||||
void ext_test(void)
|
||||
{
|
||||
memset(buffer, 0, sizeof(buffer));
|
||||
|
||||
OER_Stream strm_enc, strm_dec;
|
||||
oer_init_stream(&strm_enc, buffer, sizeof(buffer));
|
||||
oer_init_stream(&strm_dec, buffer, sizeof(buffer));
|
||||
|
||||
OER_Extension ext_enc, ext_dec;
|
||||
oer_init_extension(&ext_enc);
|
||||
oer_init_extension(&ext_dec);
|
||||
|
||||
printf("Extensions test:\n");
|
||||
|
||||
// encode
|
||||
oer_set_ext_ellipses(&ext_enc);
|
||||
oer_set_ext_length(&ext_enc, 257);
|
||||
oer_put_extension(&strm_enc, &ext_enc);
|
||||
|
||||
printf("\tENCODED ");
|
||||
opt_ext_print(&ext_enc);
|
||||
printf("\tENCODED ");
|
||||
stream_print(&strm_enc);
|
||||
printf("\tENCODED");
|
||||
buffer_print(&strm_enc, buffer);
|
||||
|
||||
// decode
|
||||
oer_get_extension(&strm_dec, &ext_dec, 0, 1);
|
||||
|
||||
printf("\tDECODED ");
|
||||
opt_ext_print(&ext_dec);
|
||||
printf("\tDECODED ");
|
||||
stream_print(&strm_dec);
|
||||
|
||||
if (ext_enc.options == ext_dec.options && ext_enc.ellipses == ext_dec.ellipses && ext_enc.length == ext_dec.length && ext_enc.value == ext_dec.value)
|
||||
printf("\tPASSED\n");
|
||||
else
|
||||
printf("\tFAILED\n");
|
||||
}
|
||||
|
||||
void opt_ext_test(void)
|
||||
{
|
||||
memset(buffer, 0, sizeof(buffer));
|
||||
|
||||
OER_Stream strm_enc, strm_dec;
|
||||
oer_init_stream(&strm_enc, buffer, sizeof(buffer));
|
||||
oer_init_stream(&strm_dec, buffer, sizeof(buffer));
|
||||
|
||||
OER_Extension ext_enc, ext_dec;
|
||||
oer_init_extension(&ext_enc);
|
||||
oer_init_extension(&ext_dec);
|
||||
|
||||
printf("Extensions and Options test:\n");
|
||||
|
||||
// encode
|
||||
oer_set_ext_ellipses(&ext_enc);
|
||||
oer_set_ext_length(&ext_enc, 257);
|
||||
oer_set_ext_optional(&ext_enc, 2, 1, 5);
|
||||
oer_put_extension(&strm_enc, &ext_enc);
|
||||
|
||||
printf("\tENCODED ");
|
||||
opt_ext_print(&ext_enc);
|
||||
printf("\tENCODED ");
|
||||
stream_print(&strm_enc);
|
||||
printf("\tENCODED");
|
||||
buffer_print(&strm_enc, buffer);
|
||||
|
||||
// decode
|
||||
oer_get_extension(&strm_dec, &ext_dec, NUM_OPTIONS, 1);
|
||||
|
||||
printf("\tDECODED ");
|
||||
opt_ext_print(&ext_dec);
|
||||
printf("\tDECODED ");
|
||||
stream_print(&strm_dec);
|
||||
|
||||
if (ext_enc.options == ext_dec.options && ext_enc.ellipses == ext_dec.ellipses && ext_enc.length == ext_dec.length && ext_enc.value == ext_dec.value)
|
||||
printf("\tPASSED\n");
|
||||
else
|
||||
printf("\tFAILED\n");
|
||||
}
|
||||
|
||||
void octet_test(void)
|
||||
{
|
||||
memset(buffer, 0, sizeof(buffer));
|
||||
|
||||
OER_Stream strm_enc, strm_dec;
|
||||
oer_init_stream(&strm_enc, buffer, sizeof(buffer));
|
||||
oer_init_stream(&strm_dec, buffer, sizeof(buffer));
|
||||
|
||||
printf("Octet String test:\n");
|
||||
|
||||
uint8_t din[32];
|
||||
uint8_t dout[32];
|
||||
strcpy((char *)din, "0123456789ABCDEF");
|
||||
oer_put_octet_stream(&strm_enc, din, 16, 16, 32);
|
||||
|
||||
printf("\tENCODED");
|
||||
buffer_print(&strm_enc, buffer);
|
||||
|
||||
int n = oer_get_octet_stream(&strm_dec, dout, 16, 32);
|
||||
|
||||
printf("\tDATA[%d] = (", n);
|
||||
for (int i=0; i<n; i++)
|
||||
printf("%02x", dout[i]);
|
||||
printf(")\n");
|
||||
|
||||
if (memcmp(din, dout, 16) == 0)
|
||||
printf("\tPASSED\n");
|
||||
else
|
||||
printf("\tFAILED\n");
|
||||
}
|
||||
|
||||
void bitstream_test(void)
|
||||
{
|
||||
memset(buffer, 0, sizeof(buffer));
|
||||
|
||||
OER_Stream strm_enc, strm_dec;
|
||||
oer_init_stream(&strm_enc, buffer, sizeof(buffer));
|
||||
oer_init_stream(&strm_dec, buffer, sizeof(buffer));
|
||||
|
||||
OER_BitStream bs_enc, bs_dec;
|
||||
oer_init_bitstream(&bs_enc);
|
||||
oer_init_bitstream(&bs_dec);
|
||||
|
||||
printf("Bit Stream test:\n");
|
||||
oer_set_bits_bitstream(&bs_enc, 0xA5A500005A5AL, 24);
|
||||
oer_put_bitstream(&strm_enc, &bs_enc);
|
||||
|
||||
printf("\tENCODED ");
|
||||
stream_print(&strm_enc);
|
||||
printf("\tENCODED");
|
||||
buffer_print(&strm_enc, buffer);
|
||||
|
||||
int n = oer_get_bitstream(&strm_dec, &bs_dec, 24);
|
||||
printf("\tDECODED");
|
||||
buffer_print(&strm_dec, buffer);
|
||||
printf("\tDECODED BITSTREAM (%d bits) = %0lx\n", n, bs_dec.value);
|
||||
printf("\tDECODED ");
|
||||
stream_print(&strm_dec);
|
||||
|
||||
if ((bs_enc.value & 0x00ffffff) == bs_dec.value)
|
||||
printf("\tPASSED\n");
|
||||
else
|
||||
printf("\tFAILED\n");
|
||||
}
|
||||
|
||||
int main(int argc, char **argv)
|
||||
{
|
||||
asn1_set_debug(2);
|
||||
|
||||
// buffer test
|
||||
|
||||
// extension/options tests
|
||||
opt_test();
|
||||
ext_test();
|
||||
opt_ext_test();
|
||||
|
||||
// values test
|
||||
|
||||
// octet test
|
||||
octet_test();
|
||||
|
||||
// bitstream test
|
||||
bitstream_test();
|
||||
|
||||
return 0;
|
||||
}
|
||||
BIN
src/libasn1/test_asn1_uper
Executable file
BIN
src/libasn1/test_asn1_uper
Executable file
Binary file not shown.
235
src/libasn1/test_asn1_uper.c
Normal file
235
src/libasn1/test_asn1_uper.c
Normal file
@ -0,0 +1,235 @@
|
||||
|
||||
#include "asn1uper.h"
|
||||
|
||||
|
||||
static uint8_t buffer[1500];
|
||||
|
||||
#define NUM_OPTIONS 5
|
||||
|
||||
void buffer_print(UPER_Stream *strm, uint8_t *buf)
|
||||
{
|
||||
int n = ((strm->consumed-1) / 8) + 1;
|
||||
printf("\tBUFFER[%d] = (", n);
|
||||
for (int i=0; i<n; i++)
|
||||
printf("%02x", buffer[i]);
|
||||
printf(")\n");
|
||||
}
|
||||
|
||||
void stream_print(UPER_Stream *strm)
|
||||
{
|
||||
printf("Stream status: bit=%u, consumed=%u / max=%u\n", strm->bit, strm->consumed, strm->max);
|
||||
}
|
||||
|
||||
void opt_ext_print(UPER_Extension *ext)
|
||||
{
|
||||
printf("Extension status: value=%lu (%ld) (0x%lx), options=%u, %s\n", ext->value, ext->value, ext->value, ext->options, ext->ellipses?"...":"");
|
||||
}
|
||||
|
||||
void opt_test(void)
|
||||
{
|
||||
memset(buffer, 0, sizeof(buffer));
|
||||
|
||||
UPER_Stream strm_enc, strm_dec;
|
||||
uper_init_stream(&strm_enc, buffer, sizeof(buffer));
|
||||
uper_init_stream(&strm_dec, buffer, sizeof(buffer));
|
||||
|
||||
UPER_Extension ext_enc, ext_dec;
|
||||
uper_init_extension(&ext_enc);
|
||||
uper_init_extension(&ext_dec);
|
||||
|
||||
printf("Options test:\n");
|
||||
|
||||
// encode
|
||||
uper_set_ext_optional(&ext_enc, 2, 1, 5);
|
||||
uper_put_extension(&strm_enc, &ext_enc);
|
||||
|
||||
printf("\tENCODED ");
|
||||
opt_ext_print(&ext_enc);
|
||||
printf("\tENCODED ");
|
||||
stream_print(&strm_enc);
|
||||
printf("\tENCODED");
|
||||
buffer_print(&strm_enc, buffer);
|
||||
|
||||
// decode
|
||||
uper_get_extension(&strm_dec, &ext_dec, NUM_OPTIONS, 0);
|
||||
|
||||
printf("\tDECODED ");
|
||||
opt_ext_print(&ext_dec);
|
||||
printf("\tDECODED ");
|
||||
stream_print(&strm_dec);
|
||||
|
||||
if (ext_enc.options == ext_dec.options && ext_enc.ellipses == ext_dec.ellipses && ext_enc.length == ext_dec.length && ext_enc.value == ext_dec.value)
|
||||
printf("\tPASSED\n");
|
||||
else
|
||||
printf("\tFAILED\n");
|
||||
}
|
||||
|
||||
void ext_test(void)
|
||||
{
|
||||
memset(buffer, 0, sizeof(buffer));
|
||||
|
||||
UPER_Stream strm_enc, strm_dec;
|
||||
uper_init_stream(&strm_enc, buffer, sizeof(buffer));
|
||||
uper_init_stream(&strm_dec, buffer, sizeof(buffer));
|
||||
|
||||
UPER_Extension ext_enc, ext_dec;
|
||||
uper_init_extension(&ext_enc);
|
||||
uper_init_extension(&ext_dec);
|
||||
|
||||
printf("Extensions test:\n");
|
||||
|
||||
// encode
|
||||
uper_set_ext_ellipses(&ext_enc);
|
||||
uper_set_ext_length(&ext_enc, 257);
|
||||
uper_put_extension(&strm_enc, &ext_enc);
|
||||
|
||||
printf("\tENCODED ");
|
||||
opt_ext_print(&ext_enc);
|
||||
printf("\tENCODED ");
|
||||
stream_print(&strm_enc);
|
||||
printf("\tENCODED");
|
||||
buffer_print(&strm_enc, buffer);
|
||||
|
||||
// decode
|
||||
uper_get_extension(&strm_dec, &ext_dec, 0, 1);
|
||||
|
||||
printf("\tDECODED ");
|
||||
opt_ext_print(&ext_dec);
|
||||
printf("\tDECODED ");
|
||||
stream_print(&strm_dec);
|
||||
|
||||
if (ext_enc.options == ext_dec.options && ext_enc.ellipses == ext_dec.ellipses && ext_enc.length == ext_dec.length && ext_enc.value == ext_dec.value)
|
||||
printf("\tPASSED\n");
|
||||
else
|
||||
printf("\tFAILED\n");
|
||||
}
|
||||
|
||||
void opt_ext_test(void)
|
||||
{
|
||||
memset(buffer, 0, sizeof(buffer));
|
||||
|
||||
UPER_Stream strm_enc, strm_dec;
|
||||
uper_init_stream(&strm_enc, buffer, sizeof(buffer));
|
||||
uper_init_stream(&strm_dec, buffer, sizeof(buffer));
|
||||
|
||||
UPER_Extension ext_enc, ext_dec;
|
||||
uper_init_extension(&ext_enc);
|
||||
uper_init_extension(&ext_dec);
|
||||
|
||||
printf("Extensions and Options test:\n");
|
||||
|
||||
// encode
|
||||
uper_set_ext_ellipses(&ext_enc);
|
||||
uper_set_ext_length(&ext_enc, 257);
|
||||
uper_set_ext_optional(&ext_enc, 2, 1, 5);
|
||||
uper_put_extension(&strm_enc, &ext_enc);
|
||||
|
||||
printf("\tENCODED ");
|
||||
opt_ext_print(&ext_enc);
|
||||
printf("\tENCODED ");
|
||||
stream_print(&strm_enc);
|
||||
printf("\tENCODED");
|
||||
buffer_print(&strm_enc, buffer);
|
||||
|
||||
// decode
|
||||
uper_get_extension(&strm_dec, &ext_dec, NUM_OPTIONS, 1);
|
||||
|
||||
printf("\tDECODED ");
|
||||
opt_ext_print(&ext_dec);
|
||||
printf("\tDECODED ");
|
||||
stream_print(&strm_dec);
|
||||
|
||||
if (ext_enc.options == ext_dec.options && ext_enc.ellipses == ext_dec.ellipses && ext_enc.length == ext_dec.length && ext_enc.value == ext_dec.value)
|
||||
printf("\tPASSED\n");
|
||||
else
|
||||
printf("\tFAILED\n");
|
||||
}
|
||||
|
||||
void octet_test(void)
|
||||
{
|
||||
memset(buffer, 0, sizeof(buffer));
|
||||
|
||||
UPER_Stream strm_enc, strm_dec;
|
||||
uper_init_stream(&strm_enc, buffer, sizeof(buffer));
|
||||
uper_init_stream(&strm_dec, buffer, sizeof(buffer));
|
||||
|
||||
printf("Octet String test:\n");
|
||||
|
||||
uint8_t din[32];
|
||||
uint8_t dout[32];
|
||||
for (int i=0; i<16; i++)
|
||||
din[i] = 0x5a;
|
||||
// strcpy((char *)din, "0123456789ABCDEF");
|
||||
uper_put_octet_stream(&strm_enc, din, 16, 16, 32);
|
||||
|
||||
printf("\tENCODED");
|
||||
buffer_print(&strm_enc, buffer);
|
||||
|
||||
uint n = uper_get_octet_stream(&strm_dec, dout, 16, 32);
|
||||
|
||||
printf("\tDATA[%d] = (", n);
|
||||
for (int i=0; i<n; i++)
|
||||
printf("%02x", dout[i]);
|
||||
printf(")\n");
|
||||
|
||||
if (memcmp(din, dout, 16) == 0)
|
||||
printf("\tPASSED\n");
|
||||
else
|
||||
printf("\tFAILED\n");
|
||||
}
|
||||
|
||||
void bitstream_test(void)
|
||||
{
|
||||
memset(buffer, 0, sizeof(buffer));
|
||||
|
||||
UPER_Stream strm_enc, strm_dec;
|
||||
uper_init_stream(&strm_enc, buffer, sizeof(buffer));
|
||||
uper_init_stream(&strm_dec, buffer, sizeof(buffer));
|
||||
|
||||
UPER_BitStream bs_enc, bs_dec;
|
||||
uper_init_bitstream(&bs_enc);
|
||||
uper_init_bitstream(&bs_dec);
|
||||
|
||||
printf("Bit Stream test:\n");
|
||||
uper_set_bits_bitstream(&bs_enc, 0xA5A500005A5AL, 24);
|
||||
uper_put_bitstream(&strm_enc, &bs_enc);
|
||||
|
||||
printf("\tENCODED ");
|
||||
stream_print(&strm_enc);
|
||||
printf("\tENCODED");
|
||||
buffer_print(&strm_enc, buffer);
|
||||
|
||||
int n = uper_get_bitstream(&strm_dec, &bs_dec, 24);
|
||||
printf("\tDECODED");
|
||||
buffer_print(&strm_dec, buffer);
|
||||
printf("\tDECODED BITSTREAM (%d bits) = %0lx\n", n, bs_dec.value);
|
||||
printf("\tDECODED ");
|
||||
stream_print(&strm_dec);
|
||||
|
||||
if ((bs_enc.value & 0x00ffffff) == bs_dec.value)
|
||||
printf("\tPASSED\n");
|
||||
else
|
||||
printf("\tFAILED\n");
|
||||
}
|
||||
|
||||
int main(int argc, char **argv)
|
||||
{
|
||||
asn1_set_debug(0);
|
||||
|
||||
// buffer test
|
||||
|
||||
// extension/options tests
|
||||
opt_test();
|
||||
ext_test();
|
||||
opt_ext_test();
|
||||
|
||||
// values test
|
||||
|
||||
// octet test
|
||||
octet_test();
|
||||
|
||||
// bitstream test
|
||||
bitstream_test();
|
||||
|
||||
return 0;
|
||||
}
|
||||
232
src/libasn1/uper_decode.c
Normal file
232
src/libasn1/uper_decode.c
Normal file
@ -0,0 +1,232 @@
|
||||
/*****************************************************************************
|
||||
* 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 const uint8_t masks[9] = {
|
||||
0x00, 0x01, 0x03, 0x07, 0x0f, 0x1f, 0x3f, 0x7f, 0xff
|
||||
};
|
||||
|
||||
/*****************************************************************************
|
||||
* static variables
|
||||
*****************************************************************************/
|
||||
|
||||
/*****************************************************************************
|
||||
* static prototypes
|
||||
*****************************************************************************/
|
||||
|
||||
/*****************************************************************************
|
||||
* C functions
|
||||
*****************************************************************************/
|
||||
|
||||
uint uper_get_bit_direct(const uint8_t *pkt, uint bit)
|
||||
{
|
||||
if (!pkt)
|
||||
return 0;
|
||||
|
||||
pkt += (bit/8);
|
||||
return *pkt & (1 << (7-(bit%8)));
|
||||
}
|
||||
|
||||
// works well if arbitrarily peeking into the bit stream
|
||||
uint64_t uper_get_bits_direct(const uint8_t *pkt, uint bit, uint bits)
|
||||
{
|
||||
uint64_t value = 0;
|
||||
uint8_t val;
|
||||
int off;
|
||||
|
||||
if (!pkt || !bits)
|
||||
return 0;
|
||||
|
||||
// start byte from bit
|
||||
pkt += (bit/8);
|
||||
off = bit % 8; // bit offset
|
||||
if (_asn1_debug_on_ == 2)
|
||||
printf( "BIT_OFFSET = %d, %d:\n", bit, bits);
|
||||
|
||||
// grab the bits from byte array
|
||||
while (bits > 0) {
|
||||
// # bits to fetch
|
||||
int num = 8 - off;
|
||||
// # bits to extract
|
||||
int size = min(num, bits);
|
||||
|
||||
// get whole byte
|
||||
val = *pkt++;
|
||||
|
||||
// shift desired bits
|
||||
val >>= (num - size);
|
||||
|
||||
// mask desired bits
|
||||
val &= (0xff >> (8-size));
|
||||
|
||||
// update value
|
||||
value <<= size; // make room for new bits
|
||||
value |= val; // add them in
|
||||
if (_asn1_debug_on_)
|
||||
printf( "\tBIT INPUT = %d, %d, %d/%d, %d(0x%02x), %lu(0x%08x)\n", bit, off, num, bits, val, val, value, (unsigned)value);
|
||||
|
||||
bit += size;
|
||||
bits -= size;
|
||||
off = 0; // byte boundary from now on.
|
||||
}
|
||||
|
||||
return value;
|
||||
}
|
||||
|
||||
uint64_t uper_get_bits_stream(UPER_Stream *uper, uint nbits)
|
||||
{
|
||||
if (!uper || nbits > 64)
|
||||
return 0;
|
||||
|
||||
// check
|
||||
if (uper->consumed + nbits >= uper->max) {
|
||||
uper->error = 1;
|
||||
return 0;
|
||||
}
|
||||
|
||||
// copy in
|
||||
const uint8_t *raw = uper->ptr;
|
||||
uint sbit = uper->bit; // bit order in stream (not byte)
|
||||
uint ebit = (sbit+nbits) % 8; // after last bit
|
||||
uint64_t value = 0;
|
||||
|
||||
// # bytes to read
|
||||
uint n = 1 + ((sbit + nbits)/8);
|
||||
|
||||
if (_asn1_debug_on_ == 2)
|
||||
printf("\tGET_BITS_DETAIL = sbit=%d, ebit=%d, nbits=%d (%d) ", sbit, ebit, nbits, n);
|
||||
|
||||
if (nbits==1) {
|
||||
// speed short cut
|
||||
value = (*raw >> (8 - ebit)) & 1;
|
||||
}
|
||||
else {
|
||||
// read bytes
|
||||
while (n--) {
|
||||
value <<= 8;
|
||||
value |= *raw++;
|
||||
}
|
||||
|
||||
// shift
|
||||
uint shift = (8 - ebit);
|
||||
value >>= shift;
|
||||
|
||||
// mask
|
||||
uint64_t mask = (0xffffffffffffffffULL >> (64 - nbits));
|
||||
value &= mask;
|
||||
}
|
||||
|
||||
// copy out
|
||||
uper->ptr += (sbit + nbits)/8;
|
||||
uper->bit = ebit;
|
||||
uper->consumed += nbits;
|
||||
|
||||
if (_asn1_debug_on_)
|
||||
printf("\tGET_BITS = %08lx (%ld)\n", value, value);
|
||||
|
||||
return value;
|
||||
}
|
||||
|
||||
uint64_t uper_get_bits_stream_reverse(UPER_Stream *ptr, uint nbits)
|
||||
{
|
||||
uint64_t in = uper_get_bits_stream(ptr, nbits);
|
||||
uint64_t out = 0;
|
||||
uint64_t mask = 1;
|
||||
uint64_t set = 1 << (nbits-1);
|
||||
|
||||
while(nbits--) {
|
||||
if (in & mask)
|
||||
out |= set;
|
||||
mask <<= 1;
|
||||
set >>= 1;
|
||||
}
|
||||
|
||||
return out;
|
||||
}
|
||||
|
||||
uint uper_get_bytes_stream(UPER_Stream *uper, uint8_t *buf, uint nbytes)
|
||||
{
|
||||
if (!uper)
|
||||
return 0;
|
||||
uint8_t nbits = 8;
|
||||
|
||||
// check
|
||||
if (uper->consumed + nbits >= uper->max) {
|
||||
uper->error = 1;
|
||||
return 0;
|
||||
}
|
||||
|
||||
// copy in
|
||||
const uint8_t *raw = uper->ptr;
|
||||
uint sbit = uper->bit; // bit order in stream (not byte)
|
||||
uint ebit = (sbit+nbits) % 8; // after last bit
|
||||
|
||||
// # bytes to read
|
||||
uint n = 1 + ((sbit + nbits)/8);
|
||||
|
||||
if (_asn1_debug_on_ == 2)
|
||||
printf("\tGET_BYTES_DETAIL = sbit=%d, ebit=%d, nbits=%d (%d)\n", sbit, ebit, nbits, n);
|
||||
|
||||
// read
|
||||
uint ntotal = nbytes;
|
||||
while (nbytes--) {
|
||||
uint8_t value = 0;
|
||||
|
||||
if (n==1) {
|
||||
*buf++ = *raw++;
|
||||
}
|
||||
else {
|
||||
uint nhi = 8 - ebit;
|
||||
uint nlo = 8 - nhi;
|
||||
|
||||
value = (*raw++ & masks[nhi]) << (nlo);
|
||||
value |= (*raw >> (nhi)) & masks[nlo];
|
||||
|
||||
*buf++ = value;
|
||||
}
|
||||
}
|
||||
|
||||
// copy out
|
||||
uper->ptr += ntotal;
|
||||
uper->bit = ebit;
|
||||
uper->consumed += ntotal * nbits;
|
||||
|
||||
return ntotal;
|
||||
}
|
||||
206
src/libasn1/uper_encode.c
Normal file
206
src/libasn1/uper_encode.c
Normal file
@ -0,0 +1,206 @@
|
||||
/*****************************************************************************
|
||||
* 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
|
||||
*****************************************************************************/
|
||||
|
||||
void uper_put_bit_direct(uint8_t *pkt, uint val, uint bit)
|
||||
{
|
||||
if (!pkt)
|
||||
return;
|
||||
|
||||
pkt += (bit/8);
|
||||
|
||||
if (val)
|
||||
*pkt |= (1 << (7-(bit%8)));
|
||||
else
|
||||
*pkt &= ~((1 << (7-(bit%8))));
|
||||
}
|
||||
|
||||
// works well if arbitrarily poking into the bit stream
|
||||
void uper_put_bits_direct(uint8_t *pkt, uint64_t value, uint bit, uint bits)
|
||||
{
|
||||
uint8_t val;
|
||||
int off;
|
||||
|
||||
if (!pkt || !bits)
|
||||
return;
|
||||
|
||||
// start byte from bit
|
||||
pkt += (bit/8);
|
||||
off = bit % 8; // bit offset
|
||||
if (_asn1_debug_on_ == 2)
|
||||
printf( "BIT_OFFSET = %d(%d):\n", bit, bits);
|
||||
|
||||
// grab the bits from byte array
|
||||
while (bits > 0) {
|
||||
// # bits to fetch
|
||||
int num = 8 - off;
|
||||
// # bits to extract
|
||||
int size = min(num, bits);
|
||||
|
||||
// get whole byte
|
||||
val = *pkt;
|
||||
|
||||
// shift desired bits
|
||||
val >>= (num - size);
|
||||
|
||||
// mask desired bits
|
||||
val &= (0xff >> (8-size));
|
||||
|
||||
// set the byte
|
||||
*pkt++ |= val;
|
||||
|
||||
// update value
|
||||
if (_asn1_debug_on_)
|
||||
printf( "\tBIT OUTPUT = %d, %d, %d/%d, %d(0x%02x), %lu(0x%08lx)\n", bit, off, num, bits, val, val, value, value);
|
||||
|
||||
bit += size;
|
||||
bits -= size;
|
||||
off = 0; // byte boundary from now on.
|
||||
}
|
||||
}
|
||||
|
||||
void uper_put_bits_stream(UPER_Stream *uper, uint64_t value, uint nbits)
|
||||
{
|
||||
if (!uper || nbits > 64)
|
||||
return;
|
||||
|
||||
// check
|
||||
if (uper->consumed + nbits >= uper->max) {
|
||||
uper->error = 1;
|
||||
return;
|
||||
}
|
||||
|
||||
// copy in
|
||||
uint8_t *raw = uper->ptr;
|
||||
uint sbit = uper->bit; // bit order in stream (not byte)
|
||||
uint ebit = (sbit+nbits) % 8; // after last bit
|
||||
|
||||
// # bytes to write
|
||||
uint n = 1 + ((sbit + nbits)/8);
|
||||
|
||||
// shift
|
||||
uint shift = (8 - ebit);
|
||||
uint64_t ovalue = value;
|
||||
|
||||
if (_asn1_debug_on_ == 2)
|
||||
printf("\tPUT_BITS_DETAIL = sbit=%d, ebit=%d, nbits=%d (%d) ", sbit, ebit, nbits, n);
|
||||
if (_asn1_debug_on_)
|
||||
printf("\tPUT_BITS = %08lx (%ld) = %lx << %d\n", value, value, ovalue, shift);
|
||||
|
||||
// update
|
||||
if (nbits == 1) {
|
||||
// speed short cut
|
||||
*raw |= (value & 1) << (shift);
|
||||
}
|
||||
else {
|
||||
// shift up
|
||||
value <<= shift;
|
||||
shift = (n-1)*8;
|
||||
|
||||
for (int i=0 ; i<n ; i++) {
|
||||
*raw++ |= (uint8_t)(value >> shift);
|
||||
shift -= 8;
|
||||
}
|
||||
}
|
||||
|
||||
// copy out
|
||||
uper->ptr += (sbit + nbits)/8;
|
||||
uper->bit = ebit;
|
||||
uper->consumed += nbits;
|
||||
}
|
||||
|
||||
void uper_put_bytes_stream(UPER_Stream *uper, uint8_t *buf, uint nbytes)
|
||||
{
|
||||
if (!uper)
|
||||
return;
|
||||
uint8_t nbits = 8;
|
||||
|
||||
// check
|
||||
if (uper->consumed + nbits >= uper->max) {
|
||||
uper->error = 1;
|
||||
return;
|
||||
}
|
||||
|
||||
// copy in
|
||||
uint8_t *raw = uper->ptr;
|
||||
uint sbit = uper->bit; // bit order in stream (not byte)
|
||||
uint ebit = (sbit+nbits) % 8; // after last bit
|
||||
|
||||
// # bytes to write
|
||||
uint n = 1 + ((sbit + nbits)/8);
|
||||
|
||||
if (_asn1_debug_on_ == 2)
|
||||
printf("\tPUT_BYTES_DETAIL = sbit=%d, ebit=%d, nbits=%d (%d)\n", sbit, ebit, nbits, n);
|
||||
|
||||
int ntotal = nbytes;
|
||||
while (nbytes--) {
|
||||
uint8_t value = *buf++;
|
||||
|
||||
// if (_asn1_debug_on_)
|
||||
// printf("\tPUT_BYTE = %02x (%d)\n", value, value);
|
||||
|
||||
// update
|
||||
if (n==1) {
|
||||
*raw++ = value;
|
||||
}
|
||||
else {
|
||||
*raw++ |= value >> sbit;
|
||||
if (n>1)
|
||||
*raw = value << (8 - sbit);
|
||||
}
|
||||
}
|
||||
|
||||
// copy out
|
||||
uper->ptr += ntotal;
|
||||
uper->bit = ebit;
|
||||
uper->consumed += ntotal * nbits;
|
||||
}
|
||||
71
src/libasn1/uper_get_bits.c
Normal file
71
src/libasn1/uper_get_bits.c
Normal file
@ -0,0 +1,71 @@
|
||||
/*****************************************************************************
|
||||
* 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
|
||||
*****************************************************************************/
|
||||
|
||||
uint64_t uper_get_bitstream(UPER_Stream *uper, UPER_BitStream *bs, uint max)
|
||||
{
|
||||
if (!uper || !bs || !max)
|
||||
return 0;
|
||||
|
||||
bs->value = uper_get_bits_stream(uper, max);
|
||||
bs->max = max;
|
||||
return max;
|
||||
}
|
||||
|
||||
// from the front (high->low bits)
|
||||
uint64_t uper_get_bit_bstream(UPER_BitStream *bs, uint bit)
|
||||
{
|
||||
if (!bs || (bit>=bs->max))
|
||||
return 0;
|
||||
|
||||
return (bs->value & 1<<(bs->max-bit-1));
|
||||
}
|
||||
142
src/libasn1/uper_get_ext.c
Normal file
142
src/libasn1/uper_get_ext.c
Normal file
@ -0,0 +1,142 @@
|
||||
/*****************************************************************************
|
||||
* 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;
|
||||
}
|
||||
79
src/libasn1/uper_get_octet.c
Normal file
79
src/libasn1/uper_get_octet.c
Normal file
@ -0,0 +1,79 @@
|
||||
/*****************************************************************************
|
||||
* 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
|
||||
*****************************************************************************/
|
||||
|
||||
uint uper_get_octet_stream(UPER_Stream *uper, uint8_t *buf, uint min, uint max)
|
||||
{
|
||||
if (!uper || !buf || !max)
|
||||
return 0;
|
||||
|
||||
// # bits
|
||||
uint num = 0;
|
||||
uint m = max;
|
||||
while (m) {
|
||||
m >>= 1;
|
||||
num++;
|
||||
}
|
||||
|
||||
uint len = max;
|
||||
|
||||
// # bytes
|
||||
if (min < max) {
|
||||
len = uper_get_bits_length(uper, num);
|
||||
}
|
||||
|
||||
if (_asn1_debug_on_)
|
||||
printf("\tGET_OCTET = bits=%d, len/max=%d/%d\n", num, len, max);
|
||||
|
||||
// slurp bytes
|
||||
return uper_get_bytes_stream(uper, buf, len);
|
||||
}
|
||||
80
src/libasn1/uper_put_bits.c
Normal file
80
src/libasn1/uper_put_bits.c
Normal file
@ -0,0 +1,80 @@
|
||||
/*****************************************************************************
|
||||
* Copyright (C) 2004-2021
|
||||
* ProbeStar Technical Systems, LLC
|
||||
* All Rights Reserved. Proprietary and Confidential.
|
||||
*============================================================================
|
||||
*****************************************************************************/
|
||||
|
||||
/*****************************************************************************
|
||||
* includes
|
||||
*****************************************************************************/
|
||||
|
||||
#include "embtypes.h"
|
||||
#include "asn1uper.h"
|
||||
|
||||
/*****************************************************************************
|
||||
* defines
|
||||
*****************************************************************************/
|
||||
|
||||
/*****************************************************************************
|
||||
* macros
|
||||
*****************************************************************************/
|
||||
|
||||
/*****************************************************************************
|
||||
* structs & typedefs
|
||||
*****************************************************************************/
|
||||
|
||||
/*****************************************************************************
|
||||
* global constants
|
||||
*****************************************************************************/
|
||||
|
||||
/*****************************************************************************
|
||||
* global variables
|
||||
*****************************************************************************/
|
||||
|
||||
/*****************************************************************************
|
||||
* static constants
|
||||
*****************************************************************************/
|
||||
|
||||
/*****************************************************************************
|
||||
* static variables
|
||||
*****************************************************************************/
|
||||
|
||||
/*****************************************************************************
|
||||
* static prototypes
|
||||
*****************************************************************************/
|
||||
|
||||
/*****************************************************************************
|
||||
* C functions
|
||||
*****************************************************************************/
|
||||
|
||||
void uper_put_bitstream(UPER_Stream *uper, UPER_BitStream *bs)
|
||||
{
|
||||
if (!uper || !bs)
|
||||
return;
|
||||
|
||||
// limit 64 bits
|
||||
uper_put_bits_stream(uper, bs->value, bs->max);
|
||||
}
|
||||
|
||||
// from the front (high->low bits)
|
||||
void uper_set_bit_bitstream(UPER_BitStream *bs, uint bit, uint max)
|
||||
{
|
||||
if (!bs || !max)
|
||||
return;
|
||||
bs->max = max;
|
||||
|
||||
if (bit)
|
||||
bs->value |= 1<<(bs->max-bit-1);
|
||||
else
|
||||
bs->value &= ~(1<<(bs->max-bit-1));
|
||||
}
|
||||
|
||||
void uper_set_bits_bitstream(UPER_BitStream *bs, uint64_t value, uint max)
|
||||
{
|
||||
if (!bs)
|
||||
return;
|
||||
|
||||
bs->value = value;
|
||||
bs->max = max;
|
||||
}
|
||||
118
src/libasn1/uper_put_ext.c
Normal file
118
src/libasn1/uper_put_ext.c
Normal file
@ -0,0 +1,118 @@
|
||||
/*****************************************************************************
|
||||
* 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
|
||||
void uper_put_extension(UPER_Stream *uper, UPER_Extension *ext)
|
||||
{
|
||||
if (!uper || !ext)
|
||||
return;
|
||||
|
||||
if (ext->ellipses) {
|
||||
uper_put_bits_stream(uper, 1, 1);
|
||||
}
|
||||
else {
|
||||
ext->length = 0;
|
||||
}
|
||||
|
||||
if (ext->options) {
|
||||
uper_put_bits_stream(uper, ext->value, ext->options);
|
||||
}
|
||||
|
||||
if (ext->length) {
|
||||
uint len = ext->length & 0x7fff;
|
||||
if (_asn1_debug_on_)
|
||||
printf("\tEXT_LEN = %d\n", ext->length);
|
||||
if (len >= 128) {
|
||||
uper_put_bits_stream(uper, 0x8000 | len, 16);
|
||||
}
|
||||
else {
|
||||
uper_put_bits_stream(uper, len & 0x7f, 8);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void uper_set_ext_length(UPER_Extension *ext, uint len)
|
||||
{
|
||||
if (ext && len && len < 32768) {
|
||||
ext->ellipses = 1;
|
||||
ext->length = len & 0x7fff;
|
||||
}
|
||||
}
|
||||
|
||||
// if ellipses extension available
|
||||
void uper_set_ext_ellipses(UPER_Extension *ext)
|
||||
{
|
||||
if (ext) {
|
||||
ext->ellipses = 1;
|
||||
ext->length = 0;
|
||||
}
|
||||
}
|
||||
|
||||
// from the front (high->low bits)
|
||||
void uper_set_ext_optional(UPER_Extension *ext, uint option, uint val, uint nbits)
|
||||
{
|
||||
if (!ext)
|
||||
return;
|
||||
|
||||
if (!ext->options)
|
||||
ext->options = nbits;
|
||||
|
||||
// check
|
||||
if (option >= ext->options)
|
||||
return;
|
||||
|
||||
if (val)
|
||||
ext->value |= 1<<(ext->options-option-1);
|
||||
else
|
||||
ext->value &= ~(1<<(ext->options-option-1));
|
||||
}
|
||||
77
src/libasn1/uper_put_octet.c
Normal file
77
src/libasn1/uper_put_octet.c
Normal file
@ -0,0 +1,77 @@
|
||||
/*****************************************************************************
|
||||
* 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
|
||||
*****************************************************************************/
|
||||
|
||||
void uper_put_octet_stream(UPER_Stream *uper, uint8_t *buf, uint len, uint min, uint max)
|
||||
{
|
||||
if (!uper || !buf || !max)
|
||||
return;
|
||||
|
||||
// # bits
|
||||
uint num = 0;
|
||||
uint m = max;
|
||||
while (m) {
|
||||
m >>= 1;
|
||||
num++;
|
||||
}
|
||||
|
||||
if (_asn1_debug_on_)
|
||||
printf("\tGET_OCTET = bits=%d, len/max=%d/%d\n", num, len, max);
|
||||
|
||||
// # bytes
|
||||
if (min < max) {
|
||||
uper_put_bits_length(uper, len, num);
|
||||
}
|
||||
|
||||
// slurp bytes
|
||||
uper_put_bytes_stream(uper, buf, len);
|
||||
}
|
||||
88
src/libasn1/uper_stream.c
Normal file
88
src/libasn1/uper_stream.c
Normal file
@ -0,0 +1,88 @@
|
||||
/*****************************************************************************
|
||||
* Copyright (C) 2004-2021
|
||||
* ProbeStar Technical Systems, LLC
|
||||
* All Rights Reserved. Proprietary and Confidential.
|
||||
*============================================================================
|
||||
*****************************************************************************/
|
||||
|
||||
/*****************************************************************************
|
||||
* includes
|
||||
*****************************************************************************/
|
||||
|
||||
#include <stdlib.h>
|
||||
|
||||
#include "embtypes.h"
|
||||
#include "asn1uper.h"
|
||||
|
||||
/*****************************************************************************
|
||||
* defines
|
||||
*****************************************************************************/
|
||||
|
||||
/*****************************************************************************
|
||||
* macros
|
||||
*****************************************************************************/
|
||||
|
||||
/*****************************************************************************
|
||||
* structs & typedefs
|
||||
*****************************************************************************/
|
||||
|
||||
/*****************************************************************************
|
||||
* global constants
|
||||
*****************************************************************************/
|
||||
|
||||
/*****************************************************************************
|
||||
* global variables
|
||||
*****************************************************************************/
|
||||
|
||||
/*****************************************************************************
|
||||
* static constants
|
||||
*****************************************************************************/
|
||||
|
||||
/*****************************************************************************
|
||||
* static variables
|
||||
*****************************************************************************/
|
||||
|
||||
/*****************************************************************************
|
||||
* static prototypes
|
||||
*****************************************************************************/
|
||||
|
||||
/*****************************************************************************
|
||||
* C functions
|
||||
*****************************************************************************/
|
||||
|
||||
void uper_init_stream(UPER_Stream *strm, uint8_t *ptr, uint max)
|
||||
{
|
||||
if (strm && ptr && max) {
|
||||
strm->buf = ptr;
|
||||
strm->ptr = ptr;
|
||||
strm->align = ASN1_UPER_ALIGNED;
|
||||
strm->bit = 0;
|
||||
strm->max = max;
|
||||
strm->consumed = 0;
|
||||
strm->error = 0;
|
||||
}
|
||||
}
|
||||
|
||||
UPER_Stream *uper_alloc_stream(uint8_t *ptr, uint max)
|
||||
{
|
||||
UPER_Stream *uper = (UPER_Stream*)calloc(1, sizeof(UPER_Stream));
|
||||
|
||||
if (uper) {
|
||||
if (!ptr && max) {
|
||||
ptr = (uint8_t*)calloc(max/8+1,1);
|
||||
uper->alloc = 1;
|
||||
}
|
||||
uper_init_stream(uper, ptr, max);
|
||||
}
|
||||
|
||||
return uper;
|
||||
}
|
||||
|
||||
void uper_free_stream(UPER_Stream *uper)
|
||||
{
|
||||
if (uper) {
|
||||
if (uper->buf && uper->alloc)
|
||||
free(uper->buf);
|
||||
free(uper);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user