-
Notifications
You must be signed in to change notification settings - Fork 54
/
Copy patharchive_string.c
4240 lines (3901 loc) · 104 KB
/
archive_string.c
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
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*-
* Copyright (c) 2003-2011 Tim Kientzle
* Copyright (c) 2011-2012 Michihiro NAKAJIMA
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR(S) ``AS IS'' AND ANY EXPRESS OR
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
* IN NO EVENT SHALL THE AUTHOR(S) BE LIABLE FOR ANY DIRECT, INDIRECT,
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#include "archive_platform.h"
__FBSDID("$FreeBSD: head/lib/libarchive/archive_string.c 201095 2009-12-28 02:33:22Z kientzle $");
/*
* Basic resizable string support, to simplify manipulating arbitrary-sized
* strings while minimizing heap activity.
*
* In particular, the buffer used by a string object is only grown, it
* never shrinks, so you can clear and reuse the same string object
* without incurring additional memory allocations.
*/
#ifdef HAVE_ERRNO_H
#include <errno.h>
#endif
#ifdef HAVE_ICONV_H
#include <iconv.h>
#endif
#ifdef HAVE_LANGINFO_H
#include <langinfo.h>
#endif
#ifdef HAVE_LOCALCHARSET_H
#include <localcharset.h>
#endif
#ifdef HAVE_STDLIB_H
#include <stdlib.h>
#endif
#ifdef HAVE_STRING_H
#include <string.h>
#endif
#ifdef HAVE_WCHAR_H
#include <wchar.h>
#endif
#if defined(_WIN32) && !defined(__CYGWIN__)
#include <windows.h>
#include <locale.h>
#endif
#include "archive_endian.h"
#include "archive_private.h"
#include "archive_string.h"
#include "archive_string_composition.h"
#if !defined(HAVE_WMEMCPY) && !defined(wmemcpy)
#define wmemcpy(a,b,i) (wchar_t *)memcpy((a), (b), (i) * sizeof(wchar_t))
#endif
#if !defined(HAVE_WMEMMOVE) && !defined(wmemmove)
#define wmemmove(a,b,i) (wchar_t *)memmove((a), (b), (i) * sizeof(wchar_t))
#endif
#undef max
#define max(a, b) ((a)>(b)?(a):(b))
struct archive_string_conv {
struct archive_string_conv *next;
char *from_charset;
char *to_charset;
unsigned from_cp;
unsigned to_cp;
/* Set 1 if from_charset and to_charset are the same. */
int same;
int flag;
#define SCONV_TO_CHARSET 1 /* MBS is being converted to specified
* charset. */
#define SCONV_FROM_CHARSET (1<<1) /* MBS is being converted from
* specified charset. */
#define SCONV_BEST_EFFORT (1<<2) /* Copy at least ASCII code. */
#define SCONV_WIN_CP (1<<3) /* Use Windows API for converting
* MBS. */
#define SCONV_UTF8_LIBARCHIVE_2 (1<<4) /* Incorrect UTF-8 made by libarchive
* 2.x in the wrong assumption. */
#define SCONV_NORMALIZATION_C (1<<6) /* Need normalization to be Form C.
* Before UTF-8 characters are actually
* processed. */
#define SCONV_NORMALIZATION_D (1<<7) /* Need normalization to be Form D.
* Before UTF-8 characters are actually
* processed.
* Currently this only for MAC OS X. */
#define SCONV_TO_UTF8 (1<<8) /* "to charset" side is UTF-8. */
#define SCONV_FROM_UTF8 (1<<9) /* "from charset" side is UTF-8. */
#define SCONV_TO_UTF16BE (1<<10) /* "to charset" side is UTF-16BE. */
#define SCONV_FROM_UTF16BE (1<<11) /* "from charset" side is UTF-16BE. */
#define SCONV_TO_UTF16LE (1<<12) /* "to charset" side is UTF-16LE. */
#define SCONV_FROM_UTF16LE (1<<13) /* "from charset" side is UTF-16LE. */
#define SCONV_TO_UTF16 (SCONV_TO_UTF16BE | SCONV_TO_UTF16LE)
#define SCONV_FROM_UTF16 (SCONV_FROM_UTF16BE | SCONV_FROM_UTF16LE)
#if HAVE_ICONV
iconv_t cd;
iconv_t cd_w;/* Use at archive_mstring on
* Windows. */
#endif
/* A temporary buffer for normalization. */
struct archive_string utftmp;
int (*converter[2])(struct archive_string *, const void *, size_t,
struct archive_string_conv *);
int nconverter;
};
#define CP_C_LOCALE 0 /* "C" locale only for this file. */
#define CP_UTF16LE 1200
#define CP_UTF16BE 1201
#define IS_HIGH_SURROGATE_LA(uc) ((uc) >= 0xD800 && (uc) <= 0xDBFF)
#define IS_LOW_SURROGATE_LA(uc) ((uc) >= 0xDC00 && (uc) <= 0xDFFF)
#define IS_SURROGATE_PAIR_LA(uc) ((uc) >= 0xD800 && (uc) <= 0xDFFF)
#define UNICODE_MAX 0x10FFFF
#define UNICODE_R_CHAR 0xFFFD /* Replacement character. */
/* Set U+FFFD(Replacement character) in UTF-8. */
static const char utf8_replacement_char[] = {0xef, 0xbf, 0xbd};
static struct archive_string_conv *find_sconv_object(struct archive *,
const char *, const char *);
static void add_sconv_object(struct archive *, struct archive_string_conv *);
static struct archive_string_conv *create_sconv_object(const char *,
const char *, unsigned, int);
static void free_sconv_object(struct archive_string_conv *);
static struct archive_string_conv *get_sconv_object(struct archive *,
const char *, const char *, int);
static unsigned make_codepage_from_charset(const char *);
static unsigned get_current_codepage(void);
static unsigned get_current_oemcp(void);
static size_t mbsnbytes(const void *, size_t);
static size_t utf16nbytes(const void *, size_t);
#if defined(_WIN32) && !defined(__CYGWIN__)
static int archive_wstring_append_from_mbs_in_codepage(
struct archive_wstring *, const char *, size_t,
struct archive_string_conv *);
static int archive_string_append_from_wcs_in_codepage(struct archive_string *,
const wchar_t *, size_t, struct archive_string_conv *);
static int is_big_endian(void);
static int strncat_in_codepage(struct archive_string *, const void *,
size_t, struct archive_string_conv *);
static int win_strncat_from_utf16be(struct archive_string *, const void *,
size_t, struct archive_string_conv *);
static int win_strncat_from_utf16le(struct archive_string *, const void *,
size_t, struct archive_string_conv *);
static int win_strncat_to_utf16be(struct archive_string *, const void *,
size_t, struct archive_string_conv *);
static int win_strncat_to_utf16le(struct archive_string *, const void *,
size_t, struct archive_string_conv *);
#endif
static int best_effort_strncat_from_utf16be(struct archive_string *,
const void *, size_t, struct archive_string_conv *);
static int best_effort_strncat_from_utf16le(struct archive_string *,
const void *, size_t, struct archive_string_conv *);
static int best_effort_strncat_to_utf16be(struct archive_string *,
const void *, size_t, struct archive_string_conv *);
static int best_effort_strncat_to_utf16le(struct archive_string *,
const void *, size_t, struct archive_string_conv *);
#if defined(HAVE_ICONV)
static int iconv_strncat_in_locale(struct archive_string *, const void *,
size_t, struct archive_string_conv *);
#endif
static int best_effort_strncat_in_locale(struct archive_string *,
const void *, size_t, struct archive_string_conv *);
static int _utf8_to_unicode(uint32_t *, const char *, size_t);
static int utf8_to_unicode(uint32_t *, const char *, size_t);
static inline uint32_t combine_surrogate_pair(uint32_t, uint32_t);
static int cesu8_to_unicode(uint32_t *, const char *, size_t);
static size_t unicode_to_utf8(char *, size_t, uint32_t);
static int utf16_to_unicode(uint32_t *, const char *, size_t, int);
static size_t unicode_to_utf16be(char *, size_t, uint32_t);
static size_t unicode_to_utf16le(char *, size_t, uint32_t);
static int strncat_from_utf8_libarchive2(struct archive_string *,
const void *, size_t, struct archive_string_conv *);
static int strncat_from_utf8_to_utf8(struct archive_string *, const void *,
size_t, struct archive_string_conv *);
static int archive_string_normalize_C(struct archive_string *, const void *,
size_t, struct archive_string_conv *);
static int archive_string_normalize_D(struct archive_string *, const void *,
size_t, struct archive_string_conv *);
static int archive_string_append_unicode(struct archive_string *,
const void *, size_t, struct archive_string_conv *);
static struct archive_string *
archive_string_append(struct archive_string *as, const char *p, size_t s)
{
if (archive_string_ensure(as, as->length + s + 1) == NULL)
return (NULL);
if (s)
memmove(as->s + as->length, p, s);
as->length += s;
as->s[as->length] = 0;
return (as);
}
static struct archive_wstring *
archive_wstring_append(struct archive_wstring *as, const wchar_t *p, size_t s)
{
if (archive_wstring_ensure(as, as->length + s + 1) == NULL)
return (NULL);
if (s)
wmemmove(as->s + as->length, p, s);
as->length += s;
as->s[as->length] = 0;
return (as);
}
struct archive_string *
archive_array_append(struct archive_string *as, const char *p, size_t s)
{
return archive_string_append(as, p, s);
}
void
archive_string_concat(struct archive_string *dest, struct archive_string *src)
{
if (archive_string_append(dest, src->s, src->length) == NULL)
__archive_errx(1, "Out of memory");
}
void
archive_wstring_concat(struct archive_wstring *dest,
struct archive_wstring *src)
{
if (archive_wstring_append(dest, src->s, src->length) == NULL)
__archive_errx(1, "Out of memory");
}
void
archive_string_free(struct archive_string *as)
{
as->length = 0;
as->buffer_length = 0;
free(as->s);
as->s = NULL;
}
void
archive_wstring_free(struct archive_wstring *as)
{
as->length = 0;
as->buffer_length = 0;
free(as->s);
as->s = NULL;
}
struct archive_wstring *
archive_wstring_ensure(struct archive_wstring *as, size_t s)
{
return (struct archive_wstring *)
archive_string_ensure((struct archive_string *)as,
s * sizeof(wchar_t));
}
/* Returns NULL on any allocation failure. */
struct archive_string *
archive_string_ensure(struct archive_string *as, size_t s)
{
char *p;
size_t new_length;
/* If buffer is already big enough, don't reallocate. */
if (as->s && (s <= as->buffer_length))
return (as);
/*
* Growing the buffer at least exponentially ensures that
* append operations are always linear in the number of
* characters appended. Using a smaller growth rate for
* larger buffers reduces memory waste somewhat at the cost of
* a larger constant factor.
*/
if (as->buffer_length < 32)
/* Start with a minimum 32-character buffer. */
new_length = 32;
else if (as->buffer_length < 8192)
/* Buffers under 8k are doubled for speed. */
new_length = as->buffer_length + as->buffer_length;
else {
/* Buffers 8k and over grow by at least 25% each time. */
new_length = as->buffer_length + as->buffer_length / 4;
/* Be safe: If size wraps, fail. */
if (new_length < as->buffer_length) {
/* On failure, wipe the string and return NULL. */
archive_string_free(as);
errno = ENOMEM;/* Make sure errno has ENOMEM. */
return (NULL);
}
}
/*
* The computation above is a lower limit to how much we'll
* grow the buffer. In any case, we have to grow it enough to
* hold the request.
*/
if (new_length < s)
new_length = s;
/* Now we can reallocate the buffer. */
p = (char *)realloc(as->s, new_length);
if (p == NULL) {
/* On failure, wipe the string and return NULL. */
archive_string_free(as);
errno = ENOMEM;/* Make sure errno has ENOMEM. */
return (NULL);
}
as->s = p;
as->buffer_length = new_length;
return (as);
}
/*
* TODO: See if there's a way to avoid scanning
* the source string twice. Then test to see
* if it actually helps (remember that we're almost
* always called with pretty short arguments, so
* such an optimization might not help).
*/
struct archive_string *
archive_strncat(struct archive_string *as, const void *_p, size_t n)
{
size_t s;
const char *p, *pp;
p = (const char *)_p;
/* Like strlen(p), except won't examine positions beyond p[n]. */
s = 0;
pp = p;
while (s < n && *pp) {
pp++;
s++;
}
if ((as = archive_string_append(as, p, s)) == NULL)
__archive_errx(1, "Out of memory");
return (as);
}
struct archive_wstring *
archive_wstrncat(struct archive_wstring *as, const wchar_t *p, size_t n)
{
size_t s;
const wchar_t *pp;
/* Like strlen(p), except won't examine positions beyond p[n]. */
s = 0;
pp = p;
while (s < n && *pp) {
pp++;
s++;
}
if ((as = archive_wstring_append(as, p, s)) == NULL)
__archive_errx(1, "Out of memory");
return (as);
}
struct archive_string *
archive_strcat(struct archive_string *as, const void *p)
{
/* strcat is just strncat without an effective limit.
* Assert that we'll never get called with a source
* string over 16MB.
* TODO: Review all uses of strcat in the source
* and try to replace them with strncat().
*/
return archive_strncat(as, p, 0x1000000);
}
struct archive_wstring *
archive_wstrcat(struct archive_wstring *as, const wchar_t *p)
{
/* Ditto. */
return archive_wstrncat(as, p, 0x1000000);
}
struct archive_string *
archive_strappend_char(struct archive_string *as, char c)
{
if ((as = archive_string_append(as, &c, 1)) == NULL)
__archive_errx(1, "Out of memory");
return (as);
}
struct archive_wstring *
archive_wstrappend_wchar(struct archive_wstring *as, wchar_t c)
{
if ((as = archive_wstring_append(as, &c, 1)) == NULL)
__archive_errx(1, "Out of memory");
return (as);
}
/*
* Get the "current character set" name to use with iconv.
* On FreeBSD, the empty character set name "" chooses
* the correct character encoding for the current locale,
* so this isn't necessary.
* But iconv on Mac OS 10.6 doesn't seem to handle this correctly;
* on that system, we have to explicitly call nl_langinfo()
* to get the right name. Not sure about other platforms.
*
* NOTE: GNU libiconv does not recognize the character-set name
* which some platform nl_langinfo(CODESET) returns, so we should
* use locale_charset() instead of nl_langinfo(CODESET) for GNU libiconv.
*/
static const char *
default_iconv_charset(const char *charset) {
if (charset != NULL && charset[0] != '\0')
return charset;
#if HAVE_LOCALE_CHARSET && !defined(__APPLE__)
/* locale_charset() is broken on Mac OS */
return locale_charset();
#elif HAVE_NL_LANGINFO
return nl_langinfo(CODESET);
#else
return "";
#endif
}
#if defined(_WIN32) && !defined(__CYGWIN__)
/*
* Convert MBS to WCS.
* Note: returns -1 if conversion fails.
*/
int
archive_wstring_append_from_mbs(struct archive_wstring *dest,
const char *p, size_t len)
{
return archive_wstring_append_from_mbs_in_codepage(dest, p, len, NULL);
}
static int
archive_wstring_append_from_mbs_in_codepage(struct archive_wstring *dest,
const char *s, size_t length, struct archive_string_conv *sc)
{
int count, ret = 0;
UINT from_cp;
if (sc != NULL)
from_cp = sc->from_cp;
else
from_cp = get_current_codepage();
if (from_cp == CP_C_LOCALE) {
/*
* "C" locale special processing.
*/
wchar_t *ws;
const unsigned char *mp;
if (NULL == archive_wstring_ensure(dest,
dest->length + length + 1))
return (-1);
ws = dest->s + dest->length;
mp = (const unsigned char *)s;
count = 0;
while (count < (int)length && *mp) {
*ws++ = (wchar_t)*mp++;
count++;
}
} else if (sc != NULL &&
(sc->flag & (SCONV_NORMALIZATION_C | SCONV_NORMALIZATION_D))) {
/*
* Normalize UTF-8 and UTF-16BE and convert it directly
* to UTF-16 as wchar_t.
*/
struct archive_string u16;
int saved_flag = sc->flag;/* save current flag. */
if (is_big_endian())
sc->flag |= SCONV_TO_UTF16BE;
else
sc->flag |= SCONV_TO_UTF16LE;
if (sc->flag & SCONV_FROM_UTF16) {
/*
* UTF-16BE/LE NFD ===> UTF-16 NFC
* UTF-16BE/LE NFC ===> UTF-16 NFD
*/
count = (int)utf16nbytes(s, length);
} else {
/*
* UTF-8 NFD ===> UTF-16 NFC
* UTF-8 NFC ===> UTF-16 NFD
*/
count = (int)mbsnbytes(s, length);
}
u16.s = (char *)dest->s;
u16.length = dest->length << 1;;
u16.buffer_length = dest->buffer_length;
if (sc->flag & SCONV_NORMALIZATION_C)
ret = archive_string_normalize_C(&u16, s, count, sc);
else
ret = archive_string_normalize_D(&u16, s, count, sc);
dest->s = (wchar_t *)u16.s;
dest->length = u16.length >> 1;
dest->buffer_length = u16.buffer_length;
sc->flag = saved_flag;/* restore the saved flag. */
return (ret);
} else if (sc != NULL && (sc->flag & SCONV_FROM_UTF16)) {
count = (int)utf16nbytes(s, length);
count >>= 1; /* to be WCS length */
/* Allocate memory for WCS. */
if (NULL == archive_wstring_ensure(dest,
dest->length + count + 1))
return (-1);
wmemcpy(dest->s + dest->length, (const wchar_t *)s, count);
if ((sc->flag & SCONV_FROM_UTF16BE) && !is_big_endian()) {
uint16_t *u16 = (uint16_t *)(dest->s + dest->length);
int b;
for (b = 0; b < count; b++) {
uint16_t val = archive_le16dec(u16+b);
archive_be16enc(u16+b, val);
}
} else if ((sc->flag & SCONV_FROM_UTF16LE) && is_big_endian()) {
uint16_t *u16 = (uint16_t *)(dest->s + dest->length);
int b;
for (b = 0; b < count; b++) {
uint16_t val = archive_be16dec(u16+b);
archive_le16enc(u16+b, val);
}
}
} else {
DWORD mbflag;
size_t buffsize;
if (sc == NULL)
mbflag = 0;
else if (sc->flag & SCONV_FROM_CHARSET) {
/* Do not trust the length which comes from
* an archive file. */
length = mbsnbytes(s, length);
mbflag = 0;
} else
mbflag = MB_PRECOMPOSED;
buffsize = dest->length + length + 1;
do {
/* Allocate memory for WCS. */
if (NULL == archive_wstring_ensure(dest, buffsize))
return (-1);
/* Convert MBS to WCS. */
count = MultiByteToWideChar(from_cp,
mbflag, s, (int)length, dest->s + dest->length,
(int)(dest->buffer_length >> 1) -1);
if (count == 0 &&
GetLastError() == ERROR_INSUFFICIENT_BUFFER) {
/* Expand the WCS buffer. */
buffsize = dest->buffer_length << 1;
continue;
}
if (count == 0 && length != 0)
ret = -1;
break;
} while (1);
}
dest->length += count;
dest->s[dest->length] = L'\0';
return (ret);
}
#else
/*
* Convert MBS to WCS.
* Note: returns -1 if conversion fails.
*/
int
archive_wstring_append_from_mbs(struct archive_wstring *dest,
const char *p, size_t len)
{
size_t r;
int ret_val = 0;
/*
* No single byte will be more than one wide character,
* so this length estimate will always be big enough.
*/
// size_t wcs_length = len;
size_t mbs_length = len;
const char *mbs = p;
wchar_t *wcs;
#if HAVE_MBRTOWC
mbstate_t shift_state;
memset(&shift_state, 0, sizeof(shift_state));
#endif
/*
* As we decided to have wcs_length == mbs_length == len
* we can use len here instead of wcs_length
*/
if (NULL == archive_wstring_ensure(dest, dest->length + len + 1))
return (-1);
wcs = dest->s + dest->length;
/*
* We cannot use mbsrtowcs/mbstowcs here because those may convert
* extra MBS when strlen(p) > len and one wide character consists of
* multi bytes.
*/
while (*mbs && mbs_length > 0) {
/*
* The buffer we allocated is always big enough.
* Keep this code path in a comment if we decide to choose
* smaller wcs_length in the future
*/
/*
if (wcs_length == 0) {
dest->length = wcs - dest->s;
dest->s[dest->length] = L'\0';
wcs_length = mbs_length;
if (NULL == archive_wstring_ensure(dest,
dest->length + wcs_length + 1))
return (-1);
wcs = dest->s + dest->length;
}
*/
#if HAVE_MBRTOWC
r = mbrtowc(wcs, mbs, mbs_length, &shift_state);
#else
r = mbtowc(wcs, mbs, mbs_length);
#endif
if (r == (size_t)-1 || r == (size_t)-2) {
ret_val = -1;
break;
}
if (r == 0 || r > mbs_length)
break;
wcs++;
// wcs_length--;
mbs += r;
mbs_length -= r;
}
dest->length = wcs - dest->s;
dest->s[dest->length] = L'\0';
return (ret_val);
}
#endif
#if defined(_WIN32) && !defined(__CYGWIN__)
/*
* WCS ==> MBS.
* Note: returns -1 if conversion fails.
*
* Win32 builds use WideCharToMultiByte from the Windows API.
* (Maybe Cygwin should too? WideCharToMultiByte will know a
* lot more about local character encodings than the wcrtomb()
* wrapper is going to know.)
*/
int
archive_string_append_from_wcs(struct archive_string *as,
const wchar_t *w, size_t len)
{
return archive_string_append_from_wcs_in_codepage(as, w, len, NULL);
}
static int
archive_string_append_from_wcs_in_codepage(struct archive_string *as,
const wchar_t *ws, size_t len, struct archive_string_conv *sc)
{
BOOL defchar_used, *dp;
int count, ret = 0;
UINT to_cp;
int wslen = (int)len;
if (sc != NULL)
to_cp = sc->to_cp;
else
to_cp = get_current_codepage();
if (to_cp == CP_C_LOCALE) {
/*
* "C" locale special processing.
*/
const wchar_t *wp = ws;
char *p;
if (NULL == archive_string_ensure(as,
as->length + wslen +1))
return (-1);
p = as->s + as->length;
count = 0;
defchar_used = 0;
while (count < wslen && *wp) {
if (*wp > 255) {
*p++ = '?';
wp++;
defchar_used = 1;
} else
*p++ = (char)*wp++;
count++;
}
} else if (sc != NULL && (sc->flag & SCONV_TO_UTF16)) {
uint16_t *u16;
if (NULL ==
archive_string_ensure(as, as->length + len * 2 + 2))
return (-1);
u16 = (uint16_t *)(as->s + as->length);
count = 0;
defchar_used = 0;
if (sc->flag & SCONV_TO_UTF16BE) {
while (count < (int)len && *ws) {
archive_be16enc(u16+count, *ws);
ws++;
count++;
}
} else {
while (count < (int)len && *ws) {
archive_le16enc(u16+count, *ws);
ws++;
count++;
}
}
count <<= 1; /* to be byte size */
} else {
/* Make sure the MBS buffer has plenty to set. */
if (NULL ==
archive_string_ensure(as, as->length + len * 2 + 1))
return (-1);
do {
defchar_used = 0;
if (to_cp == CP_UTF8 || sc == NULL)
dp = NULL;
else
dp = &defchar_used;
count = WideCharToMultiByte(to_cp, 0, ws, wslen,
as->s + as->length,
(int)as->buffer_length - as->length - 1, NULL, dp);
if (count == 0 &&
GetLastError() == ERROR_INSUFFICIENT_BUFFER) {
/* Expand the MBS buffer and retry. */
if (NULL == archive_string_ensure(as,
as->buffer_length + len))
return (-1);
continue;
}
if (count == 0)
ret = -1;
break;
} while (1);
}
as->length += count;
as->s[as->length] = '\0';
return (defchar_used?-1:ret);
}
#elif defined(HAVE_WCTOMB) || defined(HAVE_WCRTOMB)
/*
* Translates a wide character string into current locale character set
* and appends to the archive_string. Note: returns -1 if conversion
* fails.
*/
int
archive_string_append_from_wcs(struct archive_string *as,
const wchar_t *w, size_t len)
{
/* We cannot use the standard wcstombs() here because it
* cannot tell us how big the output buffer should be. So
* I've built a loop around wcrtomb() or wctomb() that
* converts a character at a time and resizes the string as
* needed. We prefer wcrtomb() when it's available because
* it's thread-safe. */
int n, ret_val = 0;
char *p;
char *end;
#if HAVE_WCRTOMB
mbstate_t shift_state;
memset(&shift_state, 0, sizeof(shift_state));
#else
/* Clear the shift state before starting. */
wctomb(NULL, L'\0');
#endif
/*
* Allocate buffer for MBS.
* We need this allocation here since it is possible that
* as->s is still NULL.
*/
if (archive_string_ensure(as, as->length + len + 1) == NULL)
return (-1);
p = as->s + as->length;
end = as->s + as->buffer_length - MB_CUR_MAX -1;
while (*w != L'\0' && len > 0) {
if (p >= end) {
as->length = p - as->s;
as->s[as->length] = '\0';
/* Re-allocate buffer for MBS. */
if (archive_string_ensure(as,
as->length + max(len * 2,
(size_t)MB_CUR_MAX) + 1) == NULL)
return (-1);
p = as->s + as->length;
end = as->s + as->buffer_length - MB_CUR_MAX -1;
}
#if HAVE_WCRTOMB
n = wcrtomb(p, *w++, &shift_state);
#else
n = wctomb(p, *w++);
#endif
if (n == -1) {
if (errno == EILSEQ) {
/* Skip an illegal wide char. */
*p++ = '?';
ret_val = -1;
} else {
ret_val = -1;
break;
}
} else
p += n;
len--;
}
as->length = p - as->s;
as->s[as->length] = '\0';
return (ret_val);
}
#else /* HAVE_WCTOMB || HAVE_WCRTOMB */
/*
* TODO: Test if __STDC_ISO_10646__ is defined.
* Non-Windows uses ISO C wcrtomb() or wctomb() to perform the conversion
* one character at a time. If a non-Windows platform doesn't have
* either of these, fall back to the built-in UTF8 conversion.
*/
int
archive_string_append_from_wcs(struct archive_string *as,
const wchar_t *w, size_t len)
{
(void)as;/* UNUSED */
(void)w;/* UNUSED */
(void)len;/* UNUSED */
errno = ENOSYS;
return (-1);
}
#endif /* HAVE_WCTOMB || HAVE_WCRTOMB */
/*
* Find a string conversion object by a pair of 'from' charset name
* and 'to' charset name from an archive object.
* Return NULL if not found.
*/
static struct archive_string_conv *
find_sconv_object(struct archive *a, const char *fc, const char *tc)
{
struct archive_string_conv *sc;
if (a == NULL)
return (NULL);
for (sc = a->sconv; sc != NULL; sc = sc->next) {
if (strcmp(sc->from_charset, fc) == 0 &&
strcmp(sc->to_charset, tc) == 0)
break;
}
return (sc);
}
/*
* Register a string object to an archive object.
*/
static void
add_sconv_object(struct archive *a, struct archive_string_conv *sc)
{
struct archive_string_conv **psc;
/* Add a new sconv to sconv list. */
psc = &(a->sconv);
while (*psc != NULL)
psc = &((*psc)->next);
*psc = sc;
}
static void
add_converter(struct archive_string_conv *sc, int (*converter)
(struct archive_string *, const void *, size_t,
struct archive_string_conv *))
{
if (sc == NULL || sc->nconverter >= 2)
__archive_errx(1, "Programming error");
sc->converter[sc->nconverter++] = converter;
}
static void
setup_converter(struct archive_string_conv *sc)
{
/* Reset. */
sc->nconverter = 0;
/*
* Perform special sequence for the incorrect UTF-8 filenames
* made by libarchive2.x.
*/
if (sc->flag & SCONV_UTF8_LIBARCHIVE_2) {
add_converter(sc, strncat_from_utf8_libarchive2);
return;
}
/*
* Convert a string to UTF-16BE/LE.
*/
if (sc->flag & SCONV_TO_UTF16) {
/*
* If the current locale is UTF-8, we can translate
* a UTF-8 string into a UTF-16BE string.
*/
if (sc->flag & SCONV_FROM_UTF8) {
add_converter(sc, archive_string_append_unicode);
return;
}
#if defined(_WIN32) && !defined(__CYGWIN__)
if (sc->flag & SCONV_WIN_CP) {
if (sc->flag & SCONV_TO_UTF16BE)
add_converter(sc, win_strncat_to_utf16be);
else
add_converter(sc, win_strncat_to_utf16le);
return;
}
#endif
#if defined(HAVE_ICONV)
if (sc->cd != (iconv_t)-1) {
add_converter(sc, iconv_strncat_in_locale);
return;
}
#endif
if (sc->flag & SCONV_BEST_EFFORT) {
if (sc->flag & SCONV_TO_UTF16BE)
add_converter(sc,
best_effort_strncat_to_utf16be);
else
add_converter(sc,
best_effort_strncat_to_utf16le);
} else
/* Make sure we have no converter. */
sc->nconverter = 0;
return;
}
/*
* Convert a string from UTF-16BE/LE.
*/
if (sc->flag & SCONV_FROM_UTF16) {
/*
* At least we should normalize a UTF-16BE string.
*/
if (sc->flag & SCONV_NORMALIZATION_D)
add_converter(sc,archive_string_normalize_D);
else if (sc->flag & SCONV_NORMALIZATION_C)
add_converter(sc, archive_string_normalize_C);
if (sc->flag & SCONV_TO_UTF8) {
/*
* If the current locale is UTF-8, we can translate
* a UTF-16BE/LE string into a UTF-8 string directly.
*/
if (!(sc->flag &
(SCONV_NORMALIZATION_D |SCONV_NORMALIZATION_C)))
add_converter(sc,
archive_string_append_unicode);
return;
}
#if defined(_WIN32) && !defined(__CYGWIN__)
if (sc->flag & SCONV_WIN_CP) {
if (sc->flag & SCONV_FROM_UTF16BE)
add_converter(sc, win_strncat_from_utf16be);
else
add_converter(sc, win_strncat_from_utf16le);
return;
}
#endif
#if defined(HAVE_ICONV)