VBoxDMG.cpp revision e64031e20c39650a7bc902a3e1aba613b9415dee
/* $Id$ */
/** @file
* VBoxDMG - Intepreter for Apple Disk Images (DMG).
*/
/*
* Copyright (C) 2009 Oracle Corporation
*
* This file is part of VirtualBox Open Source Edition (OSE), as
* available from http://www.virtualbox.org. This file is free software;
* General Public License (GPL) as published by the Free Software
* Foundation, in version 2 as it comes in the "COPYING" file of the
* VirtualBox OSE distribution. VirtualBox OSE is distributed in the
* hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
*/
/*******************************************************************************
* Header Files *
*******************************************************************************/
#ifdef VBOXDMG_TESTING
# include <iprt/initterm.h>
#endif
/*******************************************************************************
* Structures and Typedefs *
*******************************************************************************/
/**
* UDIF checksum structure.
*/
typedef struct VBOXUDIFCKSUM
{
union
{
} uSum; /**< The checksum. */
typedef VBOXUDIFCKSUM *PVBOXUDIFCKSUM;
typedef const VBOXUDIFCKSUM *PCVBOXUDIFCKSUM;
/** @name Checksum Kind (VBOXUDIFCKSUM::u32Kind)
* @{ */
/** No checksum. */
#define VBOXUDIFCKSUM_NONE UINT32_C(0)
/** CRC-32. */
/** @} */
/**
* UDIF ID.
* This is kind of like a UUID only it isn't, but we'll use the UUID
* representation of it for simplicity.
*/
typedef RTUUID VBOXUDIFID;
typedef VBOXUDIFID *PVBOXUDIFID;
typedef const VBOXUDIFID *PCVBOXUDIFID;
/**
* UDIF footer used by Apple Disk Images (DMG).
*
* This is a footer placed 512 bytes from the end of the file. Typically a DMG
* file starts with the data, which is followed by the block table and then ends
* with this structure.
*
* All fields are stored in big endian format.
*/
#pragma pack(1)
typedef struct VBOXUDIF
{
uint64_t offRunData; /**< 0x010 - Where the running data fork starts (usually 0). (fUDIFRunningDataForkOffset) */
uint64_t offRsrc; /**< 0x028 - Where the resource fork starts (usually cbData or 0). (fUDIFRsrcForkOffset) */
} VBOXUDIF;
#pragma pack(0)
typedef const VBOXUDIF *PCVBOXUDIF;
/** The UDIF magic 'koly' (VBOXUDIF::u32Magic). */
/** The current UDIF version (VBOXUDIF::u32Version).
* This is currently the only we recognizes and will create. */
#define VBOXUDIF_VER_CURRENT 4
/** @name UDIF flags (VBOXUDIF::fFlags).
* @{ */
/** Flatten image whatever that means.
* (hdiutil -debug calls it kUDIFFlagsFlattened.) */
#define VBOXUDIF_FLAGS_FLATTENED RT_BIT_32(0)
/** Internet enabled image.
* (hdiutil -debug calls it kUDIFFlagsInternetEnabled) */
/** Mask of known bits. */
/** @} */
/** @name UDIF Image Types (VBOXUDIF::u32Type).
* @{ */
/** Device image type. (kUDIFDeviceImageType) */
#define VBOXUDIF_TYPE_DEVICE 1
/** Device image type. (kUDIFPartitionImageType) */
#define VBOXUDIF_TYPE_PARTITION 2
/** @} */
/**
* UDIF Resource Entry.
*/
typedef struct VBOXUDIFRSRCENTRY
{
/** The ID. */
/** Attributes. */
/** The name. */
char *pszName;
/** The CoreFoundation name. Can be NULL. */
char *pszCFName;
/** The size of the data. */
/** The raw data. */
/** Pointer to an UDIF resource entry. */
typedef VBOXUDIFRSRCENTRY *PVBOXUDIFRSRCENTRY;
/** Pointer to a const UDIF resource entry. */
typedef VBOXUDIFRSRCENTRY const *PCVBOXUDIFRSRCENTRY;
/**
* UDIF Resource Array.
*/
typedef struct VBOXUDIFRSRCARRAY
{
/** The array name. */
char szName[12];
/** The number of occupied entries. */
/** The array entries.
* A lazy bird ASSUME there are no more than 4 entries in any DMG. Increase the
* size if DMGs with more are found. */
/** Pointer to a UDIF resource array. */
typedef VBOXUDIFRSRCARRAY *PVBOXUDIFRSRCARRAY;
/** Pointer to a const UDIF resource array. */
typedef VBOXUDIFRSRCARRAY const *PCVBOXUDIFRSRCARRAY;
/**
* VirtualBox Apple Disk Image (DMG) interpreter instance data.
*/
typedef struct VBOXDMG
{
/** The open image file. */
/** The current file size. */
/** Flags the image was opened with. */
/** The filename.
* Kept around for logging and delete-on-close purposes. */
const char *pszFilename;
/** The resources.
* A lazy bird ASSUME there are only two arrays in the resource-fork section in
* the XML, namely 'blkx' and 'plst'. These have been assigned fixed indexes. */
/** The UDIF footer. */
} VBOXDMG;
/** Pointer to an instance of the DMG Image Interpreter. */
/** @name Resources indexes (into VBOXDMG::aRsrcs).
* @{ */
#define VBOXDMG_RSRC_IDX_BLKX 0
#define VBOXDMG_RSRC_IDX_PLST 1
/** @} */
/*******************************************************************************
* Defined Constants And Macros *
*******************************************************************************/
/** @def VBOXDMG_PRINTF
*/
#ifdef VBOXDMG_TESTING
# define VBOXDMG_PRINTF(a) RTPrintf a
#else
# define VBOXDMG_PRINTF(a) LogRel(a)
#endif
/** @def VBOXDMG_VALIDATE
*/
#ifdef VBOXDMG_TESTING
do { \
if (!(expr)) \
{ \
fRc = false; \
} \
} while (0)
#else
do { \
if (!(expr)) \
{ \
fRc = false; \
} \
} while (0)
#endif
/** VBoxDMG: Unable to parse the XML. */
#define VERR_VD_DMG_XML_PARSE_ERROR (-3280)
/*******************************************************************************
* Internal Functions *
*******************************************************************************/
/**
* Swaps endian.
* @param pUdif The structure.
*/
{
#ifndef RT_BIG_ENDIAN
#endif
}
/**
* Swaps endian from host cpu to file.
* @param pUdif The structure.
*/
{
}
/**
* Swaps endian from file to host cpu.
* @param pUdif The structure.
*/
{
}
/**
* Validates an UDIF footer structure.
*
* @returns true if valid, false and LogRel()s on failure.
* @param pFtr The UDIF footer to validate.
* @param offFtr The offset of the structure.
*/
{
bool fRc = true;
VBOXDMG_VALIDATE(!(pFtr->fFlags & ~VBOXUDIF_FLAGS_KNOWN_MASK), ("fFlags=%#RX32 fKnown=%RX32\n", pFtr->fFlags, VBOXUDIF_FLAGS_KNOWN_MASK));
VBOXDMG_VALIDATE(pFtr->cbData <= offFtr && pFtr->offData + pFtr->cbData <= offFtr, ("cbData=%#RX64 offData=%#RX64 offFtr=%#RX64\n", pFtr->cbData, pFtr->offData, offFtr));
VBOXDMG_VALIDATE(pFtr->cbRsrc <= offFtr && pFtr->offRsrc + pFtr->cbRsrc <= offFtr, ("cbRsrc=%#RX64 offRsrc=%#RX64 offFtr=%#RX64\n", pFtr->cbRsrc, pFtr->offRsrc, offFtr));
VBOXDMG_VALIDATE(pFtr->iSegment == 0, ("iSegment=%RU32 cSegments=%RU32\n", pFtr->iSegment, pFtr->cSegments));
VBOXDMG_VALIDATE(pFtr->cbXml <= offFtr && pFtr->offXml + pFtr->cbXml <= offFtr, ("cbXml=%#RX64 offXml=%#RX64 offFtr=%#RX64\n", pFtr->cbXml, pFtr->offXml, offFtr));
VBOXDMG_VALIDATE(pFtr->u32Type == VBOXUDIF_TYPE_DEVICE || pFtr->u32Type == VBOXUDIF_TYPE_PARTITION, ("u32Type=%RU32\n", pFtr->u32Type));
return fRc;
}
/**
* Swaps endian from host cpu to file.
* @param pId The structure.
*/
{
}
/**
* Swaps endian from file to host cpu.
* @param pId The structure.
*/
{
}
/**
* Swaps endian.
* @param pCkSum The structure.
*/
{
#ifdef RT_BIG_ENDIAN
#else
switch (u32Kind)
{
case VBOXUDIFCKSUM_NONE:
/* nothing to do here */
break;
case VBOXUDIFCKSUM_CRC32:
break;
default:
break;
}
#endif
}
/**
* Swaps endian from host cpu to file.
* @param pCkSum The structure.
*/
{
}
/**
* Swaps endian from file to host cpu.
* @param pCkSum The structure.
*/
{
}
/**
* Validates an UDIF checksum structure.
*
* @returns true if valid, false and LogRel()s on failure.
* @param pCkSum The checksum structure.
* @param pszPrefix The message prefix.
* @remarks This does not check the checksummed data.
*/
{
bool fRc = true;
{
case VBOXUDIFCKSUM_NONE:
break;
case VBOXUDIFCKSUM_CRC32:
break;
default:
break;
}
return fRc;
}
/** @copydoc VBOXHDDBACKEND::pfnClose */
{
{
/*
* If writable, flush changes, fix checksums, ++.
*/
/** @todo writable images. */
/*
* Close the file.
*/
}
/*
* Delete the file if requested, then free the remaining resources.
*/
int rc = VINF_SUCCESS;
if (fDelete)
{
{
}
{
}
{
}
}
return rc;
}
do { \
} while (0)
do { \
return psz; \
} while (0)
do { \
return psz; \
} while (0)
do { \
return psz; \
} while (0)
do { \
return psz; \
} while (0)
/**
* Finds the next tag end.
*
* @returns Pointer to a '>' or '\0'.
* @param pszCur The current position.
*/
static const char *vboxdmgXmlFindTagEnd(const char *pszCur)
{
/* Might want to take quoted '>' into account? */
char ch;
pszCur++;
return pszCur;
}
/**
* Finds the end tag.
*
* Does not deal with '<tag attr="1"/>' style tags.
*
* @returns Pointer to the first char in the end tag. NULL if another tag
* was encountered first or if we hit the end of the file.
* @param pszTag The tag name.
*/
{
char ch;
{
if (ch == '<')
{
{
return psz;
}
break;
}
psz++;
}
return NULL;
}
/**
* Reads a signed 32-bit value.
*
* @returns NULL on success, pointer to the offending text on failure.
* @param pi32 Where to store the value.
*/
{
/*
* <string>-1</string>
*/
char *pszNext;
if (rc != VWRN_TRAILING_CHARS)
return *ppszCur;
return NULL;
}
/**
* Reads an unsigned 32-bit value.
*
* @returns NULL on success, pointer to the offending text on failure.
* @param pu32 Where to store the value.
*/
{
/*
* <string>0x00ff</string>
*/
char *pszNext;
if (rc != VWRN_TRAILING_CHARS)
return *ppszCur;
return NULL;
}
/**
* Reads a string value.
*
* @returns NULL on success, pointer to the offending text on failure.
* @param ppszString Where to store the pointer to the string. The caller
* must free this using RTMemFree.
*/
{
/*
* <string>Driver Descriptor Map (DDM : 0)</string>
*/
if (!pszEnd)
return *ppszCur;
if (!*ppszString)
return *ppszCur;
return NULL;
}
/**
* Parses the BASE-64 coded data tags.
*
* @returns NULL on success, pointer to the offending text on failure.
* @param ppbData Where to store the pointer to the data we've read. The
* caller must free this using RTMemFree.
* @param pcbData The number of bytes we're returning.
*/
{
/*
* <data> AAAAA... </data>
*/
if (cbData == -1)
return *ppszCur;
if (!*ppbData)
return *ppszCur;
char *pszIgnored;
if (RT_FAILURE(rc))
{
return *ppszCur;
}
return NULL;
}
/**
* Parses the XML resource-fork in a rather presumptive manner.
*
* This function is supposed to construct the VBOXDMG::aRsrcs instance data
* parts.
*
* @returns NULL on success, pointer to the problematic text on failure.
* @param pThis The DMG instance data.
* @param pszXml The XML text to parse, UTF-8.
* @param cch The size of the the XML text.
*/
{
/*
* Verify the ?xml, !DOCTYPE and plist tags.
*/
/* <?xml version="1.0" encoding="UTF-8"?> */
while (*psz != '?')
{
if (!*psz)
return psz;
{
}
{
}
else
return psz;
}
/* <!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd"> */
/* <plist version="1.0"> */
/*
* Decend down to the 'resource-fork' dictionary.
* ASSUME it's the only top level dictionary.
*/
/* <dict> <key>resource-fork</key> */
/*
* Parse the keys in the resource-fork dictionary.
* ASSUME that there are just two, 'blkx' and 'plst'.
*/
{
/*
* Parse the key and Create the resource-fork entry.
*/
unsigned iRsrc;
{
}
{
}
else
return psz;
/*
* Decend into the array and add the elements to the resource entry.
*/
/* <array> */
{
return psz;
{
/* switch on the key. */
const char *pszErr;
{
}
{
}
{
}
{
}
{
pszErr = vboxdmgXmlParseData(&psz, &pThis->aRsrcs[iRsrc].aEntries[i].pbData, &pThis->aRsrcs[iRsrc].aEntries[i].cbData);
}
else
if (pszErr)
return pszErr;
} /* while not </dict> */
} /* while not </array> */
} /* while not </dict> */
/*
* ASSUMING there is only the 'resource-fork', we'll now see the end of
* the outer dict, plist and text.
*/
/* </dict> </plist> */
/* the end */
if (*psz)
return psz;
return NULL;
}
/**
* Worker for vboxdmgOpen that reads in and validates all the necessary
* structures from the image.
*
* This worker is really just a construct to avoid gotos and do-break-while-zero
* uglyness.
*
* @returns VBox status code.
* @param pThis The DMG instance data.
*/
{
/*
* Read the footer.
*/
if (RT_FAILURE(rc))
return rc;
return VERR_VD_GEN_INVALID_HEADER;
rc = RTFileReadAt(pThis->hFile, pThis->cbFile - sizeof(pThis->Ftr), &pThis->Ftr, sizeof(pThis->Ftr), NULL);
if (RT_FAILURE(rc))
return rc;
/*
* Do we recognize the footer structure? If so, is it valid?
*/
return VERR_VD_GEN_INVALID_HEADER;
return VERR_VD_GEN_INVALID_HEADER;
return VERR_VD_GEN_INVALID_HEADER;
{
return VERR_VD_GEN_INVALID_HEADER;
}
/*
* Read and parse the XML portion.
*/
if (!pszXml)
return VERR_NO_MEMORY;
if (RT_SUCCESS(rc))
{
if (!pszError)
{
/*
* What is next?
*/
}
else
{
VBOXDMG_PRINTF(("**** Bad XML at %#lx (%lu) ***\n%.256s\n**** Bad XML END ****\n",
}
}
if (RT_FAILURE(rc))
return rc;
return VINF_SUCCESS;
}
/** @copydoc VBOXHDDBACKEND::pfnOpen */
void **ppvBackendData)
{
/*
* Reject combinations we don't currently support.
*
* There is no point in being paranoid about the input here as we're just a
* simple backend and can expect the caller to be the only user and already
* have validate what it passes thru to us.
*/
if (!(fOpenFlags & VD_OPEN_FLAGS_READONLY))
return VERR_NOT_SUPPORTED;
/*
* Create the basic instance data structure and open the file,
* then hand it over to a worker function that does all the rest.
*/
if (!pThis)
return VERR_NO_MEMORY;
//pThis->cbFile = 0;
if (RT_SUCCESS(rc))
{
if (RT_SUCCESS(rc))
{
*ppvBackendData = pThis;
return rc;
}
}
/* We failed, let the close method clean up. */
return rc;
}
/** @copydoc VBOXHDDBACKEND::pfnCheckIfValid */
{
/*
* Open the file and read the footer.
*/
if (RT_FAILURE(rc))
return rc;
if (RT_SUCCESS(rc))
{
if (RT_SUCCESS(rc))
{
/*
* Do we recognize this stuff? Does it look valid?
*/
{
rc = VINF_SUCCESS;
else
{
}
}
else
}
}
return rc;
}
#ifdef VBOXDMG_TESTING
{
RTR3Init();
{
RTPrintf("syntax: tstVBoxDMG <image>\n");
return 1;
}
/* test 1 - vboxdmgCheckIfValid */
if (RT_FAILURE(rc))
{
return 1;
}
/* test 1 - vboxdmgOpen(RDONLY) & Close. */
if (RT_FAILURE(rc))
{
return 1;
}
if (RT_FAILURE(rc))
{
return 1;
}
RTPrintf("tstVBoxDMG: SUCCESS\n");
return 0;
}
#endif