diff options
Diffstat (limited to 'src/pkg/compress')
-rw-r--r-- | src/pkg/compress/flate/deflate.go | 139 | ||||
-rw-r--r-- | src/pkg/compress/flate/deflate_test.go | 337 | ||||
-rw-r--r-- | src/pkg/compress/flate/flate_test.go | 12 | ||||
-rw-r--r-- | src/pkg/compress/flate/huffman_bit_writer.go | 112 | ||||
-rw-r--r-- | src/pkg/compress/flate/inflate.go | 22 | ||||
-rw-r--r-- | src/pkg/compress/flate/reverse_bits.go | 2 | ||||
-rw-r--r-- | src/pkg/compress/flate/token.go | 16 | ||||
-rw-r--r-- | src/pkg/compress/gzip/gunzip.go | 2 | ||||
-rw-r--r-- | src/pkg/compress/gzip/gunzip_test.go | 59 | ||||
-rw-r--r-- | src/pkg/compress/zlib/reader.go | 4 |
10 files changed, 350 insertions, 355 deletions
diff --git a/src/pkg/compress/flate/deflate.go b/src/pkg/compress/flate/deflate.go index 548bff420..79b8ec3ba 100644 --- a/src/pkg/compress/flate/deflate.go +++ b/src/pkg/compress/flate/deflate.go @@ -12,67 +12,66 @@ import ( ) const ( - NoCompression = 0; - BestSpeed = 1; - fastCompression = 3; - BestCompression = 9; - DefaultCompression = -1; - - logMaxOffsetSize = 15; // Standard DEFLATE - wideLogMaxOffsetSize = 22; // Wide DEFLATE - minMatchLength = 3; // The smallest match that the deflater looks for - maxMatchLength = 258; // The longest match for the deflater - minOffsetSize = 1; // The shortest offset that makes any sence + NoCompression = 0; + BestSpeed = 1; + fastCompression = 3; + BestCompression = 9; + DefaultCompression = -1; + logMaxOffsetSize = 15; // Standard DEFLATE + wideLogMaxOffsetSize = 22; // Wide DEFLATE + minMatchLength = 3; // The smallest match that the deflater looks for + maxMatchLength = 258; // The longest match for the deflater + minOffsetSize = 1; // The shortest offset that makes any sence // The maximum number of tokens we put into a single flat block, just too // stop things from getting too large. - maxFlateBlockTokens = 1 << 14; - maxStoreBlockSize = 65535; - hashBits = 15; - hashSize = 1 << hashBits; - hashMask = (1 << hashBits) - 1; - hashShift = (hashBits + minMatchLength - 1) / minMatchLength; + maxFlateBlockTokens = 1<<14; + maxStoreBlockSize = 65535; + hashBits = 15; + hashSize = 1<<hashBits; + hashMask = (1<<hashBits)-1; + hashShift = (hashBits + minMatchLength - 1) / minMatchLength; ) type syncPipeReader struct { *io.PipeReader; - closeChan chan bool; + closeChan chan bool; } func (sr *syncPipeReader) CloseWithError(err os.Error) os.Error { retErr := sr.PipeReader.CloseWithError(err); - sr.closeChan <- true; // finish writer close + sr.closeChan <- true; // finish writer close return retErr; } type syncPipeWriter struct { *io.PipeWriter; - closeChan chan bool; + closeChan chan bool; } type compressionLevel struct { good, lazy, nice, chain, fastSkipHashing int; } -var levels = [] compressionLevel { - compressionLevel {}, // 0 +var levels = []compressionLevel{ + compressionLevel{}, // 0 // For levels 1-3 we don't bother trying with lazy matches - compressionLevel { 3, 0, 8, 4, 4, }, - compressionLevel { 3, 0, 16, 8, 5, }, - compressionLevel { 3, 0, 32, 32, 6 }, + compressionLevel{3, 0, 8, 4, 4}, + compressionLevel{3, 0, 16, 8, 5}, + compressionLevel{3, 0, 32, 32, 6}, // Levels 4-9 use increasingly more lazy matching // and increasingly stringent conditions for "good enough". - compressionLevel { 4, 4, 16, 16, math.MaxInt32 }, - compressionLevel { 8, 16, 32, 32, math.MaxInt32 }, - compressionLevel { 8, 16, 128, 128, math.MaxInt32 }, - compressionLevel { 8, 32, 128, 256, math.MaxInt32 }, - compressionLevel { 32, 128, 258, 1024, math.MaxInt32 }, - compressionLevel { 32, 258, 258, 4096, math.MaxInt32 }, + compressionLevel{4, 4, 16, 16, math.MaxInt32}, + compressionLevel{8, 16, 32, 32, math.MaxInt32}, + compressionLevel{8, 16, 128, 128, math.MaxInt32}, + compressionLevel{8, 32, 128, 256, math.MaxInt32}, + compressionLevel{32, 128, 258, 1024, math.MaxInt32}, + compressionLevel{32, 258, 258, 4096, math.MaxInt32}, } func (sw *syncPipeWriter) Close() os.Error { err := sw.PipeWriter.Close(); - <-sw.closeChan; // wait for reader close + <-sw.closeChan; // wait for reader close return err; } @@ -84,40 +83,40 @@ func syncPipe() (*syncPipeReader, *syncPipeWriter) { } type deflater struct { - level int; - logWindowSize uint; - w *huffmanBitWriter; - r io.Reader; + level int; + logWindowSize uint; + w *huffmanBitWriter; + r io.Reader; // (1 << logWindowSize) - 1. - windowMask int; + windowMask int; // hashHead[hashValue] contains the largest inputIndex with the specified hash value - hashHead []int; + hashHead []int; // If hashHead[hashValue] is within the current window, then // hashPrev[hashHead[hashValue] & windowMask] contains the previous index // with the same hash value. - hashPrev []int; + hashPrev []int; // If we find a match of length >= niceMatch, then we don't bother searching // any further. - niceMatch int; + niceMatch int; // If we find a match of length >= goodMatch, we only do a half-hearted // effort at doing lazy matching starting at the next character - goodMatch int; + goodMatch int; // The maximum number of chains we look at when finding a match - maxChainLength int; + maxChainLength int; // The sliding window we use for matching - window []byte; + window []byte; // The index just past the last valid character - windowEnd int; + windowEnd int; // index in "window" at which current block starts - blockStart int; + blockStart int; } func (d *deflater) flush() os.Error { @@ -127,9 +126,9 @@ func (d *deflater) flush() os.Error { func (d *deflater) fillWindow(index int) (int, os.Error) { wSize := d.windowMask + 1; - if index >= wSize + wSize - (minMatchLength + maxMatchLength) { + if index >= wSize+wSize-(minMatchLength + maxMatchLength) { // shift the window by wSize - bytes.Copy(d.window, d.window[wSize:2*wSize]); + bytes.Copy(d.window, d.window[wSize : 2*wSize]); index -= wSize; d.windowEnd -= wSize; if d.blockStart >= wSize { @@ -138,10 +137,10 @@ func (d *deflater) fillWindow(index int) (int, os.Error) { d.blockStart = math.MaxInt32; } for i, h := range d.hashHead { - d.hashHead[i] = max(h - wSize, -1); + d.hashHead[i] = max(h-wSize, -1); } for i, h := range d.hashPrev { - d.hashPrev[i] = max(h - wSize, -1); + d.hashPrev[i] = max(h-wSize, -1); } } var count int; @@ -158,7 +157,7 @@ func (d *deflater) writeBlock(tokens []token, index int, eof bool) os.Error { if index > 0 || eof { var window []byte; if d.blockStart <= index { - window = d.window[d.blockStart:index]; + window = d.window[d.blockStart : index]; } d.blockStart = index; d.w.writeBlock(tokens, eof, window); @@ -170,10 +169,10 @@ func (d *deflater) writeBlock(tokens []token, index int, eof bool) os.Error { // Try to find a match starting at index whose length is greater than prevSize. // We only look at chainCount possibilities before giving up. func (d *deflater) findMatch(pos int, prevHead int, prevLength int, lookahead int) (length, offset int, ok bool) { - win := d.window[0:pos+min(maxMatchLength, lookahead)]; + win := d.window[0 : pos + min(maxMatchLength, lookahead)]; // We quit when we get a match that's at least nice long - nice := min(d.niceMatch, len(win) - pos); + nice := min(d.niceMatch, len(win)-pos); // If we've got a match that's good enough, only look in 1/4 the chain. tries := d.maxChainLength; @@ -183,21 +182,21 @@ func (d *deflater) findMatch(pos int, prevHead int, prevLength int, lookahead in } w0 := win[pos]; - w1 := win[pos + 1]; - wEnd := win[pos + length]; - minIndex := pos - (d.windowMask + 1); + w1 := win[pos+1]; + wEnd := win[pos+length]; + minIndex := pos-(d.windowMask + 1); for i := prevHead; tries > 0; tries-- { if w0 == win[i] && w1 == win[i+1] && wEnd == win[i+length] { // The hash function ensures that if win[i] and win[i+1] match, win[i+2] matches n := 3; - for pos + n < len(win) && win[i+n] == win[pos+n] { + for pos+n < len(win) && win[i+n] == win[pos+n] { n++; } if n > length && (n > 3 || pos-i <= 4096) { length = n; - offset = pos - i; + offset = pos-i; ok = true; if n >= nice { // The match is good enough that we don't try to find a better one. @@ -246,7 +245,7 @@ func (d *deflater) storedDeflate() os.Error { func (d *deflater) doDeflate() (err os.Error) { // init - d.windowMask = 1<<d.logWindowSize - 1; + d.windowMask = 1 << d.logWindowSize - 1; d.hashHead = make([]int, hashSize); d.hashPrev = make([]int, 1 << d.logWindowSize); d.window = make([]byte, 2 << d.logWindowSize); @@ -266,7 +265,7 @@ func (d *deflater) doDeflate() (err os.Error) { if index, err = d.fillWindow(index); err != nil { return; } - maxOffset := d.windowMask + 1; // (1 << logWindowSize); + maxOffset := d.windowMask + 1; // (1 << logWindowSize); // only need to change when you refill the window windowEnd := d.windowEnd; maxInsertIndex := windowEnd - (minMatchLength - 1); @@ -274,7 +273,7 @@ func (d *deflater) doDeflate() (err os.Error) { hash := int(0); if index < maxInsertIndex { - hash = int(d.window[index])<<hashShift + int(d.window[index+1]); + hash = int(d.window[index]) << hashShift + int(d.window[index+1]); } chainHead := -1; for { @@ -298,7 +297,7 @@ func (d *deflater) doDeflate() (err os.Error) { } if index < maxInsertIndex { // Update the hash - hash = (hash<<hashShift + int(d.window[index+2])) & hashMask; + hash = (hash << hashShift + int(d.window[index+2]))&hashMask; chainHead = d.hashHead[hash]; d.hashPrev[index & d.windowMask] = chainHead; d.hashHead[hash] = index; @@ -311,8 +310,8 @@ func (d *deflater) doDeflate() (err os.Error) { if chainHead >= minIndex && (isFastDeflate && lookahead > minMatchLength - 1 || - !isFastDeflate && lookahead > prevLength && prevLength < lazyMatch) { - if newLength, newOffset, ok := d.findMatch(index, chainHead, minMatchLength -1 , lookahead); ok { + !isFastDeflate && lookahead > prevLength && prevLength < lazyMatch) { + if newLength, newOffset, ok := d.findMatch(index, chainHead, minMatchLength - 1, lookahead); ok { length = newLength; offset = newOffset; } @@ -334,13 +333,13 @@ func (d *deflater) doDeflate() (err os.Error) { if length <= l.fastSkipHashing { var newIndex int; if isFastDeflate { - newIndex = index + length; + newIndex = index+length; } else { newIndex = prevLength - 1; } for index++; index < newIndex; index++ { if index < maxInsertIndex { - hash = (hash<<hashShift + int(d.window[index+2])) & hashMask; + hash = (hash << hashShift + int(d.window[index+2]))&hashMask; // Get previous value with the same hash. // Our chain should point to the previous value. d.hashPrev[index & d.windowMask] = d.hashHead[hash]; @@ -356,7 +355,7 @@ func (d *deflater) doDeflate() (err os.Error) { // For matches this long, we don't bother inserting each individual // item into the table. index += length; - hash = (int(d.window[index])<<hashShift + int(d.window[index+1])); + hash = (int(d.window[index]) << hashShift + int(d.window[index+1])); } if ti == maxFlateBlockTokens { // The block includes the current character @@ -367,11 +366,11 @@ func (d *deflater) doDeflate() (err os.Error) { } } else { if isFastDeflate || byteAvailable { - i := index - 1; + i := index-1; if isFastDeflate { i = index; } - tokens[ti] = literalToken(uint32(d.window[i]) & 0xFF); + tokens[ti] = literalToken(uint32(d.window[i])&0xFF); ti++; if ti == maxFlateBlockTokens { if err = d.writeBlock(tokens, i+1, false); err != nil { @@ -389,7 +388,7 @@ func (d *deflater) doDeflate() (err os.Error) { } if byteAvailable { // There is still one pending token that needs to be flushed - tokens[ti] = literalToken(uint32(d.window[index - 1]) & 0xFF); + tokens[ti] = literalToken(uint32(d.window[index-1])&0xFF); ti++; } @@ -416,7 +415,7 @@ func (d *deflater) deflater(r io.Reader, w io.Writer, level int, logWindowSize u case 1 <= level && level <= 9: err = d.doDeflate(); default: - return WrongValueError { "level", 0, 9, int32(level) }; + return WrongValueError{"level", 0, 9, int32(level)}; } if err != nil { diff --git a/src/pkg/compress/flate/deflate_test.go b/src/pkg/compress/flate/deflate_test.go index 24042a278..3f6c60bf5 100644 --- a/src/pkg/compress/flate/deflate_test.go +++ b/src/pkg/compress/flate/deflate_test.go @@ -13,65 +13,66 @@ import ( ) type deflateTest struct { - in []byte; - level int; - out []byte; + in []byte; + level int; + out []byte; } type deflateInflateTest struct { - in [] byte; + in []byte; } type reverseBitsTest struct { - in uint16; - bitCount uint8; - out uint16; + in uint16; + bitCount uint8; + out uint16; } -var deflateTests = []*deflateTest { - &deflateTest { []byte{ }, 0, []byte{ 1, 0, 0, 255, 255 } }, - &deflateTest { []byte{ 0x11 }, -1, []byte{ 18, 4, 4, 0, 0, 255, 255 } }, - &deflateTest { []byte{ 0x11 }, DefaultCompression, []byte{ 18, 4, 4, 0, 0, 255, 255 } }, - &deflateTest { []byte{ 0x11 }, 4, []byte{ 18, 4, 4, 0, 0, 255, 255 } }, +var deflateTests = []*deflateTest{ + &deflateTest{[]byte{}, 0, []byte{1, 0, 0, 255, 255}}, + &deflateTest{[]byte{0x11}, -1, []byte{18, 4, 4, 0, 0, 255, 255}}, + &deflateTest{[]byte{0x11}, DefaultCompression, []byte{18, 4, 4, 0, 0, 255, 255}}, + &deflateTest{[]byte{0x11}, 4, []byte{18, 4, 4, 0, 0, 255, 255}}, - &deflateTest { []byte{ 0x11 }, 0, []byte { 0, 1, 0, 254, 255, 17, 1, 0, 0, 255, 255 } }, - &deflateTest { []byte{ 0x11, 0x12 }, 0, []byte{ 0, 2, 0, 253, 255, 17, 18, 1, 0, 0, 255, 255 } }, - &deflateTest { []byte{ 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11 }, 0, - []byte{ 0, 8, 0, 247, 255, 17, 17, 17, 17, 17, 17, 17, 17, 1, 0, 0, 255, 255 } }, - &deflateTest { []byte{}, 1, []byte{ 1, 0, 0, 255, 255 } }, - &deflateTest { []byte{ 0x11 }, 1, []byte{ 18, 4, 4, 0, 0, 255, 255 } }, - &deflateTest { []byte{ 0x11, 0x12 }, 1, []byte{ 18, 20, 2, 4, 0, 0, 255, 255 } }, - &deflateTest { []byte{ 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11 }, 1, []byte{ 18, 132, 2, 64, 0, 0, 0, 255, 255 } }, - &deflateTest { []byte{}, 9, []byte{ 1, 0, 0, 255, 255 } }, - &deflateTest { []byte{ 0x11 }, 9, []byte{ 18, 4, 4, 0, 0, 255, 255 } }, - &deflateTest { []byte{ 0x11, 0x12 }, 9, []byte{ 18, 20, 2, 4, 0, 0, 255, 255 } }, - &deflateTest { []byte{ 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11 }, 9, []byte{ 18, 132, 2, 64, 0, 0, 0, 255, 255 } }, + &deflateTest{[]byte{0x11}, 0, []byte{0, 1, 0, 254, 255, 17, 1, 0, 0, 255, 255}}, + &deflateTest{[]byte{0x11, 0x12}, 0, []byte{0, 2, 0, 253, 255, 17, 18, 1, 0, 0, 255, 255}}, + &deflateTest{[]byte{0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11}, 0, + []byte{0, 8, 0, 247, 255, 17, 17, 17, 17, 17, 17, 17, 17, 1, 0, 0, 255, 255}, + }, + &deflateTest{[]byte{}, 1, []byte{1, 0, 0, 255, 255}}, + &deflateTest{[]byte{0x11}, 1, []byte{18, 4, 4, 0, 0, 255, 255}}, + &deflateTest{[]byte{0x11, 0x12}, 1, []byte{18, 20, 2, 4, 0, 0, 255, 255}}, + &deflateTest{[]byte{0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11}, 1, []byte{18, 132, 2, 64, 0, 0, 0, 255, 255}}, + &deflateTest{[]byte{}, 9, []byte{1, 0, 0, 255, 255}}, + &deflateTest{[]byte{0x11}, 9, []byte{18, 4, 4, 0, 0, 255, 255}}, + &deflateTest{[]byte{0x11, 0x12}, 9, []byte{18, 20, 2, 4, 0, 0, 255, 255}}, + &deflateTest{[]byte{0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11}, 9, []byte{18, 132, 2, 64, 0, 0, 0, 255, 255}}, } -var deflateInflateTests = []*deflateInflateTest { - &deflateInflateTest { []byte{ } }, - &deflateInflateTest { []byte{ 0x11 } }, - &deflateInflateTest { []byte{ 0x11, 0x12 } }, - &deflateInflateTest { []byte{ 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11 } }, - &deflateInflateTest { []byte{ 0x11, 0x10, 0x13, 0x41, 0x21, 0x21, 0x41, 0x13, 0x87, 0x78, 0x13 } }, - &deflateInflateTest { getLargeDataChunk() }, +var deflateInflateTests = []*deflateInflateTest{ + &deflateInflateTest{[]byte{}}, + &deflateInflateTest{[]byte{0x11}}, + &deflateInflateTest{[]byte{0x11, 0x12}}, + &deflateInflateTest{[]byte{0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11}}, + &deflateInflateTest{[]byte{0x11, 0x10, 0x13, 0x41, 0x21, 0x21, 0x41, 0x13, 0x87, 0x78, 0x13}}, + &deflateInflateTest{getLargeDataChunk()}, } -var reverseBitsTests = []*reverseBitsTest { - &reverseBitsTest { 1, 1, 1 }, - &reverseBitsTest { 1, 2, 2 }, - &reverseBitsTest { 1, 3, 4 }, - &reverseBitsTest { 1, 4, 8 }, - &reverseBitsTest { 1, 5, 16 }, - &reverseBitsTest { 17, 5, 17 }, - &reverseBitsTest { 257, 9, 257 }, - &reverseBitsTest { 29, 5, 23 }, +var reverseBitsTests = []*reverseBitsTest{ + &reverseBitsTest{1, 1, 1}, + &reverseBitsTest{1, 2, 2}, + &reverseBitsTest{1, 3, 4}, + &reverseBitsTest{1, 4, 8}, + &reverseBitsTest{1, 5, 16}, + &reverseBitsTest{17, 5, 17}, + &reverseBitsTest{257, 9, 257}, + &reverseBitsTest{29, 5, 23}, } func getLargeDataChunk() []byte { result := make([]byte, 100000); for i := range result { - result[i] = byte(int64(i) * int64(i) & 0xFF); + result[i] = byte(int64(i)*int64(i)&0xFF); } return result; } @@ -107,7 +108,7 @@ func testToFromWithLevel(t *testing.T, level int, input []byte, name string) os. return nil; } -func testToFrom(t *testing.T, input[] byte, name string) { +func testToFrom(t *testing.T, input []byte, name string) { for i := 0; i < 10; i++ { testToFromWithLevel(t, i, input, name); } @@ -134,130 +135,130 @@ func TestDeflateInflateString(t *testing.T) { } func getEdata() string { - return "2.718281828459045235360287471352662497757247093699959574966967627724076630353547"+ - "59457138217852516642742746639193200305992181741359662904357290033429526059563073"+ - "81323286279434907632338298807531952510190115738341879307021540891499348841675092"+ - "44761460668082264800168477411853742345442437107539077744992069551702761838606261"+ - "33138458300075204493382656029760673711320070932870912744374704723069697720931014"+ - "16928368190255151086574637721112523897844250569536967707854499699679468644549059"+ - "87931636889230098793127736178215424999229576351482208269895193668033182528869398"+ - "49646510582093923982948879332036250944311730123819706841614039701983767932068328"+ - "23764648042953118023287825098194558153017567173613320698112509961818815930416903"+ - "51598888519345807273866738589422879228499892086805825749279610484198444363463244"+ - "96848756023362482704197862320900216099023530436994184914631409343173814364054625"+ - "31520961836908887070167683964243781405927145635490613031072085103837505101157477"+ - "04171898610687396965521267154688957035035402123407849819334321068170121005627880"+ - "23519303322474501585390473041995777709350366041699732972508868769664035557071622"+ - "68447162560798826517871341951246652010305921236677194325278675398558944896970964"+ - "09754591856956380236370162112047742722836489613422516445078182442352948636372141"+ - "74023889344124796357437026375529444833799801612549227850925778256209262264832627"+ - "79333865664816277251640191059004916449982893150566047258027786318641551956532442"+ - "58698294695930801915298721172556347546396447910145904090586298496791287406870504"+ - "89585867174798546677575732056812884592054133405392200011378630094556068816674001"+ - "69842055804033637953764520304024322566135278369511778838638744396625322498506549"+ - "95886234281899707733276171783928034946501434558897071942586398772754710962953741"+ - "52111513683506275260232648472870392076431005958411661205452970302364725492966693"+ - "81151373227536450988890313602057248176585118063036442812314965507047510254465011"+ - "72721155519486685080036853228183152196003735625279449515828418829478761085263981"+ - "39559900673764829224437528718462457803619298197139914756448826260390338144182326"+ - "25150974827987779964373089970388867782271383605772978824125611907176639465070633"+ - "04527954661855096666185664709711344474016070462621568071748187784437143698821855"+ - "96709591025968620023537185887485696522000503117343920732113908032936344797273559"+ - "55277349071783793421637012050054513263835440001863239914907054797780566978533580"+ - "48966906295119432473099587655236812859041383241160722602998330535370876138939639"+ - "17795745401613722361878936526053815584158718692553860616477983402543512843961294"+ - "60352913325942794904337299085731580290958631382683291477116396337092400316894586"+ - "36060645845925126994655724839186564209752685082307544254599376917041977780085362"+ - "73094171016343490769642372229435236612557250881477922315197477806056967253801718"+ - "07763603462459278778465850656050780844211529697521890874019660906651803516501792"+ - "50461950136658543663271254963990854914420001457476081930221206602433009641270489"+ - "43903971771951806990869986066365832322787093765022601492910115171776359446020232"+ - "49300280401867723910288097866605651183260043688508817157238669842242201024950551"+ - "88169480322100251542649463981287367765892768816359831247788652014117411091360116"+ - "49950766290779436460058519419985601626479076153210387275571269925182756879893027"+ - "61761146162549356495903798045838182323368612016243736569846703785853305275833337"+ - "93990752166069238053369887956513728559388349989470741618155012539706464817194670"+ - "83481972144888987906765037959036696724949925452790337296361626589760394985767413"+ - "97359441023744329709355477982629614591442936451428617158587339746791897571211956"+ - "18738578364475844842355558105002561149239151889309946342841393608038309166281881"+ - "15037152849670597416256282360921680751501777253874025642534708790891372917228286"+ - "11515915683725241630772254406337875931059826760944203261924285317018781772960235"+ - "41306067213604600038966109364709514141718577701418060644363681546444005331608778"+ - "31431744408119494229755993140118886833148328027065538330046932901157441475631399"+ - "97221703804617092894579096271662260740718749975359212756084414737823303270330168"+ - "23719364800217328573493594756433412994302485023573221459784328264142168487872167"+ - "33670106150942434569844018733128101079451272237378861260581656680537143961278887"+ - "32527373890392890506865324138062796025930387727697783792868409325365880733988457"+ - "21874602100531148335132385004782716937621800490479559795929059165547050577751430"+ - "81751126989851884087185640260353055837378324229241856256442550226721559802740126"+ - "17971928047139600689163828665277009752767069777036439260224372841840883251848770"+ - "47263844037953016690546593746161932384036389313136432713768884102681121989127522"+ - "30562567562547017250863497653672886059667527408686274079128565769963137897530346"+ - "60616669804218267724560530660773899624218340859882071864682623215080288286359746"+ - "83965435885668550377313129658797581050121491620765676995065971534476347032085321"+ - "56036748286083786568030730626576334697742956346437167093971930608769634953288468"+ - "33613038829431040800296873869117066666146800015121143442256023874474325250769387"+ - "07777519329994213727721125884360871583483562696166198057252661220679754062106208"+ - "06498829184543953015299820925030054982570433905535701686531205264956148572492573"+ - "86206917403695213533732531666345466588597286659451136441370331393672118569553952"+ - "10845840724432383558606310680696492485123263269951460359603729725319836842336390"+ - "46321367101161928217111502828016044880588023820319814930963695967358327420249882"+ - "45684941273860566491352526706046234450549227581151709314921879592718001940968866"+ - "98683703730220047531433818109270803001720593553052070070607223399946399057131158"+ - "70996357773590271962850611465148375262095653467132900259943976631145459026858989"+ - "79115837093419370441155121920117164880566945938131183843765620627846310490346293"+ - "95002945834116482411496975832601180073169943739350696629571241027323913874175492"+ - "30718624545432220395527352952402459038057445028922468862853365422138157221311632"+ - "88112052146489805180092024719391710555390113943316681515828843687606961102505171"+ - "00739276238555338627255353883096067164466237092264680967125406186950214317621166"+ - "81400975952814939072226011126811531083873176173232352636058381731510345957365382"+ - "23534992935822836851007810884634349983518404451704270189381994243410090575376257"+ - "76757111809008816418331920196262341628816652137471732547772778348877436651882875"+ - "21566857195063719365653903894493664217640031215278702223664636357555035655769488"+ - "86549500270853923617105502131147413744106134445544192101336172996285694899193369"+ - "18472947858072915608851039678195942983318648075608367955149663644896559294818785"+ - "17840387733262470519450504198477420141839477312028158868457072905440575106012852"+ - "58056594703046836344592652552137008068752009593453607316226118728173928074623094"+ - "68536782310609792159936001994623799343421068781349734695924646975250624695861690"+ - "91785739765951993929939955675427146549104568607020990126068187049841780791739240"+ - "71945996323060254707901774527513186809982284730860766536866855516467702911336827"+ - "56310722334672611370549079536583453863719623585631261838715677411873852772292259"+ - "47433737856955384562468010139057278710165129666367644518724656537304024436841408"+ - "14488732957847348490003019477888020460324660842875351848364959195082888323206522"+ - "12810419044804724794929134228495197002260131043006241071797150279343326340799596"+ - "05314460532304885289729176598760166678119379323724538572096075822771784833616135"+ - "82612896226118129455927462767137794487586753657544861407611931125958512655759734"+ - "57301533364263076798544338576171533346232527057200530398828949903425956623297578"+ - "24887350292591668258944568946559926584547626945287805165017206747854178879822768"+ - "06536650641910973434528878338621726156269582654478205672987756426325321594294418"+ - "03994321700009054265076309558846589517170914760743713689331946909098190450129030"+ - "70995662266203031826493657336984195557769637876249188528656866076005660256054457"+ - "11337286840205574416030837052312242587223438854123179481388550075689381124935386"+ - "31863528708379984569261998179452336408742959118074745341955142035172618420084550"+ - "91708456823682008977394558426792142734775608796442792027083121501564063413416171"+ - "66448069815483764491573900121217041547872591998943825364950514771379399147205219"+ - "52907939613762110723849429061635760459623125350606853765142311534966568371511660"+ - "42207963944666211632551577290709784731562782775987881364919512574833287937715714"+ - "59091064841642678309949723674420175862269402159407924480541255360431317992696739"+ - "15754241929660731239376354213923061787675395871143610408940996608947141834069836"+ - "29936753626215452472984642137528910798843813060955526227208375186298370667872244"+ - "30195793793786072107254277289071732854874374355781966511716618330881129120245204"+ - "04868220007234403502544820283425418788465360259150644527165770004452109773558589"+ - "76226554849416217149895323834216001140629507184904277892585527430352213968356790"+ - "18076406042138307308774460170842688272261177180842664333651780002171903449234264"+ - "26629226145600433738386833555534345300426481847398921562708609565062934040526494"+ - "32442614456659212912256488935696550091543064261342526684725949143142393988454324"+ - "86327461842846655985332312210466259890141712103446084271616619001257195870793217"+ - "56969854401339762209674945418540711844643394699016269835160784892451405894094639"+ - "52678073545797003070511636825194877011897640028276484141605872061841852971891540"+ - "19688253289309149665345753571427318482016384644832499037886069008072709327673127"+ - "58196656394114896171683298045513972950668760474091542042842999354102582911350224"+ - "16907694316685742425225090269390348148564513030699251995904363840284292674125734"+ - "22447765584177886171737265462085498294498946787350929581652632072258992368768457"+ - "01782303809656788311228930580914057261086588484587310165815116753332767488701482"+ - "91674197015125597825727074064318086014281490241467804723275976842696339357735429"+ - "30186739439716388611764209004068663398856841681003872389214483176070116684503887"+ - "21236436704331409115573328018297798873659091665961240202177855885487617616198937"+ - "07943800566633648843650891448055710397652146960276625835990519870423001794655367"+ - "9"; + return "2.718281828459045235360287471352662497757247093699959574966967627724076630353547" + + "59457138217852516642742746639193200305992181741359662904357290033429526059563073" + + "81323286279434907632338298807531952510190115738341879307021540891499348841675092" + + "44761460668082264800168477411853742345442437107539077744992069551702761838606261" + + "33138458300075204493382656029760673711320070932870912744374704723069697720931014" + + "16928368190255151086574637721112523897844250569536967707854499699679468644549059" + + "87931636889230098793127736178215424999229576351482208269895193668033182528869398" + + "49646510582093923982948879332036250944311730123819706841614039701983767932068328" + + "23764648042953118023287825098194558153017567173613320698112509961818815930416903" + + "51598888519345807273866738589422879228499892086805825749279610484198444363463244" + + "96848756023362482704197862320900216099023530436994184914631409343173814364054625" + + "31520961836908887070167683964243781405927145635490613031072085103837505101157477" + + "04171898610687396965521267154688957035035402123407849819334321068170121005627880" + + "23519303322474501585390473041995777709350366041699732972508868769664035557071622" + + "68447162560798826517871341951246652010305921236677194325278675398558944896970964" + + "09754591856956380236370162112047742722836489613422516445078182442352948636372141" + + "74023889344124796357437026375529444833799801612549227850925778256209262264832627" + + "79333865664816277251640191059004916449982893150566047258027786318641551956532442" + + "58698294695930801915298721172556347546396447910145904090586298496791287406870504" + + "89585867174798546677575732056812884592054133405392200011378630094556068816674001" + + "69842055804033637953764520304024322566135278369511778838638744396625322498506549" + + "95886234281899707733276171783928034946501434558897071942586398772754710962953741" + + "52111513683506275260232648472870392076431005958411661205452970302364725492966693" + + "81151373227536450988890313602057248176585118063036442812314965507047510254465011" + + "72721155519486685080036853228183152196003735625279449515828418829478761085263981" + + "39559900673764829224437528718462457803619298197139914756448826260390338144182326" + + "25150974827987779964373089970388867782271383605772978824125611907176639465070633" + + "04527954661855096666185664709711344474016070462621568071748187784437143698821855" + + "96709591025968620023537185887485696522000503117343920732113908032936344797273559" + + "55277349071783793421637012050054513263835440001863239914907054797780566978533580" + + "48966906295119432473099587655236812859041383241160722602998330535370876138939639" + + "17795745401613722361878936526053815584158718692553860616477983402543512843961294" + + "60352913325942794904337299085731580290958631382683291477116396337092400316894586" + + "36060645845925126994655724839186564209752685082307544254599376917041977780085362" + + "73094171016343490769642372229435236612557250881477922315197477806056967253801718" + + "07763603462459278778465850656050780844211529697521890874019660906651803516501792" + + "50461950136658543663271254963990854914420001457476081930221206602433009641270489" + + "43903971771951806990869986066365832322787093765022601492910115171776359446020232" + + "49300280401867723910288097866605651183260043688508817157238669842242201024950551" + + "88169480322100251542649463981287367765892768816359831247788652014117411091360116" + + "49950766290779436460058519419985601626479076153210387275571269925182756879893027" + + "61761146162549356495903798045838182323368612016243736569846703785853305275833337" + + "93990752166069238053369887956513728559388349989470741618155012539706464817194670" + + "83481972144888987906765037959036696724949925452790337296361626589760394985767413" + + "97359441023744329709355477982629614591442936451428617158587339746791897571211956" + + "18738578364475844842355558105002561149239151889309946342841393608038309166281881" + + "15037152849670597416256282360921680751501777253874025642534708790891372917228286" + + "11515915683725241630772254406337875931059826760944203261924285317018781772960235" + + "41306067213604600038966109364709514141718577701418060644363681546444005331608778" + + "31431744408119494229755993140118886833148328027065538330046932901157441475631399" + + "97221703804617092894579096271662260740718749975359212756084414737823303270330168" + + "23719364800217328573493594756433412994302485023573221459784328264142168487872167" + + "33670106150942434569844018733128101079451272237378861260581656680537143961278887" + + "32527373890392890506865324138062796025930387727697783792868409325365880733988457" + + "21874602100531148335132385004782716937621800490479559795929059165547050577751430" + + "81751126989851884087185640260353055837378324229241856256442550226721559802740126" + + "17971928047139600689163828665277009752767069777036439260224372841840883251848770" + + "47263844037953016690546593746161932384036389313136432713768884102681121989127522" + + "30562567562547017250863497653672886059667527408686274079128565769963137897530346" + + "60616669804218267724560530660773899624218340859882071864682623215080288286359746" + + "83965435885668550377313129658797581050121491620765676995065971534476347032085321" + + "56036748286083786568030730626576334697742956346437167093971930608769634953288468" + + "33613038829431040800296873869117066666146800015121143442256023874474325250769387" + + "07777519329994213727721125884360871583483562696166198057252661220679754062106208" + + "06498829184543953015299820925030054982570433905535701686531205264956148572492573" + + "86206917403695213533732531666345466588597286659451136441370331393672118569553952" + + "10845840724432383558606310680696492485123263269951460359603729725319836842336390" + + "46321367101161928217111502828016044880588023820319814930963695967358327420249882" + + "45684941273860566491352526706046234450549227581151709314921879592718001940968866" + + "98683703730220047531433818109270803001720593553052070070607223399946399057131158" + + "70996357773590271962850611465148375262095653467132900259943976631145459026858989" + + "79115837093419370441155121920117164880566945938131183843765620627846310490346293" + + "95002945834116482411496975832601180073169943739350696629571241027323913874175492" + + "30718624545432220395527352952402459038057445028922468862853365422138157221311632" + + "88112052146489805180092024719391710555390113943316681515828843687606961102505171" + + "00739276238555338627255353883096067164466237092264680967125406186950214317621166" + + "81400975952814939072226011126811531083873176173232352636058381731510345957365382" + + "23534992935822836851007810884634349983518404451704270189381994243410090575376257" + + "76757111809008816418331920196262341628816652137471732547772778348877436651882875" + + "21566857195063719365653903894493664217640031215278702223664636357555035655769488" + + "86549500270853923617105502131147413744106134445544192101336172996285694899193369" + + "18472947858072915608851039678195942983318648075608367955149663644896559294818785" + + "17840387733262470519450504198477420141839477312028158868457072905440575106012852" + + "58056594703046836344592652552137008068752009593453607316226118728173928074623094" + + "68536782310609792159936001994623799343421068781349734695924646975250624695861690" + + "91785739765951993929939955675427146549104568607020990126068187049841780791739240" + + "71945996323060254707901774527513186809982284730860766536866855516467702911336827" + + "56310722334672611370549079536583453863719623585631261838715677411873852772292259" + + "47433737856955384562468010139057278710165129666367644518724656537304024436841408" + + "14488732957847348490003019477888020460324660842875351848364959195082888323206522" + + "12810419044804724794929134228495197002260131043006241071797150279343326340799596" + + "05314460532304885289729176598760166678119379323724538572096075822771784833616135" + + "82612896226118129455927462767137794487586753657544861407611931125958512655759734" + + "57301533364263076798544338576171533346232527057200530398828949903425956623297578" + + "24887350292591668258944568946559926584547626945287805165017206747854178879822768" + + "06536650641910973434528878338621726156269582654478205672987756426325321594294418" + + "03994321700009054265076309558846589517170914760743713689331946909098190450129030" + + "70995662266203031826493657336984195557769637876249188528656866076005660256054457" + + "11337286840205574416030837052312242587223438854123179481388550075689381124935386" + + "31863528708379984569261998179452336408742959118074745341955142035172618420084550" + + "91708456823682008977394558426792142734775608796442792027083121501564063413416171" + + "66448069815483764491573900121217041547872591998943825364950514771379399147205219" + + "52907939613762110723849429061635760459623125350606853765142311534966568371511660" + + "42207963944666211632551577290709784731562782775987881364919512574833287937715714" + + "59091064841642678309949723674420175862269402159407924480541255360431317992696739" + + "15754241929660731239376354213923061787675395871143610408940996608947141834069836" + + "29936753626215452472984642137528910798843813060955526227208375186298370667872244" + + "30195793793786072107254277289071732854874374355781966511716618330881129120245204" + + "04868220007234403502544820283425418788465360259150644527165770004452109773558589" + + "76226554849416217149895323834216001140629507184904277892585527430352213968356790" + + "18076406042138307308774460170842688272261177180842664333651780002171903449234264" + + "26629226145600433738386833555534345300426481847398921562708609565062934040526494" + + "32442614456659212912256488935696550091543064261342526684725949143142393988454324" + + "86327461842846655985332312210466259890141712103446084271616619001257195870793217" + + "56969854401339762209674945418540711844643394699016269835160784892451405894094639" + + "52678073545797003070511636825194877011897640028276484141605872061841852971891540" + + "19688253289309149665345753571427318482016384644832499037886069008072709327673127" + + "58196656394114896171683298045513972950668760474091542042842999354102582911350224" + + "16907694316685742425225090269390348148564513030699251995904363840284292674125734" + + "22447765584177886171737265462085498294498946787350929581652632072258992368768457" + + "01782303809656788311228930580914057261086588484587310165815116753332767488701482" + + "91674197015125597825727074064318086014281490241467804723275976842696339357735429" + + "30186739439716388611764209004068663398856841681003872389214483176070116684503887" + + "21236436704331409115573328018297798873659091665961240202177855885487617616198937" + + "07943800566633648843650891448055710397652146960276625835990519870423001794655367" + + "9"; } diff --git a/src/pkg/compress/flate/flate_test.go b/src/pkg/compress/flate/flate_test.go index ba5b0235e..ab7ce250b 100644 --- a/src/pkg/compress/flate/flate_test.go +++ b/src/pkg/compress/flate/flate_test.go @@ -56,8 +56,8 @@ var initDecoderTests = []*InitDecoderTest{ []int{3, 5, 2, 4, 3, 5, 5, 4, 4, 3, 4, 5}, huffmanDecoder{ 2, 5, - [maxCodeLen+1]int{2: 0, 4, 13, 31}, - [maxCodeLen+1]int{2: 0, 1, 6, 20}, + [maxCodeLen + 1]int{2: 0, 4, 13, 31}, + [maxCodeLen + 1]int{2: 0, 1, 6, 20}, // Paper used different code assignment: // 2, 9, 4, 0, 10, 8, 3, 7, 1, 5, 11, 6 // Reordered here so that codes of same length @@ -72,8 +72,8 @@ var initDecoderTests = []*InitDecoderTest{ []int{2, 1, 3, 3}, huffmanDecoder{ 1, 3, - [maxCodeLen+1]int{1: 0, 2, 7}, - [maxCodeLen+1]int{1: 0, 1, 4}, + [maxCodeLen + 1]int{1: 0, 2, 7}, + [maxCodeLen + 1]int{1: 0, 1, 4}, []int{1, 0, 2, 3}, }, true, @@ -84,8 +84,8 @@ var initDecoderTests = []*InitDecoderTest{ []int{3, 3, 3, 3, 3, 2, 4, 4}, huffmanDecoder{ 2, 4, - [maxCodeLen+1]int{2: 0, 6, 15}, - [maxCodeLen+1]int{2: 0, 1, 8}, + [maxCodeLen + 1]int{2: 0, 6, 15}, + [maxCodeLen + 1]int{2: 0, 1, 8}, []int{5, 0, 1, 2, 3, 4, 6, 7}, }, true, diff --git a/src/pkg/compress/flate/huffman_bit_writer.go b/src/pkg/compress/flate/huffman_bit_writer.go index dbf59f2ae..74c21bd2f 100644 --- a/src/pkg/compress/flate/huffman_bit_writer.go +++ b/src/pkg/compress/flate/huffman_bit_writer.go @@ -13,48 +13,47 @@ import ( const ( // The largest offset code. - offsetCodeCount = 30; + offsetCodeCount = 30; // The largest offset code in the extensions. - extendedOffsetCodeCount = 42; + extendedOffsetCodeCount = 42; // The special code used to mark the end of a block. - endBlockMarker = 256; + endBlockMarker = 256; // The first length code. - lengthCodesStart = 257; + lengthCodesStart = 257; // The number of codegen codes. - codegenCodeCount = 19; - - badCode = 255; + codegenCodeCount = 19; + badCode = 255; ) // The number of extra bits needed by length code X - LENGTH_CODES_START. -var lengthExtraBits = []int8 { - /* 257 */ 0, 0, 0, - /* 260 */ 0, 0, 0, 0, 0, 1, 1, 1, 1, 2, - /* 270 */ 2, 2, 2, 3, 3, 3, 3, 4, 4, 4, - /* 280 */ 4, 5, 5, 5, 5, 0, +var lengthExtraBits = []int8{ + /* 257 */0, 0, 0, + /* 260 */0, 0, 0, 0, 0, 1, 1, 1, 1, 2, + /* 270 */2, 2, 2, 3, 3, 3, 3, 4, 4, 4, + /* 280 */4, 5, 5, 5, 5, 0, } // The length indicated by length code X - LENGTH_CODES_START. -var lengthBase = []uint32 { - 0, 1, 2, 3, 4, 5, 6, 7, 8, 10, - 12, 14, 16, 20, 24, 28, 32, 40, 48, 56, - 64, 80, 96, 112, 128, 160, 192, 224, 255 +var lengthBase = []uint32{ + 0, 1, 2, 3, 4, 5, 6, 7, 8, 10, + 12, 14, 16, 20, 24, 28, 32, 40, 48, 56, + 64, 80, 96, 112, 128, 160, 192, 224, 255, } // offset code word extra bits. -var offsetExtraBits = []int8 { - 0, 0, 0, 0, 1, 1, 2, 2, 3, 3, - 4, 4, 5, 5, 6, 6, 7, 7, 8, 8, - 9, 9, 10, 10, 11, 11, 12, 12, 13, 13, +var offsetExtraBits = []int8{ + 0, 0, 0, 0, 1, 1, 2, 2, 3, 3, + 4, 4, 5, 5, 6, 6, 7, 7, 8, 8, + 9, 9, 10, 10, 11, 11, 12, 12, 13, 13, /* extended window */ 14, 14, 15, 15, 16, 16, 17, 17, 18, 18, 19, 19, 20, 20, } -var offsetBase = []uint32 { +var offsetBase = []uint32{ /* normal deflate */ 0x000000, 0x000001, 0x000002, 0x000003, 0x000004, 0x000006, 0x000008, 0x00000c, 0x000010, 0x000018, @@ -66,37 +65,35 @@ var offsetBase = []uint32 { /* extended window */ 0x008000, 0x00c000, 0x010000, 0x018000, 0x020000, 0x030000, 0x040000, 0x060000, 0x080000, 0x0c0000, - 0x100000, 0x180000, 0x200000, 0x300000 + 0x100000, 0x180000, 0x200000, 0x300000, } // The odd order in which the codegen code sizes are written. -var codegenOrder = []uint32 { - 16, 17, 18, 0, 8, 7, 9, 6, 10, 5, 11, 4, 12, 3, 13, 2, 14, 1, 15 -} +var codegenOrder = []uint32{16, 17, 18, 0, 8, 7, 9, 6, 10, 5, 11, 4, 12, 3, 13, 2, 14, 1, 15} type huffmanBitWriter struct { - w io.Writer; + w io.Writer; // Data waiting to be written is bytes[0:nbytes] // and then the low nbits of bits. - bits uint32; - nbits uint32; - bytes [64]byte; - nbytes int; - literalFreq []int32; - offsetFreq []int32; - codegen []uint8; - codegenFreq []int32; - literalEncoding *huffmanEncoder; - offsetEncoding *huffmanEncoder; - codegenEncoding *huffmanEncoder; - err os.Error; + bits uint32; + nbits uint32; + bytes [64]byte; + nbytes int; + literalFreq []int32; + offsetFreq []int32; + codegen []uint8; + codegenFreq []int32; + literalEncoding *huffmanEncoder; + offsetEncoding *huffmanEncoder; + codegenEncoding *huffmanEncoder; + err os.Error; } type WrongValueError struct { - name string; - from int32; - to int32; - value int32; + name string; + from int32; + to int32; + value int32; } func newHuffmanBitWriter(w io.Writer) *huffmanBitWriter { @@ -175,7 +172,7 @@ func (w *huffmanBitWriter) writeBytes(bytes []byte) { n++; } if w.nbits != 0 { - w.err = InternalError("writeBytes with unfinished bits"); + w.err = InternalError("writeBytes with unfinished bits"); return; } if n != 0 { @@ -205,7 +202,7 @@ func (w *huffmanBitWriter) generateCodegen(numLiterals int, numOffsets int) { // a copy of the frequencies, and as the place where we put the result. // This is fine because the output is always shorter than the input used // so far. - codegen := w.codegen; // cache + codegen := w.codegen; // cache // Copy the concatenated code sizes to codegen. Put a marker at the end. copyUint8s(codegen[0 : numLiterals], w.literalEncoding.codeBits); copyUint8s(codegen[numLiterals : numLiterals + numOffsets], w.offsetEncoding.codeBits); @@ -232,7 +229,7 @@ func (w *huffmanBitWriter) generateCodegen(numLiterals int, numOffsets int) { n := min(count, 6); codegen[outIndex] = 16; outIndex++; - codegen[outIndex] = uint8(n - 3); + codegen[outIndex] = uint8(n-3); outIndex++; w.codegenFreq[16]++; count -= n; @@ -242,7 +239,7 @@ func (w *huffmanBitWriter) generateCodegen(numLiterals int, numOffsets int) { n := min(count, 138); codegen[outIndex] = 18; outIndex++; - codegen[outIndex] = uint8(n - 11); + codegen[outIndex] = uint8(n-11); outIndex++; w.codegenFreq[18]++; count -= n; @@ -251,7 +248,7 @@ func (w *huffmanBitWriter) generateCodegen(numLiterals int, numOffsets int) { // count >= 3 && count <= 10 codegen[outIndex] = 17; outIndex++; - codegen[outIndex] = uint8(count - 3); + codegen[outIndex] = uint8(count-3); outIndex++; w.codegenFreq[17]++; count = 0; @@ -295,8 +292,8 @@ func (w *huffmanBitWriter) writeDynamicHeader(numLiterals int, numOffsets int, n w.writeBits(int32(numLiterals - 257), 5); if numOffsets > offsetCodeCount { // Extended version of deflater - w.writeBits(int32(offsetCodeCount + ((numOffsets - (1 + offsetCodeCount)) >> 3)), 5); - w.writeBits(int32((numOffsets - (1 + offsetCodeCount)) & 0x7), 3); + w.writeBits(int32(offsetCodeCount + ((numOffsets - (1 + offsetCodeCount))>>3)), 5); + w.writeBits(int32((numOffsets - (1 + offsetCodeCount))&0x7), 3); } else { w.writeBits(int32(numOffsets - 1), 5); } @@ -368,10 +365,10 @@ func (w *huffmanBitWriter) writeBlock(tokens []token, eof bool, input []byte) { fillInt32s(w.offsetFreq, 0); n := len(tokens); - tokens = tokens[0:n+1]; + tokens = tokens[0 : n+1]; tokens[n] = endBlockMarker; - totalLength := -1; // Subtract 1 for endBlock. + totalLength := -1; // Subtract 1 for endBlock. for _, t := range tokens { switch t.typ() { case literalType: @@ -381,7 +378,7 @@ func (w *huffmanBitWriter) writeBlock(tokens []token, eof bool, input []byte) { case matchType: length := t.length(); offset := t.offset(); - totalLength += int(length + 3); + totalLength += int(length+3); w.literalFreq[lengthCodesStart + lengthCode(length)]++; w.offsetFreq[offsetCode(offset)]++; break; @@ -407,18 +404,18 @@ func (w *huffmanBitWriter) writeBlock(tokens []token, eof bool, input []byte) { var extraBits int64; var storedSize int64; if storedBytes <= maxStoreBlockSize && input != nil { - storedSize = int64((storedBytes + 5) * 8); + storedSize = int64((storedBytes + 5)*8); // We only bother calculating the costs of the extra bits required by // the length of offset fields (which will be the same for both fixed // and dynamic encoding), if we need to compare those two encodings // against stored encoding. for lengthCode := lengthCodesStart + 8; lengthCode < numLiterals; lengthCode++ { // First eight length codes have extra size = 0. - extraBits += int64(w.literalFreq[lengthCode]) * int64(lengthExtraBits[lengthCode - lengthCodesStart]); + extraBits += int64(w.literalFreq[lengthCode])*int64(lengthExtraBits[lengthCode - lengthCodesStart]); } for offsetCode := 4; offsetCode < numOffsets; offsetCode++ { // First four offset codes have extra size = 0. - extraBits += int64(w.offsetFreq[offsetCode]) * int64(offsetExtraBits[offsetCode]); + extraBits += int64(w.offsetFreq[offsetCode])*int64(offsetExtraBits[offsetCode]); } } else { storedSize = math.MaxInt32; @@ -445,7 +442,7 @@ func (w *huffmanBitWriter) writeBlock(tokens []token, eof bool, input []byte) { if numOffsets > offsetCodeCount { extensionSummand = 3; } - dynamicHeader := int64(3 + 5 + 5 + 4 + (3 * numCodegens)) + + dynamicHeader := int64(3+5+5+4+(3 * numCodegens)) + // Following line is an extension. int64(extensionSummand) + w.codegenEncoding.bitLength(w.codegenFreq) + @@ -459,7 +456,7 @@ func (w *huffmanBitWriter) writeBlock(tokens []token, eof bool, input []byte) { if storedSize < fixedSize && storedSize < dynamicSize { w.writeStoredHeader(storedBytes, eof); - w.writeBytes(input[0:storedBytes]); + w.writeBytes(input[0 : storedBytes]); return; } var literalEncoding *huffmanEncoder; @@ -507,4 +504,3 @@ func (w *huffmanBitWriter) writeBlock(tokens []token, eof bool, input []byte) { } } } - diff --git a/src/pkg/compress/flate/inflate.go b/src/pkg/compress/flate/inflate.go index 4c4780156..302cbd376 100644 --- a/src/pkg/compress/flate/inflate.go +++ b/src/pkg/compress/flate/inflate.go @@ -66,10 +66,10 @@ type huffmanDecoder struct { // limit[i] = largest code word of length i // Given code v of length n, // need more bits if v > limit[n]. - limit [maxCodeLen+1]int; + limit [maxCodeLen + 1]int; // base[i] = smallest code word of length i - seq number - base [maxCodeLen+1]int; + base [maxCodeLen + 1]int; // codes[seq number] = output code. // Given code v of length n, value is @@ -83,7 +83,7 @@ func (h *huffmanDecoder) init(bits []int) bool { // Count number of codes of each length, // compute min and max length. - var count [maxCodeLen+1]int; + var count [maxCodeLen + 1]int; var min, max int; for _, n := range bits { if n == 0 { @@ -142,8 +142,8 @@ func (h *huffmanDecoder) init(bits []int) bool { // See RFC 1951, section 3.2.6. var fixedHuffmanDecoder = huffmanDecoder{ 7, 9, - [maxCodeLen+1]int{7: 23, 199, 511}, - [maxCodeLen+1]int{7: 0, 24, 224}, + [maxCodeLen + 1]int{7: 23, 199, 511}, + [maxCodeLen + 1]int{7: 0, 24, 224}, []int{ // length 7: 256-279 256, 257, 258, 259, 260, 261, 262, @@ -271,11 +271,11 @@ func (f *inflater) readHuffman() os.Error { return err; } } - nlit := int(f.b & 0x1F) + 257; + nlit := int(f.b & 0x1F)+257; f.b >>= 5; - ndist := int(f.b & 0x1F) + 1; + ndist := int(f.b & 0x1F)+1; f.b >>= 5; - nclen := int(f.b & 0xF) + 4; + nclen := int(f.b & 0xF)+4; f.b >>= 4; f.nb -= 5+5+4; @@ -437,7 +437,7 @@ func (f *inflater) decodeBlock(hl, hd *huffmanDecoder) os.Error { case dist >= 30: return CorruptInputError(f.roffset); default: - nb := uint(dist-2) >> 1; + nb := uint(dist-2)>>1; // have 1 bit in bottom of dist, need nb more. extra := (dist&1)<<nb; for f.nb < nb { @@ -495,8 +495,8 @@ func (f *inflater) dataBlock() os.Error { if err != nil { return &ReadError{f.roffset, err}; } - n := int(f.buf[0]) | int(f.buf[1]) << 8; - nn := int(f.buf[2]) | int(f.buf[3]) << 8; + n := int(f.buf[0]) | int(f.buf[1])<<8; + nn := int(f.buf[2]) | int(f.buf[3])<<8; if uint16(nn) != uint16(^n) { return CorruptInputError(f.roffset); } diff --git a/src/pkg/compress/flate/reverse_bits.go b/src/pkg/compress/flate/reverse_bits.go index 7274541d0..76693d475 100644 --- a/src/pkg/compress/flate/reverse_bits.go +++ b/src/pkg/compress/flate/reverse_bits.go @@ -44,5 +44,5 @@ func reverseUint16(v uint16) uint16 { } func reverseBits(number uint16, bitLength byte) uint16 { - return reverseUint16(number << uint8(16-bitLength)); + return reverseUint16(number<<uint8(16 - bitLength)); } diff --git a/src/pkg/compress/flate/token.go b/src/pkg/compress/flate/token.go index daa23da55..476eae783 100644 --- a/src/pkg/compress/flate/token.go +++ b/src/pkg/compress/flate/token.go @@ -9,7 +9,7 @@ const ( // 8 bits: xlength = length - MIN_MATCH_LENGTH // 22 bits xoffset = offset - MIN_OFFSET_SIZE, or literal lengthShift = 22; - offsetMask = 1<<lengthShift - 1; + offsetMask = 1 << lengthShift - 1; typeMask = 3<<30; literalType = 0<<30; matchType = 1<<30; @@ -69,12 +69,12 @@ type token uint32 // Convert a literal into a literal token. func literalToken(literal uint32) token { - return token(literalType+literal); + return token(literalType + literal); } // Convert a < xlength, xoffset > pair into a match token. func matchToken(xlength uint32, xoffset uint32) token { - return token(matchType + xlength<<lengthShift + xoffset); + return token(matchType + xlength << lengthShift + xoffset); } // Returns the type of a token @@ -84,16 +84,16 @@ func (t token) typ() uint32 { // Returns the literal of a literal token func (t token) literal() uint32 { - return uint32(t-literalType); + return uint32(t - literalType); } // Returns the extra offset of a match token func (t token) offset() uint32 { - return uint32(t)&offsetMask; + return uint32(t) & offsetMask; } func (t token) length() uint32 { - return uint32((t-matchType)>>lengthShift); + return uint32((t - matchType) >> lengthShift); } func lengthCode(len uint32) uint32 { @@ -107,9 +107,9 @@ func offsetCode(off uint32) uint32 { case off < n: return offsetCodes[off]; case off>>7 < n: - return offsetCodes[off>>7]+14; + return offsetCodes[off>>7] + 14; default: - return offsetCodes[off>>14]+28; + return offsetCodes[off>>14] + 28; } panic("unreachable"); } diff --git a/src/pkg/compress/gzip/gunzip.go b/src/pkg/compress/gzip/gunzip.go index c13184d72..07906cd38 100644 --- a/src/pkg/compress/gzip/gunzip.go +++ b/src/pkg/compress/gzip/gunzip.go @@ -110,7 +110,7 @@ func (z *Inflater) read2() (uint32, os.Error) { if err != nil { return 0, err; } - return uint32(z.buf[0]) | uint32(z.buf[1]) << 8, nil; + return uint32(z.buf[0]) | uint32(z.buf[1])<<8, nil; } func (z *Inflater) readHeader(save bool) os.Error { diff --git a/src/pkg/compress/gzip/gunzip_test.go b/src/pkg/compress/gzip/gunzip_test.go index 8cc7890e4..867f61efa 100644 --- a/src/pkg/compress/gzip/gunzip_test.go +++ b/src/pkg/compress/gzip/gunzip_test.go @@ -12,31 +12,31 @@ import ( ) type gzipTest struct { - name string; - desc string; - raw string; - gzip []byte; - err os.Error; + name string; + desc string; + raw string; + gzip []byte; + err os.Error; } -var gzipTests = []gzipTest { - gzipTest { // has 1 empty fixed-huffman block +var gzipTests = []gzipTest{ + gzipTest{ // has 1 empty fixed-huffman block "empty.txt", "empty.txt", "", - []byte { + []byte{ 0x1f, 0x8b, 0x08, 0x08, 0xf7, 0x5e, 0x14, 0x4a, 0x00, 0x03, 0x65, 0x6d, 0x70, 0x74, 0x79, 0x2e, 0x74, 0x78, 0x74, 0x00, 0x03, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, }, - nil + nil, }, - gzipTest { // has 1 non-empty fixed huffman block + gzipTest{ // has 1 non-empty fixed huffman block "hello.txt", "hello.txt", "hello world\n", - []byte { + []byte{ 0x1f, 0x8b, 0x08, 0x08, 0xc8, 0x58, 0x13, 0x4a, 0x00, 0x03, 0x68, 0x65, 0x6c, 0x6c, 0x6f, 0x2e, 0x74, 0x78, 0x74, 0x00, 0xcb, 0x48, 0xcd, 0xc9, @@ -44,14 +44,14 @@ var gzipTests = []gzipTest { 0x02, 0x00, 0x2d, 0x3b, 0x08, 0xaf, 0x0c, 0x00, 0x00, 0x00, }, - nil + nil, }, - gzipTest { // concatenation + gzipTest{ // concatenation "hello.txt", "hello.txt x2", "hello world\n" "hello world\n", - []byte { + []byte{ 0x1f, 0x8b, 0x08, 0x08, 0xc8, 0x58, 0x13, 0x4a, 0x00, 0x03, 0x68, 0x65, 0x6c, 0x6c, 0x6f, 0x2e, 0x74, 0x78, 0x74, 0x00, 0xcb, 0x48, 0xcd, 0xc9, @@ -65,13 +65,13 @@ var gzipTests = []gzipTest { 0x02, 0x00, 0x2d, 0x3b, 0x08, 0xaf, 0x0c, 0x00, 0x00, 0x00, }, - nil + nil, }, - gzipTest { // has a fixed huffman block with some length-distance pairs + gzipTest{ // has a fixed huffman block with some length-distance pairs "shesells.txt", "shesells.txt", "she sells seashells by the seashore\n", - []byte { + []byte{ 0x1f, 0x8b, 0x08, 0x08, 0x72, 0x66, 0x8b, 0x4a, 0x00, 0x03, 0x73, 0x68, 0x65, 0x73, 0x65, 0x6c, 0x6c, 0x73, 0x2e, 0x74, 0x78, 0x74, 0x00, 0x2b, @@ -81,9 +81,9 @@ var gzipTests = []gzipTest { 0x94, 0xca, 0x05, 0x00, 0x76, 0xb0, 0x3b, 0xeb, 0x24, 0x00, 0x00, 0x00, }, - nil + nil, }, - gzipTest { // has dynamic huffman blocks + gzipTest{ // has dynamic huffman blocks "gettysburg", "gettysburg", " Four score and seven years ago our fathers brought forth on\n" @@ -115,7 +115,7 @@ var gzipTests = []gzipTest { "people, for the people, shall not perish from this earth.\n" "\n" "Abraham Lincoln, November 19, 1863, Gettysburg, Pennsylvania\n", - []byte { + []byte{ 0x1f, 0x8b, 0x08, 0x08, 0xd1, 0x12, 0x2b, 0x4a, 0x00, 0x03, 0x67, 0x65, 0x74, 0x74, 0x79, 0x73, 0x62, 0x75, 0x72, 0x67, 0x00, 0x65, 0x54, 0xcd, @@ -219,13 +219,13 @@ var gzipTests = []gzipTest { 0x4a, 0x65, 0x8f, 0x08, 0x42, 0x60, 0xf7, 0x0f, 0xb9, 0x16, 0x0b, 0x0c, 0x1a, 0x06, 0x00, 0x00, }, - nil + nil, }, - gzipTest { // has 1 non-empty fixed huffman block then garbage + gzipTest{ // has 1 non-empty fixed huffman block then garbage "hello.txt", "hello.txt + garbage", "hello world\n", - []byte { + []byte{ 0x1f, 0x8b, 0x08, 0x08, 0xc8, 0x58, 0x13, 0x4a, 0x00, 0x03, 0x68, 0x65, 0x6c, 0x6c, 0x6f, 0x2e, 0x74, 0x78, 0x74, 0x00, 0xcb, 0x48, 0xcd, 0xc9, @@ -235,11 +235,11 @@ var gzipTests = []gzipTest { }, HeaderError, }, - gzipTest { // has 1 non-empty fixed huffman block not enough header + gzipTest{ // has 1 non-empty fixed huffman block not enough header "hello.txt", "hello.txt + garbage", "hello world\n", - []byte { + []byte{ 0x1f, 0x8b, 0x08, 0x08, 0xc8, 0x58, 0x13, 0x4a, 0x00, 0x03, 0x68, 0x65, 0x6c, 0x6c, 0x6f, 0x2e, 0x74, 0x78, 0x74, 0x00, 0xcb, 0x48, 0xcd, 0xc9, @@ -249,11 +249,11 @@ var gzipTests = []gzipTest { }, io.ErrUnexpectedEOF, }, - gzipTest { // has 1 non-empty fixed huffman block but corrupt checksum + gzipTest{ // has 1 non-empty fixed huffman block but corrupt checksum "hello.txt", "hello.txt + corrupt checksum", "hello world\n", - []byte { + []byte{ 0x1f, 0x8b, 0x08, 0x08, 0xc8, 0x58, 0x13, 0x4a, 0x00, 0x03, 0x68, 0x65, 0x6c, 0x6c, 0x6f, 0x2e, 0x74, 0x78, 0x74, 0x00, 0xcb, 0x48, 0xcd, 0xc9, @@ -263,11 +263,11 @@ var gzipTests = []gzipTest { }, ChecksumError, }, - gzipTest { // has 1 non-empty fixed huffman block but corrupt size + gzipTest{ // has 1 non-empty fixed huffman block but corrupt size "hello.txt", "hello.txt + corrupt size", "hello world\n", - []byte { + []byte{ 0x1f, 0x8b, 0x08, 0x08, 0xc8, 0x58, 0x13, 0x4a, 0x00, 0x03, 0x68, 0x65, 0x6c, 0x6c, 0x6f, 0x2e, 0x74, 0x78, 0x74, 0x00, 0xcb, 0x48, 0xcd, 0xc9, @@ -303,4 +303,3 @@ func TestInflater(t *testing.T) { } } } - diff --git a/src/pkg/compress/zlib/reader.go b/src/pkg/compress/zlib/reader.go index 96dbbf992..cf0eb1b29 100644 --- a/src/pkg/compress/zlib/reader.go +++ b/src/pkg/compress/zlib/reader.go @@ -43,7 +43,7 @@ func NewInflater(r io.Reader) (io.ReadCloser, os.Error) { if err != nil { return nil, err; } - h := uint(z.scratch[0]) << 8 | uint(z.scratch[1]); + h := uint(z.scratch[0])<<8 | uint(z.scratch[1]); if (z.scratch[0] & 0x0f != zlibDeflate) || (h%31 != 0) { return nil, HeaderError; } @@ -77,7 +77,7 @@ func (z *reader) Read(p []byte) (n int, err os.Error) { return 0, err; } // ZLIB (RFC 1950) is big-endian, unlike GZIP (RFC 1952). - checksum := uint32(z.scratch[0]) << 24 | uint32(z.scratch[1]) << 16 | uint32(z.scratch[2]) << 8 | uint32(z.scratch[3]); + checksum := uint32(z.scratch[0])<<24 | uint32(z.scratch[1])<<16 | uint32(z.scratch[2])<<8 | uint32(z.scratch[3]); if checksum != z.digest.Sum32() { z.err = ChecksumError; return 0, z.err; |