mirror of
https://github.com/Laupetin/OpenAssetTools.git
synced 2025-07-02 01:01:54 +00:00
Import code from previous AssetBuilder version
This commit is contained in:
114
thirdparty/libtomcrypt/pk/asn1/der/utf8/der_decode_utf8_string.c
vendored
Normal file
114
thirdparty/libtomcrypt/pk/asn1/der/utf8/der_decode_utf8_string.c
vendored
Normal file
@ -0,0 +1,114 @@
|
||||
/* LibTomCrypt, modular cryptographic library -- Tom St Denis
|
||||
*
|
||||
* LibTomCrypt is a library that provides various cryptographic
|
||||
* algorithms in a highly modular and flexible manner.
|
||||
*
|
||||
* The library is free for all purposes without any express
|
||||
* guarantee it works.
|
||||
*/
|
||||
#include "tomcrypt.h"
|
||||
|
||||
/**
|
||||
@file der_decode_utf8_string.c
|
||||
ASN.1 DER, encode a UTF8 STRING, Tom St Denis
|
||||
*/
|
||||
|
||||
|
||||
#ifdef LTC_DER
|
||||
|
||||
/**
|
||||
Store a UTF8 STRING
|
||||
@param in The DER encoded UTF8 STRING
|
||||
@param inlen The size of the DER UTF8 STRING
|
||||
@param out [out] The array of utf8s stored (one per char)
|
||||
@param outlen [in/out] The number of utf8s stored
|
||||
@return CRYPT_OK if successful
|
||||
*/
|
||||
int der_decode_utf8_string(const unsigned char *in, unsigned long inlen,
|
||||
wchar_t *out, unsigned long *outlen)
|
||||
{
|
||||
wchar_t tmp;
|
||||
unsigned long x, y, z, len;
|
||||
int err;
|
||||
|
||||
LTC_ARGCHK(in != NULL);
|
||||
LTC_ARGCHK(out != NULL);
|
||||
LTC_ARGCHK(outlen != NULL);
|
||||
|
||||
/* must have header at least */
|
||||
if (inlen < 2) {
|
||||
return CRYPT_INVALID_PACKET;
|
||||
}
|
||||
|
||||
/* check for 0x0C */
|
||||
if ((in[0] & 0x1F) != 0x0C) {
|
||||
return CRYPT_INVALID_PACKET;
|
||||
}
|
||||
x = 1;
|
||||
|
||||
/* decode the length */
|
||||
if (in[x] & 0x80) {
|
||||
/* valid # of bytes in length are 1,2,3 */
|
||||
y = in[x] & 0x7F;
|
||||
if ((y == 0) || (y > 3) || ((x + y) > inlen)) {
|
||||
return CRYPT_INVALID_PACKET;
|
||||
}
|
||||
|
||||
/* read the length in */
|
||||
len = 0;
|
||||
++x;
|
||||
while (y--) {
|
||||
len = (len << 8) | in[x++];
|
||||
}
|
||||
} else {
|
||||
len = in[x++] & 0x7F;
|
||||
}
|
||||
|
||||
if (len + x > inlen) {
|
||||
return CRYPT_INVALID_PACKET;
|
||||
}
|
||||
|
||||
/* proceed to decode */
|
||||
for (y = 0; x < inlen; ) {
|
||||
/* get first byte */
|
||||
tmp = in[x++];
|
||||
|
||||
/* count number of bytes */
|
||||
for (z = 0; (tmp & 0x80) && (z <= 4); z++, tmp = (tmp << 1) & 0xFF);
|
||||
|
||||
if (z > 4 || (x + (z - 1) > inlen)) {
|
||||
return CRYPT_INVALID_PACKET;
|
||||
}
|
||||
|
||||
/* decode, grab upper bits */
|
||||
tmp >>= z;
|
||||
|
||||
/* grab remaining bytes */
|
||||
if (z > 1) { --z; }
|
||||
while (z-- != 0) {
|
||||
if ((in[x] & 0xC0) != 0x80) {
|
||||
return CRYPT_INVALID_PACKET;
|
||||
}
|
||||
tmp = (tmp << 6) | ((wchar_t)in[x++] & 0x3F);
|
||||
}
|
||||
|
||||
if (y < *outlen) {
|
||||
out[y] = tmp;
|
||||
}
|
||||
y++;
|
||||
}
|
||||
if (y > *outlen) {
|
||||
err = CRYPT_BUFFER_OVERFLOW;
|
||||
} else {
|
||||
err = CRYPT_OK;
|
||||
}
|
||||
*outlen = y;
|
||||
|
||||
return err;
|
||||
}
|
||||
|
||||
#endif
|
||||
|
||||
/* ref: HEAD -> master, tag: v1.18.2 */
|
||||
/* git commit: 7e7eb695d581782f04b24dc444cbfde86af59853 */
|
||||
/* commit time: 2018-07-01 22:49:01 +0200 */
|
104
thirdparty/libtomcrypt/pk/asn1/der/utf8/der_encode_utf8_string.c
vendored
Normal file
104
thirdparty/libtomcrypt/pk/asn1/der/utf8/der_encode_utf8_string.c
vendored
Normal file
@ -0,0 +1,104 @@
|
||||
/* LibTomCrypt, modular cryptographic library -- Tom St Denis
|
||||
*
|
||||
* LibTomCrypt is a library that provides various cryptographic
|
||||
* algorithms in a highly modular and flexible manner.
|
||||
*
|
||||
* The library is free for all purposes without any express
|
||||
* guarantee it works.
|
||||
*/
|
||||
#include "tomcrypt.h"
|
||||
|
||||
/**
|
||||
@file der_encode_utf8_string.c
|
||||
ASN.1 DER, encode a UTF8 STRING, Tom St Denis
|
||||
*/
|
||||
|
||||
|
||||
#ifdef LTC_DER
|
||||
|
||||
/**
|
||||
Store an UTF8 STRING
|
||||
@param in The array of UTF8 to store (one per wchar_t)
|
||||
@param inlen The number of UTF8 to store
|
||||
@param out [out] The destination for the DER encoded UTF8 STRING
|
||||
@param outlen [in/out] The max size and resulting size of the DER UTF8 STRING
|
||||
@return CRYPT_OK if successful
|
||||
*/
|
||||
int der_encode_utf8_string(const wchar_t *in, unsigned long inlen,
|
||||
unsigned char *out, unsigned long *outlen)
|
||||
{
|
||||
unsigned long x, y, len;
|
||||
|
||||
LTC_ARGCHK(in != NULL);
|
||||
LTC_ARGCHK(out != NULL);
|
||||
LTC_ARGCHK(outlen != NULL);
|
||||
|
||||
/* get the size */
|
||||
for (x = len = 0; x < inlen; x++) {
|
||||
if (!der_utf8_valid_char(in[x])) return CRYPT_INVALID_ARG;
|
||||
len += der_utf8_charsize(in[x]);
|
||||
}
|
||||
|
||||
if (len < 128) {
|
||||
y = 2 + len;
|
||||
} else if (len < 256) {
|
||||
y = 3 + len;
|
||||
} else if (len < 65536UL) {
|
||||
y = 4 + len;
|
||||
} else if (len < 16777216UL) {
|
||||
y = 5 + len;
|
||||
} else {
|
||||
return CRYPT_INVALID_ARG;
|
||||
}
|
||||
|
||||
/* too big? */
|
||||
if (y > *outlen) {
|
||||
*outlen = y;
|
||||
return CRYPT_BUFFER_OVERFLOW;
|
||||
}
|
||||
|
||||
/* encode the header+len */
|
||||
x = 0;
|
||||
out[x++] = 0x0C;
|
||||
if (len < 128) {
|
||||
out[x++] = (unsigned char)len;
|
||||
} else if (len < 256) {
|
||||
out[x++] = 0x81;
|
||||
out[x++] = (unsigned char)len;
|
||||
} else if (len < 65536UL) {
|
||||
out[x++] = 0x82;
|
||||
out[x++] = (unsigned char)((len>>8)&255);
|
||||
out[x++] = (unsigned char)(len&255);
|
||||
} else if (len < 16777216UL) {
|
||||
out[x++] = 0x83;
|
||||
out[x++] = (unsigned char)((len>>16)&255);
|
||||
out[x++] = (unsigned char)((len>>8)&255);
|
||||
out[x++] = (unsigned char)(len&255);
|
||||
} else {
|
||||
/* coverity[dead_error_line] */
|
||||
return CRYPT_INVALID_ARG;
|
||||
}
|
||||
|
||||
/* store UTF8 */
|
||||
for (y = 0; y < inlen; y++) {
|
||||
switch (der_utf8_charsize(in[y])) {
|
||||
case 1: out[x++] = (unsigned char)in[y]; break;
|
||||
case 2: out[x++] = 0xC0 | ((in[y] >> 6) & 0x1F); out[x++] = 0x80 | (in[y] & 0x3F); break;
|
||||
case 3: out[x++] = 0xE0 | ((in[y] >> 12) & 0x0F); out[x++] = 0x80 | ((in[y] >> 6) & 0x3F); out[x++] = 0x80 | (in[y] & 0x3F); break;
|
||||
#if !defined(LTC_WCHAR_MAX) || LTC_WCHAR_MAX > 0xFFFF
|
||||
case 4: out[x++] = 0xF0 | ((in[y] >> 18) & 0x07); out[x++] = 0x80 | ((in[y] >> 12) & 0x3F); out[x++] = 0x80 | ((in[y] >> 6) & 0x3F); out[x++] = 0x80 | (in[y] & 0x3F); break;
|
||||
#endif
|
||||
}
|
||||
}
|
||||
|
||||
/* retun length */
|
||||
*outlen = x;
|
||||
|
||||
return CRYPT_OK;
|
||||
}
|
||||
|
||||
#endif
|
||||
|
||||
/* ref: HEAD -> master, tag: v1.18.2 */
|
||||
/* git commit: 7e7eb695d581782f04b24dc444cbfde86af59853 */
|
||||
/* commit time: 2018-07-01 22:49:01 +0200 */
|
102
thirdparty/libtomcrypt/pk/asn1/der/utf8/der_length_utf8_string.c
vendored
Normal file
102
thirdparty/libtomcrypt/pk/asn1/der/utf8/der_length_utf8_string.c
vendored
Normal file
@ -0,0 +1,102 @@
|
||||
/* LibTomCrypt, modular cryptographic library -- Tom St Denis
|
||||
*
|
||||
* LibTomCrypt is a library that provides various cryptographic
|
||||
* algorithms in a highly modular and flexible manner.
|
||||
*
|
||||
* The library is free for all purposes without any express
|
||||
* guarantee it works.
|
||||
*/
|
||||
#include "tomcrypt.h"
|
||||
|
||||
/**
|
||||
@file der_length_utf8_string.c
|
||||
ASN.1 DER, get length of UTF8 STRING, Tom St Denis
|
||||
*/
|
||||
|
||||
#ifdef LTC_DER
|
||||
|
||||
/** Return the size in bytes of a UTF-8 character
|
||||
@param c The UTF-8 character to measure
|
||||
@return The size in bytes
|
||||
*/
|
||||
unsigned long der_utf8_charsize(const wchar_t c)
|
||||
{
|
||||
if (c <= 0x7F) {
|
||||
return 1;
|
||||
} else if (c <= 0x7FF) {
|
||||
return 2;
|
||||
#if LTC_WCHAR_MAX == 0xFFFF
|
||||
} else {
|
||||
return 3;
|
||||
}
|
||||
#else
|
||||
} else if (c <= 0xFFFF) {
|
||||
return 3;
|
||||
} else {
|
||||
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)
|
||||
{
|
||||
LTC_UNUSED_PARAM(c);
|
||||
#if !defined(LTC_WCHAR_MAX) || LTC_WCHAR_MAX > 0xFFFF
|
||||
if (c > 0x10FFFF) return 0;
|
||||
#endif
|
||||
#if LTC_WCHAR_MAX != 0xFFFF && LTC_WCHAR_MAX != 0xFFFFFFFF
|
||||
if (c < 0) return 0;
|
||||
#endif
|
||||
return 1;
|
||||
}
|
||||
|
||||
/**
|
||||
Gets length of DER encoding of UTF8 STRING
|
||||
@param in The characters to measure the length of
|
||||
@param noctets The number of octets in the string to encode
|
||||
@param outlen [out] The length of the DER encoding for the given string
|
||||
@return CRYPT_OK if successful
|
||||
*/
|
||||
int der_length_utf8_string(const wchar_t *in, unsigned long noctets, unsigned long *outlen)
|
||||
{
|
||||
unsigned long x, len;
|
||||
|
||||
LTC_ARGCHK(in != NULL);
|
||||
LTC_ARGCHK(outlen != NULL);
|
||||
|
||||
len = 0;
|
||||
for (x = 0; x < noctets; x++) {
|
||||
if (!der_utf8_valid_char(in[x])) return CRYPT_INVALID_ARG;
|
||||
len += der_utf8_charsize(in[x]);
|
||||
}
|
||||
|
||||
if (len < 128) {
|
||||
/* 0C LL DD DD DD ... */
|
||||
*outlen = 2 + len;
|
||||
} else if (len < 256) {
|
||||
/* 0C 81 LL DD DD DD ... */
|
||||
*outlen = 3 + len;
|
||||
} else if (len < 65536UL) {
|
||||
/* 0C 82 LL LL DD DD DD ... */
|
||||
*outlen = 4 + len;
|
||||
} else if (len < 16777216UL) {
|
||||
/* 0C 83 LL LL LL DD DD DD ... */
|
||||
*outlen = 5 + len;
|
||||
} else {
|
||||
return CRYPT_INVALID_ARG;
|
||||
}
|
||||
|
||||
return CRYPT_OK;
|
||||
}
|
||||
|
||||
#endif
|
||||
|
||||
|
||||
/* ref: HEAD -> master, tag: v1.18.2 */
|
||||
/* git commit: 7e7eb695d581782f04b24dc444cbfde86af59853 */
|
||||
/* commit time: 2018-07-01 22:49:01 +0200 */
|
Reference in New Issue
Block a user