Skip to content

Latest commit

 

History

History
1924 lines (1520 loc) · 41.7 KB

xdmain.c

File metadata and controls

1924 lines (1520 loc) · 41.7 KB
 
Jan 5, 2004
Jan 5, 2004
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
/* -*- Mode: C;-*-
*
* This file is part of XDelta - A binary delta generator.
*
* Copyright (C) 1997, 1998, 1999, 2001 Josh MacDonald
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program 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 General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*
* Author: Josh MacDonald <jmacd@CS.Berkeley.EDU>
*
* $Id: xdmain.c,v 1.1 2004/01/05 18:54:22 icculus Exp $
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <ctype.h>
#include <fcntl.h>
#include <sys/stat.h>
#if defined(_WIN32) || defined(__DJGPP__)
#define WINHACK
#endif
#ifndef WINHACK
#include <unistd.h>
#include <sys/mman.h>
#define O_BINARY 0
#else /* WINHACK */
#include <io.h>
#include <process.h>
#ifdef __DJGPP__
#include <unistd.h>
#include <dpmi.h>
#endif
#define STDOUT_FILENO 1
#define lstat stat
#ifndef __DJGPP__
#define S_IFMT _S_IFMT
#define S_IFREG _S_IFREG
#endif /* !__DJGPP__ */
#endif /* !WINHACK_ */
#include <zlib.h>
#include "xdelta.h"
extern HandleFuncTable xd_handle_table;
#define XD_PAGE_SIZE (1<<20)
#define XDELTA_110_PREFIX "%XDZ004%"
#define XDELTA_104_PREFIX "%XDZ003%"
#define XDELTA_100_PREFIX "%XDZ002%"
#define XDELTA_020_PREFIX "%XDZ001%"
#define XDELTA_018_PREFIX "%XDZ000%"
#define XDELTA_014_PREFIX "%XDELTA%"
#define XDELTA_PREFIX XDELTA_110_PREFIX
#define XDELTA_PREFIX_LEN 8
#define HEADER_WORDS (6)
#define HEADER_SPACE (HEADER_WORDS*4)
/* The header is composed of 4-byte words (network byte order) as follows:
* word 1: flags
* word 2: (from name length) << 16 | (to name length)
* word 3: (reserved)
* word 4: (reserved)
* word 5: (reserved)
* word 6: (reserved)
* flags are:
*/
#define FLAG_NO_VERIFY 1
#define FLAG_FROM_COMPRESSED 2
#define FLAG_TO_COMPRESSED 4
#define FLAG_PATCH_COMPRESSED 8
/* and, the header is follwed by the from file name, then the to file
* name, then the data. */
#ifdef WINHACK
#define FOPEN_READ_ARG "rb"
#define FOPEN_WRITE_ARG "wb"
#define FILE_SEPARATOR '\\'
#else
#define FOPEN_READ_ARG "r"
#define FOPEN_WRITE_ARG "w"
#define FILE_SEPARATOR '/'
#endif /* WINHACK */
#include "getopt.h"
typedef struct _LRU LRU;
struct _LRU
{
LRU *next;
LRU *prev;
gint refs;
guint page;
guint8* buffer;
};
typedef struct _XdFileHandle XdFileHandle;
typedef struct {
gboolean patch_is_compressed;
const gchar* patch_name;
guint patch_flags;
const gchar* patch_version;
gboolean has_trailer;
XdeltaSourceInfo* data_source;
XdeltaSourceInfo* from_source;
gchar *from_name;
gchar *to_name;
guint control_offset;
guint header_offset;
gint16 from_name_len;
gint16 to_name_len;
guint32 header_space[HEADER_WORDS];
guint8 magic_buf[XDELTA_PREFIX_LEN];
XdFileHandle *patch_in;
XdeltaControl *cont;
} XdeltaPatch;
struct _XdFileHandle
{
FileHandle fh;
guint length;
guint real_length;
gint type;
const char* name;
const char* cleanup;
guint8 md5[16];
EdsioMD5Ctx ctx;
/* for write */
int out_fd;
void* out;
gboolean (* out_write) (XdFileHandle* handle, const void* buf, gint nbyte);
gboolean (* out_close) (XdFileHandle* handle);
/* for read */
GPtrArray *lru_table;
LRU *lru_head; /* most recently used. */
LRU *lru_tail; /* least recently used. */
GMemChunk *lru_chunk;
guint lru_count;
guint lru_outstanding_refs;
guint narrow_low;
guint narrow_high;
guint current_pos;
FILE* in;
gboolean (* in_read) (XdFileHandle* handle, void* buf, gint nbyte);
gboolean (* in_close) (XdFileHandle* handle);
gboolean in_compressed;
const guint8* copy_page;
guint copy_pgno;
gboolean md5_good;
gboolean reset_length_next_write;
gint md5_page;
gint fd;
};
/* $Format: "static const char xdelta_version[] = \"$ReleaseVersion$\"; " $ */
static const char xdelta_version[] = "1.1.3";
typedef struct _Command Command;
struct _Command {
gchar* name;
gint (* func) (gint argc, gchar** argv);
gint nargs;
};
static gint delta_command (gint argc, gchar** argv);
static gint patch_command (gint argc, gchar** argv);
static gint info_command (gint argc, gchar** argv);
static const Command commands[] =
{
{ "delta", delta_command, -1 },
{ "patch", patch_command, -1 },
{ "info", info_command, 1 },
{ NULL, NULL, 0 }
};
static struct option const long_options[] =
{
{"help", no_argument, 0, 'h'},
{"version", no_argument, 0, 'v'},
{"verbose", no_argument, 0, 'V'},
{"noverify", no_argument, 0, 'n'},
{"pristine", no_argument, 0, 'p'},
{"quiet", no_argument, 0, 'q'},
{"maxmem", required_argument, 0, 'm'},
{"blocksize", required_argument, 0, 's'},
{0,0,0,0}
};
static const gchar* program_name;
static gint compress_level = Z_DEFAULT_COMPRESSION;
static gint no_verify = FALSE;
static gint pristine = FALSE;
static gint verbose = FALSE;
static gint max_mapped_pages = G_MAXINT;
static gint quiet = FALSE;
#define xd_error g_warning
static void
usage ()
{
xd_error ("usage: %s COMMAND [OPTIONS] [ARG1 ...]\n", program_name);
xd_error ("use --help for more help\n");
exit (2);
}
static void
help ()
{
xd_error ("usage: %s COMMAND [OPTIONS] [ARG1 ...]\n", program_name);
xd_error ("COMMAND is one of:\n");
xd_error (" delta Produce a delta from ARG1 to ARG2 producing ARG3\n");
xd_error (" info List details about delta ARG1\n");
xd_error (" patch Apply patch ARG1 using file ARG2 producing ARG3\n");
xd_error ("OPTIONS are:\n");
xd_error (" -v, --version Print version information\n");
xd_error (" -V, --verbose Print verbose error messages\n");
xd_error (" -h, --help Print this summary\n");
xd_error (" -n, --noverify Disable automatic MD5 verification\n");
xd_error (" -p, --pristine Disable automatic GZIP decompression\n");
xd_error (" -m, --maxmem=SIZE Set the buffer size limit, e.g. 640K, 16M\n");
xd_error (" -[0-9] ZLIB compress level: 0=none, 1=fast, 6=default, 9=best\n");
xd_error (" -s=BLOCK_SIZE Sets block size (power of 2), minimum match length\n");
xd_error (" In-core memory requirement is (FROM_LEN * 8) / BLOCK_SIZE\n");
exit (2);
}
static void
version ()
{
xd_error ("version %s\n", xdelta_version);
exit (2);
}
static FILE* xd_error_file = NULL;
static void
xd_error_func (const gchar *log_domain,
GLogLevelFlags log_level,
const gchar *message,
gpointer user_data)
{
if (! xd_error_file)
xd_error_file = stderr;
fprintf (xd_error_file, "%s: %s", program_name, message);
}
static gboolean
event_devel (void)
{
static gboolean once = FALSE;
static gboolean devel = FALSE;
if (! once)
{
devel = g_getenv ("EDSIO_DEVEL") != NULL;
once = TRUE;
}
return devel;
}
static gboolean
event_watch (GenericEvent* ev, GenericEventDef* def, const char* message)
{
if (quiet && def->level <= EL_Warning)
return TRUE;
if (event_devel ())
fprintf (stderr, "%s:%d: %s\n", ev->srcfile, ev->srcline, message);
else
fprintf (stderr, "%s: %s\n", program_name, message);
return TRUE;
}
gint
main (gint argc, gchar** argv)
{
const Command *cmd = NULL;
gint c;
gint longind;
eventdelivery_event_watch_all (event_watch);
if (! xd_edsio_init ())
return 2;
#ifdef __DJGPP__
/*
* (richdawe@bigfoot.com): Limit maximum memory usage to 87.5% of available
* physical memory for DJGPP. Otherwise the replacement for mmap() will
* exhaust memory and XDelta will fail.
*/
{
unsigned long phys_free = _go32_dpmi_remaining_physical_memory();
max_mapped_pages = phys_free / XD_PAGE_SIZE;
max_mapped_pages %= 8;
max_mapped_pages *= 7;
}
#endif /* DJGPP */
program_name = g_basename (argv[0]);
g_log_set_handler (G_LOG_DOMAIN,
G_LOG_LEVEL_WARNING,
xd_error_func,
NULL);
if (argc < 2)
usage ();
for (cmd = commands; cmd->name; cmd += 1)
if (strcmp (cmd->name, argv[1]) == 0)
break;
if (strcmp (argv[1], "-h") == 0 ||
strcmp (argv[1], "--help") == 0)
help ();
if (strcmp (argv[1], "-v") == 0 ||
strcmp (argv[1], "--version") == 0)
version ();
if (!cmd->name)
{
xd_error ("unrecognized command\n");
help ();
}
argc -= 1;
argv += 1;
while ((c = getopt_long(argc,
argv,
"+nqphvVs:m:0123456789",
long_options,
&longind)) != EOF)
{
switch (c)
{
case 'q': quiet = TRUE; break;
case 'n': no_verify = TRUE; break;
case 'p': pristine = TRUE; break;
case 'V': verbose = TRUE; break;
case 's':
{
int ret;
int s = atoi (optarg);
if ((ret = xdp_set_query_size_pow (s)) && ! quiet)
{
xd_error ("illegal query size: %s\n", xdp_errno (ret));
}
}
break;
case 'm':
{
gchar* end = NULL;
glong l = strtol (optarg, &end, 0);
if (end && g_strcasecmp (end, "M") == 0)
l <<= 20;
else if (end && g_strcasecmp (end, "K") == 0)
l <<= 10;
else if (end || l < 0)
{
xd_error ("illegal maxmem argument %s\n", optarg);
return 2;
}
#ifdef __DJGPP__
/*
* (richdawe@bigfoot.com): Old MS-DOS systems may have a maximum
* of 8MB memory == XD_PAGE_SIZE * 8. Therefore, do what the user
* asks for on MS-DOS.
*/
#else /* !__DJGPP__ */
l = MAX (l, XD_PAGE_SIZE * 8);
#endif /* __DJGPP__ */
max_mapped_pages = l / XD_PAGE_SIZE;
}
break;
case 'h': help (); break;
case 'v': version (); break;
case '0': case '1': case '2': case '3': case '4':
case '5': case '6': case '7': case '8': case '9':
compress_level = c - '0';
break;
case '?':
default:
xd_error ("illegal argument, use --help for help\n");
return 2;
}
}
if (verbose && max_mapped_pages < G_MAXINT)
xd_error ("using %d kilobytes of buffer space\n", (max_mapped_pages * XD_PAGE_SIZE) >> 10);
argc -= optind;
argv += optind;
if (cmd->nargs >= 0 && argc != cmd->nargs)
{
xd_error ("wrong number of arguments\n");
help ();
return 2;
}
return (* cmd->func) (argc, argv);
}
/* Commands */
#define READ_TYPE 1
#define READ_NOSEEK_TYPE 1
#define READ_SEEK_TYPE 3
#define WRITE_TYPE 4
/* Note to the casual reader: the filehandle implemented here is
* highly aware of the calling patterns of the Xdelta library. It
* also plays games with narrowing to implement several files in the
* same handle. So, if you try to modify or use this code, BEWARE.
* See the repository package for a complete implementation.
* In fact, its really quite a hack. */
static gssize xd_handle_map_page (XdFileHandle *fh, guint pgno, const guint8** mem);
static gboolean xd_handle_unmap_page (XdFileHandle *fh, guint pgno, const guint8** mem);
static gboolean
xd_fwrite (XdFileHandle* fh, const void* buf, gint nbyte)
{
return fwrite (buf, nbyte, 1, fh->out) == 1;
}
static gboolean
xd_fread (XdFileHandle* fh, void* buf, gint nbyte)
{
return fread (buf, nbyte, 1, fh->in) == 1;
}
static gboolean
xd_fclose (XdFileHandle* fh)
{
return fclose (fh->out) == 0;
}
static gboolean
xd_frclose (XdFileHandle* fh)
{
return fclose (fh->in) == 0;
}
static gboolean
xd_gzwrite (XdFileHandle* fh, const void* buf, gint nbyte)
{
return gzwrite (fh->out, (void*) buf, nbyte) == nbyte;
}
static gboolean
xd_gzread (XdFileHandle* fh, void* buf, gint nbyte)
{
return gzread (fh->in, buf, nbyte) == nbyte;
}
static gboolean
xd_gzclose (XdFileHandle* fh)
{
return gzclose (fh->out) == Z_OK;
}
static gboolean
xd_gzrclose (XdFileHandle* fh)
{
return gzclose (fh->in) == Z_OK;
}
static void
init_table (XdFileHandle* fh)
{
fh->lru_table = g_ptr_array_new ();
fh->lru_chunk = g_mem_chunk_create(LRU, 1<<9, G_ALLOC_ONLY);
fh->lru_head = NULL;
fh->lru_tail = NULL;
}
static XdFileHandle*
open_common (const char* name, const char* real_name)
{
XdFileHandle* fh;
gint fd;
struct stat buf;
if ((fd = open (name, O_RDONLY | O_BINARY, 0)) < 0)
{
xd_error ("open %s failed: %s\n", name, g_strerror (errno));
return NULL;
}
if (stat (name, &buf) < 0)
{
xd_error ("stat %s failed: %s\n", name, g_strerror (errno));
return NULL;
}
/* S_ISREG() is not on Windows */
if ((buf.st_mode & S_IFMT) != S_IFREG)
{
xd_error ("%s is not a regular file\n", name);
return NULL;
}
fh = g_new0 (XdFileHandle, 1);
fh->fh.table = & xd_handle_table;
fh->name = real_name;
fh->fd = fd;
fh->length = buf.st_size;
fh->narrow_high = buf.st_size;
return fh;
}
static gboolean
file_gzipped (const char* name, gboolean *is_compressed)
{
FILE* f = fopen (name, FOPEN_READ_ARG);
guint8 buf[2];
(*is_compressed) = FALSE;
if (! f)
{
xd_error ("open %s failed: %s\n", name, g_strerror (errno));
return FALSE;
}
if (fread (buf, 2, 1, f) != 1)
return TRUE;
#define GZIP_MAGIC1 037
#define GZIP_MAGIC2 0213
if (buf[0] == GZIP_MAGIC1 && buf[1] == GZIP_MAGIC2)
(* is_compressed) = TRUE;
return TRUE;
}
static const char*
xd_tmpname (void)
{
const char* tmpdir = g_get_tmp_dir ();
GString* s;
gint x = getpid ();
static gint seq = 0;
struct stat buf;
s = g_string_new (NULL);
do
{
/*
* (richdawe@bigfoot.com): Limit temporary filenames to
* the MS-DOS 8+3 convention for DJGPP.
*/
g_string_sprintf (s, "%s/xd-%05d.%03d", tmpdir, x, seq++);
}
while (lstat (s->str, &buf) == 0);
return s->str;
}
static const char*
file_gunzip (const char* name)
{
const char* new_name = xd_tmpname ();
FILE* out = fopen (new_name, FOPEN_WRITE_ARG);
gzFile in = gzopen (name, "rb");
guint8 buf[1024];
int nread;
while ((nread = gzread (in, buf, 1024)) > 0)
{
if (fwrite (buf, nread, 1, out) != 1)
{
xd_error ("write %s failed (during uncompression): %s\n", new_name, g_strerror (errno));
return NULL;
}
}
if (nread < 0)
{
xd_error ("gzread %s failed\n", name);
return NULL;
}
gzclose (in);
if (fclose (out))
{
xd_error ("close %s failed (during uncompression): %s\n", new_name, g_strerror (errno));
return NULL;
}
return new_name;
}
static XdFileHandle*
open_read_noseek_handle (const char* name, gboolean* is_compressed, gboolean will_read, gboolean honor_pristine)
{
XdFileHandle* fh;
const char* name0 = name;
/* we _could_ stream-read this file if compressed, but it adds a
* lot of complexity. the library can handle it, just set the
* length to (XDELTA_MAX_FILE_LEN-1) and make sure that the end
* of file condition is set when on the last page. However, I
* don't feel like it. */
if (honor_pristine && pristine)
*is_compressed = FALSE;
else
{
if (! file_gzipped (name, is_compressed))
return NULL;
}
if ((* is_compressed) && ! (name = file_gunzip (name)))
return NULL;
if (! (fh = open_common (name, name0)))
return NULL;
fh->type = READ_NOSEEK_TYPE;
edsio_md5_init (&fh->ctx);
if (*is_compressed)
fh->cleanup = name;
if (will_read)
{
g_assert (fh->fd >= 0);
if (! (fh->in = fdopen (dup (fh->fd), FOPEN_READ_ARG)))
{
xd_error ("fdopen: %s\n", g_strerror (errno));
return NULL;
}
fh->in_read = &xd_fread;
fh->in_close = &xd_frclose;
}
else
{
init_table (fh);
}
return fh;
}
static void
xd_read_close (XdFileHandle* fh)
{
/*
* (richdawe@bigfoot.com): On Unix you can unlink a file while it is
* still open. On MS-DOS this can lead to filesystem corruption. Close
* the file before unlinking it.
*/
close (fh->fd);
if (fh->cleanup)
unlink (fh->cleanup);
if (fh->in)
(*fh->in_close) (fh);
}
static XdFileHandle*
open_read_seek_handle (const char* name, gboolean* is_compressed, gboolean honor_pristine)
{
XdFileHandle* fh;
const char* name0 = name;
if (honor_pristine && pristine)
*is_compressed = FALSE;
else
{
if (! file_gzipped (name, is_compressed))
return NULL;
}
if ((* is_compressed) && ! (name = file_gunzip (name)))
return NULL;
if (! (fh = open_common (name, name0)))
return NULL;
fh->type = READ_SEEK_TYPE;
if (*is_compressed)
fh->cleanup = name;
init_table (fh);
edsio_md5_init (&fh->ctx);
return fh;
}
static XdFileHandle*
open_write_handle (int fd, const char* name)
{
XdFileHandle* fh = g_new0 (XdFileHandle, 1);
int nfd;
fh->fh.table = & xd_handle_table;
fh->out_fd = fd;
fh->out_write = &xd_fwrite;
fh->out_close = &xd_fclose;
g_assert (fh->out_fd >= 0);
nfd = dup (fh->out_fd);
if (! (fh->out = fdopen (nfd, FOPEN_WRITE_ARG)))
{
xd_error ("fdopen %s failed: %s\n", name, g_strerror (errno));
return NULL;
}
fh->type = WRITE_TYPE;
fh->name = name;
edsio_md5_init (&fh->ctx);
return fh;
}
static gint
xd_begin_compression (XdFileHandle* fh)
{
gint filepos, nfd;
if (compress_level == 0)
return fh->real_length;
if (! (fh->out_close) (fh))
{
xd_error ("fclose failed: %s\n", g_strerror (errno));
return -1;
}
filepos = lseek (fh->out_fd, 0, SEEK_END);
if (filepos < 0)
{
xd_error ("lseek failed: %s\n", g_strerror (errno));
return -1;
}
g_assert (fh->out_fd >= 0);
nfd = dup (fh->out_fd);
fh->out = gzdopen (nfd, "wb");
fh->out_write = &xd_gzwrite;
fh->out_close = &xd_gzclose;
if (! fh->out)
{
xd_error ("gzdopen failed: %s\n", g_strerror (errno));
return -1;
}
if (gzsetparams(fh->out, compress_level, Z_DEFAULT_STRATEGY) != Z_OK)
{
int foo;
xd_error ("gzsetparams failed: %s\n", gzerror (fh->out, &foo));
return -1;
}
return filepos;
}
static gboolean
xd_end_compression (XdFileHandle* fh)
{
if (compress_level == 0)
return TRUE;
if (! (fh->out_close) (fh))
{
xd_error ("fdclose failed: %s\n", g_strerror (errno));
return FALSE;
}
if (lseek (fh->out_fd, 0, SEEK_END) < 0)
{
xd_error ("lseek failed: %s\n", g_strerror (errno));
return FALSE;
}
g_assert (fh->out_fd >= 0);
fh->out = fdopen (dup (fh->out_fd), FOPEN_WRITE_ARG);
fh->out_write = &xd_fwrite;
fh->out_close = &xd_fclose;
if (! fh->out)
{
xd_error ("fdopen failed: %s\n", g_strerror (errno));
return FALSE;
}
return TRUE;
}
static gssize
xd_handle_length (XdFileHandle *fh)
{
if (fh->in_compressed)
return fh->current_pos;
else
return fh->narrow_high - fh->narrow_low;
}
static gssize
xd_handle_pages (XdFileHandle *fh)
{
g_assert (fh->type & READ_TYPE);
return xd_handle_length (fh) / XD_PAGE_SIZE;
}
static gssize
xd_handle_pagesize (XdFileHandle *fh)
{
g_assert (fh->type & READ_TYPE);
return XD_PAGE_SIZE;
}
static gint
on_page (XdFileHandle* fh, guint pgno)
{
if (pgno > xd_handle_pages (fh))
return -1;
if (pgno == xd_handle_pages (fh))
return xd_handle_length (fh) % XD_PAGE_SIZE;
return XD_PAGE_SIZE;
}
static gboolean
xd_handle_close (XdFileHandle *fh, gint ignore)
{
/* this is really a reset for writable files */
if (fh->type == WRITE_TYPE)
{
if (fh->reset_length_next_write)
{
fh->reset_length_next_write = FALSE;
fh->length = 0;
fh->narrow_high = 0;
}
fh->reset_length_next_write = TRUE;
edsio_md5_final (fh->md5, &fh->ctx);
edsio_md5_init (&fh->ctx);
}
else if (fh->in)
{
edsio_md5_final (fh->md5, &fh->ctx);
edsio_md5_init (&fh->ctx);
fh->md5_good = FALSE;
}
return TRUE;
}
static const guint8*
xd_handle_checksum_md5 (XdFileHandle *fh)
{
if (fh->in && ! fh->md5_good)
{
edsio_md5_final (fh->md5, &fh->ctx);
fh->md5_good = TRUE;
}
else if (fh->type != WRITE_TYPE && !fh->in)
{
const guint8* page;
while (fh->md5_page <= xd_handle_pages (fh))
{
gint pgno = fh->md5_page;
gint onpage;
if ((onpage = xd_handle_map_page (fh, pgno, &page)) < 0)
return NULL;
if (pgno == fh->md5_page)
{
fh->md5_page += 1;
edsio_md5_update (&fh->ctx, page, onpage);
if (fh->md5_page > xd_handle_pages (fh))
edsio_md5_final (fh->md5, &fh->ctx);
}
if (! xd_handle_unmap_page (fh, pgno, &page))
return NULL;
}
}
return g_memdup (fh->md5, 16);
}
static gboolean
xd_handle_set_pos (XdFileHandle *fh, guint pos)
{
if (fh->current_pos == pos + fh->narrow_low)
return TRUE;
if (pos + fh->narrow_low > fh->narrow_high)
{
xd_error ("unexpected EOF in %s\n", fh->name);
return FALSE;
}
fh->current_pos = pos + fh->narrow_low;
if (fseek (fh->in, fh->current_pos, SEEK_SET))
{
xd_error ("fseek failed: %s\n", g_strerror (errno));
return FALSE;
}
return TRUE;
}
static gboolean
xd_handle_narrow (XdFileHandle* fh, guint low, guint high, gboolean compressed)
{
if (high > fh->length)
{
xd_error ("%s: corrupt or truncated delta\n", fh->name);
return FALSE;
}
fh->narrow_low = low;
fh->narrow_high = high;
edsio_md5_init (&fh->ctx);
if (compressed)
{