Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ The worst scenario would likely be to extract the archive in reverse order.
### Detecting the wrong password

It's virtually impossible to _reliably_ detect the wrong password versus some other corruption in a password protected archive.
This is partly due to how CBC decryption works; with the wrong password you don't get any sort of decryption error, you just a stream of bytes that aren't the correct ones.
This is partly due to how CBC decryption works; with the wrong password you don't get any sort of decryption error, you just get a stream of bytes that aren't the correct ones.
This manifests itself when the file has been compressed _and_ encrypted; during extraction the file is decrypted and then decompressed so with the wrong password the decompression algorithm gets handed a stream which isn't valid so that's the error you see.

A `sevenzip.ReadError` error type can be returned for certain operations.
Expand Down
2 changes: 1 addition & 1 deletion reader.go
Original file line number Diff line number Diff line change
Expand Up @@ -515,7 +515,7 @@ func (z *Reader) init(r io.ReaderAt, size int64) (err error) {
}

if !fh.isEmptyStream && !fh.isEmptyFile {
f.folder, _ = header.streamsInfo.FileFolderAndSize(j)
f.folder, _, _ = header.streamsInfo.FileFolderAndSize(j)

// Make an exported copy of the folder index
f.Stream = f.folder
Expand Down
19 changes: 14 additions & 5 deletions struct.go
Original file line number Diff line number Diff line change
Expand Up @@ -210,28 +210,37 @@ func (si *streamsInfo) Folders() int {
return 0
}

func (si *streamsInfo) FileFolderAndSize(file int) (int, uint64) {
total := uint64(0)

func (si *streamsInfo) FileFolderAndSize(file int) (int, uint64, uint32) {
var (
folder int
streams uint64 = 1
crc uint32
)

if si.subStreamsInfo != nil {
total := uint64(0)

for folder, streams = range si.subStreamsInfo.streams {
total += streams
if uint64(file) < total { //nolint:gosec
break
}
}

if len(si.subStreamsInfo.digest) > 0 {
crc = si.subStreamsInfo.digest[file]
}
}

if streams == 1 {
return folder, si.unpackInfo.folder[folder].size[len(si.unpackInfo.folder[folder].coder)-1]
if len(si.unpackInfo.digest) > 0 {
crc = si.unpackInfo.digest[folder]
}

return folder, si.unpackInfo.folder[folder].size[len(si.unpackInfo.folder[folder].coder)-1], crc
}

return folder, si.subStreamsInfo.size[file]
return folder, si.subStreamsInfo.size[file], crc
}

func (si *streamsInfo) folderOffset(folder int) int64 {
Expand Down
6 changes: 1 addition & 5 deletions types.go
Original file line number Diff line number Diff line change
Expand Up @@ -851,11 +851,7 @@ func readHeader(r util.Reader) (*header, error) {
continue
}

if h.streamsInfo.subStreamsInfo != nil {
h.filesInfo.file[i].CRC32 = h.streamsInfo.subStreamsInfo.digest[j]
}

_, h.filesInfo.file[i].UncompressedSize = h.streamsInfo.FileFolderAndSize(j)
_, h.filesInfo.file[i].UncompressedSize, h.filesInfo.file[i].CRC32 = h.streamsInfo.FileFolderAndSize(j)
j++
}

Expand Down
Loading