Pkg.DMG — API for parsing Apple Disk Image (DMG) files

Overview

The Pkg.DMG module contains the API for parsing Apple Disk Image (DMG) files.

DMG is Apple’s native disk image format, commonly used for distributing macOS applications. A DMG file contains one or more partitions, each of which can hold a file system such as APFS or HFS+. Partitions are typically compressed using zlib, bzip2, LZFSE, LZMA, or ADC, and optionally encrypted with AES-128 or AES-256.

Enumerating Partitions

The following code example demonstrates how to list the partitions in a DMG file:

from Pro.Core import *
from Pkg.DMG import *

def listPartitions(fname):
    c = createContainerFromFile(fname)
    if c.isNull():
        return
    obj = DMGObject()
    if not obj.Load(c) or not obj.Initialize():
        return
    for i in range(obj.GetPartitionCount()):
        pi = obj.GetPartitionInfo(i)
        print("Partition %d: %s (0x%X bytes)" % (i, pi.name, pi.size))

Extracting Partition Data

The following code example demonstrates how to extract the raw data from a partition:

from Pro.Core import *
from Pkg.DMG import *

def extractPartition(fname, index):
    c = createContainerFromFile(fname)
    if c.isNull():
        return
    obj = DMGObject()
    if not obj.Load(c) or not obj.Initialize():
        return
    data = obj.GetPartitionData(index)
    if data.isNull():
        print("Failed to decompress partition %d" % index)
        return
    print("Extracted %d bytes" % data.size())

Module API

Pkg.DMG module API.

Classes:

DMGObject()

This class represents an Apple Disk Image (DMG) file.

PartitionInfo()

Contains information about a partition within a DMG image.

class DMGObject

Bases: Pro.Core.CFFObject

This class represents an Apple Disk Image (DMG) file.

Use GetPartitionCount() and GetPartitionInfo() to enumerate the partitions within the image.

Methods:

GetPartitionCount()

Returns the number of data partitions in the DMG.

GetPartitionData(i[, wo])

Decompresses and returns the raw data for the partition at the given index.

GetPartitionInfo(i)

Returns information about the partition at the given index.

GetPlist()

Returns the raw XML plist data from the DMG resource fork.

GetPartitionCount()int

Returns the number of data partitions in the DMG.

Returns

The partition count.

Return type

int

GetPartitionData(i: int, wo: Optional[object] = None)Pro.Core.NTContainer

Decompresses and returns the raw data for the partition at the given index.

Parameters
  • i (int) – The zero-based partition index.

  • wo – An optional wait object for abort support.

Returns

The decompressed partition data, or a null container on failure.

Return type

NTContainer

See also GetPartitionInfo().

GetPartitionInfo(i: int)Optional[Pkg.DMG.PartitionInfo]

Returns information about the partition at the given index.

Parameters

i (int) – The zero-based partition index.

Returns

The partition information, or None if the index is invalid.

Return type

Optional[PartitionInfo]

See also GetPartitionCount().

GetPlist()Pro.Core.NTContainer

Returns the raw XML plist data from the DMG resource fork.

Returns

The plist data, or a null container on failure.

Return type

NTContainer

class PartitionInfo

Contains information about a partition within a DMG image.

Attributes:

index

The zero-based index of the partition within the DMG.

name

The partition name.

size

The uncompressed size of the partition in bytes.

index: int

The zero-based index of the partition within the DMG.

name: str

The partition name.

size: int

The uncompressed size of the partition in bytes.