better handling wchar_t when __WCHAR_MAX__ is 0xFFFF (2 bytes only)
This commit is contained in:
parent
05f85c3e6d
commit
4bd327a4cc
@ -619,6 +619,7 @@ int der_encode_utf8_string(const wchar_t *in, unsigned long inlen,
|
|||||||
int der_decode_utf8_string(const unsigned char *in, unsigned long inlen,
|
int der_decode_utf8_string(const unsigned char *in, unsigned long inlen,
|
||||||
wchar_t *out, unsigned long *outlen);
|
wchar_t *out, unsigned long *outlen);
|
||||||
unsigned long der_utf8_charsize(const wchar_t c);
|
unsigned long der_utf8_charsize(const wchar_t c);
|
||||||
|
int der_utf8_valid_char(const wchar_t c);
|
||||||
int der_length_utf8_string(const wchar_t *in, unsigned long noctets, unsigned long *outlen);
|
int der_length_utf8_string(const wchar_t *in, unsigned long noctets, unsigned long *outlen);
|
||||||
|
|
||||||
|
|
||||||
|
@ -37,9 +37,7 @@ int der_encode_utf8_string(const wchar_t *in, unsigned long inlen,
|
|||||||
|
|
||||||
/* get the size */
|
/* get the size */
|
||||||
for (x = len = 0; x < inlen; x++) {
|
for (x = len = 0; x < inlen; x++) {
|
||||||
if (in[x] < 0 || in[x] > 0x1FFFF) {
|
if (!der_utf8_valid_char(in[x])) return CRYPT_INVALID_ARG;
|
||||||
return CRYPT_INVALID_ARG;
|
|
||||||
}
|
|
||||||
len += der_utf8_charsize(in[x]);
|
len += der_utf8_charsize(in[x]);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -27,11 +27,33 @@ unsigned long der_utf8_charsize(const wchar_t c)
|
|||||||
return 1;
|
return 1;
|
||||||
} else if (c <= 0x7FF) {
|
} else if (c <= 0x7FF) {
|
||||||
return 2;
|
return 2;
|
||||||
|
#if __WCHAR_MAX__ == 0xFFFF
|
||||||
|
} else {
|
||||||
|
return 3;
|
||||||
|
}
|
||||||
|
#else
|
||||||
} else if (c <= 0xFFFF) {
|
} else if (c <= 0xFFFF) {
|
||||||
return 3;
|
return 3;
|
||||||
} else {
|
} else {
|
||||||
return 4;
|
return 4;
|
||||||
}
|
}
|
||||||
|
#endif
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
Test whether the given code point is valid character
|
||||||
|
@param c The UTF-8 character to test
|
||||||
|
@return 1 - valid, 0 - invalid
|
||||||
|
*/
|
||||||
|
int der_utf8_valid_char(const wchar_t c)
|
||||||
|
{
|
||||||
|
#if !defined(__WCHAR_MAX__) || __WCHAR_MAX__ > 0xFFFF
|
||||||
|
if (in[x] > 0x10FFFF) return 0;
|
||||||
|
#endif
|
||||||
|
#if !defined(__WCHAR_MAX__) || __WCHAR_MAX__ != 0xFFFF && __WCHAR_MAX__ != 0xFFFFFFFF
|
||||||
|
if (in[x] < 0) return 0;
|
||||||
|
#endif
|
||||||
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -50,9 +72,7 @@ int der_length_utf8_string(const wchar_t *in, unsigned long noctets, unsigned lo
|
|||||||
|
|
||||||
len = 0;
|
len = 0;
|
||||||
for (x = 0; x < noctets; x++) {
|
for (x = 0; x < noctets; x++) {
|
||||||
if (in[x] < 0 || in[x] > 0x10FFFF) {
|
if (!der_utf8_valid_char(in[x])) return CRYPT_INVALID_ARG;
|
||||||
return CRYPT_INVALID_ARG;
|
|
||||||
}
|
|
||||||
len += der_utf8_charsize(in[x]);
|
len += der_utf8_charsize(in[x]);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user