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