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