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_printable_string.c
15 ASN.1 DER, get length of Printable STRING, Tom St Denis
22 } printable_table[] = {
99 int der_printable_char_encode(int c)
102 for (x = 0; x < (int)(sizeof(printable_table)/sizeof(printable_table[0])); x++) {
103 if (printable_table[x].code == c) {
104 return printable_table[x].value;
110 int der_printable_value_decode(int v)
113 for (x = 0; x < (int)(sizeof(printable_table)/sizeof(printable_table[0])); x++) {
114 if (printable_table[x].value == v) {
115 return printable_table[x].code;
122 Gets length of DER encoding of Printable STRING
123 @param octets The values you want to encode
124 @param noctets The number of octets in the string to encode
125 @param outlen [out] The length of the DER encoding for the given string
126 @return CRYPT_OK if successful
128 int der_length_printable_string(const unsigned char *octets, unsigned long noctets, unsigned long *outlen)
132 LTC_ARGCHK(outlen != NULL);
133 LTC_ARGCHK(octets != NULL);
135 /* scan string for validity */
136 for (x = 0; x < noctets; x++) {
137 if (der_printable_char_encode(octets[x]) == -1) {
138 return CRYPT_INVALID_ARG;
143 /* 16 LL DD DD DD ... */
144 *outlen = 2 + noctets;
145 } else if (noctets < 256) {
146 /* 16 81 LL DD DD DD ... */
147 *outlen = 3 + noctets;
148 } else if (noctets < 65536UL) {
149 /* 16 82 LL LL DD DD DD ... */
150 *outlen = 4 + noctets;
151 } else if (noctets < 16777216UL) {
152 /* 16 83 LL LL LL DD DD DD ... */
153 *outlen = 5 + noctets;
155 return CRYPT_INVALID_ARG;
164 /* $Source: /cvs/libtom/libtomcrypt/src/pk/asn1/der/printable_string/der_length_printable_string.c,v $ */
165 /* $Revision: 1.2 $ */
166 /* $Date: 2006/03/31 14:15:35 $ */