// Copyright 2018 The Wuffs Authors. // // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. // You may obtain a copy of the License at // // https://www.apache.org/licenses/LICENSE-2.0 // // Unless required by applicable law or agreed to in writing, software // distributed under the License is distributed on an "AS IS" BASIS, // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. // See the License for the specific language governing permissions and // limitations under the License. // +build ignore package main // make-deflate.go makes some data in the deflate format. // // See test/data/artificial/*.deflate.make.txt for examples. // // Usage: go run make-deflate.go < foo.make.txt import ( "fmt" "io/ioutil" "os" "strconv" "strings" ) // Global state. var ( block uint32 bncData []byte repeats []repeat stream bitStream ) const ( blockZero = 0 blockNoCompression = 1 blockFixedHuffman = 2 // TODO: blockDynamicHuffman. ) var doFuncs = [...]func(s string) error{ blockZero: doBlockZero, blockNoCompression: doBlockNoCompression, blockFixedHuffman: doBlockFixedHuffman, } type bitStream struct { buf []byte bits uint32 nBits uint32 // Always within [0, 7]. } // writeBits writes the low n bits of b to z. func (z *bitStream) writeBits(b uint32, n uint32) { if n > 24 { panic("writeBits: n is too large") } z.bits |= b << z.nBits z.nBits += n for z.nBits >= 8 { z.buf = append(z.buf, uint8(z.bits)) z.bits >>= 8 z.nBits -= 8 } } func (z *bitStream) writeBytes(b []byte) { z.flush() z.buf = append(z.buf, b...) } func (z *bitStream) writeLCode(lCode uint32) { switch { case lCode < 144: // 0b._0011_0000 through 0b._1011_1111 lCode += 0x030 z.writeBits(reverse(lCode, 8), 8) case lCode < 256: // 0b1_1001_0000 through 0b1_1111_1111 lCode += 0x190 - 144 z.writeBits(reverse(lCode, 9), 9) case lCode < 280: // 0b._.000_0000 through 0b._.001_0111 lCode -= 256 - 0x000 z.writeBits(reverse(lCode, 7), 7) default: // 0b._1100_0000 through 0b._1100_0111 lCode -= 280 - 0x0C0 z.writeBits(reverse(lCode, 8), 8) } } func (z *bitStream) flush() { if z.nBits > 0 { z.buf = append(z.buf, uint8(z.bits)) z.bits = 0 z.nBits = 0 } } type repeat struct { count uint32 remaining string } func main() { if err := main1(); err != nil { os.Stderr.WriteString(err.Error() + "\n") os.Exit(1) } } func main1() error { stdin, err := ioutil.ReadAll(os.Stdin) if err != nil { return err } s := string(stdin) for remaining := ""; len(s) > 0; s, remaining = remaining, "" { if i := strings.IndexByte(s, '\n'); i >= 0 { s, remaining = s[:i], s[i+1:] } s = strings.TrimSpace(s) if s == "" || s[0] == '#' { continue } const rep = "repeat " if strings.HasPrefix(s, rep) { args := s[len(rep):] count, args, ok := parseNum(args) if !ok || count <= 0 || args != "[" { return fmt.Errorf("bad repeat command: %q", s) } repeats = append(repeats, repeat{ count: count, remaining: remaining, }) continue } if s == "]" { if len(repeats) <= 0 { return fmt.Errorf(`unbalanced close-repeat command: "]"`) } i := len(repeats) - 1 repeats[i].count-- if repeats[i].count == 0 { repeats = repeats[:i] } else { remaining = repeats[i].remaining } continue } if err := doFuncs[block](s); err != nil { return err } } stream.flush() _, err = os.Stdout.Write(stream.buf) return err } func doBlockZero(line string) error { const ( bnc = "blockNoCompression " bfh = "blockFixedHuffman " ) bits := uint32(0) s := "" switch { case strings.HasPrefix(line, bnc): s = line[len(bnc):] block = blockNoCompression bits |= 0 << 1 case strings.HasPrefix(line, bfh): s = line[len(bfh):] block = blockFixedHuffman bits |= 1 << 1 default: return fmt.Errorf("bad blockZero command: %q", line) } switch s { case "(final) {": bits |= 1 case "(nonFinal) {": // No-op. default: return fmt.Errorf("bad blockZero command: %q", line) } stream.writeBits(bits, 3) return nil } func doBlockNoCompression(line string) error { if line == "}" { if len(bncData) > 0xFFFF { return fmt.Errorf("bncData is too long") } n := uint32(len(bncData)) stream.flush() stream.writeBits(n, 16) stream.writeBits(0xFFFF-n, 16) stream.writeBytes(bncData) bncData = bncData[:0] block = blockZero return nil } if lit, ok := parseLiteral(line); ok { bncData = append(bncData, lit...) return nil } return fmt.Errorf("bad blockNoCompression command: %q", line) } func doBlockFixedHuffman(line string) error { if line == "}" { block = blockZero return nil } if line == "endOfBlock" { stream.writeBits(0, 7) return nil } if lit, ok := parseLiteral(line); ok { // TODO: support "\xAB" escape codes in the script? for i := 0; i < len(lit); i++ { stream.writeLCode(uint32(lit[i])) } return nil } if l, d, ok := parseLenDist(line); ok { lCode, lExtra, lNExtra := encodeLength(l) stream.writeLCode(lCode) stream.writeBits(lExtra, lNExtra) dCode, dExtra, dNExtra := encodeDistance(d) stream.writeBits(reverse(dCode, 5), 5) stream.writeBits(dExtra, dNExtra) return nil } return fmt.Errorf("bad blockFixedHuffman command: %q", line) } func encodeLength(l uint32) (code uint32, extra uint32, nExtra uint32) { switch { case l < 3: // No-op. case l < 11: l -= 3 return (l >> 0) + 257, l & 0x0000, 0 case l < 19: l -= 11 return (l >> 1) + 265, l & 0x0001, 1 case l < 35: l -= 19 return (l >> 2) + 269, l & 0x0003, 2 case l < 67: l -= 35 return (l >> 3) + 273, l & 0x0007, 3 case l < 131: l -= 67 return (l >> 4) + 277, l & 0x000F, 4 case l < 258: l -= 131 return (l >> 5) + 281, l & 0x001F, 5 case l == 258: return 285, 0, 0 } panic(fmt.Sprintf("encodeLength: l=%d", l)) } func encodeDistance(d uint32) (code uint32, extra uint32, nExtra uint32) { switch { case d < 1: // No-op. case d < 5: d -= 1 return (d >> 0) + 0, d & 0x0000, 0 case d < 9: d -= 5 return (d >> 1) + 4, d & 0x0001, 1 case d < 17: d -= 9 return (d >> 2) + 6, d & 0x0003, 2 case d < 33: d -= 17 return (d >> 3) + 8, d & 0x0007, 3 case d < 65: d -= 33 return (d >> 4) + 10, d & 0x000F, 4 case d < 129: d -= 65 return (d >> 5) + 12, d & 0x001F, 5 case d < 257: d -= 129 return (d >> 6) + 14, d & 0x003F, 6 case d < 513: d -= 257 return (d >> 7) + 16, d & 0x007F, 7 case d < 1025: d -= 513 return (d >> 8) + 18, d & 0x00FF, 8 case d < 2049: d -= 1025 return (d >> 9) + 20, d & 0x01FF, 9 case d < 4097: d -= 2049 return (d >> 10) + 22, d & 0x03FF, 10 case d < 8193: d -= 4097 return (d >> 11) + 24, d & 0x07FF, 11 case d < 16385: d -= 8193 return (d >> 12) + 26, d & 0x0FFF, 12 case d < 32769: d -= 16385 return (d >> 13) + 28, d & 0x1FFF, 13 } panic(fmt.Sprintf("encodeDistance: d=%d", d)) } func reverse(x uint32, n uint32) uint32 { x = uint32(reverse8[0xFF&(x>>0)])<<24 | uint32(reverse8[0xFF&(x>>8)])<<16 | uint32(reverse8[0xFF&(x>>16)])<<8 | uint32(reverse8[0xFF&(x>>24)])<<0 return x >> (32 - n) } var reverse8 = [256]byte{ 0x00, 0x80, 0x40, 0xC0, 0x20, 0xA0, 0x60, 0xE0, // 0x00 - 0x07 0x10, 0x90, 0x50, 0xD0, 0x30, 0xB0, 0x70, 0xF0, // 0x08 - 0x0F 0x08, 0x88, 0x48, 0xC8, 0x28, 0xA8, 0x68, 0xE8, // 0x10 - 0x17 0x18, 0x98, 0x58, 0xD8, 0x38, 0xB8, 0x78, 0xF8, // 0x18 - 0x1F 0x04, 0x84, 0x44, 0xC4, 0x24, 0xA4, 0x64, 0xE4, // 0x20 - 0x27 0x14, 0x94, 0x54, 0xD4, 0x34, 0xB4, 0x74, 0xF4, // 0x28 - 0x2F 0x0C, 0x8C, 0x4C, 0xCC, 0x2C, 0xAC, 0x6C, 0xEC, // 0x30 - 0x37 0x1C, 0x9C, 0x5C, 0xDC, 0x3C, 0xBC, 0x7C, 0xFC, // 0x38 - 0x3F 0x02, 0x82, 0x42, 0xC2, 0x22, 0xA2, 0x62, 0xE2, // 0x40 - 0x47 0x12, 0x92, 0x52, 0xD2, 0x32, 0xB2, 0x72, 0xF2, // 0x48 - 0x4F 0x0A, 0x8A, 0x4A, 0xCA, 0x2A, 0xAA, 0x6A, 0xEA, // 0x50 - 0x57 0x1A, 0x9A, 0x5A, 0xDA, 0x3A, 0xBA, 0x7A, 0xFA, // 0x58 - 0x5F 0x06, 0x86, 0x46, 0xC6, 0x26, 0xA6, 0x66, 0xE6, // 0x60 - 0x67 0x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 0xF7 0x1F, 0x9F, 0x5F, 0xDF, 0x3F, 0xBF, 0x7F, 0xFF, // 0xF8 - 0xFF } func parseLiteral(s string) (lit string, ok bool) { const ( prefix = `literal "` suffix = `"` ) if strings.HasPrefix(s, prefix) { s = s[len(prefix):] if strings.HasSuffix(s, suffix) { return s[:len(s)-len(suffix)], true } } return "", false } func parseLenDist(line string) (l uint32, d uint32, ok bool) { const ( lStr = "len " dStr = "dist " ) s := line if strings.HasPrefix(s, lStr) { s = s[len(lStr):] if l, s, ok := parseNum(s); ok && 3 <= l && l <= 258 { if strings.HasPrefix(s, dStr) { s = s[len(dStr):] if d, s, ok := parseNum(s); ok && 1 <= d && d <= 32768 && s == "" { return l, d, true } } } } return 0, 0, false } func parseNum(s string) (num uint32, remaining string, ok bool) { if i := strings.IndexByte(s, ' '); i >= 0 { s, remaining = s[:i], s[i+1:] for len(remaining) > 0 && remaining[0] == ' ' { remaining = remaining[1:] } } u, err := strconv.ParseUint(s, 10, 32) if err != nil { return 0, "", false } return uint32(u), remaining, true }