|
| 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 | + |
| 10 | +#include "tomcrypt_private.h" |
| 11 | + |
| 12 | +#ifdef LTC_PKCS_12 |
| 13 | + |
| 14 | +int pkcs12_utf8_to_utf16(const unsigned char *in, unsigned long inlen, |
| 15 | + unsigned char *out, unsigned long *outlen) { |
| 16 | + unsigned long len = 0; |
| 17 | + const unsigned char* in_end = in + inlen; |
| 18 | + const ulong32 offset[6] = { |
| 19 | + 0x00000000UL, 0x00003080UL, 0x000E2080UL, |
| 20 | + 0x03C82080UL, 0xFA082080UL, 0x82082080UL |
| 21 | + }; |
| 22 | + int err = CRYPT_ERROR; |
| 23 | + |
| 24 | + while (in < in_end) { |
| 25 | + ulong32 ch = 0; |
| 26 | + unsigned short extra = 0; /* 0 */ |
| 27 | + if (*in >= 192) extra++; /* 1 */ |
| 28 | + if (*in >= 224) extra++; /* 2 */ |
| 29 | + if (*in >= 240) extra++; /* 3 */ |
| 30 | + if (*in >= 248) extra++; /* 4 */ |
| 31 | + if (*in >= 252) extra++; /* 5 */ |
| 32 | + if (in + extra >= in_end) goto ERROR; |
| 33 | + switch (extra) { |
| 34 | + case 5: ch += *in++; ch <<= 6; |
| 35 | + case 4: ch += *in++; ch <<= 6; |
| 36 | + case 3: ch += *in++; ch <<= 6; |
| 37 | + case 2: ch += *in++; ch <<= 6; |
| 38 | + case 1: ch += *in++; ch <<= 6; |
| 39 | + case 0: ch += *in++; |
| 40 | + } |
| 41 | + ch -= offset[extra]; |
| 42 | + if (ch > 0xFFFF) goto ERROR; |
| 43 | + if (*outlen >= len + 2) { |
| 44 | + out[len] = (unsigned short)((ch >> 8) & 0xFF); |
| 45 | + out[len + 1] = (unsigned char)(ch & 0xFF); |
| 46 | + } |
| 47 | + len += 2; |
| 48 | + } |
| 49 | + |
| 50 | + err = len > *outlen ? CRYPT_BUFFER_OVERFLOW : CRYPT_OK; |
| 51 | + *outlen = len; |
| 52 | +ERROR: |
| 53 | + return err; |
| 54 | +} |
| 55 | + |
| 56 | +#endif |
| 57 | + |
| 58 | +/* ref: $Format:%D$ */ |
| 59 | +/* git commit: $Format:%H$ */ |
| 60 | +/* commit time: $Format:%ai$ */ |
0 commit comments