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_encode_printable_string.c
13 ASN.1 DER, encode a printable STRING, Tom St Denis
19 Store an printable STRING
20 @param in The array of printable to store (one per char)
21 @param inlen The number of printable to store
22 @param out [out] The destination for the DER encoded printable STRING
23 @param outlen [in/out] The max size and resulting size of the DER printable STRING
24 @return CRYPT_OK if successful
26 int der_encode_printable_string(const unsigned char *in, unsigned long inlen,
27 unsigned char *out, unsigned long *outlen)
29 unsigned long x, y, len;
32 LTC_ARGCHK(in != NULL);
33 LTC_ARGCHK(out != NULL);
34 LTC_ARGCHK(outlen != NULL);
37 if ((err = der_length_printable_string(in, inlen, &len)) != CRYPT_OK) {
44 return CRYPT_BUFFER_OVERFLOW;
47 /* encode the header+len */
51 out[x++] = (unsigned char)inlen;
52 } else if (inlen < 256) {
54 out[x++] = (unsigned char)inlen;
55 } else if (inlen < 65536UL) {
57 out[x++] = (unsigned char)((inlen>>8)&255);
58 out[x++] = (unsigned char)(inlen&255);
59 } else if (inlen < 16777216UL) {
61 out[x++] = (unsigned char)((inlen>>16)&255);
62 out[x++] = (unsigned char)((inlen>>8)&255);
63 out[x++] = (unsigned char)(inlen&255);
65 return CRYPT_INVALID_ARG;
69 for (y = 0; y < inlen; y++) {
70 out[x++] = der_printable_char_encode(in[y]);
81 /* ref: $Format:%D$ */
82 /* git commit: $Format:%H$ */
83 /* commit time: $Format:%ai$ */