diff options
Diffstat (limited to 'xdelta3/xdelta3.c')
-rw-r--r-- | xdelta3/xdelta3.c | 354 |
1 files changed, 81 insertions, 273 deletions
diff --git a/xdelta3/xdelta3.c b/xdelta3/xdelta3.c index cf2d224..0f739f2 100644 --- a/xdelta3/xdelta3.c +++ b/xdelta3/xdelta3.c | |||
@@ -455,55 +455,6 @@ XD3_MAKELIST(xd3_rlist, xd3_rinst, link); | |||
455 | #define IF_BUILD_DEFAULT(x) | 455 | #define IF_BUILD_DEFAULT(x) |
456 | #endif | 456 | #endif |
457 | 457 | ||
458 | IF_BUILD_SOFT(static const xd3_smatcher __smatcher_soft;) | ||
459 | IF_BUILD_FAST(static const xd3_smatcher __smatcher_fast;) | ||
460 | IF_BUILD_SLOW(static const xd3_smatcher __smatcher_slow;) | ||
461 | IF_BUILD_FASTER(static const xd3_smatcher __smatcher_faster;) | ||
462 | IF_BUILD_FASTEST(static const xd3_smatcher __smatcher_fastest;) | ||
463 | IF_BUILD_DEFAULT(static const xd3_smatcher __smatcher_default;) | ||
464 | |||
465 | #if XD3_DEBUG | ||
466 | #define SMALL_HASH_DEBUG1(s,inp) \ | ||
467 | usize_t debug_hval = xd3_checksum_hash (& (s)->small_hash, \ | ||
468 | xd3_scksum ((inp), (s)->smatcher.small_look)) | ||
469 | #define SMALL_HASH_DEBUG2(s,inp) \ | ||
470 | XD3_ASSERT (debug_hval == xd3_checksum_hash (& (s)->small_hash, \ | ||
471 | xd3_scksum ((inp), (s)->smatcher.small_look))) | ||
472 | #else | ||
473 | #define SMALL_HASH_DEBUG1(s,inp) | ||
474 | #define SMALL_HASH_DEBUG2(s,inp) | ||
475 | #endif /* XD3_DEBUG */ | ||
476 | |||
477 | /* Update the run-length state */ | ||
478 | #define NEXTRUN(c) do { if ((c) == run_c) { run_l += 1; } \ | ||
479 | else { run_c = (c); run_l = 1; } } while (0) | ||
480 | |||
481 | /* Update the checksum state. */ | ||
482 | #define OLD_LARGE_CKSUM 1 | ||
483 | #if OLD_LARGE_CKSUM | ||
484 | #define LARGE_CKSUM_UPDATE(cksum,base,look) \ | ||
485 | do { \ | ||
486 | uint32_t old_c = PERMUTE((base)[0]); \ | ||
487 | uint32_t new_c = PERMUTE((base)[(look)]); \ | ||
488 | uint32_t low = (((cksum) & 0xffff) - old_c + new_c) & 0xffff; \ | ||
489 | uint32_t high = (((cksum) >> 16) - (old_c * (look)) + low) & 0xffff; \ | ||
490 | (cksum) = (high << 16) | low; \ | ||
491 | } while (0) | ||
492 | #else | ||
493 | #define LARGE_CKSUM_UPDATE(cksum,base,look) \ | ||
494 | do { \ | ||
495 | // linear congruential generators of different | ||
496 | // sizes and good lattice structure | ||
497 | } while (1) | ||
498 | #endif | ||
499 | |||
500 | /* Multiply and add hash function */ | ||
501 | #if ARITH_SMALL_CKSUM | ||
502 | #define SMALL_CKSUM_UPDATE(cksum,base,look) (cksum) = ((*(unsigned long*)(base+1)) * 71143) | ||
503 | #else | ||
504 | #define SMALL_CKSUM_UPDATE LARGE_CKSUM_UPDATE | ||
505 | #endif | ||
506 | |||
507 | /* Consume N bytes of input, only used by the decoder. */ | 458 | /* Consume N bytes of input, only used by the decoder. */ |
508 | #define DECODE_INPUT(n) \ | 459 | #define DECODE_INPUT(n) \ |
509 | do { \ | 460 | do { \ |
@@ -571,6 +522,37 @@ static int xd3_srcwin_setup (xd3_stream *stream); | |||
571 | static usize_t xd3_iopt_last_matched (xd3_stream *stream); | 522 | static usize_t xd3_iopt_last_matched (xd3_stream *stream); |
572 | static int xd3_emit_uint32_t (xd3_stream *stream, xd3_output **output, uint32_t num); | 523 | static int xd3_emit_uint32_t (xd3_stream *stream, xd3_output **output, uint32_t num); |
573 | 524 | ||
525 | static usize_t xd3_smatch (xd3_stream *stream, | ||
526 | usize_t base, | ||
527 | usize_t scksum, | ||
528 | usize_t *match_offset); | ||
529 | static int xd3_string_match_init (xd3_stream *stream); | ||
530 | static usize_t xd3_scksum (const uint8_t *seg, const int ln); | ||
531 | static int xd3_comprun (const uint8_t *seg, int slook, uint8_t *run_cp); | ||
532 | static int xd3_srcwin_move_point (xd3_stream *stream, usize_t *next_move_point); | ||
533 | |||
534 | static int xd3_emit_run (xd3_stream *stream, usize_t pos, usize_t size, uint8_t run_c); | ||
535 | static usize_t xd3_checksum_hash (const xd3_hash_cfg *cfg, const usize_t cksum); | ||
536 | static xoff_t xd3_source_cksum_offset(xd3_stream *stream, usize_t low); | ||
537 | static void xd3_scksum_insert (xd3_stream *stream, | ||
538 | usize_t inx, | ||
539 | usize_t scksum, | ||
540 | usize_t pos); | ||
541 | |||
542 | |||
543 | #if XD3_DEBUG | ||
544 | static void xd3_verify_run_state (xd3_stream *stream, | ||
545 | const uint8_t *inp, | ||
546 | int x_run_l, | ||
547 | uint8_t x_run_c); | ||
548 | static void xd3_verify_large_state (xd3_stream *stream, | ||
549 | const uint8_t *inp, | ||
550 | uint32_t x_cksum); | ||
551 | static void xd3_verify_small_state (xd3_stream *stream, | ||
552 | const uint8_t *inp, | ||
553 | uint32_t x_cksum); | ||
554 | |||
555 | #endif /* XD3_DEBUG */ | ||
574 | #endif /* XD3_ENCODER */ | 556 | #endif /* XD3_ENCODER */ |
575 | 557 | ||
576 | static int xd3_decode_allocate (xd3_stream *stream, usize_t size, | 558 | static int xd3_decode_allocate (xd3_stream *stream, usize_t size, |
@@ -734,6 +716,13 @@ static const xd3_sec_type djw_sec_type; | |||
734 | 716 | ||
735 | /***********************************************************************/ | 717 | /***********************************************************************/ |
736 | 718 | ||
719 | #include "xdelta3-hash.h" | ||
720 | |||
721 | /* Process template passes - this includes xdelta3.c several times. */ | ||
722 | #define __XDELTA3_C_TEMPLATE_PASS__ | ||
723 | #include "xdelta3-cfgs.h" | ||
724 | #undef __XDELTA3_C_TEMPLATE_PASS__ | ||
725 | |||
737 | /* Process the inline pass. */ | 726 | /* Process the inline pass. */ |
738 | #define __XDELTA3_C_INLINE_PASS__ | 727 | #define __XDELTA3_C_INLINE_PASS__ |
739 | #include "xdelta3.c" | 728 | #include "xdelta3.c" |
@@ -774,11 +763,6 @@ static const xd3_sec_type djw_sec_type = | |||
774 | }; | 763 | }; |
775 | #endif | 764 | #endif |
776 | 765 | ||
777 | /* Process template passes - this includes xdelta3.c several times. */ | ||
778 | #define __XDELTA3_C_TEMPLATE_PASS__ | ||
779 | #include "xdelta3-cfgs.h" | ||
780 | #undef __XDELTA3_C_TEMPLATE_PASS__ | ||
781 | |||
782 | #if XD3_MAIN || PYTHON_MODULE || SWIG_MODULE || NOT_MAIN | 766 | #if XD3_MAIN || PYTHON_MODULE || SWIG_MODULE || NOT_MAIN |
783 | #include "xdelta3-main.h" | 767 | #include "xdelta3-main.h" |
784 | #endif | 768 | #endif |
@@ -859,13 +843,19 @@ struct _xd3_code_table_desc | |||
859 | uint8_t same_modes; /* Number of same copy modes (default 3) */ | 843 | uint8_t same_modes; /* Number of same copy modes (default 3) */ |
860 | uint8_t cpy_sizes; /* Number of immediate-size single copies (default 15) */ | 844 | uint8_t cpy_sizes; /* Number of immediate-size single copies (default 15) */ |
861 | 845 | ||
862 | uint8_t addcopy_add_max; /* Maximum add size for an add-copy double instruction, all modes (default 4) */ | 846 | uint8_t addcopy_add_max; /* Maximum add size for an add-copy double instruction, |
863 | uint8_t addcopy_near_cpy_max; /* Maximum cpy size for an add-copy double instruction, up through VCD_NEAR modes (default 6) */ | 847 | all modes (default 4) */ |
864 | uint8_t addcopy_same_cpy_max; /* Maximum cpy size for an add-copy double instruction, VCD_SAME modes (default 4) */ | 848 | uint8_t addcopy_near_cpy_max; /* Maximum cpy size for an add-copy double instruction, |
849 | up through VCD_NEAR modes (default 6) */ | ||
850 | uint8_t addcopy_same_cpy_max; /* Maximum cpy size for an add-copy double instruction, | ||
851 | VCD_SAME modes (default 4) */ | ||
865 | 852 | ||
866 | uint8_t copyadd_add_max; /* Maximum add size for a copy-add double instruction, all modes (default 1) */ | 853 | uint8_t copyadd_add_max; /* Maximum add size for a copy-add double instruction, |
867 | uint8_t copyadd_near_cpy_max; /* Maximum cpy size for a copy-add double instruction, up through VCD_NEAR modes (default 4) */ | 854 | all modes (default 1) */ |
868 | uint8_t copyadd_same_cpy_max; /* Maximum cpy size for a copy-add double instruction, VCD_SAME modes (default 4) */ | 855 | uint8_t copyadd_near_cpy_max; /* Maximum cpy size for a copy-add double instruction, |
856 | up through VCD_NEAR modes (default 4) */ | ||
857 | uint8_t copyadd_same_cpy_max; /* Maximum cpy size for a copy-add double instruction, | ||
858 | VCD_SAME modes (default 4) */ | ||
869 | 859 | ||
870 | xd3_code_table_sizes addcopy_max_sizes[MAX_MODES]; | 860 | xd3_code_table_sizes addcopy_max_sizes[MAX_MODES]; |
871 | xd3_code_table_sizes copyadd_max_sizes[MAX_MODES]; | 861 | xd3_code_table_sizes copyadd_max_sizes[MAX_MODES]; |
@@ -940,8 +930,8 @@ static const xd3_code_table_desc __alternate_code_table_desc = { | |||
940 | static void | 930 | static void |
941 | xd3_build_code_table (const xd3_code_table_desc *desc, xd3_dinst *tbl) | 931 | xd3_build_code_table (const xd3_code_table_desc *desc, xd3_dinst *tbl) |
942 | { | 932 | { |
943 | int size1, size2, mode; | 933 | usize_t size1, size2, mode; |
944 | int cpy_modes = 2 + desc->near_modes + desc->same_modes; | 934 | usize_t cpy_modes = 2 + desc->near_modes + desc->same_modes; |
945 | xd3_dinst *d = tbl; | 935 | xd3_dinst *d = tbl; |
946 | 936 | ||
947 | (d++)->type1 = XD3_RUN; | 937 | (d++)->type1 = XD3_RUN; |
@@ -968,7 +958,7 @@ xd3_build_code_table (const xd3_code_table_desc *desc, xd3_dinst *tbl) | |||
968 | { | 958 | { |
969 | for (size1 = 1; size1 <= desc->addcopy_add_max; size1 += 1) | 959 | for (size1 = 1; size1 <= desc->addcopy_add_max; size1 += 1) |
970 | { | 960 | { |
971 | int max = (mode < 2 + desc->near_modes) ? | 961 | usize_t max = (mode < 2U + desc->near_modes) ? |
972 | desc->addcopy_near_cpy_max : | 962 | desc->addcopy_near_cpy_max : |
973 | desc->addcopy_same_cpy_max; | 963 | desc->addcopy_same_cpy_max; |
974 | 964 | ||
@@ -984,7 +974,7 @@ xd3_build_code_table (const xd3_code_table_desc *desc, xd3_dinst *tbl) | |||
984 | 974 | ||
985 | for (mode = 0; mode < cpy_modes; mode += 1) | 975 | for (mode = 0; mode < cpy_modes; mode += 1) |
986 | { | 976 | { |
987 | int max = (mode < 2 + desc->near_modes) ? | 977 | usize_t max = (mode < 2U + desc->near_modes) ? |
988 | desc->copyadd_near_cpy_max : | 978 | desc->copyadd_near_cpy_max : |
989 | desc->copyadd_same_cpy_max; | 979 | desc->copyadd_same_cpy_max; |
990 | 980 | ||
@@ -1371,7 +1361,8 @@ xd3_apply_table_string (xd3_stream *stream, const uint8_t *code_string) | |||
1371 | int modes = TOTAL_MODES (stream); | 1361 | int modes = TOTAL_MODES (stream); |
1372 | xd3_dinst *code_table; | 1362 | xd3_dinst *code_table; |
1373 | 1363 | ||
1374 | if ((code_table = stream->code_table_alloc = xd3_alloc (stream, sizeof (xd3_dinst), 256)) == NULL) | 1364 | if ((code_table = stream->code_table_alloc = |
1365 | (xd3_dinst*) xd3_alloc (stream, sizeof (xd3_dinst), 256)) == NULL) | ||
1375 | { | 1366 | { |
1376 | return ENOMEM; | 1367 | return ENOMEM; |
1377 | } | 1368 | } |
@@ -1493,91 +1484,7 @@ xd3_apply_table_encoding (xd3_stream *in_stream, const uint8_t *data, usize_t si | |||
1493 | return ret; | 1484 | return ret; |
1494 | } | 1485 | } |
1495 | 1486 | ||
1496 | /*********************************************************************** | 1487 | /***********************************************************************/ |
1497 | Permute stuff | ||
1498 | ***********************************************************************/ | ||
1499 | |||
1500 | #if HASH_PERMUTE == 0 | ||
1501 | #define PERMUTE(x) (x) | ||
1502 | #else | ||
1503 | #define PERMUTE(x) (__single_hash[(uint)x]) | ||
1504 | |||
1505 | static const uint16_t __single_hash[256] = | ||
1506 | { | ||
1507 | /* Random numbers generated using SLIB's pseudo-random number generator. This hashes | ||
1508 | * the input alphabet. */ | ||
1509 | 0xbcd1, 0xbb65, 0x42c2, 0xdffe, 0x9666, 0x431b, 0x8504, 0xeb46, | ||
1510 | 0x6379, 0xd460, 0xcf14, 0x53cf, 0xdb51, 0xdb08, 0x12c8, 0xf602, | ||
1511 | 0xe766, 0x2394, 0x250d, 0xdcbb, 0xa678, 0x02af, 0xa5c6, 0x7ea6, | ||
1512 | 0xb645, 0xcb4d, 0xc44b, 0xe5dc, 0x9fe6, 0x5b5c, 0x35f5, 0x701a, | ||
1513 | 0x220f, 0x6c38, 0x1a56, 0x4ca3, 0xffc6, 0xb152, 0x8d61, 0x7a58, | ||
1514 | 0x9025, 0x8b3d, 0xbf0f, 0x95a3, 0xe5f4, 0xc127, 0x3bed, 0x320b, | ||
1515 | 0xb7f3, 0x6054, 0x333c, 0xd383, 0x8154, 0x5242, 0x4e0d, 0x0a94, | ||
1516 | 0x7028, 0x8689, 0x3a22, 0x0980, 0x1847, 0xb0f1, 0x9b5c, 0x4176, | ||
1517 | 0xb858, 0xd542, 0x1f6c, 0x2497, 0x6a5a, 0x9fa9, 0x8c5a, 0x7743, | ||
1518 | 0xa8a9, 0x9a02, 0x4918, 0x438c, 0xc388, 0x9e2b, 0x4cad, 0x01b6, | ||
1519 | 0xab19, 0xf777, 0x365f, 0x1eb2, 0x091e, 0x7bf8, 0x7a8e, 0x5227, | ||
1520 | 0xeab1, 0x2074, 0x4523, 0xe781, 0x01a3, 0x163d, 0x3b2e, 0x287d, | ||
1521 | 0x5e7f, 0xa063, 0xb134, 0x8fae, 0x5e8e, 0xb7b7, 0x4548, 0x1f5a, | ||
1522 | 0xfa56, 0x7a24, 0x900f, 0x42dc, 0xcc69, 0x02a0, 0x0b22, 0xdb31, | ||
1523 | 0x71fe, 0x0c7d, 0x1732, 0x1159, 0xcb09, 0xe1d2, 0x1351, 0x52e9, | ||
1524 | 0xf536, 0x5a4f, 0xc316, 0x6bf9, 0x8994, 0xb774, 0x5f3e, 0xf6d6, | ||
1525 | 0x3a61, 0xf82c, 0xcc22, 0x9d06, 0x299c, 0x09e5, 0x1eec, 0x514f, | ||
1526 | 0x8d53, 0xa650, 0x5c6e, 0xc577, 0x7958, 0x71ac, 0x8916, 0x9b4f, | ||
1527 | 0x2c09, 0x5211, 0xf6d8, 0xcaaa, 0xf7ef, 0x287f, 0x7a94, 0xab49, | ||
1528 | 0xfa2c, 0x7222, 0xe457, 0xd71a, 0x00c3, 0x1a76, 0xe98c, 0xc037, | ||
1529 | 0x8208, 0x5c2d, 0xdfda, 0xe5f5, 0x0b45, 0x15ce, 0x8a7e, 0xfcad, | ||
1530 | 0xaa2d, 0x4b5c, 0xd42e, 0xb251, 0x907e, 0x9a47, 0xc9a6, 0xd93f, | ||
1531 | 0x085e, 0x35ce, 0xa153, 0x7e7b, 0x9f0b, 0x25aa, 0x5d9f, 0xc04d, | ||
1532 | 0x8a0e, 0x2875, 0x4a1c, 0x295f, 0x1393, 0xf760, 0x9178, 0x0f5b, | ||
1533 | 0xfa7d, 0x83b4, 0x2082, 0x721d, 0x6462, 0x0368, 0x67e2, 0x8624, | ||
1534 | 0x194d, 0x22f6, 0x78fb, 0x6791, 0xb238, 0xb332, 0x7276, 0xf272, | ||
1535 | 0x47ec, 0x4504, 0xa961, 0x9fc8, 0x3fdc, 0xb413, 0x007a, 0x0806, | ||
1536 | 0x7458, 0x95c6, 0xccaa, 0x18d6, 0xe2ae, 0x1b06, 0xf3f6, 0x5050, | ||
1537 | 0xc8e8, 0xf4ac, 0xc04c, 0xf41c, 0x992f, 0xae44, 0x5f1b, 0x1113, | ||
1538 | 0x1738, 0xd9a8, 0x19ea, 0x2d33, 0x9698, 0x2fe9, 0x323f, 0xcde2, | ||
1539 | 0x6d71, 0xe37d, 0xb697, 0x2c4f, 0x4373, 0x9102, 0x075d, 0x8e25, | ||
1540 | 0x1672, 0xec28, 0x6acb, 0x86cc, 0x186e, 0x9414, 0xd674, 0xd1a5 | ||
1541 | }; | ||
1542 | #endif | ||
1543 | |||
1544 | /*********************************************************************** | ||
1545 | Ctable stuff | ||
1546 | ***********************************************************************/ | ||
1547 | |||
1548 | #if HASH_PRIME | ||
1549 | static const usize_t __primes[] = | ||
1550 | { | ||
1551 | 11, 19, 37, 73, 109, | ||
1552 | 163, 251, 367, 557, 823, | ||
1553 | 1237, 1861, 2777, 4177, 6247, | ||
1554 | 9371, 14057, 21089, 31627, 47431, | ||
1555 | 71143, 106721, 160073, 240101, 360163, | ||
1556 | 540217, 810343, 1215497, 1823231, 2734867, | ||
1557 | 4102283, 6153409, 9230113, 13845163, 20767711, | ||
1558 | 31151543, 46727321, 70090921, 105136301, 157704401, | ||
1559 | 236556601, 354834919, 532252367, 798378509, 1197567719, | ||
1560 | 1796351503 | ||
1561 | }; | ||
1562 | |||
1563 | static const usize_t __nprimes = SIZEOF_ARRAY (__primes); | ||
1564 | #endif | ||
1565 | |||
1566 | static inline uint32_t | ||
1567 | xd3_checksum_hash (const xd3_hash_cfg *cfg, const uint32_t cksum) | ||
1568 | { | ||
1569 | #if HASH_PRIME | ||
1570 | /* If the table is prime compute the modulus. */ | ||
1571 | return (cksum % cfg->size); | ||
1572 | #else | ||
1573 | /* If the table is power-of-two compute the mask.*/ | ||
1574 | return (cksum ^ (cksum >> cfg->shift)) & cfg->mask; | ||
1575 | #endif | ||
1576 | } | ||
1577 | |||
1578 | /*********************************************************************** | ||
1579 | Create the hash table. | ||
1580 | ***********************************************************************/ | ||
1581 | 1488 | ||
1582 | static inline void | 1489 | static inline void |
1583 | xd3_swap_uint8p (uint8_t** p1, uint8_t** p2) | 1490 | xd3_swap_uint8p (uint8_t** p1, uint8_t** p2) |
@@ -1621,11 +1528,9 @@ xd3_check_pow2 (usize_t value, usize_t *logof) | |||
1621 | static usize_t | 1528 | static usize_t |
1622 | xd3_pow2_roundup (usize_t x) | 1529 | xd3_pow2_roundup (usize_t x) |
1623 | { | 1530 | { |
1624 | int i = 1; | 1531 | usize_t i = 1; |
1625 | int s = 0; | ||
1626 | while (x > i) { | 1532 | while (x > i) { |
1627 | i <<= 1; | 1533 | i <<= 1; |
1628 | s++; | ||
1629 | } | 1534 | } |
1630 | return i; | 1535 | return i; |
1631 | } | 1536 | } |
@@ -1640,106 +1545,6 @@ xd3_round_blksize (usize_t sz, usize_t blksz) | |||
1640 | return mod ? (sz + (blksz - mod)) : sz; | 1545 | return mod ? (sz + (blksz - mod)) : sz; |
1641 | } | 1546 | } |
1642 | 1547 | ||
1643 | #if XD3_ENCODER | ||
1644 | #if !HASH_PRIME | ||
1645 | static usize_t | ||
1646 | xd3_size_log2 (usize_t slots) | ||
1647 | { | ||
1648 | int bits = 28; /* This should not be an unreasonable limit. */ | ||
1649 | int i; | ||
1650 | |||
1651 | for (i = 3; i <= bits; i += 1) | ||
1652 | { | ||
1653 | if (slots < (1 << i)) | ||
1654 | { | ||
1655 | bits = i-1; | ||
1656 | break; | ||
1657 | } | ||
1658 | } | ||
1659 | |||
1660 | return bits; | ||
1661 | } | ||
1662 | #endif | ||
1663 | |||
1664 | static void | ||
1665 | xd3_size_hashtable (xd3_stream *stream, | ||
1666 | usize_t slots, | ||
1667 | xd3_hash_cfg *cfg) | ||
1668 | { | ||
1669 | /* initialize ctable: the number of hash buckets is computed from the table of primes or | ||
1670 | * the nearest power-of-two, in both cases rounding down in favor of using less | ||
1671 | * memory. */ | ||
1672 | |||
1673 | #if HASH_PRIME | ||
1674 | usize_t i; | ||
1675 | |||
1676 | cfg->size = __primes[__nprimes-1]; | ||
1677 | |||
1678 | for (i = 1; i < __nprimes; i += 1) | ||
1679 | { | ||
1680 | if (slots < __primes[i]) | ||
1681 | { | ||
1682 | cfg->size = __primes[i-1]; | ||
1683 | break; | ||
1684 | } | ||
1685 | } | ||
1686 | #else | ||
1687 | int bits = xd3_size_log2 (slots); | ||
1688 | |||
1689 | cfg->size = (1 << bits); | ||
1690 | cfg->mask = (cfg->size - 1); | ||
1691 | cfg->shift = min (32 - bits, 16); | ||
1692 | #endif | ||
1693 | } | ||
1694 | #endif | ||
1695 | |||
1696 | /*********************************************************************** | ||
1697 | Cksum function | ||
1698 | ***********************************************************************/ | ||
1699 | |||
1700 | #if OLD_LARGE_CKSUM | ||
1701 | static inline uint32_t | ||
1702 | xd3_lcksum (const uint8_t *seg, const int ln) | ||
1703 | { | ||
1704 | int i = 0; | ||
1705 | uint32_t low = 0; | ||
1706 | uint32_t high = 0; | ||
1707 | |||
1708 | for (; i < ln; i += 1) | ||
1709 | { | ||
1710 | low += PERMUTE(*seg++); | ||
1711 | high += low; | ||
1712 | } | ||
1713 | |||
1714 | return ((high & 0xffff) << 16) | (low & 0xffff); | ||
1715 | } | ||
1716 | #else | ||
1717 | static inline int | ||
1718 | xd3_lcksum (const uint8_t *seg, const int ln) | ||
1719 | { | ||
1720 | int i; | ||
1721 | int x = 0; | ||
1722 | for (i = 0; i < ln; ++i) | ||
1723 | { | ||
1724 | x = (x * 103) + *seg++; | ||
1725 | } | ||
1726 | return x; | ||
1727 | } | ||
1728 | #endif | ||
1729 | |||
1730 | #if ARITH_SMALL_CKSUM | ||
1731 | static inline usize_t | ||
1732 | xd3_scksum (const uint8_t *seg, const int ln) | ||
1733 | { | ||
1734 | usize_t c; | ||
1735 | /* The -1 is because UPDATE operates on seg[1..ln] */ | ||
1736 | SMALL_CKSUM_UPDATE (c,(seg-1),ln); | ||
1737 | return c; | ||
1738 | } | ||
1739 | #else | ||
1740 | #define xd3_scksum(seg,ln) xd3_lcksum(seg,ln) | ||
1741 | #endif | ||
1742 | |||
1743 | /*********************************************************************** | 1548 | /*********************************************************************** |
1744 | Adler32 stream function: code copied from Zlib, defined in RFC1950 | 1549 | Adler32 stream function: code copied from Zlib, defined in RFC1950 |
1745 | ***********************************************************************/ | 1550 | ***********************************************************************/ |
@@ -1792,7 +1597,7 @@ static unsigned long adler32 (unsigned long adler, const uint8_t *buf, usize_t l | |||
1792 | Run-length function | 1597 | Run-length function |
1793 | ***********************************************************************/ | 1598 | ***********************************************************************/ |
1794 | 1599 | ||
1795 | static inline int | 1600 | static int |
1796 | xd3_comprun (const uint8_t *seg, int slook, uint8_t *run_cp) | 1601 | xd3_comprun (const uint8_t *seg, int slook, uint8_t *run_cp) |
1797 | { | 1602 | { |
1798 | int i; | 1603 | int i; |
@@ -2076,10 +1881,10 @@ static int | |||
2076 | xd3_alloc_cache (xd3_stream *stream) | 1881 | xd3_alloc_cache (xd3_stream *stream) |
2077 | { | 1882 | { |
2078 | if (((stream->acache.s_near > 0) && | 1883 | if (((stream->acache.s_near > 0) && |
2079 | (stream->acache.near_array = | 1884 | (stream->acache.near_array = (usize_t*) |
2080 | xd3_alloc (stream, stream->acache.s_near, sizeof (usize_t))) == NULL) || | 1885 | xd3_alloc (stream, stream->acache.s_near, sizeof (usize_t))) == NULL) || |
2081 | ((stream->acache.s_same > 0) && | 1886 | ((stream->acache.s_same > 0) && |
2082 | (stream->acache.same_array = | 1887 | (stream->acache.same_array = (usize_t*) |
2083 | xd3_alloc (stream, stream->acache.s_same * 256, sizeof (usize_t))) == NULL)) | 1888 | xd3_alloc (stream, stream->acache.s_same * 256, sizeof (usize_t))) == NULL)) |
2084 | { | 1889 | { |
2085 | return ENOMEM; | 1890 | return ENOMEM; |
@@ -2124,7 +1929,7 @@ static int | |||
2124 | xd3_encode_address (xd3_stream *stream, usize_t addr, usize_t here, uint8_t* mode) | 1929 | xd3_encode_address (xd3_stream *stream, usize_t addr, usize_t here, uint8_t* mode) |
2125 | { | 1930 | { |
2126 | usize_t d, bestd; | 1931 | usize_t d, bestd; |
2127 | int i, bestm, ret; | 1932 | usize_t i, bestm, ret; |
2128 | xd3_addr_cache* acache = & stream->acache; | 1933 | xd3_addr_cache* acache = & stream->acache; |
2129 | 1934 | ||
2130 | #define SMALLEST_INT(x) do { if (((x) & ~127) == 0) { goto good; } } while (0) | 1935 | #define SMALLEST_INT(x) do { if (((x) & ~127) == 0) { goto good; } } while (0) |
@@ -2300,12 +2105,12 @@ xd3_alloc_output (xd3_stream *stream, | |||
2300 | } | 2105 | } |
2301 | else | 2106 | else |
2302 | { | 2107 | { |
2303 | if ((output = xd3_alloc (stream, 1, sizeof (xd3_output))) == NULL) | 2108 | if ((output = (xd3_output*) xd3_alloc (stream, 1, sizeof (xd3_output))) == NULL) |
2304 | { | 2109 | { |
2305 | return NULL; | 2110 | return NULL; |
2306 | } | 2111 | } |
2307 | 2112 | ||
2308 | if ((base = xd3_alloc (stream, XD3_ALLOCSIZE, sizeof (uint8_t))) == NULL) | 2113 | if ((base = (uint8_t*) xd3_alloc (stream, XD3_ALLOCSIZE, sizeof (uint8_t))) == NULL) |
2309 | { | 2114 | { |
2310 | xd3_free (stream, output); | 2115 | xd3_free (stream, output); |
2311 | return NULL; | 2116 | return NULL; |
@@ -3395,7 +3200,7 @@ xd3_emit_double (xd3_stream *stream, xd3_rinst *first, xd3_rinst *second, uint c | |||
3395 | 3200 | ||
3396 | /* This enters a potential run instruction into the iopt buffer. The position argument is | 3201 | /* This enters a potential run instruction into the iopt buffer. The position argument is |
3397 | * relative to the target window. */ | 3202 | * relative to the target window. */ |
3398 | static inline int | 3203 | static int |
3399 | xd3_emit_run (xd3_stream *stream, usize_t pos, usize_t size, uint8_t run_c) | 3204 | xd3_emit_run (xd3_stream *stream, usize_t pos, usize_t size, uint8_t run_c) |
3400 | { | 3205 | { |
3401 | xd3_rinst* ri; | 3206 | xd3_rinst* ri; |
@@ -3604,7 +3409,8 @@ xd3_encode_buffer_leftover (xd3_stream *stream) | |||
3604 | usize_t room; | 3409 | usize_t room; |
3605 | 3410 | ||
3606 | /* Allocate the buffer. */ | 3411 | /* Allocate the buffer. */ |
3607 | if (stream->buf_in == NULL && (stream->buf_in = xd3_alloc (stream, stream->winsize, 1)) == NULL) | 3412 | if (stream->buf_in == NULL && |
3413 | (stream->buf_in = (uint8_t*) xd3_alloc (stream, stream->winsize, 1)) == NULL) | ||
3608 | { | 3414 | { |
3609 | return ENOMEM; | 3415 | return ENOMEM; |
3610 | } | 3416 | } |
@@ -3659,10 +3465,11 @@ static int | |||
3659 | xd3_alloc_iopt (xd3_stream *stream, int elts) | 3465 | xd3_alloc_iopt (xd3_stream *stream, int elts) |
3660 | { | 3466 | { |
3661 | int i; | 3467 | int i; |
3662 | xd3_iopt_buflist* last = xd3_alloc (stream, sizeof (xd3_iopt_buflist), 1); | 3468 | xd3_iopt_buflist* last = |
3469 | (xd3_iopt_buflist*) xd3_alloc (stream, sizeof (xd3_iopt_buflist), 1); | ||
3663 | 3470 | ||
3664 | if (last == NULL || | 3471 | if (last == NULL || |
3665 | (last->buffer = xd3_alloc (stream, sizeof (xd3_rinst), elts)) == NULL) | 3472 | (last->buffer = (xd3_rinst*) xd3_alloc (stream, sizeof (xd3_rinst), elts)) == NULL) |
3666 | { | 3473 | { |
3667 | return ENOMEM; | 3474 | return ENOMEM; |
3668 | } | 3475 | } |
@@ -4238,7 +4045,8 @@ xd3_string_match_init (xd3_stream *stream) | |||
4238 | 4045 | ||
4239 | if (DO_LARGE && stream->large_table == NULL) | 4046 | if (DO_LARGE && stream->large_table == NULL) |
4240 | { | 4047 | { |
4241 | if ((stream->large_table = xd3_alloc0 (stream, stream->large_hash.size, sizeof (usize_t))) == NULL) | 4048 | if ((stream->large_table = |
4049 | (usize_t*) xd3_alloc0 (stream, stream->large_hash.size, sizeof (usize_t))) == NULL) | ||
4242 | { | 4050 | { |
4243 | return ENOMEM; | 4051 | return ENOMEM; |
4244 | } | 4052 | } |
@@ -4262,9 +4070,9 @@ xd3_string_match_init (xd3_stream *stream) | |||
4262 | return 0; | 4070 | return 0; |
4263 | } | 4071 | } |
4264 | 4072 | ||
4265 | if ((stream->small_table = xd3_alloc0 (stream, | 4073 | if ((stream->small_table = (usize_t*) xd3_alloc0 (stream, |
4266 | stream->small_hash.size, | 4074 | stream->small_hash.size, |
4267 | sizeof (usize_t))) == NULL) | 4075 | sizeof (usize_t))) == NULL) |
4268 | { | 4076 | { |
4269 | return ENOMEM; | 4077 | return ENOMEM; |
4270 | } | 4078 | } |
@@ -4273,9 +4081,9 @@ xd3_string_match_init (xd3_stream *stream) | |||
4273 | if (stream->smatcher.small_lchain > 1 || | 4081 | if (stream->smatcher.small_lchain > 1 || |
4274 | stream->smatcher.small_chain > 1) | 4082 | stream->smatcher.small_chain > 1) |
4275 | { | 4083 | { |
4276 | if ((stream->small_prev = xd3_alloc (stream, | 4084 | if ((stream->small_prev = (xd3_slist*) xd3_alloc (stream, |
4277 | stream->sprevsz, | 4085 | stream->sprevsz, |
4278 | sizeof (xd3_slist))) == NULL) | 4086 | sizeof (xd3_slist))) == NULL) |
4279 | { | 4087 | { |
4280 | return ENOMEM; | 4088 | return ENOMEM; |
4281 | } | 4089 | } |
@@ -4530,7 +4338,7 @@ static inline usize_t | |||
4530 | xd3_forward_match(const uint8_t *s1c, | 4338 | xd3_forward_match(const uint8_t *s1c, |
4531 | const uint8_t *s2c, | 4339 | const uint8_t *s2c, |
4532 | usize_t n) { | 4340 | usize_t n) { |
4533 | int i = 0; | 4341 | usize_t i = 0; |
4534 | while (i < n && s1c[i] == s2c[i]) | 4342 | while (i < n && s1c[i] == s2c[i]) |
4535 | { | 4343 | { |
4536 | i++; | 4344 | i++; |
@@ -5020,7 +4828,7 @@ xd3_srcwin_move_point (xd3_stream *stream, usize_t *next_move_point) | |||
5020 | oldpos = blkrem; | 4828 | oldpos = blkrem; |
5021 | blkpos = xd3_bytes_on_srcblk_fast (stream->src, blkno); | 4829 | blkpos = xd3_bytes_on_srcblk_fast (stream->src, blkno); |
5022 | 4830 | ||
5023 | if (oldpos + stream->smatcher.large_look > blkpos) | 4831 | if (oldpos + stream->smatcher.large_look > (usize_t) blkpos) |
5024 | { | 4832 | { |
5025 | stream->srcwin_cksum_pos = (blkno + 1) * stream->src->blksize; | 4833 | stream->srcwin_cksum_pos = (blkno + 1) * stream->src->blksize; |
5026 | continue; | 4834 | continue; |
@@ -5130,7 +4938,7 @@ XD3_TEMPLATE(xd3_string_match_) (xd3_stream *stream) | |||
5130 | usize_t sinx; | 4938 | usize_t sinx; |
5131 | usize_t linx; | 4939 | usize_t linx; |
5132 | uint8_t run_c; | 4940 | uint8_t run_c; |
5133 | int run_l; | 4941 | size_t run_l; |
5134 | int ret; | 4942 | int ret; |
5135 | usize_t match_length; | 4943 | usize_t match_length; |
5136 | usize_t match_offset = 0; | 4944 | usize_t match_offset = 0; |