forked from mattiasgustavsson/libs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
libxdiff.h
4264 lines (3344 loc) · 108 KB
/
libxdiff.h
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
/*
* This is a modified version of the original LibXDiff. Basically, I merged
* it into a single-file header-only library, and added some casts to make
* the code compile in both C and C++. I place my changes under the same
* license as the original code.
* /Mattias Gustavsson
* Do this:
* #define LIBXDIFF_IMPLEMENTATION
* before including this file in *one* C/C++ file to get the implementation
*
****************************************************************************
*
* LibXDiff by Davide Libenzi ( File Differential Library )
* Copyright (C) 2003 Davide Libenzi
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library 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
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*
* Davide Libenzi <[email protected]>
*
*/
/* BEGIN xdiff.h */
#if !defined(XDIFF_H)
#define XDIFF_H
//#ifdef __cplusplus
//extern "C" {
//#endif /* #ifdef __cplusplus */
//
#define XDF_NEED_MINIMAL (1 << 1)
#define XDL_PATCH_NORMAL '-'
#define XDL_PATCH_REVERSE '+'
#define XDL_PATCH_MODEMASK ((1 << 8) - 1)
#define XDL_PATCH_IGNOREBSPACE (1 << 8)
#define XDL_MMB_READONLY (1 << 0)
#define XDL_MMF_ATOMIC (1 << 0)
#define XDL_BDOP_INS 1
#define XDL_BDOP_CPY 2
#define XDL_BDOP_INSB 3
typedef struct s_memallocator {
void *priv;
void *(*malloc)(void *, unsigned int);
void (*free)(void *, void *);
void *(*realloc)(void *, void *, unsigned int);
} memallocator_t;
typedef struct s_mmblock {
struct s_mmblock *next;
unsigned long flags;
long size, bsize;
char *ptr;
} mmblock_t;
typedef struct s_mmfile {
unsigned long flags;
mmblock_t *head, *tail;
long bsize, fsize, rpos;
mmblock_t *rcur, *wcur;
} mmfile_t;
typedef struct s_mmbuffer {
char *ptr;
long size;
} mmbuffer_t;
typedef struct s_xpparam {
unsigned long flags;
} xpparam_t;
typedef struct s_xdemitcb {
void *priv;
int (*outf)(void *, mmbuffer_t *, int);
} xdemitcb_t;
typedef struct s_xdemitconf {
long ctxlen;
} xdemitconf_t;
typedef struct s_bdiffparam {
long bsize;
} bdiffparam_t;
int xdl_set_allocator(memallocator_t const *malt);
void *xdl_malloc(unsigned int size);
void xdl_free(void *ptr);
void *xdl_realloc(void *ptr, unsigned int size);
int xdl_init_mmfile(mmfile_t *mmf, long bsize, unsigned long flags);
void xdl_free_mmfile(mmfile_t *mmf);
int xdl_mmfile_iscompact(mmfile_t *mmf);
int xdl_seek_mmfile(mmfile_t *mmf, long off);
long xdl_read_mmfile(mmfile_t *mmf, void *data, long size);
long xdl_write_mmfile(mmfile_t *mmf, void const *data, long size);
long xdl_writem_mmfile(mmfile_t *mmf, mmbuffer_t *mb, int nbuf);
void *xdl_mmfile_writeallocate(mmfile_t *mmf, long size);
long xdl_mmfile_ptradd(mmfile_t *mmf, char *ptr, long size, unsigned long flags);
long xdl_copy_mmfile(mmfile_t *mmf, long size, xdemitcb_t *ecb);
void *xdl_mmfile_first(mmfile_t *mmf, long *size);
void *xdl_mmfile_next(mmfile_t *mmf, long *size);
long xdl_mmfile_size(mmfile_t *mmf);
int xdl_mmfile_cmp(mmfile_t *mmf1, mmfile_t *mmf2);
int xdl_mmfile_compact(mmfile_t *mmfo, mmfile_t *mmfc, long bsize, unsigned long flags);
int xdl_diff(mmfile_t *mf1, mmfile_t *mf2, xpparam_t const *xpp,
xdemitconf_t const *xecfg, xdemitcb_t *ecb);
int xdl_patch(mmfile_t *mf, mmfile_t *mfp, int mode, xdemitcb_t *ecb,
xdemitcb_t *rjecb);
int xdl_merge3(mmfile_t *mmfo, mmfile_t *mmf1, mmfile_t *mmf2, xdemitcb_t *ecb,
xdemitcb_t *rjecb);
int xdl_bdiff_mb(mmbuffer_t *mmb1, mmbuffer_t *mmb2, bdiffparam_t const *bdp, xdemitcb_t *ecb);
int xdl_bdiff(mmfile_t *mmf1, mmfile_t *mmf2, bdiffparam_t const *bdp, xdemitcb_t *ecb);
int xdl_rabdiff_mb(mmbuffer_t *mmb1, mmbuffer_t *mmb2, xdemitcb_t *ecb);
int xdl_rabdiff(mmfile_t *mmf1, mmfile_t *mmf2, xdemitcb_t *ecb);
long xdl_bdiff_tgsize(mmfile_t *mmfp);
int xdl_bpatch(mmfile_t *mmf, mmfile_t *mmfp, xdemitcb_t *ecb);
int xdl_bpatch_multi(mmbuffer_t *base, mmbuffer_t *mbpch, int n, xdemitcb_t *ecb);
//#ifdef __cplusplus
//}
//#endif /* #ifdef __cplusplus */
#endif /* #if !defined(XDIFF_H) */
/* END xdiff.h */
#ifdef LIBXDIFF_IMPLEMENTATION
#undef LIBXDIFF_IMPLEMENTATION
/* BEGIN xinclude.h */
#if !defined(XINCLUDE_H)
#define XINCLUDE_H
#if defined(HAVE_WINCONFIG_H)
#include "winconfig.h"
#endif /* #if defined(HAVE_CONFIG_H) */
#if defined(HAVE_CONFIG_H)
#include "config.h"
#endif /* #if defined(HAVE_CONFIG_H) */
#if defined(HAVE_STDIO_H)
#include <stdio.h>
#endif /* #if defined(HAVE_STDIO_H) */
#if defined(HAVE_STDLIB_H)
#include <stdlib.h>
#endif /* #if defined(HAVE_STDLIB_H) */
#if defined(HAVE_UNISTD_H)
#include <unistd.h>
#endif /* #if defined(HAVE_UNISTD_H) */
#if defined(HAVE_STRING_H)
#include <string.h>
#endif /* #if defined(HAVE_STRING_H) */
#if defined(HAVE_LIMITS_H)
#include <limits.h>
#endif /* #if defined(HAVE_LIMITS_H) */
/*
#include "xmacros.h"
#include "xmissing.h"
#include "xdiff.h"
#include "xtypes.h"
#include "xutils.h"
#include "xadler32.h"
#include "xprepare.h"
#include "xdiffi.h"
#include "xemit.h"
#include "xbdiff.h"
*/
#endif /* #if !defined(XINCLUDE_H) */
/* END xinclude.h */
/* BEGIN xmacros.h */
#if !defined(XMACROS_H)
#define XMACROS_H
#define XDL_MIN(a, b) ((a) < (b) ? (a): (b))
#define XDL_MAX(a, b) ((a) > (b) ? (a): (b))
#define XDL_ABS(v) ((v) >= 0 ? (v): -(v))
#define XDL_ISDIGIT(c) ((c) >= '0' && (c) <= '9')
#define XDL_ADDBITS(v, b) ((v) + ((v) >> (b)))
#define XDL_MASKBITS(b) ((1UL << (b)) - 1)
#define XDL_HASHLONG(v, b) (XDL_ADDBITS((unsigned long) (v), b) & XDL_MASKBITS(b))
#define XDL_PTRFREE(p) do { if (p) { xdl_free(p); (p) = NULL; } } while (0)
#define XDL_RECMATCH(r1, r2) ((r1)->size == (r2)->size && memcmp((r1)->ptr, (r2)->ptr, (r1)->size) == 0)
#define XDL_LE32_PUT(p, v) do { \
unsigned char *__p = (unsigned char *) (p); \
*__p++ = (unsigned char) (v); \
*__p++ = (unsigned char) ((v) >> 8); \
*__p++ = (unsigned char) ((v) >> 16); \
*__p = (unsigned char) ((v) >> 24); \
} while (0)
#define XDL_LE32_GET(p, v) do { \
unsigned char const *__p = (unsigned char const *) (p); \
(v) = (unsigned long) __p[0] | ((unsigned long) __p[1]) << 8 | \
((unsigned long) __p[2]) << 16 | ((unsigned long) __p[3]) << 24; \
} while (0)
#endif /* #if !defined(XMACROS_H) */
/* END macros.h */
/* BEGIN xmissing.h */
#if !defined(XMISSING_H)
#define XMISSING_H
#if !defined(CHAR_BIT)
#define CHAR_BIT 8
#endif /* #if !defined(CHAR_BIT) */
#if !defined(HAVE_MEMCHR)
void *memchr(void const *p, int c, long n);
#endif /* #if !defined(HAVE_MEMCHR) */
#if !defined(HAVE_MEMCMP)
int memcmp(void const *p1, void const *p2, long n);
#endif /* #if !defined(HAVE_MEMCMP) */
#if !defined(HAVE_MEMCPY)
void *memcpy(void *d, void const *s, long n);
#endif /* #if !defined(HAVE_MEMCPY) */
#if !defined(HAVE_MEMSET)
void *memset(void *d, int c, long n);
#endif /* #if !defined(HAVE_MEMSET) */
#if !defined(HAVE_STRLEN)
long strlen(char const *s);
#endif /* #if !defined(HAVE_STRLEN) */
#endif /* #if !defined(XMISSING_H) */
/* END xmissing.h */
/* BEGIN xtypes.h */
#if !defined(XTYPES_H)
#define XTYPES_H
typedef struct s_chanode {
struct s_chanode *next;
long icurr;
} chanode_t;
typedef struct s_chastore {
chanode_t *head, *tail;
long isize, nsize;
chanode_t *ancur;
chanode_t *sncur;
long scurr;
} chastore_t;
typedef struct s_xrecord {
struct s_xrecord *next;
char const *ptr;
long size;
unsigned long ha;
} xrecord_t;
typedef struct s_xdfile {
chastore_t rcha;
long nrec;
unsigned int hbits;
xrecord_t **rhash;
long dstart, dend;
xrecord_t **recs;
char *rchg;
long *rindex;
long nreff;
unsigned long *ha;
} xdfile_t;
typedef struct s_xdfenv {
xdfile_t xdf1, xdf2;
} xdfenv_t;
#endif /* #if !defined(XTYPES_H) */
/* END xtypes.h */
/* BEGIN xutils.h */
#if !defined(XUTILS_H)
#define XUTILS_H
long xdl_bogosqrt(long n);
int xdl_emit_diffrec(char const *rec, long size, char const *pre, long psize,
xdemitcb_t *ecb);
int xdl_mmfile_outf(void *priv, mmbuffer_t *mb, int nbuf);
int xdl_cha_init(chastore_t *cha, long isize, long icount);
void xdl_cha_free(chastore_t *cha);
void *xdl_cha_alloc(chastore_t *cha);
void *xdl_cha_first(chastore_t *cha);
void *xdl_cha_next(chastore_t *cha);
long xdl_guess_lines(mmfile_t *mf);
unsigned long xdl_hash_record(char const **data, char const *top);
unsigned int xdl_hashbits(unsigned int size);
int xdl_num_out(char *out, long val);
long xdl_atol(char const *str, char const **next);
int xdl_emit_hunk_hdr(long s1, long c1, long s2, long c2, xdemitcb_t *ecb);
#endif /* #if !defined(XUTILS_H) */
/* END xutils.h */
/* BEGIN xadler32.h */
#if !defined(XADLER32_H)
#define XADLER32_H
unsigned long xdl_adler32(unsigned long adler, unsigned char const *buf,
unsigned int len);
#endif /* #if !defined(XADLER32_H) */
/* END xadler32.h */
/* BEGIN xprepare.h */
#if !defined(XPREPARE_H)
#define XPREPARE_H
int xdl_prepare_env(mmfile_t *mf1, mmfile_t *mf2, xpparam_t const *xpp,
xdfenv_t *xe);
void xdl_free_env(xdfenv_t *xe);
#endif /* #if !defined(XPREPARE_H) */
/* END xprepare.h */
/* BEGIN xdiffi.h */
#if !defined(XDIFFI_H)
#define XDIFFI_H
typedef struct s_diffdata {
long nrec;
unsigned long const *ha;
long *rindex;
char *rchg;
} diffdata_t;
typedef struct s_xdalgoenv {
long mxcost;
long snake_cnt;
long heur_min;
} xdalgoenv_t;
typedef struct s_xdchange {
struct s_xdchange *next;
long i1, i2;
long chg1, chg2;
} xdchange_t;
int xdl_recs_cmp(diffdata_t *dd1, long off1, long lim1,
diffdata_t *dd2, long off2, long lim2,
long *kvdf, long *kvdb, int need_min, xdalgoenv_t *xenv);
int xdl_do_diff(mmfile_t *mf1, mmfile_t *mf2, xpparam_t const *xpp,
xdfenv_t *xe);
int xdl_build_script(xdfenv_t *xe, xdchange_t **xscr);
void xdl_free_script(xdchange_t *xscr);
int xdl_emit_diff(xdfenv_t *xe, xdchange_t *xscr, xdemitcb_t *ecb,
xdemitconf_t const *xecfg);
#endif /* #if !defined(XDIFFI_H) */
/* END xdiffi.h */
/* BEGIN xemit.h */
#if !defined(XEMIT_H)
#define XEMIT_H
int xdl_emit_diff(xdfenv_t *xe, xdchange_t *xscr, xdemitcb_t *ecb,
xdemitconf_t const *xecfg);
#endif /* #if !defined(XEMIT_H) */
/* END xemit.h */
/* BEGIN xbdiff.h */
#if !defined(XBDIFF_H)
#define XBDIFF_H
#define XDL_BPATCH_HDR_SIZE (4 + 4)
#define XDL_MIN_BLKSIZE 16
#define XDL_INSBOP_SIZE (1 + 4)
#define XDL_COPYOP_SIZE (1 + 4 + 4)
unsigned long xdl_mmb_adler32(mmbuffer_t *mmb);
unsigned long xdl_mmf_adler32(mmfile_t *mmf);
#endif /* #if !defined(XBDIFF_H) */
/* END xbdiff.h */
/* BEGIN xadler32.c */
/* largest prime smaller than 65536 */
#define BASE 65521L
/* NMAX is the largest n such that 255n(n+1)/2 + (n+1)(BASE-1) <= 2^32-1 */
#define NMAX 5552
#define DO1(buf, i) { s1 += buf[i]; s2 += s1; }
#define DO2(buf, i) DO1(buf, i); DO1(buf, i + 1);
#define DO4(buf, i) DO2(buf, i); DO2(buf, i + 2);
#define DO8(buf, i) DO4(buf, i); DO4(buf, i + 4);
#define DO16(buf) DO8(buf, 0); DO8(buf, 8);
unsigned long xdl_adler32(unsigned long adler, unsigned char const *buf,
unsigned int len) {
int k;
unsigned long s1 = adler & 0xffff;
unsigned long s2 = (adler >> 16) & 0xffff;
if (!buf)
return 1;
while (len > 0) {
k = len < NMAX ? len :NMAX;
len -= k;
while (k >= 16) {
DO16(buf);
buf += 16;
k -= 16;
}
if (k != 0)
do {
s1 += *buf++;
s2 += s1;
} while (--k);
s1 %= BASE;
s2 %= BASE;
}
return (s2 << 16) | s1;
}
/* END xadler32.c */
/* BEGIN xalloc.c */
static void *wrap_malloc(void *priv, unsigned int size) {
return malloc(size);
}
static void wrap_free(void *priv, void *ptr) {
free(ptr);
}
static void *wrap_realloc(void *priv, void *ptr, unsigned int size) {
return realloc(ptr, size);
}
static memallocator_t xmalt = { NULL, wrap_malloc, wrap_free, wrap_realloc };
int xdl_set_allocator(memallocator_t const *malt) {
xmalt = *malt;
return 0;
}
void *xdl_malloc(unsigned int size) {
return xmalt.malloc ? xmalt.malloc(xmalt.priv, size): NULL;
}
void xdl_free(void *ptr) {
if (xmalt.free)
xmalt.free(xmalt.priv, ptr);
}
void *xdl_realloc(void *ptr, unsigned int size) {
return xmalt.realloc ? xmalt.realloc(xmalt.priv, ptr, size): NULL;
}
/* END xalloc.x */
/* BEGIN xbdiff. c */
typedef struct s_bdrecord {
struct s_bdrecord *next;
unsigned long fp;
char const *ptr;
} bdrecord_t;
typedef struct s_bdfile {
char const *data, *top;
chastore_t cha;
unsigned int fphbits;
bdrecord_t **fphash;
} bdfile_t;
static int xdl_prepare_bdfile(mmbuffer_t *mmb, long fpbsize, bdfile_t *bdf) {
unsigned int fphbits;
long i, size, hsize;
char const *base, *data, *top;
bdrecord_t *brec;
bdrecord_t **fphash;
fphbits = xdl_hashbits((unsigned int) (mmb->size / fpbsize) + 1);
hsize = 1 << fphbits;
if (!(fphash = (bdrecord_t **) xdl_malloc(hsize * sizeof(bdrecord_t *)))) {
return -1;
}
for (i = 0; i < hsize; i++)
fphash[i] = NULL;
if (xdl_cha_init(&bdf->cha, sizeof(bdrecord_t), hsize / 4 + 1) < 0) {
xdl_free(fphash);
return -1;
}
if (!(size = mmb->size)) {
bdf->data = bdf->top = NULL;
} else {
bdf->data = data = base = mmb->ptr;
bdf->top = top = mmb->ptr + mmb->size;
if ((data += (size / fpbsize) * fpbsize) == top)
data -= fpbsize;
for (; data >= base; data -= fpbsize) {
if (!(brec = (bdrecord_t *) xdl_cha_alloc(&bdf->cha))) {
xdl_cha_free(&bdf->cha);
xdl_free(fphash);
return -1;
}
brec->fp = xdl_adler32(0, (unsigned char const *) data,
XDL_MIN(fpbsize, (long) (top - data)));
brec->ptr = data;
i = (long) XDL_HASHLONG(brec->fp, fphbits);
brec->next = fphash[i];
fphash[i] = brec;
}
}
bdf->fphbits = fphbits;
bdf->fphash = fphash;
return 0;
}
static void xdl_free_bdfile(bdfile_t *bdf) {
xdl_free(bdf->fphash);
xdl_cha_free(&bdf->cha);
}
unsigned long xdl_mmb_adler32(mmbuffer_t *mmb) {
return mmb->size ? xdl_adler32(0, (unsigned char const *) mmb->ptr, mmb->size): 0;
}
unsigned long xdl_mmf_adler32(mmfile_t *mmf) {
unsigned long fp = 0;
long size;
char const *blk;
if ((blk = (char const *) xdl_mmfile_first(mmf, &size)) != NULL) {
do {
fp = xdl_adler32(fp, (unsigned char const *) blk, size);
} while ((blk = (char const *) xdl_mmfile_next(mmf, &size)) != NULL);
}
return fp;
}
int xdl_bdiff_mb(mmbuffer_t *mmb1, mmbuffer_t *mmb2, bdiffparam_t const *bdp, xdemitcb_t *ecb) {
long i, rsize, size, bsize, csize, msize, moff;
unsigned long fp;
char const *blk, *base, *data, *top, *ptr1, *ptr2;
bdrecord_t *brec;
bdfile_t bdf;
mmbuffer_t mb[2];
unsigned char cpybuf[32];
if ((bsize = bdp->bsize) < XDL_MIN_BLKSIZE)
bsize = XDL_MIN_BLKSIZE;
if (xdl_prepare_bdfile(mmb1, bsize, &bdf) < 0) {
return -1;
}
/*
* Prepare and emit the binary patch file header. It will be used
* to verify that that file being patched matches in size and fingerprint
* the one that generated the patch.
*/
fp = xdl_mmb_adler32(mmb1);
size = mmb1->size;
XDL_LE32_PUT(cpybuf, fp);
XDL_LE32_PUT(cpybuf + 4, size);
mb[0].ptr = (char *) cpybuf;
mb[0].size = 4 + 4;
if (ecb->outf(ecb->priv, mb, 1) < 0) {
xdl_free_bdfile(&bdf);
return -1;
}
if ((blk = (char const *) mmb2->ptr) != NULL) {
size = mmb2->size;
for (base = data = blk, top = data + size; data < top;) {
rsize = XDL_MIN(bsize, (long) (top - data));
fp = xdl_adler32(0, (unsigned char const *) data, rsize);
i = (long) XDL_HASHLONG(fp, bdf.fphbits);
for (msize = 0, brec = bdf.fphash[i]; brec; brec = brec->next)
if (brec->fp == fp) {
csize = XDL_MIN((long) (top - data), (long) (bdf.top - brec->ptr));
for (ptr1 = brec->ptr, ptr2 = data; csize && *ptr1 == *ptr2;
csize--, ptr1++, ptr2++);
if ((csize = (long) (ptr1 - brec->ptr)) > msize) {
moff = (long) (brec->ptr - bdf.data);
msize = csize;
}
}
if (msize < XDL_COPYOP_SIZE) {
data++;
} else {
if (data > base) {
i = (long) (data - base);
if (i > 255) {
cpybuf[0] = XDL_BDOP_INSB;
XDL_LE32_PUT(cpybuf + 1, i);
mb[0].ptr = (char *) cpybuf;
mb[0].size = XDL_INSBOP_SIZE;
} else {
cpybuf[0] = XDL_BDOP_INS;
cpybuf[1] = (unsigned char) i;
mb[0].ptr = (char *) cpybuf;
mb[0].size = 2;
}
mb[1].ptr = (char *) base;
mb[1].size = i;
if (ecb->outf(ecb->priv, mb, 2) < 0) {
xdl_free_bdfile(&bdf);
return -1;
}
}
data += msize;
cpybuf[0] = XDL_BDOP_CPY;
XDL_LE32_PUT(cpybuf + 1, moff);
XDL_LE32_PUT(cpybuf + 5, msize);
mb[0].ptr = (char *) cpybuf;
mb[0].size = XDL_COPYOP_SIZE;
if (ecb->outf(ecb->priv, mb, 1) < 0) {
xdl_free_bdfile(&bdf);
return -1;
}
base = data;
}
}
if (data > base) {
i = (long) (data - base);
if (i > 255) {
cpybuf[0] = XDL_BDOP_INSB;
XDL_LE32_PUT(cpybuf + 1, i);
mb[0].ptr = (char *) cpybuf;
mb[0].size = XDL_INSBOP_SIZE;
} else {
cpybuf[0] = XDL_BDOP_INS;
cpybuf[1] = (unsigned char) i;
mb[0].ptr = (char *) cpybuf;
mb[0].size = 2;
}
mb[1].ptr = (char *) base;
mb[1].size = i;
if (ecb->outf(ecb->priv, mb, 2) < 0) {
xdl_free_bdfile(&bdf);
return -1;
}
}
}
xdl_free_bdfile(&bdf);
return 0;
}
int xdl_bdiff(mmfile_t *mmf1, mmfile_t *mmf2, bdiffparam_t const *bdp, xdemitcb_t *ecb) {
mmbuffer_t mmb1, mmb2;
if (!xdl_mmfile_iscompact(mmf1) || !xdl_mmfile_iscompact(mmf2)) {
return -1;
}
if ((mmb1.ptr = (char *) xdl_mmfile_first(mmf1, &mmb1.size)) == NULL)
mmb1.size = 0;
if ((mmb2.ptr = (char *) xdl_mmfile_first(mmf2, &mmb2.size)) == NULL)
mmb2.size = 0;
return xdl_bdiff_mb(&mmb1, &mmb2, bdp, ecb);
}
long xdl_bdiff_tgsize(mmfile_t *mmfp) {
long tgsize = 0, size, off, csize;
char const *blk;
unsigned char const *data, *top;
if ((blk = (char const *) xdl_mmfile_first(mmfp, &size)) == NULL ||
size < XDL_BPATCH_HDR_SIZE) {
return -1;
}
blk += XDL_BPATCH_HDR_SIZE;
size -= XDL_BPATCH_HDR_SIZE;
do {
for (data = (unsigned char const *) blk, top = data + size;
data < top;) {
if (*data == XDL_BDOP_INS) {
data++;
csize = (long) *data++;
tgsize += csize;
data += csize;
} else if (*data == XDL_BDOP_INSB) {
data++;
XDL_LE32_GET(data, csize);
data += 4;
tgsize += csize;
data += csize;
} else if (*data == XDL_BDOP_CPY) {
data++;
XDL_LE32_GET(data, off);
data += 4;
XDL_LE32_GET(data, csize);
data += 4;
tgsize += csize;
} else {
return -1;
}
}
} while ((blk = (char const *) xdl_mmfile_next(mmfp, &size)) != NULL);
return tgsize;
}
/* END xbdiff.c */
/* BEGIN xpatchi.c */
#define XDL_MOBF_MINALLOC 128
typedef struct s_mmoffbuffer {
long off, size;
char *ptr;
} mmoffbuffer_t;
static int xdl_copy_range(mmfile_t *mmf, long off, long size, xdemitcb_t *ecb) {
if (xdl_seek_mmfile(mmf, off) < 0) {
return -1;
}
if (xdl_copy_mmfile(mmf, size, ecb) != size) {
return -1;
}
return 0;
}
int xdl_bpatch(mmfile_t *mmf, mmfile_t *mmfp, xdemitcb_t *ecb) {
long size, off, csize, osize;
unsigned long fp, ofp;
char const *blk;
unsigned char const *data, *top;
mmbuffer_t mb;
if ((blk = (char const *) xdl_mmfile_first(mmfp, &size)) == NULL ||
size < XDL_BPATCH_HDR_SIZE) {
return -1;
}
ofp = xdl_mmf_adler32(mmf);
osize = xdl_mmfile_size(mmf);
XDL_LE32_GET(blk, fp);
XDL_LE32_GET(blk + 4, csize);
if (fp != ofp || csize != osize) {
return -1;
}
blk += XDL_BPATCH_HDR_SIZE;
size -= XDL_BPATCH_HDR_SIZE;
do {
for (data = (unsigned char const *) blk, top = data + size;
data < top;) {
if (*data == XDL_BDOP_INS) {
data++;
mb.size = (long) *data++;
mb.ptr = (char *) data;
data += mb.size;
if (ecb->outf(ecb->priv, &mb, 1) < 0) {
return -1;
}
} else if (*data == XDL_BDOP_INSB) {
data++;
XDL_LE32_GET(data, csize);
data += 4;
mb.size = csize;
mb.ptr = (char *) data;
data += mb.size;
if (ecb->outf(ecb->priv, &mb, 1) < 0) {
return -1;
}
} else if (*data == XDL_BDOP_CPY) {
data++;
XDL_LE32_GET(data, off);
data += 4;
XDL_LE32_GET(data, csize);
data += 4;
if (xdl_copy_range(mmf, off, csize, ecb) < 0) {
return -1;
}
} else {
return -1;
}
}
} while ((blk = (char const *) xdl_mmfile_next(mmfp, &size)) != NULL);
return 0;
}
static unsigned long xdl_mmob_adler32(mmoffbuffer_t *obf, int n) {
unsigned long ha;
for (ha = 0; n > 0; n--, obf++)
ha = xdl_adler32(ha, (unsigned char const *) obf->ptr, obf->size);
return ha;
}
static long xdl_mmob_size(mmoffbuffer_t *obf, int n) {
return n > 0 ? obf[n - 1].off + obf[n - 1].size: 0;
}
static mmoffbuffer_t *xdl_mmob_new(mmoffbuffer_t **probf, int *pnobf, int *paobf) {
int aobf;
mmoffbuffer_t *cobf, *rrobf;
if (*pnobf >= *paobf) {
aobf = 2 * (*paobf) + 1;
if ((rrobf = (mmoffbuffer_t *)