// Package rar5 reads RAR 5 archives (the format written by WinRAR 5 and // later, including the RAR 7 dictionary extension). // // It covers what an uploaded diagnostic bundle needs and nothing more: a // single-volume archive read sequentially from a stream. Encrypted archives, // multi-volume sets, self-extracting archives and the legacy RAR 1.5-4.x // format are rejected with a dedicated error. package rar5 import ( "bufio" "encoding/binary" "errors" "fmt" "hash" "hash/crc32" "io" "time" ) var ( ErrNotRAR = errors.New("rar5: not a RAR archive") ErrLegacyFormat = errors.New("rar5: RAR 4.x and older archives are not supported") ErrEncrypted = errors.New("rar5: password-protected archives are not supported") ErrMultiVolume = errors.New("rar5: multi-volume archives are not supported") ErrUnsupported = errors.New("rar5: unsupported compression version") ErrDictionaryTooBig = errors.New("rar5: dictionary size exceeds the supported limit") ErrCorrupt = errors.New("rar5: corrupt archive") ErrChecksum = errors.New("rar5: file checksum mismatch") ) const ( signature = "Rar!\x1a\x07\x01\x00" legacySignature = "Rar!\x1a\x07\x00" maxHeaderSize = 2 << 20 // maxDictionarySize bounds the sliding window an archive may request. maxDictionarySize = 1 << 30 blockMain = 1 blockFile = 2 blockService = 3 blockEncryption = 4 blockEnd = 5 headerHasExtra = 0x0001 headerHasData = 0x0002 headerSplitBefore = 0x0008 headerSplitAfter = 0x0010 archiveIsVolume = 0x0001 archiveIsSolid = 0x0004 fileIsDirectory = 0x0001 fileHasMTime = 0x0002 fileHasCRC32 = 0x0004 fileUnknownSize = 0x0008 extraEncryption = 0x01 extraTime = 0x03 extraRedirection = 0x05 // windowsEpochOffset is the number of 100ns ticks between the Windows // FILETIME epoch (1601) and the Unix epoch. windowsEpochOffset = 116444736000000000 ) // Header describes one archive member. type Header struct { Name string // path with '/' separators IsDir bool UnpackedSize int64 // valid unless UnknownSize UnknownSize bool ModTime time.Time // zero when the archive does not record it } // Reader reads the members of an archive in order. After Next returns a // header, Read returns that member's content. type Reader struct { br *bufio.Reader solid bool // archive-level solid flag dec *decoder file *fileReader } // fileReader is the content stream of the current member. type fileReader struct { packed packedReader src io.Reader // packed (stored) or the decoder compressed bool size int64 // -1 when unknown read int64 sum hash.Hash32 wantSum uint32 err error } // packedReader limits reads to the data area of the current file block. type packedReader struct { br *bufio.Reader n int64 } func (p *packedReader) Read(b []byte) (int, error) { if p.n <= 0 { return 0, io.EOF } if int64(len(b)) > p.n { b = b[:p.n] } n, err := p.br.Read(b) p.n -= int64(n) if err == io.EOF { err = io.ErrUnexpectedEOF } return n, err } func (p *packedReader) ReadByte() (byte, error) { if p.n <= 0 { return 0, io.EOF } c, err := p.br.ReadByte() if err != nil { if err == io.EOF { err = io.ErrUnexpectedEOF } return 0, err } p.n-- return c, nil } // NewReader checks the archive signature and returns a Reader positioned // before the first member. func NewReader(r io.Reader) (*Reader, error) { br := bufio.NewReaderSize(r, 64*1024) head, err := br.Peek(len(signature)) if err != nil { if len(head) >= len(legacySignature) && string(head[:len(legacySignature)]) == legacySignature { return nil, ErrLegacyFormat } return nil, ErrNotRAR } switch { case string(head) == signature: case string(head[:len(legacySignature)]) == legacySignature: return nil, ErrLegacyFormat default: return nil, ErrNotRAR } if _, err := br.Discard(len(signature)); err != nil { return nil, err } return &Reader{br: br}, nil } // Next advances to the next member. It returns io.EOF after the last one. func (r *Reader) Next() (*Header, error) { if err := r.finishFile(); err != nil { return nil, err } for { block, err := r.readBlock() if err != nil { return nil, err } switch block.kind { case blockMain: flags := block.vint() if block.err != nil { return nil, ErrCorrupt } if flags&archiveIsVolume != 0 { return nil, ErrMultiVolume } r.solid = flags&archiveIsSolid != 0 case blockFile: header, err := r.openFile(block) if err != nil { return nil, err } if header != nil { return header, nil } case blockEncryption: return nil, ErrEncrypted case blockEnd: return nil, io.EOF } // Service blocks (comments, quick-open data, recovery records), // unknown block types and members that are not regular files. if err := r.discard(block.dataSize); err != nil { return nil, err } } } // Read reads the content of the current member. func (r *Reader) Read(p []byte) (int, error) { f := r.file if f == nil { return 0, io.EOF } if f.err != nil { return 0, f.err } n, err := f.src.Read(p) f.read += int64(n) if f.sum != nil { f.sum.Write(p[:n]) } if err == nil && f.size >= 0 && f.read > f.size { err = ErrCorrupt } if err == io.EOF { switch { case f.size >= 0 && f.read != f.size: err = ErrCorrupt case f.sum != nil && f.sum.Sum32() != f.wantSum: err = ErrChecksum } } if err != nil { f.err = err } return n, err } // finishFile moves past the current member. In a solid archive the // compressed stream of every file continues the previous one, so the rest of // the file has to be decoded; otherwise its packed data is skipped. func (r *Reader) finishFile() error { f := r.file if f == nil { return nil } r.file = nil if f.err != nil && f.err != io.EOF { return f.err } if r.solid && f.compressed && f.err == nil { r.file = f _, err := io.Copy(io.Discard, r) r.file = nil if err != nil { return err } } return r.discard(f.packed.n) } func (r *Reader) discard(n int64) error { for n > 0 { step := int(min(n, 1<<20)) discarded, err := r.br.Discard(step) n -= int64(discarded) if err != nil { return ErrCorrupt } } return nil } // block is one parsed archive block header. type block struct { kind uint64 flags uint64 dataSize int64 extra []byte // extra area records fields // type-specific fields } // readBlock reads and CRC-checks the next block header. The data area that // may follow is left unread. func (r *Reader) readBlock() (*block, error) { var crcBytes [4]byte if _, err := io.ReadFull(r.br, crcBytes[:]); err != nil { if err == io.EOF { // an archive without an end-of-archive block return nil, io.EOF } return nil, ErrCorrupt } // header size: a vint of at most 3 bytes var sizeBytes []byte var size uint64 for shift := 0; ; shift += 7 { c, err := r.br.ReadByte() if err != nil || shift > 14 { return nil, ErrCorrupt } sizeBytes = append(sizeBytes, c) size |= uint64(c&0x7f) << shift if c&0x80 == 0 { break } } if size == 0 || size > maxHeaderSize { return nil, ErrCorrupt } body := make([]byte, size) if _, err := io.ReadFull(r.br, body); err != nil { return nil, ErrCorrupt } sum := crc32.Update(crc32.ChecksumIEEE(sizeBytes), crc32.IEEETable, body) if sum != binary.LittleEndian.Uint32(crcBytes[:]) { return nil, ErrCorrupt } b := &block{fields: fields{b: body}} b.kind = b.vint() b.flags = b.vint() var extraSize uint64 if b.flags&headerHasExtra != 0 { extraSize = b.vint() } if b.flags&headerHasData != 0 { dataSize := b.vint() if dataSize > 1<<62 { return nil, ErrCorrupt } b.dataSize = int64(dataSize) } if b.err != nil || extraSize > uint64(len(b.b)) { return nil, ErrCorrupt } b.extra = b.b[uint64(len(b.b))-extraSize:] b.b = b.b[:uint64(len(b.b))-extraSize] return b, nil } // openFile parses a file block and prepares its content stream. It returns a // nil header for members that are skipped (links). func (r *Reader) openFile(b *block) (*Header, error) { if b.flags&(headerSplitBefore|headerSplitAfter) != 0 { return nil, ErrMultiVolume } fileFlags := b.vint() unpackedSize := b.vint() b.vint() // attributes header := &Header{ IsDir: fileFlags&fileIsDirectory != 0, UnknownSize: fileFlags&fileUnknownSize != 0, UnpackedSize: int64(unpackedSize), } if fileFlags&fileHasMTime != 0 { header.ModTime = time.Unix(int64(b.uint32()), 0) } var wantSum uint32 if fileFlags&fileHasCRC32 != 0 { wantSum = b.uint32() } compression := b.vint() b.vint() // host OS header.Name = string(b.bytes(b.vint())) if b.err != nil || unpackedSize > 1<<62 { return nil, ErrCorrupt } encrypted, link, err := parseFileExtra(b.extra, header) if err != nil { return nil, err } if encrypted { return nil, fmt.Errorf("%w: %s", ErrEncrypted, header.Name) } if link { return nil, nil } f := &fileReader{packed: packedReader{br: r.br, n: b.dataSize}, size: header.UnpackedSize} if header.UnknownSize { f.size = -1 } if fileFlags&fileHasCRC32 != 0 && !header.IsDir { f.sum = crc32.NewIEEE() f.wantSum = wantSum } method := (compression >> 7) & 7 if method == 0 || header.IsDir || (b.dataSize == 0 && f.size == 0) { f.src = &f.packed } else { if err := r.openDecoder(f, compression); err != nil { return nil, err } } r.file = f return header, nil } // openDecoder points the shared decoder at the file's packed data. func (r *Reader) openDecoder(f *fileReader, compression uint64) error { version := compression & 0x3f fileSolid := compression&0x40 != 0 var dictionary uint64 switch version { case 0: dictionary = 0x20000 << ((compression >> 10) & 0x0f) case 1: dictionary = 0x20000 << ((compression >> 10) & 0x1f) dictionary += dictionary / 32 * ((compression >> 15) & 0x1f) default: return ErrUnsupported } if dictionary > maxDictionarySize { return ErrDictionaryTooBig } if r.dec == nil { if fileSolid { return ErrCorrupt } r.dec = new(decoder) } windowSize := int(dictionary) if !r.solid && !fileSolid && f.size >= 0 { // Matches never reach back past the start of a standalone file, so // a small file does not need the whole dictionary. windowSize = int(min(dictionary, uint64(f.size))) } r.dec.init(&f.packed, !fileSolid, windowSize, version == 1) f.src = r.dec f.compressed = true return nil } // parseFileExtra reads the extra area records of a file block. func parseFileExtra(extra []byte, header *Header) (encrypted, link bool, err error) { records := fields{b: extra} for len(records.b) > 0 { record := fields{b: records.bytes(records.vint())} if records.err != nil { return false, false, ErrCorrupt } switch record.vint() { case extraEncryption: encrypted = true case extraRedirection: link = true case extraTime: flags := record.vint() if flags&0x02 == 0 { continue } if flags&0x01 != 0 { header.ModTime = time.Unix(int64(record.uint32()), 0) } else { ticks := int64(record.uint64()) - windowsEpochOffset header.ModTime = time.Unix(ticks/1e7, ticks%1e7*100) } if record.err != nil { return false, false, ErrCorrupt } } } return encrypted, link, nil } // fields decodes the fields of a block header. The first failure is kept in // err and every later read returns zero. type fields struct { b []byte err error } func (f *fields) vint() uint64 { var v uint64 for shift := 0; shift < 70; shift += 7 { if f.err != nil || len(f.b) == 0 { break } c := f.b[0] f.b = f.b[1:] v |= uint64(c&0x7f) << shift if c&0x80 == 0 { return v } } f.err = ErrCorrupt return 0 } func (f *fields) bytes(n uint64) []byte { if f.err != nil || n > uint64(len(f.b)) { f.err = ErrCorrupt return nil } out := f.b[:n] f.b = f.b[n:] return out } func (f *fields) uint32() uint32 { b := f.bytes(4) if b == nil { return 0 } return binary.LittleEndian.Uint32(b) } func (f *fields) uint64() uint64 { b := f.bytes(8) if b == nil { return 0 } return binary.LittleEndian.Uint64(b) }