| 1 | package rardecode |
| 2 | |
| 3 | import ( |
| 4 | "encoding/binary" |
| 5 | "errors" |
| 6 | "hash" |
| 7 | ) |
| 8 | |
| 9 | const ( |
| 10 | _ = iota |
| 11 | decode20Ver |
| 12 | decode29Ver |
| 13 | decode50Ver |
| 14 | decode70Ver |
| 15 | |
| 16 | archiveVersion15 = 0 |
| 17 | archiveVersion50 = 1 |
| 18 | ) |
| 19 | |
| 20 | var ( |
| 21 | ErrCorruptBlockHeader = errors.New("rardecode: corrupt block header") |
| 22 | ErrCorruptFileHeader = errors.New("rardecode: corrupt file header") |
| 23 | ErrBadHeaderCRC = errors.New("rardecode: bad header crc") |
| 24 | ErrUnknownDecoder = errors.New("rardecode: unknown decoder version") |
| 25 | ErrDecoderOutOfData = errors.New("rardecode: decoder expected more data than is in packed file") |
| 26 | ErrArchiveEncrypted = errors.New("rardecode: archive encrypted, password required") |
| 27 | ErrArchivedFileEncrypted = errors.New("rardecode: archived files encrypted, password required") |
| 28 | ErrMultiVolume = errors.New("rardecode: multi-volume archive continues in next file") |
| 29 | errVolumeOrArchiveEnd = errors.New("rardecode: archive or volume end") |
| 30 | ) |
| 31 | |
| 32 | type readBuf []byte |
| 33 | |
| 34 | func (b *readBuf) byte() byte { |
| 35 | v := (*b)[0] |
| 36 | *b = (*b)[1:] |
| 37 | return v |
| 38 | } |
| 39 | |
| 40 | func (b *readBuf) uint16() uint16 { |
| 41 | v := binary.LittleEndian.Uint16(*b) |
| 42 | *b = (*b)[2:] |
| 43 | return v |
| 44 | } |
| 45 | |
| 46 | func (b *readBuf) uint32() uint32 { |
| 47 | v := binary.LittleEndian.Uint32(*b) |
| 48 | *b = (*b)[4:] |
| 49 | return v |
| 50 | } |
| 51 | |
| 52 | func (b *readBuf) uint64() uint64 { |
| 53 | v := binary.LittleEndian.Uint64(*b) |
| 54 | *b = (*b)[8:] |
| 55 | return v |
| 56 | } |
| 57 | |
| 58 | func (b *readBuf) bytes(n int) []byte { |
| 59 | v := (*b)[:n] |
| 60 | *b = (*b)[n:] |
| 61 | return v |
| 62 | } |
| 63 | |
| 64 | func (b *readBuf) uvarint() uint64 { |
| 65 | n, cnt := binary.Uvarint(*b) |
| 66 | if cnt == 0 { |
| 67 | cnt = len(*b) |
| 68 | } |
| 69 | *b = (*b)[cnt:] |
| 70 | return n |
| 71 | } |
| 72 | |
| 73 | // fileBlockHeader represents a file block in a RAR archive. |
| 74 | // Files may comprise one or more file blocks. |
| 75 | // Solid files retain decode tables and dictionary from previous solid files in the archive. |
| 76 | type fileBlockHeader struct { |
| 77 | first bool // first block in file |
| 78 | last bool // last block in file |
| 79 | arcSolid bool // archive is solid |
| 80 | dataOff int64 // offset to data for file block in archive volume |
| 81 | packedOff int64 // offset to data in packed file |
| 82 | blocknum int // number for current block in file |
| 83 | volnum int // archive volume number |
| 84 | winSize int64 // decode window size |
| 85 | hash func() hash.Hash // hash used for file checksum |
| 86 | hashKey []byte // optional hmac key to be used calculate file checksum |
| 87 | sum []byte // expected checksum for file contents |
| 88 | decVer int // decoder to use for file |
| 89 | key []byte // key for AES, non-empty if file encrypted |
| 90 | iv []byte // iv for AES, non-empty if file encrypted |
| 91 | errs []error // errors to return when trying to read file body |
| 92 | FileHeader |
| 93 | } |
| 94 | |
| 95 | // archiveBlockReader returns the next fileBlockHeader in an archive volume. |
| 96 | type archiveBlockReader interface { |
| 97 | init(br *bufVolumeReader) (int, error) // init volume and returns optional (>=0) volume number |
| 98 | nextBlock(br *bufVolumeReader) (*fileBlockHeader, error) // reads the volume and returns the next fileBlockHeader |
| 99 | useOldNaming() bool |
| 100 | } |