ASN.1
authorVolker Birk <vb@pep-project.org>
Sun, 15 May 2016 22:29:04 +0200
changeset 6287a1615476a52
parent 627 ef1fdfde8ced
child 629 4d95caf6d119
ASN.1
Makefile.conf
asn.1/BIT_STRING.c
asn.1/BIT_STRING.h
asn.1/Header.c
asn.1/Header.h
asn.1/INTEGER.c
asn.1/INTEGER.h
asn.1/Identity.c
asn.1/Identity.h
asn.1/Makefile
asn.1/Makefile.am.sample
asn.1/NativeEnumerated.c
asn.1/NativeEnumerated.h
asn.1/NativeInteger.c
asn.1/NativeInteger.h
asn.1/OCTET_STRING.c
asn.1/OCTET_STRING.h
asn.1/PrintableString.c
asn.1/PrintableString.h
asn.1/Stringlist.c
asn.1/Stringlist.h
asn.1/UTF8String.c
asn.1/UTF8String.h
asn.1/Version.c
asn.1/Version.h
asn.1/asn_SEQUENCE_OF.c
asn.1/asn_SEQUENCE_OF.h
asn.1/asn_SET_OF.c
asn.1/asn_SET_OF.h
asn.1/asn_application.h
asn.1/asn_codecs.h
asn.1/asn_codecs_prim.c
asn.1/asn_codecs_prim.h
asn.1/asn_internal.h
asn.1/asn_system.h
asn.1/ber_decoder.c
asn.1/ber_decoder.h
asn.1/ber_tlv_length.c
asn.1/ber_tlv_length.h
asn.1/ber_tlv_tag.c
asn.1/ber_tlv_tag.h
asn.1/constr_SEQUENCE.c
asn.1/constr_SEQUENCE.h
asn.1/constr_SEQUENCE_OF.c
asn.1/constr_SEQUENCE_OF.h
asn.1/constr_SET_OF.c
asn.1/constr_SET_OF.h
asn.1/constr_TYPE.c
asn.1/constr_TYPE.h
asn.1/constraints.c
asn.1/constraints.h
asn.1/der_encoder.c
asn.1/der_encoder.h
asn.1/devicegroup.asn1
asn.1/per_decoder.c
asn.1/per_decoder.h
asn.1/per_encoder.c
asn.1/per_encoder.h
asn.1/per_opentype.c
asn.1/per_opentype.h
asn.1/per_support.c
asn.1/per_support.h
asn.1/xer_decoder.c
asn.1/xer_decoder.h
asn.1/xer_encoder.c
asn.1/xer_encoder.h
asn.1/xer_support.c
asn.1/xer_support.h
     1.1 --- a/Makefile.conf	Sun May 15 21:23:36 2016 +0200
     1.2 +++ b/Makefile.conf	Sun May 15 22:29:04 2016 +0200
     1.3 @@ -10,6 +10,8 @@
     1.4  PREFIX=$(HOME)
     1.5  YML2PROC=$(HOME)/yml2/yml2proc
     1.6  YML_HOME=$(HOME)
     1.7 +ASN1C=asn1c
     1.8 +ASN1C_INCLUDE=/opt/local/share/asn1c
     1.9  
    1.10  # C makros (not environment variables) to overwrite:
    1.11  #
     2.1 --- a/asn.1/BIT_STRING.c	Sun May 15 21:23:36 2016 +0200
     2.2 +++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
     2.3 @@ -1,189 +0,0 @@
     2.4 -/*-
     2.5 - * Copyright (c) 2003, 2004 Lev Walkin <vlm@lionet.info>. All rights reserved.
     2.6 - * Redistribution and modifications are permitted subject to BSD license.
     2.7 - */
     2.8 -#include <asn_internal.h>
     2.9 -#include <BIT_STRING.h>
    2.10 -#include <asn_internal.h>
    2.11 -
    2.12 -/*
    2.13 - * BIT STRING basic type description.
    2.14 - */
    2.15 -static ber_tlv_tag_t asn_DEF_BIT_STRING_tags[] = {
    2.16 -	(ASN_TAG_CLASS_UNIVERSAL | (3 << 2))
    2.17 -};
    2.18 -static asn_OCTET_STRING_specifics_t asn_DEF_BIT_STRING_specs = {
    2.19 -	sizeof(BIT_STRING_t),
    2.20 -	offsetof(BIT_STRING_t, _asn_ctx),
    2.21 -	ASN_OSUBV_BIT
    2.22 -};
    2.23 -asn_TYPE_descriptor_t asn_DEF_BIT_STRING = {
    2.24 -	"BIT STRING",
    2.25 -	"BIT_STRING",
    2.26 -	OCTET_STRING_free,         /* Implemented in terms of OCTET STRING */
    2.27 -	BIT_STRING_print,
    2.28 -	BIT_STRING_constraint,
    2.29 -	OCTET_STRING_decode_ber,   /* Implemented in terms of OCTET STRING */
    2.30 -	OCTET_STRING_encode_der,   /* Implemented in terms of OCTET STRING */
    2.31 -	OCTET_STRING_decode_xer_binary,
    2.32 -	BIT_STRING_encode_xer,
    2.33 -	OCTET_STRING_decode_uper,	/* Unaligned PER decoder */
    2.34 -	OCTET_STRING_encode_uper,	/* Unaligned PER encoder */
    2.35 -	0, /* Use generic outmost tag fetcher */
    2.36 -	asn_DEF_BIT_STRING_tags,
    2.37 -	sizeof(asn_DEF_BIT_STRING_tags)
    2.38 -	  / sizeof(asn_DEF_BIT_STRING_tags[0]),
    2.39 -	asn_DEF_BIT_STRING_tags,	/* Same as above */
    2.40 -	sizeof(asn_DEF_BIT_STRING_tags)
    2.41 -	  / sizeof(asn_DEF_BIT_STRING_tags[0]),
    2.42 -	0,	/* No PER visible constraints */
    2.43 -	0, 0,	/* No members */
    2.44 -	&asn_DEF_BIT_STRING_specs
    2.45 -};
    2.46 -
    2.47 -/*
    2.48 - * BIT STRING generic constraint.
    2.49 - */
    2.50 -int
    2.51 -BIT_STRING_constraint(asn_TYPE_descriptor_t *td, const void *sptr,
    2.52 -		asn_app_constraint_failed_f *ctfailcb, void *app_key) {
    2.53 -	const BIT_STRING_t *st = (const BIT_STRING_t *)sptr;
    2.54 -
    2.55 -	if(st && st->buf) {
    2.56 -		if((st->size == 0 && st->bits_unused)
    2.57 -		|| st->bits_unused < 0 || st->bits_unused > 7) {
    2.58 -			_ASN_CTFAIL(app_key, td, sptr,
    2.59 -				"%s: invalid padding byte (%s:%d)",
    2.60 -				td->name, __FILE__, __LINE__);
    2.61 -			return -1;
    2.62 -		}
    2.63 -	} else {
    2.64 -		_ASN_CTFAIL(app_key, td, sptr,
    2.65 -			"%s: value not given (%s:%d)",
    2.66 -			td->name, __FILE__, __LINE__);
    2.67 -		return -1;
    2.68 -	}
    2.69 -
    2.70 -	return 0;
    2.71 -}
    2.72 -
    2.73 -static char *_bit_pattern[16] = {
    2.74 -	"0000", "0001", "0010", "0011", "0100", "0101", "0110", "0111",
    2.75 -	"1000", "1001", "1010", "1011", "1100", "1101", "1110", "1111"
    2.76 -};
    2.77 -
    2.78 -asn_enc_rval_t
    2.79 -BIT_STRING_encode_xer(asn_TYPE_descriptor_t *td, void *sptr,
    2.80 -	int ilevel, enum xer_encoder_flags_e flags,
    2.81 -		asn_app_consume_bytes_f *cb, void *app_key) {
    2.82 -	asn_enc_rval_t er;
    2.83 -	char scratch[128];
    2.84 -	char *p = scratch;
    2.85 -	char *scend = scratch + (sizeof(scratch) - 10);
    2.86 -	const BIT_STRING_t *st = (const BIT_STRING_t *)sptr;
    2.87 -	int xcan = (flags & XER_F_CANONICAL);
    2.88 -	uint8_t *buf;
    2.89 -	uint8_t *end;
    2.90 -
    2.91 -	if(!st || !st->buf)
    2.92 -		_ASN_ENCODE_FAILED;
    2.93 -
    2.94 -	er.encoded = 0;
    2.95 -
    2.96 -	buf = st->buf;
    2.97 -	end = buf + st->size - 1;	/* Last byte is special */
    2.98 -
    2.99 -	/*
   2.100 -	 * Binary dump
   2.101 -	 */
   2.102 -	for(; buf < end; buf++) {
   2.103 -		int v = *buf;
   2.104 -		int nline = xcan?0:(((buf - st->buf) % 8) == 0);
   2.105 -		if(p >= scend || nline) {
   2.106 -			er.encoded += p - scratch;
   2.107 -			_ASN_CALLBACK(scratch, p - scratch);
   2.108 -			p = scratch;
   2.109 -			if(nline) _i_ASN_TEXT_INDENT(1, ilevel);
   2.110 -		}
   2.111 -		memcpy(p + 0, _bit_pattern[v >> 4], 4);
   2.112 -		memcpy(p + 4, _bit_pattern[v & 0x0f], 4);
   2.113 -		p += 8;
   2.114 -	}
   2.115 -
   2.116 -	if(!xcan && ((buf - st->buf) % 8) == 0)
   2.117 -		_i_ASN_TEXT_INDENT(1, ilevel);
   2.118 -	er.encoded += p - scratch;
   2.119 -	_ASN_CALLBACK(scratch, p - scratch);
   2.120 -	p = scratch;
   2.121 -
   2.122 -	if(buf == end) {
   2.123 -		int v = *buf;
   2.124 -		int ubits = st->bits_unused;
   2.125 -		int i;
   2.126 -		for(i = 7; i >= ubits; i--)
   2.127 -			*p++ = (v & (1 << i)) ? 0x31 : 0x30;
   2.128 -		er.encoded += p - scratch;
   2.129 -		_ASN_CALLBACK(scratch, p - scratch);
   2.130 -	}
   2.131 -
   2.132 -	if(!xcan) _i_ASN_TEXT_INDENT(1, ilevel - 1);
   2.133 -
   2.134 -	_ASN_ENCODED_OK(er);
   2.135 -cb_failed:
   2.136 -	_ASN_ENCODE_FAILED;
   2.137 -}
   2.138 -
   2.139 -
   2.140 -/*
   2.141 - * BIT STRING specific contents printer.
   2.142 - */
   2.143 -int
   2.144 -BIT_STRING_print(asn_TYPE_descriptor_t *td, const void *sptr, int ilevel,
   2.145 -		asn_app_consume_bytes_f *cb, void *app_key) {
   2.146 -	static const char *h2c = "0123456789ABCDEF";
   2.147 -	char scratch[64];
   2.148 -	const BIT_STRING_t *st = (const BIT_STRING_t *)sptr;
   2.149 -	uint8_t *buf;
   2.150 -	uint8_t *end;
   2.151 -	char *p = scratch;
   2.152 -
   2.153 -	(void)td;	/* Unused argument */
   2.154 -
   2.155 -	if(!st || !st->buf)
   2.156 -		return (cb("<absent>", 8, app_key) < 0) ? -1 : 0;
   2.157 -
   2.158 -	ilevel++;
   2.159 -	buf = st->buf;
   2.160 -	end = buf + st->size;
   2.161 -
   2.162 -	/*
   2.163 -	 * Hexadecimal dump.
   2.164 -	 */
   2.165 -	for(; buf < end; buf++) {
   2.166 -		if((buf - st->buf) % 16 == 0 && (st->size > 16)
   2.167 -				&& buf != st->buf) {
   2.168 -			_i_INDENT(1);
   2.169 -			/* Dump the string */
   2.170 -			if(cb(scratch, p - scratch, app_key) < 0) return -1;
   2.171 -			p = scratch;
   2.172 -		}
   2.173 -		*p++ = h2c[*buf >> 4];
   2.174 -		*p++ = h2c[*buf & 0x0F];
   2.175 -		*p++ = 0x20;
   2.176 -	}
   2.177 -
   2.178 -	if(p > scratch) {
   2.179 -		p--;	/* Eat the tailing space */
   2.180 -
   2.181 -		if((st->size > 16)) {
   2.182 -			_i_INDENT(1);
   2.183 -		}
   2.184 -
   2.185 -		/* Dump the incomplete 16-bytes row */
   2.186 -		if(cb(scratch, p - scratch, app_key) < 0)
   2.187 -			return -1;
   2.188 -	}
   2.189 -
   2.190 -	return 0;
   2.191 -}
   2.192 -
     3.1 --- a/asn.1/BIT_STRING.h	Sun May 15 21:23:36 2016 +0200
     3.2 +++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
     3.3 @@ -1,33 +0,0 @@
     3.4 -/*-
     3.5 - * Copyright (c) 2003 Lev Walkin <vlm@lionet.info>. All rights reserved.
     3.6 - * Redistribution and modifications are permitted subject to BSD license.
     3.7 - */
     3.8 -#ifndef	_BIT_STRING_H_
     3.9 -#define	_BIT_STRING_H_
    3.10 -
    3.11 -#include <OCTET_STRING.h>	/* Some help from OCTET STRING */
    3.12 -
    3.13 -#ifdef __cplusplus
    3.14 -extern "C" {
    3.15 -#endif
    3.16 -
    3.17 -typedef struct BIT_STRING_s {
    3.18 -	uint8_t *buf;	/* BIT STRING body */
    3.19 -	int size;	/* Size of the above buffer */
    3.20 -
    3.21 -	int bits_unused;/* Unused trailing bits in the last octet (0..7) */
    3.22 -
    3.23 -	asn_struct_ctx_t _asn_ctx;	/* Parsing across buffer boundaries */
    3.24 -} BIT_STRING_t;
    3.25 -
    3.26 -extern asn_TYPE_descriptor_t asn_DEF_BIT_STRING;
    3.27 -
    3.28 -asn_struct_print_f BIT_STRING_print;	/* Human-readable output */
    3.29 -asn_constr_check_f BIT_STRING_constraint;
    3.30 -xer_type_encoder_f BIT_STRING_encode_xer;
    3.31 -
    3.32 -#ifdef __cplusplus
    3.33 -}
    3.34 -#endif
    3.35 -
    3.36 -#endif	/* _BIT_STRING_H_ */
     4.1 --- a/asn.1/Header.c	Sun May 15 21:23:36 2016 +0200
     4.2 +++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
     4.3 @@ -1,139 +0,0 @@
     4.4 -/*
     4.5 - * Generated by asn1c-0.9.26 (http://lionet.info/asn1c)
     4.6 - * From ASN.1 module "PEP"
     4.7 - * 	found in "pEp.asn1"
     4.8 - */
     4.9 -
    4.10 -#include "Header.h"
    4.11 -
    4.12 -static int
    4.13 -memb_protocol_constraint_1(asn_TYPE_descriptor_t *td, const void *sptr,
    4.14 -			asn_app_constraint_failed_f *ctfailcb, void *app_key) {
    4.15 -	long value;
    4.16 -	
    4.17 -	if(!sptr) {
    4.18 -		_ASN_CTFAIL(app_key, td, sptr,
    4.19 -			"%s: value not given (%s:%d)",
    4.20 -			td->name, __FILE__, __LINE__);
    4.21 -		return -1;
    4.22 -	}
    4.23 -	
    4.24 -	value = *(const long *)sptr;
    4.25 -	
    4.26 -	if((value >= 0 && value <= 65535)) {
    4.27 -		/* Constraint check succeeded */
    4.28 -		return 0;
    4.29 -	} else {
    4.30 -		_ASN_CTFAIL(app_key, td, sptr,
    4.31 -			"%s: constraint failed (%s:%d)",
    4.32 -			td->name, __FILE__, __LINE__);
    4.33 -		return -1;
    4.34 -	}
    4.35 -}
    4.36 -
    4.37 -static int
    4.38 -memb_message_constraint_1(asn_TYPE_descriptor_t *td, const void *sptr,
    4.39 -			asn_app_constraint_failed_f *ctfailcb, void *app_key) {
    4.40 -	long value;
    4.41 -	
    4.42 -	if(!sptr) {
    4.43 -		_ASN_CTFAIL(app_key, td, sptr,
    4.44 -			"%s: value not given (%s:%d)",
    4.45 -			td->name, __FILE__, __LINE__);
    4.46 -		return -1;
    4.47 -	}
    4.48 -	
    4.49 -	value = *(const long *)sptr;
    4.50 -	
    4.51 -	if((value >= 0 && value <= 65535)) {
    4.52 -		/* Constraint check succeeded */
    4.53 -		return 0;
    4.54 -	} else {
    4.55 -		_ASN_CTFAIL(app_key, td, sptr,
    4.56 -			"%s: constraint failed (%s:%d)",
    4.57 -			td->name, __FILE__, __LINE__);
    4.58 -		return -1;
    4.59 -	}
    4.60 -}
    4.61 -
    4.62 -static asn_per_constraints_t asn_PER_memb_protocol_constr_2 GCC_NOTUSED = {
    4.63 -	{ APC_CONSTRAINED,	 16,  16,  0,  65535 }	/* (0..65535) */,
    4.64 -	{ APC_UNCONSTRAINED,	-1, -1,  0,  0 },
    4.65 -	0, 0	/* No PER value map */
    4.66 -};
    4.67 -static asn_per_constraints_t asn_PER_memb_message_constr_4 GCC_NOTUSED = {
    4.68 -	{ APC_CONSTRAINED,	 16,  16,  0,  65535 }	/* (0..65535) */,
    4.69 -	{ APC_UNCONSTRAINED,	-1, -1,  0,  0 },
    4.70 -	0, 0	/* No PER value map */
    4.71 -};
    4.72 -static asn_TYPE_member_t asn_MBR_Header_1[] = {
    4.73 -	{ ATF_NOFLAGS, 0, offsetof(struct Header, protocol),
    4.74 -		(ASN_TAG_CLASS_UNIVERSAL | (2 << 2)),
    4.75 -		0,
    4.76 -		&asn_DEF_NativeInteger,
    4.77 -		memb_protocol_constraint_1,
    4.78 -		&asn_PER_memb_protocol_constr_2,
    4.79 -		0,
    4.80 -		"protocol"
    4.81 -		},
    4.82 -	{ ATF_NOFLAGS, 0, offsetof(struct Header, version),
    4.83 -		(ASN_TAG_CLASS_UNIVERSAL | (16 << 2)),
    4.84 -		0,
    4.85 -		&asn_DEF_Version,
    4.86 -		0,	/* Defer constraints checking to the member type */
    4.87 -		0,	/* No PER visible constraints */
    4.88 -		0,
    4.89 -		"version"
    4.90 -		},
    4.91 -	{ ATF_NOFLAGS, 0, offsetof(struct Header, message),
    4.92 -		(ASN_TAG_CLASS_UNIVERSAL | (2 << 2)),
    4.93 -		0,
    4.94 -		&asn_DEF_NativeInteger,
    4.95 -		memb_message_constraint_1,
    4.96 -		&asn_PER_memb_message_constr_4,
    4.97 -		0,
    4.98 -		"message"
    4.99 -		},
   4.100 -};
   4.101 -static ber_tlv_tag_t asn_DEF_Header_tags_1[] = {
   4.102 -	(ASN_TAG_CLASS_UNIVERSAL | (16 << 2))
   4.103 -};
   4.104 -static asn_TYPE_tag2member_t asn_MAP_Header_tag2el_1[] = {
   4.105 -    { (ASN_TAG_CLASS_UNIVERSAL | (2 << 2)), 0, 0, 1 }, /* protocol */
   4.106 -    { (ASN_TAG_CLASS_UNIVERSAL | (2 << 2)), 2, -1, 0 }, /* message */
   4.107 -    { (ASN_TAG_CLASS_UNIVERSAL | (16 << 2)), 1, 0, 0 } /* version */
   4.108 -};
   4.109 -static asn_SEQUENCE_specifics_t asn_SPC_Header_specs_1 = {
   4.110 -	sizeof(struct Header),
   4.111 -	offsetof(struct Header, _asn_ctx),
   4.112 -	asn_MAP_Header_tag2el_1,
   4.113 -	3,	/* Count of tags in the map */
   4.114 -	0, 0, 0,	/* Optional elements (not needed) */
   4.115 -	-1,	/* Start extensions */
   4.116 -	-1	/* Stop extensions */
   4.117 -};
   4.118 -asn_TYPE_descriptor_t asn_DEF_Header = {
   4.119 -	"Header",
   4.120 -	"Header",
   4.121 -	SEQUENCE_free,
   4.122 -	SEQUENCE_print,
   4.123 -	SEQUENCE_constraint,
   4.124 -	SEQUENCE_decode_ber,
   4.125 -	SEQUENCE_encode_der,
   4.126 -	SEQUENCE_decode_xer,
   4.127 -	SEQUENCE_encode_xer,
   4.128 -	SEQUENCE_decode_uper,
   4.129 -	SEQUENCE_encode_uper,
   4.130 -	0,	/* Use generic outmost tag fetcher */
   4.131 -	asn_DEF_Header_tags_1,
   4.132 -	sizeof(asn_DEF_Header_tags_1)
   4.133 -		/sizeof(asn_DEF_Header_tags_1[0]), /* 1 */
   4.134 -	asn_DEF_Header_tags_1,	/* Same as above */
   4.135 -	sizeof(asn_DEF_Header_tags_1)
   4.136 -		/sizeof(asn_DEF_Header_tags_1[0]), /* 1 */
   4.137 -	0,	/* No PER visible constraints */
   4.138 -	asn_MBR_Header_1,
   4.139 -	3,	/* Elements count */
   4.140 -	&asn_SPC_Header_specs_1	/* Additional specs */
   4.141 -};
   4.142 -
     5.1 --- a/asn.1/Header.h	Sun May 15 21:23:36 2016 +0200
     5.2 +++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
     5.3 @@ -1,40 +0,0 @@
     5.4 -/*
     5.5 - * Generated by asn1c-0.9.26 (http://lionet.info/asn1c)
     5.6 - * From ASN.1 module "PEP"
     5.7 - * 	found in "pEp.asn1"
     5.8 - */
     5.9 -
    5.10 -#ifndef	_Header_H_
    5.11 -#define	_Header_H_
    5.12 -
    5.13 -
    5.14 -#include <asn_application.h>
    5.15 -
    5.16 -/* Including external dependencies */
    5.17 -#include <NativeInteger.h>
    5.18 -#include "Version.h"
    5.19 -#include <constr_SEQUENCE.h>
    5.20 -
    5.21 -#ifdef __cplusplus
    5.22 -extern "C" {
    5.23 -#endif
    5.24 -
    5.25 -/* Header */
    5.26 -typedef struct Header {
    5.27 -	long	 protocol;
    5.28 -	Version_t	 version;
    5.29 -	long	 message;
    5.30 -	
    5.31 -	/* Context for parsing across buffer boundaries */
    5.32 -	asn_struct_ctx_t _asn_ctx;
    5.33 -} Header_t;
    5.34 -
    5.35 -/* Implementation */
    5.36 -extern asn_TYPE_descriptor_t asn_DEF_Header;
    5.37 -
    5.38 -#ifdef __cplusplus
    5.39 -}
    5.40 -#endif
    5.41 -
    5.42 -#endif	/* _Header_H_ */
    5.43 -#include <asn_internal.h>
     6.1 --- a/asn.1/INTEGER.c	Sun May 15 21:23:36 2016 +0200
     6.2 +++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
     6.3 @@ -1,1023 +0,0 @@
     6.4 -/*-
     6.5 - * Copyright (c) 2003-2014 Lev Walkin <vlm@lionet.info>.
     6.6 - * All rights reserved.
     6.7 - * Redistribution and modifications are permitted subject to BSD license.
     6.8 - */
     6.9 -#include <asn_internal.h>
    6.10 -#include <INTEGER.h>
    6.11 -#include <asn_codecs_prim.h>	/* Encoder and decoder of a primitive type */
    6.12 -#include <errno.h>
    6.13 -
    6.14 -/*
    6.15 - * INTEGER basic type description.
    6.16 - */
    6.17 -static ber_tlv_tag_t asn_DEF_INTEGER_tags[] = {
    6.18 -	(ASN_TAG_CLASS_UNIVERSAL | (2 << 2))
    6.19 -};
    6.20 -asn_TYPE_descriptor_t asn_DEF_INTEGER = {
    6.21 -	"INTEGER",
    6.22 -	"INTEGER",
    6.23 -	ASN__PRIMITIVE_TYPE_free,
    6.24 -	INTEGER_print,
    6.25 -	asn_generic_no_constraint,
    6.26 -	ber_decode_primitive,
    6.27 -	INTEGER_encode_der,
    6.28 -	INTEGER_decode_xer,
    6.29 -	INTEGER_encode_xer,
    6.30 -#ifdef	ASN_DISABLE_PER_SUPPORT
    6.31 -	0,
    6.32 -	0,
    6.33 -#else
    6.34 -	INTEGER_decode_uper,	/* Unaligned PER decoder */
    6.35 -	INTEGER_encode_uper,	/* Unaligned PER encoder */
    6.36 -#endif	/* ASN_DISABLE_PER_SUPPORT */
    6.37 -	0, /* Use generic outmost tag fetcher */
    6.38 -	asn_DEF_INTEGER_tags,
    6.39 -	sizeof(asn_DEF_INTEGER_tags) / sizeof(asn_DEF_INTEGER_tags[0]),
    6.40 -	asn_DEF_INTEGER_tags,	/* Same as above */
    6.41 -	sizeof(asn_DEF_INTEGER_tags) / sizeof(asn_DEF_INTEGER_tags[0]),
    6.42 -	0,	/* No PER visible constraints */
    6.43 -	0, 0,	/* No members */
    6.44 -	0	/* No specifics */
    6.45 -};
    6.46 -
    6.47 -/*
    6.48 - * Encode INTEGER type using DER.
    6.49 - */
    6.50 -asn_enc_rval_t
    6.51 -INTEGER_encode_der(asn_TYPE_descriptor_t *td, void *sptr,
    6.52 -	int tag_mode, ber_tlv_tag_t tag,
    6.53 -	asn_app_consume_bytes_f *cb, void *app_key) {
    6.54 -	INTEGER_t *st = (INTEGER_t *)sptr;
    6.55 -
    6.56 -	ASN_DEBUG("%s %s as INTEGER (tm=%d)",
    6.57 -		cb?"Encoding":"Estimating", td->name, tag_mode);
    6.58 -
    6.59 -	/*
    6.60 -	 * Canonicalize integer in the buffer.
    6.61 -	 * (Remove too long sign extension, remove some first 0x00 bytes)
    6.62 -	 */
    6.63 -	if(st->buf) {
    6.64 -		uint8_t *buf = st->buf;
    6.65 -		uint8_t *end1 = buf + st->size - 1;
    6.66 -		int shift;
    6.67 -
    6.68 -		/* Compute the number of superfluous leading bytes */
    6.69 -		for(; buf < end1; buf++) {
    6.70 -			/*
    6.71 -			 * If the contents octets of an integer value encoding
    6.72 -			 * consist of more than one octet, then the bits of the
    6.73 -			 * first octet and bit 8 of the second octet:
    6.74 -			 * a) shall not all be ones; and
    6.75 -			 * b) shall not all be zero.
    6.76 -			 */
    6.77 -			switch(*buf) {
    6.78 -			case 0x00: if((buf[1] & 0x80) == 0)
    6.79 -					continue;
    6.80 -				break;
    6.81 -			case 0xff: if((buf[1] & 0x80))
    6.82 -					continue;
    6.83 -				break;
    6.84 -			}
    6.85 -			break;
    6.86 -		}
    6.87 -
    6.88 -		/* Remove leading superfluous bytes from the integer */
    6.89 -		shift = buf - st->buf;
    6.90 -		if(shift) {
    6.91 -			uint8_t *nb = st->buf;
    6.92 -			uint8_t *end;
    6.93 -
    6.94 -			st->size -= shift;	/* New size, minus bad bytes */
    6.95 -			end = nb + st->size;
    6.96 -
    6.97 -			for(; nb < end; nb++, buf++)
    6.98 -				*nb = *buf;
    6.99 -		}
   6.100 -
   6.101 -	} /* if(1) */
   6.102 -
   6.103 -	return der_encode_primitive(td, sptr, tag_mode, tag, cb, app_key);
   6.104 -}
   6.105 -
   6.106 -static const asn_INTEGER_enum_map_t *INTEGER_map_enum2value(asn_INTEGER_specifics_t *specs, const char *lstart, const char *lstop);
   6.107 -
   6.108 -/*
   6.109 - * INTEGER specific human-readable output.
   6.110 - */
   6.111 -static ssize_t
   6.112 -INTEGER__dump(asn_TYPE_descriptor_t *td, const INTEGER_t *st, asn_app_consume_bytes_f *cb, void *app_key, int plainOrXER) {
   6.113 -	asn_INTEGER_specifics_t *specs=(asn_INTEGER_specifics_t *)td->specifics;
   6.114 -	char scratch[32];	/* Enough for 64-bit integer */
   6.115 -	uint8_t *buf = st->buf;
   6.116 -	uint8_t *buf_end = st->buf + st->size;
   6.117 -	signed long value;
   6.118 -	ssize_t wrote = 0;
   6.119 -	char *p;
   6.120 -	int ret;
   6.121 -
   6.122 -	if(specs && specs->field_unsigned)
   6.123 -		ret = asn_INTEGER2ulong(st, (unsigned long *)&value);
   6.124 -	else
   6.125 -		ret = asn_INTEGER2long(st, &value);
   6.126 -
   6.127 -	/* Simple case: the integer size is small */
   6.128 -	if(ret == 0) {
   6.129 -		const asn_INTEGER_enum_map_t *el;
   6.130 -		size_t scrsize;
   6.131 -		char *scr;
   6.132 -
   6.133 -		el = (value >= 0 || !specs || !specs->field_unsigned)
   6.134 -			? INTEGER_map_value2enum(specs, value) : 0;
   6.135 -		if(el) {
   6.136 -			scrsize = el->enum_len + 32;
   6.137 -			scr = (char *)alloca(scrsize);
   6.138 -			if(plainOrXER == 0)
   6.139 -				ret = snprintf(scr, scrsize,
   6.140 -					"%ld (%s)", value, el->enum_name);
   6.141 -			else
   6.142 -				ret = snprintf(scr, scrsize,
   6.143 -					"<%s/>", el->enum_name);
   6.144 -		} else if(plainOrXER && specs && specs->strict_enumeration) {
   6.145 -			ASN_DEBUG("ASN.1 forbids dealing with "
   6.146 -				"unknown value of ENUMERATED type");
   6.147 -			errno = EPERM;
   6.148 -			return -1;
   6.149 -		} else {
   6.150 -			scrsize = sizeof(scratch);
   6.151 -			scr = scratch;
   6.152 -			ret = snprintf(scr, scrsize,
   6.153 -				(specs && specs->field_unsigned)
   6.154 -				?"%lu":"%ld", value);
   6.155 -		}
   6.156 -		assert(ret > 0 && (size_t)ret < scrsize);
   6.157 -		return (cb(scr, ret, app_key) < 0) ? -1 : ret;
   6.158 -	} else if(plainOrXER && specs && specs->strict_enumeration) {
   6.159 -		/*
   6.160 -		 * Here and earlier, we cannot encode the ENUMERATED values
   6.161 -		 * if there is no corresponding identifier.
   6.162 -		 */
   6.163 -		ASN_DEBUG("ASN.1 forbids dealing with "
   6.164 -			"unknown value of ENUMERATED type");
   6.165 -		errno = EPERM;
   6.166 -		return -1;
   6.167 -	}
   6.168 -
   6.169 -	/* Output in the long xx:yy:zz... format */
   6.170 -	/* TODO: replace with generic algorithm (Knuth TAOCP Vol 2, 4.3.1) */
   6.171 -	for(p = scratch; buf < buf_end; buf++) {
   6.172 -		static const char *h2c = "0123456789ABCDEF";
   6.173 -		if((p - scratch) >= (ssize_t)(sizeof(scratch) - 4)) {
   6.174 -			/* Flush buffer */
   6.175 -			if(cb(scratch, p - scratch, app_key) < 0)
   6.176 -				return -1;
   6.177 -			wrote += p - scratch;
   6.178 -			p = scratch;
   6.179 -		}
   6.180 -		*p++ = h2c[*buf >> 4];
   6.181 -		*p++ = h2c[*buf & 0x0F];
   6.182 -		*p++ = 0x3a;	/* ":" */
   6.183 -	}
   6.184 -	if(p != scratch)
   6.185 -		p--;	/* Remove the last ":" */
   6.186 -
   6.187 -	wrote += p - scratch;
   6.188 -	return (cb(scratch, p - scratch, app_key) < 0) ? -1 : wrote;
   6.189 -}
   6.190 -
   6.191 -/*
   6.192 - * INTEGER specific human-readable output.
   6.193 - */
   6.194 -int
   6.195 -INTEGER_print(asn_TYPE_descriptor_t *td, const void *sptr, int ilevel,
   6.196 -	asn_app_consume_bytes_f *cb, void *app_key) {
   6.197 -	const INTEGER_t *st = (const INTEGER_t *)sptr;
   6.198 -	ssize_t ret;
   6.199 -
   6.200 -	(void)td;
   6.201 -	(void)ilevel;
   6.202 -
   6.203 -	if(!st || !st->buf)
   6.204 -		ret = cb("<absent>", 8, app_key);
   6.205 -	else
   6.206 -		ret = INTEGER__dump(td, st, cb, app_key, 0);
   6.207 -
   6.208 -	return (ret < 0) ? -1 : 0;
   6.209 -}
   6.210 -
   6.211 -struct e2v_key {
   6.212 -	const char *start;
   6.213 -	const char *stop;
   6.214 -	asn_INTEGER_enum_map_t *vemap;
   6.215 -	unsigned int *evmap;
   6.216 -};
   6.217 -static int
   6.218 -INTEGER__compar_enum2value(const void *kp, const void *am) {
   6.219 -	const struct e2v_key *key = (const struct e2v_key *)kp;
   6.220 -	const asn_INTEGER_enum_map_t *el = (const asn_INTEGER_enum_map_t *)am;
   6.221 -	const char *ptr, *end, *name;
   6.222 -
   6.223 -	/* Remap the element (sort by different criterion) */
   6.224 -	el = key->vemap + key->evmap[el - key->vemap];
   6.225 -
   6.226 -	/* Compare strings */
   6.227 -	for(ptr = key->start, end = key->stop, name = el->enum_name;
   6.228 -			ptr < end; ptr++, name++) {
   6.229 -		if(*ptr != *name)
   6.230 -			return *(const unsigned char *)ptr
   6.231 -				- *(const unsigned char *)name;
   6.232 -	}
   6.233 -	return name[0] ? -1 : 0;
   6.234 -}
   6.235 -
   6.236 -static const asn_INTEGER_enum_map_t *
   6.237 -INTEGER_map_enum2value(asn_INTEGER_specifics_t *specs, const char *lstart, const char *lstop) {
   6.238 -	asn_INTEGER_enum_map_t *el_found;
   6.239 -	int count = specs ? specs->map_count : 0;
   6.240 -	struct e2v_key key;
   6.241 -	const char *lp;
   6.242 -
   6.243 -	if(!count) return NULL;
   6.244 -
   6.245 -	/* Guaranteed: assert(lstart < lstop); */
   6.246 -	/* Figure out the tag name */
   6.247 -	for(lstart++, lp = lstart; lp < lstop; lp++) {
   6.248 -		switch(*lp) {
   6.249 -		case 9: case 10: case 11: case 12: case 13: case 32: /* WSP */
   6.250 -		case 0x2f: /* '/' */ case 0x3e: /* '>' */
   6.251 -			break;
   6.252 -		default:
   6.253 -			continue;
   6.254 -		}
   6.255 -		break;
   6.256 -	}
   6.257 -	if(lp == lstop) return NULL;	/* No tag found */
   6.258 -	lstop = lp;
   6.259 -
   6.260 -	key.start = lstart;
   6.261 -	key.stop = lstop;
   6.262 -	key.vemap = specs->value2enum;
   6.263 -	key.evmap = specs->enum2value;
   6.264 -	el_found = (asn_INTEGER_enum_map_t *)bsearch(&key,
   6.265 -		specs->value2enum, count, sizeof(specs->value2enum[0]),
   6.266 -		INTEGER__compar_enum2value);
   6.267 -	if(el_found) {
   6.268 -		/* Remap enum2value into value2enum */
   6.269 -		el_found = key.vemap + key.evmap[el_found - key.vemap];
   6.270 -	}
   6.271 -	return el_found;
   6.272 -}
   6.273 -
   6.274 -static int
   6.275 -INTEGER__compar_value2enum(const void *kp, const void *am) {
   6.276 -	long a = *(const long *)kp;
   6.277 -	const asn_INTEGER_enum_map_t *el = (const asn_INTEGER_enum_map_t *)am;
   6.278 -	long b = el->nat_value;
   6.279 -	if(a < b) return -1;
   6.280 -	else if(a == b) return 0;
   6.281 -	else return 1;
   6.282 -}
   6.283 -
   6.284 -const asn_INTEGER_enum_map_t *
   6.285 -INTEGER_map_value2enum(asn_INTEGER_specifics_t *specs, long value) {
   6.286 -	int count = specs ? specs->map_count : 0;
   6.287 -	if(!count) return 0;
   6.288 -	return (asn_INTEGER_enum_map_t *)bsearch(&value, specs->value2enum,
   6.289 -		count, sizeof(specs->value2enum[0]),
   6.290 -		INTEGER__compar_value2enum);
   6.291 -}
   6.292 -
   6.293 -static int
   6.294 -INTEGER_st_prealloc(INTEGER_t *st, int min_size) {
   6.295 -	void *p = MALLOC(min_size + 1);
   6.296 -	if(p) {
   6.297 -		void *b = st->buf;
   6.298 -		st->size = 0;
   6.299 -		st->buf = p;
   6.300 -		FREEMEM(b);
   6.301 -		return 0;
   6.302 -	} else {
   6.303 -		return -1;
   6.304 -	}
   6.305 -}
   6.306 -
   6.307 -/*
   6.308 - * Decode the chunk of XML text encoding INTEGER.
   6.309 - */
   6.310 -static enum xer_pbd_rval
   6.311 -INTEGER__xer_body_decode(asn_TYPE_descriptor_t *td, void *sptr, const void *chunk_buf, size_t chunk_size) {
   6.312 -	INTEGER_t *st = (INTEGER_t *)sptr;
   6.313 -	long dec_value;
   6.314 -	long hex_value = 0;
   6.315 -	const char *lp;
   6.316 -	const char *lstart = (const char *)chunk_buf;
   6.317 -	const char *lstop = lstart + chunk_size;
   6.318 -	enum {
   6.319 -		ST_LEADSPACE,
   6.320 -		ST_SKIPSPHEX,
   6.321 -		ST_WAITDIGITS,
   6.322 -		ST_DIGITS,
   6.323 -		ST_DIGITS_TRAILSPACE,
   6.324 -		ST_HEXDIGIT1,
   6.325 -		ST_HEXDIGIT2,
   6.326 -		ST_HEXDIGITS_TRAILSPACE,
   6.327 -		ST_HEXCOLON,
   6.328 -		ST_END_ENUM,
   6.329 -		ST_UNEXPECTED
   6.330 -	} state = ST_LEADSPACE;
   6.331 -	const char *dec_value_start = 0; /* INVARIANT: always !0 in ST_DIGITS */
   6.332 -	const char *dec_value_end = 0;
   6.333 -
   6.334 -	if(chunk_size)
   6.335 -		ASN_DEBUG("INTEGER body %ld 0x%2x..0x%2x",
   6.336 -			(long)chunk_size, *lstart, lstop[-1]);
   6.337 -
   6.338 -	if(INTEGER_st_prealloc(st, (chunk_size/3) + 1))
   6.339 -		return XPBD_SYSTEM_FAILURE;
   6.340 -
   6.341 -	/*
   6.342 -	 * We may have received a tag here. It will be processed inline.
   6.343 -	 * Use strtoul()-like code and serialize the result.
   6.344 -	 */
   6.345 -	for(lp = lstart; lp < lstop; lp++) {
   6.346 -		int lv = *lp;
   6.347 -		switch(lv) {
   6.348 -		case 0x09: case 0x0a: case 0x0d: case 0x20:
   6.349 -			switch(state) {
   6.350 -			case ST_LEADSPACE:
   6.351 -			case ST_DIGITS_TRAILSPACE:
   6.352 -			case ST_HEXDIGITS_TRAILSPACE:
   6.353 -			case ST_SKIPSPHEX:
   6.354 -				continue;
   6.355 -			case ST_DIGITS:
   6.356 -				dec_value_end = lp;
   6.357 -				state = ST_DIGITS_TRAILSPACE;
   6.358 -				continue;
   6.359 -			case ST_HEXCOLON:
   6.360 -				state = ST_HEXDIGITS_TRAILSPACE;
   6.361 -				continue;
   6.362 -			default:
   6.363 -				break;
   6.364 -			}
   6.365 -			break;
   6.366 -		case 0x2d:	/* '-' */
   6.367 -			if(state == ST_LEADSPACE) {
   6.368 -				dec_value = 0;
   6.369 -				dec_value_start = lp;
   6.370 -				state = ST_WAITDIGITS;
   6.371 -				continue;
   6.372 -			}
   6.373 -			break;
   6.374 -		case 0x2b:	/* '+' */
   6.375 -			if(state == ST_LEADSPACE) {
   6.376 -				dec_value = 0;
   6.377 -				dec_value_start = lp;
   6.378 -				state = ST_WAITDIGITS;
   6.379 -				continue;
   6.380 -			}
   6.381 -			break;
   6.382 -		case 0x30: case 0x31: case 0x32: case 0x33: case 0x34:
   6.383 -		case 0x35: case 0x36: case 0x37: case 0x38: case 0x39:
   6.384 -			switch(state) {
   6.385 -			case ST_DIGITS: continue;
   6.386 -			case ST_SKIPSPHEX:	/* Fall through */
   6.387 -			case ST_HEXDIGIT1:
   6.388 -				hex_value = (lv - 0x30) << 4;
   6.389 -				state = ST_HEXDIGIT2;
   6.390 -				continue;
   6.391 -			case ST_HEXDIGIT2:
   6.392 -				hex_value += (lv - 0x30);
   6.393 -				state = ST_HEXCOLON;
   6.394 -				st->buf[st->size++] = (uint8_t)hex_value;
   6.395 -				continue;
   6.396 -			case ST_HEXCOLON:
   6.397 -				return XPBD_BROKEN_ENCODING;
   6.398 -			case ST_LEADSPACE:
   6.399 -				dec_value = 0;
   6.400 -				dec_value_start = lp;
   6.401 -				/* FALL THROUGH */
   6.402 -			case ST_WAITDIGITS:
   6.403 -				state = ST_DIGITS;
   6.404 -				continue;
   6.405 -			default:
   6.406 -				break;
   6.407 -			}
   6.408 -			break;
   6.409 -		case 0x3c:	/* '<', start of XML encoded enumeration */
   6.410 -			if(state == ST_LEADSPACE) {
   6.411 -				const asn_INTEGER_enum_map_t *el;
   6.412 -				el = INTEGER_map_enum2value(
   6.413 -					(asn_INTEGER_specifics_t *)
   6.414 -					td->specifics, lstart, lstop);
   6.415 -				if(el) {
   6.416 -					ASN_DEBUG("Found \"%s\" => %ld",
   6.417 -						el->enum_name, el->nat_value);
   6.418 -					dec_value = el->nat_value;
   6.419 -					state = ST_END_ENUM;
   6.420 -					lp = lstop - 1;
   6.421 -					continue;
   6.422 -				}
   6.423 -				ASN_DEBUG("Unknown identifier for INTEGER");
   6.424 -			}
   6.425 -			return XPBD_BROKEN_ENCODING;
   6.426 -		case 0x3a:	/* ':' */
   6.427 -			if(state == ST_HEXCOLON) {
   6.428 -				/* This colon is expected */
   6.429 -				state = ST_HEXDIGIT1;
   6.430 -				continue;
   6.431 -			} else if(state == ST_DIGITS) {
   6.432 -				/* The colon here means that we have
   6.433 -				 * decoded the first two hexadecimal
   6.434 -				 * places as a decimal value.
   6.435 -				 * Switch decoding mode. */
   6.436 -				ASN_DEBUG("INTEGER re-evaluate as hex form");
   6.437 -				state = ST_SKIPSPHEX;
   6.438 -				dec_value_start = 0;
   6.439 -				lp = lstart - 1;
   6.440 -				continue;
   6.441 -			} else {
   6.442 -				ASN_DEBUG("state %d at %ld", state, (long)(lp - lstart));
   6.443 -				break;
   6.444 -			}
   6.445 -		/* [A-Fa-f] */
   6.446 -		case 0x41:case 0x42:case 0x43:case 0x44:case 0x45:case 0x46:
   6.447 -		case 0x61:case 0x62:case 0x63:case 0x64:case 0x65:case 0x66:
   6.448 -			switch(state) {
   6.449 -			case ST_SKIPSPHEX:
   6.450 -			case ST_LEADSPACE: /* Fall through */
   6.451 -			case ST_HEXDIGIT1:
   6.452 -				hex_value = lv - ((lv < 0x61) ? 0x41 : 0x61);
   6.453 -				hex_value += 10;
   6.454 -				hex_value <<= 4;
   6.455 -				state = ST_HEXDIGIT2;
   6.456 -				continue;
   6.457 -			case ST_HEXDIGIT2:
   6.458 -				hex_value += lv - ((lv < 0x61) ? 0x41 : 0x61);
   6.459 -				hex_value += 10;
   6.460 -				st->buf[st->size++] = (uint8_t)hex_value;
   6.461 -				state = ST_HEXCOLON;
   6.462 -				continue;
   6.463 -			case ST_DIGITS:
   6.464 -				ASN_DEBUG("INTEGER re-evaluate as hex form");
   6.465 -				state = ST_SKIPSPHEX;
   6.466 -				dec_value_start = 0;
   6.467 -				lp = lstart - 1;
   6.468 -				continue;
   6.469 -			default:
   6.470 -				break;
   6.471 -			}
   6.472 -			break;
   6.473 -		}
   6.474 -
   6.475 -		/* Found extra non-numeric stuff */
   6.476 -		ASN_DEBUG("INTEGER :: Found non-numeric 0x%2x at %ld",
   6.477 -			lv, (long)(lp - lstart));
   6.478 -		state = ST_UNEXPECTED;
   6.479 -		break;
   6.480 -	}
   6.481 -
   6.482 -	switch(state) {
   6.483 -	case ST_END_ENUM:
   6.484 -		/* Got a complete and valid enumeration encoded as a tag. */
   6.485 -		break;
   6.486 -	case ST_DIGITS:
   6.487 -		dec_value_end = lstop;
   6.488 -		/* FALL THROUGH */
   6.489 -	case ST_DIGITS_TRAILSPACE:
   6.490 -		/* The last symbol encountered was a digit. */
   6.491 -		switch(asn_strtol_lim(dec_value_start, &dec_value_end, &dec_value)) {
   6.492 -		case ASN_STRTOL_OK:
   6.493 -			break;
   6.494 -		case ASN_STRTOL_ERROR_RANGE:
   6.495 -			return XPBD_DECODER_LIMIT;
   6.496 -		case ASN_STRTOL_ERROR_INVAL:
   6.497 -		case ASN_STRTOL_EXPECT_MORE:
   6.498 -		case ASN_STRTOL_EXTRA_DATA:
   6.499 -			return XPBD_BROKEN_ENCODING;
   6.500 -		}
   6.501 -		break;
   6.502 -	case ST_HEXCOLON:
   6.503 -	case ST_HEXDIGITS_TRAILSPACE:
   6.504 -		st->buf[st->size] = 0;	/* Just in case termination */
   6.505 -		return XPBD_BODY_CONSUMED;
   6.506 -	case ST_HEXDIGIT1:
   6.507 -	case ST_HEXDIGIT2:
   6.508 -	case ST_SKIPSPHEX:
   6.509 -		return XPBD_BROKEN_ENCODING;
   6.510 -	case ST_LEADSPACE:
   6.511 -		/* Content not found */
   6.512 -		return XPBD_NOT_BODY_IGNORE;
   6.513 -	case ST_WAITDIGITS:
   6.514 -	case ST_UNEXPECTED:
   6.515 -		ASN_DEBUG("INTEGER: No useful digits (state %d)", state);
   6.516 -		return XPBD_BROKEN_ENCODING;	/* No digits */
   6.517 -	}
   6.518 -
   6.519 -	/*
   6.520 -	 * Convert the result of parsing of enumeration or a straight
   6.521 -	 * decimal value into a BER representation.
   6.522 -	 */
   6.523 -	if(asn_long2INTEGER(st, dec_value))
   6.524 -		return XPBD_SYSTEM_FAILURE;
   6.525 -
   6.526 -	return XPBD_BODY_CONSUMED;
   6.527 -}
   6.528 -
   6.529 -asn_dec_rval_t
   6.530 -INTEGER_decode_xer(asn_codec_ctx_t *opt_codec_ctx,
   6.531 -	asn_TYPE_descriptor_t *td, void **sptr, const char *opt_mname,
   6.532 -		const void *buf_ptr, size_t size) {
   6.533 -
   6.534 -	return xer_decode_primitive(opt_codec_ctx, td,
   6.535 -		sptr, sizeof(INTEGER_t), opt_mname,
   6.536 -		buf_ptr, size, INTEGER__xer_body_decode);
   6.537 -}
   6.538 -
   6.539 -asn_enc_rval_t
   6.540 -INTEGER_encode_xer(asn_TYPE_descriptor_t *td, void *sptr,
   6.541 -	int ilevel, enum xer_encoder_flags_e flags,
   6.542 -		asn_app_consume_bytes_f *cb, void *app_key) {
   6.543 -	const INTEGER_t *st = (const INTEGER_t *)sptr;
   6.544 -	asn_enc_rval_t er;
   6.545 -
   6.546 -	(void)ilevel;
   6.547 -	(void)flags;
   6.548 -	
   6.549 -	if(!st || !st->buf)
   6.550 -		_ASN_ENCODE_FAILED;
   6.551 -
   6.552 -	er.encoded = INTEGER__dump(td, st, cb, app_key, 1);
   6.553 -	if(er.encoded < 0) _ASN_ENCODE_FAILED;
   6.554 -
   6.555 -	_ASN_ENCODED_OK(er);
   6.556 -}
   6.557 -
   6.558 -#ifndef	ASN_DISABLE_PER_SUPPORT
   6.559 -
   6.560 -asn_dec_rval_t
   6.561 -INTEGER_decode_uper(asn_codec_ctx_t *opt_codec_ctx, asn_TYPE_descriptor_t *td,
   6.562 -	asn_per_constraints_t *constraints, void **sptr, asn_per_data_t *pd) {
   6.563 -	asn_INTEGER_specifics_t *specs=(asn_INTEGER_specifics_t *)td->specifics;
   6.564 -	asn_dec_rval_t rval = { RC_OK, 0 };
   6.565 -	INTEGER_t *st = (INTEGER_t *)*sptr;
   6.566 -	asn_per_constraint_t *ct;
   6.567 -	int repeat;
   6.568 -
   6.569 -	(void)opt_codec_ctx;
   6.570 -
   6.571 -	if(!st) {
   6.572 -		st = (INTEGER_t *)(*sptr = CALLOC(1, sizeof(*st)));
   6.573 -		if(!st) _ASN_DECODE_FAILED;
   6.574 -	}
   6.575 -
   6.576 -	if(!constraints) constraints = td->per_constraints;
   6.577 -	ct = constraints ? &constraints->value : 0;
   6.578 -
   6.579 -	if(ct && ct->flags & APC_EXTENSIBLE) {
   6.580 -		int inext = per_get_few_bits(pd, 1);
   6.581 -		if(inext < 0) _ASN_DECODE_STARVED;
   6.582 -		if(inext) ct = 0;
   6.583 -	}
   6.584 -
   6.585 -	FREEMEM(st->buf);
   6.586 -	st->buf = 0;
   6.587 -	st->size = 0;
   6.588 -	if(ct) {
   6.589 -		if(ct->flags & APC_SEMI_CONSTRAINED) {
   6.590 -			st->buf = (uint8_t *)CALLOC(1, 2);
   6.591 -			if(!st->buf) _ASN_DECODE_FAILED;
   6.592 -			st->size = 1;
   6.593 -		} else if(ct->flags & APC_CONSTRAINED && ct->range_bits >= 0) {
   6.594 -			size_t size = (ct->range_bits + 7) >> 3;
   6.595 -			st->buf = (uint8_t *)MALLOC(1 + size + 1);
   6.596 -			if(!st->buf) _ASN_DECODE_FAILED;
   6.597 -			st->size = size;
   6.598 -		}
   6.599 -	}
   6.600 -
   6.601 -	/* X.691-2008/11, #13.2.2, constrained whole number */
   6.602 -	if(ct && ct->flags != APC_UNCONSTRAINED) {
   6.603 -		/* #11.5.6 */
   6.604 -		ASN_DEBUG("Integer with range %d bits", ct->range_bits);
   6.605 -		if(ct->range_bits >= 0) {
   6.606 -			if((size_t)ct->range_bits > 8 * sizeof(unsigned long))
   6.607 -				_ASN_DECODE_FAILED;
   6.608 -
   6.609 -			if(specs && specs->field_unsigned) {
   6.610 -				unsigned long uvalue;
   6.611 -				if(uper_get_constrained_whole_number(pd,
   6.612 -					&uvalue, ct->range_bits))
   6.613 -					_ASN_DECODE_STARVED;
   6.614 -				ASN_DEBUG("Got value %lu + low %ld",
   6.615 -					uvalue, ct->lower_bound);
   6.616 -				uvalue += ct->lower_bound;
   6.617 -				if(asn_ulong2INTEGER(st, uvalue))
   6.618 -					_ASN_DECODE_FAILED;
   6.619 -			} else {
   6.620 -				unsigned long svalue;
   6.621 -				if(uper_get_constrained_whole_number(pd,
   6.622 -					&svalue, ct->range_bits))
   6.623 -					_ASN_DECODE_STARVED;
   6.624 -				ASN_DEBUG("Got value %ld + low %ld",
   6.625 -					svalue, ct->lower_bound);
   6.626 -				svalue += ct->lower_bound;
   6.627 -				if(asn_long2INTEGER(st, svalue))
   6.628 -					_ASN_DECODE_FAILED;
   6.629 -			}
   6.630 -			return rval;
   6.631 -		}
   6.632 -	} else {
   6.633 -		ASN_DEBUG("Decoding unconstrained integer %s", td->name);
   6.634 -	}
   6.635 -
   6.636 -	/* X.691, #12.2.3, #12.2.4 */
   6.637 -	do {
   6.638 -		ssize_t len;
   6.639 -		void *p;
   6.640 -		int ret;
   6.641 -
   6.642 -		/* Get the PER length */
   6.643 -		len = uper_get_length(pd, -1, &repeat);
   6.644 -		if(len < 0) _ASN_DECODE_STARVED;
   6.645 -
   6.646 -		p = REALLOC(st->buf, st->size + len + 1);
   6.647 -		if(!p) _ASN_DECODE_FAILED;
   6.648 -		st->buf = (uint8_t *)p;
   6.649 -
   6.650 -		ret = per_get_many_bits(pd, &st->buf[st->size], 0, 8 * len);
   6.651 -		if(ret < 0) _ASN_DECODE_STARVED;
   6.652 -		st->size += len;
   6.653 -	} while(repeat);
   6.654 -	st->buf[st->size] = 0;	/* JIC */
   6.655 -
   6.656 -	/* #12.2.3 */
   6.657 -	if(ct && ct->lower_bound) {
   6.658 -		/*
   6.659 -		 * TODO: replace by in-place arithmetics.
   6.660 -		 */
   6.661 -		long value;
   6.662 -		if(asn_INTEGER2long(st, &value))
   6.663 -			_ASN_DECODE_FAILED;
   6.664 -		if(asn_long2INTEGER(st, value + ct->lower_bound))
   6.665 -			_ASN_DECODE_FAILED;
   6.666 -	}
   6.667 -
   6.668 -	return rval;
   6.669 -}
   6.670 -
   6.671 -asn_enc_rval_t
   6.672 -INTEGER_encode_uper(asn_TYPE_descriptor_t *td,
   6.673 -	asn_per_constraints_t *constraints, void *sptr, asn_per_outp_t *po) {
   6.674 -	asn_INTEGER_specifics_t *specs=(asn_INTEGER_specifics_t *)td->specifics;
   6.675 -	asn_enc_rval_t er;
   6.676 -	INTEGER_t *st = (INTEGER_t *)sptr;
   6.677 -	const uint8_t *buf;
   6.678 -	const uint8_t *end;
   6.679 -	asn_per_constraint_t *ct;
   6.680 -	long value = 0;
   6.681 -
   6.682 -	if(!st || st->size == 0) _ASN_ENCODE_FAILED;
   6.683 -
   6.684 -	if(!constraints) constraints = td->per_constraints;
   6.685 -	ct = constraints ? &constraints->value : 0;
   6.686 -
   6.687 -	er.encoded = 0;
   6.688 -
   6.689 -	if(ct) {
   6.690 -		int inext = 0;
   6.691 -		if(specs && specs->field_unsigned) {
   6.692 -			unsigned long uval;
   6.693 -			if(asn_INTEGER2ulong(st, &uval))
   6.694 -				_ASN_ENCODE_FAILED;
   6.695 -			/* Check proper range */
   6.696 -			if(ct->flags & APC_SEMI_CONSTRAINED) {
   6.697 -				if(uval < (unsigned long)ct->lower_bound)
   6.698 -					inext = 1;
   6.699 -			} else if(ct->range_bits >= 0) {
   6.700 -				if(uval < (unsigned long)ct->lower_bound
   6.701 -				|| uval > (unsigned long)ct->upper_bound)
   6.702 -					inext = 1;
   6.703 -			}
   6.704 -			ASN_DEBUG("Value %lu (%02x/%d) lb %lu ub %lu %s",
   6.705 -				uval, st->buf[0], st->size,
   6.706 -				ct->lower_bound, ct->upper_bound,
   6.707 -				inext ? "ext" : "fix");
   6.708 -			value = uval;
   6.709 -		} else {
   6.710 -			if(asn_INTEGER2long(st, &value))
   6.711 -				_ASN_ENCODE_FAILED;
   6.712 -			/* Check proper range */
   6.713 -			if(ct->flags & APC_SEMI_CONSTRAINED) {
   6.714 -				if(value < ct->lower_bound)
   6.715 -					inext = 1;
   6.716 -			} else if(ct->range_bits >= 0) {
   6.717 -				if(value < ct->lower_bound
   6.718 -				|| value > ct->upper_bound)
   6.719 -					inext = 1;
   6.720 -			}
   6.721 -			ASN_DEBUG("Value %ld (%02x/%d) lb %ld ub %ld %s",
   6.722 -				value, st->buf[0], st->size,
   6.723 -				ct->lower_bound, ct->upper_bound,
   6.724 -				inext ? "ext" : "fix");
   6.725 -		}
   6.726 -		if(ct->flags & APC_EXTENSIBLE) {
   6.727 -			if(per_put_few_bits(po, inext, 1))
   6.728 -				_ASN_ENCODE_FAILED;
   6.729 -			if(inext) ct = 0;
   6.730 -		} else if(inext) {
   6.731 -			_ASN_ENCODE_FAILED;
   6.732 -		}
   6.733 -	}
   6.734 -
   6.735 -
   6.736 -	/* X.691-11/2008, #13.2.2, test if constrained whole number */
   6.737 -	if(ct && ct->range_bits >= 0) {
   6.738 -		/* #11.5.6 -> #11.3 */
   6.739 -		ASN_DEBUG("Encoding integer %ld (%lu) with range %d bits",
   6.740 -			value, value - ct->lower_bound, ct->range_bits);
   6.741 -		unsigned long v = value - ct->lower_bound;
   6.742 -		if(uper_put_constrained_whole_number_u(po, v, ct->range_bits))
   6.743 -			_ASN_ENCODE_FAILED;
   6.744 -		_ASN_ENCODED_OK(er);
   6.745 -	}
   6.746 -
   6.747 -	if(ct && ct->lower_bound) {
   6.748 -		ASN_DEBUG("Adjust lower bound to %ld", ct->lower_bound);
   6.749 -		/* TODO: adjust lower bound */
   6.750 -		_ASN_ENCODE_FAILED;
   6.751 -	}
   6.752 -
   6.753 -	for(buf = st->buf, end = st->buf + st->size; buf < end;) {
   6.754 -		ssize_t mayEncode = uper_put_length(po, end - buf);
   6.755 -		if(mayEncode < 0)
   6.756 -			_ASN_ENCODE_FAILED;
   6.757 -		if(per_put_many_bits(po, buf, 8 * mayEncode))
   6.758 -			_ASN_ENCODE_FAILED;
   6.759 -		buf += mayEncode;
   6.760 -	}
   6.761 -
   6.762 -	_ASN_ENCODED_OK(er);
   6.763 -}
   6.764 -
   6.765 -#endif	/* ASN_DISABLE_PER_SUPPORT */
   6.766 -
   6.767 -int
   6.768 -asn_INTEGER2long(const INTEGER_t *iptr, long *lptr) {
   6.769 -	uint8_t *b, *end;
   6.770 -	size_t size;
   6.771 -	long l;
   6.772 -
   6.773 -	/* Sanity checking */
   6.774 -	if(!iptr || !iptr->buf || !lptr) {
   6.775 -		errno = EINVAL;
   6.776 -		return -1;
   6.777 -	}
   6.778 -
   6.779 -	/* Cache the begin/end of the buffer */
   6.780 -	b = iptr->buf;	/* Start of the INTEGER buffer */
   6.781 -	size = iptr->size;
   6.782 -	end = b + size;	/* Where to stop */
   6.783 -
   6.784 -	if(size > sizeof(long)) {
   6.785 -		uint8_t *end1 = end - 1;
   6.786 -		/*
   6.787 -		 * Slightly more advanced processing,
   6.788 -		 * able to >sizeof(long) bytes,
   6.789 -		 * when the actual value is small
   6.790 -		 * (0x0000000000abcdef would yield a fine 0x00abcdef)
   6.791 -		 */
   6.792 -		/* Skip out the insignificant leading bytes */
   6.793 -		for(; b < end1; b++) {
   6.794 -			switch(*b) {
   6.795 -			case 0x00: if((b[1] & 0x80) == 0) continue; break;
   6.796 -			case 0xff: if((b[1] & 0x80) != 0) continue; break;
   6.797 -			}
   6.798 -			break;
   6.799 -		}
   6.800 -
   6.801 -		size = end - b;
   6.802 -		if(size > sizeof(long)) {
   6.803 -			/* Still cannot fit the long */
   6.804 -			errno = ERANGE;
   6.805 -			return -1;
   6.806 -		}
   6.807 -	}
   6.808 -
   6.809 -	/* Shortcut processing of a corner case */
   6.810 -	if(end == b) {
   6.811 -		*lptr = 0;
   6.812 -		return 0;
   6.813 -	}
   6.814 -
   6.815 -	/* Perform the sign initialization */
   6.816 -	/* Actually l = -(*b >> 7); gains nothing, yet unreadable! */
   6.817 -	if((*b >> 7)) l = -1; else l = 0;
   6.818 -
   6.819 -	/* Conversion engine */
   6.820 -	for(; b < end; b++)
   6.821 -		l = (l << 8) | *b;
   6.822 -
   6.823 -	*lptr = l;
   6.824 -	return 0;
   6.825 -}
   6.826 -
   6.827 -int
   6.828 -asn_INTEGER2ulong(const INTEGER_t *iptr, unsigned long *lptr) {
   6.829 -	uint8_t *b, *end;
   6.830 -	unsigned long l;
   6.831 -	size_t size;
   6.832 -
   6.833 -	if(!iptr || !iptr->buf || !lptr) {
   6.834 -		errno = EINVAL;
   6.835 -		return -1;
   6.836 -	}
   6.837 -
   6.838 -	b = iptr->buf;
   6.839 -	size = iptr->size;
   6.840 -	end = b + size;
   6.841 -
   6.842 -	/* If all extra leading bytes are zeroes, ignore them */
   6.843 -	for(; size > sizeof(unsigned long); b++, size--) {
   6.844 -		if(*b) {
   6.845 -			/* Value won't fit unsigned long */
   6.846 -			errno = ERANGE;
   6.847 -			return -1;
   6.848 -		}
   6.849 -	}
   6.850 -
   6.851 -	/* Conversion engine */
   6.852 -	for(l = 0; b < end; b++)
   6.853 -		l = (l << 8) | *b;
   6.854 -
   6.855 -	*lptr = l;
   6.856 -	return 0;
   6.857 -}
   6.858 -
   6.859 -int
   6.860 -asn_ulong2INTEGER(INTEGER_t *st, unsigned long value) {
   6.861 -	uint8_t *buf;
   6.862 -	uint8_t *end;
   6.863 -	uint8_t *b;
   6.864 -	int shr;
   6.865 -
   6.866 -	if(value <= LONG_MAX)
   6.867 -		return asn_long2INTEGER(st, value);
   6.868 -
   6.869 -	buf = (uint8_t *)MALLOC(1 + sizeof(value));
   6.870 -	if(!buf) return -1;
   6.871 -
   6.872 -	end = buf + (sizeof(value) + 1);
   6.873 -	buf[0] = 0;
   6.874 -	for(b = buf + 1, shr = (sizeof(long)-1)*8; b < end; shr -= 8, b++)
   6.875 -		*b = (uint8_t)(value >> shr);
   6.876 -
   6.877 -	if(st->buf) FREEMEM(st->buf);
   6.878 -	st->buf = buf;
   6.879 -	st->size = 1 + sizeof(value);
   6.880 -
   6.881 -	return 0;
   6.882 -}
   6.883 -
   6.884 -int
   6.885 -asn_long2INTEGER(INTEGER_t *st, long value) {
   6.886 -	uint8_t *buf, *bp;
   6.887 -	uint8_t *p;
   6.888 -	uint8_t *pstart;
   6.889 -	uint8_t *pend1;
   6.890 -	int littleEndian = 1;	/* Run-time detection */
   6.891 -	int add;
   6.892 -
   6.893 -	if(!st) {
   6.894 -		errno = EINVAL;
   6.895 -		return -1;
   6.896 -	}
   6.897 -
   6.898 -	buf = (uint8_t *)MALLOC(sizeof(value));
   6.899 -	if(!buf) return -1;
   6.900 -
   6.901 -	if(*(char *)&littleEndian) {
   6.902 -		pstart = (uint8_t *)&value + sizeof(value) - 1;
   6.903 -		pend1 = (uint8_t *)&value;
   6.904 -		add = -1;
   6.905 -	} else {
   6.906 -		pstart = (uint8_t *)&value;
   6.907 -		pend1 = pstart + sizeof(value) - 1;
   6.908 -		add = 1;
   6.909 -	}
   6.910 -
   6.911 -	/*
   6.912 -	 * If the contents octet consists of more than one octet,
   6.913 -	 * then bits of the first octet and bit 8 of the second octet:
   6.914 -	 * a) shall not all be ones; and
   6.915 -	 * b) shall not all be zero.
   6.916 -	 */
   6.917 -	for(p = pstart; p != pend1; p += add) {
   6.918 -		switch(*p) {
   6.919 -		case 0x00: if((*(p+add) & 0x80) == 0)
   6.920 -				continue;
   6.921 -			break;
   6.922 -		case 0xff: if((*(p+add) & 0x80))
   6.923 -				continue;
   6.924 -			break;
   6.925 -		}
   6.926 -		break;
   6.927 -	}
   6.928 -	/* Copy the integer body */
   6.929 -	for(pstart = p, bp = buf, pend1 += add; p != pend1; p += add)
   6.930 -		*bp++ = *p;
   6.931 -
   6.932 -	if(st->buf) FREEMEM(st->buf);
   6.933 -	st->buf = buf;
   6.934 -	st->size = bp - buf;
   6.935 -
   6.936 -	return 0;
   6.937 -}
   6.938 -
   6.939 -/*
   6.940 - * This function is going to be DEPRECATED soon.
   6.941 - */
   6.942 -enum asn_strtol_result_e
   6.943 -asn_strtol(const char *str, const char *end, long *lp) {
   6.944 -    const char *endp = end;
   6.945 -
   6.946 -    switch(asn_strtol_lim(str, &endp, lp)) {
   6.947 -    case ASN_STRTOL_ERROR_RANGE:
   6.948 -        return ASN_STRTOL_ERROR_RANGE;
   6.949 -    case ASN_STRTOL_ERROR_INVAL:
   6.950 -        return ASN_STRTOL_ERROR_INVAL;
   6.951 -    case ASN_STRTOL_EXPECT_MORE:
   6.952 -        return ASN_STRTOL_ERROR_INVAL;  /* Retain old behavior */
   6.953 -    case ASN_STRTOL_OK:
   6.954 -        return ASN_STRTOL_OK;
   6.955 -    case ASN_STRTOL_EXTRA_DATA:
   6.956 -        return ASN_STRTOL_ERROR_INVAL;  /* Retain old behavior */
   6.957 -    }
   6.958 -
   6.959 -    return ASN_STRTOL_ERROR_INVAL;  /* Retain old behavior */
   6.960 -}
   6.961 -
   6.962 -/*
   6.963 - * Parse the number in the given string until the given *end position,
   6.964 - * returning the position after the last parsed character back using the
   6.965 - * same (*end) pointer.
   6.966 - * WARNING: This behavior is different from the standard strtol(3).
   6.967 - */
   6.968 -enum asn_strtol_result_e
   6.969 -asn_strtol_lim(const char *str, const char **end, long *lp) {
   6.970 -	int sign = 1;
   6.971 -	long l;
   6.972 -
   6.973 -	const long upper_boundary = LONG_MAX / 10;
   6.974 -	long last_digit_max = LONG_MAX % 10;
   6.975 -
   6.976 -	if(str >= *end) return ASN_STRTOL_ERROR_INVAL;
   6.977 -
   6.978 -	switch(*str) {
   6.979 -	case '-':
   6.980 -		last_digit_max++;
   6.981 -		sign = -1;
   6.982 -	case '+':
   6.983 -		str++;
   6.984 -		if(str >= *end) {
   6.985 -			*end = str;
   6.986 -			return ASN_STRTOL_EXPECT_MORE;
   6.987 -		}
   6.988 -	}
   6.989 -
   6.990 -	for(l = 0; str < (*end); str++) {
   6.991 -		switch(*str) {
   6.992 -		case 0x30: case 0x31: case 0x32: case 0x33: case 0x34:
   6.993 -		case 0x35: case 0x36: case 0x37: case 0x38: case 0x39: {
   6.994 -			int d = *str - '0';
   6.995 -			if(l < upper_boundary) {
   6.996 -				l = l * 10 + d;
   6.997 -			} else if(l == upper_boundary) {
   6.998 -				if(d <= last_digit_max) {
   6.999 -					if(sign > 0) {
  6.1000 -						l = l * 10 + d;
  6.1001 -					} else {
  6.1002 -						sign = 1;
  6.1003 -						l = -l * 10 - d;
  6.1004 -					}
  6.1005 -				} else {
  6.1006 -					*end = str;
  6.1007 -					return ASN_STRTOL_ERROR_RANGE;
  6.1008 -				}
  6.1009 -			} else {
  6.1010 -				*end = str;
  6.1011 -				return ASN_STRTOL_ERROR_RANGE;
  6.1012 -			}
  6.1013 -		    }
  6.1014 -		    continue;
  6.1015 -		default:
  6.1016 -		    *end = str;
  6.1017 -		    *lp = sign * l;
  6.1018 -		    return ASN_STRTOL_EXTRA_DATA;
  6.1019 -		}
  6.1020 -	}
  6.1021 -
  6.1022 -	*end = str;
  6.1023 -	*lp = sign * l;
  6.1024 -	return ASN_STRTOL_OK;
  6.1025 -}
  6.1026 -
     7.1 --- a/asn.1/INTEGER.h	Sun May 15 21:23:36 2016 +0200
     7.2 +++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
     7.3 @@ -1,82 +0,0 @@
     7.4 -/*-
     7.5 - * Copyright (c) 2003, 2005 Lev Walkin <vlm@lionet.info>. All rights reserved.
     7.6 - * Redistribution and modifications are permitted subject to BSD license.
     7.7 - */
     7.8 -#ifndef	_INTEGER_H_
     7.9 -#define	_INTEGER_H_
    7.10 -
    7.11 -#include <asn_application.h>
    7.12 -#include <asn_codecs_prim.h>
    7.13 -
    7.14 -#ifdef __cplusplus
    7.15 -extern "C" {
    7.16 -#endif
    7.17 -
    7.18 -typedef ASN__PRIMITIVE_TYPE_t INTEGER_t;
    7.19 -
    7.20 -extern asn_TYPE_descriptor_t asn_DEF_INTEGER;
    7.21 -
    7.22 -/* Map with <tag> to integer value association */
    7.23 -typedef struct asn_INTEGER_enum_map_s {
    7.24 -	long		 nat_value;	/* associated native integer value */
    7.25 -	size_t		 enum_len;	/* strlen("tag") */
    7.26 -	const char	*enum_name;	/* "tag" */
    7.27 -} asn_INTEGER_enum_map_t;
    7.28 -
    7.29 -/* This type describes an enumeration for INTEGER and ENUMERATED types */
    7.30 -typedef struct asn_INTEGER_specifics_s {
    7.31 -	asn_INTEGER_enum_map_t *value2enum;	/* N -> "tag"; sorted by N */
    7.32 -	unsigned int *enum2value;		/* "tag" => N; sorted by tag */
    7.33 -	int map_count;				/* Elements in either map */
    7.34 -	int extension;				/* This map is extensible */
    7.35 -	int strict_enumeration;			/* Enumeration set is fixed */
    7.36 -	int field_width;			/* Size of native integer */
    7.37 -	int field_unsigned;			/* Signed=0, unsigned=1 */
    7.38 -} asn_INTEGER_specifics_t;
    7.39 -
    7.40 -asn_struct_print_f INTEGER_print;
    7.41 -ber_type_decoder_f INTEGER_decode_ber;
    7.42 -der_type_encoder_f INTEGER_encode_der;
    7.43 -xer_type_decoder_f INTEGER_decode_xer;
    7.44 -xer_type_encoder_f INTEGER_encode_xer;
    7.45 -per_type_decoder_f INTEGER_decode_uper;
    7.46 -per_type_encoder_f INTEGER_encode_uper;
    7.47 -
    7.48 -/***********************************
    7.49 - * Some handy conversion routines. *
    7.50 - ***********************************/
    7.51 -
    7.52 -/*
    7.53 - * Returns 0 if it was possible to convert, -1 otherwise.
    7.54 - * -1/EINVAL: Mandatory argument missing
    7.55 - * -1/ERANGE: Value encoded is out of range for long representation
    7.56 - * -1/ENOMEM: Memory allocation failed (in asn_long2INTEGER()).
    7.57 - */
    7.58 -int asn_INTEGER2long(const INTEGER_t *i, long *l);
    7.59 -int asn_INTEGER2ulong(const INTEGER_t *i, unsigned long *l);
    7.60 -int asn_long2INTEGER(INTEGER_t *i, long l);
    7.61 -int asn_ulong2INTEGER(INTEGER_t *i, unsigned long l);
    7.62 -
    7.63 -/* A a reified version of strtol(3) with nicer error reporting. */
    7.64 -enum asn_strtol_result_e {
    7.65 -    ASN_STRTOL_ERROR_RANGE = -3,  /* Input outside of numeric range for long type */
    7.66 -    ASN_STRTOL_ERROR_INVAL = -2,  /* Invalid data encountered (e.g., "+-") */
    7.67 -    ASN_STRTOL_EXPECT_MORE = -1,  /* More data expected (e.g. "+") */
    7.68 -    ASN_STRTOL_OK          =  0,  /* Conversion succeded, number ends at (*end) */
    7.69 -    ASN_STRTOL_EXTRA_DATA  =  1,  /* Conversion succeded, but the string has extra stuff */
    7.70 -};
    7.71 -enum asn_strtol_result_e asn_strtol_lim(const char *str, const char **end, long *l);
    7.72 -
    7.73 -/* The asn_strtol is going to be DEPRECATED soon */
    7.74 -enum asn_strtol_result_e asn_strtol(const char *str, const char *end, long *l);
    7.75 -
    7.76 -/*
    7.77 - * Convert the integer value into the corresponding enumeration map entry.
    7.78 - */
    7.79 -const asn_INTEGER_enum_map_t *INTEGER_map_value2enum(asn_INTEGER_specifics_t *specs, long value);
    7.80 -
    7.81 -#ifdef __cplusplus
    7.82 -}
    7.83 -#endif
    7.84 -
    7.85 -#endif	/* _INTEGER_H_ */
     8.1 --- a/asn.1/Identity.c	Sun May 15 21:23:36 2016 +0200
     8.2 +++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
     8.3 @@ -1,139 +0,0 @@
     8.4 -/*
     8.5 - * Generated by asn1c-0.9.26 (http://lionet.info/asn1c)
     8.6 - * From ASN.1 module "PEP"
     8.7 - * 	found in "pEp.asn1"
     8.8 - */
     8.9 -
    8.10 -#include "Identity.h"
    8.11 -
    8.12 -static int
    8.13 -memb_comm_type_constraint_1(asn_TYPE_descriptor_t *td, const void *sptr,
    8.14 -			asn_app_constraint_failed_f *ctfailcb, void *app_key) {
    8.15 -	long value;
    8.16 -	
    8.17 -	if(!sptr) {
    8.18 -		_ASN_CTFAIL(app_key, td, sptr,
    8.19 -			"%s: value not given (%s:%d)",
    8.20 -			td->name, __FILE__, __LINE__);
    8.21 -		return -1;
    8.22 -	}
    8.23 -	
    8.24 -	value = *(const long *)sptr;
    8.25 -	
    8.26 -	if((value >= 0 && value <= 255)) {
    8.27 -		/* Constraint check succeeded */
    8.28 -		return 0;
    8.29 -	} else {
    8.30 -		_ASN_CTFAIL(app_key, td, sptr,
    8.31 -			"%s: constraint failed (%s:%d)",
    8.32 -			td->name, __FILE__, __LINE__);
    8.33 -		return -1;
    8.34 -	}
    8.35 -}
    8.36 -
    8.37 -static asn_per_constraints_t asn_PER_memb_comm_type_constr_6 GCC_NOTUSED = {
    8.38 -	{ APC_CONSTRAINED,	 8,  8,  0,  255 }	/* (0..255) */,
    8.39 -	{ APC_UNCONSTRAINED,	-1, -1,  0,  0 },
    8.40 -	0, 0	/* No PER value map */
    8.41 -};
    8.42 -static asn_TYPE_member_t asn_MBR_Identity_1[] = {
    8.43 -	{ ATF_NOFLAGS, 0, offsetof(struct Identity, address),
    8.44 -		(ASN_TAG_CLASS_UNIVERSAL | (12 << 2)),
    8.45 -		0,
    8.46 -		&asn_DEF_UTF8String,
    8.47 -		0,	/* Defer constraints checking to the member type */
    8.48 -		0,	/* No PER visible constraints */
    8.49 -		0,
    8.50 -		"address"
    8.51 -		},
    8.52 -	{ ATF_NOFLAGS, 0, offsetof(struct Identity, fpr),
    8.53 -		(ASN_TAG_CLASS_UNIVERSAL | (12 << 2)),
    8.54 -		0,
    8.55 -		&asn_DEF_UTF8String,
    8.56 -		0,	/* Defer constraints checking to the member type */
    8.57 -		0,	/* No PER visible constraints */
    8.58 -		0,
    8.59 -		"fpr"
    8.60 -		},
    8.61 -	{ ATF_NOFLAGS, 0, offsetof(struct Identity, user_id),
    8.62 -		(ASN_TAG_CLASS_UNIVERSAL | (12 << 2)),
    8.63 -		0,
    8.64 -		&asn_DEF_UTF8String,
    8.65 -		0,	/* Defer constraints checking to the member type */
    8.66 -		0,	/* No PER visible constraints */
    8.67 -		0,
    8.68 -		"user-id"
    8.69 -		},
    8.70 -	{ ATF_NOFLAGS, 0, offsetof(struct Identity, username),
    8.71 -		(ASN_TAG_CLASS_UNIVERSAL | (12 << 2)),
    8.72 -		0,
    8.73 -		&asn_DEF_UTF8String,
    8.74 -		0,	/* Defer constraints checking to the member type */
    8.75 -		0,	/* No PER visible constraints */
    8.76 -		0,
    8.77 -		"username"
    8.78 -		},
    8.79 -	{ ATF_NOFLAGS, 0, offsetof(struct Identity, comm_type),
    8.80 -		(ASN_TAG_CLASS_UNIVERSAL | (2 << 2)),
    8.81 -		0,
    8.82 -		&asn_DEF_NativeInteger,
    8.83 -		memb_comm_type_constraint_1,
    8.84 -		&asn_PER_memb_comm_type_constr_6,
    8.85 -		0,
    8.86 -		"comm-type"
    8.87 -		},
    8.88 -	{ ATF_NOFLAGS, 0, offsetof(struct Identity, lang),
    8.89 -		(ASN_TAG_CLASS_UNIVERSAL | (19 << 2)),
    8.90 -		0,
    8.91 -		&asn_DEF_PrintableString,
    8.92 -		0,	/* Defer constraints checking to the member type */
    8.93 -		0,	/* No PER visible constraints */
    8.94 -		0,
    8.95 -		"lang"
    8.96 -		},
    8.97 -};
    8.98 -static ber_tlv_tag_t asn_DEF_Identity_tags_1[] = {
    8.99 -	(ASN_TAG_CLASS_UNIVERSAL | (16 << 2))
   8.100 -};
   8.101 -static asn_TYPE_tag2member_t asn_MAP_Identity_tag2el_1[] = {
   8.102 -    { (ASN_TAG_CLASS_UNIVERSAL | (2 << 2)), 4, 0, 0 }, /* comm-type */
   8.103 -    { (ASN_TAG_CLASS_UNIVERSAL | (12 << 2)), 0, 0, 3 }, /* address */
   8.104 -    { (ASN_TAG_CLASS_UNIVERSAL | (12 << 2)), 1, -1, 2 }, /* fpr */
   8.105 -    { (ASN_TAG_CLASS_UNIVERSAL | (12 << 2)), 2, -2, 1 }, /* user-id */
   8.106 -    { (ASN_TAG_CLASS_UNIVERSAL | (12 << 2)), 3, -3, 0 }, /* username */
   8.107 -    { (ASN_TAG_CLASS_UNIVERSAL | (19 << 2)), 5, 0, 0 } /* lang */
   8.108 -};
   8.109 -static asn_SEQUENCE_specifics_t asn_SPC_Identity_specs_1 = {
   8.110 -	sizeof(struct Identity),
   8.111 -	offsetof(struct Identity, _asn_ctx),
   8.112 -	asn_MAP_Identity_tag2el_1,
   8.113 -	6,	/* Count of tags in the map */
   8.114 -	0, 0, 0,	/* Optional elements (not needed) */
   8.115 -	-1,	/* Start extensions */
   8.116 -	-1	/* Stop extensions */
   8.117 -};
   8.118 -asn_TYPE_descriptor_t asn_DEF_Identity = {
   8.119 -	"Identity",
   8.120 -	"Identity",
   8.121 -	SEQUENCE_free,
   8.122 -	SEQUENCE_print,
   8.123 -	SEQUENCE_constraint,
   8.124 -	SEQUENCE_decode_ber,
   8.125 -	SEQUENCE_encode_der,
   8.126 -	SEQUENCE_decode_xer,
   8.127 -	SEQUENCE_encode_xer,
   8.128 -	SEQUENCE_decode_uper,
   8.129 -	SEQUENCE_encode_uper,
   8.130 -	0,	/* Use generic outmost tag fetcher */
   8.131 -	asn_DEF_Identity_tags_1,
   8.132 -	sizeof(asn_DEF_Identity_tags_1)
   8.133 -		/sizeof(asn_DEF_Identity_tags_1[0]), /* 1 */
   8.134 -	asn_DEF_Identity_tags_1,	/* Same as above */
   8.135 -	sizeof(asn_DEF_Identity_tags_1)
   8.136 -		/sizeof(asn_DEF_Identity_tags_1[0]), /* 1 */
   8.137 -	0,	/* No PER visible constraints */
   8.138 -	asn_MBR_Identity_1,
   8.139 -	6,	/* Elements count */
   8.140 -	&asn_SPC_Identity_specs_1	/* Additional specs */
   8.141 -};
   8.142 -
     9.1 --- a/asn.1/Identity.h	Sun May 15 21:23:36 2016 +0200
     9.2 +++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
     9.3 @@ -1,44 +0,0 @@
     9.4 -/*
     9.5 - * Generated by asn1c-0.9.26 (http://lionet.info/asn1c)
     9.6 - * From ASN.1 module "PEP"
     9.7 - * 	found in "pEp.asn1"
     9.8 - */
     9.9 -
    9.10 -#ifndef	_Identity_H_
    9.11 -#define	_Identity_H_
    9.12 -
    9.13 -
    9.14 -#include <asn_application.h>
    9.15 -
    9.16 -/* Including external dependencies */
    9.17 -#include <UTF8String.h>
    9.18 -#include <NativeInteger.h>
    9.19 -#include <PrintableString.h>
    9.20 -#include <constr_SEQUENCE.h>
    9.21 -
    9.22 -#ifdef __cplusplus
    9.23 -extern "C" {
    9.24 -#endif
    9.25 -
    9.26 -/* Identity */
    9.27 -typedef struct Identity {
    9.28 -	UTF8String_t	 address;
    9.29 -	UTF8String_t	 fpr;
    9.30 -	UTF8String_t	 user_id;
    9.31 -	UTF8String_t	 username;
    9.32 -	long	 comm_type;
    9.33 -	PrintableString_t	 lang;
    9.34 -	
    9.35 -	/* Context for parsing across buffer boundaries */
    9.36 -	asn_struct_ctx_t _asn_ctx;
    9.37 -} Identity_t;
    9.38 -
    9.39 -/* Implementation */
    9.40 -extern asn_TYPE_descriptor_t asn_DEF_Identity;
    9.41 -
    9.42 -#ifdef __cplusplus
    9.43 -}
    9.44 -#endif
    9.45 -
    9.46 -#endif	/* _Identity_H_ */
    9.47 -#include <asn_internal.h>
    10.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
    10.2 +++ b/asn.1/Makefile	Sun May 15 22:29:04 2016 +0200
    10.3 @@ -0,0 +1,14 @@
    10.4 +include ../Makefile.conf
    10.5 +
    10.6 +all: Beacon.c Beacon.h
    10.7 +
    10.8 +%.o: %.c %.h
    10.9 +	$(CC) $(CFLAGS) -I$(ASN1C_INCLUDE) -c $< -o $@
   10.10 +
   10.11 +Beacon.c Beacon.h: devicegroup.asn1
   10.12 +	$(ASN1C) -gen-PER -fcompound-names pEp.asn1 $<
   10.13 +
   10.14 +.PHONY: clean
   10.15 +
   10.16 +clean:
   10.17 +	rm -f *.o *.c *.h *.sample
    11.1 --- a/asn.1/Makefile.am.sample	Sun May 15 21:23:36 2016 +0200
    11.2 +++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
    11.3 @@ -1,100 +0,0 @@
    11.4 -ASN_MODULE_SOURCES=	\
    11.5 -	Identity.c	\
    11.6 -	Stringlist.c	\
    11.7 -	Version.c	\
    11.8 -	Header.c
    11.9 -
   11.10 -ASN_MODULE_HEADERS=	\
   11.11 -	Identity.h	\
   11.12 -	Stringlist.h	\
   11.13 -	Version.h	\
   11.14 -	Header.h
   11.15 -
   11.16 -ASN_MODULE_HEADERS+=UTF8String.h
   11.17 -ASN_MODULE_HEADERS+=INTEGER.h
   11.18 -ASN_MODULE_HEADERS+=NativeEnumerated.h
   11.19 -ASN_MODULE_SOURCES+=INTEGER.c
   11.20 -ASN_MODULE_SOURCES+=NativeEnumerated.c
   11.21 -ASN_MODULE_HEADERS+=NativeInteger.h
   11.22 -ASN_MODULE_SOURCES+=NativeInteger.c
   11.23 -ASN_MODULE_HEADERS+=PrintableString.h
   11.24 -ASN_MODULE_SOURCES+=PrintableString.c
   11.25 -ASN_MODULE_SOURCES+=UTF8String.c
   11.26 -ASN_MODULE_HEADERS+=asn_SEQUENCE_OF.h
   11.27 -ASN_MODULE_SOURCES+=asn_SEQUENCE_OF.c
   11.28 -ASN_MODULE_HEADERS+=asn_SET_OF.h
   11.29 -ASN_MODULE_SOURCES+=asn_SET_OF.c
   11.30 -ASN_MODULE_HEADERS+=constr_SEQUENCE.h
   11.31 -ASN_MODULE_SOURCES+=constr_SEQUENCE.c
   11.32 -ASN_MODULE_HEADERS+=constr_SEQUENCE_OF.h
   11.33 -ASN_MODULE_SOURCES+=constr_SEQUENCE_OF.c
   11.34 -ASN_MODULE_HEADERS+=constr_SET_OF.h
   11.35 -ASN_MODULE_SOURCES+=constr_SET_OF.c
   11.36 -ASN_MODULE_HEADERS+=asn_application.h
   11.37 -ASN_MODULE_HEADERS+=asn_system.h
   11.38 -ASN_MODULE_HEADERS+=asn_codecs.h
   11.39 -ASN_MODULE_HEADERS+=asn_internal.h
   11.40 -ASN_MODULE_HEADERS+=OCTET_STRING.h
   11.41 -ASN_MODULE_SOURCES+=OCTET_STRING.c
   11.42 -ASN_MODULE_HEADERS+=BIT_STRING.h
   11.43 -ASN_MODULE_SOURCES+=BIT_STRING.c
   11.44 -ASN_MODULE_SOURCES+=asn_codecs_prim.c
   11.45 -ASN_MODULE_HEADERS+=asn_codecs_prim.h
   11.46 -ASN_MODULE_HEADERS+=ber_tlv_length.h
   11.47 -ASN_MODULE_SOURCES+=ber_tlv_length.c
   11.48 -ASN_MODULE_HEADERS+=ber_tlv_tag.h
   11.49 -ASN_MODULE_SOURCES+=ber_tlv_tag.c
   11.50 -ASN_MODULE_HEADERS+=ber_decoder.h
   11.51 -ASN_MODULE_SOURCES+=ber_decoder.c
   11.52 -ASN_MODULE_HEADERS+=der_encoder.h
   11.53 -ASN_MODULE_SOURCES+=der_encoder.c
   11.54 -ASN_MODULE_HEADERS+=constr_TYPE.h
   11.55 -ASN_MODULE_SOURCES+=constr_TYPE.c
   11.56 -ASN_MODULE_HEADERS+=constraints.h
   11.57 -ASN_MODULE_SOURCES+=constraints.c
   11.58 -ASN_MODULE_HEADERS+=xer_support.h
   11.59 -ASN_MODULE_SOURCES+=xer_support.c
   11.60 -ASN_MODULE_HEADERS+=xer_decoder.h
   11.61 -ASN_MODULE_SOURCES+=xer_decoder.c
   11.62 -ASN_MODULE_HEADERS+=xer_encoder.h
   11.63 -ASN_MODULE_SOURCES+=xer_encoder.c
   11.64 -ASN_MODULE_HEADERS+=per_support.h
   11.65 -ASN_MODULE_SOURCES+=per_support.c
   11.66 -ASN_MODULE_HEADERS+=per_decoder.h
   11.67 -ASN_MODULE_SOURCES+=per_decoder.c
   11.68 -ASN_MODULE_HEADERS+=per_encoder.h
   11.69 -ASN_MODULE_SOURCES+=per_encoder.c
   11.70 -ASN_MODULE_HEADERS+=per_opentype.h
   11.71 -ASN_MODULE_SOURCES+=per_opentype.c
   11.72 -ASN_CONVERTER_SOURCES+=converter-sample.c
   11.73 -
   11.74 -
   11.75 -lib_LTLIBRARIES=libsomething.la
   11.76 -libsomething_la_SOURCES=$(ASN_MODULE_SOURCES) $(ASN_MODULE_HEADERS)
   11.77 -
   11.78 -# This file may be used as an input for make(3)
   11.79 -# Remove the lines below to convert it into a pure .am file
   11.80 -TARGET = progname
   11.81 -CFLAGS += -I.
   11.82 -OBJS=${ASN_MODULE_SOURCES:.c=.o} ${ASN_CONVERTER_SOURCES:.c=.o}
   11.83 -
   11.84 -all: $(TARGET)
   11.85 -
   11.86 -$(TARGET): ${OBJS}
   11.87 -	$(CC) $(CFLAGS) -o $(TARGET) ${OBJS} $(LDFLAGS) $(LIBS)
   11.88 -
   11.89 -.SUFFIXES:
   11.90 -.SUFFIXES: .c .o
   11.91 -
   11.92 -.c.o:
   11.93 -	$(CC) $(CFLAGS) -o $@ -c $<
   11.94 -
   11.95 -clean:
   11.96 -	rm -f $(TARGET)
   11.97 -	rm -f $(OBJS)
   11.98 -
   11.99 -regen: regenerate-from-asn1-source
  11.100 -
  11.101 -regenerate-from-asn1-source:
  11.102 -	asn1c -gen-PER pEp.asn1
  11.103 -
    12.1 --- a/asn.1/NativeEnumerated.c	Sun May 15 21:23:36 2016 +0200
    12.2 +++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
    12.3 @@ -1,207 +0,0 @@
    12.4 -/*-
    12.5 - * Copyright (c) 2004, 2007 Lev Walkin <vlm@lionet.info>. All rights reserved.
    12.6 - * Redistribution and modifications are permitted subject to BSD license.
    12.7 - */
    12.8 -/*
    12.9 - * Read the NativeInteger.h for the explanation wrt. differences between
   12.10 - * INTEGER and NativeInteger.
   12.11 - * Basically, both are decoders and encoders of ASN.1 INTEGER type, but this
   12.12 - * implementation deals with the standard (machine-specific) representation
   12.13 - * of them instead of using the platform-independent buffer.
   12.14 - */
   12.15 -#include <asn_internal.h>
   12.16 -#include <NativeEnumerated.h>
   12.17 -
   12.18 -/*
   12.19 - * NativeEnumerated basic type description.
   12.20 - */
   12.21 -static ber_tlv_tag_t asn_DEF_NativeEnumerated_tags[] = {
   12.22 -	(ASN_TAG_CLASS_UNIVERSAL | (10 << 2))
   12.23 -};
   12.24 -asn_TYPE_descriptor_t asn_DEF_NativeEnumerated = {
   12.25 -	"ENUMERATED",			/* The ASN.1 type is still ENUMERATED */
   12.26 -	"ENUMERATED",
   12.27 -	NativeInteger_free,
   12.28 -	NativeInteger_print,
   12.29 -	asn_generic_no_constraint,
   12.30 -	NativeInteger_decode_ber,
   12.31 -	NativeInteger_encode_der,
   12.32 -	NativeInteger_decode_xer,
   12.33 -	NativeEnumerated_encode_xer,
   12.34 -	NativeEnumerated_decode_uper,
   12.35 -	NativeEnumerated_encode_uper,
   12.36 -	0, /* Use generic outmost tag fetcher */
   12.37 -	asn_DEF_NativeEnumerated_tags,
   12.38 -	sizeof(asn_DEF_NativeEnumerated_tags) / sizeof(asn_DEF_NativeEnumerated_tags[0]),
   12.39 -	asn_DEF_NativeEnumerated_tags,	/* Same as above */
   12.40 -	sizeof(asn_DEF_NativeEnumerated_tags) / sizeof(asn_DEF_NativeEnumerated_tags[0]),
   12.41 -	0,	/* No PER visible constraints */
   12.42 -	0, 0,	/* No members */
   12.43 -	0	/* No specifics */
   12.44 -};
   12.45 -
   12.46 -asn_enc_rval_t
   12.47 -NativeEnumerated_encode_xer(asn_TYPE_descriptor_t *td, void *sptr,
   12.48 -        int ilevel, enum xer_encoder_flags_e flags,
   12.49 -                asn_app_consume_bytes_f *cb, void *app_key) {
   12.50 -	asn_INTEGER_specifics_t *specs=(asn_INTEGER_specifics_t *)td->specifics;
   12.51 -        asn_enc_rval_t er;
   12.52 -        const long *native = (const long *)sptr;
   12.53 -	const asn_INTEGER_enum_map_t *el;
   12.54 -
   12.55 -        (void)ilevel;
   12.56 -        (void)flags;
   12.57 -
   12.58 -        if(!native) _ASN_ENCODE_FAILED;
   12.59 -
   12.60 -	el = INTEGER_map_value2enum(specs, *native);
   12.61 -	if(el) {
   12.62 -		size_t srcsize = el->enum_len + 5;
   12.63 -		char *src = (char *)alloca(srcsize);
   12.64 -
   12.65 -		er.encoded = snprintf(src, srcsize, "<%s/>", el->enum_name);
   12.66 -		assert(er.encoded > 0 && (size_t)er.encoded < srcsize);
   12.67 -		if(cb(src, er.encoded, app_key) < 0) _ASN_ENCODE_FAILED;
   12.68 -		_ASN_ENCODED_OK(er);
   12.69 -	} else {
   12.70 -		ASN_DEBUG("ASN.1 forbids dealing with "
   12.71 -			"unknown value of ENUMERATED type");
   12.72 -		_ASN_ENCODE_FAILED;
   12.73 -	}
   12.74 -}
   12.75 -
   12.76 -asn_dec_rval_t
   12.77 -NativeEnumerated_decode_uper(asn_codec_ctx_t *opt_codec_ctx,
   12.78 -	asn_TYPE_descriptor_t *td, asn_per_constraints_t *constraints,
   12.79 -	void **sptr, asn_per_data_t *pd) {
   12.80 -	asn_INTEGER_specifics_t *specs = (asn_INTEGER_specifics_t *)td->specifics;
   12.81 -	asn_dec_rval_t rval = { RC_OK, 0 };
   12.82 -	long *native = (long *)*sptr;
   12.83 -	asn_per_constraint_t *ct;
   12.84 -	long value;
   12.85 -
   12.86 -	(void)opt_codec_ctx;
   12.87 -
   12.88 -	if(constraints) ct = &constraints->value;
   12.89 -	else if(td->per_constraints) ct = &td->per_constraints->value;
   12.90 -	else _ASN_DECODE_FAILED;	/* Mandatory! */
   12.91 -	if(!specs) _ASN_DECODE_FAILED;
   12.92 -
   12.93 -	if(!native) {
   12.94 -		native = (long *)(*sptr = CALLOC(1, sizeof(*native)));
   12.95 -		if(!native) _ASN_DECODE_FAILED;
   12.96 -	}
   12.97 -
   12.98 -	ASN_DEBUG("Decoding %s as NativeEnumerated", td->name);
   12.99 -
  12.100 -	if(ct->flags & APC_EXTENSIBLE) {
  12.101 -		int inext = per_get_few_bits(pd, 1);
  12.102 -		if(inext < 0) _ASN_DECODE_STARVED;
  12.103 -		if(inext) ct = 0;
  12.104 -	}
  12.105 -
  12.106 -	if(ct && ct->range_bits >= 0) {
  12.107 -		value = per_get_few_bits(pd, ct->range_bits);
  12.108 -		if(value < 0) _ASN_DECODE_STARVED;
  12.109 -		if(value >= (specs->extension
  12.110 -			? specs->extension - 1 : specs->map_count))
  12.111 -			_ASN_DECODE_FAILED;
  12.112 -	} else {
  12.113 -		if(!specs->extension)
  12.114 -			_ASN_DECODE_FAILED;
  12.115 -		/*
  12.116 -		 * X.691, #10.6: normally small non-negative whole number;
  12.117 -		 */
  12.118 -		value = uper_get_nsnnwn(pd);
  12.119 -		if(value < 0) _ASN_DECODE_STARVED;
  12.120 -		value += specs->extension - 1;
  12.121 -		if(value >= specs->map_count)
  12.122 -			_ASN_DECODE_FAILED;
  12.123 -	}
  12.124 -
  12.125 -	*native = specs->value2enum[value].nat_value;
  12.126 -	ASN_DEBUG("Decoded %s = %ld", td->name, *native);
  12.127 -
  12.128 -	return rval;
  12.129 -}
  12.130 -
  12.131 -static int
  12.132 -NativeEnumerated__compar_value2enum(const void *ap, const void *bp) {
  12.133 -	const asn_INTEGER_enum_map_t *a = ap;
  12.134 -	const asn_INTEGER_enum_map_t *b = bp;
  12.135 -	if(a->nat_value == b->nat_value)
  12.136 -		return 0;
  12.137 -	if(a->nat_value < b->nat_value)
  12.138 -		return -1;
  12.139 -	return 1;
  12.140 -}
  12.141 -
  12.142 -asn_enc_rval_t
  12.143 -NativeEnumerated_encode_uper(asn_TYPE_descriptor_t *td,
  12.144 -	asn_per_constraints_t *constraints, void *sptr, asn_per_outp_t *po) {
  12.145 -	asn_INTEGER_specifics_t *specs = (asn_INTEGER_specifics_t *)td->specifics;
  12.146 -	asn_enc_rval_t er;
  12.147 -	long native, value;
  12.148 -	asn_per_constraint_t *ct;
  12.149 -	int inext = 0;
  12.150 -	asn_INTEGER_enum_map_t key;
  12.151 -	asn_INTEGER_enum_map_t *kf;
  12.152 -
  12.153 -	if(!sptr) _ASN_ENCODE_FAILED;
  12.154 -	if(!specs) _ASN_ENCODE_FAILED;
  12.155 -
  12.156 -	if(constraints) ct = &constraints->value;
  12.157 -	else if(td->per_constraints) ct = &td->per_constraints->value;
  12.158 -	else _ASN_ENCODE_FAILED;	/* Mandatory! */
  12.159 -
  12.160 -	ASN_DEBUG("Encoding %s as NativeEnumerated", td->name);
  12.161 -
  12.162 -	er.encoded = 0;
  12.163 -
  12.164 -	native = *(long *)sptr;
  12.165 -	if(native < 0) _ASN_ENCODE_FAILED;
  12.166 -
  12.167 -	key.nat_value = native;
  12.168 -	kf = bsearch(&key, specs->value2enum, specs->map_count,
  12.169 -		sizeof(key), NativeEnumerated__compar_value2enum);
  12.170 -	if(!kf) {
  12.171 -		ASN_DEBUG("No element corresponds to %ld", native);
  12.172 -		_ASN_ENCODE_FAILED;
  12.173 -	}
  12.174 -	value = kf - specs->value2enum;
  12.175 -
  12.176 -	if(ct->range_bits >= 0) {
  12.177 -		int cmpWith = specs->extension
  12.178 -				? specs->extension - 1 : specs->map_count;
  12.179 -		if(value >= cmpWith)
  12.180 -			inext = 1;
  12.181 -	}
  12.182 -	if(ct->flags & APC_EXTENSIBLE) {
  12.183 -		if(per_put_few_bits(po, inext, 1))
  12.184 -			_ASN_ENCODE_FAILED;
  12.185 -		if(inext) ct = 0;
  12.186 -	} else if(inext) {
  12.187 -		_ASN_ENCODE_FAILED;
  12.188 -	}
  12.189 -
  12.190 -	if(ct && ct->range_bits >= 0) {
  12.191 -		if(per_put_few_bits(po, value, ct->range_bits))
  12.192 -			_ASN_ENCODE_FAILED;
  12.193 -		_ASN_ENCODED_OK(er);
  12.194 -	}
  12.195 -
  12.196 -	if(!specs->extension)
  12.197 -		_ASN_ENCODE_FAILED;
  12.198 -
  12.199 -	/*
  12.200 -	 * X.691, #10.6: normally small non-negative whole number;
  12.201 -	 */
  12.202 -	ASN_DEBUG("value = %ld, ext = %d, inext = %d, res = %ld",
  12.203 -		value, specs->extension, inext,
  12.204 -		value - (inext ? (specs->extension - 1) : 0));
  12.205 -	if(uper_put_nsnnwn(po, value - (inext ? (specs->extension - 1) : 0)))
  12.206 -		_ASN_ENCODE_FAILED;
  12.207 -
  12.208 -	_ASN_ENCODED_OK(er);
  12.209 -}
  12.210 -
    13.1 --- a/asn.1/NativeEnumerated.h	Sun May 15 21:23:36 2016 +0200
    13.2 +++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
    13.3 @@ -1,32 +0,0 @@
    13.4 -/*-
    13.5 - * Copyright (c) 2004, 2005, 2006 Lev Walkin <vlm@lionet.info>.
    13.6 - * All rights reserved.
    13.7 - * Redistribution and modifications are permitted subject to BSD license.
    13.8 - */
    13.9 -/*
   13.10 - * This type differs from the standard ENUMERATED in that it is modelled using
   13.11 - * the fixed machine type (long, int, short), so it can hold only values of
   13.12 - * limited length. There is no type (i.e., NativeEnumerated_t, any integer type
   13.13 - * will do).
   13.14 - * This type may be used when integer range is limited by subtype constraints.
   13.15 - */
   13.16 -#ifndef	_NativeEnumerated_H_
   13.17 -#define	_NativeEnumerated_H_
   13.18 -
   13.19 -#include <NativeInteger.h>
   13.20 -
   13.21 -#ifdef __cplusplus
   13.22 -extern "C" {
   13.23 -#endif
   13.24 -
   13.25 -extern asn_TYPE_descriptor_t asn_DEF_NativeEnumerated;
   13.26 -
   13.27 -xer_type_encoder_f NativeEnumerated_encode_xer;
   13.28 -per_type_decoder_f NativeEnumerated_decode_uper;
   13.29 -per_type_encoder_f NativeEnumerated_encode_uper;
   13.30 -
   13.31 -#ifdef __cplusplus
   13.32 -}
   13.33 -#endif
   13.34 -
   13.35 -#endif	/* _NativeEnumerated_H_ */
    14.1 --- a/asn.1/NativeInteger.c	Sun May 15 21:23:36 2016 +0200
    14.2 +++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
    14.3 @@ -1,332 +0,0 @@
    14.4 -/*-
    14.5 - * Copyright (c) 2004, 2005, 2006 Lev Walkin <vlm@lionet.info>.
    14.6 - * All rights reserved.
    14.7 - * Redistribution and modifications are permitted subject to BSD license.
    14.8 - */
    14.9 -/*
   14.10 - * Read the NativeInteger.h for the explanation wrt. differences between
   14.11 - * INTEGER and NativeInteger.
   14.12 - * Basically, both are decoders and encoders of ASN.1 INTEGER type, but this
   14.13 - * implementation deals with the standard (machine-specific) representation
   14.14 - * of them instead of using the platform-independent buffer.
   14.15 - */
   14.16 -#include <asn_internal.h>
   14.17 -#include <NativeInteger.h>
   14.18 -
   14.19 -/*
   14.20 - * NativeInteger basic type description.
   14.21 - */
   14.22 -static ber_tlv_tag_t asn_DEF_NativeInteger_tags[] = {
   14.23 -	(ASN_TAG_CLASS_UNIVERSAL | (2 << 2))
   14.24 -};
   14.25 -asn_TYPE_descriptor_t asn_DEF_NativeInteger = {
   14.26 -	"INTEGER",			/* The ASN.1 type is still INTEGER */
   14.27 -	"INTEGER",
   14.28 -	NativeInteger_free,
   14.29 -	NativeInteger_print,
   14.30 -	asn_generic_no_constraint,
   14.31 -	NativeInteger_decode_ber,
   14.32 -	NativeInteger_encode_der,
   14.33 -	NativeInteger_decode_xer,
   14.34 -	NativeInteger_encode_xer,
   14.35 -	NativeInteger_decode_uper,	/* Unaligned PER decoder */
   14.36 -	NativeInteger_encode_uper,	/* Unaligned PER encoder */
   14.37 -	0, /* Use generic outmost tag fetcher */
   14.38 -	asn_DEF_NativeInteger_tags,
   14.39 -	sizeof(asn_DEF_NativeInteger_tags) / sizeof(asn_DEF_NativeInteger_tags[0]),
   14.40 -	asn_DEF_NativeInteger_tags,	/* Same as above */
   14.41 -	sizeof(asn_DEF_NativeInteger_tags) / sizeof(asn_DEF_NativeInteger_tags[0]),
   14.42 -	0,	/* No PER visible constraints */
   14.43 -	0, 0,	/* No members */
   14.44 -	0	/* No specifics */
   14.45 -};
   14.46 -
   14.47 -/*
   14.48 - * Decode INTEGER type.
   14.49 - */
   14.50 -asn_dec_rval_t
   14.51 -NativeInteger_decode_ber(asn_codec_ctx_t *opt_codec_ctx,
   14.52 -	asn_TYPE_descriptor_t *td,
   14.53 -	void **nint_ptr, const void *buf_ptr, size_t size, int tag_mode) {
   14.54 -	asn_INTEGER_specifics_t *specs=(asn_INTEGER_specifics_t *)td->specifics;
   14.55 -	long *native = (long *)*nint_ptr;
   14.56 -	asn_dec_rval_t rval;
   14.57 -	ber_tlv_len_t length;
   14.58 -
   14.59 -	/*
   14.60 -	 * If the structure is not there, allocate it.
   14.61 -	 */
   14.62 -	if(native == NULL) {
   14.63 -		native = (long *)(*nint_ptr = CALLOC(1, sizeof(*native)));
   14.64 -		if(native == NULL) {
   14.65 -			rval.code = RC_FAIL;
   14.66 -			rval.consumed = 0;
   14.67 -			return rval;
   14.68 -		}
   14.69 -	}
   14.70 -
   14.71 -	ASN_DEBUG("Decoding %s as INTEGER (tm=%d)",
   14.72 -		td->name, tag_mode);
   14.73 -
   14.74 -	/*
   14.75 -	 * Check tags.
   14.76 -	 */
   14.77 -	rval = ber_check_tags(opt_codec_ctx, td, 0, buf_ptr, size,
   14.78 -			tag_mode, 0, &length, 0);
   14.79 -	if(rval.code != RC_OK)
   14.80 -		return rval;
   14.81 -
   14.82 -	ASN_DEBUG("%s length is %d bytes", td->name, (int)length);
   14.83 -
   14.84 -	/*
   14.85 -	 * Make sure we have this length.
   14.86 -	 */
   14.87 -	buf_ptr = ((const char *)buf_ptr) + rval.consumed;
   14.88 -	size -= rval.consumed;
   14.89 -	if(length > (ber_tlv_len_t)size) {
   14.90 -		rval.code = RC_WMORE;
   14.91 -		rval.consumed = 0;
   14.92 -		return rval;
   14.93 -	}
   14.94 -
   14.95 -	/*
   14.96 -	 * ASN.1 encoded INTEGER: buf_ptr, length
   14.97 -	 * Fill the native, at the same time checking for overflow.
   14.98 -	 * If overflow occured, return with RC_FAIL.
   14.99 -	 */
  14.100 -	{
  14.101 -		INTEGER_t tmp;
  14.102 -		union {
  14.103 -			const void *constbuf;
  14.104 -			void *nonconstbuf;
  14.105 -		} unconst_buf;
  14.106 -		long l;
  14.107 -
  14.108 -		unconst_buf.constbuf = buf_ptr;
  14.109 -		tmp.buf = (uint8_t *)unconst_buf.nonconstbuf;
  14.110 -		tmp.size = length;
  14.111 -
  14.112 -		if((specs&&specs->field_unsigned)
  14.113 -			? asn_INTEGER2ulong(&tmp, (unsigned long *)&l) /* sic */
  14.114 -			: asn_INTEGER2long(&tmp, &l)) {
  14.115 -			rval.code = RC_FAIL;
  14.116 -			rval.consumed = 0;
  14.117 -			return rval;
  14.118 -		}
  14.119 -
  14.120 -		*native = l;
  14.121 -	}
  14.122 -
  14.123 -	rval.code = RC_OK;
  14.124 -	rval.consumed += length;
  14.125 -
  14.126 -	ASN_DEBUG("Took %ld/%ld bytes to encode %s (%ld)",
  14.127 -		(long)rval.consumed, (long)length, td->name, (long)*native);
  14.128 -
  14.129 -	return rval;
  14.130 -}
  14.131 -
  14.132 -/*
  14.133 - * Encode the NativeInteger using the standard INTEGER type DER encoder.
  14.134 - */
  14.135 -asn_enc_rval_t
  14.136 -NativeInteger_encode_der(asn_TYPE_descriptor_t *sd, void *ptr,
  14.137 -	int tag_mode, ber_tlv_tag_t tag,
  14.138 -	asn_app_consume_bytes_f *cb, void *app_key) {
  14.139 -	unsigned long native = *(unsigned long *)ptr;	/* Disable sign ext. */
  14.140 -	asn_enc_rval_t erval;
  14.141 -	INTEGER_t tmp;
  14.142 -
  14.143 -#ifdef	WORDS_BIGENDIAN		/* Opportunistic optimization */
  14.144 -
  14.145 -	tmp.buf = (uint8_t *)&native;
  14.146 -	tmp.size = sizeof(native);
  14.147 -
  14.148 -#else	/* Works even if WORDS_BIGENDIAN is not set where should've been */
  14.149 -	uint8_t buf[sizeof(native)];
  14.150 -	uint8_t *p;
  14.151 -
  14.152 -	/* Prepare a fake INTEGER */
  14.153 -	for(p = buf + sizeof(buf) - 1; p >= buf; p--, native >>= 8)
  14.154 -		*p = (uint8_t)native;
  14.155 -
  14.156 -	tmp.buf = buf;
  14.157 -	tmp.size = sizeof(buf);
  14.158 -#endif	/* WORDS_BIGENDIAN */
  14.159 -	
  14.160 -	/* Encode fake INTEGER */
  14.161 -	erval = INTEGER_encode_der(sd, &tmp, tag_mode, tag, cb, app_key);
  14.162 -	if(erval.encoded == -1) {
  14.163 -		assert(erval.structure_ptr == &tmp);
  14.164 -		erval.structure_ptr = ptr;
  14.165 -	}
  14.166 -	return erval;
  14.167 -}
  14.168 -
  14.169 -/*
  14.170 - * Decode the chunk of XML text encoding INTEGER.
  14.171 - */
  14.172 -asn_dec_rval_t
  14.173 -NativeInteger_decode_xer(asn_codec_ctx_t *opt_codec_ctx,
  14.174 -	asn_TYPE_descriptor_t *td, void **sptr, const char *opt_mname,
  14.175 -		const void *buf_ptr, size_t size) {
  14.176 -	asn_INTEGER_specifics_t *specs=(asn_INTEGER_specifics_t *)td->specifics;
  14.177 -	asn_dec_rval_t rval;
  14.178 -	INTEGER_t st;
  14.179 -	void *st_ptr = (void *)&st;
  14.180 -	long *native = (long *)*sptr;
  14.181 -
  14.182 -	if(!native) {
  14.183 -		native = (long *)(*sptr = CALLOC(1, sizeof(*native)));
  14.184 -		if(!native) _ASN_DECODE_FAILED;
  14.185 -	}
  14.186 -
  14.187 -	memset(&st, 0, sizeof(st));
  14.188 -	rval = INTEGER_decode_xer(opt_codec_ctx, td, &st_ptr, 
  14.189 -		opt_mname, buf_ptr, size);
  14.190 -	if(rval.code == RC_OK) {
  14.191 -		long l;
  14.192 -		if((specs&&specs->field_unsigned)
  14.193 -			? asn_INTEGER2ulong(&st, (unsigned long *)&l) /* sic */
  14.194 -			: asn_INTEGER2long(&st, &l)) {
  14.195 -			rval.code = RC_FAIL;
  14.196 -			rval.consumed = 0;
  14.197 -		} else {
  14.198 -			*native = l;
  14.199 -		}
  14.200 -	} else {
  14.201 -		/*
  14.202 -		 * Cannot restart from the middle;
  14.203 -		 * there is no place to save state in the native type.
  14.204 -		 * Request a continuation from the very beginning.
  14.205 -		 */
  14.206 -		rval.consumed = 0;
  14.207 -	}
  14.208 -	ASN_STRUCT_FREE_CONTENTS_ONLY(asn_DEF_INTEGER, &st);
  14.209 -	return rval;
  14.210 -}
  14.211 -
  14.212 -
  14.213 -asn_enc_rval_t
  14.214 -NativeInteger_encode_xer(asn_TYPE_descriptor_t *td, void *sptr,
  14.215 -	int ilevel, enum xer_encoder_flags_e flags,
  14.216 -		asn_app_consume_bytes_f *cb, void *app_key) {
  14.217 -	asn_INTEGER_specifics_t *specs=(asn_INTEGER_specifics_t *)td->specifics;
  14.218 -	char scratch[32];	/* Enough for 64-bit int */
  14.219 -	asn_enc_rval_t er;
  14.220 -	const long *native = (const long *)sptr;
  14.221 -
  14.222 -	(void)ilevel;
  14.223 -	(void)flags;
  14.224 -
  14.225 -	if(!native) _ASN_ENCODE_FAILED;
  14.226 -
  14.227 -	er.encoded = snprintf(scratch, sizeof(scratch),
  14.228 -			(specs && specs->field_unsigned)
  14.229 -			? "%lu" : "%ld", *native);
  14.230 -	if(er.encoded <= 0 || (size_t)er.encoded >= sizeof(scratch)
  14.231 -		|| cb(scratch, er.encoded, app_key) < 0)
  14.232 -		_ASN_ENCODE_FAILED;
  14.233 -
  14.234 -	_ASN_ENCODED_OK(er);
  14.235 -}
  14.236 -
  14.237 -asn_dec_rval_t
  14.238 -NativeInteger_decode_uper(asn_codec_ctx_t *opt_codec_ctx,
  14.239 -	asn_TYPE_descriptor_t *td,
  14.240 -	asn_per_constraints_t *constraints, void **sptr, asn_per_data_t *pd) {
  14.241 -
  14.242 -	asn_INTEGER_specifics_t *specs=(asn_INTEGER_specifics_t *)td->specifics;
  14.243 -	asn_dec_rval_t rval;
  14.244 -	long *native = (long *)*sptr;
  14.245 -	INTEGER_t tmpint;
  14.246 -	void *tmpintptr = &tmpint;
  14.247 -
  14.248 -	(void)opt_codec_ctx;
  14.249 -	ASN_DEBUG("Decoding NativeInteger %s (UPER)", td->name);
  14.250 -
  14.251 -	if(!native) {
  14.252 -		native = (long *)(*sptr = CALLOC(1, sizeof(*native)));
  14.253 -		if(!native) _ASN_DECODE_FAILED;
  14.254 -	}
  14.255 -
  14.256 -	memset(&tmpint, 0, sizeof tmpint);
  14.257 -	rval = INTEGER_decode_uper(opt_codec_ctx, td, constraints,
  14.258 -				   &tmpintptr, pd);
  14.259 -	if(rval.code == RC_OK) {
  14.260 -		if((specs&&specs->field_unsigned)
  14.261 -			? asn_INTEGER2ulong(&tmpint, (unsigned long *)native)
  14.262 -			: asn_INTEGER2long(&tmpint, native))
  14.263 -			rval.code = RC_FAIL;
  14.264 -		else
  14.265 -			ASN_DEBUG("NativeInteger %s got value %ld",
  14.266 -				td->name, *native);
  14.267 -	}
  14.268 -	ASN_STRUCT_FREE_CONTENTS_ONLY(asn_DEF_INTEGER, &tmpint);
  14.269 -
  14.270 -	return rval;
  14.271 -}
  14.272 -
  14.273 -asn_enc_rval_t
  14.274 -NativeInteger_encode_uper(asn_TYPE_descriptor_t *td,
  14.275 -	asn_per_constraints_t *constraints, void *sptr, asn_per_outp_t *po) {
  14.276 -	asn_INTEGER_specifics_t *specs=(asn_INTEGER_specifics_t *)td->specifics;
  14.277 -	asn_enc_rval_t er;
  14.278 -	long native;
  14.279 -	INTEGER_t tmpint;
  14.280 -
  14.281 -	if(!sptr) _ASN_ENCODE_FAILED;
  14.282 -
  14.283 -	native = *(long *)sptr;
  14.284 -
  14.285 -	ASN_DEBUG("Encoding NativeInteger %s %ld (UPER)", td->name, native);
  14.286 -
  14.287 -	memset(&tmpint, 0, sizeof(tmpint));
  14.288 -	if((specs&&specs->field_unsigned)
  14.289 -		? asn_ulong2INTEGER(&tmpint, native)
  14.290 -		: asn_long2INTEGER(&tmpint, native))
  14.291 -		_ASN_ENCODE_FAILED;
  14.292 -	er = INTEGER_encode_uper(td, constraints, &tmpint, po);
  14.293 -	ASN_STRUCT_FREE_CONTENTS_ONLY(asn_DEF_INTEGER, &tmpint);
  14.294 -	return er;
  14.295 -}
  14.296 -
  14.297 -/*
  14.298 - * INTEGER specific human-readable output.
  14.299 - */
  14.300 -int
  14.301 -NativeInteger_print(asn_TYPE_descriptor_t *td, const void *sptr, int ilevel,
  14.302 -	asn_app_consume_bytes_f *cb, void *app_key) {
  14.303 -	asn_INTEGER_specifics_t *specs=(asn_INTEGER_specifics_t *)td->specifics;
  14.304 -	const long *native = (const long *)sptr;
  14.305 -	char scratch[32];	/* Enough for 64-bit int */
  14.306 -	int ret;
  14.307 -
  14.308 -	(void)td;	/* Unused argument */
  14.309 -	(void)ilevel;	/* Unused argument */
  14.310 -
  14.311 -	if(native) {
  14.312 -		ret = snprintf(scratch, sizeof(scratch),
  14.313 -			(specs && specs->field_unsigned)
  14.314 -			? "%lu" : "%ld", *native);
  14.315 -		assert(ret > 0 && (size_t)ret < sizeof(scratch));
  14.316 -		return (cb(scratch, ret, app_key) < 0) ? -1 : 0;
  14.317 -	} else {
  14.318 -		return (cb("<absent>", 8, app_key) < 0) ? -1 : 0;
  14.319 -	}
  14.320 -}
  14.321 -
  14.322 -void
  14.323 -NativeInteger_free(asn_TYPE_descriptor_t *td, void *ptr, int contents_only) {
  14.324 -
  14.325 -	if(!td || !ptr)
  14.326 -		return;
  14.327 -
  14.328 -	ASN_DEBUG("Freeing %s as INTEGER (%d, %p, Native)",
  14.329 -		td->name, contents_only, ptr);
  14.330 -
  14.331 -	if(!contents_only) {
  14.332 -		FREEMEM(ptr);
  14.333 -	}
  14.334 -}
  14.335 -
    15.1 --- a/asn.1/NativeInteger.h	Sun May 15 21:23:36 2016 +0200
    15.2 +++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
    15.3 @@ -1,37 +0,0 @@
    15.4 -/*-
    15.5 - * Copyright (c) 2004 Lev Walkin <vlm@lionet.info>. All rights reserved.
    15.6 - * Redistribution and modifications are permitted subject to BSD license.
    15.7 - */
    15.8 -/*
    15.9 - * This type differs from the standard INTEGER in that it is modelled using
   15.10 - * the fixed machine type (long, int, short), so it can hold only values of
   15.11 - * limited length. There is no type (i.e., NativeInteger_t, any integer type
   15.12 - * will do).
   15.13 - * This type may be used when integer range is limited by subtype constraints.
   15.14 - */
   15.15 -#ifndef	_NativeInteger_H_
   15.16 -#define	_NativeInteger_H_
   15.17 -
   15.18 -#include <asn_application.h>
   15.19 -#include <INTEGER.h>
   15.20 -
   15.21 -#ifdef __cplusplus
   15.22 -extern "C" {
   15.23 -#endif
   15.24 -
   15.25 -extern asn_TYPE_descriptor_t asn_DEF_NativeInteger;
   15.26 -
   15.27 -asn_struct_free_f  NativeInteger_free;
   15.28 -asn_struct_print_f NativeInteger_print;
   15.29 -ber_type_decoder_f NativeInteger_decode_ber;
   15.30 -der_type_encoder_f NativeInteger_encode_der;
   15.31 -xer_type_decoder_f NativeInteger_decode_xer;
   15.32 -xer_type_encoder_f NativeInteger_encode_xer;
   15.33 -per_type_decoder_f NativeInteger_decode_uper;
   15.34 -per_type_encoder_f NativeInteger_encode_uper;
   15.35 -
   15.36 -#ifdef __cplusplus
   15.37 -}
   15.38 -#endif
   15.39 -
   15.40 -#endif	/* _NativeInteger_H_ */
    16.1 --- a/asn.1/OCTET_STRING.c	Sun May 15 21:23:36 2016 +0200
    16.2 +++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
    16.3 @@ -1,1805 +0,0 @@
    16.4 -/*-
    16.5 - * Copyright (c) 2003, 2004, 2005, 2006 Lev Walkin <vlm@lionet.info>.
    16.6 - * All rights reserved.
    16.7 - * Redistribution and modifications are permitted subject to BSD license.
    16.8 - */
    16.9 -#include <asn_internal.h>
   16.10 -#include <OCTET_STRING.h>
   16.11 -#include <BIT_STRING.h>	/* for .bits_unused member */
   16.12 -#include <errno.h>
   16.13 -
   16.14 -/*
   16.15 - * OCTET STRING basic type description.
   16.16 - */
   16.17 -static ber_tlv_tag_t asn_DEF_OCTET_STRING_tags[] = {
   16.18 -	(ASN_TAG_CLASS_UNIVERSAL | (4 << 2))
   16.19 -};
   16.20 -static asn_OCTET_STRING_specifics_t asn_DEF_OCTET_STRING_specs = {
   16.21 -	sizeof(OCTET_STRING_t),
   16.22 -	offsetof(OCTET_STRING_t, _asn_ctx),
   16.23 -	ASN_OSUBV_STR
   16.24 -};
   16.25 -static asn_per_constraints_t asn_DEF_OCTET_STRING_constraints = {
   16.26 -	{ APC_CONSTRAINED, 8, 8, 0, 255 },
   16.27 -	{ APC_SEMI_CONSTRAINED, -1, -1, 0, 0 },
   16.28 -	0, 0
   16.29 -};
   16.30 -asn_TYPE_descriptor_t asn_DEF_OCTET_STRING = {
   16.31 -	"OCTET STRING",		/* Canonical name */
   16.32 -	"OCTET_STRING",		/* XML tag name */
   16.33 -	OCTET_STRING_free,
   16.34 -	OCTET_STRING_print,	/* non-ascii stuff, generally */
   16.35 -	asn_generic_no_constraint,
   16.36 -	OCTET_STRING_decode_ber,
   16.37 -	OCTET_STRING_encode_der,
   16.38 -	OCTET_STRING_decode_xer_hex,
   16.39 -	OCTET_STRING_encode_xer,
   16.40 -	OCTET_STRING_decode_uper,	/* Unaligned PER decoder */
   16.41 -	OCTET_STRING_encode_uper,	/* Unaligned PER encoder */
   16.42 -	0, /* Use generic outmost tag fetcher */
   16.43 -	asn_DEF_OCTET_STRING_tags,
   16.44 -	sizeof(asn_DEF_OCTET_STRING_tags)
   16.45 -	  / sizeof(asn_DEF_OCTET_STRING_tags[0]),
   16.46 -	asn_DEF_OCTET_STRING_tags,	/* Same as above */
   16.47 -	sizeof(asn_DEF_OCTET_STRING_tags)
   16.48 -	  / sizeof(asn_DEF_OCTET_STRING_tags[0]),
   16.49 -	0,	/* No PER visible constraints */
   16.50 -	0, 0,	/* No members */
   16.51 -	&asn_DEF_OCTET_STRING_specs
   16.52 -};
   16.53 -
   16.54 -#undef	_CH_PHASE
   16.55 -#undef	NEXT_PHASE
   16.56 -#undef	PREV_PHASE
   16.57 -#define	_CH_PHASE(ctx, inc) do {					\
   16.58 -		if(ctx->phase == 0)					\
   16.59 -			ctx->context = 0;				\
   16.60 -		ctx->phase += inc;					\
   16.61 -	} while(0)
   16.62 -#define	NEXT_PHASE(ctx)	_CH_PHASE(ctx, +1)
   16.63 -#define	PREV_PHASE(ctx)	_CH_PHASE(ctx, -1)
   16.64 -
   16.65 -#undef	ADVANCE
   16.66 -#define	ADVANCE(num_bytes)	do {					\
   16.67 -		size_t num = (num_bytes);				\
   16.68 -		buf_ptr = ((const char *)buf_ptr) + num;		\
   16.69 -		size -= num;						\
   16.70 -		consumed_myself += num;					\
   16.71 -	} while(0)
   16.72 -
   16.73 -#undef	RETURN
   16.74 -#define	RETURN(_code)	do {						\
   16.75 -		asn_dec_rval_t tmprval;					\
   16.76 -		tmprval.code = _code;					\
   16.77 -		tmprval.consumed = consumed_myself;			\
   16.78 -		return tmprval;						\
   16.79 -	} while(0)
   16.80 -
   16.81 -#undef	APPEND
   16.82 -#define	APPEND(bufptr, bufsize)	do {					\
   16.83 -		size_t _bs = (bufsize);		/* Append size */	\
   16.84 -		size_t _ns = ctx->context;	/* Allocated now */	\
   16.85 -		size_t _es = st->size + _bs;	/* Expected size */	\
   16.86 -		/* int is really a typeof(st->size): */			\
   16.87 -		if((int)_es < 0) RETURN(RC_FAIL);			\
   16.88 -		if(_ns <= _es) {					\
   16.89 -			void *ptr;					\
   16.90 -			/* Be nice and round to the memory allocator */	\
   16.91 -			do { _ns = _ns ? _ns << 1 : 16; }		\
   16.92 -			    while(_ns <= _es);				\
   16.93 -			/* int is really a typeof(st->size): */		\
   16.94 -			if((int)_ns < 0) RETURN(RC_FAIL);		\
   16.95 -			ptr = REALLOC(st->buf, _ns);			\
   16.96 -			if(ptr) {					\
   16.97 -				st->buf = (uint8_t *)ptr;		\
   16.98 -				ctx->context = _ns;			\
   16.99 -			} else {					\
  16.100 -				RETURN(RC_FAIL);			\
  16.101 -			}						\
  16.102 -			ASN_DEBUG("Reallocating into %ld", (long)_ns);	\
  16.103 -		}							\
  16.104 -		memcpy(st->buf + st->size, bufptr, _bs);		\
  16.105 -		/* Convenient nul-termination */			\
  16.106 -		st->buf[_es] = '\0';					\
  16.107 -		st->size = _es;						\
  16.108 -	} while(0)
  16.109 -
  16.110 -/*
  16.111 - * The main reason why ASN.1 is still alive is that too much time and effort
  16.112 - * is necessary for learning it more or less adequately, thus creating a gut
  16.113 - * necessity to demonstrate that aquired skill everywhere afterwards.
  16.114 - * No, I am not going to explain what the following stuff is.
  16.115 - */
  16.116 -struct _stack_el {
  16.117 -	ber_tlv_len_t	left;	/* What's left to read (or -1) */
  16.118 -	ber_tlv_len_t	got;	/* What was actually processed */
  16.119 -	int	cont_level;	/* Depth of subcontainment */
  16.120 -	int	want_nulls;	/* Want null "end of content" octets? */
  16.121 -	int	bits_chopped;	/* Flag in BIT STRING mode */
  16.122 -	ber_tlv_tag_t	tag;	/* For debugging purposes */
  16.123 -	struct _stack_el *prev;
  16.124 -	struct _stack_el *next;
  16.125 -};
  16.126 -struct _stack {
  16.127 -	struct _stack_el *tail;
  16.128 -	struct _stack_el *cur_ptr;
  16.129 -};
  16.130 -
  16.131 -static struct _stack_el *
  16.132 -OS__add_stack_el(struct _stack *st) {
  16.133 -	struct _stack_el *nel;
  16.134 -
  16.135 -	/*
  16.136 -	 * Reuse the old stack frame or allocate a new one.
  16.137 -	 */
  16.138 -	if(st->cur_ptr && st->cur_ptr->next) {
  16.139 -		nel = st->cur_ptr->next;
  16.140 -		nel->bits_chopped = 0;
  16.141 -		nel->got = 0;
  16.142 -		/* Retain the nel->cont_level, it's correct. */
  16.143 -	} else {
  16.144 -		nel = (struct _stack_el *)CALLOC(1, sizeof(struct _stack_el));
  16.145 -		if(nel == NULL)
  16.146 -			return NULL;
  16.147 -	
  16.148 -		if(st->tail) {
  16.149 -			/* Increase a subcontainment depth */
  16.150 -			nel->cont_level = st->tail->cont_level + 1;
  16.151 -			st->tail->next = nel;
  16.152 -		}
  16.153 -		nel->prev = st->tail;
  16.154 -		st->tail = nel;
  16.155 -	}
  16.156 -
  16.157 -	st->cur_ptr = nel;
  16.158 -
  16.159 -	return nel;
  16.160 -}
  16.161 -
  16.162 -static struct _stack *
  16.163 -_new_stack() {
  16.164 -	return (struct _stack *)CALLOC(1, sizeof(struct _stack));
  16.165 -}
  16.166 -
  16.167 -/*
  16.168 - * Decode OCTET STRING type.
  16.169 - */
  16.170 -asn_dec_rval_t
  16.171 -OCTET_STRING_decode_ber(asn_codec_ctx_t *opt_codec_ctx,
  16.172 -	asn_TYPE_descriptor_t *td,
  16.173 -	void **sptr, const void *buf_ptr, size_t size, int tag_mode) {
  16.174 -	asn_OCTET_STRING_specifics_t *specs = td->specifics
  16.175 -				? (asn_OCTET_STRING_specifics_t *)td->specifics
  16.176 -				: &asn_DEF_OCTET_STRING_specs;
  16.177 -	BIT_STRING_t *st = (BIT_STRING_t *)*sptr;
  16.178 -	asn_dec_rval_t rval;
  16.179 -	asn_struct_ctx_t *ctx;
  16.180 -	ssize_t consumed_myself = 0;
  16.181 -	struct _stack *stck;		/* Expectations stack structure */
  16.182 -	struct _stack_el *sel = 0;	/* Stack element */
  16.183 -	int tlv_constr;
  16.184 -	enum asn_OS_Subvariant type_variant = specs->subvariant;
  16.185 -
  16.186 -	ASN_DEBUG("Decoding %s as %s (frame %ld)",
  16.187 -		td->name,
  16.188 -		(type_variant == ASN_OSUBV_STR) ?
  16.189 -			"OCTET STRING" : "OS-SpecialCase",
  16.190 -		(long)size);
  16.191 -
  16.192 -	/*
  16.193 -	 * Create the string if does not exist.
  16.194 -	 */
  16.195 -	if(st == NULL) {
  16.196 -		st = (BIT_STRING_t *)(*sptr = CALLOC(1, specs->struct_size));
  16.197 -		if(st == NULL) RETURN(RC_FAIL);
  16.198 -	}
  16.199 -
  16.200 -	/* Restore parsing context */
  16.201 -	ctx = (asn_struct_ctx_t *)((char *)st + specs->ctx_offset);
  16.202 -
  16.203 -	switch(ctx->phase) {
  16.204 -	case 0:
  16.205 -		/*
  16.206 -		 * Check tags.
  16.207 -		 */
  16.208 -		rval = ber_check_tags(opt_codec_ctx, td, ctx,
  16.209 -			buf_ptr, size, tag_mode, -1,
  16.210 -			&ctx->left, &tlv_constr);
  16.211 -		if(rval.code != RC_OK)
  16.212 -			return rval;
  16.213 -
  16.214 -		if(tlv_constr) {
  16.215 -			/*
  16.216 -			 * Complex operation, requires stack of expectations.
  16.217 -			 */
  16.218 -			ctx->ptr = _new_stack();
  16.219 -			if(ctx->ptr) {
  16.220 -				stck = (struct _stack *)ctx->ptr;
  16.221 -			} else {
  16.222 -				RETURN(RC_FAIL);
  16.223 -			}
  16.224 -		} else {
  16.225 -			/*
  16.226 -			 * Jump into stackless primitive decoding.
  16.227 -			 */
  16.228 -			_CH_PHASE(ctx, 3);
  16.229 -			if(type_variant == ASN_OSUBV_ANY && tag_mode != 1)
  16.230 -				APPEND(buf_ptr, rval.consumed);
  16.231 -			ADVANCE(rval.consumed);
  16.232 -			goto phase3;
  16.233 -		}
  16.234 -
  16.235 -		NEXT_PHASE(ctx);
  16.236 -		/* Fall through */
  16.237 -	case 1:
  16.238 -	phase1:
  16.239 -		/*
  16.240 -		 * Fill the stack with expectations.
  16.241 -		 */
  16.242 -		stck = (struct _stack *)ctx->ptr;
  16.243 -		sel = stck->cur_ptr;
  16.244 -	  do {
  16.245 -		ber_tlv_tag_t tlv_tag;
  16.246 -		ber_tlv_len_t tlv_len;
  16.247 -		ber_tlv_tag_t expected_tag;
  16.248 -		ssize_t tl, ll, tlvl;
  16.249 -				/* This one works even if (sel->left == -1) */
  16.250 -		ssize_t Left = ((!sel||(size_t)sel->left >= size)
  16.251 -					?(ssize_t)size:sel->left);
  16.252 -
  16.253 -
  16.254 -		ASN_DEBUG("%p, s->l=%ld, s->wn=%ld, s->g=%ld\n", sel,
  16.255 -			(long)(sel?sel->left:0),
  16.256 -			(long)(sel?sel->want_nulls:0),
  16.257 -			(long)(sel?sel->got:0)
  16.258 -		);
  16.259 -		if(sel && sel->left <= 0 && sel->want_nulls == 0) {
  16.260 -			if(sel->prev) {
  16.261 -				struct _stack_el *prev = sel->prev;
  16.262 -				if(prev->left != -1) {
  16.263 -					if(prev->left < sel->got)
  16.264 -						RETURN(RC_FAIL);
  16.265 -					prev->left -= sel->got;
  16.266 -				}
  16.267 -				prev->got += sel->got;
  16.268 -				sel = stck->cur_ptr = prev;
  16.269 -				if(!sel) break;
  16.270 -				tlv_constr = 1;
  16.271 -				continue;
  16.272 -			} else {
  16.273 -				sel = stck->cur_ptr = 0;
  16.274 -				break;	/* Nothing to wait */
  16.275 -			}
  16.276 -		}
  16.277 -
  16.278 -		tl = ber_fetch_tag(buf_ptr, Left, &tlv_tag);
  16.279 -		ASN_DEBUG("fetch tag(size=%ld,L=%ld), %sstack, left=%ld, wn=%ld, tl=%ld",
  16.280 -			(long)size, (long)Left, sel?"":"!",
  16.281 -			(long)(sel?sel->left:0),
  16.282 -			(long)(sel?sel->want_nulls:0),
  16.283 -			(long)tl);
  16.284 -		switch(tl) {
  16.285 -		case -1: RETURN(RC_FAIL);
  16.286 -		case 0: RETURN(RC_WMORE);
  16.287 -		}
  16.288 -
  16.289 -		tlv_constr = BER_TLV_CONSTRUCTED(buf_ptr);
  16.290 -
  16.291 -		ll = ber_fetch_length(tlv_constr,
  16.292 -				(const char *)buf_ptr + tl,Left - tl,&tlv_len);
  16.293 -		ASN_DEBUG("Got tag=%s, tc=%d, left=%ld, tl=%ld, len=%ld, ll=%ld",
  16.294 -			ber_tlv_tag_string(tlv_tag), tlv_constr,
  16.295 -				(long)Left, (long)tl, (long)tlv_len, (long)ll);
  16.296 -		switch(ll) {
  16.297 -		case -1: RETURN(RC_FAIL);
  16.298 -		case 0: RETURN(RC_WMORE);
  16.299 -		}
  16.300 -
  16.301 -		if(sel && sel->want_nulls
  16.302 -			&& ((const uint8_t *)buf_ptr)[0] == 0
  16.303 -			&& ((const uint8_t *)buf_ptr)[1] == 0)
  16.304 -		{
  16.305 -
  16.306 -			ASN_DEBUG("Eat EOC; wn=%d--", sel->want_nulls);
  16.307 -
  16.308 -			if(type_variant == ASN_OSUBV_ANY
  16.309 -			&& (tag_mode != 1 || sel->cont_level))
  16.310 -				APPEND("\0\0", 2);
  16.311 -
  16.312 -			ADVANCE(2);
  16.313 -			sel->got += 2;
  16.314 -			if(sel->left != -1) {
  16.315 -				sel->left -= 2;	/* assert(sel->left >= 2) */
  16.316 -			}
  16.317 -
  16.318 -			sel->want_nulls--;
  16.319 -			if(sel->want_nulls == 0) {
  16.320 -				/* Move to the next expectation */
  16.321 -				sel->left = 0;
  16.322 -				tlv_constr = 1;
  16.323 -			}
  16.324 -
  16.325 -			continue;
  16.326 -		}
  16.327 -
  16.328 -		/*
  16.329 -		 * Set up expected tags,
  16.330 -		 * depending on ASN.1 type being decoded.
  16.331 -		 */
  16.332 -		switch(type_variant) {
  16.333 -		case ASN_OSUBV_BIT:
  16.334 -			/* X.690: 8.6.4.1, NOTE 2 */
  16.335 -			/* Fall through */
  16.336 -		case ASN_OSUBV_STR:
  16.337 -		default:
  16.338 -			if(sel) {
  16.339 -				int level = sel->cont_level;
  16.340 -				if(level < td->all_tags_count) {
  16.341 -					expected_tag = td->all_tags[level];
  16.342 -					break;
  16.343 -				} else if(td->all_tags_count) {
  16.344 -					expected_tag = td->all_tags
  16.345 -						[td->all_tags_count - 1];
  16.346 -					break;
  16.347 -				}
  16.348 -				/* else, Fall through */
  16.349 -			}
  16.350 -			/* Fall through */
  16.351 -		case ASN_OSUBV_ANY:
  16.352 -			expected_tag = tlv_tag;
  16.353 -			break;
  16.354 -		}
  16.355 -
  16.356 -
  16.357 -		if(tlv_tag != expected_tag) {
  16.358 -			char buf[2][32];
  16.359 -			ber_tlv_tag_snprint(tlv_tag,
  16.360 -				buf[0], sizeof(buf[0]));
  16.361 -			ber_tlv_tag_snprint(td->tags[td->tags_count-1],
  16.362 -				buf[1], sizeof(buf[1]));
  16.363 -			ASN_DEBUG("Tag does not match expectation: %s != %s",
  16.364 -				buf[0], buf[1]);
  16.365 -			RETURN(RC_FAIL);
  16.366 -		}
  16.367 -
  16.368 -		tlvl = tl + ll;	/* Combined length of T and L encoding */
  16.369 -		if((tlv_len + tlvl) < 0) {
  16.370 -			/* tlv_len value is too big */
  16.371 -			ASN_DEBUG("TLV encoding + length (%ld) is too big",
  16.372 -				(long)tlv_len);
  16.373 -			RETURN(RC_FAIL);
  16.374 -		}
  16.375 -
  16.376 -		/*
  16.377 -		 * Append a new expectation.
  16.378 -		 */
  16.379 -		sel = OS__add_stack_el(stck);
  16.380 -		if(!sel) RETURN(RC_FAIL);
  16.381 -
  16.382 -		sel->tag = tlv_tag;
  16.383 -
  16.384 -		sel->want_nulls = (tlv_len==-1);
  16.385 -		if(sel->prev && sel->prev->left != -1) {
  16.386 -			/* Check that the parent frame is big enough */
  16.387 -			if(sel->prev->left < tlvl + (tlv_len==-1?0:tlv_len))
  16.388 -				RETURN(RC_FAIL);
  16.389 -			if(tlv_len == -1)
  16.390 -				sel->left = sel->prev->left - tlvl;
  16.391 -			else
  16.392 -				sel->left = tlv_len;
  16.393 -		} else {
  16.394 -			sel->left = tlv_len;
  16.395 -		}
  16.396 -		if(type_variant == ASN_OSUBV_ANY
  16.397 -		&& (tag_mode != 1 || sel->cont_level))
  16.398 -			APPEND(buf_ptr, tlvl);
  16.399 -		sel->got += tlvl;
  16.400 -		ADVANCE(tlvl);
  16.401 -
  16.402 -		ASN_DEBUG("+EXPECT2 got=%ld left=%ld, wn=%d, clvl=%d",
  16.403 -			(long)sel->got, (long)sel->left,
  16.404 -			sel->want_nulls, sel->cont_level);
  16.405 -
  16.406 -	  } while(tlv_constr);
  16.407 -		if(sel == NULL) {
  16.408 -			/* Finished operation, "phase out" */
  16.409 -			ASN_DEBUG("Phase out");
  16.410 -			_CH_PHASE(ctx, +3);
  16.411 -			break;
  16.412 -		}
  16.413 -
  16.414 -		NEXT_PHASE(ctx);
  16.415 -		/* Fall through */
  16.416 -	case 2:
  16.417 -		stck = (struct _stack *)ctx->ptr;
  16.418 -		sel = stck->cur_ptr;
  16.419 -		ASN_DEBUG("Phase 2: Need %ld bytes, size=%ld, alrg=%ld, wn=%d",
  16.420 -			(long)sel->left, (long)size, (long)sel->got,
  16.421 -				sel->want_nulls);
  16.422 -	    {
  16.423 -		ber_tlv_len_t len;
  16.424 -
  16.425 -		assert(sel->left >= 0);
  16.426 -
  16.427 -		len = ((ber_tlv_len_t)size < sel->left)
  16.428 -				? (ber_tlv_len_t)size : sel->left;
  16.429 -		if(len > 0) {
  16.430 -			if(type_variant == ASN_OSUBV_BIT
  16.431 -			&& sel->bits_chopped == 0) {
  16.432 -				/* Put the unused-bits-octet away */
  16.433 -				st->bits_unused = *(const uint8_t *)buf_ptr;
  16.434 -				APPEND(((const char *)buf_ptr+1), (len - 1));
  16.435 -				sel->bits_chopped = 1;
  16.436 -			} else {
  16.437 -				APPEND(buf_ptr, len);
  16.438 -			}
  16.439 -			ADVANCE(len);
  16.440 -			sel->left -= len;
  16.441 -			sel->got += len;
  16.442 -		}
  16.443 -
  16.444 -		if(sel->left) {
  16.445 -			ASN_DEBUG("OS left %ld, size = %ld, wn=%d\n",
  16.446 -				(long)sel->left, (long)size, sel->want_nulls);
  16.447 -			RETURN(RC_WMORE);
  16.448 -		}
  16.449 -
  16.450 -		PREV_PHASE(ctx);
  16.451 -		goto phase1;
  16.452 -	    }
  16.453 -		break;
  16.454 -	case 3:
  16.455 -	phase3:
  16.456 -		/*
  16.457 -		 * Primitive form, no stack required.
  16.458 -		 */
  16.459 -		assert(ctx->left >= 0);
  16.460 -
  16.461 -		if(size < (size_t)ctx->left) {
  16.462 -			if(!size) RETURN(RC_WMORE);
  16.463 -			if(type_variant == ASN_OSUBV_BIT && !ctx->context) {
  16.464 -				st->bits_unused = *(const uint8_t *)buf_ptr;
  16.465 -				ctx->left--;
  16.466 -				ADVANCE(1);
  16.467 -			}
  16.468 -			APPEND(buf_ptr, size);
  16.469 -			assert(ctx->context > 0);
  16.470 -			ctx->left -= size;
  16.471 -			ADVANCE(size);
  16.472 -			RETURN(RC_WMORE);
  16.473 -		} else {
  16.474 -			if(type_variant == ASN_OSUBV_BIT
  16.475 -			&& !ctx->context && ctx->left) {
  16.476 -				st->bits_unused = *(const uint8_t *)buf_ptr;
  16.477 -				ctx->left--;
  16.478 -				ADVANCE(1);
  16.479 -			}
  16.480 -			APPEND(buf_ptr, ctx->left);
  16.481 -			ADVANCE(ctx->left);
  16.482 -			ctx->left = 0;
  16.483 -
  16.484 -			NEXT_PHASE(ctx);
  16.485 -		}
  16.486 -		break;
  16.487 -	}
  16.488 -
  16.489 -	if(sel) {
  16.490 -		ASN_DEBUG("3sel p=%p, wn=%d, l=%ld, g=%ld, size=%ld",
  16.491 -			sel->prev, sel->want_nulls,
  16.492 -			(long)sel->left, (long)sel->got, (long)size);
  16.493 -		if(sel->prev || sel->want_nulls > 1 || sel->left > 0) {
  16.494 -			RETURN(RC_WMORE);
  16.495 -		}
  16.496 -	}
  16.497 -
  16.498 -	/*
  16.499 -	 * BIT STRING-specific processing.
  16.500 -	 */
  16.501 -	if(type_variant == ASN_OSUBV_BIT && st->size) {
  16.502 -		/* Finalize BIT STRING: zero out unused bits. */
  16.503 -		st->buf[st->size-1] &= 0xff << st->bits_unused;
  16.504 -	}
  16.505 -
  16.506 -	ASN_DEBUG("Took %ld bytes to encode %s: [%s]:%ld",
  16.507 -		(long)consumed_myself, td->name,
  16.508 -		(type_variant == ASN_OSUBV_STR) ? (char *)st->buf : "<data>",
  16.509 -		(long)st->size);
  16.510 -
  16.511 -
  16.512 -	RETURN(RC_OK);
  16.513 -}
  16.514 -
  16.515 -/*
  16.516 - * Encode OCTET STRING type using DER.
  16.517 - */
  16.518 -asn_enc_rval_t
  16.519 -OCTET_STRING_encode_der(asn_TYPE_descriptor_t *td, void *sptr,
  16.520 -	int tag_mode, ber_tlv_tag_t tag,
  16.521 -	asn_app_consume_bytes_f *cb, void *app_key) {
  16.522 -	asn_enc_rval_t er;
  16.523 -	asn_OCTET_STRING_specifics_t *specs = td->specifics
  16.524 -				? (asn_OCTET_STRING_specifics_t *)td->specifics
  16.525 -				: &asn_DEF_OCTET_STRING_specs;
  16.526 -	BIT_STRING_t *st = (BIT_STRING_t *)sptr;
  16.527 -	enum asn_OS_Subvariant type_variant = specs->subvariant;
  16.528 -	int fix_last_byte = 0;
  16.529 -
  16.530 -	ASN_DEBUG("%s %s as OCTET STRING",
  16.531 -		cb?"Estimating":"Encoding", td->name);
  16.532 -
  16.533 -	/*
  16.534 -	 * Write tags.
  16.535 -	 */
  16.536 -	if(type_variant != ASN_OSUBV_ANY || tag_mode == 1) {
  16.537 -		er.encoded = der_write_tags(td,
  16.538 -				(type_variant == ASN_OSUBV_BIT) + st->size,
  16.539 -			tag_mode, type_variant == ASN_OSUBV_ANY, tag,
  16.540 -			cb, app_key);
  16.541 -		if(er.encoded == -1) {
  16.542 -			er.failed_type = td;
  16.543 -			er.structure_ptr = sptr;
  16.544 -			return er;
  16.545 -		}
  16.546 -	} else {
  16.547 -		/* Disallow: [<tag>] IMPLICIT ANY */
  16.548 -		assert(type_variant != ASN_OSUBV_ANY || tag_mode != -1);
  16.549 -		er.encoded = 0;
  16.550 -	}
  16.551 -
  16.552 -	if(!cb) {
  16.553 -		er.encoded += (type_variant == ASN_OSUBV_BIT) + st->size;
  16.554 -		_ASN_ENCODED_OK(er);
  16.555 -	}
  16.556 -
  16.557 -	/*
  16.558 -	 * Prepare to deal with the last octet of BIT STRING.
  16.559 -	 */
  16.560 -	if(type_variant == ASN_OSUBV_BIT) {
  16.561 -		uint8_t b = st->bits_unused & 0x07;
  16.562 -		if(b && st->size) fix_last_byte = 1;
  16.563 -		_ASN_CALLBACK(&b, 1);
  16.564 -		er.encoded++;
  16.565 -	}
  16.566 -
  16.567 -	/* Invoke callback for the main part of the buffer */
  16.568 -	_ASN_CALLBACK(st->buf, st->size - fix_last_byte);
  16.569 -
  16.570 -	/* The last octet should be stripped off the unused bits */
  16.571 -	if(fix_last_byte) {
  16.572 -		uint8_t b = st->buf[st->size-1] & (0xff << st->bits_unused);
  16.573 -		_ASN_CALLBACK(&b, 1);
  16.574 -	}
  16.575 -
  16.576 -	er.encoded += st->size;
  16.577 -	_ASN_ENCODED_OK(er);
  16.578 -cb_failed:
  16.579 -	_ASN_ENCODE_FAILED;
  16.580 -}
  16.581 -
  16.582 -asn_enc_rval_t
  16.583 -OCTET_STRING_encode_xer(asn_TYPE_descriptor_t *td, void *sptr,
  16.584 -	int ilevel, enum xer_encoder_flags_e flags,
  16.585 -		asn_app_consume_bytes_f *cb, void *app_key) {
  16.586 -	static const char *h2c = "0123456789ABCDEF";
  16.587 -	const OCTET_STRING_t *st = (const OCTET_STRING_t *)sptr;
  16.588 -	asn_enc_rval_t er;
  16.589 -	char scratch[16 * 3 + 4];
  16.590 -	char *p = scratch;
  16.591 -	uint8_t *buf;
  16.592 -	uint8_t *end;
  16.593 -	size_t i;
  16.594 -
  16.595 -	if(!st || (!st->buf && st->size))
  16.596 -		_ASN_ENCODE_FAILED;
  16.597 -
  16.598 -	er.encoded = 0;
  16.599 -
  16.600 -	/*
  16.601 -	 * Dump the contents of the buffer in hexadecimal.
  16.602 -	 */
  16.603 -	buf = st->buf;
  16.604 -	end = buf + st->size;
  16.605 -	if(flags & XER_F_CANONICAL) {
  16.606 -		char *scend = scratch + (sizeof(scratch) - 2);
  16.607 -		for(; buf < end; buf++) {
  16.608 -			if(p >= scend) {
  16.609 -				_ASN_CALLBACK(scratch, p - scratch);
  16.610 -				er.encoded += p - scratch;
  16.611 -				p = scratch;
  16.612 -			}
  16.613 -			*p++ = h2c[(*buf >> 4) & 0x0F];
  16.614 -			*p++ = h2c[*buf & 0x0F];
  16.615 -		}
  16.616 -
  16.617 -		_ASN_CALLBACK(scratch, p-scratch);	/* Dump the rest */
  16.618 -		er.encoded += p - scratch;
  16.619 -	} else {
  16.620 -		for(i = 0; buf < end; buf++, i++) {
  16.621 -			if(!(i % 16) && (i || st->size > 16)) {
  16.622 -				_ASN_CALLBACK(scratch, p-scratch);
  16.623 -				er.encoded += (p-scratch);
  16.624 -				p = scratch;
  16.625 -				_i_ASN_TEXT_INDENT(1, ilevel);
  16.626 -			}
  16.627 -			*p++ = h2c[(*buf >> 4) & 0x0F];
  16.628 -			*p++ = h2c[*buf & 0x0F];
  16.629 -			*p++ = 0x20;
  16.630 -		}
  16.631 -		if(p - scratch) {
  16.632 -			p--;	/* Remove the tail space */
  16.633 -			_ASN_CALLBACK(scratch, p-scratch); /* Dump the rest */
  16.634 -			er.encoded += p - scratch;
  16.635 -			if(st->size > 16)
  16.636 -				_i_ASN_TEXT_INDENT(1, ilevel-1);
  16.637 -		}
  16.638 -	}
  16.639 -
  16.640 -	_ASN_ENCODED_OK(er);
  16.641 -cb_failed:
  16.642 -	_ASN_ENCODE_FAILED;
  16.643 -}
  16.644 -
  16.645 -static struct OCTET_STRING__xer_escape_table_s {
  16.646 -	char *string;
  16.647 -	int size;
  16.648 -} OCTET_STRING__xer_escape_table[] = {
  16.649 -#define	OSXET(s)	{ s, sizeof(s) - 1 }
  16.650 -	OSXET("\074\156\165\154\057\076"),	/* <nul/> */
  16.651 -	OSXET("\074\163\157\150\057\076"),	/* <soh/> */
  16.652 -	OSXET("\074\163\164\170\057\076"),	/* <stx/> */
  16.653 -	OSXET("\074\145\164\170\057\076"),	/* <etx/> */
  16.654 -	OSXET("\074\145\157\164\057\076"),	/* <eot/> */
  16.655 -	OSXET("\074\145\156\161\057\076"),	/* <enq/> */
  16.656 -	OSXET("\074\141\143\153\057\076"),	/* <ack/> */
  16.657 -	OSXET("\074\142\145\154\057\076"),	/* <bel/> */
  16.658 -	OSXET("\074\142\163\057\076"),		/* <bs/> */
  16.659 -	OSXET("\011"),				/* \t */
  16.660 -	OSXET("\012"),				/* \n */
  16.661 -	OSXET("\074\166\164\057\076"),		/* <vt/> */
  16.662 -	OSXET("\074\146\146\057\076"),		/* <ff/> */
  16.663 -	OSXET("\015"),				/* \r */
  16.664 -	OSXET("\074\163\157\057\076"),		/* <so/> */
  16.665 -	OSXET("\074\163\151\057\076"),		/* <si/> */
  16.666 -	OSXET("\074\144\154\145\057\076"),	/* <dle/> */
  16.667 -	OSXET("\074\144\143\061\057\076"),	/* <de1/> */
  16.668 -	OSXET("\074\144\143\062\057\076"),	/* <de2/> */
  16.669 -	OSXET("\074\144\143\063\057\076"),	/* <de3/> */
  16.670 -	OSXET("\074\144\143\064\057\076"),	/* <de4/> */
  16.671 -	OSXET("\074\156\141\153\057\076"),	/* <nak/> */
  16.672 -	OSXET("\074\163\171\156\057\076"),	/* <syn/> */
  16.673 -	OSXET("\074\145\164\142\057\076"),	/* <etb/> */
  16.674 -	OSXET("\074\143\141\156\057\076"),	/* <can/> */
  16.675 -	OSXET("\074\145\155\057\076"),		/* <em/> */
  16.676 -	OSXET("\074\163\165\142\057\076"),	/* <sub/> */
  16.677 -	OSXET("\074\145\163\143\057\076"),	/* <esc/> */
  16.678 -	OSXET("\074\151\163\064\057\076"),	/* <is4/> */
  16.679 -	OSXET("\074\151\163\063\057\076"),	/* <is3/> */
  16.680 -	OSXET("\074\151\163\062\057\076"),	/* <is2/> */
  16.681 -	OSXET("\074\151\163\061\057\076"),	/* <is1/> */
  16.682 -	{ 0, 0 },	/* " " */
  16.683 -	{ 0, 0 },	/* ! */
  16.684 -	{ 0, 0 },	/* \" */
  16.685 -	{ 0, 0 },	/* # */
  16.686 -	{ 0, 0 },	/* $ */
  16.687 -	{ 0, 0 },	/* % */
  16.688 -	OSXET("\046\141\155\160\073"),	/* &amp; */
  16.689 -	{ 0, 0 },	/* ' */
  16.690 -	{0,0},{0,0},{0,0},{0,0},{0,0},{0,0},{0,0},{0,0}, /* ()*+,-./ */
  16.691 -	{0,0},{0,0},{0,0},{0,0},{0,0},{0,0},{0,0},{0,0}, /* 01234567 */
  16.692 -	{0,0},{0,0},{0,0},{0,0},			 /* 89:; */
  16.693 -	OSXET("\046\154\164\073"),	/* &lt; */
  16.694 -	{ 0, 0 },	/* = */
  16.695 -	OSXET("\046\147\164\073"),	/* &gt; */
  16.696 -};
  16.697 -
  16.698 -static int
  16.699 -OS__check_escaped_control_char(const void *buf, int size) {
  16.700 -	size_t i;
  16.701 -	/*
  16.702 -	 * Inefficient algorithm which translates the escape sequences
  16.703 -	 * defined above into characters. Returns -1 if not found.
  16.704 -	 * TODO: replace by a faster algorithm (bsearch(), hash or
  16.705 -	 * nested table lookups).
  16.706 -	 */
  16.707 -	for(i = 0; i < 32 /* Don't spend time on the bottom half */; i++) {
  16.708 -		struct OCTET_STRING__xer_escape_table_s *el;
  16.709 -		el = &OCTET_STRING__xer_escape_table[i];
  16.710 -		if(el->size == size && memcmp(buf, el->string, size) == 0)
  16.711 -			return i;
  16.712 -	}
  16.713 -	return -1;
  16.714 -}
  16.715 -
  16.716 -static int
  16.717 -OCTET_STRING__handle_control_chars(void *struct_ptr, const void *chunk_buf, size_t chunk_size) {
  16.718 -	/*
  16.719 -	 * This might be one of the escape sequences
  16.720 -	 * for control characters. Check it out.
  16.721 -	 * #11.15.5
  16.722 -	 */
  16.723 -	int control_char = OS__check_escaped_control_char(chunk_buf,chunk_size);
  16.724 -	if(control_char >= 0) {
  16.725 -		OCTET_STRING_t *st = (OCTET_STRING_t *)struct_ptr;
  16.726 -		void *p = REALLOC(st->buf, st->size + 2);
  16.727 -		if(p) {
  16.728 -			st->buf = (uint8_t *)p;
  16.729 -			st->buf[st->size++] = control_char;
  16.730 -			st->buf[st->size] = '\0';	/* nul-termination */
  16.731 -			return 0;
  16.732 -		}
  16.733 -	}
  16.734 -	
  16.735 -	return -1;	/* No, it's not */
  16.736 -}
  16.737 -
  16.738 -asn_enc_rval_t
  16.739 -OCTET_STRING_encode_xer_utf8(asn_TYPE_descriptor_t *td, void *sptr,
  16.740 -	int ilevel, enum xer_encoder_flags_e flags,
  16.741 -		asn_app_consume_bytes_f *cb, void *app_key) {
  16.742 -	const OCTET_STRING_t *st = (const OCTET_STRING_t *)sptr;
  16.743 -	asn_enc_rval_t er;
  16.744 -	uint8_t *buf, *end;
  16.745 -	uint8_t *ss;	/* Sequence start */
  16.746 -	ssize_t encoded_len = 0;
  16.747 -
  16.748 -	(void)ilevel;	/* Unused argument */
  16.749 -	(void)flags;	/* Unused argument */
  16.750 -
  16.751 -	if(!st || (!st->buf && st->size))
  16.752 -		_ASN_ENCODE_FAILED;
  16.753 -
  16.754 -	buf = st->buf;
  16.755 -	end = buf + st->size;
  16.756 -	for(ss = buf; buf < end; buf++) {
  16.757 -		unsigned int ch = *buf;
  16.758 -		int s_len;	/* Special encoding sequence length */
  16.759 -
  16.760 -		/*
  16.761 -		 * Escape certain characters: X.680/11.15
  16.762 -		 */
  16.763 -		if(ch < sizeof(OCTET_STRING__xer_escape_table)
  16.764 -			/sizeof(OCTET_STRING__xer_escape_table[0])
  16.765 -		&& (s_len = OCTET_STRING__xer_escape_table[ch].size)) {
  16.766 -			if(((buf - ss) && cb(ss, buf - ss, app_key) < 0)
  16.767 -			|| cb(OCTET_STRING__xer_escape_table[ch].string, s_len,
  16.768 -					app_key) < 0)
  16.769 -				_ASN_ENCODE_FAILED;
  16.770 -			encoded_len += (buf - ss) + s_len;
  16.771 -			ss = buf + 1;
  16.772 -		}
  16.773 -	}
  16.774 -
  16.775 -	encoded_len += (buf - ss);
  16.776 -	if((buf - ss) && cb(ss, buf - ss, app_key) < 0)
  16.777 -		_ASN_ENCODE_FAILED;
  16.778 -
  16.779 -	er.encoded = encoded_len;
  16.780 -	_ASN_ENCODED_OK(er);
  16.781 -}
  16.782 -
  16.783 -/*
  16.784 - * Convert from hexadecimal format (cstring): "AB CD EF"
  16.785 - */
  16.786 -static ssize_t OCTET_STRING__convert_hexadecimal(void *sptr, const void *chunk_buf, size_t chunk_size, int have_more) {
  16.787 -	OCTET_STRING_t *st = (OCTET_STRING_t *)sptr;
  16.788 -	const char *chunk_stop = (const char *)chunk_buf;
  16.789 -	const char *p = chunk_stop;
  16.790 -	const char *pend = p + chunk_size;
  16.791 -	unsigned int clv = 0;
  16.792 -	int half = 0;	/* Half bit */
  16.793 -	uint8_t *buf;
  16.794 -
  16.795 -	/* Reallocate buffer according to high cap estimation */
  16.796 -	ssize_t _ns = st->size + (chunk_size + 1) / 2;
  16.797 -	void *nptr = REALLOC(st->buf, _ns + 1);
  16.798 -	if(!nptr) return -1;
  16.799 -	st->buf = (uint8_t *)nptr;
  16.800 -	buf = st->buf + st->size;
  16.801 -
  16.802 -	/*
  16.803 -	 * If something like " a b c " appears here, the " a b":3 will be
  16.804 -	 * converted, and the rest skipped. That is, unless buf_size is greater
  16.805 -	 * than chunk_size, then it'll be equivalent to "ABC0".
  16.806 -	 */
  16.807 -	for(; p < pend; p++) {
  16.808 -		int ch = *(const unsigned char *)p;
  16.809 -		switch(ch) {
  16.810 -		case 0x09: case 0x0a: case 0x0c: case 0x0d:
  16.811 -		case 0x20:
  16.812 -			/* Ignore whitespace */
  16.813 -			continue;
  16.814 -		case 0x30: case 0x31: case 0x32: case 0x33: case 0x34: /*01234*/
  16.815 -		case 0x35: case 0x36: case 0x37: case 0x38: case 0x39: /*56789*/
  16.816 -			clv = (clv << 4) + (ch - 0x30);
  16.817 -			break;
  16.818 -		case 0x41: case 0x42: case 0x43:	/* ABC */
  16.819 -		case 0x44: case 0x45: case 0x46:	/* DEF */
  16.820 -			clv = (clv << 4) + (ch - 0x41 + 10);
  16.821 -			break;
  16.822 -		case 0x61: case 0x62: case 0x63:	/* abc */
  16.823 -		case 0x64: case 0x65: case 0x66:	/* def */
  16.824 -			clv = (clv << 4) + (ch - 0x61 + 10);
  16.825 -			break;
  16.826 -		default:
  16.827 -			*buf = 0;	/* JIC */
  16.828 -			return -1;
  16.829 -		}
  16.830 -		if(half++) {
  16.831 -			half = 0;
  16.832 -			*buf++ = clv;
  16.833 -			chunk_stop = p + 1;
  16.834 -		}
  16.835 -	}
  16.836 -
  16.837 -	/*
  16.838 -	 * Check partial decoding.
  16.839 -	 */
  16.840 -	if(half) {
  16.841 -		if(have_more) {
  16.842 -			/*
  16.843 -			 * Partial specification is fine,
  16.844 -			 * because no more more PXER_TEXT data is available.
  16.845 -			 */
  16.846 -			*buf++ = clv << 4;
  16.847 -			chunk_stop = p;
  16.848 -		}
  16.849 -	} else {
  16.850 -		chunk_stop = p;
  16.851 -	}
  16.852 -
  16.853 -	st->size = buf - st->buf;	/* Adjust the buffer size */
  16.854 -	assert(st->size <= _ns);
  16.855 -	st->buf[st->size] = 0;		/* Courtesy termination */
  16.856 -
  16.857 -	return (chunk_stop - (const char *)chunk_buf);	/* Converted size */
  16.858 -}
  16.859 -
  16.860 -/*
  16.861 - * Convert from binary format: "00101011101"
  16.862 - */
  16.863 -static ssize_t OCTET_STRING__convert_binary(void *sptr, const void *chunk_buf, size_t chunk_size, int have_more) {
  16.864 -	BIT_STRING_t *st = (BIT_STRING_t *)sptr;
  16.865 -	const char *p = (const char *)chunk_buf;
  16.866 -	const char *pend = p + chunk_size;
  16.867 -	int bits_unused = st->bits_unused & 0x7;
  16.868 -	uint8_t *buf;
  16.869 -
  16.870 -	/* Reallocate buffer according to high cap estimation */
  16.871 -	ssize_t _ns = st->size + (chunk_size + 7) / 8;
  16.872 -	void *nptr = REALLOC(st->buf, _ns + 1);
  16.873 -	if(!nptr) return -1;
  16.874 -	st->buf = (uint8_t *)nptr;
  16.875 -	buf = st->buf + st->size;
  16.876 -
  16.877 -	(void)have_more;
  16.878 -
  16.879 -	if(bits_unused == 0)
  16.880 -		bits_unused = 8;
  16.881 -	else if(st->size)
  16.882 -		buf--;
  16.883 -
  16.884 -	/*
  16.885 -	 * Convert series of 0 and 1 into the octet string.
  16.886 -	 */
  16.887 -	for(; p < pend; p++) {
  16.888 -		int ch = *(const unsigned char *)p;
  16.889 -		switch(ch) {
  16.890 -		case 0x09: case 0x0a: case 0x0c: case 0x0d:
  16.891 -		case 0x20:
  16.892 -			/* Ignore whitespace */
  16.893 -			break;
  16.894 -		case 0x30:
  16.895 -		case 0x31:
  16.896 -			if(bits_unused-- <= 0) {
  16.897 -				*++buf = 0;	/* Clean the cell */
  16.898 -				bits_unused = 7;
  16.899 -			}
  16.900 -			*buf |= (ch&1) << bits_unused;
  16.901 -			break;
  16.902 -		default:
  16.903 -			st->bits_unused = bits_unused;
  16.904 -			return -1;
  16.905 -		}
  16.906 -	}
  16.907 -
  16.908 -	if(bits_unused == 8) {
  16.909 -		st->size = buf - st->buf;
  16.910 -		st->bits_unused = 0;
  16.911 -	} else {
  16.912 -		st->size = buf - st->buf + 1;
  16.913 -		st->bits_unused = bits_unused;
  16.914 -	}
  16.915 -
  16.916 -	assert(st->size <= _ns);
  16.917 -	st->buf[st->size] = 0;		/* Courtesy termination */
  16.918 -
  16.919 -	return chunk_size;	/* Converted in full */
  16.920 -}
  16.921 -
  16.922 -/*
  16.923 - * Something like strtod(), but with stricter rules.
  16.924 - */
  16.925 -static int
  16.926 -OS__strtoent(int base, const char *buf, const char *end, int32_t *ret_value) {
  16.927 -	int32_t val = 0;
  16.928 -	const char *p;
  16.929 -
  16.930 -	for(p = buf; p < end; p++) {
  16.931 -		int ch = *p;
  16.932 -
  16.933 -		/* Strange huge value */
  16.934 -		if((val * base + base) < 0)
  16.935 -			return -1;
  16.936 -
  16.937 -		switch(ch) {
  16.938 -		case 0x30: case 0x31: case 0x32: case 0x33: case 0x34: /*01234*/
  16.939 -		case 0x35: case 0x36: case 0x37: case 0x38: case 0x39: /*56789*/
  16.940 -			val = val * base + (ch - 0x30);
  16.941 -			break;
  16.942 -		case 0x41: case 0x42: case 0x43:	/* ABC */
  16.943 -		case 0x44: case 0x45: case 0x46:	/* DEF */
  16.944 -			val = val * base + (ch - 0x41 + 10);
  16.945 -			break;
  16.946 -		case 0x61: case 0x62: case 0x63:	/* abc */
  16.947 -		case 0x64: case 0x65: case 0x66:	/* def */
  16.948 -			val = val * base + (ch - 0x61 + 10);
  16.949 -			break;
  16.950 -		case 0x3b:	/* ';' */
  16.951 -			*ret_value = val;
  16.952 -			return (p - buf) + 1;
  16.953 -		default:
  16.954 -			return -1;	/* Character set error */
  16.955 -		}
  16.956 -	}
  16.957 -
  16.958 -	*ret_value = -1;
  16.959 -	return (p - buf);
  16.960 -}
  16.961 -
  16.962 -/*
  16.963 - * Convert from the plain UTF-8 format, expanding entity references: "2 &lt; 3"
  16.964 - */
  16.965 -static ssize_t OCTET_STRING__convert_entrefs(void *sptr, const void *chunk_buf, size_t chunk_size, int have_more) {
  16.966 -	OCTET_STRING_t *st = (OCTET_STRING_t *)sptr;
  16.967 -	const char *p = (const char *)chunk_buf;
  16.968 -	const char *pend = p + chunk_size;
  16.969 -	uint8_t *buf;
  16.970 -
  16.971 -	/* Reallocate buffer */
  16.972 -	ssize_t _ns = st->size + chunk_size;
  16.973 -	void *nptr = REALLOC(st->buf, _ns + 1);
  16.974 -	if(!nptr) return -1;
  16.975 -	st->buf = (uint8_t *)nptr;
  16.976 -	buf = st->buf + st->size;
  16.977 -
  16.978 -	/*
  16.979 -	 * Convert series of 0 and 1 into the octet string.
  16.980 -	 */
  16.981 -	for(; p < pend; p++) {
  16.982 -		int ch = *(const unsigned char *)p;
  16.983 -		int len;	/* Length of the rest of the chunk */
  16.984 -
  16.985 -		if(ch != 0x26 /* '&' */) {
  16.986 -			*buf++ = ch;
  16.987 -			continue;	/* That was easy... */
  16.988 -		}
  16.989 -
  16.990 -		/*
  16.991 -		 * Process entity reference.
  16.992 -		 */
  16.993 -		len = chunk_size - (p - (const char *)chunk_buf);
  16.994 -		if(len == 1 /* "&" */) goto want_more;
  16.995 -		if(p[1] == 0x23 /* '#' */) {
  16.996 -			const char *pval;	/* Pointer to start of digits */
  16.997 -			int32_t val = 0;	/* Entity reference value */
  16.998 -			int base;
  16.999 -
 16.1000 -			if(len == 2 /* "&#" */) goto want_more;
 16.1001 -			if(p[2] == 0x78 /* 'x' */)
 16.1002 -				pval = p + 3, base = 16;
 16.1003 -			else
 16.1004 -				pval = p + 2, base = 10;
 16.1005 -			len = OS__strtoent(base, pval, p + len, &val);
 16.1006 -			if(len == -1) {
 16.1007 -				/* Invalid charset. Just copy verbatim. */
 16.1008 -				*buf++ = ch;
 16.1009 -				continue;
 16.1010 -			}
 16.1011 -			if(!len || pval[len-1] != 0x3b) goto want_more;
 16.1012 -			assert(val > 0);
 16.1013 -			p += (pval - p) + len - 1; /* Advance past entref */
 16.1014 -
 16.1015 -			if(val < 0x80) {
 16.1016 -				*buf++ = (char)val;
 16.1017 -			} else if(val < 0x800) {
 16.1018 -				*buf++ = 0xc0 | ((val >> 6));
 16.1019 -				*buf++ = 0x80 | ((val & 0x3f));
 16.1020 -			} else if(val < 0x10000) {
 16.1021 -				*buf++ = 0xe0 | ((val >> 12));
 16.1022 -				*buf++ = 0x80 | ((val >> 6) & 0x3f);
 16.1023 -				*buf++ = 0x80 | ((val & 0x3f));
 16.1024 -			} else if(val < 0x200000) {
 16.1025 -				*buf++ = 0xf0 | ((val >> 18));
 16.1026 -				*buf++ = 0x80 | ((val >> 12) & 0x3f);
 16.1027 -				*buf++ = 0x80 | ((val >> 6) & 0x3f);
 16.1028 -				*buf++ = 0x80 | ((val & 0x3f));
 16.1029 -			} else if(val < 0x4000000) {
 16.1030 -				*buf++ = 0xf8 | ((val >> 24));
 16.1031 -				*buf++ = 0x80 | ((val >> 18) & 0x3f);
 16.1032 -				*buf++ = 0x80 | ((val >> 12) & 0x3f);
 16.1033 -				*buf++ = 0x80 | ((val >> 6) & 0x3f);
 16.1034 -				*buf++ = 0x80 | ((val & 0x3f));
 16.1035 -			} else {
 16.1036 -				*buf++ = 0xfc | ((val >> 30) & 0x1);
 16.1037 -				*buf++ = 0x80 | ((val >> 24) & 0x3f);
 16.1038 -				*buf++ = 0x80 | ((val >> 18) & 0x3f);
 16.1039 -				*buf++ = 0x80 | ((val >> 12) & 0x3f);
 16.1040 -				*buf++ = 0x80 | ((val >> 6) & 0x3f);
 16.1041 -				*buf++ = 0x80 | ((val & 0x3f));
 16.1042 -			}
 16.1043 -		} else {
 16.1044 -			/*
 16.1045 -			 * Ugly, limited parsing of &amp; &gt; &lt;
 16.1046 -			 */
 16.1047 -			char *sc = (char *)memchr(p, 0x3b, len > 5 ? 5 : len);
 16.1048 -			if(!sc) goto want_more;
 16.1049 -			if((sc - p) == 4
 16.1050 -				&& p[1] == 0x61	/* 'a' */
 16.1051 -				&& p[2] == 0x6d	/* 'm' */
 16.1052 -				&& p[3] == 0x70	/* 'p' */) {
 16.1053 -				*buf++ = 0x26;
 16.1054 -				p = sc;
 16.1055 -				continue;
 16.1056 -			}
 16.1057 -			if((sc - p) == 3) {
 16.1058 -				if(p[1] == 0x6c) {
 16.1059 -					*buf = 0x3c;	/* '<' */
 16.1060 -				} else if(p[1] == 0x67) {
 16.1061 -					*buf = 0x3e;	/* '>' */
 16.1062 -				} else {
 16.1063 -					/* Unsupported entity reference */
 16.1064 -					*buf++ = ch;
 16.1065 -					continue;
 16.1066 -				}
 16.1067 -				if(p[2] != 0x74) {
 16.1068 -					/* Unsupported entity reference */
 16.1069 -					*buf++ = ch;
 16.1070 -					continue;
 16.1071 -				}
 16.1072 -				buf++;
 16.1073 -				p = sc;
 16.1074 -				continue;
 16.1075 -			}
 16.1076 -			/* Unsupported entity reference */
 16.1077 -			*buf++ = ch;
 16.1078 -		}
 16.1079 -
 16.1080 -		continue;
 16.1081 -	want_more:
 16.1082 -		if(have_more) {
 16.1083 -			/*
 16.1084 -			 * We know that no more data (of the same type)
 16.1085 -			 * is coming. Copy the rest verbatim.
 16.1086 -			 */
 16.1087 -			*buf++ = ch;
 16.1088 -			continue;
 16.1089 -		}
 16.1090 -		chunk_size = (p - (const char *)chunk_buf);
 16.1091 -		/* Processing stalled: need more data */
 16.1092 -		break;
 16.1093 -	}
 16.1094 -
 16.1095 -	st->size = buf - st->buf;
 16.1096 -	assert(st->size <= _ns);
 16.1097 -	st->buf[st->size] = 0;		/* Courtesy termination */
 16.1098 -
 16.1099 -	return chunk_size;	/* Converted in full */
 16.1100 -}
 16.1101 -
 16.1102 -/*
 16.1103 - * Decode OCTET STRING from the XML element's body.
 16.1104 - */
 16.1105 -static asn_dec_rval_t
 16.1106 -OCTET_STRING__decode_xer(asn_codec_ctx_t *opt_codec_ctx,
 16.1107 -	asn_TYPE_descriptor_t *td, void **sptr,
 16.1108 -	const char *opt_mname, const void *buf_ptr, size_t size,
 16.1109 -	int (*opt_unexpected_tag_decoder)
 16.1110 -		(void *struct_ptr, const void *chunk_buf, size_t chunk_size),
 16.1111 -	ssize_t (*body_receiver)
 16.1112 -		(void *struct_ptr, const void *chunk_buf, size_t chunk_size,
 16.1113 -			int have_more)
 16.1114 -) {
 16.1115 -	OCTET_STRING_t *st = (OCTET_STRING_t *)*sptr;
 16.1116 -	asn_OCTET_STRING_specifics_t *specs = td->specifics
 16.1117 -				? (asn_OCTET_STRING_specifics_t *)td->specifics
 16.1118 -				: &asn_DEF_OCTET_STRING_specs;
 16.1119 -	const char *xml_tag = opt_mname ? opt_mname : td->xml_tag;
 16.1120 -	asn_struct_ctx_t *ctx;		/* Per-structure parser context */
 16.1121 -	asn_dec_rval_t rval;		/* Return value from the decoder */
 16.1122 -	int st_allocated;
 16.1123 -
 16.1124 -	/*
 16.1125 -	 * Create the string if does not exist.
 16.1126 -	 */
 16.1127 -	if(!st) {
 16.1128 -		st = (OCTET_STRING_t *)CALLOC(1, specs->struct_size);
 16.1129 -		*sptr = (void *)st;
 16.1130 -		if(!st) goto sta_failed;
 16.1131 -		st_allocated = 1;
 16.1132 -	} else {
 16.1133 -		st_allocated = 0;
 16.1134 -	}
 16.1135 -	if(!st->buf) {
 16.1136 -		/* This is separate from above section */
 16.1137 -		st->buf = (uint8_t *)CALLOC(1, 1);
 16.1138 -		if(!st->buf) {
 16.1139 -			if(st_allocated) {
 16.1140 -				*sptr = 0;
 16.1141 -				goto stb_failed;
 16.1142 -			} else {
 16.1143 -				goto sta_failed;
 16.1144 -			}
 16.1145 -		}
 16.1146 -	}
 16.1147 -
 16.1148 -	/* Restore parsing context */
 16.1149 -	ctx = (asn_struct_ctx_t *)(((char *)*sptr) + specs->ctx_offset);
 16.1150 -
 16.1151 -	return xer_decode_general(opt_codec_ctx, ctx, *sptr, xml_tag,
 16.1152 -		buf_ptr, size, opt_unexpected_tag_decoder, body_receiver);
 16.1153 -
 16.1154 -stb_failed:
 16.1155 -	FREEMEM(st);
 16.1156 -sta_failed:
 16.1157 -	rval.code = RC_FAIL;
 16.1158 -	rval.consumed = 0;
 16.1159 -	return rval;
 16.1160 -}
 16.1161 -
 16.1162 -/*
 16.1163 - * Decode OCTET STRING from the hexadecimal data.
 16.1164 - */
 16.1165 -asn_dec_rval_t
 16.1166 -OCTET_STRING_decode_xer_hex(asn_codec_ctx_t *opt_codec_ctx,
 16.1167 -	asn_TYPE_descriptor_t *td, void **sptr,
 16.1168 -		const char *opt_mname, const void *buf_ptr, size_t size) {
 16.1169 -	return OCTET_STRING__decode_xer(opt_codec_ctx, td, sptr, opt_mname,
 16.1170 -		buf_ptr, size, 0, OCTET_STRING__convert_hexadecimal);
 16.1171 -}
 16.1172 -
 16.1173 -/*
 16.1174 - * Decode OCTET STRING from the binary (0/1) data.
 16.1175 - */
 16.1176 -asn_dec_rval_t
 16.1177 -OCTET_STRING_decode_xer_binary(asn_codec_ctx_t *opt_codec_ctx,
 16.1178 -	asn_TYPE_descriptor_t *td, void **sptr,
 16.1179 -		const char *opt_mname, const void *buf_ptr, size_t size) {
 16.1180 -	return OCTET_STRING__decode_xer(opt_codec_ctx, td, sptr, opt_mname,
 16.1181 -		buf_ptr, size, 0, OCTET_STRING__convert_binary);
 16.1182 -}
 16.1183 -
 16.1184 -/*
 16.1185 - * Decode OCTET STRING from the string (ASCII/UTF-8) data.
 16.1186 - */
 16.1187 -asn_dec_rval_t
 16.1188 -OCTET_STRING_decode_xer_utf8(asn_codec_ctx_t *opt_codec_ctx,
 16.1189 -	asn_TYPE_descriptor_t *td, void **sptr,
 16.1190 -		const char *opt_mname, const void *buf_ptr, size_t size) {
 16.1191 -	return OCTET_STRING__decode_xer(opt_codec_ctx, td, sptr, opt_mname,
 16.1192 -		buf_ptr, size,
 16.1193 -		OCTET_STRING__handle_control_chars,
 16.1194 -		OCTET_STRING__convert_entrefs);
 16.1195 -}
 16.1196 -
 16.1197 -static int
 16.1198 -OCTET_STRING_per_get_characters(asn_per_data_t *po, uint8_t *buf,
 16.1199 -		size_t units, unsigned int bpc, unsigned int unit_bits,
 16.1200 -		long lb, long ub, asn_per_constraints_t *pc) {
 16.1201 -	uint8_t *end = buf + units * bpc;
 16.1202 -
 16.1203 -	ASN_DEBUG("Expanding %d characters into (%ld..%ld):%d",
 16.1204 -		(int)units, lb, ub, unit_bits);
 16.1205 -
 16.1206 -	/* X.691: 27.5.4 */
 16.1207 -	if((unsigned long)ub <= ((unsigned long)2 << (unit_bits - 1))) {
 16.1208 -		/* Decode without translation */
 16.1209 -		lb = 0;
 16.1210 -	} else if(pc && pc->code2value) {
 16.1211 -		if(unit_bits > 16)
 16.1212 -			return 1;	/* FATAL: can't have constrained
 16.1213 -					 * UniversalString with more than
 16.1214 -					 * 16 million code points */
 16.1215 -		for(; buf < end; buf += bpc) {
 16.1216 -			int value;
 16.1217 -			int code = per_get_few_bits(po, unit_bits);
 16.1218 -			if(code < 0) return -1;	/* WMORE */
 16.1219 -			value = pc->code2value(code);
 16.1220 -			if(value < 0) {
 16.1221 -				ASN_DEBUG("Code %d (0x%02x) is"
 16.1222 -					" not in map (%ld..%ld)",
 16.1223 -					code, code, lb, ub);
 16.1224 -				return 1;	/* FATAL */
 16.1225 -			}
 16.1226 -			switch(bpc) {
 16.1227 -			case 1: *buf = value; break;
 16.1228 -			case 2: buf[0] = value >> 8; buf[1] = value; break;
 16.1229 -			case 4: buf[0] = value >> 24; buf[1] = value >> 16;
 16.1230 -				buf[2] = value >> 8; buf[3] = value; break;
 16.1231 -			}
 16.1232 -		}
 16.1233 -		return 0;
 16.1234 -	}
 16.1235 -
 16.1236 -	/* Shortcut the no-op copying to the aligned structure */
 16.1237 -	if(lb == 0 && (unit_bits == 8 * bpc)) {
 16.1238 -		return per_get_many_bits(po, buf, 0, unit_bits * units);
 16.1239 -	}
 16.1240 -
 16.1241 -	for(; buf < end; buf += bpc) {
 16.1242 -		int code = per_get_few_bits(po, unit_bits);
 16.1243 -		int ch = code + lb;
 16.1244 -		if(code < 0) return -1;	/* WMORE */
 16.1245 -		if(ch > ub) {
 16.1246 -			ASN_DEBUG("Code %d is out of range (%ld..%ld)",
 16.1247 -				ch, lb, ub);
 16.1248 -			return 1;	/* FATAL */
 16.1249 -		}
 16.1250 -		switch(bpc) {
 16.1251 -		case 1: *buf = ch; break;
 16.1252 -		case 2: buf[0] = ch >> 8; buf[1] = ch; break;
 16.1253 -		case 4: buf[0] = ch >> 24; buf[1] = ch >> 16;
 16.1254 -			buf[2] = ch >> 8; buf[3] = ch; break;
 16.1255 -		}
 16.1256 -	}
 16.1257 -
 16.1258 -	return 0;
 16.1259 -}
 16.1260 -
 16.1261 -static int
 16.1262 -OCTET_STRING_per_put_characters(asn_per_outp_t *po, const uint8_t *buf,
 16.1263 -		size_t units, unsigned int bpc, unsigned int unit_bits,
 16.1264 -		long lb, long ub, asn_per_constraints_t *pc) {
 16.1265 -	const uint8_t *end = buf + units * bpc;
 16.1266 -
 16.1267 -	ASN_DEBUG("Squeezing %d characters into (%ld..%ld):%d (%d bpc)",
 16.1268 -		(int)units, lb, ub, unit_bits, bpc);
 16.1269 -
 16.1270 -	/* X.691: 27.5.4 */
 16.1271 -	if((unsigned long)ub <= ((unsigned long)2 << (unit_bits - 1))) {
 16.1272 -		/* Encode as is */
 16.1273 -		lb = 0;
 16.1274 -	} else if(pc && pc->value2code) {
 16.1275 -		for(; buf < end; buf += bpc) {
 16.1276 -			int code;
 16.1277 -			uint32_t value;
 16.1278 -			switch(bpc) {
 16.1279 -			case 1: value = *(const uint8_t *)buf; break;
 16.1280 -			case 2: value = (buf[0] << 8) | buf[1]; break;
 16.1281 -			case 4: value = (buf[0] << 24) | (buf[1] << 16)
 16.1282 -					| (buf[2] << 8) | buf[3]; break;
 16.1283 -			default: return -1;
 16.1284 -			}
 16.1285 -			code = pc->value2code(value);
 16.1286 -			if(code < 0) {
 16.1287 -				ASN_DEBUG("Character %d (0x%02x) is"
 16.1288 -					" not in map (%ld..%ld)",
 16.1289 -					*buf, *buf, lb, ub);
 16.1290 -				return -1;
 16.1291 -			}
 16.1292 -			if(per_put_few_bits(po, code, unit_bits))
 16.1293 -				return -1;
 16.1294 -		}
 16.1295 -	}
 16.1296 -
 16.1297 -	/* Shortcut the no-op copying to the aligned structure */
 16.1298 -	if(lb == 0 && (unit_bits == 8 * bpc)) {
 16.1299 -		return per_put_many_bits(po, buf, unit_bits * units);
 16.1300 -	}
 16.1301 -
 16.1302 -	for(ub -= lb; buf < end; buf += bpc) {
 16.1303 -		int ch;
 16.1304 -		uint32_t value;
 16.1305 -		switch(bpc) {
 16.1306 -		case 1: value = *(const uint8_t *)buf; break;
 16.1307 -		case 2: value = (buf[0] << 8) | buf[1]; break;
 16.1308 -		case 4: value = (buf[0] << 24) | (buf[1] << 16)
 16.1309 -				| (buf[2] << 8) | buf[3]; break;
 16.1310 -		default: return -1;
 16.1311 -		}
 16.1312 -		ch = value - lb;
 16.1313 -		if(ch < 0 || ch > ub) {
 16.1314 -			ASN_DEBUG("Character %d (0x%02x)"
 16.1315 -			" is out of range (%ld..%ld)",
 16.1316 -				*buf, *buf, lb, ub + lb);
 16.1317 -			return -1;
 16.1318 -		}
 16.1319 -		if(per_put_few_bits(po, ch, unit_bits))
 16.1320 -			return -1;
 16.1321 -	}
 16.1322 -
 16.1323 -	return 0;
 16.1324 -}
 16.1325 -
 16.1326 -asn_dec_rval_t
 16.1327 -OCTET_STRING_decode_uper(asn_codec_ctx_t *opt_codec_ctx,
 16.1328 -	asn_TYPE_descriptor_t *td, asn_per_constraints_t *constraints,
 16.1329 -	void **sptr, asn_per_data_t *pd) {
 16.1330 -
 16.1331 -	asn_OCTET_STRING_specifics_t *specs = td->specifics
 16.1332 -		? (asn_OCTET_STRING_specifics_t *)td->specifics
 16.1333 -		: &asn_DEF_OCTET_STRING_specs;
 16.1334 -	asn_per_constraints_t *pc = constraints ? constraints
 16.1335 -				: td->per_constraints;
 16.1336 -	asn_per_constraint_t *cval;
 16.1337 -	asn_per_constraint_t *csiz;
 16.1338 -	asn_dec_rval_t rval = { RC_OK, 0 };
 16.1339 -	BIT_STRING_t *st = (BIT_STRING_t *)*sptr;
 16.1340 -	ssize_t consumed_myself = 0;
 16.1341 -	int repeat;
 16.1342 -	enum {
 16.1343 -		OS__BPC_BIT	= 0,
 16.1344 -		OS__BPC_CHAR	= 1,
 16.1345 -		OS__BPC_U16	= 2,
 16.1346 -		OS__BPC_U32	= 4
 16.1347 -	} bpc;	/* Bytes per character */
 16.1348 -	unsigned int unit_bits;
 16.1349 -	unsigned int canonical_unit_bits;
 16.1350 -
 16.1351 -	(void)opt_codec_ctx;
 16.1352 -
 16.1353 -	if(pc) {
 16.1354 -		cval = &pc->value;
 16.1355 -		csiz = &pc->size;
 16.1356 -	} else {
 16.1357 -		cval = &asn_DEF_OCTET_STRING_constraints.value;
 16.1358 -		csiz = &asn_DEF_OCTET_STRING_constraints.size;
 16.1359 -	}
 16.1360 -
 16.1361 -	switch(specs->subvariant) {
 16.1362 -	default:
 16.1363 -	case ASN_OSUBV_ANY:
 16.1364 -		ASN_DEBUG("Unrecognized subvariant %d", specs->subvariant);
 16.1365 -		RETURN(RC_FAIL);
 16.1366 -	case ASN_OSUBV_BIT:
 16.1367 -		canonical_unit_bits = unit_bits = 1;
 16.1368 -		bpc = OS__BPC_BIT;
 16.1369 -		break;
 16.1370 -	case ASN_OSUBV_STR:
 16.1371 -		canonical_unit_bits = unit_bits = 8;
 16.1372 -		if(cval->flags & APC_CONSTRAINED)
 16.1373 -			unit_bits = cval->range_bits;
 16.1374 -		bpc = OS__BPC_CHAR;
 16.1375 -		break;
 16.1376 -	case ASN_OSUBV_U16:
 16.1377 -		canonical_unit_bits = unit_bits = 16;
 16.1378 -		if(cval->flags & APC_CONSTRAINED)
 16.1379 -			unit_bits = cval->range_bits;
 16.1380 -		bpc = OS__BPC_U16;
 16.1381 -		break;
 16.1382 -	case ASN_OSUBV_U32:
 16.1383 -		canonical_unit_bits = unit_bits = 32;
 16.1384 -		if(cval->flags & APC_CONSTRAINED)
 16.1385 -			unit_bits = cval->range_bits;
 16.1386 -		bpc = OS__BPC_U32;
 16.1387 -		break;
 16.1388 -	}
 16.1389 -
 16.1390 -	/*
 16.1391 -	 * Allocate the string.
 16.1392 -	 */
 16.1393 -	if(!st) {
 16.1394 -		st = (BIT_STRING_t *)(*sptr = CALLOC(1, specs->struct_size));
 16.1395 -		if(!st) RETURN(RC_FAIL);
 16.1396 -	}
 16.1397 -
 16.1398 -	ASN_DEBUG("PER Decoding %s size %ld .. %ld bits %d",
 16.1399 -		csiz->flags & APC_EXTENSIBLE ? "extensible" : "non-extensible",
 16.1400 -		csiz->lower_bound, csiz->upper_bound, csiz->effective_bits);
 16.1401 -
 16.1402 -	if(csiz->flags & APC_EXTENSIBLE) {
 16.1403 -		int inext = per_get_few_bits(pd, 1);
 16.1404 -		if(inext < 0) RETURN(RC_WMORE);
 16.1405 -		if(inext) {
 16.1406 -			csiz = &asn_DEF_OCTET_STRING_constraints.size;
 16.1407 -			cval = &asn_DEF_OCTET_STRING_constraints.value;
 16.1408 -			unit_bits = canonical_unit_bits;
 16.1409 -		}
 16.1410 -	}
 16.1411 -
 16.1412 -	if(csiz->effective_bits >= 0) {
 16.1413 -		FREEMEM(st->buf);
 16.1414 -		if(bpc) {
 16.1415 -			st->size = csiz->upper_bound * bpc;
 16.1416 -		} else {
 16.1417 -			st->size = (csiz->upper_bound + 7) >> 3;
 16.1418 -		}
 16.1419 -		st->buf = (uint8_t *)MALLOC(st->size + 1);
 16.1420 -		if(!st->buf) { st->size = 0; RETURN(RC_FAIL); }
 16.1421 -	}
 16.1422 -
 16.1423 -	/* X.691, #16.5: zero-length encoding */
 16.1424 -	/* X.691, #16.6: short fixed length encoding (up to 2 octets) */
 16.1425 -	/* X.691, #16.7: long fixed length encoding (up to 64K octets) */
 16.1426 -	if(csiz->effective_bits == 0) {
 16.1427 -		int ret;
 16.1428 -		if(bpc) {
 16.1429 -			ASN_DEBUG("Encoding OCTET STRING size %ld",
 16.1430 -				csiz->upper_bound);
 16.1431 -			ret = OCTET_STRING_per_get_characters(pd, st->buf,
 16.1432 -				csiz->upper_bound, bpc, unit_bits,
 16.1433 -				cval->lower_bound, cval->upper_bound, pc);
 16.1434 -			if(ret > 0) RETURN(RC_FAIL);
 16.1435 -		} else {
 16.1436 -			ASN_DEBUG("Encoding BIT STRING size %ld",
 16.1437 -				csiz->upper_bound);
 16.1438 -			ret = per_get_many_bits(pd, st->buf, 0,
 16.1439 -					    unit_bits * csiz->upper_bound);
 16.1440 -		}
 16.1441 -		if(ret < 0) RETURN(RC_WMORE);
 16.1442 -		consumed_myself += unit_bits * csiz->upper_bound;
 16.1443 -		st->buf[st->size] = 0;
 16.1444 -		if(bpc == 0) {
 16.1445 -			int ubs = (csiz->upper_bound & 0x7);
 16.1446 -			st->bits_unused = ubs ? 8 - ubs : 0;
 16.1447 -		}
 16.1448 -		RETURN(RC_OK);
 16.1449 -	}
 16.1450 -
 16.1451 -	st->size = 0;
 16.1452 -	do {
 16.1453 -		ssize_t raw_len;
 16.1454 -		ssize_t len_bytes;
 16.1455 -		ssize_t len_bits;
 16.1456 -		void *p;
 16.1457 -		int ret;
 16.1458 -
 16.1459 -		/* Get the PER length */
 16.1460 -		raw_len = uper_get_length(pd, csiz->effective_bits, &repeat);
 16.1461 -		if(raw_len < 0) RETURN(RC_WMORE);
 16.1462 -		raw_len += csiz->lower_bound;
 16.1463 -
 16.1464 -		ASN_DEBUG("Got PER length eb %ld, len %ld, %s (%s)",
 16.1465 -			(long)csiz->effective_bits, (long)raw_len,
 16.1466 -			repeat ? "repeat" : "once", td->name);
 16.1467 -		if(bpc) {
 16.1468 -			len_bytes = raw_len * bpc;
 16.1469 -			len_bits = len_bytes * unit_bits;
 16.1470 -		} else {
 16.1471 -			len_bits = raw_len;
 16.1472 -			len_bytes = (len_bits + 7) >> 3;
 16.1473 -			if(len_bits & 0x7)
 16.1474 -				st->bits_unused = 8 - (len_bits & 0x7);
 16.1475 -			/* len_bits be multiple of 16K if repeat is set */
 16.1476 -		}
 16.1477 -		p = REALLOC(st->buf, st->size + len_bytes + 1);
 16.1478 -		if(!p) RETURN(RC_FAIL);
 16.1479 -		st->buf = (uint8_t *)p;
 16.1480 -
 16.1481 -		if(bpc) {
 16.1482 -			ret = OCTET_STRING_per_get_characters(pd,
 16.1483 -				&st->buf[st->size], raw_len, bpc, unit_bits,
 16.1484 -				cval->lower_bound, cval->upper_bound, pc);
 16.1485 -			if(ret > 0) RETURN(RC_FAIL);
 16.1486 -		} else {
 16.1487 -			ret = per_get_many_bits(pd, &st->buf[st->size],
 16.1488 -				0, len_bits);
 16.1489 -		}
 16.1490 -		if(ret < 0) RETURN(RC_WMORE);
 16.1491 -		st->size += len_bytes;
 16.1492 -	} while(repeat);
 16.1493 -	st->buf[st->size] = 0;	/* nul-terminate */
 16.1494 -
 16.1495 -	return rval;
 16.1496 -}
 16.1497 -
 16.1498 -asn_enc_rval_t
 16.1499 -OCTET_STRING_encode_uper(asn_TYPE_descriptor_t *td,
 16.1500 -        asn_per_constraints_t *constraints, void *sptr, asn_per_outp_t *po) {
 16.1501 -
 16.1502 -	asn_OCTET_STRING_specifics_t *specs = td->specifics
 16.1503 -		? (asn_OCTET_STRING_specifics_t *)td->specifics
 16.1504 -		: &asn_DEF_OCTET_STRING_specs;
 16.1505 -	asn_per_constraints_t *pc = constraints ? constraints
 16.1506 -				: td->per_constraints;
 16.1507 -	asn_per_constraint_t *cval;
 16.1508 -	asn_per_constraint_t *csiz;
 16.1509 -	const BIT_STRING_t *st = (const BIT_STRING_t *)sptr;
 16.1510 -	asn_enc_rval_t er = { 0, 0, 0 };
 16.1511 -	int inext = 0;		/* Lies not within extension root */
 16.1512 -	unsigned int unit_bits;
 16.1513 -	unsigned int canonical_unit_bits;
 16.1514 -	unsigned int sizeinunits;
 16.1515 -	const uint8_t *buf;
 16.1516 -	int ret;
 16.1517 -	enum {
 16.1518 -		OS__BPC_BIT	= 0,
 16.1519 -		OS__BPC_CHAR	= 1,
 16.1520 -		OS__BPC_U16	= 2,
 16.1521 -		OS__BPC_U32	= 4
 16.1522 -	} bpc;	/* Bytes per character */
 16.1523 -	int ct_extensible;
 16.1524 -
 16.1525 -	if(!st || (!st->buf && st->size))
 16.1526 -		_ASN_ENCODE_FAILED;
 16.1527 -
 16.1528 -	if(pc) {
 16.1529 -		cval = &pc->value;
 16.1530 -		csiz = &pc->size;
 16.1531 -	} else {
 16.1532 -		cval = &asn_DEF_OCTET_STRING_constraints.value;
 16.1533 -		csiz = &asn_DEF_OCTET_STRING_constraints.size;
 16.1534 -	}
 16.1535 -	ct_extensible = csiz->flags & APC_EXTENSIBLE;
 16.1536 -
 16.1537 -	switch(specs->subvariant) {
 16.1538 -	default:
 16.1539 -	case ASN_OSUBV_ANY:
 16.1540 -		_ASN_ENCODE_FAILED;
 16.1541 -	case ASN_OSUBV_BIT:
 16.1542 -		canonical_unit_bits = unit_bits = 1;
 16.1543 -		bpc = OS__BPC_BIT;
 16.1544 -		sizeinunits = st->size * 8 - (st->bits_unused & 0x07);
 16.1545 -		ASN_DEBUG("BIT STRING of %d bytes, %d bits unused",
 16.1546 -				sizeinunits, st->bits_unused);
 16.1547 -		break;
 16.1548 -	case ASN_OSUBV_STR:
 16.1549 -		canonical_unit_bits = unit_bits = 8;
 16.1550 -		if(cval->flags & APC_CONSTRAINED)
 16.1551 -			unit_bits = cval->range_bits;
 16.1552 -		bpc = OS__BPC_CHAR;
 16.1553 -		sizeinunits = st->size;
 16.1554 -		break;
 16.1555 -	case ASN_OSUBV_U16:
 16.1556 -		canonical_unit_bits = unit_bits = 16;
 16.1557 -		if(cval->flags & APC_CONSTRAINED)
 16.1558 -			unit_bits = cval->range_bits;
 16.1559 -		bpc = OS__BPC_U16;
 16.1560 -		sizeinunits = st->size / 2;
 16.1561 -		break;
 16.1562 -	case ASN_OSUBV_U32:
 16.1563 -		canonical_unit_bits = unit_bits = 32;
 16.1564 -		if(cval->flags & APC_CONSTRAINED)
 16.1565 -			unit_bits = cval->range_bits;
 16.1566 -		bpc = OS__BPC_U32;
 16.1567 -		sizeinunits = st->size / 4;
 16.1568 -		break;
 16.1569 -	}
 16.1570 -
 16.1571 -	ASN_DEBUG("Encoding %s into %d units of %d bits"
 16.1572 -		" (%ld..%ld, effective %d)%s",
 16.1573 -		td->name, sizeinunits, unit_bits,
 16.1574 -		csiz->lower_bound, csiz->upper_bound,
 16.1575 -		csiz->effective_bits, ct_extensible ? " EXT" : "");
 16.1576 -
 16.1577 -	/* Figure out whether size lies within PER visible constraint */
 16.1578 -
 16.1579 -	if(csiz->effective_bits >= 0) {
 16.1580 -		if((int)sizeinunits < csiz->lower_bound
 16.1581 -		|| (int)sizeinunits > csiz->upper_bound) {
 16.1582 -			if(ct_extensible) {
 16.1583 -				cval = &asn_DEF_OCTET_STRING_constraints.value;
 16.1584 -				csiz = &asn_DEF_OCTET_STRING_constraints.size;
 16.1585 -				unit_bits = canonical_unit_bits;
 16.1586 -				inext = 1;
 16.1587 -			} else
 16.1588 -				_ASN_ENCODE_FAILED;
 16.1589 -		}
 16.1590 -	} else {
 16.1591 -		inext = 0;
 16.1592 -	}
 16.1593 -
 16.1594 -	if(ct_extensible) {
 16.1595 -		/* Declare whether length is [not] within extension root */
 16.1596 -		if(per_put_few_bits(po, inext, 1))
 16.1597 -			_ASN_ENCODE_FAILED;
 16.1598 -	}
 16.1599 -
 16.1600 -	/* X.691, #16.5: zero-length encoding */
 16.1601 -	/* X.691, #16.6: short fixed length encoding (up to 2 octets) */
 16.1602 -	/* X.691, #16.7: long fixed length encoding (up to 64K octets) */
 16.1603 -	if(csiz->effective_bits >= 0) {
 16.1604 -		ASN_DEBUG("Encoding %d bytes (%ld), length in %d bits",
 16.1605 -				st->size, sizeinunits - csiz->lower_bound,
 16.1606 -				csiz->effective_bits);
 16.1607 -		ret = per_put_few_bits(po, sizeinunits - csiz->lower_bound,
 16.1608 -				csiz->effective_bits);
 16.1609 -		if(ret) _ASN_ENCODE_FAILED;
 16.1610 -		if(bpc) {
 16.1611 -			ret = OCTET_STRING_per_put_characters(po, st->buf,
 16.1612 -				sizeinunits, bpc, unit_bits,
 16.1613 -				cval->lower_bound, cval->upper_bound, pc);
 16.1614 -		} else {
 16.1615 -			ret = per_put_many_bits(po, st->buf,
 16.1616 -				sizeinunits * unit_bits);
 16.1617 -		}
 16.1618 -		if(ret) _ASN_ENCODE_FAILED;
 16.1619 -		_ASN_ENCODED_OK(er);
 16.1620 -	}
 16.1621 -
 16.1622 -	ASN_DEBUG("Encoding %d bytes", st->size);
 16.1623 -
 16.1624 -	if(sizeinunits == 0) {
 16.1625 -		if(uper_put_length(po, 0))
 16.1626 -			_ASN_ENCODE_FAILED;
 16.1627 -		_ASN_ENCODED_OK(er);
 16.1628 -	}
 16.1629 -
 16.1630 -	buf = st->buf;
 16.1631 -	while(sizeinunits) {
 16.1632 -		ssize_t maySave = uper_put_length(po, sizeinunits);
 16.1633 -		if(maySave < 0) _ASN_ENCODE_FAILED;
 16.1634 -
 16.1635 -		ASN_DEBUG("Encoding %ld of %ld",
 16.1636 -			(long)maySave, (long)sizeinunits);
 16.1637 -
 16.1638 -		if(bpc) {
 16.1639 -			ret = OCTET_STRING_per_put_characters(po, buf,
 16.1640 -				maySave, bpc, unit_bits,
 16.1641 -				cval->lower_bound, cval->upper_bound, pc);
 16.1642 -		} else {
 16.1643 -			ret = per_put_many_bits(po, buf, maySave * unit_bits);
 16.1644 -		}
 16.1645 -		if(ret) _ASN_ENCODE_FAILED;
 16.1646 -
 16.1647 -		if(bpc)
 16.1648 -			buf += maySave * bpc;
 16.1649 -		else
 16.1650 -			buf += maySave >> 3;
 16.1651 -		sizeinunits -= maySave;
 16.1652 -		assert(!(maySave & 0x07) || !sizeinunits);
 16.1653 -	}
 16.1654 -
 16.1655 -	_ASN_ENCODED_OK(er);
 16.1656 -}
 16.1657 -
 16.1658 -int
 16.1659 -OCTET_STRING_print(asn_TYPE_descriptor_t *td, const void *sptr, int ilevel,
 16.1660 -	asn_app_consume_bytes_f *cb, void *app_key) {
 16.1661 -	static const char *h2c = "0123456789ABCDEF";
 16.1662 -	const OCTET_STRING_t *st = (const OCTET_STRING_t *)sptr;
 16.1663 -	char scratch[16 * 3 + 4];
 16.1664 -	char *p = scratch;
 16.1665 -	uint8_t *buf;
 16.1666 -	uint8_t *end;
 16.1667 -	size_t i;
 16.1668 -
 16.1669 -	(void)td;	/* Unused argument */
 16.1670 -
 16.1671 -	if(!st || (!st->buf && st->size))
 16.1672 -		return (cb("<absent>", 8, app_key) < 0) ? -1 : 0;
 16.1673 -
 16.1674 -	/*
 16.1675 -	 * Dump the contents of the buffer in hexadecimal.
 16.1676 -	 */
 16.1677 -	buf = st->buf;
 16.1678 -	end = buf + st->size;
 16.1679 -	for(i = 0; buf < end; buf++, i++) {
 16.1680 -		if(!(i % 16) && (i || st->size > 16)) {
 16.1681 -			if(cb(scratch, p - scratch, app_key) < 0)
 16.1682 -				return -1;
 16.1683 -			_i_INDENT(1);
 16.1684 -			p = scratch;
 16.1685 -		}
 16.1686 -		*p++ = h2c[(*buf >> 4) & 0x0F];
 16.1687 -		*p++ = h2c[*buf & 0x0F];
 16.1688 -		*p++ = 0x20;
 16.1689 -	}
 16.1690 -
 16.1691 -	if(p > scratch) {
 16.1692 -		p--;	/* Remove the tail space */
 16.1693 -		if(cb(scratch, p - scratch, app_key) < 0)
 16.1694 -			return -1;
 16.1695 -	}
 16.1696 -
 16.1697 -	return 0;
 16.1698 -}
 16.1699 -
 16.1700 -int
 16.1701 -OCTET_STRING_print_utf8(asn_TYPE_descriptor_t *td, const void *sptr,
 16.1702 -		int ilevel, asn_app_consume_bytes_f *cb, void *app_key) {
 16.1703 -	const OCTET_STRING_t *st = (const OCTET_STRING_t *)sptr;
 16.1704 -
 16.1705 -	(void)td;	/* Unused argument */
 16.1706 -	(void)ilevel;	/* Unused argument */
 16.1707 -
 16.1708 -	if(st && (st->buf || !st->size)) {
 16.1709 -		return (cb(st->buf, st->size, app_key) < 0) ? -1 : 0;
 16.1710 -	} else {
 16.1711 -		return (cb("<absent>", 8, app_key) < 0) ? -1 : 0;
 16.1712 -	}
 16.1713 -}
 16.1714 -
 16.1715 -void
 16.1716 -OCTET_STRING_free(asn_TYPE_descriptor_t *td, void *sptr, int contents_only) {
 16.1717 -	OCTET_STRING_t *st = (OCTET_STRING_t *)sptr;
 16.1718 -	asn_OCTET_STRING_specifics_t *specs = td->specifics
 16.1719 -				? (asn_OCTET_STRING_specifics_t *)td->specifics
 16.1720 -				: &asn_DEF_OCTET_STRING_specs;
 16.1721 -	asn_struct_ctx_t *ctx = (asn_struct_ctx_t *)
 16.1722 -					((char *)st + specs->ctx_offset);
 16.1723 -	struct _stack *stck;
 16.1724 -
 16.1725 -	if(!td || !st)
 16.1726 -		return;
 16.1727 -
 16.1728 -	ASN_DEBUG("Freeing %s as OCTET STRING", td->name);
 16.1729 -
 16.1730 -	if(st->buf) {
 16.1731 -		FREEMEM(st->buf);
 16.1732 -		st->buf = 0;
 16.1733 -	}
 16.1734 -
 16.1735 -	/*
 16.1736 -	 * Remove decode-time stack.
 16.1737 -	 */
 16.1738 -	stck = (struct _stack *)ctx->ptr;
 16.1739 -	if(stck) {
 16.1740 -		while(stck->tail) {
 16.1741 -			struct _stack_el *sel = stck->tail;
 16.1742 -			stck->tail = sel->prev;
 16.1743 -			FREEMEM(sel);
 16.1744 -		}
 16.1745 -		FREEMEM(stck);
 16.1746 -	}
 16.1747 -
 16.1748 -	if(!contents_only) {
 16.1749 -		FREEMEM(st);
 16.1750 -	}
 16.1751 -}
 16.1752 -
 16.1753 -/*
 16.1754 - * Conversion routines.
 16.1755 - */
 16.1756 -int
 16.1757 -OCTET_STRING_fromBuf(OCTET_STRING_t *st, const char *str, int len) {
 16.1758 -	void *buf;
 16.1759 -
 16.1760 -	if(st == 0 || (str == 0 && len)) {
 16.1761 -		errno = EINVAL;
 16.1762 -		return -1;
 16.1763 -	}
 16.1764 -
 16.1765 -	/*
 16.1766 -	 * Clear the OCTET STRING.
 16.1767 -	 */
 16.1768 -	if(str == NULL) {
 16.1769 -		FREEMEM(st->buf);
 16.1770 -		st->buf = 0;
 16.1771 -		st->size = 0;
 16.1772 -		return 0;
 16.1773 -	}
 16.1774 -
 16.1775 -	/* Determine the original string size, if not explicitly given */
 16.1776 -	if(len < 0)
 16.1777 -		len = strlen(str);
 16.1778 -
 16.1779 -	/* Allocate and fill the memory */
 16.1780 -	buf = MALLOC(len + 1);
 16.1781 -	if(buf == NULL)
 16.1782 -		return -1;
 16.1783 -
 16.1784 -	memcpy(buf, str, len);
 16.1785 -	((uint8_t *)buf)[len] = '\0';	/* Couldn't use memcpy(len+1)! */
 16.1786 -	FREEMEM(st->buf);
 16.1787 -	st->buf = (uint8_t *)buf;
 16.1788 -	st->size = len;
 16.1789 -
 16.1790 -	return 0;
 16.1791 -}
 16.1792 -
 16.1793 -OCTET_STRING_t *
 16.1794 -OCTET_STRING_new_fromBuf(asn_TYPE_descriptor_t *td, const char *str, int len) {
 16.1795 -	asn_OCTET_STRING_specifics_t *specs = td->specifics
 16.1796 -				? (asn_OCTET_STRING_specifics_t *)td->specifics
 16.1797 -				: &asn_DEF_OCTET_STRING_specs;
 16.1798 -	OCTET_STRING_t *st;
 16.1799 -
 16.1800 -	st = (OCTET_STRING_t *)CALLOC(1, specs->struct_size);
 16.1801 -	if(st && str && OCTET_STRING_fromBuf(st, str, len)) {
 16.1802 -		FREEMEM(st);
 16.1803 -		st = NULL;
 16.1804 -	}
 16.1805 -
 16.1806 -	return st;
 16.1807 -}
 16.1808 -
    17.1 --- a/asn.1/OCTET_STRING.h	Sun May 15 21:23:36 2016 +0200
    17.2 +++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
    17.3 @@ -1,86 +0,0 @@
    17.4 -/*-
    17.5 - * Copyright (c) 2003 Lev Walkin <vlm@lionet.info>. All rights reserved.
    17.6 - * Redistribution and modifications are permitted subject to BSD license.
    17.7 - */
    17.8 -#ifndef	_OCTET_STRING_H_
    17.9 -#define	_OCTET_STRING_H_
   17.10 -
   17.11 -#include <asn_application.h>
   17.12 -
   17.13 -#ifdef __cplusplus
   17.14 -extern "C" {
   17.15 -#endif
   17.16 -
   17.17 -typedef struct OCTET_STRING {
   17.18 -	uint8_t *buf;	/* Buffer with consecutive OCTET_STRING bits */
   17.19 -	int size;	/* Size of the buffer */
   17.20 -
   17.21 -	asn_struct_ctx_t _asn_ctx;	/* Parsing across buffer boundaries */
   17.22 -} OCTET_STRING_t;
   17.23 -
   17.24 -extern asn_TYPE_descriptor_t asn_DEF_OCTET_STRING;
   17.25 -
   17.26 -asn_struct_free_f OCTET_STRING_free;
   17.27 -asn_struct_print_f OCTET_STRING_print;
   17.28 -asn_struct_print_f OCTET_STRING_print_utf8;
   17.29 -ber_type_decoder_f OCTET_STRING_decode_ber;
   17.30 -der_type_encoder_f OCTET_STRING_encode_der;
   17.31 -xer_type_decoder_f OCTET_STRING_decode_xer_hex;		/* Hexadecimal */
   17.32 -xer_type_decoder_f OCTET_STRING_decode_xer_binary;	/* 01010111010 */
   17.33 -xer_type_decoder_f OCTET_STRING_decode_xer_utf8;	/* ASCII/UTF-8 */
   17.34 -xer_type_encoder_f OCTET_STRING_encode_xer;
   17.35 -xer_type_encoder_f OCTET_STRING_encode_xer_utf8;
   17.36 -per_type_decoder_f OCTET_STRING_decode_uper;
   17.37 -per_type_encoder_f OCTET_STRING_encode_uper;
   17.38 -
   17.39 -/******************************
   17.40 - * Handy conversion routines. *
   17.41 - ******************************/
   17.42 -
   17.43 -/*
   17.44 - * This function clears the previous value of the OCTET STRING (if any)
   17.45 - * and then allocates a new memory with the specified content (str/size).
   17.46 - * If size = -1, the size of the original string will be determined
   17.47 - * using strlen(str).
   17.48 - * If str equals to NULL, the function will silently clear the
   17.49 - * current contents of the OCTET STRING.
   17.50 - * Returns 0 if it was possible to perform operation, -1 otherwise.
   17.51 - */
   17.52 -int OCTET_STRING_fromBuf(OCTET_STRING_t *s, const char *str, int size);
   17.53 -
   17.54 -/* Handy conversion from the C string into the OCTET STRING. */
   17.55 -#define	OCTET_STRING_fromString(s, str)	OCTET_STRING_fromBuf(s, str, -1)
   17.56 -
   17.57 -/*
   17.58 - * Allocate and fill the new OCTET STRING and return a pointer to the newly
   17.59 - * allocated object. NULL is permitted in str: the function will just allocate
   17.60 - * empty OCTET STRING.
   17.61 - */
   17.62 -OCTET_STRING_t *OCTET_STRING_new_fromBuf(asn_TYPE_descriptor_t *td,
   17.63 -	const char *str, int size);
   17.64 -
   17.65 -/****************************
   17.66 - * Internally useful stuff. *
   17.67 - ****************************/
   17.68 -
   17.69 -typedef struct asn_OCTET_STRING_specifics_s {
   17.70 -	/*
   17.71 -	 * Target structure description.
   17.72 -	 */
   17.73 -	int struct_size;	/* Size of the structure */
   17.74 -	int ctx_offset;		/* Offset of the asn_struct_ctx_t member */
   17.75 -
   17.76 -	enum asn_OS_Subvariant {
   17.77 -		ASN_OSUBV_ANY,	/* The open type (ANY) */
   17.78 -		ASN_OSUBV_BIT,	/* BIT STRING */
   17.79 -		ASN_OSUBV_STR,	/* String types, not {BMP,Universal}String  */
   17.80 -		ASN_OSUBV_U16,	/* 16-bit character (BMPString) */
   17.81 -		ASN_OSUBV_U32	/* 32-bit character (UniversalString) */
   17.82 -	} subvariant;
   17.83 -} asn_OCTET_STRING_specifics_t;
   17.84 -
   17.85 -#ifdef __cplusplus
   17.86 -}
   17.87 -#endif
   17.88 -
   17.89 -#endif	/* _OCTET_STRING_H_ */
    18.1 --- a/asn.1/PrintableString.c	Sun May 15 21:23:36 2016 +0200
    18.2 +++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
    18.3 @@ -1,109 +0,0 @@
    18.4 -/*-
    18.5 - * Copyright (c) 2003, 2004, 2006 Lev Walkin <vlm@lionet.info>.
    18.6 - * All rights reserved.
    18.7 - * Redistribution and modifications are permitted subject to BSD license.
    18.8 - */
    18.9 -#include <asn_internal.h>
   18.10 -#include <PrintableString.h>
   18.11 -
   18.12 -/*
   18.13 - * ASN.1:1984 (X.409)
   18.14 - */
   18.15 -static int _PrintableString_alphabet[256] = {
   18.16 - 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,	/*                  */
   18.17 - 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,	/*                  */
   18.18 - 1, 0, 0, 0, 0, 0, 0, 2, 3, 4, 0, 5, 6, 7, 8, 9,	/* .      '() +,-./ */
   18.19 -10,11,12,13,14,15,16,17,18,19,20, 0, 0,21, 0,22,	/* 0123456789:  = ? */
   18.20 - 0,23,24,25,26,27,28,29,30,31,32,33,34,35,36,37,	/*  ABCDEFGHIJKLMNO */
   18.21 -38,39,40,41,42,43,44,45,46,47,48, 0, 0, 0, 0, 0,	/* PQRSTUVWXYZ      */
   18.22 - 0,49,50,51,52,53,54,55,56,57,58,59,60,61,62,63,	/*  abcdefghijklmno */
   18.23 -64,65,66,67,68,69,70,71,72,73,74, 0, 0, 0, 0, 0,	/* pqrstuvwxyz      */
   18.24 -};
   18.25 -static int _PrintableString_code2value[74] = { 
   18.26 -32,39,40,41,43,44,45,46,47,48,49,50,51,52,53,54,
   18.27 -55,56,57,58,61,63,65,66,67,68,69,70,71,72,73,74,
   18.28 -75,76,77,78,79,80,81,82,83,84,85,86,87,88,89,90,
   18.29 -97,98,99,100,101,102,103,104,105,106,107,108,109,110,111,112,
   18.30 -113,114,115,116,117,118,119,120,121,122};
   18.31 -
   18.32 -/*
   18.33 - * PrintableString basic type description.
   18.34 - */
   18.35 -static ber_tlv_tag_t asn_DEF_PrintableString_tags[] = {
   18.36 -	(ASN_TAG_CLASS_UNIVERSAL | (19 << 2)),	/* [UNIVERSAL 19] IMPLICIT ...*/
   18.37 -	(ASN_TAG_CLASS_UNIVERSAL | (4 << 2))	/* ... OCTET STRING */
   18.38 -};
   18.39 -static int asn_DEF_PrintableString_v2c(unsigned int value) {
   18.40 -	return _PrintableString_alphabet[value > 255 ? 0 : value] - 1;
   18.41 -}
   18.42 -static int asn_DEF_PrintableString_c2v(unsigned int code) {
   18.43 -	if(code < 74)
   18.44 -		return _PrintableString_code2value[code];
   18.45 -	return -1;
   18.46 -}
   18.47 -static asn_per_constraints_t asn_DEF_PrintableString_constraints = {
   18.48 -	{ APC_CONSTRAINED, 4, 4, 0x20, 0x39 },	/* Value */
   18.49 -	{ APC_SEMI_CONSTRAINED, -1, -1, 0, 0 },	/* Size */
   18.50 -	asn_DEF_PrintableString_v2c,
   18.51 -	asn_DEF_PrintableString_c2v
   18.52 -};
   18.53 -asn_TYPE_descriptor_t asn_DEF_PrintableString = {
   18.54 -	"PrintableString",
   18.55 -	"PrintableString",
   18.56 -	OCTET_STRING_free,
   18.57 -	OCTET_STRING_print_utf8,	/* ASCII subset */
   18.58 -	PrintableString_constraint,
   18.59 -	OCTET_STRING_decode_ber,      /* Implemented in terms of OCTET STRING */
   18.60 -	OCTET_STRING_encode_der,
   18.61 -	OCTET_STRING_decode_xer_utf8,
   18.62 -	OCTET_STRING_encode_xer_utf8,
   18.63 -	OCTET_STRING_decode_uper,
   18.64 -	OCTET_STRING_encode_uper,
   18.65 -	0, /* Use generic outmost tag fetcher */
   18.66 -	asn_DEF_PrintableString_tags,
   18.67 -	sizeof(asn_DEF_PrintableString_tags)
   18.68 -	  / sizeof(asn_DEF_PrintableString_tags[0]) - 1,
   18.69 -	asn_DEF_PrintableString_tags,
   18.70 -	sizeof(asn_DEF_PrintableString_tags)
   18.71 -	  / sizeof(asn_DEF_PrintableString_tags[0]),
   18.72 -	&asn_DEF_PrintableString_constraints,
   18.73 -	0, 0,	/* No members */
   18.74 -	0	/* No specifics */
   18.75 -};
   18.76 -
   18.77 -
   18.78 -int
   18.79 -PrintableString_constraint(asn_TYPE_descriptor_t *td, const void *sptr,
   18.80 -		asn_app_constraint_failed_f *ctfailcb, void *app_key) {
   18.81 -	const PrintableString_t *st = (const PrintableString_t *)sptr;
   18.82 -
   18.83 -	if(st && st->buf) {
   18.84 -		uint8_t *buf = st->buf;
   18.85 -		uint8_t *end = buf + st->size;
   18.86 -
   18.87 -		/*
   18.88 -		 * Check the alphabet of the PrintableString.
   18.89 -		 * ASN.1:1984 (X.409)
   18.90 -		 */
   18.91 -		for(; buf < end; buf++) {
   18.92 -			if(!_PrintableString_alphabet[*buf]) {
   18.93 -				_ASN_CTFAIL(app_key, td, sptr,
   18.94 -					"%s: value byte %ld (%d) "
   18.95 -					"not in PrintableString alphabet "
   18.96 -					"(%s:%d)",
   18.97 -					td->name,
   18.98 -					(long)((buf - st->buf) + 1),
   18.99 -					*buf,
  18.100 -					__FILE__, __LINE__);
  18.101 -				return -1;
  18.102 -			}
  18.103 -		}
  18.104 -	} else {
  18.105 -		_ASN_CTFAIL(app_key, td, sptr,
  18.106 -			"%s: value not given (%s:%d)",
  18.107 -			td->name, __FILE__, __LINE__);
  18.108 -		return -1;
  18.109 -	}
  18.110 -
  18.111 -	return 0;
  18.112 -}
    19.1 --- a/asn.1/PrintableString.h	Sun May 15 21:23:36 2016 +0200
    19.2 +++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
    19.3 @@ -1,24 +0,0 @@
    19.4 -/*-
    19.5 - * Copyright (c) 2003 Lev Walkin <vlm@lionet.info>. All rights reserved.
    19.6 - * Redistribution and modifications are permitted subject to BSD license.
    19.7 - */
    19.8 -#ifndef	_PrintableString_H_
    19.9 -#define	_PrintableString_H_
   19.10 -
   19.11 -#include <OCTET_STRING.h>
   19.12 -
   19.13 -#ifdef __cplusplus
   19.14 -extern "C" {
   19.15 -#endif
   19.16 -
   19.17 -typedef OCTET_STRING_t PrintableString_t;  /* Implemented via OCTET STRING */
   19.18 -
   19.19 -extern asn_TYPE_descriptor_t asn_DEF_PrintableString;
   19.20 -
   19.21 -asn_constr_check_f PrintableString_constraint;
   19.22 -
   19.23 -#ifdef __cplusplus
   19.24 -}
   19.25 -#endif
   19.26 -
   19.27 -#endif	/* _PrintableString_H_ */
    20.1 --- a/asn.1/Stringlist.c	Sun May 15 21:23:36 2016 +0200
    20.2 +++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
    20.3 @@ -1,52 +0,0 @@
    20.4 -/*
    20.5 - * Generated by asn1c-0.9.26 (http://lionet.info/asn1c)
    20.6 - * From ASN.1 module "PEP"
    20.7 - * 	found in "pEp.asn1"
    20.8 - */
    20.9 -
   20.10 -#include "Stringlist.h"
   20.11 -
   20.12 -static asn_TYPE_member_t asn_MBR_Stringlist_1[] = {
   20.13 -	{ ATF_POINTER, 0, 0,
   20.14 -		(ASN_TAG_CLASS_UNIVERSAL | (12 << 2)),
   20.15 -		0,
   20.16 -		&asn_DEF_UTF8String,
   20.17 -		0,	/* Defer constraints checking to the member type */
   20.18 -		0,	/* No PER visible constraints */
   20.19 -		0,
   20.20 -		""
   20.21 -		},
   20.22 -};
   20.23 -static ber_tlv_tag_t asn_DEF_Stringlist_tags_1[] = {
   20.24 -	(ASN_TAG_CLASS_UNIVERSAL | (16 << 2))
   20.25 -};
   20.26 -static asn_SET_OF_specifics_t asn_SPC_Stringlist_specs_1 = {
   20.27 -	sizeof(struct Stringlist),
   20.28 -	offsetof(struct Stringlist, _asn_ctx),
   20.29 -	0,	/* XER encoding is XMLDelimitedItemList */
   20.30 -};
   20.31 -asn_TYPE_descriptor_t asn_DEF_Stringlist = {
   20.32 -	"Stringlist",
   20.33 -	"Stringlist",
   20.34 -	SEQUENCE_OF_free,
   20.35 -	SEQUENCE_OF_print,
   20.36 -	SEQUENCE_OF_constraint,
   20.37 -	SEQUENCE_OF_decode_ber,
   20.38 -	SEQUENCE_OF_encode_der,
   20.39 -	SEQUENCE_OF_decode_xer,
   20.40 -	SEQUENCE_OF_encode_xer,
   20.41 -	SEQUENCE_OF_decode_uper,
   20.42 -	SEQUENCE_OF_encode_uper,
   20.43 -	0,	/* Use generic outmost tag fetcher */
   20.44 -	asn_DEF_Stringlist_tags_1,
   20.45 -	sizeof(asn_DEF_Stringlist_tags_1)
   20.46 -		/sizeof(asn_DEF_Stringlist_tags_1[0]), /* 1 */
   20.47 -	asn_DEF_Stringlist_tags_1,	/* Same as above */
   20.48 -	sizeof(asn_DEF_Stringlist_tags_1)
   20.49 -		/sizeof(asn_DEF_Stringlist_tags_1[0]), /* 1 */
   20.50 -	0,	/* No PER visible constraints */
   20.51 -	asn_MBR_Stringlist_1,
   20.52 -	1,	/* Single element */
   20.53 -	&asn_SPC_Stringlist_specs_1	/* Additional specs */
   20.54 -};
   20.55 -
    21.1 --- a/asn.1/Stringlist.h	Sun May 15 21:23:36 2016 +0200
    21.2 +++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
    21.3 @@ -1,38 +0,0 @@
    21.4 -/*
    21.5 - * Generated by asn1c-0.9.26 (http://lionet.info/asn1c)
    21.6 - * From ASN.1 module "PEP"
    21.7 - * 	found in "pEp.asn1"
    21.8 - */
    21.9 -
   21.10 -#ifndef	_Stringlist_H_
   21.11 -#define	_Stringlist_H_
   21.12 -
   21.13 -
   21.14 -#include <asn_application.h>
   21.15 -
   21.16 -/* Including external dependencies */
   21.17 -#include <UTF8String.h>
   21.18 -#include <asn_SEQUENCE_OF.h>
   21.19 -#include <constr_SEQUENCE_OF.h>
   21.20 -
   21.21 -#ifdef __cplusplus
   21.22 -extern "C" {
   21.23 -#endif
   21.24 -
   21.25 -/* Stringlist */
   21.26 -typedef struct Stringlist {
   21.27 -	A_SEQUENCE_OF(UTF8String_t) list;
   21.28 -	
   21.29 -	/* Context for parsing across buffer boundaries */
   21.30 -	asn_struct_ctx_t _asn_ctx;
   21.31 -} Stringlist_t;
   21.32 -
   21.33 -/* Implementation */
   21.34 -extern asn_TYPE_descriptor_t asn_DEF_Stringlist;
   21.35 -
   21.36 -#ifdef __cplusplus
   21.37 -}
   21.38 -#endif
   21.39 -
   21.40 -#endif	/* _Stringlist_H_ */
   21.41 -#include <asn_internal.h>
    22.1 --- a/asn.1/UTF8String.c	Sun May 15 21:23:36 2016 +0200
    22.2 +++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
    22.3 @@ -1,184 +0,0 @@
    22.4 -/*-
    22.5 - * Copyright (c) 2003, 2004, 2006 Lev Walkin <vlm@lionet.info>.
    22.6 - * All rights reserved.
    22.7 - * Redistribution and modifications are permitted subject to BSD license.
    22.8 - */
    22.9 -#include <asn_internal.h>
   22.10 -#include <UTF8String.h>
   22.11 -
   22.12 -/*
   22.13 - * UTF8String basic type description.
   22.14 - */
   22.15 -static ber_tlv_tag_t asn_DEF_UTF8String_tags[] = {
   22.16 -	(ASN_TAG_CLASS_UNIVERSAL | (12 << 2)),	/* [UNIVERSAL 12] IMPLICIT ...*/
   22.17 -	(ASN_TAG_CLASS_UNIVERSAL | (4 << 2)),	/* ... OCTET STRING */
   22.18 -};
   22.19 -asn_TYPE_descriptor_t asn_DEF_UTF8String = {
   22.20 -	"UTF8String",
   22.21 -	"UTF8String",
   22.22 -	OCTET_STRING_free,
   22.23 -	UTF8String_print,
   22.24 -	UTF8String_constraint,      /* Check for invalid codes, etc. */
   22.25 -	OCTET_STRING_decode_ber,    /* Implemented in terms of OCTET STRING */
   22.26 -	OCTET_STRING_encode_der,
   22.27 -	OCTET_STRING_decode_xer_utf8,
   22.28 -	OCTET_STRING_encode_xer_utf8,
   22.29 -	OCTET_STRING_decode_uper,
   22.30 -	OCTET_STRING_encode_uper,
   22.31 -	0, /* Use generic outmost tag fetcher */
   22.32 -	asn_DEF_UTF8String_tags,
   22.33 -	sizeof(asn_DEF_UTF8String_tags)
   22.34 -	  / sizeof(asn_DEF_UTF8String_tags[0]) - 1,
   22.35 -	asn_DEF_UTF8String_tags,
   22.36 -	sizeof(asn_DEF_UTF8String_tags)
   22.37 -	  / sizeof(asn_DEF_UTF8String_tags[0]),
   22.38 -	0,	/* No PER visible constraints */
   22.39 -	0, 0,	/* No members */
   22.40 -	0	/* No specifics */
   22.41 -};
   22.42 -
   22.43 -/*
   22.44 - * This is the table of length expectations.
   22.45 - * The second half of this table is only applicable to the long sequences.
   22.46 - */
   22.47 -static int UTF8String_ht[2][16] = {
   22.48 -	{ /* 0x0 ... 0x7 */
   22.49 -	  /* 0000..0111 */
   22.50 -	  1, 1, 1, 1, 1, 1, 1, 1,
   22.51 -	  /* 1000..1011(0), 1100..1101(2), 1110(3), 1111(-1) */
   22.52 -	  0, 0, 0, 0, 2, 2, 3, -1 },
   22.53 -	{ /* 0xF0 .. 0xF7 */
   22.54 -	  /* 11110000..11110111 */
   22.55 -	  4, 4, 4, 4, 4, 4, 4, 4,
   22.56 -	  5, 5, 5, 5, 6, 6, -1, -1 }
   22.57 -};
   22.58 -static int32_t UTF8String_mv[7] = { 0, 0,
   22.59 -	0x00000080,
   22.60 -	0x00000800,
   22.61 -	0x00010000,
   22.62 -	0x00200000,
   22.63 -	0x04000000
   22.64 -};
   22.65 -
   22.66 -/* Internal aliases for return codes */
   22.67 -#define	U8E_TRUNC	-1	/* UTF-8 sequence truncated */
   22.68 -#define	U8E_ILLSTART	-2	/* Illegal UTF-8 sequence start */
   22.69 -#define	U8E_NOTCONT	-3	/* Continuation expectation failed */
   22.70 -#define	U8E_NOTMIN	-4	/* Not minimal length encoding */
   22.71 -#define	U8E_EINVAL	-5	/* Invalid arguments */
   22.72 -
   22.73 -int
   22.74 -UTF8String_constraint(asn_TYPE_descriptor_t *td, const void *sptr,
   22.75 -		asn_app_constraint_failed_f *ctfailcb, void *app_key) {
   22.76 -	ssize_t len = UTF8String_length((const UTF8String_t *)sptr);
   22.77 -	switch(len) {
   22.78 -	case U8E_EINVAL:
   22.79 -		_ASN_CTFAIL(app_key, td, sptr,
   22.80 -			"%s: value not given", td->name);
   22.81 -		break;
   22.82 -	case U8E_TRUNC:
   22.83 -		_ASN_CTFAIL(app_key, td, sptr,
   22.84 -			"%s: truncated UTF-8 sequence (%s:%d)",
   22.85 -			td->name, __FILE__, __LINE__);
   22.86 -		break;
   22.87 -	case U8E_ILLSTART:
   22.88 -		_ASN_CTFAIL(app_key, td, sptr,
   22.89 -			"%s: UTF-8 illegal start of encoding (%s:%d)",
   22.90 -			td->name, __FILE__, __LINE__);
   22.91 -		break;
   22.92 -	case U8E_NOTCONT:
   22.93 -		_ASN_CTFAIL(app_key, td, sptr,
   22.94 -			"%s: UTF-8 not continuation (%s:%d)",
   22.95 -			td->name, __FILE__, __LINE__);
   22.96 -		break;
   22.97 -	case U8E_NOTMIN:
   22.98 -		_ASN_CTFAIL(app_key, td, sptr,
   22.99 -			"%s: UTF-8 not minimal sequence (%s:%d)",
  22.100 -			td->name, __FILE__, __LINE__);
  22.101 -		break;
  22.102 -	}
  22.103 -	return (len < 0) ? -1 : 0;
  22.104 -}
  22.105 -
  22.106 -static ssize_t
  22.107 -UTF8String__process(const UTF8String_t *st, uint32_t *dst, size_t dstlen) {
  22.108 -	size_t length;
  22.109 -	uint8_t *buf = st->buf;
  22.110 -	uint8_t *end = buf + st->size;
  22.111 -	uint32_t *dstend = dst + dstlen;
  22.112 -
  22.113 -	for(length = 0; buf < end; length++) {
  22.114 -		int ch = *buf;
  22.115 -		uint8_t *cend;
  22.116 -		int32_t value;
  22.117 -		int want;
  22.118 -
  22.119 -		/* Compute the sequence length */
  22.120 -		want = UTF8String_ht[0][ch >> 4];
  22.121 -		switch(want) {
  22.122 -		case -1:
  22.123 -			/* Second half of the table, long sequence */
  22.124 -			want = UTF8String_ht[1][ch & 0x0F];
  22.125 -			if(want != -1) break;
  22.126 -			/* Fall through */
  22.127 -		case 0:
  22.128 -			return U8E_ILLSTART;
  22.129 -		}
  22.130 -
  22.131 -		/* assert(want >= 1 && want <= 6) */
  22.132 -
  22.133 -		/* Check character sequence length */
  22.134 -		if(buf + want > end) return U8E_TRUNC;
  22.135 -
  22.136 -		value = ch & (0xff >> want);
  22.137 -		cend = buf + want;
  22.138 -		for(buf++; buf < cend; buf++) {
  22.139 -			ch = *buf;
  22.140 -			if(ch < 0x80 || ch > 0xbf) return U8E_NOTCONT;
  22.141 -			value = (value << 6) | (ch & 0x3F);
  22.142 -		}
  22.143 -		if(value < UTF8String_mv[want])
  22.144 -			return U8E_NOTMIN;
  22.145 -		if(dst < dstend)
  22.146 -			*dst++ = value;	/* Record value */
  22.147 -	}
  22.148 -
  22.149 -	if(dst < dstend) *dst = 0;	/* zero-terminate */
  22.150 -
  22.151 -	return length;
  22.152 -}
  22.153 -
  22.154 -
  22.155 -ssize_t
  22.156 -UTF8String_length(const UTF8String_t *st) {
  22.157 -	if(st && st->buf) {
  22.158 -		return UTF8String__process(st, 0, 0);
  22.159 -	} else {
  22.160 -		return U8E_EINVAL;
  22.161 -	}
  22.162 -}
  22.163 -
  22.164 -size_t
  22.165 -UTF8String_to_wcs(const UTF8String_t *st, uint32_t *dst, size_t dstlen) {
  22.166 -	if(st && st->buf) {
  22.167 -		ssize_t ret = UTF8String__process(st, dst, dstlen);
  22.168 -		return (ret < 0) ? 0 : ret;
  22.169 -	} else {
  22.170 -		return 0;
  22.171 -	}
  22.172 -}
  22.173 -
  22.174 -int
  22.175 -UTF8String_print(asn_TYPE_descriptor_t *td, const void *sptr, int ilevel,
  22.176 -	asn_app_consume_bytes_f *cb, void *app_key) {
  22.177 -	const UTF8String_t *st = (const UTF8String_t *)sptr;
  22.178 -
  22.179 -	(void)td;	/* Unused argument */
  22.180 -	(void)ilevel;	/* Unused argument */
  22.181 -
  22.182 -	if(st && st->buf) {
  22.183 -		return (cb(st->buf, st->size, app_key) < 0) ? -1 : 0;
  22.184 -	} else {
  22.185 -		return (cb("<absent>", 8, app_key) < 0) ? -1 : 0;
  22.186 -	}
  22.187 -}
    23.1 --- a/asn.1/UTF8String.h	Sun May 15 21:23:36 2016 +0200
    23.2 +++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
    23.3 @@ -1,49 +0,0 @@
    23.4 -/*-
    23.5 - * Copyright (c) 2003, 2004 Lev Walkin <vlm@lionet.info>. All rights reserved.
    23.6 - * Redistribution and modifications are permitted subject to BSD license.
    23.7 - */
    23.8 -#ifndef	_UTF8String_H_
    23.9 -#define	_UTF8String_H_
   23.10 -
   23.11 -#include <OCTET_STRING.h>
   23.12 -
   23.13 -#ifdef __cplusplus
   23.14 -extern "C" {
   23.15 -#endif
   23.16 -
   23.17 -typedef OCTET_STRING_t UTF8String_t;	/* Implemented via OCTET STRING */
   23.18 -
   23.19 -extern asn_TYPE_descriptor_t asn_DEF_UTF8String;
   23.20 -
   23.21 -asn_struct_print_f UTF8String_print;
   23.22 -asn_constr_check_f UTF8String_constraint;
   23.23 -
   23.24 -/*
   23.25 - * Returns length of the given UTF-8 string in characters,
   23.26 - * or a negative error code:
   23.27 - * -1:	UTF-8 sequence truncated 
   23.28 - * -2:	Illegal UTF-8 sequence start
   23.29 - * -3:	Continuation expectation failed
   23.30 - * -4:	Not minimal length encoding
   23.31 - * -5:	Invalid arguments
   23.32 - */
   23.33 -ssize_t UTF8String_length(const UTF8String_t *st);
   23.34 -
   23.35 -/*
   23.36 - * Convert the UTF-8 string into a sequence of wide characters.
   23.37 - * Returns the number of characters necessary.
   23.38 - * Returned value might be greater than dstlen.
   23.39 - * In case of conversion error, 0 is returned.
   23.40 - * 
   23.41 - * If st points to a valid UTF-8 string, calling
   23.42 - * 	UTF8String_to_wcs(st, 0, 0);
   23.43 - * is equivalent to
   23.44 - * 	UTF8String_length(const UTF8String_t *st);
   23.45 - */
   23.46 -size_t UTF8String_to_wcs(const UTF8String_t *st, uint32_t *dst, size_t dstlen);
   23.47 -
   23.48 -#ifdef __cplusplus
   23.49 -}
   23.50 -#endif
   23.51 -
   23.52 -#endif	/* _UTF8String_H_ */
    24.1 --- a/asn.1/Version.c	Sun May 15 21:23:36 2016 +0200
    24.2 +++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
    24.3 @@ -1,129 +0,0 @@
    24.4 -/*
    24.5 - * Generated by asn1c-0.9.26 (http://lionet.info/asn1c)
    24.6 - * From ASN.1 module "PEP"
    24.7 - * 	found in "pEp.asn1"
    24.8 - */
    24.9 -
   24.10 -#include "Version.h"
   24.11 -
   24.12 -static int
   24.13 -memb_major_constraint_1(asn_TYPE_descriptor_t *td, const void *sptr,
   24.14 -			asn_app_constraint_failed_f *ctfailcb, void *app_key) {
   24.15 -	long value;
   24.16 -	
   24.17 -	if(!sptr) {
   24.18 -		_ASN_CTFAIL(app_key, td, sptr,
   24.19 -			"%s: value not given (%s:%d)",
   24.20 -			td->name, __FILE__, __LINE__);
   24.21 -		return -1;
   24.22 -	}
   24.23 -	
   24.24 -	value = *(const long *)sptr;
   24.25 -	
   24.26 -	if((value >= 0 && value <= 255)) {
   24.27 -		/* Constraint check succeeded */
   24.28 -		return 0;
   24.29 -	} else {
   24.30 -		_ASN_CTFAIL(app_key, td, sptr,
   24.31 -			"%s: constraint failed (%s:%d)",
   24.32 -			td->name, __FILE__, __LINE__);
   24.33 -		return -1;
   24.34 -	}
   24.35 -}
   24.36 -
   24.37 -static int
   24.38 -memb_minor_constraint_1(asn_TYPE_descriptor_t *td, const void *sptr,
   24.39 -			asn_app_constraint_failed_f *ctfailcb, void *app_key) {
   24.40 -	long value;
   24.41 -	
   24.42 -	if(!sptr) {
   24.43 -		_ASN_CTFAIL(app_key, td, sptr,
   24.44 -			"%s: value not given (%s:%d)",
   24.45 -			td->name, __FILE__, __LINE__);
   24.46 -		return -1;
   24.47 -	}
   24.48 -	
   24.49 -	value = *(const long *)sptr;
   24.50 -	
   24.51 -	if((value >= 0 && value <= 255)) {
   24.52 -		/* Constraint check succeeded */
   24.53 -		return 0;
   24.54 -	} else {
   24.55 -		_ASN_CTFAIL(app_key, td, sptr,
   24.56 -			"%s: constraint failed (%s:%d)",
   24.57 -			td->name, __FILE__, __LINE__);
   24.58 -		return -1;
   24.59 -	}
   24.60 -}
   24.61 -
   24.62 -static asn_per_constraints_t asn_PER_memb_major_constr_2 GCC_NOTUSED = {
   24.63 -	{ APC_CONSTRAINED,	 8,  8,  0,  255 }	/* (0..255) */,
   24.64 -	{ APC_UNCONSTRAINED,	-1, -1,  0,  0 },
   24.65 -	0, 0	/* No PER value map */
   24.66 -};
   24.67 -static asn_per_constraints_t asn_PER_memb_minor_constr_3 GCC_NOTUSED = {
   24.68 -	{ APC_CONSTRAINED,	 8,  8,  0,  255 }	/* (0..255) */,
   24.69 -	{ APC_UNCONSTRAINED,	-1, -1,  0,  0 },
   24.70 -	0, 0	/* No PER value map */
   24.71 -};
   24.72 -static asn_TYPE_member_t asn_MBR_Version_1[] = {
   24.73 -	{ ATF_NOFLAGS, 0, offsetof(struct Version, major),
   24.74 -		(ASN_TAG_CLASS_UNIVERSAL | (2 << 2)),
   24.75 -		0,
   24.76 -		&asn_DEF_NativeInteger,
   24.77 -		memb_major_constraint_1,
   24.78 -		&asn_PER_memb_major_constr_2,
   24.79 -		0,
   24.80 -		"major"
   24.81 -		},
   24.82 -	{ ATF_NOFLAGS, 0, offsetof(struct Version, minor),
   24.83 -		(ASN_TAG_CLASS_UNIVERSAL | (2 << 2)),
   24.84 -		0,
   24.85 -		&asn_DEF_NativeInteger,
   24.86 -		memb_minor_constraint_1,
   24.87 -		&asn_PER_memb_minor_constr_3,
   24.88 -		0,
   24.89 -		"minor"
   24.90 -		},
   24.91 -};
   24.92 -static ber_tlv_tag_t asn_DEF_Version_tags_1[] = {
   24.93 -	(ASN_TAG_CLASS_UNIVERSAL | (16 << 2))
   24.94 -};
   24.95 -static asn_TYPE_tag2member_t asn_MAP_Version_tag2el_1[] = {
   24.96 -    { (ASN_TAG_CLASS_UNIVERSAL | (2 << 2)), 0, 0, 1 }, /* major */
   24.97 -    { (ASN_TAG_CLASS_UNIVERSAL | (2 << 2)), 1, -1, 0 } /* minor */
   24.98 -};
   24.99 -static asn_SEQUENCE_specifics_t asn_SPC_Version_specs_1 = {
  24.100 -	sizeof(struct Version),
  24.101 -	offsetof(struct Version, _asn_ctx),
  24.102 -	asn_MAP_Version_tag2el_1,
  24.103 -	2,	/* Count of tags in the map */
  24.104 -	0, 0, 0,	/* Optional elements (not needed) */
  24.105 -	-1,	/* Start extensions */
  24.106 -	-1	/* Stop extensions */
  24.107 -};
  24.108 -asn_TYPE_descriptor_t asn_DEF_Version = {
  24.109 -	"Version",
  24.110 -	"Version",
  24.111 -	SEQUENCE_free,
  24.112 -	SEQUENCE_print,
  24.113 -	SEQUENCE_constraint,
  24.114 -	SEQUENCE_decode_ber,
  24.115 -	SEQUENCE_encode_der,
  24.116 -	SEQUENCE_decode_xer,
  24.117 -	SEQUENCE_encode_xer,
  24.118 -	SEQUENCE_decode_uper,
  24.119 -	SEQUENCE_encode_uper,
  24.120 -	0,	/* Use generic outmost tag fetcher */
  24.121 -	asn_DEF_Version_tags_1,
  24.122 -	sizeof(asn_DEF_Version_tags_1)
  24.123 -		/sizeof(asn_DEF_Version_tags_1[0]), /* 1 */
  24.124 -	asn_DEF_Version_tags_1,	/* Same as above */
  24.125 -	sizeof(asn_DEF_Version_tags_1)
  24.126 -		/sizeof(asn_DEF_Version_tags_1[0]), /* 1 */
  24.127 -	0,	/* No PER visible constraints */
  24.128 -	asn_MBR_Version_1,
  24.129 -	2,	/* Elements count */
  24.130 -	&asn_SPC_Version_specs_1	/* Additional specs */
  24.131 -};
  24.132 -
    25.1 --- a/asn.1/Version.h	Sun May 15 21:23:36 2016 +0200
    25.2 +++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
    25.3 @@ -1,38 +0,0 @@
    25.4 -/*
    25.5 - * Generated by asn1c-0.9.26 (http://lionet.info/asn1c)
    25.6 - * From ASN.1 module "PEP"
    25.7 - * 	found in "pEp.asn1"
    25.8 - */
    25.9 -
   25.10 -#ifndef	_Version_H_
   25.11 -#define	_Version_H_
   25.12 -
   25.13 -
   25.14 -#include <asn_application.h>
   25.15 -
   25.16 -/* Including external dependencies */
   25.17 -#include <NativeInteger.h>
   25.18 -#include <constr_SEQUENCE.h>
   25.19 -
   25.20 -#ifdef __cplusplus
   25.21 -extern "C" {
   25.22 -#endif
   25.23 -
   25.24 -/* Version */
   25.25 -typedef struct Version {
   25.26 -	long	 major;
   25.27 -	long	 minor;
   25.28 -	
   25.29 -	/* Context for parsing across buffer boundaries */
   25.30 -	asn_struct_ctx_t _asn_ctx;
   25.31 -} Version_t;
   25.32 -
   25.33 -/* Implementation */
   25.34 -extern asn_TYPE_descriptor_t asn_DEF_Version;
   25.35 -
   25.36 -#ifdef __cplusplus
   25.37 -}
   25.38 -#endif
   25.39 -
   25.40 -#endif	/* _Version_H_ */
   25.41 -#include <asn_internal.h>
    26.1 --- a/asn.1/asn_SEQUENCE_OF.c	Sun May 15 21:23:36 2016 +0200
    26.2 +++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
    26.3 @@ -1,41 +0,0 @@
    26.4 -/*-
    26.5 - * Copyright (c) 2003, 2004 Lev Walkin <vlm@lionet.info>. All rights reserved.
    26.6 - * Redistribution and modifications are permitted subject to BSD license.
    26.7 - */
    26.8 -#include <asn_internal.h>
    26.9 -#include <asn_SEQUENCE_OF.h>
   26.10 -
   26.11 -typedef A_SEQUENCE_OF(void) asn_sequence;
   26.12 -
   26.13 -void
   26.14 -asn_sequence_del(void *asn_sequence_of_x, int number, int _do_free) {
   26.15 -	asn_sequence *as = (asn_sequence *)asn_sequence_of_x;
   26.16 -
   26.17 -	if(as) {
   26.18 -		void *ptr;
   26.19 -		int n;
   26.20 -
   26.21 -		if(number < 0 || number >= as->count)
   26.22 -			return;	/* Nothing to delete */
   26.23 -
   26.24 -		if(_do_free && as->free) {
   26.25 -			ptr = as->array[number];
   26.26 -		} else {
   26.27 -			ptr = 0;
   26.28 -		}
   26.29 -
   26.30 -		/*
   26.31 -		 * Shift all elements to the left to hide the gap.
   26.32 -		 */
   26.33 -		--as->count;
   26.34 -		for(n = number; n < as->count; n++)
   26.35 -			as->array[n] = as->array[n+1];
   26.36 -
   26.37 -		/*
   26.38 -		 * Invoke the third-party function only when the state
   26.39 -		 * of the parent structure is consistent.
   26.40 -		 */
   26.41 -		if(ptr) as->free(ptr);
   26.42 -	}
   26.43 -}
   26.44 -
    27.1 --- a/asn.1/asn_SEQUENCE_OF.h	Sun May 15 21:23:36 2016 +0200
    27.2 +++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
    27.3 @@ -1,52 +0,0 @@
    27.4 -/*-
    27.5 - * Copyright (c) 2003, 2004 Lev Walkin <vlm@lionet.info>. All rights reserved.
    27.6 - * Redistribution and modifications are permitted subject to BSD license.
    27.7 - */
    27.8 -#ifndef	ASN_SEQUENCE_OF_H
    27.9 -#define	ASN_SEQUENCE_OF_H
   27.10 -
   27.11 -#include <asn_SET_OF.h>
   27.12 -
   27.13 -#ifdef __cplusplus
   27.14 -extern "C" {
   27.15 -#endif
   27.16 -
   27.17 -/*
   27.18 - * SEQUENCE OF is the same as SET OF with a tiny difference:
   27.19 - * the delete operation preserves the initial order of elements
   27.20 - * and thus MAY operate in non-constant time.
   27.21 - */
   27.22 -#define	A_SEQUENCE_OF(type)	A_SET_OF(type)
   27.23 -
   27.24 -#define	ASN_SEQUENCE_ADD(headptr, ptr)		\
   27.25 -	asn_sequence_add((headptr), (ptr))
   27.26 -
   27.27 -/***********************************************
   27.28 - * Implementation of the SEQUENCE OF structure.
   27.29 - */
   27.30 -
   27.31 -#define	asn_sequence_add	asn_set_add
   27.32 -#define	asn_sequence_empty	asn_set_empty
   27.33 -
   27.34 -/*
   27.35 - * Delete the element from the set by its number (base 0).
   27.36 - * This is NOT a constant-time operation.
   27.37 - * The order of elements is preserved.
   27.38 - * If _do_free is given AND the (*free) is initialized, the element
   27.39 - * will be freed using the custom (*free) function as well.
   27.40 - */
   27.41 -void asn_sequence_del(void *asn_sequence_of_x, int number, int _do_free);
   27.42 -
   27.43 -/*
   27.44 - * Cope with different conversions requirements to/from void in C and C++.
   27.45 - * This is mostly useful for support library.
   27.46 - */
   27.47 -typedef A_SEQUENCE_OF(void) asn_anonymous_sequence_;
   27.48 -#define _A_SEQUENCE_FROM_VOID(ptr)	((asn_anonymous_sequence_ *)(ptr))
   27.49 -#define _A_CSEQUENCE_FROM_VOID(ptr) 	((const asn_anonymous_sequence_ *)(ptr))
   27.50 -
   27.51 -#ifdef __cplusplus
   27.52 -}
   27.53 -#endif
   27.54 -
   27.55 -#endif	/* ASN_SEQUENCE_OF_H */
    28.1 --- a/asn.1/asn_SET_OF.c	Sun May 15 21:23:36 2016 +0200
    28.2 +++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
    28.3 @@ -1,88 +0,0 @@
    28.4 -/*-
    28.5 - * Copyright (c) 2003, 2004 Lev Walkin <vlm@lionet.info>. All rights reserved.
    28.6 - * Redistribution and modifications are permitted subject to BSD license.
    28.7 - */
    28.8 -#include <asn_internal.h>
    28.9 -#include <asn_SET_OF.h>
   28.10 -#include <errno.h>
   28.11 -
   28.12 -/*
   28.13 - * Add another element into the set.
   28.14 - */
   28.15 -int
   28.16 -asn_set_add(void *asn_set_of_x, void *ptr) {
   28.17 -	asn_anonymous_set_ *as = _A_SET_FROM_VOID(asn_set_of_x);
   28.18 -
   28.19 -	if(as == 0 || ptr == 0) {
   28.20 -		errno = EINVAL;		/* Invalid arguments */
   28.21 -		return -1;
   28.22 -	}
   28.23 -
   28.24 -	/*
   28.25 -	 * Make sure there's enough space to insert an element.
   28.26 -	 */
   28.27 -	if(as->count == as->size) {
   28.28 -		int _newsize = as->size ? (as->size << 1) : 4;
   28.29 -		void *_new_arr;
   28.30 -		_new_arr = REALLOC(as->array, _newsize * sizeof(as->array[0]));
   28.31 -		if(_new_arr) {
   28.32 -			as->array = (void **)_new_arr;
   28.33 -			as->size = _newsize;
   28.34 -		} else {
   28.35 -			/* ENOMEM */
   28.36 -			return -1;
   28.37 -		}
   28.38 -	}
   28.39 -
   28.40 -	as->array[as->count++] = ptr;
   28.41 -
   28.42 -	return 0;
   28.43 -}
   28.44 -
   28.45 -void
   28.46 -asn_set_del(void *asn_set_of_x, int number, int _do_free) {
   28.47 -	asn_anonymous_set_ *as = _A_SET_FROM_VOID(asn_set_of_x);
   28.48 -
   28.49 -	if(as) {
   28.50 -		void *ptr;
   28.51 -		if(number < 0 || number >= as->count)
   28.52 -			return;
   28.53 -
   28.54 -		if(_do_free && as->free) {
   28.55 -			ptr = as->array[number];
   28.56 -		} else {
   28.57 -			ptr = 0;
   28.58 -		}
   28.59 -
   28.60 -		as->array[number] = as->array[--as->count];
   28.61 -
   28.62 -		/*
   28.63 -		 * Invoke the third-party function only when the state
   28.64 -		 * of the parent structure is consistent.
   28.65 -		 */
   28.66 -		if(ptr) as->free(ptr);
   28.67 -	}
   28.68 -}
   28.69 -
   28.70 -/*
   28.71 - * Free the contents of the set, do not free the set itself.
   28.72 - */
   28.73 -void
   28.74 -asn_set_empty(void *asn_set_of_x) {
   28.75 -	asn_anonymous_set_ *as = _A_SET_FROM_VOID(asn_set_of_x);
   28.76 -
   28.77 -	if(as) {
   28.78 -		if(as->array) {
   28.79 -			if(as->free) {
   28.80 -				while(as->count--)
   28.81 -					as->free(as->array[as->count]);
   28.82 -			}
   28.83 -			FREEMEM(as->array);
   28.84 -			as->array = 0;
   28.85 -		}
   28.86 -		as->count = 0;
   28.87 -		as->size = 0;
   28.88 -	}
   28.89 -
   28.90 -}
   28.91 -
    29.1 --- a/asn.1/asn_SET_OF.h	Sun May 15 21:23:36 2016 +0200
    29.2 +++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
    29.3 @@ -1,62 +0,0 @@
    29.4 -/*-
    29.5 - * Copyright (c) 2003, 2004 Lev Walkin <vlm@lionet.info>. All rights reserved.
    29.6 - * Redistribution and modifications are permitted subject to BSD license.
    29.7 - */
    29.8 -#ifndef	ASN_SET_OF_H
    29.9 -#define	ASN_SET_OF_H
   29.10 -
   29.11 -#ifdef __cplusplus
   29.12 -extern "C" {
   29.13 -#endif
   29.14 -
   29.15 -#define	A_SET_OF(type)					\
   29.16 -	struct {					\
   29.17 -		type **array;				\
   29.18 -		int count;	/* Meaningful size */	\
   29.19 -		int size;	/* Allocated size */	\
   29.20 -		void (*free)(type *);			\
   29.21 -	}
   29.22 -
   29.23 -#define	ASN_SET_ADD(headptr, ptr)		\
   29.24 -	asn_set_add((headptr), (ptr))
   29.25 -
   29.26 -/*******************************************
   29.27 - * Implementation of the SET OF structure.
   29.28 - */
   29.29 -
   29.30 -/*
   29.31 - * Add another structure into the set by its pointer.
   29.32 - * RETURN VALUES:
   29.33 - * 0 for success and -1/errno for failure.
   29.34 - */
   29.35 -int  asn_set_add(void *asn_set_of_x, void *ptr);
   29.36 -
   29.37 -/*
   29.38 - * Delete the element from the set by its number (base 0).
   29.39 - * This is a constant-time operation. The order of elements before the
   29.40 - * deleted ones is guaranteed, the order of elements after the deleted
   29.41 - * one is NOT guaranteed.
   29.42 - * If _do_free is given AND the (*free) is initialized, the element
   29.43 - * will be freed using the custom (*free) function as well.
   29.44 - */
   29.45 -void asn_set_del(void *asn_set_of_x, int number, int _do_free);
   29.46 -
   29.47 -/*
   29.48 - * Empty the contents of the set. Will free the elements, if (*free) is given.
   29.49 - * Will NOT free the set itself.
   29.50 - */
   29.51 -void asn_set_empty(void *asn_set_of_x);
   29.52 -
   29.53 -/*
   29.54 - * Cope with different conversions requirements to/from void in C and C++.
   29.55 - * This is mostly useful for support library.
   29.56 - */
   29.57 -typedef A_SET_OF(void) asn_anonymous_set_;
   29.58 -#define _A_SET_FROM_VOID(ptr)		((asn_anonymous_set_ *)(ptr))
   29.59 -#define _A_CSET_FROM_VOID(ptr)		((const asn_anonymous_set_ *)(ptr))
   29.60 -
   29.61 -#ifdef __cplusplus
   29.62 -}
   29.63 -#endif
   29.64 -
   29.65 -#endif	/* ASN_SET_OF_H */
    30.1 --- a/asn.1/asn_application.h	Sun May 15 21:23:36 2016 +0200
    30.2 +++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
    30.3 @@ -1,47 +0,0 @@
    30.4 -/*-
    30.5 - * Copyright (c) 2004, 2006 Lev Walkin <vlm@lionet.info>. All rights reserved.
    30.6 - * Redistribution and modifications are permitted subject to BSD license.
    30.7 - */
    30.8 -/*
    30.9 - * Application-level ASN.1 callbacks.
   30.10 - */
   30.11 -#ifndef	_ASN_APPLICATION_H_
   30.12 -#define	_ASN_APPLICATION_H_
   30.13 -
   30.14 -#include "asn_system.h"		/* for platform-dependent types */
   30.15 -#include "asn_codecs.h"		/* for ASN.1 codecs specifics */
   30.16 -
   30.17 -#ifdef __cplusplus
   30.18 -extern "C" {
   30.19 -#endif
   30.20 -
   30.21 -/*
   30.22 - * Generic type of an application-defined callback to return various
   30.23 - * types of data to the application.
   30.24 - * EXPECTED RETURN VALUES:
   30.25 - *  -1: Failed to consume bytes. Abort the mission.
   30.26 - * Non-negative return values indicate success, and ignored.
   30.27 - */
   30.28 -typedef int (asn_app_consume_bytes_f)(const void *buffer, size_t size,
   30.29 -	void *application_specific_key);
   30.30 -
   30.31 -/*
   30.32 - * A callback of this type is called whenever constraint validation fails
   30.33 - * on some ASN.1 type. See "constraints.h" for more details on constraint
   30.34 - * validation.
   30.35 - * This callback specifies a descriptor of the ASN.1 type which failed
   30.36 - * the constraint check, as well as human readable message on what
   30.37 - * particular constraint has failed.
   30.38 - */
   30.39 -typedef void (asn_app_constraint_failed_f)(void *application_specific_key,
   30.40 -	struct asn_TYPE_descriptor_s *type_descriptor_which_failed,
   30.41 -	const void *structure_which_failed_ptr,
   30.42 -	const char *error_message_format, ...) GCC_PRINTFLIKE(4, 5);
   30.43 -
   30.44 -#ifdef __cplusplus
   30.45 -}
   30.46 -#endif
   30.47 -
   30.48 -#include "constr_TYPE.h"	/* for asn_TYPE_descriptor_t */
   30.49 -
   30.50 -#endif	/* _ASN_APPLICATION_H_ */
    31.1 --- a/asn.1/asn_codecs.h	Sun May 15 21:23:36 2016 +0200
    31.2 +++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
    31.3 @@ -1,109 +0,0 @@
    31.4 -/*-
    31.5 - * Copyright (c) 2003, 2004, 2005 Lev Walkin <vlm@lionet.info>.
    31.6 - * All rights reserved.
    31.7 - * Redistribution and modifications are permitted subject to BSD license.
    31.8 - */
    31.9 -#ifndef	_ASN_CODECS_H_
   31.10 -#define	_ASN_CODECS_H_
   31.11 -
   31.12 -#ifdef __cplusplus
   31.13 -extern "C" {
   31.14 -#endif
   31.15 -
   31.16 -struct asn_TYPE_descriptor_s;	/* Forward declaration */
   31.17 -
   31.18 -/*
   31.19 - * This structure defines a set of parameters that may be passed
   31.20 - * to every ASN.1 encoder or decoder function.
   31.21 - * WARNING: if max_stack_size member is set, and you are calling the
   31.22 - *   function pointers of the asn_TYPE_descriptor_t directly,
   31.23 - *   this structure must be ALLOCATED ON THE STACK!
   31.24 - *   If you can't always satisfy this requirement, use ber_decode(),
   31.25 - *   xer_decode() and uper_decode() functions instead.
   31.26 - */
   31.27 -typedef struct asn_codec_ctx_s {
   31.28 -	/*
   31.29 -	 * Limit the decoder routines to use no (much) more stack than a given
   31.30 -	 * number of bytes. Most of decoders are stack-based, and this
   31.31 -	 * would protect against stack overflows if the number of nested
   31.32 -	 * encodings is high.
   31.33 -	 * The OCTET STRING, BIT STRING and ANY BER decoders are heap-based,
   31.34 -	 * and are safe from this kind of overflow.
   31.35 -	 * A value from getrlimit(RLIMIT_STACK) may be used to initialize
   31.36 -	 * this variable. Be careful in multithreaded environments, as the
   31.37 -	 * stack size is rather limited.
   31.38 -	 */
   31.39 -	size_t  max_stack_size; /* 0 disables stack bounds checking */
   31.40 -} asn_codec_ctx_t;
   31.41 -
   31.42 -/*
   31.43 - * Type of the return value of the encoding functions (der_encode, xer_encode).
   31.44 - */
   31.45 -typedef struct asn_enc_rval_s {
   31.46 -	/*
   31.47 -	 * Number of bytes encoded.
   31.48 -	 * -1 indicates failure to encode the structure.
   31.49 -	 * In this case, the members below this one are meaningful.
   31.50 -	 */
   31.51 -	ssize_t encoded;
   31.52 -
   31.53 -	/*
   31.54 -	 * Members meaningful when (encoded == -1), for post mortem analysis.
   31.55 -	 */
   31.56 -
   31.57 -	/* Type which cannot be encoded */
   31.58 -	struct asn_TYPE_descriptor_s *failed_type;
   31.59 -
   31.60 -	/* Pointer to the structure of that type */
   31.61 -	void *structure_ptr;
   31.62 -} asn_enc_rval_t;
   31.63 -#define	_ASN_ENCODE_FAILED do {					\
   31.64 -	asn_enc_rval_t tmp_error;				\
   31.65 -	tmp_error.encoded = -1;					\
   31.66 -	tmp_error.failed_type = td;				\
   31.67 -	tmp_error.structure_ptr = sptr;				\
   31.68 -	ASN_DEBUG("Failed to encode element %s", td ? td->name : "");	\
   31.69 -	return tmp_error;					\
   31.70 -} while(0)
   31.71 -#define	_ASN_ENCODED_OK(rval) do {				\
   31.72 -	rval.structure_ptr = 0;					\
   31.73 -	rval.failed_type = 0;					\
   31.74 -	return rval;						\
   31.75 -} while(0)
   31.76 -
   31.77 -/*
   31.78 - * Type of the return value of the decoding functions (ber_decode, xer_decode)
   31.79 - * 
   31.80 - * Please note that the number of consumed bytes is ALWAYS meaningful,
   31.81 - * even if code==RC_FAIL. This is to indicate the number of successfully
   31.82 - * decoded bytes, hence providing a possibility to fail with more diagnostics
   31.83 - * (i.e., print the offending remainder of the buffer).
   31.84 - */
   31.85 -enum asn_dec_rval_code_e {
   31.86 -	RC_OK,		/* Decoded successfully */
   31.87 -	RC_WMORE,	/* More data expected, call again */
   31.88 -	RC_FAIL		/* Failure to decode data */
   31.89 -};
   31.90 -typedef struct asn_dec_rval_s {
   31.91 -	enum asn_dec_rval_code_e code;	/* Result code */
   31.92 -	size_t consumed;		/* Number of bytes consumed */
   31.93 -} asn_dec_rval_t;
   31.94 -#define	_ASN_DECODE_FAILED do {					\
   31.95 -	asn_dec_rval_t tmp_error;				\
   31.96 -	tmp_error.code = RC_FAIL;				\
   31.97 -	tmp_error.consumed = 0;					\
   31.98 -	ASN_DEBUG("Failed to decode element %s", td ? td->name : "");	\
   31.99 -	return tmp_error;					\
  31.100 -} while(0)
  31.101 -#define	_ASN_DECODE_STARVED do {				\
  31.102 -	asn_dec_rval_t tmp_error;				\
  31.103 -	tmp_error.code = RC_WMORE;				\
  31.104 -	tmp_error.consumed = 0;					\
  31.105 -	return tmp_error;					\
  31.106 -} while(0)
  31.107 -
  31.108 -#ifdef __cplusplus
  31.109 -}
  31.110 -#endif
  31.111 -
  31.112 -#endif	/* _ASN_CODECS_H_ */
    32.1 --- a/asn.1/asn_codecs_prim.c	Sun May 15 21:23:36 2016 +0200
    32.2 +++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
    32.3 @@ -1,312 +0,0 @@
    32.4 -/*-
    32.5 - * Copyright (c) 2003, 2004 Lev Walkin <vlm@lionet.info>. All rights reserved.
    32.6 - * Redistribution and modifications are permitted subject to BSD license.
    32.7 - */
    32.8 -#include <asn_internal.h>
    32.9 -#include <asn_codecs_prim.h>
   32.10 -#include <errno.h>
   32.11 -
   32.12 -/*
   32.13 - * Decode an always-primitive type.
   32.14 - */
   32.15 -asn_dec_rval_t
   32.16 -ber_decode_primitive(asn_codec_ctx_t *opt_codec_ctx,
   32.17 -	asn_TYPE_descriptor_t *td,
   32.18 -	void **sptr, const void *buf_ptr, size_t size, int tag_mode) {
   32.19 -	ASN__PRIMITIVE_TYPE_t *st = (ASN__PRIMITIVE_TYPE_t *)*sptr;
   32.20 -	asn_dec_rval_t rval;
   32.21 -	ber_tlv_len_t length = 0; // =0 to avoid [incorrect] warning.
   32.22 -
   32.23 -	/*
   32.24 -	 * If the structure is not there, allocate it.
   32.25 -	 */
   32.26 -	if(st == NULL) {
   32.27 -		st = (ASN__PRIMITIVE_TYPE_t *)CALLOC(1, sizeof(*st));
   32.28 -		if(st == NULL) _ASN_DECODE_FAILED;
   32.29 -		*sptr = (void *)st;
   32.30 -	}
   32.31 -
   32.32 -	ASN_DEBUG("Decoding %s as plain primitive (tm=%d)",
   32.33 -		td->name, tag_mode);
   32.34 -
   32.35 -	/*
   32.36 -	 * Check tags and extract value length.
   32.37 -	 */
   32.38 -	rval = ber_check_tags(opt_codec_ctx, td, 0, buf_ptr, size,
   32.39 -			tag_mode, 0, &length, 0);
   32.40 -	if(rval.code != RC_OK)
   32.41 -		return rval;
   32.42 -
   32.43 -	ASN_DEBUG("%s length is %d bytes", td->name, (int)length);
   32.44 -
   32.45 -	/*
   32.46 -	 * Make sure we have this length.
   32.47 -	 */
   32.48 -	buf_ptr = ((const char *)buf_ptr) + rval.consumed;
   32.49 -	size -= rval.consumed;
   32.50 -	if(length > (ber_tlv_len_t)size) {
   32.51 -		rval.code = RC_WMORE;
   32.52 -		rval.consumed = 0;
   32.53 -		return rval;
   32.54 -	}
   32.55 -
   32.56 -	st->size = (int)length;
   32.57 -	/* The following better be optimized away. */
   32.58 -	if(sizeof(st->size) != sizeof(length)
   32.59 -			&& (ber_tlv_len_t)st->size != length) {
   32.60 -		st->size = 0;
   32.61 -		_ASN_DECODE_FAILED;
   32.62 -	}
   32.63 -
   32.64 -	st->buf = (uint8_t *)MALLOC(length + 1);
   32.65 -	if(!st->buf) {
   32.66 -		st->size = 0;
   32.67 -		_ASN_DECODE_FAILED;
   32.68 -	}
   32.69 -
   32.70 -	memcpy(st->buf, buf_ptr, length);
   32.71 -	st->buf[length] = '\0';		/* Just in case */
   32.72 -
   32.73 -	rval.code = RC_OK;
   32.74 -	rval.consumed += length;
   32.75 -
   32.76 -	ASN_DEBUG("Took %ld/%ld bytes to encode %s",
   32.77 -		(long)rval.consumed,
   32.78 -		(long)length, td->name);
   32.79 -
   32.80 -	return rval;
   32.81 -}
   32.82 -
   32.83 -/*
   32.84 - * Encode an always-primitive type using DER.
   32.85 - */
   32.86 -asn_enc_rval_t
   32.87 -der_encode_primitive(asn_TYPE_descriptor_t *td, void *sptr,
   32.88 -	int tag_mode, ber_tlv_tag_t tag,
   32.89 -	asn_app_consume_bytes_f *cb, void *app_key) {
   32.90 -	asn_enc_rval_t erval;
   32.91 -	ASN__PRIMITIVE_TYPE_t *st = (ASN__PRIMITIVE_TYPE_t *)sptr;
   32.92 -
   32.93 -	ASN_DEBUG("%s %s as a primitive type (tm=%d)",
   32.94 -		cb?"Encoding":"Estimating", td->name, tag_mode);
   32.95 -
   32.96 -	erval.encoded = der_write_tags(td, st->size, tag_mode, 0, tag,
   32.97 -		cb, app_key);
   32.98 -	ASN_DEBUG("%s wrote tags %d", td->name, (int)erval.encoded);
   32.99 -	if(erval.encoded == -1) {
  32.100 -		erval.failed_type = td;
  32.101 -		erval.structure_ptr = sptr;
  32.102 -		return erval;
  32.103 -	}
  32.104 -
  32.105 -	if(cb && st->buf) {
  32.106 -		if(cb(st->buf, st->size, app_key) < 0) {
  32.107 -			erval.encoded = -1;
  32.108 -			erval.failed_type = td;
  32.109 -			erval.structure_ptr = sptr;
  32.110 -			return erval;
  32.111 -		}
  32.112 -	} else {
  32.113 -		assert(st->buf || st->size == 0);
  32.114 -	}
  32.115 -
  32.116 -	erval.encoded += st->size;
  32.117 -	_ASN_ENCODED_OK(erval);
  32.118 -}
  32.119 -
  32.120 -void
  32.121 -ASN__PRIMITIVE_TYPE_free(asn_TYPE_descriptor_t *td, void *sptr,
  32.122 -		int contents_only) {
  32.123 -	ASN__PRIMITIVE_TYPE_t *st = (ASN__PRIMITIVE_TYPE_t *)sptr;
  32.124 -
  32.125 -	if(!td || !sptr)
  32.126 -		return;
  32.127 -
  32.128 -	ASN_DEBUG("Freeing %s as a primitive type", td->name);
  32.129 -
  32.130 -	if(st->buf)
  32.131 -		FREEMEM(st->buf);
  32.132 -
  32.133 -	if(!contents_only)
  32.134 -		FREEMEM(st);
  32.135 -}
  32.136 -
  32.137 -
  32.138 -/*
  32.139 - * Local internal type passed around as an argument.
  32.140 - */
  32.141 -struct xdp_arg_s {
  32.142 -	asn_TYPE_descriptor_t *type_descriptor;
  32.143 -	void *struct_key;
  32.144 -	xer_primitive_body_decoder_f *prim_body_decoder;
  32.145 -	int decoded_something;
  32.146 -	int want_more;
  32.147 -};
  32.148 -
  32.149 -/*
  32.150 - * Since some kinds of primitive values can be encoded using value-specific
  32.151 - * tags (<MINUS-INFINITY>, <enum-element>, etc), the primitive decoder must
  32.152 - * be supplied with such tags to parse them as needed.
  32.153 - */
  32.154 -static int
  32.155 -xer_decode__unexpected_tag(void *key, const void *chunk_buf, size_t chunk_size) {
  32.156 -	struct xdp_arg_s *arg = (struct xdp_arg_s *)key;
  32.157 -	enum xer_pbd_rval bret;
  32.158 -
  32.159 -	/*
  32.160 -	 * The chunk_buf is guaranteed to start at '<'.
  32.161 -	 */
  32.162 -	assert(chunk_size && ((const char *)chunk_buf)[0] == 0x3c);
  32.163 -
  32.164 -	/*
  32.165 -	 * Decoding was performed once already. Prohibit doing it again.
  32.166 -	 */
  32.167 -	if(arg->decoded_something)
  32.168 -		return -1;
  32.169 -
  32.170 -	bret = arg->prim_body_decoder(arg->type_descriptor,
  32.171 -		arg->struct_key, chunk_buf, chunk_size);
  32.172 -	switch(bret) {
  32.173 -	case XPBD_SYSTEM_FAILURE:
  32.174 -	case XPBD_DECODER_LIMIT:
  32.175 -	case XPBD_BROKEN_ENCODING:
  32.176 -		break;
  32.177 -	case XPBD_BODY_CONSUMED:
  32.178 -		/* Tag decoded successfully */
  32.179 -		arg->decoded_something = 1;
  32.180 -		/* Fall through */
  32.181 -	case XPBD_NOT_BODY_IGNORE:	/* Safe to proceed further */
  32.182 -		return 0;
  32.183 -	}
  32.184 -
  32.185 -	return -1;
  32.186 -}
  32.187 -
  32.188 -static ssize_t
  32.189 -xer_decode__primitive_body(void *key, const void *chunk_buf, size_t chunk_size, int have_more) {
  32.190 -	struct xdp_arg_s *arg = (struct xdp_arg_s *)key;
  32.191 -	enum xer_pbd_rval bret;
  32.192 -	size_t lead_wsp_size;
  32.193 -
  32.194 -	if(arg->decoded_something) {
  32.195 -		if(xer_whitespace_span(chunk_buf, chunk_size) == chunk_size) {
  32.196 -			/*
  32.197 -			 * Example:
  32.198 -			 * "<INTEGER>123<!--/--> </INTEGER>"
  32.199 -			 *                      ^- chunk_buf position.
  32.200 -			 */
  32.201 -			return chunk_size;
  32.202 -		}
  32.203 -		/*
  32.204 -		 * Decoding was done once already. Prohibit doing it again.
  32.205 -		 */
  32.206 -		return -1;
  32.207 -	}
  32.208 -
  32.209 -	if(!have_more) {
  32.210 -		/*
  32.211 -		 * If we've received something like "1", we can't really
  32.212 -		 * tell whether it is really `1` or `123`, until we know
  32.213 -		 * that there is no more data coming.
  32.214 -		 * The have_more argument will be set to 1 once something
  32.215 -		 * like this is available to the caller of this callback:
  32.216 -		 * "1<tag_start..."
  32.217 -		 */
  32.218 -		arg->want_more = 1;
  32.219 -		return -1;
  32.220 -	}
  32.221 -
  32.222 -	lead_wsp_size = xer_whitespace_span(chunk_buf, chunk_size);
  32.223 -	chunk_buf = (const char *)chunk_buf + lead_wsp_size;
  32.224 -	chunk_size -= lead_wsp_size;
  32.225 -
  32.226 -	bret = arg->prim_body_decoder(arg->type_descriptor,
  32.227 -		arg->struct_key, chunk_buf, chunk_size);
  32.228 -	switch(bret) {
  32.229 -	case XPBD_SYSTEM_FAILURE:
  32.230 -	case XPBD_DECODER_LIMIT:
  32.231 -	case XPBD_BROKEN_ENCODING:
  32.232 -		break;
  32.233 -	case XPBD_BODY_CONSUMED:
  32.234 -		/* Tag decoded successfully */
  32.235 -		arg->decoded_something = 1;
  32.236 -		/* Fall through */
  32.237 -	case XPBD_NOT_BODY_IGNORE:	/* Safe to proceed further */
  32.238 -		return lead_wsp_size + chunk_size;
  32.239 -	}
  32.240 -
  32.241 -	return -1;
  32.242 -}
  32.243 -
  32.244 -
  32.245 -asn_dec_rval_t
  32.246 -xer_decode_primitive(asn_codec_ctx_t *opt_codec_ctx,
  32.247 -	asn_TYPE_descriptor_t *td,
  32.248 -	void **sptr,
  32.249 -	size_t struct_size,
  32.250 -	const char *opt_mname,
  32.251 -	const void *buf_ptr, size_t size,
  32.252 -	xer_primitive_body_decoder_f *prim_body_decoder
  32.253 -) {
  32.254 -	const char *xml_tag = opt_mname ? opt_mname : td->xml_tag;
  32.255 -	asn_struct_ctx_t s_ctx;
  32.256 -	struct xdp_arg_s s_arg;
  32.257 -	asn_dec_rval_t rc;
  32.258 -
  32.259 -	/*
  32.260 -	 * Create the structure if does not exist.
  32.261 -	 */
  32.262 -	if(!*sptr) {
  32.263 -		*sptr = CALLOC(1, struct_size);
  32.264 -		if(!*sptr) _ASN_DECODE_FAILED;
  32.265 -	}
  32.266 -
  32.267 -	memset(&s_ctx, 0, sizeof(s_ctx));
  32.268 -	s_arg.type_descriptor = td;
  32.269 -	s_arg.struct_key = *sptr;
  32.270 -	s_arg.prim_body_decoder = prim_body_decoder;
  32.271 -	s_arg.decoded_something = 0;
  32.272 -	s_arg.want_more = 0;
  32.273 -
  32.274 -	rc = xer_decode_general(opt_codec_ctx, &s_ctx, &s_arg,
  32.275 -		xml_tag, buf_ptr, size,
  32.276 -		xer_decode__unexpected_tag, xer_decode__primitive_body);
  32.277 -	switch(rc.code) {
  32.278 -	case RC_OK:
  32.279 -		if(!s_arg.decoded_something) {
  32.280 -			char ch;
  32.281 -			ASN_DEBUG("Primitive body is not recognized, "
  32.282 -				"supplying empty one");
  32.283 -			/*
  32.284 -			 * Decoding opportunity has come and gone.
  32.285 -			 * Where's the result?
  32.286 -			 * Try to feed with empty body, see if it eats it.
  32.287 -			 */
  32.288 -			if(prim_body_decoder(s_arg.type_descriptor,
  32.289 -				s_arg.struct_key, &ch, 0)
  32.290 -					!= XPBD_BODY_CONSUMED) {
  32.291 -				/*
  32.292 -				 * This decoder does not like empty stuff.
  32.293 -				 */
  32.294 -				_ASN_DECODE_FAILED;
  32.295 -			}
  32.296 -		}
  32.297 -		break;
  32.298 -	case RC_WMORE:
  32.299 -		/*
  32.300 -		 * Redo the whole thing later.
  32.301 -		 * We don't have a context to save intermediate parsing state.
  32.302 -		 */
  32.303 -		rc.consumed = 0;
  32.304 -		break;
  32.305 -	case RC_FAIL:
  32.306 -		rc.consumed = 0;
  32.307 -		if(s_arg.want_more)
  32.308 -			rc.code = RC_WMORE;
  32.309 -		else
  32.310 -			_ASN_DECODE_FAILED;
  32.311 -		break;
  32.312 -	}
  32.313 -	return rc;
  32.314 -}
  32.315 -
    33.1 --- a/asn.1/asn_codecs_prim.h	Sun May 15 21:23:36 2016 +0200
    33.2 +++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
    33.3 @@ -1,53 +0,0 @@
    33.4 -/*-
    33.5 - * Copyright (c) 2004 Lev Walkin <vlm@lionet.info>. All rights reserved.
    33.6 - * Redistribution and modifications are permitted subject to BSD license.
    33.7 - */
    33.8 -#ifndef	ASN_CODECS_PRIM_H
    33.9 -#define	ASN_CODECS_PRIM_H
   33.10 -
   33.11 -#include <asn_application.h>
   33.12 -
   33.13 -#ifdef __cplusplus
   33.14 -extern "C" {
   33.15 -#endif
   33.16 -
   33.17 -typedef struct ASN__PRIMITIVE_TYPE_s {
   33.18 -	uint8_t *buf;	/* Buffer with consecutive primitive encoding bytes */
   33.19 -	int size;	/* Size of the buffer */
   33.20 -} ASN__PRIMITIVE_TYPE_t;	/* Do not use this type directly! */
   33.21 -
   33.22 -asn_struct_free_f ASN__PRIMITIVE_TYPE_free;
   33.23 -ber_type_decoder_f ber_decode_primitive;
   33.24 -der_type_encoder_f der_encode_primitive;
   33.25 -
   33.26 -/*
   33.27 - * A callback specification for the xer_decode_primitive() function below.
   33.28 - */
   33.29 -enum xer_pbd_rval {
   33.30 -	XPBD_SYSTEM_FAILURE,	/* System failure (memory shortage, etc) */
   33.31 -	XPBD_DECODER_LIMIT,	/* Hit some decoder limitation or deficiency */
   33.32 -	XPBD_BROKEN_ENCODING,	/* Encoding of a primitive body is broken */
   33.33 -	XPBD_NOT_BODY_IGNORE,	/* Not a body format, but safe to ignore */
   33.34 -	XPBD_BODY_CONSUMED	/* Body is recognized and consumed */
   33.35 -};
   33.36 -typedef enum xer_pbd_rval (xer_primitive_body_decoder_f)
   33.37 -	(asn_TYPE_descriptor_t *td, void *struct_ptr,
   33.38 -		const void *chunk_buf, size_t chunk_size);
   33.39 -
   33.40 -/*
   33.41 - * Specific function to decode simple primitive types.
   33.42 - * Also see xer_decode_general() in xer_decoder.h
   33.43 - */
   33.44 -asn_dec_rval_t xer_decode_primitive(asn_codec_ctx_t *opt_codec_ctx,
   33.45 -	asn_TYPE_descriptor_t *type_descriptor,
   33.46 -	void **struct_ptr, size_t struct_size,
   33.47 -	const char *opt_mname,
   33.48 -	const void *buf_ptr, size_t size,
   33.49 -	xer_primitive_body_decoder_f *prim_body_decoder
   33.50 -);
   33.51 -
   33.52 -#ifdef __cplusplus
   33.53 -}
   33.54 -#endif
   33.55 -
   33.56 -#endif	/* ASN_CODECS_PRIM_H */
    34.1 --- a/asn.1/asn_internal.h	Sun May 15 21:23:36 2016 +0200
    34.2 +++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
    34.3 @@ -1,126 +0,0 @@
    34.4 -/*-
    34.5 - * Copyright (c) 2003, 2004, 2005, 2007 Lev Walkin <vlm@lionet.info>.
    34.6 - * All rights reserved.
    34.7 - * Redistribution and modifications are permitted subject to BSD license.
    34.8 - */
    34.9 -/*
   34.10 - * Declarations internally useful for the ASN.1 support code.
   34.11 - */
   34.12 -#ifndef	_ASN_INTERNAL_H_
   34.13 -#define	_ASN_INTERNAL_H_
   34.14 -
   34.15 -#include "asn_application.h"	/* Application-visible API */
   34.16 -
   34.17 -#ifndef	__NO_ASSERT_H__		/* Include assert.h only for internal use. */
   34.18 -#include <assert.h>		/* for assert() macro */
   34.19 -#endif
   34.20 -
   34.21 -#ifdef	__cplusplus
   34.22 -extern "C" {
   34.23 -#endif
   34.24 -
   34.25 -/* Environment version might be used to avoid running with the old library */
   34.26 -#define	ASN1C_ENVIRONMENT_VERSION	923	/* Compile-time version */
   34.27 -int get_asn1c_environment_version(void);	/* Run-time version */
   34.28 -
   34.29 -#define	CALLOC(nmemb, size)	calloc(nmemb, size)
   34.30 -#define	MALLOC(size)		malloc(size)
   34.31 -#define	REALLOC(oldptr, size)	realloc(oldptr, size)
   34.32 -#define	FREEMEM(ptr)		free(ptr)
   34.33 -
   34.34 -#define	asn_debug_indent	0
   34.35 -#define ASN_DEBUG_INDENT_ADD(i) do{}while(0)
   34.36 -
   34.37 -/*
   34.38 - * A macro for debugging the ASN.1 internals.
   34.39 - * You may enable or override it.
   34.40 - */
   34.41 -#ifndef	ASN_DEBUG	/* If debugging code is not defined elsewhere... */
   34.42 -#if	EMIT_ASN_DEBUG == 1	/* And it was asked to emit this code... */
   34.43 -#ifdef	__GNUC__
   34.44 -#ifdef	ASN_THREAD_SAFE
   34.45 -/* Thread safety requires sacrifice in output indentation:
   34.46 - * Retain empty definition of ASN_DEBUG_INDENT_ADD. */
   34.47 -#else	/* !ASN_THREAD_SAFE */
   34.48 -#undef  ASN_DEBUG_INDENT_ADD
   34.49 -#undef  asn_debug_indent
   34.50 -int asn_debug_indent;
   34.51 -#define ASN_DEBUG_INDENT_ADD(i) do { asn_debug_indent += i; } while(0)
   34.52 -#endif	/* ASN_THREAD_SAFE */
   34.53 -#define	ASN_DEBUG(fmt, args...)	do {			\
   34.54 -		int adi = asn_debug_indent;		\
   34.55 -		while(adi--) fprintf(stderr, " ");	\
   34.56 -		fprintf(stderr, fmt, ##args);		\
   34.57 -		fprintf(stderr, " (%s:%d)\n",		\
   34.58 -			__FILE__, __LINE__);		\
   34.59 -	} while(0)
   34.60 -#else	/* !__GNUC__ */
   34.61 -void ASN_DEBUG_f(const char *fmt, ...);
   34.62 -#define	ASN_DEBUG	ASN_DEBUG_f
   34.63 -#endif	/* __GNUC__ */
   34.64 -#else	/* EMIT_ASN_DEBUG != 1 */
   34.65 -static inline void ASN_DEBUG(const char *fmt, ...) { (void)fmt; }
   34.66 -#endif	/* EMIT_ASN_DEBUG */
   34.67 -#endif	/* ASN_DEBUG */
   34.68 -
   34.69 -/*
   34.70 - * Invoke the application-supplied callback and fail, if something is wrong.
   34.71 - */
   34.72 -#define	__ASN_E_cbc(buf, size)	(cb((buf), (size), app_key) < 0)
   34.73 -#define	_ASN_E_CALLBACK(foo)	do {					\
   34.74 -		if(foo)	goto cb_failed;					\
   34.75 -	} while(0)
   34.76 -#define	_ASN_CALLBACK(buf, size)					\
   34.77 -	_ASN_E_CALLBACK(__ASN_E_cbc(buf, size))
   34.78 -#define	_ASN_CALLBACK2(buf1, size1, buf2, size2)			\
   34.79 -	_ASN_E_CALLBACK(__ASN_E_cbc(buf1, size1) || __ASN_E_cbc(buf2, size2))
   34.80 -#define	_ASN_CALLBACK3(buf1, size1, buf2, size2, buf3, size3)		\
   34.81 -	_ASN_E_CALLBACK(__ASN_E_cbc(buf1, size1)			\
   34.82 -		|| __ASN_E_cbc(buf2, size2)				\
   34.83 -		|| __ASN_E_cbc(buf3, size3))
   34.84 -
   34.85 -#define	_i_ASN_TEXT_INDENT(nl, level) do {				\
   34.86 -	int __level = (level);						\
   34.87 -	int __nl = ((nl) != 0);						\
   34.88 -	int __i;							\
   34.89 -	if(__nl) _ASN_CALLBACK("\n", 1);				\
   34.90 -	if(__level < 0) __level = 0;					\
   34.91 -	for(__i = 0; __i < __level; __i++)				\
   34.92 -		_ASN_CALLBACK("    ", 4);				\
   34.93 -	er.encoded += __nl + 4 * __level;				\
   34.94 -} while(0)
   34.95 -
   34.96 -#define	_i_INDENT(nl)	do {						\
   34.97 -	int __i;							\
   34.98 -	if((nl) && cb("\n", 1, app_key) < 0) return -1;			\
   34.99 -	for(__i = 0; __i < ilevel; __i++)				\
  34.100 -		if(cb("    ", 4, app_key) < 0) return -1;		\
  34.101 -} while(0)
  34.102 -
  34.103 -/*
  34.104 - * Check stack against overflow, if limit is set.
  34.105 - */
  34.106 -#define	_ASN_DEFAULT_STACK_MAX	(30000)
  34.107 -static inline int
  34.108 -_ASN_STACK_OVERFLOW_CHECK(asn_codec_ctx_t *ctx) {
  34.109 -	if(ctx && ctx->max_stack_size) {
  34.110 -
  34.111 -		/* ctx MUST be allocated on the stack */
  34.112 -		ptrdiff_t usedstack = ((char *)ctx - (char *)&ctx);
  34.113 -		if(usedstack > 0) usedstack = -usedstack; /* grows up! */
  34.114 -
  34.115 -		/* double negative required to avoid int wrap-around */
  34.116 -		if(usedstack < -(ptrdiff_t)ctx->max_stack_size) {
  34.117 -			ASN_DEBUG("Stack limit %ld reached",
  34.118 -				(long)ctx->max_stack_size);
  34.119 -			return -1;
  34.120 -		}
  34.121 -	}
  34.122 -	return 0;
  34.123 -}
  34.124 -
  34.125 -#ifdef	__cplusplus
  34.126 -}
  34.127 -#endif
  34.128 -
  34.129 -#endif	/* _ASN_INTERNAL_H_ */
    35.1 --- a/asn.1/asn_system.h	Sun May 15 21:23:36 2016 +0200
    35.2 +++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
    35.3 @@ -1,129 +0,0 @@
    35.4 -/*-
    35.5 - * Copyright (c) 2003, 2004, 2007 Lev Walkin <vlm@lionet.info>.
    35.6 - * All rights reserved.
    35.7 - * Redistribution and modifications are permitted subject to BSD license.
    35.8 - */
    35.9 -/*
   35.10 - * Miscellaneous system-dependent types.
   35.11 - */
   35.12 -#ifndef	_ASN_SYSTEM_H_
   35.13 -#define	_ASN_SYSTEM_H_
   35.14 -
   35.15 -#ifdef	HAVE_CONFIG_H
   35.16 -#include "config.h"
   35.17 -#endif
   35.18 -
   35.19 -#include <stdio.h>	/* For snprintf(3) */
   35.20 -#include <stdlib.h>	/* For *alloc(3) */
   35.21 -#include <string.h>	/* For memcpy(3) */
   35.22 -#include <sys/types.h>	/* For size_t */
   35.23 -#include <limits.h>	/* For LONG_MAX */
   35.24 -#include <stdarg.h>	/* For va_start */
   35.25 -#include <stddef.h>	/* for offsetof and ptrdiff_t */
   35.26 -
   35.27 -#ifdef	_WIN32
   35.28 -
   35.29 -#include <malloc.h>
   35.30 -#define	 snprintf	_snprintf
   35.31 -#define	 vsnprintf	_vsnprintf
   35.32 -
   35.33 -/* To avoid linking with ws2_32.lib, here's the definition of ntohl() */
   35.34 -#define sys_ntohl(l)	((((l) << 24)  & 0xff000000)	\
   35.35 -			| (((l) << 8) & 0xff0000)	\
   35.36 -			| (((l) >> 8)  & 0xff00)	\
   35.37 -			| ((l >> 24) & 0xff))
   35.38 -
   35.39 -#ifdef _MSC_VER			/* MSVS.Net */
   35.40 -#ifndef __cplusplus
   35.41 -#define inline __inline
   35.42 -#endif
   35.43 -#ifndef	ASSUMESTDTYPES	/* Standard types have been defined elsewhere */
   35.44 -#define	ssize_t		SSIZE_T
   35.45 -typedef	char		int8_t;
   35.46 -typedef	short		int16_t;
   35.47 -typedef	int		int32_t;
   35.48 -typedef	unsigned char	uint8_t;
   35.49 -typedef	unsigned short	uint16_t;
   35.50 -typedef	unsigned int	uint32_t;
   35.51 -#endif	/* ASSUMESTDTYPES */
   35.52 -#define WIN32_LEAN_AND_MEAN
   35.53 -#include <windows.h>
   35.54 -#include <float.h>
   35.55 -#define isnan _isnan
   35.56 -#define finite _finite
   35.57 -#define copysign _copysign
   35.58 -#define	ilogb	_logb
   35.59 -#else	/* !_MSC_VER */
   35.60 -#include <stdint.h>
   35.61 -#endif	/* _MSC_VER */
   35.62 -
   35.63 -#else	/* !_WIN32 */
   35.64 -
   35.65 -#if defined(__vxworks)
   35.66 -#include <types/vxTypes.h>
   35.67 -#else	/* !defined(__vxworks) */
   35.68 -
   35.69 -#include <inttypes.h>	/* C99 specifies this file */
   35.70 -/*
   35.71 - * 1. Earlier FreeBSD version didn't have <stdint.h>,
   35.72 - * but <inttypes.h> was present.
   35.73 - * 2. Sun Solaris requires <alloca.h> for alloca(3),
   35.74 - * but does not have <stdint.h>.
   35.75 - */
   35.76 -#if	(!defined(__FreeBSD__) || !defined(_SYS_INTTYPES_H_))
   35.77 -#if	defined(sun)
   35.78 -#include <alloca.h>	/* For alloca(3) */
   35.79 -#include <ieeefp.h>	/* for finite(3) */
   35.80 -#elif	defined(__hpux)
   35.81 -#ifdef	__GNUC__
   35.82 -#include <alloca.h>	/* For alloca(3) */
   35.83 -#else	/* !__GNUC__ */
   35.84 -#define inline
   35.85 -#endif	/* __GNUC__ */
   35.86 -#else
   35.87 -#include <stdint.h>	/* SUSv2+ and C99 specify this file, for uintXX_t */
   35.88 -#endif	/* defined(sun) */
   35.89 -#endif
   35.90 -
   35.91 -#include <netinet/in.h> /* for ntohl() */
   35.92 -#define	sys_ntohl(foo)	ntohl(foo)
   35.93 -
   35.94 -#endif	/* defined(__vxworks) */
   35.95 -
   35.96 -#endif	/* _WIN32 */
   35.97 -
   35.98 -#if	__GNUC__ >= 3
   35.99 -#ifndef	GCC_PRINTFLIKE
  35.100 -#define	GCC_PRINTFLIKE(fmt,var)	__attribute__((format(printf,fmt,var)))
  35.101 -#endif
  35.102 -#ifndef	GCC_NOTUSED
  35.103 -#define	GCC_NOTUSED		__attribute__((unused))
  35.104 -#endif
  35.105 -#else
  35.106 -#ifndef	GCC_PRINTFLIKE
  35.107 -#define	GCC_PRINTFLIKE(fmt,var)	/* nothing */
  35.108 -#endif
  35.109 -#ifndef	GCC_NOTUSED
  35.110 -#define	GCC_NOTUSED
  35.111 -#endif
  35.112 -#endif
  35.113 -
  35.114 -/* Figure out if thread safety is requested */
  35.115 -#if !defined(ASN_THREAD_SAFE) && (defined(THREAD_SAFE) || defined(_REENTRANT))
  35.116 -#define	ASN_THREAD_SAFE
  35.117 -#endif	/* Thread safety */
  35.118 -
  35.119 -#ifndef	offsetof	/* If not defined by <stddef.h> */
  35.120 -#define	offsetof(s, m)	((ptrdiff_t)&(((s *)0)->m) - (ptrdiff_t)((s *)0))
  35.121 -#endif	/* offsetof */
  35.122 -
  35.123 -#ifndef	MIN		/* Suitable for comparing primitive types (integers) */
  35.124 -#if defined(__GNUC__)
  35.125 -#define	MIN(a,b)	({ __typeof a _a = a; __typeof b _b = b;	\
  35.126 -	((_a)<(_b)?(_a):(_b)); })
  35.127 -#else	/* !__GNUC__ */
  35.128 -#define	MIN(a,b)	((a)<(b)?(a):(b))	/* Unsafe variant */
  35.129 -#endif /* __GNUC__ */
  35.130 -#endif	/* MIN */
  35.131 -
  35.132 -#endif	/* _ASN_SYSTEM_H_ */
    36.1 --- a/asn.1/ber_decoder.c	Sun May 15 21:23:36 2016 +0200
    36.2 +++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
    36.3 @@ -1,283 +0,0 @@
    36.4 -/*-
    36.5 - * Copyright (c) 2003, 2004 Lev Walkin <vlm@lionet.info>. All rights reserved.
    36.6 - * Redistribution and modifications are permitted subject to BSD license.
    36.7 - */
    36.8 -#include <asn_internal.h>
    36.9 -
   36.10 -#undef	ADVANCE
   36.11 -#define	ADVANCE(num_bytes)	do {					\
   36.12 -		size_t num = num_bytes;					\
   36.13 -		ptr = ((const char *)ptr) + num;			\
   36.14 -		size -= num;						\
   36.15 -		consumed_myself += num;					\
   36.16 -	} while(0)
   36.17 -#undef	RETURN
   36.18 -#define	RETURN(_code)	do {						\
   36.19 -		asn_dec_rval_t rval;					\
   36.20 -		rval.code = _code;					\
   36.21 -		if(opt_ctx) opt_ctx->step = step; /* Save context */	\
   36.22 -		if(_code == RC_OK || opt_ctx)				\
   36.23 -			rval.consumed = consumed_myself;		\
   36.24 -		else							\
   36.25 -			rval.consumed = 0;	/* Context-free */	\
   36.26 -		return rval;						\
   36.27 -	} while(0)
   36.28 -
   36.29 -/*
   36.30 - * The BER decoder of any type.
   36.31 - */
   36.32 -asn_dec_rval_t
   36.33 -ber_decode(asn_codec_ctx_t *opt_codec_ctx,
   36.34 -	asn_TYPE_descriptor_t *type_descriptor,
   36.35 -	void **struct_ptr, const void *ptr, size_t size) {
   36.36 -	asn_codec_ctx_t s_codec_ctx;
   36.37 -
   36.38 -	/*
   36.39 -	 * Stack checker requires that the codec context
   36.40 -	 * must be allocated on the stack.
   36.41 -	 */
   36.42 -	if(opt_codec_ctx) {
   36.43 -		if(opt_codec_ctx->max_stack_size) {
   36.44 -			s_codec_ctx = *opt_codec_ctx;
   36.45 -			opt_codec_ctx = &s_codec_ctx;
   36.46 -		}
   36.47 -	} else {
   36.48 -		/* If context is not given, be security-conscious anyway */
   36.49 -		memset(&s_codec_ctx, 0, sizeof(s_codec_ctx));
   36.50 -		s_codec_ctx.max_stack_size = _ASN_DEFAULT_STACK_MAX;
   36.51 -		opt_codec_ctx = &s_codec_ctx;
   36.52 -	}
   36.53 -
   36.54 -	/*
   36.55 -	 * Invoke type-specific decoder.
   36.56 -	 */
   36.57 -	return type_descriptor->ber_decoder(opt_codec_ctx, type_descriptor,
   36.58 -		struct_ptr,	/* Pointer to the destination structure */
   36.59 -		ptr, size,	/* Buffer and its size */
   36.60 -		0		/* Default tag mode is 0 */
   36.61 -		);
   36.62 -}
   36.63 -
   36.64 -/*
   36.65 - * Check the set of <TL<TL<TL...>>> tags matches the definition.
   36.66 - */
   36.67 -asn_dec_rval_t
   36.68 -ber_check_tags(asn_codec_ctx_t *opt_codec_ctx,
   36.69 -		asn_TYPE_descriptor_t *td, asn_struct_ctx_t *opt_ctx,
   36.70 -		const void *ptr, size_t size, int tag_mode, int last_tag_form,
   36.71 -		ber_tlv_len_t *last_length, int *opt_tlv_form) {
   36.72 -	ssize_t consumed_myself = 0;
   36.73 -	ssize_t tag_len;
   36.74 -	ssize_t len_len;
   36.75 -	ber_tlv_tag_t tlv_tag;
   36.76 -	ber_tlv_len_t tlv_len;
   36.77 -	ber_tlv_len_t limit_len = -1;
   36.78 -	int expect_00_terminators = 0;
   36.79 -	int tlv_constr = -1;	/* If CHOICE, opt_tlv_form is not given */
   36.80 -	int step = opt_ctx ? opt_ctx->step : 0;	/* Where we left previously */
   36.81 -	int tagno;
   36.82 -
   36.83 -	/*
   36.84 -	 * Make sure we didn't exceed the maximum stack size.
   36.85 -	 */
   36.86 -	if(_ASN_STACK_OVERFLOW_CHECK(opt_codec_ctx))
   36.87 -		RETURN(RC_FAIL);
   36.88 -
   36.89 -	/*
   36.90 -	 * So what does all this implicit skip stuff mean?
   36.91 -	 * Imagine two types,
   36.92 -	 * 	A ::= [5] IMPLICIT	T
   36.93 -	 * 	B ::= [2] EXPLICIT	T
   36.94 -	 * Where T is defined as
   36.95 -	 *	T ::= [4] IMPLICIT SEQUENCE { ... }
   36.96 -	 * 
   36.97 -	 * Let's say, we are starting to decode type A, given the
   36.98 -	 * following TLV stream: <5> <0>. What does this mean?
   36.99 -	 * It means that the type A contains type T which is,
  36.100 -	 * in turn, empty.
  36.101 -	 * Remember though, that we are still in A. We cannot
  36.102 -	 * just pass control to the type T decoder. Why? Because
  36.103 -	 * the type T decoder expects <4> <0>, not <5> <0>.
  36.104 -	 * So, we must make sure we are going to receive <5> while
  36.105 -	 * still in A, then pass control to the T decoder, indicating
  36.106 -	 * that the tag <4> was implicitly skipped. The decoder of T
  36.107 -	 * hence will be prepared to treat <4> as valid tag, and decode
  36.108 -	 * it appropriately.
  36.109 -	 */
  36.110 -
  36.111 -	tagno = step	/* Continuing where left previously */
  36.112 -		+ (tag_mode==1?-1:0)
  36.113 -		;
  36.114 -	ASN_DEBUG("ber_check_tags(%s, size=%ld, tm=%d, step=%d, tagno=%d)",
  36.115 -		td->name, (long)size, tag_mode, step, tagno);
  36.116 -	/* assert(td->tags_count >= 1) May not be the case for CHOICE or ANY */
  36.117 -
  36.118 -	if(tag_mode == 0 && tagno == td->tags_count) {
  36.119 -		/*
  36.120 -		 * This must be the _untagged_ ANY type,
  36.121 -		 * which outermost tag isn't known in advance.
  36.122 -		 * Fetch the tag and length separately.
  36.123 -		 */
  36.124 -		tag_len = ber_fetch_tag(ptr, size, &tlv_tag);
  36.125 -		switch(tag_len) {
  36.126 -		case -1: RETURN(RC_FAIL);
  36.127 -		case 0: RETURN(RC_WMORE);
  36.128 -		}
  36.129 -		tlv_constr = BER_TLV_CONSTRUCTED(ptr);
  36.130 -		len_len = ber_fetch_length(tlv_constr,
  36.131 -			(const char *)ptr + tag_len, size - tag_len, &tlv_len);
  36.132 -		switch(len_len) {
  36.133 -		case -1: RETURN(RC_FAIL);
  36.134 -		case 0: RETURN(RC_WMORE);
  36.135 -		}
  36.136 -		ASN_DEBUG("Advancing %ld in ANY case",
  36.137 -			(long)(tag_len + len_len));
  36.138 -		ADVANCE(tag_len + len_len);
  36.139 -	} else {
  36.140 -		assert(tagno < td->tags_count);	/* At least one loop */
  36.141 -	}
  36.142 -	for((void)tagno; tagno < td->tags_count; tagno++, step++) {
  36.143 -
  36.144 -		/*
  36.145 -		 * Fetch and process T from TLV.
  36.146 -		 */
  36.147 -		tag_len = ber_fetch_tag(ptr, size, &tlv_tag);
  36.148 -			ASN_DEBUG("Fetching tag from {%p,%ld}: "
  36.149 -				"len %ld, step %d, tagno %d got %s",
  36.150 -				ptr, (long)size,
  36.151 -				(long)tag_len, step, tagno,
  36.152 -				ber_tlv_tag_string(tlv_tag));
  36.153 -		switch(tag_len) {
  36.154 -		case -1: RETURN(RC_FAIL);
  36.155 -		case 0: RETURN(RC_WMORE);
  36.156 -		}
  36.157 -
  36.158 -		tlv_constr = BER_TLV_CONSTRUCTED(ptr);
  36.159 -
  36.160 -		/*
  36.161 -		 * If {I}, don't check anything.
  36.162 -		 * If {I,B,C}, check B and C unless we're at I.
  36.163 -		 */
  36.164 -		if(tag_mode != 0 && step == 0) {
  36.165 -			/*
  36.166 -			 * We don't expect tag to match here.
  36.167 -			 * It's just because we don't know how the tag
  36.168 -			 * is supposed to look like.
  36.169 -			 */
  36.170 -		} else {
  36.171 -		    assert(tagno >= 0);	/* Guaranteed by the code above */
  36.172 -		    if(tlv_tag != td->tags[tagno]) {
  36.173 -			/*
  36.174 -			 * Unexpected tag. Too bad.
  36.175 -			 */
  36.176 -		    	ASN_DEBUG("Expected: %s, "
  36.177 -				"expectation failed (tn=%d, tm=%d)",
  36.178 -				ber_tlv_tag_string(td->tags[tagno]),
  36.179 -				tagno, tag_mode
  36.180 -			);
  36.181 -			RETURN(RC_FAIL);
  36.182 -		    }
  36.183 -		}
  36.184 -
  36.185 -		/*
  36.186 -		 * Attention: if there are more tags expected,
  36.187 -		 * ensure that the current tag is presented
  36.188 -		 * in constructed form (it contains other tags!).
  36.189 -		 * If this one is the last one, check that the tag form
  36.190 -		 * matches the one given in descriptor.
  36.191 -		 */
  36.192 -		if(tagno < (td->tags_count - 1)) {
  36.193 -			if(tlv_constr == 0) {
  36.194 -				ASN_DEBUG("tlv_constr = %d, expfail",
  36.195 -					tlv_constr);
  36.196 -				RETURN(RC_FAIL);
  36.197 -			}
  36.198 -		} else {
  36.199 -			if(last_tag_form != tlv_constr
  36.200 -			&& last_tag_form != -1) {
  36.201 -				ASN_DEBUG("last_tag_form %d != %d",
  36.202 -					last_tag_form, tlv_constr);
  36.203 -				RETURN(RC_FAIL);
  36.204 -			}
  36.205 -		}
  36.206 -
  36.207 -		/*
  36.208 -		 * Fetch and process L from TLV.
  36.209 -		 */
  36.210 -		len_len = ber_fetch_length(tlv_constr,
  36.211 -			(const char *)ptr + tag_len, size - tag_len, &tlv_len);
  36.212 -		ASN_DEBUG("Fetching len = %ld", (long)len_len);
  36.213 -		switch(len_len) {
  36.214 -		case -1: RETURN(RC_FAIL);
  36.215 -		case 0: RETURN(RC_WMORE);
  36.216 -		}
  36.217 -
  36.218 -		/*
  36.219 -		 * FIXME
  36.220 -		 * As of today, the chain of tags
  36.221 -		 * must either contain several indefinite length TLVs,
  36.222 -		 * or several definite length ones.
  36.223 -		 * No mixing is allowed.
  36.224 -		 */
  36.225 -		if(tlv_len == -1) {
  36.226 -			/*
  36.227 -			 * Indefinite length.
  36.228 -			 */
  36.229 -			if(limit_len == -1) {
  36.230 -				expect_00_terminators++;
  36.231 -			} else {
  36.232 -				ASN_DEBUG("Unexpected indefinite length "
  36.233 -					"in a chain of definite lengths");
  36.234 -				RETURN(RC_FAIL);
  36.235 -			}
  36.236 -			ADVANCE(tag_len + len_len);
  36.237 -			continue;
  36.238 -		} else {
  36.239 -			if(expect_00_terminators) {
  36.240 -				ASN_DEBUG("Unexpected definite length "
  36.241 -					"in a chain of indefinite lengths");
  36.242 -				RETURN(RC_FAIL);
  36.243 -			}
  36.244 -		}
  36.245 -
  36.246 -		/*
  36.247 -		 * Check that multiple TLVs specify ever decreasing length,
  36.248 -		 * which is consistent.
  36.249 -		 */
  36.250 -		if(limit_len == -1) {
  36.251 -			limit_len    = tlv_len + tag_len + len_len;
  36.252 -			if(limit_len < 0) {
  36.253 -				/* Too great tlv_len value? */
  36.254 -				RETURN(RC_FAIL);
  36.255 -			}
  36.256 -		} else if(limit_len != tlv_len + tag_len + len_len) {
  36.257 -			/*
  36.258 -			 * Inner TLV specifies length which is inconsistent
  36.259 -			 * with the outer TLV's length value.
  36.260 -			 */
  36.261 -			ASN_DEBUG("Outer TLV is %ld and inner is %ld",
  36.262 -				(long)limit_len, (long)tlv_len);
  36.263 -			RETURN(RC_FAIL);
  36.264 -		}
  36.265 -
  36.266 -		ADVANCE(tag_len + len_len);
  36.267 -
  36.268 -		limit_len -= (tag_len + len_len);
  36.269 -		if((ssize_t)size > limit_len) {
  36.270 -			/*
  36.271 -			 * Make sure that we won't consume more bytes
  36.272 -			 * from the parent frame than the inferred limit.
  36.273 -			 */
  36.274 -			size = limit_len;
  36.275 -		}
  36.276 -	}
  36.277 -
  36.278 -	if(opt_tlv_form)
  36.279 -		*opt_tlv_form = tlv_constr;
  36.280 -	if(expect_00_terminators)
  36.281 -		*last_length = -expect_00_terminators;
  36.282 -	else
  36.283 -		*last_length = tlv_len;
  36.284 -
  36.285 -	RETURN(RC_OK);
  36.286 -}
    37.1 --- a/asn.1/ber_decoder.h	Sun May 15 21:23:36 2016 +0200
    37.2 +++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
    37.3 @@ -1,64 +0,0 @@
    37.4 -/*-
    37.5 - * Copyright (c) 2003, 2004 Lev Walkin <vlm@lionet.info>. All rights reserved.
    37.6 - * Redistribution and modifications are permitted subject to BSD license.
    37.7 - */
    37.8 -#ifndef	_BER_DECODER_H_
    37.9 -#define	_BER_DECODER_H_
   37.10 -
   37.11 -#include <asn_application.h>
   37.12 -
   37.13 -#ifdef __cplusplus
   37.14 -extern "C" {
   37.15 -#endif
   37.16 -
   37.17 -struct asn_TYPE_descriptor_s;	/* Forward declaration */
   37.18 -struct asn_codec_ctx_s;		/* Forward declaration */
   37.19 -
   37.20 -/*
   37.21 - * The BER decoder of any type.
   37.22 - * This function may be invoked directly from the application.
   37.23 - * The der_encode() function (der_encoder.h) is an opposite to ber_decode().
   37.24 - */
   37.25 -asn_dec_rval_t ber_decode(struct asn_codec_ctx_s *opt_codec_ctx,
   37.26 -	struct asn_TYPE_descriptor_s *type_descriptor,
   37.27 -	void **struct_ptr,	/* Pointer to a target structure's pointer */
   37.28 -	const void *buffer,	/* Data to be decoded */
   37.29 -	size_t size		/* Size of that buffer */
   37.30 -	);
   37.31 -
   37.32 -/*
   37.33 - * Type of generic function which decodes the byte stream into the structure.
   37.34 - */
   37.35 -typedef asn_dec_rval_t (ber_type_decoder_f)(
   37.36 -		struct asn_codec_ctx_s *opt_codec_ctx,
   37.37 -		struct asn_TYPE_descriptor_s *type_descriptor,
   37.38 -		void **struct_ptr, const void *buf_ptr, size_t size,
   37.39 -		int tag_mode);
   37.40 -
   37.41 -/*******************************
   37.42 - * INTERNALLY USEFUL FUNCTIONS *
   37.43 - *******************************/
   37.44 -
   37.45 -/*
   37.46 - * Check that all tags correspond to the type definition (as given in head).
   37.47 - * On return, last_length would contain either a non-negative length of the
   37.48 - * value part of the last TLV, or the negative number of expected
   37.49 - * "end of content" sequences. The number may only be negative if the
   37.50 - * head->last_tag_form is non-zero.
   37.51 - */
   37.52 -asn_dec_rval_t ber_check_tags(
   37.53 -		struct asn_codec_ctx_s *opt_codec_ctx,	/* codec options */
   37.54 -		struct asn_TYPE_descriptor_s *type_descriptor,
   37.55 -		asn_struct_ctx_t *opt_ctx,	/* saved decoding context */
   37.56 -		const void *ptr, size_t size,
   37.57 -		int tag_mode,		/* {-1,0,1}: IMPLICIT, no, EXPLICIT */
   37.58 -		int last_tag_form,	/* {-1,0:1}: any, primitive, constr */
   37.59 -		ber_tlv_len_t *last_length,
   37.60 -		int *opt_tlv_form	/* optional tag form */
   37.61 -	);
   37.62 -
   37.63 -#ifdef __cplusplus
   37.64 -}
   37.65 -#endif
   37.66 -
   37.67 -#endif	/* _BER_DECODER_H_ */
    38.1 --- a/asn.1/ber_tlv_length.c	Sun May 15 21:23:36 2016 +0200
    38.2 +++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
    38.3 @@ -1,178 +0,0 @@
    38.4 -/*-
    38.5 - * Copyright (c) 2003, 2004 Lev Walkin <vlm@lionet.info>. All rights reserved.
    38.6 - * Redistribution and modifications are permitted subject to BSD license.
    38.7 - */
    38.8 -#include <asn_internal.h>
    38.9 -#include <ber_tlv_length.h>
   38.10 -#include <ber_tlv_tag.h>
   38.11 -
   38.12 -ssize_t
   38.13 -ber_fetch_length(int _is_constructed, const void *bufptr, size_t size,
   38.14 -		ber_tlv_len_t *len_r) {
   38.15 -	const uint8_t *buf = (const uint8_t *)bufptr;
   38.16 -	unsigned oct;
   38.17 -
   38.18 -	if(size == 0)
   38.19 -		return 0;	/* Want more */
   38.20 -
   38.21 -	oct = *(const uint8_t *)buf;
   38.22 -	if((oct & 0x80) == 0) {
   38.23 -		/*
   38.24 -		 * Short definite length.
   38.25 -		 */
   38.26 -		*len_r = oct;	/* & 0x7F */
   38.27 -		return 1;
   38.28 -	} else {
   38.29 -		ber_tlv_len_t len;
   38.30 -		size_t skipped;
   38.31 -
   38.32 -		if(_is_constructed && oct == 0x80) {
   38.33 -			*len_r = -1;	/* Indefinite length */
   38.34 -			return 1;
   38.35 -		}
   38.36 -
   38.37 -		if(oct == 0xff) {
   38.38 -			/* Reserved in standard for future use. */
   38.39 -			return -1;
   38.40 -		}
   38.41 -
   38.42 -		oct &= 0x7F;	/* Leave only the 7 LS bits */
   38.43 -		for(len = 0, buf++, skipped = 1;
   38.44 -			oct && (++skipped <= size); buf++, oct--) {
   38.45 -
   38.46 -			len = (len << 8) | *buf;
   38.47 -			if(len < 0
   38.48 -			|| (len >> ((8 * sizeof(len)) - 8) && oct > 1)) {
   38.49 -				/*
   38.50 -				 * Too large length value.
   38.51 -				 */
   38.52 -				return -1;
   38.53 -			}
   38.54 -		}
   38.55 -
   38.56 -		if(oct == 0) {
   38.57 -			ber_tlv_len_t lenplusepsilon = (size_t)len + 1024;
   38.58 -			/*
   38.59 -			 * Here length may be very close or equal to 2G.
   38.60 -			 * However, the arithmetics used in some decoders
   38.61 -			 * may add some (small) quantities to the length,
   38.62 -			 * to check the resulting value against some limits.
   38.63 -			 * This may result in integer wrap-around, which
   38.64 -			 * we try to avoid by checking it earlier here.
   38.65 -			 */
   38.66 -			if(lenplusepsilon < 0) {
   38.67 -				/* Too large length value */
   38.68 -				return -1;
   38.69 -			}
   38.70 -
   38.71 -			*len_r = len;
   38.72 -			return skipped;
   38.73 -		}
   38.74 -
   38.75 -		return 0;	/* Want more */
   38.76 -	}
   38.77 -
   38.78 -}
   38.79 -
   38.80 -ssize_t
   38.81 -ber_skip_length(asn_codec_ctx_t *opt_codec_ctx,
   38.82 -		int _is_constructed, const void *ptr, size_t size) {
   38.83 -	ber_tlv_len_t vlen;	/* Length of V in TLV */
   38.84 -	ssize_t tl;		/* Length of L in TLV */
   38.85 -	ssize_t ll;		/* Length of L in TLV */
   38.86 -	size_t skip;
   38.87 -
   38.88 -	/*
   38.89 -	 * Make sure we didn't exceed the maximum stack size.
   38.90 -	 */
   38.91 -	if(_ASN_STACK_OVERFLOW_CHECK(opt_codec_ctx))
   38.92 -		return -1;
   38.93 -
   38.94 -	/*
   38.95 -	 * Determine the size of L in TLV.
   38.96 -	 */
   38.97 -	ll = ber_fetch_length(_is_constructed, ptr, size, &vlen);
   38.98 -	if(ll <= 0) return ll;
   38.99 -
  38.100 -	/*
  38.101 -	 * Definite length.
  38.102 -	 */
  38.103 -	if(vlen >= 0) {
  38.104 -		skip = ll + vlen;
  38.105 -		if(skip > size)
  38.106 -			return 0;	/* Want more */
  38.107 -		return skip;
  38.108 -	}
  38.109 -
  38.110 -	/*
  38.111 -	 * Indefinite length!
  38.112 -	 */
  38.113 -	ASN_DEBUG("Skipping indefinite length");
  38.114 -	for(skip = ll, ptr = ((const char *)ptr) + ll, size -= ll;;) {
  38.115 -		ber_tlv_tag_t tag;
  38.116 -
  38.117 -		/* Fetch the tag */
  38.118 -		tl = ber_fetch_tag(ptr, size, &tag);
  38.119 -		if(tl <= 0) return tl;
  38.120 -
  38.121 -		ll = ber_skip_length(opt_codec_ctx,
  38.122 -			BER_TLV_CONSTRUCTED(ptr),
  38.123 -			((const char *)ptr) + tl, size - tl);
  38.124 -		if(ll <= 0) return ll;
  38.125 -
  38.126 -		skip += tl + ll;
  38.127 -
  38.128 -		/*
  38.129 -		 * This may be the end of the indefinite length structure,
  38.130 -		 * two consecutive 0 octets.
  38.131 -		 * Check if it is true.
  38.132 -		 */
  38.133 -		if(((const uint8_t *)ptr)[0] == 0
  38.134 -		&& ((const uint8_t *)ptr)[1] == 0)
  38.135 -			return skip;
  38.136 -
  38.137 -		ptr = ((const char *)ptr) + tl + ll;
  38.138 -		size -= tl + ll;
  38.139 - 	}
  38.140 -
  38.141 -	/* UNREACHABLE */
  38.142 -}
  38.143 -
  38.144 -size_t
  38.145 -der_tlv_length_serialize(ber_tlv_len_t len, void *bufp, size_t size) {
  38.146 -	size_t required_size;	/* Size of len encoding */
  38.147 -	uint8_t *buf = (uint8_t *)bufp;
  38.148 -	uint8_t *end;
  38.149 -	size_t i;
  38.150 -
  38.151 -	if(len <= 127) {
  38.152 -		/* Encoded in 1 octet */
  38.153 -		if(size) *buf = (uint8_t)len;
  38.154 -		return 1;
  38.155 -	}
  38.156 -
  38.157 -	/*
  38.158 -	 * Compute the size of the subsequent bytes.
  38.159 -	 */
  38.160 -	for(required_size = 1, i = 8; i < 8 * sizeof(len); i += 8) {
  38.161 -		if(len >> i)
  38.162 -			required_size++;
  38.163 -		else
  38.164 -			break;
  38.165 -	}
  38.166 -
  38.167 -	if(size <= required_size)
  38.168 -		return required_size + 1;
  38.169 -
  38.170 -	*buf++ = (uint8_t)(0x80 | required_size);  /* Length of the encoding */
  38.171 -
  38.172 -	/*
  38.173 -	 * Produce the len encoding, space permitting.
  38.174 -	 */
  38.175 -	end = buf + required_size;
  38.176 -	for(i -= 8; buf < end; i -= 8, buf++)
  38.177 -		*buf = (uint8_t)(len >> i);
  38.178 -
  38.179 -	return required_size + 1;
  38.180 -}
  38.181 -
    39.1 --- a/asn.1/ber_tlv_length.h	Sun May 15 21:23:36 2016 +0200
    39.2 +++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
    39.3 @@ -1,50 +0,0 @@
    39.4 -/*-
    39.5 - * Copyright (c) 2003 Lev Walkin <vlm@lionet.info>. All rights reserved.
    39.6 - * Redistribution and modifications are permitted subject to BSD license.
    39.7 - */
    39.8 -#ifndef	_BER_TLV_LENGTH_H_
    39.9 -#define	_BER_TLV_LENGTH_H_
   39.10 -
   39.11 -#ifdef __cplusplus
   39.12 -extern "C" {
   39.13 -#endif
   39.14 -
   39.15 -typedef ssize_t ber_tlv_len_t;
   39.16 -
   39.17 -/*
   39.18 - * This function tries to fetch the length of the BER TLV value and place it
   39.19 - * in *len_r.
   39.20 - * RETURN VALUES:
   39.21 - *	 0:	More data expected than bufptr contains.
   39.22 - *	-1:	Fatal error deciphering length.
   39.23 - *	>0:	Number of bytes used from bufptr.
   39.24 - * On return with >0, len_r is constrained as -1..MAX, where -1 mean
   39.25 - * that the value is of indefinite length.
   39.26 - */
   39.27 -ssize_t ber_fetch_length(int _is_constructed, const void *bufptr, size_t size,
   39.28 -	ber_tlv_len_t *len_r);
   39.29 -
   39.30 -/*
   39.31 - * This function expects bufptr to be positioned over L in TLV.
   39.32 - * It returns number of bytes occupied by L and V together, suitable
   39.33 - * for skipping. The function properly handles indefinite length.
   39.34 - * RETURN VALUES:
   39.35 - * 	Standard {-1,0,>0} convention.
   39.36 - */
   39.37 -ssize_t ber_skip_length(
   39.38 -	struct asn_codec_ctx_s *opt_codec_ctx,	/* optional context */
   39.39 -	int _is_constructed, const void *bufptr, size_t size);
   39.40 -
   39.41 -/*
   39.42 - * This function serializes the length (L from TLV) in DER format.
   39.43 - * It always returns number of bytes necessary to represent the length,
   39.44 - * it is a caller's responsibility to check the return value
   39.45 - * against the supplied buffer's size.
   39.46 - */
   39.47 -size_t der_tlv_length_serialize(ber_tlv_len_t len, void *bufptr, size_t size);
   39.48 -
   39.49 -#ifdef __cplusplus
   39.50 -}
   39.51 -#endif
   39.52 -
   39.53 -#endif	/* _BER_TLV_LENGTH_H_ */
    40.1 --- a/asn.1/ber_tlv_tag.c	Sun May 15 21:23:36 2016 +0200
    40.2 +++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
    40.3 @@ -1,144 +0,0 @@
    40.4 -/*-
    40.5 - * Copyright (c) 2003, 2004 Lev Walkin <vlm@lionet.info>. All rights reserved.
    40.6 - * Redistribution and modifications are permitted subject to BSD license.
    40.7 - */
    40.8 -#include <asn_internal.h>
    40.9 -#include <ber_tlv_tag.h>
   40.10 -#include <errno.h>
   40.11 -
   40.12 -ssize_t
   40.13 -ber_fetch_tag(const void *ptr, size_t size, ber_tlv_tag_t *tag_r) {
   40.14 -	ber_tlv_tag_t val;
   40.15 -	ber_tlv_tag_t tclass;
   40.16 -	size_t skipped;
   40.17 -
   40.18 -	if(size == 0)
   40.19 -		return 0;
   40.20 -
   40.21 -	val = *(const uint8_t *)ptr;
   40.22 -	tclass = (val >> 6);
   40.23 -	if((val &= 0x1F) != 0x1F) {
   40.24 -		/*
   40.25 -		 * Simple form: everything encoded in a single octet.
   40.26 -		 * Tag Class is encoded using two least significant bits.
   40.27 -		 */
   40.28 -		*tag_r = (val << 2) | tclass;
   40.29 -		return 1;
   40.30 -	}
   40.31 -
   40.32 -	/*
   40.33 -	 * Each octet contains 7 bits of useful information.
   40.34 -	 * The MSB is 0 if it is the last octet of the tag.
   40.35 -	 */
   40.36 -	for(val = 0, ptr = ((const char *)ptr) + 1, skipped = 2;
   40.37 -			skipped <= size;
   40.38 -				ptr = ((const char *)ptr) + 1, skipped++) {
   40.39 -		unsigned int oct = *(const uint8_t *)ptr;
   40.40 -		if(oct & 0x80) {
   40.41 -			val = (val << 7) | (oct & 0x7F);
   40.42 -			/*
   40.43 -			 * Make sure there are at least 9 bits spare
   40.44 -			 * at the MS side of a value.
   40.45 -			 */
   40.46 -			if(val >> ((8 * sizeof(val)) - 9)) {
   40.47 -				/*
   40.48 -				 * We would not be able to accomodate
   40.49 -				 * any more tag bits.
   40.50 -				 */
   40.51 -				return -1;
   40.52 -			}
   40.53 -		} else {
   40.54 -			val = (val << 7) | oct;
   40.55 -			*tag_r = (val << 2) | tclass;
   40.56 -			return skipped;
   40.57 -		}
   40.58 -	}
   40.59 -
   40.60 -	return 0;	/* Want more */
   40.61 -}
   40.62 -
   40.63 -
   40.64 -ssize_t
   40.65 -ber_tlv_tag_fwrite(ber_tlv_tag_t tag, FILE *f) {
   40.66 -	char buf[sizeof("[APPLICATION ]") + 32];
   40.67 -	ssize_t ret;
   40.68 -
   40.69 -	ret = ber_tlv_tag_snprint(tag, buf, sizeof(buf));
   40.70 -	if(ret >= (ssize_t)sizeof(buf) || ret < 2) {
   40.71 -		errno = EPERM;
   40.72 -		return -1;
   40.73 -	}
   40.74 -
   40.75 -	return fwrite(buf, 1, ret, f);
   40.76 -}
   40.77 -
   40.78 -ssize_t
   40.79 -ber_tlv_tag_snprint(ber_tlv_tag_t tag, char *buf, size_t size) {
   40.80 -	char *type = 0;
   40.81 -	int ret;
   40.82 -
   40.83 -	switch(tag & 0x3) {
   40.84 -	case ASN_TAG_CLASS_UNIVERSAL:	type = "UNIVERSAL ";	break;
   40.85 -	case ASN_TAG_CLASS_APPLICATION:	type = "APPLICATION ";	break;
   40.86 -	case ASN_TAG_CLASS_CONTEXT:	type = "";		break;
   40.87 -	case ASN_TAG_CLASS_PRIVATE:	type = "PRIVATE ";	break;
   40.88 -	}
   40.89 -
   40.90 -	ret = snprintf(buf, size, "[%s%u]", type, ((unsigned)tag) >> 2);
   40.91 -	if(ret <= 0 && size) buf[0] = '\0';	/* against broken libc's */
   40.92 -
   40.93 -	return ret;
   40.94 -}
   40.95 -
   40.96 -char *
   40.97 -ber_tlv_tag_string(ber_tlv_tag_t tag) {
   40.98 -	static char buf[sizeof("[APPLICATION ]") + 32];
   40.99 -
  40.100 -	(void)ber_tlv_tag_snprint(tag, buf, sizeof(buf));
  40.101 -
  40.102 -	return buf;
  40.103 -}
  40.104 -
  40.105 -
  40.106 -size_t
  40.107 -ber_tlv_tag_serialize(ber_tlv_tag_t tag, void *bufp, size_t size) {
  40.108 -	int tclass = BER_TAG_CLASS(tag);
  40.109 -	ber_tlv_tag_t tval = BER_TAG_VALUE(tag);
  40.110 -	uint8_t *buf = (uint8_t *)bufp;
  40.111 -	uint8_t *end;
  40.112 -	size_t required_size;
  40.113 -	size_t i;
  40.114 -
  40.115 -	if(tval <= 30) {
  40.116 -		/* Encoded in 1 octet */
  40.117 -		if(size) buf[0] = (tclass << 6) | tval;
  40.118 -		return 1;
  40.119 -	} else if(size) {
  40.120 -		*buf++ = (tclass << 6) | 0x1F;
  40.121 -		size--;
  40.122 -	}
  40.123 -
  40.124 -	/*
  40.125 -	 * Compute the size of the subsequent bytes.
  40.126 -	 */
  40.127 -	for(required_size = 1, i = 7; i < 8 * sizeof(tval); i += 7) {
  40.128 -		if(tval >> i)
  40.129 -			required_size++;
  40.130 -		else
  40.131 -			break;
  40.132 -	}
  40.133 -
  40.134 -	if(size < required_size)
  40.135 -		return required_size + 1;
  40.136 -
  40.137 -	/*
  40.138 -	 * Fill in the buffer, space permitting.
  40.139 -	 */
  40.140 -	end = buf + required_size - 1;
  40.141 -	for(i -= 7; buf < end; i -= 7, buf++)
  40.142 -		*buf = 0x80 | ((tval >> i) & 0x7F);
  40.143 -	*buf = (tval & 0x7F);	/* Last octet without high bit */
  40.144 -
  40.145 -	return required_size + 1;
  40.146 -}
  40.147 -
    41.1 --- a/asn.1/ber_tlv_tag.h	Sun May 15 21:23:36 2016 +0200
    41.2 +++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
    41.3 @@ -1,60 +0,0 @@
    41.4 -/*-
    41.5 - * Copyright (c) 2003, 2004 Lev Walkin <vlm@lionet.info>. All rights reserved.
    41.6 - * Redistribution and modifications are permitted subject to BSD license.
    41.7 - */
    41.8 -#ifndef	_BER_TLV_TAG_H_
    41.9 -#define	_BER_TLV_TAG_H_
   41.10 -
   41.11 -#ifdef __cplusplus
   41.12 -extern "C" {
   41.13 -#endif
   41.14 -
   41.15 -enum asn_tag_class {
   41.16 -	ASN_TAG_CLASS_UNIVERSAL		= 0,	/* 0b00 */
   41.17 -	ASN_TAG_CLASS_APPLICATION	= 1,	/* 0b01 */
   41.18 -	ASN_TAG_CLASS_CONTEXT		= 2,	/* 0b10 */
   41.19 -	ASN_TAG_CLASS_PRIVATE		= 3	/* 0b11 */
   41.20 -};
   41.21 -typedef unsigned ber_tlv_tag_t;	/* BER TAG from Tag-Length-Value */
   41.22 -
   41.23 -/*
   41.24 - * Tag class is encoded together with tag value for optimization purposes.
   41.25 - */
   41.26 -#define	BER_TAG_CLASS(tag)	((tag) & 0x3)
   41.27 -#define	BER_TAG_VALUE(tag)	((tag) >> 2)
   41.28 -#define	BER_TLV_CONSTRUCTED(tagptr)	(((*(const uint8_t *)tagptr)&0x20)?1:0)
   41.29 -
   41.30 -#define	BER_TAGS_EQUAL(tag1, tag2)	((tag1) == (tag2))
   41.31 -
   41.32 -/*
   41.33 - * Several functions for printing the TAG in the canonical form
   41.34 - * (i.e. "[PRIVATE 0]").
   41.35 - * Return values correspond to their libc counterparts (if any).
   41.36 - */
   41.37 -ssize_t ber_tlv_tag_snprint(ber_tlv_tag_t tag, char *buf, size_t buflen);
   41.38 -ssize_t ber_tlv_tag_fwrite(ber_tlv_tag_t tag, FILE *);
   41.39 -char *ber_tlv_tag_string(ber_tlv_tag_t tag);
   41.40 -
   41.41 -
   41.42 -/*
   41.43 - * This function tries to fetch the tag from the input stream.
   41.44 - * RETURN VALUES:
   41.45 - * 	 0:	More data expected than bufptr contains.
   41.46 - * 	-1:	Fatal error deciphering tag.
   41.47 - *	>0:	Number of bytes used from bufptr. tag_r will contain the tag.
   41.48 - */
   41.49 -ssize_t ber_fetch_tag(const void *bufptr, size_t size, ber_tlv_tag_t *tag_r);
   41.50 -
   41.51 -/*
   41.52 - * This function serializes the tag (T from TLV) in BER format.
   41.53 - * It always returns number of bytes necessary to represent the tag,
   41.54 - * it is a caller's responsibility to check the return value
   41.55 - * against the supplied buffer's size.
   41.56 - */
   41.57 -size_t ber_tlv_tag_serialize(ber_tlv_tag_t tag, void *bufptr, size_t size);
   41.58 -
   41.59 -#ifdef __cplusplus
   41.60 -}
   41.61 -#endif
   41.62 -
   41.63 -#endif	/* _BER_TLV_TAG_H_ */
    42.1 --- a/asn.1/constr_SEQUENCE.c	Sun May 15 21:23:36 2016 +0200
    42.2 +++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
    42.3 @@ -1,1422 +0,0 @@
    42.4 -/*-
    42.5 - * Copyright (c) 2003, 2004, 2005, 2006, 2007 Lev Walkin <vlm@lionet.info>.
    42.6 - * All rights reserved.
    42.7 - * Redistribution and modifications are permitted subject to BSD license.
    42.8 - */
    42.9 -#include <asn_internal.h>
   42.10 -#include <constr_SEQUENCE.h>
   42.11 -#include <per_opentype.h>
   42.12 -
   42.13 -/*
   42.14 - * Number of bytes left for this structure.
   42.15 - * (ctx->left) indicates the number of bytes _transferred_ for the structure.
   42.16 - * (size) contains the number of bytes in the buffer passed.
   42.17 - */
   42.18 -#define	LEFT	((size<(size_t)ctx->left)?size:(size_t)ctx->left)
   42.19 -
   42.20 -/*
   42.21 - * If the subprocessor function returns with an indication that it wants
   42.22 - * more data, it may well be a fatal decoding problem, because the
   42.23 - * size is constrained by the <TLV>'s L, even if the buffer size allows
   42.24 - * reading more data.
   42.25 - * For example, consider the buffer containing the following TLVs:
   42.26 - * <T:5><L:1><V> <T:6>...
   42.27 - * The TLV length clearly indicates that one byte is expected in V, but
   42.28 - * if the V processor returns with "want more data" even if the buffer
   42.29 - * contains way more data than the V processor have seen.
   42.30 - */
   42.31 -#define	SIZE_VIOLATION	(ctx->left >= 0 && (size_t)ctx->left <= size)
   42.32 -
   42.33 -/*
   42.34 - * This macro "eats" the part of the buffer which is definitely "consumed",
   42.35 - * i.e. was correctly converted into local representation or rightfully skipped.
   42.36 - */
   42.37 -#undef	ADVANCE
   42.38 -#define	ADVANCE(num_bytes)	do {		\
   42.39 -		size_t num = num_bytes;		\
   42.40 -		ptr = ((const char *)ptr) + num; \
   42.41 -		size -= num;			\
   42.42 -		if(ctx->left >= 0)		\
   42.43 -			ctx->left -= num;	\
   42.44 -		consumed_myself += num;		\
   42.45 -	} while(0)
   42.46 -
   42.47 -/*
   42.48 - * Switch to the next phase of parsing.
   42.49 - */
   42.50 -#undef	NEXT_PHASE
   42.51 -#undef	PHASE_OUT
   42.52 -#define	NEXT_PHASE(ctx)	do {			\
   42.53 -		ctx->phase++;			\
   42.54 -		ctx->step = 0;			\
   42.55 -	} while(0)
   42.56 -#define	PHASE_OUT(ctx)	do { ctx->phase = 10; } while(0)
   42.57 -
   42.58 -/*
   42.59 - * Return a standardized complex structure.
   42.60 - */
   42.61 -#undef	RETURN
   42.62 -#define	RETURN(_code)	do {			\
   42.63 -		rval.code = _code;		\
   42.64 -		rval.consumed = consumed_myself;\
   42.65 -		return rval;			\
   42.66 -	} while(0)
   42.67 -
   42.68 -/*
   42.69 - * Check whether we are inside the extensions group.
   42.70 - */
   42.71 -#define	IN_EXTENSION_GROUP(specs, memb_idx)	\
   42.72 -	( ((memb_idx) > (specs)->ext_after)	\
   42.73 -	&&((memb_idx) < (specs)->ext_before))
   42.74 -
   42.75 -
   42.76 -/*
   42.77 - * Tags are canonically sorted in the tag2element map.
   42.78 - */
   42.79 -static int
   42.80 -_t2e_cmp(const void *ap, const void *bp) {
   42.81 -	const asn_TYPE_tag2member_t *a = (const asn_TYPE_tag2member_t *)ap;
   42.82 -	const asn_TYPE_tag2member_t *b = (const asn_TYPE_tag2member_t *)bp;
   42.83 -
   42.84 -	int a_class = BER_TAG_CLASS(a->el_tag);
   42.85 -	int b_class = BER_TAG_CLASS(b->el_tag);
   42.86 -
   42.87 -	if(a_class == b_class) {
   42.88 -		ber_tlv_tag_t a_value = BER_TAG_VALUE(a->el_tag);
   42.89 -		ber_tlv_tag_t b_value = BER_TAG_VALUE(b->el_tag);
   42.90 -
   42.91 -		if(a_value == b_value) {
   42.92 -			if(a->el_no > b->el_no)
   42.93 -				return 1;
   42.94 -			/*
   42.95 -			 * Important: we do not check
   42.96 -			 * for a->el_no <= b->el_no!
   42.97 -			 */
   42.98 -			return 0;
   42.99 -		} else if(a_value < b_value)
  42.100 -			return -1;
  42.101 -		else
  42.102 -			return 1;
  42.103 -	} else if(a_class < b_class) {
  42.104 -		return -1;
  42.105 -	} else {
  42.106 -		return 1;
  42.107 -	}
  42.108 -}
  42.109 -
  42.110 -
  42.111 -/*
  42.112 - * The decoder of the SEQUENCE type.
  42.113 - */
  42.114 -asn_dec_rval_t
  42.115 -SEQUENCE_decode_ber(asn_codec_ctx_t *opt_codec_ctx, asn_TYPE_descriptor_t *td,
  42.116 -	void **struct_ptr, const void *ptr, size_t size, int tag_mode) {
  42.117 -	/*
  42.118 -	 * Bring closer parts of structure description.
  42.119 -	 */
  42.120 -	asn_SEQUENCE_specifics_t *specs = (asn_SEQUENCE_specifics_t *)td->specifics;
  42.121 -	asn_TYPE_member_t *elements = td->elements;
  42.122 -
  42.123 -	/*
  42.124 -	 * Parts of the structure being constructed.
  42.125 -	 */
  42.126 -	void *st = *struct_ptr;	/* Target structure. */
  42.127 -	asn_struct_ctx_t *ctx;	/* Decoder context */
  42.128 -
  42.129 -	ber_tlv_tag_t tlv_tag;	/* T from TLV */
  42.130 -	asn_dec_rval_t rval;	/* Return code from subparsers */
  42.131 -
  42.132 -	ssize_t consumed_myself = 0;	/* Consumed bytes from ptr */
  42.133 -	int edx;			/* SEQUENCE element's index */
  42.134 -
  42.135 -	ASN_DEBUG("Decoding %s as SEQUENCE", td->name);
  42.136 -	
  42.137 -	/*
  42.138 -	 * Create the target structure if it is not present already.
  42.139 -	 */
  42.140 -	if(st == 0) {
  42.141 -		st = *struct_ptr = CALLOC(1, specs->struct_size);
  42.142 -		if(st == 0) {
  42.143 -			RETURN(RC_FAIL);
  42.144 -		}
  42.145 -	}
  42.146 -
  42.147 -	/*
  42.148 -	 * Restore parsing context.
  42.149 -	 */
  42.150 -	ctx = (asn_struct_ctx_t *)((char *)st + specs->ctx_offset);
  42.151 -	
  42.152 -	/*
  42.153 -	 * Start to parse where left previously
  42.154 -	 */
  42.155 -	switch(ctx->phase) {
  42.156 -	case 0:
  42.157 -		/*
  42.158 -		 * PHASE 0.
  42.159 -		 * Check that the set of tags associated with given structure
  42.160 -		 * perfectly fits our expectations.
  42.161 -		 */
  42.162 -
  42.163 -		rval = ber_check_tags(opt_codec_ctx, td, ctx, ptr, size,
  42.164 -			tag_mode, 1, &ctx->left, 0);
  42.165 -		if(rval.code != RC_OK) {
  42.166 -			ASN_DEBUG("%s tagging check failed: %d",
  42.167 -				td->name, rval.code);
  42.168 -			return rval;
  42.169 -		}
  42.170 -
  42.171 -		if(ctx->left >= 0)
  42.172 -			ctx->left += rval.consumed; /* ?Substracted below! */
  42.173 -		ADVANCE(rval.consumed);
  42.174 -
  42.175 -		NEXT_PHASE(ctx);
  42.176 -
  42.177 -		ASN_DEBUG("Structure consumes %ld bytes, buffer %ld",
  42.178 -			(long)ctx->left, (long)size);
  42.179 -
  42.180 -		/* Fall through */
  42.181 -	case 1:
  42.182 -		/*
  42.183 -		 * PHASE 1.
  42.184 -		 * From the place where we've left it previously,
  42.185 -		 * try to decode the next member from the list of
  42.186 -		 * this structure's elements.
  42.187 -		 * (ctx->step) stores the member being processed
  42.188 -		 * between invocations and the microphase {0,1} of parsing
  42.189 -		 * that member:
  42.190 -		 * 	step = (<member_number> * 2 + <microphase>).
  42.191 -		 */
  42.192 -	  for(edx = (ctx->step >> 1); edx < td->elements_count;
  42.193 -			edx++, ctx->step = (ctx->step & ~1) + 2) {
  42.194 -		void *memb_ptr;		/* Pointer to the member */
  42.195 -		void **memb_ptr2;	/* Pointer to that pointer */
  42.196 -		ssize_t tag_len;	/* Length of TLV's T */
  42.197 -		int opt_edx_end;	/* Next non-optional element */
  42.198 -		int use_bsearch;
  42.199 -		int n;
  42.200 -
  42.201 -		if(ctx->step & 1)
  42.202 -			goto microphase2;
  42.203 -
  42.204 -		/*
  42.205 -		 * MICROPHASE 1: Synchronize decoding.
  42.206 -		 */
  42.207 -		ASN_DEBUG("In %s SEQUENCE left %d, edx=%d flags=%d"
  42.208 -				" opt=%d ec=%d",
  42.209 -			td->name, (int)ctx->left, edx,
  42.210 -			elements[edx].flags, elements[edx].optional,
  42.211 -			td->elements_count);
  42.212 -
  42.213 -		if(ctx->left == 0	/* No more stuff is expected */
  42.214 -		&& (
  42.215 -			/* Explicit OPTIONAL specification reaches the end */
  42.216 -			(edx + elements[edx].optional
  42.217 -					== td->elements_count)
  42.218 -			||
  42.219 -			/* All extensions are optional */
  42.220 -			(IN_EXTENSION_GROUP(specs, edx)
  42.221 -				&& specs->ext_before > td->elements_count)
  42.222 -		   )
  42.223 -		) {
  42.224 -			ASN_DEBUG("End of SEQUENCE %s", td->name);
  42.225 -			/*
  42.226 -			 * Found the legitimate end of the structure.
  42.227 -			 */
  42.228 -			PHASE_OUT(ctx);
  42.229 -			RETURN(RC_OK);
  42.230 -		}
  42.231 -
  42.232 -		/*
  42.233 -		 * Fetch the T from TLV.
  42.234 -		 */
  42.235 -		tag_len = ber_fetch_tag(ptr, LEFT, &tlv_tag);
  42.236 -		ASN_DEBUG("Current tag in %s SEQUENCE for element %d "
  42.237 -			"(%s) is %s encoded in %d bytes, of frame %ld",
  42.238 -			td->name, edx, elements[edx].name,
  42.239 -			ber_tlv_tag_string(tlv_tag), (int)tag_len, (long)LEFT);
  42.240 -		switch(tag_len) {
  42.241 -		case 0: if(!SIZE_VIOLATION) RETURN(RC_WMORE);
  42.242 -			/* Fall through */
  42.243 -		case -1: RETURN(RC_FAIL);
  42.244 -		}
  42.245 -
  42.246 -		if(ctx->left < 0 && ((const uint8_t *)ptr)[0] == 0) {
  42.247 -			if(LEFT < 2) {
  42.248 -				if(SIZE_VIOLATION)
  42.249 -					RETURN(RC_FAIL);
  42.250 -				else
  42.251 -					RETURN(RC_WMORE);
  42.252 -			} else if(((const uint8_t *)ptr)[1] == 0) {
  42.253 -			ASN_DEBUG("edx = %d, opt = %d, ec=%d",
  42.254 -				edx, elements[edx].optional,
  42.255 -				td->elements_count);
  42.256 -				if((edx + elements[edx].optional
  42.257 -					== td->elements_count)
  42.258 -				|| (IN_EXTENSION_GROUP(specs, edx)
  42.259 -					&& specs->ext_before
  42.260 -						> td->elements_count)) {
  42.261 -					/*
  42.262 -					 * Yeah, baby! Found the terminator
  42.263 -					 * of the indefinite length structure.
  42.264 -					 */
  42.265 -					/*
  42.266 -					 * Proceed to the canonical
  42.267 -					 * finalization function.
  42.268 -					 * No advancing is necessary.
  42.269 -					 */
  42.270 -					goto phase3;
  42.271 -				}
  42.272 -			}
  42.273 -		}
  42.274 -
  42.275 -		/*
  42.276 -		 * Find the next available type with this tag.
  42.277 -		 */
  42.278 -		use_bsearch = 0;
  42.279 -		opt_edx_end = edx + elements[edx].optional + 1;
  42.280 -		if(opt_edx_end > td->elements_count)
  42.281 -			opt_edx_end = td->elements_count;	/* Cap */
  42.282 -		else if(opt_edx_end - edx > 8) {
  42.283 -			/* Limit the scope of linear search... */
  42.284 -			opt_edx_end = edx + 8;
  42.285 -			use_bsearch = 1;
  42.286 -			/* ... and resort to bsearch() */
  42.287 -		}
  42.288 -		for(n = edx; n < opt_edx_end; n++) {
  42.289 -			if(BER_TAGS_EQUAL(tlv_tag, elements[n].tag)) {
  42.290 -				/*
  42.291 -				 * Found element corresponding to the tag
  42.292 -				 * being looked at.
  42.293 -				 * Reposition over the right element.
  42.294 -				 */
  42.295 -				edx = n;
  42.296 -				ctx->step = 1 + 2 * edx;	/* Remember! */
  42.297 -				goto microphase2;
  42.298 -			} else if(elements[n].flags & ATF_OPEN_TYPE) {
  42.299 -				/*
  42.300 -				 * This is the ANY type, which may bear
  42.301 -				 * any flag whatsoever.
  42.302 -				 */
  42.303 -				edx = n;
  42.304 -				ctx->step = 1 + 2 * edx;	/* Remember! */
  42.305 -				goto microphase2;
  42.306 -			} else if(elements[n].tag == (ber_tlv_tag_t)-1) {
  42.307 -				use_bsearch = 1;
  42.308 -				break;
  42.309 -			}
  42.310 -		}
  42.311 -		if(use_bsearch) {
  42.312 -			/*
  42.313 -			 * Resort to a binary search over
  42.314 -			 * sorted array of tags.
  42.315 -			 */
  42.316 -			asn_TYPE_tag2member_t *t2m;
  42.317 -			asn_TYPE_tag2member_t key;
  42.318 -			key.el_tag = tlv_tag;
  42.319 -			key.el_no = edx;
  42.320 -			t2m = (asn_TYPE_tag2member_t *)bsearch(&key,
  42.321 -				specs->tag2el, specs->tag2el_count,
  42.322 -				sizeof(specs->tag2el[0]), _t2e_cmp);
  42.323 -			if(t2m) {
  42.324 -				asn_TYPE_tag2member_t *best = 0;
  42.325 -				asn_TYPE_tag2member_t *t2m_f, *t2m_l;
  42.326 -				int edx_max = edx + elements[edx].optional;
  42.327 -				/*
  42.328 -				 * Rewind to the first element with that tag,
  42.329 -				 * `cause bsearch() does not guarantee order.
  42.330 -				 */
  42.331 -				t2m_f = t2m + t2m->toff_first;
  42.332 -				t2m_l = t2m + t2m->toff_last;
  42.333 -				for(t2m = t2m_f; t2m <= t2m_l; t2m++) {
  42.334 -					if(t2m->el_no > edx_max) break;
  42.335 -					if(t2m->el_no < edx) continue;
  42.336 -					best = t2m;
  42.337 -				}
  42.338 -				if(best) {
  42.339 -					edx = best->el_no;
  42.340 -					ctx->step = 1 + 2 * edx;
  42.341 -					goto microphase2;
  42.342 -				}
  42.343 -			}
  42.344 -			n = opt_edx_end;
  42.345 -		}
  42.346 -		if(n == opt_edx_end) {
  42.347 -			/*
  42.348 -			 * If tag is unknown, it may be either
  42.349 -			 * an unknown (thus, incorrect) tag,
  42.350 -			 * or an extension (...),
  42.351 -			 * or an end of the indefinite-length structure.
  42.352 -			 */
  42.353 -			if(!IN_EXTENSION_GROUP(specs,
  42.354 -				edx + elements[edx].optional)) {
  42.355 -				ASN_DEBUG("Unexpected tag %s (at %d)",
  42.356 -					ber_tlv_tag_string(tlv_tag), edx);
  42.357 -				ASN_DEBUG("Expected tag %s (%s)%s",
  42.358 -					ber_tlv_tag_string(elements[edx].tag),
  42.359 -					elements[edx].name,
  42.360 -					elements[edx].optional
  42.361 -						?" or alternatives":"");
  42.362 -				RETURN(RC_FAIL);
  42.363 -			} else {
  42.364 -				/* Skip this tag */
  42.365 -				ssize_t skip;
  42.366 -				edx += elements[edx].optional;
  42.367 -
  42.368 -				ASN_DEBUG("Skipping unexpected %s (at %d)",
  42.369 -					ber_tlv_tag_string(tlv_tag), edx);
  42.370 -				skip = ber_skip_length(opt_codec_ctx,
  42.371 -					BER_TLV_CONSTRUCTED(ptr),
  42.372 -					(const char *)ptr + tag_len,
  42.373 -					LEFT - tag_len);
  42.374 -				ASN_DEBUG("Skip length %d in %s",
  42.375 -					(int)skip, td->name);
  42.376 -				switch(skip) {
  42.377 -				case 0: if(!SIZE_VIOLATION) RETURN(RC_WMORE);
  42.378 -					/* Fall through */
  42.379 -				case -1: RETURN(RC_FAIL);
  42.380 -				}
  42.381 -
  42.382 -				ADVANCE(skip + tag_len);
  42.383 -				ctx->step -= 2;
  42.384 -				edx--;
  42.385 -				continue;  /* Try again with the next tag */
  42.386 -			}
  42.387 -		}
  42.388 -
  42.389 -		/*
  42.390 -		 * MICROPHASE 2: Invoke the member-specific decoder.
  42.391 -		 */
  42.392 -		ctx->step |= 1;		/* Confirm entering next microphase */
  42.393 -	microphase2:
  42.394 -		ASN_DEBUG("Inside SEQUENCE %s MF2", td->name);
  42.395 -		
  42.396 -		/*
  42.397 -		 * Compute the position of the member inside a structure,
  42.398 -		 * and also a type of containment (it may be contained
  42.399 -		 * as pointer or using inline inclusion).
  42.400 -		 */
  42.401 -		if(elements[edx].flags & ATF_POINTER) {
  42.402 -			/* Member is a pointer to another structure */
  42.403 -			memb_ptr2 = (void **)((char *)st + elements[edx].memb_offset);
  42.404 -		} else {
  42.405 -			/*
  42.406 -			 * A pointer to a pointer
  42.407 -			 * holding the start of the structure
  42.408 -			 */
  42.409 -			memb_ptr = (char *)st + elements[edx].memb_offset;
  42.410 -			memb_ptr2 = &memb_ptr;
  42.411 -		}
  42.412 -		/*
  42.413 -		 * Invoke the member fetch routine according to member's type
  42.414 -		 */
  42.415 -		rval = elements[edx].type->ber_decoder(opt_codec_ctx,
  42.416 -				elements[edx].type,
  42.417 -				memb_ptr2, ptr, LEFT,
  42.418 -				elements[edx].tag_mode);
  42.419 -		ASN_DEBUG("In %s SEQUENCE decoded %d %s of %d "
  42.420 -			"in %d bytes rval.code %d, size=%d",
  42.421 -			td->name, edx, elements[edx].type->name,
  42.422 -			(int)LEFT, (int)rval.consumed, rval.code, (int)size);
  42.423 -		switch(rval.code) {
  42.424 -		case RC_OK:
  42.425 -			break;
  42.426 -		case RC_WMORE: /* More data expected */
  42.427 -			if(!SIZE_VIOLATION) {
  42.428 -				ADVANCE(rval.consumed);
  42.429 -				RETURN(RC_WMORE);
  42.430 -			}
  42.431 -			ASN_DEBUG("Size violation (c->l=%ld <= s=%ld)",
  42.432 -				(long)ctx->left, (long)size);
  42.433 -			/* Fall through */
  42.434 -		case RC_FAIL: /* Fatal error */
  42.435 -			RETURN(RC_FAIL);
  42.436 -		} /* switch(rval) */
  42.437 -		
  42.438 -		ADVANCE(rval.consumed);
  42.439 -	  }	/* for(all structure members) */
  42.440 -
  42.441 -	phase3:
  42.442 -		ctx->phase = 3;
  42.443 -	case 3:	/* 00 and other tags expected */
  42.444 -	case 4:	/* only 00's expected */
  42.445 -
  42.446 -		ASN_DEBUG("SEQUENCE %s Leftover: %ld, size = %ld",
  42.447 -			td->name, (long)ctx->left, (long)size);
  42.448 -
  42.449 -		/*
  42.450 -		 * Skip everything until the end of the SEQUENCE.
  42.451 -		 */
  42.452 -		while(ctx->left) {
  42.453 -			ssize_t tl, ll;
  42.454 -
  42.455 -			tl = ber_fetch_tag(ptr, LEFT, &tlv_tag);
  42.456 -			switch(tl) {
  42.457 -			case 0: if(!SIZE_VIOLATION) RETURN(RC_WMORE);
  42.458 -				/* Fall through */
  42.459 -			case -1: RETURN(RC_FAIL);
  42.460 -			}
  42.461 -
  42.462 -			/*
  42.463 -			 * If expected <0><0>...
  42.464 -			 */
  42.465 -			if(ctx->left < 0
  42.466 -				&& ((const uint8_t *)ptr)[0] == 0) {
  42.467 -				if(LEFT < 2) {
  42.468 -					if(SIZE_VIOLATION)
  42.469 -						RETURN(RC_FAIL);
  42.470 -					else
  42.471 -						RETURN(RC_WMORE);
  42.472 -				} else if(((const uint8_t *)ptr)[1] == 0) {
  42.473 -					/*
  42.474 -					 * Correctly finished with <0><0>.
  42.475 -					 */
  42.476 -					ADVANCE(2);
  42.477 -					ctx->left++;
  42.478 -					ctx->phase = 4;
  42.479 -					continue;
  42.480 -				}
  42.481 -			}
  42.482 -
  42.483 -			if(!IN_EXTENSION_GROUP(specs, td->elements_count)
  42.484 -			|| ctx->phase == 4) {
  42.485 -				ASN_DEBUG("Unexpected continuation "
  42.486 -					"of a non-extensible type "
  42.487 -					"%s (SEQUENCE): %s",
  42.488 -					td->name,
  42.489 -					ber_tlv_tag_string(tlv_tag));
  42.490 -				RETURN(RC_FAIL);
  42.491 -			}
  42.492 -
  42.493 -			ll = ber_skip_length(opt_codec_ctx,
  42.494 -				BER_TLV_CONSTRUCTED(ptr),
  42.495 -				(const char *)ptr + tl, LEFT - tl);
  42.496 -			switch(ll) {
  42.497 -			case 0: if(!SIZE_VIOLATION) RETURN(RC_WMORE);
  42.498 -				/* Fall through */
  42.499 -			case -1: RETURN(RC_FAIL);
  42.500 -			}
  42.501 -
  42.502 -			ADVANCE(tl + ll);
  42.503 -		}
  42.504 -
  42.505 -		PHASE_OUT(ctx);
  42.506 -	}
  42.507 -	
  42.508 -	RETURN(RC_OK);
  42.509 -}
  42.510 -
  42.511 -
  42.512 -/*
  42.513 - * The DER encoder of the SEQUENCE type.
  42.514 - */
  42.515 -asn_enc_rval_t
  42.516 -SEQUENCE_encode_der(asn_TYPE_descriptor_t *td,
  42.517 -	void *sptr, int tag_mode, ber_tlv_tag_t tag,
  42.518 -	asn_app_consume_bytes_f *cb, void *app_key) {
  42.519 -	size_t computed_size = 0;
  42.520 -	asn_enc_rval_t erval;
  42.521 -	ssize_t ret;
  42.522 -	int edx;
  42.523 -
  42.524 -	ASN_DEBUG("%s %s as SEQUENCE",
  42.525 -		cb?"Encoding":"Estimating", td->name);
  42.526 -
  42.527 -	/*
  42.528 -	 * Gather the length of the underlying members sequence.
  42.529 -	 */
  42.530 -	for(edx = 0; edx < td->elements_count; edx++) {
  42.531 -		asn_TYPE_member_t *elm = &td->elements[edx];
  42.532 -		void *memb_ptr;
  42.533 -		if(elm->flags & ATF_POINTER) {
  42.534 -			memb_ptr = *(void **)((char *)sptr + elm->memb_offset);
  42.535 -			if(!memb_ptr) {
  42.536 -				if(elm->optional) continue;
  42.537 -				/* Mandatory element is missing */
  42.538 -				_ASN_ENCODE_FAILED;
  42.539 -			}
  42.540 -		} else {
  42.541 -			memb_ptr = (void *)((char *)sptr + elm->memb_offset);
  42.542 -		}
  42.543 -		erval = elm->type->der_encoder(elm->type, memb_ptr,
  42.544 -			elm->tag_mode, elm->tag,
  42.545 -			0, 0);
  42.546 -		if(erval.encoded == -1)
  42.547 -			return erval;
  42.548 -		computed_size += erval.encoded;
  42.549 -		ASN_DEBUG("Member %d %s estimated %ld bytes",
  42.550 -			edx, elm->name, (long)erval.encoded);
  42.551 -	}
  42.552 -
  42.553 -	/*
  42.554 -	 * Encode the TLV for the sequence itself.
  42.555 -	 */
  42.556 -	ret = der_write_tags(td, computed_size, tag_mode, 1, tag, cb, app_key);
  42.557 -	ASN_DEBUG("Wrote tags: %ld (+%ld)", (long)ret, (long)computed_size);
  42.558 -	if(ret == -1)
  42.559 -		_ASN_ENCODE_FAILED;
  42.560 -	erval.encoded = computed_size + ret;
  42.561 -
  42.562 -	if(!cb) _ASN_ENCODED_OK(erval);
  42.563 -
  42.564 -	/*
  42.565 -	 * Encode all members.
  42.566 -	 */
  42.567 -	for(edx = 0; edx < td->elements_count; edx++) {
  42.568 -		asn_TYPE_member_t *elm = &td->elements[edx];
  42.569 -		asn_enc_rval_t tmperval;
  42.570 -		void *memb_ptr;
  42.571 -
  42.572 -		if(elm->flags & ATF_POINTER) {
  42.573 -			memb_ptr = *(void **)((char *)sptr + elm->memb_offset);
  42.574 -			if(!memb_ptr) continue;
  42.575 -		} else {
  42.576 -			memb_ptr = (void *)((char *)sptr + elm->memb_offset);
  42.577 -		}
  42.578 -		tmperval = elm->type->der_encoder(elm->type, memb_ptr,
  42.579 -			elm->tag_mode, elm->tag,
  42.580 -			cb, app_key);
  42.581 -		if(tmperval.encoded == -1)
  42.582 -			return tmperval;
  42.583 -		computed_size -= tmperval.encoded;
  42.584 -		ASN_DEBUG("Member %d %s of SEQUENCE %s encoded in %ld bytes",
  42.585 -			edx, elm->name, td->name, (long)tmperval.encoded);
  42.586 -	}
  42.587 -
  42.588 -	if(computed_size != 0)
  42.589 -		/*
  42.590 -		 * Encoded size is not equal to the computed size.
  42.591 -		 */
  42.592 -		_ASN_ENCODE_FAILED;
  42.593 -
  42.594 -	_ASN_ENCODED_OK(erval);
  42.595 -}
  42.596 -
  42.597 -
  42.598 -#undef	XER_ADVANCE
  42.599 -#define	XER_ADVANCE(num_bytes)	do {			\
  42.600 -		size_t num = num_bytes;			\
  42.601 -		buf_ptr = ((const char *)buf_ptr) + num;\
  42.602 -		size -= num;				\
  42.603 -		consumed_myself += num;			\
  42.604 -	} while(0)
  42.605 -
  42.606 -/*
  42.607 - * Decode the XER (XML) data.
  42.608 - */
  42.609 -asn_dec_rval_t
  42.610 -SEQUENCE_decode_xer(asn_codec_ctx_t *opt_codec_ctx, asn_TYPE_descriptor_t *td,
  42.611 -	void **struct_ptr, const char *opt_mname,
  42.612 -		const void *buf_ptr, size_t size) {
  42.613 -	/*
  42.614 -	 * Bring closer parts of structure description.
  42.615 -	 */
  42.616 -	asn_SEQUENCE_specifics_t *specs
  42.617 -		= (asn_SEQUENCE_specifics_t *)td->specifics;
  42.618 -	asn_TYPE_member_t *elements = td->elements;
  42.619 -	const char *xml_tag = opt_mname ? opt_mname : td->xml_tag;
  42.620 -
  42.621 -	/*
  42.622 -	 * ... and parts of the structure being constructed.
  42.623 -	 */
  42.624 -	void *st = *struct_ptr;	/* Target structure. */
  42.625 -	asn_struct_ctx_t *ctx;	/* Decoder context */
  42.626 -
  42.627 -	asn_dec_rval_t rval;		/* Return value from a decoder */
  42.628 -	ssize_t consumed_myself = 0;	/* Consumed bytes from ptr */
  42.629 -	int edx;			/* Element index */
  42.630 -	int edx_end;
  42.631 -
  42.632 -	/*
  42.633 -	 * Create the target structure if it is not present already.
  42.634 -	 */
  42.635 -	if(st == 0) {
  42.636 -		st = *struct_ptr = CALLOC(1, specs->struct_size);
  42.637 -		if(st == 0) RETURN(RC_FAIL);
  42.638 -	}
  42.639 -
  42.640 -	/*
  42.641 -	 * Restore parsing context.
  42.642 -	 */
  42.643 -	ctx = (asn_struct_ctx_t *)((char *)st + specs->ctx_offset);
  42.644 -
  42.645 -
  42.646 -	/*
  42.647 -	 * Phases of XER/XML processing:
  42.648 -	 * Phase 0: Check that the opening tag matches our expectations.
  42.649 -	 * Phase 1: Processing body and reacting on closing tag.
  42.650 -	 * Phase 2: Processing inner type.
  42.651 -	 * Phase 3: Skipping unknown extensions.
  42.652 -	 * Phase 4: PHASED OUT
  42.653 -	 */
  42.654 -	for(edx = ctx->step; ctx->phase <= 3;) {
  42.655 -		pxer_chunk_type_e ch_type;	/* XER chunk type */
  42.656 -		ssize_t ch_size;		/* Chunk size */
  42.657 -		xer_check_tag_e tcv;		/* Tag check value */
  42.658 -		asn_TYPE_member_t *elm;
  42.659 -		int n;
  42.660 -
  42.661 -		/*
  42.662 -		 * Go inside the inner member of a sequence.
  42.663 -		 */
  42.664 -		if(ctx->phase == 2) {
  42.665 -			asn_dec_rval_t tmprval;
  42.666 -			void *memb_ptr;		/* Pointer to the member */
  42.667 -			void **memb_ptr2;	/* Pointer to that pointer */
  42.668 -
  42.669 -			elm = &td->elements[edx];
  42.670 -
  42.671 -			if(elm->flags & ATF_POINTER) {
  42.672 -				/* Member is a pointer to another structure */
  42.673 -				memb_ptr2 = (void **)((char *)st + elm->memb_offset);
  42.674 -			} else {
  42.675 -				memb_ptr = (char *)st + elm->memb_offset;
  42.676 -				memb_ptr2 = &memb_ptr;
  42.677 -			}
  42.678 -
  42.679 -			/* Invoke the inner type decoder, m.b. multiple times */
  42.680 -			tmprval = elm->type->xer_decoder(opt_codec_ctx,
  42.681 -					elm->type, memb_ptr2, elm->name,
  42.682 -					buf_ptr, size);
  42.683 -			XER_ADVANCE(tmprval.consumed);
  42.684 -			if(tmprval.code != RC_OK)
  42.685 -				RETURN(tmprval.code);
  42.686 -			ctx->phase = 1;	/* Back to body processing */
  42.687 -			ctx->step = ++edx;
  42.688 -			ASN_DEBUG("XER/SEQUENCE phase => %d, step => %d",
  42.689 -				ctx->phase, ctx->step);
  42.690 -			/* Fall through */
  42.691 -		}
  42.692 -
  42.693 -		/*
  42.694 -		 * Get the next part of the XML stream.
  42.695 -		 */
  42.696 -		ch_size = xer_next_token(&ctx->context, buf_ptr, size,
  42.697 -			&ch_type);
  42.698 -		switch(ch_size) {
  42.699 -		case -1: RETURN(RC_FAIL);
  42.700 -		case 0:  RETURN(RC_WMORE);
  42.701 -		default:
  42.702 -			switch(ch_type) {
  42.703 -			case PXER_COMMENT:	/* Got XML comment */
  42.704 -			case PXER_TEXT:		/* Ignore free-standing text */
  42.705 -				XER_ADVANCE(ch_size);	/* Skip silently */
  42.706 -				continue;
  42.707 -			case PXER_TAG:
  42.708 -				break;	/* Check the rest down there */
  42.709 -			}
  42.710 -		}
  42.711 -
  42.712 -		tcv = xer_check_tag(buf_ptr, ch_size, xml_tag);
  42.713 -		ASN_DEBUG("XER/SEQUENCE: tcv = %d, ph=%d [%s]",
  42.714 -			tcv, ctx->phase, xml_tag);
  42.715 -
  42.716 -		/* Skip the extensions section */
  42.717 -		if(ctx->phase == 3) {
  42.718 -			switch(xer_skip_unknown(tcv, &ctx->left)) {
  42.719 -			case -1:
  42.720 -				ctx->phase = 4;
  42.721 -				RETURN(RC_FAIL);
  42.722 -			case 0:
  42.723 -				XER_ADVANCE(ch_size);
  42.724 -				continue;
  42.725 -			case 1:
  42.726 -				XER_ADVANCE(ch_size);
  42.727 -				ctx->phase = 1;
  42.728 -				continue;
  42.729 -			case 2:
  42.730 -				ctx->phase = 1;
  42.731 -				break;
  42.732 -			}
  42.733 -		}
  42.734 -
  42.735 -		switch(tcv) {
  42.736 -		case XCT_CLOSING:
  42.737 -			if(ctx->phase == 0) break;
  42.738 -			ctx->phase = 0;
  42.739 -			/* Fall through */
  42.740 -		case XCT_BOTH:
  42.741 -			if(ctx->phase == 0) {
  42.742 -				if(edx >= td->elements_count
  42.743 -				   ||
  42.744 -				   /* Explicit OPTIONAL specs reaches the end */
  42.745 -				    (edx + elements[edx].optional
  42.746 -					== td->elements_count)
  42.747 -				   ||
  42.748 -				   /* All extensions are optional */
  42.749 -				   (IN_EXTENSION_GROUP(specs, edx)
  42.750 -					&& specs->ext_before
  42.751 -						> td->elements_count)
  42.752 -				) {
  42.753 -					XER_ADVANCE(ch_size);
  42.754 -					ctx->phase = 4;	/* Phase out */
  42.755 -					RETURN(RC_OK);
  42.756 -				} else {
  42.757 -					ASN_DEBUG("Premature end of XER SEQUENCE");
  42.758 -					RETURN(RC_FAIL);
  42.759 -				}
  42.760 -			}
  42.761 -			/* Fall through */
  42.762 -		case XCT_OPENING:
  42.763 -			if(ctx->phase == 0) {
  42.764 -				XER_ADVANCE(ch_size);
  42.765 -				ctx->phase = 1;	/* Processing body phase */
  42.766 -				continue;
  42.767 -			}
  42.768 -			/* Fall through */
  42.769 -		case XCT_UNKNOWN_OP:
  42.770 -		case XCT_UNKNOWN_BO:
  42.771 -
  42.772 -			ASN_DEBUG("XER/SEQUENCE: tcv=%d, ph=%d, edx=%d",
  42.773 -				tcv, ctx->phase, edx);
  42.774 -			if(ctx->phase != 1) {
  42.775 -				break;	/* Really unexpected */
  42.776 -			}
  42.777 -
  42.778 -			if(edx < td->elements_count) {
  42.779 -				/*
  42.780 -				 * Search which member corresponds to this tag.
  42.781 -				 */
  42.782 -				edx_end = edx + elements[edx].optional + 1;
  42.783 -				if(edx_end > td->elements_count)
  42.784 -					edx_end = td->elements_count;
  42.785 -				for(n = edx; n < edx_end; n++) {
  42.786 -					elm = &td->elements[n];
  42.787 -					tcv = xer_check_tag(buf_ptr,
  42.788 -						ch_size, elm->name);
  42.789 -					switch(tcv) {
  42.790 -					case XCT_BOTH:
  42.791 -					case XCT_OPENING:
  42.792 -						/*
  42.793 -						 * Process this member.
  42.794 -						 */
  42.795 -						ctx->step = edx = n;
  42.796 -						ctx->phase = 2;
  42.797 -						break;
  42.798 -					case XCT_UNKNOWN_OP:
  42.799 -					case XCT_UNKNOWN_BO:
  42.800 -						continue;
  42.801 -					default:
  42.802 -						n = edx_end;
  42.803 -						break;	/* Phase out */
  42.804 -					}
  42.805 -					break;
  42.806 -				}
  42.807 -				if(n != edx_end)
  42.808 -					continue;
  42.809 -			} else {
  42.810 -				ASN_DEBUG("Out of defined members: %d/%d",
  42.811 -					edx, td->elements_count);
  42.812 -			}
  42.813 -
  42.814 -			/* It is expected extension */
  42.815 -			if(IN_EXTENSION_GROUP(specs,
  42.816 -				edx + (edx < td->elements_count
  42.817 -					? elements[edx].optional : 0))) {
  42.818 -				ASN_DEBUG("Got anticipated extension at %d",
  42.819 -					edx);
  42.820 -				/*
  42.821 -				 * Check for (XCT_BOTH or XCT_UNKNOWN_BO)
  42.822 -				 * By using a mask. Only record a pure
  42.823 -				 * <opening> tags.
  42.824 -				 */
  42.825 -				if(tcv & XCT_CLOSING) {
  42.826 -					/* Found </extension> without body */
  42.827 -				} else {
  42.828 -					ctx->left = 1;
  42.829 -					ctx->phase = 3;	/* Skip ...'s */
  42.830 -				}
  42.831 -				XER_ADVANCE(ch_size);
  42.832 -				continue;
  42.833 -			}
  42.834 -
  42.835 -			/* Fall through */
  42.836 -		default:
  42.837 -			break;
  42.838 -		}
  42.839 -
  42.840 -		ASN_DEBUG("Unexpected XML tag in SEQUENCE [%c%c%c%c%c%c]",
  42.841 -			size>0?((const char *)buf_ptr)[0]:'.',
  42.842 -			size>1?((const char *)buf_ptr)[1]:'.',
  42.843 -			size>2?((const char *)buf_ptr)[2]:'.',
  42.844 -			size>3?((const char *)buf_ptr)[3]:'.',
  42.845 -			size>4?((const char *)buf_ptr)[4]:'.',
  42.846 -			size>5?((const char *)buf_ptr)[5]:'.');
  42.847 -		break;
  42.848 -	}
  42.849 -
  42.850 -	ctx->phase = 4;	/* "Phase out" on hard failure */
  42.851 -	RETURN(RC_FAIL);
  42.852 -}
  42.853 -
  42.854 -asn_enc_rval_t
  42.855 -SEQUENCE_encode_xer(asn_TYPE_descriptor_t *td, void *sptr,
  42.856 -	int ilevel, enum xer_encoder_flags_e flags,
  42.857 -		asn_app_consume_bytes_f *cb, void *app_key) {
  42.858 -	asn_enc_rval_t er;
  42.859 -	int xcan = (flags & XER_F_CANONICAL);
  42.860 -	int edx;
  42.861 -
  42.862 -	if(!sptr)
  42.863 -		_ASN_ENCODE_FAILED;
  42.864 -
  42.865 -	er.encoded = 0;
  42.866 -
  42.867 -	for(edx = 0; edx < td->elements_count; edx++) {
  42.868 -		asn_enc_rval_t tmper;
  42.869 -		asn_TYPE_member_t *elm = &td->elements[edx];
  42.870 -		void *memb_ptr;
  42.871 -		const char *mname = elm->name;
  42.872 -		unsigned int mlen = strlen(mname);
  42.873 -
  42.874 -		if(elm->flags & ATF_POINTER) {
  42.875 -			memb_ptr = *(void **)((char *)sptr + elm->memb_offset);
  42.876 -			if(!memb_ptr) {
  42.877 -				if(elm->optional)
  42.878 -					continue;
  42.879 -				/* Mandatory element is missing */
  42.880 -				_ASN_ENCODE_FAILED;
  42.881 -			}
  42.882 -		} else {
  42.883 -			memb_ptr = (void *)((char *)sptr + elm->memb_offset);
  42.884 -		}
  42.885 -
  42.886 -		if(!xcan) _i_ASN_TEXT_INDENT(1, ilevel);
  42.887 -		_ASN_CALLBACK3("<", 1, mname, mlen, ">", 1);
  42.888 -
  42.889 -		/* Print the member itself */
  42.890 -		tmper = elm->type->xer_encoder(elm->type, memb_ptr,
  42.891 -			ilevel + 1, flags, cb, app_key);
  42.892 -		if(tmper.encoded == -1) return tmper;
  42.893 -
  42.894 -		_ASN_CALLBACK3("</", 2, mname, mlen, ">", 1);
  42.895 -		er.encoded += 5 + (2 * mlen) + tmper.encoded;
  42.896 -	}
  42.897 -
  42.898 -	if(!xcan) _i_ASN_TEXT_INDENT(1, ilevel - 1);
  42.899 -
  42.900 -	_ASN_ENCODED_OK(er);
  42.901 -cb_failed:
  42.902 -	_ASN_ENCODE_FAILED;
  42.903 -}
  42.904 -
  42.905 -int
  42.906 -SEQUENCE_print(asn_TYPE_descriptor_t *td, const void *sptr, int ilevel,
  42.907 -		asn_app_consume_bytes_f *cb, void *app_key) {
  42.908 -	int edx;
  42.909 -	int ret;
  42.910 -
  42.911 -	if(!sptr) return (cb("<absent>", 8, app_key) < 0) ? -1 : 0;
  42.912 -
  42.913 -	/* Dump preamble */
  42.914 -	if(cb(td->name, strlen(td->name), app_key) < 0
  42.915 -	|| cb(" ::= {", 6, app_key) < 0)
  42.916 -		return -1;
  42.917 -
  42.918 -	for(edx = 0; edx < td->elements_count; edx++) {
  42.919 -		asn_TYPE_member_t *elm = &td->elements[edx];
  42.920 -		const void *memb_ptr;
  42.921 -
  42.922 -		if(elm->flags & ATF_POINTER) {
  42.923 -			memb_ptr = *(const void * const *)((const char *)sptr + elm->memb_offset);
  42.924 -			if(!memb_ptr) {
  42.925 -				if(elm->optional) continue;
  42.926 -				/* Print <absent> line */
  42.927 -				/* Fall through */
  42.928 -			}
  42.929 -		} else {
  42.930 -			memb_ptr = (const void *)((const char *)sptr + elm->memb_offset);
  42.931 -		}
  42.932 -
  42.933 -		/* Indentation */
  42.934 -		_i_INDENT(1);
  42.935 -
  42.936 -		/* Print the member's name and stuff */
  42.937 -		if(cb(elm->name, strlen(elm->name), app_key) < 0
  42.938 -		|| cb(": ", 2, app_key) < 0)
  42.939 -			return -1;
  42.940 -
  42.941 -		/* Print the member itself */
  42.942 -		ret = elm->type->print_struct(elm->type, memb_ptr, ilevel + 1,
  42.943 -			cb, app_key);
  42.944 -		if(ret) return ret;
  42.945 -	}
  42.946 -
  42.947 -	ilevel--;
  42.948 -	_i_INDENT(1);
  42.949 -
  42.950 -	return (cb("}", 1, app_key) < 0) ? -1 : 0;
  42.951 -}
  42.952 -
  42.953 -void
  42.954 -SEQUENCE_free(asn_TYPE_descriptor_t *td, void *sptr, int contents_only) {
  42.955 -	int edx;
  42.956 -
  42.957 -	if(!td || !sptr)
  42.958 -		return;
  42.959 -
  42.960 -	ASN_DEBUG("Freeing %s as SEQUENCE", td->name);
  42.961 -
  42.962 -	for(edx = 0; edx < td->elements_count; edx++) {
  42.963 -		asn_TYPE_member_t *elm = &td->elements[edx];
  42.964 -		void *memb_ptr;
  42.965 -		if(elm->flags & ATF_POINTER) {
  42.966 -			memb_ptr = *(void **)((char *)sptr + elm->memb_offset);
  42.967 -			if(memb_ptr)
  42.968 -				ASN_STRUCT_FREE(*elm->type, memb_ptr);
  42.969 -		} else {
  42.970 -			memb_ptr = (void *)((char *)sptr + elm->memb_offset);
  42.971 -			ASN_STRUCT_FREE_CONTENTS_ONLY(*elm->type, memb_ptr);
  42.972 -		}
  42.973 -	}
  42.974 -
  42.975 -	if(!contents_only) {
  42.976 -		FREEMEM(sptr);
  42.977 -	}
  42.978 -}
  42.979 -
  42.980 -int
  42.981 -SEQUENCE_constraint(asn_TYPE_descriptor_t *td, const void *sptr,
  42.982 -		asn_app_constraint_failed_f *ctfailcb, void *app_key) {
  42.983 -	int edx;
  42.984 -
  42.985 -	if(!sptr) {
  42.986 -		_ASN_CTFAIL(app_key, td, sptr,
  42.987 -			"%s: value not given (%s:%d)",
  42.988 -			td->name, __FILE__, __LINE__);
  42.989 -		return -1;
  42.990 -	}
  42.991 -
  42.992 -	/*
  42.993 -	 * Iterate over structure members and check their validity.
  42.994 -	 */
  42.995 -	for(edx = 0; edx < td->elements_count; edx++) {
  42.996 -		asn_TYPE_member_t *elm = &td->elements[edx];
  42.997 -		const void *memb_ptr;
  42.998 -
  42.999 -		if(elm->flags & ATF_POINTER) {
 42.1000 -			memb_ptr = *(const void * const *)((const char *)sptr + elm->memb_offset);
 42.1001 -			if(!memb_ptr) {
 42.1002 -				if(elm->optional)
 42.1003 -					continue;
 42.1004 -				_ASN_CTFAIL(app_key, td, sptr,
 42.1005 -				"%s: mandatory element %s absent (%s:%d)",
 42.1006 -				td->name, elm->name, __FILE__, __LINE__);
 42.1007 -				return -1;
 42.1008 -			}
 42.1009 -		} else {
 42.1010 -			memb_ptr = (const void *)((const char *)sptr + elm->memb_offset);
 42.1011 -		}
 42.1012 -
 42.1013 -		if(elm->memb_constraints) {
 42.1014 -			int ret = elm->memb_constraints(elm->type, memb_ptr,
 42.1015 -				ctfailcb, app_key);
 42.1016 -			if(ret) return ret;
 42.1017 -		} else {
 42.1018 -			int ret = elm->type->check_constraints(elm->type,
 42.1019 -				memb_ptr, ctfailcb, app_key);
 42.1020 -			if(ret) return ret;
 42.1021 -			/*
 42.1022 -			 * Cannot inherit it earlier:
 42.1023 -			 * need to make sure we get the updated version.
 42.1024 -			 */
 42.1025 -			elm->memb_constraints = elm->type->check_constraints;
 42.1026 -		}
 42.1027 -	}
 42.1028 -
 42.1029 -	return 0;
 42.1030 -}
 42.1031 -
 42.1032 -asn_dec_rval_t
 42.1033 -SEQUENCE_decode_uper(asn_codec_ctx_t *opt_codec_ctx, asn_TYPE_descriptor_t *td,
 42.1034 -	asn_per_constraints_t *constraints, void **sptr, asn_per_data_t *pd) {
 42.1035 -	asn_SEQUENCE_specifics_t *specs = (asn_SEQUENCE_specifics_t *)td->specifics;
 42.1036 -	void *st = *sptr;	/* Target structure. */
 42.1037 -	int extpresent;		/* Extension additions are present */
 42.1038 -	uint8_t *opres;		/* Presence of optional root members */
 42.1039 -	asn_per_data_t opmd;
 42.1040 -	asn_dec_rval_t rv;
 42.1041 -	int edx;
 42.1042 -
 42.1043 -	(void)constraints;
 42.1044 -
 42.1045 -	if(_ASN_STACK_OVERFLOW_CHECK(opt_codec_ctx))
 42.1046 -		_ASN_DECODE_FAILED;
 42.1047 -
 42.1048 -	if(!st) {
 42.1049 -		st = *sptr = CALLOC(1, specs->struct_size);
 42.1050 -		if(!st) _ASN_DECODE_FAILED;
 42.1051 -	}
 42.1052 -
 42.1053 -	ASN_DEBUG("Decoding %s as SEQUENCE (UPER)", td->name);
 42.1054 -
 42.1055 -	/* Handle extensions */
 42.1056 -	if(specs->ext_before >= 0) {
 42.1057 -		extpresent = per_get_few_bits(pd, 1);
 42.1058 -		if(extpresent < 0) _ASN_DECODE_STARVED;
 42.1059 -	} else {
 42.1060 -		extpresent = 0;
 42.1061 -	}
 42.1062 -
 42.1063 -	/* Prepare a place and read-in the presence bitmap */
 42.1064 -	memset(&opmd, 0, sizeof(opmd));
 42.1065 -	if(specs->roms_count) {
 42.1066 -		opres = (uint8_t *)MALLOC(((specs->roms_count + 7) >> 3) + 1);
 42.1067 -		if(!opres) _ASN_DECODE_FAILED;
 42.1068 -		/* Get the presence map */
 42.1069 -		if(per_get_many_bits(pd, opres, 0, specs->roms_count)) {
 42.1070 -			FREEMEM(opres);
 42.1071 -			_ASN_DECODE_STARVED;
 42.1072 -		}
 42.1073 -		opmd.buffer = opres;
 42.1074 -		opmd.nbits = specs->roms_count;
 42.1075 -		ASN_DEBUG("Read in presence bitmap for %s of %d bits (%x..)",
 42.1076 -			td->name, specs->roms_count, *opres);
 42.1077 -	} else {
 42.1078 -		opres = 0;
 42.1079 -	}
 42.1080 -
 42.1081 -	/*
 42.1082 -	 * Get the sequence ROOT elements.
 42.1083 -	 */
 42.1084 -	for(edx = 0; edx < td->elements_count; edx++) {
 42.1085 -		asn_TYPE_member_t *elm = &td->elements[edx];
 42.1086 -		void *memb_ptr;		/* Pointer to the member */
 42.1087 -		void **memb_ptr2;	/* Pointer to that pointer */
 42.1088 -
 42.1089 -		if(IN_EXTENSION_GROUP(specs, edx))
 42.1090 -			continue;
 42.1091 -
 42.1092 -		/* Fetch the pointer to this member */
 42.1093 -		if(elm->flags & ATF_POINTER) {
 42.1094 -			memb_ptr2 = (void **)((char *)st + elm->memb_offset);
 42.1095 -		} else {
 42.1096 -			memb_ptr = (char *)st + elm->memb_offset;
 42.1097 -			memb_ptr2 = &memb_ptr;
 42.1098 -		}
 42.1099 -
 42.1100 -		/* Deal with optionality */
 42.1101 -		if(elm->optional) {
 42.1102 -			int present = per_get_few_bits(&opmd, 1);
 42.1103 -			ASN_DEBUG("Member %s->%s is optional, p=%d (%d->%d)",
 42.1104 -				td->name, elm->name, present,
 42.1105 -				(int)opmd.nboff, (int)opmd.nbits);
 42.1106 -			if(present == 0) {
 42.1107 -				/* This element is not present */
 42.1108 -				if(elm->default_value) {
 42.1109 -					/* Fill-in DEFAULT */
 42.1110 -					if(elm->default_value(1, memb_ptr2)) {
 42.1111 -						FREEMEM(opres);
 42.1112 -						_ASN_DECODE_FAILED;
 42.1113 -					}
 42.1114 -					ASN_DEBUG("Filled-in default");
 42.1115 -				}
 42.1116 -				/* The member is just not present */
 42.1117 -				continue;
 42.1118 -			}
 42.1119 -			/* Fall through */
 42.1120 -		}
 42.1121 -
 42.1122 -		/* Fetch the member from the stream */
 42.1123 -		ASN_DEBUG("Decoding member %s in %s", elm->name, td->name);
 42.1124 -		rv = elm->type->uper_decoder(opt_codec_ctx, elm->type,
 42.1125 -			elm->per_constraints, memb_ptr2, pd);
 42.1126 -		if(rv.code != RC_OK) {
 42.1127 -			ASN_DEBUG("Failed decode %s in %s",
 42.1128 -				elm->name, td->name);
 42.1129 -			FREEMEM(opres);
 42.1130 -			return rv;
 42.1131 -		}
 42.1132 -	}
 42.1133 -
 42.1134 -	/* Optionality map is not needed anymore */
 42.1135 -	FREEMEM(opres);
 42.1136 -
 42.1137 -	/*
 42.1138 -	 * Deal with extensions.
 42.1139 -	 */
 42.1140 -	if(extpresent) {
 42.1141 -		ssize_t bmlength;
 42.1142 -		uint8_t *epres;		/* Presence of extension members */
 42.1143 -		asn_per_data_t epmd;
 42.1144 -
 42.1145 -		bmlength = uper_get_nslength(pd);
 42.1146 -		if(bmlength < 0) _ASN_DECODE_STARVED;
 42.1147 -
 42.1148 -		ASN_DEBUG("Extensions %ld present in %s", (long)bmlength, td->name);
 42.1149 -
 42.1150 -		epres = (uint8_t *)MALLOC((bmlength + 15) >> 3);
 42.1151 -		if(!epres) _ASN_DECODE_STARVED;
 42.1152 -
 42.1153 -		/* Get the extensions map */
 42.1154 -		if(per_get_many_bits(pd, epres, 0, bmlength))
 42.1155 -			_ASN_DECODE_STARVED;
 42.1156 -
 42.1157 -		memset(&epmd, 0, sizeof(epmd));
 42.1158 -		epmd.buffer = epres;
 42.1159 -		epmd.nbits = bmlength;
 42.1160 -		ASN_DEBUG("Read in extensions bitmap for %s of %ld bits (%x..)",
 42.1161 -			td->name, (long)bmlength, *epres);
 42.1162 -
 42.1163 -	    /* Go over extensions and read them in */
 42.1164 -	    for(edx = specs->ext_after + 1; edx < td->elements_count; edx++) {
 42.1165 -		asn_TYPE_member_t *elm = &td->elements[edx];
 42.1166 -		void *memb_ptr;		/* Pointer to the member */
 42.1167 -		void **memb_ptr2;	/* Pointer to that pointer */
 42.1168 -		int present;
 42.1169 -
 42.1170 -		if(!IN_EXTENSION_GROUP(specs, edx)) {
 42.1171 -			ASN_DEBUG("%d is not extension", edx);
 42.1172 -			continue;
 42.1173 -		}
 42.1174 -
 42.1175 -		/* Fetch the pointer to this member */
 42.1176 -		if(elm->flags & ATF_POINTER) {
 42.1177 -			memb_ptr2 = (void **)((char *)st + elm->memb_offset);
 42.1178 -		} else {
 42.1179 -			memb_ptr = (void *)((char *)st + elm->memb_offset);
 42.1180 -			memb_ptr2 = &memb_ptr;
 42.1181 -		}
 42.1182 -
 42.1183 -		present = per_get_few_bits(&epmd, 1);
 42.1184 -		if(present <= 0) {
 42.1185 -			if(present < 0) break;	/* No more extensions */
 42.1186 -			continue;
 42.1187 -		}
 42.1188 -
 42.1189 -		ASN_DEBUG("Decoding member %s in %s %p", elm->name, td->name, *memb_ptr2);
 42.1190 -		rv = uper_open_type_get(opt_codec_ctx, elm->type,
 42.1191 -			elm->per_constraints, memb_ptr2, pd);
 42.1192 -		if(rv.code != RC_OK) {
 42.1193 -			FREEMEM(epres);
 42.1194 -			return rv;
 42.1195 -		}
 42.1196 -	    }
 42.1197 -
 42.1198 -		/* Skip over overflow extensions which aren't present
 42.1199 -		 * in this system's version of the protocol */
 42.1200 -		for(;;) {
 42.1201 -			ASN_DEBUG("Getting overflow extensions");
 42.1202 -			switch(per_get_few_bits(&epmd, 1)) {
 42.1203 -			case -1: break;
 42.1204 -			case 0: continue;
 42.1205 -			default:
 42.1206 -				if(uper_open_type_skip(opt_codec_ctx, pd)) {
 42.1207 -					FREEMEM(epres);
 42.1208 -					_ASN_DECODE_STARVED;
 42.1209 -				}
 42.1210 -			}
 42.1211 -			break;
 42.1212 -		}
 42.1213 -
 42.1214 -		FREEMEM(epres);
 42.1215 -	}
 42.1216 -
 42.1217 -	/* Fill DEFAULT members in extensions */
 42.1218 -	for(edx = specs->roms_count; edx < specs->roms_count
 42.1219 -			+ specs->aoms_count; edx++) {
 42.1220 -		asn_TYPE_member_t *elm = &td->elements[edx];
 42.1221 -		void **memb_ptr2;	/* Pointer to member pointer */
 42.1222 -
 42.1223 -		if(!elm->default_value) continue;
 42.1224 -
 42.1225 -		/* Fetch the pointer to this member */
 42.1226 -		if(elm->flags & ATF_POINTER) {
 42.1227 -			memb_ptr2 = (void **)((char *)st
 42.1228 -					+ elm->memb_offset);
 42.1229 -			if(*memb_ptr2) continue;
 42.1230 -		} else {
 42.1231 -			continue;	/* Extensions are all optionals */
 42.1232 -		}
 42.1233 -
 42.1234 -		/* Set default value */
 42.1235 -		if(elm->default_value(1, memb_ptr2)) {
 42.1236 -			_ASN_DECODE_FAILED;
 42.1237 -		}
 42.1238 -	}
 42.1239 -
 42.1240 -	rv.consumed = 0;
 42.1241 -	rv.code = RC_OK;
 42.1242 -	return rv;
 42.1243 -}
 42.1244 -
 42.1245 -static int
 42.1246 -SEQUENCE_handle_extensions(asn_TYPE_descriptor_t *td, void *sptr,
 42.1247 -		asn_per_outp_t *po1, asn_per_outp_t *po2) {
 42.1248 -	asn_SEQUENCE_specifics_t *specs
 42.1249 -		= (asn_SEQUENCE_specifics_t *)td->specifics;
 42.1250 -	int exts_present = 0;
 42.1251 -	int exts_count = 0;
 42.1252 -	int edx;
 42.1253 -
 42.1254 -	if(specs->ext_before < 0)
 42.1255 -		return 0;
 42.1256 -
 42.1257 -	/* Find out which extensions are present */
 42.1258 -	for(edx = specs->ext_after + 1; edx < td->elements_count; edx++) {
 42.1259 -		asn_TYPE_member_t *elm = &td->elements[edx];
 42.1260 -		void *memb_ptr;		/* Pointer to the member */
 42.1261 -		void **memb_ptr2;	/* Pointer to that pointer */
 42.1262 -		int present;
 42.1263 -
 42.1264 -		if(!IN_EXTENSION_GROUP(specs, edx)) {
 42.1265 -			ASN_DEBUG("%s (@%d) is not extension", elm->type->name, edx);
 42.1266 -			continue;
 42.1267 -		}
 42.1268 -
 42.1269 -		/* Fetch the pointer to this member */
 42.1270 -		if(elm->flags & ATF_POINTER) {
 42.1271 -			memb_ptr2 = (void **)((char *)sptr + elm->memb_offset);
 42.1272 -			present = (*memb_ptr2 != 0);
 42.1273 -		} else {
 42.1274 -			memb_ptr = (void *)((char *)sptr + elm->memb_offset);
 42.1275 -			memb_ptr2 = &memb_ptr;
 42.1276 -			present = 1;
 42.1277 -		}
 42.1278 -
 42.1279 -		ASN_DEBUG("checking %s (@%d) present => %d",
 42.1280 -			elm->type->name, edx, present);
 42.1281 -		exts_count++;
 42.1282 -		exts_present += present;
 42.1283 -
 42.1284 -		/* Encode as presence marker */
 42.1285 -		if(po1 && per_put_few_bits(po1, present, 1))
 42.1286 -			return -1;
 42.1287 -		/* Encode as open type field */
 42.1288 -		if(po2 && present && uper_open_type_put(elm->type,
 42.1289 -				elm->per_constraints, *memb_ptr2, po2))
 42.1290 -			return -1;
 42.1291 -
 42.1292 -	}
 42.1293 -
 42.1294 -	return exts_present ? exts_count : 0;
 42.1295 -}
 42.1296 -
 42.1297 -asn_enc_rval_t
 42.1298 -SEQUENCE_encode_uper(asn_TYPE_descriptor_t *td,
 42.1299 -	asn_per_constraints_t *constraints, void *sptr, asn_per_outp_t *po) {
 42.1300 -	asn_SEQUENCE_specifics_t *specs
 42.1301 -		= (asn_SEQUENCE_specifics_t *)td->specifics;
 42.1302 -	asn_enc_rval_t er;
 42.1303 -	int n_extensions;
 42.1304 -	int edx;
 42.1305 -	int i;
 42.1306 -
 42.1307 -	(void)constraints;
 42.1308 -
 42.1309 -	if(!sptr)
 42.1310 -		_ASN_ENCODE_FAILED;
 42.1311 -
 42.1312 -	er.encoded = 0;
 42.1313 -
 42.1314 -	ASN_DEBUG("Encoding %s as SEQUENCE (UPER)", td->name);
 42.1315 -
 42.1316 -
 42.1317 -	/*
 42.1318 -	 * X.691#18.1 Whether structure is extensible
 42.1319 -	 * and whether to encode extensions
 42.1320 -	 */
 42.1321 -	if(specs->ext_before >= 0) {
 42.1322 -		n_extensions = SEQUENCE_handle_extensions(td, sptr, 0, 0);
 42.1323 -		per_put_few_bits(po, n_extensions ? 1 : 0, 1);
 42.1324 -	} else {
 42.1325 -		n_extensions = 0;	/* There are no extensions to encode */
 42.1326 -	}
 42.1327 -
 42.1328 -	/* Encode a presence bitmap */
 42.1329 -	for(i = 0; i < specs->roms_count; i++) {
 42.1330 -		asn_TYPE_member_t *elm;
 42.1331 -		void *memb_ptr;		/* Pointer to the member */
 42.1332 -		void **memb_ptr2;	/* Pointer to that pointer */
 42.1333 -		int present;
 42.1334 -
 42.1335 -		edx = specs->oms[i];
 42.1336 -		elm = &td->elements[edx];
 42.1337 -
 42.1338 -		/* Fetch the pointer to this member */
 42.1339 -		if(elm->flags & ATF_POINTER) {
 42.1340 -			memb_ptr2 = (void **)((char *)sptr + elm->memb_offset);
 42.1341 -			present = (*memb_ptr2 != 0);
 42.1342 -		} else {
 42.1343 -			memb_ptr = (void *)((char *)sptr + elm->memb_offset);
 42.1344 -			memb_ptr2 = &memb_ptr;
 42.1345 -			present = 1;
 42.1346 -		}
 42.1347 -
 42.1348 -		/* Eliminate default values */
 42.1349 -		if(present && elm->default_value
 42.1350 -		&& elm->default_value(0, memb_ptr2) == 1)
 42.1351 -			present = 0;
 42.1352 -
 42.1353 -		ASN_DEBUG("Element %s %s %s->%s is %s",
 42.1354 -			elm->flags & ATF_POINTER ? "ptr" : "inline",
 42.1355 -			elm->default_value ? "def" : "wtv",
 42.1356 -			td->name, elm->name, present ? "present" : "absent");
 42.1357 -		if(per_put_few_bits(po, present, 1))
 42.1358 -			_ASN_ENCODE_FAILED;
 42.1359 -	}
 42.1360 -
 42.1361 -	/*
 42.1362 -	 * Encode the sequence ROOT elements.
 42.1363 -	 */
 42.1364 -	ASN_DEBUG("ext_after = %d, ec = %d, eb = %d", specs->ext_after, td->elements_count, specs->ext_before);
 42.1365 -	for(edx = 0; edx < ((specs->ext_after < 0)
 42.1366 -		? td->elements_count : specs->ext_before - 1); edx++) {
 42.1367 -
 42.1368 -		asn_TYPE_member_t *elm = &td->elements[edx];
 42.1369 -		void *memb_ptr;		/* Pointer to the member */
 42.1370 -		void **memb_ptr2;	/* Pointer to that pointer */
 42.1371 -
 42.1372 -		if(IN_EXTENSION_GROUP(specs, edx))
 42.1373 -			continue;
 42.1374 -
 42.1375 -		ASN_DEBUG("About to encode %s", elm->type->name);
 42.1376 -
 42.1377 -		/* Fetch the pointer to this member */
 42.1378 -		if(elm->flags & ATF_POINTER) {
 42.1379 -			memb_ptr2 = (void **)((char *)sptr + elm->memb_offset);
 42.1380 -			if(!*memb_ptr2) {
 42.1381 -				ASN_DEBUG("Element %s %d not present",
 42.1382 -					elm->name, edx);
 42.1383 -				if(elm->optional)
 42.1384 -					continue;
 42.1385 -				/* Mandatory element is missing */
 42.1386 -				_ASN_ENCODE_FAILED;
 42.1387 -			}
 42.1388 -		} else {
 42.1389 -			memb_ptr = (void *)((char *)sptr + elm->memb_offset);
 42.1390 -			memb_ptr2 = &memb_ptr;
 42.1391 -		}
 42.1392 -
 42.1393 -		/* Eliminate default values */
 42.1394 -		if(elm->default_value && elm->default_value(0, memb_ptr2) == 1)
 42.1395 -			continue;
 42.1396 -
 42.1397 -		ASN_DEBUG("Encoding %s->%s", td->name, elm->name);
 42.1398 -		er = elm->type->uper_encoder(elm->type, elm->per_constraints,
 42.1399 -			*memb_ptr2, po);
 42.1400 -		if(er.encoded == -