Remove old style ASN.1 support.
[openssl.git] / crypto / asn1 / tasn_enc.c
1 /* tasn_enc.c */
2 /*
3  * Written by Dr Stephen N Henson (steve@openssl.org) for the OpenSSL project
4  * 2000.
5  */
6 /* ====================================================================
7  * Copyright (c) 2000-2004 The OpenSSL Project.  All rights reserved.
8  *
9  * Redistribution and use in source and binary forms, with or without
10  * modification, are permitted provided that the following conditions
11  * are met:
12  *
13  * 1. Redistributions of source code must retain the above copyright
14  *    notice, this list of conditions and the following disclaimer.
15  *
16  * 2. Redistributions in binary form must reproduce the above copyright
17  *    notice, this list of conditions and the following disclaimer in
18  *    the documentation and/or other materials provided with the
19  *    distribution.
20  *
21  * 3. All advertising materials mentioning features or use of this
22  *    software must display the following acknowledgment:
23  *    "This product includes software developed by the OpenSSL Project
24  *    for use in the OpenSSL Toolkit. (http://www.OpenSSL.org/)"
25  *
26  * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
27  *    endorse or promote products derived from this software without
28  *    prior written permission. For written permission, please contact
29  *    licensing@OpenSSL.org.
30  *
31  * 5. Products derived from this software may not be called "OpenSSL"
32  *    nor may "OpenSSL" appear in their names without prior written
33  *    permission of the OpenSSL Project.
34  *
35  * 6. Redistributions of any form whatsoever must retain the following
36  *    acknowledgment:
37  *    "This product includes software developed by the OpenSSL Project
38  *    for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)"
39  *
40  * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
41  * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
42  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
43  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE OpenSSL PROJECT OR
44  * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
45  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
46  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
47  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
48  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
49  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
50  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
51  * OF THE POSSIBILITY OF SUCH DAMAGE.
52  * ====================================================================
53  *
54  * This product includes cryptographic software written by Eric Young
55  * (eay@cryptsoft.com).  This product includes software written by Tim
56  * Hudson (tjh@cryptsoft.com).
57  *
58  */
59
60 #include <stddef.h>
61 #include <string.h>
62 #include "cryptlib.h"
63 #include <openssl/asn1.h>
64 #include <openssl/asn1t.h>
65 #include <openssl/objects.h>
66
67 static int asn1_i2d_ex_primitive(ASN1_VALUE **pval, unsigned char **out,
68                                  const ASN1_ITEM *it, int tag, int aclass);
69 static int asn1_set_seq_out(STACK_OF(ASN1_VALUE) *sk, unsigned char **out,
70                             int skcontlen, const ASN1_ITEM *item,
71                             int do_sort, int iclass);
72 static int asn1_template_ex_i2d(ASN1_VALUE **pval, unsigned char **out,
73                                 const ASN1_TEMPLATE *tt, int tag, int aclass);
74 static int asn1_item_flags_i2d(ASN1_VALUE *val, unsigned char **out,
75                                const ASN1_ITEM *it, int flags);
76
77 /*
78  * Top level i2d equivalents: the 'ndef' variant instructs the encoder to use
79  * indefinite length constructed encoding, where appropriate
80  */
81
82 int ASN1_item_ndef_i2d(ASN1_VALUE *val, unsigned char **out,
83                        const ASN1_ITEM *it)
84 {
85     return asn1_item_flags_i2d(val, out, it, ASN1_TFLG_NDEF);
86 }
87
88 int ASN1_item_i2d(ASN1_VALUE *val, unsigned char **out, const ASN1_ITEM *it)
89 {
90     return asn1_item_flags_i2d(val, out, it, 0);
91 }
92
93 /*
94  * Encode an ASN1 item, this is use by the standard 'i2d' function. 'out'
95  * points to a buffer to output the data to. The new i2d has one additional
96  * feature. If the output buffer is NULL (i.e. *out == NULL) then a buffer is
97  * allocated and populated with the encoding.
98  */
99
100 static int asn1_item_flags_i2d(ASN1_VALUE *val, unsigned char **out,
101                                const ASN1_ITEM *it, int flags)
102 {
103     if (out && !*out) {
104         unsigned char *p, *buf;
105         int len;
106         len = ASN1_item_ex_i2d(&val, NULL, it, -1, flags);
107         if (len <= 0)
108             return len;
109         buf = OPENSSL_malloc(len);
110         if (!buf)
111             return -1;
112         p = buf;
113         ASN1_item_ex_i2d(&val, &p, it, -1, flags);
114         *out = buf;
115         return len;
116     }
117
118     return ASN1_item_ex_i2d(&val, out, it, -1, flags);
119 }
120
121 /*
122  * Encode an item, taking care of IMPLICIT tagging (if any). This function
123  * performs the normal item handling: it can be used in external types.
124  */
125
126 int ASN1_item_ex_i2d(ASN1_VALUE **pval, unsigned char **out,
127                      const ASN1_ITEM *it, int tag, int aclass)
128 {
129     const ASN1_TEMPLATE *tt = NULL;
130     int i, seqcontlen, seqlen, ndef = 1;
131     const ASN1_EXTERN_FUNCS *ef;
132     const ASN1_AUX *aux = it->funcs;
133     ASN1_aux_cb *asn1_cb = 0;
134
135     if ((it->itype != ASN1_ITYPE_PRIMITIVE) && !*pval)
136         return 0;
137
138     if (aux && aux->asn1_cb)
139         asn1_cb = aux->asn1_cb;
140
141     switch (it->itype) {
142
143     case ASN1_ITYPE_PRIMITIVE:
144         if (it->templates)
145             return asn1_template_ex_i2d(pval, out, it->templates,
146                                         tag, aclass);
147         return asn1_i2d_ex_primitive(pval, out, it, tag, aclass);
148
149     case ASN1_ITYPE_MSTRING:
150         return asn1_i2d_ex_primitive(pval, out, it, -1, aclass);
151
152     case ASN1_ITYPE_CHOICE:
153         if (asn1_cb && !asn1_cb(ASN1_OP_I2D_PRE, pval, it, NULL))
154             return 0;
155         i = asn1_get_choice_selector(pval, it);
156         if ((i >= 0) && (i < it->tcount)) {
157             ASN1_VALUE **pchval;
158             const ASN1_TEMPLATE *chtt;
159             chtt = it->templates + i;
160             pchval = asn1_get_field_ptr(pval, chtt);
161             return asn1_template_ex_i2d(pchval, out, chtt, -1, aclass);
162         }
163         /* Fixme: error condition if selector out of range */
164         if (asn1_cb && !asn1_cb(ASN1_OP_I2D_POST, pval, it, NULL))
165             return 0;
166         break;
167
168     case ASN1_ITYPE_EXTERN:
169         /* If new style i2d it does all the work */
170         ef = it->funcs;
171         return ef->asn1_ex_i2d(pval, out, it, tag, aclass);
172
173     case ASN1_ITYPE_NDEF_SEQUENCE:
174         /* Use indefinite length constructed if requested */
175         if (aclass & ASN1_TFLG_NDEF)
176             ndef = 2;
177         /* fall through */
178
179     case ASN1_ITYPE_SEQUENCE:
180         i = asn1_enc_restore(&seqcontlen, out, pval, it);
181         /* An error occurred */
182         if (i < 0)
183             return 0;
184         /* We have a valid cached encoding... */
185         if (i > 0)
186             return seqcontlen;
187         /* Otherwise carry on */
188         seqcontlen = 0;
189         /* If no IMPLICIT tagging set to SEQUENCE, UNIVERSAL */
190         if (tag == -1) {
191             tag = V_ASN1_SEQUENCE;
192             /* Retain any other flags in aclass */
193             aclass = (aclass & ~ASN1_TFLG_TAG_CLASS)
194                 | V_ASN1_UNIVERSAL;
195         }
196         if (asn1_cb && !asn1_cb(ASN1_OP_I2D_PRE, pval, it, NULL))
197             return 0;
198         /* First work out sequence content length */
199         for (i = 0, tt = it->templates; i < it->tcount; tt++, i++) {
200             const ASN1_TEMPLATE *seqtt;
201             ASN1_VALUE **pseqval;
202             seqtt = asn1_do_adb(pval, tt, 1);
203             if (!seqtt)
204                 return 0;
205             pseqval = asn1_get_field_ptr(pval, seqtt);
206             /* FIXME: check for errors in enhanced version */
207             seqcontlen += asn1_template_ex_i2d(pseqval, NULL, seqtt,
208                                                -1, aclass);
209         }
210
211         seqlen = ASN1_object_size(ndef, seqcontlen, tag);
212         if (!out)
213             return seqlen;
214         /* Output SEQUENCE header */
215         ASN1_put_object(out, ndef, seqcontlen, tag, aclass);
216         for (i = 0, tt = it->templates; i < it->tcount; tt++, i++) {
217             const ASN1_TEMPLATE *seqtt;
218             ASN1_VALUE **pseqval;
219             seqtt = asn1_do_adb(pval, tt, 1);
220             if (!seqtt)
221                 return 0;
222             pseqval = asn1_get_field_ptr(pval, seqtt);
223             /* FIXME: check for errors in enhanced version */
224             asn1_template_ex_i2d(pseqval, out, seqtt, -1, aclass);
225         }
226         if (ndef == 2)
227             ASN1_put_eoc(out);
228         if (asn1_cb && !asn1_cb(ASN1_OP_I2D_POST, pval, it, NULL))
229             return 0;
230         return seqlen;
231
232     default:
233         return 0;
234
235     }
236     return 0;
237 }
238
239 int ASN1_template_i2d(ASN1_VALUE **pval, unsigned char **out,
240                       const ASN1_TEMPLATE *tt)
241 {
242     return asn1_template_ex_i2d(pval, out, tt, -1, 0);
243 }
244
245 static int asn1_template_ex_i2d(ASN1_VALUE **pval, unsigned char **out,
246                                 const ASN1_TEMPLATE *tt, int tag, int iclass)
247 {
248     int i, ret, flags, ttag, tclass, ndef;
249     flags = tt->flags;
250     /*
251      * Work out tag and class to use: tagging may come either from the
252      * template or the arguments, not both because this would create
253      * ambiguity. Additionally the iclass argument may contain some
254      * additional flags which should be noted and passed down to other
255      * levels.
256      */
257     if (flags & ASN1_TFLG_TAG_MASK) {
258         /* Error if argument and template tagging */
259         if (tag != -1)
260             /* FIXME: error code here */
261             return -1;
262         /* Get tagging from template */
263         ttag = tt->tag;
264         tclass = flags & ASN1_TFLG_TAG_CLASS;
265     } else if (tag != -1) {
266         /* No template tagging, get from arguments */
267         ttag = tag;
268         tclass = iclass & ASN1_TFLG_TAG_CLASS;
269     } else {
270         ttag = -1;
271         tclass = 0;
272     }
273     /*
274      * Remove any class mask from iflag.
275      */
276     iclass &= ~ASN1_TFLG_TAG_CLASS;
277
278     /*
279      * At this point 'ttag' contains the outer tag to use, 'tclass' is the
280      * class and iclass is any flags passed to this function.
281      */
282
283     /* if template and arguments require ndef, use it */
284     if ((flags & ASN1_TFLG_NDEF) && (iclass & ASN1_TFLG_NDEF))
285         ndef = 2;
286     else
287         ndef = 1;
288
289     if (flags & ASN1_TFLG_SK_MASK) {
290         /* SET OF, SEQUENCE OF */
291         STACK_OF(ASN1_VALUE) *sk = (STACK_OF(ASN1_VALUE) *)*pval;
292         int isset, sktag, skaclass;
293         int skcontlen, sklen;
294         ASN1_VALUE *skitem;
295
296         if (!*pval)
297             return 0;
298
299         if (flags & ASN1_TFLG_SET_OF) {
300             isset = 1;
301             /* 2 means we reorder */
302             if (flags & ASN1_TFLG_SEQUENCE_OF)
303                 isset = 2;
304         } else
305             isset = 0;
306
307         /*
308          * Work out inner tag value: if EXPLICIT or no tagging use underlying
309          * type.
310          */
311         if ((ttag != -1) && !(flags & ASN1_TFLG_EXPTAG)) {
312             sktag = ttag;
313             skaclass = tclass;
314         } else {
315             skaclass = V_ASN1_UNIVERSAL;
316             if (isset)
317                 sktag = V_ASN1_SET;
318             else
319                 sktag = V_ASN1_SEQUENCE;
320         }
321
322         /* Determine total length of items */
323         skcontlen = 0;
324         for (i = 0; i < sk_ASN1_VALUE_num(sk); i++) {
325             skitem = sk_ASN1_VALUE_value(sk, i);
326             skcontlen += ASN1_item_ex_i2d(&skitem, NULL,
327                                           ASN1_ITEM_ptr(tt->item),
328                                           -1, iclass);
329         }
330         sklen = ASN1_object_size(ndef, skcontlen, sktag);
331         /* If EXPLICIT need length of surrounding tag */
332         if (flags & ASN1_TFLG_EXPTAG)
333             ret = ASN1_object_size(ndef, sklen, ttag);
334         else
335             ret = sklen;
336
337         if (!out)
338             return ret;
339
340         /* Now encode this lot... */
341         /* EXPLICIT tag */
342         if (flags & ASN1_TFLG_EXPTAG)
343             ASN1_put_object(out, ndef, sklen, ttag, tclass);
344         /* SET or SEQUENCE and IMPLICIT tag */
345         ASN1_put_object(out, ndef, skcontlen, sktag, skaclass);
346         /* And the stuff itself */
347         asn1_set_seq_out(sk, out, skcontlen, ASN1_ITEM_ptr(tt->item),
348                          isset, iclass);
349         if (ndef == 2) {
350             ASN1_put_eoc(out);
351             if (flags & ASN1_TFLG_EXPTAG)
352                 ASN1_put_eoc(out);
353         }
354
355         return ret;
356     }
357
358     if (flags & ASN1_TFLG_EXPTAG) {
359         /* EXPLICIT tagging */
360         /* Find length of tagged item */
361         i = ASN1_item_ex_i2d(pval, NULL, ASN1_ITEM_ptr(tt->item), -1, iclass);
362         if (!i)
363             return 0;
364         /* Find length of EXPLICIT tag */
365         ret = ASN1_object_size(ndef, i, ttag);
366         if (out) {
367             /* Output tag and item */
368             ASN1_put_object(out, ndef, i, ttag, tclass);
369             ASN1_item_ex_i2d(pval, out, ASN1_ITEM_ptr(tt->item), -1, iclass);
370             if (ndef == 2)
371                 ASN1_put_eoc(out);
372         }
373         return ret;
374     }
375
376     /* Either normal or IMPLICIT tagging: combine class and flags */
377     return ASN1_item_ex_i2d(pval, out, ASN1_ITEM_ptr(tt->item),
378                             ttag, tclass | iclass);
379
380 }
381
382 /* Temporary structure used to hold DER encoding of items for SET OF */
383
384 typedef struct {
385     unsigned char *data;
386     int length;
387     ASN1_VALUE *field;
388 } DER_ENC;
389
390 static int der_cmp(const void *a, const void *b)
391 {
392     const DER_ENC *d1 = a, *d2 = b;
393     int cmplen, i;
394     cmplen = (d1->length < d2->length) ? d1->length : d2->length;
395     i = memcmp(d1->data, d2->data, cmplen);
396     if (i)
397         return i;
398     return d1->length - d2->length;
399 }
400
401 /* Output the content octets of SET OF or SEQUENCE OF */
402
403 static int asn1_set_seq_out(STACK_OF(ASN1_VALUE) *sk, unsigned char **out,
404                             int skcontlen, const ASN1_ITEM *item,
405                             int do_sort, int iclass)
406 {
407     int i;
408     ASN1_VALUE *skitem;
409     unsigned char *tmpdat = NULL, *p = NULL;
410     DER_ENC *derlst = NULL, *tder;
411     if (do_sort) {
412         /* Don't need to sort less than 2 items */
413         if (sk_ASN1_VALUE_num(sk) < 2)
414             do_sort = 0;
415         else {
416             derlst = OPENSSL_malloc(sk_ASN1_VALUE_num(sk)
417                                     * sizeof(*derlst));
418             if (!derlst)
419                 return 0;
420             tmpdat = OPENSSL_malloc(skcontlen);
421             if (!tmpdat) {
422                 OPENSSL_free(derlst);
423                 return 0;
424             }
425         }
426     }
427     /* If not sorting just output each item */
428     if (!do_sort) {
429         for (i = 0; i < sk_ASN1_VALUE_num(sk); i++) {
430             skitem = sk_ASN1_VALUE_value(sk, i);
431             ASN1_item_ex_i2d(&skitem, out, item, -1, iclass);
432         }
433         return 1;
434     }
435     p = tmpdat;
436
437     /* Doing sort: build up a list of each member's DER encoding */
438     for (i = 0, tder = derlst; i < sk_ASN1_VALUE_num(sk); i++, tder++) {
439         skitem = sk_ASN1_VALUE_value(sk, i);
440         tder->data = p;
441         tder->length = ASN1_item_ex_i2d(&skitem, &p, item, -1, iclass);
442         tder->field = skitem;
443     }
444
445     /* Now sort them */
446     qsort(derlst, sk_ASN1_VALUE_num(sk), sizeof(*derlst), der_cmp);
447     /* Output sorted DER encoding */
448     p = *out;
449     for (i = 0, tder = derlst; i < sk_ASN1_VALUE_num(sk); i++, tder++) {
450         memcpy(p, tder->data, tder->length);
451         p += tder->length;
452     }
453     *out = p;
454     /* If do_sort is 2 then reorder the STACK */
455     if (do_sort == 2) {
456         for (i = 0, tder = derlst; i < sk_ASN1_VALUE_num(sk); i++, tder++)
457             (void)sk_ASN1_VALUE_set(sk, i, tder->field);
458     }
459     OPENSSL_free(derlst);
460     OPENSSL_free(tmpdat);
461     return 1;
462 }
463
464 static int asn1_i2d_ex_primitive(ASN1_VALUE **pval, unsigned char **out,
465                                  const ASN1_ITEM *it, int tag, int aclass)
466 {
467     int len;
468     int utype;
469     int usetag;
470     int ndef = 0;
471
472     utype = it->utype;
473
474     /*
475      * Get length of content octets and maybe find out the underlying type.
476      */
477
478     len = asn1_ex_i2c(pval, NULL, &utype, it);
479
480     /*
481      * If SEQUENCE, SET or OTHER then header is included in pseudo content
482      * octets so don't include tag+length. We need to check here because the
483      * call to asn1_ex_i2c() could change utype.
484      */
485     if ((utype == V_ASN1_SEQUENCE) || (utype == V_ASN1_SET) ||
486         (utype == V_ASN1_OTHER))
487         usetag = 0;
488     else
489         usetag = 1;
490
491     /* -1 means omit type */
492
493     if (len == -1)
494         return 0;
495
496     /* -2 return is special meaning use ndef */
497     if (len == -2) {
498         ndef = 2;
499         len = 0;
500     }
501
502     /* If not implicitly tagged get tag from underlying type */
503     if (tag == -1)
504         tag = utype;
505
506     /* Output tag+length followed by content octets */
507     if (out) {
508         if (usetag)
509             ASN1_put_object(out, ndef, len, tag, aclass);
510         asn1_ex_i2c(pval, *out, &utype, it);
511         if (ndef)
512             ASN1_put_eoc(out);
513         else
514             *out += len;
515     }
516
517     if (usetag)
518         return ASN1_object_size(ndef, len, tag);
519     return len;
520 }
521
522 /* Produce content octets from a structure */
523
524 int asn1_ex_i2c(ASN1_VALUE **pval, unsigned char *cout, int *putype,
525                 const ASN1_ITEM *it)
526 {
527     ASN1_BOOLEAN *tbool = NULL;
528     ASN1_STRING *strtmp;
529     ASN1_OBJECT *otmp;
530     int utype;
531     const unsigned char *cont;
532     unsigned char c;
533     int len;
534     const ASN1_PRIMITIVE_FUNCS *pf;
535     pf = it->funcs;
536     if (pf && pf->prim_i2c)
537         return pf->prim_i2c(pval, cout, putype, it);
538
539     /* Should type be omitted? */
540     if ((it->itype != ASN1_ITYPE_PRIMITIVE)
541         || (it->utype != V_ASN1_BOOLEAN)) {
542         if (!*pval)
543             return -1;
544     }
545
546     if (it->itype == ASN1_ITYPE_MSTRING) {
547         /* If MSTRING type set the underlying type */
548         strtmp = (ASN1_STRING *)*pval;
549         utype = strtmp->type;
550         *putype = utype;
551     } else if (it->utype == V_ASN1_ANY) {
552         /* If ANY set type and pointer to value */
553         ASN1_TYPE *typ;
554         typ = (ASN1_TYPE *)*pval;
555         utype = typ->type;
556         *putype = utype;
557         pval = &typ->value.asn1_value;
558     } else
559         utype = *putype;
560
561     switch (utype) {
562     case V_ASN1_OBJECT:
563         otmp = (ASN1_OBJECT *)*pval;
564         cont = otmp->data;
565         len = otmp->length;
566         break;
567
568     case V_ASN1_NULL:
569         cont = NULL;
570         len = 0;
571         break;
572
573     case V_ASN1_BOOLEAN:
574         tbool = (ASN1_BOOLEAN *)pval;
575         if (*tbool == -1)
576             return -1;
577         if (it->utype != V_ASN1_ANY) {
578             /*
579              * Default handling if value == size field then omit
580              */
581             if (*tbool && (it->size > 0))
582                 return -1;
583             if (!*tbool && !it->size)
584                 return -1;
585         }
586         c = (unsigned char)*tbool;
587         cont = &c;
588         len = 1;
589         break;
590
591     case V_ASN1_BIT_STRING:
592         return i2c_ASN1_BIT_STRING((ASN1_BIT_STRING *)*pval,
593                                    cout ? &cout : NULL);
594
595     case V_ASN1_INTEGER:
596     case V_ASN1_NEG_INTEGER:
597     case V_ASN1_ENUMERATED:
598     case V_ASN1_NEG_ENUMERATED:
599         /*
600          * These are all have the same content format as ASN1_INTEGER
601          */
602         return i2c_ASN1_INTEGER((ASN1_INTEGER *)*pval, cout ? &cout : NULL);
603
604     case V_ASN1_OCTET_STRING:
605     case V_ASN1_NUMERICSTRING:
606     case V_ASN1_PRINTABLESTRING:
607     case V_ASN1_T61STRING:
608     case V_ASN1_VIDEOTEXSTRING:
609     case V_ASN1_IA5STRING:
610     case V_ASN1_UTCTIME:
611     case V_ASN1_GENERALIZEDTIME:
612     case V_ASN1_GRAPHICSTRING:
613     case V_ASN1_VISIBLESTRING:
614     case V_ASN1_GENERALSTRING:
615     case V_ASN1_UNIVERSALSTRING:
616     case V_ASN1_BMPSTRING:
617     case V_ASN1_UTF8STRING:
618     case V_ASN1_SEQUENCE:
619     case V_ASN1_SET:
620     default:
621         /* All based on ASN1_STRING and handled the same */
622         strtmp = (ASN1_STRING *)*pval;
623         /* Special handling for NDEF */
624         if ((it->size == ASN1_TFLG_NDEF)
625             && (strtmp->flags & ASN1_STRING_FLAG_NDEF)) {
626             if (cout) {
627                 strtmp->data = cout;
628                 strtmp->length = 0;
629             }
630             /* Special return code */
631             return -2;
632         }
633         cont = strtmp->data;
634         len = strtmp->length;
635
636         break;
637
638     }
639     if (cout && len)
640         memcpy(cout, cont, len);
641     return len;
642 }