github.com/Microsoft/azure-vhd-utils@v0.0.0-20230613175315-7c30a3748a1b/vhdcore/header/parentlocator/parentLocator.go (about)

     1  package parentlocator
     2  
     3  import (
     4  	"github.com/Microsoft/azure-vhd-utils/vhdcore/common"
     5  	"log"
     6  	"strings"
     7  )
     8  
     9  // ParentLocator represents an entry in Parent locator table. Each entry represents
    10  // details (parent-hard-disk-locator-info) of file locator which is used to locate
    11  // the parent disk file of differencing hard disk.
    12  //
    13  type ParentLocator struct {
    14  	// Offset = 0, Size = 4
    15  	// This field stores the code representing the platform-specific format used for
    16  	// the file locator. Stored in big-endian format.
    17  	PlatformCode PlatformCode
    18  
    19  	// Offset = 4, Size = 4
    20  	// This field stores the number of 512-byte sectors needed to store the parent
    21  	// hard disk locator. Stored in big-endian format.
    22  	PlatformDataSpace int32
    23  
    24  	// Offset = 8, Size = 4
    25  	// This field stores the actual length of the parent hard disk locator in bytes.
    26  	// Stored in big-endian format.
    27  	PlatformDataLength int32
    28  
    29  	// Offset = 12, Size = 4
    30  	// This field must be set to zero.
    31  	// Stored in big-endian format.
    32  	Reserved int32
    33  
    34  	// Offset = 16, Size = 8
    35  	// This field stores the absolute file offset in bytes where the platform specific
    36  	// file locator data is stored. Stored in big-endian format.
    37  	PlatformDataOffset int64
    38  
    39  	// This is not a field that get stored or retrieved from disk's ParentLocator entry.
    40  	// We use this field to store the resolved file locator path.
    41  	PlatformSpecificFileLocator string
    42  }
    43  
    44  // SetPlatformSpecificFileLocator retrieves the file locator value and store that in the property
    45  // PlatformSpecificFileLocator
    46  //
    47  func (l *ParentLocator) SetPlatformSpecificFileLocator(fileLocator []byte) {
    48  	// 1. For the platform codes - W2Ru and W2Ku, fileLocator contents is UTF-16 encoded.
    49  	// 2. For the platform code  - MacX,          fileLocator contents is UTF-8 encoded.
    50  	// 3. For unknown platform code               fileLocator contents is treated as UTF-16 encoded.
    51  	//
    52  	// For 1 the byte order is little-endian
    53  	// For 3 the byte order is big-endian
    54  
    55  	if l.PlatformCode == PlatformCodeWi2R || l.PlatformCode == PlatformCodeWi2K {
    56  		log.Panicf("Deprecated PlatformCode: %d", l.PlatformCode)
    57  	}
    58  
    59  	if l.PlatformCode == PlatformCodeMac {
    60  		log.Panicf("Handling Mac OS alias stored as a blob is not implemented, PlatformCode: %d", l.PlatformCode)
    61  	}
    62  
    63  	if l.PlatformCode == PlatformCodeNone {
    64  		l.PlatformSpecificFileLocator = ""
    65  	} else if l.PlatformCode == PlatformCodeW2Ru {
    66  		//TODO: Add differencing disks path name, this is relative path
    67  		l.PlatformSpecificFileLocator = common.Utf16BytesToStringLE(fileLocator)
    68  	} else if l.PlatformCode == PlatformCodeW2Ku {
    69  		l.PlatformSpecificFileLocator = common.Utf16BytesToStringLE(fileLocator)
    70  	} else if l.PlatformCode == PlatformCodeMacX {
    71  		l.PlatformSpecificFileLocator = string(fileLocator)
    72  	} else {
    73  		l.PlatformSpecificFileLocator = strings.TrimSuffix(common.Utf16BytesToStringBE(fileLocator), "\x00")
    74  	}
    75  }