VirtualBox

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

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

IPRT/isomaker: Added RTFsIsoMakerAddSymlink & RTFsIsoMakerAddUnnamedSymlink. [build fix]

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

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