| 1 |
#include "unicodeconv.h" |
| 2 |
|
| 3 |
// Brought from libXML2. |
| 4 |
sds utf16le_to_utf8(const uint8_t *inbyte, int inlenb) { |
| 5 |
uint8_t *inb; |
| 6 |
uint16_t *in ; |
| 7 |
uint16_t *inend; |
| 8 |
uint32_t c, d, inlen; |
| 9 |
int bits; |
| 10 |
|
| 11 |
if(inlenb<0) { |
| 12 |
fprintf(stderr,"! error utf16le_to_utf8: inlenb negative.\n"); |
| 13 |
return NULL; |
| 14 |
} |
| 15 |
inb = malloc(inlenb); |
| 16 |
if(inb==NULL && inlenb>0){ |
| 17 |
fprintf(stderr,"! error utf16le_to_utf8: allocating inb failed.\n"); |
| 18 |
exit(1); |
| 19 |
} |
| 20 |
memcpy(inb, inbyte, (size_t)(inlenb)); |
| 21 |
|
| 22 |
in = (uint16_t *)inb; |
| 23 |
|
| 24 |
if ((inlenb % 2) == 1) (inlenb)--; |
| 25 |
inlen = inlenb / 2; |
| 26 |
inend = in + inlen; |
| 27 |
// pass 1: calculate bytes used for output |
| 28 |
uint32_t bytesNeeded = 0; |
| 29 |
while (in < inend) { |
| 30 |
c = *in++; |
| 31 |
if ((c & 0xFC00) == 0xD800) { // surrogates |
| 32 |
if (in >= inend) { // (in > inend) shouldn't happens |
| 33 |
break; |
| 34 |
} |
| 35 |
d = *in++; |
| 36 |
if ((d & 0xFC00) == 0xDC00) { |
| 37 |
c &= 0x03FF; |
| 38 |
c <<= 10; |
| 39 |
c |= d & 0x03FF; |
| 40 |
c += 0x10000; |
| 41 |
} |
| 42 |
} |
| 43 |
if (c < 0x80) { |
| 44 |
bytesNeeded += 1; |
| 45 |
} else if (c < 0x800) { |
| 46 |
bytesNeeded += 2; |
| 47 |
} else if (c < 0x10000) { |
| 48 |
bytesNeeded += 3; |
| 49 |
} else { |
| 50 |
bytesNeeded += 4; |
| 51 |
} |
| 52 |
} |
| 53 |
in = (uint16_t *)inb; |
| 54 |
sds out = sdsnewlen(NULL, bytesNeeded); |
| 55 |
sds out0 = out; |
| 56 |
|
| 57 |
while (in < inend) { |
| 58 |
c = *in++; |
| 59 |
if ((c & 0xFC00) == 0xD800) { |
| 60 |
if (in >= inend) { break; } |
| 61 |
d = *in++; |
| 62 |
if ((d & 0xFC00) == 0xDC00) { |
| 63 |
c &= 0x03FF; |
| 64 |
c <<= 10; |
| 65 |
c |= d & 0x03FF; |
| 66 |
c += 0x10000; |
| 67 |
} |
| 68 |
} |
| 69 |
|
| 70 |
if (c < 0x80) { |
| 71 |
*out++ = c; |
| 72 |
bits = -6; |
| 73 |
} else if (c < 0x800) { |
| 74 |
*out++ = ((c >> 6) & 0x1F) | 0xC0; |
| 75 |
bits = 0; |
| 76 |
} else if (c < 0x10000) { |
| 77 |
*out++ = ((c >> 12) & 0x0F) | 0xE0; |
| 78 |
bits = 6; |
| 79 |
} else { |
| 80 |
*out++ = ((c >> 18) & 0x07) | 0xF0; |
| 81 |
bits = 12; |
| 82 |
} |
| 83 |
|
| 84 |
for (; bits >= 0; bits -= 6) { |
| 85 |
*out++ = ((c >> bits) & 0x3F) | 0x80; |
| 86 |
} |
| 87 |
} |
| 88 |
if (inb) { |
| 89 |
free(inb); |
| 90 |
} |
| 91 |
return out0; |
| 92 |
} |
| 93 |
|
| 94 |
sds utf16be_to_utf8(const uint8_t *inbyte, int inlenb) { |
| 95 |
uint8_t *inb; |
| 96 |
uint16_t *in ; //= (uint16_t *)inb; |
| 97 |
uint16_t *inend; |
| 98 |
uint32_t c, d, inlen; |
| 99 |
uint8_t *tmp; |
| 100 |
int bits; |
| 101 |
|
| 102 |
if(inlenb<0) { |
| 103 |
fprintf(stderr,"! error utf16be_to_utf8: inlenb negative.\n"); |
| 104 |
return NULL; |
| 105 |
} |
| 106 |
inb = malloc(inlenb); |
| 107 |
if(inb==NULL && inlenb>0){ |
| 108 |
fprintf(stderr,"! error utf16be_to_utf8: allocating inb failed.\n"); |
| 109 |
exit(1); |
| 110 |
} |
| 111 |
memcpy(inb, inbyte, (size_t)(inlenb)); |
| 112 |
|
| 113 |
in = (uint16_t *)inb; |
| 114 |
|
| 115 |
if ((inlenb % 2) == 1) (inlenb)--; |
| 116 |
inlen = inlenb / 2; |
| 117 |
inend = in + inlen; |
| 118 |
uint32_t bytesNeeded = 0; |
| 119 |
while (in < inend) { |
| 120 |
{ |
| 121 |
tmp = (uint8_t *)in; |
| 122 |
c = *tmp++; |
| 123 |
c = c << 8; |
| 124 |
c = c | (uint32_t)*tmp; |
| 125 |
in++; |
| 126 |
} |
| 127 |
if ((c & 0xFC00) == 0xD800) { |
| 128 |
if (in >= inend) { break; } |
| 129 |
{ |
| 130 |
tmp = (uint8_t *)in; |
| 131 |
d = *tmp++; |
| 132 |
d = d << 8; |
| 133 |
d = d | (uint32_t)*tmp; |
| 134 |
in++; |
| 135 |
} |
| 136 |
if ((d & 0xFC00) == 0xDC00) { |
| 137 |
c &= 0x03FF; |
| 138 |
c <<= 10; |
| 139 |
c |= d & 0x03FF; |
| 140 |
c += 0x10000; |
| 141 |
} |
| 142 |
} |
| 143 |
if (c < 0x80) { |
| 144 |
bytesNeeded += 1; |
| 145 |
} else if (c < 0x800) { |
| 146 |
bytesNeeded += 2; |
| 147 |
} else if (c < 0x10000) { |
| 148 |
bytesNeeded += 3; |
| 149 |
} else { |
| 150 |
bytesNeeded += 4; |
| 151 |
} |
| 152 |
} |
| 153 |
in = (uint16_t *)inb; |
| 154 |
sds out = sdsnewlen(NULL, bytesNeeded); |
| 155 |
sds out0 = out; |
| 156 |
|
| 157 |
while (in < inend) { |
| 158 |
{ |
| 159 |
tmp = (uint8_t *)in; |
| 160 |
c = *tmp++; |
| 161 |
c = c << 8; |
| 162 |
c = c | (uint32_t)*tmp; |
| 163 |
in++; |
| 164 |
} |
| 165 |
if ((c & 0xFC00) == 0xD800) { |
| 166 |
if (in >= inend) { break; } |
| 167 |
{ |
| 168 |
tmp = (uint8_t *)in; |
| 169 |
d = *tmp++; |
| 170 |
d = d << 8; |
| 171 |
d = d | (uint32_t)*tmp; |
| 172 |
in++; |
| 173 |
} |
| 174 |
if ((d & 0xFC00) == 0xDC00) { |
| 175 |
c &= 0x03FF; |
| 176 |
c <<= 10; |
| 177 |
c |= d & 0x03FF; |
| 178 |
c += 0x10000; |
| 179 |
} |
| 180 |
} |
| 181 |
|
| 182 |
if (c < 0x80) { |
| 183 |
*out++ = c; |
| 184 |
bits = -6; |
| 185 |
} else if (c < 0x800) { |
| 186 |
*out++ = ((c >> 6) & 0x1F) | 0xC0; |
| 187 |
bits = 0; |
| 188 |
} else if (c < 0x10000) { |
| 189 |
*out++ = ((c >> 12) & 0x0F) | 0xE0; |
| 190 |
bits = 6; |
| 191 |
} else { |
| 192 |
*out++ = ((c >> 18) & 0x07) | 0xF0; |
| 193 |
bits = 12; |
| 194 |
} |
| 195 |
|
| 196 |
for (; bits >= 0; bits -= 6) { |
| 197 |
*out++ = ((c >> bits) & 0x3F) | 0x80; |
| 198 |
} |
| 199 |
} |
| 200 |
if (inb) { |
| 201 |
free(inb); |
| 202 |
} |
| 203 |
return out0; |
| 204 |
} |
| 205 |
|
| 206 |
uint8_t *utf8toutf16be(sds _in, size_t *out_bytes) { |
| 207 |
if (!_in) { |
| 208 |
*out_bytes = 0; |
| 209 |
return NULL; |
| 210 |
} |
| 211 |
sds in = _in; |
| 212 |
size_t inlen = sdslen(in); |
| 213 |
char *inend = in + inlen; |
| 214 |
|
| 215 |
uint32_t wordsNeeded = 0; |
| 216 |
uint8_t trailing = 0; |
| 217 |
uint32_t c = 0; |
| 218 |
while (in < inend) { |
| 219 |
uint8_t d = *in++; |
| 220 |
if (d < 0x80) { |
| 221 |
c = d; |
| 222 |
trailing = 0; |
| 223 |
} else if (d < 0xC0) { |
| 224 |
break; // trailing byte in leading position |
| 225 |
} else if (d < 0xE0) { |
| 226 |
c = d & 0x1F; |
| 227 |
trailing = 1; |
| 228 |
} else if (d < 0xF0) { |
| 229 |
c = d & 0x0F; |
| 230 |
trailing = 2; |
| 231 |
} else if (d < 0xF8) { |
| 232 |
c = d & 0x07; |
| 233 |
trailing = 3; |
| 234 |
} else { |
| 235 |
break; |
| 236 |
} |
| 237 |
if (inend - in < trailing) { break; } |
| 238 |
for (; trailing; trailing--) { |
| 239 |
if ((in >= inend) || (((d = *in++) & 0xC0) != 0x80)) break; |
| 240 |
c <<= 6; |
| 241 |
c |= d & 0x3F; |
| 242 |
} |
| 243 |
if (c < 0x10000) { |
| 244 |
wordsNeeded += 1; |
| 245 |
} else if (c < 0x110000) { |
| 246 |
wordsNeeded += 2; |
| 247 |
} |
| 248 |
} |
| 249 |
uint8_t *_out = malloc(2 * wordsNeeded * sizeof(uint8_t)); |
| 250 |
uint8_t *out = _out; |
| 251 |
in = _in; |
| 252 |
while (in < inend) { |
| 253 |
uint8_t d = *in++; |
| 254 |
if (d < 0x80) { |
| 255 |
c = d; |
| 256 |
trailing = 0; |
| 257 |
} else if (d < 0xC0) { |
| 258 |
break; // trailing byte in leading position |
| 259 |
} else if (d < 0xE0) { |
| 260 |
c = d & 0x1F; |
| 261 |
trailing = 1; |
| 262 |
} else if (d < 0xF0) { |
| 263 |
c = d & 0x0F; |
| 264 |
trailing = 2; |
| 265 |
} else if (d < 0xF8) { |
| 266 |
c = d & 0x07; |
| 267 |
trailing = 3; |
| 268 |
} else { |
| 269 |
break; |
| 270 |
} |
| 271 |
if (inend - in < trailing) { break; } |
| 272 |
for (; trailing; trailing--) { |
| 273 |
if ((in >= inend) || (((d = *in++) & 0xC0) != 0x80)) break; |
| 274 |
c <<= 6; |
| 275 |
c |= d & 0x3F; |
| 276 |
} |
| 277 |
if (c < 0x10000) { |
| 278 |
*(out++) = (c >> 8) & 0xFF; |
| 279 |
*(out++) = c & 0xFF; |
| 280 |
} else if (c < 0x110000) { |
| 281 |
uint16_t tmp1 = 0xD800 | (c >> 10); |
| 282 |
*(out++) = (tmp1 >> 8) & 0xFF; |
| 283 |
*(out++) = tmp1 & 0xFF; |
| 284 |
uint16_t tmp2 = 0xDC00 | (c & 0x03FF); |
| 285 |
*(out++) = (tmp2 >> 8) & 0xFF; |
| 286 |
*(out++) = tmp2 & 0xFF; |
| 287 |
} |
| 288 |
} |
| 289 |
*out_bytes = wordsNeeded * 2; |
| 290 |
return _out; |
| 291 |
} |