Codebase list golang-github-ulikunitz-xz / db0cc5b
Import upstream version 0.5.10 Debian Janitor 2 years ago
88 changed file(s) with 458 addition(s) and 301 deletion(s). Raw diff Collapse all Expand all
0 # For most projects, this workflow file will not need changing; you simply need
1 # to commit it to your repository.
2 #
3 # You may wish to alter this file to override the set of languages analyzed,
4 # or to provide custom queries or build logic.
5 #
6 # ******** NOTE ********
7 # We have attempted to detect the languages in your repository. Please check
8 # the `language` matrix defined below to confirm you have the correct set of
9 # supported CodeQL languages.
10 #
11 name: "CodeQL"
12
13 on:
14 push:
15 branches: [ master ]
16 pull_request:
17 # The branches below must be a subset of the branches above
18 branches: [ master ]
19 schedule:
20 - cron: '22 20 * * 3'
21
22 jobs:
23 analyze:
24 name: Analyze
25 runs-on: ubuntu-latest
26
27 strategy:
28 fail-fast: false
29 matrix:
30 language: [ 'go' ]
31 # CodeQL supports [ 'cpp', 'csharp', 'go', 'java', 'javascript', 'python' ]
32 # Learn more:
33 # https://docs.github.com/en/free-pro-team@latest/github/finding-security-vulnerabilities-and-errors-in-your-code/configuring-code-scanning#changing-the-languages-that-are-analyzed
34
35 steps:
36 - name: Checkout repository
37 uses: actions/checkout@v2
38
39 # Initializes the CodeQL tools for scanning.
40 - name: Initialize CodeQL
41 uses: github/codeql-action/init@v1
42 with:
43 languages: ${{ matrix.language }}
44 # If you wish to specify custom queries, you can do so here or in a config file.
45 # By default, queries listed here will override any specified in a config file.
46 # Prefix the list here with "+" to use these queries and those in the config file.
47 # queries: ./path/to/local/query, your-org/your-repo/queries@main
48
49 # Autobuild attempts to build any compiled languages (C/C++, C#, or Java).
50 # If this step fails, then you should remove it and run the build manually (see below)
51 - name: Autobuild
52 uses: github/codeql-action/autobuild@v1
53
54 # ℹī¸ Command-line programs to run using the OS shell.
55 # 📚 https://git.io/JvXDl
56
57 # ✏ī¸ If the Autobuild fails above, remove it and uncomment the following three lines
58 # and modify them (or add more) to build your code if your project
59 # uses a compiled language
60
61 #- run: |
62 # make bootstrap
63 # make release
64
65 - name: Perform CodeQL Analysis
66 uses: github/codeql-action/analyze@v1
0 Copyright (c) 2014-2016 Ulrich Kunitz
0 Copyright (c) 2014-2021 Ulrich Kunitz
11 All rights reserved.
22
33 Redistribution and use in source and binary forms, with or without
0 # Security Policy
1
2 ## Supported Versions
3
4 Currently the last minor version v0.5.x is supported.
5
6 ## Reporting a Vulnerability
7
8 Report a vulnerability by creating a Github issue at
9 <https://github.com/ulikunitz/xz/issues>. Expect a response in a week.
00 # TODO list
1
2 ## Release v0.5.x
3
4 1. Support check flag in gxz command.
15
26 ## Release v0.6
37
48 1. Review encoder and check for lzma improvements under xz.
59 2. Fix binary tree matcher.
6 3. Compare compression ratio with xz tool using comparable parameters
7 and optimize parameters
8 4. Do some optimizations
9 - rename operation action and make it a simple type of size 8
10 - make maxMatches, wordSize parameters
11 - stop searching after a certain length is found (parameter sweetLen)
10 3. Compare compression ratio with xz tool using comparable parameters and optimize parameters
11 4. rename operation action and make it a simple type of size 8
12 5. make maxMatches, wordSize parameters
13 6. stop searching after a certain length is found (parameter sweetLen)
1214
1315 ## Release v0.7
1416
1517 1. Optimize code
1618 2. Do statistical analysis to get linear presets.
1719 3. Test sync.Pool compatability for xz and lzma Writer and Reader
18 3. Fuzz optimized code.
20 4. Fuzz optimized code.
1921
2022 ## Release v0.8
2123
3941
4042 ## Package lzma
4143
42 ### Release v0.6
43
44 - Rewrite Encoder into a simple greedy one-op-at-a-time encoder
45 including
46 + simple scan at the dictionary head for the same byte
47 + use the killer byte (requiring matches to get longer, the first
48 test should be the byte that would make the match longer)
49
44 ### v0.6
45
46 * Rewrite Encoder into a simple greedy one-op-at-a-time encoder including
47 * simple scan at the dictionary head for the same byte
48 * use the killer byte (requiring matches to get longer, the first test should be the byte that would make the match longer)
5049
5150 ## Optimizations
5251
53 - There may be a lot of false sharing in lzma.State; check whether this
54 can be improved by reorganizing the internal structure of it.
55 - Check whether batching encoding and decoding improves speed.
52 * There may be a lot of false sharing in lzma. State; check whether this can be improved by reorganizing the internal structure of it.
53
54 * Check whether batching encoding and decoding improves speed.
5655
5756 ### DAG optimizations
5857
59 - Use full buffer to create minimal bit-length above range encoder.
60 - Might be too slow (see v0.4)
58 * Use full buffer to create minimal bit-length above range encoder.
59 * Might be too slow (see v0.4)
6160
6261 ### Different match finders
6362
64 - hashes with 2, 3 characters additional to 4 characters
65 - binary trees with 2-7 characters (uint64 as key, use uint32 as
63 * hashes with 2, 3 characters additional to 4 characters
64 * binary trees with 2-7 characters (uint64 as key, use uint32 as
65
6666 pointers into a an array)
67 - rb-trees with 2-7 characters (uint64 as key, use uint32 as pointers
67
68 * rb-trees with 2-7 characters (uint64 as key, use uint32 as pointers
69
6870 into an array with bit-steeling for the colors)
6971
7072 ## Release Procedure
7173
72 - execute goch -l for all packages; probably with lower param like 0.5.
73 - check orthography with gospell
74 - Write release notes in doc/relnotes.
75 - Update README.md
76 - xb copyright . in xz directory to ensure all new files have Copyright
77 header
78 - VERSION=<version> go generate github.com/ulikunitz/xz/... to update
79 version files
80 - Execute test for Linux/amd64, Linux/x86 and Windows/amd64.
81 - Update TODO.md - write short log entry
82 - git checkout master && git merge dev
83 - git tag -a <version>
84 - git push
74 * execute goch -l for all packages; probably with lower param like 0.5.
75 * check orthography with gospell
76 * Write release notes in doc/relnotes.
77 * Update README.md
78 * xb copyright . in xz directory to ensure all new files have Copyright header
79 * `VERSION=<version> go generate github.com/ulikunitz/xz/...` to update version files
80 * Execute test for Linux/amd64, Linux/x86 and Windows/amd64.
81 * Update TODO.md - write short log entry
82 * `git checkout master && git merge dev`
83 * `git tag -a <version>`
84 * `git push`
8585
8686 ## Log
87
88 ### 2021-02-02
89
90 Mituo Heijo has fuzzed xz and found a bug in the function readIndexBody. The
91 function allocated a slice of records immediately after reading the value
92 without further checks. Since the number has been too large the make function
93 did panic. The fix is to check the number against the expected number of records
94 before allocating the records.
95
96 ### 2020-12-17
97
98 Release v0.5.9 fixes warnings, a typo and adds SECURITY.md.
99
100 One fix is interesting.
101
102 ```go
103 const (
104 a byte = 0x1
105 b = 0x2
106 )
107 ```
108
109 The constants a and b don't have the same type. Correct is
110
111 ```go
112 const (
113 a byte = 0x1
114 b byte = 0x2
115 )
116 ```
117
118 ### 2020-08-19
119
120 Release v0.5.8 fixes issue
121 [issue #35](https://github.com/ulikunitz/xz/issues/35).
122
123 ### 2020-02-24
124
125 Release v0.5.7 supports the check-ID None and fixes
126 [issue #27](https://github.com/ulikunitz/xz/issues/27).
87127
88128 ### 2019-02-20
89129
193233
194234 ### 2015-06-04
195235
196 It has been a productive day. I improved the interface of lzma.Reader
197 and lzma.Writer and fixed the error handling.
236 It has been a productive day. I improved the interface of lzma. Reader
237 and lzma. Writer and fixed the error handling.
198238
199239 ### 2015-06-01
200240
245285
246286 However I will implement a ReaderState and WriterState type to use
247287 static typing to ensure the right State object is combined with the
248 right lzbase.Reader and lzbase.Writer.
288 right lzbase. Reader and lzbase. Writer.
249289
250290 As a start I have implemented ReaderState and WriterState to ensure
251291 that the state for reading is only used by readers and WriterState only
267307
268308 ### 2015-04-05
269309
270 Implemented lzma.Reader and tested it.
310 Implemented lzma. Reader and tested it.
271311
272312 ### 2015-04-04
273313
274 Implemented baseReader by adapting code form lzma.Reader.
314 Implemented baseReader by adapting code form lzma. Reader.
275315
276316 ### 2015-04-03
277317
287327 (Javaïstes?)" is the the idea that using an embedded field E, all the
288328 methods of E will be defined on T. If E is an interface T satisfies E.
289329
290 https://talks.golang.org/2014/go4java.slide#51
330 <https://talks.golang.org/2014/go4java.slide#51>
291331
292332 I have never used this, but it seems to be a cool idea.
293333
312352
313353 1. Implemented simple lzmago tool
314354 2. Tested tool against large 4.4G file
315 - compression worked correctly; tested decompression with lzma
316 - decompression hits a full buffer condition
355 * compression worked correctly; tested decompression with lzma
356 * decompression hits a full buffer condition
317357 3. Fixed a bug in the compressor and wrote a test for it
318358 4. Executed full cycle for 4.4 GB file; performance can be improved ;-)
319359
320360 ### 2015-01-11
321361
322 - Release v0.2 because of the working LZMA encoder and decoder
362 * Release v0.2 because of the working LZMA encoder and decoder
0 // Copyright 2014-2017 Ulrich Kunitz. All rights reserved.
0 // Copyright 2014-2021 Ulrich Kunitz. All rights reserved.
11 // Use of this source code is governed by a BSD-style
22 // license that can be found in the LICENSE file.
33
5353
5454 // readUvarint reads a uvarint from the given byte reader.
5555 func readUvarint(r io.ByteReader) (x uint64, n int, err error) {
56 const maxUvarintLen = 10
57
5658 var s uint
5759 i := 0
5860 for {
6163 return x, i, err
6264 }
6365 i++
66 if i > maxUvarintLen {
67 return x, i, errOverflowU64
68 }
6469 if b < 0x80 {
65 if i > 10 || i == 10 && b > 1 {
70 if i == maxUvarintLen && b > 1 {
6671 return x, i, errOverflowU64
6772 }
6873 return x | uint64(b)<<s, i, nil
0 // Copyright 2014-2017 Ulrich Kunitz. All rights reserved.
0 // Copyright 2014-2021 Ulrich Kunitz. All rights reserved.
11 // Use of this source code is governed by a BSD-style
22 // license that can be found in the LICENSE file.
33
3030 }
3131 }
3232 }
33
34 func TestUvarIntCVE_2020_16845(t *testing.T) {
35 var a = []byte{0x81, 0x82, 0x83, 0x84, 0x85, 0x86, 0x87,
36 0x88, 0x89, 0x8a, 0x8b}
37
38 r := bytes.NewReader(a)
39 _, _, err := readUvarint(r)
40 if err != errOverflowU64 {
41 t.Fatalf("readUvarint overflow not detected")
42 }
43 }
0 // Copyright 2014-2017 Ulrich Kunitz. All rights reserved.
0 // Copyright 2014-2021 Ulrich Kunitz. All rights reserved.
11 // Use of this source code is governed by a BSD-style
22 // license that can be found in the LICENSE file.
33
2727 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
2828 OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
2929 `
30 const xzLicense = `Copyright (c) 2014-2019 Ulrich Kunitz
30 const xzLicense = `Copyright (c) 2014-2021 Ulrich Kunitz
3131 All rights reserved.
3232
3333 Redistribution and use in source and binary forms, with or without
0 // Copyright 2014-2017 Ulrich Kunitz. All rights reserved.
0 // Copyright 2014-2021 Ulrich Kunitz. All rights reserved.
11 // Use of this source code is governed by a BSD-style
22 // license that can be found in the LICENSE file.
33
00 package main
11
2 const version = "v0.5.6"
2 const version = "v0.5.10"
0 // Copyright 2014-2017 Ulrich Kunitz. All rights reserved.
0 // Copyright 2014-2021 Ulrich Kunitz. All rights reserved.
11 // Use of this source code is governed by a BSD-style
22 // license that can be found in the LICENSE file.
33
0 // Copyright 2014-2017 Ulrich Kunitz. All rights reserved.
0 // Copyright 2014-2021 Ulrich Kunitz. All rights reserved.
11 // Use of this source code is governed by a BSD-style
22 // license that can be found in the LICENSE file.
33
2727 }
2828
2929 const copyrightText = `
30 Copyright 2014-2019 Ulrich Kunitz. All rights reserved.
30 Copyright 2014-2021 Ulrich Kunitz. All rights reserved.
3131 Use of this source code is governed by a BSD-style
3232 license that can be found in the LICENSE file.
3333 `
0 // Copyright 2014-2017 Ulrich Kunitz. All rights reserved.
0 // Copyright 2014-2021 Ulrich Kunitz. All rights reserved.
11 // Use of this source code is governed by a BSD-style
22 // license that can be found in the LICENSE file.
33
0 // Copyright 2014-2017 Ulrich Kunitz. All rights reserved.
0 // Copyright 2014-2021 Ulrich Kunitz. All rights reserved.
11 // Use of this source code is governed by a BSD-style
22 // license that can be found in the LICENSE file.
33
00 package main
11
2 const version = "v0.5.6"
2 const version = "v0.5.10"
0 // Copyright 2014-2017 Ulrich Kunitz. All rights reserved.
0 // Copyright 2014-2021 Ulrich Kunitz. All rights reserved.
11 // Use of this source code is governed by a BSD-style
22 // license that can be found in the LICENSE file.
33
0 # Release Notes v0.5.10
1
2 This release fixes issue #40. Many thanks to Github user Misuo Heijo for fuzzing
3 the xz module.
0 # Release Notes v0.5.7
1
2 This release fixes issue #27 "Checksum None is valid" by supporting the
3 check-ID None.
4
5 Many thanks to [blacktop](https://github.com/blacktop) for reporting the
6 bug.
0 # Release Notes v0.5.8
1
2 This release fixes the security issue #35. The readUvarint function
3 would run infinitely given specific input. The function is now
4 terminating if more than 10 bytes of input have been read. The behavior
5 is tested.
6
7 Many thanks to Github user 0xdecaf for reporting the issue.
0 # Release Notes v0.5.9
1
2 This release fixes:
3
4 - all staticcheck issues in the public repositories.
5 - fixes wrong const type definitions; thanks Matt LaPlante
6 - fixes a typo; Michael Vetter
7 - adds a SECURITY.md file
0 // Copyright 2014-2017 Ulrich Kunitz. All rights reserved.
0 // Copyright 2014-2021 Ulrich Kunitz. All rights reserved.
11 // Use of this source code is governed by a BSD-style
22 // license that can be found in the LICENSE file.
33
0 // Copyright 2014-2017 Ulrich Kunitz. All rights reserved.
0 // Copyright 2014-2021 Ulrich Kunitz. All rights reserved.
11 // Use of this source code is governed by a BSD-style
22 // license that can be found in the LICENSE file.
33
4545
4646 // Constants for the checksum methods supported by xz.
4747 const (
48 None byte = 0x0
4849 CRC32 byte = 0x1
49 CRC64 = 0x4
50 SHA256 = 0xa
50 CRC64 byte = 0x4
51 SHA256 byte = 0xa
5152 )
5253
5354 // errInvalidFlags indicates that flags are invalid.
5758 // invalid.
5859 func verifyFlags(flags byte) error {
5960 switch flags {
60 case CRC32, CRC64, SHA256:
61 case None, CRC32, CRC64, SHA256:
6162 return nil
6263 default:
6364 return errInvalidFlags
6667
6768 // flagstrings maps flag values to strings.
6869 var flagstrings = map[byte]string{
70 None: "None",
6971 CRC32: "CRC-32",
7072 CRC64: "CRC-64",
7173 SHA256: "SHA-256",
8486 // hash method encoded in flags.
8587 func newHashFunc(flags byte) (newHash func() hash.Hash, err error) {
8688 switch flags {
89 case None:
90 newHash = newNoneHash
8791 case CRC32:
8892 newHash = newCRC32
8993 case CRC64:
564568 return []filter{f}, err
565569 }
566570
567 // writeFilters writes the filters.
568 func writeFilters(w io.Writer, filters []filter) (n int, err error) {
569 for _, f := range filters {
570 p, err := f.MarshalBinary()
571 if err != nil {
572 return n, err
573 }
574 k, err := w.Write(p)
575 n += k
576 if err != nil {
577 return n, err
578 }
579 }
580 return n, nil
581 }
582
583571 /*** Index ***/
584572
585573 // record describes a block in the xz file index.
673661
674662 // readIndexBody reads the index from the reader. It assumes that the
675663 // index indicator has already been read.
676 func readIndexBody(r io.Reader) (records []record, n int64, err error) {
664 func readIndexBody(r io.Reader, expectedRecordLen int) (records []record, n int64, err error) {
677665 crc := crc32.NewIEEE()
678666 // index indicator
679667 crc.Write([]byte{0})
689677 recLen := int(u)
690678 if recLen < 0 || uint64(recLen) != u {
691679 return nil, n, errors.New("xz: record number overflow")
680 }
681 if recLen != expectedRecordLen {
682 return nil, n, fmt.Errorf(
683 "xz: index length is %d; want %d",
684 recLen, expectedRecordLen)
692685 }
693686
694687 // list of records
0 // Copyright 2014-2017 Ulrich Kunitz. All rights reserved.
0 // Copyright 2014-2021 Ulrich Kunitz. All rights reserved.
11 // Use of this source code is governed by a BSD-style
22 // license that can be found in the LICENSE file.
33
8484 t.Fatalf("indicator %d; want %d", c, 0)
8585 }
8686
87 g, m, err := readIndexBody(&buf)
87 g, m, err := readIndexBody(&buf, len(records))
8888 if err != nil {
8989 for i, r := range g {
9090 t.Logf("records[%d] %v", i, r)
Binary diff not shown
00 module github.com/ulikunitz/xz
1
2 go 1.12
0 // Copyright 2014-2017 Ulrich Kunitz. All rights reserved.
0 // Copyright 2014-2021 Ulrich Kunitz. All rights reserved.
11 // Use of this source code is governed by a BSD-style
22 // license that can be found in the LICENSE file.
33
0 // Copyright 2014-2017 Ulrich Kunitz. All rights reserved.
0 // Copyright 2014-2021 Ulrich Kunitz. All rights reserved.
11 // Use of this source code is governed by a BSD-style
22 // license that can be found in the LICENSE file.
33
0 // Copyright 2014-2017 Ulrich Kunitz. All rights reserved.
0 // Copyright 2014-2021 Ulrich Kunitz. All rights reserved.
11 // Use of this source code is governed by a BSD-style
22 // license that can be found in the LICENSE file.
33
0 // Copyright 2014-2017 Ulrich Kunitz. All rights reserved.
0 // Copyright 2014-2021 Ulrich Kunitz. All rights reserved.
11 // Use of this source code is governed by a BSD-style
22 // license that can be found in the LICENSE file.
33
0 // Copyright 2014-2017 Ulrich Kunitz. All rights reserved.
0 // Copyright 2014-2021 Ulrich Kunitz. All rights reserved.
11 // Use of this source code is governed by a BSD-style
22 // license that can be found in the LICENSE file.
33
0 // Copyright 2014-2017 Ulrich Kunitz. All rights reserved.
0 // Copyright 2014-2021 Ulrich Kunitz. All rights reserved.
11 // Use of this source code is governed by a BSD-style
22 // license that can be found in the LICENSE file.
33
0 // Copyright 2014-2017 Ulrich Kunitz. All rights reserved.
0 // Copyright 2014-2021 Ulrich Kunitz. All rights reserved.
11 // Use of this source code is governed by a BSD-style
22 // license that can be found in the LICENSE file.
33
0 // Copyright 2014-2017 Ulrich Kunitz. All rights reserved.
0 // Copyright 2014-2021 Ulrich Kunitz. All rights reserved.
11 // Use of this source code is governed by a BSD-style
22 // license that can be found in the LICENSE file.
33
0 // Copyright 2014-2017 Ulrich Kunitz. All rights reserved.
0 // Copyright 2014-2021 Ulrich Kunitz. All rights reserved.
11 // Use of this source code is governed by a BSD-style
22 // license that can be found in the LICENSE file.
33
0 // Copyright 2014-2017 Ulrich Kunitz. All rights reserved.
0 // Copyright 2014-2021 Ulrich Kunitz. All rights reserved.
11 // Use of this source code is governed by a BSD-style
22 // license that can be found in the LICENSE file.
33
0 // Copyright 2014-2017 Ulrich Kunitz. All rights reserved.
0 // Copyright 2014-2021 Ulrich Kunitz. All rights reserved.
11 // Use of this source code is governed by a BSD-style
22 // license that can be found in the LICENSE file.
33
0 // Copyright 2014-2017 Ulrich Kunitz. All rights reserved.
0 // Copyright 2014-2021 Ulrich Kunitz. All rights reserved.
11 // Use of this source code is governed by a BSD-style
22 // license that can be found in the LICENSE file.
33
0 // Copyright 2014-2017 Ulrich Kunitz. All rights reserved.
0 // Copyright 2014-2021 Ulrich Kunitz. All rights reserved.
11 // Use of this source code is governed by a BSD-style
22 // license that can be found in the LICENSE file.
33
0 // Copyright 2014-2017 Ulrich Kunitz. All rights reserved.
0 // Copyright 2014-2021 Ulrich Kunitz. All rights reserved.
11 // Use of this source code is governed by a BSD-style
22 // license that can be found in the LICENSE file.
33
0 // Copyright 2014-2017 Ulrich Kunitz. All rights reserved.
0 // Copyright 2014-2021 Ulrich Kunitz. All rights reserved.
11 // Use of this source code is governed by a BSD-style
22 // license that can be found in the LICENSE file.
33
0 // Copyright 2014-2017 Ulrich Kunitz. All rights reserved.
0 // Copyright 2014-2021 Ulrich Kunitz. All rights reserved.
11 // Use of this source code is governed by a BSD-style
22 // license that can be found in the LICENSE file.
33
0 // Copyright 2014-2017 Ulrich Kunitz. All rights reserved.
0 // Copyright 2014-2021 Ulrich Kunitz. All rights reserved.
11 // Use of this source code is governed by a BSD-style
22 // license that can be found in the LICENSE file.
33
0 // Copyright 2014-2017 Ulrich Kunitz. All rights reserved.
0 // Copyright 2014-2021 Ulrich Kunitz. All rights reserved.
11 // Use of this source code is governed by a BSD-style
22 // license that can be found in the LICENSE file.
33
44 package lzma
55
66 import (
7 "bufio"
87 "errors"
9 "fmt"
10 "io"
118 "unicode"
129 )
1310
348345 return string(a)
349346 }
350347
348 /*
351349 // dumpNode writes a representation of the node v into the io.Writer.
352350 func (t *binTree) dumpNode(w io.Writer, v uint32, indent int) {
353351 if v == null {
376374 t.dumpNode(bw, t.root, 0)
377375 return bw.Flush()
378376 }
377 */
379378
380379 func (t *binTree) distance(v uint32) int {
381380 dist := int(t.front) - int(v)
0 // Copyright 2014-2017 Ulrich Kunitz. All rights reserved.
0 // Copyright 2014-2021 Ulrich Kunitz. All rights reserved.
11 // Use of this source code is governed by a BSD-style
22 // license that can be found in the LICENSE file.
33
0 // Copyright 2014-2017 Ulrich Kunitz. All rights reserved.
0 // Copyright 2014-2021 Ulrich Kunitz. All rights reserved.
11 // Use of this source code is governed by a BSD-style
22 // license that can be found in the LICENSE file.
33
1717 30, 17, 8, 14, 29, 13, 28, 27,
1818 }
1919
20 /*
2021 // ntz32 computes the number of trailing zeros for an unsigned 32-bit integer.
2122 func ntz32(x uint32) int {
2223 if x == 0 {
2526 x = (x & -x) * ntz32Const
2627 return int(ntz32Table[x>>27])
2728 }
29 */
2830
2931 // nlz32 computes the number of leading zeros for an unsigned 32-bit integer.
3032 func nlz32(x uint32) int {
0 // Copyright 2014-2017 Ulrich Kunitz. All rights reserved.
0 // Copyright 2014-2021 Ulrich Kunitz. All rights reserved.
11 // Use of this source code is governed by a BSD-style
22 // license that can be found in the LICENSE file.
33
0 // Copyright 2014-2017 Ulrich Kunitz. All rights reserved.
0 // Copyright 2014-2021 Ulrich Kunitz. All rights reserved.
11 // Use of this source code is governed by a BSD-style
22 // license that can be found in the LICENSE file.
33
0 // Copyright 2014-2017 Ulrich Kunitz. All rights reserved.
0 // Copyright 2014-2021 Ulrich Kunitz. All rights reserved.
11 // Use of this source code is governed by a BSD-style
22 // license that can be found in the LICENSE file.
33
0 // Copyright 2014-2017 Ulrich Kunitz. All rights reserved.
0 // Copyright 2014-2021 Ulrich Kunitz. All rights reserved.
11 // Use of this source code is governed by a BSD-style
22 // license that can be found in the LICENSE file.
33
0 // Copyright 2014-2017 Ulrich Kunitz. All rights reserved.
0 // Copyright 2014-2021 Ulrich Kunitz. All rights reserved.
11 // Use of this source code is governed by a BSD-style
22 // license that can be found in the LICENSE file.
33
199199 op, err := d.readOp()
200200 switch err {
201201 case nil:
202 break
202 // break
203203 case errEOS:
204204 d.eos = true
205205 if !d.rd.possiblyAtEnd() {
0 // Copyright 2014-2017 Ulrich Kunitz. All rights reserved.
0 // Copyright 2014-2021 Ulrich Kunitz. All rights reserved.
11 // Use of this source code is governed by a BSD-style
22 // license that can be found in the LICENSE file.
33
0 // Copyright 2014-2017 Ulrich Kunitz. All rights reserved.
0 // Copyright 2014-2021 Ulrich Kunitz. All rights reserved.
11 // Use of this source code is governed by a BSD-style
22 // license that can be found in the LICENSE file.
33
125125
126126 // Read reads data from the buffer contained in the decoder dictionary.
127127 func (d *decoderDict) Read(p []byte) (n int, err error) { return d.buf.Read(p) }
128
129 // Buffered returns the number of bytes currently buffered in the
130 // decoder dictionary.
131 func (d *decoderDict) buffered() int { return d.buf.Buffered() }
132
133 // Peek gets data from the buffer without advancing the rear index.
134 func (d *decoderDict) peek(p []byte) (n int, err error) { return d.buf.Peek(p) }
0 // Copyright 2014-2017 Ulrich Kunitz. All rights reserved.
0 // Copyright 2014-2021 Ulrich Kunitz. All rights reserved.
11 // Use of this source code is governed by a BSD-style
22 // license that can be found in the LICENSE file.
33
44 package lzma
55
66 import (
7 "fmt"
87 "testing"
98 )
10
11 func peek(d *decoderDict) []byte {
12 p := make([]byte, d.buffered())
13 k, err := d.peek(p)
14 if err != nil {
15 panic(fmt.Errorf("peek: "+
16 "Read returned unexpected error %s", err))
17 }
18 if k != len(p) {
19 panic(fmt.Errorf("peek: "+
20 "Read returned %d; wanted %d", k, len(p)))
21 }
22 return p
23 }
249
2510 func TestNewDecoderDict(t *testing.T) {
2611 if _, err := newDecoderDict(0); err == nil {
0 // Copyright 2014-2017 Ulrich Kunitz. All rights reserved.
0 // Copyright 2014-2021 Ulrich Kunitz. All rights reserved.
11 // Use of this source code is governed by a BSD-style
22 // license that can be found in the LICENSE file.
33
44 package lzma
55
6 import "fmt"
7
86 // directCodec allows the encoding and decoding of values with a fixed number
97 // of bits. The number of bits must be in the range [1,32].
108 type directCodec byte
11
12 // makeDirectCodec creates a directCodec. The function panics if the number of
13 // bits is not in the range [1,32].
14 func makeDirectCodec(bits int) directCodec {
15 if !(1 <= bits && bits <= 32) {
16 panic(fmt.Errorf("bits=%d out of range", bits))
17 }
18 return directCodec(bits)
19 }
209
2110 // Bits returns the number of bits supported by this codec.
2211 func (dc directCodec) Bits() int {
0 // Copyright 2014-2017 Ulrich Kunitz. All rights reserved.
0 // Copyright 2014-2021 Ulrich Kunitz. All rights reserved.
11 // Use of this source code is governed by a BSD-style
22 // license that can be found in the LICENSE file.
33
1919 posSlotBits = 6
2020 // number of align bits
2121 alignBits = 4
22 // maximum position slot
23 maxPosSlot = 63
2422 )
2523
2624 // distCodec provides encoding and decoding of distance values.
4240 dc.posModel[i].deepcopy(&src.posModel[i])
4341 }
4442 dc.alignCodec.deepcopy(&src.alignCodec)
45 }
46
47 // distBits returns the number of bits required to encode dist.
48 func distBits(dist uint32) int {
49 if dist < startPosModel {
50 return 6
51 }
52 // slot s > 3, dist d
53 // s = 2(bits(d)-1) + bit(d, bits(d)-2)
54 // s>>1 = bits(d)-1
55 // bits(d) = 32-nlz32(d)
56 // s>>1=31-nlz32(d)
57 // n = 5 + (s>>1) = 36 - nlz32(d)
58 return 36 - nlz32(dist)
5943 }
6044
6145 // newDistCodec creates a new distance codec.
0 // Copyright 2014-2017 Ulrich Kunitz. All rights reserved.
0 // Copyright 2014-2021 Ulrich Kunitz. All rights reserved.
11 // Use of this source code is governed by a BSD-style
22 // license that can be found in the LICENSE file.
33
0 // Copyright 2014-2017 Ulrich Kunitz. All rights reserved.
0 // Copyright 2014-2021 Ulrich Kunitz. All rights reserved.
11 // Use of this source code is governed by a BSD-style
22 // license that can be found in the LICENSE file.
33
0 // Copyright 2014-2017 Ulrich Kunitz. All rights reserved.
0 // Copyright 2014-2021 Ulrich Kunitz. All rights reserved.
11 // Use of this source code is governed by a BSD-style
22 // license that can be found in the LICENSE file.
33
1818 }
1919
2020 // encoderDict provides the dictionary of the encoder. It includes an
21 // addtional buffer atop of the actual dictionary.
21 // additional buffer atop of the actual dictionary.
2222 type encoderDict struct {
2323 buf buffer
2424 m matcher
0 // Copyright 2014-2017 Ulrich Kunitz. All rights reserved.
0 // Copyright 2014-2021 Ulrich Kunitz. All rights reserved.
11 // Use of this source code is governed by a BSD-style
22 // license that can be found in the LICENSE file.
33
0 // Copyright 2014-2017 Ulrich Kunitz. All rights reserved.
0 // Copyright 2014-2021 Ulrich Kunitz. All rights reserved.
11 // Use of this source code is governed by a BSD-style
22 // license that can be found in the LICENSE file.
33
0 // Copyright 2014-2017 Ulrich Kunitz. All rights reserved.
0 // Copyright 2014-2021 Ulrich Kunitz. All rights reserved.
11 // Use of this source code is governed by a BSD-style
22 // license that can be found in the LICENSE file.
33
0 // Copyright 2014-2017 Ulrich Kunitz. All rights reserved.
0 // Copyright 2014-2021 Ulrich Kunitz. All rights reserved.
11 // Use of this source code is governed by a BSD-style
22 // license that can be found in the LICENSE file.
33
263263 // state
264264 const (
265265 start chunkState = 'S'
266 stop = 'T'
266 stop chunkState = 'T'
267267 )
268268
269269 // errors for the chunk state handling
0 // Copyright 2014-2017 Ulrich Kunitz. All rights reserved.
0 // Copyright 2014-2021 Ulrich Kunitz. All rights reserved.
11 // Use of this source code is governed by a BSD-style
22 // license that can be found in the LICENSE file.
33
0 // Copyright 2014-2017 Ulrich Kunitz. All rights reserved.
0 // Copyright 2014-2021 Ulrich Kunitz. All rights reserved.
11 // Use of this source code is governed by a BSD-style
22 // license that can be found in the LICENSE file.
33
0 // Copyright 2014-2017 Ulrich Kunitz. All rights reserved.
0 // Copyright 2014-2021 Ulrich Kunitz. All rights reserved.
11 // Use of this source code is governed by a BSD-style
22 // license that can be found in the LICENSE file.
33
5353 lc.mid[i] = makeTreeCodec(3)
5454 }
5555 lc.high = makeTreeCodec(8)
56 }
57
58 // lBits gives the number of bits used for the encoding of the l value
59 // provided to the range encoder.
60 func lBits(l uint32) int {
61 switch {
62 case l < 8:
63 return 4
64 case l < 16:
65 return 5
66 default:
67 return 10
68 }
6956 }
7057
7158 // Encode encodes the length offset. The length offset l can be compute by
0 // Copyright 2014-2017 Ulrich Kunitz. All rights reserved.
0 // Copyright 2014-2021 Ulrich Kunitz. All rights reserved.
11 // Use of this source code is governed by a BSD-style
22 // license that can be found in the LICENSE file.
33
122122 minLP = 0
123123 maxLP = 4
124124 )
125
126 // minState and maxState define a range for the state values stored in
127 // the State values.
128 const (
129 minState = 0
130 maxState = 11
131 )
0 // Copyright 2014-2017 Ulrich Kunitz. All rights reserved.
0 // Copyright 2014-2021 Ulrich Kunitz. All rights reserved.
11 // Use of this source code is governed by a BSD-style
22 // license that can be found in the LICENSE file.
33
0 // Copyright 2014-2017 Ulrich Kunitz. All rights reserved.
0 // Copyright 2014-2021 Ulrich Kunitz. All rights reserved.
11 // Use of this source code is governed by a BSD-style
22 // license that can be found in the LICENSE file.
33
44 package lzma
55
66 import (
7 "errors"
87 "fmt"
98 "unicode"
109 )
2120 distance int64
2221 // length
2322 n int
24 }
25
26 // verify checks whether the match is valid. If that is not the case an
27 // error is returned.
28 func (m match) verify() error {
29 if !(minDistance <= m.distance && m.distance <= maxDistance) {
30 return errors.New("distance out of range")
31 }
32 if !(1 <= m.n && m.n <= maxMatchLen) {
33 return errors.New("length out of range")
34 }
35 return nil
36 }
37
38 // l return the l-value for the match, which is the difference of length
39 // n and 2.
40 func (m match) l() uint32 {
41 return uint32(m.n - minMatchLen)
42 }
43
44 // dist returns the dist value for the match, which is one less of the
45 // distance stored in the match.
46 func (m match) dist() uint32 {
47 return uint32(m.distance - minDistance)
4823 }
4924
5025 // Len returns the number of bytes matched.
0 // Copyright 2014-2017 Ulrich Kunitz. All rights reserved.
0 // Copyright 2014-2021 Ulrich Kunitz. All rights reserved.
11 // Use of this source code is governed by a BSD-style
22 // license that can be found in the LICENSE file.
33
0 // Copyright 2014-2017 Ulrich Kunitz. All rights reserved.
0 // Copyright 2014-2021 Ulrich Kunitz. All rights reserved.
11 // Use of this source code is governed by a BSD-style
22 // license that can be found in the LICENSE file.
33
0 // Copyright 2014-2017 Ulrich Kunitz. All rights reserved.
0 // Copyright 2014-2021 Ulrich Kunitz. All rights reserved.
11 // Use of this source code is governed by a BSD-style
22 // license that can be found in the LICENSE file.
33
130130 code uint32
131131 }
132132
133 // init initializes the range decoder, by reading from the byte reader.
134 func (d *rangeDecoder) init() error {
135 d.nrange = 0xffffffff
136 d.code = 0
137
138 b, err := d.br.ReadByte()
139 if err != nil {
140 return err
141 }
142 if b != 0 {
143 return errors.New("newRangeDecoder: first byte not zero")
144 }
145
146 for i := 0; i < 4; i++ {
147 if err = d.updateCode(); err != nil {
148 return err
149 }
150 }
151
152 if d.code >= d.nrange {
153 return errors.New("newRangeDecoder: d.code >= d.nrange")
154 }
155
156 return nil
157 }
158
159133 // newRangeDecoder initializes a range decoder. It reads five bytes from the
160134 // reader and therefore may return an error.
161135 func newRangeDecoder(br io.ByteReader) (d *rangeDecoder, err error) {
0 // Copyright 2014-2017 Ulrich Kunitz. All rights reserved.
0 // Copyright 2014-2021 Ulrich Kunitz. All rights reserved.
11 // Use of this source code is governed by a BSD-style
22 // license that can be found in the LICENSE file.
33
0 // Copyright 2014-2017 Ulrich Kunitz. All rights reserved.
0 // Copyright 2014-2021 Ulrich Kunitz. All rights reserved.
11 // Use of this source code is governed by a BSD-style
22 // license that can be found in the LICENSE file.
33
4747 chunkReader io.Reader
4848
4949 cstate chunkState
50 ctype chunkType
5150 }
5251
5352 // NewReader2 creates a reader for an LZMA2 chunk sequence.
0 // Copyright 2014-2017 Ulrich Kunitz. All rights reserved.
0 // Copyright 2014-2021 Ulrich Kunitz. All rights reserved.
11 // Use of this source code is governed by a BSD-style
22 // license that can be found in the LICENSE file.
33
0 // Copyright 2014-2017 Ulrich Kunitz. All rights reserved.
0 // Copyright 2014-2021 Ulrich Kunitz. All rights reserved.
11 // Use of this source code is governed by a BSD-style
22 // license that can be found in the LICENSE file.
33
5050 s.lenCodec.init()
5151 s.repLenCodec.init()
5252 s.distCodec.init()
53 }
54
55 // initState initializes the state.
56 func initState(s *state, p Properties) {
57 *s = state{Properties: p}
58 s.Reset()
5953 }
6054
6155 // newState creates a new state from the give Properties.
0 // Copyright 2014-2017 Ulrich Kunitz. All rights reserved.
0 // Copyright 2014-2021 Ulrich Kunitz. All rights reserved.
11 // Use of this source code is governed by a BSD-style
22 // license that can be found in the LICENSE file.
33
0 // Copyright 2014-2017 Ulrich Kunitz. All rights reserved.
0 // Copyright 2014-2021 Ulrich Kunitz. All rights reserved.
11 // Use of this source code is governed by a BSD-style
22 // license that can be found in the LICENSE file.
33
0 // Copyright 2014-2017 Ulrich Kunitz. All rights reserved.
0 // Copyright 2014-2021 Ulrich Kunitz. All rights reserved.
11 // Use of this source code is governed by a BSD-style
22 // license that can be found in the LICENSE file.
33
0 // Copyright 2014-2017 Ulrich Kunitz. All rights reserved.
0 // Copyright 2014-2021 Ulrich Kunitz. All rights reserved.
11 // Use of this source code is governed by a BSD-style
22 // license that can be found in the LICENSE file.
33
0 // Copyright 2014-2017 Ulrich Kunitz. All rights reserved.
0 // Copyright 2014-2021 Ulrich Kunitz. All rights reserved.
11 // Use of this source code is governed by a BSD-style
22 // license that can be found in the LICENSE file.
33
0 // Copyright 2014-2017 Ulrich Kunitz. All rights reserved.
0 // Copyright 2014-2021 Ulrich Kunitz. All rights reserved.
11 // Use of this source code is governed by a BSD-style
22 // license that can be found in the LICENSE file.
33
0 // Copyright 2014-2021 Ulrich Kunitz. All rights reserved.
1 // Use of this source code is governed by a BSD-style
2 // license that can be found in the LICENSE file.
3
4 package xz
5
6 import "hash"
7
8 type noneHash struct{}
9
10 func (h noneHash) Write(p []byte) (n int, err error) { return len(p), nil }
11
12 func (h noneHash) Sum(b []byte) []byte { return b }
13
14 func (h noneHash) Reset() {}
15
16 func (h noneHash) Size() int { return 0 }
17
18 func (h noneHash) BlockSize() int { return 0 }
19
20 func newNoneHash() hash.Hash {
21 return &noneHash{}
22 }
0 // Copyright 2014-2021 Ulrich Kunitz. All rights reserved.
1 // Use of this source code is governed by a BSD-style
2 // license that can be found in the LICENSE file.
3
4 package xz
5
6 import (
7 "bytes"
8 "testing"
9 )
10
11 func TestNoneHash(t *testing.T) {
12 h := newNoneHash()
13
14 p := []byte("foo")
15 q := h.Sum(p)
16
17 if !bytes.Equal(q, p) {
18 t.Fatalf("h.Sum: got %q; want %q", q, p)
19 }
20
21 }
0 // Copyright 2014-2017 Ulrich Kunitz. All rights reserved.
0 // Copyright 2014-2021 Ulrich Kunitz. All rights reserved.
11 // Use of this source code is governed by a BSD-style
22 // license that can be found in the LICENSE file.
33
2323 type ReaderConfig struct {
2424 DictCap int
2525 SingleStream bool
26 }
27
28 // fill replaces all zero values with their default values.
29 func (c *ReaderConfig) fill() {
30 if c.DictCap == 0 {
31 c.DictCap = 8 * 1024 * 1024
32 }
3326 }
3427
3528 // Verify checks the reader parameters for Validity. Zero values will be
164157 return r, nil
165158 }
166159
167 // errIndex indicates an error with the xz file index.
168 var errIndex = errors.New("xz: error in xz file index")
169
170160 // readTail reads the index body and the xz footer.
171161 func (r *streamReader) readTail() error {
172 index, n, err := readIndexBody(r.xz)
162 index, n, err := readIndexBody(r.xz, len(r.index))
173163 if err != nil {
174164 if err == io.EOF {
175165 err = io.ErrUnexpectedEOF
176166 }
177167 return err
178168 }
179 if len(index) != len(r.index) {
180 return fmt.Errorf("xz: index length is %d; want %d",
181 len(index), len(r.index))
182 }
169
183170 for i, rec := range r.index {
184171 if rec != index[i] {
185172 return fmt.Errorf("xz: record %d is %v; want %v",
264251 n int64
265252 hash hash.Hash
266253 r io.Reader
267 err error
268254 }
269255
270256 // newBlockReader creates a new block reader.
282268 if err != nil {
283269 return nil, err
284270 }
285 br.r = io.TeeReader(fr, br.hash)
271 if br.hash.Size() != 0 {
272 br.r = io.TeeReader(fr, br.hash)
273 } else {
274 br.r = fr
275 }
286276
287277 return br, nil
288278 }
310300 return record{br.unpaddedSize(), br.uncompressedSize()}
311301 }
312302
313 // errBlockSize indicates that the size of the block in the block header
314 // is wrong.
315 var errBlockSize = errors.New("xz: wrong uncompressed size for block")
316
317303 // Read reads data from the block.
318304 func (br *blockReader) Read(p []byte) (n int, err error) {
319305 n, err = br.r.Read(p)
0 // Copyright 2014-2017 Ulrich Kunitz. All rights reserved.
0 // Copyright 2014-2021 Ulrich Kunitz. All rights reserved.
11 // Use of this source code is governed by a BSD-style
22 // license that can be found in the LICENSE file.
33
5454 }
5555 }
5656
57 func TestReaaderMultipleStreams(t *testing.T) {
57 func TestReaderMultipleStreams(t *testing.T) {
5858 data, err := ioutil.ReadFile("fox.xz")
5959 if err != nil {
6060 t.Fatalf("ReadFile error %s", err)
7878 t.Fatalf("io.Copy error %s", err)
7979 }
8080 }
81
82 func TestCheckNone(t *testing.T) {
83 const file = "fox-check-none.xz"
84 xz, err := os.Open(file)
85 if err != nil {
86 t.Fatalf("os.Open(%q) error %s", file, err)
87 }
88 r, err := NewReader(xz)
89 if err != nil {
90 t.Fatalf("NewReader error %s", err)
91 }
92 var buf bytes.Buffer
93 if _, err = io.Copy(&buf, r); err != nil {
94 t.Fatalf("io.Copy error %s", err)
95 }
96 }
0 // Copyright 2014-2017 Ulrich Kunitz. All rights reserved.
0 // Copyright 2014-2021 Ulrich Kunitz. All rights reserved.
11 // Use of this source code is governed by a BSD-style
22 // license that can be found in the LICENSE file.
33
55
66 import (
77 "errors"
8 "fmt"
89 "hash"
910 "io"
1011
1718 DictCap int
1819 BufSize int
1920 BlockSize int64
20 // checksum method: CRC32, CRC64 or SHA256
21 // checksum method: CRC32, CRC64 or SHA256 (default: CRC64)
2122 CheckSum byte
23 // Forces NoChecksum (default: false)
24 NoCheckSum bool
2225 // match algorithm
2326 Matcher lzma.MatchAlgorithm
2427 }
3942 }
4043 if c.CheckSum == 0 {
4144 c.CheckSum = CRC64
45 }
46 if c.NoCheckSum {
47 c.CheckSum = None
4248 }
4349 }
4450
184190 return nil, err
185191 }
186192 data, err := w.h.MarshalBinary()
193 if err != nil {
194 return nil, fmt.Errorf("w.h.MarshalBinary(): error %w", err)
195 }
187196 if _, err = xz.Write(data); err != nil {
188197 return nil, err
189198 }
283292 if err != nil {
284293 return nil, err
285294 }
286 bw.mw = io.MultiWriter(bw.w, bw.hash)
295 if bw.hash.Size() != 0 {
296 bw.mw = io.MultiWriter(bw.w, bw.hash)
297 } else {
298 bw.mw = bw.w
299 }
287300 return bw, nil
288301 }
289302
0 // Copyright 2014-2017 Ulrich Kunitz. All rights reserved.
0 // Copyright 2014-2021 Ulrich Kunitz. All rights reserved.
11 // Use of this source code is governed by a BSD-style
22 // license that can be found in the LICENSE file.
33
135135 t.Fatal("decompressed data differs from original")
136136 }
137137 }
138
139 func TestWriterNoneCheck(t *testing.T) {
140 const txtlen = 1023
141 var buf bytes.Buffer
142 io.CopyN(&buf, randtxt.NewReader(rand.NewSource(41)), txtlen)
143 txt := buf.String()
144
145 buf.Reset()
146 w, err := WriterConfig{NoCheckSum: true}.NewWriter(&buf)
147 if err != nil {
148 t.Fatalf("NewWriter error %s", err)
149 }
150 n, err := io.WriteString(w, txt)
151 if err != nil {
152 t.Fatalf("WriteString error %s", err)
153 }
154 if n != len(txt) {
155 t.Fatalf("WriteString wrote %d bytes; want %d", n, len(txt))
156 }
157 if err = w.Close(); err != nil {
158 t.Fatalf("Close error %s", err)
159 }
160 t.Logf("buf.Len() %d", buf.Len())
161 r, err := NewReader(&buf)
162 if err != nil {
163 t.Fatalf("NewReader error %s", err)
164 }
165 var out bytes.Buffer
166 k, err := io.Copy(&out, r)
167 if err != nil {
168 t.Fatalf("Decompressing copy error %s after %d bytes", err, n)
169 }
170 if k != txtlen {
171 t.Fatalf("Decompression data length %d; want %d", k, txtlen)
172 }
173 if txt != out.String() {
174 t.Fatal("decompressed data differs from original")
175 }
176 }
0 // Copyright 2014-2021 Ulrich Kunitz. All rights reserved.
1 // Use of this source code is governed by a BSD-style
2 // license that can be found in the LICENSE file.
3
4 package xz_test
5
6 import (
7 "bytes"
8 "io/ioutil"
9 "testing"
10
11 "github.com/ulikunitz/xz"
12 )
13
14 func TestPanic(t *testing.T) {
15 data := []byte([]uint8{253, 55, 122, 88, 90, 0, 0, 0, 255, 18, 217, 65, 0, 189, 191, 239, 189, 191, 239, 48})
16 t.Logf("%q", string(data))
17 t.Logf("0x%02x", data)
18 r, err := xz.NewReader(bytes.NewReader(data))
19 if err != nil {
20 t.Logf("xz.NewReader error %s", err)
21 return
22 }
23 _, err = ioutil.ReadAll(r)
24 if err != nil {
25 t.Logf("ioutil.ReadAll(r) error %s", err)
26 return
27 }
28 }