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