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
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
|
/*
Copyright (c) 2001, 2013, Oracle and/or its affiliates.
Copyright (c) 2010, 2019, MariaDB
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; version 2 of the License.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
/*
Character set conversion utility
*/
#include "mariadb.h"
#include "client_priv.h"
#include "sql_string.h"
#include "my_dir.h"
#define CONV_VERSION "1.0"
class CmdOpt
{
public:
const char *m_charset_from;
const char *m_charset_to;
const char *m_delimiter;
my_bool m_continue;
CmdOpt()
:m_charset_from("latin1"),
m_charset_to("latin1"),
m_delimiter(NULL),
m_continue(FALSE)
{ }
static CHARSET_INFO *csinfo_by_name(const char *csname)
{
return get_charset_by_csname(csname, MY_CS_PRIMARY, MYF(MY_UTF8_IS_UTF8MB3));
}
CHARSET_INFO *csinfo_from() const
{
return m_charset_from ? csinfo_by_name(m_charset_from) : NULL;
}
CHARSET_INFO *csinfo_to() const
{
return m_charset_to ? csinfo_by_name(m_charset_to) : NULL;
}
};
static CmdOpt opt;
static struct my_option long_options[] =
{
{"from", 'f', "Specifies the encoding of the input.", &opt.m_charset_from,
&opt.m_charset_from, 0, GET_STR, REQUIRED_ARG, 0, 0, 0, 0, 0, 0},
{"to", 't', "Specifies the encoding of the output.", &opt.m_charset_to,
&opt.m_charset_to, 0, GET_STR, REQUIRED_ARG, 0, 0, 0, 0, 0, 0},
{"continue", 'c', "Silently ignore conversion errors.",
&opt.m_continue, &opt.m_continue, 0, GET_BOOL, NO_ARG, 0, 0, 0, 0, 0, 0},
{"delimiter", 0, "Treat the specified characters as delimiters.",
&opt.m_delimiter, &opt.m_delimiter, 0, GET_STR, REQUIRED_ARG,
0, 0, 0, 0, 0, 0},
{"character-sets-dir", OPT_CHARSETS_DIR,
"Directory for character set files.", &charsets_dir,
&charsets_dir, 0, GET_STR, REQUIRED_ARG, 0, 0, 0, 0, 0, 0},
{0, 0, 0, 0, 0, 0, GET_NO_ARG, NO_ARG, 0, 0, 0, 0, 0, 0}
};
my_bool
get_one_option(const struct my_option *opt,
const char *value, const char *filename)
{
return 0;
}
class File_buffer: public Binary_string
{
public:
bool load_binary_stream(FILE *file);
bool load_binary_file_by_name(const char *file);
};
/*
Load data from a binary stream whose length is not known in advance,
e.g. from stdin.
*/
bool File_buffer::load_binary_stream(FILE *file)
{
for ( ; ; )
{
char buf[1024];
if (length() + sizeof(buf) > UINT_MAX32 || reserve(sizeof(buf)))
{
fprintf(stderr, "Input data is too large\n");
return true;
}
size_t nbytes= my_fread(file, (uchar *) end(), sizeof(buf), MYF(0));
if (!nbytes || nbytes == (size_t) -1)
return false;
str_length+= (uint32) nbytes;
}
return false;
}
/*
Load data from a file by name.
The file size is know.
*/
bool File_buffer::load_binary_file_by_name(const char *filename)
{
MY_STAT sbuf;
File fd;
if (!my_stat(filename, &sbuf, MYF(0)))
{
fprintf(stderr, "my_stat failed for '%s'\n", filename);
return true;
}
if (!MY_S_ISREG(sbuf.st_mode))
{
fprintf(stderr, "'%s' is not a regular file\n", filename);
return true;
}
if ((size_t) sbuf.st_size > UINT_MAX32)
{
fprintf(stderr, "File '%s' is too large\n", filename);
return true;
}
if (alloc((uint32) sbuf.st_size))
{
fprintf(stderr, "Failed to allocate read buffer\n");
return true;
}
if ((fd= my_open(filename, O_RDONLY, MYF(0))) == -1)
{
fprintf(stderr, "Could not open '%s'\n", filename);
return true;
}
size_t nbytes= my_read(fd, (uchar*) Ptr, (size_t)sbuf.st_size, MYF(0));
my_close(fd, MYF(0));
length((uint32) nbytes);
return false;
}
class Delimiter
{
protected:
bool m_delimiter[127];
bool m_has_delimiter_cached;
bool has_delimiter_slow() const
{
for (size_t i= 0; i < sizeof(m_delimiter); i++)
{
if (m_delimiter[i])
return true;
}
return false;
}
bool unescape(char *to, char from) const
{
switch (from) {
case '\\': *to= '\\'; return false;
case 'r': *to= '\r'; return false;
case 'n': *to= '\n'; return false;
case 't': *to= '\t'; return false;
case '0': *to= '\0'; return false;
}
*to= '\0';
return true;
}
bool is_delimiter(char ch) const
{
return (signed char) ch < 0 ? false : m_delimiter[(uint32) ch];
}
public:
Delimiter()
:m_has_delimiter_cached(false)
{
bzero(&m_delimiter, sizeof(m_delimiter));
}
bool has_delimiter() const
{
return m_has_delimiter_cached;
}
bool set_delimiter_unescape(const char *str)
{
m_has_delimiter_cached= false;
for ( ; *str; str++)
{
if ((signed char) *str < 0)
return true;
if (*str == '\\')
{
char unescaped;
str++;
if (!*str || unescape(&unescaped, *str))
return true;
m_delimiter[(uint) unescaped]= true;
}
else
m_delimiter[(uint) *str]= true;
}
m_has_delimiter_cached= has_delimiter_slow();
return false;
}
size_t get_delimiter_length(const char *str, const char *end) const
{
const char *str0= str;
for ( ; str < end; str++)
{
if (!is_delimiter(*str))
break;
}
return str - str0;
}
size_t get_data_length(const char *str, const char *end) const
{
const char *str0= str;
for ( ; str < end; str++)
{
if (is_delimiter(*str))
break;
}
return str - str0;
}
};
class Conv_inbuf
{
const char *m_ptr;
const char *m_end;
public:
Conv_inbuf(const char *from, size_t length)
:m_ptr(from), m_end(from + length)
{ }
const char *ptr() const { return m_ptr; }
const char *end() const { return m_end; }
size_t length() const
{
return m_end - m_ptr;
}
private:
LEX_CSTRING get_prefix(size_t len)
{
LEX_CSTRING res;
res.str= ptr();
res.length= len;
m_ptr+= len;
return res;
}
LEX_CSTRING get_empty_string() const
{
static LEX_CSTRING str= {NULL, 0};
return str;
}
public:
LEX_CSTRING get_delimiter_chunk(const Delimiter &delimiter)
{
if (!delimiter.has_delimiter())
return get_empty_string();
size_t len= delimiter.get_delimiter_length(ptr(), end());
return get_prefix(len);
}
LEX_CSTRING get_data_chunk(const Delimiter &delimiter)
{
if (!delimiter.has_delimiter())
return get_prefix(length());
size_t len= delimiter.get_data_length(ptr(), end());
return get_prefix(len);
}
};
class Conv_outbuf: public Binary_string
{
public:
bool alloc(size_t out_max_length)
{
if (out_max_length >= UINT_MAX32)
{
fprintf(stderr, "The data needs a too large output buffer\n");
return true;
}
if (Binary_string::alloc((uint32) out_max_length))
{
fprintf(stderr, "Failed to allocate the output buffer\n");
return true;
}
return false;
}
};
class Conv: public String_copier, public Delimiter
{
CHARSET_INFO *m_tocs;
CHARSET_INFO *m_fromcs;
bool m_continue;
public:
Conv(CHARSET_INFO *tocs, CHARSET_INFO *fromcs, bool opt_continue)
:m_tocs(tocs), m_fromcs(fromcs), m_continue(opt_continue)
{ }
size_t out_buffer_max_length(size_t from_length) const
{
return from_length / m_fromcs->mbminlen * m_tocs->mbmaxlen;
}
bool convert_data(const char *from, size_t length);
bool convert_binary_stream(FILE *file)
{
File_buffer buf;
return buf.load_binary_stream(file) ||
convert_data(buf.ptr(), buf.length());
}
bool convert_binary_file_by_name(const char *filename)
{
File_buffer buf;
return buf.load_binary_file_by_name(filename)||
convert_data(buf.ptr(), buf.length());
}
private:
void report_error(const char *from) const
{
if (well_formed_error_pos())
{
fflush(stdout);
fprintf(stderr,
"Illegal %s byte sequence at position %d\n",
m_fromcs->cs_name.str,
(uint) (well_formed_error_pos() - from));
}
else if (cannot_convert_error_pos())
{
fflush(stdout);
fprintf(stderr,
"Conversion from %s to %s failed at position %d\n",
m_fromcs->cs_name.str, m_tocs->cs_name.str,
(uint) (cannot_convert_error_pos() - from));
}
}
size_t write(const char *str, size_t length) const
{
return my_fwrite(stdout, (uchar *) str, length, MY_WME);
}
};
bool Conv::convert_data(const char *from, size_t from_length)
{
Conv_inbuf inbuf(from, from_length);
Conv_outbuf outbuf;
if (outbuf.alloc(out_buffer_max_length(from_length)))
return true;
for ( ; ; )
{
LEX_CSTRING delim, data;
delim= inbuf.get_delimiter_chunk(*this);
if (delim.length)
write(delim.str, delim.length);
data= inbuf.get_data_chunk(*this);
if (!data.length)
break;
size_t length= well_formed_copy(m_tocs,
(char *) outbuf.ptr(),
outbuf.alloced_length(),
m_fromcs, data.str, data.length);
outbuf.length((uint32) length);
if (most_important_error_pos() && !m_continue)
{
report_error(from);
return true;
}
write(outbuf.ptr(), outbuf.length());
}
return false;
}
class Session
{
public:
Session(const char *prog)
{
MY_INIT(prog);
}
~Session()
{
my_end(0);
}
void usage(void)
{
printf("%s Ver %s Distrib %s for %s on %s\n", my_progname, CONV_VERSION,
MYSQL_SERVER_VERSION, SYSTEM_TYPE, MACHINE_TYPE);
puts("Character set conversion utility for MariaDB");
puts("Usage:");
printf("%s [OPTION...] [FILE...]\n", my_progname);
my_print_help(long_options);
}
};
int main(int argc, char *argv[])
{
Session session(argv[0]);
CHARSET_INFO *charset_info_from= NULL;
CHARSET_INFO *charset_info_to= NULL;
if (handle_options(&argc, &argv, long_options, get_one_option))
{
session.usage();
return 1;
}
if (!(charset_info_from= opt.csinfo_from()))
{
fprintf(stderr, "Character set %s is not supported\n", opt.m_charset_from);
return 1;
}
if (!(charset_info_to= opt.csinfo_to()))
{
fprintf(stderr, "Character set %s is not supported\n", opt.m_charset_to);
return 1;
}
Conv conv(charset_info_to, charset_info_from, opt.m_continue);
if (opt.m_delimiter)
{
if (charset_info_from->mbminlen > 1 ||
charset_info_to->mbminlen > 1)
{
fprintf(stderr, "--delimiter cannot be used with %s to %s conversion\n",
charset_info_from->cs_name.str, charset_info_to->cs_name.str);
return 1;
}
if (conv.set_delimiter_unescape(opt.m_delimiter))
{
fprintf(stderr, "Bad --delimiter value\n");
return 1;
}
}
if (argc == 0)
{
if (conv.convert_binary_stream(stdin))
return 1;
}
else
{
for (int i= 0; i < argc; i++)
{
if (conv.convert_binary_file_by_name(argv[i]))
return 1;
}
}
return 0;
} /* main */
|