summaryrefslogtreecommitdiffstats
path: root/src/VBox/Runtime/common/zip/zip.cpp
blob: 5b6ce1ef9cf067d55b92123bc7bd415ac4793c05 (plain)
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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
/* $Id: zip.cpp $ */
/** @file
 * IPRT - Compression.
 */

/*
 * Copyright (C) 2006-2019 Oracle Corporation
 *
 * This file is part of VirtualBox Open Source Edition (OSE), as
 * available from http://www.virtualbox.org. This file is free software;
 * you can redistribute it and/or modify it under the terms of the GNU
 * General Public License (GPL) as published by the Free Software
 * Foundation, in version 2 as it comes in the "COPYING" file of the
 * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
 * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
 *
 * The contents of this file may alternatively be used under the terms
 * of the Common Development and Distribution License Version 1.0
 * (CDDL) only, as it comes in the "COPYING.CDDL" file of the
 * VirtualBox OSE distribution, in which case the provisions of the
 * CDDL are applicable instead of those of the GPL.
 *
 * You may elect to license modified versions of this file under the
 * terms and conditions of either the GPL or the CDDL or both.
 */


/*********************************************************************************************************************************
*   Defined Constants And Macros                                                                                                 *
*********************************************************************************************************************************/
#define RTZIP_USE_STORE 1
#define RTZIP_USE_ZLIB 1
//#define RTZIP_USE_BZLIB 1
#ifndef IN_GUEST
# define RTZIP_USE_LZF 1
#endif
#define RTZIP_LZF_BLOCK_BY_BLOCK
//#define RTZIP_USE_LZJB 1
//#define RTZIP_USE_LZO 1

/** @todo FastLZ? QuickLZ? Others? */


/*********************************************************************************************************************************
*   Header Files                                                                                                                 *
*********************************************************************************************************************************/
#ifdef RTZIP_USE_BZLIB
# include <bzlib.h>
#endif
#ifdef RTZIP_USE_ZLIB
# include <zlib.h>
#endif
#ifdef RTZIP_USE_LZF
# include <lzf.h>
# include <iprt/crc.h>
#endif
#ifdef RTZIP_USE_LZJB
# include "lzjb.h"
#endif
#ifdef RTZIP_USE_LZO
# include <lzo/lzo1x.h>
#endif

#include <iprt/zip.h>
#include "internal/iprt.h"

/*#include <iprt/asm.h>*/
#include <iprt/alloc.h>
#include <iprt/assert.h>
#include <iprt/err.h>
#include <iprt/log.h>
#include <iprt/string.h>

#include <errno.h>


/*********************************************************************************************************************************
*   Structures and Typedefs                                                                                                      *
*********************************************************************************************************************************/

#ifdef RTZIP_USE_LZF

/**
 * LZF block header.
 */
#pragma pack(1)                         /* paranoia */
typedef struct RTZIPLZFHDR
{
    /** Magic word (RTZIPLZFHDR_MAGIC). */
    uint16_t    u16Magic;
    /** The number of bytes of data following this header. */
    uint16_t    cbData;
    /** The CRC32 of the block. */
    uint32_t    u32CRC;
    /** The size of the uncompressed data in bytes. */
    uint16_t    cbUncompressed;
} RTZIPLZFHDR;
#pragma pack()
/** Pointer to a LZF block header. */
typedef RTZIPLZFHDR *PRTZIPLZFHDR;
/** Pointer to a const LZF block header. */
typedef const RTZIPLZFHDR *PCRTZIPLZFHDR;

/** The magic of a LZF block header. */
#define RTZIPLZFHDR_MAGIC                       ('Z' | ('V' << 8))

/** The max compressed data size.
 * The maximum size of a block is currently 16KB.
 * This is very important so we don't have to move input buffers around. */
#define RTZIPLZF_MAX_DATA_SIZE                  (16384 - sizeof(RTZIPLZFHDR))

/** The max uncompressed data size.
 * This is important so we don't overflow the spill buffer in the decompressor. */
#define RTZIPLZF_MAX_UNCOMPRESSED_DATA_SIZE     (32*_1K)

#endif /* RTZIP_USE_LZF */


/**
 * Compressor/Decompressor instance data.
 */
typedef struct RTZIPCOMP
{
    /** Output buffer. */
    uint8_t             abBuffer[_128K];
    /** Compression output consumer. */
    PFNRTZIPOUT         pfnOut;
    /** User argument for the callback. */
    void               *pvUser;

    /**
     * @copydoc RTZipCompress
     */
    DECLCALLBACKMEMBER(int, pfnCompress)(PRTZIPCOMP pZip, const void *pvBuf, size_t cbBuf);

    /**
     * @copydoc RTZipCompFinish
     */
    DECLCALLBACKMEMBER(int, pfnFinish)(PRTZIPCOMP pZip);

    /**
     * @copydoc RTZipCompDestroy
     */
    DECLCALLBACKMEMBER(int, pfnDestroy)(PRTZIPCOMP pZip);

    /** Compression type. */
    RTZIPTYPE           enmType;
    /** Type specific data. */
    union
    {
#ifdef RTZIP_USE_STORE
        /** Simple storing. */
        struct
        {
            /** Current buffer position. (where to start write) */
            uint8_t    *pb;
        } Store;
#endif
#ifdef RTZIP_USE_ZLIB
        /** Zlib stream. */
        z_stream        Zlib;
#endif
#ifdef RTZIP_USE_BZLIB
        /** BZlib stream. */
        bz_stream       BZlib;
#endif
#ifdef RTZIP_USE_LZF
        /** LZF stream. */
        struct
        {
            /** Current output buffer position. */
            uint8_t    *pbOutput;
            /** The input buffer position. */
            uint8_t    *pbInput;
            /** The number of free bytes in the input buffer. */
            size_t      cbInputFree;
            /** The input buffer. */
            uint8_t     abInput[RTZIPLZF_MAX_UNCOMPRESSED_DATA_SIZE];
        } LZF;
#endif

    } u;
} RTZIPCOMP;



/**
 * Decompressor instance data.
 */
typedef struct RTZIPDECOMP
{
    /** Input buffer. */
    uint8_t             abBuffer[_128K];
    /** Decompression input producer. */
    PFNRTZIPIN          pfnIn;
    /** User argument for the callback. */
    void               *pvUser;

    /**
     * @copydoc RTZipDecompress
     */
    DECLCALLBACKMEMBER(int, pfnDecompress)(PRTZIPDECOMP pZip, void *pvBuf, size_t cbBuf, size_t *pcbWritten);

    /**
     * @copydoc RTZipDecompDestroy
     */
    DECLCALLBACKMEMBER(int, pfnDestroy)(PRTZIPDECOMP pZip);

    /** Compression type. */
    RTZIPTYPE           enmType;
    /** Type specific data. */
    union
    {
#ifdef RTZIP_USE_STORE
        /** Simple storing. */
        struct
        {
            /** Current buffer position. (where to start read) */
            uint8_t    *pb;
            /** Number of bytes left in the buffer. */
            size_t      cbBuffer;
        } Store;
#endif
#ifdef RTZIP_USE_ZLIB
        /** Zlib stream. */
        z_stream        Zlib;
#endif
#ifdef RTZIP_USE_BZLIB
        /** BZlib stream. */
        bz_stream       BZlib;
#endif
#ifdef RTZIP_USE_LZF
        /** LZF 'stream'. */
        struct
        {
# ifndef RTZIP_LZF_BLOCK_BY_BLOCK
            /** Current input buffer position. */
            uint8_t    *pbInput;
            /** The number of bytes left in the input buffer. */
            size_t      cbInput;
# endif
            /** The spill buffer.
             * LZF is a block based compressor and not a stream compressor. So,
             * we have to decompress full blocks if we want to get any of the data.
             * This buffer is to store the spill after decompressing a block. */
            uint8_t     abSpill[RTZIPLZF_MAX_UNCOMPRESSED_DATA_SIZE];
            /** The number of bytes left spill buffer. */
            unsigned    cbSpill;
            /** The current spill buffer position. */
            uint8_t    *pbSpill;
        } LZF;
#endif

    } u;
} RTZIPDECOM;



#ifdef RTZIP_USE_STORE

/**
 * @copydoc RTZipCompress
 */
static DECLCALLBACK(int) rtZipStoreCompress(PRTZIPCOMP pZip, const void *pvBuf, size_t cbBuf)
{
    uint8_t *pbDst = pZip->u.Store.pb;
    while (cbBuf)
    {
        /*
         * Flush.
         */
        size_t cb = sizeof(pZip->abBuffer) - (size_t)(pbDst - &pZip->abBuffer[0]); /* careful here, g++ 4.1.2 screws up easily */
        if (cb == 0)
        {
            int rc = pZip->pfnOut(pZip->pvUser, &pZip->abBuffer[0], sizeof(pZip->abBuffer));
            if (RT_FAILURE(rc))
                return rc;

            cb = sizeof(pZip->abBuffer);
            pbDst = &pZip->abBuffer[0];
        }

        /*
         * Add to the buffer and advance.
         */
        if (cbBuf < cb)
            cb = cbBuf;
        memcpy(pbDst, pvBuf, cb);

        pbDst += cb;
        cbBuf -= cb;
        pvBuf = (uint8_t *)pvBuf + cb;
    }
    pZip->u.Store.pb = pbDst;
    return VINF_SUCCESS;
}


/**
 * @copydoc RTZipCompFinish
 */
static DECLCALLBACK(int) rtZipStoreCompFinish(PRTZIPCOMP pZip)
{
    size_t cb = (uintptr_t)pZip->u.Store.pb - (uintptr_t)&pZip->abBuffer[0];
    if (cb > 0)
    {
        int rc = pZip->pfnOut(pZip->pvUser, &pZip->abBuffer[0], cb);
        if (RT_FAILURE(rc))
            return rc;
    }
    return VINF_SUCCESS;
}


/**
 * @copydoc RTZipCompDestroy
 */
static DECLCALLBACK(int) rtZipStoreCompDestroy(PRTZIPCOMP pZip)
{
    NOREF(pZip);
    return VINF_SUCCESS;
}


/**
 * Initializes the compressor instance.
 * @returns iprt status code.
 * @param   pZip        The compressor instance.
 * @param   enmLevel    The desired compression level.
 */
static DECLCALLBACK(int) rtZipStoreCompInit(PRTZIPCOMP pZip, RTZIPLEVEL enmLevel)
{
    NOREF(enmLevel);
    pZip->pfnCompress = rtZipStoreCompress;
    pZip->pfnFinish   = rtZipStoreCompFinish;
    pZip->pfnDestroy  = rtZipStoreCompDestroy;

    pZip->u.Store.pb = &pZip->abBuffer[1];
    return VINF_SUCCESS;
}


/**
 * @copydoc RTZipDecompress
 */
static DECLCALLBACK(int) rtZipStoreDecompress(PRTZIPDECOMP pZip, void *pvBuf, size_t cbBuf, size_t *pcbWritten)
{
    size_t cbWritten = 0;
    while (cbBuf)
    {
        /*
         * Fill buffer.
         */
        size_t cb = pZip->u.Store.cbBuffer;
        if (cb <= 0)
        {
            int rc = pZip->pfnIn(pZip->pvUser, &pZip->abBuffer[0], sizeof(pZip->abBuffer), &cb);
            if (RT_FAILURE(rc))
                return rc;
            pZip->u.Store.cbBuffer = cb;
            pZip->u.Store.pb = &pZip->abBuffer[0];
        }

        /*
         * No more data?
         */
        if (cb == 0)
        {
            if (pcbWritten)
            {
                *pcbWritten = cbWritten;
                return VINF_SUCCESS;
            }
            return VERR_NO_DATA;
        }

        /*
         * Add to the buffer and advance.
         */
        if (cbBuf < cb)
            cb = cbBuf;
        memcpy(pvBuf, pZip->u.Store.pb, cb);
        pZip->u.Store.pb += cb;
        pZip->u.Store.cbBuffer -= cb;
        cbBuf -= cb;
        pvBuf = (char *)pvBuf + cb;
        cbWritten += cb;
    }
    if (pcbWritten)
        *pcbWritten = cbWritten;
    return VINF_SUCCESS;
}


/**
 * @copydoc RTZipDecompDestroy
 */
static DECLCALLBACK(int) rtZipStoreDecompDestroy(PRTZIPDECOMP pZip)
{
    NOREF(pZip);
    return VINF_SUCCESS;
}


/**
 * Initialize the decompressor instance.
 * @returns iprt status code.
 * @param   pZip        The decompressor instance.
 */
static DECLCALLBACK(int) rtZipStoreDecompInit(PRTZIPDECOMP pZip)
{
    pZip->pfnDecompress = rtZipStoreDecompress;
    pZip->pfnDestroy = rtZipStoreDecompDestroy;

    pZip->u.Store.pb = &pZip->abBuffer[0];
    pZip->u.Store.cbBuffer = 0;
    return VINF_SUCCESS;
}

#endif /* RTZIP_USE_STORE */


#ifdef RTZIP_USE_ZLIB

/*
 * Missing definitions from zutil.h. We need these constants for calling
 * inflateInit2() / deflateInit2().
 */
# ifndef Z_DEF_WBITS
#  define Z_DEF_WBITS        MAX_WBITS
# endif
# ifndef Z_DEF_MEM_LEVEL
#  define Z_DEF_MEM_LEVEL    8
# endif

/**
 * Convert from zlib errno to iprt status code.
 * @returns iprt status code.
 * @param   rc              Zlib error code.
 * @param   fCompressing    Set if we're compressing, clear if decompressing.
 */
static int zipErrConvertFromZlib(int rc, bool fCompressing)
{
    switch (rc)
    {
        case Z_OK:
            return VINF_SUCCESS;

        case Z_STREAM_ERROR:
            return VERR_ZIP_CORRUPTED;

        case Z_DATA_ERROR:
            return fCompressing ? VERR_ZIP_ERROR : VERR_ZIP_CORRUPTED;

        case Z_MEM_ERROR:
            return VERR_ZIP_NO_MEMORY;

        case Z_BUF_ERROR:
            return VERR_ZIP_ERROR;

        case Z_VERSION_ERROR:
            return VERR_ZIP_UNSUPPORTED_VERSION;

        case Z_ERRNO: /* We shouldn't see this status! */
        default:
            AssertMsgFailed(("%d\n", rc));
            if (rc >= 0)
                return VINF_SUCCESS;
            return VERR_ZIP_ERROR;
    }
}


/**
 * @copydoc RTZipCompress
 */
static DECLCALLBACK(int) rtZipZlibCompress(PRTZIPCOMP pZip, const void *pvBuf, size_t cbBuf)
{
    pZip->u.Zlib.next_in  = (Bytef *)pvBuf;
    pZip->u.Zlib.avail_in = (uInt)cbBuf;                    Assert(pZip->u.Zlib.avail_in == cbBuf);
    while (pZip->u.Zlib.avail_in > 0)
    {
        /*
         * Flush output buffer?
         */
        if (pZip->u.Zlib.avail_out <= 0)
        {
            int rc = pZip->pfnOut(pZip->pvUser, &pZip->abBuffer[0], sizeof(pZip->abBuffer) - pZip->u.Zlib.avail_out);
            if (RT_FAILURE(rc))
                return rc;
            pZip->u.Zlib.avail_out = sizeof(pZip->abBuffer);
            pZip->u.Zlib.next_out = &pZip->abBuffer[0];
        }

        /*
         * Pass it on to zlib.
         */
        int rc = deflate(&pZip->u.Zlib, Z_NO_FLUSH);
        if (rc != Z_OK)
            return zipErrConvertFromZlib(rc, true /*fCompressing*/);
    }
    return VINF_SUCCESS;
}


/**
 * @copydoc RTZipCompFinish
 */
static DECLCALLBACK(int) rtZipZlibCompFinish(PRTZIPCOMP pZip)
{
    int rc = Z_OK;
    for (;;)
    {
        /*
         * Flush outstanding stuff. writes.
         */
        if (rc == Z_STREAM_END || pZip->u.Zlib.avail_out <= 0)
        {
            int rc2 = pZip->pfnOut(pZip->pvUser, &pZip->abBuffer[0], sizeof(pZip->abBuffer) - pZip->u.Zlib.avail_out);
            if (RT_FAILURE(rc2))
                return rc2;
            pZip->u.Zlib.avail_out = sizeof(pZip->abBuffer);
            pZip->u.Zlib.next_out = &pZip->abBuffer[0];
            if (rc == Z_STREAM_END)
                return VINF_SUCCESS;
        }

        /*
         * Tell zlib to flush.
         */
        rc = deflate(&pZip->u.Zlib, Z_FINISH);
        if (rc != Z_OK && rc != Z_STREAM_END)
            return zipErrConvertFromZlib(rc, true /*fCompressing*/);
    }
}


/**
 * @copydoc RTZipCompDestroy
 */
static DECLCALLBACK(int) rtZipZlibCompDestroy(PRTZIPCOMP pZip)
{
    /*
     * Terminate the deflate instance.
     */
    int rc = deflateEnd(&pZip->u.Zlib);
    if (rc != Z_OK)
        rc = zipErrConvertFromZlib(rc, true /*fCompressing*/);
    return rc;
}


/**
 * Initializes the compressor instance.
 * @returns iprt status code.
 * @param   pZip        The compressor instance.
 * @param   enmLevel    The desired compression level.
 * @param   fZlibHeader If true, write the Zlib header.
 */
static DECLCALLBACK(int) rtZipZlibCompInit(PRTZIPCOMP pZip, RTZIPLEVEL enmLevel, bool fZlibHeader)
{
    pZip->pfnCompress = rtZipZlibCompress;
    pZip->pfnFinish   = rtZipZlibCompFinish;
    pZip->pfnDestroy  = rtZipZlibCompDestroy;

    int iLevel = Z_DEFAULT_COMPRESSION;
    switch (enmLevel)
    {
        case RTZIPLEVEL_STORE:      iLevel = 0; break;
        case RTZIPLEVEL_FAST:       iLevel = 2; break;
        case RTZIPLEVEL_DEFAULT:    iLevel = Z_DEFAULT_COMPRESSION; break;
        case RTZIPLEVEL_MAX:        iLevel = 9; break;
    }

    memset(&pZip->u.Zlib, 0, sizeof(pZip->u.Zlib));
    pZip->u.Zlib.next_out  = &pZip->abBuffer[1];
    pZip->u.Zlib.avail_out = sizeof(pZip->abBuffer) - 1;
    pZip->u.Zlib.opaque    = pZip;

    int rc = deflateInit2(&pZip->u.Zlib, iLevel, Z_DEFLATED, fZlibHeader ? Z_DEF_WBITS : -Z_DEF_WBITS,
                          Z_DEF_MEM_LEVEL, Z_DEFAULT_STRATEGY);
    return rc >= 0 ? rc = VINF_SUCCESS : zipErrConvertFromZlib(rc, true /*fCompressing*/);
}


/**
 * @copydoc RTZipDecompress
 */
static DECLCALLBACK(int) rtZipZlibDecompress(PRTZIPDECOMP pZip, void *pvBuf, size_t cbBuf, size_t *pcbWritten)
{
    pZip->u.Zlib.next_out = (Bytef *)pvBuf;
    pZip->u.Zlib.avail_out = (uInt)cbBuf;
    Assert(pZip->u.Zlib.avail_out == cbBuf);

    /*
     * Be greedy reading input, even if no output buffer is left. It's possible
     * that it's just the end of stream marker which needs to be read. Happens
     * for incompressible blocks just larger than the input buffer size.
     */
    while (pZip->u.Zlib.avail_out > 0 || pZip->u.Zlib.avail_in <= 0)
    {
        /*
         * Read more input?
         */
        if (pZip->u.Zlib.avail_in <= 0)
        {
            size_t cb = sizeof(pZip->abBuffer);
            int rc = pZip->pfnIn(pZip->pvUser, &pZip->abBuffer[0], sizeof(pZip->abBuffer), &cb);
            if (RT_FAILURE(rc))
                return rc;
            pZip->u.Zlib.avail_in = (uInt)cb;               Assert(pZip->u.Zlib.avail_in == cb);
            pZip->u.Zlib.next_in = &pZip->abBuffer[0];
        }

        /*
         * Pass it on to zlib.
         */
        int rc = inflate(&pZip->u.Zlib, Z_NO_FLUSH);
        if (rc == Z_STREAM_END)
        {
            if (pcbWritten)
                *pcbWritten = cbBuf - pZip->u.Zlib.avail_out;
            else if (pZip->u.Zlib.avail_out > 0)
                return VERR_NO_DATA;
            break;
        }
        if (rc != Z_OK)
            return zipErrConvertFromZlib(rc, false /*fCompressing*/);
    }
    return VINF_SUCCESS;
}


/**
 * @copydoc RTZipDecompDestroy
 */
static DECLCALLBACK(int) rtZipZlibDecompDestroy(PRTZIPDECOMP pZip)
{
    /*
     * Terminate the deflate instance.
     */
    int rc = inflateEnd(&pZip->u.Zlib);
    if (rc != Z_OK)
        rc = zipErrConvertFromZlib(rc, false /*fCompressing*/);
    return rc;
}


/**
 * Initialize the decompressor instance.
 * @returns iprt status code.
 * @param   pZip        The decompressor instance.
 * @param   fZlibHeader If true, expect the Zlib header.
 */
static DECLCALLBACK(int) rtZipZlibDecompInit(PRTZIPDECOMP pZip, bool fZlibHeader)
{
    pZip->pfnDecompress = rtZipZlibDecompress;
    pZip->pfnDestroy = rtZipZlibDecompDestroy;

    memset(&pZip->u.Zlib, 0, sizeof(pZip->u.Zlib));
    pZip->u.Zlib.opaque    = pZip;

    int rc = inflateInit2(&pZip->u.Zlib, fZlibHeader ? Z_DEF_WBITS : -Z_DEF_WBITS);
    return rc >= 0 ? VINF_SUCCESS : zipErrConvertFromZlib(rc, false /*fCompressing*/);
}

#endif /* RTZIP_USE_ZLIB */


#ifdef RTZIP_USE_BZLIB
/**
 * Convert from BZlib errno to iprt status code.
 * @returns iprt status code.
 * @param   rc      BZlib error code.
 */
static int zipErrConvertFromBZlib(int rc)
{
    /** @todo proper bzlib error conversion. */
    switch (rc)
    {
        case BZ_SEQUENCE_ERROR:
            AssertMsgFailed(("BZ_SEQUENCE_ERROR shall not happen!\n"));
            return VERR_GENERAL_FAILURE;
        case BZ_PARAM_ERROR:
            return VERR_INVALID_PARAMETER;
        case BZ_MEM_ERROR:
            return VERR_NO_MEMORY;
        case BZ_DATA_ERROR:
        case BZ_DATA_ERROR_MAGIC:
        case BZ_IO_ERROR:
        case BZ_UNEXPECTED_EOF:
        case BZ_CONFIG_ERROR:
            return VERR_GENERAL_FAILURE;
        case BZ_OUTBUFF_FULL:
            AssertMsgFailed(("BZ_OUTBUFF_FULL shall not happen!\n"));
            return VERR_GENERAL_FAILURE;
        default:
            if (rc >= 0)
                return VINF_SUCCESS;
            return VERR_GENERAL_FAILURE;
    }
}


/**
 * @copydoc RTZipCompress
 */
static DECLCALLBACK(int) rtZipBZlibCompress(PRTZIPCOMP pZip, const void *pvBuf, size_t cbBuf)
{
    pZip->u.BZlib.next_in  = (char *)pvBuf;
    pZip->u.BZlib.avail_in = cbBuf;
    while (pZip->u.BZlib.avail_in > 0)
    {
        /*
         * Flush output buffer?
         */
        if (pZip->u.BZlib.avail_out <= 0)
        {
            int rc = pZip->pfnOut(pZip->pvUser, &pZip->abBuffer[0], sizeof(pZip->abBuffer) - pZip->u.BZlib.avail_out);
            if (RT_FAILURE(rc))
                return rc;
            pZip->u.BZlib.avail_out = sizeof(pZip->abBuffer);
            pZip->u.BZlib.next_out = (char *)&pZip->abBuffer[0];
        }

        /*
         * Pass it on to zlib.
         */
        int rc = BZ2_bzCompress(&pZip->u.BZlib, BZ_RUN);
        if (rc < 0 && rc != BZ_OUTBUFF_FULL)
            return zipErrConvertFromBZlib(rc);
    }
    return VINF_SUCCESS;
}


/**
 * @copydoc RTZipCompFinish
 */
static DECLCALLBACK(int) rtZipBZlibCompFinish(PRTZIPCOMP pZip)
{
    int rc = BZ_FINISH_OK;
    for (;;)
    {
        /*
         * Flush output buffer?
         */
        if (rc == BZ_STREAM_END || pZip->u.BZlib.avail_out <= 0)
        {
            int rc2 = pZip->pfnOut(pZip->pvUser, &pZip->abBuffer[0], sizeof(pZip->abBuffer) - pZip->u.BZlib.avail_out);
            if (RT_FAILURE(rc2))
                return rc2;
            pZip->u.BZlib.avail_out = sizeof(pZip->abBuffer);
            pZip->u.BZlib.next_out = (char *)&pZip->abBuffer[0];
            if (rc == BZ_STREAM_END)
                return VINF_SUCCESS;
        }

        /*
         * Tell BZlib to finish it.
         */
        rc = BZ2_bzCompress(&pZip->u.BZlib, BZ_FINISH);
        if (rc < 0 && rc != BZ_OUTBUFF_FULL)
            return zipErrConvertFromBZlib(rc);
    }
    return VINF_SUCCESS;
}


/**
 * @copydoc RTZipCompDestroy
 */
static DECLCALLBACK(int) rtZipBZlibCompDestroy(PRTZIPCOMP pZip)
{
    /*
     * Terminate the deflate instance.
     */
    int rc = BZ2_bzCompressEnd(&pZip->u.BZlib);
    if (rc != BZ_OK)
        rc = zipErrConvertFromBZlib(rc);
    return rc;
}


/**
 * Initializes the compressor instance.
 * @returns iprt status code.
 * @param   pZip        The compressor instance.
 * @param   enmLevel    The desired compression level.
 */
static DECLCALLBACK(int) rtZipBZlibCompInit(PRTZIPCOMP pZip, RTZIPLEVEL enmLevel)
{
    pZip->pfnCompress = rtZipBZlibCompress;
    pZip->pfnFinish   = rtZipBZlibCompFinish;
    pZip->pfnDestroy  = rtZipBZlibCompDestroy;

    int iSize = 6;
    int iWork = 0;
    switch (enmLevel)
    {
        case RTZIPLEVEL_STORE:      iSize = 1; iWork = 2; break;
        case RTZIPLEVEL_FAST:       iSize = 2; iWork = 0; break;
        case RTZIPLEVEL_DEFAULT:    iSize = 5; iWork = 0; break;
        case RTZIPLEVEL_MAX:        iSize = 9; iWork = 0; break;
    }

    memset(&pZip->u.BZlib, 0, sizeof(pZip->u.BZlib));
    pZip->u.BZlib.next_out  = (char *)&pZip->abBuffer[1];
    pZip->u.BZlib.avail_out = sizeof(pZip->abBuffer) - 1;
    pZip->u.BZlib.opaque    = pZip;

    int rc = BZ2_bzCompressInit(&pZip->u.BZlib, iSize, 0, iWork);
    return rc >= 0 ? VINF_SUCCESS : zipErrConvertFromBZlib(rc);;
}


/**
 * @copydoc RTZipDecompress
 */
static DECLCALLBACK(int) rtZipBZlibDecompress(PRTZIPDECOMP pZip, void *pvBuf, size_t cbBuf, size_t *pcbWritten)
{
    pZip->u.BZlib.next_out  = (char *)pvBuf;
    pZip->u.BZlib.avail_out = cbBuf;
    while (pZip->u.BZlib.avail_out > 0)
    {
        /*
         * Read more output buffer?
         */
        if (pZip->u.BZlib.avail_in <= 0)
        {
            size_t cb;
            int rc = pZip->pfnIn(pZip->pvUser, &pZip->abBuffer[0], sizeof(pZip->abBuffer), &cb);
            if (RT_FAILURE(rc))
                return rc;
            pZip->u.BZlib.avail_in = cb;
            pZip->u.BZlib.next_in = (char *)&pZip->abBuffer[0];
        }

        /*
         * Pass it on to zlib.
         */
        int rc = BZ2_bzDecompress(&pZip->u.BZlib);
        if (rc == BZ_STREAM_END || rc == BZ_OUTBUFF_FULL)
        {
            if (pcbWritten)
                *pcbWritten = cbBuf - pZip->u.BZlib.avail_out;
            else if (pZip->u.BZlib.avail_out > 0)
                return VERR_NO_DATA;
            break;
        }
        if (rc < 0)
            return zipErrConvertFromBZlib(rc);
    }
    return VINF_SUCCESS;
}


/**
 * @copydoc RTZipDecompDestroy
 */
static DECLCALLBACK(int) rtZipBZlibDecompDestroy(PRTZIPDECOMP pZip)
{
    /*
     * Terminate the deflate instance.
     */
    int rc = BZ2_bzDecompressEnd(&pZip->u.BZlib);
    if (rc != BZ_OK)
        rc = zipErrConvertFromBZlib(rc);
    return rc;
}


/**
 * Initialize the decompressor instance.
 * @returns iprt status code.
 * @param   pZip        The decompressor instance.
 */
static DECLCALLBACK(int) rtZipBZlibDecompInit(PRTZIPDECOMP pZip)
{
    pZip->pfnDecompress = rtZipBZlibDecompress;
    pZip->pfnDestroy = rtZipBZlibDecompDestroy;

    memset(&pZip->u.BZlib, 0, sizeof(pZip->u.BZlib));
    pZip->u.BZlib.opaque    = pZip;

    int rc = BZ2_bzDecompressInit(&pZip->u.BZlib, 0, 0);
    return rc >= 0 ? VINF_SUCCESS : zipErrConvertFromBZlib(rc);
}

#endif /* RTZIP_USE_BZLIB */


#ifdef RTZIP_USE_LZF

/**
 * Flushes the output buffer.
 * @returns iprt status code.
 * @param   pZip        The compressor instance.
 */
static int rtZipLZFCompFlushOutput(PRTZIPCOMP pZip)
{
    size_t      cb = pZip->u.LZF.pbOutput - &pZip->abBuffer[0];
    pZip->u.LZF.pbOutput = &pZip->abBuffer[0];
    return pZip->pfnOut(pZip->pvUser, &pZip->abBuffer[0], cb);
}


/**
 * Compresses a buffer using LZF.
 *
 * @returns VBox status code.
 * @param   pZip        The compressor instance.
 * @param   pbBuf       What to compress.
 * @param   cbBuf       How much to compress.
 */
static int rtZipLZFCompressBuffer(PRTZIPCOMP pZip, const uint8_t *pbBuf, size_t cbBuf)
{
    bool fForceFlush = false;
    while (cbBuf > 0)
    {
        /*
         * Flush output buffer?
         */
        unsigned cbFree = (unsigned)(sizeof(pZip->abBuffer) - (pZip->u.LZF.pbOutput - &pZip->abBuffer[0]));
        if (    fForceFlush
            ||  cbFree < RTZIPLZF_MAX_DATA_SIZE + sizeof(RTZIPLZFHDR))
        {
            int rc = rtZipLZFCompFlushOutput(pZip);
            if (RT_FAILURE(rc))
                return rc;
            fForceFlush = false;
            cbFree = sizeof(pZip->abBuffer);
        }

        /*
         * Setup the block header.
         */
        PRTZIPLZFHDR pHdr = (PRTZIPLZFHDR)pZip->u.LZF.pbOutput; /* warning: This might be unaligned! */
        pHdr->u16Magic = RTZIPLZFHDR_MAGIC;
        pHdr->cbData = 0;
        pHdr->u32CRC = 0;
        pHdr->cbUncompressed = 0;
        cbFree -= sizeof(*pHdr);
        pZip->u.LZF.pbOutput += sizeof(*pHdr);

        /*
         * Compress data for the block.
         *
         * We try compress as much as we have freespace for at first,
         * but if it turns out the compression is inefficient, we'll
         * reduce the size of data we try compress till it fits the
         * output space.
         */
        cbFree = RT_MIN(cbFree, RTZIPLZF_MAX_DATA_SIZE);
        unsigned cbInput = (unsigned)RT_MIN(RTZIPLZF_MAX_UNCOMPRESSED_DATA_SIZE, cbBuf);
        unsigned cbOutput = lzf_compress(pbBuf, cbInput, pZip->u.LZF.pbOutput, cbFree);
        if (!cbOutput)
        {
            /** @todo add an alternative method which stores the raw data if bad compression. */
            do
            {
                cbInput /= 2;
                if (!cbInput)
                {
                    AssertMsgFailed(("lzf_compress bug! cbFree=%zu\n", cbFree));
                    return VERR_INTERNAL_ERROR;
                }
                cbOutput = lzf_compress(pbBuf, cbInput, pZip->u.LZF.pbOutput, cbFree);
            } while (!cbOutput);
            fForceFlush = true;
        }

        /*
         * Update the header and advance the input buffer.
         */
        pHdr->cbData = cbOutput;
        //pHdr->u32CRC = RTCrc32(pbBuf, cbInput); - too slow
        pHdr->cbUncompressed = cbInput;

        pZip->u.LZF.pbOutput += cbOutput;
        cbBuf -= cbInput;
        pbBuf += cbInput;
    }
    return VINF_SUCCESS;
}


/**
 * Flushes the input buffer.
 * @returns iprt status code.
 * @param   pZip        The compressor instance.
 */
static int rtZipLZFCompFlushInput(PRTZIPCOMP pZip)
{
    size_t cb = pZip->u.LZF.pbInput - &pZip->u.LZF.abInput[0];
    pZip->u.LZF.pbInput = &pZip->u.LZF.abInput[0];
    pZip->u.LZF.cbInputFree = sizeof(pZip->u.LZF.abInput);
    if (cb)
        return rtZipLZFCompressBuffer(pZip, pZip->u.LZF.abInput, cb);
    return VINF_SUCCESS;
}


/**
 * @copydoc RTZipCompress
 */
static DECLCALLBACK(int) rtZipLZFCompress(PRTZIPCOMP pZip, const void *pvBuf, size_t cbBuf)
{
#define RTZIPLZF_SMALL_CHUNK (128)

    /*
     * Flush the input buffer if necessary.
     */
    if (    (   cbBuf <= RTZIPLZF_SMALL_CHUNK
             && cbBuf > pZip->u.LZF.cbInputFree)
        ||  (   cbBuf > RTZIPLZF_SMALL_CHUNK
             && pZip->u.LZF.cbInputFree != sizeof(pZip->u.LZF.abInput))
       )
    {
        int rc = rtZipLZFCompFlushInput(pZip);
        if (RT_FAILURE(rc))
            return rc;
    }

    /*
     * If it's a relativly small block put it in the input buffer, elsewise
     * compress directly it.
     */
    if (cbBuf <= RTZIPLZF_SMALL_CHUNK)
    {
        Assert(pZip->u.LZF.cbInputFree >= cbBuf);
        memcpy(pZip->u.LZF.pbInput, pvBuf, cbBuf);
        pZip->u.LZF.pbInput += cbBuf;
        pZip->u.LZF.cbInputFree -= cbBuf;
    }
    else
    {
        Assert(pZip->u.LZF.cbInputFree == sizeof(pZip->u.LZF.abInput));
        int rc = rtZipLZFCompressBuffer(pZip, (const uint8_t *)pvBuf, cbBuf);
        if (RT_FAILURE(rc))
            return rc;
    }
    return VINF_SUCCESS;
}


/**
 * @copydoc RTZipCompFinish
 */
static DECLCALLBACK(int) rtZipLZFCompFinish(PRTZIPCOMP pZip)
{
    int rc = rtZipLZFCompFlushInput(pZip);
    if (RT_SUCCESS(rc))
        rc = rtZipLZFCompFlushOutput(pZip);
    return rc;
}


/**
 * @copydoc RTZipCompDestroy
 */
static DECLCALLBACK(int) rtZipLZFCompDestroy(PRTZIPCOMP pZip)
{
    NOREF(pZip);
    return VINF_SUCCESS;
}


/**
 * Initializes the compressor instance.
 * @returns iprt status code.
 * @param   pZip        The compressor instance.
 * @param   enmLevel    The desired compression level.
 */
static DECLCALLBACK(int) rtZipLZFCompInit(PRTZIPCOMP pZip, RTZIPLEVEL enmLevel)
{
    NOREF(enmLevel);
    pZip->pfnCompress = rtZipLZFCompress;
    pZip->pfnFinish   = rtZipLZFCompFinish;
    pZip->pfnDestroy  = rtZipLZFCompDestroy;

    pZip->u.LZF.pbOutput = &pZip->abBuffer[1];
    pZip->u.LZF.pbInput  = &pZip->u.LZF.abInput[0];
    pZip->u.LZF.cbInputFree = sizeof(pZip->u.LZF.abInput);
    return VINF_SUCCESS;
}


/**
 * This will validate a header and to all the necessary bitching if it's invalid.
 * @returns true if valid.
 * @returns false if invalid.
 * @param   pHdr        Pointer to the header.\
 */
static bool rtZipLZFValidHeader(PCRTZIPLZFHDR pHdr)
{
    if (    pHdr->u16Magic != RTZIPLZFHDR_MAGIC
        ||  !pHdr->cbData
        ||  pHdr->cbData > RTZIPLZF_MAX_DATA_SIZE
        ||  !pHdr->cbUncompressed
        ||  pHdr->cbUncompressed > RTZIPLZF_MAX_UNCOMPRESSED_DATA_SIZE
       )
    {
        AssertMsgFailed(("Invalid LZF header! %.*Rhxs\n", sizeof(*pHdr), pHdr));
        return false;
    }
    return true;
}


/**
 * @copydoc RTZipDecompress
 */
static DECLCALLBACK(int) rtZipLZFDecompress(PRTZIPDECOMP pZip, void *pvBuf, size_t cbBuf, size_t *pcbWritten)
{
    /*
     * Decompression loop.
     *
     * This a bit ugly because we have to deal with reading block...
     * To simplify matters we've put a max block size and will never
     * fill the input buffer with more than allows us to complete
     * any partially read blocks.
     *
     * When possible we decompress directly to the user buffer, when
     * not possible we'll use the spill buffer.
     */
# ifdef RTZIP_LZF_BLOCK_BY_BLOCK
    size_t cbWritten = 0;
    while (cbBuf > 0)
    {
        /*
         * Anything in the spill buffer?
         */
        if (pZip->u.LZF.cbSpill > 0)
        {
            unsigned cb = (unsigned)RT_MIN(pZip->u.LZF.cbSpill, cbBuf);
            memcpy(pvBuf, pZip->u.LZF.pbSpill, cb);
            pZip->u.LZF.pbSpill += cb;
            pZip->u.LZF.cbSpill -= cb;
            cbWritten += cb;
            cbBuf -= cb;
            if (!cbBuf)
                break;
            pvBuf = (uint8_t *)pvBuf + cb;
        }

        /*
         * We always read and work one block at a time.
         */
        RTZIPLZFHDR Hdr;
        int rc = pZip->pfnIn(pZip->pvUser, &Hdr, sizeof(Hdr), NULL);
        if (RT_FAILURE(rc))
            return rc;
        if (!rtZipLZFValidHeader(&Hdr))
            return VERR_GENERAL_FAILURE; /** @todo Get better error codes for RTZip! */
        if (Hdr.cbData > 0)
        {
            rc = pZip->pfnIn(pZip->pvUser, &pZip->abBuffer[0], Hdr.cbData, NULL);
            if (RT_FAILURE(rc))
                return rc;
        }

        /*
         * Does the uncompressed data fit into the supplied buffer?
         * If so we uncompress it directly into the user buffer, else we'll have to use the spill buffer.
         */
        unsigned cbUncompressed = Hdr.cbUncompressed;
        if (cbUncompressed <= cbBuf)
        {
            unsigned cbOutput = lzf_decompress(&pZip->abBuffer[0], Hdr.cbData, pvBuf, cbUncompressed);
            if (cbOutput != cbUncompressed)
            {
                AssertMsgFailed(("Decompression error, errno=%d. cbOutput=%#x cbUncompressed=%#x\n",
                                 errno, cbOutput, cbUncompressed));
                return VERR_GENERAL_FAILURE; /** @todo Get better error codes for RTZip! */
            }
            cbBuf -= cbUncompressed;
            pvBuf = (uint8_t *)pvBuf + cbUncompressed;
            cbWritten += cbUncompressed;
        }
        else
        {
            unsigned cbOutput = lzf_decompress(&pZip->abBuffer[0], Hdr.cbData, pZip->u.LZF.abSpill, cbUncompressed);
            if (cbOutput != cbUncompressed)
            {
                AssertMsgFailed(("Decompression error, errno=%d. cbOutput=%#x cbUncompressed=%#x\n",
                                 errno, cbOutput, cbUncompressed));
                return VERR_GENERAL_FAILURE; /** @todo Get better error codes for RTZip! */
            }
            pZip->u.LZF.pbSpill = &pZip->u.LZF.abSpill[0];
            pZip->u.LZF.cbSpill = cbUncompressed;
        }
    }

    if (pcbWritten)
        *pcbWritten = cbWritten;
# else  /* !RTZIP_LZF_BLOCK_BY_BLOCK */
    while (cbBuf > 0)
    {
        /*
         * Anything in the spill buffer?
         */
        if (pZip->u.LZF.cbSpill > 0)
        {
            unsigned cb = (unsigned)RT_MIN(pZip->u.LZF.cbSpill, cbBuf);
            memcpy(pvBuf, pZip->u.LZF.pbSpill, cb);
            pZip->u.LZF.pbSpill += cb;
            pZip->u.LZF.cbSpill -= cb;
            cbBuf -= cb;
            if (pcbWritten)
                *pcbWritten = cb;
            if (!cbBuf)
                break;
            pvBuf = (uint8_t *)pvBuf + cb;
        }

        /*
         * Incomplete header or nothing at all.
         */
        PCRTZIPLZFHDR pHdr;
        if (pZip->u.LZF.cbInput < sizeof(RTZIPLZFHDR))
        {
            if (pZip->u.LZF.cbInput <= 0)
            {
                /* empty, fill the buffer. */
                size_t cb = 0;
                int rc = pZip->pfnIn(pZip->pvUser, &pZip->abBuffer[0],
                                     sizeof(pZip->abBuffer) - RTZIPLZF_MAX_DATA_SIZE, &cb);
                if (RT_FAILURE(rc))
                    return rc;
                pZip->u.LZF.pbInput = &pZip->abBuffer[0];
                pZip->u.LZF.cbInput = cb;
                pHdr = (PCRTZIPLZFHDR)pZip->u.LZF.pbInput;
            }
            else
            {
                /* move the header up and fill the buffer. */
                size_t cbCur = pZip->u.LZF.cbInput;
                memmove(&pZip->abBuffer[0], pZip->u.LZF.pbInput, cbCur);
                pZip->u.LZF.pbInput = &pZip->abBuffer[0];

                size_t cb = 0;
                int rc = pZip->pfnIn(pZip->pvUser, &pZip->abBuffer[cbCur],
                                     sizeof(pZip->abBuffer) - RTZIPLZF_MAX_DATA_SIZE - cbCur, &cb);
                if (RT_FAILURE(rc))
                    return rc;
                pHdr = (PCRTZIPLZFHDR)pZip->u.LZF.pbInput;
                pZip->u.LZF.cbInput += cb;
            }

            /*
             * Validate the header.
             */
            if (!rtZipLZFValidHeader(pHdr))
                return VERR_GENERAL_FAILURE; /** @todo Get better error codes for RTZip! */
        }
        else
        {
            /*
             * Validate the header and check if it's an incomplete block.
             */
            pHdr = (PCRTZIPLZFHDR)pZip->u.LZF.pbInput;
            if (!rtZipLZFValidHeader(pHdr))
                return VERR_GENERAL_FAILURE; /** @todo Get better error codes for RTZip! */

            if (pHdr->cbData > pZip->u.LZF.cbInput - sizeof(*pHdr))
            {
                /* read the remainder of the block. */
                size_t cbToRead = pHdr->cbData - (pZip->u.LZF.cbInput - sizeof(*pHdr));
                Assert(&pZip->u.LZF.pbInput[pZip->u.LZF.cbInput + cbToRead] <= &pZip->u.LZF.pbInput[sizeof(pZip->abBuffer)]);
                int rc = pZip->pfnIn(pZip->pvUser, &pZip->u.LZF.pbInput[pZip->u.LZF.cbInput],
                                     cbToRead, NULL);
                if (RT_FAILURE(rc))
                    return rc;
                pZip->u.LZF.cbInput += cbToRead;
            }
        }
        AssertMsgReturn(sizeof(*pHdr) + pHdr->cbData <= pZip->u.LZF.cbInput,
                        ("cbData=%#x cbInput=%#x\n", pHdr->cbData, pZip->u.LZF.cbInput),
                        VERR_GENERAL_FAILURE); /** @todo Get better error codes for RTZip! */

        /*
         * Does the uncompressed data fit into the supplied buffer?
         * If so we uncompress it directly into the user buffer, else we'll have to use the spill buffer.
         */
        unsigned cbUncompressed = pHdr->cbUncompressed;
        if (cbUncompressed <= cbBuf)
        {
            unsigned cbOutput = lzf_decompress(pHdr + 1, pHdr->cbData, pvBuf, cbUncompressed);
            if (cbOutput != cbUncompressed)
            {
                AssertMsgFailed(("Decompression error, errno=%d. cbOutput=%#x cbUncompressed=%#x\n",
                                 errno, cbOutput, cbUncompressed));
                return VERR_GENERAL_FAILURE; /** @todo Get better error codes for RTZip! */
            }
            cbBuf -= cbUncompressed;
            pvBuf = (uint8_t *)pvBuf + cbUncompressed;
        }
        else
        {
            unsigned cbOutput = lzf_decompress(pHdr + 1, pHdr->cbData, pZip->u.LZF.abSpill, cbUncompressed);
            if (cbOutput != cbUncompressed)
            {
                AssertMsgFailed(("Decompression error, errno=%d. cbOutput=%#x cbUncompressed=%#x\n",
                                 errno, cbOutput, cbUncompressed));
                return VERR_GENERAL_FAILURE; /** @todo Get better error codes for RTZip! */
            }
            pZip->u.LZF.pbSpill = &pZip->u.LZF.abSpill[0];
            pZip->u.LZF.cbSpill = cbUncompressed;
        }

        /* advance the input buffer */
        pZip->u.LZF.cbInput -= pHdr->cbData + sizeof(*pHdr);
        pZip->u.LZF.pbInput += pHdr->cbData + sizeof(*pHdr);
        if (pcbWritten)
            *pcbWritten += cbUncompressed;
    }
# endif /* !RTZIP_LZF_BLOCK_BY_BLOCK */
    return VINF_SUCCESS;
}


/**
 * @copydoc RTZipDecompDestroy
 */
static DECLCALLBACK(int) rtZipLZFDecompDestroy(PRTZIPDECOMP pZip)
{
    NOREF(pZip);
    return VINF_SUCCESS;
}


/**
 * Initialize the decompressor instance.
 * @returns iprt status code.
 * @param   pZip        The decompressor instance.
 */
static DECLCALLBACK(int) rtZipLZFDecompInit(PRTZIPDECOMP pZip)
{
    pZip->pfnDecompress = rtZipLZFDecompress;
    pZip->pfnDestroy = rtZipLZFDecompDestroy;

# ifndef RTZIP_LZF_BLOCK_BY_BLOCK
    pZip->u.LZF.pbInput    = NULL;
    pZip->u.LZF.cbInput    = 0;
# endif
    pZip->u.LZF.cbSpill    = 0;
    pZip->u.LZF.pbSpill    = NULL;

    return VINF_SUCCESS;
}

#endif /* RTZIP_USE_LZF */


/**
 * Create a compressor instance.
 *
 * @returns iprt status code.
 * @param   ppZip       Where to store the instance handle.
 * @param   pvUser      User argument which will be passed on to pfnOut and pfnIn.
 * @param   pfnOut      Callback for consuming output of compression.
 * @param   enmType     Type of compressor to create.
 * @param   enmLevel    Compression level.
 */
RTDECL(int)     RTZipCompCreate(PRTZIPCOMP *ppZip, void *pvUser, PFNRTZIPOUT pfnOut, RTZIPTYPE enmType, RTZIPLEVEL enmLevel)
{
    /*
     * Validate input.
     */
    AssertReturn(enmType >= RTZIPTYPE_INVALID && enmType < RTZIPTYPE_END, VERR_INVALID_PARAMETER);
    AssertReturn(enmLevel >= RTZIPLEVEL_STORE && enmLevel <= RTZIPLEVEL_MAX, VERR_INVALID_PARAMETER);
    AssertPtrReturn(pfnOut, VERR_INVALID_POINTER);
    AssertPtrReturn(ppZip, VERR_INVALID_POINTER);

    /*
     * Allocate memory for the instance data.
     */
    PRTZIPCOMP pZip = (PRTZIPCOMP)RTMemAlloc(sizeof(RTZIPCOMP));
    if (!pZip)
        return VERR_NO_MEMORY;

    /*
     * Determine auto type.
     */
    if (enmType == RTZIPTYPE_AUTO)
    {
        if (enmLevel == RTZIPLEVEL_STORE)
            enmType = RTZIPTYPE_STORE;
        else
        {
#if defined(RTZIP_USE_ZLIB) && defined(RTZIP_USE_BZLIB)
            if (enmLevel == RTZIPLEVEL_MAX)
                enmType = RTZIPTYPE_BZLIB;
            else
                enmType = RTZIPTYPE_ZLIB;
#elif defined(RTZIP_USE_ZLIB)
            enmType = RTZIPTYPE_ZLIB;
#elif defined(RTZIP_USE_BZLIB)
            enmType = RTZIPTYPE_BZLIB;
#else
            enmType = RTZIPTYPE_STORE;
#endif
        }
    }

    /*
     * Init instance.
     */
    pZip->pfnOut  = pfnOut;
    pZip->enmType = enmType;
    pZip->pvUser  = pvUser;
    pZip->abBuffer[0] = enmType;       /* first byte is the compression type. */
    int rc = VERR_NOT_IMPLEMENTED;
    switch (enmType)
    {
        case RTZIPTYPE_STORE:
#ifdef RTZIP_USE_STORE
            rc = rtZipStoreCompInit(pZip, enmLevel);
#endif
            break;

        case RTZIPTYPE_ZLIB:
        case RTZIPTYPE_ZLIB_NO_HEADER:
#ifdef RTZIP_USE_ZLIB
            rc = rtZipZlibCompInit(pZip, enmLevel, enmType == RTZIPTYPE_ZLIB /*fZlibHeader*/);
#endif
            break;

        case RTZIPTYPE_BZLIB:
#ifdef RTZIP_USE_BZLIB
            rc = rtZipBZlibCompInit(pZip, enmLevel);
#endif
            break;

        case RTZIPTYPE_LZF:
#ifdef RTZIP_USE_LZF
            rc = rtZipLZFCompInit(pZip, enmLevel);
#endif
            break;

        case RTZIPTYPE_LZJB:
        case RTZIPTYPE_LZO:
            break;

        default:
            AssertFailedBreak();
    }

    if (RT_SUCCESS(rc))
        *ppZip = pZip;
    else
        RTMemFree(pZip);
    return rc;
}
RT_EXPORT_SYMBOL(RTZipCompCreate);


/**
 * Compresses a chunk of memory.
 *
 * @returns iprt status code.
 * @param   pZip        The compressor instance.
 * @param   pvBuf       Pointer to buffer containing the bits to compress.
 * @param   cbBuf       Number of bytes to compress.
 */
RTDECL(int)     RTZipCompress(PRTZIPCOMP pZip, const void *pvBuf, size_t cbBuf)
{
    if (!cbBuf)
        return VINF_SUCCESS;
    return pZip->pfnCompress(pZip, pvBuf, cbBuf);
}
RT_EXPORT_SYMBOL(RTZipCompress);


/**
 * Finishes the compression.
 * This will flush all data and terminate the compression data stream.
 *
 * @returns iprt status code.
 * @param   pZip        The compressor instance.
 */
RTDECL(int)     RTZipCompFinish(PRTZIPCOMP pZip)
{
    return pZip->pfnFinish(pZip);
}
RT_EXPORT_SYMBOL(RTZipCompFinish);


/**
 * Destroys the compressor instance.
 *
 * @returns iprt status code.
 * @param   pZip        The compressor instance.
 */
RTDECL(int)     RTZipCompDestroy(PRTZIPCOMP pZip)
{
    /*
     * Compressor specific destruction attempt first.
     */
    int rc = pZip->pfnDestroy(pZip);
    AssertRCReturn(rc, rc);

    /*
     * Free the instance memory.
     */
    pZip->enmType = RTZIPTYPE_INVALID;
    RTMemFree(pZip);
    return VINF_SUCCESS;
}
RT_EXPORT_SYMBOL(RTZipCompDestroy);


/**
 * @copydoc RTZipDecompress
 */
static DECLCALLBACK(int) rtZipStubDecompress(PRTZIPDECOMP pZip, void *pvBuf, size_t cbBuf, size_t *pcbWritten)
{
    NOREF(pZip); NOREF(pvBuf); NOREF(cbBuf); NOREF(pcbWritten);
    return VERR_NOT_SUPPORTED;
}


/**
 * @copydoc RTZipDecompDestroy
 */
static DECLCALLBACK(int) rtZipStubDecompDestroy(PRTZIPDECOMP pZip)
{
    NOREF(pZip);
    return VINF_SUCCESS;
}


/**
 * Create a decompressor instance.
 *
 * @returns iprt status code.
 * @param   ppZip       Where to store the instance handle.
 * @param   pvUser      User argument which will be passed on to pfnOut and pfnIn.
 * @param   pfnIn       Callback for producing input for decompression.
 */
RTDECL(int)     RTZipDecompCreate(PRTZIPDECOMP *ppZip, void *pvUser, PFNRTZIPIN pfnIn)
{
    /*
     * Validate input.
     */
    AssertPtrReturn(pfnIn, VERR_INVALID_POINTER);
    AssertPtrReturn(ppZip, VERR_INVALID_POINTER);

    /*
     * Allocate memory for the instance data.
     */
    PRTZIPDECOMP pZip = (PRTZIPDECOMP)RTMemAlloc(sizeof(RTZIPDECOMP));
    if (!pZip)
        return VERR_NO_MEMORY;

    /*
     * Init instance.
     */
    pZip->pfnIn   = pfnIn;
    pZip->enmType = RTZIPTYPE_INVALID;
    pZip->pvUser  = pvUser;
    pZip->pfnDecompress = NULL;
    pZip->pfnDestroy = rtZipStubDecompDestroy;

    *ppZip = pZip;
    return VINF_SUCCESS;
}
RT_EXPORT_SYMBOL(RTZipDecompCreate);


/**
 * Lazy init of the decompressor.
 * @returns iprt status code.
 * @param   pZip        The decompressor instance.
 */
static int rtzipDecompInit(PRTZIPDECOMP pZip)
{
    /*
     * Read the first byte from the stream so we can determine the type.
     */
    uint8_t u8Type;
    int rc = pZip->pfnIn(pZip->pvUser, &u8Type, sizeof(u8Type), NULL);
    if (RT_FAILURE(rc))
        return rc;

    /*
     * Determine type and do type specific init.
     */
    pZip->enmType = (RTZIPTYPE)u8Type;
    rc = VERR_NOT_SUPPORTED;
    switch (pZip->enmType)
    {
        case RTZIPTYPE_STORE:
#ifdef RTZIP_USE_STORE
            rc = rtZipStoreDecompInit(pZip);
#else
            AssertMsgFailed(("Store is not include in this build!\n"));
#endif
            break;

        case RTZIPTYPE_ZLIB:
        case RTZIPTYPE_ZLIB_NO_HEADER:
#ifdef RTZIP_USE_ZLIB
            rc = rtZipZlibDecompInit(pZip, pZip->enmType == RTZIPTYPE_ZLIB /*fHeader*/);
#else
            AssertMsgFailed(("Zlib is not include in this build!\n"));
#endif
            break;

        case RTZIPTYPE_BZLIB:
#ifdef RTZIP_USE_BZLIB
            rc = rtZipBZlibDecompInit(pZip);
#else
            AssertMsgFailed(("BZlib is not include in this build!\n"));
#endif
            break;

        case RTZIPTYPE_LZF:
#ifdef RTZIP_USE_LZF
            rc = rtZipLZFDecompInit(pZip);
#else
            AssertMsgFailed(("LZF is not include in this build!\n"));
#endif
            break;

        case RTZIPTYPE_LZJB:
#ifdef RTZIP_USE_LZJB
            AssertMsgFailed(("LZJB streaming support is not implemented yet!\n"));
#else
            AssertMsgFailed(("LZJB is not include in this build!\n"));
#endif
            break;

        case RTZIPTYPE_LZO:
#ifdef RTZIP_USE_LZJB
            AssertMsgFailed(("LZO streaming support is not implemented yet!\n"));
#else
            AssertMsgFailed(("LZO is not include in this build!\n"));
#endif
            break;

        default:
            AssertMsgFailed(("Invalid compression type %d (%#x)!\n", pZip->enmType, pZip->enmType));
            rc = VERR_INVALID_MAGIC;
            break;
    }
    if (RT_FAILURE(rc))
    {
        pZip->pfnDecompress = rtZipStubDecompress;
        pZip->pfnDestroy = rtZipStubDecompDestroy;
    }

    return rc;
}


/**
 * Decompresses a chunk of memory.
 *
 * @returns iprt status code.
 * @param   pZip        The decompressor instance.
 * @param   pvBuf       Where to store the decompressed data.
 * @param   cbBuf       Number of bytes to produce. If pcbWritten is set
 *                      any number of bytes up to cbBuf might be returned.
 * @param   pcbWritten  Number of bytes actually written to the buffer. If NULL
 *                      cbBuf number of bytes must be written.
 */
RTDECL(int)     RTZipDecompress(PRTZIPDECOMP pZip, void *pvBuf, size_t cbBuf, size_t *pcbWritten)
{
    /*
     * Skip empty requests.
     */
    if (!cbBuf)
        return VINF_SUCCESS;

    /*
     * Lazy init.
     */
    if (!pZip->pfnDecompress)
    {
        int rc = rtzipDecompInit(pZip);
        if (RT_FAILURE(rc))
            return rc;
    }

    /*
     * 'Read' the decompressed stream.
     */
    return pZip->pfnDecompress(pZip, pvBuf, cbBuf, pcbWritten);
}
RT_EXPORT_SYMBOL(RTZipDecompress);


/**
 * Destroys the decompressor instance.
 *
 * @returns iprt status code.
 * @param   pZip        The decompressor instance.
 */
RTDECL(int)     RTZipDecompDestroy(PRTZIPDECOMP pZip)
{
    /*
     * Destroy compressor instance and flush the output buffer.
     */
    int rc = pZip->pfnDestroy(pZip);
    AssertRCReturn(rc, rc);

    /*
     * Free the instance memory.
     */
    pZip->enmType = RTZIPTYPE_INVALID;
    RTMemFree(pZip);
    return rc;
}
RT_EXPORT_SYMBOL(RTZipDecompDestroy);


RTDECL(int) RTZipBlockCompress(RTZIPTYPE enmType, RTZIPLEVEL enmLevel, uint32_t fFlags,
                               void const *pvSrc, size_t cbSrc,
                               void *pvDst, size_t cbDst, size_t *pcbDstActual) RT_NO_THROW_DEF
{
    /* input validation - the crash and burn approach as speed is essential here. */
    Assert(enmLevel <= RTZIPLEVEL_MAX && enmLevel >= RTZIPLEVEL_STORE); RT_NOREF_PV(enmLevel);
    Assert(!fFlags);                                                    RT_NOREF_PV(fFlags);

    /*
     * Deal with flags involving prefixes.
     */
    /** @todo later: type and/or compressed length prefix. */

    /*
     * The type specific part.
     */
    switch (enmType)
    {
        case RTZIPTYPE_LZF:
        {
#ifdef RTZIP_USE_LZF
# if 0
            static const uint8_t s_abZero4K[] =
            {
                0x01, 0x00, 0x00, 0xe0, 0xff, 0x00, 0xe0, 0xff,
                0x00, 0xe0, 0xff, 0x00, 0xe0, 0xff, 0x00, 0xe0,
                0xff, 0x00, 0xe0, 0xff, 0x00, 0xe0, 0xff, 0x00,
                0xe0, 0xff, 0x00, 0xe0, 0xff, 0x00, 0xe0, 0xff,
                0x00, 0xe0, 0xff, 0x00, 0xe0, 0xff, 0x00, 0xe0,
                0xff, 0x00, 0xe0, 0xff, 0x00, 0xe0, 0xff, 0x00,
                0xe0, 0x7d, 0x00
            };
            if (    cbSrc == _4K
                &&  !((uintptr_t)pvSrc & 15)
                &&  ASMMemIsZeroPage(pvSrc))
            {
                if (RT_UNLIKELY(cbDst < sizeof(s_abZero4K)))
                    return VERR_BUFFER_OVERFLOW;
                memcpy(pvDst, s_abZero4K, sizeof(s_abZero4K));
                *pcbDstActual = sizeof(s_abZero4K);
                break;
            }
# endif

            unsigned cbDstActual = lzf_compress(pvSrc, (unsigned)cbSrc, pvDst, (unsigned)cbDst);    /** @todo deal with size type overflows */
            if (RT_UNLIKELY(cbDstActual < 1))
                return VERR_BUFFER_OVERFLOW;
            *pcbDstActual = cbDstActual;
            break;
#else
            return VERR_NOT_SUPPORTED;
#endif
        }

        case RTZIPTYPE_STORE:
        {
            if (cbDst < cbSrc)
                return VERR_BUFFER_OVERFLOW;
            memcpy(pvDst, pvSrc, cbSrc);
            *pcbDstActual = cbSrc;
            break;
        }

        case RTZIPTYPE_LZJB:
        {
#ifdef RTZIP_USE_LZJB
            AssertReturn(cbDst > cbSrc, VERR_BUFFER_OVERFLOW);
            size_t cbDstActual = lzjb_compress((void *)pvSrc, (uint8_t *)pvDst + 1, cbSrc, cbSrc, 0 /*??*/);
            if (cbDstActual == cbSrc)
                *(uint8_t *)pvDst = 0;
            else
                *(uint8_t *)pvDst = 1;
            *pcbDstActual = cbDstActual + 1;
            break;
#else
            return VERR_NOT_SUPPORTED;
#endif
        }

        case RTZIPTYPE_LZO:
        {
#ifdef RTZIP_USE_LZO
            uint64_t Scratch[RT_ALIGN(LZO1X_1_MEM_COMPRESS, sizeof(uint64_t)) / sizeof(uint64_t)];
            int rc = lzo_init();
            if (RT_UNLIKELY(rc != LZO_E_OK))
                return VERR_INTERNAL_ERROR;

            lzo_uint cbDstInOut = cbDst;
            rc = lzo1x_1_compress((const lzo_bytep)pvSrc, cbSrc, (lzo_bytep )pvDst, &cbDstInOut, &Scratch[0]);
            if (RT_UNLIKELY(rc != LZO_E_OK))
                switch (rc)
                {
                    case LZO_E_OUTPUT_OVERRUN:  return VERR_BUFFER_OVERFLOW;
                    default:                    return VERR_GENERAL_FAILURE;
                }
            *pcbDstActual = cbDstInOut;
            break;
#else
            return VERR_NOT_SUPPORTED;
#endif
        }

        case RTZIPTYPE_ZLIB:
        case RTZIPTYPE_BZLIB:
            return VERR_NOT_SUPPORTED;

        default:
            AssertMsgFailed(("%d\n", enmType));
            return VERR_INVALID_PARAMETER;
    }

    return VINF_SUCCESS;
}
RT_EXPORT_SYMBOL(RTZipBlockCompress);


RTDECL(int) RTZipBlockDecompress(RTZIPTYPE enmType, uint32_t fFlags,
                                 void const *pvSrc, size_t cbSrc, size_t *pcbSrcActual,
                                 void *pvDst, size_t cbDst, size_t *pcbDstActual) RT_NO_THROW_DEF
{
    /* input validation - the crash and burn approach as speed is essential here. */
    Assert(!fFlags); RT_NOREF_PV(fFlags);

    /*
     * Deal with flags involving prefixes.
     */
    /** @todo later: type and/or compressed length prefix. */

    /*
     * The type specific part.
     */
    switch (enmType)
    {
        case RTZIPTYPE_LZF:
        {
#ifdef RTZIP_USE_LZF
            unsigned cbDstActual = lzf_decompress(pvSrc, (unsigned)cbSrc, pvDst, (unsigned)cbDst);  /** @todo deal with size type overflows */
            if (RT_UNLIKELY(cbDstActual < 1))
            {
                if (errno == E2BIG)
                    return VERR_BUFFER_OVERFLOW;
                Assert(errno == EINVAL);
                return VERR_GENERAL_FAILURE;
            }
            if (pcbDstActual)
                *pcbDstActual = cbDstActual;
            if (pcbSrcActual)
                *pcbSrcActual = cbSrc;
            break;
#else
            return VERR_NOT_SUPPORTED;
#endif
        }

        case RTZIPTYPE_STORE:
        {
            if (cbDst < cbSrc)
                return VERR_BUFFER_OVERFLOW;
            memcpy(pvDst, pvSrc, cbSrc);
            if (pcbDstActual)
                *pcbDstActual = cbSrc;
            if (pcbSrcActual)
                *pcbSrcActual = cbSrc;
            break;
        }

        case RTZIPTYPE_LZJB:
        {
#ifdef RTZIP_USE_LZJB
            if (*(uint8_t *)pvSrc == 1)
            {
                int rc = lzjb_decompress((uint8_t *)pvSrc + 1, pvDst, cbSrc - 1, cbDst, 0 /*??*/);
                if (RT_UNLIKELY(rc != 0))
                    return VERR_GENERAL_FAILURE;
                if (pcbDstActual)
                    *pcbDstActual = cbDst;
            }
            else
            {
                AssertReturn(cbDst >= cbSrc - 1, VERR_BUFFER_OVERFLOW);
                memcpy(pvDst, (uint8_t *)pvSrc + 1, cbSrc - 1);
                if (pcbDstActual)
                    *pcbDstActual = cbSrc - 1;
            }
            if (pcbSrcActual)
                *pcbSrcActual = cbSrc;
            break;
#else
            return VERR_NOT_SUPPORTED;
#endif
        }

        case RTZIPTYPE_LZO:
        {
#ifdef RTZIP_USE_LZO
            int rc = lzo_init();
            if (RT_UNLIKELY(rc != LZO_E_OK))
                return VERR_INTERNAL_ERROR;
            lzo_uint cbDstInOut = cbDst;
            rc = lzo1x_decompress((const lzo_bytep)pvSrc, cbSrc, (lzo_bytep)pvDst, &cbDstInOut, NULL);
            if (RT_UNLIKELY(rc != LZO_E_OK))
                switch (rc)
                {
                    case LZO_E_OUTPUT_OVERRUN:  return VERR_BUFFER_OVERFLOW;
                    default:
                    case LZO_E_INPUT_OVERRUN:   return VERR_GENERAL_FAILURE;
                }
            if (pcbSrcActual)
                *pcbSrcActual = cbSrc;
            if (pcbDstActual)
                *pcbDstActual = cbDstInOut;
            break;
#else
            return VERR_NOT_SUPPORTED;
#endif
        }

        case RTZIPTYPE_ZLIB:
        {
#ifdef RTZIP_USE_ZLIB
            AssertReturn(cbSrc == (uInt)cbSrc, VERR_TOO_MUCH_DATA);
            AssertReturn(cbDst == (uInt)cbDst, VERR_OUT_OF_RANGE);

            z_stream ZStrm;
            RT_ZERO(ZStrm);
            ZStrm.next_in   = (Bytef *)pvSrc;
            ZStrm.avail_in  = (uInt)cbSrc;
            ZStrm.next_out  = (Bytef *)pvDst;
            ZStrm.avail_out = (uInt)cbDst;

            int rc = inflateInit(&ZStrm);
            if (RT_UNLIKELY(rc != Z_OK))
                return zipErrConvertFromZlib(rc, false /*fCompressing*/);
            rc = inflate(&ZStrm, Z_FINISH);
            if (rc != Z_STREAM_END)
            {
                inflateEnd(&ZStrm);
                if ((rc == Z_BUF_ERROR && ZStrm.avail_in == 0) || rc == Z_NEED_DICT)
                    return VERR_ZIP_CORRUPTED;
                if (rc == Z_BUF_ERROR)
                    return VERR_BUFFER_OVERFLOW;
                AssertReturn(rc < Z_OK, VERR_GENERAL_FAILURE);
                return zipErrConvertFromZlib(rc, false /*fCompressing*/);
            }
            rc = inflateEnd(&ZStrm);
            if (rc != Z_OK)
                return zipErrConvertFromZlib(rc, false /*fCompressing*/);

            if (pcbSrcActual)
                *pcbSrcActual = cbSrc - ZStrm.avail_in;
            if (pcbDstActual)
                *pcbDstActual = ZStrm.total_out;
            break;
#else
            return VERR_NOT_SUPPORTED;
#endif
        }

        case RTZIPTYPE_BZLIB:
            return VERR_NOT_SUPPORTED;

        default:
            AssertMsgFailed(("%d\n", enmType));
            return VERR_INVALID_PARAMETER;
    }
    return VINF_SUCCESS;
}
RT_EXPORT_SYMBOL(RTZipBlockDecompress);