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_utf8_string.c
13 ASN.1 DER, get length of UTF8 STRING, Tom St Denis
18 /** Return the size in bytes of a UTF-8 character
19 @param c The UTF-8 character to measure
20 @return The size in bytes
22 unsigned long der_utf8_charsize(const wchar_t c)
26 } else if (c <= 0x7FF) {
28 #if LTC_WCHAR_MAX == 0xFFFF
33 } else if (c <= 0xFFFF) {
42 Test whether the given code point is valid character
43 @param c The UTF-8 character to test
44 @return 1 - valid, 0 - invalid
46 int der_utf8_valid_char(const wchar_t c)
49 #if !defined(LTC_WCHAR_MAX) || LTC_WCHAR_MAX > 0xFFFF
50 if (c > 0x10FFFF) return 0;
52 #if LTC_WCHAR_MAX != 0xFFFF && LTC_WCHAR_MAX != 0xFFFFFFFF
59 Gets length of DER encoding of UTF8 STRING
60 @param in The characters to measure the length of
61 @param noctets The number of octets in the string to encode
62 @param outlen [out] The length of the DER encoding for the given string
63 @return CRYPT_OK if successful
65 int der_length_utf8_string(const wchar_t *in, unsigned long noctets, unsigned long *outlen)
69 LTC_ARGCHK(in != NULL);
70 LTC_ARGCHK(outlen != NULL);
73 for (x = 0; x < noctets; x++) {
74 if (!der_utf8_valid_char(in[x])) return CRYPT_INVALID_ARG;
75 len += der_utf8_charsize(in[x]);
79 /* 0C LL DD DD DD ... */
81 } else if (len < 256) {
82 /* 0C 81 LL DD DD DD ... */
84 } else if (len < 65536UL) {
85 /* 0C 82 LL LL DD DD DD ... */
87 } else if (len < 16777216UL) {
88 /* 0C 83 LL LL LL DD DD DD ... */
91 return CRYPT_INVALID_ARG;
100 /* ref: $Format:%D$ */
101 /* git commit: $Format:%H$ */
102 /* commit time: $Format:%ai$ */