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
12 @file der_length_printable_string.c
13 ASN.1 DER, get length of Printable STRING, Tom St Denis
20 } printable_table[] = {
97 int der_printable_char_encode(int c)
100 for (x = 0; x < (int)(sizeof(printable_table)/sizeof(printable_table[0])); x++) {
101 if (printable_table[x].code == c) {
102 return printable_table[x].value;
108 int der_printable_value_decode(int v)
111 for (x = 0; x < (int)(sizeof(printable_table)/sizeof(printable_table[0])); x++) {
112 if (printable_table[x].value == v) {
113 return printable_table[x].code;
120 Gets length of DER encoding of Printable STRING
121 @param octets The values you want to encode
122 @param noctets The number of octets in the string to encode
123 @param outlen [out] The length of the DER encoding for the given string
124 @return CRYPT_OK if successful
126 int der_length_printable_string(const unsigned char *octets, unsigned long noctets, unsigned long *outlen)
130 LTC_ARGCHK(outlen != NULL);
131 LTC_ARGCHK(octets != NULL);
133 /* scan string for validity */
134 for (x = 0; x < noctets; x++) {
135 if (der_printable_char_encode(octets[x]) == -1) {
136 return CRYPT_INVALID_ARG;
141 /* 16 LL DD DD DD ... */
142 *outlen = 2 + noctets;
143 } else if (noctets < 256) {
144 /* 16 81 LL DD DD DD ... */
145 *outlen = 3 + noctets;
146 } else if (noctets < 65536UL) {
147 /* 16 82 LL LL DD DD DD ... */
148 *outlen = 4 + noctets;
149 } else if (noctets < 16777216UL) {
150 /* 16 83 LL LL LL DD DD DD ... */
151 *outlen = 5 + noctets;
153 return CRYPT_INVALID_ARG;
162 /* ref: $Format:%D$ */
163 /* git commit: $Format:%H$ */
164 /* commit time: $Format:%ai$ */