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 1001 1002 1003 1004 1005 1006 1007
|
// Copyright (C) 2004-2024 Artifex Software, Inc.
//
// This file is part of MuPDF.
//
// MuPDF is free software: you can redistribute it and/or modify it under the
// terms of the GNU Affero General Public License as published by the Free
// Software Foundation, either version 3 of the License, or (at your option)
// any later version.
//
// MuPDF 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 Affero General Public License for more
// details.
//
// You should have received a copy of the GNU Affero General Public License
// along with MuPDF. If not, see <https://www.gnu.org/licenses/agpl-3.0.en.html>
//
// Alternative licensing terms are available from the licensor.
// For commercial licensing, see <https://www.artifex.com/> or contact
// Artifex Software, Inc., 39 Mesa Street, Suite 108A, San Francisco,
// CA 94129, USA, for further information.
#include "mupdf/fitz.h"
#include <string.h>
#include <errno.h>
#include <math.h>
#include <float.h>
#include <stdlib.h>
#ifdef _WIN32
#include <windows.h> /* for MultiByteToWideChar etc. */
#endif
#include "utfdata.h"
static const int *
fz_ucd_bsearch(int c, const int *t, int n, int ne)
{
const int *p;
int m;
while (n > 1)
{
m = n/2;
p = t + m*ne;
if (c >= p[0])
{
t = p;
n = n - m;
}
else
{
n = m;
}
}
if (n && c >= t[0])
return t;
return 0;
}
int
fz_tolower(int c)
{
const int *p;
p = fz_ucd_bsearch(c, ucd_tolower2, nelem(ucd_tolower2) / 3, 3);
if (p && c >= p[0] && c <= p[1])
return c + p[2];
p = fz_ucd_bsearch(c, ucd_tolower1, nelem(ucd_tolower1) / 2, 2);
if (p && c == p[0])
return c + p[1];
return c;
}
int
fz_toupper(int c)
{
const int *p;
p = fz_ucd_bsearch(c, ucd_toupper2, nelem(ucd_toupper2) / 3, 3);
if (p && c >= p[0] && c <= p[1])
return c + p[2];
p = fz_ucd_bsearch(c, ucd_toupper1, nelem(ucd_toupper1) / 2, 2);
if (p && c == p[0])
return c + p[1];
return c;
}
size_t
fz_strnlen(const char *s, size_t n)
{
const char *p = memchr(s, 0, n);
return p ? (size_t) (p - s) : n;
}
int
fz_strncasecmp(const char *a, const char *b, size_t n)
{
if (!n--)
return 0;
for (; *a && *b && n && (*a == *b || fz_tolower(*a) == fz_tolower(*b)); a++, b++, n--)
;
return fz_tolower(*a) - fz_tolower(*b);
}
int
fz_strcasecmp(const char *a, const char *b)
{
while (fz_tolower(*a) == fz_tolower(*b))
{
if (*a++ == 0)
return 0;
b++;
}
return fz_tolower(*a) - fz_tolower(*b);
}
char *
fz_strsep(char **stringp, const char *delim)
{
char *ret = *stringp;
if (!ret) return NULL;
if ((*stringp = strpbrk(*stringp, delim)) != NULL)
*((*stringp)++) = '\0';
return ret;
}
size_t
fz_strlcpy(char *dst, const char *src, size_t siz)
{
register char *d = dst;
register const char *s = src;
register size_t n = siz;
/* Copy as many bytes as will fit */
if (n != 0 && --n != 0) {
do {
if ((*d++ = *s++) == 0)
break;
} while (--n != 0);
}
/* Not enough room in dst, add NUL and traverse rest of src */
if (n == 0) {
if (siz != 0)
*d = '\0'; /* NUL-terminate dst */
while (*s++)
;
}
return(s - src - 1); /* count does not include NUL */
}
size_t
fz_strlcat(char *dst, const char *src, size_t siz)
{
register char *d = dst;
register const char *s = src;
register size_t n = siz;
size_t dlen;
/* Find the end of dst and adjust bytes left but don't go past end */
while (*d != '\0' && n-- != 0)
d++;
dlen = d - dst;
n = siz - dlen;
if (n == 0)
return dlen + strlen(s);
while (*s != '\0') {
if (n != 1) {
*d++ = *s;
n--;
}
s++;
}
*d = '\0';
return dlen + (s - src); /* count does not include NUL */
}
void
fz_dirname(char *dir, const char *path, size_t n)
{
size_t i;
if (!path || !path[0])
{
fz_strlcpy(dir, ".", n);
return;
}
fz_strlcpy(dir, path, n);
i = strlen(dir);
for(; dir[i] == '/'; --i) if (!i) { fz_strlcpy(dir, "/", n); return; }
for(; dir[i] != '/'; --i) if (!i) { fz_strlcpy(dir, ".", n); return; }
for(; dir[i] == '/'; --i) if (!i) { fz_strlcpy(dir, "/", n); return; }
dir[i+1] = 0;
}
const char *
fz_basename(const char *path)
{
const char *name = strrchr(path, '/');
if (!name)
name = strrchr(path, '\\');
if (!name)
return path;
return name + 1;
}
#ifdef _WIN32
char *fz_realpath(const char *path, char *buf)
{
wchar_t wpath[PATH_MAX];
wchar_t wbuf[PATH_MAX];
int i;
if (!MultiByteToWideChar(CP_UTF8, 0, path, -1, wpath, PATH_MAX))
return NULL;
if (!GetFullPathNameW(wpath, PATH_MAX, wbuf, NULL))
return NULL;
if (!WideCharToMultiByte(CP_UTF8, 0, wbuf, -1, buf, PATH_MAX, NULL, NULL))
return NULL;
for (i=0; buf[i]; ++i)
if (buf[i] == '\\')
buf[i] = '/';
return buf;
}
#else
char *fz_realpath(const char *path, char *buf)
{
return realpath(path, buf);
}
#endif
static inline int ishex(int a)
{
return (a >= 'A' && a <= 'F') ||
(a >= 'a' && a <= 'f') ||
(a >= '0' && a <= '9');
}
static inline int tohex(int c)
{
if (c >= '0' && c <= '9') return c - '0';
if (c >= 'a' && c <= 'f') return c - 'a' + 0xA;
if (c >= 'A' && c <= 'F') return c - 'A' + 0xA;
return 0;
}
#define URIRESERVED ";/?:@&=+$,"
#define URIALPHA "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"
#define URIDIGIT "0123456789"
#define URIMARK "-_.!~*'()"
#define URIUNESCAPED URIALPHA URIDIGIT URIMARK
#define HEX "0123456789ABCDEF"
/* Same as fz_decode_uri_component but in-place */
char *
fz_urldecode(char *url)
{
char *s = url;
char *p = url;
while (*s)
{
int c = (unsigned char) *s++;
if (c == '%' && ishex(s[0]) && ishex(s[1]))
{
int a = tohex(*s++);
int b = tohex(*s++);
*p++ = a << 4 | b;
}
else
{
*p++ = c;
}
}
*p = 0;
return url;
}
char *
fz_decode_uri_component(fz_context *ctx, const char *s)
{
char *uri = fz_malloc(ctx, strlen(s) + 1);
char *p = uri;
while (*s)
{
int c = (unsigned char) *s++;
if (c == '%' && ishex(s[0]) && ishex(s[1]))
{
int a = tohex(*s++);
int b = tohex(*s++);
*p++ = a << 4 | b;
}
else
{
*p++ = c;
}
}
*p = 0;
return uri;
}
char *
fz_decode_uri(fz_context *ctx, const char *s)
{
char *uri = fz_malloc(ctx, strlen(s) + 1);
char *p = uri;
while (*s)
{
int c = (unsigned char) *s++;
if (c == '%' && ishex(s[0]) && ishex(s[1]))
{
int a = tohex(*s++);
int b = tohex(*s++);
int c = a << 4 | b;
if (strchr(URIRESERVED "#", c)) {
*p++ = '%';
*p++ = HEX[a];
*p++ = HEX[b];
} else {
*p++ = c;
}
}
else
{
*p++ = c;
}
}
*p = 0;
return uri;
}
static char *
fz_encode_uri_imp(fz_context *ctx, const char *s, const char *unescaped)
{
char *uri = fz_malloc(ctx, strlen(s) * 3 + 1); /* allocate enough for worst case */
char *p = uri;
while (*s)
{
int c = (unsigned char) *s++;
if (strchr(unescaped, c))
{
*p++ = c;
}
else
{
*p++ = '%';
*p++ = HEX[(c >> 4) & 15];
*p++ = HEX[(c) & 15];
}
}
*p = 0;
return uri;
}
char *
fz_encode_uri_component(fz_context *ctx, const char *s)
{
return fz_encode_uri_imp(ctx, s, URIUNESCAPED);
}
char *
fz_encode_uri_pathname(fz_context *ctx, const char *s)
{
return fz_encode_uri_imp(ctx, s, URIUNESCAPED "/");
}
char *
fz_encode_uri(fz_context *ctx, const char *s)
{
return fz_encode_uri_imp(ctx, s, URIUNESCAPED URIRESERVED "#");
}
void
fz_format_output_path(fz_context *ctx, char *path, size_t size, const char *fmt, int page)
{
const char *s, *p;
char num[40];
int i, n;
int z = 0;
for (i = 0; page; page /= 10)
num[i++] = '0' + page % 10;
num[i] = 0;
s = p = strchr(fmt, '%');
if (p)
{
++p;
while (*p >= '0' && *p <= '9')
z = z * 10 + (*p++ - '0');
}
if (p && *p == 'd')
{
++p;
}
else
{
s = p = strrchr(fmt, '.');
if (!p)
s = p = fmt + strlen(fmt);
}
if (z < 1)
z = 1;
while (i < z && i < (int)sizeof num)
num[i++] = '0';
n = s - fmt;
if (n + i + strlen(p) >= size)
fz_throw(ctx, FZ_ERROR_ARGUMENT, "path name buffer overflow");
memcpy(path, fmt, n);
while (i > 0)
path[n++] = num[--i];
fz_strlcpy(path + n, p, size - n);
}
#define SEP(x) ((x)=='/' || (x) == 0)
char *
fz_cleanname(char *name)
{
char *p, *q, *dotdot;
int rooted;
rooted = name[0] == '/';
/*
* invariants:
* p points at beginning of path element we're considering.
* q points just past the last path element we wrote (no slash).
* dotdot points just past the point where .. cannot backtrack
* any further (no slash).
*/
p = q = dotdot = name + rooted;
while (*p)
{
if(p[0] == '/') /* null element */
p++;
else if (p[0] == '.' && SEP(p[1]))
p += 1; /* don't count the separator in case it is nul */
else if (p[0] == '.' && p[1] == '.' && SEP(p[2]))
{
p += 2;
if (q > dotdot) /* can backtrack */
{
while(--q > dotdot && *q != '/')
;
}
else if (!rooted) /* /.. is / but ./../ is .. */
{
if (q != name)
*q++ = '/';
*q++ = '.';
*q++ = '.';
dotdot = q;
}
}
else /* real path element */
{
if (q != name+rooted)
*q++ = '/';
while ((*q = *p) != '/' && *q != 0)
p++, q++;
}
}
if (q == name) /* empty string is really "." */
*q++ = '.';
*q = '\0';
return name;
}
char *
fz_cleanname_strdup(fz_context *ctx, const char *name)
{
size_t len = strlen(name);
char *newname = fz_malloc(ctx, fz_maxz(2, len + 1));
memcpy(newname, name, len + 1);
newname[len] = '\0';
return fz_cleanname(newname);
}
enum
{
UTFmax = 4, /* maximum bytes per rune */
Runesync = 0x80, /* cannot represent part of a UTF sequence (<) */
Runeself = 0x80, /* rune and UTF sequences are the same (<) */
Runeerror = 0xFFFD, /* decoding error in UTF */
Runemax = 0x10FFFF, /* maximum rune value */
};
enum
{
Bit1 = 7,
Bitx = 6,
Bit2 = 5,
Bit3 = 4,
Bit4 = 3,
Bit5 = 2,
T1 = ((1<<(Bit1+1))-1) ^ 0xFF, /* 0000 0000 */
Tx = ((1<<(Bitx+1))-1) ^ 0xFF, /* 1000 0000 */
T2 = ((1<<(Bit2+1))-1) ^ 0xFF, /* 1100 0000 */
T3 = ((1<<(Bit3+1))-1) ^ 0xFF, /* 1110 0000 */
T4 = ((1<<(Bit4+1))-1) ^ 0xFF, /* 1111 0000 */
T5 = ((1<<(Bit5+1))-1) ^ 0xFF, /* 1111 1000 */
Rune1 = (1<<(Bit1+0*Bitx))-1, /* 0000 0000 0111 1111 */
Rune2 = (1<<(Bit2+1*Bitx))-1, /* 0000 0111 1111 1111 */
Rune3 = (1<<(Bit3+2*Bitx))-1, /* 1111 1111 1111 1111 */
Rune4 = (1<<(Bit4+3*Bitx))-1, /* 0001 1111 1111 1111 1111 1111 */
Maskx = (1<<Bitx)-1, /* 0011 1111 */
Testx = Maskx ^ 0xFF, /* 1100 0000 */
Bad = Runeerror,
};
int
fz_chartorune(int *rune, const char *str)
{
int c, c1, c2, c3;
int l;
/*
* one character sequence
* 00000-0007F => T1
*/
c = *(const unsigned char*)str;
if(c < Tx) {
*rune = c;
return 1;
}
/*
* two character sequence
* 0080-07FF => T2 Tx
*/
c1 = *(const unsigned char*)(str+1) ^ Tx;
if(c1 & Testx)
goto bad;
if(c < T3) {
if(c < T2)
goto bad;
l = ((c << Bitx) | c1) & Rune2;
if(l <= Rune1)
goto bad;
*rune = l;
return 2;
}
/*
* three character sequence
* 0800-FFFF => T3 Tx Tx
*/
c2 = *(const unsigned char*)(str+2) ^ Tx;
if(c2 & Testx)
goto bad;
if(c < T4) {
l = ((((c << Bitx) | c1) << Bitx) | c2) & Rune3;
if(l <= Rune2)
goto bad;
*rune = l;
return 3;
}
/*
* four character sequence (21-bit value)
* 10000-1FFFFF => T4 Tx Tx Tx
*/
c3 = *(const unsigned char*)(str+3) ^ Tx;
if (c3 & Testx)
goto bad;
if (c < T5) {
l = ((((((c << Bitx) | c1) << Bitx) | c2) << Bitx) | c3) & Rune4;
if (l <= Rune3)
goto bad;
*rune = l;
return 4;
}
/*
* Support for 5-byte or longer UTF-8 would go here, but
* since we don't have that, we'll just fall through to bad.
*/
/*
* bad decoding
*/
bad:
*rune = Bad;
return 1;
}
int
fz_runetochar(char *str, int rune)
{
/* Runes are signed, so convert to unsigned for range check. */
unsigned int c = (unsigned int)rune;
/*
* one character sequence
* 00000-0007F => 00-7F
*/
if(c <= Rune1) {
str[0] = c;
return 1;
}
/*
* two character sequence
* 0080-07FF => T2 Tx
*/
if(c <= Rune2) {
str[0] = T2 | (c >> 1*Bitx);
str[1] = Tx | (c & Maskx);
return 2;
}
/*
* If the Rune is out of range, convert it to the error rune.
* Do this test here because the error rune encodes to three bytes.
* Doing it earlier would duplicate work, since an out of range
* Rune wouldn't have fit in one or two bytes.
*/
if (c > Runemax)
c = Runeerror;
/*
* three character sequence
* 0800-FFFF => T3 Tx Tx
*/
if (c <= Rune3) {
str[0] = T3 | (c >> 2*Bitx);
str[1] = Tx | ((c >> 1*Bitx) & Maskx);
str[2] = Tx | (c & Maskx);
return 3;
}
/*
* four character sequence (21-bit value)
* 10000-1FFFFF => T4 Tx Tx Tx
*/
str[0] = T4 | (c >> 3*Bitx);
str[1] = Tx | ((c >> 2*Bitx) & Maskx);
str[2] = Tx | ((c >> 1*Bitx) & Maskx);
str[3] = Tx | (c & Maskx);
return 4;
}
int
fz_runelen(int c)
{
char str[10];
return fz_runetochar(str, c);
}
int
fz_runeidx(const char *s, const char *p)
{
int rune;
int i = 0;
while (s < p) {
if (*(unsigned char *)s < Runeself)
++s;
else
s += fz_chartorune(&rune, s);
++i;
}
return i;
}
const char *
fz_runeptr(const char *s, int i)
{
int rune;
while (i-- > 0) {
rune = *(unsigned char*)s;
if (rune < Runeself) {
if (rune == 0)
return NULL;
++s;
} else
s += fz_chartorune(&rune, s);
}
return s;
}
int
fz_utflen(const char *s)
{
int c, n, rune;
n = 0;
for(;;) {
c = *(const unsigned char*)s;
if(c < Runeself) {
if(c == 0)
return n;
s++;
} else
s += fz_chartorune(&rune, s);
n++;
}
return 0;
}
float fz_atof(const char *s)
{
float result;
if (s == NULL)
return 0;
errno = 0;
result = fz_strtof(s, NULL);
if ((errno == ERANGE && result == 0) || isnan(result))
/* Return 1.0 on underflow, as it's a small known value that won't cause a divide by 0. */
return 1;
result = fz_clamp(result, -FLT_MAX, FLT_MAX);
return result;
}
int fz_atoi(const char *s)
{
if (s == NULL)
return 0;
return atoi(s);
}
int64_t fz_atoi64(const char *s)
{
if (s == NULL)
return 0;
return atoll(s);
}
int fz_is_page_range(fz_context *ctx, const char *s)
{
/* TODO: check the actual syntax... */
while (*s)
{
if ((*s < '0' || *s > '9') && *s != 'N' && *s != '-' && *s != ',')
return 0;
s++;
}
return 1;
}
const char *fz_parse_page_range(fz_context *ctx, const char *s, int *a, int *b, int n)
{
const char *orig = s;
if (!s || !s[0])
return NULL;
if (s[0] == ',')
s += 1;
if (s[0] == 'N')
{
*a = n;
s += 1;
}
else
*a = strtol(s, (char**)&s, 10);
if (s[0] == '-')
{
if (s[1] == 'N')
{
*b = n;
s += 2;
}
else
*b = strtol(s+1, (char**)&s, 10);
}
else
*b = *a;
if (*a < 0) *a = n + 1 + *a;
if (*b < 0) *b = n + 1 + *b;
*a = fz_clampi(*a, 1, n);
*b = fz_clampi(*b, 1, n);
if (s == orig)
{
fz_warn(ctx, "skipping invalid page range");
return NULL;
}
return s;
}
/* memmem from musl */
#define MAX(a,b) ((a)>(b)?(a):(b))
#define BITOP(a,b,op) \
((a)[(size_t)(b)/(8*sizeof *(a))] op (size_t)1<<((size_t)(b)%(8*sizeof *(a))))
static char *twobyte_memmem(const unsigned char *h, size_t k, const unsigned char *n)
{
uint16_t nw = n[0]<<8 | n[1], hw = h[0]<<8 | h[1];
for (h++, k--; k; k--, hw = hw<<8 | *++h)
if (hw == nw) return (char *)h-1;
return 0;
}
static char *threebyte_memmem(const unsigned char *h, size_t k, const unsigned char *n)
{
uint32_t nw = n[0]<<24 | n[1]<<16 | n[2]<<8;
uint32_t hw = h[0]<<24 | h[1]<<16 | h[2]<<8;
for (h+=2, k-=2; k; k--, hw = (hw|*++h)<<8)
if (hw == nw) return (char *)h-2;
return 0;
}
static char *fourbyte_memmem(const unsigned char *h, size_t k, const unsigned char *n)
{
uint32_t nw = n[0]<<24 | n[1]<<16 | n[2]<<8 | n[3];
uint32_t hw = h[0]<<24 | h[1]<<16 | h[2]<<8 | h[3];
for (h+=3, k-=3; k; k--, hw = hw<<8 | *++h)
if (hw == nw) return (char *)h-3;
return 0;
}
static char *twoway_memmem(const unsigned char *h, const unsigned char *z, const unsigned char *n, size_t l)
{
size_t i, ip, jp, k, p, ms, p0, mem, mem0;
size_t byteset[32 / sizeof(size_t)] = { 0 };
size_t shift[256];
/* Computing length of needle and fill shift table */
for (i=0; i<l; i++)
BITOP(byteset, n[i], |=), shift[n[i]] = i+1;
/* Compute maximal suffix */
ip = -1; jp = 0; k = p = 1;
while (jp+k<l) {
if (n[ip+k] == n[jp+k]) {
if (k == p) {
jp += p;
k = 1;
} else k++;
} else if (n[ip+k] > n[jp+k]) {
jp += k;
k = 1;
p = jp - ip;
} else {
ip = jp++;
k = p = 1;
}
}
ms = ip;
p0 = p;
/* And with the opposite comparison */
ip = -1; jp = 0; k = p = 1;
while (jp+k<l) {
if (n[ip+k] == n[jp+k]) {
if (k == p) {
jp += p;
k = 1;
} else k++;
} else if (n[ip+k] < n[jp+k]) {
jp += k;
k = 1;
p = jp - ip;
} else {
ip = jp++;
k = p = 1;
}
}
if (ip+1 > ms+1) ms = ip;
else p = p0;
/* Periodic needle? */
if (memcmp(n, n+p, ms+1)) {
mem0 = 0;
p = MAX(ms, l-ms-1) + 1;
} else mem0 = l-p;
mem = 0;
/* Search loop */
for (;;) {
/* If remainder of haystack is shorter than needle, done */
if ((size_t)(z-h) < l) return 0;
/* Check last byte first; advance by shift on mismatch */
if (BITOP(byteset, h[l-1], &)) {
k = l-shift[h[l-1]];
if (k) {
if (mem0 && mem && k < p) k = l-p;
h += k;
mem = 0;
continue;
}
} else {
h += l;
mem = 0;
continue;
}
/* Compare right half */
for (k=MAX(ms+1,mem); k<l && n[k] == h[k]; k++);
if (k < l) {
h += k-ms;
mem = 0;
continue;
}
/* Compare left half */
for (k=ms+1; k>mem && n[k-1] == h[k-1]; k--);
if (k <= mem) return (char *)h;
h += p;
mem = mem0;
}
}
void *fz_memmem(const void *h0, size_t k, const void *n0, size_t l)
{
const unsigned char *h = h0, *n = n0;
/* Return immediately on empty needle */
if (!l) return (void *)h;
/* Return immediately when needle is longer than haystack */
if (k<l) return 0;
/* Use faster algorithms for short needles */
h = memchr(h0, *n, k);
if (!h || l==1) return (void *)h;
k -= h - (const unsigned char *)h0;
if (k<l) return 0;
if (l==2) return twobyte_memmem(h, k, n);
if (l==3) return threebyte_memmem(h, k, n);
if (l==4) return fourbyte_memmem(h, k, n);
return twoway_memmem(h, h+k, n, l);
}
char *
fz_utf8_from_wchar(fz_context *ctx, const wchar_t *s)
{
const wchar_t *src = s;
char *d;
char *dst;
int len = 1;
while (*src)
{
len += fz_runelen(*src++);
}
d = Memento_label(fz_malloc(ctx, len), "utf8_from_wchar");
dst = d;
src = s;
while (*src)
{
dst += fz_runetochar(dst, *src++);
}
*dst = 0;
return d;
}
wchar_t *
fz_wchar_from_utf8(fz_context *ctx, const char *path)
{
size_t z = 0;
const char *p = path;
wchar_t *wpath, *w;
if (!path)
return NULL;
while (*p)
{
int c;
p += fz_chartorune(&c, p);
z++;
if (c >= 0x10000)
z++;
}
w = wpath = fz_malloc(ctx, 2*(z+1));
while (*path)
{
int c;
path += fz_chartorune(&c, path);
if (c >= 0x10000)
{
c -= 0x10000;
*w++ = 0xd800 + (c>>10);
*w++ = 0xdc00 + (c&1023);
}
else
*w++ = c;
}
*w = 0;
return wpath;
}
|