blob: 0370a2d91a2edb241dad6966bf1a9018bd265e93 [file] [log] [blame]
vlmfa67ddc2004-06-03 03:38:44 +00001/*-
2 * Copyright (c) 2003, 2004 Lev Walkin <vlm@lionet.info>. All rights reserved.
3 * Redistribution and modifications are permitted subject to BSD license.
4 */
5#include <constr_SEQUENCE_OF.h>
6#include <asn_SEQUENCE_OF.h>
7
8/*
9 * The DER encoder of the SEQUENCE OF type.
10 */
11der_enc_rval_t
12SEQUENCE_OF_encode_der(asn1_TYPE_descriptor_t *sd, void *ptr,
13 int tag_mode, ber_tlv_tag_t tag,
14 asn_app_consume_bytes_f *cb, void *app_key) {
vlmda674682004-08-11 09:07:36 +000015 asn1_SET_OF_specifics_t *specs = (asn1_SET_OF_specifics_t *)sd->specifics;
vlmfa67ddc2004-06-03 03:38:44 +000016 asn1_SET_OF_element_t *elm = specs->element;
vlmda674682004-08-11 09:07:36 +000017 A_SEQUENCE_OF(void) *list;
vlmfa67ddc2004-06-03 03:38:44 +000018 size_t computed_size = 0;
19 ssize_t encoding_size = 0;
20 der_enc_rval_t erval;
21 int edx;
22
23 ASN_DEBUG("Estimating size of SEQUENCE OF %s", sd->name);
24
25 /*
26 * Gather the length of the underlying members sequence.
27 */
vlmda674682004-08-11 09:07:36 +000028 (void *)list = ptr;
vlmfa67ddc2004-06-03 03:38:44 +000029 for(edx = 0; edx < list->count; edx++) {
30 void *memb_ptr = list->array[edx];
31 erval = elm->type->der_encoder(elm->type, memb_ptr,
32 0, elm->tag,
33 0, 0);
34 if(erval.encoded == -1)
35 return erval;
36 computed_size += erval.encoded;
37 }
38
39 /*
40 * Encode the TLV for the sequence itself.
41 */
42 encoding_size = der_write_tags(sd, computed_size, tag_mode, tag,
43 cb, app_key);
44 if(encoding_size == -1) {
45 erval.encoded = -1;
46 erval.failed_type = sd;
47 erval.structure_ptr = ptr;
48 return erval;
49 }
50
51 computed_size += encoding_size;
52 if(!cb) {
53 erval.encoded = computed_size;
54 return erval;
55 }
56
57 ASN_DEBUG("Encoding members of SEQUENCE OF %s", sd->name);
58
59 /*
60 * Encode all members.
61 */
62 for(edx = 0; edx < list->count; edx++) {
63 void *memb_ptr = list->array[edx];
64 erval = elm->type->der_encoder(elm->type, memb_ptr,
65 0, elm->tag,
66 cb, app_key);
67 if(erval.encoded == -1)
68 return erval;
69 encoding_size += erval.encoded;
70 }
71
vlmb42843a2004-06-05 08:17:50 +000072 if(computed_size != (size_t)encoding_size) {
vlmfa67ddc2004-06-03 03:38:44 +000073 /*
74 * Encoded size is not equal to the computed size.
75 */
76 erval.encoded = -1;
77 erval.failed_type = sd;
78 erval.structure_ptr = ptr;
79 } else {
80 erval.encoded = computed_size;
81 }
82
83 return erval;
84}
85