comparison libtomcrypt/src/pk/asn1/der/utf8/der_encode_utf8_string.c @ 1471:6dba84798cd5

Update to libtomcrypt 1.18.1, merged with Dropbear changes
author Matt Johnston <matt@ucc.asn.au>
date Fri, 09 Feb 2018 21:44:05 +0800
parents f849a5ca2efc
children
comparison
equal deleted inserted replaced
1470:8bba51a55704 1471:6dba84798cd5
3 * LibTomCrypt is a library that provides various cryptographic 3 * LibTomCrypt is a library that provides various cryptographic
4 * algorithms in a highly modular and flexible manner. 4 * algorithms in a highly modular and flexible manner.
5 * 5 *
6 * The library is free for all purposes without any express 6 * The library is free for all purposes without any express
7 * guarantee it works. 7 * guarantee it works.
8 *
9 * Tom St Denis, [email protected], http://libtom.org
10 */ 8 */
11 #include "tomcrypt.h" 9 #include "tomcrypt.h"
12 10
13 /** 11 /**
14 @file der_encode_utf8_string.c 12 @file der_encode_utf8_string.c
35 LTC_ARGCHK(out != NULL); 33 LTC_ARGCHK(out != NULL);
36 LTC_ARGCHK(outlen != NULL); 34 LTC_ARGCHK(outlen != NULL);
37 35
38 /* get the size */ 36 /* get the size */
39 for (x = len = 0; x < inlen; x++) { 37 for (x = len = 0; x < inlen; x++) {
40 if (in[x] < 0 || in[x] > 0x1FFFF) { 38 if (!der_utf8_valid_char(in[x])) return CRYPT_INVALID_ARG;
41 return CRYPT_INVALID_ARG;
42 }
43 len += der_utf8_charsize(in[x]); 39 len += der_utf8_charsize(in[x]);
44 } 40 }
45 41
46 if (len < 128) { 42 if (len < 128) {
47 y = 2 + len; 43 y = 2 + len;
55 return CRYPT_INVALID_ARG; 51 return CRYPT_INVALID_ARG;
56 } 52 }
57 53
58 /* too big? */ 54 /* too big? */
59 if (y > *outlen) { 55 if (y > *outlen) {
60 *outlen = len; 56 *outlen = y;
61 return CRYPT_BUFFER_OVERFLOW; 57 return CRYPT_BUFFER_OVERFLOW;
62 } 58 }
63 59
64 /* encode the header+len */ 60 /* encode the header+len */
65 x = 0; 61 x = 0;
77 out[x++] = 0x83; 73 out[x++] = 0x83;
78 out[x++] = (unsigned char)((len>>16)&255); 74 out[x++] = (unsigned char)((len>>16)&255);
79 out[x++] = (unsigned char)((len>>8)&255); 75 out[x++] = (unsigned char)((len>>8)&255);
80 out[x++] = (unsigned char)(len&255); 76 out[x++] = (unsigned char)(len&255);
81 } else { 77 } else {
78 /* coverity[dead_error_line] */
82 return CRYPT_INVALID_ARG; 79 return CRYPT_INVALID_ARG;
83 } 80 }
84 81
85 /* store UTF8 */ 82 /* store UTF8 */
86 for (y = 0; y < inlen; y++) { 83 for (y = 0; y < inlen; y++) {
87 switch (der_utf8_charsize(in[y])) { 84 switch (der_utf8_charsize(in[y])) {
88 case 1: out[x++] = (unsigned char)in[y]; break; 85 case 1: out[x++] = (unsigned char)in[y]; break;
89 case 2: out[x++] = 0xC0 | ((in[y] >> 6) & 0x1F); out[x++] = 0x80 | (in[y] & 0x3F); break; 86 case 2: out[x++] = 0xC0 | ((in[y] >> 6) & 0x1F); out[x++] = 0x80 | (in[y] & 0x3F); break;
90 case 3: out[x++] = 0xE0 | ((in[y] >> 12) & 0x0F); out[x++] = 0x80 | ((in[y] >> 6) & 0x3F); out[x++] = 0x80 | (in[y] & 0x3F); break; 87 case 3: out[x++] = 0xE0 | ((in[y] >> 12) & 0x0F); out[x++] = 0x80 | ((in[y] >> 6) & 0x3F); out[x++] = 0x80 | (in[y] & 0x3F); break;
88 #if !defined(LTC_WCHAR_MAX) || LTC_WCHAR_MAX > 0xFFFF
91 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; 89 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;
90 #endif
92 } 91 }
93 } 92 }
94 93
95 /* retun length */ 94 /* retun length */
96 *outlen = x; 95 *outlen = x;
98 return CRYPT_OK; 97 return CRYPT_OK;
99 } 98 }
100 99
101 #endif 100 #endif
102 101
103 /* $Source$ */ 102 /* ref: $Format:%D$ */
104 /* $Revision$ */ 103 /* git commit: $Format:%H$ */
105 /* $Date$ */ 104 /* commit time: $Format:%ai$ */