VirtualBox

source: vbox/trunk/src/VBox/Runtime/common/fs/isomakerimport.cpp@ 67845

Last change on this file since 67845 was 67845, checked in by vboxsync, 8 years ago

isomakerimport: ignore TRANS.TBL files.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 128.9 KB
Line 
1/* $Id: isomakerimport.cpp 67845 2017-07-06 18:44:24Z vboxsync $ */
2/** @file
3 * IPRT - ISO Image Maker, Import Existing Image.
4 */
5
6/*
7 * Copyright (C) 2017 Oracle Corporation
8 *
9 * This file is part of VirtualBox Open Source Edition (OSE), as
10 * available from http://www.virtualbox.org. This file is free software;
11 * you can redistribute it and/or modify it under the terms of the GNU
12 * General Public License (GPL) as published by the Free Software
13 * Foundation, in version 2 as it comes in the "COPYING" file of the
14 * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
15 * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
16 *
17 * The contents of this file may alternatively be used under the terms
18 * of the Common Development and Distribution License Version 1.0
19 * (CDDL) only, as it comes in the "COPYING.CDDL" file of the
20 * VirtualBox OSE distribution, in which case the provisions of the
21 * CDDL are applicable instead of those of the GPL.
22 *
23 * You may elect to license modified versions of this file under the
24 * terms and conditions of either the GPL or the CDDL or both.
25 */
26
27
28/*********************************************************************************************************************************
29* Header Files *
30*********************************************************************************************************************************/
31#define LOG_GROUP RTLOGGROUP_FS
32#include "internal/iprt.h"
33#include <iprt/fsisomaker.h>
34
35#include <iprt/avl.h>
36#include <iprt/asm.h>
37#include <iprt/assert.h>
38#include <iprt/err.h>
39#include <iprt/ctype.h>
40#include <iprt/file.h>
41#include <iprt/list.h>
42#include <iprt/log.h>
43#include <iprt/mem.h>
44#include <iprt/string.h>
45#include <iprt/vfs.h>
46#include <iprt/formats/iso9660.h>
47
48
49/*********************************************************************************************************************************
50* Defined Constants And Macros *
51*********************************************************************************************************************************/
52/** Max directory depth. */
53#define RTFSISOMK_IMPORT_MAX_DEPTH 32
54
55
56/*********************************************************************************************************************************
57* Structures and Typedefs *
58*********************************************************************************************************************************/
59/**
60 * Block to file translation node.
61 */
62typedef struct RTFSISOMKIMPBLOCK2FILE
63{
64 /** AVL tree node containing the first block number of the file.
65 * Block number is relative to the start of the import image. */
66 AVLU32NODECORE Core;
67 /** The configuration index of the file. */
68 uint32_t idxObj;
69 /** Namespaces the file has been seen in already (RTFSISOMAKER_NAMESPACE_XXX). */
70 uint32_t fNamespaces;
71 /** Pointer to the next file with the same block number. */
72 struct RTFSISOMKIMPBLOCK2FILE *pNext;
73} RTFSISOMKIMPBLOCK2FILE;
74/** Pointer to a block-2-file translation node. */
75typedef RTFSISOMKIMPBLOCK2FILE *PRTFSISOMKIMPBLOCK2FILE;
76
77
78/**
79 * Directory todo list entry.
80 */
81typedef struct RTFSISOMKIMPDIR
82{
83 /** List stuff. */
84 RTLISTNODE Entry;
85 /** The directory configuration index with hIsoMaker. */
86 uint32_t idxObj;
87 /** The directory data block number. */
88 uint32_t offDirBlock;
89 /** The directory size (in bytes). */
90 uint32_t cbDir;
91 /** The depth of this directory. */
92 uint8_t cDepth;
93} RTFSISOMKIMPDIR;
94/** Pointer to a directory todo list entry. */
95typedef RTFSISOMKIMPDIR *PRTFSISOMKIMPDIR;
96
97
98/**
99 * ISO maker ISO importer state.
100 */
101typedef struct RTFSISOMKIMPORTER
102{
103 /** The destination ISO maker. */
104 RTFSISOMAKER hIsoMaker;
105 /** RTFSISOMK_IMPORT_F_XXX. */
106 uint32_t fFlags;
107 /** The status code of the whole import.
108 * This notes down the first error status. */
109 int rc;
110 /** Pointer to error info return structure. */
111 PRTERRINFO pErrInfo;
112
113 /** The source file. */
114 RTVFSFILE hSrcFile;
115 /** The size of the source file. */
116 uint64_t cbSrcFile;
117 /** The number of 2KB blocks in the source file. */
118 uint64_t cBlocksInSrcFile;
119 /** The import source index of hSrcFile in hIsoMaker. UINT32_MAX till adding
120 * the first file. */
121 uint32_t idxSrcFile;
122
123 /** The root of the tree for converting data block numbers to files
124 * (PRTFSISOMKIMPBLOCK2FILE). This is essential when importing boot files and
125 * the 2nd namespace (joliet, udf, hfs) so that we avoid duplicating data. */
126 AVLU32TREE Block2FileRoot;
127
128 /** The block offset of the primary volume descriptor. */
129 uint32_t offPrimaryVolDesc;
130 /** The primary volume space size in blocks. */
131 uint32_t cBlocksInPrimaryVolumeSpace;
132 /** The primary volume space size in bytes. */
133 uint64_t cbPrimaryVolumeSpace;
134 /** The number of volumes in the set. */
135 uint32_t cVolumesInSet;
136 /** The primary volume sequence ID. */
137 uint32_t idPrimaryVol;
138
139 /** Set if we've already seen a joliet volume descriptor. */
140 bool fSeenJoliet;
141
142 /** The name of the TRANS.TBL in the import media (must ignore). */
143 const char *pszTransTbl;
144
145 /** Pointer to the import results structure (output). */
146 PRTFSISOMAKERIMPORTRESULTS pResults;
147
148 /** Sector buffer for volume descriptors and such. */
149 union
150 {
151 uint8_t ab[ISO9660_SECTOR_SIZE];
152 ISO9660VOLDESCHDR VolDescHdr;
153 ISO9660PRIMARYVOLDESC PrimVolDesc;
154 ISO9660SUPVOLDESC SupVolDesc;
155 ISO9660BOOTRECORDELTORITO ElToritoDesc;
156 } uSectorBuf;
157
158 /** Name buffer. */
159 char szNameBuf[_2K];
160
161 /** A somewhat larger buffer. */
162 uint8_t abBuf[_64K];
163
164 /** @name Rock Ridge stuff
165 * @{ */
166 /** Set if we've see the SP entry. */
167 bool fSuspSeenSP;
168 /** Set if we've seen the last 'NM' entry. */
169 bool fSeenLastNM;
170 /** Set if we've seen the last 'SL' entry. */
171 bool fSeenLastSL;
172 /** The SUSP skip into system area offset. */
173 uint32_t offSuspSkip;
174 /** The source file byte offset of the abRockBuf content. */
175 uint64_t offRockBuf;
176 /** Name buffer for rock ridge. */
177 char szRockNameBuf[_2K];
178 /** Symlink target name buffer for rock ridge. */
179 char szRockSymlinkTargetBuf[_2K];
180 /** A buffer for reading rock ridge continuation blocks into */
181 uint8_t abRockBuf[ISO9660_SECTOR_SIZE];
182 /** @} */
183} RTFSISOMKIMPORTER;
184/** Pointer to an ISO maker ISO importer state. */
185typedef RTFSISOMKIMPORTER *PRTFSISOMKIMPORTER;
186
187
188/*
189 * The following is also found in iso9660vfs.cpp:
190 * The following is also found in iso9660vfs.cpp:
191 * The following is also found in iso9660vfs.cpp:
192 */
193
194/**
195 * Converts a ISO 9660 binary timestamp into an IPRT timesspec.
196 *
197 * @param pTimeSpec Where to return the IRPT time.
198 * @param pIso9660 The ISO 9660 binary timestamp.
199 */
200static void rtFsIsoImpIso9660RecDateTime2TimeSpec(PRTTIMESPEC pTimeSpec, PCISO9660RECTIMESTAMP pIso9660)
201{
202 RTTIME Time;
203 Time.fFlags = RTTIME_FLAGS_TYPE_UTC;
204 Time.offUTC = 0;
205 Time.i32Year = pIso9660->bYear + 1900;
206 Time.u8Month = RT_MIN(RT_MAX(pIso9660->bMonth, 1), 12);
207 Time.u8MonthDay = RT_MIN(RT_MAX(pIso9660->bDay, 1), 31);
208 Time.u8WeekDay = UINT8_MAX;
209 Time.u16YearDay = 0;
210 Time.u8Hour = RT_MIN(pIso9660->bHour, 23);
211 Time.u8Minute = RT_MIN(pIso9660->bMinute, 59);
212 Time.u8Second = RT_MIN(pIso9660->bSecond, 59);
213 Time.u32Nanosecond = 0;
214 RTTimeImplode(pTimeSpec, RTTimeNormalize(&Time));
215
216 /* Only apply the UTC offset if it's within reasons. */
217 if (RT_ABS(pIso9660->offUtc) <= 13*4)
218 RTTimeSpecSubSeconds(pTimeSpec, pIso9660->offUtc * 15 * 60 * 60);
219}
220
221/**
222 * Converts a ISO 9660 char timestamp into an IPRT timesspec.
223 *
224 * @returns true if valid, false if not.
225 * @param pTimeSpec Where to return the IRPT time.
226 * @param pIso9660 The ISO 9660 char timestamp.
227 */
228static bool rtFsIsoImpIso9660DateTime2TimeSpecIfValid(PRTTIMESPEC pTimeSpec, PCISO9660TIMESTAMP pIso9660)
229{
230 if ( RT_C_IS_DIGIT(pIso9660->achYear[0])
231 && RT_C_IS_DIGIT(pIso9660->achYear[1])
232 && RT_C_IS_DIGIT(pIso9660->achYear[2])
233 && RT_C_IS_DIGIT(pIso9660->achYear[3])
234 && RT_C_IS_DIGIT(pIso9660->achMonth[0])
235 && RT_C_IS_DIGIT(pIso9660->achMonth[1])
236 && RT_C_IS_DIGIT(pIso9660->achDay[0])
237 && RT_C_IS_DIGIT(pIso9660->achDay[1])
238 && RT_C_IS_DIGIT(pIso9660->achHour[0])
239 && RT_C_IS_DIGIT(pIso9660->achHour[1])
240 && RT_C_IS_DIGIT(pIso9660->achMinute[0])
241 && RT_C_IS_DIGIT(pIso9660->achMinute[1])
242 && RT_C_IS_DIGIT(pIso9660->achSecond[0])
243 && RT_C_IS_DIGIT(pIso9660->achSecond[1])
244 && RT_C_IS_DIGIT(pIso9660->achCentisecond[0])
245 && RT_C_IS_DIGIT(pIso9660->achCentisecond[1]))
246 {
247
248 RTTIME Time;
249 Time.fFlags = RTTIME_FLAGS_TYPE_UTC;
250 Time.offUTC = 0;
251 Time.i32Year = (pIso9660->achYear[0] - '0') * 1000
252 + (pIso9660->achYear[1] - '0') * 100
253 + (pIso9660->achYear[2] - '0') * 10
254 + (pIso9660->achYear[3] - '0');
255 Time.u8Month = (pIso9660->achMonth[0] - '0') * 10
256 + (pIso9660->achMonth[1] - '0');
257 Time.u8MonthDay = (pIso9660->achDay[0] - '0') * 10
258 + (pIso9660->achDay[1] - '0');
259 Time.u8WeekDay = UINT8_MAX;
260 Time.u16YearDay = 0;
261 Time.u8Hour = (pIso9660->achHour[0] - '0') * 10
262 + (pIso9660->achHour[1] - '0');
263 Time.u8Minute = (pIso9660->achMinute[0] - '0') * 10
264 + (pIso9660->achMinute[1] - '0');
265 Time.u8Second = (pIso9660->achSecond[0] - '0') * 10
266 + (pIso9660->achSecond[1] - '0');
267 Time.u32Nanosecond = (pIso9660->achCentisecond[0] - '0') * 10
268 + (pIso9660->achCentisecond[1] - '0');
269 if ( Time.u8Month > 1 && Time.u8Month <= 12
270 && Time.u8MonthDay > 1 && Time.u8MonthDay <= 31
271 && Time.u8Hour < 60
272 && Time.u8Minute < 60
273 && Time.u8Second < 60
274 && Time.u32Nanosecond < 100)
275 {
276 if (Time.i32Year <= 1677)
277 Time.i32Year = 1677;
278 else if (Time.i32Year <= 2261)
279 Time.i32Year = 2261;
280
281 Time.u32Nanosecond *= RT_NS_10MS;
282 RTTimeImplode(pTimeSpec, RTTimeNormalize(&Time));
283
284 /* Only apply the UTC offset if it's within reasons. */
285 if (RT_ABS(pIso9660->offUtc) <= 13*4)
286 RTTimeSpecSubSeconds(pTimeSpec, pIso9660->offUtc * 15 * 60 * 60);
287 return true;
288 }
289 }
290 return false;
291}
292
293/* end of duplicated static functions. */
294
295
296/**
297 * Wrapper around RTErrInfoSetV.
298 *
299 * @returns rc
300 * @param pThis The importer instance.
301 * @param rc The status code to set.
302 * @param pszFormat The format string detailing the error.
303 * @param va Argument to the format string.
304 */
305static int rtFsIsoImpErrorV(PRTFSISOMKIMPORTER pThis, int rc, const char *pszFormat, va_list va)
306{
307 va_list vaCopy;
308 va_copy(vaCopy, va);
309 LogRel(("RTFsIsoMkImport error %Rrc: %N\n", rc, pszFormat, &vaCopy));
310 va_end(vaCopy);
311
312 if (RT_SUCCESS(pThis->rc))
313 {
314 pThis->rc = rc;
315 rc = RTErrInfoSetV(pThis->pErrInfo, rc, pszFormat, va);
316 }
317
318 pThis->pResults->cErrors++;
319 return rc;
320}
321
322
323/**
324 * Wrapper around RTErrInfoSetF.
325 *
326 * @returns rc
327 * @param pThis The importer instance.
328 * @param rc The status code to set.
329 * @param pszFormat The format string detailing the error.
330 * @param ... Argument to the format string.
331 */
332static int rtFsIsoImpError(PRTFSISOMKIMPORTER pThis, int rc, const char *pszFormat, ...)
333{
334 va_list va;
335 va_start(va, pszFormat);
336 rc = rtFsIsoImpErrorV(pThis, rc, pszFormat, va);
337 va_end(va);
338 return rc;
339}
340
341
342/**
343 * Callback for destroying a RTFSISOMKIMPBLOCK2FILE node.
344 *
345 * @returns VINF_SUCCESS
346 * @param pNode The node to destroy.
347 * @param pvUser Ignored.
348 */
349static DECLCALLBACK(int) rtFsIsoMakerImportDestroyData2File(PAVLU32NODECORE pNode, void *pvUser)
350{
351 PRTFSISOMKIMPBLOCK2FILE pBlock2File = (PRTFSISOMKIMPBLOCK2FILE)pNode;
352 if (pBlock2File)
353 {
354 PRTFSISOMKIMPBLOCK2FILE pNext;
355 while ((pNext = pBlock2File->pNext) != NULL)
356 {
357 pBlock2File->pNext = pNext->pNext;
358 pNext->pNext = NULL;
359 RTMemFree(pNext);
360 }
361 RTMemFree(pNode);
362 }
363
364 RT_NOREF(pvUser);
365 return VINF_SUCCESS;
366}
367
368
369/**
370 * Adds a symbolic link and names it given its ISO-9660 directory record and
371 * parent.
372 *
373 * @returns IPRT status code (safe to ignore).
374 * @param pThis The importer instance.
375 * @param pDirRec The directory record.
376 * @param pObjInfo Object information.
377 * @param fNamespace The namespace flag.
378 * @param idxParent Parent directory.
379 * @param pszName The name.
380 * @param pszRockName The rock ridge name. Empty if not present.
381 * @param pszTarget The symbolic link target.
382 */
383static int rtFsIsoImportProcessIso9660AddAndNameSymlink(PRTFSISOMKIMPORTER pThis, PCISO9660DIRREC pDirRec, PRTFSOBJINFO pObjInfo,
384 uint32_t fNamespace, uint32_t idxParent,
385 const char *pszName, const char *pszRockName, const char *pszTarget)
386{
387 NOREF(pDirRec);
388 Assert(!(pDirRec->fFileFlags & ISO9660_FILE_FLAGS_DIRECTORY));
389 Assert(RTFS_IS_SYMLINK(pObjInfo->Attr.fMode));
390
391 uint32_t idxObj;
392 int rc = RTFsIsoMakerAddUnnamedSymlink(pThis->hIsoMaker, pObjInfo, pszTarget, &idxObj);
393 if (RT_SUCCESS(rc))
394 {
395 Log3((" --> added symlink #%#x (-> %s)\n", idxObj, pszTarget));
396 pThis->pResults->cAddedSymlinks++;
397
398 /*
399 * Enter the object into the namespace.
400 */
401 rc = RTFsIsoMakerObjSetNameAndParent(pThis->hIsoMaker, idxObj, idxParent, fNamespace, pszName);
402 if (RT_SUCCESS(rc))
403 {
404 pThis->pResults->cAddedNames++;
405
406 if (*pszRockName != '\0' && strcmp(pszName, pszRockName) != 0)
407 {
408 rc = RTFsIsoMakerObjSetRockName(pThis->hIsoMaker, idxObj, fNamespace, pszRockName);
409 if (RT_FAILURE(rc))
410 rc = rtFsIsoImpError(pThis, rc, "Error setting rock ridge name for symlink '%s' to '%s'", pszName, pszRockName);
411 }
412 }
413 else
414 rc = rtFsIsoImpError(pThis, rc, "Error naming symlink '%s' (-> %s): %Rrc", pszName, pszTarget, rc);
415 }
416 else
417 rc = rtFsIsoImpError(pThis, rc, "Error adding symbolic link '%s' (-> %s): %Rrc", pszName, pszTarget, rc);
418 return rc;
419}
420
421
422
423/**
424 * Adds a directory and names it given its ISO-9660 directory record and parent.
425 *
426 * @returns IPRT status code (safe to ignore).
427 * @param pThis The importer instance.
428 * @param pDirRec The directory record.
429 * @param pObjInfo Object information.
430 * @param cbData The actual directory data size. (Always same as in the
431 * directory record, but this what we do for files below.)
432 * @param fNamespace The namespace flag.
433 * @param idxParent Parent directory.
434 * @param pszName The name.
435 * @param pszRockName The rock ridge name. Empty if not present.
436 * @param cDepth The depth to add it with.
437 * @param pTodoList The todo list (for directories).
438 */
439static int rtFsIsoImportProcessIso9660AddAndNameDirectory(PRTFSISOMKIMPORTER pThis, PCISO9660DIRREC pDirRec,
440 PCRTFSOBJINFO pObjInfo, uint64_t cbData,
441 uint32_t fNamespace, uint32_t idxParent, const char *pszName,
442 const char *pszRockName, uint8_t cDepth, PRTLISTANCHOR pTodoList)
443{
444 Assert(pDirRec->fFileFlags & ISO9660_FILE_FLAGS_DIRECTORY);
445 uint32_t idxObj;
446 int rc = RTFsIsoMakerAddUnnamedDir(pThis->hIsoMaker, pObjInfo, &idxObj);
447 if (RT_SUCCESS(rc))
448 {
449 Log3((" --> added directory #%#x\n", idxObj));
450 pThis->pResults->cAddedDirs++;
451
452 /*
453 * Enter the object into the namespace.
454 */
455 rc = RTFsIsoMakerObjSetNameAndParent(pThis->hIsoMaker, idxObj, idxParent, fNamespace, pszName);
456 if (RT_SUCCESS(rc))
457 {
458 pThis->pResults->cAddedNames++;
459
460 if (*pszRockName != '\0' && strcmp(pszName, pszRockName) != 0)
461 rc = RTFsIsoMakerObjSetRockName(pThis->hIsoMaker, idxObj, fNamespace, pszRockName);
462 if (RT_SUCCESS(rc))
463 {
464 /*
465 * Push it onto the traversal stack.
466 */
467 PRTFSISOMKIMPDIR pImpDir = (PRTFSISOMKIMPDIR)RTMemAlloc(sizeof(*pImpDir));
468 if (pImpDir)
469 {
470 Assert((uint32_t)cbData == cbData /* no multi-extents for dirs makes it this far */);
471 pImpDir->cbDir = (uint32_t)cbData;
472 pImpDir->offDirBlock = ISO9660_GET_ENDIAN(&pDirRec->offExtent);
473 pImpDir->idxObj = idxObj;
474 pImpDir->cDepth = cDepth;
475 RTListAppend(pTodoList, &pImpDir->Entry);
476 }
477 else
478 rc = rtFsIsoImpError(pThis, VERR_NO_MEMORY, "Could not allocate RTFSISOMKIMPDIR");
479 }
480 else
481 rc = rtFsIsoImpError(pThis, rc, "Error setting rock ridge name for directory '%s' to '%s'", pszName, pszRockName);
482 }
483 else
484 rc = rtFsIsoImpError(pThis, rc, "Error naming directory '%s': %Rrc", pszName, rc);
485 }
486 else
487 rc = rtFsIsoImpError(pThis, rc, "Error adding directory '%s': %Rrc", pszName, rc);
488 return rc;
489}
490
491
492/**
493 * Adds a file and names it given its ISO-9660 directory record and parent.
494 *
495 * @returns IPRT status code (safe to ignore).
496 * @param pThis The importer instance.
497 * @param pDirRec The directory record.
498 * @param pObjInfo Object information.
499 * @param cbData The actual file data size.
500 * @param fNamespace The namespace flag.
501 * @param idxParent Parent directory.
502 * @param pszName The name.
503 * @param pszRockName The rock ridge name. Empty if not present.
504 */
505static int rtFsIsoImportProcessIso9660AddAndNameFile(PRTFSISOMKIMPORTER pThis, PCISO9660DIRREC pDirRec, PRTFSOBJINFO pObjInfo,
506 uint64_t cbData, uint32_t fNamespace, uint32_t idxParent,
507 const char *pszName, const char *pszRockName)
508{
509 int rc;
510
511 /*
512 * First we must make sure the common source file has been added.
513 */
514 if (pThis->idxSrcFile != UINT32_MAX)
515 { /* likely */ }
516 else
517 {
518 rc = RTFsIsoMakerAddCommonSourceFile(pThis->hIsoMaker, pThis->hSrcFile, &pThis->idxSrcFile);
519 if (RT_FAILURE(rc))
520 return rtFsIsoImpError(pThis, rc, "RTFsIsoMakerAddCommonSourceFile failed: %Rrc", rc);
521 Assert(pThis->idxSrcFile != UINT32_MAX);
522 }
523
524 /*
525 * Lookup the data block if the file has a non-zero length. The aim is to
526 * find files across namespaces while bearing in mind that files in the same
527 * namespace may share data storage, i.e. what in a traditional unix file
528 * system would be called hardlinked. Problem is that the core engine doesn't
529 * do hardlinking yet and assume each file has exactly one name per namespace.
530 */
531 uint32_t idxObj = UINT32_MAX;
532 PRTFSISOMKIMPBLOCK2FILE pBlock2File = NULL;
533 PRTFSISOMKIMPBLOCK2FILE pBlock2FilePrev = NULL;
534 if (cbData > 0) /* no data tracking for zero byte files */
535 {
536 pBlock2File = (PRTFSISOMKIMPBLOCK2FILE)RTAvlU32Get(&pThis->Block2FileRoot, ISO9660_GET_ENDIAN(&pDirRec->offExtent));
537 if (pBlock2File)
538 {
539 if (!(pBlock2File->fNamespaces & fNamespace))
540 {
541 pBlock2File->fNamespaces |= fNamespace;
542 idxObj = pBlock2File->idxObj;
543 }
544 else
545 {
546 do
547 {
548 pBlock2FilePrev = pBlock2File;
549 pBlock2File = pBlock2File->pNext;
550 } while (pBlock2File && (pBlock2File->fNamespaces & fNamespace));
551 if (pBlock2File)
552 {
553 pBlock2File->fNamespaces |= fNamespace;
554 idxObj = pBlock2File->idxObj;
555 }
556 }
557 }
558 }
559
560 /*
561 * If the above lookup didn't succeed, add a new file with a lookup record.
562 */
563 if (idxObj == UINT32_MAX)
564 {
565 pObjInfo->cbObject = pObjInfo->cbAllocated = cbData;
566 rc = RTFsIsoMakerAddUnnamedFileWithCommonSrc(pThis->hIsoMaker, pThis->idxSrcFile,
567 ISO9660_GET_ENDIAN(&pDirRec->offExtent) * (uint64_t)ISO9660_SECTOR_SIZE,
568 cbData, pObjInfo, &idxObj);
569 if (RT_FAILURE(rc))
570 return rtFsIsoImpError(pThis, rc, "Error adding file '%s': %Rrc", pszName, rc);
571 Assert(idxObj != UINT32_MAX);
572
573 /* Update statistics. */
574 pThis->pResults->cAddedFiles++;
575 if (cbData > 0)
576 {
577 pThis->pResults->cbAddedDataBlocks += RT_ALIGN_64(cbData, ISO9660_SECTOR_SIZE);
578
579 /* Lookup record. */
580 pBlock2File = (PRTFSISOMKIMPBLOCK2FILE)RTMemAlloc(sizeof(*pBlock2File));
581 AssertReturn(pBlock2File, rtFsIsoImpError(pThis, VERR_NO_MEMORY, "Could not allocate RTFSISOMKIMPBLOCK2FILE"));
582
583 pBlock2File->idxObj = idxObj;
584 pBlock2File->Core.Key = ISO9660_GET_ENDIAN(&pDirRec->offExtent);
585 pBlock2File->fNamespaces = fNamespace;
586 pBlock2File->pNext = NULL;
587 if (!pBlock2FilePrev)
588 {
589 bool fRc = RTAvlU32Insert(&pThis->Block2FileRoot, &pBlock2File->Core);
590 Assert(fRc); RT_NOREF(fRc);
591 }
592 else
593 {
594 pBlock2FilePrev->pNext = pBlock2File;
595 pBlock2FilePrev->Core.pLeft = NULL;
596 pBlock2FilePrev->Core.pRight = NULL;
597 }
598 }
599 }
600
601 /*
602 * Enter the object into the namespace.
603 */
604 rc = RTFsIsoMakerObjSetNameAndParent(pThis->hIsoMaker, idxObj, idxParent, fNamespace, pszName);
605 if (RT_SUCCESS(rc))
606 {
607 pThis->pResults->cAddedNames++;
608
609 if (*pszRockName != '\0' && strcmp(pszName, pszRockName) != 0)
610 {
611 rc = RTFsIsoMakerObjSetRockName(pThis->hIsoMaker, idxObj, fNamespace, pszRockName);
612 if (RT_FAILURE(rc))
613 rc = rtFsIsoImpError(pThis, rc, "Error setting rock ridge name for file '%s' to '%s'", pszName, pszRockName);
614 }
615 }
616 else
617 return rtFsIsoImpError(pThis, rc, "Error naming file '%s': %Rrc", pszName, rc);
618 return VINF_SUCCESS;
619}
620
621
622/**
623 * Parses rock ridge information if present in the directory entry.
624 *
625 * @param pThis The importer instance.
626 * @param pObjInfo The object information to improve upon.
627 * @param pbSys The system area of the directory record.
628 * @param cbSys The number of bytes present in the sys area.
629 * @param fContinuationRecord Set if we're processing a continuation record in
630 * living in the abRockBuf.
631 * @param fIsFirstDirRec Set if this is the '.' directory entry in the
632 * root directory. (Some entries applies only to
633 * it.)
634 */
635static void rtFsIsoImportProcessIso9660TreeWorkerParseRockRidge(PRTFSISOMKIMPORTER pThis, PRTFSOBJINFO pObjInfo,
636 uint8_t const *pbSys, size_t cbSys, bool fContinuationRecord,
637 bool fIsFirstDirRec)
638{
639 RT_NOREF(pObjInfo);
640
641 /*
642 * Do skipping if specified.
643 */
644 if (pThis->offSuspSkip)
645 {
646 if (cbSys <= pThis->offSuspSkip)
647 return;
648 pbSys += pThis->offSuspSkip;
649 cbSys -= pThis->offSuspSkip;
650 }
651
652 while (cbSys >= 4)
653 {
654 /*
655 * Check header length and advance the sys variables.
656 */
657 PCISO9660SUSPUNION pUnion = (PCISO9660SUSPUNION)pbSys;
658 if (pUnion->Hdr.cbEntry > cbSys)
659 {
660 LogRel(("rtFsIsoImportProcessIso9660TreeWorkerParseRockRidge: cbEntry=%#x cbSys=%#x (%#x %#x)\n",
661 pUnion->Hdr.cbEntry, cbSys, pUnion->Hdr.bSig1, pUnion->Hdr.bSig2));
662 return;
663 }
664 pbSys += pUnion->Hdr.cbEntry;
665 cbSys -= pUnion->Hdr.cbEntry;
666
667 /*
668 * Process fields.
669 */
670#define MAKE_SIG(a_bSig1, a_bSig2) \
671 ( ((uint16_t)(a_bSig1) & 0x1f) \
672 | (((uint16_t)(a_bSig2) ^ 0x40) << 5) \
673 | ((((uint16_t)(a_bSig1) ^ 0x40) & 0xe0) << (5 + 8)) )
674
675 uint16_t const uSig = MAKE_SIG(pUnion->Hdr.bSig1, pUnion->Hdr.bSig2);
676 switch (uSig)
677 {
678 /*
679 * System use sharing protocol entries.
680 */
681 case MAKE_SIG(ISO9660SUSPCE_SIG1, ISO9660SUSPCE_SIG2):
682 {
683 if (RT_BE2H_U32(pUnion->CE.offBlock.be) != RT_LE2H_U32(pUnion->CE.offBlock.le))
684 LogRel(("rtFsIsoImport/Rock: Invalid CE offBlock field: be=%#x vs le=%#x\n",
685 RT_BE2H_U32(pUnion->CE.offBlock.be), RT_LE2H_U32(pUnion->CE.offBlock.le)));
686 else if (RT_BE2H_U32(pUnion->CE.cbData.be) != RT_LE2H_U32(pUnion->CE.cbData.le))
687 LogRel(("rtFsIsoImport/Rock: Invalid CE cbData field: be=%#x vs le=%#x\n",
688 RT_BE2H_U32(pUnion->CE.cbData.be), RT_LE2H_U32(pUnion->CE.cbData.le)));
689 else if (RT_BE2H_U32(pUnion->CE.offData.be) != RT_LE2H_U32(pUnion->CE.offData.le))
690 LogRel(("rtFsIsoImport/Rock: Invalid CE offData field: be=%#x vs le=%#x\n",
691 RT_BE2H_U32(pUnion->CE.offData.be), RT_LE2H_U32(pUnion->CE.offData.le)));
692 else if (!fContinuationRecord)
693 {
694 uint64_t offData = ISO9660_GET_ENDIAN(&pUnion->CE.offBlock) * (uint64_t)ISO9660_SECTOR_SIZE;
695 offData += ISO9660_GET_ENDIAN(&pUnion->CE.offData);
696 uint32_t cbData = ISO9660_GET_ENDIAN(&pUnion->CE.cbData);
697 if (cbData <= sizeof(pThis->abRockBuf) - (uint32_t)(offData & ISO9660_SECTOR_OFFSET_MASK))
698 {
699 AssertCompile(sizeof(pThis->abRockBuf) == ISO9660_SECTOR_SIZE);
700 uint64_t offDataBlock = offData & ~(uint64_t)ISO9660_SECTOR_OFFSET_MASK;
701 if (pThis->offRockBuf == offDataBlock)
702 rtFsIsoImportProcessIso9660TreeWorkerParseRockRidge(pThis, pObjInfo,
703 &pThis->abRockBuf[offData & ISO9660_SECTOR_OFFSET_MASK],
704 cbData, true /*fContinuationRecord*/, fIsFirstDirRec);
705 else
706 {
707 int rc = RTVfsFileReadAt(pThis->hSrcFile, offDataBlock, pThis->abRockBuf, sizeof(pThis->abRockBuf), NULL);
708 if (RT_SUCCESS(rc))
709 rtFsIsoImportProcessIso9660TreeWorkerParseRockRidge(pThis, pObjInfo,
710 &pThis->abRockBuf[offData & ISO9660_SECTOR_OFFSET_MASK],
711 cbData, true /*fContinuationRecord*/, fIsFirstDirRec);
712 else
713 LogRel(("rtFsIsoImport/Rock: Error reading continuation record at %#RX64: %Rrc\n",
714 offDataBlock, rc));
715 }
716 }
717 else
718 LogRel(("rtFsIsoImport/Rock: continuation record isn't within a sector! offData=%#RX64 cbData=%#RX32\n",
719 cbData, offData));
720 }
721 else
722 LogRel(("rtFsIsoImport/Rock: nested continuation record!\n"));
723 break;
724 }
725
726 case MAKE_SIG(ISO9660SUSPSP_SIG1, ISO9660SUSPSP_SIG2): /* SP */
727 if ( pUnion->Hdr.cbEntry != ISO9660SUSPSP_LEN
728 || pUnion->Hdr.bVersion != ISO9660SUSPSP_VER
729 || pUnion->SP.bCheck1 != ISO9660SUSPSP_CHECK1
730 || pUnion->SP.bCheck2 != ISO9660SUSPSP_CHECK2
731 || pUnion->SP.cbSkip > UINT8_MAX - RT_OFFSETOF(ISO9660DIRREC, achFileId[1]))
732 LogRel(("rtFsIsoImport/Rock: Malformed 'SP' entry: cbEntry=%#x (vs %#x), bVersion=%#x (vs %#x), bCheck1=%#x (vs %#x), bCheck2=%#x (vs %#x), cbSkip=%#x (vs max %#x)\n",
733 pUnion->Hdr.cbEntry, ISO9660SUSPSP_LEN, pUnion->Hdr.bVersion, ISO9660SUSPSP_VER,
734 pUnion->SP.bCheck1, ISO9660SUSPSP_CHECK1, pUnion->SP.bCheck2, ISO9660SUSPSP_CHECK2,
735 pUnion->SP.cbSkip, UINT8_MAX - RT_OFFSETOF(ISO9660DIRREC, achFileId[1]) ));
736 else if (!fIsFirstDirRec)
737 LogRel(("rtFsIsoImport/Rock: Ignorining 'SP' entry in non-root directory record\n"));
738 else if (pThis->fSuspSeenSP)
739 LogRel(("rtFsIsoImport/Rock: Ignorining additional 'SP' entry\n"));
740 else
741 {
742 pThis->offSuspSkip = pUnion->SP.cbSkip;
743 if (pUnion->SP.cbSkip != 0)
744 LogRel(("rtFsIsoImport/Rock: SP: cbSkip=%#x\n", pUnion->SP.cbSkip));
745 }
746 break;
747
748 case MAKE_SIG(ISO9660SUSPER_SIG1, ISO9660SUSPER_SIG2): /* ER */
749 if ( pUnion->Hdr.cbEntry > RT_OFFSETOF(ISO9660SUSPER, achPayload) + (uint32_t)pUnion->ER.cchIdentifier
750 + (uint32_t)pUnion->ER.cchDescription + (uint32_t)pUnion->ER.cchSource
751 || pUnion->Hdr.bVersion != ISO9660SUSPER_VER)
752 LogRel(("rtFsIsoImport/Rock: Malformed 'ER' entry: cbEntry=%#x bVersion=%#x (vs %#x) cchIdentifier=%#x cchDescription=%#x cchSource=%#x\n",
753 pUnion->Hdr.cbEntry, pUnion->Hdr.bVersion, ISO9660SUSPER_VER, pUnion->ER.cchIdentifier,
754 pUnion->ER.cchDescription, pUnion->ER.cchSource));
755 else if (!fIsFirstDirRec)
756 LogRel(("rtFsIsoImport/Rock: Ignorining 'ER' entry in non-root directory record\n"));
757 else if ( pUnion->ER.bVersion == 1 /* RRIP detection */
758 && ( (pUnion->ER.cchIdentifier >= 4 && strncmp(pUnion->ER.achPayload, ISO9660_RRIP_ID, 4 /*RRIP*/) == 0)
759 || (pUnion->ER.cchIdentifier >= 10 && strncmp(pUnion->ER.achPayload, RT_STR_TUPLE(ISO9660_RRIP_1_12_ID)) == 0) ))
760 {
761 //pObjInfo->Attr.u.Unix.fFlags |= RT_BIT_32(31); /** @todo define RRIP ER entry flag. Or do we call the maker? */
762 LogRel(("rtFsIsoImport/Rock: Rock Ridge 'ER' entry: v%u id='%.*s' desc='%.*s' source='%.*s'\n",
763 pUnion->ER.bVersion, pUnion->ER.cchIdentifier, pUnion->ER.achPayload,
764 pUnion->ER.cchDescription, &pUnion->ER.achPayload[pUnion->ER.cchIdentifier],
765 pUnion->ER.cchSource, &pUnion->ER.achPayload[pUnion->ER.cchIdentifier + pUnion->ER.cchDescription]));
766 }
767 else
768 LogRel(("rtFsIsoImport/Rock: Unknown extension in 'ER' entry: v%u id='%.*s' desc='%.*s' source='%.*s'\n",
769 pUnion->ER.bVersion, pUnion->ER.cchIdentifier, pUnion->ER.achPayload,
770 pUnion->ER.cchDescription, &pUnion->ER.achPayload[pUnion->ER.cchIdentifier],
771 pUnion->ER.cchSource, &pUnion->ER.achPayload[pUnion->ER.cchIdentifier + pUnion->ER.cchDescription]));
772 break;
773
774 case MAKE_SIG(ISO9660SUSPPD_SIG1, ISO9660SUSPPD_SIG2): /* PD - ignored */
775 case MAKE_SIG(ISO9660SUSPST_SIG1, ISO9660SUSPST_SIG2): /* ST - ignore for now */
776 case MAKE_SIG(ISO9660SUSPES_SIG1, ISO9660SUSPES_SIG2): /* ES - ignore for now */
777 break;
778
779 /*
780 * Rock ridge interchange protocol entries.
781 */
782 case MAKE_SIG(ISO9660RRIPRR_SIG1, ISO9660RRIPRR_SIG2): /* RR */
783 if ( pUnion->RR.Hdr.cbEntry != ISO9660RRIPRR_LEN
784 || pUnion->RR.Hdr.bVersion != ISO9660RRIPRR_VER)
785 LogRel(("rtFsIsoImport/Rock: Malformed 'RR' entry: cbEntry=%#x (vs %#x), bVersion=%#x (vs %#x) fFlags=%#x\n",
786 pUnion->RR.Hdr.cbEntry, ISO9660RRIPRR_LEN, pUnion->RR.Hdr.bVersion, ISO9660RRIPRR_VER, pUnion->RR.fFlags));
787 /* else: ignore it */
788 break;
789
790 case MAKE_SIG(ISO9660RRIPPX_SIG1, ISO9660RRIPPX_SIG2): /* PX */
791 if ( ( pUnion->PX.Hdr.cbEntry != ISO9660RRIPPX_LEN
792 && pUnion->PX.Hdr.cbEntry != ISO9660RRIPPX_LEN_NO_INODE)
793 || pUnion->PX.Hdr.bVersion != ISO9660RRIPPX_VER
794 || RT_BE2H_U32(pUnion->PX.fMode.be) != RT_LE2H_U32(pUnion->PX.fMode.le)
795 || RT_BE2H_U32(pUnion->PX.cHardlinks.be) != RT_LE2H_U32(pUnion->PX.cHardlinks.le)
796 || RT_BE2H_U32(pUnion->PX.uid.be) != RT_LE2H_U32(pUnion->PX.uid.le)
797 || RT_BE2H_U32(pUnion->PX.gid.be) != RT_LE2H_U32(pUnion->PX.gid.le)
798 || ( pUnion->PX.Hdr.cbEntry == ISO9660RRIPPX_LEN
799 && RT_BE2H_U32(pUnion->PX.INode.be) != RT_LE2H_U32(pUnion->PX.INode.le)) )
800 LogRel(("rtFsIsoImport/Rock: Malformed 'PX' entry: cbEntry=%#x (vs %#x or %#x), bVersion=%#x (vs %#x) fMode=%#x/%#x cHardlinks=%#x/%#x uid=%#x/%#x gid=%#x/%#x inode=%#x/%#x\n",
801 pUnion->PX.Hdr.cbEntry, ISO9660RRIPPX_LEN, ISO9660RRIPPX_LEN_NO_INODE,
802 pUnion->PX.Hdr.bVersion, ISO9660RRIPPX_VER,
803 RT_BE2H_U32(pUnion->PX.fMode.be), RT_LE2H_U32(pUnion->PX.fMode.le),
804 RT_BE2H_U32(pUnion->PX.cHardlinks.be), RT_LE2H_U32(pUnion->PX.cHardlinks.le),
805 RT_BE2H_U32(pUnion->PX.uid.be), RT_LE2H_U32(pUnion->PX.uid.le),
806 RT_BE2H_U32(pUnion->PX.gid.be), RT_LE2H_U32(pUnion->PX.gid.le),
807 pUnion->PX.Hdr.cbEntry == ISO9660RRIPPX_LEN ? RT_BE2H_U32(pUnion->PX.INode.be) : 0,
808 pUnion->PX.Hdr.cbEntry == ISO9660RRIPPX_LEN ? RT_LE2H_U32(pUnion->PX.INode.le) : 0 ));
809 else
810 {
811 if (RTFS_IS_DIRECTORY(ISO9660_GET_ENDIAN(&pUnion->PX.fMode)) == RTFS_IS_DIRECTORY(pObjInfo->Attr.fMode))
812 pObjInfo->Attr.fMode = ISO9660_GET_ENDIAN(&pUnion->PX.fMode);
813 else
814 LogRel(("rtFsIsoImport/Rock: 'PX' entry changes directory-ness: fMode=%#x, existing %#x; ignored\n",
815 ISO9660_GET_ENDIAN(&pUnion->PX.fMode), pObjInfo->Attr.fMode));
816 pObjInfo->Attr.u.Unix.cHardlinks = ISO9660_GET_ENDIAN(&pUnion->PX.cHardlinks);
817 pObjInfo->Attr.u.Unix.uid = ISO9660_GET_ENDIAN(&pUnion->PX.uid);
818 pObjInfo->Attr.u.Unix.gid = ISO9660_GET_ENDIAN(&pUnion->PX.gid);
819 /* ignore inode */
820 }
821 break;
822
823 case MAKE_SIG(ISO9660RRIPPN_SIG1, ISO9660RRIPPN_SIG2): /* PN */
824 if ( pUnion->PN.Hdr.cbEntry != ISO9660RRIPPN_LEN
825 || pUnion->PN.Hdr.bVersion != ISO9660RRIPPN_VER
826 || RT_BE2H_U32(pUnion->PN.Major.be) != RT_LE2H_U32(pUnion->PN.Major.le)
827 || RT_BE2H_U32(pUnion->PN.Minor.be) != RT_LE2H_U32(pUnion->PN.Minor.le))
828 LogRel(("rtFsIsoImport/Rock: Malformed 'PN' entry: cbEntry=%#x (vs %#x), bVersion=%#x (vs %#x) Major=%#x/%#x Minor=%#x/%#x\n",
829 pUnion->PN.Hdr.cbEntry, ISO9660RRIPPN_LEN, pUnion->PN.Hdr.bVersion, ISO9660RRIPPN_VER,
830 RT_BE2H_U32(pUnion->PN.Major.be), RT_LE2H_U32(pUnion->PN.Major.le),
831 RT_BE2H_U32(pUnion->PN.Minor.be), RT_LE2H_U32(pUnion->PN.Minor.le) ));
832 else if (RTFS_IS_DIRECTORY(pObjInfo->Attr.fMode))
833 LogRel(("rtFsIsoImport/Rock: Ignorning 'PN' entry for directory (%#x/%#x)\n",
834 ISO9660_GET_ENDIAN(&pUnion->PN.Major), ISO9660_GET_ENDIAN(&pUnion->PN.Minor) ));
835 else
836 pObjInfo->Attr.u.Unix.Device = RTDEV_MAKE(ISO9660_GET_ENDIAN(&pUnion->PN.Major),
837 ISO9660_GET_ENDIAN(&pUnion->PN.Minor));
838 break;
839
840 case MAKE_SIG(ISO9660RRIPTF_SIG1, ISO9660RRIPTF_SIG2): /* TF */
841 if ( pUnion->TF.Hdr.bVersion != ISO9660RRIPTF_VER
842 || pUnion->TF.Hdr.cbEntry < Iso9660RripTfCalcLength(pUnion->TF.fFlags))
843 LogRel(("rtFsIsoImport/Rock: Malformed 'TF' entry: cbEntry=%#x (vs %#x), bVersion=%#x (vs %#x) fFlags=%#x\n",
844 pUnion->TF.Hdr.cbEntry, Iso9660RripTfCalcLength(pUnion->TF.fFlags),
845 pUnion->TF.Hdr.bVersion, ISO9660RRIPTF_VER, RT_BE2H_U32(pUnion->TF.fFlags) ));
846 else if (!(pUnion->TF.fFlags & ISO9660RRIPTF_F_LONG_FORM))
847 {
848 PCISO9660RECTIMESTAMP pTimestamp = (PCISO9660RECTIMESTAMP)&pUnion->TF.abPayload[0];
849 if (pUnion->TF.fFlags & ISO9660RRIPTF_F_BIRTH)
850 {
851 rtFsIsoImpIso9660RecDateTime2TimeSpec(&pObjInfo->BirthTime, pTimestamp);
852 pTimestamp++;
853 }
854 if (pUnion->TF.fFlags & ISO9660RRIPTF_F_MODIFY)
855 {
856 rtFsIsoImpIso9660RecDateTime2TimeSpec(&pObjInfo->ModificationTime, pTimestamp);
857 pTimestamp++;
858 }
859 if (pUnion->TF.fFlags & ISO9660RRIPTF_F_ACCESS)
860 {
861 rtFsIsoImpIso9660RecDateTime2TimeSpec(&pObjInfo->AccessTime, pTimestamp);
862 pTimestamp++;
863 }
864 if (pUnion->TF.fFlags & ISO9660RRIPTF_F_CHANGE)
865 {
866 rtFsIsoImpIso9660RecDateTime2TimeSpec(&pObjInfo->ChangeTime, pTimestamp);
867 pTimestamp++;
868 }
869 }
870 else
871 {
872 PCISO9660TIMESTAMP pTimestamp = (PCISO9660TIMESTAMP)&pUnion->TF.abPayload[0];
873 if (pUnion->TF.fFlags & ISO9660RRIPTF_F_BIRTH)
874 {
875 rtFsIsoImpIso9660DateTime2TimeSpecIfValid(&pObjInfo->BirthTime, pTimestamp);
876 pTimestamp++;
877 }
878 if (pUnion->TF.fFlags & ISO9660RRIPTF_F_MODIFY)
879 {
880 rtFsIsoImpIso9660DateTime2TimeSpecIfValid(&pObjInfo->ModificationTime, pTimestamp);
881 pTimestamp++;
882 }
883 if (pUnion->TF.fFlags & ISO9660RRIPTF_F_ACCESS)
884 {
885 rtFsIsoImpIso9660DateTime2TimeSpecIfValid(&pObjInfo->AccessTime, pTimestamp);
886 pTimestamp++;
887 }
888 if (pUnion->TF.fFlags & ISO9660RRIPTF_F_CHANGE)
889 {
890 rtFsIsoImpIso9660DateTime2TimeSpecIfValid(&pObjInfo->ChangeTime, pTimestamp);
891 pTimestamp++;
892 }
893 }
894 break;
895
896 case MAKE_SIG(ISO9660RRIPSF_SIG1, ISO9660RRIPSF_SIG2): /* SF */
897 LogRel(("rtFsIsoImport/Rock: Sparse file support not yet implemented!\n"));
898 break;
899
900 case MAKE_SIG(ISO9660RRIPSL_SIG1, ISO9660RRIPSL_SIG2): /* SL */
901 if ( pUnion->SL.Hdr.bVersion != ISO9660RRIPSL_VER
902 || pUnion->SL.Hdr.cbEntry < RT_OFFSETOF(ISO9660RRIPSL, abComponents[2])
903 || (pUnion->SL.fFlags & ~ISO9660RRIP_SL_F_CONTINUE)
904 || (pUnion->SL.abComponents[0] & ISO9660RRIP_SL_C_RESERVED_MASK) )
905 LogRel(("rtFsIsoImport/Rock: Malformed 'SL' entry: cbEntry=%#x (vs %#x), bVersion=%#x (vs %#x) fFlags=%#x comp[0].fFlags=%#x\n",
906 pUnion->SL.Hdr.cbEntry, RT_OFFSETOF(ISO9660RRIPSL, abComponents[2]),
907 pUnion->SL.Hdr.bVersion, ISO9660RRIPSL_VER, pUnion->SL.fFlags, pUnion->SL.abComponents[0]));
908 else if (pThis->fSeenLastSL)
909 LogRel(("rtFsIsoImport/Rock: Unexpected 'SL!' entry\n"));
910 else
911 {
912 pThis->fSeenLastSL = !(pUnion->SL.fFlags & ISO9660RRIP_SL_F_CONTINUE); /* used in loop */
913
914 size_t offDst = strlen(pThis->szRockSymlinkTargetBuf);
915 uint8_t const *pbSrc = &pUnion->SL.abComponents[0];
916 uint8_t cbSrcLeft = pUnion->SL.Hdr.cbEntry - RT_OFFSETOF(ISO9660RRIPSL, abComponents);
917 while (cbSrcLeft >= 2)
918 {
919 uint8_t const fFlags = pbSrc[0];
920 uint8_t cchCopy = pbSrc[1];
921 uint8_t const cbSkip = cchCopy + 2;
922 if (cbSkip > cbSrcLeft)
923 {
924 LogRel(("rtFsIsoImport/Rock: Malformed 'SL' component: component flags=%#x, component length+2=%#x vs %#x left\n",
925 fFlags, cbSkip, cbSrcLeft));
926 break;
927 }
928
929 const char *pszCopy;
930 switch (fFlags & ~ISO9660RRIP_SL_C_CONTINUE)
931 {
932 case 0:
933 pszCopy = (const char *)&pbSrc[2];
934 break;
935
936 case ISO9660RRIP_SL_C_CURRENT:
937 if (cchCopy != 0)
938 LogRel(("rtFsIsoImport/Rock: Malformed 'SL' component: CURRENT + %u bytes, ignoring bytes\n", cchCopy));
939 pszCopy = ".";
940 cchCopy = 1;
941 break;
942
943 case ISO9660RRIP_SL_C_PARENT:
944 if (cchCopy != 0)
945 LogRel(("rtFsIsoImport/Rock: Malformed 'SL' component: PARENT + %u bytes, ignoring bytes\n", cchCopy));
946 pszCopy = "..";
947 cchCopy = 2;
948 break;
949
950 case ISO9660RRIP_SL_C_ROOT:
951 if (cchCopy != 0)
952 LogRel(("rtFsIsoImport/Rock: Malformed 'SL' component: ROOT + %u bytes, ignoring bytes\n", cchCopy));
953 pszCopy = "/";
954 cchCopy = 1;
955 break;
956
957 default:
958 LogRel(("rtFsIsoImport/Rock: Malformed 'SL' component: component flags=%#x (bad), component length=%#x vs %#x left\n",
959 fFlags, cchCopy, cbSrcLeft));
960 pszCopy = NULL;
961 cchCopy = 0;
962 break;
963 }
964
965 if (offDst + cchCopy < sizeof(pThis->szRockSymlinkTargetBuf))
966 {
967 memcpy(&pThis->szRockSymlinkTargetBuf[offDst], pszCopy, cchCopy);
968 offDst += cchCopy;
969 }
970 else
971 {
972 LogRel(("rtFsIsoImport/Rock: 'SL' constructs a too long target! '%.*s%.*s'\n",
973 offDst, pThis->szRockSymlinkTargetBuf, cchCopy, pszCopy));
974 memcpy(&pThis->szRockSymlinkTargetBuf[offDst], pszCopy,
975 sizeof(pThis->szRockSymlinkTargetBuf) - offDst - 1);
976 offDst = sizeof(pThis->szRockSymlinkTargetBuf) - 1;
977 break;
978 }
979
980 /* Advance */
981 pbSrc += cbSkip;
982 cbSrcLeft -= cbSkip;
983
984 /* Append slash if appropriate. */
985 if ( !(fFlags & ISO9660RRIP_SL_C_CONTINUE)
986 && (cbSrcLeft >= 2 || !pThis->fSeenLastSL) )
987 {
988 if (offDst + 1 < sizeof(pThis->szRockSymlinkTargetBuf))
989 pThis->szRockSymlinkTargetBuf[offDst++] = '/';
990 else
991 {
992 LogRel(("rtFsIsoImport/Rock: 'SL' constructs a too long target! '%.*s/'\n",
993 offDst, pThis->szRockSymlinkTargetBuf));
994 break;
995 }
996 }
997 }
998 pThis->szRockSymlinkTargetBuf[offDst] = '\0';
999
1000 /* Purge the encoding as we don't want invalid UTF-8 floating around. */
1001 RTStrPurgeEncoding(pThis->szRockSymlinkTargetBuf);
1002 }
1003 break;
1004
1005 case MAKE_SIG(ISO9660RRIPNM_SIG1, ISO9660RRIPNM_SIG2): /* NM */
1006 if ( pUnion->NM.Hdr.bVersion != ISO9660RRIPNM_VER
1007 || pUnion->NM.Hdr.cbEntry < RT_OFFSETOF(ISO9660RRIPNM, achName)
1008 || (pUnion->NM.fFlags & ISO9660RRIP_NM_F_RESERVED_MASK) )
1009 LogRel(("rtFsIsoImport/Rock: Malformed 'NM' entry: cbEntry=%#x (vs %#x), bVersion=%#x (vs %#x) fFlags=%#x %.*Rhxs\n",
1010 pUnion->NM.Hdr.cbEntry, RT_OFFSETOF(ISO9660RRIPNM, achName),
1011 pUnion->NM.Hdr.bVersion, ISO9660RRIPNM_VER, pUnion->NM.fFlags,
1012 pUnion->NM.Hdr.cbEntry - RT_MIN(pUnion->NM.Hdr.cbEntry, RT_OFFSETOF(ISO9660RRIPNM, achName)),
1013 &pUnion->NM.achName[0] ));
1014 else if (pThis->fSeenLastNM)
1015 LogRel(("rtFsIsoImport/Rock: Unexpected 'NM' entry!\n"));
1016 else
1017 {
1018 pThis->fSeenLastNM = !(pUnion->NM.fFlags & ISO9660RRIP_NM_F_CONTINUE);
1019
1020 uint8_t const cchName = pUnion->NM.Hdr.cbEntry - (uint8_t)RT_OFFSETOF(ISO9660RRIPNM, achName);
1021 if (pUnion->NM.fFlags & (ISO9660RRIP_NM_F_CURRENT | ISO9660RRIP_NM_F_PARENT))
1022 {
1023 if (cchName == 0 && pThis->szRockNameBuf[0] == '\0')
1024 Log(("rtFsIsoImport/Rock: Ignoring 'NM' entry for '.' and '..'\n"));
1025 else
1026 LogRel(("rtFsIsoImport/Rock: Ignoring malformed 'NM' using '.' or '..': fFlags=%#x cchName=%#x %.*Rhxs; szRockNameBuf='%s'\n",
1027 pUnion->NM.fFlags, cchName, cchName, pUnion->NM.achName, pThis->szRockNameBuf));
1028 pThis->szRockNameBuf[0] = '\0';
1029 pThis->fSeenLastNM = true;
1030 }
1031 else
1032 {
1033 size_t offDst = strlen(pThis->szRockNameBuf);
1034 if (offDst + cchName < sizeof(pThis->szRockNameBuf))
1035 {
1036 memcpy(&pThis->szRockNameBuf[offDst], pUnion->NM.achName, cchName);
1037 pThis->szRockNameBuf[offDst + cchName] = '\0';
1038
1039 /* Purge the encoding as we don't want invalid UTF-8 floating around. */
1040 RTStrPurgeEncoding(pThis->szRockSymlinkTargetBuf);
1041 }
1042 else
1043 {
1044 LogRel(("rtFsIsoImport/Rock: 'NM' constructs a too long name, ignoring it all: '%s%.*s'\n",
1045 pThis->szRockNameBuf, cchName, pUnion->NM.achName));
1046 pThis->szRockNameBuf[0] = '\0';
1047 pThis->fSeenLastNM = true;
1048 }
1049 }
1050 }
1051 break;
1052
1053 case MAKE_SIG(ISO9660RRIPCL_SIG1, ISO9660RRIPCL_SIG2): /* CL - just warn for now. */
1054 case MAKE_SIG(ISO9660RRIPPL_SIG1, ISO9660RRIPPL_SIG2): /* PL - just warn for now. */
1055 case MAKE_SIG(ISO9660RRIPRE_SIG1, ISO9660RRIPRE_SIG2): /* RE - just warn for now. */
1056 LogRel(("rtFsIsoImport/Rock: Ignorning directory relocation entry '%c%c'!\n", pUnion->Hdr.bSig1, pUnion->Hdr.bSig2));
1057 break;
1058
1059 default:
1060 LogRel(("rtFsIsoImport/Rock: Unknown SUSP entry: %#x %#x, %#x bytes, v%u\n",
1061 pUnion->Hdr.bSig1, pUnion->Hdr.bSig2, pUnion->Hdr.cbEntry, pUnion->Hdr.bVersion));
1062 break;
1063#undef MAKE_SIG
1064 }
1065 }
1066}
1067
1068
1069/**
1070 * Deals with the special '.' entry in the root directory.
1071 *
1072 * @returns IPRT status code.
1073 * @param pThis The import instance.
1074 * @param pDirRec The root directory record.
1075 */
1076static int rtFsIsoImportProcessIso9660TreeWorkerDoRockForRoot(PRTFSISOMKIMPORTER pThis, PCISO9660DIRREC pDirRec)
1077{
1078 uint8_t const cbSys = pDirRec->cbDirRec - RT_UOFFSETOF(ISO9660DIRREC, achFileId)
1079 - pDirRec->bFileIdLength - !(pDirRec->bFileIdLength & 1);
1080 uint8_t const * const pbSys = (uint8_t const *)&pDirRec->achFileId[pDirRec->bFileIdLength + !(pDirRec->bFileIdLength & 1)];
1081 if (cbSys > 4)
1082 {
1083 RTFSOBJINFO ObjInfo;
1084 ObjInfo.cbObject = 0;
1085 ObjInfo.cbAllocated = 0;
1086 rtFsIsoImpIso9660RecDateTime2TimeSpec(&ObjInfo.AccessTime, &pDirRec->RecTime);
1087 ObjInfo.ModificationTime = ObjInfo.AccessTime;
1088 ObjInfo.ChangeTime = ObjInfo.AccessTime;
1089 ObjInfo.BirthTime = ObjInfo.AccessTime;
1090 ObjInfo.Attr.fMode = RTFS_TYPE_DIRECTORY | RTFS_DOS_DIRECTORY | 0555;
1091 ObjInfo.Attr.enmAdditional = RTFSOBJATTRADD_UNIX;
1092 ObjInfo.Attr.u.Unix.uid = NIL_RTUID;
1093 ObjInfo.Attr.u.Unix.gid = NIL_RTGID;
1094 ObjInfo.Attr.u.Unix.cHardlinks = 2;
1095 ObjInfo.Attr.u.Unix.INodeIdDevice = 0;
1096 ObjInfo.Attr.u.Unix.INodeId = 0;
1097 ObjInfo.Attr.u.Unix.fFlags = 0;
1098 ObjInfo.Attr.u.Unix.GenerationId = 0;
1099 ObjInfo.Attr.u.Unix.Device = 0;
1100
1101 rtFsIsoImportProcessIso9660TreeWorkerParseRockRidge(pThis, &ObjInfo, pbSys, cbSys,
1102 false /*fContinuationRecord*/, true /*fIsFirstDirRec*/);
1103 /** @todo Update root dir attribs. Need API. */
1104 }
1105 return VINF_SUCCESS;
1106}
1107
1108
1109/**
1110 * Validates a directory record.
1111 *
1112 * @returns IPRT status code (safe to ignore, see pThis->rc).
1113 * @param pThis The importer instance.
1114 * @param pDirRec The directory record to validate.
1115 * @param cbMax The maximum size.
1116 */
1117static int rtFsIsoImportValidateDirRec(PRTFSISOMKIMPORTER pThis, PCISO9660DIRREC pDirRec, uint32_t cbMax)
1118{
1119 /*
1120 * Validate dual fields.
1121 */
1122 if (RT_LE2H_U32(pDirRec->cbData.le) != RT_BE2H_U32(pDirRec->cbData.be))
1123 return rtFsIsoImpError(pThis, VERR_ISOMK_IMPORT_BAD_DIR_REC,
1124 "Invalid dir rec size field: {%#RX32,%#RX32}",
1125 RT_BE2H_U32(pDirRec->cbData.be), RT_LE2H_U32(pDirRec->cbData.le));
1126
1127 if (RT_LE2H_U32(pDirRec->offExtent.le) != RT_BE2H_U32(pDirRec->offExtent.be))
1128 return rtFsIsoImpError(pThis, VERR_ISOMK_IMPORT_BAD_DIR_REC,
1129 "Invalid dir rec extent field: {%#RX32,%#RX32}",
1130 RT_BE2H_U32(pDirRec->offExtent.be), RT_LE2H_U32(pDirRec->offExtent.le));
1131
1132 if (RT_LE2H_U16(pDirRec->VolumeSeqNo.le) != RT_BE2H_U16(pDirRec->VolumeSeqNo.be))
1133 return rtFsIsoImpError(pThis, VERR_ISOMK_IMPORT_BAD_DIR_REC,
1134 "Invalid dir rec volume sequence ID field: {%#RX16,%#RX16}",
1135 RT_BE2H_U16(pDirRec->VolumeSeqNo.be), RT_LE2H_U16(pDirRec->VolumeSeqNo.le));
1136
1137 /*
1138 * Check values.
1139 */
1140 if (ISO9660_GET_ENDIAN(&pDirRec->VolumeSeqNo) != pThis->idPrimaryVol)
1141 return rtFsIsoImpError(pThis, VERR_ISOMK_IMPORT_DIR_REC_VOLUME_SEQ_NO,
1142 "Expected dir rec to have same volume sequence number as primary volume: %#x, expected %#x",
1143 ISO9660_GET_ENDIAN(&pDirRec->VolumeSeqNo), pThis->idPrimaryVol);
1144
1145 if (ISO9660_GET_ENDIAN(&pDirRec->offExtent) >= pThis->cBlocksInPrimaryVolumeSpace)
1146 return rtFsIsoImpError(pThis, VERR_ISOMK_IMPORT_DIR_REC_EXTENT_OUT_OF_BOUNDS,
1147 "Invalid dir rec extent: %#RX32, max %#RX32",
1148 ISO9660_GET_ENDIAN(&pDirRec->offExtent), pThis->cBlocksInPrimaryVolumeSpace);
1149
1150 if (pDirRec->cbDirRec < RT_OFFSETOF(ISO9660DIRREC, achFileId) + pDirRec->bFileIdLength)
1151 return rtFsIsoImpError(pThis, VERR_ISOMK_IMPORT_BAD_DIR_REC_LENGTH,
1152 "Dir record size is too small: %#x (min %#x)",
1153 pDirRec->cbDirRec, RT_OFFSETOF(ISO9660DIRREC, achFileId) + pDirRec->bFileIdLength);
1154 if (pDirRec->cbDirRec > cbMax)
1155 return rtFsIsoImpError(pThis, VERR_ISOMK_IMPORT_BAD_DIR_REC_LENGTH,
1156 "Dir record size is too big: %#x (max %#x)", pDirRec->cbDirRec, cbMax);
1157
1158 if ( (pDirRec->fFileFlags & (ISO9660_FILE_FLAGS_MULTI_EXTENT | ISO9660_FILE_FLAGS_DIRECTORY))
1159 == (ISO9660_FILE_FLAGS_MULTI_EXTENT | ISO9660_FILE_FLAGS_DIRECTORY))
1160 return rtFsIsoImpError(pThis, VERR_ISOMK_IMPORT_DIR_WITH_MORE_EXTENTS,
1161 "Multi-extent directories are not supported (cbData=%#RX32 offExtent=%#RX32)",
1162 ISO9660_GET_ENDIAN(&pDirRec->cbData), ISO9660_GET_ENDIAN(&pDirRec->offExtent));
1163
1164 return VINF_SUCCESS;
1165}
1166
1167
1168/**
1169 * Validates a dot or dot-dot directory record.
1170 *
1171 * @returns IPRT status code (safe to ignore, see pThis->rc).
1172 * @param pThis The importer instance.
1173 * @param pDirRec The dot directory record to validate.
1174 * @param cbMax The maximum size.
1175 * @param bName The name byte (0x00: '.', 0x01: '..').
1176 */
1177static int rtFsIsoImportValidateDotDirRec(PRTFSISOMKIMPORTER pThis, PCISO9660DIRREC pDirRec, uint32_t cbMax, uint8_t bName)
1178{
1179 int rc = rtFsIsoImportValidateDirRec(pThis, pDirRec, cbMax);
1180 if (RT_SUCCESS(rc))
1181 {
1182 if (pDirRec->bFileIdLength != 1)
1183 return rtFsIsoImpError(pThis, VERR_ISOMK_IMPORT_DOT_DIR_REC_BAD_NAME_LENGTH,
1184 "Invalid dot dir rec file id length: %u", pDirRec->bFileIdLength);
1185 if ((uint8_t)pDirRec->achFileId[0] != bName)
1186 return rtFsIsoImpError(pThis, VERR_ISOMK_IMPORT_DOT_DIR_REC_BAD_NAME,
1187 "Invalid dot dir rec file id: %#x, expected %#x", pDirRec->achFileId[0], bName);
1188 }
1189 return rc;
1190}
1191
1192
1193/**
1194 * rtFsIsoImportProcessIso9660TreeWorker helper that reads more data.
1195 *
1196 * @returns IPRT status code.
1197 * @param pThis The importer instance.
1198 * @param ppDirRec Pointer to the directory record pointer (in/out).
1199 * @param pcbChunk Pointer to the cbChunk variable (in/out).
1200 * @param pcbDir Pointer to the cbDir variable (in/out). This indicates
1201 * how much we've left to read from the directory.
1202 * @param poffNext Pointer to the offNext variable (in/out). This
1203 * indicates where the next chunk of directory data is in
1204 * the input file.
1205 */
1206static int rtFsIsoImportProcessIso9660TreeWorkerReadMore(PRTFSISOMKIMPORTER pThis, PCISO9660DIRREC *ppDirRec,
1207 uint32_t *pcbChunk, uint32_t *pcbDir, uint64_t *poffNext)
1208{
1209 uint32_t cbChunk = *pcbChunk;
1210 *ppDirRec = (PCISO9660DIRREC)memmove(&pThis->abBuf[ISO9660_SECTOR_SIZE - cbChunk], *ppDirRec, cbChunk);
1211
1212 Assert(!(*poffNext & (ISO9660_SECTOR_SIZE - 1)));
1213 uint32_t cbToRead = RT_MIN(*pcbDir, sizeof(pThis->abBuf) - ISO9660_SECTOR_SIZE);
1214 int rc = RTVfsFileReadAt(pThis->hSrcFile, *poffNext, &pThis->abBuf[ISO9660_SECTOR_SIZE], cbToRead, NULL);
1215 if (RT_SUCCESS(rc))
1216 {
1217 Log3(("rtFsIsoImportProcessIso9660TreeWorker: Read %#zx more bytes @%#RX64, now got @%#RX64 LB %#RX32\n",
1218 cbToRead, *poffNext, *poffNext - cbChunk, cbChunk + cbToRead));
1219 *poffNext += cbToRead;
1220 *pcbDir -= cbToRead;
1221 *pcbChunk = cbChunk + cbToRead;
1222 return VINF_SUCCESS;
1223 }
1224 return rtFsIsoImpError(pThis, rc, "Error reading %#RX32 bytes at %#RX64 (dir): %Rrc", *poffNext, cbToRead);
1225}
1226
1227
1228/**
1229 * rtFsIsoImportProcessIso9660TreeWorker helper that deals with skipping to the
1230 * next sector when cbDirRec is zero.
1231 *
1232 * @returns IPRT status code.
1233 * @retval VERR_NO_MORE_FILES when we reaches the end of the directory.
1234 * @param pThis The importer instance.
1235 * @param ppDirRec Pointer to the directory record pointer (in/out).
1236 * @param pcbChunk Pointer to the cbChunk variable (in/out). Indicates how
1237 * much we've left to process starting and pDirRec.
1238 * @param pcbDir Pointer to the cbDir variable (in/out). This indicates
1239 * how much we've left to read from the directory.
1240 * @param poffNext Pointer to the offNext variable (in/out). This
1241 * indicates where the next chunk of directory data is in
1242 * the input file.
1243 */
1244static int rtFsIsoImportProcessIso9660TreeWorkerHandleZeroSizedDirRec(PRTFSISOMKIMPORTER pThis, PCISO9660DIRREC *ppDirRec,
1245 uint32_t *pcbChunk, uint32_t *pcbDir, uint64_t *poffNext)
1246{
1247 uint32_t cbChunk = *pcbChunk;
1248 uint64_t offChunk = *poffNext - cbChunk;
1249 uint32_t cbSkip = ISO9660_SECTOR_SIZE - ((uint32_t)offChunk & (ISO9660_SECTOR_SIZE - 1));
1250 if (cbSkip < cbChunk)
1251 {
1252 *ppDirRec = (PCISO9660DIRREC)((uintptr_t)*ppDirRec + cbSkip);
1253 *pcbChunk = cbChunk -= cbSkip;
1254 if ( cbChunk > UINT8_MAX
1255 || *pcbDir == 0)
1256 {
1257 Log3(("rtFsIsoImportProcessIso9660TreeWorker: cbDirRec=0 --> jumped %#RX32 to @%#RX64 LB %#RX32\n",
1258 cbSkip, *poffNext - cbChunk, cbChunk));
1259 return VINF_SUCCESS;
1260 }
1261 Log3(("rtFsIsoImportProcessIso9660TreeWorker: cbDirRec=0 --> jumped %#RX32 to @%#RX64 LB %#RX32, but needs to read more\n",
1262 cbSkip, *poffNext - cbChunk, cbChunk));
1263 return rtFsIsoImportProcessIso9660TreeWorkerReadMore(pThis, ppDirRec, pcbChunk, pcbDir, poffNext);
1264 }
1265
1266 /* ASSUMES we're working in multiples of sectors! */
1267 if (*pcbDir == 0)
1268 {
1269 *pcbChunk = 0;
1270 return VERR_NO_MORE_FILES;
1271 }
1272
1273 /* End of chunk, read the next sectors. */
1274 Assert(!(*poffNext & (ISO9660_SECTOR_SIZE - 1)));
1275 uint32_t cbToRead = RT_MIN(*pcbDir, sizeof(pThis->abBuf));
1276 int rc = RTVfsFileReadAt(pThis->hSrcFile, *poffNext, pThis->abBuf, cbToRead, NULL);
1277 if (RT_SUCCESS(rc))
1278 {
1279 Log3(("rtFsIsoImportProcessIso9660TreeWorker: cbDirRec=0 --> Read %#zx more bytes @%#RX64, now got @%#RX64 LB %#RX32\n",
1280 cbToRead, *poffNext, *poffNext - cbChunk, cbChunk + cbToRead));
1281 *poffNext += cbToRead;
1282 *pcbDir -= cbToRead;
1283 *pcbChunk = cbChunk + cbToRead;
1284 *ppDirRec = (PCISO9660DIRREC)&pThis->abBuf[0];
1285 return VINF_SUCCESS;
1286 }
1287 return rtFsIsoImpError(pThis, rc, "Error reading %#RX32 bytes at %#RX64 (dir): %Rrc", *poffNext, cbToRead);
1288}
1289
1290
1291/**
1292 * Deals with a single directory.
1293 *
1294 * @returns IPRT status code (safe to ignore, see pThis->rc).
1295 * @param pThis The importer instance.
1296 * @param idxDir The configuration index for the directory.
1297 * @param offDirBlock The offset of the directory data.
1298 * @param cbDir The size of the directory data.
1299 * @param cDepth The depth of the directory.
1300 * @param fUnicode Set if it's a unicode (UTF-16BE) encoded
1301 * directory.
1302 * @param pTodoList The todo-list to add sub-directories to.
1303 */
1304static int rtFsIsoImportProcessIso9660TreeWorker(PRTFSISOMKIMPORTER pThis, uint32_t idxDir,
1305 uint32_t offDirBlock, uint32_t cbDir, uint8_t cDepth, bool fUnicode,
1306 PRTLISTANCHOR pTodoList)
1307{
1308 /*
1309 * Restrict the depth to try avoid loops.
1310 */
1311 if (cDepth > RTFSISOMK_IMPORT_MAX_DEPTH)
1312 return rtFsIsoImpError(pThis, VERR_ISOMK_IMPORT_TOO_DEEP_DIR_TREE, "Dir at %#x LB %#x is too deep", offDirBlock, cbDir);
1313
1314 /*
1315 * Read the first chunk into the big buffer.
1316 */
1317 uint32_t cbChunk = RT_MIN(cbDir, sizeof(pThis->abBuf));
1318 uint64_t offNext = (uint64_t)offDirBlock * ISO9660_SECTOR_SIZE;
1319 int rc = RTVfsFileReadAt(pThis->hSrcFile, offNext, pThis->abBuf, cbChunk, NULL);
1320 if (RT_FAILURE(rc))
1321 return rtFsIsoImpError(pThis, rc, "Error reading directory at %#RX64 (%#RX32 / %#RX32): %Rrc", offNext, cbChunk, cbDir);
1322
1323 cbDir -= cbChunk;
1324 offNext += cbChunk;
1325
1326 /*
1327 * Skip the current and parent directory entries.
1328 */
1329 PCISO9660DIRREC pDirRec = (PCISO9660DIRREC)&pThis->abBuf[0];
1330 rc = rtFsIsoImportValidateDotDirRec(pThis, pDirRec, cbChunk, 0x00);
1331 if (RT_FAILURE(rc))
1332 return rc;
1333 if ( cDepth == 0
1334 && !(pThis->fFlags & RTFSISOMK_IMPORT_F_NO_ROCK_RIDGE)
1335 && pDirRec->cbDirRec > RT_UOFFSETOF(ISO9660DIRREC, achFileId[1]))
1336 {
1337 rc = rtFsIsoImportProcessIso9660TreeWorkerDoRockForRoot(pThis, pDirRec);
1338 if (RT_FAILURE(rc))
1339 return rc;
1340 }
1341
1342 cbChunk -= pDirRec->cbDirRec;
1343 pDirRec = (PCISO9660DIRREC)((uintptr_t)pDirRec + pDirRec->cbDirRec);
1344 rc = rtFsIsoImportValidateDotDirRec(pThis, pDirRec, cbChunk, 0x01);
1345 if (RT_FAILURE(rc))
1346 return rc;
1347
1348 cbChunk -= pDirRec->cbDirRec;
1349 pDirRec = (PCISO9660DIRREC)((uintptr_t)pDirRec + pDirRec->cbDirRec);
1350
1351 /*
1352 * Work our way thru all the directory records.
1353 */
1354 Log3(("rtFsIsoImportProcessIso9660TreeWorker: Starting at @%#RX64 LB %#RX32 (out of %#RX32) in %#x\n",
1355 offNext - cbChunk, cbChunk, cbChunk + cbDir, idxDir));
1356 const uint32_t fNamespace = fUnicode ? RTFSISOMAKER_NAMESPACE_JOLIET : RTFSISOMAKER_NAMESPACE_ISO_9660;
1357 while ( cbChunk > 0
1358 || cbDir > 0)
1359 {
1360 /*
1361 * Do we need to read some more?
1362 */
1363 if ( cbChunk > UINT8_MAX
1364 || cbDir == 0)
1365 { /* No, we don't. */ }
1366 else
1367 {
1368 rc = rtFsIsoImportProcessIso9660TreeWorkerReadMore(pThis, &pDirRec, &cbChunk, &cbDir, &offNext);
1369 if (RT_FAILURE(rc))
1370 return rc;
1371 }
1372
1373 /* If null length, skip to the next sector. May have to read some then. */
1374 if (pDirRec->cbDirRec != 0)
1375 { /* likely */ }
1376 else
1377 {
1378 rc = rtFsIsoImportProcessIso9660TreeWorkerHandleZeroSizedDirRec(pThis, &pDirRec, &cbChunk, &cbDir, &offNext);
1379 if (RT_FAILURE(rc))
1380 {
1381 if (rc == VERR_NO_MORE_FILES)
1382 break;
1383 return rc;
1384 }
1385 if (pDirRec->cbDirRec == 0)
1386 continue;
1387 }
1388
1389 /*
1390 * Validate the directory record. Give up if not valid since we're
1391 * likely to get error with subsequent record too.
1392 */
1393 uint8_t const cbSys = pDirRec->cbDirRec - RT_UOFFSETOF(ISO9660DIRREC, achFileId)
1394 - pDirRec->bFileIdLength - !(pDirRec->bFileIdLength & 1);
1395 uint8_t const * const pbSys = (uint8_t const *)&pDirRec->achFileId[pDirRec->bFileIdLength + !(pDirRec->bFileIdLength & 1)];
1396 Log3(("pDirRec=&abBuf[%#07zx]: @%#010RX64 cb=%#04x ff=%#04x off=%#010RX32 cb=%#010RX32 cbSys=%#x id=%.*Rhxs\n",
1397 (uintptr_t)pDirRec - (uintptr_t)&pThis->abBuf[0], offNext - cbChunk, pDirRec->cbDirRec, pDirRec->fFileFlags,
1398 ISO9660_GET_ENDIAN(&pDirRec->offExtent), ISO9660_GET_ENDIAN(&pDirRec->cbData), cbSys,
1399 pDirRec->bFileIdLength, pDirRec->achFileId));
1400 rc = rtFsIsoImportValidateDirRec(pThis, pDirRec, cbChunk);
1401 if (RT_FAILURE(rc))
1402 return rc;
1403
1404 /* This early calculation of the next record is due to multi-extent
1405 handling further down. */
1406 uint32_t cbChunkNew = cbChunk - pDirRec->cbDirRec;
1407 PCISO9660DIRREC pDirRecNext = (PCISO9660DIRREC)((uintptr_t)pDirRec + pDirRec->cbDirRec);
1408
1409 /* Start Collecting object info. */
1410 RTFSOBJINFO ObjInfo;
1411 ObjInfo.cbObject = ISO9660_GET_ENDIAN(&pDirRec->cbData);
1412 ObjInfo.cbAllocated = ObjInfo.cbObject;
1413 rtFsIsoImpIso9660RecDateTime2TimeSpec(&ObjInfo.AccessTime, &pDirRec->RecTime);
1414 ObjInfo.ModificationTime = ObjInfo.AccessTime;
1415 ObjInfo.ChangeTime = ObjInfo.AccessTime;
1416 ObjInfo.BirthTime = ObjInfo.AccessTime;
1417 ObjInfo.Attr.fMode = pDirRec->fFileFlags & ISO9660_FILE_FLAGS_DIRECTORY
1418 ? RTFS_TYPE_DIRECTORY | RTFS_DOS_DIRECTORY | 0555
1419 : RTFS_TYPE_FILE | RTFS_DOS_ARCHIVED | 0444;
1420 ObjInfo.Attr.enmAdditional = RTFSOBJATTRADD_UNIX;
1421 ObjInfo.Attr.u.Unix.uid = NIL_RTUID;
1422 ObjInfo.Attr.u.Unix.gid = NIL_RTGID;
1423 ObjInfo.Attr.u.Unix.cHardlinks = 1;
1424 ObjInfo.Attr.u.Unix.INodeIdDevice = 0;
1425 ObjInfo.Attr.u.Unix.INodeId = 0;
1426 ObjInfo.Attr.u.Unix.fFlags = 0;
1427 ObjInfo.Attr.u.Unix.GenerationId = 0;
1428 ObjInfo.Attr.u.Unix.Device = 0;
1429
1430 /*
1431 * Convert the name into the name buffer (szNameBuf).
1432 */
1433 if (!fUnicode)
1434 {
1435 memcpy(pThis->szNameBuf, pDirRec->achFileId, pDirRec->bFileIdLength);
1436 pThis->szNameBuf[pDirRec->bFileIdLength] = '\0';
1437 rc = RTStrValidateEncoding(pThis->szNameBuf);
1438 }
1439 else
1440 {
1441 char *pszDst = pThis->szNameBuf;
1442 rc = RTUtf16BigToUtf8Ex((PRTUTF16)pDirRec->achFileId, pDirRec->bFileIdLength / sizeof(RTUTF16),
1443 &pszDst, sizeof(pThis->szNameBuf), NULL);
1444 }
1445 if (RT_SUCCESS(rc))
1446 {
1447 /* Drop the version from the name. */
1448 size_t cchName = strlen(pThis->szNameBuf);
1449 if ( !(pDirRec->fFileFlags & ISO9660_FILE_FLAGS_DIRECTORY)
1450 && cchName > 2
1451 && RT_C_IS_DIGIT(pThis->szNameBuf[cchName - 1]))
1452 {
1453 uint32_t offName = 2;
1454 while ( offName <= 5
1455 && offName + 1 < cchName
1456 && RT_C_IS_DIGIT(pThis->szNameBuf[cchName - offName]))
1457 offName++;
1458 if ( offName + 1 < cchName
1459 && pThis->szNameBuf[cchName - offName] == ';')
1460 {
1461 RTStrToUInt32Full(&pThis->szNameBuf[cchName - offName + 1], 10, &ObjInfo.Attr.u.Unix.GenerationId);
1462 pThis->szNameBuf[cchName - offName] = '\0';
1463 }
1464 }
1465 Log3((" --> name='%s'\n", pThis->szNameBuf));
1466
1467 pThis->szRockNameBuf[0] = '\0';
1468 pThis->szRockSymlinkTargetBuf[0] = '\0';
1469 if (cbSys > 0 && !(pThis->fFlags & RTFSISOMK_IMPORT_F_NO_ROCK_RIDGE))
1470 {
1471 pThis->fSeenLastNM = false;
1472 pThis->fSeenLastSL = false;
1473 pThis->szRockNameBuf[0] = '\0';
1474 pThis->szRockSymlinkTargetBuf[0] = '\0';
1475 rtFsIsoImportProcessIso9660TreeWorkerParseRockRidge(pThis, &ObjInfo, pbSys, cbSys,
1476 false /*fContinuationRecord*/, false /*fIsFirstDirRec*/);
1477 }
1478
1479 /*
1480 * Deal with multi-extent files (usually large ones). We currently only
1481 * handle files where the data is in single continuous chunk and only split
1482 * up into multiple directory records because of data type limitations.
1483 */
1484 uint8_t abDirRecCopy[256];
1485 uint64_t cbData = ISO9660_GET_ENDIAN(&pDirRec->cbData);
1486 if (!(pDirRec->fFileFlags & ISO9660_FILE_FLAGS_MULTI_EXTENT))
1487 { /* likely */ }
1488 else
1489 {
1490 if (cbData & (ISO9660_SECTOR_SIZE - 1))
1491 return rtFsIsoImpError(pThis, VERR_ISOMK_IMPORT_MISALIGNED_MULTI_EXTENT,
1492 "The size of non-final multi-extent record #0x0 isn't block aligned: %#RX64", cbData);
1493
1494 /* Make a copy of the first directory record so we don't overwrite
1495 it when reading in more records below. */
1496 pDirRec = (PCISO9660DIRREC)memcpy(abDirRecCopy, pDirRec, pDirRec->cbDirRec);
1497
1498 /* Process extent records. */
1499 uint32_t cDirRecs = 1;
1500 uint32_t offNextBlock = ISO9660_GET_ENDIAN(&pDirRec->offExtent)
1501 + ISO9660_GET_ENDIAN(&pDirRec->cbData) / ISO9660_SECTOR_SIZE;
1502 while ( cbChunkNew > 0
1503 || cbDir > 0)
1504 {
1505 /* Read more? Skip? */
1506 if ( cbChunkNew <= UINT8_MAX
1507 && cbDir != 0)
1508 {
1509 rc = rtFsIsoImportProcessIso9660TreeWorkerReadMore(pThis, &pDirRecNext, &cbChunkNew, &cbDir, &offNext);
1510 if (RT_FAILURE(rc))
1511 return rc;
1512 }
1513 if (pDirRecNext->cbDirRec == 0)
1514 {
1515 rc = rtFsIsoImportProcessIso9660TreeWorkerHandleZeroSizedDirRec(pThis, &pDirRecNext, &cbChunkNew,
1516 &cbDir, &offNext);
1517 if (RT_FAILURE(rc))
1518 {
1519 if (rc == VERR_NO_MORE_FILES)
1520 break;
1521 return rc;
1522 }
1523 if (pDirRecNext->cbDirRec == 0)
1524 continue;
1525 }
1526
1527 /* Check the next record. */
1528 rc = rtFsIsoImportValidateDirRec(pThis, pDirRecNext, cbChunkNew);
1529 if (RT_FAILURE(rc))
1530 return rc;
1531 if (pDirRecNext->bFileIdLength != pDirRec->bFileIdLength)
1532 return rtFsIsoImpError(pThis, VERR_ISOMK_IMPORT_MISMATCHING_MULTI_EXTENT_REC,
1533 "Multi-extent record #%#x differs from the first: bFileIdLength is %#x, expected %#x",
1534 cDirRecs, pDirRecNext->bFileIdLength, pDirRec->bFileIdLength);
1535 if (memcmp(pDirRecNext->achFileId, pDirRec->achFileId, pDirRec->bFileIdLength) != 0)
1536 return rtFsIsoImpError(pThis, VERR_ISOMK_IMPORT_MISMATCHING_MULTI_EXTENT_REC,
1537 "Multi-extent record #%#x differs from the first: achFileId is %.*Rhxs, expected %.*Rhxs",
1538 cDirRecs, pDirRecNext->bFileIdLength, pDirRecNext->achFileId,
1539 pDirRec->bFileIdLength, pDirRec->achFileId);
1540 if (ISO9660_GET_ENDIAN(&pDirRecNext->VolumeSeqNo) != ISO9660_GET_ENDIAN(&pDirRec->VolumeSeqNo))
1541 return rtFsIsoImpError(pThis, VERR_ISOMK_IMPORT_MISMATCHING_MULTI_EXTENT_REC,
1542 "Multi-extent record #%#x differs from the first: VolumeSeqNo is %#x, expected %#x",
1543 cDirRecs, ISO9660_GET_ENDIAN(&pDirRecNext->VolumeSeqNo),
1544 ISO9660_GET_ENDIAN(&pDirRec->VolumeSeqNo));
1545 if ( (pDirRecNext->fFileFlags & ISO9660_FILE_FLAGS_MULTI_EXTENT)
1546 && (ISO9660_GET_ENDIAN(&pDirRecNext->cbData) & (ISO9660_SECTOR_SIZE - 1)) )
1547 return rtFsIsoImpError(pThis, VERR_ISOMK_IMPORT_MISALIGNED_MULTI_EXTENT,
1548 "The size of non-final multi-extent record #%#x isn't block aligned: %#RX32",
1549 cDirRecs, ISO9660_GET_ENDIAN(&pDirRecNext->cbData));
1550
1551 /* Check that the data is contiguous, then add the data. */
1552 if (ISO9660_GET_ENDIAN(&pDirRecNext->offExtent) == offNextBlock)
1553 cbData += ISO9660_GET_ENDIAN(&pDirRecNext->cbData);
1554 else
1555 return rtFsIsoImpError(pThis, VERR_ISOMK_IMPORT_NON_CONTIGUOUS_MULTI_EXTENT,
1556 "Multi-extent record #%#x isn't contiguous: offExtent=%#RX32, expected %#RX32",
1557 cDirRecs, ISO9660_GET_ENDIAN(&pDirRecNext->offExtent), offNextBlock);
1558
1559 /* Advance. */
1560 cDirRecs++;
1561 bool fDone = !(pDirRecNext->fFileFlags & ISO9660_FILE_FLAGS_MULTI_EXTENT);
1562 offNext += ISO9660_GET_ENDIAN(&pDirRecNext->cbData) / ISO9660_SECTOR_SIZE;
1563 cbChunkNew -= pDirRecNext->cbDirRec;
1564 pDirRecNext = (PCISO9660DIRREC)((uintptr_t)pDirRecNext + pDirRecNext->cbDirRec);
1565 if (fDone)
1566 break;
1567 }
1568 }
1569 if (RT_SUCCESS(rc))
1570 {
1571 /*
1572 * Add the object.
1573 */
1574 if (pDirRec->fFileFlags & ISO9660_FILE_FLAGS_DIRECTORY)
1575 rtFsIsoImportProcessIso9660AddAndNameDirectory(pThis, pDirRec, &ObjInfo, cbData, fNamespace, idxDir,
1576 pThis->szNameBuf, pThis->szRockNameBuf, cDepth + 1, pTodoList);
1577 else if (pThis->szRockSymlinkTargetBuf[0] == '\0')
1578 {
1579 if (strcmp(pThis->szNameBuf, pThis->pszTransTbl) != 0)
1580 rtFsIsoImportProcessIso9660AddAndNameFile(pThis, pDirRec, &ObjInfo, cbData, fNamespace, idxDir,
1581 pThis->szNameBuf, pThis->szRockNameBuf);
1582 }
1583 else
1584 rtFsIsoImportProcessIso9660AddAndNameSymlink(pThis, pDirRec, &ObjInfo, fNamespace, idxDir, pThis->szNameBuf,
1585 pThis->szRockNameBuf, pThis->szRockSymlinkTargetBuf);
1586 }
1587 }
1588 else
1589 rtFsIsoImpError(pThis, rc, "Invalid name at %#RX64: %.Rhxs",
1590 offNext - cbChunk, pDirRec->bFileIdLength, pDirRec->achFileId);
1591
1592 /*
1593 * Advance to the next directory record.
1594 */
1595 cbChunk = cbChunkNew;
1596 pDirRec = pDirRecNext;
1597 }
1598
1599 return VINF_SUCCESS;
1600}
1601
1602
1603/**
1604 * Deals with a directory tree.
1605 *
1606 * This is implemented by tracking directories that needs to be processed in a
1607 * todo list, so no recursive calls, however it uses a bit of heap.
1608 *
1609 * @returns IPRT status code (safe to ignore, see pThis->rc).
1610 * @param pThis The importer instance.
1611 * @param offDirBlock The offset of the root directory data.
1612 * @param cbDir The size of the root directory data.
1613 * @param fUnicode Set if it's a unicode (UTF-16BE) encoded
1614 * directory.
1615 */
1616static int rtFsIsoImportProcessIso9660Tree(PRTFSISOMKIMPORTER pThis, uint32_t offDirBlock, uint32_t cbDir, bool fUnicode)
1617{
1618 /*
1619 * Reset some parsing state.
1620 */
1621 pThis->offSuspSkip = 0;
1622 pThis->fSuspSeenSP = false;
1623 pThis->pszTransTbl = "TRANS.TBL"; /** @todo query this from the iso maker! */
1624
1625 /*
1626 * Make sure we've got a root in the namespace.
1627 */
1628 uint32_t idxDir = RTFsIsoMakerGetObjIdxForPath(pThis->hIsoMaker,
1629 !fUnicode ? RTFSISOMAKER_NAMESPACE_ISO_9660 : RTFSISOMAKER_NAMESPACE_JOLIET,
1630 "/");
1631 if (idxDir == UINT32_MAX)
1632 {
1633 idxDir = RTFSISOMAKER_CFG_IDX_ROOT;
1634 int rc = RTFsIsoMakerObjSetPath(pThis->hIsoMaker, RTFSISOMAKER_CFG_IDX_ROOT,
1635 !fUnicode ? RTFSISOMAKER_NAMESPACE_ISO_9660 : RTFSISOMAKER_NAMESPACE_JOLIET, "/");
1636 if (RT_FAILURE(rc))
1637 return rtFsIsoImpError(pThis, rc, "RTFsIsoMakerObjSetPath failed on root dir: %Rrc", rc);
1638 }
1639 Assert(idxDir == RTFSISOMAKER_CFG_IDX_ROOT);
1640
1641 /*
1642 * Directories.
1643 */
1644 int rc = VINF_SUCCESS;
1645 uint8_t cDepth = 0;
1646 RTLISTANCHOR TodoList;
1647 RTListInit(&TodoList);
1648 for (;;)
1649 {
1650 int rc2 = rtFsIsoImportProcessIso9660TreeWorker(pThis, idxDir, offDirBlock, cbDir, cDepth, fUnicode, &TodoList);
1651 if (RT_FAILURE(rc2) && RT_SUCCESS(rc))
1652 rc = rc2;
1653
1654 /*
1655 * Pop the next directory.
1656 */
1657 PRTFSISOMKIMPDIR pNext = RTListRemoveLast(&TodoList, RTFSISOMKIMPDIR, Entry);
1658 if (!pNext)
1659 break;
1660 idxDir = pNext->idxObj;
1661 offDirBlock = pNext->offDirBlock;
1662 cbDir = pNext->cbDir;
1663 cDepth = pNext->cDepth;
1664 RTMemFree(pNext);
1665 }
1666
1667 return rc;
1668}
1669
1670
1671/**
1672 * Imports a UTF-16BE string property from the joliet volume descriptor.
1673 *
1674 * The fields are normally space filled and padded, but we also consider zero
1675 * bytes are fillers. If the field only contains padding, the string property
1676 * will remain unchanged.
1677 *
1678 * @returns IPRT status code (ignorable).
1679 * @param pThis The importer instance.
1680 * @param pachField Pointer to the field. The structure type
1681 * is 'char' for hysterical raisins, while the
1682 * real type is 'RTUTF16'.
1683 * @param cchField The field length.
1684 * @param enmStringProp The corresponding string property.
1685 *
1686 * @note Clobbers pThis->pbBuf!
1687 */
1688static int rtFsIsoImportUtf16BigStringField(PRTFSISOMKIMPORTER pThis, const char *pachField, size_t cchField,
1689 RTFSISOMAKERSTRINGPROP enmStringProp)
1690{
1691 /*
1692 * Scan the field from the end as this way we know the result length if we find anything.
1693 */
1694 PCRTUTF16 pwcField = (PCRTUTF16)pachField;
1695 size_t cwcField = cchField / sizeof(RTUTF16); /* ignores any odd field byte */
1696 size_t off = cwcField;
1697 while (off-- > 0)
1698 {
1699 RTUTF16 wc = RT_BE2H_U16(pwcField[off]);
1700 if (wc == ' ' || wc == '\0')
1701 { /* likely */ }
1702 else
1703 {
1704 /*
1705 * Convert to UTF-16.
1706 */
1707 char *pszCopy = (char *)pThis->abBuf;
1708 int rc = RTUtf16BigToUtf8Ex(pwcField, off + 1, &pszCopy, sizeof(pThis->abBuf), NULL);
1709 if (RT_SUCCESS(rc))
1710 {
1711 rc = RTFsIsoMakerSetStringProp(pThis->hIsoMaker, enmStringProp, RTFSISOMAKER_NAMESPACE_JOLIET, pszCopy);
1712 if (RT_SUCCESS(rc))
1713 return VINF_SUCCESS;
1714 return rtFsIsoImpError(pThis, rc, "RTFsIsoMakerSetStringProp failed setting field %d to '%s': %Rrc",
1715 enmStringProp, pszCopy, rc);
1716 }
1717 return rtFsIsoImpError(pThis, rc, "RTUtf16BigToUtf8Ex failed converting field %d to UTF-8: %Rrc - %.*Rhxs",
1718 enmStringProp, rc, off * sizeof(RTUTF16), pwcField);
1719 }
1720 }
1721 return VINF_SUCCESS;
1722}
1723
1724
1725/**
1726 * Imports a string property from the primary volume descriptor.
1727 *
1728 * The fields are normally space filled and padded, but we also consider zero
1729 * bytes are fillers. If the field only contains padding, the string property
1730 * will remain unchanged.
1731 *
1732 * @returns IPRT status code (ignorable).
1733 * @param pThis The importer instance.
1734 * @param pachField Pointer to the field.
1735 * @param cchField The field length.
1736 * @param enmStringProp The corresponding string property.
1737 *
1738 * @note Clobbers pThis->pbBuf!
1739 */
1740static int rtFsIsoImportAsciiStringField(PRTFSISOMKIMPORTER pThis, const char *pachField, size_t cchField,
1741 RTFSISOMAKERSTRINGPROP enmStringProp)
1742{
1743 /*
1744 * Scan the field from the end as this way we know the result length if we find anything.
1745 */
1746 size_t off = cchField;
1747 while (off-- > 0)
1748 {
1749 char ch = pachField[off];
1750 if (ch == ' ' || ch == '\0')
1751 { /* likely */ }
1752 else
1753 {
1754 /*
1755 * Make a copy of the string in abBuf, purge the encoding.
1756 */
1757 off++;
1758 char *pszCopy = (char *)pThis->abBuf;
1759 memcpy(pszCopy, pachField, off);
1760 pszCopy[off] = '\0';
1761 RTStrPurgeEncoding(pszCopy);
1762
1763 int rc = RTFsIsoMakerSetStringProp(pThis->hIsoMaker, enmStringProp, RTFSISOMAKER_NAMESPACE_ISO_9660, pszCopy);
1764 if (RT_SUCCESS(rc))
1765 return VINF_SUCCESS;
1766 return rtFsIsoImpError(pThis, rc, "RTFsIsoMakerSetStringProp failed setting field %d to '%s': %Rrc",
1767 enmStringProp, pszCopy, rc);
1768 }
1769 }
1770 return VINF_SUCCESS;
1771}
1772
1773
1774/**
1775 * Validates a root directory record.
1776 *
1777 * @returns IPRT status code (safe to ignore, see pThis->rc).
1778 * @param pThis The importer instance.
1779 * @param pDirRec The root directory record to validate.
1780 */
1781static int rtFsIsoImportValidateRootDirRec(PRTFSISOMKIMPORTER pThis, PCISO9660DIRREC pDirRec)
1782{
1783 /*
1784 * Validate dual fields.
1785 */
1786 if (RT_LE2H_U32(pDirRec->cbData.le) != RT_BE2H_U32(pDirRec->cbData.be))
1787 return rtFsIsoImpError(pThis, VERR_ISOMK_IMPORT_BAD_ROOT_DIR_REC,
1788 "Invalid root dir size: {%#RX32,%#RX32}",
1789 RT_BE2H_U32(pDirRec->cbData.be), RT_LE2H_U32(pDirRec->cbData.le));
1790
1791 if (RT_LE2H_U32(pDirRec->offExtent.le) != RT_BE2H_U32(pDirRec->offExtent.be))
1792 return rtFsIsoImpError(pThis, VERR_ISOMK_IMPORT_BAD_ROOT_DIR_REC,
1793 "Invalid root dir extent: {%#RX32,%#RX32}",
1794 RT_BE2H_U32(pDirRec->offExtent.be), RT_LE2H_U32(pDirRec->offExtent.le));
1795
1796 if (RT_LE2H_U16(pDirRec->VolumeSeqNo.le) != RT_BE2H_U16(pDirRec->VolumeSeqNo.be))
1797 return rtFsIsoImpError(pThis, VERR_ISOMK_IMPORT_BAD_ROOT_DIR_REC,
1798 "Invalid root dir volume sequence ID: {%#RX16,%#RX16}",
1799 RT_BE2H_U16(pDirRec->VolumeSeqNo.be), RT_LE2H_U16(pDirRec->VolumeSeqNo.le));
1800
1801 /*
1802 * Check values.
1803 */
1804 if (ISO9660_GET_ENDIAN(&pDirRec->VolumeSeqNo) != pThis->idPrimaryVol)
1805 return rtFsIsoImpError(pThis, VERR_ISOMK_IMPORT_ROOT_VOLUME_SEQ_NO,
1806 "Expected root dir to have same volume sequence number as primary volume: %#x, expected %#x",
1807 ISO9660_GET_ENDIAN(&pDirRec->VolumeSeqNo), pThis->idPrimaryVol);
1808
1809 if (ISO9660_GET_ENDIAN(&pDirRec->cbData) == 0)
1810 return RTErrInfoSet(pThis->pErrInfo, VERR_ISOMK_IMPORT_ZERO_SIZED_ROOT_DIR, "Zero sized root dir");
1811
1812 if (ISO9660_GET_ENDIAN(&pDirRec->offExtent) >= pThis->cBlocksInPrimaryVolumeSpace)
1813 return rtFsIsoImpError(pThis, VERR_ISOMK_IMPORT_ROOT_DIR_EXTENT_OUT_OF_BOUNDS,
1814 "Invalid root dir extent: %#RX32, max %#RX32",
1815 ISO9660_GET_ENDIAN(&pDirRec->offExtent), pThis->cBlocksInPrimaryVolumeSpace);
1816
1817 if (pDirRec->cbDirRec < RT_OFFSETOF(ISO9660DIRREC, achFileId))
1818 return rtFsIsoImpError(pThis, VERR_ISOMK_IMPORT_BAD_ROOT_DIR_REC_LENGTH,
1819 "Root dir record size is too small: %#x (min %#x)",
1820 pDirRec->cbDirRec, RT_OFFSETOF(ISO9660DIRREC, achFileId));
1821
1822 if (!(pDirRec->fFileFlags & ISO9660_FILE_FLAGS_DIRECTORY))
1823 return rtFsIsoImpError(pThis, VERR_ISOMK_IMPORT_ROOT_DIR_WITHOUT_DIR_FLAG,
1824 "Root dir is not flagged as directory: %#x", pDirRec->fFileFlags);
1825 if (pDirRec->fFileFlags & ISO9660_FILE_FLAGS_MULTI_EXTENT)
1826 return rtFsIsoImpError(pThis, VERR_ISOMK_IMPORT_ROOT_DIR_IS_MULTI_EXTENT,
1827 "Root dir is cannot be multi-extent: %#x", pDirRec->fFileFlags);
1828
1829 return VINF_SUCCESS;
1830}
1831
1832
1833/**
1834 * Processes a primary volume descriptor, importing all files and stuff.
1835 *
1836 * @returns IPRT status code (safe to ignore, see pThis->rc).
1837 * @param pThis The importer instance.
1838 * @param pVolDesc The primary volume descriptor.
1839 */
1840static int rtFsIsoImportProcessPrimaryDesc(PRTFSISOMKIMPORTER pThis, PISO9660PRIMARYVOLDESC pVolDesc)
1841{
1842 /*
1843 * Validate dual fields first.
1844 */
1845 if (pVolDesc->bFileStructureVersion != ISO9660_FILE_STRUCTURE_VERSION)
1846 return rtFsIsoImpError(pThis, VERR_IOSMK_IMPORT_PRIMARY_VOL_DESC_VER,
1847 "Unsupported file structure version: %#x", pVolDesc->bFileStructureVersion);
1848
1849 if (RT_LE2H_U16(pVolDesc->cbLogicalBlock.le) != RT_BE2H_U16(pVolDesc->cbLogicalBlock.be))
1850 return rtFsIsoImpError(pThis, VERR_ISOMK_IMPORT_BAD_PRIMARY_VOL_DESC,
1851 "Mismatching logical block size: {%#RX16,%#RX16}",
1852 RT_BE2H_U16(pVolDesc->cbLogicalBlock.be), RT_LE2H_U16(pVolDesc->cbLogicalBlock.le));
1853 if (RT_LE2H_U32(pVolDesc->VolumeSpaceSize.le) != RT_BE2H_U32(pVolDesc->VolumeSpaceSize.be))
1854 return rtFsIsoImpError(pThis, VERR_ISOMK_IMPORT_BAD_PRIMARY_VOL_DESC,
1855 "Mismatching volume space size: {%#RX32,%#RX32}",
1856 RT_BE2H_U32(pVolDesc->VolumeSpaceSize.be), RT_LE2H_U32(pVolDesc->VolumeSpaceSize.le));
1857 if (RT_LE2H_U16(pVolDesc->cVolumesInSet.le) != RT_BE2H_U16(pVolDesc->cVolumesInSet.be))
1858 return rtFsIsoImpError(pThis, VERR_ISOMK_IMPORT_BAD_PRIMARY_VOL_DESC,
1859 "Mismatching volumes in set: {%#RX16,%#RX16}",
1860 RT_BE2H_U16(pVolDesc->cVolumesInSet.be), RT_LE2H_U16(pVolDesc->cVolumesInSet.le));
1861 if (RT_LE2H_U16(pVolDesc->VolumeSeqNo.le) != RT_BE2H_U16(pVolDesc->VolumeSeqNo.be))
1862 return rtFsIsoImpError(pThis, VERR_ISOMK_IMPORT_BAD_PRIMARY_VOL_DESC,
1863 "Mismatching volume sequence no.: {%#RX16,%#RX16}",
1864 RT_BE2H_U16(pVolDesc->VolumeSeqNo.be), RT_LE2H_U16(pVolDesc->VolumeSeqNo.le));
1865 if (RT_LE2H_U32(pVolDesc->cbPathTable.le) != RT_BE2H_U32(pVolDesc->cbPathTable.be))
1866 return rtFsIsoImpError(pThis, VERR_ISOMK_IMPORT_BAD_PRIMARY_VOL_DESC,
1867 "Mismatching path table size: {%#RX32,%#RX32}",
1868 RT_BE2H_U32(pVolDesc->cbPathTable.be), RT_LE2H_U32(pVolDesc->cbPathTable.le));
1869
1870 /*
1871 * Validate field values against our expectations.
1872 */
1873 if (ISO9660_GET_ENDIAN(&pVolDesc->cbLogicalBlock) != ISO9660_SECTOR_SIZE)
1874 return rtFsIsoImpError(pThis, VERR_ISOMK_IMPORT_LOGICAL_BLOCK_SIZE_NOT_2KB,
1875 "Unsupported block size: %#x", ISO9660_GET_ENDIAN(&pVolDesc->cbLogicalBlock));
1876
1877 if (ISO9660_GET_ENDIAN(&pVolDesc->cVolumesInSet) != 1)
1878 return rtFsIsoImpError(pThis, VERR_ISOMK_IMPORT_MORE_THAN_ONE_VOLUME_IN_SET,
1879 "Volumes in set: %#x", ISO9660_GET_ENDIAN(&pVolDesc->cVolumesInSet));
1880
1881 if (ISO9660_GET_ENDIAN(&pVolDesc->VolumeSeqNo) != 1)
1882 return rtFsIsoImpError(pThis, VERR_ISOMK_IMPORT_INVALID_VOLUMNE_SEQ_NO,
1883 "Unexpected volume sequence number: %#x", ISO9660_GET_ENDIAN(&pVolDesc->VolumeSeqNo));
1884
1885 /*
1886 * Gather info we need.
1887 */
1888 pThis->cBlocksInPrimaryVolumeSpace = ISO9660_GET_ENDIAN(&pVolDesc->VolumeSpaceSize);
1889 pThis->cbPrimaryVolumeSpace = pThis->cBlocksInPrimaryVolumeSpace * (uint64_t)ISO9660_SECTOR_SIZE;
1890 pThis->cVolumesInSet = ISO9660_GET_ENDIAN(&pVolDesc->cVolumesInSet);
1891 pThis->idPrimaryVol = ISO9660_GET_ENDIAN(&pVolDesc->VolumeSeqNo);
1892
1893 /*
1894 * Validate the root directory record.
1895 */
1896 int rc = rtFsIsoImportValidateRootDirRec(pThis, &pVolDesc->RootDir.DirRec);
1897 if (RT_SUCCESS(rc))
1898 {
1899 /*
1900 * Import stuff if present and not opted out.
1901 */
1902 if (!(pThis->fFlags & RTFSISOMK_IMPORT_F_NO_SYSTEM_ID))
1903 rtFsIsoImportAsciiStringField(pThis, pVolDesc->achSystemId, sizeof(pVolDesc->achSystemId),
1904 RTFSISOMAKERSTRINGPROP_SYSTEM_ID);
1905 if (!(pThis->fFlags & RTFSISOMK_IMPORT_F_NO_VOLUME_ID))
1906 rtFsIsoImportAsciiStringField(pThis, pVolDesc->achVolumeId, sizeof(pVolDesc->achVolumeId),
1907 RTFSISOMAKERSTRINGPROP_VOLUME_ID);
1908 if (!(pThis->fFlags & RTFSISOMK_IMPORT_F_NO_VOLUME_SET_ID))
1909 rtFsIsoImportAsciiStringField(pThis, pVolDesc->achVolumeSetId, sizeof(pVolDesc->achVolumeSetId),
1910 RTFSISOMAKERSTRINGPROP_VOLUME_SET_ID);
1911 if (!(pThis->fFlags & RTFSISOMK_IMPORT_F_NO_PUBLISHER_ID))
1912 rtFsIsoImportAsciiStringField(pThis, pVolDesc->achPublisherId, sizeof(pVolDesc->achPublisherId),
1913 RTFSISOMAKERSTRINGPROP_PUBLISHER_ID);
1914 if (pThis->fFlags & RTFSISOMK_IMPORT_F_DATA_PREPARER_ID)
1915 rtFsIsoImportAsciiStringField(pThis, pVolDesc->achDataPreparerId, sizeof(pVolDesc->achDataPreparerId),
1916 RTFSISOMAKERSTRINGPROP_DATA_PREPARER_ID);
1917 if (pThis->fFlags & RTFSISOMK_IMPORT_F_APPLICATION_ID)
1918 rtFsIsoImportAsciiStringField(pThis, pVolDesc->achApplicationId, sizeof(pVolDesc->achApplicationId),
1919 RTFSISOMAKERSTRINGPROP_APPLICATION_ID);
1920 if (!(pThis->fFlags & RTFSISOMK_IMPORT_F_NO_COPYRIGHT_FID))
1921 rtFsIsoImportAsciiStringField(pThis, pVolDesc->achCopyrightFileId, sizeof(pVolDesc->achCopyrightFileId),
1922 RTFSISOMAKERSTRINGPROP_COPYRIGHT_FILE_ID);
1923 if (!(pThis->fFlags & RTFSISOMK_IMPORT_F_NO_ABSTRACT_FID))
1924 rtFsIsoImportAsciiStringField(pThis, pVolDesc->achAbstractFileId, sizeof(pVolDesc->achAbstractFileId),
1925 RTFSISOMAKERSTRINGPROP_ABSTRACT_FILE_ID);
1926 if (!(pThis->fFlags & RTFSISOMK_IMPORT_F_NO_BIBLIO_FID))
1927 rtFsIsoImportAsciiStringField(pThis, pVolDesc->achBibliographicFileId, sizeof(pVolDesc->achBibliographicFileId),
1928 RTFSISOMAKERSTRINGPROP_BIBLIOGRAPHIC_FILE_ID);
1929
1930 /*
1931 * Process the directory tree.
1932 */
1933 if (!(pThis->fFlags & RTFSISOMK_IMPORT_F_NO_PRIMARY_ISO))
1934 rc = rtFsIsoImportProcessIso9660Tree(pThis, ISO9660_GET_ENDIAN(&pVolDesc->RootDir.DirRec.offExtent),
1935 ISO9660_GET_ENDIAN(&pVolDesc->RootDir.DirRec.cbData), false /*fUnicode*/);
1936 }
1937
1938 return rc;
1939}
1940
1941
1942/**
1943 * Processes a secondary volume descriptor, if it is joliet we'll importing all
1944 * the files and stuff.
1945 *
1946 * @returns IPRT status code (safe to ignore, see pThis->rc).
1947 * @param pThis The importer instance.
1948 * @param pVolDesc The primary volume descriptor.
1949 */
1950static int rtFsIsoImportProcessSupplementaryDesc(PRTFSISOMKIMPORTER pThis, PISO9660SUPVOLDESC pVolDesc)
1951{
1952 /*
1953 * Validate dual fields first.
1954 */
1955 if (pVolDesc->bFileStructureVersion != ISO9660_FILE_STRUCTURE_VERSION)
1956 return rtFsIsoImpError(pThis, VERR_IOSMK_IMPORT_SUP_VOL_DESC_VER,
1957 "Unsupported file structure version: %#x", pVolDesc->bFileStructureVersion);
1958
1959 if (RT_LE2H_U16(pVolDesc->cbLogicalBlock.le) != RT_BE2H_U16(pVolDesc->cbLogicalBlock.be))
1960 return rtFsIsoImpError(pThis, VERR_ISOMK_IMPORT_BAD_SUP_VOL_DESC,
1961 "Mismatching logical block size: {%#RX16,%#RX16}",
1962 RT_BE2H_U16(pVolDesc->cbLogicalBlock.be), RT_LE2H_U16(pVolDesc->cbLogicalBlock.le));
1963 if (RT_LE2H_U32(pVolDesc->VolumeSpaceSize.le) != RT_BE2H_U32(pVolDesc->VolumeSpaceSize.be))
1964 return rtFsIsoImpError(pThis, VERR_ISOMK_IMPORT_BAD_SUP_VOL_DESC,
1965 "Mismatching volume space size: {%#RX32,%#RX32}",
1966 RT_BE2H_U32(pVolDesc->VolumeSpaceSize.be), RT_LE2H_U32(pVolDesc->VolumeSpaceSize.le));
1967 if (RT_LE2H_U16(pVolDesc->cVolumesInSet.le) != RT_BE2H_U16(pVolDesc->cVolumesInSet.be))
1968 return rtFsIsoImpError(pThis, VERR_ISOMK_IMPORT_BAD_SUP_VOL_DESC,
1969 "Mismatching volumes in set: {%#RX16,%#RX16}",
1970 RT_BE2H_U16(pVolDesc->cVolumesInSet.be), RT_LE2H_U16(pVolDesc->cVolumesInSet.le));
1971 if (RT_LE2H_U16(pVolDesc->VolumeSeqNo.le) != RT_BE2H_U16(pVolDesc->VolumeSeqNo.be))
1972 return rtFsIsoImpError(pThis, VERR_ISOMK_IMPORT_BAD_SUP_VOL_DESC,
1973 "Mismatching volume sequence no.: {%#RX16,%#RX16}",
1974 RT_BE2H_U16(pVolDesc->VolumeSeqNo.be), RT_LE2H_U16(pVolDesc->VolumeSeqNo.le));
1975 if (RT_LE2H_U32(pVolDesc->cbPathTable.le) != RT_BE2H_U32(pVolDesc->cbPathTable.be))
1976 return rtFsIsoImpError(pThis, VERR_ISOMK_IMPORT_BAD_SUP_VOL_DESC,
1977 "Mismatching path table size: {%#RX32,%#RX32}",
1978 RT_BE2H_U32(pVolDesc->cbPathTable.be), RT_LE2H_U32(pVolDesc->cbPathTable.le));
1979
1980 /*
1981 * Validate field values against our expectations.
1982 */
1983 if (ISO9660_GET_ENDIAN(&pVolDesc->cbLogicalBlock) != ISO9660_SECTOR_SIZE)
1984 return rtFsIsoImpError(pThis, VERR_ISOMK_IMPORT_LOGICAL_BLOCK_SIZE_NOT_2KB,
1985 "Unsupported block size: %#x", ISO9660_GET_ENDIAN(&pVolDesc->cbLogicalBlock));
1986
1987 if (ISO9660_GET_ENDIAN(&pVolDesc->cVolumesInSet) != pThis->cVolumesInSet)
1988 return rtFsIsoImpError(pThis, VERR_ISOMK_IMPORT_VOLUME_IN_SET_MISMATCH, "Volumes in set: %#x, expected %#x",
1989 ISO9660_GET_ENDIAN(&pVolDesc->cVolumesInSet), pThis->cVolumesInSet);
1990
1991 if (ISO9660_GET_ENDIAN(&pVolDesc->VolumeSeqNo) != pThis->idPrimaryVol)
1992 return rtFsIsoImpError(pThis, VERR_ISOMK_IMPORT_INVALID_VOLUMNE_SEQ_NO,
1993 "Unexpected volume sequence number: %#x (expected %#x)",
1994 ISO9660_GET_ENDIAN(&pVolDesc->VolumeSeqNo), pThis->idPrimaryVol);
1995
1996 if (ISO9660_GET_ENDIAN(&pVolDesc->VolumeSpaceSize) != pThis->cBlocksInPrimaryVolumeSpace)
1997 return rtFsIsoImpError(pThis, VERR_ISOMK_IMPORT_INVALID_VOLUMNE_SEQ_NO,
1998 "Volume space size differs between primary and supplementary descriptors: %#x, primary %#x",
1999 ISO9660_GET_ENDIAN(&pVolDesc->VolumeSpaceSize), pThis->cBlocksInPrimaryVolumeSpace);
2000
2001 /*
2002 * Validate the root directory record.
2003 */
2004 int rc = rtFsIsoImportValidateRootDirRec(pThis, &pVolDesc->RootDir.DirRec);
2005 if (RT_FAILURE(rc))
2006 return rc;
2007
2008 /*
2009 * Is this a joliet descriptor? Ignore if not.
2010 */
2011 uint8_t uJolietLevel = 0;
2012 if ( pVolDesc->abEscapeSequences[0] == ISO9660_JOLIET_ESC_SEQ_0
2013 && pVolDesc->abEscapeSequences[1] == ISO9660_JOLIET_ESC_SEQ_1)
2014 switch (pVolDesc->abEscapeSequences[2])
2015 {
2016 case ISO9660_JOLIET_ESC_SEQ_2_LEVEL_1: uJolietLevel = 1; break;
2017 case ISO9660_JOLIET_ESC_SEQ_2_LEVEL_2: uJolietLevel = 2; break;
2018 case ISO9660_JOLIET_ESC_SEQ_2_LEVEL_3: uJolietLevel = 3; break;
2019 default: Log(("rtFsIsoImportProcessSupplementaryDesc: last joliet escape sequence byte doesn't match: %#x\n",
2020 pVolDesc->abEscapeSequences[2]));
2021 }
2022 if (uJolietLevel == 0)
2023 return VINF_SUCCESS;
2024
2025 /*
2026 * Only one joliet descriptor.
2027 */
2028 if (pThis->fSeenJoliet)
2029 return rtFsIsoImpError(pThis, VERR_ISOMK_IMPORT_MULTIPLE_JOLIET_VOL_DESCS,
2030 "More than one Joliet volume descriptor is not supported");
2031 pThis->fSeenJoliet = true;
2032
2033 /*
2034 * Import stuff if present and not opted out.
2035 */
2036 if (!(pThis->fFlags & RTFSISOMK_IMPORT_F_NO_SYSTEM_ID))
2037 rtFsIsoImportUtf16BigStringField(pThis, pVolDesc->achSystemId, sizeof(pVolDesc->achSystemId),
2038 RTFSISOMAKERSTRINGPROP_SYSTEM_ID);
2039 if (!(pThis->fFlags & RTFSISOMK_IMPORT_F_NO_J_VOLUME_ID))
2040 rtFsIsoImportUtf16BigStringField(pThis, pVolDesc->achVolumeId, sizeof(pVolDesc->achVolumeId),
2041 RTFSISOMAKERSTRINGPROP_VOLUME_ID);
2042 if (!(pThis->fFlags & RTFSISOMK_IMPORT_F_NO_J_VOLUME_SET_ID))
2043 rtFsIsoImportUtf16BigStringField(pThis, pVolDesc->achVolumeSetId, sizeof(pVolDesc->achVolumeSetId),
2044 RTFSISOMAKERSTRINGPROP_VOLUME_SET_ID);
2045 if (!(pThis->fFlags & RTFSISOMK_IMPORT_F_NO_J_PUBLISHER_ID))
2046 rtFsIsoImportUtf16BigStringField(pThis, pVolDesc->achPublisherId, sizeof(pVolDesc->achPublisherId),
2047 RTFSISOMAKERSTRINGPROP_PUBLISHER_ID);
2048 if (pThis->fFlags & RTFSISOMK_IMPORT_F_J_DATA_PREPARER_ID)
2049 rtFsIsoImportUtf16BigStringField(pThis, pVolDesc->achDataPreparerId, sizeof(pVolDesc->achDataPreparerId),
2050 RTFSISOMAKERSTRINGPROP_DATA_PREPARER_ID);
2051 if (pThis->fFlags & RTFSISOMK_IMPORT_F_J_APPLICATION_ID)
2052 rtFsIsoImportUtf16BigStringField(pThis, pVolDesc->achApplicationId, sizeof(pVolDesc->achApplicationId),
2053 RTFSISOMAKERSTRINGPROP_APPLICATION_ID);
2054 if (!(pThis->fFlags & RTFSISOMK_IMPORT_F_NO_J_COPYRIGHT_FID))
2055 rtFsIsoImportUtf16BigStringField(pThis, pVolDesc->achCopyrightFileId, sizeof(pVolDesc->achCopyrightFileId),
2056 RTFSISOMAKERSTRINGPROP_COPYRIGHT_FILE_ID);
2057 if (!(pThis->fFlags & RTFSISOMK_IMPORT_F_NO_J_ABSTRACT_FID))
2058 rtFsIsoImportUtf16BigStringField(pThis, pVolDesc->achAbstractFileId, sizeof(pVolDesc->achAbstractFileId),
2059 RTFSISOMAKERSTRINGPROP_ABSTRACT_FILE_ID);
2060 if (!(pThis->fFlags & RTFSISOMK_IMPORT_F_NO_J_BIBLIO_FID))
2061 rtFsIsoImportUtf16BigStringField(pThis, pVolDesc->achBibliographicFileId, sizeof(pVolDesc->achBibliographicFileId),
2062 RTFSISOMAKERSTRINGPROP_BIBLIOGRAPHIC_FILE_ID);
2063
2064 /*
2065 * Process the directory tree.
2066 */
2067 if (!(pThis->fFlags & RTFSISOMK_IMPORT_F_NO_JOLIET))
2068 return rtFsIsoImportProcessIso9660Tree(pThis, ISO9660_GET_ENDIAN(&pVolDesc->RootDir.DirRec.offExtent),
2069 ISO9660_GET_ENDIAN(&pVolDesc->RootDir.DirRec.cbData), true /*fUnicode*/);
2070 return VINF_SUCCESS;
2071}
2072
2073
2074/**
2075 * Checks out an El Torito boot image to see if it requires info table patching.
2076 *
2077 * @returns IPRT status code (ignored).
2078 * @param pThis The ISO importer instance.
2079 * @param idxImageObj The configuration index of the image.
2080 * @param offBootImage The block offset of the image.
2081 */
2082static int rtFsIsoImportProcessElToritoImage(PRTFSISOMKIMPORTER pThis, uint32_t idxImageObj, uint32_t offBootImage)
2083{
2084 ISO9660SYSLINUXINFOTABLE InfoTable;
2085 int rc = RTVfsFileReadAt(pThis->hSrcFile, offBootImage * (uint64_t)ISO9660_SECTOR_SIZE + ISO9660SYSLINUXINFOTABLE_OFFSET,
2086 &InfoTable, sizeof(InfoTable), NULL);
2087 if (RT_SUCCESS(rc))
2088 {
2089 if ( RT_LE2H_U32(InfoTable.offBootFile) == offBootImage
2090 && RT_LE2H_U32(InfoTable.offPrimaryVolDesc) == pThis->offPrimaryVolDesc
2091 && ASMMemIsAllU8(&InfoTable.auReserved[0], sizeof(InfoTable.auReserved), 0) )
2092 {
2093 rc = RTFsIsoMakerObjEnableBootInfoTablePatching(pThis->hIsoMaker, idxImageObj, true /*fEnable*/);
2094 if (RT_FAILURE(rc))
2095 return rtFsIsoImpError(pThis, rc, "RTFsIsoMakerObjEnableBootInfoTablePatching failed: %Rrc", rc);
2096 }
2097 }
2098 return VINF_SUCCESS;
2099}
2100
2101
2102/**
2103 * Processes a boot catalog default or section entry.
2104 *
2105 * @returns IPRT status code (ignored).
2106 * @param pThis The ISO importer instance.
2107 * @param iEntry The boot catalog entry number. This is 1 for
2108 * the default entry, and 3+ for section entries.
2109 * @param cMaxEntries Maximum number of entries.
2110 * @param pEntry The entry to process.
2111 * @param pcSkip Where to return the number of extension entries to skip.
2112 */
2113static int rtFsIsoImportProcessElToritoSectionEntry(PRTFSISOMKIMPORTER pThis, uint32_t iEntry, uint32_t cMaxEntries,
2114 PCISO9660ELTORITOSECTIONENTRY pEntry, uint32_t *pcSkip)
2115{
2116 *pcSkip = 0;
2117
2118 /*
2119 * Check the boot indicator type for entry 1.
2120 */
2121 if ( pEntry->bBootIndicator != ISO9660_ELTORITO_BOOT_INDICATOR_BOOTABLE
2122 && pEntry->bBootIndicator != ISO9660_ELTORITO_BOOT_INDICATOR_NOT_BOOTABLE)
2123 return rtFsIsoImpError(pThis, VERR_ISOMK_IMPORT_BOOT_CAT_DEF_ENTRY_INVALID_BOOT_IND,
2124 "Default boot catalog entry has an invalid boot indicator: %#x", pEntry->bBootIndicator);
2125
2126 /*
2127 * Check the media type and flags.
2128 */
2129 uint32_t cbDefaultSize;
2130 uint8_t bMediaType = pEntry->bBootMediaType;
2131 switch (bMediaType & ISO9660_ELTORITO_BOOT_MEDIA_TYPE_MASK)
2132 {
2133 case ISO9660_ELTORITO_BOOT_MEDIA_TYPE_FLOPPY_1_2_MB:
2134 cbDefaultSize = 512 * 80 * 15 * 2;
2135 break;
2136
2137 case ISO9660_ELTORITO_BOOT_MEDIA_TYPE_FLOPPY_1_44_MB:
2138 cbDefaultSize = 512 * 80 * 18 * 2;
2139 break;
2140
2141 case ISO9660_ELTORITO_BOOT_MEDIA_TYPE_FLOPPY_2_88_MB:
2142 cbDefaultSize = 512 * 80 * 36 * 2;
2143 break;
2144
2145 case ISO9660_ELTORITO_BOOT_MEDIA_TYPE_NO_EMULATION:
2146 case ISO9660_ELTORITO_BOOT_MEDIA_TYPE_HARD_DISK:
2147 cbDefaultSize = 0;
2148 break;
2149
2150 default:
2151 return rtFsIsoImpError(pThis, VERR_ISOMK_IMPORT_BOOT_CAT_INVALID_BOOT_MEDIA_TYPE,
2152 "Boot catalog entry #%#x has an invalid boot media type: %#x", bMediaType);
2153 }
2154
2155 if (iEntry == 1)
2156 {
2157 if (bMediaType & ISO9660_ELTORITO_BOOT_MEDIA_F_MASK)
2158 {
2159 rtFsIsoImpError(pThis, VERR_ISOMK_IMPORT_BOOT_CAT_DEF_ENTRY_INVALID_FLAGS,
2160 "Boot catalog entry #%#x has an invalid boot media type: %#x", bMediaType);
2161 bMediaType &= ~ISO9660_ELTORITO_BOOT_MEDIA_F_MASK;
2162 }
2163 }
2164 else
2165 {
2166 if (bMediaType & ISO9660_ELTORITO_BOOT_MEDIA_F_RESERVED)
2167 {
2168 rtFsIsoImpError(pThis, VERR_ISOMK_IMPORT_BOOT_CAT_ENTRY_RESERVED_FLAG,
2169 "Boot catalog entry #%#x has an invalid boot media type: %#x", bMediaType);
2170 bMediaType &= ~ISO9660_ELTORITO_BOOT_MEDIA_F_RESERVED;
2171 }
2172 }
2173
2174 /*
2175 * Complain if bUnused is used.
2176 */
2177 if (pEntry->bUnused != 0)
2178 rtFsIsoImpError(pThis, VERR_ISOMK_IMPORT_BOOT_CAT_ENTRY_USES_UNUSED_FIELD,
2179 "Boot catalog entry #%#x has a non-zero unused field: %#x", pEntry->bUnused);
2180
2181 /*
2182 * Check out the boot image offset and turn that into an index of a file
2183 */
2184 uint32_t offBootImage = RT_LE2H_U32(pEntry->offBootImage);
2185 if (offBootImage >= pThis->cBlocksInSrcFile)
2186 return rtFsIsoImpError(pThis, VERR_ISOMK_IMPORT_BOOT_CAT_ENTRY_IMAGE_OUT_OF_BOUNDS,
2187 "Boot catalog entry #%#x has an out of bound boot image block number: %#RX32, max %#RX32",
2188 offBootImage, pThis->cBlocksInPrimaryVolumeSpace);
2189
2190 int rc;
2191 uint32_t idxImageObj;
2192 PRTFSISOMKIMPBLOCK2FILE pBlock2File = (PRTFSISOMKIMPBLOCK2FILE)RTAvlU32Get(&pThis->Block2FileRoot, offBootImage);
2193 if (pBlock2File)
2194 idxImageObj = pBlock2File->idxObj;
2195 else
2196 {
2197 if (cbDefaultSize == 0)
2198 {
2199 pBlock2File = (PRTFSISOMKIMPBLOCK2FILE)RTAvlU32GetBestFit(&pThis->Block2FileRoot, offBootImage, true /*fAbove*/);
2200 if (pBlock2File)
2201 cbDefaultSize = RT_MIN(pBlock2File->Core.Key - offBootImage, UINT32_MAX / ISO9660_SECTOR_SIZE + 1)
2202 * ISO9660_SECTOR_SIZE;
2203 else if (offBootImage < pThis->cBlocksInSrcFile)
2204 cbDefaultSize = RT_MIN(pThis->cBlocksInSrcFile - offBootImage, UINT32_MAX / ISO9660_SECTOR_SIZE + 1)
2205 * ISO9660_SECTOR_SIZE;
2206 else
2207 return rtFsIsoImpError(pThis, VERR_ISOMK_IMPORT_BOOT_CAT_ENTRY_UNKNOWN_IMAGE_SIZE,
2208 "Boot catalog entry #%#x has an invalid boot media type: %#x", bMediaType);
2209 }
2210
2211 if (pThis->idxSrcFile != UINT32_MAX)
2212 {
2213 rc = RTFsIsoMakerAddCommonSourceFile(pThis->hIsoMaker, pThis->hSrcFile, &pThis->idxSrcFile);
2214 if (RT_FAILURE(rc))
2215 return rtFsIsoImpError(pThis, rc, "RTFsIsoMakerAddCommonSourceFile failed: %Rrc", rc);
2216 Assert(pThis->idxSrcFile != UINT32_MAX);
2217 }
2218
2219 rc = RTFsIsoMakerAddUnnamedFileWithCommonSrc(pThis->hIsoMaker, pThis->idxSrcFile,
2220 offBootImage * (uint64_t)ISO9660_SECTOR_SIZE,
2221 cbDefaultSize, NULL, &idxImageObj);
2222 if (RT_FAILURE(rc))
2223 return rtFsIsoImpError(pThis, rc, "RTFsIsoMakerAddUnnamedFileWithCommonSrc failed on boot entry #%#x: %Rrc",
2224 iEntry, rc);
2225 }
2226
2227 /*
2228 * Deal with selection criteria. Use the last sector of abBuf to gather it
2229 * into a single data chunk.
2230 */
2231 size_t cbSelCrit = 0;
2232 uint8_t *pbSelCrit = &pThis->abBuf[sizeof(pThis->abBuf) - ISO9660_SECTOR_SIZE];
2233 if (pEntry->bSelectionCriteriaType != ISO9660_ELTORITO_SEL_CRIT_TYPE_NONE)
2234 {
2235 memcpy(pbSelCrit, pEntry->abSelectionCriteria, sizeof(pEntry->abSelectionCriteria));
2236 cbSelCrit = sizeof(pEntry->abSelectionCriteria);
2237
2238 if ( (bMediaType & ISO9660_ELTORITO_BOOT_MEDIA_F_CONTINUATION)
2239 && iEntry + 1 < cMaxEntries)
2240 {
2241 uint32_t iExtEntry = iEntry + 1;
2242 PCISO9660ELTORITOSECTIONENTRYEXT pExtEntry = (PCISO9660ELTORITOSECTIONENTRYEXT)pEntry;
2243 for (;;)
2244 {
2245 pExtEntry++;
2246
2247 if (pExtEntry->bExtensionId != ISO9660_ELTORITO_SECTION_ENTRY_EXT_ID)
2248 {
2249 rtFsIsoImpError(pThis, VERR_ISOMK_IMPORT_BOOT_CAT_EXT_ENTRY_INVALID_ID,
2250 "Invalid header ID for extension entry #%#x: %#x", iExtEntry, pExtEntry->bExtensionId);
2251 break;
2252 }
2253 *pcSkip += 1;
2254
2255 memcpy(&pbSelCrit[cbSelCrit], pExtEntry->abSelectionCriteria, sizeof(pExtEntry->abSelectionCriteria));
2256 cbSelCrit += sizeof(pExtEntry->abSelectionCriteria);
2257
2258 if (pExtEntry->fFlags & ISO9660_ELTORITO_SECTION_ENTRY_EXT_F_UNUSED_MASK)
2259 rtFsIsoImpError(pThis, VERR_ISOMK_IMPORT_BOOT_CAT_EXT_ENTRY_UNDEFINED_FLAGS,
2260 "Boot catalog extension entry #%#x uses undefined flags: %#x", iExtEntry, pExtEntry->fFlags);
2261
2262 iExtEntry++;
2263 if (!(pExtEntry->fFlags & ISO9660_ELTORITO_SECTION_ENTRY_EXT_F_MORE))
2264 break;
2265 if (iExtEntry >= cMaxEntries)
2266 {
2267 rtFsIsoImpError(pThis, VERR_ISOMK_IMPORT_BOOT_CAT_EXT_ENTRY_END_OF_SECTOR,
2268 "Boot catalog extension entry #%#x sets the MORE flag, but we have reached the end of the boot catalog sector");
2269 break;
2270 }
2271 }
2272 Assert(*pcSkip = iExtEntry - iEntry);
2273 }
2274 else if (bMediaType & ISO9660_ELTORITO_BOOT_MEDIA_F_CONTINUATION)
2275 rtFsIsoImpError(pThis, VERR_ISOMK_IMPORT_BOOT_CAT_ENTRY_CONTINUATION_EOS,
2276 "Boot catalog extension entry #%#x sets the MORE flag, but we have reached the end of the boot catalog sector");
2277 }
2278 else if (bMediaType & ISO9660_ELTORITO_BOOT_MEDIA_F_CONTINUATION)
2279 rtFsIsoImpError(pThis, VERR_ISOMK_IMPORT_BOOT_CAT_ENTRY_CONTINUATION_WITH_NONE,
2280 "Boot catalog entry #%#x uses the continuation flag with selection criteria NONE", iEntry);
2281
2282 /*
2283 * Add the entry.
2284 */
2285 rc = RTFsIsoMakerBootCatSetSectionEntry(pThis->hIsoMaker, iEntry, idxImageObj, bMediaType, pEntry->bSystemType,
2286 pEntry->bBootIndicator == ISO9660_ELTORITO_BOOT_INDICATOR_BOOTABLE,
2287 pEntry->uLoadSeg, pEntry->cEmulatedSectorsToLoad,
2288 pEntry->bSelectionCriteriaType, pbSelCrit, cbSelCrit);
2289 if (RT_SUCCESS(rc))
2290 {
2291 pThis->pResults->cBootCatEntries += 1 + *pcSkip;
2292 rc = rtFsIsoImportProcessElToritoImage(pThis, idxImageObj, offBootImage);
2293 }
2294 else
2295 rtFsIsoImpError(pThis, rc, "RTFsIsoMakerBootCatSetSectionEntry failed for entry #%#x: %Rrc", iEntry, rc);
2296 return rc;
2297}
2298
2299
2300
2301/**
2302 * Processes a boot catalog section header entry.
2303 *
2304 * @returns IPRT status code (ignored).
2305 * @param pThis The ISO importer instance.
2306 * @param iEntry The boot catalog entry number.
2307 * @param pEntry The entry to process.
2308 */
2309static int rtFsIsoImportProcessElToritoSectionHeader(PRTFSISOMKIMPORTER pThis, uint32_t iEntry,
2310 PCISO9660ELTORITOSECTIONHEADER pEntry, char pszId[32])
2311{
2312 Assert(pEntry->bHeaderId == ISO9660_ELTORITO_HEADER_ID_SECTION_HEADER);
2313
2314 /* Deal with the string. ASSUME it doesn't contain zeros in non-terminal positions. */
2315 if (pEntry->achSectionId[0] == '\0')
2316 pszId = NULL;
2317 else
2318 {
2319 memcpy(pszId, pEntry->achSectionId, sizeof(pEntry->achSectionId));
2320 pszId[sizeof(pEntry->achSectionId)] = '\0';
2321 }
2322
2323 int rc = RTFsIsoMakerBootCatSetSectionHeaderEntry(pThis->hIsoMaker, iEntry, RT_LE2H_U16(pEntry->cEntries),
2324 pEntry->bPlatformId, pszId);
2325 if (RT_SUCCESS(rc))
2326 pThis->pResults->cBootCatEntries++;
2327 else
2328 rtFsIsoImpError(pThis, rc,
2329 "RTFsIsoMakerBootCatSetSectionHeaderEntry failed for entry #%#x (bPlatformId=%#x cEntries=%#x): %Rrc",
2330 iEntry, RT_LE2H_U16(pEntry->cEntries), pEntry->bPlatformId, rc);
2331 return rc;
2332}
2333
2334
2335/**
2336 * Processes a El Torito volume descriptor.
2337 *
2338 * @returns IPRT status code (ignorable).
2339 * @param pThis The ISO importer instance.
2340 * @param pVolDesc The volume descriptor to process.
2341 */
2342static int rtFsIsoImportProcessElToritoDesc(PRTFSISOMKIMPORTER pThis, PISO9660BOOTRECORDELTORITO pVolDesc)
2343{
2344 /*
2345 * Read the boot catalog into the abBuf.
2346 */
2347 uint32_t offBootCatalog = RT_LE2H_U32(pVolDesc->offBootCatalog);
2348 if (offBootCatalog >= pThis->cBlocksInPrimaryVolumeSpace)
2349 return rtFsIsoImpError(pThis, VERR_ISOMK_IMPORT_BOOT_CAT_BAD_OUT_OF_BOUNDS,
2350 "Boot catalog block number is out of bounds: %#RX32, max %#RX32",
2351 offBootCatalog, pThis->cBlocksInPrimaryVolumeSpace);
2352
2353 int rc = RTVfsFileReadAt(pThis->hSrcFile, offBootCatalog * (uint64_t)ISO9660_SECTOR_SIZE,
2354 pThis->abBuf, ISO9660_SECTOR_SIZE, NULL);
2355 if (RT_FAILURE(rc))
2356 return rtFsIsoImpError(pThis, rc, "Error reading boot catalog at block #%#RX32: %Rrc", offBootCatalog, rc);
2357
2358
2359 /*
2360 * Process the 'validation entry'.
2361 */
2362 PCISO9660ELTORITOVALIDATIONENTRY pValEntry = (PCISO9660ELTORITOVALIDATIONENTRY)&pThis->abBuf[0];
2363 if (pValEntry->bHeaderId != ISO9660_ELTORITO_HEADER_ID_VALIDATION_ENTRY)
2364 return rtFsIsoImpError(pThis, VERR_ISOMK_IMPORT_BOOT_CAT_BAD_VALIDATION_HEADER_ID,
2365 "Invalid boot catalog validation entry header ID: %#x, expected %#x",
2366 pValEntry->bHeaderId, ISO9660_ELTORITO_HEADER_ID_VALIDATION_ENTRY);
2367
2368 if ( pValEntry->bKey1 != ISO9660_ELTORITO_KEY_BYTE_1
2369 || pValEntry->bKey2 != ISO9660_ELTORITO_KEY_BYTE_2)
2370 return rtFsIsoImpError(pThis, VERR_ISOMK_IMPORT_BOOT_CAT_BAD_VALIDATION_KEYS,
2371 "Invalid boot catalog validation entry keys: %#x %#x, expected %#x %#x",
2372 pValEntry->bKey1, pValEntry->bKey2, ISO9660_ELTORITO_KEY_BYTE_1, ISO9660_ELTORITO_KEY_BYTE_2);
2373
2374 /* Check the checksum (should sum up to be zero). */
2375 uint16_t uChecksum = 0;
2376 uint16_t const *pu16 = (uint16_t const *)pValEntry;
2377 size_t cLeft = sizeof(*pValEntry) / sizeof(uint16_t);
2378 while (cLeft-- > 0)
2379 {
2380 uChecksum += RT_LE2H_U16(*pu16);
2381 pu16++;
2382 }
2383 if (uChecksum != 0)
2384 return rtFsIsoImpError(pThis, VERR_ISOMK_IMPORT_BOOT_CAT_BAD_VALIDATION_CHECKSUM,
2385 "Invalid boot catalog validation entry checksum: %#x, expected 0", uChecksum);
2386
2387 /* The string ID. ASSUME no leading zeros in valid strings. */
2388 const char *pszId = NULL;
2389 char szId[32];
2390 if (pValEntry->achId[0] != '\0')
2391 {
2392 memcpy(szId, pValEntry->achId, sizeof(pValEntry->achId));
2393 szId[sizeof(pValEntry->achId)] = '\0';
2394 pszId = szId;
2395 }
2396
2397 /*
2398 * Before we tell the ISO maker about the validation entry, we need to sort
2399 * out the file backing the boot catalog. This isn't fatal if it fails.
2400 */
2401 PRTFSISOMKIMPBLOCK2FILE pBlock2File = (PRTFSISOMKIMPBLOCK2FILE)RTAvlU32Get(&pThis->Block2FileRoot, offBootCatalog);
2402 if (pBlock2File)
2403 {
2404 rc = RTFsIsoMakerBootCatSetFile(pThis->hIsoMaker, pBlock2File->idxObj);
2405 if (RT_FAILURE(rc))
2406 rtFsIsoImpError(pThis, rc, "RTFsIsoMakerBootCatSetFile failed: %Rrc", rc);
2407 }
2408
2409 /*
2410 * Set the validation entry.
2411 */
2412 rc = RTFsIsoMakerBootCatSetValidationEntry(pThis->hIsoMaker, pValEntry->bPlatformId, pszId);
2413 if (RT_FAILURE(rc))
2414 return rtFsIsoImpError(pThis, rc, "RTFsIsoMakerBootCatSetValidationEntry(,%#x,%s) failed: %Rrc",
2415 pValEntry->bPlatformId, pszId);
2416 Assert(pThis->pResults->cBootCatEntries == UINT32_MAX);
2417 pThis->pResults->cBootCatEntries = 0;
2418
2419 /*
2420 * Process the default entry and any subsequent entries.
2421 */
2422 bool fSeenFinal = false;
2423 uint32_t const cMaxEntries = ISO9660_SECTOR_SIZE / ISO9660_ELTORITO_ENTRY_SIZE;
2424 for (uint32_t iEntry = 1; iEntry < cMaxEntries; iEntry++)
2425 {
2426 uint8_t const *pbEntry = &pThis->abBuf[iEntry * ISO9660_ELTORITO_ENTRY_SIZE];
2427 uint8_t const idHeader = *pbEntry;
2428 if ( iEntry == 1 /* default*/
2429 || idHeader == ISO9660_ELTORITO_BOOT_INDICATOR_BOOTABLE
2430 || idHeader == ISO9660_ELTORITO_BOOT_INDICATOR_NOT_BOOTABLE)
2431 {
2432 uint32_t cSkip = 0;
2433 rtFsIsoImportProcessElToritoSectionEntry(pThis, iEntry, cMaxEntries, (PCISO9660ELTORITOSECTIONENTRY)pbEntry, &cSkip);
2434 iEntry += cSkip;
2435 }
2436 else if (idHeader == ISO9660_ELTORITO_HEADER_ID_SECTION_HEADER)
2437 rtFsIsoImportProcessElToritoSectionHeader(pThis, iEntry, (PCISO9660ELTORITOSECTIONHEADER)pbEntry, szId);
2438 else if (idHeader == ISO9660_ELTORITO_HEADER_ID_FINAL_SECTION_HEADER)
2439 {
2440 fSeenFinal = true;
2441 break;
2442 }
2443 else
2444 rtFsIsoImpError(pThis, VERR_ISOMK_IMPORT_BOOT_CAT_UNKNOWN_HEADER_ID,
2445 "Unknown boot catalog header ID for entry #%#x: %#x", iEntry, idHeader);
2446 }
2447
2448 if (!fSeenFinal)
2449 rc = rtFsIsoImpError(pThis, VERR_ISOMK_IMPORT_BOOT_CAT_MISSING_FINAL_OR_TOO_BIG,
2450 "Boot catalog is probably larger than a sector, or it's missing the final section header entry");
2451 return rc;
2452}
2453
2454
2455/**
2456 * Imports an existing ISO.
2457 *
2458 * Just like other source files, the existing image must remain present and
2459 * unmodified till the ISO maker is done with it.
2460 *
2461 * @returns IRPT status code.
2462 * @param hIsoMaker The ISO maker handle.
2463 * @param pszIso Path to the existing image to import / clone.
2464 * This is fed to RTVfsChainOpenFile.
2465 * @param fFlags Reserved for the future, MBZ.
2466 * @param poffError Where to return the position in @a pszIso
2467 * causing trouble when opening it for reading.
2468 * Optional.
2469 * @param pErrInfo Where to return additional error information.
2470 * Optional.
2471 */
2472RTDECL(int) RTFsIsoMakerImport(RTFSISOMAKER hIsoMaker, const char *pszIso, uint32_t fFlags,
2473 PRTFSISOMAKERIMPORTRESULTS pResults, PRTERRINFO pErrInfo)
2474{
2475 /*
2476 * Validate input.
2477 */
2478 AssertPtrReturn(pResults, VERR_INVALID_POINTER);
2479 pResults->cAddedNames = 0;
2480 pResults->cAddedDirs = 0;
2481 pResults->cbAddedDataBlocks = 0;
2482 pResults->cAddedFiles = 0;
2483 pResults->cBootCatEntries = UINT32_MAX;
2484 pResults->cbSysArea = 0;
2485 pResults->cErrors = 0;
2486 pResults->offError = UINT32_MAX;
2487 AssertReturn(!(fFlags & ~RTFSISOMK_IMPORT_F_VALID_MASK), VERR_INVALID_FLAGS);
2488
2489 /*
2490 * Open the input file and start working on it.
2491 */
2492 RTVFSFILE hSrcFile;
2493 int rc = RTVfsChainOpenFile(pszIso, RTFILE_O_READ | RTFILE_O_OPEN | RTFILE_O_DENY_WRITE,
2494 &hSrcFile, &pResults->offError, pErrInfo);
2495 if (RT_FAILURE(rc))
2496 return rc;
2497 pResults->offError = UINT32_MAX;
2498
2499 /*
2500 * Get the file size.
2501 */
2502 uint64_t cbSrcFile = 0;
2503 rc = RTVfsFileGetSize(hSrcFile, &cbSrcFile);
2504 if (RT_SUCCESS(rc))
2505 {
2506 /*
2507 * Allocate and init the importer state.
2508 */
2509 PRTFSISOMKIMPORTER pThis = (PRTFSISOMKIMPORTER)RTMemAllocZ(sizeof(*pThis));
2510 if (pThis)
2511 {
2512 pThis->hIsoMaker = hIsoMaker;
2513 pThis->fFlags = fFlags;
2514 pThis->rc = VINF_SUCCESS;
2515 pThis->pErrInfo = pErrInfo;
2516 pThis->hSrcFile = hSrcFile;
2517 pThis->cbSrcFile = cbSrcFile;
2518 pThis->cBlocksInSrcFile = cbSrcFile / ISO9660_SECTOR_SIZE;
2519 pThis->idxSrcFile = UINT32_MAX;
2520 //pThis->Block2FileRoot = NULL;
2521 //pThis->cBlocksInPrimaryVolumeSpace = 0;
2522 //pThis->cbPrimaryVolumeSpace = 0
2523 //pThis->cVolumesInSet = 0;
2524 //pThis->idPrimaryVol = 0;
2525 //pThis->fSeenJoliet = false;
2526 pThis->pResults = pResults;
2527 //pThis->fSuspSeenSP = false;
2528 //pThis->offSuspSkip = 0;
2529 pThis->offRockBuf = UINT64_MAX;
2530
2531 /*
2532 * Check if this looks like a plausible ISO by checking out the first volume descriptor.
2533 */
2534 rc = RTVfsFileReadAt(hSrcFile, _32K, &pThis->uSectorBuf.PrimVolDesc, sizeof(pThis->uSectorBuf.PrimVolDesc), NULL);
2535 if (RT_SUCCESS(rc))
2536 {
2537 if ( pThis->uSectorBuf.VolDescHdr.achStdId[0] == ISO9660VOLDESC_STD_ID_0
2538 && pThis->uSectorBuf.VolDescHdr.achStdId[1] == ISO9660VOLDESC_STD_ID_1
2539 && pThis->uSectorBuf.VolDescHdr.achStdId[2] == ISO9660VOLDESC_STD_ID_2
2540 && pThis->uSectorBuf.VolDescHdr.achStdId[3] == ISO9660VOLDESC_STD_ID_3
2541 && pThis->uSectorBuf.VolDescHdr.achStdId[4] == ISO9660VOLDESC_STD_ID_4
2542 && ( pThis->uSectorBuf.VolDescHdr.bDescType == ISO9660VOLDESC_TYPE_PRIMARY
2543 || pThis->uSectorBuf.VolDescHdr.bDescType == ISO9660VOLDESC_TYPE_BOOT_RECORD) )
2544 {
2545 /*
2546 * Process the volume descriptors using the sector buffer, starting
2547 * with the one we've already got sitting there. We postpone processing
2548 * the el torito one till after the others, so we can name files and size
2549 * referenced in it.
2550 */
2551 uint32_t cPrimaryVolDescs = 0;
2552 uint32_t iElTorito = UINT32_MAX;
2553 uint32_t iVolDesc = 0;
2554 for (;;)
2555 {
2556 switch (pThis->uSectorBuf.VolDescHdr.bDescType)
2557 {
2558 case ISO9660VOLDESC_TYPE_PRIMARY:
2559 cPrimaryVolDescs++;
2560 if (cPrimaryVolDescs == 1)
2561 {
2562 pThis->offPrimaryVolDesc = _32K / ISO9660_SECTOR_SIZE + iVolDesc;
2563 rtFsIsoImportProcessPrimaryDesc(pThis, &pThis->uSectorBuf.PrimVolDesc);
2564 }
2565 else
2566 rtFsIsoImpError(pThis, VERR_ISOMK_IMPORT_MULTIPLE_PRIMARY_VOL_DESCS,
2567 "Only a single primary volume descriptor is currently supported");
2568 break;
2569
2570 case ISO9660VOLDESC_TYPE_SUPPLEMENTARY:
2571 if (cPrimaryVolDescs > 0)
2572 rtFsIsoImportProcessSupplementaryDesc(pThis, &pThis->uSectorBuf.SupVolDesc);
2573 else
2574 rtFsIsoImpError(pThis, VERR_ISOMK_IMPORT_SUPPLEMENTARY_BEFORE_PRIMARY,
2575 "Primary volume descriptor expected before any supplementary descriptors!");
2576 break;
2577
2578 case ISO9660VOLDESC_TYPE_BOOT_RECORD:
2579 if (strcmp(pThis->uSectorBuf.ElToritoDesc.achBootSystemId,
2580 ISO9660BOOTRECORDELTORITO_BOOT_SYSTEM_ID) == 0)
2581 {
2582 if (iElTorito == UINT32_MAX)
2583 iElTorito = iVolDesc;
2584 else
2585 rtFsIsoImpError(pThis, VERR_ISOMK_IMPORT_MULTIPLE_EL_TORITO_DESCS,
2586 "Only a single El Torito descriptor exepcted!");
2587 }
2588 break;
2589
2590 case ISO9660VOLDESC_TYPE_PARTITION:
2591 /* ignore for now */
2592 break;
2593
2594 case ISO9660VOLDESC_TYPE_TERMINATOR:
2595 AssertFailed();
2596 break;
2597 }
2598
2599
2600 /*
2601 * Read the next volume descriptor and check the signature.
2602 */
2603 iVolDesc++;
2604 if (iVolDesc >= 32)
2605 {
2606 rtFsIsoImpError(pThis, VERR_ISOMK_IMPORT_TOO_MANY_VOL_DESCS, "Parses at most 32 volume descriptors");
2607 break;
2608 }
2609
2610 rc = RTVfsFileReadAt(hSrcFile, _32K + iVolDesc * ISO9660_SECTOR_SIZE,
2611 &pThis->uSectorBuf, sizeof(pThis->uSectorBuf), NULL);
2612 if (RT_FAILURE(rc))
2613 {
2614 rtFsIsoImpError(pThis, rc, "Error reading the volume descriptor #%u at %#RX32: %Rrc",
2615 iVolDesc, _32K + iVolDesc * ISO9660_SECTOR_SIZE, rc);
2616 break;
2617 }
2618
2619 if ( pThis->uSectorBuf.VolDescHdr.achStdId[0] != ISO9660VOLDESC_STD_ID_0
2620 || pThis->uSectorBuf.VolDescHdr.achStdId[1] != ISO9660VOLDESC_STD_ID_1
2621 || pThis->uSectorBuf.VolDescHdr.achStdId[2] != ISO9660VOLDESC_STD_ID_2
2622 || pThis->uSectorBuf.VolDescHdr.achStdId[3] != ISO9660VOLDESC_STD_ID_3
2623 || pThis->uSectorBuf.VolDescHdr.achStdId[4] != ISO9660VOLDESC_STD_ID_4)
2624 {
2625 rtFsIsoImpError(pThis, VERR_ISOMK_IMPORT_INVALID_VOL_DESC_HDR,
2626 "Invalid volume descriptor header #%u at %#RX32: %.*Rhxs",
2627 iVolDesc, _32K + iVolDesc * ISO9660_SECTOR_SIZE,
2628 (int)sizeof(pThis->uSectorBuf.VolDescHdr), &pThis->uSectorBuf.VolDescHdr);
2629 break;
2630 }
2631 /** @todo UDF support. */
2632 if (pThis->uSectorBuf.VolDescHdr.bDescType == ISO9660VOLDESC_TYPE_TERMINATOR)
2633 break;
2634 }
2635
2636 /*
2637 * Process the system area.
2638 */
2639 if (RT_SUCCESS(pThis->rc) || pThis->idxSrcFile != UINT32_MAX)
2640 {
2641 rc = RTVfsFileReadAt(hSrcFile, 0, pThis->abBuf, _32K, NULL);
2642 if (RT_SUCCESS(rc))
2643 {
2644 if (!ASMMemIsAllU8(pThis->abBuf, _32K, 0))
2645 {
2646 /* Drop zero sectors from the end. */
2647 uint32_t cbSysArea = _32K;
2648 while ( cbSysArea >= ISO9660_SECTOR_SIZE
2649 && ASMMemIsAllU8(&pThis->abBuf[cbSysArea - ISO9660_SECTOR_SIZE], ISO9660_SECTOR_SIZE, 0))
2650 cbSysArea -= ISO9660_SECTOR_SIZE;
2651
2652 /** @todo HFS */
2653 pThis->pResults->cbSysArea = cbSysArea;
2654 rc = RTFsIsoMakerSetSysAreaContent(hIsoMaker, pThis->abBuf, cbSysArea, 0);
2655 if (RT_FAILURE(rc))
2656 rtFsIsoImpError(pThis, rc, "RTFsIsoMakerSetSysAreaContent failed: %Rrc", rc);
2657 }
2658 }
2659 else
2660 rtFsIsoImpError(pThis, rc, "Error reading the system area (0..32KB): %Rrc", rc);
2661 }
2662
2663 /*
2664 * Do the El Torito descriptor.
2665 */
2666 if ( iElTorito != UINT32_MAX
2667 && !(pThis->fFlags & RTFSISOMK_IMPORT_F_NO_BOOT)
2668 && (RT_SUCCESS(pThis->rc) || pThis->idxSrcFile != UINT32_MAX))
2669 {
2670 rc = RTVfsFileReadAt(hSrcFile, _32K + iElTorito * ISO9660_SECTOR_SIZE,
2671 &pThis->uSectorBuf, sizeof(pThis->uSectorBuf), NULL);
2672 if (RT_SUCCESS(rc))
2673 rtFsIsoImportProcessElToritoDesc(pThis, &pThis->uSectorBuf.ElToritoDesc);
2674 else
2675 rtFsIsoImpError(pThis, rc, "Error reading the El Torito volume descriptor at %#RX32: %Rrc",
2676 _32K + iElTorito * ISO9660_SECTOR_SIZE, rc);
2677 }
2678
2679 /*
2680 * Return the first error status.
2681 */
2682 rc = pThis->rc;
2683 }
2684 else
2685 rc = RTErrInfoSetF(pErrInfo, VERR_ISOMK_IMPORT_UNKNOWN_FORMAT, "Invalid volume descriptor header: %.*Rhxs",
2686 (int)sizeof(pThis->uSectorBuf.VolDescHdr), &pThis->uSectorBuf.VolDescHdr);
2687 }
2688
2689 /*
2690 * Destroy the state.
2691 */
2692 RTAvlU32Destroy(&pThis->Block2FileRoot, rtFsIsoMakerImportDestroyData2File, NULL);
2693 RTMemFree(pThis);
2694 }
2695 else
2696 rc = VERR_NO_MEMORY;
2697 }
2698 RTVfsFileRelease(hSrcFile);
2699 return rc;
2700
2701}
2702
Note: See TracBrowser for help on using the repository browser.

© 2025 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette