|
| 1 | +package magic |
| 2 | + |
| 3 | +import ( |
| 4 | + "bytes" |
| 5 | + "encoding/binary" |
| 6 | +) |
| 7 | + |
| 8 | +const ( |
| 9 | + pharHaltCompiler = "__HALT_COMPILER();" |
| 10 | + pharManifestFixedLen = 18 |
| 11 | + pharEntryFixedLen = 28 |
| 12 | + pharEntryMinLen = pharEntryFixedLen + 1 |
| 13 | + pharManifestMaxLen = 100 << 20 |
| 14 | + pharAPIVersionMask = 0xfff0 |
| 15 | + pharMinimumAPIVersion = 0x1000 |
| 16 | + pharManifestLengthSize = 4 |
| 17 | + pharClosingTagSize = 3 |
| 18 | +) |
| 19 | + |
| 20 | +type pharStatus uint8 |
| 21 | + |
| 22 | +const ( |
| 23 | + pharNotFound pharStatus = iota |
| 24 | + pharIncomplete |
| 25 | + pharValid |
| 26 | +) |
| 27 | + |
| 28 | +func nativePHAR(data []byte) pharStatus { |
| 29 | + stubEnd := bytes.Index(data, []byte(pharHaltCompiler)) |
| 30 | + if stubEnd < 0 { |
| 31 | + return pharNotFound |
| 32 | + } |
| 33 | + stubEnd += len(pharHaltCompiler) |
| 34 | + |
| 35 | + manifestOffset, status := pharManifestOffset(data, stubEnd) |
| 36 | + if status != pharValid { |
| 37 | + return status |
| 38 | + } |
| 39 | + if len(data)-manifestOffset < pharManifestLengthSize { |
| 40 | + return pharIncomplete |
| 41 | + } |
| 42 | + |
| 43 | + manifestLength := binary.LittleEndian.Uint32(data[manifestOffset:]) |
| 44 | + if manifestLength < pharManifestFixedLen || manifestLength > pharManifestMaxLen { |
| 45 | + return pharNotFound |
| 46 | + } |
| 47 | + |
| 48 | + manifestStart := manifestOffset + pharManifestLengthSize |
| 49 | + manifestEnd64 := uint64(manifestStart) + uint64(manifestLength) |
| 50 | + if manifestEnd64 > uint64(len(data)) { |
| 51 | + return pharIncomplete |
| 52 | + } |
| 53 | + manifestEnd := int(manifestEnd64) |
| 54 | + manifest := data[manifestStart:manifestEnd] |
| 55 | + |
| 56 | + entryCount := binary.LittleEndian.Uint32(manifest) |
| 57 | + if entryCount == 0 { |
| 58 | + return pharNotFound |
| 59 | + } |
| 60 | + apiVersion := binary.BigEndian.Uint16(manifest[4:6]) |
| 61 | + if apiVersion&pharAPIVersionMask < pharMinimumAPIVersion { |
| 62 | + return pharNotFound |
| 63 | + } |
| 64 | + |
| 65 | + offset := 10 // entry count, API version, and global flags |
| 66 | + aliasLength, ok := pharUint32(manifest, &offset) |
| 67 | + if !ok || !pharSkip(manifest, &offset, aliasLength) { |
| 68 | + return pharNotFound |
| 69 | + } |
| 70 | + metadataLength, ok := pharUint32(manifest, &offset) |
| 71 | + if !ok || !pharSkip(manifest, &offset, metadataLength) { |
| 72 | + return pharNotFound |
| 73 | + } |
| 74 | + if uint64(entryCount)*pharEntryMinLen > uint64(len(manifest)-offset) { |
| 75 | + return pharNotFound |
| 76 | + } |
| 77 | + |
| 78 | + var payloadLength uint64 |
| 79 | + for range entryCount { |
| 80 | + filenameLength, ok := pharUint32(manifest, &offset) |
| 81 | + if !ok || filenameLength == 0 || !pharSkip(manifest, &offset, filenameLength) { |
| 82 | + return pharNotFound |
| 83 | + } |
| 84 | + if len(manifest)-offset < pharEntryFixedLen-pharManifestLengthSize { |
| 85 | + return pharNotFound |
| 86 | + } |
| 87 | + |
| 88 | + compressedSize := binary.LittleEndian.Uint32(manifest[offset+8:]) |
| 89 | + metadataLength := binary.LittleEndian.Uint32(manifest[offset+20:]) |
| 90 | + offset += pharEntryFixedLen - pharManifestLengthSize |
| 91 | + if !pharSkip(manifest, &offset, metadataLength) { |
| 92 | + return pharNotFound |
| 93 | + } |
| 94 | + payloadLength += uint64(compressedSize) |
| 95 | + } |
| 96 | + |
| 97 | + if uint64(manifestEnd)+payloadLength > uint64(len(data)) { |
| 98 | + return pharIncomplete |
| 99 | + } |
| 100 | + return pharValid |
| 101 | +} |
| 102 | + |
| 103 | +func pharManifestOffset(data []byte, offset int) (int, pharStatus) { |
| 104 | + if offset >= len(data) { |
| 105 | + return 0, pharIncomplete |
| 106 | + } |
| 107 | + if data[offset] != ' ' && data[offset] != '\n' { |
| 108 | + return offset, pharValid |
| 109 | + } |
| 110 | + if len(data)-offset < pharClosingTagSize { |
| 111 | + return 0, pharIncomplete |
| 112 | + } |
| 113 | + if data[offset+1] != '?' || data[offset+2] != '>' { |
| 114 | + return offset, pharValid |
| 115 | + } |
| 116 | + |
| 117 | + offset += pharClosingTagSize |
| 118 | + if offset >= len(data) { |
| 119 | + return 0, pharIncomplete |
| 120 | + } |
| 121 | + switch data[offset] { |
| 122 | + case '\n': |
| 123 | + offset++ |
| 124 | + case '\r': |
| 125 | + if offset+1 >= len(data) { |
| 126 | + return 0, pharIncomplete |
| 127 | + } |
| 128 | + if data[offset+1] != '\n' { |
| 129 | + return 0, pharNotFound |
| 130 | + } |
| 131 | + offset += 2 |
| 132 | + } |
| 133 | + return offset, pharValid |
| 134 | +} |
| 135 | + |
| 136 | +func pharUint32(data []byte, offset *int) (uint32, bool) { |
| 137 | + if len(data)-*offset < pharManifestLengthSize { |
| 138 | + return 0, false |
| 139 | + } |
| 140 | + value := binary.LittleEndian.Uint32(data[*offset:]) |
| 141 | + *offset += 4 |
| 142 | + return value, true |
| 143 | +} |
| 144 | + |
| 145 | +func pharSkip(data []byte, offset *int, length uint32) bool { |
| 146 | + end := uint64(*offset) + uint64(length) |
| 147 | + if end > uint64(len(data)) { |
| 148 | + return false |
| 149 | + } |
| 150 | + *offset = int(end) |
| 151 | + return true |
| 152 | +} |
0 commit comments