1 /* LibTomCrypt, modular cryptographic library -- Tom St Denis
3 * LibTomCrypt is a library that provides various cryptographic
4 * algorithms in a highly modular and flexible manner.
6 * The library is free for all purposes without any express
9 * Tom St Denis, tomstdenis@gmail.com, http://libtomcrypt.com
14 @file der_encode_setof.c
15 ASN.1 DER, Encode SET OF, Tom St Denis
25 static int qsort_helper(const void *a, const void *b)
27 struct edge *A = (struct edge *)a, *B = (struct edge *)b;
31 /* compare min length */
32 r = XMEMCMP(A->start, B->start, MIN(A->size, B->size));
34 if (r == 0 && A->size != B->size) {
35 if (A->size > B->size) {
36 for (x = B->size; x < A->size; x++) {
42 for (x = A->size; x < B->size; x++) {
54 Encode a SETOF stucture
55 @param list The list of items to encode
56 @param inlen The number of items in the list
57 @param out [out] The destination
58 @param outlen [in/out] The size of the output
59 @return CRYPT_OK on success
61 int der_encode_setof(ltc_asn1_list *list, unsigned long inlen,
62 unsigned char *out, unsigned long *outlen)
64 unsigned long x, y, z, hdrlen;
67 unsigned char *ptr, *buf;
69 /* check that they're all the same type */
70 for (x = 1; x < inlen; x++) {
71 if (list[x].type != list[x-1].type) {
72 return CRYPT_INVALID_ARG;
76 /* alloc buffer to store copy of output */
77 buf = XCALLOC(1, *outlen);
83 if ((err = der_encode_sequence_ex(list, inlen, buf, outlen, LTC_ASN1_SETOF)) != CRYPT_OK) {
89 edges = XCALLOC(inlen, sizeof(*edges));
98 /* now skip length data */
104 /* get the size of the static header */
105 hdrlen = ((unsigned long)ptr) - ((unsigned long)buf);
110 while (ptr < (buf + *outlen)) {
112 edges[x].start = ptr;
125 edges[x].size = (edges[x].size << 8) | ((unsigned long)ptr[z++]);
131 ptr += edges[x].size;
135 /* sort based on contents (using edges) */
136 XQSORT(edges, inlen, sizeof(*edges), &qsort_helper);
138 /* copy static header */
139 XMEMCPY(out, buf, hdrlen);
141 /* copy+sort using edges+indecies to output from buffer */
142 for (y = hdrlen, x = 0; x < inlen; x++) {
143 XMEMCPY(out+y, edges[x].start, edges[x].size);
147 #ifdef LTC_CLEAN_STACK
148 zeromem(buf, *outlen);
160 /* $Source: /cvs/libtom/libtomcrypt/src/pk/asn1/der/set/der_encode_setof.c,v $ */
161 /* $Revision: 1.11 $ */
162 /* $Date: 2006/03/31 14:15:35 $ */