OCTET_STRING.c 55 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043
  1. /*-
  2. * Copyright (c) 2003-2017 Lev Walkin <vlm@lionet.info>.
  3. * All rights reserved.
  4. * Redistribution and modifications are permitted subject to BSD license.
  5. */
  6. #include <asn_internal.h>
  7. #include <OCTET_STRING.h>
  8. #include <BIT_STRING.h> /* for .bits_unused member */
  9. #include <errno.h>
  10. /*
  11. * OCTET STRING basic type description.
  12. */
  13. static const ber_tlv_tag_t asn_DEF_OCTET_STRING_tags[] = {
  14. (ASN_TAG_CLASS_UNIVERSAL | (4 << 2))
  15. };
  16. asn_OCTET_STRING_specifics_t asn_SPC_OCTET_STRING_specs = {
  17. sizeof(OCTET_STRING_t),
  18. offsetof(OCTET_STRING_t, _asn_ctx),
  19. ASN_OSUBV_STR
  20. };
  21. asn_TYPE_operation_t asn_OP_OCTET_STRING = {
  22. OCTET_STRING_free,
  23. OCTET_STRING_print, /* OCTET STRING generally means a non-ascii sequence */
  24. OCTET_STRING_compare,
  25. OCTET_STRING_decode_ber,
  26. OCTET_STRING_encode_der,
  27. OCTET_STRING_decode_xer_hex,
  28. OCTET_STRING_encode_xer,
  29. #ifdef ASN_DISABLE_OER_SUPPORT
  30. 0,
  31. 0,
  32. #else
  33. OCTET_STRING_decode_oer,
  34. OCTET_STRING_encode_oer,
  35. #endif /* ASN_DISABLE_OER_SUPPORT */
  36. #ifdef ASN_DISABLE_PER_SUPPORT
  37. 0,
  38. 0,
  39. #else
  40. OCTET_STRING_decode_uper, /* Unaligned PER decoder */
  41. OCTET_STRING_encode_uper, /* Unaligned PER encoder */
  42. #endif /* ASN_DISABLE_PER_SUPPORT */
  43. OCTET_STRING_random_fill,
  44. 0 /* Use generic outmost tag fetcher */
  45. };
  46. asn_TYPE_descriptor_t asn_DEF_OCTET_STRING = {
  47. "OCTET STRING", /* Canonical name */
  48. "OCTET_STRING", /* XML tag name */
  49. &asn_OP_OCTET_STRING,
  50. asn_DEF_OCTET_STRING_tags,
  51. sizeof(asn_DEF_OCTET_STRING_tags)
  52. / sizeof(asn_DEF_OCTET_STRING_tags[0]),
  53. asn_DEF_OCTET_STRING_tags, /* Same as above */
  54. sizeof(asn_DEF_OCTET_STRING_tags)
  55. / sizeof(asn_DEF_OCTET_STRING_tags[0]),
  56. { 0, 0, asn_generic_no_constraint },
  57. 0, 0, /* No members */
  58. &asn_SPC_OCTET_STRING_specs
  59. };
  60. #undef _CH_PHASE
  61. #undef NEXT_PHASE
  62. #undef PREV_PHASE
  63. #define _CH_PHASE(ctx, inc) do { \
  64. if(ctx->phase == 0) \
  65. ctx->context = 0; \
  66. ctx->phase += inc; \
  67. } while(0)
  68. #define NEXT_PHASE(ctx) _CH_PHASE(ctx, +1)
  69. #define PREV_PHASE(ctx) _CH_PHASE(ctx, -1)
  70. #undef ADVANCE
  71. #define ADVANCE(num_bytes) do { \
  72. size_t num = (num_bytes); \
  73. buf_ptr = ((const char *)buf_ptr) + num; \
  74. size -= num; \
  75. consumed_myself += num; \
  76. } while(0)
  77. #undef RETURN
  78. #define RETURN(_code) do { \
  79. asn_dec_rval_t tmprval; \
  80. tmprval.code = _code; \
  81. tmprval.consumed = consumed_myself; \
  82. return tmprval; \
  83. } while(0)
  84. #undef APPEND
  85. #define APPEND(bufptr, bufsize) do { \
  86. size_t _bs = (bufsize); /* Append size */ \
  87. size_t _ns = ctx->context; /* Allocated now */ \
  88. size_t _es = st->size + _bs; /* Expected size */ \
  89. /* int is really a typeof(st->size): */ \
  90. if((int)_es < 0) RETURN(RC_FAIL); \
  91. if(_ns <= _es) { \
  92. void *ptr; \
  93. /* Be nice and round to the memory allocator */ \
  94. do { _ns = _ns ? _ns << 1 : 16; } \
  95. while(_ns <= _es); \
  96. /* int is really a typeof(st->size): */ \
  97. if((int)_ns < 0) RETURN(RC_FAIL); \
  98. ptr = REALLOC(st->buf, _ns); \
  99. if(ptr) { \
  100. st->buf = (uint8_t *)ptr; \
  101. ctx->context = _ns; \
  102. } else { \
  103. RETURN(RC_FAIL); \
  104. } \
  105. ASN_DEBUG("Reallocating into %ld", (long)_ns); \
  106. } \
  107. memcpy(st->buf + st->size, bufptr, _bs); \
  108. /* Convenient nul-termination */ \
  109. st->buf[_es] = '\0'; \
  110. st->size = _es; \
  111. } while(0)
  112. /*
  113. * The main reason why ASN.1 is still alive is that too much time and effort
  114. * is necessary for learning it more or less adequately, thus creating a gut
  115. * necessity to demonstrate that aquired skill everywhere afterwards.
  116. * No, I am not going to explain what the following stuff is.
  117. */
  118. struct _stack_el {
  119. ber_tlv_len_t left; /* What's left to read (or -1) */
  120. ber_tlv_len_t got; /* What was actually processed */
  121. unsigned cont_level; /* Depth of subcontainment */
  122. int want_nulls; /* Want null "end of content" octets? */
  123. int bits_chopped; /* Flag in BIT STRING mode */
  124. ber_tlv_tag_t tag; /* For debugging purposes */
  125. struct _stack_el *prev;
  126. struct _stack_el *next;
  127. };
  128. struct _stack {
  129. struct _stack_el *tail;
  130. struct _stack_el *cur_ptr;
  131. };
  132. static struct _stack_el *
  133. OS__add_stack_el(struct _stack *st) {
  134. struct _stack_el *nel;
  135. /*
  136. * Reuse the old stack frame or allocate a new one.
  137. */
  138. if(st->cur_ptr && st->cur_ptr->next) {
  139. nel = st->cur_ptr->next;
  140. nel->bits_chopped = 0;
  141. nel->got = 0;
  142. /* Retain the nel->cont_level, it's correct. */
  143. } else {
  144. nel = (struct _stack_el *)CALLOC(1, sizeof(struct _stack_el));
  145. if(nel == NULL)
  146. return NULL;
  147. if(st->tail) {
  148. /* Increase a subcontainment depth */
  149. nel->cont_level = st->tail->cont_level + 1;
  150. st->tail->next = nel;
  151. }
  152. nel->prev = st->tail;
  153. st->tail = nel;
  154. }
  155. st->cur_ptr = nel;
  156. return nel;
  157. }
  158. static struct _stack *
  159. _new_stack(void) {
  160. return (struct _stack *)CALLOC(1, sizeof(struct _stack));
  161. }
  162. /*
  163. * Decode OCTET STRING type.
  164. */
  165. asn_dec_rval_t
  166. OCTET_STRING_decode_ber(const asn_codec_ctx_t *opt_codec_ctx,
  167. const asn_TYPE_descriptor_t *td, void **sptr,
  168. const void *buf_ptr, size_t size, int tag_mode) {
  169. const asn_OCTET_STRING_specifics_t *specs = td->specifics
  170. ? (const asn_OCTET_STRING_specifics_t *)td->specifics
  171. : &asn_SPC_OCTET_STRING_specs;
  172. BIT_STRING_t *st = (BIT_STRING_t *)*sptr;
  173. asn_dec_rval_t rval;
  174. asn_struct_ctx_t *ctx;
  175. ssize_t consumed_myself = 0;
  176. struct _stack *stck; /* Expectations stack structure */
  177. struct _stack_el *sel = 0; /* Stack element */
  178. int tlv_constr;
  179. enum asn_OS_Subvariant type_variant = specs->subvariant;
  180. ASN_DEBUG("Decoding %s as %s (frame %ld)",
  181. td->name,
  182. (type_variant == ASN_OSUBV_STR) ?
  183. "OCTET STRING" : "OS-SpecialCase",
  184. (long)size);
  185. /*
  186. * Create the string if does not exist.
  187. */
  188. if(st == NULL) {
  189. st = (BIT_STRING_t *)(*sptr = CALLOC(1, specs->struct_size));
  190. if(st == NULL) RETURN(RC_FAIL);
  191. }
  192. /* Restore parsing context */
  193. ctx = (asn_struct_ctx_t *)((char *)st + specs->ctx_offset);
  194. switch(ctx->phase) {
  195. case 0:
  196. /*
  197. * Check tags.
  198. */
  199. rval = ber_check_tags(opt_codec_ctx, td, ctx,
  200. buf_ptr, size, tag_mode, -1,
  201. &ctx->left, &tlv_constr);
  202. if(rval.code != RC_OK)
  203. return rval;
  204. if(tlv_constr) {
  205. /*
  206. * Complex operation, requires stack of expectations.
  207. */
  208. ctx->ptr = _new_stack();
  209. if(!ctx->ptr) {
  210. RETURN(RC_FAIL);
  211. }
  212. } else {
  213. /*
  214. * Jump into stackless primitive decoding.
  215. */
  216. _CH_PHASE(ctx, 3);
  217. if(type_variant == ASN_OSUBV_ANY && tag_mode != 1)
  218. APPEND(buf_ptr, rval.consumed);
  219. ADVANCE(rval.consumed);
  220. goto phase3;
  221. }
  222. NEXT_PHASE(ctx);
  223. /* Fall through */
  224. case 1:
  225. phase1:
  226. /*
  227. * Fill the stack with expectations.
  228. */
  229. stck = (struct _stack *)ctx->ptr;
  230. sel = stck->cur_ptr;
  231. do {
  232. ber_tlv_tag_t tlv_tag;
  233. ber_tlv_len_t tlv_len;
  234. ber_tlv_tag_t expected_tag;
  235. ssize_t tl, ll, tlvl;
  236. /* This one works even if (sel->left == -1) */
  237. size_t Left = ((!sel||(size_t)sel->left >= size)
  238. ?size:(size_t)sel->left);
  239. ASN_DEBUG("%p, s->l=%ld, s->wn=%ld, s->g=%ld\n", (void *)sel,
  240. (long)(sel?sel->left:0),
  241. (long)(sel?sel->want_nulls:0),
  242. (long)(sel?sel->got:0)
  243. );
  244. if(sel && sel->left <= 0 && sel->want_nulls == 0) {
  245. if(sel->prev) {
  246. struct _stack_el *prev = sel->prev;
  247. if(prev->left != -1) {
  248. if(prev->left < sel->got)
  249. RETURN(RC_FAIL);
  250. prev->left -= sel->got;
  251. }
  252. prev->got += sel->got;
  253. sel = stck->cur_ptr = prev;
  254. if(!sel) break;
  255. tlv_constr = 1;
  256. continue;
  257. } else {
  258. sel = stck->cur_ptr = 0;
  259. break; /* Nothing to wait */
  260. }
  261. }
  262. tl = ber_fetch_tag(buf_ptr, Left, &tlv_tag);
  263. ASN_DEBUG("fetch tag(size=%ld,L=%ld), %sstack, left=%ld, wn=%ld, tl=%ld",
  264. (long)size, (long)Left, sel?"":"!",
  265. (long)(sel?sel->left:0),
  266. (long)(sel?sel->want_nulls:0),
  267. (long)tl);
  268. switch(tl) {
  269. case -1: RETURN(RC_FAIL);
  270. case 0: RETURN(RC_WMORE);
  271. }
  272. tlv_constr = BER_TLV_CONSTRUCTED(buf_ptr);
  273. ll = ber_fetch_length(tlv_constr,
  274. (const char *)buf_ptr + tl,Left - tl,&tlv_len);
  275. ASN_DEBUG("Got tag=%s, tc=%d, left=%ld, tl=%ld, len=%ld, ll=%ld",
  276. ber_tlv_tag_string(tlv_tag), tlv_constr,
  277. (long)Left, (long)tl, (long)tlv_len, (long)ll);
  278. switch(ll) {
  279. case -1: RETURN(RC_FAIL);
  280. case 0: RETURN(RC_WMORE);
  281. }
  282. if(sel && sel->want_nulls
  283. && ((const uint8_t *)buf_ptr)[0] == 0
  284. && ((const uint8_t *)buf_ptr)[1] == 0)
  285. {
  286. ASN_DEBUG("Eat EOC; wn=%d--", sel->want_nulls);
  287. if(type_variant == ASN_OSUBV_ANY
  288. && (tag_mode != 1 || sel->cont_level))
  289. APPEND("\0\0", 2);
  290. ADVANCE(2);
  291. sel->got += 2;
  292. if(sel->left != -1) {
  293. sel->left -= 2; /* assert(sel->left >= 2) */
  294. }
  295. sel->want_nulls--;
  296. if(sel->want_nulls == 0) {
  297. /* Move to the next expectation */
  298. sel->left = 0;
  299. tlv_constr = 1;
  300. }
  301. continue;
  302. }
  303. /*
  304. * Set up expected tags,
  305. * depending on ASN.1 type being decoded.
  306. */
  307. switch(type_variant) {
  308. case ASN_OSUBV_BIT:
  309. /* X.690: 8.6.4.1, NOTE 2 */
  310. /* Fall through */
  311. case ASN_OSUBV_STR:
  312. default:
  313. if(sel) {
  314. unsigned level = sel->cont_level;
  315. if(level < td->all_tags_count) {
  316. expected_tag = td->all_tags[level];
  317. break;
  318. } else if(td->all_tags_count) {
  319. expected_tag = td->all_tags
  320. [td->all_tags_count - 1];
  321. break;
  322. }
  323. /* else, Fall through */
  324. }
  325. /* Fall through */
  326. case ASN_OSUBV_ANY:
  327. expected_tag = tlv_tag;
  328. break;
  329. }
  330. if(tlv_tag != expected_tag) {
  331. char buf[2][32];
  332. ber_tlv_tag_snprint(tlv_tag,
  333. buf[0], sizeof(buf[0]));
  334. ber_tlv_tag_snprint(td->tags[td->tags_count-1],
  335. buf[1], sizeof(buf[1]));
  336. ASN_DEBUG("Tag does not match expectation: %s != %s",
  337. buf[0], buf[1]);
  338. RETURN(RC_FAIL);
  339. }
  340. tlvl = tl + ll; /* Combined length of T and L encoding */
  341. if((tlv_len + tlvl) < 0) {
  342. /* tlv_len value is too big */
  343. ASN_DEBUG("TLV encoding + length (%ld) is too big",
  344. (long)tlv_len);
  345. RETURN(RC_FAIL);
  346. }
  347. /*
  348. * Append a new expectation.
  349. */
  350. sel = OS__add_stack_el(stck);
  351. if(!sel) RETURN(RC_FAIL);
  352. sel->tag = tlv_tag;
  353. sel->want_nulls = (tlv_len==-1);
  354. if(sel->prev && sel->prev->left != -1) {
  355. /* Check that the parent frame is big enough */
  356. if(sel->prev->left < tlvl + (tlv_len==-1?0:tlv_len))
  357. RETURN(RC_FAIL);
  358. if(tlv_len == -1)
  359. sel->left = sel->prev->left - tlvl;
  360. else
  361. sel->left = tlv_len;
  362. } else {
  363. sel->left = tlv_len;
  364. }
  365. if(type_variant == ASN_OSUBV_ANY
  366. && (tag_mode != 1 || sel->cont_level))
  367. APPEND(buf_ptr, tlvl);
  368. sel->got += tlvl;
  369. ADVANCE(tlvl);
  370. ASN_DEBUG("+EXPECT2 got=%ld left=%ld, wn=%d, clvl=%u",
  371. (long)sel->got, (long)sel->left,
  372. sel->want_nulls, sel->cont_level);
  373. } while(tlv_constr);
  374. if(sel == NULL) {
  375. /* Finished operation, "phase out" */
  376. ASN_DEBUG("Phase out");
  377. _CH_PHASE(ctx, +3);
  378. break;
  379. }
  380. NEXT_PHASE(ctx);
  381. /* Fall through */
  382. case 2:
  383. stck = (struct _stack *)ctx->ptr;
  384. sel = stck->cur_ptr;
  385. ASN_DEBUG("Phase 2: Need %ld bytes, size=%ld, alrg=%ld, wn=%d",
  386. (long)sel->left, (long)size, (long)sel->got,
  387. sel->want_nulls);
  388. {
  389. ber_tlv_len_t len;
  390. assert(sel->left >= 0);
  391. len = ((ber_tlv_len_t)size < sel->left)
  392. ? (ber_tlv_len_t)size : sel->left;
  393. if(len > 0) {
  394. if(type_variant == ASN_OSUBV_BIT
  395. && sel->bits_chopped == 0) {
  396. /* Put the unused-bits-octet away */
  397. st->bits_unused = *(const uint8_t *)buf_ptr;
  398. APPEND(((const char *)buf_ptr+1), (len - 1));
  399. sel->bits_chopped = 1;
  400. } else {
  401. APPEND(buf_ptr, len);
  402. }
  403. ADVANCE(len);
  404. sel->left -= len;
  405. sel->got += len;
  406. }
  407. if(sel->left) {
  408. ASN_DEBUG("OS left %ld, size = %ld, wn=%d\n",
  409. (long)sel->left, (long)size, sel->want_nulls);
  410. RETURN(RC_WMORE);
  411. }
  412. PREV_PHASE(ctx);
  413. goto phase1;
  414. }
  415. break;
  416. case 3:
  417. phase3:
  418. /*
  419. * Primitive form, no stack required.
  420. */
  421. assert(ctx->left >= 0);
  422. if(size < (size_t)ctx->left) {
  423. if(!size) RETURN(RC_WMORE);
  424. if(type_variant == ASN_OSUBV_BIT && !ctx->context) {
  425. st->bits_unused = *(const uint8_t *)buf_ptr;
  426. ctx->left--;
  427. ADVANCE(1);
  428. }
  429. APPEND(buf_ptr, size);
  430. assert(ctx->context > 0);
  431. ctx->left -= size;
  432. ADVANCE(size);
  433. RETURN(RC_WMORE);
  434. } else {
  435. if(type_variant == ASN_OSUBV_BIT
  436. && !ctx->context && ctx->left) {
  437. st->bits_unused = *(const uint8_t *)buf_ptr;
  438. ctx->left--;
  439. ADVANCE(1);
  440. }
  441. APPEND(buf_ptr, ctx->left);
  442. ADVANCE(ctx->left);
  443. ctx->left = 0;
  444. NEXT_PHASE(ctx);
  445. }
  446. break;
  447. }
  448. if(sel) {
  449. ASN_DEBUG("3sel p=%p, wn=%d, l=%ld, g=%ld, size=%ld",
  450. (void *)sel->prev, sel->want_nulls,
  451. (long)sel->left, (long)sel->got, (long)size);
  452. if(sel->prev || sel->want_nulls > 1 || sel->left > 0) {
  453. RETURN(RC_WMORE);
  454. }
  455. }
  456. /*
  457. * BIT STRING-specific processing.
  458. */
  459. if(type_variant == ASN_OSUBV_BIT) {
  460. if(st->size) {
  461. if(st->bits_unused < 0 || st->bits_unused > 7) {
  462. RETURN(RC_FAIL);
  463. }
  464. /* Finalize BIT STRING: zero out unused bits. */
  465. st->buf[st->size-1] &= 0xff << st->bits_unused;
  466. } else {
  467. if(st->bits_unused) {
  468. RETURN(RC_FAIL);
  469. }
  470. }
  471. }
  472. ASN_DEBUG("Took %ld bytes to encode %s: [%s]:%ld",
  473. (long)consumed_myself, td->name,
  474. (type_variant == ASN_OSUBV_STR) ? (char *)st->buf : "<data>",
  475. (long)st->size);
  476. RETURN(RC_OK);
  477. }
  478. /*
  479. * Encode OCTET STRING type using DER.
  480. */
  481. asn_enc_rval_t
  482. OCTET_STRING_encode_der(const asn_TYPE_descriptor_t *td, const void *sptr,
  483. int tag_mode, ber_tlv_tag_t tag,
  484. asn_app_consume_bytes_f *cb, void *app_key) {
  485. asn_enc_rval_t er;
  486. const asn_OCTET_STRING_specifics_t *specs = td->specifics
  487. ? (const asn_OCTET_STRING_specifics_t *)td->specifics
  488. : &asn_SPC_OCTET_STRING_specs;
  489. const BIT_STRING_t *st = (const BIT_STRING_t *)sptr;
  490. enum asn_OS_Subvariant type_variant = specs->subvariant;
  491. int fix_last_byte = 0;
  492. ASN_DEBUG("%s %s as OCTET STRING",
  493. cb?"Estimating":"Encoding", td->name);
  494. /*
  495. * Write tags.
  496. */
  497. if(type_variant != ASN_OSUBV_ANY || tag_mode == 1) {
  498. er.encoded = der_write_tags(td,
  499. (type_variant == ASN_OSUBV_BIT) + st->size,
  500. tag_mode, type_variant == ASN_OSUBV_ANY, tag,
  501. cb, app_key);
  502. if(er.encoded == -1) {
  503. er.failed_type = td;
  504. er.structure_ptr = sptr;
  505. return er;
  506. }
  507. } else {
  508. /* Disallow: [<tag>] IMPLICIT ANY */
  509. assert(type_variant != ASN_OSUBV_ANY || tag_mode != -1);
  510. er.encoded = 0;
  511. }
  512. if(!cb) {
  513. er.encoded += (type_variant == ASN_OSUBV_BIT) + st->size;
  514. ASN__ENCODED_OK(er);
  515. }
  516. /*
  517. * Prepare to deal with the last octet of BIT STRING.
  518. */
  519. if(type_variant == ASN_OSUBV_BIT) {
  520. uint8_t b = st->bits_unused & 0x07;
  521. if(b && st->size) fix_last_byte = 1;
  522. ASN__CALLBACK(&b, 1);
  523. }
  524. /* Invoke callback for the main part of the buffer */
  525. ASN__CALLBACK(st->buf, st->size - fix_last_byte);
  526. /* The last octet should be stripped off the unused bits */
  527. if(fix_last_byte) {
  528. uint8_t b = st->buf[st->size-1] & (0xff << st->bits_unused);
  529. ASN__CALLBACK(&b, 1);
  530. }
  531. ASN__ENCODED_OK(er);
  532. cb_failed:
  533. ASN__ENCODE_FAILED;
  534. }
  535. asn_enc_rval_t
  536. OCTET_STRING_encode_xer(const asn_TYPE_descriptor_t *td, const void *sptr,
  537. int ilevel, enum xer_encoder_flags_e flags,
  538. asn_app_consume_bytes_f *cb, void *app_key) {
  539. const char * const h2c = "0123456789ABCDEF";
  540. const OCTET_STRING_t *st = (const OCTET_STRING_t *)sptr;
  541. asn_enc_rval_t er;
  542. char scratch[16 * 3 + 4];
  543. char *p = scratch;
  544. uint8_t *buf;
  545. uint8_t *end;
  546. size_t i;
  547. if(!st || (!st->buf && st->size))
  548. ASN__ENCODE_FAILED;
  549. er.encoded = 0;
  550. /*
  551. * Dump the contents of the buffer in hexadecimal.
  552. */
  553. buf = st->buf;
  554. end = buf + st->size;
  555. if(flags & XER_F_CANONICAL) {
  556. char *scend = scratch + (sizeof(scratch) - 2);
  557. for(; buf < end; buf++) {
  558. if(p >= scend) {
  559. ASN__CALLBACK(scratch, p - scratch);
  560. p = scratch;
  561. }
  562. *p++ = h2c[(*buf >> 4) & 0x0F];
  563. *p++ = h2c[*buf & 0x0F];
  564. }
  565. ASN__CALLBACK(scratch, p-scratch); /* Dump the rest */
  566. } else {
  567. for(i = 0; buf < end; buf++, i++) {
  568. if(!(i % 16) && (i || st->size > 16)) {
  569. ASN__CALLBACK(scratch, p-scratch);
  570. p = scratch;
  571. ASN__TEXT_INDENT(1, ilevel);
  572. }
  573. *p++ = h2c[(*buf >> 4) & 0x0F];
  574. *p++ = h2c[*buf & 0x0F];
  575. *p++ = 0x20;
  576. }
  577. if(p - scratch) {
  578. p--; /* Remove the tail space */
  579. ASN__CALLBACK(scratch, p-scratch); /* Dump the rest */
  580. if(st->size > 16)
  581. ASN__TEXT_INDENT(1, ilevel-1);
  582. }
  583. }
  584. ASN__ENCODED_OK(er);
  585. cb_failed:
  586. ASN__ENCODE_FAILED;
  587. }
  588. static const struct OCTET_STRING__xer_escape_table_s {
  589. const char *string;
  590. int size;
  591. } OCTET_STRING__xer_escape_table[] = {
  592. #define OSXET(s) { s, sizeof(s) - 1 }
  593. OSXET("\074\156\165\154\057\076"), /* <nul/> */
  594. OSXET("\074\163\157\150\057\076"), /* <soh/> */
  595. OSXET("\074\163\164\170\057\076"), /* <stx/> */
  596. OSXET("\074\145\164\170\057\076"), /* <etx/> */
  597. OSXET("\074\145\157\164\057\076"), /* <eot/> */
  598. OSXET("\074\145\156\161\057\076"), /* <enq/> */
  599. OSXET("\074\141\143\153\057\076"), /* <ack/> */
  600. OSXET("\074\142\145\154\057\076"), /* <bel/> */
  601. OSXET("\074\142\163\057\076"), /* <bs/> */
  602. OSXET("\011"), /* \t */
  603. OSXET("\012"), /* \n */
  604. OSXET("\074\166\164\057\076"), /* <vt/> */
  605. OSXET("\074\146\146\057\076"), /* <ff/> */
  606. OSXET("\015"), /* \r */
  607. OSXET("\074\163\157\057\076"), /* <so/> */
  608. OSXET("\074\163\151\057\076"), /* <si/> */
  609. OSXET("\074\144\154\145\057\076"), /* <dle/> */
  610. OSXET("\074\144\143\061\057\076"), /* <de1/> */
  611. OSXET("\074\144\143\062\057\076"), /* <de2/> */
  612. OSXET("\074\144\143\063\057\076"), /* <de3/> */
  613. OSXET("\074\144\143\064\057\076"), /* <de4/> */
  614. OSXET("\074\156\141\153\057\076"), /* <nak/> */
  615. OSXET("\074\163\171\156\057\076"), /* <syn/> */
  616. OSXET("\074\145\164\142\057\076"), /* <etb/> */
  617. OSXET("\074\143\141\156\057\076"), /* <can/> */
  618. OSXET("\074\145\155\057\076"), /* <em/> */
  619. OSXET("\074\163\165\142\057\076"), /* <sub/> */
  620. OSXET("\074\145\163\143\057\076"), /* <esc/> */
  621. OSXET("\074\151\163\064\057\076"), /* <is4/> */
  622. OSXET("\074\151\163\063\057\076"), /* <is3/> */
  623. OSXET("\074\151\163\062\057\076"), /* <is2/> */
  624. OSXET("\074\151\163\061\057\076"), /* <is1/> */
  625. { 0, 0 }, /* " " */
  626. { 0, 0 }, /* ! */
  627. { 0, 0 }, /* \" */
  628. { 0, 0 }, /* # */
  629. { 0, 0 }, /* $ */
  630. { 0, 0 }, /* % */
  631. OSXET("\046\141\155\160\073"), /* &amp; */
  632. { 0, 0 }, /* ' */
  633. {0,0},{0,0},{0,0},{0,0},{0,0},{0,0},{0,0},{0,0}, /* ()*+,-./ */
  634. {0,0},{0,0},{0,0},{0,0},{0,0},{0,0},{0,0},{0,0}, /* 01234567 */
  635. {0,0},{0,0},{0,0},{0,0}, /* 89:; */
  636. OSXET("\046\154\164\073"), /* &lt; */
  637. { 0, 0 }, /* = */
  638. OSXET("\046\147\164\073"), /* &gt; */
  639. };
  640. static int
  641. OS__check_escaped_control_char(const void *buf, int size) {
  642. size_t i;
  643. /*
  644. * Inefficient algorithm which translates the escape sequences
  645. * defined above into characters. Returns -1 if not found.
  646. * TODO: replace by a faster algorithm (bsearch(), hash or
  647. * nested table lookups).
  648. */
  649. for(i = 0; i < 32 /* Don't spend time on the bottom half */; i++) {
  650. const struct OCTET_STRING__xer_escape_table_s *el;
  651. el = &OCTET_STRING__xer_escape_table[i];
  652. if(el->size == size && memcmp(buf, el->string, size) == 0)
  653. return i;
  654. }
  655. return -1;
  656. }
  657. static int
  658. OCTET_STRING__handle_control_chars(void *struct_ptr, const void *chunk_buf, size_t chunk_size) {
  659. /*
  660. * This might be one of the escape sequences
  661. * for control characters. Check it out.
  662. * #11.15.5
  663. */
  664. int control_char = OS__check_escaped_control_char(chunk_buf,chunk_size);
  665. if(control_char >= 0) {
  666. OCTET_STRING_t *st = (OCTET_STRING_t *)struct_ptr;
  667. void *p = REALLOC(st->buf, st->size + 2);
  668. if(p) {
  669. st->buf = (uint8_t *)p;
  670. st->buf[st->size++] = control_char;
  671. st->buf[st->size] = '\0'; /* nul-termination */
  672. return 0;
  673. }
  674. }
  675. return -1; /* No, it's not */
  676. }
  677. asn_enc_rval_t
  678. OCTET_STRING_encode_xer_utf8(const asn_TYPE_descriptor_t *td, const void *sptr,
  679. int ilevel, enum xer_encoder_flags_e flags,
  680. asn_app_consume_bytes_f *cb, void *app_key) {
  681. const OCTET_STRING_t *st = (const OCTET_STRING_t *)sptr;
  682. asn_enc_rval_t er;
  683. uint8_t *buf, *end;
  684. uint8_t *ss; /* Sequence start */
  685. ssize_t encoded_len = 0;
  686. (void)ilevel; /* Unused argument */
  687. (void)flags; /* Unused argument */
  688. if(!st || (!st->buf && st->size))
  689. ASN__ENCODE_FAILED;
  690. buf = st->buf;
  691. end = buf + st->size;
  692. for(ss = buf; buf < end; buf++) {
  693. unsigned int ch = *buf;
  694. int s_len; /* Special encoding sequence length */
  695. /*
  696. * Escape certain characters: X.680/11.15
  697. */
  698. if(ch < sizeof(OCTET_STRING__xer_escape_table)
  699. /sizeof(OCTET_STRING__xer_escape_table[0])
  700. && (s_len = OCTET_STRING__xer_escape_table[ch].size)) {
  701. if(((buf - ss) && cb(ss, buf - ss, app_key) < 0)
  702. || cb(OCTET_STRING__xer_escape_table[ch].string, s_len,
  703. app_key) < 0)
  704. ASN__ENCODE_FAILED;
  705. encoded_len += (buf - ss) + s_len;
  706. ss = buf + 1;
  707. }
  708. }
  709. encoded_len += (buf - ss);
  710. if((buf - ss) && cb(ss, buf - ss, app_key) < 0)
  711. ASN__ENCODE_FAILED;
  712. er.encoded = encoded_len;
  713. ASN__ENCODED_OK(er);
  714. }
  715. /*
  716. * Convert from hexadecimal format (cstring): "AB CD EF"
  717. */
  718. static ssize_t OCTET_STRING__convert_hexadecimal(void *sptr, const void *chunk_buf, size_t chunk_size, int have_more) {
  719. OCTET_STRING_t *st = (OCTET_STRING_t *)sptr;
  720. const char *chunk_stop = (const char *)chunk_buf;
  721. const char *p = chunk_stop;
  722. const char *pend = p + chunk_size;
  723. unsigned int clv = 0;
  724. int half = 0; /* Half bit */
  725. uint8_t *buf;
  726. /* Reallocate buffer according to high cap estimation */
  727. size_t new_size = st->size + (chunk_size + 1) / 2;
  728. void *nptr = REALLOC(st->buf, new_size + 1);
  729. if(!nptr) return -1;
  730. st->buf = (uint8_t *)nptr;
  731. buf = st->buf + st->size;
  732. /*
  733. * If something like " a b c " appears here, the " a b":3 will be
  734. * converted, and the rest skipped. That is, unless buf_size is greater
  735. * than chunk_size, then it'll be equivalent to "ABC0".
  736. */
  737. for(; p < pend; p++) {
  738. int ch = *(const unsigned char *)p;
  739. switch(ch) {
  740. case 0x09: case 0x0a: case 0x0c: case 0x0d:
  741. case 0x20:
  742. /* Ignore whitespace */
  743. continue;
  744. case 0x30: case 0x31: case 0x32: case 0x33: case 0x34: /*01234*/
  745. case 0x35: case 0x36: case 0x37: case 0x38: case 0x39: /*56789*/
  746. clv = (clv << 4) + (ch - 0x30);
  747. break;
  748. case 0x41: case 0x42: case 0x43: /* ABC */
  749. case 0x44: case 0x45: case 0x46: /* DEF */
  750. clv = (clv << 4) + (ch - 0x41 + 10);
  751. break;
  752. case 0x61: case 0x62: case 0x63: /* abc */
  753. case 0x64: case 0x65: case 0x66: /* def */
  754. clv = (clv << 4) + (ch - 0x61 + 10);
  755. break;
  756. default:
  757. *buf = 0; /* JIC */
  758. return -1;
  759. }
  760. if(half++) {
  761. half = 0;
  762. *buf++ = clv;
  763. chunk_stop = p + 1;
  764. }
  765. }
  766. /*
  767. * Check partial decoding.
  768. */
  769. if(half) {
  770. if(have_more) {
  771. /*
  772. * Partial specification is fine,
  773. * because no more more PXER_TEXT data is available.
  774. */
  775. *buf++ = clv << 4;
  776. chunk_stop = p;
  777. }
  778. } else {
  779. chunk_stop = p;
  780. }
  781. st->size = buf - st->buf; /* Adjust the buffer size */
  782. assert(st->size <= new_size);
  783. st->buf[st->size] = 0; /* Courtesy termination */
  784. return (chunk_stop - (const char *)chunk_buf); /* Converted size */
  785. }
  786. /*
  787. * Convert from binary format: "00101011101"
  788. */
  789. static ssize_t OCTET_STRING__convert_binary(void *sptr, const void *chunk_buf, size_t chunk_size, int have_more) {
  790. BIT_STRING_t *st = (BIT_STRING_t *)sptr;
  791. const char *p = (const char *)chunk_buf;
  792. const char *pend = p + chunk_size;
  793. int bits_unused = st->bits_unused & 0x7;
  794. uint8_t *buf;
  795. /* Reallocate buffer according to high cap estimation */
  796. size_t new_size = st->size + (chunk_size + 7) / 8;
  797. void *nptr = REALLOC(st->buf, new_size + 1);
  798. if(!nptr) return -1;
  799. st->buf = (uint8_t *)nptr;
  800. buf = st->buf + st->size;
  801. (void)have_more;
  802. if(bits_unused == 0)
  803. bits_unused = 8;
  804. else if(st->size)
  805. buf--;
  806. /*
  807. * Convert series of 0 and 1 into the octet string.
  808. */
  809. for(; p < pend; p++) {
  810. int ch = *(const unsigned char *)p;
  811. switch(ch) {
  812. case 0x09: case 0x0a: case 0x0c: case 0x0d:
  813. case 0x20:
  814. /* Ignore whitespace */
  815. break;
  816. case 0x30:
  817. case 0x31:
  818. if(bits_unused-- <= 0) {
  819. *++buf = 0; /* Clean the cell */
  820. bits_unused = 7;
  821. }
  822. *buf |= (ch&1) << bits_unused;
  823. break;
  824. default:
  825. st->bits_unused = bits_unused;
  826. return -1;
  827. }
  828. }
  829. if(bits_unused == 8) {
  830. st->size = buf - st->buf;
  831. st->bits_unused = 0;
  832. } else {
  833. st->size = buf - st->buf + 1;
  834. st->bits_unused = bits_unused;
  835. }
  836. assert(st->size <= new_size);
  837. st->buf[st->size] = 0; /* Courtesy termination */
  838. return chunk_size; /* Converted in full */
  839. }
  840. /*
  841. * Something like strtod(), but with stricter rules.
  842. */
  843. static int
  844. OS__strtoent(int base, const char *buf, const char *end, int32_t *ret_value) {
  845. const int32_t last_unicode_codepoint = 0x10ffff;
  846. int32_t val = 0;
  847. const char *p;
  848. for(p = buf; p < end; p++) {
  849. int ch = *p;
  850. switch(ch) {
  851. case 0x30: case 0x31: case 0x32: case 0x33: case 0x34: /*01234*/
  852. case 0x35: case 0x36: case 0x37: case 0x38: case 0x39: /*56789*/
  853. val = val * base + (ch - 0x30);
  854. break;
  855. case 0x41: case 0x42: case 0x43: /* ABC */
  856. case 0x44: case 0x45: case 0x46: /* DEF */
  857. val = val * base + (ch - 0x41 + 10);
  858. break;
  859. case 0x61: case 0x62: case 0x63: /* abc */
  860. case 0x64: case 0x65: case 0x66: /* def */
  861. val = val * base + (ch - 0x61 + 10);
  862. break;
  863. case 0x3b: /* ';' */
  864. *ret_value = val;
  865. return (p - buf) + 1;
  866. default:
  867. return -1; /* Character set error */
  868. }
  869. /* Value exceeds the Unicode range. */
  870. if(val > last_unicode_codepoint) {
  871. return -1;
  872. }
  873. }
  874. *ret_value = -1;
  875. return (p - buf);
  876. }
  877. /*
  878. * Convert from the plain UTF-8 format, expanding entity references: "2 &lt; 3"
  879. */
  880. static ssize_t
  881. OCTET_STRING__convert_entrefs(void *sptr, const void *chunk_buf,
  882. size_t chunk_size, int have_more) {
  883. OCTET_STRING_t *st = (OCTET_STRING_t *)sptr;
  884. const char *p = (const char *)chunk_buf;
  885. const char *pend = p + chunk_size;
  886. uint8_t *buf;
  887. /* Reallocate buffer */
  888. size_t new_size = st->size + chunk_size;
  889. void *nptr = REALLOC(st->buf, new_size + 1);
  890. if(!nptr) return -1;
  891. st->buf = (uint8_t *)nptr;
  892. buf = st->buf + st->size;
  893. /*
  894. * Convert series of 0 and 1 into the octet string.
  895. */
  896. for(; p < pend; p++) {
  897. int ch = *(const unsigned char *)p;
  898. int len; /* Length of the rest of the chunk */
  899. if(ch != 0x26 /* '&' */) {
  900. *buf++ = ch;
  901. continue; /* That was easy... */
  902. }
  903. /*
  904. * Process entity reference.
  905. */
  906. len = chunk_size - (p - (const char *)chunk_buf);
  907. if(len == 1 /* "&" */) goto want_more;
  908. if(p[1] == 0x23 /* '#' */) {
  909. const char *pval; /* Pointer to start of digits */
  910. int32_t val = 0; /* Entity reference value */
  911. int base;
  912. if(len == 2 /* "&#" */) goto want_more;
  913. if(p[2] == 0x78 /* 'x' */)
  914. pval = p + 3, base = 16;
  915. else
  916. pval = p + 2, base = 10;
  917. len = OS__strtoent(base, pval, p + len, &val);
  918. if(len == -1) {
  919. /* Invalid charset. Just copy verbatim. */
  920. *buf++ = ch;
  921. continue;
  922. }
  923. if(!len || pval[len-1] != 0x3b) goto want_more;
  924. assert(val > 0);
  925. p += (pval - p) + len - 1; /* Advance past entref */
  926. if(val < 0x80) {
  927. *buf++ = (char)val;
  928. } else if(val < 0x800) {
  929. *buf++ = 0xc0 | ((val >> 6));
  930. *buf++ = 0x80 | ((val & 0x3f));
  931. } else if(val < 0x10000) {
  932. *buf++ = 0xe0 | ((val >> 12));
  933. *buf++ = 0x80 | ((val >> 6) & 0x3f);
  934. *buf++ = 0x80 | ((val & 0x3f));
  935. } else if(val < 0x200000) {
  936. *buf++ = 0xf0 | ((val >> 18));
  937. *buf++ = 0x80 | ((val >> 12) & 0x3f);
  938. *buf++ = 0x80 | ((val >> 6) & 0x3f);
  939. *buf++ = 0x80 | ((val & 0x3f));
  940. } else if(val < 0x4000000) {
  941. *buf++ = 0xf8 | ((val >> 24));
  942. *buf++ = 0x80 | ((val >> 18) & 0x3f);
  943. *buf++ = 0x80 | ((val >> 12) & 0x3f);
  944. *buf++ = 0x80 | ((val >> 6) & 0x3f);
  945. *buf++ = 0x80 | ((val & 0x3f));
  946. } else {
  947. *buf++ = 0xfc | ((val >> 30) & 0x1);
  948. *buf++ = 0x80 | ((val >> 24) & 0x3f);
  949. *buf++ = 0x80 | ((val >> 18) & 0x3f);
  950. *buf++ = 0x80 | ((val >> 12) & 0x3f);
  951. *buf++ = 0x80 | ((val >> 6) & 0x3f);
  952. *buf++ = 0x80 | ((val & 0x3f));
  953. }
  954. } else {
  955. /*
  956. * Ugly, limited parsing of &amp; &gt; &lt;
  957. */
  958. char *sc = (char *)memchr(p, 0x3b, len > 5 ? 5 : len);
  959. if(!sc) goto want_more;
  960. if((sc - p) == 4
  961. && p[1] == 0x61 /* 'a' */
  962. && p[2] == 0x6d /* 'm' */
  963. && p[3] == 0x70 /* 'p' */) {
  964. *buf++ = 0x26;
  965. p = sc;
  966. continue;
  967. }
  968. if((sc - p) == 3) {
  969. if(p[1] == 0x6c) {
  970. *buf = 0x3c; /* '<' */
  971. } else if(p[1] == 0x67) {
  972. *buf = 0x3e; /* '>' */
  973. } else {
  974. /* Unsupported entity reference */
  975. *buf++ = ch;
  976. continue;
  977. }
  978. if(p[2] != 0x74) {
  979. /* Unsupported entity reference */
  980. *buf++ = ch;
  981. continue;
  982. }
  983. buf++;
  984. p = sc;
  985. continue;
  986. }
  987. /* Unsupported entity reference */
  988. *buf++ = ch;
  989. }
  990. continue;
  991. want_more:
  992. if(have_more) {
  993. /*
  994. * We know that no more data (of the same type)
  995. * is coming. Copy the rest verbatim.
  996. */
  997. *buf++ = ch;
  998. continue;
  999. }
  1000. chunk_size = (p - (const char *)chunk_buf);
  1001. /* Processing stalled: need more data */
  1002. break;
  1003. }
  1004. st->size = buf - st->buf;
  1005. assert(st->size <= new_size);
  1006. st->buf[st->size] = 0; /* Courtesy termination */
  1007. return chunk_size; /* Converted in full */
  1008. }
  1009. /*
  1010. * Decode OCTET STRING from the XML element's body.
  1011. */
  1012. static asn_dec_rval_t
  1013. OCTET_STRING__decode_xer(
  1014. const asn_codec_ctx_t *opt_codec_ctx, const asn_TYPE_descriptor_t *td,
  1015. void **sptr, const char *opt_mname, const void *buf_ptr, size_t size,
  1016. int (*opt_unexpected_tag_decoder)(void *struct_ptr, const void *chunk_buf,
  1017. size_t chunk_size),
  1018. ssize_t (*body_receiver)(void *struct_ptr, const void *chunk_buf,
  1019. size_t chunk_size, int have_more)) {
  1020. OCTET_STRING_t *st = (OCTET_STRING_t *)*sptr;
  1021. const asn_OCTET_STRING_specifics_t *specs = td->specifics
  1022. ? (const asn_OCTET_STRING_specifics_t *)td->specifics
  1023. : &asn_SPC_OCTET_STRING_specs;
  1024. const char *xml_tag = opt_mname ? opt_mname : td->xml_tag;
  1025. asn_struct_ctx_t *ctx; /* Per-structure parser context */
  1026. asn_dec_rval_t rval; /* Return value from the decoder */
  1027. int st_allocated;
  1028. /*
  1029. * Create the string if does not exist.
  1030. */
  1031. if(!st) {
  1032. st = (OCTET_STRING_t *)CALLOC(1, specs->struct_size);
  1033. *sptr = (void *)st;
  1034. if(!st) goto sta_failed;
  1035. st_allocated = 1;
  1036. } else {
  1037. st_allocated = 0;
  1038. }
  1039. if(!st->buf) {
  1040. /* This is separate from above section */
  1041. st->buf = (uint8_t *)CALLOC(1, 1);
  1042. if(!st->buf) {
  1043. if(st_allocated) {
  1044. *sptr = 0;
  1045. goto stb_failed;
  1046. } else {
  1047. goto sta_failed;
  1048. }
  1049. }
  1050. }
  1051. /* Restore parsing context */
  1052. ctx = (asn_struct_ctx_t *)(((char *)*sptr) + specs->ctx_offset);
  1053. return xer_decode_general(opt_codec_ctx, ctx, *sptr, xml_tag,
  1054. buf_ptr, size, opt_unexpected_tag_decoder, body_receiver);
  1055. stb_failed:
  1056. FREEMEM(st);
  1057. sta_failed:
  1058. rval.code = RC_FAIL;
  1059. rval.consumed = 0;
  1060. return rval;
  1061. }
  1062. /*
  1063. * Decode OCTET STRING from the hexadecimal data.
  1064. */
  1065. asn_dec_rval_t
  1066. OCTET_STRING_decode_xer_hex(const asn_codec_ctx_t *opt_codec_ctx,
  1067. const asn_TYPE_descriptor_t *td, void **sptr,
  1068. const char *opt_mname, const void *buf_ptr,
  1069. size_t size) {
  1070. return OCTET_STRING__decode_xer(opt_codec_ctx, td, sptr, opt_mname,
  1071. buf_ptr, size, 0, OCTET_STRING__convert_hexadecimal);
  1072. }
  1073. /*
  1074. * Decode OCTET STRING from the binary (0/1) data.
  1075. */
  1076. asn_dec_rval_t
  1077. OCTET_STRING_decode_xer_binary(const asn_codec_ctx_t *opt_codec_ctx,
  1078. const asn_TYPE_descriptor_t *td, void **sptr,
  1079. const char *opt_mname, const void *buf_ptr,
  1080. size_t size) {
  1081. return OCTET_STRING__decode_xer(opt_codec_ctx, td, sptr, opt_mname,
  1082. buf_ptr, size, 0, OCTET_STRING__convert_binary);
  1083. }
  1084. /*
  1085. * Decode OCTET STRING from the string (ASCII/UTF-8) data.
  1086. */
  1087. asn_dec_rval_t
  1088. OCTET_STRING_decode_xer_utf8(const asn_codec_ctx_t *opt_codec_ctx,
  1089. const asn_TYPE_descriptor_t *td, void **sptr,
  1090. const char *opt_mname, const void *buf_ptr,
  1091. size_t size) {
  1092. return OCTET_STRING__decode_xer(opt_codec_ctx, td, sptr, opt_mname,
  1093. buf_ptr, size,
  1094. OCTET_STRING__handle_control_chars,
  1095. OCTET_STRING__convert_entrefs);
  1096. }
  1097. #ifndef ASN_DISABLE_PER_SUPPORT
  1098. static int
  1099. OCTET_STRING_per_get_characters(asn_per_data_t *po, uint8_t *buf,
  1100. size_t units, unsigned int bpc, unsigned int unit_bits,
  1101. long lb, long ub, const asn_per_constraints_t *pc) {
  1102. uint8_t *end = buf + units * bpc;
  1103. ASN_DEBUG("Expanding %d characters into (%ld..%ld):%d",
  1104. (int)units, lb, ub, unit_bits);
  1105. /* X.691: 27.5.4 */
  1106. if((unsigned long)ub <= ((unsigned long)2 << (unit_bits - 1))) {
  1107. /* Decode without translation */
  1108. lb = 0;
  1109. } else if(pc && pc->code2value) {
  1110. if(unit_bits > 16)
  1111. return 1; /* FATAL: can't have constrained
  1112. * UniversalString with more than
  1113. * 16 million code points */
  1114. for(; buf < end; buf += bpc) {
  1115. int value;
  1116. int code = per_get_few_bits(po, unit_bits);
  1117. if(code < 0) return -1; /* WMORE */
  1118. value = pc->code2value(code);
  1119. if(value < 0) {
  1120. ASN_DEBUG("Code %d (0x%02x) is"
  1121. " not in map (%ld..%ld)",
  1122. code, code, lb, ub);
  1123. return 1; /* FATAL */
  1124. }
  1125. switch(bpc) {
  1126. case 1: *buf = value; break;
  1127. case 2: buf[0] = value >> 8; buf[1] = value; break;
  1128. case 4: buf[0] = value >> 24; buf[1] = value >> 16;
  1129. buf[2] = value >> 8; buf[3] = value; break;
  1130. }
  1131. }
  1132. return 0;
  1133. }
  1134. /* Shortcut the no-op copying to the aligned structure */
  1135. if(lb == 0 && (unit_bits == 8 * bpc)) {
  1136. return per_get_many_bits(po, buf, 0, unit_bits * units);
  1137. }
  1138. for(; buf < end; buf += bpc) {
  1139. int32_t code = per_get_few_bits(po, unit_bits);
  1140. int32_t ch = code + lb;
  1141. if(code < 0) return -1; /* WMORE */
  1142. if(ch > ub) {
  1143. ASN_DEBUG("Code %d is out of range (%ld..%ld)",
  1144. ch, lb, ub);
  1145. return 1; /* FATAL */
  1146. }
  1147. switch(bpc) {
  1148. case 1: *buf = ch; break;
  1149. case 2: buf[0] = ch >> 8; buf[1] = ch; break;
  1150. case 4: buf[0] = ch >> 24; buf[1] = ch >> 16;
  1151. buf[2] = ch >> 8; buf[3] = ch; break;
  1152. }
  1153. }
  1154. return 0;
  1155. }
  1156. static int
  1157. OCTET_STRING_per_put_characters(asn_per_outp_t *po, const uint8_t *buf,
  1158. size_t units, unsigned int bpc, unsigned int unit_bits,
  1159. long lb, long ub, const asn_per_constraints_t *pc) {
  1160. const uint8_t *end = buf + units * bpc;
  1161. ASN_DEBUG("Squeezing %d characters into (%ld..%ld):%d (%d bpc)",
  1162. (int)units, lb, ub, unit_bits, bpc);
  1163. /* X.691: 27.5.4 */
  1164. if((unsigned long)ub <= ((unsigned long)2 << (unit_bits - 1))) {
  1165. /* Encode as is */
  1166. lb = 0;
  1167. } else if(pc && pc->value2code) {
  1168. for(; buf < end; buf += bpc) {
  1169. int code;
  1170. uint32_t value;
  1171. switch(bpc) {
  1172. case 1: value = *(const uint8_t *)buf; break;
  1173. case 2: value = (buf[0] << 8) | buf[1]; break;
  1174. case 4: value = (buf[0] << 24) | (buf[1] << 16)
  1175. | (buf[2] << 8) | buf[3]; break;
  1176. default: return -1;
  1177. }
  1178. code = pc->value2code(value);
  1179. if(code < 0) {
  1180. ASN_DEBUG("Character %d (0x%02x) is"
  1181. " not in map (%ld..%ld)",
  1182. *buf, *buf, lb, ub);
  1183. return -1;
  1184. }
  1185. if(per_put_few_bits(po, code, unit_bits))
  1186. return -1;
  1187. }
  1188. }
  1189. /* Shortcut the no-op copying to the aligned structure */
  1190. if(lb == 0 && (unit_bits == 8 * bpc)) {
  1191. return per_put_many_bits(po, buf, unit_bits * units);
  1192. }
  1193. for(ub -= lb; buf < end; buf += bpc) {
  1194. int ch;
  1195. uint32_t value;
  1196. switch(bpc) {
  1197. case 1:
  1198. value = *(const uint8_t *)buf;
  1199. break;
  1200. case 2:
  1201. value = (buf[0] << 8) | buf[1];
  1202. break;
  1203. case 4:
  1204. value = (buf[0] << 24) | (buf[1] << 16) | (buf[2] << 8) | buf[3];
  1205. break;
  1206. default:
  1207. return -1;
  1208. }
  1209. ch = value - lb;
  1210. if(ch < 0 || ch > ub) {
  1211. ASN_DEBUG("Character %d (0x%02x) is out of range (%ld..%ld)", *buf,
  1212. value, lb, ub + lb);
  1213. return -1;
  1214. }
  1215. if(per_put_few_bits(po, ch, unit_bits)) return -1;
  1216. }
  1217. return 0;
  1218. }
  1219. static asn_per_constraints_t asn_DEF_OCTET_STRING_constraints = {
  1220. { APC_CONSTRAINED, 8, 8, 0, 255 },
  1221. { APC_SEMI_CONSTRAINED, -1, -1, 0, 0 },
  1222. 0, 0
  1223. };
  1224. asn_dec_rval_t
  1225. OCTET_STRING_decode_uper(const asn_codec_ctx_t *opt_codec_ctx,
  1226. const asn_TYPE_descriptor_t *td,
  1227. const asn_per_constraints_t *constraints, void **sptr,
  1228. asn_per_data_t *pd) {
  1229. const asn_OCTET_STRING_specifics_t *specs = td->specifics
  1230. ? (const asn_OCTET_STRING_specifics_t *)td->specifics
  1231. : &asn_SPC_OCTET_STRING_specs;
  1232. const asn_per_constraints_t *pc =
  1233. constraints ? constraints : td->encoding_constraints.per_constraints;
  1234. const asn_per_constraint_t *cval;
  1235. const asn_per_constraint_t *csiz;
  1236. asn_dec_rval_t rval = { RC_OK, 0 };
  1237. OCTET_STRING_t *st = (OCTET_STRING_t *)*sptr;
  1238. ssize_t consumed_myself = 0;
  1239. int repeat;
  1240. enum {
  1241. OS__BPC_CHAR = 1,
  1242. OS__BPC_U16 = 2,
  1243. OS__BPC_U32 = 4
  1244. } bpc; /* Bytes per character */
  1245. unsigned int unit_bits;
  1246. unsigned int canonical_unit_bits;
  1247. (void)opt_codec_ctx;
  1248. if(pc) {
  1249. cval = &pc->value;
  1250. csiz = &pc->size;
  1251. } else {
  1252. cval = &asn_DEF_OCTET_STRING_constraints.value;
  1253. csiz = &asn_DEF_OCTET_STRING_constraints.size;
  1254. }
  1255. switch(specs->subvariant) {
  1256. default:
  1257. case ASN_OSUBV_ANY:
  1258. case ASN_OSUBV_BIT:
  1259. ASN_DEBUG("Unrecognized subvariant %d", specs->subvariant);
  1260. RETURN(RC_FAIL);
  1261. break;
  1262. case ASN_OSUBV_STR:
  1263. canonical_unit_bits = unit_bits = 8;
  1264. if(cval->flags & APC_CONSTRAINED)
  1265. unit_bits = cval->range_bits;
  1266. bpc = OS__BPC_CHAR;
  1267. break;
  1268. case ASN_OSUBV_U16:
  1269. canonical_unit_bits = unit_bits = 16;
  1270. if(cval->flags & APC_CONSTRAINED)
  1271. unit_bits = cval->range_bits;
  1272. bpc = OS__BPC_U16;
  1273. break;
  1274. case ASN_OSUBV_U32:
  1275. canonical_unit_bits = unit_bits = 32;
  1276. if(cval->flags & APC_CONSTRAINED)
  1277. unit_bits = cval->range_bits;
  1278. bpc = OS__BPC_U32;
  1279. break;
  1280. }
  1281. /*
  1282. * Allocate the string.
  1283. */
  1284. if(!st) {
  1285. st = (OCTET_STRING_t *)(*sptr = CALLOC(1, specs->struct_size));
  1286. if(!st) RETURN(RC_FAIL);
  1287. }
  1288. ASN_DEBUG("PER Decoding %s size %ld .. %ld bits %d",
  1289. csiz->flags & APC_EXTENSIBLE ? "extensible" : "non-extensible",
  1290. csiz->lower_bound, csiz->upper_bound, csiz->effective_bits);
  1291. if(csiz->flags & APC_EXTENSIBLE) {
  1292. int inext = per_get_few_bits(pd, 1);
  1293. if(inext < 0) RETURN(RC_WMORE);
  1294. if(inext) {
  1295. csiz = &asn_DEF_OCTET_STRING_constraints.size;
  1296. unit_bits = canonical_unit_bits;
  1297. }
  1298. }
  1299. if(csiz->effective_bits >= 0) {
  1300. FREEMEM(st->buf);
  1301. if(bpc) {
  1302. st->size = csiz->upper_bound * bpc;
  1303. } else {
  1304. st->size = (csiz->upper_bound + 7) >> 3;
  1305. }
  1306. st->buf = (uint8_t *)MALLOC(st->size + 1);
  1307. if(!st->buf) { st->size = 0; RETURN(RC_FAIL); }
  1308. }
  1309. /* X.691, #16.5: zero-length encoding */
  1310. /* X.691, #16.6: short fixed length encoding (up to 2 octets) */
  1311. /* X.691, #16.7: long fixed length encoding (up to 64K octets) */
  1312. if(csiz->effective_bits == 0) {
  1313. int ret;
  1314. if(bpc) {
  1315. ASN_DEBUG("Encoding OCTET STRING size %ld",
  1316. csiz->upper_bound);
  1317. ret = OCTET_STRING_per_get_characters(pd, st->buf,
  1318. csiz->upper_bound, bpc, unit_bits,
  1319. cval->lower_bound, cval->upper_bound, pc);
  1320. if(ret > 0) RETURN(RC_FAIL);
  1321. } else {
  1322. ASN_DEBUG("Encoding BIT STRING size %ld",
  1323. csiz->upper_bound);
  1324. ret = per_get_many_bits(pd, st->buf, 0,
  1325. unit_bits * csiz->upper_bound);
  1326. }
  1327. if(ret < 0) RETURN(RC_WMORE);
  1328. consumed_myself += unit_bits * csiz->upper_bound;
  1329. st->buf[st->size] = 0;
  1330. RETURN(RC_OK);
  1331. }
  1332. st->size = 0;
  1333. do {
  1334. ssize_t raw_len;
  1335. ssize_t len_bytes;
  1336. void *p;
  1337. int ret;
  1338. /* Get the PER length */
  1339. raw_len = uper_get_length(pd, csiz->effective_bits, csiz->lower_bound,
  1340. &repeat);
  1341. if(raw_len < 0) RETURN(RC_WMORE);
  1342. if(raw_len == 0 && st->buf) break;
  1343. ASN_DEBUG("Got PER length eb %ld, len %ld, %s (%s)",
  1344. (long)csiz->effective_bits, (long)raw_len,
  1345. repeat ? "repeat" : "once", td->name);
  1346. len_bytes = raw_len * bpc;
  1347. p = REALLOC(st->buf, st->size + len_bytes + 1);
  1348. if(!p) RETURN(RC_FAIL);
  1349. st->buf = (uint8_t *)p;
  1350. ret = OCTET_STRING_per_get_characters(pd, &st->buf[st->size], raw_len,
  1351. bpc, unit_bits, cval->lower_bound,
  1352. cval->upper_bound, pc);
  1353. if(ret > 0) RETURN(RC_FAIL);
  1354. if(ret < 0) RETURN(RC_WMORE);
  1355. st->size += len_bytes;
  1356. } while(repeat);
  1357. st->buf[st->size] = 0; /* nul-terminate */
  1358. return rval;
  1359. }
  1360. asn_enc_rval_t
  1361. OCTET_STRING_encode_uper(const asn_TYPE_descriptor_t *td,
  1362. const asn_per_constraints_t *constraints,
  1363. const void *sptr, asn_per_outp_t *po) {
  1364. const asn_OCTET_STRING_specifics_t *specs = td->specifics
  1365. ? (const asn_OCTET_STRING_specifics_t *)td->specifics
  1366. : &asn_SPC_OCTET_STRING_specs;
  1367. const asn_per_constraints_t *pc = constraints ? constraints
  1368. : td->encoding_constraints.per_constraints;
  1369. const asn_per_constraint_t *cval;
  1370. const asn_per_constraint_t *csiz;
  1371. const OCTET_STRING_t *st = (const OCTET_STRING_t *)sptr;
  1372. asn_enc_rval_t er = { 0, 0, 0 };
  1373. int inext = 0; /* Lies not within extension root */
  1374. unsigned int unit_bits;
  1375. unsigned int canonical_unit_bits;
  1376. size_t size_in_units;
  1377. const uint8_t *buf;
  1378. int ret;
  1379. enum {
  1380. OS__BPC_CHAR = 1,
  1381. OS__BPC_U16 = 2,
  1382. OS__BPC_U32 = 4
  1383. } bpc; /* Bytes per character */
  1384. int ct_extensible;
  1385. if(!st || (!st->buf && st->size))
  1386. ASN__ENCODE_FAILED;
  1387. if(pc) {
  1388. cval = &pc->value;
  1389. csiz = &pc->size;
  1390. } else {
  1391. cval = &asn_DEF_OCTET_STRING_constraints.value;
  1392. csiz = &asn_DEF_OCTET_STRING_constraints.size;
  1393. }
  1394. ct_extensible = csiz->flags & APC_EXTENSIBLE;
  1395. switch(specs->subvariant) {
  1396. default:
  1397. case ASN_OSUBV_ANY:
  1398. case ASN_OSUBV_BIT:
  1399. ASN__ENCODE_FAILED;
  1400. case ASN_OSUBV_STR:
  1401. canonical_unit_bits = unit_bits = 8;
  1402. if(cval->flags & APC_CONSTRAINED)
  1403. unit_bits = cval->range_bits;
  1404. bpc = OS__BPC_CHAR;
  1405. size_in_units = st->size;
  1406. break;
  1407. case ASN_OSUBV_U16:
  1408. canonical_unit_bits = unit_bits = 16;
  1409. if(cval->flags & APC_CONSTRAINED)
  1410. unit_bits = cval->range_bits;
  1411. bpc = OS__BPC_U16;
  1412. size_in_units = st->size >> 1;
  1413. if(st->size & 1) {
  1414. ASN_DEBUG("%s string size is not modulo 2", td->name);
  1415. ASN__ENCODE_FAILED;
  1416. }
  1417. break;
  1418. case ASN_OSUBV_U32:
  1419. canonical_unit_bits = unit_bits = 32;
  1420. if(cval->flags & APC_CONSTRAINED)
  1421. unit_bits = cval->range_bits;
  1422. bpc = OS__BPC_U32;
  1423. size_in_units = st->size >> 2;
  1424. if(st->size & 3) {
  1425. ASN_DEBUG("%s string size is not modulo 4", td->name);
  1426. ASN__ENCODE_FAILED;
  1427. }
  1428. break;
  1429. }
  1430. ASN_DEBUG("Encoding %s into %" ASN_PRI_SIZE " units of %d bits"
  1431. " (%ld..%ld, effective %d)%s",
  1432. td->name, size_in_units, unit_bits,
  1433. csiz->lower_bound, csiz->upper_bound,
  1434. csiz->effective_bits, ct_extensible ? " EXT" : "");
  1435. /* Figure out whether size lies within PER visible constraint */
  1436. if(csiz->effective_bits >= 0) {
  1437. if((ssize_t)size_in_units < csiz->lower_bound
  1438. || (ssize_t)size_in_units > csiz->upper_bound) {
  1439. if(ct_extensible) {
  1440. csiz = &asn_DEF_OCTET_STRING_constraints.size;
  1441. unit_bits = canonical_unit_bits;
  1442. inext = 1;
  1443. } else {
  1444. ASN__ENCODE_FAILED;
  1445. }
  1446. }
  1447. } else {
  1448. inext = 0;
  1449. }
  1450. if(ct_extensible) {
  1451. /* Declare whether length is [not] within extension root */
  1452. if(per_put_few_bits(po, inext, 1))
  1453. ASN__ENCODE_FAILED;
  1454. }
  1455. if(csiz->effective_bits >= 0 && !inext) {
  1456. ASN_DEBUG("Encoding %" ASN_PRI_SIZE " bytes (%ld), length in %d bits", st->size,
  1457. size_in_units - csiz->lower_bound, csiz->effective_bits);
  1458. ret = per_put_few_bits(po, size_in_units - csiz->lower_bound,
  1459. csiz->effective_bits);
  1460. if(ret) ASN__ENCODE_FAILED;
  1461. ret = OCTET_STRING_per_put_characters(po, st->buf, size_in_units, bpc,
  1462. unit_bits, cval->lower_bound,
  1463. cval->upper_bound, pc);
  1464. if(ret) ASN__ENCODE_FAILED;
  1465. ASN__ENCODED_OK(er);
  1466. }
  1467. ASN_DEBUG("Encoding %" ASN_PRI_SIZE " bytes", st->size);
  1468. buf = st->buf;
  1469. ASN_DEBUG("Encoding %" ASN_PRI_SIZE " in units", size_in_units);
  1470. do {
  1471. int need_eom = 0;
  1472. ssize_t may_save = uper_put_length(po, size_in_units, &need_eom);
  1473. if(may_save < 0) ASN__ENCODE_FAILED;
  1474. ASN_DEBUG("Encoding %" ASN_PRI_SSIZE " of %" ASN_PRI_SIZE "%s", may_save, size_in_units,
  1475. need_eom ? ",+EOM" : "");
  1476. ret = OCTET_STRING_per_put_characters(po, buf, may_save, bpc, unit_bits,
  1477. cval->lower_bound,
  1478. cval->upper_bound, pc);
  1479. if(ret) ASN__ENCODE_FAILED;
  1480. buf += may_save * bpc;
  1481. size_in_units -= may_save;
  1482. assert(!(may_save & 0x07) || !size_in_units);
  1483. if(need_eom && uper_put_length(po, 0, 0))
  1484. ASN__ENCODE_FAILED; /* End of Message length */
  1485. } while(size_in_units);
  1486. ASN__ENCODED_OK(er);
  1487. }
  1488. #endif /* ASN_DISABLE_PER_SUPPORT */
  1489. int
  1490. OCTET_STRING_print(const asn_TYPE_descriptor_t *td, const void *sptr,
  1491. int ilevel, asn_app_consume_bytes_f *cb, void *app_key) {
  1492. const char * const h2c = "0123456789ABCDEF";
  1493. const OCTET_STRING_t *st = (const OCTET_STRING_t *)sptr;
  1494. char scratch[16 * 3 + 4];
  1495. char *p = scratch;
  1496. uint8_t *buf;
  1497. uint8_t *end;
  1498. size_t i;
  1499. (void)td; /* Unused argument */
  1500. if(!st || (!st->buf && st->size))
  1501. return (cb("<absent>", 8, app_key) < 0) ? -1 : 0;
  1502. /*
  1503. * Dump the contents of the buffer in hexadecimal.
  1504. */
  1505. buf = st->buf;
  1506. end = buf + st->size;
  1507. for(i = 0; buf < end; buf++, i++) {
  1508. if(!(i % 16) && (i || st->size > 16)) {
  1509. if(cb(scratch, p - scratch, app_key) < 0)
  1510. return -1;
  1511. _i_INDENT(1);
  1512. p = scratch;
  1513. }
  1514. *p++ = h2c[(*buf >> 4) & 0x0F];
  1515. *p++ = h2c[*buf & 0x0F];
  1516. *p++ = 0x20;
  1517. }
  1518. if(p > scratch) {
  1519. p--; /* Remove the tail space */
  1520. if(cb(scratch, p - scratch, app_key) < 0)
  1521. return -1;
  1522. }
  1523. return 0;
  1524. }
  1525. int
  1526. OCTET_STRING_print_utf8(const asn_TYPE_descriptor_t *td, const void *sptr,
  1527. int ilevel, asn_app_consume_bytes_f *cb,
  1528. void *app_key) {
  1529. const OCTET_STRING_t *st = (const OCTET_STRING_t *)sptr;
  1530. (void)td; /* Unused argument */
  1531. (void)ilevel; /* Unused argument */
  1532. if(st && (st->buf || !st->size)) {
  1533. return (cb(st->buf, st->size, app_key) < 0) ? -1 : 0;
  1534. } else {
  1535. return (cb("<absent>", 8, app_key) < 0) ? -1 : 0;
  1536. }
  1537. }
  1538. void
  1539. OCTET_STRING_free(const asn_TYPE_descriptor_t *td, void *sptr,
  1540. enum asn_struct_free_method method) {
  1541. OCTET_STRING_t *st = (OCTET_STRING_t *)sptr;
  1542. const asn_OCTET_STRING_specifics_t *specs;
  1543. asn_struct_ctx_t *ctx;
  1544. struct _stack *stck;
  1545. if(!td || !st)
  1546. return;
  1547. specs = td->specifics
  1548. ? (const asn_OCTET_STRING_specifics_t *)td->specifics
  1549. : &asn_SPC_OCTET_STRING_specs;
  1550. ctx = (asn_struct_ctx_t *)((char *)st + specs->ctx_offset);
  1551. ASN_DEBUG("Freeing %s as OCTET STRING", td->name);
  1552. if(st->buf) {
  1553. FREEMEM(st->buf);
  1554. st->buf = 0;
  1555. }
  1556. /*
  1557. * Remove decode-time stack.
  1558. */
  1559. stck = (struct _stack *)ctx->ptr;
  1560. if(stck) {
  1561. while(stck->tail) {
  1562. struct _stack_el *sel = stck->tail;
  1563. stck->tail = sel->prev;
  1564. FREEMEM(sel);
  1565. }
  1566. FREEMEM(stck);
  1567. }
  1568. switch(method) {
  1569. case ASFM_FREE_EVERYTHING:
  1570. FREEMEM(sptr);
  1571. break;
  1572. case ASFM_FREE_UNDERLYING:
  1573. break;
  1574. case ASFM_FREE_UNDERLYING_AND_RESET:
  1575. memset(sptr, 0,
  1576. td->specifics
  1577. ? ((const asn_OCTET_STRING_specifics_t *)(td->specifics))
  1578. ->struct_size
  1579. : sizeof(OCTET_STRING_t));
  1580. break;
  1581. }
  1582. }
  1583. /*
  1584. * Conversion routines.
  1585. */
  1586. int
  1587. OCTET_STRING_fromBuf(OCTET_STRING_t *st, const char *str, int len) {
  1588. void *buf;
  1589. if(st == 0 || (str == 0 && len)) {
  1590. errno = EINVAL;
  1591. return -1;
  1592. }
  1593. /*
  1594. * Clear the OCTET STRING.
  1595. */
  1596. if(str == NULL) {
  1597. FREEMEM(st->buf);
  1598. st->buf = 0;
  1599. st->size = 0;
  1600. return 0;
  1601. }
  1602. /* Determine the original string size, if not explicitly given */
  1603. if(len < 0)
  1604. len = strlen(str);
  1605. /* Allocate and fill the memory */
  1606. buf = MALLOC(len + 1);
  1607. if(buf == NULL)
  1608. return -1;
  1609. memcpy(buf, str, len);
  1610. ((uint8_t *)buf)[len] = '\0'; /* Couldn't use memcpy(len+1)! */
  1611. FREEMEM(st->buf);
  1612. st->buf = (uint8_t *)buf;
  1613. st->size = len;
  1614. return 0;
  1615. }
  1616. OCTET_STRING_t *
  1617. OCTET_STRING_new_fromBuf(const asn_TYPE_descriptor_t *td, const char *str,
  1618. int len) {
  1619. const asn_OCTET_STRING_specifics_t *specs =
  1620. td->specifics ? (const asn_OCTET_STRING_specifics_t *)td->specifics
  1621. : &asn_SPC_OCTET_STRING_specs;
  1622. OCTET_STRING_t *st;
  1623. st = (OCTET_STRING_t *)CALLOC(1, specs->struct_size);
  1624. if(st && str && OCTET_STRING_fromBuf(st, str, len)) {
  1625. FREEMEM(st);
  1626. st = NULL;
  1627. }
  1628. return st;
  1629. }
  1630. /*
  1631. * Lexicographically compare the common prefix of both strings,
  1632. * and if it is the same return -1 for the smallest string.
  1633. */
  1634. int
  1635. OCTET_STRING_compare(const asn_TYPE_descriptor_t *td, const void *aptr,
  1636. const void *bptr) {
  1637. const asn_OCTET_STRING_specifics_t *specs = td->specifics;
  1638. const OCTET_STRING_t *a = aptr;
  1639. const OCTET_STRING_t *b = bptr;
  1640. assert(!specs || specs->subvariant != ASN_OSUBV_BIT);
  1641. if(a && b) {
  1642. size_t common_prefix_size = a->size <= b->size ? a->size : b->size;
  1643. int ret = memcmp(a->buf, b->buf, common_prefix_size);
  1644. if(ret == 0) {
  1645. /* Figure out which string with equal prefixes is longer. */
  1646. if(a->size < b->size) {
  1647. return -1;
  1648. } else if(a->size > b->size) {
  1649. return 1;
  1650. } else {
  1651. return 0;
  1652. }
  1653. } else {
  1654. return ret < 0 ? -1 : 1;
  1655. }
  1656. } else if(!a && !b) {
  1657. return 0;
  1658. } else if(!a) {
  1659. return -1;
  1660. } else {
  1661. return 1;
  1662. }
  1663. }
  1664. /*
  1665. * Biased function for randomizing character values around their limits.
  1666. */
  1667. static uint32_t
  1668. OCTET_STRING__random_char(unsigned long lb, unsigned long ub) {
  1669. assert(lb <= ub);
  1670. switch(asn_random_between(0, 16)) {
  1671. case 0:
  1672. if(lb < ub) return lb + 1;
  1673. /* Fall through */
  1674. case 1:
  1675. return lb;
  1676. case 2:
  1677. if(lb < ub) return ub - 1;
  1678. /* Fall through */
  1679. case 3:
  1680. return ub;
  1681. default:
  1682. return asn_random_between(lb, ub);
  1683. }
  1684. }
  1685. size_t
  1686. OCTET_STRING_random_length_constrained(
  1687. const asn_TYPE_descriptor_t *td,
  1688. const asn_encoding_constraints_t *constraints, size_t max_length) {
  1689. const unsigned lengths[] = {0, 1, 2, 3, 4, 8,
  1690. 126, 127, 128, 16383, 16384, 16385,
  1691. 65534, 65535, 65536, 65537};
  1692. size_t rnd_len;
  1693. /* Figure out how far we should go */
  1694. rnd_len = lengths[asn_random_between(
  1695. 0, sizeof(lengths) / sizeof(lengths[0]) - 1)];
  1696. if(!constraints || !constraints->per_constraints)
  1697. constraints = &td->encoding_constraints;
  1698. if(constraints->per_constraints) {
  1699. const asn_per_constraint_t *pc = &constraints->per_constraints->size;
  1700. if(pc->flags & APC_CONSTRAINED) {
  1701. long suggested_upper_bound = pc->upper_bound < (ssize_t)max_length
  1702. ? pc->upper_bound
  1703. : (ssize_t)max_length;
  1704. if(max_length <= (size_t)pc->lower_bound) {
  1705. return pc->lower_bound;
  1706. }
  1707. if(pc->flags & APC_EXTENSIBLE) {
  1708. switch(asn_random_between(0, 5)) {
  1709. case 0:
  1710. if(pc->lower_bound > 0) {
  1711. rnd_len = pc->lower_bound - 1;
  1712. break;
  1713. }
  1714. /* Fall through */
  1715. case 1:
  1716. rnd_len = pc->upper_bound + 1;
  1717. break;
  1718. case 2:
  1719. /* Keep rnd_len from the table */
  1720. if(rnd_len <= max_length) {
  1721. break;
  1722. }
  1723. /* Fall through */
  1724. default:
  1725. rnd_len = asn_random_between(pc->lower_bound,
  1726. suggested_upper_bound);
  1727. }
  1728. } else {
  1729. rnd_len =
  1730. asn_random_between(pc->lower_bound, suggested_upper_bound);
  1731. }
  1732. } else {
  1733. rnd_len = asn_random_between(0, max_length);
  1734. }
  1735. } else if(rnd_len > max_length) {
  1736. rnd_len = asn_random_between(0, max_length);
  1737. }
  1738. return rnd_len;
  1739. }
  1740. asn_random_fill_result_t
  1741. OCTET_STRING_random_fill(const asn_TYPE_descriptor_t *td, void **sptr,
  1742. const asn_encoding_constraints_t *constraints,
  1743. size_t max_length) {
  1744. const asn_OCTET_STRING_specifics_t *specs = td->specifics
  1745. ? (const asn_OCTET_STRING_specifics_t *)td->specifics
  1746. : &asn_SPC_OCTET_STRING_specs;
  1747. asn_random_fill_result_t result_ok = {ARFILL_OK, 1};
  1748. asn_random_fill_result_t result_failed = {ARFILL_FAILED, 0};
  1749. asn_random_fill_result_t result_skipped = {ARFILL_SKIPPED, 0};
  1750. unsigned int unit_bytes = 1;
  1751. unsigned long clb = 0; /* Lower bound on char */
  1752. unsigned long cub = 255; /* Higher bound on char value */
  1753. uint8_t *buf;
  1754. uint8_t *bend;
  1755. uint8_t *b;
  1756. size_t rnd_len;
  1757. OCTET_STRING_t *st;
  1758. if(max_length == 0 && !*sptr) return result_skipped;
  1759. switch(specs->subvariant) {
  1760. default:
  1761. case ASN_OSUBV_ANY:
  1762. return result_failed;
  1763. case ASN_OSUBV_BIT:
  1764. /* Handled by BIT_STRING itself. */
  1765. return result_failed;
  1766. case ASN_OSUBV_STR:
  1767. unit_bytes = 1;
  1768. clb = 0;
  1769. cub = 255;
  1770. break;
  1771. case ASN_OSUBV_U16:
  1772. unit_bytes = 2;
  1773. clb = 0;
  1774. cub = 65535;
  1775. break;
  1776. case ASN_OSUBV_U32:
  1777. unit_bytes = 4;
  1778. clb = 0;
  1779. cub = 0x10FFFF;
  1780. break;
  1781. }
  1782. if(!constraints || !constraints->per_constraints)
  1783. constraints = &td->encoding_constraints;
  1784. if(constraints->per_constraints) {
  1785. const asn_per_constraint_t *pc = &constraints->per_constraints->value;
  1786. if(pc->flags & APC_SEMI_CONSTRAINED) {
  1787. clb = pc->lower_bound;
  1788. } else if(pc->flags & APC_CONSTRAINED) {
  1789. clb = pc->lower_bound;
  1790. cub = pc->upper_bound;
  1791. }
  1792. }
  1793. rnd_len =
  1794. OCTET_STRING_random_length_constrained(td, constraints, max_length);
  1795. buf = CALLOC(unit_bytes, rnd_len + 1);
  1796. if(!buf) return result_failed;
  1797. bend = &buf[unit_bytes * rnd_len];
  1798. switch(unit_bytes) {
  1799. case 1:
  1800. for(b = buf; b < bend; b += unit_bytes) {
  1801. *(uint8_t *)b = OCTET_STRING__random_char(clb, cub);
  1802. }
  1803. *(uint8_t *)b = 0;
  1804. break;
  1805. case 2:
  1806. for(b = buf; b < bend; b += unit_bytes) {
  1807. uint32_t code = OCTET_STRING__random_char(clb, cub);
  1808. b[0] = code >> 8;
  1809. b[1] = code;
  1810. }
  1811. *(uint16_t *)b = 0;
  1812. break;
  1813. case 4:
  1814. for(b = buf; b < bend; b += unit_bytes) {
  1815. uint32_t code = OCTET_STRING__random_char(clb, cub);
  1816. b[0] = code >> 24;
  1817. b[1] = code >> 16;
  1818. b[2] = code >> 8;
  1819. b[3] = code;
  1820. }
  1821. *(uint32_t *)b = 0;
  1822. break;
  1823. }
  1824. if(*sptr) {
  1825. st = *sptr;
  1826. FREEMEM(st->buf);
  1827. } else {
  1828. st = (OCTET_STRING_t *)(*sptr = CALLOC(1, specs->struct_size));
  1829. if(!st) {
  1830. FREEMEM(buf);
  1831. return result_failed;
  1832. }
  1833. }
  1834. st->buf = buf;
  1835. st->size = unit_bytes * rnd_len;
  1836. result_ok.length = st->size;
  1837. return result_ok;
  1838. }