VirtualBox

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

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

Storage: Doxygen fixes

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 81.7 KB
Line 
1/* $Id: DMG.cpp 64272 2016-10-14 08:25:05Z 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*********************************************************************************************************************************/
434#if 0 /* unused */
435static void dmgUdifFtrHost2FileEndian(PDMGUDIF pUdif);
436#endif
437static void dmgUdifFtrFile2HostEndian(PDMGUDIF pUdif);
438
439static void dmgUdifIdHost2FileEndian(PDMGUDIFID pId);
440static void dmgUdifIdFile2HostEndian(PDMGUDIFID pId);
441
442#if 0 /* unused */
443static void dmgUdifCkSumHost2FileEndian(PDMGUDIFCKSUM pCkSum);
444#endif
445static void dmgUdifCkSumFile2HostEndian(PDMGUDIFCKSUM pCkSum);
446static bool dmgUdifCkSumIsValid(PCDMGUDIFCKSUM pCkSum, const char *pszPrefix);
447
448
449
450/**
451 * vdIfIoIntFileReadSync / RTVfsFileReadAt wrapper.
452 */
453static int dmgWrapFileReadSync(PDMGIMAGE pThis, RTFOFF off, void *pvBuf, size_t cbToRead)
454{
455 int rc;
456 if (pThis->hDmgFileInXar == NIL_RTVFSFILE)
457 rc = vdIfIoIntFileReadSync(pThis->pIfIoXxx, pThis->pStorage, off, pvBuf, cbToRead);
458 else
459 rc = RTVfsFileReadAt(pThis->hDmgFileInXar, off, pvBuf, cbToRead, NULL);
460 return rc;
461}
462
463/**
464 * vdIfIoIntFileReadUser / RTVfsFileReadAt wrapper.
465 */
466static int dmgWrapFileReadUser(PDMGIMAGE pThis, RTFOFF off, PVDIOCTX pIoCtx, size_t cbToRead)
467{
468 int rc;
469 if (pThis->hDmgFileInXar == NIL_RTVFSFILE)
470 rc = vdIfIoIntFileReadUser(pThis->pIfIoXxx, pThis->pStorage, off, pIoCtx, cbToRead);
471 else
472 {
473 /*
474 * Alloate a temporary buffer on the stack or heap and use
475 * vdIfIoIntIoCtxCopyTo to work the context.
476 *
477 * The I/O context stuff seems too complicated and undocument that I'm
478 * not going to bother trying to implement this efficiently right now.
479 */
480 void *pvFree = NULL;
481 void *pvBuf;
482 if (cbToRead < _32K)
483 pvBuf = alloca(cbToRead);
484 else
485 pvFree = pvBuf = RTMemTmpAlloc(cbToRead);
486 if (pvBuf)
487 {
488 rc = RTVfsFileReadAt(pThis->hDmgFileInXar, off, pvBuf, cbToRead, NULL);
489 if (RT_SUCCESS(rc))
490 vdIfIoIntIoCtxCopyTo(pThis->pIfIoXxx, pIoCtx, pvBuf, cbToRead);
491 if (pvFree)
492 RTMemTmpFree(pvFree);
493 }
494 else
495 rc = VERR_NO_TMP_MEMORY;
496 }
497 return rc;
498}
499
500/**
501 * vdIfIoIntFileGetSize / RTVfsFileGetSize wrapper.
502 */
503static int dmgWrapFileGetSize(PDMGIMAGE pThis, uint64_t *pcbFile)
504{
505 int rc;
506 if (pThis->hDmgFileInXar == NIL_RTVFSFILE)
507 rc = vdIfIoIntFileGetSize(pThis->pIfIoXxx, pThis->pStorage, pcbFile);
508 else
509 rc = RTVfsFileGetSize(pThis->hDmgFileInXar, pcbFile);
510 return rc;
511}
512
513
514
515static DECLCALLBACK(int) dmgFileInflateHelper(void *pvUser, void *pvBuf, size_t cbBuf, size_t *pcbBuf)
516{
517 DMGINFLATESTATE *pInflateState = (DMGINFLATESTATE *)pvUser;
518
519 Assert(cbBuf);
520 if (pInflateState->iOffset < 0)
521 {
522 *(uint8_t *)pvBuf = RTZIPTYPE_ZLIB;
523 if (pcbBuf)
524 *pcbBuf = 1;
525 pInflateState->iOffset = 0;
526 return VINF_SUCCESS;
527 }
528 cbBuf = RT_MIN(cbBuf, pInflateState->cbSize);
529 int rc = dmgWrapFileReadSync(pInflateState->pImage, pInflateState->uFileOffset, pvBuf, cbBuf);
530 if (RT_FAILURE(rc))
531 return rc;
532 pInflateState->uFileOffset += cbBuf;
533 pInflateState->iOffset += cbBuf;
534 pInflateState->cbSize -= cbBuf;
535 Assert(pcbBuf);
536 *pcbBuf = cbBuf;
537 return VINF_SUCCESS;
538}
539
540/**
541 * Internal: read from a file and inflate the compressed data,
542 * distinguishing between async and normal operation
543 */
544DECLINLINE(int) dmgFileInflateSync(PDMGIMAGE pImage, uint64_t uOffset, size_t cbToRead,
545 void *pvBuf, size_t cbBuf)
546{
547 int rc;
548 PRTZIPDECOMP pZip = NULL;
549 DMGINFLATESTATE InflateState;
550 size_t cbActuallyRead;
551
552 InflateState.pImage = pImage;
553 InflateState.cbSize = cbToRead;
554 InflateState.uFileOffset = uOffset;
555 InflateState.iOffset = -1;
556
557 rc = RTZipDecompCreate(&pZip, &InflateState, dmgFileInflateHelper);
558 if (RT_FAILURE(rc))
559 return rc;
560 rc = RTZipDecompress(pZip, pvBuf, cbBuf, &cbActuallyRead);
561 RTZipDecompDestroy(pZip);
562 if (RT_FAILURE(rc))
563 return rc;
564 if (cbActuallyRead != cbBuf)
565 rc = VERR_VD_VMDK_INVALID_FORMAT;
566 return rc;
567}
568
569/**
570 * Swaps endian.
571 * @param pUdif The structure.
572 */
573static void dmgSwapEndianUdif(PDMGUDIF pUdif)
574{
575#ifndef RT_BIG_ENDIAN
576 pUdif->u32Magic = RT_BSWAP_U32(pUdif->u32Magic);
577 pUdif->u32Version = RT_BSWAP_U32(pUdif->u32Version);
578 pUdif->cbFooter = RT_BSWAP_U32(pUdif->cbFooter);
579 pUdif->fFlags = RT_BSWAP_U32(pUdif->fFlags);
580 pUdif->offRunData = RT_BSWAP_U64(pUdif->offRunData);
581 pUdif->offData = RT_BSWAP_U64(pUdif->offData);
582 pUdif->cbData = RT_BSWAP_U64(pUdif->cbData);
583 pUdif->offRsrc = RT_BSWAP_U64(pUdif->offRsrc);
584 pUdif->cbRsrc = RT_BSWAP_U64(pUdif->cbRsrc);
585 pUdif->iSegment = RT_BSWAP_U32(pUdif->iSegment);
586 pUdif->cSegments = RT_BSWAP_U32(pUdif->cSegments);
587 pUdif->offXml = RT_BSWAP_U64(pUdif->offXml);
588 pUdif->cbXml = RT_BSWAP_U64(pUdif->cbXml);
589 pUdif->u32Type = RT_BSWAP_U32(pUdif->u32Type);
590 pUdif->cSectors = RT_BSWAP_U64(pUdif->cSectors);
591#endif
592}
593
594
595#if 0 /* unused */
596/**
597 * Swaps endian from host cpu to file.
598 * @param pUdif The structure.
599 */
600static void dmgUdifFtrHost2FileEndian(PDMGUDIF pUdif)
601{
602 dmgSwapEndianUdif(pUdif);
603 dmgUdifIdHost2FileEndian(&pUdif->SegmentId);
604 dmgUdifCkSumHost2FileEndian(&pUdif->DataCkSum);
605 dmgUdifCkSumHost2FileEndian(&pUdif->MasterCkSum);
606}
607#endif
608
609
610/**
611 * Swaps endian from file to host cpu.
612 * @param pUdif The structure.
613 */
614static void dmgUdifFtrFile2HostEndian(PDMGUDIF pUdif)
615{
616 dmgSwapEndianUdif(pUdif);
617 dmgUdifIdFile2HostEndian(&pUdif->SegmentId);
618 dmgUdifCkSumFile2HostEndian(&pUdif->DataCkSum);
619 dmgUdifCkSumFile2HostEndian(&pUdif->MasterCkSum);
620}
621
622/**
623 * Swaps endian from file to host cpu.
624 * @param pBlkx The blkx structure.
625 */
626static void dmgBlkxFile2HostEndian(PDMGBLKX pBlkx)
627{
628 pBlkx->u32Magic = RT_BE2H_U32(pBlkx->u32Magic);
629 pBlkx->u32Version = RT_BE2H_U32(pBlkx->u32Version);
630 pBlkx->cSectornumberFirst = RT_BE2H_U64(pBlkx->cSectornumberFirst);
631 pBlkx->cSectors = RT_BE2H_U64(pBlkx->cSectors);
632 pBlkx->offDataStart = RT_BE2H_U64(pBlkx->offDataStart);
633 pBlkx->cSectorsDecompress = RT_BE2H_U32(pBlkx->cSectorsDecompress);
634 pBlkx->u32BlocksDescriptor = RT_BE2H_U32(pBlkx->u32BlocksDescriptor);
635 pBlkx->cBlocksRunCount = RT_BE2H_U32(pBlkx->cBlocksRunCount);
636 dmgUdifCkSumFile2HostEndian(&pBlkx->BlkxCkSum);
637}
638
639/**
640 * Swaps endian from file to host cpu.
641 * @param pBlkxDesc The blkx descriptor structure.
642 */
643static void dmgBlkxDescFile2HostEndian(PDMGBLKXDESC pBlkxDesc)
644{
645 pBlkxDesc->u32Type = RT_BE2H_U32(pBlkxDesc->u32Type);
646 pBlkxDesc->u32Reserved = RT_BE2H_U32(pBlkxDesc->u32Reserved);
647 pBlkxDesc->u64SectorStart = RT_BE2H_U64(pBlkxDesc->u64SectorStart);
648 pBlkxDesc->u64SectorCount = RT_BE2H_U64(pBlkxDesc->u64SectorCount);
649 pBlkxDesc->offData = RT_BE2H_U64(pBlkxDesc->offData);
650 pBlkxDesc->cbData = RT_BE2H_U64(pBlkxDesc->cbData);
651}
652
653/**
654 * Validates an UDIF footer structure.
655 *
656 * @returns true if valid, false and LogRel()s on failure.
657 * @param pFtr The UDIF footer to validate.
658 * @param offFtr The offset of the structure.
659 */
660static bool dmgUdifFtrIsValid(PCDMGUDIF pFtr, uint64_t offFtr)
661{
662 bool fRc = true;
663
664 DMG_VALIDATE(!(pFtr->fFlags & ~DMGUDIF_FLAGS_KNOWN_MASK), ("fFlags=%#RX32 fKnown=%RX32\n", pFtr->fFlags, DMGUDIF_FLAGS_KNOWN_MASK));
665 DMG_VALIDATE(pFtr->offRunData < offFtr, ("offRunData=%#RX64\n", pFtr->offRunData));
666 DMG_VALIDATE(pFtr->cbData <= offFtr && pFtr->offData + pFtr->cbData <= offFtr, ("cbData=%#RX64 offData=%#RX64 offFtr=%#RX64\n", pFtr->cbData, pFtr->offData, offFtr));
667 DMG_VALIDATE(pFtr->offData < offFtr, ("offData=%#RX64\n", pFtr->offData));
668 DMG_VALIDATE(pFtr->cbRsrc <= offFtr && pFtr->offRsrc + pFtr->cbRsrc <= offFtr, ("cbRsrc=%#RX64 offRsrc=%#RX64 offFtr=%#RX64\n", pFtr->cbRsrc, pFtr->offRsrc, offFtr));
669 DMG_VALIDATE(pFtr->offRsrc < offFtr, ("offRsrc=%#RX64\n", pFtr->offRsrc));
670 DMG_VALIDATE(pFtr->cSegments <= 1, ("cSegments=%RU32\n", pFtr->cSegments));
671 DMG_VALIDATE(pFtr->iSegment == 0 || pFtr->iSegment == 1, ("iSegment=%RU32 cSegments=%RU32\n", pFtr->iSegment, pFtr->cSegments));
672 DMG_VALIDATE(pFtr->cbXml <= offFtr && pFtr->offXml + pFtr->cbXml <= offFtr, ("cbXml=%#RX64 offXml=%#RX64 offFtr=%#RX64\n", pFtr->cbXml, pFtr->offXml, offFtr));
673 DMG_VALIDATE(pFtr->offXml < offFtr, ("offXml=%#RX64\n", pFtr->offXml));
674 DMG_VALIDATE(pFtr->cbXml > 128, ("cbXml=%#RX64\n", pFtr->cbXml));
675 DMG_VALIDATE(pFtr->cbXml < 10 * _1M, ("cbXml=%#RX64\n", pFtr->cbXml));
676 DMG_VALIDATE(pFtr->u32Type == DMGUDIF_TYPE_DEVICE || pFtr->u32Type == DMGUDIF_TYPE_PARTITION, ("u32Type=%RU32\n", pFtr->u32Type));
677 DMG_VALIDATE(pFtr->cSectors != 0, ("cSectors=%#RX64\n", pFtr->cSectors));
678 fRc &= dmgUdifCkSumIsValid(&pFtr->DataCkSum, "DataCkSum");
679 fRc &= dmgUdifCkSumIsValid(&pFtr->MasterCkSum, "MasterCkSum");
680
681 return fRc;
682}
683
684
685static bool dmgBlkxIsValid(PCDMGBLKX pBlkx)
686{
687 bool fRc = true;
688
689 fRc &= dmgUdifCkSumIsValid(&pBlkx->BlkxCkSum, "BlkxCkSum");
690 DMG_VALIDATE(pBlkx->u32Magic == DMGBLKX_MAGIC, ("u32Magic=%#RX32 u32MagicExpected=%#RX32\n", pBlkx->u32Magic, DMGBLKX_MAGIC));
691 DMG_VALIDATE(pBlkx->u32Version == DMGBLKX_VERSION, ("u32Version=%#RX32 u32VersionExpected=%#RX32\n", pBlkx->u32Magic, DMGBLKX_VERSION));
692
693 return fRc;
694}
695
696/**
697 * Swaps endian from host cpu to file.
698 * @param pId The structure.
699 */
700static void dmgUdifIdHost2FileEndian(PDMGUDIFID pId)
701{
702 NOREF(pId);
703}
704
705
706/**
707 * Swaps endian from file to host cpu.
708 * @param pId The structure.
709 */
710static void dmgUdifIdFile2HostEndian(PDMGUDIFID pId)
711{
712 dmgUdifIdHost2FileEndian(pId);
713}
714
715
716/**
717 * Swaps endian.
718 * @param pCkSum The structure.
719 * @param u32Kind Kind of the checksum (CRC32, none)
720 * @param cBits Size of the checksum in bits.
721 */
722static void dmgSwapEndianUdifCkSum(PDMGUDIFCKSUM pCkSum, uint32_t u32Kind, uint32_t cBits)
723{
724#ifdef RT_BIG_ENDIAN
725 NOREF(pCkSum);
726 NOREF(u32Kind);
727 NOREF(cBits);
728#else
729 switch (u32Kind)
730 {
731 case DMGUDIFCKSUM_NONE:
732 /* nothing to do here */
733 break;
734
735 case DMGUDIFCKSUM_CRC32:
736 Assert(cBits == 32);
737 pCkSum->u32Kind = RT_BSWAP_U32(pCkSum->u32Kind);
738 pCkSum->cBits = RT_BSWAP_U32(pCkSum->cBits);
739 pCkSum->uSum.au32[0] = RT_BSWAP_U32(pCkSum->uSum.au32[0]);
740 break;
741
742 default:
743 AssertMsgFailed(("%x\n", u32Kind));
744 break;
745 }
746 NOREF(cBits);
747#endif
748}
749
750
751#if 0 /* unused */
752/**
753 * Swaps endian from host cpu to file.
754 * @param pCkSum The structure.
755 */
756static void dmgUdifCkSumHost2FileEndian(PDMGUDIFCKSUM pCkSum)
757{
758 dmgSwapEndianUdifCkSum(pCkSum, pCkSum->u32Kind, pCkSum->cBits);
759}
760#endif
761
762
763/**
764 * Swaps endian from file to host cpu.
765 * @param pCkSum The structure.
766 */
767static void dmgUdifCkSumFile2HostEndian(PDMGUDIFCKSUM pCkSum)
768{
769 dmgSwapEndianUdifCkSum(pCkSum, RT_BE2H_U32(pCkSum->u32Kind), RT_BE2H_U32(pCkSum->cBits));
770}
771
772
773/**
774 * Validates an UDIF checksum structure.
775 *
776 * @returns true if valid, false and LogRel()s on failure.
777 * @param pCkSum The checksum structure.
778 * @param pszPrefix The message prefix.
779 * @remarks This does not check the checksummed data.
780 */
781static bool dmgUdifCkSumIsValid(PCDMGUDIFCKSUM pCkSum, const char *pszPrefix)
782{
783 bool fRc = true;
784
785 switch (pCkSum->u32Kind)
786 {
787 case DMGUDIFCKSUM_NONE:
788 DMG_VALIDATE(pCkSum->cBits == 0, ("%s/NONE: cBits=%d\n", pszPrefix, pCkSum->cBits));
789 break;
790
791 case DMGUDIFCKSUM_CRC32:
792 DMG_VALIDATE(pCkSum->cBits == 32, ("%s/NONE: cBits=%d\n", pszPrefix, pCkSum->cBits));
793 break;
794
795 default:
796 DMG_VALIDATE(0, ("%s: u32Kind=%#RX32\n", pszPrefix, pCkSum->u32Kind));
797 break;
798 }
799 return fRc;
800}
801
802
803/**
804 * Internal. Flush image data to disk.
805 */
806static int dmgFlushImage(PDMGIMAGE pThis)
807{
808 int rc = VINF_SUCCESS;
809
810 if ( pThis
811 && (pThis->pStorage || pThis->hDmgFileInXar != NIL_RTVFSFILE)
812 && !(pThis->uOpenFlags & VD_OPEN_FLAGS_READONLY))
813 {
814 /** @todo handle writable files, update checksums etc. */
815 }
816
817 return rc;
818}
819
820
821/**
822 * Internal. Free all allocated space for representing an image except pThis,
823 * and optionally delete the image from disk.
824 */
825static int dmgFreeImage(PDMGIMAGE pThis, bool fDelete)
826{
827 int rc = VINF_SUCCESS;
828
829 /* Freeing a never allocated image (e.g. because the open failed) is
830 * not signalled as an error. After all nothing bad happens. */
831 if (pThis)
832 {
833 RTVfsFileRelease(pThis->hDmgFileInXar);
834 pThis->hDmgFileInXar = NIL_RTVFSFILE;
835
836 RTVfsFsStrmRelease(pThis->hXarFss);
837 pThis->hXarFss = NIL_RTVFSFSSTREAM;
838
839 if (pThis->pStorage)
840 {
841 /* No point updating the file that is deleted anyway. */
842 if (!fDelete)
843 dmgFlushImage(pThis);
844
845 rc = vdIfIoIntFileClose(pThis->pIfIoXxx, pThis->pStorage);
846 pThis->pStorage = NULL;
847 }
848
849 for (unsigned iRsrc = 0; iRsrc < RT_ELEMENTS(pThis->aRsrcs); iRsrc++)
850 for (unsigned i = 0; i < pThis->aRsrcs[iRsrc].cEntries; i++)
851 {
852 if (pThis->aRsrcs[iRsrc].aEntries[i].pbData)
853 {
854 RTMemFree(pThis->aRsrcs[iRsrc].aEntries[i].pbData);
855 pThis->aRsrcs[iRsrc].aEntries[i].pbData = NULL;
856 }
857 if (pThis->aRsrcs[iRsrc].aEntries[i].pszName)
858 {
859 RTMemFree(pThis->aRsrcs[iRsrc].aEntries[i].pszName);
860 pThis->aRsrcs[iRsrc].aEntries[i].pszName = NULL;
861 }
862 if (pThis->aRsrcs[iRsrc].aEntries[i].pszCFName)
863 {
864 RTMemFree(pThis->aRsrcs[iRsrc].aEntries[i].pszCFName);
865 pThis->aRsrcs[iRsrc].aEntries[i].pszCFName = NULL;
866 }
867 }
868
869 if (fDelete && pThis->pszFilename)
870 vdIfIoIntFileDelete(pThis->pIfIoXxx, pThis->pszFilename);
871
872 if (pThis->pvDecompExtent)
873 {
874 RTMemFree(pThis->pvDecompExtent);
875 pThis->pvDecompExtent = NULL;
876 pThis->cbDecompExtent = 0;
877 }
878
879 if (pThis->paExtents)
880 {
881 RTMemFree(pThis->paExtents);
882 pThis->paExtents = NULL;
883 }
884 }
885
886 LogFlowFunc(("returns %Rrc\n", rc));
887 return rc;
888}
889
890
891#define STARTS_WITH(pszString, szStart) \
892 ( strncmp(pszString, szStart, sizeof(szStart) - 1) == 0 )
893
894#define STARTS_WITH_WORD(pszString, szWord) \
895 ( STARTS_WITH(pszString, szWord) \
896 && !RT_C_IS_ALNUM((pszString)[sizeof(szWord) - 1]) )
897
898#define SKIP_AHEAD(psz, szWord) \
899 do { \
900 (psz) = RTStrStripL((psz) + sizeof(szWord) - 1); \
901 } while (0)
902
903#define REQUIRE_WORD(psz, szWord) \
904 do { \
905 if (!STARTS_WITH_WORD(psz, szWord)) \
906 return psz; \
907 (psz) = RTStrStripL((psz) + sizeof(szWord) - 1); \
908 } while (0)
909
910#define REQUIRE_TAG(psz, szTag) \
911 do { \
912 if (!STARTS_WITH(psz, "<" szTag ">")) \
913 return psz; \
914 (psz) = RTStrStripL((psz) + sizeof("<" szTag ">") - 1); \
915 } while (0)
916
917#define REQUIRE_TAG_NO_STRIP(psz, szTag) \
918 do { \
919 if (!STARTS_WITH(psz, "<" szTag ">")) \
920 return psz; \
921 (psz) += sizeof("<" szTag ">") - 1; \
922 } while (0)
923
924#define REQUIRE_END_TAG(psz, szTag) \
925 do { \
926 if (!STARTS_WITH(psz, "</" szTag ">")) \
927 return psz; \
928 (psz) = RTStrStripL((psz) + sizeof("</" szTag ">") - 1); \
929 } while (0)
930
931
932/**
933 * Finds the next tag end.
934 *
935 * @returns Pointer to a '>' or '\0'.
936 * @param pszCur The current position.
937 */
938static const char *dmgXmlFindTagEnd(const char *pszCur)
939{
940 /* Might want to take quoted '>' into account? */
941 char ch;
942 while ((ch = *pszCur) != '\0' && ch != '>')
943 pszCur++;
944 return pszCur;
945}
946
947
948/**
949 * Finds the end tag.
950 *
951 * Does not deal with @verbatim<tag attr="1"/>@endverbatim style tags.
952 *
953 * @returns Pointer to the first char in the end tag. NULL if another tag
954 * was encountered first or if we hit the end of the file.
955 * @param ppszCur The current position (IN/OUT).
956 * @param pszTag The tag name.
957 */
958static const char *dmgXmlFindEndTag(const char **ppszCur, const char *pszTag)
959{
960 const char *psz = *ppszCur;
961 char ch;
962 while ((ch = *psz))
963 {
964 if (ch == '<')
965 {
966 size_t const cchTag = strlen(pszTag);
967 if ( psz[1] == '/'
968 && !memcmp(&psz[2], pszTag, cchTag)
969 && psz[2 + cchTag] == '>')
970 {
971 *ppszCur = psz + 2 + cchTag + 1;
972 return psz;
973 }
974 break;
975 }
976 psz++;
977 }
978 return NULL;
979}
980
981
982/**
983 * Reads a signed 32-bit value.
984 *
985 * @returns NULL on success, pointer to the offending text on failure.
986 * @param ppszCur The text position (IN/OUT).
987 * @param pi32 Where to store the value.
988 */
989static const char *dmgXmlParseS32(const char **ppszCur, int32_t *pi32)
990{
991 const char *psz = *ppszCur;
992
993 /*
994 * <string>-1</string>
995 */
996 REQUIRE_TAG_NO_STRIP(psz, "string");
997
998 char *pszNext;
999 int rc = RTStrToInt32Ex(psz, &pszNext, 0, pi32);
1000 if (rc != VWRN_TRAILING_CHARS)
1001 return *ppszCur;
1002 psz = pszNext;
1003
1004 REQUIRE_END_TAG(psz, "string");
1005 *ppszCur = psz;
1006 return NULL;
1007}
1008
1009
1010/**
1011 * Reads an unsigned 32-bit value.
1012 *
1013 * @returns NULL on success, pointer to the offending text on failure.
1014 * @param ppszCur The text position (IN/OUT).
1015 * @param pu32 Where to store the value.
1016 */
1017static const char *dmgXmlParseU32(const char **ppszCur, uint32_t *pu32)
1018{
1019 const char *psz = *ppszCur;
1020
1021 /*
1022 * <string>0x00ff</string>
1023 */
1024 REQUIRE_TAG_NO_STRIP(psz, "string");
1025
1026 char *pszNext;
1027 int rc = RTStrToUInt32Ex(psz, &pszNext, 0, pu32);
1028 if (rc != VWRN_TRAILING_CHARS)
1029 return *ppszCur;
1030 psz = pszNext;
1031
1032 REQUIRE_END_TAG(psz, "string");
1033 *ppszCur = psz;
1034 return NULL;
1035}
1036
1037
1038/**
1039 * Reads a string value.
1040 *
1041 * @returns NULL on success, pointer to the offending text on failure.
1042 * @param ppszCur The text position (IN/OUT).
1043 * @param ppszString Where to store the pointer to the string. The caller
1044 * must free this using RTMemFree.
1045 */
1046static const char *dmgXmlParseString(const char **ppszCur, char **ppszString)
1047{
1048 const char *psz = *ppszCur;
1049
1050 /*
1051 * <string>Driver Descriptor Map (DDM : 0)</string>
1052 */
1053 REQUIRE_TAG_NO_STRIP(psz, "string");
1054
1055 const char *pszStart = psz;
1056 const char *pszEnd = dmgXmlFindEndTag(&psz, "string");
1057 if (!pszEnd)
1058 return *ppszCur;
1059 psz = RTStrStripL(psz);
1060
1061 *ppszString = (char *)RTMemDupEx(pszStart, pszEnd - pszStart, 1);
1062 if (!*ppszString)
1063 return *ppszCur;
1064
1065 *ppszCur = psz;
1066 return NULL;
1067}
1068
1069
1070/**
1071 * Parses the BASE-64 coded data tags.
1072 *
1073 * @returns NULL on success, pointer to the offending text on failure.
1074 * @param ppszCur The text position (IN/OUT).
1075 * @param ppbData Where to store the pointer to the data we've read. The
1076 * caller must free this using RTMemFree.
1077 * @param pcbData The number of bytes we're returning.
1078 */
1079static const char *dmgXmlParseData(const char **ppszCur, uint8_t **ppbData, size_t *pcbData)
1080{
1081 const char *psz = *ppszCur;
1082
1083 /*
1084 * <data> AAAAA... </data>
1085 */
1086 REQUIRE_TAG(psz, "data");
1087
1088 const char *pszStart = psz;
1089 ssize_t cbData = RTBase64DecodedSize(pszStart, (char **)&psz);
1090 if (cbData == -1)
1091 return *ppszCur;
1092
1093 REQUIRE_END_TAG(psz, "data");
1094
1095 *ppbData = (uint8_t *)RTMemAlloc(cbData);
1096 if (!*ppbData)
1097 return *ppszCur;
1098 char *pszIgnored;
1099 int rc = RTBase64Decode(pszStart, *ppbData, cbData, pcbData, &pszIgnored);
1100 if (RT_FAILURE(rc))
1101 {
1102 RTMemFree(*ppbData);
1103 *ppbData = NULL;
1104 return *ppszCur;
1105 }
1106
1107 *ppszCur = psz;
1108 return NULL;
1109}
1110
1111
1112/**
1113 * Parses the XML resource-fork in a rather presumptive manner.
1114 *
1115 * This function is supposed to construct the DMG::aRsrcs instance data
1116 * parts.
1117 *
1118 * @returns NULL on success, pointer to the problematic text on failure.
1119 * @param pThis The DMG instance data.
1120 * @param pszXml The XML text to parse, UTF-8.
1121 */
1122static const char *dmgOpenXmlToRsrc(PDMGIMAGE pThis, char const *pszXml)
1123{
1124 const char *psz = pszXml;
1125
1126 /*
1127 * Verify the ?xml, !DOCTYPE and plist tags.
1128 */
1129 SKIP_AHEAD(psz, "");
1130
1131 /* <?xml version="1.0" encoding="UTF-8"?> */
1132 REQUIRE_WORD(psz, "<?xml");
1133 while (*psz != '?')
1134 {
1135 if (!*psz)
1136 return psz;
1137 if (STARTS_WITH_WORD(psz, "version="))
1138 {
1139 SKIP_AHEAD(psz, "version=");
1140 REQUIRE_WORD(psz, "\"1.0\"");
1141 }
1142 else if (STARTS_WITH_WORD(psz, "encoding="))
1143 {
1144 SKIP_AHEAD(psz, "encoding=");
1145 REQUIRE_WORD(psz, "\"UTF-8\"");
1146 }
1147 else
1148 return psz;
1149 }
1150 SKIP_AHEAD(psz, "?>");
1151
1152 /* <!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd"> */
1153 REQUIRE_WORD(psz, "<!DOCTYPE");
1154 REQUIRE_WORD(psz, "plist");
1155 REQUIRE_WORD(psz, "PUBLIC");
1156 psz = dmgXmlFindTagEnd(psz);
1157 REQUIRE_WORD(psz, ">");
1158
1159 /* <plist version="1.0"> */
1160 REQUIRE_WORD(psz, "<plist");
1161 REQUIRE_WORD(psz, "version=");
1162 REQUIRE_WORD(psz, "\"1.0\"");
1163 REQUIRE_WORD(psz, ">");
1164
1165 /*
1166 * Descend down to the 'resource-fork' dictionary.
1167 * ASSUME it's the only top level dictionary.
1168 */
1169 /* <dict> <key>resource-fork</key> */
1170 REQUIRE_TAG(psz, "dict");
1171 REQUIRE_WORD(psz, "<key>resource-fork</key>");
1172
1173 /*
1174 * Parse the keys in the resource-fork dictionary.
1175 * ASSUME that there are just two, 'blkx' and 'plst'.
1176 */
1177 REQUIRE_TAG(psz, "dict");
1178 while (!STARTS_WITH_WORD(psz, "</dict>"))
1179 {
1180 /*
1181 * Parse the key and Create the resource-fork entry.
1182 */
1183 unsigned iRsrc;
1184 if (STARTS_WITH_WORD(psz, "<key>blkx</key>"))
1185 {
1186 REQUIRE_WORD(psz, "<key>blkx</key>");
1187 iRsrc = DMG_RSRC_IDX_BLKX;
1188 strcpy(&pThis->aRsrcs[iRsrc].szName[0], "blkx");
1189 }
1190 else if (STARTS_WITH_WORD(psz, "<key>plst</key>"))
1191 {
1192 REQUIRE_WORD(psz, "<key>plst</key>");
1193 iRsrc = DMG_RSRC_IDX_PLST;
1194 strcpy(&pThis->aRsrcs[iRsrc].szName[0], "plst");
1195 }
1196 else
1197 {
1198 SKIP_AHEAD(psz, "</array>");
1199 continue;
1200 }
1201
1202
1203 /*
1204 * Descend into the array and add the elements to the resource entry.
1205 */
1206 /* <array> */
1207 REQUIRE_TAG(psz, "array");
1208 while (!STARTS_WITH_WORD(psz, "</array>"))
1209 {
1210 REQUIRE_TAG(psz, "dict");
1211 uint32_t i = pThis->aRsrcs[iRsrc].cEntries;
1212 if (i == RT_ELEMENTS(pThis->aRsrcs[iRsrc].aEntries))
1213 return psz;
1214
1215 while (!STARTS_WITH_WORD(psz, "</dict>"))
1216 {
1217
1218 /* switch on the key. */
1219 const char *pszErr;
1220 if (STARTS_WITH_WORD(psz, "<key>Attributes</key>"))
1221 {
1222 REQUIRE_WORD(psz, "<key>Attributes</key>");
1223 pszErr = dmgXmlParseU32(&psz, &pThis->aRsrcs[iRsrc].aEntries[i].fAttributes);
1224 }
1225 else if (STARTS_WITH_WORD(psz, "<key>ID</key>"))
1226 {
1227 REQUIRE_WORD(psz, "<key>ID</key>");
1228 pszErr = dmgXmlParseS32(&psz, &pThis->aRsrcs[iRsrc].aEntries[i].iId);
1229 }
1230 else if (STARTS_WITH_WORD(psz, "<key>Name</key>"))
1231 {
1232 REQUIRE_WORD(psz, "<key>Name</key>");
1233 pszErr = dmgXmlParseString(&psz, &pThis->aRsrcs[iRsrc].aEntries[i].pszName);
1234 }
1235 else if (STARTS_WITH_WORD(psz, "<key>CFName</key>"))
1236 {
1237 REQUIRE_WORD(psz, "<key>CFName</key>");
1238 pszErr = dmgXmlParseString(&psz, &pThis->aRsrcs[iRsrc].aEntries[i].pszCFName);
1239 }
1240 else if (STARTS_WITH_WORD(psz, "<key>Data</key>"))
1241 {
1242 REQUIRE_WORD(psz, "<key>Data</key>");
1243 pszErr = dmgXmlParseData(&psz, &pThis->aRsrcs[iRsrc].aEntries[i].pbData, &pThis->aRsrcs[iRsrc].aEntries[i].cbData);
1244 }
1245 else
1246 pszErr = psz;
1247 if (pszErr)
1248 return pszErr;
1249 } /* while not </dict> */
1250 REQUIRE_END_TAG(psz, "dict");
1251
1252 pThis->aRsrcs[iRsrc].cEntries++;
1253 } /* while not </array> */
1254 REQUIRE_END_TAG(psz, "array");
1255
1256 } /* while not </dict> */
1257 REQUIRE_END_TAG(psz, "dict");
1258
1259 /*
1260 * ASSUMING there is only the 'resource-fork', we'll now see the end of
1261 * the outer dict, plist and text.
1262 */
1263 /* </dict> </plist> */
1264 REQUIRE_END_TAG(psz, "dict");
1265 REQUIRE_END_TAG(psz, "plist");
1266
1267 /* the end */
1268 if (*psz)
1269 return psz;
1270
1271 return NULL;
1272}
1273
1274#undef REQUIRE_END_TAG
1275#undef REQUIRE_TAG_NO_STRIP
1276#undef REQUIRE_TAG
1277#undef REQUIRE_WORD
1278#undef SKIP_AHEAD
1279#undef STARTS_WITH_WORD
1280#undef STARTS_WITH
1281
1282/**
1283 * Returns the data attached to a resource.
1284 *
1285 * @returns VBox status code.
1286 * @param pThis The DMG instance data.
1287 * @param pcszRsrcName Name of the resource to get.
1288 * @param ppcRsrc Where to store the pointer to the resource data on success.
1289 */
1290static int dmgGetRsrcData(PDMGIMAGE pThis, const char *pcszRsrcName,
1291 PCDMGUDIFRSRCARRAY *ppcRsrc)
1292{
1293 int rc = VERR_NOT_FOUND;
1294
1295 for (unsigned i = 0; i < RT_ELEMENTS(pThis->aRsrcs); i++)
1296 {
1297 if (!strcmp(pThis->aRsrcs[i].szName, pcszRsrcName))
1298 {
1299 *ppcRsrc = &pThis->aRsrcs[i];
1300 rc = VINF_SUCCESS;
1301 break;
1302 }
1303 }
1304
1305 return rc;
1306}
1307
1308/**
1309 * Creates a new extent from the given blkx descriptor.
1310 *
1311 * @returns VBox status code.
1312 * @param pThis DMG instance data.
1313 * @param uSectorPart First sector the partition owning the blkx descriptor has.
1314 * @param pBlkxDesc The blkx descriptor.
1315 */
1316static int dmgExtentCreateFromBlkxDesc(PDMGIMAGE pThis, uint64_t uSectorPart, PDMGBLKXDESC pBlkxDesc)
1317{
1318 int rc = VINF_SUCCESS;
1319 DMGEXTENTTYPE enmExtentTypeNew;
1320 PDMGEXTENT pExtentNew = NULL;
1321
1322 if (pBlkxDesc->u32Type == DMGBLKXDESC_TYPE_RAW)
1323 enmExtentTypeNew = DMGEXTENTTYPE_RAW;
1324 else if (pBlkxDesc->u32Type == DMGBLKXDESC_TYPE_IGNORE)
1325 enmExtentTypeNew = DMGEXTENTTYPE_ZERO;
1326 else if (pBlkxDesc->u32Type == DMGBLKXDESC_TYPE_ZLIB)
1327 enmExtentTypeNew = DMGEXTENTTYPE_COMP_ZLIB;
1328 else
1329 {
1330 AssertMsgFailed(("This method supports only raw or zero extents!\n"));
1331 return VERR_NOT_SUPPORTED;
1332 }
1333
1334 /** @todo Merge raw extents if possible to save memory. */
1335#if 0
1336 pExtentNew = pThis->pExtentLast;
1337 if ( pExtentNew
1338 && pExtentNew->enmType == enmExtentTypeNew
1339 && enmExtentTypeNew == DMGEXTENTTYPE_RAW
1340 && pExtentNew->uSectorExtent + pExtentNew->cSectorsExtent == offDevice + pBlkxDesc->u64SectorStart * DMG_SECTOR_SIZE;
1341 && pExtentNew->offFileStart + pExtentNew->cbExtent == pBlkxDesc->offData)
1342 {
1343 /* Increase the last extent. */
1344 pExtentNew->cbExtent += pBlkxDesc->cbData;
1345 }
1346 else
1347#endif
1348 {
1349 if (pThis->cExtentsMax == pThis->cExtents)
1350 {
1351 pThis->cExtentsMax += 64;
1352
1353 /* Increase the array. */
1354 PDMGEXTENT paExtentsNew = (PDMGEXTENT)RTMemRealloc(pThis->paExtents, sizeof(DMGEXTENT) * pThis->cExtentsMax);
1355 if (!paExtentsNew)
1356 {
1357 rc = VERR_NO_MEMORY;
1358 pThis->cExtentsMax -= 64;
1359 }
1360 else
1361 pThis->paExtents = paExtentsNew;
1362 }
1363
1364 if (RT_SUCCESS(rc))
1365 {
1366 pExtentNew = &pThis->paExtents[pThis->cExtents++];
1367
1368 pExtentNew->enmType = enmExtentTypeNew;
1369 pExtentNew->uSectorExtent = uSectorPart + pBlkxDesc->u64SectorStart;
1370 pExtentNew->cSectorsExtent = pBlkxDesc->u64SectorCount;
1371 pExtentNew->offFileStart = pBlkxDesc->offData;
1372 pExtentNew->cbFile = pBlkxDesc->cbData;
1373 }
1374 }
1375
1376 return rc;
1377}
1378
1379/**
1380 * Find the extent for the given sector number.
1381 */
1382static PDMGEXTENT dmgExtentGetFromOffset(PDMGIMAGE pThis, uint64_t uSector)
1383{
1384 /*
1385 * We assume that the array is ordered from lower to higher sector
1386 * numbers.
1387 * This makes it possible to bisect the array to find the extent
1388 * faster than using a linked list.
1389 */
1390 PDMGEXTENT pExtent = NULL;
1391 unsigned idxCur = pThis->idxExtentLast;
1392 unsigned idxMax = pThis->cExtents;
1393 unsigned idxMin = 0;
1394
1395 while (idxMin < idxMax)
1396 {
1397 PDMGEXTENT pExtentCur = &pThis->paExtents[idxCur];
1398
1399 /* Determine the search direction. */
1400 if (uSector < pExtentCur->uSectorExtent)
1401 {
1402 /* Search left from the current extent. */
1403 idxMax = idxCur;
1404 }
1405 else if (uSector >= pExtentCur->uSectorExtent + pExtentCur->cSectorsExtent)
1406 {
1407 /* Search right from the current extent. */
1408 idxMin = idxCur;
1409 }
1410 else
1411 {
1412 /* The sector lies in the extent, stop searching. */
1413 pExtent = pExtentCur;
1414 break;
1415 }
1416
1417 idxCur = idxMin + (idxMax - idxMin) / 2;
1418 }
1419
1420 if (pExtent)
1421 pThis->idxExtentLast = idxCur;
1422
1423 return pExtent;
1424}
1425
1426/**
1427 * Goes through the BLKX structure and creates the necessary extents.
1428 */
1429static int dmgBlkxParse(PDMGIMAGE pThis, PDMGBLKX pBlkx)
1430{
1431 int rc = VINF_SUCCESS;
1432 PDMGBLKXDESC pBlkxDesc = (PDMGBLKXDESC)(pBlkx + 1);
1433
1434 for (unsigned i = 0; i < pBlkx->cBlocksRunCount; i++)
1435 {
1436 dmgBlkxDescFile2HostEndian(pBlkxDesc);
1437
1438 switch (pBlkxDesc->u32Type)
1439 {
1440 case DMGBLKXDESC_TYPE_RAW:
1441 case DMGBLKXDESC_TYPE_IGNORE:
1442 case DMGBLKXDESC_TYPE_ZLIB:
1443 {
1444 rc = dmgExtentCreateFromBlkxDesc(pThis, pBlkx->cSectornumberFirst, pBlkxDesc);
1445 break;
1446 }
1447 case DMGBLKXDESC_TYPE_COMMENT:
1448 case DMGBLKXDESC_TYPE_TERMINATOR:
1449 break;
1450 default:
1451 rc = VERR_VD_DMG_INVALID_HEADER;
1452 break;
1453 }
1454
1455 if ( pBlkxDesc->u32Type == DMGBLKXDESC_TYPE_TERMINATOR
1456 || RT_FAILURE(rc))
1457 break;
1458
1459 pBlkxDesc++;
1460 }
1461
1462 return rc;
1463}
1464
1465
1466/**
1467 * Worker for dmgOpenImage that tries to open a DMG inside a XAR file.
1468 *
1469 * We'll select the first .dmg inside the archive that we can get a file
1470 * interface to.
1471 *
1472 * @returns VBox status code.
1473 * @param fOpen Flags for defining the open type.
1474 * @param pVDIfIoInt The internal VD I/O interface to use.
1475 * @param pvStorage The storage pointer that goes with @a pVDIfsIo.
1476 * @param pszFilename The input filename, optional.
1477 * @param phXarFss Where to return the XAR file system stream handle on
1478 * success
1479 * @param phDmgFileInXar Where to return the VFS handle to the DMG file
1480 * within the XAR image on success.
1481 *
1482 * @remarks Not using the PDMGIMAGE structure directly here because the function
1483 * is being in serveral places.
1484 */
1485static int dmgOpenImageWithinXar(uint32_t fOpen, PVDINTERFACEIOINT pVDIfIoInt, void *pvStorage, const char *pszFilename,
1486 PRTVFSFSSTREAM phXarFss, PRTVFSFILE phDmgFileInXar)
1487{
1488 /*
1489 * Open the XAR file stream.
1490 */
1491 RTVFSFILE hVfsFile;
1492#ifdef VBOX_WITH_DIRECT_XAR_ACCESS
1493 int rc = RTVfsFileOpenNormal(pszFilename, fOpen, &hVfsFile);
1494#else
1495 int rc = VDIfCreateVfsFile(NULL, pVDIfIoInt, pvStorage, fOpen, &hVfsFile);
1496#endif
1497 if (RT_FAILURE(rc))
1498 return rc;
1499
1500 RTVFSIOSTREAM hVfsIos = RTVfsFileToIoStream(hVfsFile);
1501 RTVfsFileRelease(hVfsFile);
1502
1503 RTVFSFSSTREAM hXarFss;
1504 rc = RTZipXarFsStreamFromIoStream(hVfsIos, 0 /*fFlags*/, &hXarFss);
1505 RTVfsIoStrmRelease(hVfsIos);
1506 if (RT_FAILURE(rc))
1507 return rc;
1508
1509 /*
1510 * Look for a DMG in the stream that we can use.
1511 */
1512 for (;;)
1513 {
1514 char *pszName;
1515 RTVFSOBJTYPE enmType;
1516 RTVFSOBJ hVfsObj;
1517 rc = RTVfsFsStrmNext(hXarFss, &pszName, &enmType, &hVfsObj);
1518 if (RT_FAILURE(rc))
1519 break;
1520
1521 /* It must be a file object so it can be seeked, this also implies that
1522 it's uncompressed. Then it must have the .dmg suffix. */
1523 if (enmType == RTVFSOBJTYPE_FILE)
1524 {
1525 size_t cchName = strlen(pszName);
1526 const char *pszSuff = pszName + cchName - 4;
1527 if ( cchName >= 4
1528 && pszSuff[0] == '.'
1529 && (pszSuff[1] == 'd' || pszSuff[1] == 'D')
1530 && (pszSuff[2] == 'm' || pszSuff[2] == 'M')
1531 && (pszSuff[3] == 'g' || pszSuff[3] == 'G'))
1532 {
1533 RTVFSFILE hDmgFileInXar = RTVfsObjToFile(hVfsObj);
1534 AssertBreakStmt(hDmgFileInXar != NIL_RTVFSFILE, rc = VERR_INTERNAL_ERROR_3);
1535
1536 if (pszFilename)
1537 DMG_PRINTF(("DMG: Using '%s' within XAR file '%s'...\n", pszName, pszFilename));
1538 *phXarFss = hXarFss;
1539 *phDmgFileInXar = hDmgFileInXar;
1540
1541 RTStrFree(pszName);
1542 RTVfsObjRelease(hVfsObj);
1543
1544 return VINF_SUCCESS;
1545 }
1546 }
1547
1548 /* Release the current return values. */
1549 RTStrFree(pszName);
1550 RTVfsObjRelease(hVfsObj);
1551 }
1552
1553 /* Not found or some kind of error. */
1554 RTVfsFsStrmRelease(hXarFss);
1555 if (rc == VERR_EOF)
1556 rc = VERR_VD_DMG_NOT_FOUND_INSIDE_XAR;
1557 AssertStmt(RT_FAILURE_NP(rc), rc = VERR_INTERNAL_ERROR_4);
1558 return rc;
1559}
1560
1561
1562/**
1563 * Worker for dmgOpen that reads in and validates all the necessary
1564 * structures from the image.
1565 *
1566 * @returns VBox status code.
1567 * @param pThis The DMG instance data.
1568 * @param uOpenFlags Flags for defining the open type.
1569 */
1570static DECLCALLBACK(int) dmgOpenImage(PDMGIMAGE pThis, unsigned uOpenFlags)
1571{
1572 pThis->uOpenFlags = uOpenFlags;
1573
1574 pThis->pIfError = VDIfErrorGet(pThis->pVDIfsDisk);
1575 pThis->pIfIoXxx = VDIfIoIntGet(pThis->pVDIfsImage);
1576 pThis->hDmgFileInXar = NIL_RTVFSFILE;
1577 pThis->hXarFss = NIL_RTVFSFSSTREAM;
1578 AssertPtrReturn(pThis->pIfIoXxx, VERR_INVALID_PARAMETER);
1579
1580 int rc = vdIfIoIntFileOpen(pThis->pIfIoXxx, pThis->pszFilename,
1581 VDOpenFlagsToFileOpenFlags(uOpenFlags, false /* fCreate */),
1582 &pThis->pStorage);
1583 if (RT_FAILURE(rc))
1584 {
1585 /* Do NOT signal an appropriate error here, as the VD layer has the
1586 * choice of retrying the open if it failed. */
1587 return rc;
1588 }
1589
1590 /*
1591 * Check for XAR archive.
1592 */
1593 uint32_t u32XarMagic;
1594 rc = dmgWrapFileReadSync(pThis, 0, &u32XarMagic, sizeof(u32XarMagic));
1595 if (RT_FAILURE(rc))
1596 return rc;
1597 if (u32XarMagic == XAR_HEADER_MAGIC)
1598 {
1599 rc = dmgOpenImageWithinXar(VDOpenFlagsToFileOpenFlags(uOpenFlags, false /* fCreate */),
1600 pThis->pIfIoXxx,
1601 pThis->pStorage,
1602 pThis->pszFilename,
1603 &pThis->hXarFss, &pThis->hDmgFileInXar);
1604 if (RT_FAILURE(rc))
1605 return rc;
1606#ifdef VBOX_WITH_DIRECT_XAR_ACCESS
1607 vdIfIoIntFileClose(pThis->pIfIoXxx, pThis->pStorage);
1608 pThis->pStorage = NULL;
1609#endif
1610 }
1611#if 0 /* This is for testing whether the VFS wrappers actually works. */
1612 else
1613 {
1614 rc = RTVfsFileOpenNormal(pThis->pszFilename, VDOpenFlagsToFileOpenFlags(uOpenFlags, false /* fCreate */),
1615 &pThis->hDmgFileInXar);
1616 if (RT_FAILURE(rc))
1617 return rc;
1618 vdIfIoIntFileClose(pThis->pIfIoXxx, pThis->pStorage);
1619 pThis->pStorage = NULL;
1620 }
1621#endif
1622
1623 /*
1624 * Read the footer.
1625 */
1626 rc = dmgWrapFileGetSize(pThis, &pThis->cbFile);
1627 if (RT_FAILURE(rc))
1628 return rc;
1629 if (pThis->cbFile < 1024)
1630 return VERR_VD_DMG_INVALID_HEADER;
1631 rc = dmgWrapFileReadSync(pThis, pThis->cbFile - sizeof(pThis->Ftr), &pThis->Ftr, sizeof(pThis->Ftr));
1632 if (RT_FAILURE(rc))
1633 return rc;
1634 dmgUdifFtrFile2HostEndian(&pThis->Ftr);
1635
1636 /*
1637 * Do we recognize the footer structure? If so, is it valid?
1638 */
1639 if (pThis->Ftr.u32Magic != DMGUDIF_MAGIC)
1640 return VERR_VD_DMG_INVALID_HEADER;
1641 if (pThis->Ftr.u32Version != DMGUDIF_VER_CURRENT)
1642 return VERR_VD_DMG_INVALID_HEADER;
1643 if (pThis->Ftr.cbFooter != sizeof(pThis->Ftr))
1644 return VERR_VD_DMG_INVALID_HEADER;
1645
1646 if (!dmgUdifFtrIsValid(&pThis->Ftr, pThis->cbFile - sizeof(pThis->Ftr)))
1647 {
1648 DMG_PRINTF(("Bad DMG: '%s' cbFile=%RTfoff\n", pThis->pszFilename, pThis->cbFile));
1649 return VERR_VD_DMG_INVALID_HEADER;
1650 }
1651
1652 pThis->cbSize = pThis->Ftr.cSectors * DMG_SECTOR_SIZE;
1653
1654 /*
1655 * Read and parse the XML portion.
1656 */
1657 size_t cchXml = (size_t)pThis->Ftr.cbXml;
1658 char *pszXml = (char *)RTMemAlloc(cchXml + 1);
1659 if (!pszXml)
1660 return VERR_NO_MEMORY;
1661 rc = dmgWrapFileReadSync(pThis, pThis->Ftr.offXml, pszXml, cchXml);
1662 if (RT_SUCCESS(rc))
1663 {
1664 pszXml[cchXml] = '\0';
1665 const char *pszError = dmgOpenXmlToRsrc(pThis, pszXml);
1666 if (!pszError)
1667 {
1668 PCDMGUDIFRSRCARRAY pRsrcBlkx = NULL;
1669
1670 rc = dmgGetRsrcData(pThis, "blkx", &pRsrcBlkx);
1671 if (RT_SUCCESS(rc))
1672 {
1673 for (unsigned idxBlkx = 0; idxBlkx < pRsrcBlkx->cEntries; idxBlkx++)
1674 {
1675 PDMGBLKX pBlkx = NULL;
1676
1677 if (pRsrcBlkx->aEntries[idxBlkx].cbData < sizeof(DMGBLKX))
1678 {
1679 rc = VERR_VD_DMG_INVALID_HEADER;
1680 break;
1681 }
1682
1683 pBlkx = (PDMGBLKX)RTMemAllocZ(pRsrcBlkx->aEntries[idxBlkx].cbData);
1684 if (!pBlkx)
1685 {
1686 rc = VERR_NO_MEMORY;
1687 break;
1688 }
1689
1690 memcpy(pBlkx, pRsrcBlkx->aEntries[idxBlkx].pbData, pRsrcBlkx->aEntries[idxBlkx].cbData);
1691
1692 dmgBlkxFile2HostEndian(pBlkx);
1693
1694 if ( dmgBlkxIsValid(pBlkx)
1695 && pRsrcBlkx->aEntries[idxBlkx].cbData == pBlkx->cBlocksRunCount * sizeof(DMGBLKXDESC) + sizeof(DMGBLKX))
1696 rc = dmgBlkxParse(pThis, pBlkx);
1697 else
1698 rc = VERR_VD_DMG_INVALID_HEADER;
1699
1700 RTMemFree(pBlkx);
1701
1702 if (RT_FAILURE(rc))
1703 break;
1704 }
1705 }
1706 else
1707 rc = VERR_VD_DMG_INVALID_HEADER;
1708 }
1709 else
1710 {
1711 DMG_PRINTF(("**** XML DUMP BEGIN ***\n%s\n**** XML DUMP END ****\n", pszXml));
1712 DMG_PRINTF(("**** Bad XML at %#lx (%lu) ***\n%.256s\n**** Bad XML END ****\n",
1713 (unsigned long)(pszError - pszXml), (unsigned long)(pszError - pszXml), pszError));
1714 rc = VERR_VD_DMG_XML_PARSE_ERROR;
1715 }
1716 }
1717 RTMemFree(pszXml);
1718
1719 if (RT_FAILURE(rc))
1720 dmgFreeImage(pThis, false);
1721 return rc;
1722}
1723
1724
1725/** @interface_method_impl{VDIMAGEBACKEND,pfnProbe} */
1726static DECLCALLBACK(int) dmgProbe(const char *pszFilename, PVDINTERFACE pVDIfsDisk,
1727 PVDINTERFACE pVDIfsImage, VDTYPE *penmType)
1728{
1729 RT_NOREF1(pVDIfsDisk);
1730 LogFlowFunc(("pszFilename=\"%s\" pVDIfsDisk=%#p pVDIfsImage=%#p penmType=%#p\n",
1731 pszFilename, pVDIfsDisk, pVDIfsImage, penmType));
1732
1733 PVDINTERFACEIOINT pIfIo = VDIfIoIntGet(pVDIfsImage);
1734 AssertPtrReturn(pIfIo, VERR_INVALID_PARAMETER);
1735
1736 /*
1737 * Open the file and check for XAR.
1738 */
1739 PVDIOSTORAGE pStorage = NULL;
1740 int rc = vdIfIoIntFileOpen(pIfIo, pszFilename,
1741 VDOpenFlagsToFileOpenFlags(VD_OPEN_FLAGS_READONLY, false /* fCreate */),
1742 &pStorage);
1743 if (RT_FAILURE(rc))
1744 {
1745 LogFlowFunc(("returns %Rrc (error opening file)\n", rc));
1746 return rc;
1747 }
1748
1749 /*
1750 * Check for XAR file.
1751 */
1752 RTVFSFSSTREAM hXarFss = NIL_RTVFSFSSTREAM;
1753 RTVFSFILE hDmgFileInXar = NIL_RTVFSFILE;
1754 uint32_t u32XarMagic;
1755 rc = vdIfIoIntFileReadSync(pIfIo, pStorage, 0, &u32XarMagic, sizeof(u32XarMagic));
1756 if ( RT_SUCCESS(rc)
1757 && u32XarMagic == XAR_HEADER_MAGIC)
1758 {
1759 rc = dmgOpenImageWithinXar(RTFILE_O_OPEN | RTFILE_O_READ | RTFILE_O_DENY_WRITE,
1760 pIfIo, pStorage, pszFilename,
1761 &hXarFss, &hDmgFileInXar);
1762 if (RT_FAILURE(rc))
1763 return rc;
1764 }
1765
1766 /*
1767 * Read the DMG footer.
1768 */
1769 uint64_t cbFile;
1770 if (hDmgFileInXar == NIL_RTVFSFILE)
1771 rc = vdIfIoIntFileGetSize(pIfIo, pStorage, &cbFile);
1772 else
1773 rc = RTVfsFileGetSize(hDmgFileInXar, &cbFile);
1774 if (RT_SUCCESS(rc))
1775 {
1776 DMGUDIF Ftr;
1777 uint64_t offFtr = cbFile - sizeof(Ftr);
1778 if (hDmgFileInXar == NIL_RTVFSFILE)
1779 rc = vdIfIoIntFileReadSync(pIfIo, pStorage, offFtr, &Ftr, sizeof(Ftr));
1780 else
1781 rc = RTVfsFileReadAt(hDmgFileInXar, offFtr, &Ftr, sizeof(Ftr), NULL);
1782 if (RT_SUCCESS(rc))
1783 {
1784 /*
1785 * Do we recognize this stuff? Does it look valid?
1786 */
1787 if ( Ftr.u32Magic == RT_H2BE_U32_C(DMGUDIF_MAGIC)
1788 && Ftr.u32Version == RT_H2BE_U32_C(DMGUDIF_VER_CURRENT)
1789 && Ftr.cbFooter == RT_H2BE_U32_C(sizeof(Ftr)))
1790 {
1791 dmgUdifFtrFile2HostEndian(&Ftr);
1792 if (dmgUdifFtrIsValid(&Ftr, offFtr))
1793 {
1794 rc = VINF_SUCCESS;
1795 *penmType = VDTYPE_DVD;
1796 }
1797 else
1798 {
1799 DMG_PRINTF(("Bad DMG: '%s' offFtr=%RTfoff\n", pszFilename, offFtr));
1800 rc = VERR_VD_DMG_INVALID_HEADER;
1801 }
1802 }
1803 else
1804 rc = VERR_VD_DMG_INVALID_HEADER;
1805 }
1806 }
1807 else
1808 rc = VERR_VD_DMG_INVALID_HEADER;
1809
1810 /* Clean up. */
1811 RTVfsFileRelease(hDmgFileInXar);
1812 RTVfsFsStrmRelease(hXarFss);
1813 vdIfIoIntFileClose(pIfIo, pStorage);
1814
1815 LogFlowFunc(("returns %Rrc\n", rc));
1816 return rc;
1817}
1818
1819/** @interface_method_impl{VDIMAGEBACKEND,pfnOpen} */
1820static DECLCALLBACK(int) dmgOpen(const char *pszFilename, unsigned uOpenFlags,
1821 PVDINTERFACE pVDIfsDisk, PVDINTERFACE pVDIfsImage,
1822 VDTYPE enmType, void **ppBackendData)
1823{
1824 LogFlowFunc(("pszFilename=\"%s\" uOpenFlags=%#x pVDIfsDisk=%#p pVDIfsImage=%#p enmType=%u ppBackendData=%#p\n", pszFilename, uOpenFlags, pVDIfsDisk, pVDIfsImage, enmType, ppBackendData));
1825
1826 NOREF(enmType); /**< @todo r=klaus make use of the type info. */
1827
1828 /* Check open flags. All valid flags are (in principle) supported. */
1829 AssertReturn(!(uOpenFlags & ~VD_OPEN_FLAGS_MASK), VERR_INVALID_PARAMETER);
1830
1831 /* Check remaining arguments. */
1832 AssertPtrReturn(pszFilename, VERR_INVALID_POINTER);
1833 AssertReturn(*pszFilename, VERR_INVALID_PARAMETER);
1834
1835 /*
1836 * Reject combinations we don't currently support.
1837 *
1838 * There is no point in being paranoid about the input here as we're just a
1839 * simple backend and can expect the caller to be the only user and already
1840 * have validate what it passes thru to us.
1841 */
1842 if ( !(uOpenFlags & VD_OPEN_FLAGS_READONLY)
1843 || (uOpenFlags & VD_OPEN_FLAGS_ASYNC_IO))
1844 {
1845 LogFlowFunc(("Unsupported flag(s): %#x\n", uOpenFlags));
1846 return VERR_INVALID_PARAMETER;
1847 }
1848
1849 /*
1850 * Create the basic instance data structure and open the file,
1851 * then hand it over to a worker function that does all the rest.
1852 */
1853 int rc = VERR_NO_MEMORY;
1854 PDMGIMAGE pThis = (PDMGIMAGE)RTMemAllocZ(sizeof(*pThis));
1855 if (pThis)
1856 {
1857 pThis->pszFilename = pszFilename;
1858 pThis->pStorage = NULL;
1859 pThis->pVDIfsDisk = pVDIfsDisk;
1860 pThis->pVDIfsImage = pVDIfsImage;
1861
1862 rc = dmgOpenImage(pThis, uOpenFlags);
1863 if (RT_SUCCESS(rc))
1864 *ppBackendData = pThis;
1865 else
1866 RTMemFree(pThis);
1867 }
1868
1869 LogFlowFunc(("returns %Rrc (pBackendData=%#p)\n", rc, *ppBackendData));
1870 return rc;
1871}
1872
1873/** @interface_method_impl{VDIMAGEBACKEND,pfnCreate} */
1874static DECLCALLBACK(int) dmgCreate(const char *pszFilename, uint64_t cbSize,
1875 unsigned uImageFlags, const char *pszComment,
1876 PCVDGEOMETRY pPCHSGeometry, PCVDGEOMETRY pLCHSGeometry,
1877 PCRTUUID pUuid, unsigned uOpenFlags,
1878 unsigned uPercentStart, unsigned uPercentSpan,
1879 PVDINTERFACE pVDIfsDisk, PVDINTERFACE pVDIfsImage,
1880 PVDINTERFACE pVDIfsOperation, VDTYPE enmType,
1881 void **ppBackendData)
1882{
1883 RT_NOREF8(pszFilename, cbSize, uImageFlags, pszComment, pPCHSGeometry, pLCHSGeometry, pUuid, uOpenFlags);
1884 RT_NOREF7(uPercentStart, uPercentSpan, pVDIfsDisk, pVDIfsImage, pVDIfsOperation, enmType, ppBackendData);
1885 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",
1886 pszFilename, cbSize, uImageFlags, pszComment, pPCHSGeometry, pLCHSGeometry, pUuid, uOpenFlags, uPercentStart, uPercentSpan, pVDIfsDisk, pVDIfsImage, pVDIfsOperation, enmType, ppBackendData));
1887 int rc = VERR_NOT_SUPPORTED;
1888
1889 LogFlowFunc(("returns %Rrc\n", rc));
1890 return rc;
1891}
1892
1893/** @interface_method_impl{VDIMAGEBACKEND,pfnRename} */
1894static DECLCALLBACK(int) dmgRename(void *pBackendData, const char *pszFilename)
1895{
1896 RT_NOREF2(pBackendData, pszFilename);
1897 LogFlowFunc(("pBackendData=%#p pszFilename=%#p\n", pBackendData, pszFilename));
1898 int rc = VERR_NOT_SUPPORTED;
1899
1900 LogFlowFunc(("returns %Rrc\n", rc));
1901 return rc;
1902}
1903
1904/** @interface_method_impl{VDIMAGEBACKEND,pfnClose} */
1905static DECLCALLBACK(int) dmgClose(void *pBackendData, bool fDelete)
1906{
1907 LogFlowFunc(("pBackendData=%#p fDelete=%d\n", pBackendData, fDelete));
1908 PDMGIMAGE pThis = (PDMGIMAGE)pBackendData;
1909
1910 int rc = dmgFreeImage(pThis, fDelete);
1911 RTMemFree(pThis);
1912
1913 LogFlowFunc(("returns %Rrc\n", rc));
1914 return rc;
1915}
1916
1917/** @interface_method_impl{VDIMAGEBACKEND,pfnRead} */
1918static DECLCALLBACK(int) dmgRead(void *pBackendData, uint64_t uOffset, size_t cbToRead,
1919 PVDIOCTX pIoCtx, size_t *pcbActuallyRead)
1920{
1921 LogFlowFunc(("pBackendData=%#p uOffset=%llu pIoCtx=%#p cbToRead=%zu pcbActuallyRead=%#p\n",
1922 pBackendData, uOffset, pIoCtx, cbToRead, pcbActuallyRead));
1923 PDMGIMAGE pThis = (PDMGIMAGE)pBackendData;
1924 PDMGEXTENT pExtent = NULL;
1925 int rc = VINF_SUCCESS;
1926
1927 AssertPtr(pThis);
1928 Assert(uOffset % DMG_SECTOR_SIZE == 0);
1929 Assert(cbToRead % DMG_SECTOR_SIZE == 0);
1930
1931 if ( uOffset + cbToRead > pThis->cbSize
1932 || cbToRead == 0)
1933 {
1934 LogFlowFunc(("returns VERR_INVALID_PARAMETER\n"));
1935 return VERR_INVALID_PARAMETER;
1936 }
1937
1938 pExtent = dmgExtentGetFromOffset(pThis, DMG_BYTE2BLOCK(uOffset));
1939
1940 if (pExtent)
1941 {
1942 uint64_t uExtentRel = DMG_BYTE2BLOCK(uOffset) - pExtent->uSectorExtent;
1943
1944 /* Remain in this extent. */
1945 cbToRead = RT_MIN(cbToRead, DMG_BLOCK2BYTE(pExtent->cSectorsExtent - uExtentRel));
1946
1947 switch (pExtent->enmType)
1948 {
1949 case DMGEXTENTTYPE_RAW:
1950 {
1951 rc = dmgWrapFileReadUser(pThis, pExtent->offFileStart + DMG_BLOCK2BYTE(uExtentRel), pIoCtx, cbToRead);
1952 break;
1953 }
1954 case DMGEXTENTTYPE_ZERO:
1955 {
1956 vdIfIoIntIoCtxSet(pThis->pIfIoXxx, pIoCtx, 0, cbToRead);
1957 break;
1958 }
1959 case DMGEXTENTTYPE_COMP_ZLIB:
1960 {
1961 if (pThis->pExtentDecomp != pExtent)
1962 {
1963 if (DMG_BLOCK2BYTE(pExtent->cSectorsExtent) > pThis->cbDecompExtent)
1964 {
1965 if (RT_LIKELY(pThis->pvDecompExtent))
1966 RTMemFree(pThis->pvDecompExtent);
1967
1968 pThis->pvDecompExtent = RTMemAllocZ(DMG_BLOCK2BYTE(pExtent->cSectorsExtent));
1969 if (!pThis->pvDecompExtent)
1970 rc = VERR_NO_MEMORY;
1971 else
1972 pThis->cbDecompExtent = DMG_BLOCK2BYTE(pExtent->cSectorsExtent);
1973 }
1974
1975 if (RT_SUCCESS(rc))
1976 {
1977 rc = dmgFileInflateSync(pThis, pExtent->offFileStart, pExtent->cbFile,
1978 pThis->pvDecompExtent,
1979 RT_MIN(pThis->cbDecompExtent, DMG_BLOCK2BYTE(pExtent->cSectorsExtent)));
1980 if (RT_SUCCESS(rc))
1981 pThis->pExtentDecomp = pExtent;
1982 }
1983 }
1984
1985 if (RT_SUCCESS(rc))
1986 vdIfIoIntIoCtxCopyTo(pThis->pIfIoXxx, pIoCtx,
1987 (uint8_t *)pThis->pvDecompExtent + DMG_BLOCK2BYTE(uExtentRel),
1988 cbToRead);
1989 break;
1990 }
1991 default:
1992 AssertMsgFailed(("Invalid extent type\n"));
1993 }
1994
1995 if (RT_SUCCESS(rc))
1996 *pcbActuallyRead = cbToRead;
1997 }
1998 else
1999 rc = VERR_INVALID_PARAMETER;
2000
2001 LogFlowFunc(("returns %Rrc\n", rc));
2002 return rc;
2003}
2004
2005/** @interface_method_impl{VDIMAGEBACKEND,pfnWrite} */
2006static DECLCALLBACK(int) dmgWrite(void *pBackendData, uint64_t uOffset, size_t cbToWrite,
2007 PVDIOCTX pIoCtx, size_t *pcbWriteProcess, size_t *pcbPreRead,
2008 size_t *pcbPostRead, unsigned fWrite)
2009{
2010 RT_NOREF7(uOffset, cbToWrite, pIoCtx, pcbWriteProcess, pcbPreRead, pcbPostRead, fWrite);
2011 LogFlowFunc(("pBackendData=%#p uOffset=%llu pIoCtx=%#p cbToWrite=%zu pcbWriteProcess=%#p pcbPreRead=%#p pcbPostRead=%#p\n",
2012 pBackendData, uOffset, pIoCtx, cbToWrite, pcbWriteProcess, pcbPreRead, pcbPostRead));
2013 PDMGIMAGE pThis = (PDMGIMAGE)pBackendData;
2014 int rc = VERR_NOT_IMPLEMENTED;
2015
2016 AssertPtr(pThis);
2017 Assert(uOffset % 512 == 0);
2018 Assert(cbToWrite % 512 == 0);
2019
2020 if (!(pThis->uOpenFlags & VD_OPEN_FLAGS_READONLY))
2021 AssertMsgFailed(("Not implemented\n"));
2022 else
2023 rc = VERR_VD_IMAGE_READ_ONLY;
2024
2025 LogFlowFunc(("returns %Rrc\n", rc));
2026 return rc;
2027}
2028
2029/** @interface_method_impl{VDIMAGEBACKEND,pfnFlush} */
2030static DECLCALLBACK(int) dmgFlush(void *pBackendData, PVDIOCTX pIoCtx)
2031{
2032 RT_NOREF1(pIoCtx);
2033 LogFlowFunc(("pBackendData=%#p\n", pBackendData));
2034 PDMGIMAGE pThis = (PDMGIMAGE)pBackendData;
2035 int rc;
2036
2037 AssertPtr(pThis);
2038
2039 rc = dmgFlushImage(pThis);
2040
2041 LogFlowFunc(("returns %Rrc\n", rc));
2042 return rc;
2043}
2044
2045/** @interface_method_impl{VDIMAGEBACKEND,pfnGetVersion} */
2046static DECLCALLBACK(unsigned) dmgGetVersion(void *pBackendData)
2047{
2048 LogFlowFunc(("pBackendData=%#p\n", pBackendData));
2049 PDMGIMAGE pThis = (PDMGIMAGE)pBackendData;
2050
2051 AssertPtrReturn(pThis, 0);
2052
2053 return 1;
2054}
2055
2056/** @interface_method_impl{VDIMAGEBACKEND,pfnGetSectorSize} */
2057static DECLCALLBACK(uint32_t) dmgGetSectorSize(void *pBackendData)
2058{
2059 LogFlowFunc(("pBackendData=%#p\n", pBackendData));
2060 PDMGIMAGE pThis = (PDMGIMAGE)pBackendData;
2061
2062 AssertPtrReturn(pThis, 0);
2063
2064 uint32_t cb = 0;
2065 if (pThis->pStorage || pThis->hDmgFileInXar != NIL_RTVFSFILE)
2066 cb = 2048;
2067
2068 LogFlowFunc(("returns %u\n", cb));
2069 return cb;
2070}
2071
2072/** @interface_method_impl{VDIMAGEBACKEND,pfnGetSize} */
2073static DECLCALLBACK(uint64_t) dmgGetSize(void *pBackendData)
2074{
2075 LogFlowFunc(("pBackendData=%#p\n", pBackendData));
2076 PDMGIMAGE pThis = (PDMGIMAGE)pBackendData;
2077
2078 AssertPtrReturn(pThis, 0);
2079
2080 uint64_t cb = 0;
2081 if (pThis->pStorage || pThis->hDmgFileInXar != NIL_RTVFSFILE)
2082 cb = pThis->cbSize;
2083
2084 LogFlowFunc(("returns %llu\n", cb));
2085 return cb;
2086}
2087
2088/** @interface_method_impl{VDIMAGEBACKEND,pfnGetFileSize} */
2089static DECLCALLBACK(uint64_t) dmgGetFileSize(void *pBackendData)
2090{
2091 LogFlowFunc(("pBackendData=%#p\n", pBackendData));
2092 PDMGIMAGE pThis = (PDMGIMAGE)pBackendData;
2093
2094 AssertPtrReturn(pThis, 0);
2095
2096 uint64_t cbFile = 0;
2097 if (pThis->pStorage || pThis->hDmgFileInXar != NIL_RTVFSFILE)
2098 {
2099 int rc = dmgWrapFileGetSize(pThis, &cbFile);
2100 if (RT_FAILURE(rc))
2101 cbFile = 0; /* Make sure it is 0 */
2102 }
2103
2104 LogFlowFunc(("returns %lld\n", cbFile));
2105 return cbFile;
2106}
2107
2108/** @interface_method_impl{VDIMAGEBACKEND,pfnGetPCHSGeometry} */
2109static DECLCALLBACK(int) dmgGetPCHSGeometry(void *pBackendData, PVDGEOMETRY pPCHSGeometry)
2110{
2111 LogFlowFunc(("pBackendData=%#p pPCHSGeometry=%#p\n", pBackendData, pPCHSGeometry));
2112 PDMGIMAGE pThis = (PDMGIMAGE)pBackendData;
2113 int rc = VINF_SUCCESS;
2114
2115 AssertPtrReturn(pThis, VERR_VD_NOT_OPENED);
2116
2117 if (pThis->PCHSGeometry.cCylinders)
2118 *pPCHSGeometry = pThis->PCHSGeometry;
2119 else
2120 rc = VERR_VD_GEOMETRY_NOT_SET;
2121
2122 LogFlowFunc(("returns %Rrc (PCHS=%u/%u/%u)\n", rc, pPCHSGeometry->cCylinders, pPCHSGeometry->cHeads, pPCHSGeometry->cSectors));
2123 return rc;
2124}
2125
2126/** @interface_method_impl{VDIMAGEBACKEND,pfnSetPCHSGeometry} */
2127static DECLCALLBACK(int) dmgSetPCHSGeometry(void *pBackendData, PCVDGEOMETRY pPCHSGeometry)
2128{
2129 LogFlowFunc(("pBackendData=%#p pPCHSGeometry=%#p PCHS=%u/%u/%u\n",
2130 pBackendData, pPCHSGeometry, pPCHSGeometry->cCylinders, pPCHSGeometry->cHeads, pPCHSGeometry->cSectors));
2131 PDMGIMAGE pThis = (PDMGIMAGE)pBackendData;
2132 int rc = VINF_SUCCESS;
2133
2134 AssertPtrReturn(pThis, VERR_VD_NOT_OPENED);
2135
2136 if (pThis->uOpenFlags & VD_OPEN_FLAGS_READONLY)
2137 rc = VERR_VD_IMAGE_READ_ONLY;
2138 else
2139 pThis->PCHSGeometry = *pPCHSGeometry;
2140
2141 LogFlowFunc(("returns %Rrc\n", rc));
2142 return rc;
2143}
2144
2145/** @interface_method_impl{VDIMAGEBACKEND,pfnGetLCHSGeometry} */
2146static DECLCALLBACK(int) dmgGetLCHSGeometry(void *pBackendData, PVDGEOMETRY pLCHSGeometry)
2147{
2148 LogFlowFunc(("pBackendData=%#p pLCHSGeometry=%#p\n", pBackendData, pLCHSGeometry));
2149 PDMGIMAGE pThis = (PDMGIMAGE)pBackendData;
2150 int rc = VINF_SUCCESS;
2151
2152 AssertPtrReturn(pThis, VERR_VD_NOT_OPENED);
2153
2154 if (pThis->LCHSGeometry.cCylinders)
2155 *pLCHSGeometry = pThis->LCHSGeometry;
2156 else
2157 rc = VERR_VD_GEOMETRY_NOT_SET;
2158
2159 LogFlowFunc(("returns %Rrc (LCHS=%u/%u/%u)\n", rc, pLCHSGeometry->cCylinders, pLCHSGeometry->cHeads, pLCHSGeometry->cSectors));
2160 return rc;
2161}
2162
2163/** @interface_method_impl{VDIMAGEBACKEND,pfnSetLCHSGeometry} */
2164static DECLCALLBACK(int) dmgSetLCHSGeometry(void *pBackendData, PCVDGEOMETRY pLCHSGeometry)
2165{
2166 LogFlowFunc(("pBackendData=%#p pLCHSGeometry=%#p LCHS=%u/%u/%u\n",
2167 pBackendData, pLCHSGeometry, pLCHSGeometry->cCylinders, pLCHSGeometry->cHeads, pLCHSGeometry->cSectors));
2168 PDMGIMAGE pThis = (PDMGIMAGE)pBackendData;
2169 int rc = VINF_SUCCESS;
2170
2171 AssertPtrReturn(pThis, VERR_VD_NOT_OPENED);
2172
2173 if (pThis->uOpenFlags & VD_OPEN_FLAGS_READONLY)
2174 rc = VERR_VD_IMAGE_READ_ONLY;
2175 else
2176 pThis->LCHSGeometry = *pLCHSGeometry;
2177
2178 LogFlowFunc(("returns %Rrc\n", rc));
2179 return rc;
2180}
2181
2182/** @interface_method_impl{VDIMAGEBACKEND,pfnGetImageFlags} */
2183static DECLCALLBACK(unsigned) dmgGetImageFlags(void *pBackendData)
2184{
2185 LogFlowFunc(("pBackendData=%#p\n", pBackendData));
2186 PDMGIMAGE pThis = (PDMGIMAGE)pBackendData;
2187 AssertPtrReturn(pThis, 0);
2188
2189 LogFlowFunc(("returns %#x\n", pThis->uImageFlags));
2190 return pThis->uImageFlags;
2191}
2192
2193/** @interface_method_impl{VDIMAGEBACKEND,pfnGetOpenFlags} */
2194static DECLCALLBACK(unsigned) dmgGetOpenFlags(void *pBackendData)
2195{
2196 LogFlowFunc(("pBackendData=%#p\n", pBackendData));
2197 PDMGIMAGE pThis = (PDMGIMAGE)pBackendData;
2198
2199 AssertPtrReturn(pThis, 0);
2200
2201 LogFlowFunc(("returns %#x\n", pThis->uOpenFlags));
2202 return pThis->uOpenFlags;
2203}
2204
2205/** @interface_method_impl{VDIMAGEBACKEND,pfnSetOpenFlags} */
2206static DECLCALLBACK(int) dmgSetOpenFlags(void *pBackendData, unsigned uOpenFlags)
2207{
2208 LogFlowFunc(("pBackendData=%#p\n uOpenFlags=%#x", pBackendData, uOpenFlags));
2209 PDMGIMAGE pThis = (PDMGIMAGE)pBackendData;
2210 int rc = VINF_SUCCESS;
2211
2212 /* Image must be opened and the new flags must be valid. */
2213 if (!pThis || (uOpenFlags & ~( VD_OPEN_FLAGS_READONLY | VD_OPEN_FLAGS_INFO
2214 | VD_OPEN_FLAGS_SHAREABLE | VD_OPEN_FLAGS_SEQUENTIAL
2215 | VD_OPEN_FLAGS_SKIP_CONSISTENCY_CHECKS)))
2216 rc = VERR_INVALID_PARAMETER;
2217 else
2218 {
2219 /* Implement this operation via reopening the image. */
2220 rc = dmgFreeImage(pThis, false);
2221 if (RT_SUCCESS(rc))
2222 rc = dmgOpenImage(pThis, uOpenFlags);
2223 }
2224
2225 LogFlowFunc(("returns %Rrc\n", rc));
2226 return rc;
2227}
2228
2229/** @interface_method_impl{VDIMAGEBACKEND,pfnGetComment} */
2230static DECLCALLBACK(int) dmgGetComment(void *pBackendData, char *pszComment, size_t cbComment)
2231{
2232 RT_NOREF2(pszComment, cbComment);
2233 LogFlowFunc(("pBackendData=%#p pszComment=%#p cbComment=%zu\n", pBackendData, pszComment, cbComment));
2234 PDMGIMAGE pThis = (PDMGIMAGE)pBackendData;
2235
2236 AssertPtrReturn(pThis, VERR_VD_NOT_OPENED);
2237
2238 LogFlowFunc(("returns %Rrc comment='%s'\n", VERR_NOT_SUPPORTED, pszComment));
2239 return VERR_NOT_SUPPORTED;
2240}
2241
2242/** @interface_method_impl{VDIMAGEBACKEND,pfnSetComment} */
2243static DECLCALLBACK(int) dmgSetComment(void *pBackendData, const char *pszComment)
2244{
2245 RT_NOREF1(pszComment);
2246 LogFlowFunc(("pBackendData=%#p pszComment=\"%s\"\n", pBackendData, pszComment));
2247 PDMGIMAGE pThis = (PDMGIMAGE)pBackendData;
2248
2249 AssertPtrReturn(pThis, VERR_VD_NOT_OPENED);
2250
2251 int rc;
2252 if (pThis->uOpenFlags & VD_OPEN_FLAGS_READONLY)
2253 rc = VERR_VD_IMAGE_READ_ONLY;
2254 else
2255 rc = VERR_NOT_SUPPORTED;
2256
2257 LogFlowFunc(("returns %Rrc\n", rc));
2258 return rc;
2259}
2260
2261/** @interface_method_impl{VDIMAGEBACKEND,pfnGetUuid} */
2262static DECLCALLBACK(int) dmgGetUuid(void *pBackendData, PRTUUID pUuid)
2263{
2264 RT_NOREF1(pUuid);
2265 LogFlowFunc(("pBackendData=%#p pUuid=%#p\n", pBackendData, pUuid));
2266 PDMGIMAGE pThis = (PDMGIMAGE)pBackendData;
2267
2268 AssertPtrReturn(pThis, VERR_VD_NOT_OPENED);
2269
2270 LogFlowFunc(("returns %Rrc (%RTuuid)\n", VERR_NOT_SUPPORTED, pUuid));
2271 return VERR_NOT_SUPPORTED;
2272}
2273
2274/** @interface_method_impl{VDIMAGEBACKEND,pfnSetUuid} */
2275static DECLCALLBACK(int) dmgSetUuid(void *pBackendData, PCRTUUID pUuid)
2276{
2277 RT_NOREF1(pUuid);
2278 LogFlowFunc(("pBackendData=%#p Uuid=%RTuuid\n", pBackendData, pUuid));
2279 PDMGIMAGE pThis = (PDMGIMAGE)pBackendData;
2280
2281 AssertPtrReturn(pThis, VERR_VD_NOT_OPENED);
2282
2283 int rc;
2284 if (pThis->uOpenFlags & VD_OPEN_FLAGS_READONLY)
2285 rc = VERR_VD_IMAGE_READ_ONLY;
2286 else
2287 rc = VERR_NOT_SUPPORTED;
2288
2289 LogFlowFunc(("returns %Rrc\n", rc));
2290 return rc;
2291}
2292
2293/** @interface_method_impl{VDIMAGEBACKEND,pfnGetModificationUuid} */
2294static DECLCALLBACK(int) dmgGetModificationUuid(void *pBackendData, PRTUUID pUuid)
2295{
2296 RT_NOREF1(pUuid);
2297 LogFlowFunc(("pBackendData=%#p pUuid=%#p\n", pBackendData, pUuid));
2298 PDMGIMAGE pThis = (PDMGIMAGE)pBackendData;
2299
2300 AssertPtrReturn(pThis, VERR_VD_NOT_OPENED);
2301
2302 LogFlowFunc(("returns %Rrc (%RTuuid)\n", VERR_NOT_SUPPORTED, pUuid));
2303 return VERR_NOT_SUPPORTED;
2304}
2305
2306/** @interface_method_impl{VDIMAGEBACKEND,pfnSetModificationUuid} */
2307static DECLCALLBACK(int) dmgSetModificationUuid(void *pBackendData, PCRTUUID pUuid)
2308{
2309 RT_NOREF1(pUuid);
2310 LogFlowFunc(("pBackendData=%#p Uuid=%RTuuid\n", pBackendData, pUuid));
2311 PDMGIMAGE pThis = (PDMGIMAGE)pBackendData;
2312
2313 AssertPtrReturn(pThis, VERR_VD_NOT_OPENED);
2314
2315 int rc;
2316 if (pThis->uOpenFlags & VD_OPEN_FLAGS_READONLY)
2317 rc = VERR_VD_IMAGE_READ_ONLY;
2318 else
2319 rc = VERR_NOT_SUPPORTED;
2320
2321 LogFlowFunc(("returns %Rrc\n", rc));
2322 return rc;
2323}
2324
2325/** @interface_method_impl{VDIMAGEBACKEND,pfnGetParentUuid} */
2326static DECLCALLBACK(int) dmgGetParentUuid(void *pBackendData, PRTUUID pUuid)
2327{
2328 RT_NOREF1(pUuid);
2329 LogFlowFunc(("pBackendData=%#p pUuid=%#p\n", pBackendData, pUuid));
2330 PDMGIMAGE pThis = (PDMGIMAGE)pBackendData;
2331
2332 AssertPtrReturn(pThis, VERR_VD_NOT_OPENED);
2333
2334 LogFlowFunc(("returns %Rrc (%RTuuid)\n", VERR_NOT_SUPPORTED, pUuid));
2335 return VERR_NOT_SUPPORTED;
2336}
2337
2338/** @interface_method_impl{VDIMAGEBACKEND,pfnSetParentUuid} */
2339static DECLCALLBACK(int) dmgSetParentUuid(void *pBackendData, PCRTUUID pUuid)
2340{
2341 RT_NOREF1(pUuid);
2342 LogFlowFunc(("pBackendData=%#p Uuid=%RTuuid\n", pBackendData, pUuid));
2343 PDMGIMAGE pThis = (PDMGIMAGE)pBackendData;
2344
2345 AssertPtrReturn(pThis, VERR_VD_NOT_OPENED);
2346
2347 int rc;
2348 if (pThis->uOpenFlags & VD_OPEN_FLAGS_READONLY)
2349 rc = VERR_VD_IMAGE_READ_ONLY;
2350 else
2351 rc = VERR_NOT_SUPPORTED;
2352
2353 LogFlowFunc(("returns %Rrc\n", rc));
2354 return rc;
2355}
2356
2357/** @interface_method_impl{VDIMAGEBACKEND,pfnGetParentModificationUuid} */
2358static DECLCALLBACK(int) dmgGetParentModificationUuid(void *pBackendData, PRTUUID pUuid)
2359{
2360 RT_NOREF1(pUuid);
2361 LogFlowFunc(("pBackendData=%#p pUuid=%#p\n", pBackendData, pUuid));
2362 PDMGIMAGE pThis = (PDMGIMAGE)pBackendData;
2363
2364 AssertPtrReturn(pThis, VERR_VD_NOT_OPENED);
2365
2366 int rc;
2367 if (pThis->uOpenFlags & VD_OPEN_FLAGS_READONLY)
2368 rc = VERR_VD_IMAGE_READ_ONLY;
2369 else
2370 rc = VERR_NOT_SUPPORTED;
2371
2372 LogFlowFunc(("returns %Rrc (%RTuuid)\n", rc, pUuid));
2373 return rc;
2374}
2375
2376/** @interface_method_impl{VDIMAGEBACKEND,pfnSetParentModificationUuid} */
2377static DECLCALLBACK(int) dmgSetParentModificationUuid(void *pBackendData, PCRTUUID pUuid)
2378{
2379 RT_NOREF1(pUuid);
2380 LogFlowFunc(("pBackendData=%#p Uuid=%RTuuid\n", pBackendData, pUuid));
2381 PDMGIMAGE pThis = (PDMGIMAGE)pBackendData;
2382
2383 AssertPtrReturn(pThis, VERR_VD_NOT_OPENED);
2384
2385 int rc;
2386 if (pThis->uOpenFlags & VD_OPEN_FLAGS_READONLY)
2387 rc = VERR_VD_IMAGE_READ_ONLY;
2388 else
2389 rc = VERR_NOT_SUPPORTED;
2390
2391 LogFlowFunc(("returns %Rrc\n", rc));
2392 return rc;
2393}
2394
2395/** @interface_method_impl{VDIMAGEBACKEND,pfnDump} */
2396static DECLCALLBACK(void) dmgDump(void *pBackendData)
2397{
2398 PDMGIMAGE pThis = (PDMGIMAGE)pBackendData;
2399
2400 AssertPtrReturnVoid(pThis);
2401 vdIfErrorMessage(pThis->pIfError, "Header: Geometry PCHS=%u/%u/%u LCHS=%u/%u/%u cSectors=%llu\n",
2402 pThis->PCHSGeometry.cCylinders, pThis->PCHSGeometry.cHeads, pThis->PCHSGeometry.cSectors,
2403 pThis->LCHSGeometry.cCylinders, pThis->LCHSGeometry.cHeads, pThis->LCHSGeometry.cSectors,
2404 pThis->cbSize / DMG_SECTOR_SIZE);
2405}
2406
2407
2408const VDIMAGEBACKEND g_DmgBackend =
2409{
2410 /* u32Version */
2411 VD_IMGBACKEND_VERSION,
2412 /* pszBackendName */
2413 "DMG",
2414 /* uBackendCaps */
2415 VD_CAP_FILE | VD_CAP_VFS,
2416 /* paFileExtensions */
2417 s_aDmgFileExtensions,
2418 /* paConfigInfo */
2419 NULL,
2420 /* pfnProbe */
2421 dmgProbe,
2422 /* pfnOpen */
2423 dmgOpen,
2424 /* pfnCreate */
2425 dmgCreate,
2426 /* pfnRename */
2427 dmgRename,
2428 /* pfnClose */
2429 dmgClose,
2430 /* pfnRead */
2431 dmgRead,
2432 /* pfnWrite */
2433 dmgWrite,
2434 /* pfnFlush */
2435 dmgFlush,
2436 /* pfnDiscard */
2437 NULL,
2438 /* pfnGetVersion */
2439 dmgGetVersion,
2440 /* pfnGetSectorSize */
2441 dmgGetSectorSize,
2442 /* pfnGetSize */
2443 dmgGetSize,
2444 /* pfnGetFileSize */
2445 dmgGetFileSize,
2446 /* pfnGetPCHSGeometry */
2447 dmgGetPCHSGeometry,
2448 /* pfnSetPCHSGeometry */
2449 dmgSetPCHSGeometry,
2450 /* pfnGetLCHSGeometry */
2451 dmgGetLCHSGeometry,
2452 /* pfnSetLCHSGeometry */
2453 dmgSetLCHSGeometry,
2454 /* pfnGetImageFlags */
2455 dmgGetImageFlags,
2456 /* pfnGetOpenFlags */
2457 dmgGetOpenFlags,
2458 /* pfnSetOpenFlags */
2459 dmgSetOpenFlags,
2460 /* pfnGetComment */
2461 dmgGetComment,
2462 /* pfnSetComment */
2463 dmgSetComment,
2464 /* pfnGetUuid */
2465 dmgGetUuid,
2466 /* pfnSetUuid */
2467 dmgSetUuid,
2468 /* pfnGetModificationUuid */
2469 dmgGetModificationUuid,
2470 /* pfnSetModificationUuid */
2471 dmgSetModificationUuid,
2472 /* pfnGetParentUuid */
2473 dmgGetParentUuid,
2474 /* pfnSetParentUuid */
2475 dmgSetParentUuid,
2476 /* pfnGetParentModificationUuid */
2477 dmgGetParentModificationUuid,
2478 /* pfnSetParentModificationUuid */
2479 dmgSetParentModificationUuid,
2480 /* pfnDump */
2481 dmgDump,
2482 /* pfnGetTimestamp */
2483 NULL,
2484 /* pfnGetParentTimestamp */
2485 NULL,
2486 /* pfnSetParentTimestamp */
2487 NULL,
2488 /* pfnGetParentFilename */
2489 NULL,
2490 /* pfnSetParentFilename */
2491 NULL,
2492 /* pfnComposeLocation */
2493 genericFileComposeLocation,
2494 /* pfnComposeName */
2495 genericFileComposeName,
2496 /* pfnCompact */
2497 NULL,
2498 /* pfnResize */
2499 NULL,
2500 /* pfnRepair */
2501 NULL,
2502 /* pfnTraverseMetadata */
2503 NULL,
2504 /* u32VersionEnd */
2505 VD_IMGBACKEND_VERSION
2506};
2507
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