github.com/Microsoft/azure-vhd-utils@v0.0.0-20230613175315-7c30a3748a1b/vhdcore/vhdCookie.go (about) 1 package vhdcore 2 3 import "bytes" 4 5 // VhdFooterCookie is the cookie value stored in VHD footer 6 // Microsoft uses the “conectix” string to identify a hard disk image created by 7 // Microsoft Virtual Server, Virtual PC, and predecessor products. The cookie is 8 // stored as an eight-character ASCII string with the “c” in the first byte, 9 // the “o” in the second byte, and so on. 10 // 11 const VhdFooterCookie = "conectix" 12 13 // VhdHeaderCookie is the header cookie which is always cxsparse 14 // 15 const VhdHeaderCookie = "cxsparse" 16 17 // Cookie represents the Vhd header or Vhd footer cookie. 18 // Footer Cookie are used to uniquely identify the original creator of the hard disk 19 // image. The values are case-sensitive. Header Cookie holds the value "cxsparse". 20 type Cookie struct { 21 Data []byte 22 isHeader bool 23 } 24 25 // CreateNewVhdCookie creates a new VhdCookie, the new instance's Data will be simply 26 // reference to the byte slice data (i.e. this function will not create a copy) 27 // 28 func CreateNewVhdCookie(isHeader bool, data []byte) *Cookie { 29 return &Cookie{isHeader: isHeader, Data: data} 30 } 31 32 // CreateFooterCookie creates a VhdCookie representing vhd footer cookie 33 // 34 func CreateFooterCookie() *Cookie { 35 return CreateNewVhdCookie(false, []byte(VhdFooterCookie)) 36 } 37 38 // CreateHeaderCookie creates a VhdCookie representing vhd header cookie 39 // 40 func CreateHeaderCookie() *Cookie { 41 return CreateNewVhdCookie(true, []byte(VhdHeaderCookie)) 42 } 43 44 // IsValid checks whether this this instance's internal cookie string is valid. 45 // 46 func (c *Cookie) IsValid() bool { 47 if c.isHeader { 48 return bytes.Equal(c.Data, []byte(VhdHeaderCookie)) 49 } 50 51 return bytes.Equal(c.Data, []byte(VhdFooterCookie)) 52 } 53 54 // CreateCopy creates a copy of this instance 55 // 56 func (c *Cookie) CreateCopy() *Cookie { 57 cp := &Cookie{isHeader: c.isHeader} 58 cp.Data = make([]byte, len(c.Data)) 59 copy(cp.Data, c.Data) 60 return cp 61 } 62 63 // Equal returns true if this and other points to the same instance or contents of field 64 // values of two are same. 65 // 66 func (c *Cookie) Equal(other *Cookie) bool { 67 if other == nil { 68 return false 69 } 70 71 if c == other { 72 return true 73 } 74 75 return c.isHeader == other.isHeader && bytes.Equal(c.Data, other.Data) 76 } 77 78 // String returns the string representation of this range, this satisfies stringer interface. 79 // 80 func (c *Cookie) String() string { 81 return string(c.Data) 82 }