tasn_enc.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609
  1. /*
  2. * Copyright 2000-2018 The OpenSSL Project Authors. All Rights Reserved.
  3. *
  4. * Licensed under the Apache License 2.0 (the "License"). You may not use
  5. * this file except in compliance with the License. You can obtain a copy
  6. * in the file LICENSE in the source distribution or at
  7. * https://www.openssl.org/source/license.html
  8. */
  9. #include <stddef.h>
  10. #include <string.h>
  11. #include "internal/cryptlib.h"
  12. #include <openssl/asn1.h>
  13. #include <openssl/asn1t.h>
  14. #include <openssl/objects.h>
  15. #include "internal/asn1_int.h"
  16. #include "asn1_locl.h"
  17. static int asn1_i2d_ex_primitive(ASN1_VALUE **pval, unsigned char **out,
  18. const ASN1_ITEM *it, int tag, int aclass);
  19. static int asn1_set_seq_out(STACK_OF(ASN1_VALUE) *sk, unsigned char **out,
  20. int skcontlen, const ASN1_ITEM *item,
  21. int do_sort, int iclass);
  22. static int asn1_template_ex_i2d(ASN1_VALUE **pval, unsigned char **out,
  23. const ASN1_TEMPLATE *tt, int tag, int aclass);
  24. static int asn1_item_flags_i2d(ASN1_VALUE *val, unsigned char **out,
  25. const ASN1_ITEM *it, int flags);
  26. static int asn1_ex_i2c(ASN1_VALUE **pval, unsigned char *cout, int *putype,
  27. const ASN1_ITEM *it);
  28. /*
  29. * Top level i2d equivalents: the 'ndef' variant instructs the encoder to use
  30. * indefinite length constructed encoding, where appropriate
  31. */
  32. int ASN1_item_ndef_i2d(ASN1_VALUE *val, unsigned char **out,
  33. const ASN1_ITEM *it)
  34. {
  35. return asn1_item_flags_i2d(val, out, it, ASN1_TFLG_NDEF);
  36. }
  37. int ASN1_item_i2d(ASN1_VALUE *val, unsigned char **out, const ASN1_ITEM *it)
  38. {
  39. return asn1_item_flags_i2d(val, out, it, 0);
  40. }
  41. /*
  42. * Encode an ASN1 item, this is use by the standard 'i2d' function. 'out'
  43. * points to a buffer to output the data to. The new i2d has one additional
  44. * feature. If the output buffer is NULL (i.e. *out == NULL) then a buffer is
  45. * allocated and populated with the encoding.
  46. */
  47. static int asn1_item_flags_i2d(ASN1_VALUE *val, unsigned char **out,
  48. const ASN1_ITEM *it, int flags)
  49. {
  50. if (out && !*out) {
  51. unsigned char *p, *buf;
  52. int len;
  53. len = ASN1_item_ex_i2d(&val, NULL, it, -1, flags);
  54. if (len <= 0)
  55. return len;
  56. if ((buf = OPENSSL_malloc(len)) == NULL) {
  57. ASN1err(ASN1_F_ASN1_ITEM_FLAGS_I2D, ERR_R_MALLOC_FAILURE);
  58. return -1;
  59. }
  60. p = buf;
  61. ASN1_item_ex_i2d(&val, &p, it, -1, flags);
  62. *out = buf;
  63. return len;
  64. }
  65. return ASN1_item_ex_i2d(&val, out, it, -1, flags);
  66. }
  67. /*
  68. * Encode an item, taking care of IMPLICIT tagging (if any). This function
  69. * performs the normal item handling: it can be used in external types.
  70. */
  71. int ASN1_item_ex_i2d(ASN1_VALUE **pval, unsigned char **out,
  72. const ASN1_ITEM *it, int tag, int aclass)
  73. {
  74. const ASN1_TEMPLATE *tt = NULL;
  75. int i, seqcontlen, seqlen, ndef = 1;
  76. const ASN1_EXTERN_FUNCS *ef;
  77. const ASN1_AUX *aux = it->funcs;
  78. ASN1_aux_cb *asn1_cb = 0;
  79. if ((it->itype != ASN1_ITYPE_PRIMITIVE) && !*pval)
  80. return 0;
  81. if (aux && aux->asn1_cb)
  82. asn1_cb = aux->asn1_cb;
  83. switch (it->itype) {
  84. case ASN1_ITYPE_PRIMITIVE:
  85. if (it->templates)
  86. return asn1_template_ex_i2d(pval, out, it->templates,
  87. tag, aclass);
  88. return asn1_i2d_ex_primitive(pval, out, it, tag, aclass);
  89. case ASN1_ITYPE_MSTRING:
  90. return asn1_i2d_ex_primitive(pval, out, it, -1, aclass);
  91. case ASN1_ITYPE_CHOICE:
  92. if (asn1_cb && !asn1_cb(ASN1_OP_I2D_PRE, pval, it, NULL))
  93. return 0;
  94. i = asn1_get_choice_selector(pval, it);
  95. if ((i >= 0) && (i < it->tcount)) {
  96. ASN1_VALUE **pchval;
  97. const ASN1_TEMPLATE *chtt;
  98. chtt = it->templates + i;
  99. pchval = asn1_get_field_ptr(pval, chtt);
  100. return asn1_template_ex_i2d(pchval, out, chtt, -1, aclass);
  101. }
  102. /* Fixme: error condition if selector out of range */
  103. if (asn1_cb && !asn1_cb(ASN1_OP_I2D_POST, pval, it, NULL))
  104. return 0;
  105. break;
  106. case ASN1_ITYPE_EXTERN:
  107. /* If new style i2d it does all the work */
  108. ef = it->funcs;
  109. return ef->asn1_ex_i2d(pval, out, it, tag, aclass);
  110. case ASN1_ITYPE_NDEF_SEQUENCE:
  111. /* Use indefinite length constructed if requested */
  112. if (aclass & ASN1_TFLG_NDEF)
  113. ndef = 2;
  114. /* fall through */
  115. case ASN1_ITYPE_SEQUENCE:
  116. i = asn1_enc_restore(&seqcontlen, out, pval, it);
  117. /* An error occurred */
  118. if (i < 0)
  119. return 0;
  120. /* We have a valid cached encoding... */
  121. if (i > 0)
  122. return seqcontlen;
  123. /* Otherwise carry on */
  124. seqcontlen = 0;
  125. /* If no IMPLICIT tagging set to SEQUENCE, UNIVERSAL */
  126. if (tag == -1) {
  127. tag = V_ASN1_SEQUENCE;
  128. /* Retain any other flags in aclass */
  129. aclass = (aclass & ~ASN1_TFLG_TAG_CLASS)
  130. | V_ASN1_UNIVERSAL;
  131. }
  132. if (asn1_cb && !asn1_cb(ASN1_OP_I2D_PRE, pval, it, NULL))
  133. return 0;
  134. /* First work out sequence content length */
  135. for (i = 0, tt = it->templates; i < it->tcount; tt++, i++) {
  136. const ASN1_TEMPLATE *seqtt;
  137. ASN1_VALUE **pseqval;
  138. int tmplen;
  139. seqtt = asn1_do_adb(pval, tt, 1);
  140. if (!seqtt)
  141. return 0;
  142. pseqval = asn1_get_field_ptr(pval, seqtt);
  143. tmplen = asn1_template_ex_i2d(pseqval, NULL, seqtt, -1, aclass);
  144. if (tmplen == -1 || (tmplen > INT_MAX - seqcontlen))
  145. return -1;
  146. seqcontlen += tmplen;
  147. }
  148. seqlen = ASN1_object_size(ndef, seqcontlen, tag);
  149. if (!out || seqlen == -1)
  150. return seqlen;
  151. /* Output SEQUENCE header */
  152. ASN1_put_object(out, ndef, seqcontlen, tag, aclass);
  153. for (i = 0, tt = it->templates; i < it->tcount; tt++, i++) {
  154. const ASN1_TEMPLATE *seqtt;
  155. ASN1_VALUE **pseqval;
  156. seqtt = asn1_do_adb(pval, tt, 1);
  157. if (!seqtt)
  158. return 0;
  159. pseqval = asn1_get_field_ptr(pval, seqtt);
  160. /* FIXME: check for errors in enhanced version */
  161. asn1_template_ex_i2d(pseqval, out, seqtt, -1, aclass);
  162. }
  163. if (ndef == 2)
  164. ASN1_put_eoc(out);
  165. if (asn1_cb && !asn1_cb(ASN1_OP_I2D_POST, pval, it, NULL))
  166. return 0;
  167. return seqlen;
  168. default:
  169. return 0;
  170. }
  171. return 0;
  172. }
  173. static int asn1_template_ex_i2d(ASN1_VALUE **pval, unsigned char **out,
  174. const ASN1_TEMPLATE *tt, int tag, int iclass)
  175. {
  176. int i, ret, flags, ttag, tclass, ndef;
  177. ASN1_VALUE *tval;
  178. flags = tt->flags;
  179. /*
  180. * If field is embedded then val needs fixing so it is a pointer to
  181. * a pointer to a field.
  182. */
  183. if (flags & ASN1_TFLG_EMBED) {
  184. tval = (ASN1_VALUE *)pval;
  185. pval = &tval;
  186. }
  187. /*
  188. * Work out tag and class to use: tagging may come either from the
  189. * template or the arguments, not both because this would create
  190. * ambiguity. Additionally the iclass argument may contain some
  191. * additional flags which should be noted and passed down to other
  192. * levels.
  193. */
  194. if (flags & ASN1_TFLG_TAG_MASK) {
  195. /* Error if argument and template tagging */
  196. if (tag != -1)
  197. /* FIXME: error code here */
  198. return -1;
  199. /* Get tagging from template */
  200. ttag = tt->tag;
  201. tclass = flags & ASN1_TFLG_TAG_CLASS;
  202. } else if (tag != -1) {
  203. /* No template tagging, get from arguments */
  204. ttag = tag;
  205. tclass = iclass & ASN1_TFLG_TAG_CLASS;
  206. } else {
  207. ttag = -1;
  208. tclass = 0;
  209. }
  210. /*
  211. * Remove any class mask from iflag.
  212. */
  213. iclass &= ~ASN1_TFLG_TAG_CLASS;
  214. /*
  215. * At this point 'ttag' contains the outer tag to use, 'tclass' is the
  216. * class and iclass is any flags passed to this function.
  217. */
  218. /* if template and arguments require ndef, use it */
  219. if ((flags & ASN1_TFLG_NDEF) && (iclass & ASN1_TFLG_NDEF))
  220. ndef = 2;
  221. else
  222. ndef = 1;
  223. if (flags & ASN1_TFLG_SK_MASK) {
  224. /* SET OF, SEQUENCE OF */
  225. STACK_OF(ASN1_VALUE) *sk = (STACK_OF(ASN1_VALUE) *)*pval;
  226. int isset, sktag, skaclass;
  227. int skcontlen, sklen;
  228. ASN1_VALUE *skitem;
  229. if (!*pval)
  230. return 0;
  231. if (flags & ASN1_TFLG_SET_OF) {
  232. isset = 1;
  233. /* 2 means we reorder */
  234. if (flags & ASN1_TFLG_SEQUENCE_OF)
  235. isset = 2;
  236. } else
  237. isset = 0;
  238. /*
  239. * Work out inner tag value: if EXPLICIT or no tagging use underlying
  240. * type.
  241. */
  242. if ((ttag != -1) && !(flags & ASN1_TFLG_EXPTAG)) {
  243. sktag = ttag;
  244. skaclass = tclass;
  245. } else {
  246. skaclass = V_ASN1_UNIVERSAL;
  247. if (isset)
  248. sktag = V_ASN1_SET;
  249. else
  250. sktag = V_ASN1_SEQUENCE;
  251. }
  252. /* Determine total length of items */
  253. skcontlen = 0;
  254. for (i = 0; i < sk_ASN1_VALUE_num(sk); i++) {
  255. int tmplen;
  256. skitem = sk_ASN1_VALUE_value(sk, i);
  257. tmplen = ASN1_item_ex_i2d(&skitem, NULL, ASN1_ITEM_ptr(tt->item),
  258. -1, iclass);
  259. if (tmplen == -1 || (skcontlen > INT_MAX - tmplen))
  260. return -1;
  261. skcontlen += tmplen;
  262. }
  263. sklen = ASN1_object_size(ndef, skcontlen, sktag);
  264. if (sklen == -1)
  265. return -1;
  266. /* If EXPLICIT need length of surrounding tag */
  267. if (flags & ASN1_TFLG_EXPTAG)
  268. ret = ASN1_object_size(ndef, sklen, ttag);
  269. else
  270. ret = sklen;
  271. if (!out || ret == -1)
  272. return ret;
  273. /* Now encode this lot... */
  274. /* EXPLICIT tag */
  275. if (flags & ASN1_TFLG_EXPTAG)
  276. ASN1_put_object(out, ndef, sklen, ttag, tclass);
  277. /* SET or SEQUENCE and IMPLICIT tag */
  278. ASN1_put_object(out, ndef, skcontlen, sktag, skaclass);
  279. /* And the stuff itself */
  280. asn1_set_seq_out(sk, out, skcontlen, ASN1_ITEM_ptr(tt->item),
  281. isset, iclass);
  282. if (ndef == 2) {
  283. ASN1_put_eoc(out);
  284. if (flags & ASN1_TFLG_EXPTAG)
  285. ASN1_put_eoc(out);
  286. }
  287. return ret;
  288. }
  289. if (flags & ASN1_TFLG_EXPTAG) {
  290. /* EXPLICIT tagging */
  291. /* Find length of tagged item */
  292. i = ASN1_item_ex_i2d(pval, NULL, ASN1_ITEM_ptr(tt->item), -1, iclass);
  293. if (!i)
  294. return 0;
  295. /* Find length of EXPLICIT tag */
  296. ret = ASN1_object_size(ndef, i, ttag);
  297. if (out && ret != -1) {
  298. /* Output tag and item */
  299. ASN1_put_object(out, ndef, i, ttag, tclass);
  300. ASN1_item_ex_i2d(pval, out, ASN1_ITEM_ptr(tt->item), -1, iclass);
  301. if (ndef == 2)
  302. ASN1_put_eoc(out);
  303. }
  304. return ret;
  305. }
  306. /* Either normal or IMPLICIT tagging: combine class and flags */
  307. return ASN1_item_ex_i2d(pval, out, ASN1_ITEM_ptr(tt->item),
  308. ttag, tclass | iclass);
  309. }
  310. /* Temporary structure used to hold DER encoding of items for SET OF */
  311. typedef struct {
  312. unsigned char *data;
  313. int length;
  314. ASN1_VALUE *field;
  315. } DER_ENC;
  316. static int der_cmp(const void *a, const void *b)
  317. {
  318. const DER_ENC *d1 = a, *d2 = b;
  319. int cmplen, i;
  320. cmplen = (d1->length < d2->length) ? d1->length : d2->length;
  321. i = memcmp(d1->data, d2->data, cmplen);
  322. if (i)
  323. return i;
  324. return d1->length - d2->length;
  325. }
  326. /* Output the content octets of SET OF or SEQUENCE OF */
  327. static int asn1_set_seq_out(STACK_OF(ASN1_VALUE) *sk, unsigned char **out,
  328. int skcontlen, const ASN1_ITEM *item,
  329. int do_sort, int iclass)
  330. {
  331. int i;
  332. ASN1_VALUE *skitem;
  333. unsigned char *tmpdat = NULL, *p = NULL;
  334. DER_ENC *derlst = NULL, *tder;
  335. if (do_sort) {
  336. /* Don't need to sort less than 2 items */
  337. if (sk_ASN1_VALUE_num(sk) < 2)
  338. do_sort = 0;
  339. else {
  340. derlst = OPENSSL_malloc(sk_ASN1_VALUE_num(sk)
  341. * sizeof(*derlst));
  342. if (derlst == NULL)
  343. return 0;
  344. tmpdat = OPENSSL_malloc(skcontlen);
  345. if (tmpdat == NULL) {
  346. OPENSSL_free(derlst);
  347. return 0;
  348. }
  349. }
  350. }
  351. /* If not sorting just output each item */
  352. if (!do_sort) {
  353. for (i = 0; i < sk_ASN1_VALUE_num(sk); i++) {
  354. skitem = sk_ASN1_VALUE_value(sk, i);
  355. ASN1_item_ex_i2d(&skitem, out, item, -1, iclass);
  356. }
  357. return 1;
  358. }
  359. p = tmpdat;
  360. /* Doing sort: build up a list of each member's DER encoding */
  361. for (i = 0, tder = derlst; i < sk_ASN1_VALUE_num(sk); i++, tder++) {
  362. skitem = sk_ASN1_VALUE_value(sk, i);
  363. tder->data = p;
  364. tder->length = ASN1_item_ex_i2d(&skitem, &p, item, -1, iclass);
  365. tder->field = skitem;
  366. }
  367. /* Now sort them */
  368. qsort(derlst, sk_ASN1_VALUE_num(sk), sizeof(*derlst), der_cmp);
  369. /* Output sorted DER encoding */
  370. p = *out;
  371. for (i = 0, tder = derlst; i < sk_ASN1_VALUE_num(sk); i++, tder++) {
  372. memcpy(p, tder->data, tder->length);
  373. p += tder->length;
  374. }
  375. *out = p;
  376. /* If do_sort is 2 then reorder the STACK */
  377. if (do_sort == 2) {
  378. for (i = 0, tder = derlst; i < sk_ASN1_VALUE_num(sk); i++, tder++)
  379. (void)sk_ASN1_VALUE_set(sk, i, tder->field);
  380. }
  381. OPENSSL_free(derlst);
  382. OPENSSL_free(tmpdat);
  383. return 1;
  384. }
  385. static int asn1_i2d_ex_primitive(ASN1_VALUE **pval, unsigned char **out,
  386. const ASN1_ITEM *it, int tag, int aclass)
  387. {
  388. int len;
  389. int utype;
  390. int usetag;
  391. int ndef = 0;
  392. utype = it->utype;
  393. /*
  394. * Get length of content octets and maybe find out the underlying type.
  395. */
  396. len = asn1_ex_i2c(pval, NULL, &utype, it);
  397. /*
  398. * If SEQUENCE, SET or OTHER then header is included in pseudo content
  399. * octets so don't include tag+length. We need to check here because the
  400. * call to asn1_ex_i2c() could change utype.
  401. */
  402. if ((utype == V_ASN1_SEQUENCE) || (utype == V_ASN1_SET) ||
  403. (utype == V_ASN1_OTHER))
  404. usetag = 0;
  405. else
  406. usetag = 1;
  407. /* -1 means omit type */
  408. if (len == -1)
  409. return 0;
  410. /* -2 return is special meaning use ndef */
  411. if (len == -2) {
  412. ndef = 2;
  413. len = 0;
  414. }
  415. /* If not implicitly tagged get tag from underlying type */
  416. if (tag == -1)
  417. tag = utype;
  418. /* Output tag+length followed by content octets */
  419. if (out) {
  420. if (usetag)
  421. ASN1_put_object(out, ndef, len, tag, aclass);
  422. asn1_ex_i2c(pval, *out, &utype, it);
  423. if (ndef)
  424. ASN1_put_eoc(out);
  425. else
  426. *out += len;
  427. }
  428. if (usetag)
  429. return ASN1_object_size(ndef, len, tag);
  430. return len;
  431. }
  432. /* Produce content octets from a structure */
  433. static int asn1_ex_i2c(ASN1_VALUE **pval, unsigned char *cout, int *putype,
  434. const ASN1_ITEM *it)
  435. {
  436. ASN1_BOOLEAN *tbool = NULL;
  437. ASN1_STRING *strtmp;
  438. ASN1_OBJECT *otmp;
  439. int utype;
  440. const unsigned char *cont;
  441. unsigned char c;
  442. int len;
  443. const ASN1_PRIMITIVE_FUNCS *pf;
  444. pf = it->funcs;
  445. if (pf && pf->prim_i2c)
  446. return pf->prim_i2c(pval, cout, putype, it);
  447. /* Should type be omitted? */
  448. if ((it->itype != ASN1_ITYPE_PRIMITIVE)
  449. || (it->utype != V_ASN1_BOOLEAN)) {
  450. if (!*pval)
  451. return -1;
  452. }
  453. if (it->itype == ASN1_ITYPE_MSTRING) {
  454. /* If MSTRING type set the underlying type */
  455. strtmp = (ASN1_STRING *)*pval;
  456. utype = strtmp->type;
  457. *putype = utype;
  458. } else if (it->utype == V_ASN1_ANY) {
  459. /* If ANY set type and pointer to value */
  460. ASN1_TYPE *typ;
  461. typ = (ASN1_TYPE *)*pval;
  462. utype = typ->type;
  463. *putype = utype;
  464. pval = &typ->value.asn1_value;
  465. } else
  466. utype = *putype;
  467. switch (utype) {
  468. case V_ASN1_OBJECT:
  469. otmp = (ASN1_OBJECT *)*pval;
  470. cont = otmp->data;
  471. len = otmp->length;
  472. if (cont == NULL || len == 0)
  473. return -1;
  474. break;
  475. case V_ASN1_NULL:
  476. cont = NULL;
  477. len = 0;
  478. break;
  479. case V_ASN1_BOOLEAN:
  480. tbool = (ASN1_BOOLEAN *)pval;
  481. if (*tbool == -1)
  482. return -1;
  483. if (it->utype != V_ASN1_ANY) {
  484. /*
  485. * Default handling if value == size field then omit
  486. */
  487. if (*tbool && (it->size > 0))
  488. return -1;
  489. if (!*tbool && !it->size)
  490. return -1;
  491. }
  492. c = (unsigned char)*tbool;
  493. cont = &c;
  494. len = 1;
  495. break;
  496. case V_ASN1_BIT_STRING:
  497. return i2c_ASN1_BIT_STRING((ASN1_BIT_STRING *)*pval,
  498. cout ? &cout : NULL);
  499. case V_ASN1_INTEGER:
  500. case V_ASN1_ENUMERATED:
  501. /*
  502. * These are all have the same content format as ASN1_INTEGER
  503. */
  504. return i2c_ASN1_INTEGER((ASN1_INTEGER *)*pval, cout ? &cout : NULL);
  505. case V_ASN1_OCTET_STRING:
  506. case V_ASN1_NUMERICSTRING:
  507. case V_ASN1_PRINTABLESTRING:
  508. case V_ASN1_T61STRING:
  509. case V_ASN1_VIDEOTEXSTRING:
  510. case V_ASN1_IA5STRING:
  511. case V_ASN1_UTCTIME:
  512. case V_ASN1_GENERALIZEDTIME:
  513. case V_ASN1_GRAPHICSTRING:
  514. case V_ASN1_VISIBLESTRING:
  515. case V_ASN1_GENERALSTRING:
  516. case V_ASN1_UNIVERSALSTRING:
  517. case V_ASN1_BMPSTRING:
  518. case V_ASN1_UTF8STRING:
  519. case V_ASN1_SEQUENCE:
  520. case V_ASN1_SET:
  521. default:
  522. /* All based on ASN1_STRING and handled the same */
  523. strtmp = (ASN1_STRING *)*pval;
  524. /* Special handling for NDEF */
  525. if ((it->size == ASN1_TFLG_NDEF)
  526. && (strtmp->flags & ASN1_STRING_FLAG_NDEF)) {
  527. if (cout) {
  528. strtmp->data = cout;
  529. strtmp->length = 0;
  530. }
  531. /* Special return code */
  532. return -2;
  533. }
  534. cont = strtmp->data;
  535. len = strtmp->length;
  536. break;
  537. }
  538. if (cout && len)
  539. memcpy(cout, cont, len);
  540. return len;
  541. }