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_length_ia5_string.c
15 ASN.1 DER, get length of IA5 STRING, Tom St Denis
127 int der_ia5_char_encode(int c)
130 for (x = 0; x < (int)(sizeof(ia5_table)/sizeof(ia5_table[0])); x++) {
131 if (ia5_table[x].code == c) {
132 return ia5_table[x].value;
138 int der_ia5_value_decode(int v)
141 for (x = 0; x < (int)(sizeof(ia5_table)/sizeof(ia5_table[0])); x++) {
142 if (ia5_table[x].value == v) {
143 return ia5_table[x].code;
150 Gets length of DER encoding of IA5 STRING
151 @param octets The values you want to encode
152 @param noctets The number of octets in the string to encode
153 @param outlen [out] The length of the DER encoding for the given string
154 @return CRYPT_OK if successful
156 int der_length_ia5_string(const unsigned char *octets, unsigned long noctets, unsigned long *outlen)
160 LTC_ARGCHK(outlen != NULL);
161 LTC_ARGCHK(octets != NULL);
163 /* scan string for validity */
164 for (x = 0; x < noctets; x++) {
165 if (der_ia5_char_encode(octets[x]) == -1) {
166 return CRYPT_INVALID_ARG;
171 /* 16 LL DD DD DD ... */
172 *outlen = 2 + noctets;
173 } else if (noctets < 256) {
174 /* 16 81 LL DD DD DD ... */
175 *outlen = 3 + noctets;
176 } else if (noctets < 65536UL) {
177 /* 16 82 LL LL DD DD DD ... */
178 *outlen = 4 + noctets;
179 } else if (noctets < 16777216UL) {
180 /* 16 83 LL LL LL DD DD DD ... */
181 *outlen = 5 + noctets;
183 return CRYPT_INVALID_ARG;
192 /* $Source: /cvs/libtom/libtomcrypt/src/pk/asn1/der/ia5/der_length_ia5_string.c,v $ */
193 /* $Revision: 1.2 $ */
194 /* $Date: 2006/03/31 14:15:35 $ */