Unused code. Don't know what it's for.
OCTET_STRING_decode_ber,
OCTET_STRING_encode_der,
ANY_encode_xer,
- 0, /* Random fill is not defined for ANY type */
0 /* Use generic outmost tag fetcher */
};
asn_TYPE_descriptor_t asn_DEF_ANY = {
OCTET_STRING_decode_ber, /* Implemented in terms of OCTET STRING */
OCTET_STRING_encode_der, /* Implemented in terms of OCTET STRING */
BIT_STRING_encode_xer,
- BIT_STRING_random_fill,
0 /* Use generic outmost tag fetcher */
};
asn_TYPE_descriptor_t asn_DEF_BIT_STRING = {
return 1;
}
}
-
-asn_random_fill_result_t
-BIT_STRING_random_fill(const asn_TYPE_descriptor_t *td, void **sptr,
- const asn_encoding_constraints_t *constraints,
- size_t max_length) {
- const asn_OCTET_STRING_specifics_t *specs =
- td->specifics ? (const asn_OCTET_STRING_specifics_t *)td->specifics
- : &asn_SPC_BIT_STRING_specs;
- asn_random_fill_result_t result_ok = {ARFILL_OK, 1};
- asn_random_fill_result_t result_failed = {ARFILL_FAILED, 0};
- asn_random_fill_result_t result_skipped = {ARFILL_SKIPPED, 0};
- static unsigned lengths[] = {0, 1, 2, 3, 4, 8,
- 126, 127, 128, 16383, 16384, 16385,
- 65534, 65535, 65536, 65537};
- uint8_t *buf;
- uint8_t *bend;
- uint8_t *b;
- size_t rnd_bits, rnd_len;
- BIT_STRING_t *st;
-
- if(max_length == 0) return result_skipped;
-
- switch(specs->subvariant) {
- case ASN_OSUBV_ANY:
- return result_failed;
- case ASN_OSUBV_BIT:
- break;
- default:
- break;
- }
-
- /* Figure out how far we should go */
- rnd_bits = lengths[asn_random_between(
- 0, sizeof(lengths) / sizeof(lengths[0]) - 1)];
- if(rnd_bits >= max_length) {
- rnd_bits = asn_random_between(0, max_length - 1);
- }
-
- rnd_len = (rnd_bits + 7) / 8;
- buf = CALLOC(1, rnd_len + 1);
- if(!buf) return result_failed;
-
- bend = &buf[rnd_len];
-
- for(b = buf; b < bend; b++) {
- *(uint8_t *)b = asn_random_between(0, 255);
- }
- *b = 0; /* Zero-terminate just in case. */
-
- if(*sptr) {
- st = *sptr;
- FREEMEM(st->buf);
- } else {
- st = (BIT_STRING_t *)(*sptr = CALLOC(1, specs->struct_size));
- if(!st) {
- FREEMEM(buf);
- return result_failed;
- }
- }
-
- st->buf = buf;
- st->size = rnd_len;
- st->bits_unused = (8 - (rnd_bits & 0x7)) & 0x7;
- if(st->bits_unused) {
- assert(st->size > 0);
- st->buf[st->size-1] &= 0xff << st->bits_unused;
- }
-
- result_ok.length = st->size;
- return result_ok;
-}
asn_struct_compare_f BIT_STRING_compare;
asn_constr_check_f BIT_STRING_constraint;
xer_type_encoder_f BIT_STRING_encode_xer;
-asn_random_fill_f BIT_STRING_random_fill;
#define BIT_STRING_free OCTET_STRING_free
#define BIT_STRING_decode_ber OCTET_STRING_decode_ber
BOOLEAN_decode_ber,
BOOLEAN_encode_der,
BOOLEAN_encode_xer,
- BOOLEAN_random_fill,
0 /* Use generic outmost tag fetcher */
};
asn_TYPE_descriptor_t asn_DEF_BOOLEAN = {
return 1;
}
}
-
-asn_random_fill_result_t
-BOOLEAN_random_fill(const asn_TYPE_descriptor_t *td, void **sptr,
- const asn_encoding_constraints_t *constraints,
- size_t max_length) {
- asn_random_fill_result_t result_ok = {ARFILL_OK, 1};
- asn_random_fill_result_t result_failed = {ARFILL_FAILED, 0};
- asn_random_fill_result_t result_skipped = {ARFILL_SKIPPED, 0};
- BOOLEAN_t *st = *sptr;
-
- if(max_length == 0) return result_skipped;
-
- if(st == NULL) {
- st = (BOOLEAN_t *)(*sptr = CALLOC(1, sizeof(*st)));
- if(st == NULL) {
- return result_failed;
- }
- }
-
- /* Simulate booleans that are sloppily set and biased. */
- switch(asn_random_between(0, 7)) {
- case 0:
- case 1:
- case 2:
- *st = 0; break;
- case 3: *st = -1; break;
- case 4: *st = 1; break;
- case 5: *st = INT_MIN; break;
- case 6: *st = INT_MAX; break;
- default:
- *st = asn_random_between(INT_MIN, INT_MAX);
- break;
- }
- return result_ok;
-}
ber_type_decoder_f BOOLEAN_decode_ber;
der_type_encoder_f BOOLEAN_encode_der;
xer_type_encoder_f BOOLEAN_encode_xer;
-asn_random_fill_f BOOLEAN_random_fill;
#define BOOLEAN_constraint asn_generic_no_constraint
OCTET_STRING_decode_ber, /* Implemented in terms of OCTET STRING */
GeneralizedTime_encode_der,
GeneralizedTime_encode_xer,
- GeneralizedTime_random_fill,
0 /* Use generic outmost tag fetcher */
};
asn_TYPE_descriptor_t asn_DEF_GeneralizedTime = {
return opt_gt;
}
-asn_random_fill_result_t
-GeneralizedTime_random_fill(const asn_TYPE_descriptor_t *td, void **sptr,
- const asn_encoding_constraints_t *constraints,
- size_t max_length) {
- asn_random_fill_result_t result_ok = {ARFILL_OK, 1};
- asn_random_fill_result_t result_failed = {ARFILL_FAILED, 0};
- asn_random_fill_result_t result_skipped = {ARFILL_SKIPPED, 0};
- static const char *values[] = {
- "19700101000000", "19700101000000-0000", "19700101000000+0000",
- "19700101000000Z", "19700101000000.3Z", "19821106210623.3",
- "19821106210629.3Z", "19691106210827.3-0500", "19821106210629.456",
- };
- size_t rnd = asn_random_between(0, sizeof(values)/sizeof(values[0])-1);
-
- (void)constraints;
-
- if(max_length < sizeof("yyyymmddhhmmss") && !*sptr) {
- return result_skipped;
- }
-
- if(*sptr) {
- if(OCTET_STRING_fromBuf(*sptr, values[rnd], -1) != 0) {
- if(!sptr) return result_failed;
- }
- } else {
- *sptr = OCTET_STRING_new_fromBuf(td, values[rnd], -1);
- if(!sptr) return result_failed;
- }
-
- return result_ok;
-}
-
int
GeneralizedTime_compare(const asn_TYPE_descriptor_t *td, const void *aptr,
const void *bptr) {
asn_constr_check_f GeneralizedTime_constraint;
der_type_encoder_f GeneralizedTime_encode_der;
xer_type_encoder_f GeneralizedTime_encode_xer;
-asn_random_fill_f GeneralizedTime_random_fill;
#define GeneralizedTime_free OCTET_STRING_free
#define GeneralizedTime_decode_ber OCTET_STRING_decode_ber
OCTET_STRING_decode_ber, /* Implemented in terms of OCTET STRING */
OCTET_STRING_encode_der,
OCTET_STRING_encode_xer_utf8,
- OCTET_STRING_random_fill,
0 /* Use generic outmost tag fetcher */
};
asn_TYPE_descriptor_t asn_DEF_IA5String = {
ber_decode_primitive,
INTEGER_encode_der,
INTEGER_encode_xer,
- INTEGER_random_fill,
0 /* Use generic outmost tag fetcher */
};
asn_TYPE_descriptor_t asn_DEF_INTEGER = {
}
}
-
-asn_random_fill_result_t
-INTEGER_random_fill(const asn_TYPE_descriptor_t *td, void **sptr,
- const asn_encoding_constraints_t *constraints,
- size_t max_length) {
- const asn_INTEGER_specifics_t *specs =
- (const asn_INTEGER_specifics_t *)td->specifics;
- asn_random_fill_result_t result_ok = {ARFILL_OK, 1};
- asn_random_fill_result_t result_failed = {ARFILL_FAILED, 0};
- asn_random_fill_result_t result_skipped = {ARFILL_SKIPPED, 0};
- INTEGER_t *st = *sptr;
- const asn_INTEGER_enum_map_t *emap;
- size_t emap_len;
- intmax_t value;
- int find_inside_map;
-
- if(max_length == 0) return result_skipped;
-
- if(st == NULL) {
- st = (INTEGER_t *)CALLOC(1, sizeof(*st));
- if(st == NULL) {
- return result_failed;
- }
- }
-
- if(specs) {
- emap = specs->value2enum;
- emap_len = specs->map_count;
- if(specs->strict_enumeration) {
- find_inside_map = emap_len > 0;
- } else {
- find_inside_map = emap_len ? asn_random_between(0, 1) : 0;
- }
- } else {
- emap = 0;
- emap_len = 0;
- find_inside_map = 0;
- }
-
- if(find_inside_map) {
- assert(emap_len > 0);
- value = emap[asn_random_between(0, emap_len - 1)].nat_value;
- } else {
- static const long variants[] = {
- -65536, -65535, -65534, -32769, -32768, -32767, -16385, -16384,
- -16383, -257, -256, -255, -254, -129, -128, -127,
- -126, -1, 0, 1, 126, 127, 128, 129,
- 254, 255, 256, 257, 16383, 16384, 16385, 32767,
- 32768, 32769, 65534, 65535, 65536, 65537};
- if(specs && specs->field_unsigned) {
- assert(variants[18] == 0);
- value = variants[asn_random_between(
- 18, sizeof(variants) / sizeof(variants[0]) - 1)];
- } else {
- value = variants[asn_random_between(
- 0, sizeof(variants) / sizeof(variants[0]) - 1)];
- }
- }
-
- if(asn_imax2INTEGER(st, value)) {
- if(st == *sptr) {
- ASN_STRUCT_RESET(*td, st);
- } else {
- ASN_STRUCT_FREE(*td, st);
- }
- return result_failed;
- } else {
- *sptr = st;
- result_ok.length = st->size;
- return result_ok;
- }
-}
asn_struct_compare_f INTEGER_compare;
der_type_encoder_f INTEGER_encode_der;
xer_type_encoder_f INTEGER_encode_xer;
-asn_random_fill_f INTEGER_random_fill;
/***********************************
* Some handy conversion routines. *
ASN_MODULE_HDRS+=asn1/asn1c/asn_codecs.h
ASN_MODULE_HDRS+=asn1/asn1c/asn_internal.h
ASN_MODULE_SRCS+=asn1/asn1c/asn_internal.c
-ASN_MODULE_HDRS+=asn1/asn1c/asn_random_fill.h
-ASN_MODULE_SRCS+=asn1/asn1c/asn_random_fill.c
ASN_MODULE_SRCS+=asn1/asn1c/OCTET_STRING.c
ASN_MODULE_HDRS+=asn1/asn1c/BIT_STRING.h
ASN_MODULE_SRCS+=asn1/asn1c/BIT_STRING.c
NULL_decode_ber,
NULL_encode_der, /* Special handling of DER encoding */
NULL_encode_xer,
- NULL_random_fill,
0 /* Use generic outmost tag fetcher */
};
asn_TYPE_descriptor_t asn_DEF_NULL = {
return (cb("<absent>", 8, app_key) < 0) ? -1 : 0;
}
}
-
-asn_random_fill_result_t
-NULL_random_fill(const asn_TYPE_descriptor_t *td, void **sptr,
- const asn_encoding_constraints_t *constr,
- size_t max_length) {
- asn_random_fill_result_t result_ok = {ARFILL_OK, 1};
- asn_random_fill_result_t result_failed = {ARFILL_FAILED, 0};
- asn_random_fill_result_t result_skipped = {ARFILL_SKIPPED, 0};
- NULL_t *st = *sptr;
-
- (void)td;
- (void)constr;
-
- if(max_length == 0) return result_skipped;
-
- if(st == NULL) {
- st = (NULL_t *)(*sptr = CALLOC(1, sizeof(*st)));
- if(st == NULL) {
- return result_failed;
- }
- }
-
- return result_ok;
-}
ber_type_decoder_f NULL_decode_ber;
der_type_encoder_f NULL_encode_der;
xer_type_encoder_f NULL_encode_xer;
-asn_random_fill_f NULL_random_fill;
#define NULL_constraint asn_generic_no_constraint
ber_decode_primitive,
der_encode_primitive,
OBJECT_IDENTIFIER_encode_xer,
- OBJECT_IDENTIFIER_random_fill,
0 /* Use generic outmost tag fetcher */
};
asn_TYPE_descriptor_t asn_DEF_OBJECT_IDENTIFIER = {
errno = EINVAL; /* Broken OID */
return -1;
}
-
-/*
- * Generate values from the list of interesting values, or just a random
- * value up to the upper limit.
- */
-static asn_oid_arc_t
-OBJECT_IDENTIFIER__biased_random_arc(asn_oid_arc_t upper_bound) {
- const asn_oid_arc_t values[] = {0, 1, 127, 128, 129, 254, 255, 256};
- size_t idx;
-
- switch(asn_random_between(0, 2)) {
- case 0:
- idx = asn_random_between(0, sizeof(values) / sizeof(values[0]) - 1);
- if(values[idx] < upper_bound) {
- return values[idx];
- }
- /* Fall through */
- case 1:
- return asn_random_between(0, upper_bound);
- case 2:
- default:
- return upper_bound;
- }
-}
-
-asn_random_fill_result_t
-OBJECT_IDENTIFIER_random_fill(const asn_TYPE_descriptor_t *td, void **sptr,
- const asn_encoding_constraints_t *constraints,
- size_t max_length) {
- asn_random_fill_result_t result_ok = {ARFILL_OK, 1};
- asn_random_fill_result_t result_failed = {ARFILL_FAILED, 0};
- asn_random_fill_result_t result_skipped = {ARFILL_SKIPPED, 0};
- OBJECT_IDENTIFIER_t *st;
- asn_oid_arc_t arcs[5];
- size_t arcs_len = asn_random_between(2, 5);
- size_t i;
-
- (void)constraints;
-
- if(max_length < arcs_len) return result_skipped;
-
- if(*sptr) {
- st = *sptr;
- } else {
- st = CALLOC(1, sizeof(*st));
- }
-
- arcs[0] = asn_random_between(0, 2);
- arcs[1] = OBJECT_IDENTIFIER__biased_random_arc(
- arcs[0] <= 1 ? 39 : (ASN_OID_ARC_MAX - 80));
- for(i = 2; i < arcs_len; i++) {
- arcs[i] = OBJECT_IDENTIFIER__biased_random_arc(ASN_OID_ARC_MAX);
- }
-
- if(OBJECT_IDENTIFIER_set_arcs(st, arcs, arcs_len)) {
- if(st != *sptr) {
- ASN_STRUCT_FREE(*td, st);
- }
- return result_failed;
- }
-
- *sptr = st;
-
- result_ok.length = st->size;
- return result_ok;
-}
asn_constr_check_f OBJECT_IDENTIFIER_constraint;
der_type_encoder_f OBJECT_IDENTIFIER_encode_der;
xer_type_encoder_f OBJECT_IDENTIFIER_encode_xer;
-asn_random_fill_f OBJECT_IDENTIFIER_random_fill;
#define OBJECT_IDENTIFIER_free ASN__PRIMITIVE_TYPE_free
#define OBJECT_IDENTIFIER_compare OCTET_STRING_compare
OCTET_STRING_decode_ber,
OCTET_STRING_encode_der,
OCTET_STRING_encode_xer,
- OCTET_STRING_random_fill,
0 /* Use generic outmost tag fetcher */
};
asn_TYPE_descriptor_t asn_DEF_OCTET_STRING = {
}
}
-
-/*
- * Biased function for randomizing character values around their limits.
- */
-static uint32_t
-OCTET_STRING__random_char(unsigned long lb, unsigned long ub) {
- assert(lb <= ub);
- switch(asn_random_between(0, 16)) {
- case 0:
- if(lb < ub) return lb + 1;
- /* Fall through */
- case 1:
- return lb;
- case 2:
- if(lb < ub) return ub - 1;
- /* Fall through */
- case 3:
- return ub;
- default:
- return asn_random_between(lb, ub);
- }
-}
-
-
-size_t
-OCTET_STRING_random_length_constrained(
- const asn_TYPE_descriptor_t *td,
- const asn_encoding_constraints_t *constraints, size_t max_length) {
- const unsigned lengths[] = {0, 1, 2, 3, 4, 8,
- 126, 127, 128, 16383, 16384, 16385,
- 65534, 65535, 65536, 65537};
- size_t rnd_len;
-
- /* Figure out how far we should go */
- rnd_len = lengths[asn_random_between(
- 0, sizeof(lengths) / sizeof(lengths[0]) - 1)];
-
- if(rnd_len > max_length) {
- rnd_len = asn_random_between(0, max_length);
- }
-
- return rnd_len;
-}
-
-asn_random_fill_result_t
-OCTET_STRING_random_fill(const asn_TYPE_descriptor_t *td, void **sptr,
- const asn_encoding_constraints_t *constraints,
- size_t max_length) {
- const asn_OCTET_STRING_specifics_t *specs = td->specifics
- ? (const asn_OCTET_STRING_specifics_t *)td->specifics
- : &asn_SPC_OCTET_STRING_specs;
- asn_random_fill_result_t result_ok = {ARFILL_OK, 1};
- asn_random_fill_result_t result_failed = {ARFILL_FAILED, 0};
- asn_random_fill_result_t result_skipped = {ARFILL_SKIPPED, 0};
- unsigned int unit_bytes = 1;
- unsigned long clb = 0; /* Lower bound on char */
- unsigned long cub = 255; /* Higher bound on char value */
- uint8_t *buf;
- uint8_t *bend;
- uint8_t *b;
- size_t rnd_len;
- OCTET_STRING_t *st;
-
- if(max_length == 0 && !*sptr) return result_skipped;
-
- switch(specs->subvariant) {
- default:
- case ASN_OSUBV_ANY:
- return result_failed;
- case ASN_OSUBV_BIT:
- /* Handled by BIT_STRING itself. */
- return result_failed;
- case ASN_OSUBV_STR:
- unit_bytes = 1;
- clb = 0;
- cub = 255;
- break;
- case ASN_OSUBV_U16:
- unit_bytes = 2;
- clb = 0;
- cub = 65535;
- break;
- case ASN_OSUBV_U32:
- unit_bytes = 4;
- clb = 0;
- cub = 0x10FFFF;
- break;
- }
-
- if(!constraints)
- constraints = &td->encoding_constraints;
-
- rnd_len =
- OCTET_STRING_random_length_constrained(td, constraints, max_length);
-
- buf = CALLOC(unit_bytes, rnd_len + 1);
- if(!buf) return result_failed;
-
- bend = &buf[unit_bytes * rnd_len];
-
- switch(unit_bytes) {
- case 1:
- for(b = buf; b < bend; b += unit_bytes) {
- *(uint8_t *)b = OCTET_STRING__random_char(clb, cub);
- }
- *(uint8_t *)b = 0;
- break;
- case 2:
- for(b = buf; b < bend; b += unit_bytes) {
- uint32_t code = OCTET_STRING__random_char(clb, cub);
- b[0] = code >> 8;
- b[1] = code;
- }
- *(uint16_t *)b = 0;
- break;
- case 4:
- for(b = buf; b < bend; b += unit_bytes) {
- uint32_t code = OCTET_STRING__random_char(clb, cub);
- b[0] = code >> 24;
- b[1] = code >> 16;
- b[2] = code >> 8;
- b[3] = code;
- }
- *(uint32_t *)b = 0;
- break;
- }
-
- if(*sptr) {
- st = *sptr;
- FREEMEM(st->buf);
- } else {
- st = (OCTET_STRING_t *)(*sptr = CALLOC(1, specs->struct_size));
- if(!st) {
- FREEMEM(buf);
- return result_failed;
- }
- }
-
- st->buf = buf;
- st->size = unit_bytes * rnd_len;
-
- result_ok.length = st->size;
- return result_ok;
-}
der_type_encoder_f OCTET_STRING_encode_der;
xer_type_encoder_f OCTET_STRING_encode_xer;
xer_type_encoder_f OCTET_STRING_encode_xer_utf8;
-asn_random_fill_f OCTET_STRING_random_fill;
#define OCTET_STRING_constraint asn_generic_no_constraint
extern asn_OCTET_STRING_specifics_t asn_SPC_OCTET_STRING_specs;
-size_t OCTET_STRING_random_length_constrained(
- const asn_TYPE_descriptor_t *, const asn_encoding_constraints_t *,
- size_t max_length);
-
#endif /* _OCTET_STRING_H_ */
OPEN_TYPE_decode_ber,
OPEN_TYPE_encode_der,
OPEN_TYPE_encode_xer,
- 0, /* Random fill is not supported for open type */
0, /* Use generic outmost tag fetcher */
};
OCTET_STRING_decode_ber, /* Implemented in terms of OCTET STRING */
OCTET_STRING_encode_der, /* Implemented in terms of OCTET STRING */
UTCTime_encode_xer,
- UTCTime_random_fill,
0 /* Use generic outmost tag fetcher */
};
asn_TYPE_descriptor_t asn_DEF_UTCTime = {
return (UTCTime_t *)gt;
}
-
-asn_random_fill_result_t
-UTCTime_random_fill(const asn_TYPE_descriptor_t *td, void **sptr,
- const asn_encoding_constraints_t *constraints,
- size_t max_length) {
- asn_random_fill_result_t result_ok = {ARFILL_OK, 1};
- asn_random_fill_result_t result_failed = {ARFILL_FAILED, 0};
- asn_random_fill_result_t result_skipped = {ARFILL_SKIPPED, 0};
- static const char *values[] = {
- "700101000000", "700101000000-0000", "700101000000+0000",
- "700101000000Z", "821106210623", "691106210827-0500",
- "821106210629Z",
- };
- size_t rnd = asn_random_between(0, sizeof(values)/sizeof(values[0])-1);
-
- (void)constraints;
-
- if(max_length < sizeof("yymmddhhmmss") && !*sptr) {
- return result_skipped;
- }
-
- if(*sptr) {
- if(OCTET_STRING_fromBuf(*sptr, values[rnd], -1) != 0) {
- if(!sptr) return result_failed;
- }
- } else {
- *sptr = OCTET_STRING_new_fromBuf(td, values[rnd], -1);
- if(!sptr) return result_failed;
- }
-
- return result_ok;
-}
-
int
UTCTime_compare(const asn_TYPE_descriptor_t *td, const void *aptr,
const void *bptr) {
asn_struct_compare_f UTCTime_compare;
asn_constr_check_f UTCTime_constraint;
xer_type_encoder_f UTCTime_encode_xer;
-asn_random_fill_f UTCTime_random_fill;
#define UTCTime_free OCTET_STRING_free
#define UTCTime_decode_ber OCTET_STRING_decode_ber
}
break;
- case ATS_RANDOM:
- errno = ENOENT; /* Randomization doesn't make sense on output. */
- ASN__ENCODE_FAILED;
-
case ATS_BER:
/* BER is a superset of DER. */
/* Fall through. */
errno = ENOENT;
ASN__DECODE_FAILED;
- case ATS_RANDOM:
- if(!td->op->random_fill) {
- ASN__DECODE_FAILED;
- } else {
- if(asn_random_fill(td, sptr, 16000) == 0) {
- asn_dec_rval_t ret = {RC_OK, 0};
- return ret;
- } else {
- ASN__DECODE_FAILED;
- }
- }
- break;
-
case ATS_DER:
case ATS_BER:
return ber_decode(opt_codec_ctx, td, sptr, buffer, size);
ATS_INVALID = 0,
/* Plaintext output (not conforming to any standard), for debugging. */
ATS_NONSTANDARD_PLAINTEXT,
- /* Returns a randomly generatede structure. */
- ATS_RANDOM,
/*
* X.690:
* BER: Basic Encoding Rules.
+++ /dev/null
-/*
- * Copyright (c) 2017 Lev Walkin <vlm@lionet.info>.
- * All rights reserved.
- * Redistribution and modifications are permitted subject to BSD license.
- */
-
-#include <assert.h>
-
-#include "asn1/asn1c/asn_internal.h"
-#include "asn1/asn1c/asn_random_fill.h"
-#include "asn1/asn1c/constr_TYPE.h"
-
-int
-asn_random_fill(const struct asn_TYPE_descriptor_s *td, void **struct_ptr,
- size_t length) {
-
- if(td && td->op->random_fill) {
- asn_random_fill_result_t res =
- td->op->random_fill(td, struct_ptr, 0, length);
- return (res.code == ARFILL_OK) ? 0 : -1;
- } else {
- return -1;
- }
-}
-
-static uintmax_t
-asn__intmax_range(intmax_t lb, intmax_t ub) {
- assert(lb <= ub);
- if((ub < 0) == (lb < 0)) {
- return ub - lb;
- } else if(lb < 0) {
- return 1 + ((uintmax_t)ub + (uintmax_t)-(lb + 1));
- } else {
- assert(!"Unreachable");
- return 0;
- }
-}
-
-intmax_t
-asn_random_between(intmax_t lb, intmax_t rb) {
- if(lb == rb) {
- return lb;
- } else {
- const uintmax_t intmax_max = ((~(uintmax_t)0) >> 1);
- uintmax_t range = asn__intmax_range(lb, rb);
- uintmax_t value = 0;
- uintmax_t got_entropy = 0;
-
- assert(RAND_MAX > 0xffffff); /* Seen 7ffffffd! */
- assert(range < intmax_max);
-
- for(; got_entropy < range;) {
- got_entropy = (got_entropy << 24) | 0xffffff;
- value = (value << 24) | (random() % 0xffffff);
- }
-
- return lb + (intmax_t)(value % (range + 1));
- }
-}
+++ /dev/null
-/*
- * Copyright (c) 2017 Lev Walkin <vlm@lionet.info>. All rights reserved.
- * Redistribution and modifications are permitted subject to BSD license.
- */
-#ifndef ASN_RANDOM_FILL
-#define ASN_RANDOM_FILL
-
-#include <stddef.h>
-#include <stdio.h>
-#include <stdlib.h>
-#include <string.h>
-#include <strings.h>
-#include <sys/types.h>
-#include <unistd.h>
-
-/* Forward declarations */
-struct asn_TYPE_descriptor_s;
-struct asn_encoding_constraints_s;
-
-/*
- * Initialize a structure with random data according to the type specification
- * and optional member constraints.
- * ARGUMENTS:
- * (max_length) - See (approx_max_length_limit).
- * (memb_constraints) - Member constraints, if exist.
- * In case the return differs from ARFILL_OK, the (struct_ptr) contents
- * and (current_length) value remain in their original state.
- */
-typedef struct asn_random_fill_result_s {
- enum {
- ARFILL_FAILED = -1, /* System error (memory?) */
- ARFILL_OK = 0, /* Initialization succeeded */
- ARFILL_SKIPPED = 1 /* Not done due to (length?) constraint */
- } code;
- size_t length; /* Approximate number of bytes created. */
-} asn_random_fill_result_t;
-typedef asn_random_fill_result_t(asn_random_fill_f)(
- const struct asn_TYPE_descriptor_s *td, void **struct_ptr,
- const struct asn_encoding_constraints_s *memb_constraints,
- size_t max_length);
-
-/*
- * Returns 0 if the structure was properly initialized, -1 otherwise.
- * The (approx_max_length_limit) specifies the approximate limit of the
- * resulting structure in units closely resembling bytes. The actual result
- * might be several times larger or smaller than the length limit.
- */
-int asn_random_fill(const struct asn_TYPE_descriptor_s *td, void **struct_ptr,
- size_t approx_max_length_limit);
-
-/*
- * Returns a random number between min and max.
- */
-intmax_t asn_random_between(intmax_t min, intmax_t max);
-
-#endif /* ASN_RANDOM_FILL */
return 0;
}
-
-asn_random_fill_result_t
-CHOICE_random_fill(const asn_TYPE_descriptor_t *td, void **sptr,
- const asn_encoding_constraints_t *constr,
- size_t max_length) {
- const asn_CHOICE_specifics_t *specs =
- (const asn_CHOICE_specifics_t *)td->specifics;
- asn_random_fill_result_t res;
- asn_random_fill_result_t result_failed = {ARFILL_FAILED, 0};
- asn_random_fill_result_t result_skipped = {ARFILL_SKIPPED, 0};
- const asn_TYPE_member_t *elm;
- unsigned present;
- void *memb_ptr; /* Pointer to the member */
- void **memb_ptr2; /* Pointer to that pointer */
- void *st = *sptr;
-
- if(max_length == 0) return result_skipped;
-
- (void)constr;
-
- if(st == NULL) {
- st = CALLOC(1, specs->struct_size);
- if(st == NULL) {
- return result_failed;
- }
- }
-
- present = asn_random_between(1, td->elements_count);
- elm = &td->elements[present - 1];
-
- if(elm->flags & ATF_POINTER) {
- /* Member is a pointer to another structure */
- memb_ptr2 = (void **)((char *)st + elm->memb_offset);
- } else {
- memb_ptr = (char *)st + elm->memb_offset;
- memb_ptr2 = &memb_ptr;
- }
-
- res = elm->type->op->random_fill(elm->type, memb_ptr2,
- &elm->encoding_constraints, max_length);
- _set_present_idx(st, specs->pres_offset, specs->pres_size, present);
- if(res.code == ARFILL_OK) {
- *sptr = st;
- } else {
- if(st == *sptr) {
- ASN_STRUCT_RESET(*td, st);
- } else {
- ASN_STRUCT_FREE(*td, st);
- }
- }
-
- return res;
-}
-
-
asn_TYPE_operation_t asn_OP_CHOICE = {
CHOICE_free,
CHOICE_print,
CHOICE_decode_ber,
CHOICE_encode_der,
CHOICE_encode_xer,
- CHOICE_random_fill,
CHOICE_outmost_tag
};
der_type_encoder_f CHOICE_encode_der;
xer_type_encoder_f CHOICE_encode_xer;
asn_outmost_tag_f CHOICE_outmost_tag;
-asn_random_fill_f CHOICE_random_fill;
extern asn_TYPE_operation_t asn_OP_CHOICE;
/*
SEQUENCE_decode_ber,
SEQUENCE_encode_der,
SEQUENCE_encode_xer,
- SEQUENCE_random_fill,
0 /* Use generic outmost tag fetcher */
};
-
-
-asn_random_fill_result_t
-SEQUENCE_random_fill(const asn_TYPE_descriptor_t *td, void **sptr,
- const asn_encoding_constraints_t *constr,
- size_t max_length) {
- const asn_SEQUENCE_specifics_t *specs =
- (const asn_SEQUENCE_specifics_t *)td->specifics;
- asn_random_fill_result_t result_ok = {ARFILL_OK, 0};
- asn_random_fill_result_t result_failed = {ARFILL_FAILED, 0};
- asn_random_fill_result_t result_skipped = {ARFILL_SKIPPED, 0};
- void *st = *sptr;
- size_t edx;
-
- if(max_length == 0) return result_skipped;
-
- (void)constr;
-
- if(st == NULL) {
- st = CALLOC(1, specs->struct_size);
- if(st == NULL) {
- return result_failed;
- }
- }
-
- for(edx = 0; edx < td->elements_count; edx++) {
- const asn_TYPE_member_t *elm = &td->elements[edx];
- void *memb_ptr; /* Pointer to the member */
- void **memb_ptr2; /* Pointer to that pointer */
- asn_random_fill_result_t tmpres;
-
- if(elm->optional && asn_random_between(0, 4) == 2) {
- /* Sometimes decide not to fill the optional value */
- continue;
- }
-
- if(elm->flags & ATF_POINTER) {
- /* Member is a pointer to another structure */
- memb_ptr2 = (void **)((char *)st + elm->memb_offset);
- } else {
- memb_ptr = (char *)st + elm->memb_offset;
- memb_ptr2 = &memb_ptr;
- }
-
- tmpres = elm->type->op->random_fill(
- elm->type, memb_ptr2, &elm->encoding_constraints,
- max_length > result_ok.length ? max_length - result_ok.length : 0);
- switch(tmpres.code) {
- case ARFILL_OK:
- result_ok.length += tmpres.length;
- continue;
- case ARFILL_SKIPPED:
- assert(!(elm->flags & ATF_POINTER) || *memb_ptr2 == NULL);
- continue;
- case ARFILL_FAILED:
- if(st == *sptr) {
- ASN_STRUCT_RESET(*td, st);
- } else {
- ASN_STRUCT_FREE(*td, st);
- }
- return tmpres;
- }
- }
-
- *sptr = st;
-
- return result_ok;
-}
ber_type_decoder_f SEQUENCE_decode_ber;
der_type_encoder_f SEQUENCE_encode_der;
xer_type_encoder_f SEQUENCE_encode_xer;
-asn_random_fill_f SEQUENCE_random_fill;
extern asn_TYPE_operation_t asn_OP_SEQUENCE;
#endif /* _CONSTR_SEQUENCE_H_ */
SEQUENCE_OF_decode_ber,
SEQUENCE_OF_encode_der,
SEQUENCE_OF_encode_xer,
- SEQUENCE_OF_random_fill,
0 /* Use generic outmost tag fetcher */
};
#define SEQUENCE_OF_print SET_OF_print
#define SEQUENCE_OF_constraint SET_OF_constraint
#define SEQUENCE_OF_decode_ber SET_OF_decode_ber
-#define SEQUENCE_OF_random_fill SET_OF_random_fill
#endif /* _CONSTR_SET_OF_H_ */
SET_OF_decode_ber,
SET_OF_encode_der,
SET_OF_encode_xer,
- SET_OF_random_fill,
0 /* Use generic outmost tag fetcher */
};
-
-
-asn_random_fill_result_t
-SET_OF_random_fill(const asn_TYPE_descriptor_t *td, void **sptr,
- const asn_encoding_constraints_t *constraints,
- size_t max_length) {
- const asn_SET_OF_specifics_t *specs =
- (const asn_SET_OF_specifics_t *)td->specifics;
- asn_random_fill_result_t res_ok = {ARFILL_OK, 0};
- asn_random_fill_result_t result_failed = {ARFILL_FAILED, 0};
- asn_random_fill_result_t result_skipped = {ARFILL_SKIPPED, 0};
- const asn_TYPE_member_t *elm = td->elements;
- void *st = *sptr;
- long max_elements = 5;
- long slb = 0; /* Lower size bound */
- long sub = 0; /* Upper size bound */
- size_t rnd_len;
-
- if(max_length == 0) return result_skipped;
-
- if(st == NULL) {
- st = (*sptr = CALLOC(1, specs->struct_size));
- if(st == NULL) {
- return result_failed;
- }
- }
-
- switch(asn_random_between(0, 6)) {
- case 0: max_elements = 0; break;
- case 1: max_elements = 1; break;
- case 2: max_elements = 5; break;
- case 3: max_elements = max_length; break;
- case 4: max_elements = max_length / 2; break;
- case 5: max_elements = max_length / 4; break;
- default: break;
- }
- sub = slb + max_elements;
-
- /* Bias towards edges of allowed space */
- switch(asn_random_between(-1, 4)) {
- default:
- case -1:
- case 0:
- rnd_len = asn_random_between(slb, sub);
- break;
- case 1:
- if(slb < sub) {
- rnd_len = asn_random_between(slb + 1, sub);
- break;
- }
- /* Fall through */
- case 2:
- rnd_len = asn_random_between(slb, slb);
- break;
- case 3:
- if(slb < sub) {
- rnd_len = asn_random_between(slb, sub - 1);
- break;
- }
- /* Fall through */
- case 4:
- rnd_len = asn_random_between(sub, sub);
- break;
- }
-
- for(; rnd_len > 0; rnd_len--) {
- asn_anonymous_set_ *list = _A_SET_FROM_VOID(st);
- void *ptr = 0;
- asn_random_fill_result_t tmpres = elm->type->op->random_fill(
- elm->type, &ptr, &elm->encoding_constraints,
- (max_length > res_ok.length ? max_length - res_ok.length : 0)
- / rnd_len);
- switch(tmpres.code) {
- case ARFILL_OK:
- ASN_SET_ADD(list, ptr);
- res_ok.length += tmpres.length;
- break;
- case ARFILL_SKIPPED:
- break;
- case ARFILL_FAILED:
- assert(ptr == 0);
- return tmpres;
- }
- }
-
- return res_ok;
-}
ber_type_decoder_f SET_OF_decode_ber;
der_type_encoder_f SET_OF_encode_der;
xer_type_encoder_f SET_OF_encode_xer;
-asn_random_fill_f SET_OF_random_fill;
extern asn_TYPE_operation_t asn_OP_SET_OF;
#endif /* CONSTR_SET_OF_H */
#include "asn1/asn1c/der_encoder.h" /* Distinguished Encoding Rules encoder */
#include "asn1/asn1c/xer_encoder.h" /* Encoder into XER (XML, text) */
#include "asn1/asn1c/constraints.h" /* Subtype constraints support */
-#include "asn1/asn1c/asn_random_fill.h" /* Random structures support */
/*
* Free the structure according to its specification.
ber_type_decoder_f *ber_decoder; /* Generic BER decoder */
der_type_encoder_f *der_encoder; /* Canonical DER encoder */
xer_type_encoder_f *xer_encoder; /* [Canonical] XER encoder */
- asn_random_fill_f *random_fill; /* Initialize with a random value */
asn_outmost_tag_f *outmost_tag; /* <optional, internal> */
} asn_TYPE_operation_t;