VirtualBox

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

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

Storage/DMG: Fix opening a non DMG image file which is too small to contain the UDIF descriptor

  • 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 66212 2017-03-22 19:46:16Z 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_OPTICAL_DISC},
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 && cbFile >= sizeof(DMGUDIF))
1776 {
1777 DMGUDIF Ftr;
1778 uint64_t offFtr = cbFile - sizeof(Ftr);
1779 if (hDmgFileInXar == NIL_RTVFSFILE)
1780 rc = vdIfIoIntFileReadSync(pIfIo, pStorage, offFtr, &Ftr, sizeof(Ftr));
1781 else
1782 rc = RTVfsFileReadAt(hDmgFileInXar, offFtr, &Ftr, sizeof(Ftr), NULL);
1783 if (RT_SUCCESS(rc))
1784 {
1785 /*
1786 * Do we recognize this stuff? Does it look valid?
1787 */
1788 if ( Ftr.u32Magic == RT_H2BE_U32_C(DMGUDIF_MAGIC)
1789 && Ftr.u32Version == RT_H2BE_U32_C(DMGUDIF_VER_CURRENT)
1790 && Ftr.cbFooter == RT_H2BE_U32_C(sizeof(Ftr)))
1791 {
1792 dmgUdifFtrFile2HostEndian(&Ftr);
1793 if (dmgUdifFtrIsValid(&Ftr, offFtr))
1794 {
1795 rc = VINF_SUCCESS;
1796 *penmType = VDTYPE_OPTICAL_DISC;
1797 }
1798 else
1799 {
1800 DMG_PRINTF(("Bad DMG: '%s' offFtr=%RTfoff\n", pszFilename, offFtr));
1801 rc = VERR_VD_DMG_INVALID_HEADER;
1802 }
1803 }
1804 else
1805 rc = VERR_VD_DMG_INVALID_HEADER;
1806 }
1807 else
1808 rc = VERR_VD_DMG_INVALID_HEADER;
1809 }
1810 else
1811 rc = VERR_VD_DMG_INVALID_HEADER;
1812
1813 /* Clean up. */
1814 RTVfsFileRelease(hDmgFileInXar);
1815 RTVfsFsStrmRelease(hXarFss);
1816 vdIfIoIntFileClose(pIfIo, pStorage);
1817
1818 LogFlowFunc(("returns %Rrc\n", rc));
1819 return rc;
1820}
1821
1822/** @interface_method_impl{VDIMAGEBACKEND,pfnOpen} */
1823static DECLCALLBACK(int) dmgOpen(const char *pszFilename, unsigned uOpenFlags,
1824 PVDINTERFACE pVDIfsDisk, PVDINTERFACE pVDIfsImage,
1825 VDTYPE enmType, void **ppBackendData)
1826{
1827 LogFlowFunc(("pszFilename=\"%s\" uOpenFlags=%#x pVDIfsDisk=%#p pVDIfsImage=%#p enmType=%u ppBackendData=%#p\n", pszFilename, uOpenFlags, pVDIfsDisk, pVDIfsImage, enmType, ppBackendData));
1828
1829 NOREF(enmType); /**< @todo r=klaus make use of the type info. */
1830
1831 /* Check open flags. All valid flags are (in principle) supported. */
1832 AssertReturn(!(uOpenFlags & ~VD_OPEN_FLAGS_MASK), VERR_INVALID_PARAMETER);
1833
1834 /* Check remaining arguments. */
1835 AssertPtrReturn(pszFilename, VERR_INVALID_POINTER);
1836 AssertReturn(*pszFilename, VERR_INVALID_PARAMETER);
1837
1838 /*
1839 * Reject combinations we don't currently support.
1840 *
1841 * There is no point in being paranoid about the input here as we're just a
1842 * simple backend and can expect the caller to be the only user and already
1843 * have validate what it passes thru to us.
1844 */
1845 if ( !(uOpenFlags & VD_OPEN_FLAGS_READONLY)
1846 || (uOpenFlags & VD_OPEN_FLAGS_ASYNC_IO))
1847 {
1848 LogFlowFunc(("Unsupported flag(s): %#x\n", uOpenFlags));
1849 return VERR_INVALID_PARAMETER;
1850 }
1851
1852 /*
1853 * Create the basic instance data structure and open the file,
1854 * then hand it over to a worker function that does all the rest.
1855 */
1856 int rc = VERR_NO_MEMORY;
1857 PDMGIMAGE pThis = (PDMGIMAGE)RTMemAllocZ(sizeof(*pThis));
1858 if (pThis)
1859 {
1860 pThis->pszFilename = pszFilename;
1861 pThis->pStorage = NULL;
1862 pThis->pVDIfsDisk = pVDIfsDisk;
1863 pThis->pVDIfsImage = pVDIfsImage;
1864
1865 rc = dmgOpenImage(pThis, uOpenFlags);
1866 if (RT_SUCCESS(rc))
1867 *ppBackendData = pThis;
1868 else
1869 RTMemFree(pThis);
1870 }
1871
1872 LogFlowFunc(("returns %Rrc (pBackendData=%#p)\n", rc, *ppBackendData));
1873 return rc;
1874}
1875
1876/** @interface_method_impl{VDIMAGEBACKEND,pfnCreate} */
1877static DECLCALLBACK(int) dmgCreate(const char *pszFilename, uint64_t cbSize,
1878 unsigned uImageFlags, const char *pszComment,
1879 PCVDGEOMETRY pPCHSGeometry, PCVDGEOMETRY pLCHSGeometry,
1880 PCRTUUID pUuid, unsigned uOpenFlags,
1881 unsigned uPercentStart, unsigned uPercentSpan,
1882 PVDINTERFACE pVDIfsDisk, PVDINTERFACE pVDIfsImage,
1883 PVDINTERFACE pVDIfsOperation, VDTYPE enmType,
1884 void **ppBackendData)
1885{
1886 RT_NOREF8(pszFilename, cbSize, uImageFlags, pszComment, pPCHSGeometry, pLCHSGeometry, pUuid, uOpenFlags);
1887 RT_NOREF7(uPercentStart, uPercentSpan, pVDIfsDisk, pVDIfsImage, pVDIfsOperation, enmType, ppBackendData);
1888 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",
1889 pszFilename, cbSize, uImageFlags, pszComment, pPCHSGeometry, pLCHSGeometry, pUuid, uOpenFlags, uPercentStart, uPercentSpan, pVDIfsDisk, pVDIfsImage, pVDIfsOperation, enmType, ppBackendData));
1890 int rc = VERR_NOT_SUPPORTED;
1891
1892 LogFlowFunc(("returns %Rrc\n", rc));
1893 return rc;
1894}
1895
1896/** @interface_method_impl{VDIMAGEBACKEND,pfnRename} */
1897static DECLCALLBACK(int) dmgRename(void *pBackendData, const char *pszFilename)
1898{
1899 RT_NOREF2(pBackendData, pszFilename);
1900 LogFlowFunc(("pBackendData=%#p pszFilename=%#p\n", pBackendData, pszFilename));
1901 int rc = VERR_NOT_SUPPORTED;
1902
1903 LogFlowFunc(("returns %Rrc\n", rc));
1904 return rc;
1905}
1906
1907/** @interface_method_impl{VDIMAGEBACKEND,pfnClose} */
1908static DECLCALLBACK(int) dmgClose(void *pBackendData, bool fDelete)
1909{
1910 LogFlowFunc(("pBackendData=%#p fDelete=%d\n", pBackendData, fDelete));
1911 PDMGIMAGE pThis = (PDMGIMAGE)pBackendData;
1912
1913 int rc = dmgFreeImage(pThis, fDelete);
1914 RTMemFree(pThis);
1915
1916 LogFlowFunc(("returns %Rrc\n", rc));
1917 return rc;
1918}
1919
1920/** @interface_method_impl{VDIMAGEBACKEND,pfnRead} */
1921static DECLCALLBACK(int) dmgRead(void *pBackendData, uint64_t uOffset, size_t cbToRead,
1922 PVDIOCTX pIoCtx, size_t *pcbActuallyRead)
1923{
1924 LogFlowFunc(("pBackendData=%#p uOffset=%llu pIoCtx=%#p cbToRead=%zu pcbActuallyRead=%#p\n",
1925 pBackendData, uOffset, pIoCtx, cbToRead, pcbActuallyRead));
1926 PDMGIMAGE pThis = (PDMGIMAGE)pBackendData;
1927 PDMGEXTENT pExtent = NULL;
1928 int rc = VINF_SUCCESS;
1929
1930 AssertPtr(pThis);
1931 Assert(uOffset % DMG_SECTOR_SIZE == 0);
1932 Assert(cbToRead % DMG_SECTOR_SIZE == 0);
1933
1934 if ( uOffset + cbToRead > pThis->cbSize
1935 || cbToRead == 0)
1936 {
1937 LogFlowFunc(("returns VERR_INVALID_PARAMETER\n"));
1938 return VERR_INVALID_PARAMETER;
1939 }
1940
1941 pExtent = dmgExtentGetFromOffset(pThis, DMG_BYTE2BLOCK(uOffset));
1942
1943 if (pExtent)
1944 {
1945 uint64_t uExtentRel = DMG_BYTE2BLOCK(uOffset) - pExtent->uSectorExtent;
1946
1947 /* Remain in this extent. */
1948 cbToRead = RT_MIN(cbToRead, DMG_BLOCK2BYTE(pExtent->cSectorsExtent - uExtentRel));
1949
1950 switch (pExtent->enmType)
1951 {
1952 case DMGEXTENTTYPE_RAW:
1953 {
1954 rc = dmgWrapFileReadUser(pThis, pExtent->offFileStart + DMG_BLOCK2BYTE(uExtentRel), pIoCtx, cbToRead);
1955 break;
1956 }
1957 case DMGEXTENTTYPE_ZERO:
1958 {
1959 vdIfIoIntIoCtxSet(pThis->pIfIoXxx, pIoCtx, 0, cbToRead);
1960 break;
1961 }
1962 case DMGEXTENTTYPE_COMP_ZLIB:
1963 {
1964 if (pThis->pExtentDecomp != pExtent)
1965 {
1966 if (DMG_BLOCK2BYTE(pExtent->cSectorsExtent) > pThis->cbDecompExtent)
1967 {
1968 if (RT_LIKELY(pThis->pvDecompExtent))
1969 RTMemFree(pThis->pvDecompExtent);
1970
1971 pThis->pvDecompExtent = RTMemAllocZ(DMG_BLOCK2BYTE(pExtent->cSectorsExtent));
1972 if (!pThis->pvDecompExtent)
1973 rc = VERR_NO_MEMORY;
1974 else
1975 pThis->cbDecompExtent = DMG_BLOCK2BYTE(pExtent->cSectorsExtent);
1976 }
1977
1978 if (RT_SUCCESS(rc))
1979 {
1980 rc = dmgFileInflateSync(pThis, pExtent->offFileStart, pExtent->cbFile,
1981 pThis->pvDecompExtent,
1982 RT_MIN(pThis->cbDecompExtent, DMG_BLOCK2BYTE(pExtent->cSectorsExtent)));
1983 if (RT_SUCCESS(rc))
1984 pThis->pExtentDecomp = pExtent;
1985 }
1986 }
1987
1988 if (RT_SUCCESS(rc))
1989 vdIfIoIntIoCtxCopyTo(pThis->pIfIoXxx, pIoCtx,
1990 (uint8_t *)pThis->pvDecompExtent + DMG_BLOCK2BYTE(uExtentRel),
1991 cbToRead);
1992 break;
1993 }
1994 default:
1995 AssertMsgFailed(("Invalid extent type\n"));
1996 }
1997
1998 if (RT_SUCCESS(rc))
1999 *pcbActuallyRead = cbToRead;
2000 }
2001 else
2002 rc = VERR_INVALID_PARAMETER;
2003
2004 LogFlowFunc(("returns %Rrc\n", rc));
2005 return rc;
2006}
2007
2008/** @interface_method_impl{VDIMAGEBACKEND,pfnWrite} */
2009static DECLCALLBACK(int) dmgWrite(void *pBackendData, uint64_t uOffset, size_t cbToWrite,
2010 PVDIOCTX pIoCtx, size_t *pcbWriteProcess, size_t *pcbPreRead,
2011 size_t *pcbPostRead, unsigned fWrite)
2012{
2013 RT_NOREF7(uOffset, cbToWrite, pIoCtx, pcbWriteProcess, pcbPreRead, pcbPostRead, fWrite);
2014 LogFlowFunc(("pBackendData=%#p uOffset=%llu pIoCtx=%#p cbToWrite=%zu pcbWriteProcess=%#p pcbPreRead=%#p pcbPostRead=%#p\n",
2015 pBackendData, uOffset, pIoCtx, cbToWrite, pcbWriteProcess, pcbPreRead, pcbPostRead));
2016 PDMGIMAGE pThis = (PDMGIMAGE)pBackendData;
2017 int rc = VERR_NOT_IMPLEMENTED;
2018
2019 AssertPtr(pThis);
2020 Assert(uOffset % 512 == 0);
2021 Assert(cbToWrite % 512 == 0);
2022
2023 if (!(pThis->uOpenFlags & VD_OPEN_FLAGS_READONLY))
2024 AssertMsgFailed(("Not implemented\n"));
2025 else
2026 rc = VERR_VD_IMAGE_READ_ONLY;
2027
2028 LogFlowFunc(("returns %Rrc\n", rc));
2029 return rc;
2030}
2031
2032/** @interface_method_impl{VDIMAGEBACKEND,pfnFlush} */
2033static DECLCALLBACK(int) dmgFlush(void *pBackendData, PVDIOCTX pIoCtx)
2034{
2035 RT_NOREF1(pIoCtx);
2036 LogFlowFunc(("pBackendData=%#p\n", pBackendData));
2037 PDMGIMAGE pThis = (PDMGIMAGE)pBackendData;
2038 int rc;
2039
2040 AssertPtr(pThis);
2041
2042 rc = dmgFlushImage(pThis);
2043
2044 LogFlowFunc(("returns %Rrc\n", rc));
2045 return rc;
2046}
2047
2048/** @interface_method_impl{VDIMAGEBACKEND,pfnGetVersion} */
2049static DECLCALLBACK(unsigned) dmgGetVersion(void *pBackendData)
2050{
2051 LogFlowFunc(("pBackendData=%#p\n", pBackendData));
2052 PDMGIMAGE pThis = (PDMGIMAGE)pBackendData;
2053
2054 AssertPtrReturn(pThis, 0);
2055
2056 return 1;
2057}
2058
2059/** @interface_method_impl{VDIMAGEBACKEND,pfnGetSectorSize} */
2060static DECLCALLBACK(uint32_t) dmgGetSectorSize(void *pBackendData)
2061{
2062 LogFlowFunc(("pBackendData=%#p\n", pBackendData));
2063 PDMGIMAGE pThis = (PDMGIMAGE)pBackendData;
2064
2065 AssertPtrReturn(pThis, 0);
2066
2067 uint32_t cb = 0;
2068 if (pThis->pStorage || pThis->hDmgFileInXar != NIL_RTVFSFILE)
2069 cb = 2048;
2070
2071 LogFlowFunc(("returns %u\n", cb));
2072 return cb;
2073}
2074
2075/** @interface_method_impl{VDIMAGEBACKEND,pfnGetSize} */
2076static DECLCALLBACK(uint64_t) dmgGetSize(void *pBackendData)
2077{
2078 LogFlowFunc(("pBackendData=%#p\n", pBackendData));
2079 PDMGIMAGE pThis = (PDMGIMAGE)pBackendData;
2080
2081 AssertPtrReturn(pThis, 0);
2082
2083 uint64_t cb = 0;
2084 if (pThis->pStorage || pThis->hDmgFileInXar != NIL_RTVFSFILE)
2085 cb = pThis->cbSize;
2086
2087 LogFlowFunc(("returns %llu\n", cb));
2088 return cb;
2089}
2090
2091/** @interface_method_impl{VDIMAGEBACKEND,pfnGetFileSize} */
2092static DECLCALLBACK(uint64_t) dmgGetFileSize(void *pBackendData)
2093{
2094 LogFlowFunc(("pBackendData=%#p\n", pBackendData));
2095 PDMGIMAGE pThis = (PDMGIMAGE)pBackendData;
2096
2097 AssertPtrReturn(pThis, 0);
2098
2099 uint64_t cbFile = 0;
2100 if (pThis->pStorage || pThis->hDmgFileInXar != NIL_RTVFSFILE)
2101 {
2102 int rc = dmgWrapFileGetSize(pThis, &cbFile);
2103 if (RT_FAILURE(rc))
2104 cbFile = 0; /* Make sure it is 0 */
2105 }
2106
2107 LogFlowFunc(("returns %lld\n", cbFile));
2108 return cbFile;
2109}
2110
2111/** @interface_method_impl{VDIMAGEBACKEND,pfnGetPCHSGeometry} */
2112static DECLCALLBACK(int) dmgGetPCHSGeometry(void *pBackendData, PVDGEOMETRY pPCHSGeometry)
2113{
2114 LogFlowFunc(("pBackendData=%#p pPCHSGeometry=%#p\n", pBackendData, pPCHSGeometry));
2115 PDMGIMAGE pThis = (PDMGIMAGE)pBackendData;
2116 int rc = VINF_SUCCESS;
2117
2118 AssertPtrReturn(pThis, VERR_VD_NOT_OPENED);
2119
2120 if (pThis->PCHSGeometry.cCylinders)
2121 *pPCHSGeometry = pThis->PCHSGeometry;
2122 else
2123 rc = VERR_VD_GEOMETRY_NOT_SET;
2124
2125 LogFlowFunc(("returns %Rrc (PCHS=%u/%u/%u)\n", rc, pPCHSGeometry->cCylinders, pPCHSGeometry->cHeads, pPCHSGeometry->cSectors));
2126 return rc;
2127}
2128
2129/** @interface_method_impl{VDIMAGEBACKEND,pfnSetPCHSGeometry} */
2130static DECLCALLBACK(int) dmgSetPCHSGeometry(void *pBackendData, PCVDGEOMETRY pPCHSGeometry)
2131{
2132 LogFlowFunc(("pBackendData=%#p pPCHSGeometry=%#p PCHS=%u/%u/%u\n",
2133 pBackendData, pPCHSGeometry, pPCHSGeometry->cCylinders, pPCHSGeometry->cHeads, pPCHSGeometry->cSectors));
2134 PDMGIMAGE pThis = (PDMGIMAGE)pBackendData;
2135 int rc = VINF_SUCCESS;
2136
2137 AssertPtrReturn(pThis, VERR_VD_NOT_OPENED);
2138
2139 if (pThis->uOpenFlags & VD_OPEN_FLAGS_READONLY)
2140 rc = VERR_VD_IMAGE_READ_ONLY;
2141 else
2142 pThis->PCHSGeometry = *pPCHSGeometry;
2143
2144 LogFlowFunc(("returns %Rrc\n", rc));
2145 return rc;
2146}
2147
2148/** @interface_method_impl{VDIMAGEBACKEND,pfnGetLCHSGeometry} */
2149static DECLCALLBACK(int) dmgGetLCHSGeometry(void *pBackendData, PVDGEOMETRY pLCHSGeometry)
2150{
2151 LogFlowFunc(("pBackendData=%#p pLCHSGeometry=%#p\n", pBackendData, pLCHSGeometry));
2152 PDMGIMAGE pThis = (PDMGIMAGE)pBackendData;
2153 int rc = VINF_SUCCESS;
2154
2155 AssertPtrReturn(pThis, VERR_VD_NOT_OPENED);
2156
2157 if (pThis->LCHSGeometry.cCylinders)
2158 *pLCHSGeometry = pThis->LCHSGeometry;
2159 else
2160 rc = VERR_VD_GEOMETRY_NOT_SET;
2161
2162 LogFlowFunc(("returns %Rrc (LCHS=%u/%u/%u)\n", rc, pLCHSGeometry->cCylinders, pLCHSGeometry->cHeads, pLCHSGeometry->cSectors));
2163 return rc;
2164}
2165
2166/** @interface_method_impl{VDIMAGEBACKEND,pfnSetLCHSGeometry} */
2167static DECLCALLBACK(int) dmgSetLCHSGeometry(void *pBackendData, PCVDGEOMETRY pLCHSGeometry)
2168{
2169 LogFlowFunc(("pBackendData=%#p pLCHSGeometry=%#p LCHS=%u/%u/%u\n",
2170 pBackendData, pLCHSGeometry, pLCHSGeometry->cCylinders, pLCHSGeometry->cHeads, pLCHSGeometry->cSectors));
2171 PDMGIMAGE pThis = (PDMGIMAGE)pBackendData;
2172 int rc = VINF_SUCCESS;
2173
2174 AssertPtrReturn(pThis, VERR_VD_NOT_OPENED);
2175
2176 if (pThis->uOpenFlags & VD_OPEN_FLAGS_READONLY)
2177 rc = VERR_VD_IMAGE_READ_ONLY;
2178 else
2179 pThis->LCHSGeometry = *pLCHSGeometry;
2180
2181 LogFlowFunc(("returns %Rrc\n", rc));
2182 return rc;
2183}
2184
2185/** @interface_method_impl{VDIMAGEBACKEND,pfnGetImageFlags} */
2186static DECLCALLBACK(unsigned) dmgGetImageFlags(void *pBackendData)
2187{
2188 LogFlowFunc(("pBackendData=%#p\n", pBackendData));
2189 PDMGIMAGE pThis = (PDMGIMAGE)pBackendData;
2190 AssertPtrReturn(pThis, 0);
2191
2192 LogFlowFunc(("returns %#x\n", pThis->uImageFlags));
2193 return pThis->uImageFlags;
2194}
2195
2196/** @interface_method_impl{VDIMAGEBACKEND,pfnGetOpenFlags} */
2197static DECLCALLBACK(unsigned) dmgGetOpenFlags(void *pBackendData)
2198{
2199 LogFlowFunc(("pBackendData=%#p\n", pBackendData));
2200 PDMGIMAGE pThis = (PDMGIMAGE)pBackendData;
2201
2202 AssertPtrReturn(pThis, 0);
2203
2204 LogFlowFunc(("returns %#x\n", pThis->uOpenFlags));
2205 return pThis->uOpenFlags;
2206}
2207
2208/** @interface_method_impl{VDIMAGEBACKEND,pfnSetOpenFlags} */
2209static DECLCALLBACK(int) dmgSetOpenFlags(void *pBackendData, unsigned uOpenFlags)
2210{
2211 LogFlowFunc(("pBackendData=%#p\n uOpenFlags=%#x", pBackendData, uOpenFlags));
2212 PDMGIMAGE pThis = (PDMGIMAGE)pBackendData;
2213 int rc = VINF_SUCCESS;
2214
2215 /* Image must be opened and the new flags must be valid. */
2216 if (!pThis || (uOpenFlags & ~( VD_OPEN_FLAGS_READONLY | VD_OPEN_FLAGS_INFO
2217 | VD_OPEN_FLAGS_SHAREABLE | VD_OPEN_FLAGS_SEQUENTIAL
2218 | VD_OPEN_FLAGS_SKIP_CONSISTENCY_CHECKS)))
2219 rc = VERR_INVALID_PARAMETER;
2220 else
2221 {
2222 /* Implement this operation via reopening the image. */
2223 rc = dmgFreeImage(pThis, false);
2224 if (RT_SUCCESS(rc))
2225 rc = dmgOpenImage(pThis, uOpenFlags);
2226 }
2227
2228 LogFlowFunc(("returns %Rrc\n", rc));
2229 return rc;
2230}
2231
2232/** @interface_method_impl{VDIMAGEBACKEND,pfnGetComment} */
2233static DECLCALLBACK(int) dmgGetComment(void *pBackendData, char *pszComment, size_t cbComment)
2234{
2235 RT_NOREF2(pszComment, cbComment);
2236 LogFlowFunc(("pBackendData=%#p pszComment=%#p cbComment=%zu\n", pBackendData, pszComment, cbComment));
2237 PDMGIMAGE pThis = (PDMGIMAGE)pBackendData;
2238
2239 AssertPtrReturn(pThis, VERR_VD_NOT_OPENED);
2240
2241 LogFlowFunc(("returns %Rrc comment='%s'\n", VERR_NOT_SUPPORTED, pszComment));
2242 return VERR_NOT_SUPPORTED;
2243}
2244
2245/** @interface_method_impl{VDIMAGEBACKEND,pfnSetComment} */
2246static DECLCALLBACK(int) dmgSetComment(void *pBackendData, const char *pszComment)
2247{
2248 RT_NOREF1(pszComment);
2249 LogFlowFunc(("pBackendData=%#p pszComment=\"%s\"\n", pBackendData, pszComment));
2250 PDMGIMAGE pThis = (PDMGIMAGE)pBackendData;
2251
2252 AssertPtrReturn(pThis, VERR_VD_NOT_OPENED);
2253
2254 int rc;
2255 if (pThis->uOpenFlags & VD_OPEN_FLAGS_READONLY)
2256 rc = VERR_VD_IMAGE_READ_ONLY;
2257 else
2258 rc = VERR_NOT_SUPPORTED;
2259
2260 LogFlowFunc(("returns %Rrc\n", rc));
2261 return rc;
2262}
2263
2264/** @interface_method_impl{VDIMAGEBACKEND,pfnGetUuid} */
2265static DECLCALLBACK(int) dmgGetUuid(void *pBackendData, PRTUUID pUuid)
2266{
2267 RT_NOREF1(pUuid);
2268 LogFlowFunc(("pBackendData=%#p pUuid=%#p\n", pBackendData, pUuid));
2269 PDMGIMAGE pThis = (PDMGIMAGE)pBackendData;
2270
2271 AssertPtrReturn(pThis, VERR_VD_NOT_OPENED);
2272
2273 LogFlowFunc(("returns %Rrc (%RTuuid)\n", VERR_NOT_SUPPORTED, pUuid));
2274 return VERR_NOT_SUPPORTED;
2275}
2276
2277/** @interface_method_impl{VDIMAGEBACKEND,pfnSetUuid} */
2278static DECLCALLBACK(int) dmgSetUuid(void *pBackendData, PCRTUUID pUuid)
2279{
2280 RT_NOREF1(pUuid);
2281 LogFlowFunc(("pBackendData=%#p Uuid=%RTuuid\n", pBackendData, pUuid));
2282 PDMGIMAGE pThis = (PDMGIMAGE)pBackendData;
2283
2284 AssertPtrReturn(pThis, VERR_VD_NOT_OPENED);
2285
2286 int rc;
2287 if (pThis->uOpenFlags & VD_OPEN_FLAGS_READONLY)
2288 rc = VERR_VD_IMAGE_READ_ONLY;
2289 else
2290 rc = VERR_NOT_SUPPORTED;
2291
2292 LogFlowFunc(("returns %Rrc\n", rc));
2293 return rc;
2294}
2295
2296/** @interface_method_impl{VDIMAGEBACKEND,pfnGetModificationUuid} */
2297static DECLCALLBACK(int) dmgGetModificationUuid(void *pBackendData, PRTUUID pUuid)
2298{
2299 RT_NOREF1(pUuid);
2300 LogFlowFunc(("pBackendData=%#p pUuid=%#p\n", pBackendData, pUuid));
2301 PDMGIMAGE pThis = (PDMGIMAGE)pBackendData;
2302
2303 AssertPtrReturn(pThis, VERR_VD_NOT_OPENED);
2304
2305 LogFlowFunc(("returns %Rrc (%RTuuid)\n", VERR_NOT_SUPPORTED, pUuid));
2306 return VERR_NOT_SUPPORTED;
2307}
2308
2309/** @interface_method_impl{VDIMAGEBACKEND,pfnSetModificationUuid} */
2310static DECLCALLBACK(int) dmgSetModificationUuid(void *pBackendData, PCRTUUID pUuid)
2311{
2312 RT_NOREF1(pUuid);
2313 LogFlowFunc(("pBackendData=%#p Uuid=%RTuuid\n", pBackendData, pUuid));
2314 PDMGIMAGE pThis = (PDMGIMAGE)pBackendData;
2315
2316 AssertPtrReturn(pThis, VERR_VD_NOT_OPENED);
2317
2318 int rc;
2319 if (pThis->uOpenFlags & VD_OPEN_FLAGS_READONLY)
2320 rc = VERR_VD_IMAGE_READ_ONLY;
2321 else
2322 rc = VERR_NOT_SUPPORTED;
2323
2324 LogFlowFunc(("returns %Rrc\n", rc));
2325 return rc;
2326}
2327
2328/** @interface_method_impl{VDIMAGEBACKEND,pfnGetParentUuid} */
2329static DECLCALLBACK(int) dmgGetParentUuid(void *pBackendData, PRTUUID pUuid)
2330{
2331 RT_NOREF1(pUuid);
2332 LogFlowFunc(("pBackendData=%#p pUuid=%#p\n", pBackendData, pUuid));
2333 PDMGIMAGE pThis = (PDMGIMAGE)pBackendData;
2334
2335 AssertPtrReturn(pThis, VERR_VD_NOT_OPENED);
2336
2337 LogFlowFunc(("returns %Rrc (%RTuuid)\n", VERR_NOT_SUPPORTED, pUuid));
2338 return VERR_NOT_SUPPORTED;
2339}
2340
2341/** @interface_method_impl{VDIMAGEBACKEND,pfnSetParentUuid} */
2342static DECLCALLBACK(int) dmgSetParentUuid(void *pBackendData, PCRTUUID pUuid)
2343{
2344 RT_NOREF1(pUuid);
2345 LogFlowFunc(("pBackendData=%#p Uuid=%RTuuid\n", pBackendData, pUuid));
2346 PDMGIMAGE pThis = (PDMGIMAGE)pBackendData;
2347
2348 AssertPtrReturn(pThis, VERR_VD_NOT_OPENED);
2349
2350 int rc;
2351 if (pThis->uOpenFlags & VD_OPEN_FLAGS_READONLY)
2352 rc = VERR_VD_IMAGE_READ_ONLY;
2353 else
2354 rc = VERR_NOT_SUPPORTED;
2355
2356 LogFlowFunc(("returns %Rrc\n", rc));
2357 return rc;
2358}
2359
2360/** @interface_method_impl{VDIMAGEBACKEND,pfnGetParentModificationUuid} */
2361static DECLCALLBACK(int) dmgGetParentModificationUuid(void *pBackendData, PRTUUID pUuid)
2362{
2363 RT_NOREF1(pUuid);
2364 LogFlowFunc(("pBackendData=%#p pUuid=%#p\n", pBackendData, pUuid));
2365 PDMGIMAGE pThis = (PDMGIMAGE)pBackendData;
2366
2367 AssertPtrReturn(pThis, VERR_VD_NOT_OPENED);
2368
2369 int rc;
2370 if (pThis->uOpenFlags & VD_OPEN_FLAGS_READONLY)
2371 rc = VERR_VD_IMAGE_READ_ONLY;
2372 else
2373 rc = VERR_NOT_SUPPORTED;
2374
2375 LogFlowFunc(("returns %Rrc (%RTuuid)\n", rc, pUuid));
2376 return rc;
2377}
2378
2379/** @interface_method_impl{VDIMAGEBACKEND,pfnSetParentModificationUuid} */
2380static DECLCALLBACK(int) dmgSetParentModificationUuid(void *pBackendData, PCRTUUID pUuid)
2381{
2382 RT_NOREF1(pUuid);
2383 LogFlowFunc(("pBackendData=%#p Uuid=%RTuuid\n", pBackendData, pUuid));
2384 PDMGIMAGE pThis = (PDMGIMAGE)pBackendData;
2385
2386 AssertPtrReturn(pThis, VERR_VD_NOT_OPENED);
2387
2388 int rc;
2389 if (pThis->uOpenFlags & VD_OPEN_FLAGS_READONLY)
2390 rc = VERR_VD_IMAGE_READ_ONLY;
2391 else
2392 rc = VERR_NOT_SUPPORTED;
2393
2394 LogFlowFunc(("returns %Rrc\n", rc));
2395 return rc;
2396}
2397
2398/** @interface_method_impl{VDIMAGEBACKEND,pfnDump} */
2399static DECLCALLBACK(void) dmgDump(void *pBackendData)
2400{
2401 PDMGIMAGE pThis = (PDMGIMAGE)pBackendData;
2402
2403 AssertPtrReturnVoid(pThis);
2404 vdIfErrorMessage(pThis->pIfError, "Header: Geometry PCHS=%u/%u/%u LCHS=%u/%u/%u cSectors=%llu\n",
2405 pThis->PCHSGeometry.cCylinders, pThis->PCHSGeometry.cHeads, pThis->PCHSGeometry.cSectors,
2406 pThis->LCHSGeometry.cCylinders, pThis->LCHSGeometry.cHeads, pThis->LCHSGeometry.cSectors,
2407 pThis->cbSize / DMG_SECTOR_SIZE);
2408}
2409
2410
2411const VDIMAGEBACKEND g_DmgBackend =
2412{
2413 /* u32Version */
2414 VD_IMGBACKEND_VERSION,
2415 /* pszBackendName */
2416 "DMG",
2417 /* uBackendCaps */
2418 VD_CAP_FILE | VD_CAP_VFS,
2419 /* paFileExtensions */
2420 s_aDmgFileExtensions,
2421 /* paConfigInfo */
2422 NULL,
2423 /* pfnProbe */
2424 dmgProbe,
2425 /* pfnOpen */
2426 dmgOpen,
2427 /* pfnCreate */
2428 dmgCreate,
2429 /* pfnRename */
2430 dmgRename,
2431 /* pfnClose */
2432 dmgClose,
2433 /* pfnRead */
2434 dmgRead,
2435 /* pfnWrite */
2436 dmgWrite,
2437 /* pfnFlush */
2438 dmgFlush,
2439 /* pfnDiscard */
2440 NULL,
2441 /* pfnGetVersion */
2442 dmgGetVersion,
2443 /* pfnGetSectorSize */
2444 dmgGetSectorSize,
2445 /* pfnGetSize */
2446 dmgGetSize,
2447 /* pfnGetFileSize */
2448 dmgGetFileSize,
2449 /* pfnGetPCHSGeometry */
2450 dmgGetPCHSGeometry,
2451 /* pfnSetPCHSGeometry */
2452 dmgSetPCHSGeometry,
2453 /* pfnGetLCHSGeometry */
2454 dmgGetLCHSGeometry,
2455 /* pfnSetLCHSGeometry */
2456 dmgSetLCHSGeometry,
2457 /* pfnQueryRegions */
2458 NULL,
2459 /* pfnRegionListRelease */
2460 NULL,
2461 /* pfnGetImageFlags */
2462 dmgGetImageFlags,
2463 /* pfnGetOpenFlags */
2464 dmgGetOpenFlags,
2465 /* pfnSetOpenFlags */
2466 dmgSetOpenFlags,
2467 /* pfnGetComment */
2468 dmgGetComment,
2469 /* pfnSetComment */
2470 dmgSetComment,
2471 /* pfnGetUuid */
2472 dmgGetUuid,
2473 /* pfnSetUuid */
2474 dmgSetUuid,
2475 /* pfnGetModificationUuid */
2476 dmgGetModificationUuid,
2477 /* pfnSetModificationUuid */
2478 dmgSetModificationUuid,
2479 /* pfnGetParentUuid */
2480 dmgGetParentUuid,
2481 /* pfnSetParentUuid */
2482 dmgSetParentUuid,
2483 /* pfnGetParentModificationUuid */
2484 dmgGetParentModificationUuid,
2485 /* pfnSetParentModificationUuid */
2486 dmgSetParentModificationUuid,
2487 /* pfnDump */
2488 dmgDump,
2489 /* pfnGetTimestamp */
2490 NULL,
2491 /* pfnGetParentTimestamp */
2492 NULL,
2493 /* pfnSetParentTimestamp */
2494 NULL,
2495 /* pfnGetParentFilename */
2496 NULL,
2497 /* pfnSetParentFilename */
2498 NULL,
2499 /* pfnComposeLocation */
2500 genericFileComposeLocation,
2501 /* pfnComposeName */
2502 genericFileComposeName,
2503 /* pfnCompact */
2504 NULL,
2505 /* pfnResize */
2506 NULL,
2507 /* pfnRepair */
2508 NULL,
2509 /* pfnTraverseMetadata */
2510 NULL,
2511 /* u32VersionEnd */
2512 VD_IMGBACKEND_VERSION
2513};
2514
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