summaryrefslogtreecommitdiffstats
path: root/src/VBox/Runtime/common/zip/cpiovfs.cpp
blob: 04dc8675645a2959d0287b02d40561fcf73eea50 (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
/* $Id: cpiovfs.cpp $ */
/** @file
 * IPRT - CPIO Virtual Filesystem, Reader.
 */

/*
 * Copyright (C) 2020-2023 Oracle and/or its affiliates.
 *
 * This file is part of VirtualBox base platform packages, as
 * available from https://www.virtualbox.org.
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation, in version 3 of the
 * License.
 *
 * This program is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, see <https://www.gnu.org/licenses>.
 *
 * The contents of this file may alternatively be used under the terms
 * of the Common Development and Distribution License Version 1.0
 * (CDDL), a copy of it is provided in the "COPYING.CDDL" file included
 * in the VirtualBox 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.
 *
 * SPDX-License-Identifier: GPL-3.0-only OR CDDL-1.0
 */


/*********************************************************************************************************************************
*   Header Files                                                                                                                 *
*********************************************************************************************************************************/
#include "internal/iprt.h"
#include <iprt/zip.h>

#include <iprt/asm.h>
#include <iprt/assert.h>
#include <iprt/ctype.h>
#include <iprt/err.h>
#include <iprt/poll.h>
#include <iprt/file.h>
#include <iprt/string.h>
#include <iprt/vfs.h>
#include <iprt/vfslowlevel.h>

#include "cpiovfsreader.h"


/**
 * Converts a octal numeric header field to the C native type.
 *
 * @returns IPRT status code.
 * @param   pachField           The CPIO header field.
 * @param   cchField            The length of the field.
 * @param   pi64                Where to store the value.
 */
static int rtZipCpioHdrOctalFieldToNum(const char *pachField, size_t cchField, int64_t *pi64)
{
    /*
     * Skip leading zeros to save a few slower loops below.
     */
    while (cchField > 0 && *pachField == '0')
        cchField--, pachField++;

    /*
     * Convert octal digits.
     */
    int64_t i64 = 0;
    while (cchField > 0)
    {
        unsigned char uDigit = *pachField - '0';
        if (uDigit >= 8)
            return VERR_TAR_BAD_NUM_FIELD;
        i64 <<= 3;
        i64 |= uDigit;

        pachField++;
        cchField--;
    }
    *pi64 = i64;

    return VINF_SUCCESS;
}


/**
 * Converts a hex character to the appropriate nibble.
 *
 * @returns Nibble of the character.
 * @param   chVal                   The value to convert.
 */
static inline uint8_t rtZipCpioHexToNibble(char chVal)
{
    if (chVal >= '0' && chVal <= '9')
        return chVal - '0';
    else if (chVal >= 'a' && chVal <= 'f')
        return chVal - 'a' + 10;
    else if (chVal >= 'A' && chVal <= 'F')
        return chVal - 'A' + 10;

    return 0xff;
}


/**
 * Converts a hexadecimal numeric header field to the C native type.
 *
 * @returns IPRT status code.
 * @param   pachField           The CPIO header field.
 * @param   cchField            The length of the field.
 * @param   pi64                Where to store the value.
 */
static int rtZipCpioHdrHexFieldToNum(const char *pachField, size_t cchField, int64_t *pi64)
{
    uint64_t u64 = 0;

    while (cchField-- > 0)
    {
        uint8_t bNb = rtZipCpioHexToNibble(*pachField++);

        if (RT_LIKELY(bNb != 0xff))
            u64 = (u64 << 4) | bNb;
        else
            return VERR_TAR_BAD_NUM_FIELD;
    }

    *pi64 = (int64_t)u64;
    return VINF_SUCCESS;
}


/**
 * Parses the given ancient binary header and converts it to an FS object info structure.
 *
 * @returns IPRT status code.
 * @param   pThis               The CPIO reader state.
 * @param   pHdr                The header to convert.
 * @param   pcbFilePath         Where to store the file path size on success.
 * @param   pcbPad              Where to store the number of bytes padded after the header and file path
 *                              before the content begins.
 */
static int rtZipCpioReaderParseHeaderAncientBin(PRTZIPCPIOREADER pThis, PCCPIOHDRBIN pHdr,
                                                uint32_t *pcbFilePath, uint32_t *pcbPad)
{
    RT_NOREF(pThis, pHdr, pcbFilePath, pcbPad);
    return VERR_NOT_SUPPORTED;
}


/**
 * Parses the given SuSv2 ASCII header and converts it to an FS object info structure.
 *
 * @returns IPRT status code.
 * @param   pThis               The CPIO reader state.
 * @param   pHdr                The header to convert.
 * @param   pcbFilePath         Where to store the file path size on success.
 * @param   pcbPad              Where to store the number of bytes padded after the header and file path
 *                              before the content begins.
 */
static int rtZipCpioReaderParseHeaderAsciiSusV2(PRTZIPCPIOREADER pThis, PCCPIOHDRSUSV2 pHdr,
                                                uint32_t *pcbFilePath, uint32_t *pcbPad)
{
    PRTFSOBJINFO pObjInfo = &pThis->ObjInfo;
    int rc;
    int64_t i64Tmp;
    int64_t c64SecModTime;

    pObjInfo->Attr.u.Unix.INodeIdDevice = 0;
    pObjInfo->Attr.u.Unix.Device        = 0;
    pObjInfo->Attr.enmAdditional        = RTFSOBJATTRADD_UNIX;

#define GET_CPIO_NUMERIC_FIELD_RET(a_Var, a_Field) \
        do { \
            rc = rtZipCpioHdrOctalFieldToNum(a_Field, sizeof(a_Field), &i64Tmp); \
            if (RT_FAILURE(rc)) \
                return rc; \
            (a_Var) = i64Tmp; \
            if ((a_Var) != i64Tmp) \
                return VERR_TAR_NUM_VALUE_TOO_LARGE; \
        } while (0)

#define GET_CPIO_NUMERIC_FIELD_RET_U64(a_Var, a_Field) \
        do { \
            rc = rtZipCpioHdrOctalFieldToNum(a_Field, sizeof(a_Field), &i64Tmp); \
            if (RT_FAILURE(rc)) \
                return rc; \
            (a_Var) = (uint64_t)i64Tmp; \
            if ((a_Var) != (uint64_t)i64Tmp) \
                return VERR_TAR_NUM_VALUE_TOO_LARGE; \
        } while (0)

    GET_CPIO_NUMERIC_FIELD_RET(    pObjInfo->Attr.fMode,             pHdr->achMode);
    GET_CPIO_NUMERIC_FIELD_RET(    pObjInfo->Attr.u.Unix.uid,        pHdr->achUid);
    GET_CPIO_NUMERIC_FIELD_RET(    pObjInfo->Attr.u.Unix.gid,        pHdr->achGid);
    GET_CPIO_NUMERIC_FIELD_RET(    pObjInfo->Attr.u.Unix.cHardlinks, pHdr->achNLinks);
    GET_CPIO_NUMERIC_FIELD_RET_U64(pObjInfo->Attr.u.Unix.INodeId,    pHdr->achInode);
    GET_CPIO_NUMERIC_FIELD_RET(    pObjInfo->Attr.u.Unix.Device,     pHdr->achDev);
    GET_CPIO_NUMERIC_FIELD_RET(    pObjInfo->cbObject,               pHdr->achFileSize);
    pObjInfo->cbAllocated = pObjInfo->cbObject;
    GET_CPIO_NUMERIC_FIELD_RET(    c64SecModTime,                    pHdr->achMTime);
    RTTimeSpecSetSeconds(&pObjInfo->ChangeTime,                  c64SecModTime);
    RTTimeSpecSetSeconds(&pObjInfo->ModificationTime,            c64SecModTime);
    RTTimeSpecSetSeconds(&pObjInfo->AccessTime,                  c64SecModTime);
    RTTimeSpecSetSeconds(&pObjInfo->BirthTime,                   c64SecModTime);
    if (c64SecModTime != RTTimeSpecGetSeconds(&pObjInfo->ModificationTime))
        return VERR_TAR_NUM_VALUE_TOO_LARGE;

    GET_CPIO_NUMERIC_FIELD_RET(*pcbFilePath, pHdr->achNameSize);

    /* There is never any padding. */
    *pcbPad = 0;

#undef GET_CPIO_NUMERIC_FIELD_RET
#undef GET_CPIO_NUMERIC_FIELD_RET_U64

    return rc;
}


/**
 * Parses the given "new" ASCII header and converts it to an FS object info structure.
 *
 * @returns IPRT status code.
 * @param   pThis               The CPIO reader state.
 * @param   pHdr                The header to convert.
 * @param   fWithChksum         Flag whether the header uses the checksum field.
 * @param   pcbFilePath         Where to store the file path size on success.
 * @param   pcbPad              Where to store the number of bytes padded after the header and file path
 *                              before the content begins.
 */
static int rtZipCpioReaderParseHeaderAsciiNew(PRTZIPCPIOREADER pThis, PCCPIOHDRNEW pHdr, bool fWithChksum,
                                              uint32_t *pcbFilePath, uint32_t *pcbPad)
{
    RT_NOREF(fWithChksum); /** @todo */
    PRTFSOBJINFO pObjInfo = &pThis->ObjInfo;
    int rc;
    int64_t i64Tmp;
    int64_t c64SecModTime;
    uint32_t uMajor, uMinor;

    pObjInfo->Attr.u.Unix.INodeIdDevice = 0;
    pObjInfo->Attr.u.Unix.Device        = 0;
    pObjInfo->Attr.enmAdditional        = RTFSOBJATTRADD_UNIX;

#define GET_CPIO_NUMERIC_FIELD_RET(a_Var, a_Field) \
        do { \
            rc = rtZipCpioHdrHexFieldToNum(a_Field, sizeof(a_Field), &i64Tmp); \
            if (RT_FAILURE(rc)) \
                return rc; \
            (a_Var) = i64Tmp; \
            if ((a_Var) != i64Tmp) \
                return VERR_TAR_NUM_VALUE_TOO_LARGE; \
        } while (0)

#define GET_CPIO_NUMERIC_FIELD_RET_U64(a_Var, a_Field) \
        do { \
            rc = rtZipCpioHdrHexFieldToNum(a_Field, sizeof(a_Field), &i64Tmp); \
            if (RT_FAILURE(rc)) \
                return rc; \
            (a_Var) = (uint64_t)i64Tmp; \
            if ((a_Var) != (uint64_t)i64Tmp) \
                return VERR_TAR_NUM_VALUE_TOO_LARGE; \
        } while (0)

    GET_CPIO_NUMERIC_FIELD_RET(    pObjInfo->Attr.fMode,             pHdr->achMode);
    GET_CPIO_NUMERIC_FIELD_RET(    pObjInfo->Attr.u.Unix.uid,        pHdr->achUid);
    GET_CPIO_NUMERIC_FIELD_RET(    pObjInfo->Attr.u.Unix.gid,        pHdr->achGid);
    GET_CPIO_NUMERIC_FIELD_RET(    pObjInfo->Attr.u.Unix.cHardlinks, pHdr->achNLinks);
    GET_CPIO_NUMERIC_FIELD_RET_U64(pObjInfo->Attr.u.Unix.INodeId,    pHdr->achInode);
    GET_CPIO_NUMERIC_FIELD_RET(    uMajor,                           pHdr->achDevMajor);
    GET_CPIO_NUMERIC_FIELD_RET(    uMinor,                           pHdr->achDevMinor);
    GET_CPIO_NUMERIC_FIELD_RET(    pObjInfo->cbObject,               pHdr->achFileSize);
    pObjInfo->cbAllocated = RT_ALIGN_64(pObjInfo->cbObject, 4);
    GET_CPIO_NUMERIC_FIELD_RET(    c64SecModTime,                    pHdr->achMTime);
    RTTimeSpecSetSeconds(&pObjInfo->ChangeTime,                  c64SecModTime);
    RTTimeSpecSetSeconds(&pObjInfo->ModificationTime,            c64SecModTime);
    RTTimeSpecSetSeconds(&pObjInfo->AccessTime,                  c64SecModTime);
    RTTimeSpecSetSeconds(&pObjInfo->BirthTime,                   c64SecModTime);
    if (c64SecModTime != RTTimeSpecGetSeconds(&pObjInfo->ModificationTime))
        return VERR_TAR_NUM_VALUE_TOO_LARGE;
    pObjInfo->Attr.u.Unix.Device = RTDEV_MAKE(uMajor, uMinor);
    if (   uMajor != RTDEV_MAJOR(pObjInfo->Attr.u.Unix.Device)
        || uMinor != RTDEV_MINOR(pObjInfo->Attr.u.Unix.Device))
        return VERR_TAR_DEV_VALUE_TOO_LARGE;

    GET_CPIO_NUMERIC_FIELD_RET(*pcbFilePath, pHdr->achNameSize);

    /* Header and file path are padded with 0 bytes to a 4 byte boundary. */
    uint32_t cbComp = *pcbFilePath + sizeof(*pHdr);
    *pcbPad = RT_ALIGN_32(cbComp, 4) - cbComp;

#undef GET_CPIO_NUMERIC_FIELD_RET
#undef GET_CPIO_NUMERIC_FIELD_RET_U64

    return rc;
}


/**
 * Parses and validates a CPIO header.
 *
 * @returns IPRT status code.
 * @param   pThis               The CPIO reader state.
 * @param   enmType             The CPIO header type.
 * @param   pHdr                The CPIO header that has been read.
 * @param   pcbFilePath         Where to store the size of the file path on success.
 * @param   pcbPad              Where to store the number of bytes padded after the header and file path
 *                              before the content begins.
 */
static int rtZipCpioReaderParseHeader(PRTZIPCPIOREADER pThis, RTZIPCPIOTYPE enmType, PCCPIOHDR pHdr,
                                      uint32_t *pcbFilePath, uint32_t *pcbPad)
{
    int rc;

    switch (enmType)
    {
        case RTZIPCPIOTYPE_ANCIENT_BIN:
            rc = rtZipCpioReaderParseHeaderAncientBin(pThis, &pHdr->AncientBin,
                                                      pcbFilePath, pcbPad);
            break;
        case RTZIPCPIOTYPE_ASCII_SUSV2:
            rc = rtZipCpioReaderParseHeaderAsciiSusV2(pThis, &pHdr->AsciiSuSv2,
                                                      pcbFilePath, pcbPad);
            break;
        case RTZIPCPIOTYPE_ASCII_NEW:
            rc = rtZipCpioReaderParseHeaderAsciiNew(pThis, &pHdr->AsciiNew, false /*fWithChksum*/,
                                                    pcbFilePath, pcbPad);
            break;
        case RTZIPCPIOTYPE_ASCII_NEW_CHKSUM:
            rc = rtZipCpioReaderParseHeaderAsciiNew(pThis, &pHdr->AsciiNew, true /*fWithChksum*/,
                                                    pcbFilePath, pcbPad);
            break;
        default:
            AssertMsgFailedBreakStmt(("Invalid CPIO type %d\n", enmType), rc = VERR_INTERNAL_ERROR);
    }

    return rc;
}


/**
 * Reads the file path from the CPIO archive stream.
 *
 * @returns IPRT status code.
 * @param   hVfsIos             The I/O stream to read from.
 * @param   pThis               The CPIO reader state.
 * @param   cbFilePath          Size of the file path in bytes.
 */
static int rtZipCpioReaderReadPath(RTVFSIOSTREAM hVfsIos, PRTZIPCPIOREADER pThis, size_t cbFilePath)
{
    if (cbFilePath >= sizeof(pThis->szName))
        return VERR_TAR_NAME_TOO_LONG;

    size_t cbRead;
    int rc = RTVfsIoStrmRead(hVfsIos, &pThis->szName[0], cbFilePath, true /*fBlocking*/, &cbRead);
    if (RT_FAILURE(rc))
        return rc;
    if (cbRead != cbFilePath)
        return VERR_TAR_UNEXPECTED_EOS;

    /* The read file name should be zero terminated at the end. */
    if (pThis->szName[cbFilePath - 1] != '\0')
        return VERR_TAR_MALFORMED_GNU_LONGXXXX;

    return VINF_SUCCESS;
}


/*
 *
 * T h e   V F S   F i l e s y s t e m   S t r e a m   B i t s.
 * T h e   V F S   F i l e s y s t e m   S t r e a m   B i t s.
 * T h e   V F S   F i l e s y s t e m   S t r e a m   B i t s.
 *
 */

/**
 * @interface_method_impl{RTVFSOBJOPS,pfnClose}
 */
static DECLCALLBACK(int) rtZipCpioFssBaseObj_Close(void *pvThis)
{
    PRTZIPCPIOBASEOBJ pThis = (PRTZIPCPIOBASEOBJ)pvThis;

    /* Currently there is nothing we really have to do here. */
    pThis->offHdr = -1;

    return VINF_SUCCESS;
}


/**
 * @interface_method_impl{RTVFSOBJOPS,pfnQueryInfo}
 */
static DECLCALLBACK(int) rtZipCpioFssBaseObj_QueryInfo(void *pvThis, PRTFSOBJINFO pObjInfo, RTFSOBJATTRADD enmAddAttr)
{
    PRTZIPCPIOBASEOBJ pThis = (PRTZIPCPIOBASEOBJ)pvThis;

    /*
     * Copy the desired data.
     */
    switch (enmAddAttr)
    {
        case RTFSOBJATTRADD_NOTHING:
        case RTFSOBJATTRADD_UNIX:
            *pObjInfo = pThis->ObjInfo;
            break;

        case RTFSOBJATTRADD_UNIX_OWNER:
            *pObjInfo = pThis->ObjInfo;
            pObjInfo->Attr.enmAdditional         = RTFSOBJATTRADD_UNIX_OWNER;
            pObjInfo->Attr.u.UnixOwner.uid       = pThis->ObjInfo.Attr.u.Unix.uid;
            pObjInfo->Attr.u.UnixOwner.szName[0] = '\0';
            break;

        case RTFSOBJATTRADD_UNIX_GROUP:
            *pObjInfo = pThis->ObjInfo;
            pObjInfo->Attr.enmAdditional         = RTFSOBJATTRADD_UNIX_GROUP;
            pObjInfo->Attr.u.UnixGroup.gid       = pThis->ObjInfo.Attr.u.Unix.gid;
            pObjInfo->Attr.u.UnixGroup.szName[0] = '\0';
            break;

        case RTFSOBJATTRADD_EASIZE:
            *pObjInfo = pThis->ObjInfo;
            pObjInfo->Attr.enmAdditional = RTFSOBJATTRADD_EASIZE;
            RT_ZERO(pObjInfo->Attr.u);
            break;

        default:
            return VERR_NOT_SUPPORTED;
    }

    return VINF_SUCCESS;
}


/**
 * Tar filesystem base object operations.
 */
static const RTVFSOBJOPS g_rtZipCpioFssBaseObjOps =
{
    RTVFSOBJOPS_VERSION,
    RTVFSOBJTYPE_BASE,
    "CpioFsStream::Obj",
    rtZipCpioFssBaseObj_Close,
    rtZipCpioFssBaseObj_QueryInfo,
    NULL,
    RTVFSOBJOPS_VERSION
};


/**
 * @interface_method_impl{RTVFSOBJOPS,pfnClose}
 */
static DECLCALLBACK(int) rtZipCpioFssIos_Close(void *pvThis)
{
    PRTZIPCPIOIOSTREAM pThis = (PRTZIPCPIOIOSTREAM)pvThis;

    RTVfsIoStrmRelease(pThis->hVfsIos);
    pThis->hVfsIos = NIL_RTVFSIOSTREAM;

    return rtZipCpioFssBaseObj_Close(&pThis->BaseObj);
}


/**
 * @interface_method_impl{RTVFSOBJOPS,pfnQueryInfo}
 */
static DECLCALLBACK(int) rtZipCpioFssIos_QueryInfo(void *pvThis, PRTFSOBJINFO pObjInfo, RTFSOBJATTRADD enmAddAttr)
{
    PRTZIPCPIOIOSTREAM pThis = (PRTZIPCPIOIOSTREAM)pvThis;
    return rtZipCpioFssBaseObj_QueryInfo(&pThis->BaseObj, pObjInfo, enmAddAttr);
}


/**
 * @interface_method_impl{RTVFSIOSTREAMOPS,pfnRead}
 */
static DECLCALLBACK(int) rtZipCpioFssIos_Read(void *pvThis, RTFOFF off, PCRTSGBUF pSgBuf, bool fBlocking, size_t *pcbRead)
{
    PRTZIPCPIOIOSTREAM pThis = (PRTZIPCPIOIOSTREAM)pvThis;
    Assert(pSgBuf->cSegs == 1);

    /*
     * Make offset into a real offset so it's possible to do random access
     * on CPIO files that are seekable.  Fend of reads beyond the end of the
     * stream.
     */
    if (off < 0)
        off = pThis->offFile;
    if (off >= pThis->cbFile)
        return pcbRead ? VINF_EOF : VERR_EOF;


    Assert(pThis->cbFile >= pThis->offFile);
    uint64_t cbLeft   = (uint64_t)(pThis->cbFile - off);
    size_t   cbToRead = pSgBuf->paSegs[0].cbSeg;
    if (cbToRead > cbLeft)
    {
        if (!pcbRead)
            return VERR_EOF;
        cbToRead = (size_t)cbLeft;
    }

    /*
     * Do the reading.
     */
    size_t cbReadStack = 0;
    if (!pcbRead)
        pcbRead = &cbReadStack;
    int rc = RTVfsIoStrmReadAt(pThis->hVfsIos, pThis->offStart + off, pSgBuf->paSegs[0].pvSeg, cbToRead, fBlocking, pcbRead);
    pThis->offFile = off + *pcbRead;
    if (pThis->offFile >= pThis->cbFile)
    {
        Assert(pThis->offFile == pThis->cbFile);
        pThis->fEndOfStream = true;
        RTVfsIoStrmSkip(pThis->hVfsIos, pThis->cbPadding);
    }

    return rc;
}


/**
 * @interface_method_impl{RTVFSIOSTREAMOPS,pfnWrite}
 */
static DECLCALLBACK(int) rtZipCpioFssIos_Write(void *pvThis, RTFOFF off, PCRTSGBUF pSgBuf, bool fBlocking, size_t *pcbWritten)
{
    /* Cannot write to a read-only I/O stream. */
    NOREF(pvThis); NOREF(off); NOREF(pSgBuf); NOREF(fBlocking); NOREF(pcbWritten);
    return VERR_ACCESS_DENIED;
}


/**
 * @interface_method_impl{RTVFSIOSTREAMOPS,pfnFlush}
 */
static DECLCALLBACK(int) rtZipCpioFssIos_Flush(void *pvThis)
{
    /* It's a read only stream, nothing dirty to flush. */
    NOREF(pvThis);
    return VINF_SUCCESS;
}


/**
 * @interface_method_impl{RTVFSIOSTREAMOPS,pfnPollOne}
 */
static DECLCALLBACK(int) rtZipCpioFssIos_PollOne(void *pvThis, uint32_t fEvents, RTMSINTERVAL cMillies, bool fIntr,
                                                 uint32_t *pfRetEvents)
{
    PRTZIPCPIOIOSTREAM pThis = (PRTZIPCPIOIOSTREAM)pvThis;

    /* When we've reached the end, read will be set to indicate it. */
    if (   (fEvents & RTPOLL_EVT_READ)
        && pThis->fEndOfStream)
    {
        int rc = RTVfsIoStrmPoll(pThis->hVfsIos, fEvents, 0, fIntr, pfRetEvents);
        if (RT_SUCCESS(rc))
            *pfRetEvents |= RTPOLL_EVT_READ;
        else
            *pfRetEvents = RTPOLL_EVT_READ;
        return VINF_SUCCESS;
    }

    return RTVfsIoStrmPoll(pThis->hVfsIos, fEvents, cMillies, fIntr, pfRetEvents);
}


/**
 * @interface_method_impl{RTVFSIOSTREAMOPS,pfnTell}
 */
static DECLCALLBACK(int) rtZipCpioFssIos_Tell(void *pvThis, PRTFOFF poffActual)
{
    PRTZIPCPIOIOSTREAM pThis = (PRTZIPCPIOIOSTREAM)pvThis;
    *poffActual = pThis->offFile;
    return VINF_SUCCESS;
}


/**
 * Tar I/O stream operations.
 */
static const RTVFSIOSTREAMOPS g_rtZipCpioFssIosOps =
{
    { /* Obj */
        RTVFSOBJOPS_VERSION,
        RTVFSOBJTYPE_IO_STREAM,
        "CpioFsStream::IoStream",
        rtZipCpioFssIos_Close,
        rtZipCpioFssIos_QueryInfo,
        NULL,
        RTVFSOBJOPS_VERSION
    },
    RTVFSIOSTREAMOPS_VERSION,
    RTVFSIOSTREAMOPS_FEAT_NO_SG,
    rtZipCpioFssIos_Read,
    rtZipCpioFssIos_Write,
    rtZipCpioFssIos_Flush,
    rtZipCpioFssIos_PollOne,
    rtZipCpioFssIos_Tell,
    NULL /*Skip*/,
    NULL /*ZeroFill*/,
    RTVFSIOSTREAMOPS_VERSION
};


/**
 * @interface_method_impl{RTVFSOBJOPS,pfnClose}
 */
static DECLCALLBACK(int) rtZipCpioFssSym_Close(void *pvThis)
{
    PRTZIPCPIOBASEOBJ pThis = (PRTZIPCPIOBASEOBJ)pvThis;
    return rtZipCpioFssBaseObj_Close(pThis);
}


/**
 * @interface_method_impl{RTVFSOBJOPS,pfnQueryInfo}
 */
static DECLCALLBACK(int) rtZipCpioFssSym_QueryInfo(void *pvThis, PRTFSOBJINFO pObjInfo, RTFSOBJATTRADD enmAddAttr)
{
    PRTZIPCPIOBASEOBJ pThis = (PRTZIPCPIOBASEOBJ)pvThis;
    return rtZipCpioFssBaseObj_QueryInfo(pThis, pObjInfo, enmAddAttr);
}

/**
 * @interface_method_impl{RTVFSOBJSETOPS,pfnMode}
 */
static DECLCALLBACK(int) rtZipCpioFssSym_SetMode(void *pvThis, RTFMODE fMode, RTFMODE fMask)
{
    NOREF(pvThis); NOREF(fMode); NOREF(fMask);
    return VERR_ACCESS_DENIED;
}


/**
 * @interface_method_impl{RTVFSOBJSETOPS,pfnSetTimes}
 */
static DECLCALLBACK(int) rtZipCpioFssSym_SetTimes(void *pvThis, PCRTTIMESPEC pAccessTime, PCRTTIMESPEC pModificationTime,
                                                  PCRTTIMESPEC pChangeTime, PCRTTIMESPEC pBirthTime)
{
    NOREF(pvThis); NOREF(pAccessTime); NOREF(pModificationTime); NOREF(pChangeTime); NOREF(pBirthTime);
    return VERR_ACCESS_DENIED;
}


/**
 * @interface_method_impl{RTVFSOBJSETOPS,pfnSetOwner}
 */
static DECLCALLBACK(int) rtZipCpioFssSym_SetOwner(void *pvThis, RTUID uid, RTGID gid)
{
    NOREF(pvThis); NOREF(uid); NOREF(gid);
    return VERR_ACCESS_DENIED;
}


/**
 * @interface_method_impl{RTVFSSYMLINKOPS,pfnRead}
 */
static DECLCALLBACK(int) rtZipCpioFssSym_Read(void *pvThis, char *pszTarget, size_t cbTarget)
{
    PRTZIPCPIOBASEOBJ pThis = (PRTZIPCPIOBASEOBJ)pvThis;
    return RTStrCopy(pszTarget, cbTarget, pThis->pCpioReader->szTarget);
}


/**
 * CPIO symbolic (and hardlink) operations.
 */
static const RTVFSSYMLINKOPS g_rtZipCpioFssSymOps =
{
    { /* Obj */
        RTVFSOBJOPS_VERSION,
        RTVFSOBJTYPE_SYMLINK,
        "CpioFsStream::Symlink",
        rtZipCpioFssSym_Close,
        rtZipCpioFssSym_QueryInfo,
        NULL,
        RTVFSOBJOPS_VERSION
    },
    RTVFSSYMLINKOPS_VERSION,
    0,
    { /* ObjSet */
        RTVFSOBJSETOPS_VERSION,
        RT_UOFFSETOF(RTVFSSYMLINKOPS, ObjSet) - RT_UOFFSETOF(RTVFSSYMLINKOPS, Obj),
        rtZipCpioFssSym_SetMode,
        rtZipCpioFssSym_SetTimes,
        rtZipCpioFssSym_SetOwner,
        RTVFSOBJSETOPS_VERSION
    },
    rtZipCpioFssSym_Read,
    RTVFSSYMLINKOPS_VERSION
};


/**
 * @interface_method_impl{RTVFSOBJOPS,pfnClose}
 */
static DECLCALLBACK(int) rtZipCpioFss_Close(void *pvThis)
{
    PRTZIPCPIOFSSTREAM pThis = (PRTZIPCPIOFSSTREAM)pvThis;

    RTVfsObjRelease(pThis->hVfsCurObj);
    pThis->hVfsCurObj  = NIL_RTVFSOBJ;
    pThis->pCurIosData = NULL;

    RTVfsIoStrmRelease(pThis->hVfsIos);
    pThis->hVfsIos = NIL_RTVFSIOSTREAM;

    return VINF_SUCCESS;
}


/**
 * @interface_method_impl{RTVFSOBJOPS,pfnQueryInfo}
 */
static DECLCALLBACK(int) rtZipCpioFss_QueryInfo(void *pvThis, PRTFSOBJINFO pObjInfo, RTFSOBJATTRADD enmAddAttr)
{
    PRTZIPCPIOFSSTREAM pThis = (PRTZIPCPIOFSSTREAM)pvThis;
    /* Take the lazy approach here, with the sideffect of providing some info
       that is actually kind of useful. */
    return RTVfsIoStrmQueryInfo(pThis->hVfsIos, pObjInfo, enmAddAttr);
}


/**
 * @interface_method_impl{RTVFSFSSTREAMOPS,pfnNext}
 */
DECL_HIDDEN_CALLBACK(int) rtZipCpioFss_Next(void *pvThis, char **ppszName, RTVFSOBJTYPE *penmType, PRTVFSOBJ phVfsObj)
{
    PRTZIPCPIOFSSTREAM pThis = (PRTZIPCPIOFSSTREAM)pvThis;

    /*
     * Dispense with the current object.
     */
    if (pThis->hVfsCurObj != NIL_RTVFSOBJ)
    {
        if (pThis->pCurIosData)
        {
            pThis->pCurIosData->fEndOfStream = true;
            pThis->pCurIosData->offFile      = pThis->pCurIosData->cbFile;
            pThis->pCurIosData = NULL;
        }

        RTVfsObjRelease(pThis->hVfsCurObj);
        pThis->hVfsCurObj = NIL_RTVFSOBJ;
    }

    /*
     * Check if we've already reached the end in some way.
     */
    if (pThis->fEndOfStream)
        return VERR_EOF;
    if (pThis->rcFatal != VINF_SUCCESS)
        return pThis->rcFatal;

    /*
     * Make sure the input stream is in the right place.
     */
    RTFOFF offHdr = RTVfsIoStrmTell(pThis->hVfsIos);
    while (   offHdr >= 0
           && offHdr < pThis->offNextHdr)
    {
        int rc = RTVfsIoStrmSkip(pThis->hVfsIos, pThis->offNextHdr - offHdr);
        if (RT_FAILURE(rc))
        {
            /** @todo Ignore if we're at the end of the stream? */
            return pThis->rcFatal = rc;
        }

        offHdr = RTVfsIoStrmTell(pThis->hVfsIos);
    }

    if (offHdr < 0)
        return pThis->rcFatal = (int)offHdr;
    if (offHdr > pThis->offNextHdr)
        return pThis->rcFatal = VERR_INTERNAL_ERROR_3;
    Assert(pThis->offNextHdr == offHdr);
    pThis->offCurHdr = offHdr;

    /*
     * Consume CPIO headers.
     */
    size_t cbHdr = 0;
    /*
     * Read the next header.
     *
     * Read the first 6 bytes to determine the header type and continue reading the
     * rest of the header.
     */
    CPIOHDR Hdr;
    RTZIPCPIOTYPE enmHdrType = RTZIPCPIOTYPE_INVALID;
    size_t cbRead;
    int rc = RTVfsIoStrmRead(pThis->hVfsIos, &Hdr.ab[0], sizeof(Hdr.AsciiNew.achMagic), true /*fBlocking*/, &cbRead);
    if (RT_FAILURE(rc))
        return pThis->rcFatal = rc;
    if (rc == VINF_EOF && cbRead == 0)
    {
        pThis->fEndOfStream = true;
        return VERR_EOF;
    }
    if (cbRead != sizeof(Hdr.AsciiNew.achMagic))
        return pThis->rcFatal = VERR_TAR_UNEXPECTED_EOS;

    if (Hdr.AncientBin.u16Magic == CPIO_HDR_BIN_MAGIC)
    {
        cbHdr = sizeof(Hdr.AncientBin);
        enmHdrType = RTZIPCPIOTYPE_ANCIENT_BIN;
    }
    else if (!strncmp(&Hdr.AsciiSuSv2.achMagic[0], CPIO_HDR_SUSV2_MAGIC, sizeof(Hdr.AsciiSuSv2.achMagic)))
    {
        cbHdr = sizeof(Hdr.AsciiSuSv2);
        enmHdrType = RTZIPCPIOTYPE_ASCII_SUSV2;
    }
    else if (!strncmp(&Hdr.AsciiNew.achMagic[0], CPIO_HDR_NEW_MAGIC, sizeof(Hdr.AsciiNew.achMagic)))
    {
        cbHdr = sizeof(Hdr.AsciiNew);
        enmHdrType = RTZIPCPIOTYPE_ASCII_NEW;
    }
    else if (!strncmp(&Hdr.AsciiNew.achMagic[0], CPIO_HDR_NEW_CHKSUM_MAGIC, sizeof(Hdr.AsciiNew.achMagic)))
    {
        cbHdr = sizeof(Hdr.AsciiNew);
        enmHdrType = RTZIPCPIOTYPE_ASCII_NEW_CHKSUM;
    }
    else
        return pThis->rcFatal = VERR_TAR_UNKNOWN_TYPE_FLAG; /** @todo Dedicated status code. */

    /* Read the rest of the header. */
    size_t cbHdrLeft = cbHdr - sizeof(Hdr.AsciiNew.achMagic);
    rc = RTVfsIoStrmRead(pThis->hVfsIos, &Hdr.ab[sizeof(Hdr.AsciiNew.achMagic)], cbHdr - sizeof(Hdr.AsciiNew.achMagic), true /*fBlocking*/, &cbRead);
    if (RT_FAILURE(rc))
        return pThis->rcFatal = rc;
    if (cbRead != cbHdrLeft)
        return pThis->rcFatal = VERR_TAR_UNEXPECTED_EOS;

    /*
     * Parse it.
     */
    uint32_t cbFilePath = 0;
    uint32_t cbPad = 0;
    rc = rtZipCpioReaderParseHeader(&pThis->CpioReader, enmHdrType, &Hdr, &cbFilePath, &cbPad);
    if (RT_FAILURE(rc))
        return pThis->rcFatal = rc;

    /* Read the file path following the header. */
    rc = rtZipCpioReaderReadPath(pThis->hVfsIos, &pThis->CpioReader, cbFilePath);
    if (RT_FAILURE(rc))
        return pThis->rcFatal = rc;

    if (cbPad)
        RTVfsIoStrmSkip(pThis->hVfsIos, cbPad);
    pThis->offNextHdr = offHdr + cbHdr + cbFilePath + cbPad;

    /*
     * CPIO uses a special trailer file record with a 0 mode and size and using a special
     * marker filename. The filesystem stream is marked EOS When such a record is encountered
     * to not try to read anything which might come behind it, imagine an initramfs image consisting
     * of multiple archives which don't need to be necessarily be all of the CPIO kind (yes, this is
     * a reality with ubuntu for example containing microcode updates as seperate CPIO archives
     * coming before the main LZ4 compressed CPIO archive...).
     */
    PCRTFSOBJINFO pInfo = &pThis->CpioReader.ObjInfo;
    if (RT_UNLIKELY(   !pInfo->Attr.fMode
                    && !pInfo->cbAllocated
                    && !strcmp(&pThis->CpioReader.szName[0], CPIO_EOS_FILE_NAME)))
    {
        pThis->fEndOfStream = true;
        return VERR_EOF;
    }

    /*
     * Create an object of the appropriate type.
     */
    RTVFSOBJTYPE    enmType;
    RTVFSOBJ        hVfsObj;
    RTFMODE         fType = pInfo->Attr.fMode & RTFS_TYPE_MASK;
    switch (fType)
    {
        /*
         * Files are represented by a VFS I/O stream, hardlinks have their content
         * embedded as it is another file.
         */
        case RTFS_TYPE_FILE:
        {
            RTVFSIOSTREAM       hVfsIos;
            PRTZIPCPIOIOSTREAM  pIosData;
            rc = RTVfsNewIoStream(&g_rtZipCpioFssIosOps,
                                  sizeof(*pIosData),
                                  RTFILE_O_READ | RTFILE_O_DENY_NONE | RTFILE_O_OPEN,
                                  NIL_RTVFS,
                                  NIL_RTVFSLOCK,
                                  &hVfsIos,
                                  (void **)&pIosData);
            if (RT_FAILURE(rc))
                return pThis->rcFatal = rc;

            pIosData->BaseObj.offHdr      = offHdr;
            pIosData->BaseObj.offNextHdr  = pThis->offNextHdr;
            pIosData->BaseObj.pCpioReader = &pThis->CpioReader;
            pIosData->BaseObj.ObjInfo     = *pInfo;
            pIosData->cbFile              = pInfo->cbObject;
            pIosData->offFile             = 0;
            pIosData->offStart            = RTVfsIoStrmTell(pThis->hVfsIos);
            pIosData->cbPadding           = (uint32_t)(pInfo->cbAllocated - pInfo->cbObject);
            pIosData->fEndOfStream        = false;
            pIosData->hVfsIos             = pThis->hVfsIos;
            RTVfsIoStrmRetain(pThis->hVfsIos);

            pThis->pCurIosData = pIosData;
            pThis->offNextHdr += pIosData->cbFile + pIosData->cbPadding;

            enmType = RTVFSOBJTYPE_IO_STREAM;
            hVfsObj = RTVfsObjFromIoStream(hVfsIos);
            RTVfsIoStrmRelease(hVfsIos);
            break;
        }

        case RTFS_TYPE_SYMLINK:
        {
            RTVFSSYMLINK        hVfsSym;
            PRTZIPCPIOBASEOBJ   pBaseObjData;
            rc = RTVfsNewSymlink(&g_rtZipCpioFssSymOps,
                                 sizeof(*pBaseObjData),
                                 NIL_RTVFS,
                                 NIL_RTVFSLOCK,
                                 &hVfsSym,
                                 (void **)&pBaseObjData);
            if (RT_FAILURE(rc))
                return pThis->rcFatal = rc;

            pBaseObjData->offHdr      = offHdr;
            pBaseObjData->offNextHdr  = pThis->offNextHdr;
            pBaseObjData->pCpioReader = &pThis->CpioReader;
            pBaseObjData->ObjInfo     = *pInfo;

            /* Read the body of the symlink (as normal file data). */
            if (pInfo->cbObject + 1 > (RTFOFF)sizeof(pThis->CpioReader.szTarget))
                return VERR_TAR_NAME_TOO_LONG;

            cbPad = (uint32_t)(pInfo->cbAllocated - pInfo->cbObject);
            rc = RTVfsIoStrmRead(pThis->hVfsIos, &pThis->CpioReader.szTarget[0], pInfo->cbObject, true /*fBlocking*/, &cbRead);
            if (RT_FAILURE(rc))
                return pThis->rcFatal = rc;
            if (cbRead != (uint32_t)pInfo->cbObject)
                return pThis->rcFatal = VERR_TAR_UNEXPECTED_EOS;

            pThis->CpioReader.szTarget[pInfo->cbObject] = '\0';

            if (cbPad)
               rc = RTVfsIoStrmSkip(pThis->hVfsIos, cbPad);
            if (RT_FAILURE(rc))
                return pThis->rcFatal = rc;

            pThis->offNextHdr += pInfo->cbAllocated;

            enmType = RTVFSOBJTYPE_SYMLINK;
            hVfsObj = RTVfsObjFromSymlink(hVfsSym);
            RTVfsSymlinkRelease(hVfsSym);
            break;
        }

        /*
         * All other objects are repesented using a VFS base object since they
         * carry no data streams.
         */
        case RTFS_TYPE_DEV_BLOCK:
        case RTFS_TYPE_DEV_CHAR:
        case RTFS_TYPE_DIRECTORY:
        case RTFS_TYPE_FIFO:
        {
            PRTZIPCPIOBASEOBJ pBaseObjData;
            rc = RTVfsNewBaseObj(&g_rtZipCpioFssBaseObjOps,
                                 sizeof(*pBaseObjData),
                                 NIL_RTVFS,
                                 NIL_RTVFSLOCK,
                                 &hVfsObj,
                                 (void **)&pBaseObjData);
            if (RT_FAILURE(rc))
                return pThis->rcFatal = rc;

            pBaseObjData->offHdr      = offHdr;
            pBaseObjData->offNextHdr  = pThis->offNextHdr;
            pBaseObjData->pCpioReader = &pThis->CpioReader;
            pBaseObjData->ObjInfo     = *pInfo;

            enmType = RTVFSOBJTYPE_BASE;
            break;
        }

        default:
            AssertFailed();
            return pThis->rcFatal = VERR_INTERNAL_ERROR_5;
    }
    pThis->hVfsCurObj = hVfsObj;

    /*
     * Set the return data and we're done.
     */
    if (ppszName)
    {
        rc = RTStrDupEx(ppszName, pThis->CpioReader.szName);
        if (RT_FAILURE(rc))
            return rc;
    }

    if (phVfsObj)
    {
        RTVfsObjRetain(hVfsObj);
        *phVfsObj = hVfsObj;
    }

    if (penmType)
        *penmType = enmType;

    return VINF_SUCCESS;
}



/**
 * CPIO filesystem stream operations.
 */
static const RTVFSFSSTREAMOPS rtZipCpioFssOps =
{
    { /* Obj */
        RTVFSOBJOPS_VERSION,
        RTVFSOBJTYPE_FS_STREAM,
        "CpioFsStream",
        rtZipCpioFss_Close,
        rtZipCpioFss_QueryInfo,
        NULL,
        RTVFSOBJOPS_VERSION
    },
    RTVFSFSSTREAMOPS_VERSION,
    0,
    rtZipCpioFss_Next,
    NULL,
    NULL,
    NULL,
    RTVFSFSSTREAMOPS_VERSION
};


/**
 * Internal function use both by RTZipCpioFsStreamFromIoStream() and by
 * RTZipCpioFsStreamForFile() in updating mode.
 */
DECLHIDDEN(void) rtZipCpioReaderInit(PRTZIPCPIOFSSTREAM pThis, RTVFSIOSTREAM hVfsIos, uint64_t offStart)
{
    pThis->hVfsIos              = hVfsIos;
    pThis->hVfsCurObj           = NIL_RTVFSOBJ;
    pThis->pCurIosData          = NULL;
    pThis->offStart             = offStart;
    pThis->offNextHdr           = offStart;
    pThis->fEndOfStream         = false;
    pThis->rcFatal              = VINF_SUCCESS;

    /* Don't check if it's a CPIO stream here, do that in the
       rtZipCpioFss_Next. */
}


RTDECL(int) RTZipCpioFsStreamFromIoStream(RTVFSIOSTREAM hVfsIosIn, uint32_t fFlags, PRTVFSFSSTREAM phVfsFss)
{
    /*
     * Input validation.
     */
    AssertPtrReturn(phVfsFss, VERR_INVALID_HANDLE);
    *phVfsFss = NIL_RTVFSFSSTREAM;
    AssertPtrReturn(hVfsIosIn, VERR_INVALID_HANDLE);
    AssertReturn(!fFlags, VERR_INVALID_PARAMETER);

    RTFOFF const offStart = RTVfsIoStrmTell(hVfsIosIn);
    AssertReturn(offStart >= 0, (int)offStart);

    uint32_t cRefs = RTVfsIoStrmRetain(hVfsIosIn);
    AssertReturn(cRefs != UINT32_MAX, VERR_INVALID_HANDLE);

    /*
     * Retain the input stream and create a new filesystem stream handle.
     */
    PRTZIPCPIOFSSTREAM pThis;
    RTVFSFSSTREAM      hVfsFss;
    int rc = RTVfsNewFsStream(&rtZipCpioFssOps, sizeof(*pThis), NIL_RTVFS, NIL_RTVFSLOCK, RTFILE_O_READ,
                              &hVfsFss, (void **)&pThis);
    if (RT_SUCCESS(rc))
    {
        rtZipCpioReaderInit(pThis, hVfsIosIn, fFlags);
        *phVfsFss = hVfsFss;
        return VINF_SUCCESS;
    }

    RTVfsIoStrmRelease(hVfsIosIn);
    return rc;
}


/**
 * Used by RTZipCpioFsStreamTruncate to resolve @a hVfsObj.
 */
DECLHIDDEN(PRTZIPCPIOBASEOBJ) rtZipCpioFsStreamBaseObjToPrivate(PRTZIPCPIOFSSTREAM pThis, RTVFSOBJ hVfsObj)
{
    PRTZIPCPIOBASEOBJ pThisObj;
    RTVFSOBJTYPE enmType = RTVfsObjGetType(hVfsObj);
    switch (enmType)
    {
        case RTVFSOBJTYPE_IO_STREAM:
        {
            RTVFSIOSTREAM hVfsIos = RTVfsObjToIoStream(hVfsObj);
            AssertReturn(hVfsIos != NIL_RTVFSIOSTREAM, NULL);
            PRTZIPCPIOIOSTREAM pThisStrm = (PRTZIPCPIOIOSTREAM)RTVfsIoStreamToPrivate(hVfsIos, &g_rtZipCpioFssIosOps);
            RTVfsIoStrmRelease(hVfsIos);
            pThisObj = &pThisStrm->BaseObj;
            break;
        }

        case RTVFSOBJTYPE_SYMLINK:
        {
            RTVFSSYMLINK hVfsSymlink = RTVfsObjToSymlink(hVfsObj);
            AssertReturn(hVfsSymlink != NIL_RTVFSSYMLINK, NULL);
            pThisObj = (PRTZIPCPIOBASEOBJ)RTVfsSymlinkToPrivate(hVfsSymlink, &g_rtZipCpioFssSymOps);
            RTVfsSymlinkRelease(hVfsSymlink);
            break;
        }

        case RTVFSOBJTYPE_BASE:
            pThisObj = (PRTZIPCPIOBASEOBJ)RTVfsObjToPrivate(hVfsObj, &g_rtZipCpioFssBaseObjOps);
            break;

        default:
            /** @todo implement.   */
            AssertFailedReturn(NULL);
    }

    AssertReturn(pThisObj->pCpioReader == &pThis->CpioReader, NULL);
    return pThisObj;
}