Debian Codegraph @

src/fcstr.c

raw
1 /*
2 * fontconfig/src/fcstr.c
3 *
4 * Copyright © 2000 Keith Packard
5 *
6 * Permission to use, copy, modify, distribute, and sell this software and its
7 * documentation for any purpose is hereby granted without fee, provided that
8 * the above copyright notice appear in all copies and that both that
9 * copyright notice and this permission notice appear in supporting
10 * documentation, and that the name of the author(s) not be used in
11 * advertising or publicity pertaining to distribution of the software without
12 * specific, written prior permission. The authors make no
13 * representations about the suitability of this software for any purpose. It
14 * is provided "as is" without express or implied warranty.
15 *
16 * THE AUTHOR(S) DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
17 * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO
18 * EVENT SHALL THE AUTHOR(S) BE LIABLE FOR ANY SPECIAL, INDIRECT OR
19 * CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE,
20 * DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
21 * TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
22 * PERFORMANCE OF THIS SOFTWARE.
23 */
24
25 #include "fcint.h"
26
27 #include <ctype.h>
28 #include <stdlib.h>
29 #include <string.h>
30
31 /* Objects MT-safe for readonly access. */
32
33 FcChar8 *
34 FcStrCopy (const FcChar8 *s)
35 {
36 return FcStrdup (s);
37 }
38
39 static FcChar8 *
40 FcStrMakeTriple (const FcChar8 *s1, const FcChar8 *s2, const FcChar8 *s3)
41 {
42 int s1l = s1 ? strlen ((char *)s1) : 0;
43 int s2l = s2 ? strlen ((char *)s2) : 0;
44 int s3l = s3 ? strlen ((char *)s3) : 0;
45 int l = s1l + 1 + s2l + 1 + s3l + 1;
46 FcChar8 *s = malloc (l);
47
48 if (!s)
49 return 0;
50 if (s1)
51 memcpy (s, s1, s1l + 1);
52 else
53 s[0] = '\0';
54 if (s2)
55 memcpy (s + s1l + 1, s2, s2l + 1);
56 else
57 s[s1l + 1] = '\0';
58 if (s3)
59 memcpy (s + s1l + 1 + s2l + 1, s3, s3l + 1);
60 else
61 s[s1l + 1 + s2l + 1] = '\0';
62 return s;
63 }
64
65 FcChar8 *
66 FcStrPlus (const FcChar8 *s1, const FcChar8 *s2)
67 {
68 int s1l = strlen ((char *)s1);
69 int s2l = strlen ((char *)s2);
70 int l = s1l + s2l + 1;
71 FcChar8 *s = malloc (l);
72
73 if (!s)
74 return 0;
75 memcpy (s, s1, s1l);
76 memcpy (s + s1l, s2, s2l + 1);
77 return s;
78 }
79
80 void
81 FcStrFree (FcChar8 *s)
82 {
83 free (s);
84 }
85
86 #include "../fc-case/fccase.h"
87
88 #define FcCaseFoldUpperCount(cf) \
89 ((cf)->method == FC_CASE_FOLD_FULL ? 1 : (cf)->count)
90
91 typedef struct _FcCaseWalker {
92 const FcChar8 *read;
93 const FcChar8 *src;
94 FcChar8 utf8[FC_MAX_CASE_FOLD_CHARS + 1];
95 } FcCaseWalker;
96
97 static void
98 FcStrCaseWalkerInit (const FcChar8 *src, FcCaseWalker *w)
99 {
100 w->src = src;
101 w->read = 0;
102 }
103
104 static FcChar8
105 FcStrCaseWalkerLong (FcCaseWalker *w, FcChar8 r)
106 {
107 FcChar32 ucs4;
108 int slen;
109 int len = strlen ((char *)w->src);
110
111 slen = FcUtf8ToUcs4 (w->src - 1, &ucs4, len + 1);
112 if (slen <= 0)
113 return r;
114 if (FC_MIN_FOLD_CHAR <= ucs4 && ucs4 <= FC_MAX_FOLD_CHAR) {
115 int min = 0;
116 int max = FC_NUM_CASE_FOLD;
117
118 while (min <= max) {
119 int mid = (min + max) >> 1;
120 FcChar32 low = fcCaseFold[mid].upper;
121 FcChar32 high = low + FcCaseFoldUpperCount (&fcCaseFold[mid]);
122
123 if (high <= ucs4)
124 min = mid + 1;
125 else if (ucs4 < low)
126 max = mid - 1;
127 else {
128 const FcCaseFold *fold = &fcCaseFold[mid];
129 int dlen;
130
131 switch (fold->method) {
132 case FC_CASE_FOLD_EVEN_ODD:
133 if ((ucs4 & 1) != (fold->upper & 1))
134 return r;
135 /* fall through ... */
136 default:
137 dlen = FcUcs4ToUtf8 (ucs4 + fold->offset, w->utf8);
138 break;
139 case FC_CASE_FOLD_FULL:
140 dlen = fold->count;
141 memcpy (w->utf8, fcCaseFoldChars + fold->offset, dlen);
142 break;
143 }
144
145 /* consume rest of src utf-8 bytes */
146 w->src += slen - 1;
147
148 /* read from temp buffer */
149 w->utf8[dlen] = '\0';
150 w->read = w->utf8;
151 return *w->read++;
152 }
153 }
154 }
155 return r;
156 }
157
158 static FcChar8
159 FcStrCaseWalkerNextNonDelim (FcCaseWalker *w, const char *delims)
160 {
161 FcChar8 r;
162
163 if (FC_UNLIKELY (w->read != NULL)) {
164 if ((r = *w->read++))
165 return r;
166 w->read = 0;
167 }
168 do {
169 r = *w->src++;
170 } while (r != 0 && delims && strchr (delims, r));
171
172 if (FC_UNLIKELY ((r & 0xc0) == 0xc0))
173 return FcStrCaseWalkerLong (w, r);
174 if ('A' <= r && r <= 'Z')
175 r = r - 'A' + 'a';
176 return r;
177 }
178
179 static FcChar8
180 FcStrCaseWalkerNextNonBlank (FcCaseWalker *w)
181 {
182 FcChar8 r;
183
184 if (FC_UNLIKELY (w->read != NULL)) {
185 if ((r = *w->read++))
186 return r;
187 w->read = 0;
188 }
189 do {
190 r = *w->src++;
191 } while (r == ' ');
192
193 if (FC_UNLIKELY ((r & 0xc0) == 0xc0))
194 return FcStrCaseWalkerLong (w, r);
195 if ('A' <= r && r <= 'Z')
196 r = r - 'A' + 'a';
197 return r;
198 }
199
200 static FcChar8
201 FcStrCaseWalkerNext (FcCaseWalker *w)
202 {
203 FcChar8 r;
204
205 if (FC_UNLIKELY (w->read != NULL)) {
206 if ((r = *w->read++))
207 return r;
208 w->read = 0;
209 }
210
211 r = *w->src++;
212
213 if (FC_UNLIKELY ((r & 0xc0) == 0xc0))
214 return FcStrCaseWalkerLong (w, r);
215 if ('A' <= r && r <= 'Z')
216 r = r - 'A' + 'a';
217 return r;
218 }
219
220 FcChar8 *
221 FcStrDowncase (const FcChar8 *s)
222 {
223 FcCaseWalker w;
224 int len = 0;
225 FcChar8 *dst, *d;
226
227 FcStrCaseWalkerInit (s, &w);
228 while (FcStrCaseWalkerNext (&w))
229 len++;
230 d = dst = malloc (len + 1);
231 if (!d)
232 return 0;
233 FcStrCaseWalkerInit (s, &w);
234 while ((*d++ = FcStrCaseWalkerNext (&w)))
235 ;
236 return dst;
237 }
238
239 int
240 FcStrCmpIgnoreCase (const FcChar8 *s1, const FcChar8 *s2)
241 {
242 FcCaseWalker w1, w2;
243 FcChar8 c1, c2;
244
245 if (s1 == s2)
246 return 0;
247
248 FcStrCaseWalkerInit (s1, &w1);
249 FcStrCaseWalkerInit (s2, &w2);
250
251 for (;;) {
252 c1 = FcStrCaseWalkerNext (&w1);
253 c2 = FcStrCaseWalkerNext (&w2);
254 if (!c1 || (c1 != c2))
255 break;
256 }
257 return (int)c1 - (int)c2;
258 }
259
260 int
261 FcStrCmpIgnoreBlanksAndCase (const FcChar8 *s1, const FcChar8 *s2)
262 {
263 FcCaseWalker w1, w2;
264 FcChar8 c1, c2;
265
266 if (s1 == s2)
267 return 0;
268
269 FcStrCaseWalkerInit (s1, &w1);
270 FcStrCaseWalkerInit (s2, &w2);
271
272 for (;;) {
273 c1 = FcStrCaseWalkerNextNonBlank (&w1);
274 c2 = FcStrCaseWalkerNextNonBlank (&w2);
275 if (!c1 || (c1 != c2))
276 break;
277 }
278 return (int)c1 - (int)c2;
279 }
280
281 int
282 FcStrCmp (const FcChar8 *s1, const FcChar8 *s2)
283 {
284 FcChar8 c1, c2;
285
286 if (s1 == s2)
287 return 0;
288 for (;;) {
289 c1 = *s1++;
290 c2 = *s2++;
291 if (!c1 || c1 != c2)
292 break;
293 }
294 return (int)c1 - (int)c2;
295 }
296
297 /*
298 * Return a hash value for a string
299 */
300
301 FcChar32
302 FcStrHashIgnoreCase (const FcChar8 *s)
303 {
304 FcChar32 h = 0;
305 FcCaseWalker w;
306 FcChar8 c;
307
308 FcStrCaseWalkerInit (s, &w);
309 while ((c = FcStrCaseWalkerNext (&w)))
310 h = ((h << 3) ^ (h >> 3)) ^ c;
311 return h;
312 }
313
314 FcChar32
315 FcStrHashIgnoreBlanksAndCase (const FcChar8 *s)
316 {
317 FcChar32 h = 0;
318 FcCaseWalker w;
319 FcChar8 c;
320
321 FcStrCaseWalkerInit (s, &w);
322 while ((c = FcStrCaseWalkerNextNonBlank (&w)))
323 h = ((h << 3) ^ (h >> 3)) ^ c;
324 return h;
325 }
326
327 /*
328 * Is the head of s1 equal to s2?
329 */
330
331 static FcBool
332 FcStrIsAtIgnoreBlanksAndCase (const FcChar8 *s1, const FcChar8 *s2)
333 {
334 FcCaseWalker w1, w2;
335 FcChar8 c1, c2;
336
337 FcStrCaseWalkerInit (s1, &w1);
338 FcStrCaseWalkerInit (s2, &w2);
339
340 for (;;) {
341 c1 = FcStrCaseWalkerNextNonBlank (&w1);
342 c2 = FcStrCaseWalkerNextNonBlank (&w2);
343 if (!c1 || (c1 != c2))
344 break;
345 }
346 return c1 == c2 || !c2;
347 }
348
349 /*
350 * Does s1 contain an instance of s2 (ignoring blanks and case)?
351 */
352
353 const FcChar8 *
354 FcStrContainsIgnoreBlanksAndCase (const FcChar8 *s1, const FcChar8 *s2)
355 {
356 while (*s1) {
357 if (FcStrIsAtIgnoreBlanksAndCase (s1, s2))
358 return s1;
359 s1++;
360 }
361 return 0;
362 }
363
364 static FcBool
365 FcCharIsPunct (const FcChar8 c)
366 {
367 if (c < '0')
368 return FcTrue;
369 if (c <= '9')
370 return FcFalse;
371 if (c < 'A')
372 return FcTrue;
373 if (c <= 'Z')
374 return FcFalse;
375 if (c < 'a')
376 return FcTrue;
377 if (c <= 'z')
378 return FcFalse;
379 if (c <= '~')
380 return FcTrue;
381 return FcFalse;
382 }
383
384 /*
385 * Is the head of s1 equal to s2?
386 */
387
388 static FcBool
389 FcStrIsAtIgnoreCase (const FcChar8 *s1, const FcChar8 *s2)
390 {
391 FcCaseWalker w1, w2;
392 FcChar8 c1, c2;
393
394 FcStrCaseWalkerInit (s1, &w1);
395 FcStrCaseWalkerInit (s2, &w2);
396
397 for (;;) {
398 c1 = FcStrCaseWalkerNext (&w1);
399 c2 = FcStrCaseWalkerNext (&w2);
400 if (!c1 || (c1 != c2))
401 break;
402 }
403 return c1 == c2 || !c2;
404 }
405
406 /*
407 * Does s1 contain an instance of s2 (ignoring blanks and case)?
408 */
409
410 const FcChar8 *
411 FcStrContainsIgnoreCase (const FcChar8 *s1, const FcChar8 *s2)
412 {
413 while (*s1) {
414 if (FcStrIsAtIgnoreCase (s1, s2))
415 return s1;
416 s1++;
417 }
418 return 0;
419 }
420
421 /*
422 * Does s1 contain an instance of s2 on a word boundary (ignoring case)?
423 */
424
425 const FcChar8 *
426 FcStrContainsWord (const FcChar8 *s1, const FcChar8 *s2)
427 {
428 FcBool wordStart = FcTrue;
429 int s1len = strlen ((char *)s1);
430 int s2len = strlen ((char *)s2);
431
432 while (s1len >= s2len) {
433 if (wordStart &&
434 FcStrIsAtIgnoreCase (s1, s2) &&
435 (s1len == s2len || FcCharIsPunct (s1[s2len]))) {
436 return s1;
437 }
438 wordStart = FcFalse;
439 if (FcCharIsPunct (*s1))
440 wordStart = FcTrue;
441 s1++;
442 s1len--;
443 }
444 return 0;
445 }
446
447 /*
448 * returns the number of strings (ignoring delimiters and case) being matched
449 */
450
451 int
452 FcStrMatchIgnoreCaseAndDelims (const FcChar8 *s1, const FcChar8 *s2, const FcChar8 *delims)
453 {
454 FcCaseWalker w1, w2;
455 FcChar8 c1, c2;
456
457 if (s1 == s2)
458 return 0;
459
460 FcStrCaseWalkerInit (s1, &w1);
461 FcStrCaseWalkerInit (s2, &w2);
462
463 for (;;) {
464 c1 = FcStrCaseWalkerNextNonDelim (&w1, (const char *)delims);
465 c2 = FcStrCaseWalkerNextNonDelim (&w2, (const char *)delims);
466 if (!c1 || (c1 != c2))
467 break;
468 }
469 return w1.src - s1 - 1;
470 }
471
472 FcBool
473 FcStrGlobMatch (const FcChar8 *glob,
474 const FcChar8 *string)
475 {
476 FcChar8 c;
477
478 while ((c = *glob++)) {
479 switch (c) {
480 case '*':
481 /* short circuit common case */
482 if (!*glob)
483 return FcTrue;
484 /* short circuit another common case */
485 if (strchr ((char *)glob, '*') == 0) {
486 size_t l1, l2;
487
488 l1 = strlen ((char *)string);
489 l2 = strlen ((char *)glob);
490 if (l1 < l2)
491 return FcFalse;
492 string += (l1 - l2);
493 }
494 while (*string) {
495 if (FcStrGlobMatch (glob, string))
496 return FcTrue;
497 string++;
498 }
499 return FcFalse;
500 case '?':
501 if (*string++ == '\0')
502 return FcFalse;
503 break;
504 default:
505 if (*string++ != c)
506 return FcFalse;
507 break;
508 }
509 }
510 return *string == '\0';
511 }
512
513 const FcChar8 *
514 FcStrStrIgnoreCase (const FcChar8 *s1, const FcChar8 *s2)
515 {
516 FcCaseWalker w1, w2;
517 FcChar8 c1, c2;
518 const FcChar8 *cur;
519
520 if (!s1 || !s2)
521 return 0;
522
523 if (s1 == s2)
524 return s1;
525
526 FcStrCaseWalkerInit (s1, &w1);
527 FcStrCaseWalkerInit (s2, &w2);
528
529 c2 = FcStrCaseWalkerNext (&w2);
530
531 for (;;) {
532 cur = w1.src;
533 c1 = FcStrCaseWalkerNext (&w1);
534 if (!c1)
535 break;
536 if (c1 == c2) {
537 FcCaseWalker w1t = w1;
538 FcCaseWalker w2t = w2;
539 FcChar8 c1t, c2t;
540
541 for (;;) {
542 c1t = FcStrCaseWalkerNext (&w1t);
543 c2t = FcStrCaseWalkerNext (&w2t);
544
545 if (!c2t)
546 return cur;
547 if (c2t != c1t)
548 break;
549 }
550 }
551 }
552 return 0;
553 }
554
555 const FcChar8 *
556 FcStrStr (const FcChar8 *s1, const FcChar8 *s2)
557 {
558 FcChar8 c1, c2;
559 const FcChar8 *p = s1;
560 const FcChar8 *b = s2;
561
562 if (!s1 || !s2)
563 return 0;
564
565 if (s1 == s2)
566 return s1;
567
568 again:
569 c2 = *s2++;
570
571 if (!c2)
572 return 0;
573
574 for (;;) {
575 p = s1;
576 c1 = *s1++;
577 if (!c1 || c1 == c2)
578 break;
579 }
580
581 if (c1 != c2)
582 return 0;
583
584 for (;;) {
585 c1 = *s1;
586 c2 = *s2;
587 if (c1 && c2 && c1 != c2) {
588 s1 = p + 1;
589 s2 = b;
590 goto again;
591 }
592 if (!c2)
593 return p;
594 if (!c1)
595 return 0;
596 ++s1;
597 ++s2;
598 }
599 /* never reached. */
600 }
601
602 int
603 FcUtf8ToUcs4 (const FcChar8 *src_orig,
604 FcChar32 *dst,
605 int len)
606 {
607 const FcChar8 *src = src_orig;
608 FcChar8 s;
609 int extra;
610 FcChar32 result;
611
612 if (len == 0)
613 return 0;
614
615 s = *src++;
616 len--;
617
618 if (!(s & 0x80)) {
619 result = s;
620 extra = 0;
621 } else if (!(s & 0x40)) {
622 return -1;
623 } else if (!(s & 0x20)) {
624 result = s & 0x1f;
625 extra = 1;
626 } else if (!(s & 0x10)) {
627 result = s & 0xf;
628 extra = 2;
629 } else if (!(s & 0x08)) {
630 result = s & 0x07;
631 extra = 3;
632 } else if (!(s & 0x04)) {
633 result = s & 0x03;
634 extra = 4;
635 } else if (!(s & 0x02)) {
636 result = s & 0x01;
637 extra = 5;
638 } else {
639 return -1;
640 }
641 if (extra > len)
642 return -1;
643
644 while (extra--) {
645 result <<= 6;
646 s = *src++;
647
648 if ((s & 0xc0) != 0x80)
649 return -1;
650
651 result |= s & 0x3f;
652 }
653 *dst = result;
654 return src - src_orig;
655 }
656
657 FcBool
658 FcUtf8Len (const FcChar8 *string,
659 int len,
660 int *nchar,
661 int *wchar)
662 {
663 int n;
664 int clen;
665 FcChar32 c;
666 FcChar32 max;
667
668 n = 0;
669 max = 0;
670 while (len) {
671 clen = FcUtf8ToUcs4 (string, &c, len);
672 if (clen <= 0) /* malformed UTF8 string */
673 return FcFalse;
674 if (c > max)
675 max = c;
676 string += clen;
677 len -= clen;
678 n++;
679 }
680 *nchar = n;
681 if (max >= 0x10000)
682 *wchar = 4;
683 else if (max > 0x100)
684 *wchar = 2;
685 else
686 *wchar = 1;
687 return FcTrue;
688 }
689
690 int
691 FcUcs4ToUtf8 (FcChar32 ucs4,
692 FcChar8 dest[FC_UTF8_MAX_LEN])
693 {
694 int bits;
695 FcChar8 *d = dest;
696
697 if (ucs4 < 0x80) {
698 *d++ = ucs4;
699 bits = -6;
700 } else if (ucs4 < 0x800) {
701 *d++ = ((ucs4 >> 6) & 0x1F) | 0xC0;
702 bits = 0;
703 } else if (ucs4 < 0x10000) {
704 *d++ = ((ucs4 >> 12) & 0x0F) | 0xE0;
705 bits = 6;
706 } else if (ucs4 < 0x200000) {
707 *d++ = ((ucs4 >> 18) & 0x07) | 0xF0;
708 bits = 12;
709 } else if (ucs4 < 0x4000000) {
710 *d++ = ((ucs4 >> 24) & 0x03) | 0xF8;
711 bits = 18;
712 } else if (ucs4 < 0x80000000) {
713 *d++ = ((ucs4 >> 30) & 0x01) | 0xFC;
714 bits = 24;
715 } else
716 return 0;
717
718 for (; bits >= 0; bits -= 6) {
719 *d++ = ((ucs4 >> bits) & 0x3F) | 0x80;
720 }
721 return d - dest;
722 }
723
724 #define GetUtf16(src, endian) \
725 ((FcChar16)((src)[endian == FcEndianBig ? 0 : 1] << 8) | \
726 (FcChar16)((src)[endian == FcEndianBig ? 1 : 0]))
727
728 int
729 FcUtf16ToUcs4 (const FcChar8 *src_orig,
730 FcEndian endian,
731 FcChar32 *dst,
732 int len) /* in bytes */
733 {
734 const FcChar8 *src = src_orig;
735 FcChar16 a, b;
736 FcChar32 result;
737
738 if (len < 2)
739 return 0;
740
741 a = GetUtf16 (src, endian);
742 src += 2;
743 len -= 2;
744
745 /*
746 * Check for surrogate
747 */
748 if ((a & 0xfc00) == 0xd800) {
749 if (len < 2)
750 return 0;
751 b = GetUtf16 (src, endian);
752 src += 2;
753 len -= 2;
754 /*
755 * Check for invalid surrogate sequence
756 */
757 if ((b & 0xfc00) != 0xdc00)
758 return 0;
759 result = ((((FcChar32)a & 0x3ff) << 10) |
760 ((FcChar32)b & 0x3ff)) +
761 0x10000;
762 } else
763 result = a;
764 *dst = result;
765 return src - src_orig;
766 }
767
768 FcBool
769 FcUtf16Len (const FcChar8 *string,
770 FcEndian endian,
771 int len, /* in bytes */
772 int *nchar,
773 int *wchar)
774 {
775 int n;
776 int clen;
777 FcChar32 c;
778 FcChar32 max;
779
780 n = 0;
781 max = 0;
782 while (len) {
783 clen = FcUtf16ToUcs4 (string, endian, &c, len);
784 if (clen <= 0) /* malformed UTF8 string */
785 return FcFalse;
786 if (c > max)
787 max = c;
788 string += clen;
789 len -= clen;
790 n++;
791 }
792 *nchar = n;
793 if (max >= 0x10000)
794 *wchar = 4;
795 else if (max > 0x100)
796 *wchar = 2;
797 else
798 *wchar = 1;
799 return FcTrue;
800 }
801
802 void
803 FcStrBufInit (FcStrBuf *buf, FcChar8 *init, int size)
804 {
805 if (init) {
806 buf->buf = init;
807 buf->size = size;
808 } else {
809 buf->buf = buf->buf_static;
810 buf->size = sizeof (buf->buf_static);
811 }
812 buf->allocated = FcFalse;
813 buf->failed = FcFalse;
814 buf->len = 0;
815 }
816
817 void
818 FcStrBufDestroy (FcStrBuf *buf)
819 {
820 if (buf->allocated) {
821 free (buf->buf);
822 FcStrBufInit (buf, 0, 0);
823 }
824 }
825
826 FcChar8 *
827 FcStrBufDone (FcStrBuf *buf)
828 {
829 FcChar8 *ret;
830
831 if (buf->failed)
832 ret = NULL;
833 else
834 ret = malloc (buf->len + 1);
835 if (ret) {
836 memcpy (ret, buf->buf, buf->len);
837 ret[buf->len] = '\0';
838 }
839 FcStrBufDestroy (buf);
840 return ret;
841 }
842
843 FcChar8 *
844 FcStrBufDoneStatic (FcStrBuf *buf)
845 {
846 FcStrBufChar (buf, '\0');
847
848 if (buf->failed)
849 return NULL;
850
851 return buf->buf;
852 }
853
854 FcBool
855 FcStrBufChar (FcStrBuf *buf, FcChar8 c)
856 {
857 if (buf->len == buf->size) {
858 FcChar8 *newp;
859 int size;
860
861 if (buf->failed)
862 return FcFalse;
863
864 if (buf->allocated) {
865 size = buf->size * 2;
866 newp = realloc (buf->buf, size);
867 } else {
868 size = buf->size + 64;
869 newp = malloc (size);
870 if (newp) {
871 buf->allocated = FcTrue;
872 memcpy (newp, buf->buf, buf->len);
873 }
874 }
875 if (!newp) {
876 buf->failed = FcTrue;
877 return FcFalse;
878 }
879 buf->size = size;
880 buf->buf = newp;
881 }
882 buf->buf[buf->len++] = c;
883 return FcTrue;
884 }
885
886 FcBool
887 FcStrBufString (FcStrBuf *buf, const FcChar8 *s)
888 {
889 FcChar8 c;
890 while ((c = *s++))
891 if (!FcStrBufChar (buf, c))
892 return FcFalse;
893 return FcTrue;
894 }
895
896 FcBool
897 FcStrBufData (FcStrBuf *buf, const FcChar8 *s, int len)
898 {
899 while (len-- > 0)
900 if (!FcStrBufChar (buf, *s++))
901 return FcFalse;
902 return FcTrue;
903 }
904
905 FcBool
906 FcStrUsesHome (const FcChar8 *s)
907 {
908 return *s == '~';
909 }
910
911 FcBool
912 FcStrIsAbsoluteFilename (const FcChar8 *s)
913 {
914 #ifdef _WIN32
915 if (*s == '\\' ||
916 (isalpha (*s) && s[1] == ':' && (s[2] == '/' || s[2] == '\\')))
917 return FcTrue;
918 #endif
919 return *s == '/';
920 }
921
922 FcChar8 *
923 FcStrBuildFilename (const FcChar8 *path,
924 ...)
925 {
926 va_list ap;
927 FcStrSet *sset;
928 FcStrList *list;
929 FcChar8 *s, *ret = NULL, *p;
930 size_t len = 0;
931
932 if (!path)
933 return NULL;
934
935 sset = FcStrSetCreateEx (FCSS_ALLOW_DUPLICATES | FCSS_GROW_BY_64);
936 if (!sset)
937 return NULL;
938
939 if (!FcStrSetAdd (sset, path))
940 goto bail0;
941
942 va_start (ap, path);
943 while (1) {
944 s = (FcChar8 *)va_arg (ap, FcChar8 *);
945 if (!s)
946 break;
947 if (!FcStrSetAdd (sset, s))
948 goto bail1;
949 }
950 list = FcStrListCreate (sset);
951 while ((s = FcStrListNext (list))) {
952 len += strlen ((const char *)s) + 1;
953 }
954 list->n = 0;
955 ret = malloc (sizeof (FcChar8) * (len + 1));
956 if (!ret)
957 goto bail2;
958 p = ret;
959 while ((s = FcStrListNext (list))) {
960 if (p != ret) {
961 p[0] = FC_DIR_SEPARATOR;
962 p++;
963 }
964 len = strlen ((const char *)s);
965 memcpy (p, s, len);
966 p += len;
967 }
968 *p = 0;
969
970 bail2:
971 FcStrListDone (list);
972 bail1:
973 va_end (ap);
974 bail0:
975 FcStrSetDestroy (sset);
976
977 return ret;
978 }
979
980 FcChar8 *
981 FcStrCopyFilename (const FcChar8 *s)
982 {
983 FcChar8 *newp;
984
985 if (*s == '~') {
986 FcChar8 *home = FcConfigHome();
987 FcChar8 *full;
988 int size;
989 if (!home)
990 return NULL;
991 size = strlen ((char *)home) + strlen ((char *)s);
992 full = (FcChar8 *)malloc (size + 1);
993 if (!full)
994 return NULL;
995 strcpy ((char *)full, (char *)home);
996 strcat ((char *)full, (char *)s + 1);
997 newp = FcStrCanonFilename (full);
998 free (full);
999 } else
1000 newp = FcStrCanonFilename (s);
1001
1002 return newp;
1003 }
1004
1005 FcChar8 *
1006 FcStrLastSlash (const FcChar8 *path)
1007 {
1008 FcChar8 *slash;
1009
1010 slash = (FcChar8 *)strrchr ((const char *)path, '/');
1011 #ifdef _WIN32
1012 {
1013 FcChar8 *backslash;
1014
1015 backslash = (FcChar8 *)strrchr ((const char *)path, '\\');
1016 if (!slash || (backslash && backslash > slash))
1017 slash = backslash;
1018 }
1019 #endif
1020
1021 return slash;
1022 }
1023
1024 FcChar8 *
1025 FcStrDirname (const FcChar8 *file)
1026 {
1027 FcChar8 *slash;
1028 FcChar8 *dir;
1029
1030 slash = FcStrLastSlash (file);
1031 if (!slash)
1032 return FcStrCopy ((FcChar8 *)".");
1033 dir = malloc ((slash - file) + 1);
1034 if (!dir)
1035 return 0;
1036 strncpy ((char *)dir, (const char *)file, slash - file);
1037 dir[slash - file] = '\0';
1038 return dir;
1039 }
1040
1041 FcChar8 *
1042 FcStrBasename (const FcChar8 *file)
1043 {
1044 FcChar8 *slash;
1045
1046 slash = FcStrLastSlash (file);
1047 if (!slash)
1048 return FcStrCopy (file);
1049 return FcStrCopy (slash + 1);
1050 }
1051
1052 FcChar8 *
1053 FcStrRealPath (const FcChar8 *path)
1054 {
1055 char resolved_name[FC_PATH_MAX + 1];
1056 char *resolved_ret;
1057
1058 if (!path)
1059 return NULL;
1060
1061 #ifndef _WIN32
1062 resolved_ret = realpath ((const char *)path, resolved_name);
1063 #else
1064 if (GetFullPathNameA ((LPCSTR)path, FC_PATH_MAX, resolved_name, NULL) == 0) {
1065 fprintf (stderr, "Fontconfig warning: GetFullPathNameA failed.\n");
1066 return NULL;
1067 }
1068 resolved_ret = resolved_name;
1069 #endif
1070 if (resolved_ret)
1071 path = (FcChar8 *)resolved_ret;
1072 return FcStrCopyFilename (path);
1073 }
1074
1075 static FcChar8 *
1076 FcStrCanonAbsoluteFilename (const FcChar8 *s)
1077 {
1078 FcChar8 *file;
1079 FcChar8 *f;
1080 const FcChar8 *slash;
1081 int size;
1082
1083 size = strlen ((char *)s) + 1;
1084 file = malloc (size);
1085 if (!file)
1086 return NULL;
1087 slash = NULL;
1088 f = file;
1089 #ifdef _WIN32
1090 if (*s == '/' && *(s + 1) == '/') /* Network path, do not squash // */
1091 *f++ = *s++;
1092 #endif
1093 for (;;) {
1094 if (*s == '/' || *s == '\0') {
1095 if (slash) {
1096 switch (s - slash) {
1097 case 1:
1098 f -= 1; /* squash // and trim final / from file */
1099 break;
1100 case 2:
1101 if (!strncmp ((char *)slash, "/.", 2)) {
1102 f -= 2; /* trim /. from file */
1103 }
1104 break;
1105 case 3:
1106 if (!strncmp ((char *)slash, "/..", 3)) {
1107 f -= 3; /* trim /.. from file */
1108 while (f > file) {
1109 if (*--f == '/')
1110 break;
1111 }
1112 }
1113 break;
1114 }
1115 }
1116 slash = s;
1117 }
1118 if (!(*f++ = *s++))
1119 break;
1120 }
1121 return file;
1122 }
1123
1124 #ifdef _WIN32
1125 /*
1126 * Convert '\\' to '/' , remove double '/'
1127 */
1128 static void
1129 FcConvertDosPath (char *str)
1130 {
1131 size_t len = strlen (str);
1132 char *p = str;
1133 char *dest = str;
1134 char *end = str + len;
1135 char last = 0;
1136
1137 if (*p == '\\') {
1138 *p = '/';
1139 p++;
1140 dest++;
1141 }
1142 while (p < end) {
1143 if (*p == '\\')
1144 *p = '/';
1145
1146 if (*p != '/' || last != '/') {
1147 *dest++ = *p;
1148 }
1149
1150 last = *p;
1151 p++;
1152 }
1153
1154 *dest = 0;
1155 }
1156 #endif
1157
1158 FcChar8 *
1159 FcStrCanonFilename (const FcChar8 *s)
1160 {
1161 #ifdef _WIN32
1162 FcChar8 full[FC_MAX_FILE_LEN + 2];
1163 int size = GetFullPathName ((LPCSTR)s, sizeof (full) - 1,
1164 (LPSTR)full, NULL);
1165
1166 if (size == 0)
1167 perror ("GetFullPathName");
1168
1169 FcConvertDosPath ((char *)full);
1170 return FcStrCanonAbsoluteFilename (full);
1171 #else
1172 if (s[0] == '/')
1173 return FcStrCanonAbsoluteFilename (s);
1174 else {
1175 FcChar8 *full;
1176 FcChar8 *file;
1177
1178 FcChar8 cwd[FC_MAX_FILE_LEN + 2];
1179 if (getcwd ((char *)cwd, FC_MAX_FILE_LEN) == NULL)
1180 return NULL;
1181 full = FcStrBuildFilename (cwd, s, NULL);
1182 file = FcStrCanonAbsoluteFilename (full);
1183 FcStrFree (full);
1184 return file;
1185 }
1186 #endif
1187 }
1188
1189 FcStrSet *
1190 FcStrSetCreate (void)
1191 {
1192 return FcStrSetCreateEx (FCSS_DEFAULT);
1193 }
1194
1195 FcStrSet *
1196 FcStrSetCreateEx (unsigned int control)
1197 {
1198 FcStrSet *set = malloc (sizeof (FcStrSet));
1199 if (!set)
1200 return 0;
1201 FcRefInit (&set->ref, 1);
1202 set->num = 0;
1203 set->size = 0;
1204 set->strs = 0;
1205 set->control = control;
1206 return set;
1207 }
1208
1209 static FcBool
1210 _FcStrSetGrow (FcStrSet *set, int growElements)
1211 {
1212 /* accommodate an additional NULL entry at the end of the array */
1213 FcChar8 **strs = malloc ((set->size + growElements + 1) * sizeof (FcChar8 *));
1214 if (!strs)
1215 return FcFalse;
1216 if (set->num)
1217 memcpy (strs, set->strs, set->num * sizeof (FcChar8 *));
1218 if (set->strs)
1219 free (set->strs);
1220 set->size = set->size + growElements;
1221 set->strs = strs;
1222 return FcTrue;
1223 }
1224
1225 static FcBool
1226 _FcStrSetInsert (FcStrSet *set, FcChar8 *s, int pos)
1227 {
1228 if (!FcStrSetHasControlBit (set, FCSS_ALLOW_DUPLICATES)) {
1229 if (FcStrSetMember (set, s)) {
1230 FcStrFree (s);
1231 return FcTrue;
1232 }
1233 }
1234 if (set->num == set->size) {
1235 int growElements = FcStrSetHasControlBit (set, FCSS_GROW_BY_64) ? 64 : 1;
1236 if (!_FcStrSetGrow (set, growElements))
1237 return FcFalse;
1238 }
1239 if (pos >= set->num) {
1240 set->strs[set->num++] = s;
1241 set->strs[set->num] = 0;
1242 } else {
1243 int i;
1244
1245 set->num++;
1246 set->strs[set->num] = 0;
1247 for (i = set->num - 1; i > pos; i--)
1248 set->strs[i] = set->strs[i - 1];
1249 set->strs[pos] = s;
1250 }
1251 return FcTrue;
1252 }
1253
1254 FcBool
1255 FcStrSetMember (FcStrSet *set, const FcChar8 *s)
1256 {
1257 int i;
1258
1259 for (i = 0; i < set->num; i++)
1260 if (!FcStrCmp (set->strs[i], s))
1261 return FcTrue;
1262 return FcFalse;
1263 }
1264
1265 static int
1266 fc_strcmp_r (const FcChar8 *s1, const FcChar8 *s2, const FcChar8 **ret)
1267 {
1268 FcChar8 c1, c2;
1269
1270 if (s1 == s2) {
1271 if (ret)
1272 *ret = NULL;
1273 return 0;
1274 }
1275 for (;;) {
1276 if (s1)
1277 c1 = *s1++;
1278 else
1279 c1 = 0;
1280 if (s2)
1281 c2 = *s2++;
1282 else
1283 c2 = 0;
1284 if (!c1 || c1 != c2)
1285 break;
1286 }
1287 if (ret)
1288 *ret = s1;
1289 return (int)c1 - (int)c2;
1290 }
1291
1292 FcBool
1293 FcStrSetMemberAB (FcStrSet *set, const FcChar8 *a, FcChar8 *b, FcChar8 **ret)
1294 {
1295 int i;
1296 const FcChar8 *s = NULL;
1297
1298 for (i = 0; i < set->num; i++) {
1299 if (!fc_strcmp_r (set->strs[i], a, &s) && s) {
1300 if (!fc_strcmp_r (s, b, NULL)) {
1301 if (ret)
1302 *ret = set->strs[i];
1303 return FcTrue;
1304 }
1305 }
1306 }
1307 if (ret)
1308 *ret = NULL;
1309 return FcFalse;
1310 }
1311
1312 FcBool
1313 FcStrSetEqual (FcStrSet *sa, FcStrSet *sb)
1314 {
1315 int i;
1316 if (sa->num != sb->num)
1317 return FcFalse;
1318 for (i = 0; i < sa->num; i++)
1319 if (!FcStrSetMember (sb, sa->strs[i]))
1320 return FcFalse;
1321 return FcTrue;
1322 }
1323
1324 FcBool
1325 FcStrSetAdd (FcStrSet *set, const FcChar8 *s)
1326 {
1327 FcChar8 *newp = FcStrCopy (s);
1328 if (!newp)
1329 return FcFalse;
1330 if (!_FcStrSetInsert (set, newp, set->num)) {
1331 FcStrFree (newp);
1332 return FcFalse;
1333 }
1334 return FcTrue;
1335 }
1336
1337 FcBool
1338 FcStrSetInsert (FcStrSet *set, const FcChar8 *s, int pos)
1339 {
1340 FcChar8 *newp = FcStrCopy (s);
1341 if (!newp)
1342 return FcFalse;
1343 if (!_FcStrSetInsert (set, newp, pos)) {
1344 FcStrFree (newp);
1345 return FcFalse;
1346 }
1347 return FcTrue;
1348 }
1349
1350 FcBool
1351 FcStrSetAddTriple (FcStrSet *set, const FcChar8 *a, const FcChar8 *b, const FcChar8 *c)
1352 {
1353 FcChar8 *newp = FcStrMakeTriple (a, b, c);
1354 if (!newp)
1355 return FcFalse;
1356 if (!_FcStrSetInsert (set, newp, set->num)) {
1357 FcStrFree (newp);
1358 return FcFalse;
1359 }
1360 return FcTrue;
1361 }
1362
1363 const FcChar8 *
1364 FcStrTripleSecond (FcChar8 *str)
1365 {
1366 FcChar8 *second = str + strlen ((char *)str) + 1;
1367
1368 if (*second == '\0')
1369 return 0;
1370 return second;
1371 }
1372
1373 const FcChar8 *
1374 FcStrTripleThird (FcChar8 *str)
1375 {
1376 FcChar8 *second = str + strlen ((char *)str) + 1;
1377 FcChar8 *third = second + strlen ((char *)second) + 1;
1378
1379 if (*third == '\0')
1380 return 0;
1381 return third;
1382 }
1383
1384 FcBool
1385 FcStrSetAddFilename (FcStrSet *set, const FcChar8 *s)
1386 {
1387 FcChar8 *newp = FcStrCopyFilename (s);
1388 if (!newp)
1389 return FcFalse;
1390 if (!_FcStrSetInsert (set, newp, set->num)) {
1391 FcStrFree (newp);
1392 return FcFalse;
1393 }
1394 return FcTrue;
1395 }
1396
1397 FcBool
1398 FcStrSetAddFilenamePairWithSalt (FcStrSet *set, const FcChar8 *a, const FcChar8 *b, const FcChar8 *salt)
1399 {
1400 FcChar8 *new_a = NULL;
1401 FcChar8 *new_b = NULL;
1402 FcBool ret;
1403
1404 if (a) {
1405 new_a = FcStrCopyFilename (a);
1406 if (!new_a)
1407 return FcFalse;
1408 }
1409 if (b) {
1410 new_b = FcStrCopyFilename (b);
1411 if (!new_b) {
1412 if (new_a)
1413 FcStrFree (new_a);
1414 return FcFalse;
1415 }
1416 }
1417 /* Override maps with new one if exists */
1418 FcStrSetDel (set, new_a);
1419 ret = FcStrSetAddTriple (set, new_a, new_b, salt);
1420 if (new_a)
1421 FcStrFree (new_a);
1422 if (new_b)
1423 FcStrFree (new_b);
1424 return ret;
1425 }
1426
1427 FcBool
1428 FcStrSetAddLangs (FcStrSet *strs, const char *languages)
1429 {
1430 const char *p = languages, *next;
1431 FcChar8 lang[128] = { 0 }, *normalized_lang;
1432 size_t len;
1433 FcBool ret = FcFalse;
1434
1435 if (!languages)
1436 return FcFalse;
1437
1438 while ((next = strchr (p, ':'))) {
1439 len = next - p;
1440 len = FC_MIN (len, 127);
1441 strncpy ((char *)lang, p, len);
1442 lang[len] = 0;
1443 /* ignore an empty item */
1444 if (*lang) {
1445 normalized_lang = FcLangNormalize ((const FcChar8 *)lang);
1446 if (normalized_lang) {
1447 FcStrSetAdd (strs, normalized_lang);
1448 FcStrFree (normalized_lang);
1449 ret = FcTrue;
1450 }
1451 }
1452 p = next + 1;
1453 }
1454 if (*p) {
1455 normalized_lang = FcLangNormalize ((const FcChar8 *)p);
1456 if (normalized_lang) {
1457 FcStrSetAdd (strs, normalized_lang);
1458 FcStrFree (normalized_lang);
1459 ret = FcTrue;
1460 }
1461 }
1462
1463 return ret;
1464 }
1465
1466 FcBool
1467 FcStrSetDel (FcStrSet *set, const FcChar8 *s)
1468 {
1469 int i;
1470
1471 for (i = 0; i < set->num; i++)
1472 if (!FcStrCmp (set->strs[i], s)) {
1473 FcStrFree (set->strs[i]);
1474 /*
1475 * copy remaining string pointers and trailing
1476 * NULL
1477 */
1478 memmove (&set->strs[i], &set->strs[i + 1],
1479 (set->num - i) * sizeof (FcChar8 *));
1480 set->num--;
1481 return FcTrue;
1482 }
1483 return FcFalse;
1484 }
1485
1486 FcBool
1487 FcStrSetDeleteAll (FcStrSet *set)
1488 {
1489 int i;
1490
1491 if (FcRefIsConst (&set->ref))
1492 return FcFalse;
1493
1494 for (i = set->num; i > 0; i--) {
1495 FcStrFree (set->strs[i - 1]);
1496 set->num--;
1497 }
1498 return FcTrue;
1499 }
1500
1501 /* TODO Make public */
1502 static FcStrSet *
1503 FcStrSetReference (FcStrSet *set)
1504 {
1505 if (FcRefIsConst (&set->ref))
1506 return set;
1507
1508 FcRefInc (&set->ref);
1509 return set;
1510 }
1511
1512 void
1513 FcStrSetDestroy (FcStrSet *set)
1514 {
1515 if (set) {
1516 int i;
1517
1518 /* We rely on this in FcGetDefaultLangs for caching. */
1519 if (FcRefIsConst (&set->ref))
1520 return;
1521
1522 if (FcRefDec (&set->ref) != 1)
1523 return;
1524
1525 for (i = 0; i < set->num; i++)
1526 FcStrFree (set->strs[i]);
1527 if (set->strs)
1528 free (set->strs);
1529 free (set);
1530 }
1531 }
1532
1533 FcStrList *
1534 FcStrListCreate (FcStrSet *set)
1535 {
1536 FcStrList *list;
1537
1538 list = malloc (sizeof (FcStrList));
1539 if (!list)
1540 return 0;
1541 list->set = set;
1542 FcStrSetReference (set);
1543 list->n = 0;
1544 return list;
1545 }
1546
1547 void
1548 FcStrListFirst (FcStrList *list)
1549 {
1550 list->n = 0;
1551 }
1552
1553 FcChar8 *
1554 FcStrListNext (FcStrList *list)
1555 {
1556 if (list->n >= list->set->num)
1557 return 0;
1558 return list->set->strs[list->n++];
1559 }
1560
1561 void
1562 FcStrListDone (FcStrList *list)
1563 {
1564 FcStrSetDestroy (list->set);
1565 free (list);
1566 }
1567
1568 #define __fcstr__
1569 #include "fcaliastail.h"
1570 #undef __fcstr__