UTF8String_rfill.c
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2017 Lev Walkin <vlm@lionet.info>.
3  * All rights reserved.
4  * Redistribution and modifications are permitted subject to BSD license.
5  */
8 
9 /*
10  * Biased function for randomizing UTF-8 sequences.
11  */
12 static size_t
13 UTF8String__random_char(uint8_t *b, size_t size) {
14  static const struct rnd_value {
15  const char *value;
16  size_t size;
17  } values[] = {{"\0", 1},
18  {"\x01", 1},
19  {"\x7f", 1},
20  {"\xc2\xa2", 2},
21  {"\xe2\x82\xac", 3},
22  {"\xf0\x90\x8d\x88", 4},
23  {"\xf4\x8f\xbf\xbf", 4}};
24 
25  const struct rnd_value *v;
26  size_t max_idx = 0;
27 
28  switch(size) {
29  case 0:
30  assert(size != 0);
31  return 0;
32  case 1:
33  max_idx = 2;
34  break;
35  case 2:
36  max_idx = 3;
37  break;
38  default:
39  case 4:
40  max_idx = sizeof(values) / sizeof(values[0]) - 1;
41  break;
42  }
43 
44  v = &values[asn_random_between(0, max_idx)];
45  memcpy(b, v->value, v->size);
46  return v->size;
47 }
48 
51  const asn_encoding_constraints_t *constraints,
52  size_t max_length) {
53  asn_random_fill_result_t result_ok = {ARFILL_OK, 1};
54  asn_random_fill_result_t result_failed = {ARFILL_FAILED, 0};
55  asn_random_fill_result_t result_skipped = {ARFILL_SKIPPED, 0};
56  uint8_t *buf;
57  uint8_t *bend;
58  uint8_t *b;
59  size_t rnd_len;
60  size_t idx;
61  UTF8String_t *st;
62 
63  if(max_length == 0 && !*sptr) return result_skipped;
64 
65  /* Figure out how far we should go */
66  rnd_len = OCTET_STRING_random_length_constrained(td, constraints,
67  max_length / 4);
68 
69  buf = CALLOC(4, rnd_len + 1);
70  if(!buf) return result_failed;
71 
72  bend = &buf[4 * rnd_len];
73 
74  for(b = buf, idx = 0; idx < rnd_len; idx++) {
75  b += UTF8String__random_char(b, (bend - b));
76  }
77  *(uint8_t *)b = 0;
78 
79  if(*sptr) {
80  st = *sptr;
81  FREEMEM(st->buf);
82  } else {
83  st = (OCTET_STRING_t *)(*sptr = CALLOC(1, sizeof(UTF8String_t)));
84  if(!st) {
85  FREEMEM(buf);
86  return result_failed;
87  }
88  }
89 
90  st->buf = buf;
91  st->size = b - buf;
92 
93  assert(UTF8String_length(st) == (ssize_t)rnd_len);
94 
95  return result_ok;
96 }
CALLOC
#define CALLOC(nmemb, size)
Definition: asn_internal.h:37
OCTET_STRING::buf
uint8_t * buf
Definition: OCTET_STRING.h:15
FREEMEM
#define FREEMEM(ptr)
Definition: asn_internal.h:40
UTF8String.h
asn_TYPE_descriptor_s
Definition: constr_TYPE.h:224
asn_random_fill_result_s
Definition: asn_random_fill.h:24
asn_random_between
intmax_t asn_random_between(intmax_t min, intmax_t max)
Definition: asn_random_fill.c:38
UTF8String_length
ssize_t UTF8String_length(const UTF8String_t *st)
Definition: UTF8String.c:213
OCTET_STRING_random_length_constrained
size_t OCTET_STRING_random_length_constrained(const asn_TYPE_descriptor_t *, const asn_encoding_constraints_t *, size_t max_length)
Definition: OCTET_STRING_rfill.c:146
asn_encoding_constraints_s
Definition: constr_TYPE.h:208
OCTET_STRING
Definition: OCTET_STRING.h:14
asn_internal.h
UTF8String_random_fill
asn_random_fill_result_t UTF8String_random_fill(const asn_TYPE_descriptor_t *td, void **sptr, const asn_encoding_constraints_t *constraints, size_t max_length)
Definition: UTF8String_rfill.c:50
UTF8String__random_char
static size_t UTF8String__random_char(uint8_t *b, size_t size)
Definition: UTF8String_rfill.c:13
OCTET_STRING::size
size_t size
Definition: OCTET_STRING.h:16


etsi_its_cam_ts_coding
Author(s): Jean-Pierre Busch , Guido Küppers , Lennart Reiher
autogenerated on Sun May 18 2025 02:21:23