version 0.3.33
[fms.git] / libs / libtomcrypt / pk / asn1 / der / printable_string / der_length_printable_string.c
1 /* LibTomCrypt, modular cryptographic library -- Tom St Denis
2  *
3  * LibTomCrypt is a library that provides various cryptographic
4  * algorithms in a highly modular and flexible manner.
5  *
6  * The library is free for all purposes without any express
7  * guarantee it works.
8  *
9  * Tom St Denis, tomstdenis@gmail.com, http://libtomcrypt.com
10  */
11 #include "tomcrypt.h"
12
13 /**
14   @file der_length_printable_string.c
15   ASN.1 DER, get length of Printable STRING, Tom St Denis
16 */
17
18 #ifdef LTC_DER
19
20 static const struct {
21    int code, value;
22 } printable_table[] = {
23 { ' ', 32 }, 
24 { '\'', 39 }, 
25 { '(', 40 }, 
26 { ')', 41 }, 
27 { '+', 43 }, 
28 { ',', 44 }, 
29 { '-', 45 }, 
30 { '.', 46 }, 
31 { '/', 47 }, 
32 { '0', 48 }, 
33 { '1', 49 }, 
34 { '2', 50 }, 
35 { '3', 51 }, 
36 { '4', 52 }, 
37 { '5', 53 }, 
38 { '6', 54 }, 
39 { '7', 55 }, 
40 { '8', 56 }, 
41 { '9', 57 }, 
42 { ':', 58 }, 
43 { '=', 61 }, 
44 { '?', 63 }, 
45 { 'A', 65 }, 
46 { 'B', 66 }, 
47 { 'C', 67 }, 
48 { 'D', 68 }, 
49 { 'E', 69 }, 
50 { 'F', 70 }, 
51 { 'G', 71 }, 
52 { 'H', 72 }, 
53 { 'I', 73 }, 
54 { 'J', 74 }, 
55 { 'K', 75 }, 
56 { 'L', 76 }, 
57 { 'M', 77 }, 
58 { 'N', 78 }, 
59 { 'O', 79 }, 
60 { 'P', 80 }, 
61 { 'Q', 81 }, 
62 { 'R', 82 }, 
63 { 'S', 83 }, 
64 { 'T', 84 }, 
65 { 'U', 85 }, 
66 { 'V', 86 }, 
67 { 'W', 87 }, 
68 { 'X', 88 }, 
69 { 'Y', 89 }, 
70 { 'Z', 90 }, 
71 { 'a', 97 }, 
72 { 'b', 98 }, 
73 { 'c', 99 }, 
74 { 'd', 100 }, 
75 { 'e', 101 }, 
76 { 'f', 102 }, 
77 { 'g', 103 }, 
78 { 'h', 104 }, 
79 { 'i', 105 }, 
80 { 'j', 106 }, 
81 { 'k', 107 }, 
82 { 'l', 108 }, 
83 { 'm', 109 }, 
84 { 'n', 110 }, 
85 { 'o', 111 }, 
86 { 'p', 112 }, 
87 { 'q', 113 }, 
88 { 'r', 114 }, 
89 { 's', 115 }, 
90 { 't', 116 }, 
91 { 'u', 117 }, 
92 { 'v', 118 }, 
93 { 'w', 119 }, 
94 { 'x', 120 }, 
95 { 'y', 121 }, 
96 { 'z', 122 }, 
97 };
98
99 int der_printable_char_encode(int c)
100 {
101    int x;
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;
105        }
106    }
107    return -1;
108 }
109
110 int der_printable_value_decode(int v)
111 {
112    int x;
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;
116        }
117    }
118    return -1;
119 }
120    
121 /**
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
127 */
128 int der_length_printable_string(const unsigned char *octets, unsigned long noctets, unsigned long *outlen)
129 {
130    unsigned long x;
131
132    LTC_ARGCHK(outlen != NULL);
133    LTC_ARGCHK(octets != NULL);
134
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;
139        }
140    }
141
142    if (noctets < 128) {
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;
154    } else {
155       return CRYPT_INVALID_ARG;
156    }
157
158    return CRYPT_OK;
159 }
160
161 #endif
162
163
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 $ */