VirtualBox

source: vbox/trunk/src/VBox/Storage/DMG.cpp@ 62482

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

(C) 2016

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 81.9 KB
Line 
1/* $Id: DMG.cpp 62482 2016-07-22 18:30:37Z vboxsync $ */
2/** @file
3 * VBoxDMG - Interpreter for Apple Disk Images (DMG).
4 */
5
6/*
7 * Copyright (C) 2010-2016 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
18
19/*********************************************************************************************************************************
20* Header Files *
21*********************************************************************************************************************************/
22#define LOG_GROUP LOG_GROUP_VD_DMG
23#include <VBox/vd-plugin.h>
24#include <VBox/vd-ifs.h>
25#include <VBox/log.h>
26#include <VBox/err.h>
27
28#include <iprt/asm.h>
29#include <iprt/alloca.h>
30#include <iprt/assert.h>
31#include <iprt/base64.h>
32#include <iprt/ctype.h>
33#include <iprt/mem.h>
34#include <iprt/string.h>
35#include <iprt/zip.h>
36#include <iprt/formats/xar.h>
37
38#include "VDBackends.h"
39
40
41/*********************************************************************************************************************************
42* Structures and Typedefs *
43*********************************************************************************************************************************/
44#if 0
45/** @def VBOX_WITH_DIRECT_XAR_ACCESS
46 * When defined, we will use RTVfs to access the XAR file instead of going
47 * the slightly longer way thru the VFS -> VD wrapper. */
48# define VBOX_WITH_DIRECT_XAR_ACCESS
49#endif
50
51/** Sector size, multiply with all sector counts to get number of bytes. */
52#define DMG_SECTOR_SIZE 512
53
54/** Convert block number/size to byte offset/size. */
55#define DMG_BLOCK2BYTE(u) ((uint64_t)(u) << 9)
56
57/** Convert byte offset/size to block number/size. */
58#define DMG_BYTE2BLOCK(u) ((u) >> 9)
59
60/**
61 * UDIF checksum structure.
62 */
63typedef struct DMGUDIFCKSUM
64{
65 uint32_t u32Kind; /**< The kind of checksum. */
66 uint32_t cBits; /**< The size of the checksum. */
67 union
68 {
69 uint8_t au8[128]; /**< 8-bit view. */
70 uint32_t au32[32]; /**< 32-bit view. */
71 } uSum; /**< The checksum. */
72} DMGUDIFCKSUM;
73AssertCompileSize(DMGUDIFCKSUM, 8 + 128);
74typedef DMGUDIFCKSUM *PDMGUDIFCKSUM;
75typedef const DMGUDIFCKSUM *PCDMGUDIFCKSUM;
76
77/** @name Checksum Kind (DMGUDIFCKSUM::u32Kind)
78 * @{ */
79/** No checksum. */
80#define DMGUDIFCKSUM_NONE UINT32_C(0)
81/** CRC-32. */
82#define DMGUDIFCKSUM_CRC32 UINT32_C(2)
83/** @} */
84
85/**
86 * UDIF ID.
87 * This is kind of like a UUID only it isn't, but we'll use the UUID
88 * representation of it for simplicity.
89 */
90typedef RTUUID DMGUDIFID;
91AssertCompileSize(DMGUDIFID, 16);
92typedef DMGUDIFID *PDMGUDIFID;
93typedef const DMGUDIFID *PCDMGUDIFID;
94
95/**
96 * UDIF footer used by Apple Disk Images (DMG).
97 *
98 * This is a footer placed 512 bytes from the end of the file. Typically a DMG
99 * file starts with the data, which is followed by the block table and then ends
100 * with this structure.
101 *
102 * All fields are stored in big endian format.
103 */
104#pragma pack(1)
105typedef struct DMGUDIF
106{
107 uint32_t u32Magic; /**< 0x000 - Magic, 'koly' (DMGUDIF_MAGIC). (fUDIFSignature) */
108 uint32_t u32Version; /**< 0x004 - The UDIF version (DMGUDIF_VER_CURRENT). (fUDIFVersion) */
109 uint32_t cbFooter; /**< 0x008 - The size of the this structure (512). (fUDIFHeaderSize) */
110 uint32_t fFlags; /**< 0x00c - Flags. (fUDIFFlags) */
111 uint64_t offRunData; /**< 0x010 - Where the running data fork starts (usually 0). (fUDIFRunningDataForkOffset) */
112 uint64_t offData; /**< 0x018 - Where the data fork starts (usually 0). (fUDIFDataForkOffset) */
113 uint64_t cbData; /**< 0x020 - Size of the data fork (in bytes). (fUDIFDataForkLength) */
114 uint64_t offRsrc; /**< 0x028 - Where the resource fork starts (usually cbData or 0). (fUDIFRsrcForkOffset) */
115 uint64_t cbRsrc; /**< 0x030 - The size of the resource fork. (fUDIFRsrcForkLength)*/
116 uint32_t iSegment; /**< 0x038 - The segment number of this file. (fUDIFSegmentNumber) */
117 uint32_t cSegments; /**< 0x03c - The number of segments. (fUDIFSegmentCount) */
118 DMGUDIFID SegmentId; /**< 0x040 - The segment ID. (fUDIFSegmentID) */
119 DMGUDIFCKSUM DataCkSum; /**< 0x050 - The data checksum. (fUDIFDataForkChecksum) */
120 uint64_t offXml; /**< 0x0d8 - The XML offset (.plist kind of data). (fUDIFXMLOffset) */
121 uint64_t cbXml; /**< 0x0e0 - The size of the XML. (fUDIFXMLSize) */
122 uint8_t abUnknown[120]; /**< 0x0e8 - Unknown stuff, hdiutil doesn't dump it... */
123 DMGUDIFCKSUM MasterCkSum; /**< 0x160 - The master checksum. (fUDIFMasterChecksum) */
124 uint32_t u32Type; /**< 0x1e8 - The image type. (fUDIFImageVariant) */
125 uint64_t cSectors; /**< 0x1ec - The sector count. Warning! Unaligned! (fUDISectorCount) */
126 uint32_t au32Unknown[3]; /**< 0x1f4 - Unknown stuff, hdiutil doesn't dump it... */
127} DMGUDIF;
128#pragma pack()
129AssertCompileSize(DMGUDIF, 512);
130AssertCompileMemberOffset(DMGUDIF, cbRsrc, 0x030);
131AssertCompileMemberOffset(DMGUDIF, cbXml, 0x0e0);
132AssertCompileMemberOffset(DMGUDIF, cSectors, 0x1ec);
133
134typedef DMGUDIF *PDMGUDIF;
135typedef const DMGUDIF *PCDMGUDIF;
136
137/** The UDIF magic 'koly' (DMGUDIF::u32Magic). */
138#define DMGUDIF_MAGIC UINT32_C(0x6b6f6c79)
139
140/** The current UDIF version (DMGUDIF::u32Version).
141 * This is currently the only we recognizes and will create. */
142#define DMGUDIF_VER_CURRENT 4
143
144/** @name UDIF flags (DMGUDIF::fFlags).
145 * @{ */
146/** Flatten image whatever that means.
147 * (hdiutil -debug calls it kUDIFFlagsFlattened.) */
148#define DMGUDIF_FLAGS_FLATTENED RT_BIT_32(0)
149/** Internet enabled image.
150 * (hdiutil -debug calls it kUDIFFlagsInternetEnabled) */
151#define DMGUDIF_FLAGS_INET_ENABLED RT_BIT_32(2)
152/** Mask of known bits. */
153#define DMGUDIF_FLAGS_KNOWN_MASK (RT_BIT_32(0) | RT_BIT_32(2))
154/** @} */
155
156/** @name UDIF Image Types (DMGUDIF::u32Type).
157 * @{ */
158/** Device image type. (kUDIFDeviceImageType) */
159#define DMGUDIF_TYPE_DEVICE 1
160/** Device image type. (kUDIFPartitionImageType) */
161#define DMGUDIF_TYPE_PARTITION 2
162/** @} */
163
164/**
165 * BLKX data.
166 *
167 * This contains the start offset and size of raw data stored in the image.
168 *
169 * All fields are stored in big endian format.
170 */
171#pragma pack(1)
172typedef struct DMGBLKX
173{
174 uint32_t u32Magic; /**< 0x000 - Magic, 'mish' (DMGBLKX_MAGIC). */
175 uint32_t u32Version; /**< 0x004 - The BLKX version (DMGBLKX_VER_CURRENT). */
176 uint64_t cSectornumberFirst; /**< 0x008 - The first sector number the block represents in the virtual device. */
177 uint64_t cSectors; /**< 0x010 - Number of sectors this block represents. */
178 uint64_t offDataStart; /**< 0x018 - Start offset for raw data. */
179 uint32_t cSectorsDecompress; /**< 0x020 - Size of the buffer in sectors needed to decompress. */
180 uint32_t u32BlocksDescriptor; /**< 0x024 - Blocks descriptor. */
181 uint8_t abReserved[24];
182 DMGUDIFCKSUM BlkxCkSum; /**< 0x03c - Checksum for the BLKX table. */
183 uint32_t cBlocksRunCount; /**< 0x - Number of entries in the blkx run table afterwards. */
184} DMGBLKX;
185#pragma pack()
186AssertCompileSize(DMGBLKX, 204);
187
188typedef DMGBLKX *PDMGBLKX;
189typedef const DMGBLKX *PCDMGBLKX;
190
191/** The BLKX magic 'mish' (DMGBLKX::u32Magic). */
192#define DMGBLKX_MAGIC UINT32_C(0x6d697368)
193/** BLKX version (DMGBLKX::u32Version). */
194#define DMGBLKX_VERSION UINT32_C(0x00000001)
195
196/** Blocks descriptor type: entire device. */
197#define DMGBLKX_DESC_ENTIRE_DEVICE UINT32_C(0xfffffffe)
198
199/**
200 * BLKX table descriptor.
201 *
202 * All fields are stored in big endian format.
203 */
204#pragma pack(1)
205typedef struct DMGBLKXDESC
206{
207 uint32_t u32Type; /**< 0x000 - Type of the descriptor. */
208 uint32_t u32Reserved; /**< 0x004 - Reserved, but contains +beg or +end in case thisi is a comment descriptor. */
209 uint64_t u64SectorStart; /**< 0x008 - First sector number in the block this entry describes. */
210 uint64_t u64SectorCount; /**< 0x010 - Number of sectors this entry describes. */
211 uint64_t offData; /**< 0x018 - Offset in the image where the data starts. */
212 uint64_t cbData; /**< 0x020 - Number of bytes in the image. */
213} DMGBLKXDESC;
214#pragma pack()
215AssertCompileSize(DMGBLKXDESC, 40);
216
217typedef DMGBLKXDESC *PDMGBLKXDESC;
218typedef const DMGBLKXDESC *PCDMGBLKXDESC;
219
220/** Raw image data type. */
221#define DMGBLKXDESC_TYPE_RAW 1
222/** Ignore type. */
223#define DMGBLKXDESC_TYPE_IGNORE 2
224/** Compressed with zlib type. */
225#define DMGBLKXDESC_TYPE_ZLIB UINT32_C(0x80000005)
226/** Comment type. */
227#define DMGBLKXDESC_TYPE_COMMENT UINT32_C(0x7ffffffe)
228/** Terminator type. */
229#define DMGBLKXDESC_TYPE_TERMINATOR UINT32_C(0xffffffff)
230
231/**
232 * UDIF Resource Entry.
233 */
234typedef struct DMGUDIFRSRCENTRY
235{
236 /** The ID. */
237 int32_t iId;
238 /** Attributes. */
239 uint32_t fAttributes;
240 /** The name. */
241 char *pszName;
242 /** The CoreFoundation name. Can be NULL. */
243 char *pszCFName;
244 /** The size of the data. */
245 size_t cbData;
246 /** The raw data. */
247 uint8_t *pbData;
248} DMGUDIFRSRCENTRY;
249/** Pointer to an UDIF resource entry. */
250typedef DMGUDIFRSRCENTRY *PDMGUDIFRSRCENTRY;
251/** Pointer to a const UDIF resource entry. */
252typedef DMGUDIFRSRCENTRY const *PCDMGUDIFRSRCENTRY;
253
254/**
255 * UDIF Resource Array.
256 */
257typedef struct DMGUDIFRSRCARRAY
258{
259 /** The array name. */
260 char szName[12];
261 /** The number of occupied entries. */
262 uint32_t cEntries;
263 /** The array entries.
264 * A lazy bird ASSUME there are no more than 4 entries in any DMG. Increase the
265 * size if DMGs with more are found.
266 * r=aeichner: Saw one with 6 here (image of a whole DVD) */
267 DMGUDIFRSRCENTRY aEntries[10];
268} DMGUDIFRSRCARRAY;
269/** Pointer to a UDIF resource array. */
270typedef DMGUDIFRSRCARRAY *PDMGUDIFRSRCARRAY;
271/** Pointer to a const UDIF resource array. */
272typedef DMGUDIFRSRCARRAY const *PCDMGUDIFRSRCARRAY;
273
274/**
275 * DMG extent types.
276 */
277typedef enum DMGEXTENTTYPE
278{
279 /** Null, never used. */
280 DMGEXTENTTYPE_NULL = 0,
281 /** Raw image data. */
282 DMGEXTENTTYPE_RAW,
283 /** Zero extent, reads return 0 and writes have no effect. */
284 DMGEXTENTTYPE_ZERO,
285 /** Compressed extent - compression method ZLIB. */
286 DMGEXTENTTYPE_COMP_ZLIB,
287 /** 32bit hack. */
288 DMGEXTENTTYPE_32BIT_HACK = 0x7fffffff
289} DMGEXTENTTYPE, *PDMGEXTENTTYPE;
290
291/**
292 * DMG extent mapping a virtual image block to real file offsets.
293 */
294typedef struct DMGEXTENT
295{
296 /** Extent type. */
297 DMGEXTENTTYPE enmType;
298 /** First sector this extent describes. */
299 uint64_t uSectorExtent;
300 /** Number of sectors this extent describes. */
301 uint64_t cSectorsExtent;
302 /** Start offset in the real file. */
303 uint64_t offFileStart;
304 /** Number of bytes for the extent data in the file. */
305 uint64_t cbFile;
306} DMGEXTENT;
307/** Pointer to an DMG extent. */
308typedef DMGEXTENT *PDMGEXTENT;
309
310/**
311 * VirtualBox Apple Disk Image (DMG) interpreter instance data.
312 */
313typedef struct DMGIMAGE
314{
315 /** Image name.
316 * Kept around for logging and delete-on-close purposes. */
317 const char *pszFilename;
318 /** Storage handle. */
319 PVDIOSTORAGE pStorage;
320
321 /** Pointer to the per-disk VD interface list. */
322 PVDINTERFACE pVDIfsDisk;
323 /** Pointer to the per-image VD interface list. */
324 PVDINTERFACE pVDIfsImage;
325 /** Error interface. */
326 PVDINTERFACEERROR pIfError;
327 /** I/O interface - careful accessing this because of hDmgFileInXar. */
328 PVDINTERFACEIOINT pIfIoXxx;
329
330
331 /** The VFS file handle for a DMG within a XAR archive. */
332 RTVFSFILE hDmgFileInXar;
333 /** XAR file system stream handle.
334 * Sitting on this isn't really necessary, but insurance against the XAR code
335 * changes making back references from child objects to the stream itself. */
336 RTVFSFSSTREAM hXarFss;
337
338 /** Flags the image was opened with. */
339 uint32_t uOpenFlags;
340 /** Image flags. */
341 unsigned uImageFlags;
342 /** Total size of the virtual image. */
343 uint64_t cbSize;
344 /** Size of the image. */
345 uint64_t cbFile;
346 /** Physical geometry of this image. */
347 VDGEOMETRY PCHSGeometry;
348 /** Logical geometry of this image. */
349 VDGEOMETRY LCHSGeometry;
350
351 /** The resources.
352 * A lazy bird ASSUME there are only two arrays in the resource-fork section in
353 * the XML, namely 'blkx' and 'plst'. These have been assigned fixed indexes. */
354 DMGUDIFRSRCARRAY aRsrcs[2];
355 /** The UDIF footer. */
356 DMGUDIF Ftr;
357
358 /** Number of valid extents in the array. */
359 unsigned cExtents;
360 /** Number of entries the array can hold. */
361 unsigned cExtentsMax;
362 /** Pointer to the extent array. */
363 PDMGEXTENT paExtents;
364 /** Index of the last accessed extent. */
365 unsigned idxExtentLast;
366
367 /** Extent which owns the data in the buffer. */
368 PDMGEXTENT pExtentDecomp;
369 /** Buffer holding the decompressed data for a extent. */
370 void *pvDecompExtent;
371 /** Size of the buffer. */
372 size_t cbDecompExtent;
373} DMGIMAGE;
374/** Pointer to an instance of the DMG Image Interpreter. */
375typedef DMGIMAGE *PDMGIMAGE;
376
377/** @name Resources indexes (into DMG::aRsrcs).
378 * @{ */
379#define DMG_RSRC_IDX_BLKX 0
380#define DMG_RSRC_IDX_PLST 1
381/** @} */
382
383/** State for the input callout of the inflate reader. */
384typedef struct DMGINFLATESTATE
385{
386 /* Image this operation relates to. */
387 PDMGIMAGE pImage;
388 /* Total size of the data to read. */
389 size_t cbSize;
390 /* Offset in the file to read. */
391 uint64_t uFileOffset;
392 /* Current read position. */
393 ssize_t iOffset;
394} DMGINFLATESTATE;
395
396
397/*********************************************************************************************************************************
398* Defined Constants And Macros *
399*********************************************************************************************************************************/
400/** @def DMG_PRINTF
401 * Wrapper for LogRel.
402 */
403#define DMG_PRINTF(a) LogRel(a)
404
405/** @def DMG_VALIDATE
406 * For validating a struct thing and log/print what's wrong.
407 */
408# define DMG_VALIDATE(expr, logstuff) \
409 do { \
410 if (!(expr)) \
411 { \
412 LogRel(("DMG: validation failed: %s\nDMG: ", #expr)); \
413 LogRel(logstuff); \
414 fRc = false; \
415 } \
416 } while (0)
417
418
419/*********************************************************************************************************************************
420* Static Variables *
421*********************************************************************************************************************************/
422
423/** NULL-terminated array of supported file extensions. */
424static const VDFILEEXTENSION s_aDmgFileExtensions[] =
425{
426 {"dmg", VDTYPE_DVD},
427 {NULL, VDTYPE_INVALID}
428};
429
430
431/*********************************************************************************************************************************
432* Internal Functions *
433*********************************************************************************************************************************/
434static void dmgUdifFtrHost2FileEndian(PDMGUDIF pUdif);
435static void dmgUdifFtrFile2HostEndian(PDMGUDIF pUdif);
436
437static void dmgUdifIdHost2FileEndian(PDMGUDIFID pId);
438static void dmgUdifIdFile2HostEndian(PDMGUDIFID pId);
439
440static void dmgUdifCkSumHost2FileEndian(PDMGUDIFCKSUM pCkSum);
441static void dmgUdifCkSumFile2HostEndian(PDMGUDIFCKSUM pCkSum);
442static bool dmgUdifCkSumIsValid(PCDMGUDIFCKSUM pCkSum, const char *pszPrefix);
443
444
445
446/**
447 * vdIfIoIntFileReadSync / RTVfsFileReadAt wrapper.
448 */
449static int dmgWrapFileReadSync(PDMGIMAGE pThis, RTFOFF off, void *pvBuf, size_t cbToRead)
450{
451 int rc;
452 if (pThis->hDmgFileInXar == NIL_RTVFSFILE)
453 rc = vdIfIoIntFileReadSync(pThis->pIfIoXxx, pThis->pStorage, off, pvBuf, cbToRead);
454 else
455 rc = RTVfsFileReadAt(pThis->hDmgFileInXar, off, pvBuf, cbToRead, NULL);
456 return rc;
457}
458
459/**
460 * vdIfIoIntFileReadUser / RTVfsFileReadAt wrapper.
461 */
462static int dmgWrapFileReadUser(PDMGIMAGE pThis, RTFOFF off, PVDIOCTX pIoCtx, size_t cbToRead)
463{
464 int rc;
465 if (pThis->hDmgFileInXar == NIL_RTVFSFILE)
466 rc = vdIfIoIntFileReadUser(pThis->pIfIoXxx, pThis->pStorage, off, pIoCtx, cbToRead);
467 else
468 {
469 /*
470 * Alloate a temporary buffer on the stack or heap and use
471 * vdIfIoIntIoCtxCopyTo to work the context.
472 *
473 * The I/O context stuff seems too complicated and undocument that I'm
474 * not going to bother trying to implement this efficiently right now.
475 */
476 void *pvFree = NULL;
477 void *pvBuf;
478 if (cbToRead < _32K)
479 pvBuf = alloca(cbToRead);
480 else
481 pvFree = pvBuf = RTMemTmpAlloc(cbToRead);
482 if (pvBuf)
483 {
484 rc = RTVfsFileReadAt(pThis->hDmgFileInXar, off, pvBuf, cbToRead, NULL);
485 if (RT_SUCCESS(rc))
486 vdIfIoIntIoCtxCopyTo(pThis->pIfIoXxx, pIoCtx, pvBuf, cbToRead);
487 if (pvFree)
488 RTMemTmpFree(pvFree);
489 }
490 else
491 rc = VERR_NO_TMP_MEMORY;
492 }
493 return rc;
494}
495
496/**
497 * vdIfIoIntFileGetSize / RTVfsFileGetSize wrapper.
498 */
499static int dmgWrapFileGetSize(PDMGIMAGE pThis, uint64_t *pcbFile)
500{
501 int rc;
502 if (pThis->hDmgFileInXar == NIL_RTVFSFILE)
503 rc = vdIfIoIntFileGetSize(pThis->pIfIoXxx, pThis->pStorage, pcbFile);
504 else
505 rc = RTVfsFileGetSize(pThis->hDmgFileInXar, pcbFile);
506 return rc;
507}
508
509
510
511static DECLCALLBACK(int) dmgFileInflateHelper(void *pvUser, void *pvBuf, size_t cbBuf, size_t *pcbBuf)
512{
513 DMGINFLATESTATE *pInflateState = (DMGINFLATESTATE *)pvUser;
514
515 Assert(cbBuf);
516 if (pInflateState->iOffset < 0)
517 {
518 *(uint8_t *)pvBuf = RTZIPTYPE_ZLIB;
519 if (pcbBuf)
520 *pcbBuf = 1;
521 pInflateState->iOffset = 0;
522 return VINF_SUCCESS;
523 }
524 cbBuf = RT_MIN(cbBuf, pInflateState->cbSize);
525 int rc = dmgWrapFileReadSync(pInflateState->pImage, pInflateState->uFileOffset, pvBuf, cbBuf);
526 if (RT_FAILURE(rc))
527 return rc;
528 pInflateState->uFileOffset += cbBuf;
529 pInflateState->iOffset += cbBuf;
530 pInflateState->cbSize -= cbBuf;
531 Assert(pcbBuf);
532 *pcbBuf = cbBuf;
533 return VINF_SUCCESS;
534}
535
536/**
537 * Internal: read from a file and inflate the compressed data,
538 * distinguishing between async and normal operation
539 */
540DECLINLINE(int) dmgFileInflateSync(PDMGIMAGE pImage, uint64_t uOffset, size_t cbToRead,
541 void *pvBuf, size_t cbBuf)
542{
543 int rc;
544 PRTZIPDECOMP pZip = NULL;
545 DMGINFLATESTATE InflateState;
546 size_t cbActuallyRead;
547
548 InflateState.pImage = pImage;
549 InflateState.cbSize = cbToRead;
550 InflateState.uFileOffset = uOffset;
551 InflateState.iOffset = -1;
552
553 rc = RTZipDecompCreate(&pZip, &InflateState, dmgFileInflateHelper);
554 if (RT_FAILURE(rc))
555 return rc;
556 rc = RTZipDecompress(pZip, pvBuf, cbBuf, &cbActuallyRead);
557 RTZipDecompDestroy(pZip);
558 if (RT_FAILURE(rc))
559 return rc;
560 if (cbActuallyRead != cbBuf)
561 rc = VERR_VD_VMDK_INVALID_FORMAT;
562 return rc;
563}
564
565/**
566 * Swaps endian.
567 * @param pUdif The structure.
568 */
569static void dmgSwapEndianUdif(PDMGUDIF pUdif)
570{
571#ifndef RT_BIG_ENDIAN
572 pUdif->u32Magic = RT_BSWAP_U32(pUdif->u32Magic);
573 pUdif->u32Version = RT_BSWAP_U32(pUdif->u32Version);
574 pUdif->cbFooter = RT_BSWAP_U32(pUdif->cbFooter);
575 pUdif->fFlags = RT_BSWAP_U32(pUdif->fFlags);
576 pUdif->offRunData = RT_BSWAP_U64(pUdif->offRunData);
577 pUdif->offData = RT_BSWAP_U64(pUdif->offData);
578 pUdif->cbData = RT_BSWAP_U64(pUdif->cbData);
579 pUdif->offRsrc = RT_BSWAP_U64(pUdif->offRsrc);
580 pUdif->cbRsrc = RT_BSWAP_U64(pUdif->cbRsrc);
581 pUdif->iSegment = RT_BSWAP_U32(pUdif->iSegment);
582 pUdif->cSegments = RT_BSWAP_U32(pUdif->cSegments);
583 pUdif->offXml = RT_BSWAP_U64(pUdif->offXml);
584 pUdif->cbXml = RT_BSWAP_U64(pUdif->cbXml);
585 pUdif->u32Type = RT_BSWAP_U32(pUdif->u32Type);
586 pUdif->cSectors = RT_BSWAP_U64(pUdif->cSectors);
587#endif
588}
589
590
591/**
592 * Swaps endian from host cpu to file.
593 * @param pUdif The structure.
594 */
595static void dmgUdifFtrHost2FileEndian(PDMGUDIF pUdif)
596{
597 dmgSwapEndianUdif(pUdif);
598 dmgUdifIdHost2FileEndian(&pUdif->SegmentId);
599 dmgUdifCkSumHost2FileEndian(&pUdif->DataCkSum);
600 dmgUdifCkSumHost2FileEndian(&pUdif->MasterCkSum);
601}
602
603
604/**
605 * Swaps endian from file to host cpu.
606 * @param pUdif The structure.
607 */
608static void dmgUdifFtrFile2HostEndian(PDMGUDIF pUdif)
609{
610 dmgSwapEndianUdif(pUdif);
611 dmgUdifIdFile2HostEndian(&pUdif->SegmentId);
612 dmgUdifCkSumFile2HostEndian(&pUdif->DataCkSum);
613 dmgUdifCkSumFile2HostEndian(&pUdif->MasterCkSum);
614}
615
616/**
617 * Swaps endian from file to host cpu.
618 * @param pBlkx The blkx structure.
619 */
620static void dmgBlkxFile2HostEndian(PDMGBLKX pBlkx)
621{
622 pBlkx->u32Magic = RT_BE2H_U32(pBlkx->u32Magic);
623 pBlkx->u32Version = RT_BE2H_U32(pBlkx->u32Version);
624 pBlkx->cSectornumberFirst = RT_BE2H_U64(pBlkx->cSectornumberFirst);
625 pBlkx->cSectors = RT_BE2H_U64(pBlkx->cSectors);
626 pBlkx->offDataStart = RT_BE2H_U64(pBlkx->offDataStart);
627 pBlkx->cSectorsDecompress = RT_BE2H_U32(pBlkx->cSectorsDecompress);
628 pBlkx->u32BlocksDescriptor = RT_BE2H_U32(pBlkx->u32BlocksDescriptor);
629 pBlkx->cBlocksRunCount = RT_BE2H_U32(pBlkx->cBlocksRunCount);
630 dmgUdifCkSumFile2HostEndian(&pBlkx->BlkxCkSum);
631}
632
633/**
634 * Swaps endian from file to host cpu.
635 * @param pBlkxDesc The blkx descriptor structure.
636 */
637static void dmgBlkxDescFile2HostEndian(PDMGBLKXDESC pBlkxDesc)
638{
639 pBlkxDesc->u32Type = RT_BE2H_U32(pBlkxDesc->u32Type);
640 pBlkxDesc->u32Reserved = RT_BE2H_U32(pBlkxDesc->u32Reserved);
641 pBlkxDesc->u64SectorStart = RT_BE2H_U64(pBlkxDesc->u64SectorStart);
642 pBlkxDesc->u64SectorCount = RT_BE2H_U64(pBlkxDesc->u64SectorCount);
643 pBlkxDesc->offData = RT_BE2H_U64(pBlkxDesc->offData);
644 pBlkxDesc->cbData = RT_BE2H_U64(pBlkxDesc->cbData);
645}
646
647/**
648 * Validates an UDIF footer structure.
649 *
650 * @returns true if valid, false and LogRel()s on failure.
651 * @param pFtr The UDIF footer to validate.
652 * @param offFtr The offset of the structure.
653 */
654static bool dmgUdifFtrIsValid(PCDMGUDIF pFtr, uint64_t offFtr)
655{
656 bool fRc = true;
657
658 DMG_VALIDATE(!(pFtr->fFlags & ~DMGUDIF_FLAGS_KNOWN_MASK), ("fFlags=%#RX32 fKnown=%RX32\n", pFtr->fFlags, DMGUDIF_FLAGS_KNOWN_MASK));
659 DMG_VALIDATE(pFtr->offRunData < offFtr, ("offRunData=%#RX64\n", pFtr->offRunData));
660 DMG_VALIDATE(pFtr->cbData <= offFtr && pFtr->offData + pFtr->cbData <= offFtr, ("cbData=%#RX64 offData=%#RX64 offFtr=%#RX64\n", pFtr->cbData, pFtr->offData, offFtr));
661 DMG_VALIDATE(pFtr->offData < offFtr, ("offData=%#RX64\n", pFtr->offData));
662 DMG_VALIDATE(pFtr->cbRsrc <= offFtr && pFtr->offRsrc + pFtr->cbRsrc <= offFtr, ("cbRsrc=%#RX64 offRsrc=%#RX64 offFtr=%#RX64\n", pFtr->cbRsrc, pFtr->offRsrc, offFtr));
663 DMG_VALIDATE(pFtr->offRsrc < offFtr, ("offRsrc=%#RX64\n", pFtr->offRsrc));
664 DMG_VALIDATE(pFtr->cSegments <= 1, ("cSegments=%RU32\n", pFtr->cSegments));
665 DMG_VALIDATE(pFtr->iSegment == 0 || pFtr->iSegment == 1, ("iSegment=%RU32 cSegments=%RU32\n", pFtr->iSegment, pFtr->cSegments));
666 DMG_VALIDATE(pFtr->cbXml <= offFtr && pFtr->offXml + pFtr->cbXml <= offFtr, ("cbXml=%#RX64 offXml=%#RX64 offFtr=%#RX64\n", pFtr->cbXml, pFtr->offXml, offFtr));
667 DMG_VALIDATE(pFtr->offXml < offFtr, ("offXml=%#RX64\n", pFtr->offXml));
668 DMG_VALIDATE(pFtr->cbXml > 128, ("cbXml=%#RX64\n", pFtr->cbXml));
669 DMG_VALIDATE(pFtr->cbXml < 10 * _1M, ("cbXml=%#RX64\n", pFtr->cbXml));
670 DMG_VALIDATE(pFtr->u32Type == DMGUDIF_TYPE_DEVICE || pFtr->u32Type == DMGUDIF_TYPE_PARTITION, ("u32Type=%RU32\n", pFtr->u32Type));
671 DMG_VALIDATE(pFtr->cSectors != 0, ("cSectors=%#RX64\n", pFtr->cSectors));
672 fRc &= dmgUdifCkSumIsValid(&pFtr->DataCkSum, "DataCkSum");
673 fRc &= dmgUdifCkSumIsValid(&pFtr->MasterCkSum, "MasterCkSum");
674
675 return fRc;
676}
677
678
679static bool dmgBlkxIsValid(PCDMGBLKX pBlkx)
680{
681 bool fRc = true;
682
683 fRc &= dmgUdifCkSumIsValid(&pBlkx->BlkxCkSum, "BlkxCkSum");
684 DMG_VALIDATE(pBlkx->u32Magic == DMGBLKX_MAGIC, ("u32Magic=%#RX32 u32MagicExpected=%#RX32\n", pBlkx->u32Magic, DMGBLKX_MAGIC));
685 DMG_VALIDATE(pBlkx->u32Version == DMGBLKX_VERSION, ("u32Version=%#RX32 u32VersionExpected=%#RX32\n", pBlkx->u32Magic, DMGBLKX_VERSION));
686
687 return fRc;
688}
689
690/**
691 * Swaps endian from host cpu to file.
692 * @param pId The structure.
693 */
694static void dmgUdifIdHost2FileEndian(PDMGUDIFID pId)
695{
696 NOREF(pId);
697}
698
699
700/**
701 * Swaps endian from file to host cpu.
702 * @param pId The structure.
703 */
704static void dmgUdifIdFile2HostEndian(PDMGUDIFID pId)
705{
706 dmgUdifIdHost2FileEndian(pId);
707}
708
709
710/**
711 * Swaps endian.
712 * @param pCkSum The structure.
713 */
714static void dmgSwapEndianUdifCkSum(PDMGUDIFCKSUM pCkSum, uint32_t u32Kind, uint32_t cBits)
715{
716#ifdef RT_BIG_ENDIAN
717 NOREF(pCkSum);
718 NOREF(u32Kind);
719 NOREF(cBits);
720#else
721 switch (u32Kind)
722 {
723 case DMGUDIFCKSUM_NONE:
724 /* nothing to do here */
725 break;
726
727 case DMGUDIFCKSUM_CRC32:
728 Assert(cBits == 32);
729 pCkSum->u32Kind = RT_BSWAP_U32(pCkSum->u32Kind);
730 pCkSum->cBits = RT_BSWAP_U32(pCkSum->cBits);
731 pCkSum->uSum.au32[0] = RT_BSWAP_U32(pCkSum->uSum.au32[0]);
732 break;
733
734 default:
735 AssertMsgFailed(("%x\n", u32Kind));
736 break;
737 }
738 NOREF(cBits);
739#endif
740}
741
742
743/**
744 * Swaps endian from host cpu to file.
745 * @param pCkSum The structure.
746 */
747static void dmgUdifCkSumHost2FileEndian(PDMGUDIFCKSUM pCkSum)
748{
749 dmgSwapEndianUdifCkSum(pCkSum, pCkSum->u32Kind, pCkSum->cBits);
750}
751
752
753/**
754 * Swaps endian from file to host cpu.
755 * @param pCkSum The structure.
756 */
757static void dmgUdifCkSumFile2HostEndian(PDMGUDIFCKSUM pCkSum)
758{
759 dmgSwapEndianUdifCkSum(pCkSum, RT_BE2H_U32(pCkSum->u32Kind), RT_BE2H_U32(pCkSum->cBits));
760}
761
762
763/**
764 * Validates an UDIF checksum structure.
765 *
766 * @returns true if valid, false and LogRel()s on failure.
767 * @param pCkSum The checksum structure.
768 * @param pszPrefix The message prefix.
769 * @remarks This does not check the checksummed data.
770 */
771static bool dmgUdifCkSumIsValid(PCDMGUDIFCKSUM pCkSum, const char *pszPrefix)
772{
773 bool fRc = true;
774
775 switch (pCkSum->u32Kind)
776 {
777 case DMGUDIFCKSUM_NONE:
778 DMG_VALIDATE(pCkSum->cBits == 0, ("%s/NONE: cBits=%d\n", pszPrefix, pCkSum->cBits));
779 break;
780
781 case DMGUDIFCKSUM_CRC32:
782 DMG_VALIDATE(pCkSum->cBits == 32, ("%s/NONE: cBits=%d\n", pszPrefix, pCkSum->cBits));
783 break;
784
785 default:
786 DMG_VALIDATE(0, ("%s: u32Kind=%#RX32\n", pszPrefix, pCkSum->u32Kind));
787 break;
788 }
789 return fRc;
790}
791
792
793/**
794 * Internal. Flush image data to disk.
795 */
796static int dmgFlushImage(PDMGIMAGE pThis)
797{
798 int rc = VINF_SUCCESS;
799
800 if ( pThis
801 && (pThis->pStorage || pThis->hDmgFileInXar != NIL_RTVFSFILE)
802 && !(pThis->uOpenFlags & VD_OPEN_FLAGS_READONLY))
803 {
804 /** @todo handle writable files, update checksums etc. */
805 }
806
807 return rc;
808}
809
810
811/**
812 * Internal. Free all allocated space for representing an image except pThis,
813 * and optionally delete the image from disk.
814 */
815static int dmgFreeImage(PDMGIMAGE pThis, bool fDelete)
816{
817 int rc = VINF_SUCCESS;
818
819 /* Freeing a never allocated image (e.g. because the open failed) is
820 * not signalled as an error. After all nothing bad happens. */
821 if (pThis)
822 {
823 RTVfsFileRelease(pThis->hDmgFileInXar);
824 pThis->hDmgFileInXar = NIL_RTVFSFILE;
825
826 RTVfsFsStrmRelease(pThis->hXarFss);
827 pThis->hXarFss = NIL_RTVFSFSSTREAM;
828
829 if (pThis->pStorage)
830 {
831 /* No point updating the file that is deleted anyway. */
832 if (!fDelete)
833 dmgFlushImage(pThis);
834
835 rc = vdIfIoIntFileClose(pThis->pIfIoXxx, pThis->pStorage);
836 pThis->pStorage = NULL;
837 }
838
839 for (unsigned iRsrc = 0; iRsrc < RT_ELEMENTS(pThis->aRsrcs); iRsrc++)
840 for (unsigned i = 0; i < pThis->aRsrcs[iRsrc].cEntries; i++)
841 {
842 if (pThis->aRsrcs[iRsrc].aEntries[i].pbData)
843 {
844 RTMemFree(pThis->aRsrcs[iRsrc].aEntries[i].pbData);
845 pThis->aRsrcs[iRsrc].aEntries[i].pbData = NULL;
846 }
847 if (pThis->aRsrcs[iRsrc].aEntries[i].pszName)
848 {
849 RTMemFree(pThis->aRsrcs[iRsrc].aEntries[i].pszName);
850 pThis->aRsrcs[iRsrc].aEntries[i].pszName = NULL;
851 }
852 if (pThis->aRsrcs[iRsrc].aEntries[i].pszCFName)
853 {
854 RTMemFree(pThis->aRsrcs[iRsrc].aEntries[i].pszCFName);
855 pThis->aRsrcs[iRsrc].aEntries[i].pszCFName = NULL;
856 }
857 }
858
859 if (fDelete && pThis->pszFilename)
860 vdIfIoIntFileDelete(pThis->pIfIoXxx, pThis->pszFilename);
861
862 if (pThis->pvDecompExtent)
863 {
864 RTMemFree(pThis->pvDecompExtent);
865 pThis->pvDecompExtent = NULL;
866 pThis->cbDecompExtent = 0;
867 }
868
869 if (pThis->paExtents)
870 {
871 RTMemFree(pThis->paExtents);
872 pThis->paExtents = NULL;
873 }
874 }
875
876 LogFlowFunc(("returns %Rrc\n", rc));
877 return rc;
878}
879
880
881#define STARTS_WITH(pszString, szStart) \
882 ( strncmp(pszString, szStart, sizeof(szStart) - 1) == 0 )
883
884#define STARTS_WITH_WORD(pszString, szWord) \
885 ( STARTS_WITH(pszString, szWord) \
886 && !RT_C_IS_ALNUM((pszString)[sizeof(szWord) - 1]) )
887
888#define SKIP_AHEAD(psz, szWord) \
889 do { \
890 (psz) = RTStrStripL((psz) + sizeof(szWord) - 1); \
891 } while (0)
892
893#define REQUIRE_WORD(psz, szWord) \
894 do { \
895 if (!STARTS_WITH_WORD(psz, szWord)) \
896 return psz; \
897 (psz) = RTStrStripL((psz) + sizeof(szWord) - 1); \
898 } while (0)
899
900#define REQUIRE_TAG(psz, szTag) \
901 do { \
902 if (!STARTS_WITH(psz, "<" szTag ">")) \
903 return psz; \
904 (psz) = RTStrStripL((psz) + sizeof("<" szTag ">") - 1); \
905 } while (0)
906
907#define REQUIRE_TAG_NO_STRIP(psz, szTag) \
908 do { \
909 if (!STARTS_WITH(psz, "<" szTag ">")) \
910 return psz; \
911 (psz) += sizeof("<" szTag ">") - 1; \
912 } while (0)
913
914#define REQUIRE_END_TAG(psz, szTag) \
915 do { \
916 if (!STARTS_WITH(psz, "</" szTag ">")) \
917 return psz; \
918 (psz) = RTStrStripL((psz) + sizeof("</" szTag ">") - 1); \
919 } while (0)
920
921
922/**
923 * Finds the next tag end.
924 *
925 * @returns Pointer to a '>' or '\0'.
926 * @param pszCur The current position.
927 */
928static const char *dmgXmlFindTagEnd(const char *pszCur)
929{
930 /* Might want to take quoted '>' into account? */
931 char ch;
932 while ((ch = *pszCur) != '\0' && ch != '>')
933 pszCur++;
934 return pszCur;
935}
936
937
938/**
939 * Finds the end tag.
940 *
941 * Does not deal with '<tag attr="1"/>' style tags.
942 *
943 * @returns Pointer to the first char in the end tag. NULL if another tag
944 * was encountered first or if we hit the end of the file.
945 * @param ppszCur The current position (IN/OUT).
946 * @param pszTag The tag name.
947 */
948static const char *dmgXmlFindEndTag(const char **ppszCur, const char *pszTag)
949{
950 const char *psz = *ppszCur;
951 char ch;
952 while ((ch = *psz))
953 {
954 if (ch == '<')
955 {
956 size_t const cchTag = strlen(pszTag);
957 if ( psz[1] == '/'
958 && !memcmp(&psz[2], pszTag, cchTag)
959 && psz[2 + cchTag] == '>')
960 {
961 *ppszCur = psz + 2 + cchTag + 1;
962 return psz;
963 }
964 break;
965 }
966 psz++;
967 }
968 return NULL;
969}
970
971
972/**
973 * Reads a signed 32-bit value.
974 *
975 * @returns NULL on success, pointer to the offending text on failure.
976 * @param ppszCur The text position (IN/OUT).
977 * @param pi32 Where to store the value.
978 */
979static const char *dmgXmlParseS32(const char **ppszCur, int32_t *pi32)
980{
981 const char *psz = *ppszCur;
982
983 /*
984 * <string>-1</string>
985 */
986 REQUIRE_TAG_NO_STRIP(psz, "string");
987
988 char *pszNext;
989 int rc = RTStrToInt32Ex(psz, &pszNext, 0, pi32);
990 if (rc != VWRN_TRAILING_CHARS)
991 return *ppszCur;
992 psz = pszNext;
993
994 REQUIRE_END_TAG(psz, "string");
995 *ppszCur = psz;
996 return NULL;
997}
998
999
1000/**
1001 * Reads an unsigned 32-bit value.
1002 *
1003 * @returns NULL on success, pointer to the offending text on failure.
1004 * @param ppszCur The text position (IN/OUT).
1005 * @param pu32 Where to store the value.
1006 */
1007static const char *dmgXmlParseU32(const char **ppszCur, uint32_t *pu32)
1008{
1009 const char *psz = *ppszCur;
1010
1011 /*
1012 * <string>0x00ff</string>
1013 */
1014 REQUIRE_TAG_NO_STRIP(psz, "string");
1015
1016 char *pszNext;
1017 int rc = RTStrToUInt32Ex(psz, &pszNext, 0, pu32);
1018 if (rc != VWRN_TRAILING_CHARS)
1019 return *ppszCur;
1020 psz = pszNext;
1021
1022 REQUIRE_END_TAG(psz, "string");
1023 *ppszCur = psz;
1024 return NULL;
1025}
1026
1027
1028/**
1029 * Reads a string value.
1030 *
1031 * @returns NULL on success, pointer to the offending text on failure.
1032 * @param ppszCur The text position (IN/OUT).
1033 * @param ppszString Where to store the pointer to the string. The caller
1034 * must free this using RTMemFree.
1035 */
1036static const char *dmgXmlParseString(const char **ppszCur, char **ppszString)
1037{
1038 const char *psz = *ppszCur;
1039
1040 /*
1041 * <string>Driver Descriptor Map (DDM : 0)</string>
1042 */
1043 REQUIRE_TAG_NO_STRIP(psz, "string");
1044
1045 const char *pszStart = psz;
1046 const char *pszEnd = dmgXmlFindEndTag(&psz, "string");
1047 if (!pszEnd)
1048 return *ppszCur;
1049 psz = RTStrStripL(psz);
1050
1051 *ppszString = (char *)RTMemDupEx(pszStart, pszEnd - pszStart, 1);
1052 if (!*ppszString)
1053 return *ppszCur;
1054
1055 *ppszCur = psz;
1056 return NULL;
1057}
1058
1059
1060/**
1061 * Parses the BASE-64 coded data tags.
1062 *
1063 * @returns NULL on success, pointer to the offending text on failure.
1064 * @param ppszCur The text position (IN/OUT).
1065 * @param ppbData Where to store the pointer to the data we've read. The
1066 * caller must free this using RTMemFree.
1067 * @param pcbData The number of bytes we're returning.
1068 */
1069static const char *dmgXmlParseData(const char **ppszCur, uint8_t **ppbData, size_t *pcbData)
1070{
1071 const char *psz = *ppszCur;
1072
1073 /*
1074 * <data> AAAAA... </data>
1075 */
1076 REQUIRE_TAG(psz, "data");
1077
1078 const char *pszStart = psz;
1079 ssize_t cbData = RTBase64DecodedSize(pszStart, (char **)&psz);
1080 if (cbData == -1)
1081 return *ppszCur;
1082 const char *pszEnd = psz;
1083
1084 REQUIRE_END_TAG(psz, "data");
1085
1086 *ppbData = (uint8_t *)RTMemAlloc(cbData);
1087 if (!*ppbData)
1088 return *ppszCur;
1089 char *pszIgnored;
1090 int rc = RTBase64Decode(pszStart, *ppbData, cbData, pcbData, &pszIgnored);
1091 if (RT_FAILURE(rc))
1092 {
1093 RTMemFree(*ppbData);
1094 *ppbData = NULL;
1095 return *ppszCur;
1096 }
1097
1098 *ppszCur = psz;
1099 return NULL;
1100}
1101
1102
1103/**
1104 * Parses the XML resource-fork in a rather presumptive manner.
1105 *
1106 * This function is supposed to construct the DMG::aRsrcs instance data
1107 * parts.
1108 *
1109 * @returns NULL on success, pointer to the problematic text on failure.
1110 * @param pThis The DMG instance data.
1111 * @param pszXml The XML text to parse, UTF-8.
1112 * @param cch The size of the XML text.
1113 */
1114static const char *dmgOpenXmlToRsrc(PDMGIMAGE pThis, char const *pszXml)
1115{
1116 const char *psz = pszXml;
1117
1118 /*
1119 * Verify the ?xml, !DOCTYPE and plist tags.
1120 */
1121 SKIP_AHEAD(psz, "");
1122
1123 /* <?xml version="1.0" encoding="UTF-8"?> */
1124 REQUIRE_WORD(psz, "<?xml");
1125 while (*psz != '?')
1126 {
1127 if (!*psz)
1128 return psz;
1129 if (STARTS_WITH_WORD(psz, "version="))
1130 {
1131 SKIP_AHEAD(psz, "version=");
1132 REQUIRE_WORD(psz, "\"1.0\"");
1133 }
1134 else if (STARTS_WITH_WORD(psz, "encoding="))
1135 {
1136 SKIP_AHEAD(psz, "encoding=");
1137 REQUIRE_WORD(psz, "\"UTF-8\"");
1138 }
1139 else
1140 return psz;
1141 }
1142 SKIP_AHEAD(psz, "?>");
1143
1144 /* <!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd"> */
1145 REQUIRE_WORD(psz, "<!DOCTYPE");
1146 REQUIRE_WORD(psz, "plist");
1147 REQUIRE_WORD(psz, "PUBLIC");
1148 psz = dmgXmlFindTagEnd(psz);
1149 REQUIRE_WORD(psz, ">");
1150
1151 /* <plist version="1.0"> */
1152 REQUIRE_WORD(psz, "<plist");
1153 REQUIRE_WORD(psz, "version=");
1154 REQUIRE_WORD(psz, "\"1.0\"");
1155 REQUIRE_WORD(psz, ">");
1156
1157 /*
1158 * Descend down to the 'resource-fork' dictionary.
1159 * ASSUME it's the only top level dictionary.
1160 */
1161 /* <dict> <key>resource-fork</key> */
1162 REQUIRE_TAG(psz, "dict");
1163 REQUIRE_WORD(psz, "<key>resource-fork</key>");
1164
1165 /*
1166 * Parse the keys in the resource-fork dictionary.
1167 * ASSUME that there are just two, 'blkx' and 'plst'.
1168 */
1169 REQUIRE_TAG(psz, "dict");
1170 while (!STARTS_WITH_WORD(psz, "</dict>"))
1171 {
1172 /*
1173 * Parse the key and Create the resource-fork entry.
1174 */
1175 unsigned iRsrc;
1176 if (STARTS_WITH_WORD(psz, "<key>blkx</key>"))
1177 {
1178 REQUIRE_WORD(psz, "<key>blkx</key>");
1179 iRsrc = DMG_RSRC_IDX_BLKX;
1180 strcpy(&pThis->aRsrcs[iRsrc].szName[0], "blkx");
1181 }
1182 else if (STARTS_WITH_WORD(psz, "<key>plst</key>"))
1183 {
1184 REQUIRE_WORD(psz, "<key>plst</key>");
1185 iRsrc = DMG_RSRC_IDX_PLST;
1186 strcpy(&pThis->aRsrcs[iRsrc].szName[0], "plst");
1187 }
1188 else
1189 {
1190 SKIP_AHEAD(psz, "</array>");
1191 continue;
1192 }
1193
1194
1195 /*
1196 * Descend into the array and add the elements to the resource entry.
1197 */
1198 /* <array> */
1199 REQUIRE_TAG(psz, "array");
1200 while (!STARTS_WITH_WORD(psz, "</array>"))
1201 {
1202 REQUIRE_TAG(psz, "dict");
1203 uint32_t i = pThis->aRsrcs[iRsrc].cEntries;
1204 if (i == RT_ELEMENTS(pThis->aRsrcs[iRsrc].aEntries))
1205 return psz;
1206
1207 while (!STARTS_WITH_WORD(psz, "</dict>"))
1208 {
1209
1210 /* switch on the key. */
1211 const char *pszErr;
1212 if (STARTS_WITH_WORD(psz, "<key>Attributes</key>"))
1213 {
1214 REQUIRE_WORD(psz, "<key>Attributes</key>");
1215 pszErr = dmgXmlParseU32(&psz, &pThis->aRsrcs[iRsrc].aEntries[i].fAttributes);
1216 }
1217 else if (STARTS_WITH_WORD(psz, "<key>ID</key>"))
1218 {
1219 REQUIRE_WORD(psz, "<key>ID</key>");
1220 pszErr = dmgXmlParseS32(&psz, &pThis->aRsrcs[iRsrc].aEntries[i].iId);
1221 }
1222 else if (STARTS_WITH_WORD(psz, "<key>Name</key>"))
1223 {
1224 REQUIRE_WORD(psz, "<key>Name</key>");
1225 pszErr = dmgXmlParseString(&psz, &pThis->aRsrcs[iRsrc].aEntries[i].pszName);
1226 }
1227 else if (STARTS_WITH_WORD(psz, "<key>CFName</key>"))
1228 {
1229 REQUIRE_WORD(psz, "<key>CFName</key>");
1230 pszErr = dmgXmlParseString(&psz, &pThis->aRsrcs[iRsrc].aEntries[i].pszCFName);
1231 }
1232 else if (STARTS_WITH_WORD(psz, "<key>Data</key>"))
1233 {
1234 REQUIRE_WORD(psz, "<key>Data</key>");
1235 pszErr = dmgXmlParseData(&psz, &pThis->aRsrcs[iRsrc].aEntries[i].pbData, &pThis->aRsrcs[iRsrc].aEntries[i].cbData);
1236 }
1237 else
1238 pszErr = psz;
1239 if (pszErr)
1240 return pszErr;
1241 } /* while not </dict> */
1242 REQUIRE_END_TAG(psz, "dict");
1243
1244 pThis->aRsrcs[iRsrc].cEntries++;
1245 } /* while not </array> */
1246 REQUIRE_END_TAG(psz, "array");
1247
1248 } /* while not </dict> */
1249 REQUIRE_END_TAG(psz, "dict");
1250
1251 /*
1252 * ASSUMING there is only the 'resource-fork', we'll now see the end of
1253 * the outer dict, plist and text.
1254 */
1255 /* </dict> </plist> */
1256 REQUIRE_END_TAG(psz, "dict");
1257 REQUIRE_END_TAG(psz, "plist");
1258
1259 /* the end */
1260 if (*psz)
1261 return psz;
1262
1263 return NULL;
1264}
1265
1266#undef REQUIRE_END_TAG
1267#undef REQUIRE_TAG_NO_STRIP
1268#undef REQUIRE_TAG
1269#undef REQUIRE_WORD
1270#undef SKIP_AHEAD
1271#undef STARTS_WITH_WORD
1272#undef STARTS_WITH
1273
1274/**
1275 * Returns the data attached to a resource.
1276 *
1277 * @returns VBox status code.
1278 * @param pThis The DMG instance data.
1279 * @param pcszRsrcName Name of the resource to get.
1280 */
1281static int dmgGetRsrcData(PDMGIMAGE pThis, const char *pcszRsrcName,
1282 PCDMGUDIFRSRCARRAY *ppcRsrc)
1283{
1284 int rc = VERR_NOT_FOUND;
1285
1286 for (unsigned i = 0; i < RT_ELEMENTS(pThis->aRsrcs); i++)
1287 {
1288 if (!strcmp(pThis->aRsrcs[i].szName, pcszRsrcName))
1289 {
1290 *ppcRsrc = &pThis->aRsrcs[i];
1291 rc = VINF_SUCCESS;
1292 break;
1293 }
1294 }
1295
1296 return rc;
1297}
1298
1299/**
1300 * Creates a new extent from the given blkx descriptor.
1301 *
1302 * @returns VBox status code.
1303 * @param pThis DMG instance data.
1304 * @param uSectorPart First sector the partition owning the blkx descriptor has.
1305 * @param pBlkxDesc The blkx descriptor.
1306 */
1307static int dmgExtentCreateFromBlkxDesc(PDMGIMAGE pThis, uint64_t uSectorPart, PDMGBLKXDESC pBlkxDesc)
1308{
1309 int rc = VINF_SUCCESS;
1310 DMGEXTENTTYPE enmExtentTypeNew;
1311 PDMGEXTENT pExtentNew = NULL;
1312
1313 if (pBlkxDesc->u32Type == DMGBLKXDESC_TYPE_RAW)
1314 enmExtentTypeNew = DMGEXTENTTYPE_RAW;
1315 else if (pBlkxDesc->u32Type == DMGBLKXDESC_TYPE_IGNORE)
1316 enmExtentTypeNew = DMGEXTENTTYPE_ZERO;
1317 else if (pBlkxDesc->u32Type == DMGBLKXDESC_TYPE_ZLIB)
1318 enmExtentTypeNew = DMGEXTENTTYPE_COMP_ZLIB;
1319 else
1320 {
1321 AssertMsgFailed(("This method supports only raw or zero extents!\n"));
1322 return VERR_NOT_SUPPORTED;
1323 }
1324
1325 /** @todo: Merge raw extents if possible to save memory. */
1326#if 0
1327 pExtentNew = pThis->pExtentLast;
1328 if ( pExtentNew
1329 && pExtentNew->enmType == enmExtentTypeNew
1330 && enmExtentTypeNew == DMGEXTENTTYPE_RAW
1331 && pExtentNew->uSectorExtent + pExtentNew->cSectorsExtent == offDevice + pBlkxDesc->u64SectorStart * DMG_SECTOR_SIZE;
1332 && pExtentNew->offFileStart + pExtentNew->cbExtent == pBlkxDesc->offData)
1333 {
1334 /* Increase the last extent. */
1335 pExtentNew->cbExtent += pBlkxDesc->cbData;
1336 }
1337 else
1338#endif
1339 {
1340 if (pThis->cExtentsMax == pThis->cExtents)
1341 {
1342 pThis->cExtentsMax += 64;
1343
1344 /* Increase the array. */
1345 PDMGEXTENT paExtentsNew = (PDMGEXTENT)RTMemRealloc(pThis->paExtents, sizeof(DMGEXTENT) * pThis->cExtentsMax);
1346 if (!paExtentsNew)
1347 {
1348 rc = VERR_NO_MEMORY;
1349 pThis->cExtentsMax -= 64;
1350 }
1351 else
1352 pThis->paExtents = paExtentsNew;
1353 }
1354
1355 if (RT_SUCCESS(rc))
1356 {
1357 pExtentNew = &pThis->paExtents[pThis->cExtents++];
1358
1359 pExtentNew->enmType = enmExtentTypeNew;
1360 pExtentNew->uSectorExtent = uSectorPart + pBlkxDesc->u64SectorStart;
1361 pExtentNew->cSectorsExtent = pBlkxDesc->u64SectorCount;
1362 pExtentNew->offFileStart = pBlkxDesc->offData;
1363 pExtentNew->cbFile = pBlkxDesc->cbData;
1364 }
1365 }
1366
1367 return rc;
1368}
1369
1370/**
1371 * Find the extent for the given sector number.
1372 */
1373static PDMGEXTENT dmgExtentGetFromOffset(PDMGIMAGE pThis, uint64_t uSector)
1374{
1375 /*
1376 * We assume that the array is ordered from lower to higher sector
1377 * numbers.
1378 * This makes it possible to bisect the array to find the extent
1379 * faster than using a linked list.
1380 */
1381 PDMGEXTENT pExtent = NULL;
1382 unsigned idxCur = pThis->idxExtentLast;
1383 unsigned idxMax = pThis->cExtents;
1384 unsigned idxMin = 0;
1385
1386 while (idxMin < idxMax)
1387 {
1388 PDMGEXTENT pExtentCur = &pThis->paExtents[idxCur];
1389
1390 /* Determine the search direction. */
1391 if (uSector < pExtentCur->uSectorExtent)
1392 {
1393 /* Search left from the current extent. */
1394 idxMax = idxCur;
1395 }
1396 else if (uSector >= pExtentCur->uSectorExtent + pExtentCur->cSectorsExtent)
1397 {
1398 /* Search right from the current extent. */
1399 idxMin = idxCur;
1400 }
1401 else
1402 {
1403 /* The sector lies in the extent, stop searching. */
1404 pExtent = pExtentCur;
1405 break;
1406 }
1407
1408 idxCur = idxMin + (idxMax - idxMin) / 2;
1409 }
1410
1411 if (pExtent)
1412 pThis->idxExtentLast = idxCur;
1413
1414 return pExtent;
1415}
1416
1417/**
1418 * Goes through the BLKX structure and creates the necessary extents.
1419 */
1420static int dmgBlkxParse(PDMGIMAGE pThis, PDMGBLKX pBlkx)
1421{
1422 int rc = VINF_SUCCESS;
1423 PDMGBLKXDESC pBlkxDesc = (PDMGBLKXDESC)(pBlkx + 1);
1424
1425 for (unsigned i = 0; i < pBlkx->cBlocksRunCount; i++)
1426 {
1427 dmgBlkxDescFile2HostEndian(pBlkxDesc);
1428
1429 switch (pBlkxDesc->u32Type)
1430 {
1431 case DMGBLKXDESC_TYPE_RAW:
1432 case DMGBLKXDESC_TYPE_IGNORE:
1433 case DMGBLKXDESC_TYPE_ZLIB:
1434 {
1435 rc = dmgExtentCreateFromBlkxDesc(pThis, pBlkx->cSectornumberFirst, pBlkxDesc);
1436 break;
1437 }
1438 case DMGBLKXDESC_TYPE_COMMENT:
1439 case DMGBLKXDESC_TYPE_TERMINATOR:
1440 break;
1441 default:
1442 rc = VERR_VD_DMG_INVALID_HEADER;
1443 break;
1444 }
1445
1446 if ( pBlkxDesc->u32Type == DMGBLKXDESC_TYPE_TERMINATOR
1447 || RT_FAILURE(rc))
1448 break;
1449
1450 pBlkxDesc++;
1451 }
1452
1453 return rc;
1454}
1455
1456
1457/**
1458 * Worker for dmgOpenImage that tries to open a DMG inside a XAR file.
1459 *
1460 * We'll select the first .dmg inside the archive that we can get a file
1461 * interface to.
1462 *
1463 * @returns VBox status code.
1464 * @param fOpen Flags for defining the open type.
1465 * @param pVDIfIoInt The internal VD I/O interface to use.
1466 * @param pvStorage The storage pointer that goes with @a pVDIfsIo.
1467 * @param pszFilename The input filename, optional.
1468 * @param phXarFss Where to return the XAR file system stream handle on
1469 * success
1470 * @param phDmgFileInXar Where to return the VFS handle to the DMG file
1471 * within the XAR image on success.
1472 *
1473 * @remarks Not using the PDMGIMAGE structure directly here because the function
1474 * is being in serveral places.
1475 */
1476static int dmgOpenImageWithinXar(uint32_t fOpen, PVDINTERFACEIOINT pVDIfIoInt, void *pvStorage, const char *pszFilename,
1477 PRTVFSFSSTREAM phXarFss, PRTVFSFILE phDmgFileInXar)
1478{
1479 /*
1480 * Open the XAR file stream.
1481 */
1482 RTVFSFILE hVfsFile;
1483#ifdef VBOX_WITH_DIRECT_XAR_ACCESS
1484 int rc = RTVfsFileOpenNormal(pszFilename, fOpen, &hVfsFile);
1485#else
1486 int rc = VDIfCreateVfsFile(NULL, pVDIfIoInt, pvStorage, fOpen, &hVfsFile);
1487#endif
1488 if (RT_FAILURE(rc))
1489 return rc;
1490
1491 RTVFSIOSTREAM hVfsIos = RTVfsFileToIoStream(hVfsFile);
1492 RTVfsFileRelease(hVfsFile);
1493
1494 RTVFSFSSTREAM hXarFss;
1495 rc = RTZipXarFsStreamFromIoStream(hVfsIos, 0 /*fFlags*/, &hXarFss);
1496 RTVfsIoStrmRelease(hVfsIos);
1497 if (RT_FAILURE(rc))
1498 return rc;
1499
1500 /*
1501 * Look for a DMG in the stream that we can use.
1502 */
1503 for (;;)
1504 {
1505 char *pszName;
1506 RTVFSOBJTYPE enmType;
1507 RTVFSOBJ hVfsObj;
1508 rc = RTVfsFsStrmNext(hXarFss, &pszName, &enmType, &hVfsObj);
1509 if (RT_FAILURE(rc))
1510 break;
1511
1512 /* It must be a file object so it can be seeked, this also implies that
1513 it's uncompressed. Then it must have the .dmg suffix. */
1514 if (enmType == RTVFSOBJTYPE_FILE)
1515 {
1516 size_t cchName = strlen(pszName);
1517 const char *pszSuff = pszName + cchName - 4;
1518 if ( cchName >= 4
1519 && pszSuff[0] == '.'
1520 && (pszSuff[1] == 'd' || pszSuff[1] == 'D')
1521 && (pszSuff[2] == 'm' || pszSuff[2] == 'M')
1522 && (pszSuff[3] == 'g' || pszSuff[3] == 'G'))
1523 {
1524 RTVFSFILE hDmgFileInXar = RTVfsObjToFile(hVfsObj);
1525 AssertBreakStmt(hDmgFileInXar != NIL_RTVFSFILE, rc = VERR_INTERNAL_ERROR_3);
1526
1527 if (pszFilename)
1528 DMG_PRINTF(("DMG: Using '%s' within XAR file '%s'...\n", pszName, pszFilename));
1529 *phXarFss = hXarFss;
1530 *phDmgFileInXar = hDmgFileInXar;
1531
1532 RTStrFree(pszName);
1533 RTVfsObjRelease(hVfsObj);
1534
1535 return VINF_SUCCESS;
1536 }
1537 }
1538
1539 /* Release the current return values. */
1540 RTStrFree(pszName);
1541 RTVfsObjRelease(hVfsObj);
1542 }
1543
1544 /* Not found or some kind of error. */
1545 RTVfsFsStrmRelease(hXarFss);
1546 if (rc == VERR_EOF)
1547 rc = VERR_VD_DMG_NOT_FOUND_INSIDE_XAR;
1548 AssertStmt(RT_FAILURE_NP(rc), rc = VERR_INTERNAL_ERROR_4);
1549 return rc;
1550}
1551
1552
1553/**
1554 * Worker for dmgOpen that reads in and validates all the necessary
1555 * structures from the image.
1556 *
1557 * @returns VBox status code.
1558 * @param pThis The DMG instance data.
1559 * @param uOpenFlags Flags for defining the open type.
1560 */
1561static DECLCALLBACK(int) dmgOpenImage(PDMGIMAGE pThis, unsigned uOpenFlags)
1562{
1563 pThis->uOpenFlags = uOpenFlags;
1564
1565 pThis->pIfError = VDIfErrorGet(pThis->pVDIfsDisk);
1566 pThis->pIfIoXxx = VDIfIoIntGet(pThis->pVDIfsImage);
1567 pThis->hDmgFileInXar = NIL_RTVFSFILE;
1568 pThis->hXarFss = NIL_RTVFSFSSTREAM;
1569 AssertPtrReturn(pThis->pIfIoXxx, VERR_INVALID_PARAMETER);
1570
1571 int rc = vdIfIoIntFileOpen(pThis->pIfIoXxx, pThis->pszFilename,
1572 VDOpenFlagsToFileOpenFlags(uOpenFlags, false /* fCreate */),
1573 &pThis->pStorage);
1574 if (RT_FAILURE(rc))
1575 {
1576 /* Do NOT signal an appropriate error here, as the VD layer has the
1577 * choice of retrying the open if it failed. */
1578 return rc;
1579 }
1580
1581 /*
1582 * Check for XAR archive.
1583 */
1584 uint32_t u32XarMagic;
1585 rc = dmgWrapFileReadSync(pThis, 0, &u32XarMagic, sizeof(u32XarMagic));
1586 if (RT_FAILURE(rc))
1587 return rc;
1588 if (u32XarMagic == XAR_HEADER_MAGIC)
1589 {
1590 rc = dmgOpenImageWithinXar(VDOpenFlagsToFileOpenFlags(uOpenFlags, false /* fCreate */),
1591 pThis->pIfIoXxx,
1592 pThis->pStorage,
1593 pThis->pszFilename,
1594 &pThis->hXarFss, &pThis->hDmgFileInXar);
1595 if (RT_FAILURE(rc))
1596 return rc;
1597#ifdef VBOX_WITH_DIRECT_XAR_ACCESS
1598 vdIfIoIntFileClose(pThis->pIfIoXxx, pThis->pStorage);
1599 pThis->pStorage = NULL;
1600#endif
1601 }
1602#if 0 /* This is for testing whether the VFS wrappers actually works. */
1603 else
1604 {
1605 rc = RTVfsFileOpenNormal(pThis->pszFilename, VDOpenFlagsToFileOpenFlags(uOpenFlags, false /* fCreate */),
1606 &pThis->hDmgFileInXar);
1607 if (RT_FAILURE(rc))
1608 return rc;
1609 vdIfIoIntFileClose(pThis->pIfIoXxx, pThis->pStorage);
1610 pThis->pStorage = NULL;
1611 }
1612#endif
1613
1614 /*
1615 * Read the footer.
1616 */
1617 rc = dmgWrapFileGetSize(pThis, &pThis->cbFile);
1618 if (RT_FAILURE(rc))
1619 return rc;
1620 if (pThis->cbFile < 1024)
1621 return VERR_VD_DMG_INVALID_HEADER;
1622 rc = dmgWrapFileReadSync(pThis, pThis->cbFile - sizeof(pThis->Ftr), &pThis->Ftr, sizeof(pThis->Ftr));
1623 if (RT_FAILURE(rc))
1624 return rc;
1625 dmgUdifFtrFile2HostEndian(&pThis->Ftr);
1626
1627 /*
1628 * Do we recognize the footer structure? If so, is it valid?
1629 */
1630 if (pThis->Ftr.u32Magic != DMGUDIF_MAGIC)
1631 return VERR_VD_DMG_INVALID_HEADER;
1632 if (pThis->Ftr.u32Version != DMGUDIF_VER_CURRENT)
1633 return VERR_VD_DMG_INVALID_HEADER;
1634 if (pThis->Ftr.cbFooter != sizeof(pThis->Ftr))
1635 return VERR_VD_DMG_INVALID_HEADER;
1636
1637 if (!dmgUdifFtrIsValid(&pThis->Ftr, pThis->cbFile - sizeof(pThis->Ftr)))
1638 {
1639 DMG_PRINTF(("Bad DMG: '%s' cbFile=%RTfoff\n", pThis->pszFilename, pThis->cbFile));
1640 return VERR_VD_DMG_INVALID_HEADER;
1641 }
1642
1643 pThis->cbSize = pThis->Ftr.cSectors * DMG_SECTOR_SIZE;
1644
1645 /*
1646 * Read and parse the XML portion.
1647 */
1648 size_t cchXml = (size_t)pThis->Ftr.cbXml;
1649 char *pszXml = (char *)RTMemAlloc(cchXml + 1);
1650 if (!pszXml)
1651 return VERR_NO_MEMORY;
1652 rc = dmgWrapFileReadSync(pThis, pThis->Ftr.offXml, pszXml, cchXml);
1653 if (RT_SUCCESS(rc))
1654 {
1655 pszXml[cchXml] = '\0';
1656 const char *pszError = dmgOpenXmlToRsrc(pThis, pszXml);
1657 if (!pszError)
1658 {
1659 PCDMGUDIFRSRCARRAY pRsrcBlkx = NULL;
1660
1661 rc = dmgGetRsrcData(pThis, "blkx", &pRsrcBlkx);
1662 if (RT_SUCCESS(rc))
1663 {
1664 for (unsigned idxBlkx = 0; idxBlkx < pRsrcBlkx->cEntries; idxBlkx++)
1665 {
1666 PDMGBLKX pBlkx = NULL;
1667
1668 if (pRsrcBlkx->aEntries[idxBlkx].cbData < sizeof(DMGBLKX))
1669 {
1670 rc = VERR_VD_DMG_INVALID_HEADER;
1671 break;
1672 }
1673
1674 pBlkx = (PDMGBLKX)RTMemAllocZ(pRsrcBlkx->aEntries[idxBlkx].cbData);
1675 if (!pBlkx)
1676 {
1677 rc = VERR_NO_MEMORY;
1678 break;
1679 }
1680
1681 memcpy(pBlkx, pRsrcBlkx->aEntries[idxBlkx].pbData, pRsrcBlkx->aEntries[idxBlkx].cbData);
1682
1683 dmgBlkxFile2HostEndian(pBlkx);
1684
1685 if ( dmgBlkxIsValid(pBlkx)
1686 && pRsrcBlkx->aEntries[idxBlkx].cbData == pBlkx->cBlocksRunCount * sizeof(DMGBLKXDESC) + sizeof(DMGBLKX))
1687 rc = dmgBlkxParse(pThis, pBlkx);
1688 else
1689 rc = VERR_VD_DMG_INVALID_HEADER;
1690
1691 RTMemFree(pBlkx);
1692
1693 if (RT_FAILURE(rc))
1694 break;
1695 }
1696 }
1697 else
1698 rc = VERR_VD_DMG_INVALID_HEADER;
1699 }
1700 else
1701 {
1702 DMG_PRINTF(("**** XML DUMP BEGIN ***\n%s\n**** XML DUMP END ****\n", pszXml));
1703 DMG_PRINTF(("**** Bad XML at %#lx (%lu) ***\n%.256s\n**** Bad XML END ****\n",
1704 (unsigned long)(pszError - pszXml), (unsigned long)(pszError - pszXml), pszError));
1705 rc = VERR_VD_DMG_XML_PARSE_ERROR;
1706 }
1707 }
1708 RTMemFree(pszXml);
1709
1710 if (RT_FAILURE(rc))
1711 dmgFreeImage(pThis, false);
1712 return rc;
1713}
1714
1715
1716/** @interface_method_impl{VBOXHDDBACKEND,pfnCheckIfValid} */
1717static DECLCALLBACK(int) dmgCheckIfValid(const char *pszFilename, PVDINTERFACE pVDIfsDisk,
1718 PVDINTERFACE pVDIfsImage, VDTYPE *penmType)
1719{
1720 LogFlowFunc(("pszFilename=\"%s\" pVDIfsDisk=%#p pVDIfsImage=%#p penmType=%#p\n",
1721 pszFilename, pVDIfsDisk, pVDIfsImage, penmType));
1722
1723 PVDINTERFACEIOINT pIfIo = VDIfIoIntGet(pVDIfsImage);
1724 AssertPtrReturn(pIfIo, VERR_INVALID_PARAMETER);
1725
1726 /*
1727 * Open the file and check for XAR.
1728 */
1729 PVDIOSTORAGE pStorage = NULL;
1730 int rc = vdIfIoIntFileOpen(pIfIo, pszFilename,
1731 VDOpenFlagsToFileOpenFlags(VD_OPEN_FLAGS_READONLY, false /* fCreate */),
1732 &pStorage);
1733 if (RT_FAILURE(rc))
1734 {
1735 LogFlowFunc(("returns %Rrc (error opening file)\n", rc));
1736 return rc;
1737 }
1738
1739 /*
1740 * Check for XAR file.
1741 */
1742 RTVFSFSSTREAM hXarFss = NIL_RTVFSFSSTREAM;
1743 RTVFSFILE hDmgFileInXar = NIL_RTVFSFILE;
1744 uint32_t u32XarMagic;
1745 rc = vdIfIoIntFileReadSync(pIfIo, pStorage, 0, &u32XarMagic, sizeof(u32XarMagic));
1746 if ( RT_SUCCESS(rc)
1747 && u32XarMagic == XAR_HEADER_MAGIC)
1748 {
1749 rc = dmgOpenImageWithinXar(RTFILE_O_OPEN | RTFILE_O_READ | RTFILE_O_DENY_WRITE,
1750 pIfIo, pStorage, pszFilename,
1751 &hXarFss, &hDmgFileInXar);
1752 if (RT_FAILURE(rc))
1753 return rc;
1754 }
1755
1756 /*
1757 * Read the DMG footer.
1758 */
1759 uint64_t cbFile;
1760 if (hDmgFileInXar == NIL_RTVFSFILE)
1761 rc = vdIfIoIntFileGetSize(pIfIo, pStorage, &cbFile);
1762 else
1763 rc = RTVfsFileGetSize(hDmgFileInXar, &cbFile);
1764 if (RT_SUCCESS(rc))
1765 {
1766 DMGUDIF Ftr;
1767 uint64_t offFtr = cbFile - sizeof(Ftr);
1768 if (hDmgFileInXar == NIL_RTVFSFILE)
1769 rc = vdIfIoIntFileReadSync(pIfIo, pStorage, offFtr, &Ftr, sizeof(Ftr));
1770 else
1771 rc = RTVfsFileReadAt(hDmgFileInXar, offFtr, &Ftr, sizeof(Ftr), NULL);
1772 if (RT_SUCCESS(rc))
1773 {
1774 /*
1775 * Do we recognize this stuff? Does it look valid?
1776 */
1777 if ( Ftr.u32Magic == RT_H2BE_U32_C(DMGUDIF_MAGIC)
1778 && Ftr.u32Version == RT_H2BE_U32_C(DMGUDIF_VER_CURRENT)
1779 && Ftr.cbFooter == RT_H2BE_U32_C(sizeof(Ftr)))
1780 {
1781 dmgUdifFtrFile2HostEndian(&Ftr);
1782 if (dmgUdifFtrIsValid(&Ftr, offFtr))
1783 {
1784 rc = VINF_SUCCESS;
1785 *penmType = VDTYPE_DVD;
1786 }
1787 else
1788 {
1789 DMG_PRINTF(("Bad DMG: '%s' offFtr=%RTfoff\n", pszFilename, offFtr));
1790 rc = VERR_VD_DMG_INVALID_HEADER;
1791 }
1792 }
1793 else
1794 rc = VERR_VD_DMG_INVALID_HEADER;
1795 }
1796 }
1797 else
1798 rc = VERR_VD_DMG_INVALID_HEADER;
1799
1800 /* Clean up. */
1801 RTVfsFileRelease(hDmgFileInXar);
1802 RTVfsFsStrmRelease(hXarFss);
1803 vdIfIoIntFileClose(pIfIo, pStorage);
1804
1805 LogFlowFunc(("returns %Rrc\n", rc));
1806 return rc;
1807}
1808
1809/** @interface_method_impl{VBOXHDDBACKEND,pfnOpen} */
1810static DECLCALLBACK(int) dmgOpen(const char *pszFilename, unsigned uOpenFlags,
1811 PVDINTERFACE pVDIfsDisk, PVDINTERFACE pVDIfsImage,
1812 VDTYPE enmType, void **ppBackendData)
1813{
1814 LogFlowFunc(("pszFilename=\"%s\" uOpenFlags=%#x pVDIfsDisk=%#p pVDIfsImage=%#p enmType=%u ppBackendData=%#p\n", pszFilename, uOpenFlags, pVDIfsDisk, pVDIfsImage, enmType, ppBackendData));
1815
1816 NOREF(enmType); /**< @todo r=klaus make use of the type info. */
1817
1818 /* Check open flags. All valid flags are (in principle) supported. */
1819 AssertReturn(!(uOpenFlags & ~VD_OPEN_FLAGS_MASK), VERR_INVALID_PARAMETER);
1820
1821 /* Check remaining arguments. */
1822 AssertPtrReturn(pszFilename, VERR_INVALID_POINTER);
1823 AssertReturn(*pszFilename, VERR_INVALID_PARAMETER);
1824
1825 /*
1826 * Reject combinations we don't currently support.
1827 *
1828 * There is no point in being paranoid about the input here as we're just a
1829 * simple backend and can expect the caller to be the only user and already
1830 * have validate what it passes thru to us.
1831 */
1832 if ( !(uOpenFlags & VD_OPEN_FLAGS_READONLY)
1833 || (uOpenFlags & VD_OPEN_FLAGS_ASYNC_IO))
1834 {
1835 LogFlowFunc(("Unsupported flag(s): %#x\n", uOpenFlags));
1836 return VERR_INVALID_PARAMETER;
1837 }
1838
1839 /*
1840 * Create the basic instance data structure and open the file,
1841 * then hand it over to a worker function that does all the rest.
1842 */
1843 int rc = VERR_NO_MEMORY;
1844 PDMGIMAGE pThis = (PDMGIMAGE)RTMemAllocZ(sizeof(*pThis));
1845 if (pThis)
1846 {
1847 pThis->pszFilename = pszFilename;
1848 pThis->pStorage = NULL;
1849 pThis->pVDIfsDisk = pVDIfsDisk;
1850 pThis->pVDIfsImage = pVDIfsImage;
1851
1852 rc = dmgOpenImage(pThis, uOpenFlags);
1853 if (RT_SUCCESS(rc))
1854 *ppBackendData = pThis;
1855 else
1856 RTMemFree(pThis);
1857 }
1858
1859 LogFlowFunc(("returns %Rrc (pBackendData=%#p)\n", rc, *ppBackendData));
1860 return rc;
1861}
1862
1863/** @interface_method_impl{VBOXHDDBACKEND,pfnCreate} */
1864static DECLCALLBACK(int) dmgCreate(const char *pszFilename, uint64_t cbSize,
1865 unsigned uImageFlags, const char *pszComment,
1866 PCVDGEOMETRY pPCHSGeometry, PCVDGEOMETRY pLCHSGeometry,
1867 PCRTUUID pUuid, unsigned uOpenFlags,
1868 unsigned uPercentStart, unsigned uPercentSpan,
1869 PVDINTERFACE pVDIfsDisk, PVDINTERFACE pVDIfsImage,
1870 PVDINTERFACE pVDIfsOperation, VDTYPE enmType,
1871 void **ppBackendData)
1872{
1873 LogFlowFunc(("pszFilename=\"%s\" cbSize=%llu uImageFlags=%#x pszComment=\"%s\" pPCHSGeometry=%#p pLCHSGeometry=%#p Uuid=%RTuuid uOpenFlags=%#x uPercentStart=%u uPercentSpan=%u pVDIfsDisk=%#p pVDIfsImage=%#p pVDIfsOperation=%#p enmType=%u ppBackendData=%#p",
1874 pszFilename, cbSize, uImageFlags, pszComment, pPCHSGeometry, pLCHSGeometry, pUuid, uOpenFlags, uPercentStart, uPercentSpan, pVDIfsDisk, pVDIfsImage, pVDIfsOperation, enmType, ppBackendData));
1875 int rc = VERR_NOT_SUPPORTED;
1876
1877 LogFlowFunc(("returns %Rrc\n", rc));
1878 return rc;
1879}
1880
1881/** @interface_method_impl{VBOXHDDBACKEND,pfnRename} */
1882static DECLCALLBACK(int) dmgRename(void *pBackendData, const char *pszFilename)
1883{
1884 LogFlowFunc(("pBackendData=%#p pszFilename=%#p\n", pBackendData, pszFilename));
1885 int rc = VERR_NOT_SUPPORTED;
1886
1887 LogFlowFunc(("returns %Rrc\n", rc));
1888 return rc;
1889}
1890
1891/** @interface_method_impl{VBOXHDDBACKEND,pfnClose} */
1892static DECLCALLBACK(int) dmgClose(void *pBackendData, bool fDelete)
1893{
1894 LogFlowFunc(("pBackendData=%#p fDelete=%d\n", pBackendData, fDelete));
1895 PDMGIMAGE pThis = (PDMGIMAGE)pBackendData;
1896
1897 int rc = dmgFreeImage(pThis, fDelete);
1898 RTMemFree(pThis);
1899
1900 LogFlowFunc(("returns %Rrc\n", rc));
1901 return rc;
1902}
1903
1904/** @interface_method_impl{VBOXHDDBACKEND,pfnRead} */
1905static DECLCALLBACK(int) dmgRead(void *pBackendData, uint64_t uOffset, size_t cbToRead,
1906 PVDIOCTX pIoCtx, size_t *pcbActuallyRead)
1907{
1908 LogFlowFunc(("pBackendData=%#p uOffset=%llu pIoCtx=%#p cbToRead=%zu pcbActuallyRead=%#p\n",
1909 pBackendData, uOffset, pIoCtx, cbToRead, pcbActuallyRead));
1910 PDMGIMAGE pThis = (PDMGIMAGE)pBackendData;
1911 PDMGEXTENT pExtent = NULL;
1912 int rc = VINF_SUCCESS;
1913
1914 AssertPtr(pThis);
1915 Assert(uOffset % DMG_SECTOR_SIZE == 0);
1916 Assert(cbToRead % DMG_SECTOR_SIZE == 0);
1917
1918 if ( uOffset + cbToRead > pThis->cbSize
1919 || cbToRead == 0)
1920 {
1921 LogFlowFunc(("returns VERR_INVALID_PARAMETER\n"));
1922 return VERR_INVALID_PARAMETER;
1923 }
1924
1925 pExtent = dmgExtentGetFromOffset(pThis, DMG_BYTE2BLOCK(uOffset));
1926
1927 if (pExtent)
1928 {
1929 uint64_t uExtentRel = DMG_BYTE2BLOCK(uOffset) - pExtent->uSectorExtent;
1930
1931 /* Remain in this extent. */
1932 cbToRead = RT_MIN(cbToRead, DMG_BLOCK2BYTE(pExtent->cSectorsExtent - uExtentRel));
1933
1934 switch (pExtent->enmType)
1935 {
1936 case DMGEXTENTTYPE_RAW:
1937 {
1938 rc = dmgWrapFileReadUser(pThis, pExtent->offFileStart + DMG_BLOCK2BYTE(uExtentRel), pIoCtx, cbToRead);
1939 break;
1940 }
1941 case DMGEXTENTTYPE_ZERO:
1942 {
1943 vdIfIoIntIoCtxSet(pThis->pIfIoXxx, pIoCtx, 0, cbToRead);
1944 break;
1945 }
1946 case DMGEXTENTTYPE_COMP_ZLIB:
1947 {
1948 if (pThis->pExtentDecomp != pExtent)
1949 {
1950 if (DMG_BLOCK2BYTE(pExtent->cSectorsExtent) > pThis->cbDecompExtent)
1951 {
1952 if (RT_LIKELY(pThis->pvDecompExtent))
1953 RTMemFree(pThis->pvDecompExtent);
1954
1955 pThis->pvDecompExtent = RTMemAllocZ(DMG_BLOCK2BYTE(pExtent->cSectorsExtent));
1956 if (!pThis->pvDecompExtent)
1957 rc = VERR_NO_MEMORY;
1958 else
1959 pThis->cbDecompExtent = DMG_BLOCK2BYTE(pExtent->cSectorsExtent);
1960 }
1961
1962 if (RT_SUCCESS(rc))
1963 {
1964 rc = dmgFileInflateSync(pThis, pExtent->offFileStart, pExtent->cbFile,
1965 pThis->pvDecompExtent,
1966 RT_MIN(pThis->cbDecompExtent, DMG_BLOCK2BYTE(pExtent->cSectorsExtent)));
1967 if (RT_SUCCESS(rc))
1968 pThis->pExtentDecomp = pExtent;
1969 }
1970 }
1971
1972 if (RT_SUCCESS(rc))
1973 vdIfIoIntIoCtxCopyTo(pThis->pIfIoXxx, pIoCtx,
1974 (uint8_t *)pThis->pvDecompExtent + DMG_BLOCK2BYTE(uExtentRel),
1975 cbToRead);
1976 break;
1977 }
1978 default:
1979 AssertMsgFailed(("Invalid extent type\n"));
1980 }
1981
1982 if (RT_SUCCESS(rc))
1983 *pcbActuallyRead = cbToRead;
1984 }
1985 else
1986 rc = VERR_INVALID_PARAMETER;
1987
1988 LogFlowFunc(("returns %Rrc\n", rc));
1989 return rc;
1990}
1991
1992/** @interface_method_impl{VBOXHDDBACKEND,pfnWrite} */
1993static DECLCALLBACK(int) dmgWrite(void *pBackendData, uint64_t uOffset, size_t cbToWrite,
1994 PVDIOCTX pIoCtx, size_t *pcbWriteProcess, size_t *pcbPreRead,
1995 size_t *pcbPostRead, unsigned fWrite)
1996{
1997 LogFlowFunc(("pBackendData=%#p uOffset=%llu pIoCtx=%#p cbToWrite=%zu pcbWriteProcess=%#p pcbPreRead=%#p pcbPostRead=%#p\n",
1998 pBackendData, uOffset, pIoCtx, cbToWrite, pcbWriteProcess, pcbPreRead, pcbPostRead));
1999 PDMGIMAGE pThis = (PDMGIMAGE)pBackendData;
2000 int rc = VERR_NOT_IMPLEMENTED;
2001
2002 AssertPtr(pThis);
2003 Assert(uOffset % 512 == 0);
2004 Assert(cbToWrite % 512 == 0);
2005
2006 if (!(pThis->uOpenFlags & VD_OPEN_FLAGS_READONLY))
2007 AssertMsgFailed(("Not implemented\n"));
2008 else
2009 rc = VERR_VD_IMAGE_READ_ONLY;
2010
2011 LogFlowFunc(("returns %Rrc\n", rc));
2012 return rc;
2013}
2014
2015/** @interface_method_impl{VBOXHDDBACKEND,pfnFlush} */
2016static DECLCALLBACK(int) dmgFlush(void *pBackendData, PVDIOCTX pIoCtx)
2017{
2018 LogFlowFunc(("pBackendData=%#p\n", pBackendData));
2019 PDMGIMAGE pThis = (PDMGIMAGE)pBackendData;
2020 int rc;
2021
2022 AssertPtr(pThis);
2023
2024 rc = dmgFlushImage(pThis);
2025
2026 LogFlowFunc(("returns %Rrc\n", rc));
2027 return rc;
2028}
2029
2030/** @interface_method_impl{VBOXHDDBACKEND,pfnGetVersion} */
2031static DECLCALLBACK(unsigned) dmgGetVersion(void *pBackendData)
2032{
2033 LogFlowFunc(("pBackendData=%#p\n", pBackendData));
2034 PDMGIMAGE pThis = (PDMGIMAGE)pBackendData;
2035
2036 AssertPtr(pThis);
2037
2038 if (pThis)
2039 return 1;
2040 else
2041 return 0;
2042}
2043
2044/** @interface_method_impl{VBOXHDDBACKEND,pfnGetSectorSize} */
2045static DECLCALLBACK(uint32_t) dmgGetSectorSize(void *pBackendData)
2046{
2047 LogFlowFunc(("pBackendData=%#p\n", pBackendData));
2048 PDMGIMAGE pThis = (PDMGIMAGE)pBackendData;
2049 uint32_t cb = 0;
2050
2051 AssertPtr(pThis);
2052
2053 if (pThis && (pThis->pStorage || pThis->hDmgFileInXar != NIL_RTVFSFILE))
2054 cb = 2048;
2055
2056 LogFlowFunc(("returns %u\n", cb));
2057 return cb;
2058}
2059
2060/** @interface_method_impl{VBOXHDDBACKEND,pfnGetSize} */
2061static DECLCALLBACK(uint64_t) dmgGetSize(void *pBackendData)
2062{
2063 LogFlowFunc(("pBackendData=%#p\n", pBackendData));
2064 PDMGIMAGE pThis = (PDMGIMAGE)pBackendData;
2065 uint64_t cb = 0;
2066
2067 AssertPtr(pThis);
2068
2069 if (pThis && (pThis->pStorage || pThis->hDmgFileInXar != NIL_RTVFSFILE))
2070 cb = pThis->cbSize;
2071
2072 LogFlowFunc(("returns %llu\n", cb));
2073 return cb;
2074}
2075
2076/** @interface_method_impl{VBOXHDDBACKEND,pfnGetFileSize} */
2077static DECLCALLBACK(uint64_t) dmgGetFileSize(void *pBackendData)
2078{
2079 LogFlowFunc(("pBackendData=%#p\n", pBackendData));
2080 PDMGIMAGE pThis = (PDMGIMAGE)pBackendData;
2081 uint64_t cb = 0;
2082
2083 AssertPtr(pThis);
2084
2085 if (pThis && (pThis->pStorage || pThis->hDmgFileInXar != NIL_RTVFSFILE))
2086 {
2087 uint64_t cbFile;
2088 int rc = dmgWrapFileGetSize(pThis, &cbFile);
2089 if (RT_SUCCESS(rc))
2090 cb = cbFile;
2091 }
2092
2093 LogFlowFunc(("returns %lld\n", cb));
2094 return cb;
2095}
2096
2097/** @interface_method_impl{VBOXHDDBACKEND,pfnGetPCHSGeometry} */
2098static DECLCALLBACK(int) dmgGetPCHSGeometry(void *pBackendData, PVDGEOMETRY pPCHSGeometry)
2099{
2100 LogFlowFunc(("pBackendData=%#p pPCHSGeometry=%#p\n", pBackendData, pPCHSGeometry));
2101 PDMGIMAGE pThis = (PDMGIMAGE)pBackendData;
2102 int rc;
2103
2104 AssertPtr(pThis);
2105
2106 if (pThis)
2107 {
2108 if (pThis->PCHSGeometry.cCylinders)
2109 {
2110 *pPCHSGeometry = pThis->PCHSGeometry;
2111 rc = VINF_SUCCESS;
2112 }
2113 else
2114 rc = VERR_VD_GEOMETRY_NOT_SET;
2115 }
2116 else
2117 rc = VERR_VD_NOT_OPENED;
2118
2119 LogFlowFunc(("returns %Rrc (PCHS=%u/%u/%u)\n", rc, pPCHSGeometry->cCylinders, pPCHSGeometry->cHeads, pPCHSGeometry->cSectors));
2120 return rc;
2121}
2122
2123/** @interface_method_impl{VBOXHDDBACKEND,pfnSetPCHSGeometry} */
2124static DECLCALLBACK(int) dmgSetPCHSGeometry(void *pBackendData, PCVDGEOMETRY pPCHSGeometry)
2125{
2126 LogFlowFunc(("pBackendData=%#p pPCHSGeometry=%#p PCHS=%u/%u/%u\n",
2127 pBackendData, pPCHSGeometry, pPCHSGeometry->cCylinders, pPCHSGeometry->cHeads, pPCHSGeometry->cSectors));
2128 PDMGIMAGE pThis = (PDMGIMAGE)pBackendData;
2129 int rc;
2130
2131 AssertPtr(pThis);
2132
2133 if (pThis)
2134 {
2135 if (!(pThis->uOpenFlags & VD_OPEN_FLAGS_READONLY))
2136 {
2137 pThis->PCHSGeometry = *pPCHSGeometry;
2138 rc = VINF_SUCCESS;
2139 }
2140 else
2141 rc = VERR_VD_IMAGE_READ_ONLY;
2142 }
2143 else
2144 rc = VERR_VD_NOT_OPENED;
2145
2146 LogFlowFunc(("returns %Rrc\n", rc));
2147 return rc;
2148}
2149
2150/** @interface_method_impl{VBOXHDDBACKEND,pfnGetLCHSGeometry} */
2151static DECLCALLBACK(int) dmgGetLCHSGeometry(void *pBackendData, PVDGEOMETRY pLCHSGeometry)
2152{
2153 LogFlowFunc(("pBackendData=%#p pLCHSGeometry=%#p\n", pBackendData, pLCHSGeometry));
2154 PDMGIMAGE pThis = (PDMGIMAGE)pBackendData;
2155 int rc;
2156
2157 AssertPtr(pThis);
2158
2159 if (pThis)
2160 {
2161 if (pThis->LCHSGeometry.cCylinders)
2162 {
2163 *pLCHSGeometry = pThis->LCHSGeometry;
2164 rc = VINF_SUCCESS;
2165 }
2166 else
2167 rc = VERR_VD_GEOMETRY_NOT_SET;
2168 }
2169 else
2170 rc = VERR_VD_NOT_OPENED;
2171
2172 LogFlowFunc(("returns %Rrc (LCHS=%u/%u/%u)\n", rc, pLCHSGeometry->cCylinders, pLCHSGeometry->cHeads, pLCHSGeometry->cSectors));
2173 return rc;
2174}
2175
2176/** @interface_method_impl{VBOXHDDBACKEND,pfnSetLCHSGeometry} */
2177static DECLCALLBACK(int) dmgSetLCHSGeometry(void *pBackendData, PCVDGEOMETRY pLCHSGeometry)
2178{
2179 LogFlowFunc(("pBackendData=%#p pLCHSGeometry=%#p LCHS=%u/%u/%u\n",
2180 pBackendData, pLCHSGeometry, pLCHSGeometry->cCylinders, pLCHSGeometry->cHeads, pLCHSGeometry->cSectors));
2181 PDMGIMAGE pThis = (PDMGIMAGE)pBackendData;
2182 int rc;
2183
2184 AssertPtr(pThis);
2185
2186 if (pThis)
2187 {
2188 if (!(pThis->uOpenFlags & VD_OPEN_FLAGS_READONLY))
2189 {
2190 pThis->LCHSGeometry = *pLCHSGeometry;
2191 rc = VINF_SUCCESS;
2192 }
2193 else
2194 rc = VERR_VD_IMAGE_READ_ONLY;
2195 }
2196 else
2197 rc = VERR_VD_NOT_OPENED;
2198
2199 LogFlowFunc(("returns %Rrc\n", rc));
2200 return rc;
2201}
2202
2203/** @interface_method_impl{VBOXHDDBACKEND,pfnGetImageFlags} */
2204static DECLCALLBACK(unsigned) dmgGetImageFlags(void *pBackendData)
2205{
2206 LogFlowFunc(("pBackendData=%#p\n", pBackendData));
2207 PDMGIMAGE pThis = (PDMGIMAGE)pBackendData;
2208 unsigned uImageFlags;
2209
2210 AssertPtr(pThis);
2211
2212 if (pThis)
2213 uImageFlags = pThis->uImageFlags;
2214 else
2215 uImageFlags = 0;
2216
2217 LogFlowFunc(("returns %#x\n", uImageFlags));
2218 return uImageFlags;
2219}
2220
2221/** @interface_method_impl{VBOXHDDBACKEND,pfnGetOpenFlags} */
2222static DECLCALLBACK(unsigned) dmgGetOpenFlags(void *pBackendData)
2223{
2224 LogFlowFunc(("pBackendData=%#p\n", pBackendData));
2225 PDMGIMAGE pThis = (PDMGIMAGE)pBackendData;
2226 unsigned uOpenFlags;
2227
2228 AssertPtr(pThis);
2229
2230 if (pThis)
2231 uOpenFlags = pThis->uOpenFlags;
2232 else
2233 uOpenFlags = 0;
2234
2235 LogFlowFunc(("returns %#x\n", uOpenFlags));
2236 return uOpenFlags;
2237}
2238
2239/** @interface_method_impl{VBOXHDDBACKEND,pfnSetOpenFlags} */
2240static DECLCALLBACK(int) dmgSetOpenFlags(void *pBackendData, unsigned uOpenFlags)
2241{
2242 LogFlowFunc(("pBackendData=%#p\n uOpenFlags=%#x", pBackendData, uOpenFlags));
2243 PDMGIMAGE pThis = (PDMGIMAGE)pBackendData;
2244 int rc;
2245
2246 /* Image must be opened and the new flags must be valid. */
2247 if (!pThis || (uOpenFlags & ~( VD_OPEN_FLAGS_READONLY | VD_OPEN_FLAGS_INFO
2248 | VD_OPEN_FLAGS_SHAREABLE | VD_OPEN_FLAGS_SEQUENTIAL
2249 | VD_OPEN_FLAGS_SKIP_CONSISTENCY_CHECKS)))
2250 {
2251 rc = VERR_INVALID_PARAMETER;
2252 goto out;
2253 }
2254
2255 /* Implement this operation via reopening the image. */
2256 rc = dmgFreeImage(pThis, false);
2257 if (RT_FAILURE(rc))
2258 goto out;
2259 rc = dmgOpenImage(pThis, uOpenFlags);
2260
2261out:
2262 LogFlowFunc(("returns %Rrc\n", rc));
2263 return rc;
2264}
2265
2266/** @interface_method_impl{VBOXHDDBACKEND,pfnGetComment} */
2267static DECLCALLBACK(int) dmgGetComment(void *pBackendData, char *pszComment, size_t cbComment)
2268{
2269 LogFlowFunc(("pBackendData=%#p pszComment=%#p cbComment=%zu\n", pBackendData, pszComment, cbComment));
2270 PDMGIMAGE pThis = (PDMGIMAGE)pBackendData;
2271 int rc;
2272
2273 AssertPtr(pThis);
2274
2275 if (pThis)
2276 rc = VERR_NOT_SUPPORTED;
2277 else
2278 rc = VERR_VD_NOT_OPENED;
2279
2280 LogFlowFunc(("returns %Rrc comment='%s'\n", rc, pszComment));
2281 return rc;
2282}
2283
2284/** @interface_method_impl{VBOXHDDBACKEND,pfnSetComment} */
2285static DECLCALLBACK(int) dmgSetComment(void *pBackendData, const char *pszComment)
2286{
2287 LogFlowFunc(("pBackendData=%#p pszComment=\"%s\"\n", pBackendData, pszComment));
2288 PDMGIMAGE pImage = (PDMGIMAGE)pBackendData;
2289 int rc;
2290
2291 AssertPtr(pImage);
2292
2293 if (pImage)
2294 {
2295 if (pImage->uOpenFlags & VD_OPEN_FLAGS_READONLY)
2296 rc = VERR_VD_IMAGE_READ_ONLY;
2297 else
2298 rc = VERR_NOT_SUPPORTED;
2299 }
2300 else
2301 rc = VERR_VD_NOT_OPENED;
2302
2303 LogFlowFunc(("returns %Rrc\n", rc));
2304 return rc;
2305}
2306
2307/** @interface_method_impl{VBOXHDDBACKEND,pfnGetUuid} */
2308static DECLCALLBACK(int) dmgGetUuid(void *pBackendData, PRTUUID pUuid)
2309{
2310 LogFlowFunc(("pBackendData=%#p pUuid=%#p\n", pBackendData, pUuid));
2311 PDMGIMAGE pThis = (PDMGIMAGE)pBackendData;
2312 int rc;
2313
2314 AssertPtr(pThis);
2315
2316 if (pThis)
2317 rc = VERR_NOT_SUPPORTED;
2318 else
2319 rc = VERR_VD_NOT_OPENED;
2320
2321 LogFlowFunc(("returns %Rrc (%RTuuid)\n", rc, pUuid));
2322 return rc;
2323}
2324
2325/** @interface_method_impl{VBOXHDDBACKEND,pfnSetUuid} */
2326static DECLCALLBACK(int) dmgSetUuid(void *pBackendData, PCRTUUID pUuid)
2327{
2328 LogFlowFunc(("pBackendData=%#p Uuid=%RTuuid\n", pBackendData, pUuid));
2329 PDMGIMAGE pThis = (PDMGIMAGE)pBackendData;
2330 int rc;
2331
2332 LogFlowFunc(("%RTuuid\n", pUuid));
2333 AssertPtr(pThis);
2334
2335 if (pThis)
2336 {
2337 if (!(pThis->uOpenFlags & VD_OPEN_FLAGS_READONLY))
2338 rc = VERR_NOT_SUPPORTED;
2339 else
2340 rc = VERR_VD_IMAGE_READ_ONLY;
2341 }
2342 else
2343 rc = VERR_VD_NOT_OPENED;
2344
2345 LogFlowFunc(("returns %Rrc\n", rc));
2346 return rc;
2347}
2348
2349/** @interface_method_impl{VBOXHDDBACKEND,pfnGetModificationUuid} */
2350static DECLCALLBACK(int) dmgGetModificationUuid(void *pBackendData, PRTUUID pUuid)
2351{
2352 LogFlowFunc(("pBackendData=%#p pUuid=%#p\n", pBackendData, pUuid));
2353 PDMGIMAGE pThis = (PDMGIMAGE)pBackendData;
2354 int rc;
2355
2356 AssertPtr(pThis);
2357
2358 if (pThis)
2359 rc = VERR_NOT_SUPPORTED;
2360 else
2361 rc = VERR_VD_NOT_OPENED;
2362
2363 LogFlowFunc(("returns %Rrc (%RTuuid)\n", rc, pUuid));
2364 return rc;
2365}
2366
2367/** @interface_method_impl{VBOXHDDBACKEND,pfnSetModificationUuid} */
2368static DECLCALLBACK(int) dmgSetModificationUuid(void *pBackendData, PCRTUUID pUuid)
2369{
2370 LogFlowFunc(("pBackendData=%#p Uuid=%RTuuid\n", pBackendData, pUuid));
2371 PDMGIMAGE pThis = (PDMGIMAGE)pBackendData;
2372 int rc;
2373
2374 AssertPtr(pThis);
2375
2376 if (pThis)
2377 {
2378 if (!(pThis->uOpenFlags & VD_OPEN_FLAGS_READONLY))
2379 rc = VERR_NOT_SUPPORTED;
2380 else
2381 rc = VERR_VD_IMAGE_READ_ONLY;
2382 }
2383 else
2384 rc = VERR_VD_NOT_OPENED;
2385
2386 LogFlowFunc(("returns %Rrc\n", rc));
2387 return rc;
2388}
2389
2390/** @interface_method_impl{VBOXHDDBACKEND,pfnGetParentUuid} */
2391static DECLCALLBACK(int) dmgGetParentUuid(void *pBackendData, PRTUUID pUuid)
2392{
2393 LogFlowFunc(("pBackendData=%#p pUuid=%#p\n", pBackendData, pUuid));
2394 PDMGIMAGE pThis = (PDMGIMAGE)pBackendData;
2395 int rc;
2396
2397 AssertPtr(pThis);
2398
2399 if (pThis)
2400 rc = VERR_NOT_SUPPORTED;
2401 else
2402 rc = VERR_VD_NOT_OPENED;
2403
2404 LogFlowFunc(("returns %Rrc (%RTuuid)\n", rc, pUuid));
2405 return rc;
2406}
2407
2408/** @interface_method_impl{VBOXHDDBACKEND,pfnSetParentUuid} */
2409static DECLCALLBACK(int) dmgSetParentUuid(void *pBackendData, PCRTUUID pUuid)
2410{
2411 LogFlowFunc(("pBackendData=%#p Uuid=%RTuuid\n", pBackendData, pUuid));
2412 PDMGIMAGE pThis = (PDMGIMAGE)pBackendData;
2413 int rc;
2414
2415 AssertPtr(pThis);
2416
2417 if (pThis)
2418 {
2419 if (!(pThis->uOpenFlags & VD_OPEN_FLAGS_READONLY))
2420 rc = VERR_NOT_SUPPORTED;
2421 else
2422 rc = VERR_VD_IMAGE_READ_ONLY;
2423 }
2424 else
2425 rc = VERR_VD_NOT_OPENED;
2426
2427 LogFlowFunc(("returns %Rrc\n", rc));
2428 return rc;
2429}
2430
2431/** @interface_method_impl{VBOXHDDBACKEND,pfnGetParentModificationUuid} */
2432static DECLCALLBACK(int) dmgGetParentModificationUuid(void *pBackendData, PRTUUID pUuid)
2433{
2434 LogFlowFunc(("pBackendData=%#p pUuid=%#p\n", pBackendData, pUuid));
2435 PDMGIMAGE pThis = (PDMGIMAGE)pBackendData;
2436 int rc;
2437
2438 AssertPtr(pThis);
2439
2440 if (pThis)
2441 rc = VERR_NOT_SUPPORTED;
2442 else
2443 rc = VERR_VD_NOT_OPENED;
2444
2445 LogFlowFunc(("returns %Rrc (%RTuuid)\n", rc, pUuid));
2446 return rc;
2447}
2448
2449/** @interface_method_impl{VBOXHDDBACKEND,pfnSetParentModificationUuid} */
2450static DECLCALLBACK(int) dmgSetParentModificationUuid(void *pBackendData, PCRTUUID pUuid)
2451{
2452 LogFlowFunc(("pBackendData=%#p Uuid=%RTuuid\n", pBackendData, pUuid));
2453 PDMGIMAGE pThis = (PDMGIMAGE)pBackendData;
2454 int rc;
2455
2456 AssertPtr(pThis);
2457
2458 if (pThis)
2459 {
2460 if (!(pThis->uOpenFlags & VD_OPEN_FLAGS_READONLY))
2461 rc = VERR_NOT_SUPPORTED;
2462 else
2463 rc = VERR_VD_IMAGE_READ_ONLY;
2464 }
2465 else
2466 rc = VERR_VD_NOT_OPENED;
2467
2468 LogFlowFunc(("returns %Rrc\n", rc));
2469 return rc;
2470}
2471
2472/** @interface_method_impl{VBOXHDDBACKEND,pfnDump} */
2473static DECLCALLBACK(void) dmgDump(void *pBackendData)
2474{
2475 PDMGIMAGE pThis = (PDMGIMAGE)pBackendData;
2476
2477 AssertPtr(pThis);
2478 if (pThis)
2479 {
2480 vdIfErrorMessage(pThis->pIfError, "Header: Geometry PCHS=%u/%u/%u LCHS=%u/%u/%u cSectors=%llu\n",
2481 pThis->PCHSGeometry.cCylinders, pThis->PCHSGeometry.cHeads, pThis->PCHSGeometry.cSectors,
2482 pThis->LCHSGeometry.cCylinders, pThis->LCHSGeometry.cHeads, pThis->LCHSGeometry.cSectors,
2483 pThis->cbSize / DMG_SECTOR_SIZE);
2484 }
2485}
2486
2487
2488const VBOXHDDBACKEND g_DmgBackend =
2489{
2490 /* pszBackendName */
2491 "DMG",
2492 /* cbSize */
2493 sizeof(VBOXHDDBACKEND),
2494 /* uBackendCaps */
2495 VD_CAP_FILE | VD_CAP_VFS,
2496 /* paFileExtensions */
2497 s_aDmgFileExtensions,
2498 /* paConfigInfo */
2499 NULL,
2500 /* pfnCheckIfValid */
2501 dmgCheckIfValid,
2502 /* pfnOpen */
2503 dmgOpen,
2504 /* pfnCreate */
2505 dmgCreate,
2506 /* pfnRename */
2507 dmgRename,
2508 /* pfnClose */
2509 dmgClose,
2510 /* pfnRead */
2511 dmgRead,
2512 /* pfnWrite */
2513 dmgWrite,
2514 /* pfnFlush */
2515 dmgFlush,
2516 /* pfnDiscard */
2517 NULL,
2518 /* pfnGetVersion */
2519 dmgGetVersion,
2520 /* pfnGetSectorSize */
2521 dmgGetSectorSize,
2522 /* pfnGetSize */
2523 dmgGetSize,
2524 /* pfnGetFileSize */
2525 dmgGetFileSize,
2526 /* pfnGetPCHSGeometry */
2527 dmgGetPCHSGeometry,
2528 /* pfnSetPCHSGeometry */
2529 dmgSetPCHSGeometry,
2530 /* pfnGetLCHSGeometry */
2531 dmgGetLCHSGeometry,
2532 /* pfnSetLCHSGeometry */
2533 dmgSetLCHSGeometry,
2534 /* pfnGetImageFlags */
2535 dmgGetImageFlags,
2536 /* pfnGetOpenFlags */
2537 dmgGetOpenFlags,
2538 /* pfnSetOpenFlags */
2539 dmgSetOpenFlags,
2540 /* pfnGetComment */
2541 dmgGetComment,
2542 /* pfnSetComment */
2543 dmgSetComment,
2544 /* pfnGetUuid */
2545 dmgGetUuid,
2546 /* pfnSetUuid */
2547 dmgSetUuid,
2548 /* pfnGetModificationUuid */
2549 dmgGetModificationUuid,
2550 /* pfnSetModificationUuid */
2551 dmgSetModificationUuid,
2552 /* pfnGetParentUuid */
2553 dmgGetParentUuid,
2554 /* pfnSetParentUuid */
2555 dmgSetParentUuid,
2556 /* pfnGetParentModificationUuid */
2557 dmgGetParentModificationUuid,
2558 /* pfnSetParentModificationUuid */
2559 dmgSetParentModificationUuid,
2560 /* pfnDump */
2561 dmgDump,
2562 /* pfnGetTimestamp */
2563 NULL,
2564 /* pfnGetParentTimestamp */
2565 NULL,
2566 /* pfnSetParentTimestamp */
2567 NULL,
2568 /* pfnGetParentFilename */
2569 NULL,
2570 /* pfnSetParentFilename */
2571 NULL,
2572 /* pfnComposeLocation */
2573 genericFileComposeLocation,
2574 /* pfnComposeName */
2575 genericFileComposeName,
2576 /* pfnCompact */
2577 NULL,
2578 /* pfnResize */
2579 NULL,
2580 /* pfnRepair */
2581 NULL,
2582 /* pfnTraverseMetadata */
2583 NULL
2584};
2585
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