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_ia5_string.c
15 ASN.1 DER, encode a IA5 STRING, Tom St Denis
22 @param in The array of IA5 to store (one per char)
23 @param inlen The number of IA5 to store
24 @param out [out] The destination for the DER encoded IA5 STRING
25 @param outlen [in/out] The max size and resulting size of the DER IA5 STRING
26 @return CRYPT_OK if successful
28 int der_encode_ia5_string(const unsigned char *in, unsigned long inlen,
29 unsigned char *out, unsigned long *outlen)
31 unsigned long x, y, len;
34 LTC_ARGCHK(in != NULL);
35 LTC_ARGCHK(out != NULL);
36 LTC_ARGCHK(outlen != NULL);
39 if ((err = der_length_ia5_string(in, inlen, &len)) != CRYPT_OK) {
46 return CRYPT_BUFFER_OVERFLOW;
49 /* encode the header+len */
53 out[x++] = (unsigned char)inlen;
54 } else if (inlen < 256) {
56 out[x++] = (unsigned char)inlen;
57 } else if (inlen < 65536UL) {
59 out[x++] = (unsigned char)((inlen>>8)&255);
60 out[x++] = (unsigned char)(inlen&255);
61 } else if (inlen < 16777216UL) {
63 out[x++] = (unsigned char)((inlen>>16)&255);
64 out[x++] = (unsigned char)((inlen>>8)&255);
65 out[x++] = (unsigned char)(inlen&255);
67 return CRYPT_INVALID_ARG;
71 for (y = 0; y < inlen; y++) {
72 out[x++] = der_ia5_char_encode(in[y]);
83 /* $Source: /cvs/libtom/libtomcrypt/src/pk/asn1/der/ia5/der_encode_ia5_string.c,v $ */
84 /* $Revision: 1.4 $ */
85 /* $Date: 2006/12/04 21:34:03 $ */