-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathencoding.c
More file actions
249 lines (215 loc) · 4.76 KB
/
encoding.c
File metadata and controls
249 lines (215 loc) · 4.76 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
/*
* encoding.c
*
* Copyright (C) 2025 Brandon Casey
*/
#include "compat.h"
#include "encoding.h"
#include <assert.h>
#include <ctype.h>
#include <errno.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <stdarg.h>
#include <string.h>
#include <iconv.h>
static void* xmalloc (size_t sz)
{
void *ptr = malloc(sz);
if (!ptr) {
perror("memory allocation failed");
abort();
}
return ptr;
}
static void* xrealloc (void *ptr, size_t sz)
{
void *nptr = realloc(ptr, sz);
if (!nptr && sz) {
perror("memory allocation failed");
abort();
}
return nptr;
}
/*
* Trim leading and trailing whitespace.
*/
char* strtrim (char *s) {
char *head = s;
size_t len;
/* trim leading space */
while (isspace((unsigned char)*s))
s++;
/* trim trailing space */
for (len = strlen(s); len && isspace((unsigned char)s[len-1]); len--)
;
if (head != s)
memmove(head, s, len);
head[len] = '\0';
return head;
}
char* strdup_printf (const char *fmt, ...) {
char *s;
int sz;
va_list ap;
va_start(ap, fmt);
sz = vsnprintf(NULL, 0, fmt, ap);
if (sz < 0) {
perror("vnsprintf failed");
abort();
}
sz++;
s = xmalloc(sz);
va_end(ap);
va_start(ap, fmt);
vsnprintf(s, sz, fmt, ap);
va_end(ap);
return s;
}
/*
* Delete non-UCS-2 characters from string.
*/
size_t delete_non_ucs2le (uint8_t *s, size_t len) {
uint8_t *h = s;
size_t i;
assert(len < SIZE_MAX);
/* The range 0xD800 to 0xDFFF have a special purpose. */
for (i = 1; i < len; i+=2, s+=2)
if (s[1] < 0xD8 || s[1] > 0xDF) {
*h++ = s[0];
*h++ = s[1];
}
return s - h + (len - i + 1);
}
/*
* Converts string from src_encoding to dest_encoding. The result is
* returned in an allocated string which must be freed.
*/
static char* convert (const char *src_encoding, const char *src, size_t slen,
const char *dest_encoding, size_t *dlen)
{
iconv_t cd;
char *buf = NULL;
char *ptr;
size_t avail = 0;
size_t len = 0;
cd = iconv_open(dest_encoding, src_encoding);
if (cd == (iconv_t)-1)
return NULL;
do {
len += slen + 4;
avail += slen + 4;
buf = xrealloc(buf, len);
ptr = buf + len - avail;
if (iconv(cd, (char**)&src, &slen, &ptr, &avail) == (size_t)-1)
{
if (errno == E2BIG)
continue;
free(buf);
iconv_close(cd);
return NULL;
}
} while (slen);
while (1) {
if (iconv(cd, NULL, &slen, &ptr, &avail) == (size_t)-1) {
if (errno == E2BIG) {
len += 4;
avail += 4;
buf = xrealloc(buf, len);
ptr = buf + len - avail;
continue;
}
free(buf);
iconv_close(cd);
return NULL;
}
break;
}
if (iconv_close(cd) == -1)
perror("iconv_close failed");
/* *dlen will contain the number of bytes written to dest */
*dlen = len - avail;
return buf;
}
/*
* Converts string from src_encoding to dest_encoding using a fixed-size
* destination buffer. If the string in `src` cannot be represented in
* the new encoding within the `dest` buffer, then it will be truncated.
*/
static size_t convert_buf (const char *src_encoding, const char *src,
size_t slen, const char *dest_encoding, char *dest, size_t dlen)
{
iconv_t cd;
size_t avail = dlen;
cd = iconv_open(dest_encoding, src_encoding);
if (cd == (iconv_t)-1)
return (size_t)-1;
if (iconv(cd, (char**)&src, &slen, &dest, &avail) == (size_t)-1)
{
if (errno != E2BIG) {
iconv_close(cd);
return (size_t)-1;
}
}
if (iconv(cd, NULL, &slen, &dest, &avail) == (size_t)-1) {
if (errno != E2BIG) {
iconv_close(cd);
return (size_t)-1;
}
}
if (iconv_close(cd) == -1)
perror("iconv_close failed");
return dlen - avail;
}
/*
* Convert UTF-16 LE to UTF-8
*
* Returns allocated string which must be freed.
* `len` parameter is updated with length of returned string.
*
* Params:
* src pointer to UCS-2 LE bytes
* slen number of bytes in src to convert
*
* dlen number of bytes returned.
*/
char* utf16le_to_utf8 (const uint8_t *src, size_t slen, size_t *dlen)
{
char *dest;
dest = convert("UTF-16LE", (const char*)src, slen, "UTF-8", dlen);
if (dest) {
dest = xrealloc(dest, *dlen + 1);
dest[*dlen] = '\0';
}
return dest;
}
/*
* Convert UTF-8 to UTF-16 LE
*
* Returns allocated string which must be freed.
* `len` parameter is updated with length of returned string.
*
* Params:
* src pointer to UTF-8 bytes
* slen number of bytes in src to convert
*
* dlen number of bytes in returned buffer.
*/
uint8_t* utf8_to_utf16le (const char *src, size_t slen, size_t *dlen)
{
uint8_t *dest;
dest = (uint8_t*)convert("UTF-8", src, slen, "UTF-16LE", dlen);
if (dest) {
assert((*dlen & 0x01) == 0);
dest = xrealloc(dest, *dlen + 2);
dest[*dlen] = 0;
dest[*dlen + 1] = 0;
}
return dest;
}
size_t utf8_to_utf16le_buf (const char *src, size_t slen, uint8_t *dest,
size_t dlen)
{
return convert_buf("UTF-8", src, slen, "UTF-16LE", (char*)dest, dlen);
}