-
Notifications
You must be signed in to change notification settings - Fork 1
/
zio.c
2065 lines (1783 loc) · 49.1 KB
/
zio.c
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
/* -*- C -*-
* Copyright (c) 1998-2006 Motoyuki Kasahara
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. Neither the name of the project nor the names of its contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*/
#include <stdio.h>
#include <sys/types.h>
#include <errno.h>
#include <string.h>
#include <stdlib.h>
#include <limits.h>
#include "custom_unistd.h"
#include <fcntl.h>
#ifdef ENABLE_PTHREAD
#include <pthread.h>
#endif
#include <zlib.h>
#include "zio.h"
#include <locale.h>
/*
* Flags for open().
*/
#ifndef O_BINARY
#define O_BINARY 0
#endif
#if defined( _WIN32 )
#include <fcntl.h>
#include <windows.h>
#endif
/*
* The maximum length of path name.
*/
#ifndef PATH_MAX
#ifdef MAXPATHLEN
#define PATH_MAX MAXPATHLEN
#else /* not MAXPATHLEN */
#define PATH_MAX 1024
#endif /* not MAXPATHLEN */
#endif /* not PATH_MAX */
/*
* Mutual exclusion lock of Pthreads.
*/
#ifndef ENABLE_PTHREAD
#define pthread_mutex_lock(m)
#define pthread_mutex_unlock(m)
#endif
/*
* Debug message handler.
*/
extern int eb_log_flag;
extern void eb_log(const char *, ...);
#define LOG(x) do {if (eb_log_flag) eb_log x;} while (0)
/*
* Get an unsigned value from an octet stream buffer.
*/
#define zio_uint1(p) (*(const unsigned char *)(p))
#define zio_uint2(p) ((*(const unsigned char *)(p) << 8) \
+ (*(const unsigned char *)((p) + 1)))
#define zio_uint3(p) ((*(const unsigned char *)(p) << 16) \
+ (*(const unsigned char *)((p) + 1) << 8) \
+ (*(const unsigned char *)((p) + 2)))
#define zio_uint4(p) (((off_t) *(const unsigned char *)(p) << 24) \
+ (*(const unsigned char *)((p) + 1) << 16) \
+ (*(const unsigned char *)((p) + 2) << 8) \
+ (*(const unsigned char *)((p) + 3)))
#define zio_uint5(p) (((off_t) (*(const unsigned char *)(p)) << 32) \
+ ((off_t) (*(const unsigned char *)((p) + 1)) << 24) \
+ (*(const unsigned char *)((p) + 2) << 16) \
+ (*(const unsigned char *)((p) + 3) << 8) \
+ (*(const unsigned char *)((p) + 4)))
/*
* Test whether the path is URL with the `ebnet' scheme.
*/
#define is_ebnet_url(p) \
( ((p)[0] == 'E' || (p)[0] == 'e') \
&& ((p)[1] == 'B' || (p)[1] == 'b') \
&& ((p)[2] == 'N' || (p)[2] == 'n') \
&& ((p)[3] == 'E' || (p)[3] == 'e') \
&& ((p)[4] == 'T' || (p)[4] == 't') \
&& (p)[5] == ':' && (p)[6] == '/' && (p)[7] == '/')
/*
* Size of a page (The term `page' means `block' in JIS X 4081).
*/
#define ZIO_SIZE_PAGE 2048
/*
* Size of a cache buffer.
* It must be large enough to memory an uncompressed slice.
*
* (In EBZIP and EPWING compressions, the size of uncompressed slice
* is 2048. In S-EBXA compression, the size is 4096.)
*/
#define ZIO_CACHE_BUFFER_SIZE (ZIO_SIZE_PAGE << ZIO_MAX_EBZIP_LEVEL)
/*
* NULL Zio ID.
*/
#define ZIO_ID_NONE -1
/*
* Buffer for caching uncompressed data.
*/
static char *cache_buffer = NULL;
/*
* Zio ID which caches data in `cache_buffer'.
*/
static int cache_zio_id = ZIO_ID_NONE;
/*
* Offset of the beginning of the cached data `cache_buffer'.
*/
static off_t cache_location;
/*
* Zio object counter.
*/
static int zio_counter = 0;
/*
* Mutex for cache variables.
*/
#ifdef ENABLE_PTHREAD
static pthread_mutex_t zio_mutex = PTHREAD_MUTEX_INITIALIZER;
#endif
/*
* Test whether `off_t' represents a large integer.
*/
#define off_t_is_large \
((((off_t) 1 << 41) + ((off_t) 1 << 40) + 1) % 9999991 == 7852006)
/*
* Unexported function.
*/
static int zio_reopen(Zio *zio, const char *file_name);
static int zio_open_plain(Zio *zio, const char *file_name);
static int zio_open_ebzip(Zio *zio, const char *file_name);
static int zio_open_epwing(Zio *zio, const char *file_name);
static int zio_open_epwing6(Zio *zio, const char *file_name);
static int zio_make_epwing_huffman_tree(Zio *zio, int leaf_count);
static ssize_t zio_read_ebzip(Zio *zio, char *buffer, size_t length);
static ssize_t zio_read_epwing(Zio *zio, char *buffer, size_t length);
static ssize_t zio_read_sebxa(Zio *zio, char *buffer, size_t length);
static int zio_unzip_slice_ebzip1(Zio *zio, char *out_buffer,
size_t zipped_slice_size);
static int zio_unzip_slice_epwing(Zio *zio, char *out_buffer);
static int zio_unzip_slice_epwing6(Zio *zio, char *out_buffer);
static int zio_unzip_slice_sebxa(Zio *zio, char *out_buffer);
static int zio_open_raw(Zio *zio, const char *file_name);
static void zio_close_raw(Zio *zio);
static off_t zio_lseek_raw(Zio *zio, off_t offset, int whence);
static ssize_t zio_read_raw(Zio *zio, void *buffer, size_t length);
/*
* Initialize cache buffer.
*/
int
zio_initialize_library(void)
{
pthread_mutex_lock(&zio_mutex);
LOG(("in: zio_initialize_library()"));
/*
* Allocate memory for cache buffer.
*/
if (cache_buffer == NULL) {
cache_buffer = (char *) malloc(ZIO_CACHE_BUFFER_SIZE);
if (cache_buffer == NULL)
goto failed;
}
LOG(("out: zio_initialize_library() = %d", 0));
pthread_mutex_unlock(&zio_mutex);
return 0;
/*
* An error occurs...
*/
failed:
LOG(("out: zio_initialize_library() = %d", -1));
pthread_mutex_unlock(&zio_mutex);
return -1;
}
/*
* Clear cache buffer.
*/
void
zio_finalize_library(void)
{
pthread_mutex_lock(&zio_mutex);
LOG(("in: zio_finalize_library()"));
if (cache_buffer != NULL)
free(cache_buffer);
cache_buffer = NULL;
cache_zio_id = ZIO_ID_NONE;
LOG(("out: zio_finalize_library()"));
pthread_mutex_unlock(&zio_mutex);
}
/*
* Initialize `zio'.
*/
void
zio_initialize(Zio *zio)
{
LOG(("in: zio_initialize()"));
zio->id = -1;
zio->file = -1;
zio->huffman_nodes = NULL;
zio->huffman_root = NULL;
zio->code = ZIO_INVALID;
zio->file_size = 0;
zio->is_ebnet = 0;
LOG(("out: zio_initialize()"));
}
/*
* Finalize `zio'.
*/
void
zio_finalize(Zio *zio)
{
LOG(("in: zio_finalize(zio=%d)", (int)zio->id));
zio_close(zio);
if (zio->huffman_nodes != NULL)
free(zio->huffman_nodes);
zio->id = -1;
zio->huffman_nodes = NULL;
zio->huffman_root = NULL;
zio->code = ZIO_INVALID;
LOG(("out: zio_finalize()"));
}
/*
* Set S-EBXA compression mode.
*/
int
zio_set_sebxa_mode(Zio *zio, off_t index_location, off_t index_base,
off_t zio_start_location, off_t zio_end_location)
{
LOG(("in: zio_set_sebxa_mode(zio=%d, index_location=%ld, index_base=%ld, \
zio_start_location=%ld, zio_end_location=%ld)",
(int)zio->id, (long)index_location, (long)index_base,
(long)zio_start_location, (long)zio_end_location));
if (zio->code != ZIO_PLAIN)
goto failed;
zio->code = ZIO_SEBXA;
zio->index_location = index_location;
zio->index_base = index_base;
zio->zio_start_location = zio_start_location;
zio->zio_end_location = zio_end_location;
zio->file_size = zio_end_location;
LOG(("out: zio_set_sebxa_mode() = %d", 0));
return 0;
/*
* An error occurs...
*/
failed:
LOG(("out: zio_set_sebxa_mode() = %d", -1));
return -1;
}
/*
* Open `file'.
*/
int
zio_open(Zio *zio, const char *file_name, Zio_Code zio_code)
{
int result;
LOG(("in: zio_open(zio=%d, file_name=%s, zio_code=%d)",
(int)zio->id, file_name, zio_code));
if (0 <= zio->file) {
if (zio_code == ZIO_REOPEN) {
result = 0;
goto succeeded;
}
zio_finalize(zio);
zio_initialize(zio);
}
switch (zio_code) {
case ZIO_REOPEN:
result = zio_reopen(zio, file_name);
break;
case ZIO_PLAIN:
result = zio_open_plain(zio, file_name);
break;
case ZIO_EBZIP1:
result = zio_open_ebzip(zio, file_name);
break;
case ZIO_EPWING:
result = zio_open_epwing(zio, file_name);
break;
case ZIO_EPWING6:
result = zio_open_epwing6(zio, file_name);
break;
case ZIO_SEBXA:
result = zio_open_plain(zio, file_name);
break;
default:
result = -1;
}
succeeded:
LOG(("out: zio_open() = %d", result));
return result;
}
/*
* Reopen a file.
*/
static int
zio_reopen(Zio *zio, const char *file_name)
{
LOG(("in: zio_reopen(zio=%d, file_name=%s)", (int)zio->id, file_name));
if (zio->code == ZIO_INVALID)
goto failed;
if (zio_open_raw(zio, file_name) < 0) {
zio->code = ZIO_INVALID;
goto failed;
}
zio->location = 0;
LOG(("out: zio_reopen() = %d", zio->file));
return zio->file;
/*
* An error occurs...
*/
failed:
LOG(("out: zio_reopen() = %d", -1));
return -1;
}
/*
* Open an non-compressed file.
*/
static int
zio_open_plain(Zio *zio, const char *file_name)
{
LOG(("in: zio_open_plain(zio=%d, file_name=%s)", (int)zio->id, file_name));
if (zio_open_raw(zio, file_name) < 0)
goto failed;
zio->code = ZIO_PLAIN;
zio->slice_size = ZIO_SIZE_PAGE;
zio->file_size = zio_lseek_raw(zio, 0, SEEK_END);
if (zio->file_size < 0 || zio_lseek_raw(zio, 0, SEEK_SET) < 0)
goto failed;
/*
* Assign ID.
*/
pthread_mutex_lock(&zio_mutex);
zio->id = zio_counter++;
pthread_mutex_unlock(&zio_mutex);
LOG(("out: zio_open_plain(zio=%d) = %d", (int)zio->id, zio->file));
return zio->file;
/*
* An error occurs...
*/
failed:
if (0 <= zio->file)
zio_close_raw(zio);
zio->file = -1;
zio->code = ZIO_INVALID;
LOG(("out: zio_open_plain() = %d", -1));
return -1;
}
/*
* Open an EBZIP compression file.
*/
static int
zio_open_ebzip(Zio *zio, const char *file_name)
{
char header[ZIO_SIZE_EBZIP_HEADER];
int ebzip_mode;
LOG(("in: zio_open_ebzip(zio=%d, file_name=%s)", (int)zio->id, file_name));
/*
* Try to open a `*.ebz' file.
*/
if (zio_open_raw(zio, file_name) < 0)
goto failed;
/*
* Read header part of the ebzip'ped file.
*/
if (zio_read_raw(zio, header, ZIO_SIZE_EBZIP_HEADER)
!= ZIO_SIZE_EBZIP_HEADER)
goto failed;
ebzip_mode = zio_uint1(header + 5) >> 4;
zio->code = ZIO_EBZIP1;
zio->zip_level = zio_uint1(header + 5) & 0x0f;
zio->slice_size = ZIO_SIZE_PAGE << zio->zip_level;
zio->file_size = zio_uint5(header + 9);
zio->crc = zio_uint4(header + 14);
zio->mtime = zio_uint4(header + 18);
zio->location = 0;
if (zio->file_size < (off_t) 1 << 16)
zio->index_width = 2;
else if (zio->file_size < (off_t) 1 << 24)
zio->index_width = 3;
else if (zio->file_size < (off_t) 1 << 32 || !off_t_is_large)
zio->index_width = 4;
else
zio->index_width = 5;
/*
* Check zio header information.
*/
if (memcmp(header, "EBZip", 5) != 0
|| ZIO_SIZE_PAGE << ZIO_MAX_EBZIP_LEVEL < zio->slice_size)
goto failed;
if (off_t_is_large) {
if (ebzip_mode != 1 && ebzip_mode != 2)
goto failed;
} else {
if (ebzip_mode != 1)
goto failed;
}
/*
* Assign ID.
*/
pthread_mutex_lock(&zio_mutex);
zio->id = zio_counter++;
pthread_mutex_unlock(&zio_mutex);
LOG(("out: zio_open_ebzip(zio=%d) = %d", (int)zio->id, zio->file));
return zio->file;
/*
* An error occurs...
*/
failed:
if (0 <= zio->file)
zio_close_raw(zio);
zio->file = -1;
zio->code = ZIO_INVALID;
LOG(("out: zio_open_ebzip() = %d", -1));
return -1;
}
/*
* The buffer size must be 512 bytes, the number of 8 bit nodes.
*/
#define ZIO_EPWING_BUFFER_SIZE 512
/*
* Open an EPWING compression file.
*/
static int
zio_open_epwing(Zio *zio, const char *file_name)
{
int leaf16_count;
int leaf_count;
char buffer[ZIO_EPWING_BUFFER_SIZE];
char *buffer_p;
ssize_t read_length;
Zio_Huffman_Node *tail_node_p;
int i;
LOG(("in: zio_open_epwing(zio=%d, file_name=%s)", (int)zio->id,
file_name));
zio->code = ZIO_EPWING;
zio->huffman_nodes = NULL;
/*
* Open `HONMON2'.
*/
if (zio_open_raw(zio, file_name) < 0)
goto failed;
/*
* Read a header of `HONMON2' (32 bytes).
* When `frequencies_length' is shorter than 512, we assumes the
* file is broken.
*/
if (zio_read_raw(zio, buffer, 32) != 32)
goto failed;
zio->location = 0;
zio->slice_size = ZIO_SIZE_PAGE;
zio->index_location = zio_uint4(buffer);
zio->index_length = zio_uint4(buffer + 4);
zio->frequencies_location = zio_uint4(buffer + 8);
zio->frequencies_length = zio_uint4(buffer + 12);
leaf16_count = (zio->frequencies_length - (256 * 2)) / 4;
leaf_count = leaf16_count + 256 + 1;
if (zio->index_length < 36 || zio->frequencies_length < 512)
goto failed;
/*
* Check for the length of an uncompressed file.
*
* If the index of the non-first page in the last index group
* is 0x0000, we assumes the data corresponding with the index
* doesn't exist.
*/
if (zio_lseek_raw(zio, zio->index_location
+ ((off_t) zio->index_length - 36) / 36 * 36, SEEK_SET) < 0)
goto failed;
if (zio_read_raw(zio, buffer, 36) != 36)
goto failed;
zio->file_size = ((off_t) zio->index_length / 36) * (ZIO_SIZE_PAGE * 16);
for (i = 1, buffer_p = buffer + 4 + 2; i < 16; i++, buffer_p += 2) {
if (zio_uint2(buffer_p) == 0)
break;
}
zio->file_size -= ZIO_SIZE_PAGE * (16 - i);
/*
* Allocate memory for huffman nodes.
*/
zio->huffman_nodes = (Zio_Huffman_Node *) malloc(sizeof(Zio_Huffman_Node)
* leaf_count * 2);
if (zio->huffman_nodes == NULL)
goto failed;
tail_node_p = zio->huffman_nodes;
/*
* Make leafs for 16bit character.
*/
read_length = ZIO_EPWING_BUFFER_SIZE - (ZIO_EPWING_BUFFER_SIZE % 4);
if (zio_lseek_raw(zio, zio->frequencies_location, SEEK_SET) < 0)
goto failed;
if (zio_read_raw(zio, buffer, read_length) != read_length)
goto failed;
buffer_p = buffer;
for (i = 0; i < leaf16_count; i++) {
if (buffer + read_length <= buffer_p) {
if (zio_read_raw(zio, buffer, read_length) != read_length)
goto failed;
buffer_p = buffer;
}
tail_node_p->type = ZIO_HUFFMAN_NODE_LEAF16;
tail_node_p->value = zio_uint2(buffer_p);
tail_node_p->frequency = zio_uint2(buffer_p + 2);
tail_node_p->left = NULL;
tail_node_p->right = NULL;
buffer_p += 4;
tail_node_p++;
}
/*
* Make leafs for 8bit character.
*/
if (zio_lseek_raw(zio, zio->frequencies_location + leaf16_count * 4,
SEEK_SET) < 0)
goto failed;
if (zio_read_raw(zio, buffer, 512) != 512)
goto failed;
buffer_p = buffer;
for (i = 0; i < 256; i++) {
tail_node_p->type = ZIO_HUFFMAN_NODE_LEAF8;
tail_node_p->value = i;
tail_node_p->frequency = zio_uint2(buffer_p);
tail_node_p->left = NULL;
tail_node_p->right = NULL;
buffer_p += 2;
tail_node_p++;
}
/*
* Make a leaf for the end-of-page character.
*/
tail_node_p->type = ZIO_HUFFMAN_NODE_EOF;
tail_node_p->value = 256;
tail_node_p->frequency = 1;
tail_node_p++;
/*
* Make a huffman tree.
*/
if (zio_make_epwing_huffman_tree(zio, leaf_count) < 0)
goto failed;
/*
* Assign ID.
*/
pthread_mutex_lock(&zio_mutex);
zio->id = zio_counter++;
pthread_mutex_unlock(&zio_mutex);
LOG(("out: zio_open_epwing(zio=%d) = %d", (int)zio->id, zio->file));
return zio->file;
/*
* An error occurs...
*/
failed:
if (0 <= zio->file)
zio_close_raw(zio);
if (zio->huffman_nodes != NULL)
free(zio->huffman_nodes);
zio->file = -1;
zio->huffman_nodes = NULL;
zio->huffman_root = NULL;
zio->code = ZIO_INVALID;
LOG(("out: zio_open_epwing() = %d", -1));
return -1;
}
/*
* Open an EPWING compression file.
*/
static int
zio_open_epwing6(Zio *zio, const char *file_name)
{
int leaf32_count;
int leaf16_count;
int leaf_count;
char buffer[ZIO_EPWING_BUFFER_SIZE];
char *buffer_p;
ssize_t read_length;
Zio_Huffman_Node *tail_node_p;
int i;
LOG(("in: zio_open_epwing6(zio=%d, file_name=%s)", (int)zio->id,
file_name));
zio->code = ZIO_EPWING6;
zio->huffman_nodes = NULL;
/*
* Open `HONMON2'.
*/
if (zio_open_raw(zio, file_name) < 0)
goto failed;
/*
* Read a header of `HONMON2' (48 bytes).
* When `frequencies_length' is shorter than 512, we assumes the
* file is broken.
*/
if (zio_read_raw(zio, buffer, 48) != 48)
goto failed;
zio->location = 0;
zio->slice_size = ZIO_SIZE_PAGE;
zio->index_location = zio_uint4(buffer);
zio->index_length = zio_uint4(buffer + 4);
zio->frequencies_location = zio_uint4(buffer + 8);
zio->frequencies_length = zio_uint4(buffer + 12);
leaf16_count = 0x400;
leaf32_count = (zio->frequencies_length - (leaf16_count * 4) - (256 * 2))
/ 6;
leaf_count = leaf32_count + leaf16_count + 256 + 1;
if (zio->index_length < 36 || zio->frequencies_length < 512)
goto failed;
/*
* Check for the length of an uncompressed file.
*
* If the index of the non-first page in the last index group
* is 0x0000, we assumes the data corresponding with the index
* doesn't exist.
*/
if (zio_lseek_raw(zio, zio->index_location
+ ((off_t) zio->index_length - 36) / 36 * 36, SEEK_SET) < 0)
goto failed;
if (zio_read_raw(zio, buffer, 36) != 36)
goto failed;
zio->file_size = ((off_t) zio->index_length / 36) * (ZIO_SIZE_PAGE * 16);
for (i = 1, buffer_p = buffer + 4 + 2; i < 16; i++, buffer_p += 2) {
if (zio_uint2(buffer_p) == 0)
break;
}
zio->file_size -= ZIO_SIZE_PAGE * (16 - i);
/*
* Allocate memory for huffman nodes.
*/
zio->huffman_nodes = (Zio_Huffman_Node *) malloc(sizeof(Zio_Huffman_Node)
* leaf_count * 2);
if (zio->huffman_nodes == NULL)
goto failed;
tail_node_p = zio->huffman_nodes;
/*
* Make leafs for 32bit character.
*/
read_length = ZIO_EPWING_BUFFER_SIZE - (ZIO_EPWING_BUFFER_SIZE % 6);
if (zio_lseek_raw(zio, zio->frequencies_location, SEEK_SET) < 0)
goto failed;
if (zio_read_raw(zio, buffer, read_length) != read_length)
goto failed;
buffer_p = buffer;
for (i = 0; i < leaf32_count; i++) {
if (buffer + read_length <= buffer_p) {
if (zio_read_raw(zio, buffer, read_length) != read_length)
goto failed;
buffer_p = buffer;
}
tail_node_p->type = ZIO_HUFFMAN_NODE_LEAF32;
tail_node_p->value = zio_uint4(buffer_p);
tail_node_p->frequency = zio_uint2(buffer_p + 4);
tail_node_p->left = NULL;
tail_node_p->right = NULL;
buffer_p += 6;
tail_node_p++;
}
/*
* Make leafs for 16bit character.
*/
read_length = ZIO_EPWING_BUFFER_SIZE - (ZIO_EPWING_BUFFER_SIZE % 4);
if (zio_lseek_raw(zio, zio->frequencies_location + leaf32_count * 6,
SEEK_SET) < 0)
goto failed;
if (zio_read_raw(zio, buffer, read_length) != read_length)
goto failed;
buffer_p = buffer;
for (i = 0; i < leaf16_count; i++) {
if (buffer + read_length <= buffer_p) {
if (zio_read_raw(zio, buffer, read_length) != read_length)
goto failed;
buffer_p = buffer;
}
tail_node_p->type = ZIO_HUFFMAN_NODE_LEAF16;
tail_node_p->value = zio_uint2(buffer_p);
tail_node_p->frequency = zio_uint2(buffer_p + 2);
tail_node_p->left = NULL;
tail_node_p->right = NULL;
buffer_p += 4;
tail_node_p++;
}
/*
* Make leafs for 8bit character.
*/
if (zio_lseek_raw(zio, zio->frequencies_location + leaf32_count * 6
+ leaf16_count * 4, SEEK_SET) < 0)
goto failed;
if (zio_read_raw(zio, buffer, 512) != 512)
goto failed;
buffer_p = buffer;
for (i = 0; i < 256; i++) {
tail_node_p->type = ZIO_HUFFMAN_NODE_LEAF8;
tail_node_p->value = i;
tail_node_p->frequency = zio_uint2(buffer_p);
tail_node_p->left = NULL;
tail_node_p->right = NULL;
buffer_p += 2;
tail_node_p++;
}
/*
* Make a leaf for the end-of-page character.
*/
tail_node_p->type = ZIO_HUFFMAN_NODE_EOF;
tail_node_p->value = 256;
tail_node_p->frequency = 1;
tail_node_p++;
/*
* Make a huffman tree.
*/
if (zio_make_epwing_huffman_tree(zio, leaf_count) < 0)
goto failed;
/*
* Assign ID.
*/
pthread_mutex_lock(&zio_mutex);
zio->id = zio_counter++;
pthread_mutex_unlock(&zio_mutex);
LOG(("out: zio_open_epwing6(zio=%d) = %d", (int)zio->id, zio->file));
return zio->file;
/*
* An error occurs...
*/
failed:
if (0 <= zio->file)
zio_close_raw(zio);
if (zio->huffman_nodes != NULL)
free(zio->huffman_nodes);
zio->file = -1;
zio->huffman_nodes = NULL;
zio->huffman_root = NULL;
zio->code = ZIO_INVALID;
LOG(("out: zio_open_epwing6() = %d", -1));
return -1;
}
/*
* Make a huffman tree for decompressing EPWING compression data.
*/
static int
zio_make_epwing_huffman_tree(Zio *zio, int leaf_count)
{
Zio_Huffman_Node *target_node;
Zio_Huffman_Node *most_node;
Zio_Huffman_Node *node_p;
Zio_Huffman_Node temporary_node;
Zio_Huffman_Node *least_node_p;
Zio_Huffman_Node *tail_node_p;
int i;
int j;
LOG(("in: zio_make_epwing_huffman_tree(zio=%d, leaf_count=%d)",
(int)zio->id, leaf_count));
tail_node_p = zio->huffman_nodes + leaf_count;
/*
* Sort the leaf nodes in frequency order.
*/
for (i = 0; i < leaf_count - 1; i++) {
target_node = zio->huffman_nodes + i;
most_node = target_node;
node_p = zio->huffman_nodes + i + 1;
for (j = i + 1; j < leaf_count; j++) {
if (most_node->frequency < node_p->frequency)
most_node = node_p;
node_p++;
}
temporary_node.type = most_node->type;
temporary_node.value = most_node->value;
temporary_node.frequency = most_node->frequency;
most_node->type = target_node->type;
most_node->value = target_node->value;
most_node->frequency = target_node->frequency;
target_node->type = temporary_node.type;
target_node->value = temporary_node.value;
target_node->frequency = temporary_node.frequency;
}
/*
* Make intermediate nodes of the huffman tree.
* The number of intermediate nodes of the tree is <the number of
* leaf nodes> - 1.
*/
for (i = 1; i < leaf_count; i++) {
/*
* Initialize a new intermediate node.
*/
tail_node_p->type = ZIO_HUFFMAN_NODE_INTERMEDIATE;
tail_node_p->left = NULL;
tail_node_p->right = NULL;
/*
* Find for a least frequent node.
* That node becomes a left child of the new intermediate node.
*/
least_node_p = NULL;
for (node_p = zio->huffman_nodes; node_p < tail_node_p; node_p++) {
if (node_p->frequency == 0)
continue;
if (least_node_p == NULL
|| node_p->frequency <= least_node_p->frequency)
least_node_p = node_p;
}
if (least_node_p == NULL)
goto failed;
tail_node_p->left = least_node_p;
tail_node_p->frequency = least_node_p->frequency;
least_node_p->frequency = 0;
/*
* Find for a next least frequent node.
* That node becomes a right child of the new intermediate node.
*/
least_node_p = NULL;
for (node_p = zio->huffman_nodes; node_p < tail_node_p; node_p++) {
if (node_p->frequency == 0)
continue;
if (least_node_p == NULL
|| node_p->frequency <= least_node_p->frequency)
least_node_p = node_p;
}
if (least_node_p == NULL)
goto failed;
tail_node_p->right = least_node_p;
tail_node_p->frequency += least_node_p->frequency;
least_node_p->frequency = 0;
tail_node_p++;
}
/*
* Set a root node of the huffman tree.
*/
zio->huffman_root = tail_node_p - 1;
LOG(("out: zio_make_epwing_huffman_tree() = %d", 0));
return 0;
/*
* An error occurs...
*/
failed:
LOG(("out: zio_make_epwing_huffman_tree() = %d", -1));
return -1;
}
/*
* Close `zio'.
*/
void
zio_close(Zio *zio)
{
pthread_mutex_lock(&zio_mutex);
LOG(("in: zio_close(zio=%d)", (int)zio->id));
/*
* If contents of the file is cached, clear the cache.
*/
if (0 <= zio->file)
zio_close_raw(zio);
zio->file = -1;