-
-
Notifications
You must be signed in to change notification settings - Fork 2.6k
/
Copy pathcpl_vsil_gzip.cpp
5350 lines (4620 loc) · 175 KB
/
cpl_vsil_gzip.cpp
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
/******************************************************************************
*
* Project: CPL - Common Portability Library
* Purpose: Implement VSI large file api for gz/zip files (.gz and .zip).
* Author: Even Rouault, even.rouault at spatialys.com
*
******************************************************************************
* Copyright (c) 2008-2014, Even Rouault <even dot rouault at spatialys.com>
*
* SPDX-License-Identifier: MIT
****************************************************************************/
//! @cond Doxygen_Suppress
/* gzio.c -- IO on .gz files
Copyright (C) 1995-2005 Jean-loup Gailly.
This software is provided 'as-is', without any express or implied
warranty. In no event will the authors be held liable for any damages
arising from the use of this software.
Permission is granted to anyone to use this software for any purpose,
including commercial applications, and to alter it and redistribute it
freely, subject to the following restrictions:
1. The origin of this software must not be misrepresented; you must not
claim that you wrote the original software. If you use this software
in a product, an acknowledgment in the product documentation would be
appreciated but is not required.
2. Altered source versions must be plainly marked as such, and must not be
misrepresented as being the original software.
3. This notice may not be removed or altered from any source distribution.
Jean-loup Gailly Mark Adler
jloup@gzip.org madler@alumni.caltech.edu
The data format used by the zlib library is described by RFCs (Request for
Comments) 1950 to 1952 in the files http://www.ietf.org/rfc/rfc1950.txt
(zlib format), rfc1951.txt (deflate format) and rfc1952.txt (gzip format).
*/
/* This file contains a refactoring of gzio.c from zlib project.
It replaces classical calls operating on FILE* by calls to the VSI large file
API. It also adds the capability to seek at the end of the file, which is not
implemented in original gzSeek. It also implements a concept of in-memory
"snapshots", that are a way of improving efficiency while seeking GZip
files. Snapshots are created regularly when decompressing the data a snapshot
of the gzip state. Later we can seek directly in the compressed data to the
closest snapshot in order to reduce the amount of data to uncompress again.
For .gz files, an effort is done to cache the size of the uncompressed data
in a .gz.properties file, so that we don't need to seek at the end of the
file each time a Stat() is done.
For .zip and .gz, both reading and writing are supported, but just one mode
at a time (read-only or write-only).
*/
#include "cpl_port.h"
#include "cpl_conv.h"
#include "cpl_vsi.h"
#include <cerrno>
#include <climits>
#include <cstddef>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#if HAVE_FCNTL_H
#include <fcntl.h>
#endif
#if HAVE_SYS_STAT_H
#include <sys/stat.h>
#endif
#include "cpl_zlib_header.h" // to avoid warnings when including zlib.h
#ifdef HAVE_LIBDEFLATE
#include "libdeflate.h"
#endif
#include <algorithm>
#include <iterator>
#include <limits>
#include <list>
#include <map>
#include <memory>
#include <mutex>
#include <string>
#include <utility>
#include <vector>
#include "cpl_error.h"
#include "cpl_minizip_ioapi.h"
#include "cpl_minizip_unzip.h"
#include "cpl_multiproc.h"
#include "cpl_string.h"
#include "cpl_time.h"
#include "cpl_vsi_virtual.h"
#include "cpl_worker_thread_pool.h"
constexpr int Z_BUFSIZE = 65536; // Original size is 16384
constexpr int gz_magic[2] = {0x1f, 0x8b}; // gzip magic header
// gzip flag byte.
#define ASCII_FLAG 0x01 // bit 0 set: file probably ascii text
#define HEAD_CRC 0x02 // bit 1 set: header CRC present
#define EXTRA_FIELD 0x04 // bit 2 set: extra field present
#define ORIG_NAME 0x08 // bit 3 set: original file name present
#define COMMENT 0x10 // bit 4 set: file comment present
#define RESERVED 0xE0 // bits 5..7: reserved
#define ALLOC(size) malloc(size)
#define TRYFREE(p) \
{ \
if (p) \
free(p); \
}
#define CPL_VSIL_GZ_RETURN(ret) \
CPLError(CE_Failure, CPLE_AppDefined, "In file %s, at line %d, return %d", \
__FILE__, __LINE__, ret)
// To avoid aliasing to CopyFile to CopyFileA on Windows
#ifdef CopyFile
#undef CopyFile
#endif
// #define ENABLE_DEBUG 1
/************************************************************************/
/* ==================================================================== */
/* VSIGZipHandle */
/* ==================================================================== */
/************************************************************************/
typedef struct
{
vsi_l_offset posInBaseHandle;
z_stream stream;
uLong crc;
int transparent;
vsi_l_offset in;
vsi_l_offset out;
} GZipSnapshot;
class VSIGZipHandle final : public VSIVirtualHandle
{
VSIVirtualHandle *m_poBaseHandle = nullptr;
#ifdef DEBUG
vsi_l_offset m_offset = 0;
#endif
vsi_l_offset m_compressed_size = 0;
vsi_l_offset m_uncompressed_size = 0;
vsi_l_offset offsetEndCompressedData = 0;
uLong m_expected_crc = 0;
char *m_pszBaseFileName = nullptr; /* optional */
bool m_bWriteProperties = false;
bool m_bCanSaveInfo = false;
/* Fields from gz_stream structure */
z_stream stream;
int z_err = Z_OK; /* error code for last stream operation */
int z_eof = 0; /* set if end of input file (but not necessarily of the
uncompressed stream !) */
bool m_bEOF = false; /* EOF flag for uncompressed stream */
Byte *inbuf = nullptr; /* input buffer */
Byte *outbuf = nullptr; /* output buffer */
uLong crc = 0; /* crc32 of uncompressed data */
int m_transparent = 0; /* 1 if input file is not a .gz file */
vsi_l_offset startOff =
0; /* startOff of compressed data in file (header skipped) */
vsi_l_offset in = 0; /* bytes into deflate or inflate */
vsi_l_offset out = 0; /* bytes out of deflate or inflate */
vsi_l_offset m_nLastReadOffset = 0;
GZipSnapshot *snapshots = nullptr;
vsi_l_offset snapshot_byte_interval =
0; /* number of compressed bytes at which we create a "snapshot" */
void check_header();
int get_byte();
bool gzseek(vsi_l_offset nOffset, int nWhence);
int gzrewind();
uLong getLong();
CPL_DISALLOW_COPY_ASSIGN(VSIGZipHandle)
public:
VSIGZipHandle(VSIVirtualHandle *poBaseHandle, const char *pszBaseFileName,
vsi_l_offset offset = 0, vsi_l_offset compressed_size = 0,
vsi_l_offset uncompressed_size = 0, uLong expected_crc = 0,
int transparent = 0);
~VSIGZipHandle() override;
bool IsInitOK() const
{
return inbuf != nullptr;
}
int Seek(vsi_l_offset nOffset, int nWhence) override;
vsi_l_offset Tell() override;
size_t Read(void *pBuffer, size_t nSize, size_t nMemb) override;
size_t Write(const void *pBuffer, size_t nSize, size_t nMemb) override;
void ClearErr() override;
int Eof() override;
int Error() override;
int Flush() override;
int Close() override;
VSIGZipHandle *Duplicate();
bool CloseBaseHandle();
vsi_l_offset GetLastReadOffset()
{
return m_nLastReadOffset;
}
const char *GetBaseFileName()
{
return m_pszBaseFileName;
}
void SetUncompressedSize(vsi_l_offset nUncompressedSize)
{
m_uncompressed_size = nUncompressedSize;
}
vsi_l_offset GetUncompressedSize()
{
return m_uncompressed_size;
}
void SaveInfo_unlocked();
void UnsetCanSaveInfo()
{
m_bCanSaveInfo = false;
}
};
#ifdef ENABLE_DEFLATE64
/************************************************************************/
/* ==================================================================== */
/* VSIDeflate64Handle */
/* ==================================================================== */
/************************************************************************/
struct VSIDeflate64Snapshot
{
vsi_l_offset posInBaseHandle = 0;
z_stream stream{};
uLong crc = 0;
vsi_l_offset in = 0;
vsi_l_offset out = 0;
std::vector<GByte> extraOutput{};
bool m_bStreamEndReached = false;
};
class VSIDeflate64Handle final : public VSIVirtualHandle
{
VSIVirtualHandle *m_poBaseHandle = nullptr;
#ifdef DEBUG
vsi_l_offset m_offset = 0;
#endif
vsi_l_offset m_compressed_size = 0;
vsi_l_offset m_uncompressed_size = 0;
vsi_l_offset offsetEndCompressedData = 0;
uLong m_expected_crc = 0;
char *m_pszBaseFileName = nullptr; /* optional */
/* Fields from gz_stream structure */
z_stream stream;
int z_err = Z_OK; /* error code for last stream operation */
int z_eof = 0; /* set if end of input file (but not necessarily of the
uncompressed stream ! ) */
bool m_bEOF = false; /* EOF flag for uncompressed stream */
Byte *inbuf = nullptr; /* input buffer */
Byte *outbuf = nullptr; /* output buffer */
std::vector<GByte> extraOutput{};
bool m_bStreamEndReached = false;
uLong crc = 0; /* crc32 of uncompressed data */
vsi_l_offset startOff =
0; /* startOff of compressed data in file (header skipped) */
vsi_l_offset in = 0; /* bytes into deflate or inflate */
vsi_l_offset out = 0; /* bytes out of deflate or inflate */
std::vector<VSIDeflate64Snapshot> snapshots{};
vsi_l_offset snapshot_byte_interval =
0; /* number of compressed bytes at which we create a "snapshot" */
bool gzseek(vsi_l_offset nOffset, int nWhence);
int gzrewind();
CPL_DISALLOW_COPY_ASSIGN(VSIDeflate64Handle)
public:
VSIDeflate64Handle(VSIVirtualHandle *poBaseHandle,
const char *pszBaseFileName, vsi_l_offset offset = 0,
vsi_l_offset compressed_size = 0,
vsi_l_offset uncompressed_size = 0,
uLong expected_crc = 0);
~VSIDeflate64Handle() override;
bool IsInitOK() const
{
return inbuf != nullptr;
}
int Seek(vsi_l_offset nOffset, int nWhence) override;
vsi_l_offset Tell() override;
size_t Read(void *pBuffer, size_t nSize, size_t nMemb) override;
size_t Write(const void *pBuffer, size_t nSize, size_t nMemb) override;
void ClearErr() override;
int Eof() override;
int Error() override;
int Flush() override;
int Close() override;
VSIDeflate64Handle *Duplicate();
bool CloseBaseHandle();
const char *GetBaseFileName()
{
return m_pszBaseFileName;
}
void SetUncompressedSize(vsi_l_offset nUncompressedSize)
{
m_uncompressed_size = nUncompressedSize;
}
vsi_l_offset GetUncompressedSize()
{
return m_uncompressed_size;
}
};
#endif
class VSIGZipFilesystemHandler final : public VSIFilesystemHandler
{
CPL_DISALLOW_COPY_ASSIGN(VSIGZipFilesystemHandler)
CPLMutex *hMutex = nullptr;
VSIGZipHandle *poHandleLastGZipFile = nullptr;
bool m_bInSaveInfo = false;
public:
VSIGZipFilesystemHandler() = default;
~VSIGZipFilesystemHandler() override;
VSIVirtualHandle *Open(const char *pszFilename, const char *pszAccess,
bool bSetError,
CSLConstList /* papszOptions */) override;
VSIGZipHandle *OpenGZipReadOnly(const char *pszFilename,
const char *pszAccess);
int Stat(const char *pszFilename, VSIStatBufL *pStatBuf,
int nFlags) override;
int Unlink(const char *pszFilename) override;
int Rename(const char *oldpath, const char *newpath) override;
int Mkdir(const char *pszDirname, long nMode) override;
int Rmdir(const char *pszDirname) override;
char **ReadDirEx(const char *pszDirname, int nMaxFiles) override;
const char *GetOptions() override;
virtual bool SupportsSequentialWrite(const char *pszPath,
bool bAllowLocalTempFile) override;
virtual bool SupportsRandomWrite(const char * /* pszPath */,
bool /* bAllowLocalTempFile */) override
{
return false;
}
void SaveInfo(VSIGZipHandle *poHandle);
void SaveInfo_unlocked(VSIGZipHandle *poHandle);
};
/************************************************************************/
/* Duplicate() */
/************************************************************************/
VSIGZipHandle *VSIGZipHandle::Duplicate()
{
CPLAssert(m_offset == 0);
CPLAssert(m_compressed_size != 0);
CPLAssert(m_pszBaseFileName != nullptr);
VSIFilesystemHandler *poFSHandler =
VSIFileManager::GetHandler(m_pszBaseFileName);
VSIVirtualHandle *poNewBaseHandle =
poFSHandler->Open(m_pszBaseFileName, "rb");
if (poNewBaseHandle == nullptr)
return nullptr;
VSIGZipHandle *poHandle =
new VSIGZipHandle(poNewBaseHandle, m_pszBaseFileName, 0,
m_compressed_size, m_uncompressed_size);
if (!(poHandle->IsInitOK()))
{
delete poHandle;
return nullptr;
}
poHandle->m_nLastReadOffset = m_nLastReadOffset;
// Most important: duplicate the snapshots!
for (unsigned int i = 0; i < m_compressed_size / snapshot_byte_interval + 1;
i++)
{
if (snapshots[i].posInBaseHandle == 0)
break;
poHandle->snapshots[i].posInBaseHandle = snapshots[i].posInBaseHandle;
inflateCopy(&poHandle->snapshots[i].stream, &snapshots[i].stream);
poHandle->snapshots[i].crc = snapshots[i].crc;
poHandle->snapshots[i].transparent = snapshots[i].transparent;
poHandle->snapshots[i].in = snapshots[i].in;
poHandle->snapshots[i].out = snapshots[i].out;
}
return poHandle;
}
/************************************************************************/
/* CloseBaseHandle() */
/************************************************************************/
bool VSIGZipHandle::CloseBaseHandle()
{
bool bRet = true;
if (m_poBaseHandle)
{
bRet = m_poBaseHandle->Close() == 0;
delete m_poBaseHandle;
}
m_poBaseHandle = nullptr;
return bRet;
}
/************************************************************************/
/* VSIGZipHandle() */
/************************************************************************/
VSIGZipHandle::VSIGZipHandle(VSIVirtualHandle *poBaseHandle,
const char *pszBaseFileName, vsi_l_offset offset,
vsi_l_offset compressed_size,
vsi_l_offset uncompressed_size, uLong expected_crc,
int transparent)
: m_poBaseHandle(poBaseHandle),
#ifdef DEBUG
m_offset(offset),
#endif
m_uncompressed_size(uncompressed_size), m_expected_crc(expected_crc),
m_pszBaseFileName(pszBaseFileName ? CPLStrdup(pszBaseFileName) : nullptr),
m_bWriteProperties(CPLTestBool(
CPLGetConfigOption("CPL_VSIL_GZIP_WRITE_PROPERTIES", "YES"))),
m_bCanSaveInfo(
CPLTestBool(CPLGetConfigOption("CPL_VSIL_GZIP_SAVE_INFO", "YES"))),
stream(), crc(0), m_transparent(transparent)
{
if (compressed_size || transparent)
{
m_compressed_size = compressed_size;
}
else
{
if (poBaseHandle->Seek(0, SEEK_END) != 0)
CPLError(CE_Failure, CPLE_FileIO, "Seek() failed");
m_compressed_size = poBaseHandle->Tell() - offset;
compressed_size = m_compressed_size;
}
offsetEndCompressedData = offset + compressed_size;
if (poBaseHandle->Seek(offset, SEEK_SET) != 0)
CPLError(CE_Failure, CPLE_FileIO, "Seek() failed");
stream.zalloc = nullptr;
stream.zfree = nullptr;
stream.opaque = nullptr;
stream.next_in = inbuf = nullptr;
stream.next_out = outbuf = nullptr;
stream.avail_in = stream.avail_out = 0;
inbuf = static_cast<Byte *>(ALLOC(Z_BUFSIZE));
stream.next_in = inbuf;
int err = inflateInit2(&(stream), -MAX_WBITS);
// windowBits is passed < 0 to tell that there is no zlib header.
// Note that in this case inflate *requires* an extra "dummy" byte
// after the compressed stream in order to complete decompression and
// return Z_STREAM_END. Here the gzip CRC32 ensures that 4 bytes are
// present after the compressed stream.
if (err != Z_OK || inbuf == nullptr)
{
CPLError(CE_Failure, CPLE_NotSupported, "inflateInit2 init failed");
TRYFREE(inbuf);
inbuf = nullptr;
return;
}
stream.avail_out = static_cast<uInt>(Z_BUFSIZE);
if (offset == 0)
check_header(); // Skip the .gz header.
startOff = poBaseHandle->Tell() - stream.avail_in;
if (transparent == 0)
{
snapshot_byte_interval = std::max(static_cast<vsi_l_offset>(Z_BUFSIZE),
compressed_size / 100);
snapshots = static_cast<GZipSnapshot *>(CPLCalloc(
sizeof(GZipSnapshot),
static_cast<size_t>(compressed_size / snapshot_byte_interval + 1)));
}
}
/************************************************************************/
/* SaveInfo_unlocked() */
/************************************************************************/
void VSIGZipHandle::SaveInfo_unlocked()
{
if (m_pszBaseFileName && m_bCanSaveInfo)
{
VSIFilesystemHandler *poFSHandler =
VSIFileManager::GetHandler("/vsigzip/");
cpl::down_cast<VSIGZipFilesystemHandler *>(poFSHandler)
->SaveInfo_unlocked(this);
m_bCanSaveInfo = false;
}
}
/************************************************************************/
/* ~VSIGZipHandle() */
/************************************************************************/
VSIGZipHandle::~VSIGZipHandle()
{
if (m_pszBaseFileName && m_bCanSaveInfo)
{
VSIFilesystemHandler *poFSHandler =
VSIFileManager::GetHandler("/vsigzip/");
cpl::down_cast<VSIGZipFilesystemHandler *>(poFSHandler)->SaveInfo(this);
}
if (stream.state != nullptr)
{
inflateEnd(&(stream));
}
TRYFREE(inbuf);
TRYFREE(outbuf);
if (snapshots != nullptr)
{
for (size_t i = 0; i < m_compressed_size / snapshot_byte_interval + 1;
i++)
{
if (snapshots[i].posInBaseHandle)
{
inflateEnd(&(snapshots[i].stream));
}
}
CPLFree(snapshots);
}
CPLFree(m_pszBaseFileName);
CloseBaseHandle();
}
/************************************************************************/
/* check_header() */
/************************************************************************/
void VSIGZipHandle::check_header()
{
// Assure two bytes in the buffer so we can peek ahead -- handle case
// where first byte of header is at the end of the buffer after the last
// gzip segment.
uInt len = stream.avail_in;
if (len < 2)
{
if (len)
inbuf[0] = stream.next_in[0];
errno = 0;
size_t nToRead = static_cast<size_t>(Z_BUFSIZE - len);
CPLAssert(m_poBaseHandle->Tell() <= offsetEndCompressedData);
if (m_poBaseHandle->Tell() + nToRead > offsetEndCompressedData)
nToRead = static_cast<size_t>(offsetEndCompressedData -
m_poBaseHandle->Tell());
len = static_cast<uInt>(m_poBaseHandle->Read(inbuf + len, 1, nToRead));
#ifdef ENABLE_DEBUG
CPLDebug("GZIP", CPL_FRMT_GUIB " " CPL_FRMT_GUIB,
m_poBaseHandle->Tell(), offsetEndCompressedData);
#endif
if (len == 0) // && ferror(file)
{
if (m_poBaseHandle->Tell() != offsetEndCompressedData)
z_err = Z_ERRNO;
}
stream.avail_in += len;
stream.next_in = inbuf;
if (stream.avail_in < 2)
{
m_transparent = stream.avail_in;
return;
}
}
// Peek ahead to check the gzip magic header.
if (stream.next_in[0] != gz_magic[0] || stream.next_in[1] != gz_magic[1])
{
m_transparent = 1;
return;
}
stream.avail_in -= 2;
stream.next_in += 2;
// Check the rest of the gzip header.
const int method = get_byte();
const int flags = get_byte();
if (method != Z_DEFLATED || (flags & RESERVED) != 0)
{
z_err = Z_DATA_ERROR;
return;
}
// Discard time, xflags and OS code:
for (len = 0; len < 6; len++)
CPL_IGNORE_RET_VAL(get_byte());
if ((flags & EXTRA_FIELD) != 0)
{
// Skip the extra field.
len = static_cast<uInt>(get_byte()) & 0xFF;
len += (static_cast<uInt>(get_byte()) & 0xFF) << 8;
// len is garbage if EOF but the loop below will quit anyway.
while (len != 0 && get_byte() != EOF)
{
--len;
}
}
if ((flags & ORIG_NAME) != 0)
{
// Skip the original file name.
int c;
while ((c = get_byte()) != 0 && c != EOF)
{
}
}
if ((flags & COMMENT) != 0)
{
// skip the .gz file comment.
int c;
while ((c = get_byte()) != 0 && c != EOF)
{
}
}
if ((flags & HEAD_CRC) != 0)
{
// Skip the header crc.
for (len = 0; len < 2; len++)
CPL_IGNORE_RET_VAL(get_byte());
}
z_err = z_eof ? Z_DATA_ERROR : Z_OK;
}
/************************************************************************/
/* get_byte() */
/************************************************************************/
int VSIGZipHandle::get_byte()
{
if (z_eof)
return EOF;
if (stream.avail_in == 0)
{
errno = 0;
size_t nToRead = static_cast<size_t>(Z_BUFSIZE);
CPLAssert(m_poBaseHandle->Tell() <= offsetEndCompressedData);
if (m_poBaseHandle->Tell() + nToRead > offsetEndCompressedData)
nToRead = static_cast<size_t>(offsetEndCompressedData -
m_poBaseHandle->Tell());
stream.avail_in =
static_cast<uInt>(m_poBaseHandle->Read(inbuf, 1, nToRead));
#ifdef ENABLE_DEBUG
CPLDebug("GZIP", CPL_FRMT_GUIB " " CPL_FRMT_GUIB,
m_poBaseHandle->Tell(), offsetEndCompressedData);
#endif
if (stream.avail_in == 0)
{
z_eof = 1;
if (m_poBaseHandle->Tell() != offsetEndCompressedData)
z_err = Z_ERRNO;
// if( ferror(file) ) z_err = Z_ERRNO;
return EOF;
}
stream.next_in = inbuf;
}
stream.avail_in--;
return *(stream.next_in)++;
}
/************************************************************************/
/* gzrewind() */
/************************************************************************/
int VSIGZipHandle::gzrewind()
{
z_err = Z_OK;
z_eof = 0;
m_bEOF = false;
stream.avail_in = 0;
stream.next_in = inbuf;
crc = 0;
if (!m_transparent)
CPL_IGNORE_RET_VAL(inflateReset(&stream));
in = 0;
out = 0;
return m_poBaseHandle->Seek(startOff, SEEK_SET);
}
/************************************************************************/
/* Seek() */
/************************************************************************/
int VSIGZipHandle::Seek(vsi_l_offset nOffset, int nWhence)
{
m_bEOF = false;
return gzseek(nOffset, nWhence) ? 0 : -1;
}
/************************************************************************/
/* gzseek() */
/************************************************************************/
bool VSIGZipHandle::gzseek(vsi_l_offset offset, int whence)
{
const vsi_l_offset original_offset = offset;
const int original_nWhence = whence;
z_eof = 0;
#ifdef ENABLE_DEBUG
CPLDebug("GZIP", "Seek(" CPL_FRMT_GUIB ",%d)", offset, whence);
#endif
if (m_transparent)
{
stream.avail_in = 0;
stream.next_in = inbuf;
if (whence == SEEK_CUR)
{
if (out + offset > m_compressed_size)
{
CPL_VSIL_GZ_RETURN(FALSE);
return false;
}
offset = startOff + out + offset;
}
else if (whence == SEEK_SET)
{
if (offset > m_compressed_size)
{
CPL_VSIL_GZ_RETURN(FALSE);
return false;
}
offset = startOff + offset;
}
else if (whence == SEEK_END)
{
// Commented test: because vsi_l_offset is unsigned (for the moment)
// so no way to seek backward. See #1590 */
if (offset > 0) // || -offset > compressed_size
{
CPL_VSIL_GZ_RETURN(FALSE);
return false;
}
offset = startOff + m_compressed_size - offset;
}
else
{
CPL_VSIL_GZ_RETURN(FALSE);
return false;
}
if (m_poBaseHandle->Seek(offset, SEEK_SET) < 0)
{
CPL_VSIL_GZ_RETURN(FALSE);
return false;
}
out = offset - startOff;
in = out;
return true;
}
// whence == SEEK_END is unsuppored in original gzseek.
if (whence == SEEK_END)
{
// If we known the uncompressed size, we can fake a jump to
// the end of the stream.
if (offset == 0 && m_uncompressed_size != 0)
{
out = m_uncompressed_size;
return true;
}
// We don't know the uncompressed size. This is unfortunate.
// Do the slow version.
static int firstWarning = 1;
if (m_compressed_size > 10 * 1024 * 1024 && firstWarning)
{
CPLError(CE_Warning, CPLE_AppDefined,
"VSIFSeekL(xxx, SEEK_END) may be really slow "
"on GZip streams.");
firstWarning = 0;
}
whence = SEEK_CUR;
offset = 1024 * 1024 * 1024;
offset *= 1024 * 1024;
}
// Rest of function is for reading only.
// Compute absolute position.
if (whence == SEEK_CUR)
{
offset += out;
}
// For a negative seek, rewind and use positive seek.
if (offset >= out)
{
offset -= out;
}
else if (gzrewind() < 0)
{
CPL_VSIL_GZ_RETURN(FALSE);
return false;
}
if (z_err != Z_OK && z_err != Z_STREAM_END)
{
CPL_VSIL_GZ_RETURN(FALSE);
return false;
}
for (unsigned int i = 0; i < m_compressed_size / snapshot_byte_interval + 1;
i++)
{
if (snapshots[i].posInBaseHandle == 0)
break;
if (snapshots[i].out <= out + offset &&
(i == m_compressed_size / snapshot_byte_interval ||
snapshots[i + 1].out == 0 || snapshots[i + 1].out > out + offset))
{
if (out >= snapshots[i].out)
break;
#ifdef ENABLE_DEBUG
CPLDebug("SNAPSHOT",
"using snapshot %d : "
"posInBaseHandle(snapshot)=" CPL_FRMT_GUIB
" in(snapshot)=" CPL_FRMT_GUIB
" out(snapshot)=" CPL_FRMT_GUIB " out=" CPL_FRMT_GUIB
" offset=" CPL_FRMT_GUIB,
i, snapshots[i].posInBaseHandle, snapshots[i].in,
snapshots[i].out, out, offset);
#endif
offset = out + offset - snapshots[i].out;
if (m_poBaseHandle->Seek(snapshots[i].posInBaseHandle, SEEK_SET) !=
0)
CPLError(CE_Failure, CPLE_FileIO, "Seek() failed");
inflateEnd(&stream);
inflateCopy(&stream, &snapshots[i].stream);
crc = snapshots[i].crc;
m_transparent = snapshots[i].transparent;
in = snapshots[i].in;
out = snapshots[i].out;
break;
}
}
// Offset is now the number of bytes to skip.
if (offset != 0 && outbuf == nullptr)
{
outbuf = static_cast<Byte *>(ALLOC(Z_BUFSIZE));
if (outbuf == nullptr)
{
CPL_VSIL_GZ_RETURN(FALSE);
return false;
}
}
if (original_nWhence == SEEK_END && z_err == Z_STREAM_END)
{
return true;
}
while (offset > 0)
{
int size = Z_BUFSIZE;
if (offset < static_cast<vsi_l_offset>(Z_BUFSIZE))
size = static_cast<int>(offset);
int read_size =
static_cast<int>(Read(outbuf, 1, static_cast<uInt>(size)));
if (original_nWhence == SEEK_END)
{
if (size != read_size)
{
z_err = Z_STREAM_END;
break;
}
}
else if (read_size == 0)
{
// CPL_VSIL_GZ_RETURN(FALSE);
return false;
}
offset -= read_size;
}
#ifdef ENABLE_DEBUG
CPLDebug("GZIP", "gzseek at offset " CPL_FRMT_GUIB, out);
#endif
if (original_offset == 0 && original_nWhence == SEEK_END)
{
m_uncompressed_size = out;
if (m_pszBaseFileName && !STARTS_WITH(m_pszBaseFileName, "/vsicurl/") &&
!STARTS_WITH(m_pszBaseFileName, "/vsitar/") &&
!STARTS_WITH(m_pszBaseFileName, "/vsizip/") && m_bWriteProperties)
{
CPLErrorStateBackuper oErrorStateBackuper(CPLQuietErrorHandler);
CPLString osCacheFilename(m_pszBaseFileName);
osCacheFilename += ".properties";
// Write a .properties file to avoid seeking next time.
VSILFILE *fpCacheLength = VSIFOpenL(osCacheFilename.c_str(), "wb");
if (fpCacheLength)
{
char szBuffer[32] = {};
CPLPrintUIntBig(szBuffer, m_compressed_size, 31);
char *pszFirstNonSpace = szBuffer;
while (*pszFirstNonSpace == ' ')
pszFirstNonSpace++;
CPL_IGNORE_RET_VAL(VSIFPrintfL(
fpCacheLength, "compressed_size=%s\n", pszFirstNonSpace));
CPLPrintUIntBig(szBuffer, m_uncompressed_size, 31);
pszFirstNonSpace = szBuffer;
while (*pszFirstNonSpace == ' ')
pszFirstNonSpace++;
CPL_IGNORE_RET_VAL(VSIFPrintfL(
fpCacheLength, "uncompressed_size=%s\n", pszFirstNonSpace));
CPL_IGNORE_RET_VAL(VSIFCloseL(fpCacheLength));
}
}
}
return true;
}
/************************************************************************/
/* Tell() */
/************************************************************************/
vsi_l_offset VSIGZipHandle::Tell()
{
#ifdef ENABLE_DEBUG
CPLDebug("GZIP", "Tell() = " CPL_FRMT_GUIB, out);
#endif
return out;
}
/************************************************************************/
/* Read() */
/************************************************************************/
size_t VSIGZipHandle::Read(void *const buf, size_t const nSize,