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
9 * Tom St Denis, tomstdenis@gmail.com, http://libtomcrypt.com
14 @file der_length_utf8_string.c
15 ASN.1 DER, get length of UTF8 STRING, Tom St Denis
20 /** Return the size in bytes of a UTF-8 character
21 @param c The UTF-8 character to measure
22 @return The size in bytes
24 unsigned long der_utf8_charsize(const wchar_t c)
28 } else if (c <= 0x7FF) {
30 } else if (c <= 0xFFFF) {
38 Gets length of DER encoding of UTF8 STRING
39 @param in The characters to measure the length of
40 @param noctets The number of octets in the string to encode
41 @param outlen [out] The length of the DER encoding for the given string
42 @return CRYPT_OK if successful
44 int der_length_utf8_string(const wchar_t *in, unsigned long noctets, unsigned long *outlen)
48 LTC_ARGCHK(in != NULL);
49 LTC_ARGCHK(outlen != NULL);
52 for (x = 0; x < noctets; x++) {
53 if (in[x] < 0 || in[x] > 0x10FFFF) {
54 return CRYPT_INVALID_ARG;
56 len += der_utf8_charsize(in[x]);
60 /* 0C LL DD DD DD ... */
62 } else if (len < 256) {
63 /* 0C 81 LL DD DD DD ... */
65 } else if (len < 65536UL) {
66 /* 0C 82 LL LL DD DD DD ... */
68 } else if (len < 16777216UL) {
69 /* 0C 83 LL LL LL DD DD DD ... */
72 return CRYPT_INVALID_ARG;
81 /* $Source: /cvs/libtom/libtomcrypt/src/pk/asn1/der/utf8/der_length_utf8_string.c,v $ */
82 /* $Revision: 1.5 $ */
83 /* $Date: 2006/12/16 17:41:21 $ */