MCPcopy
hub / github.com/klauspost/compress / writeHeader

Method writeHeader

zlib/writer.go:95–141  ·  view source on GitHub ↗

writeHeader writes the ZLIB header.

()

Source from the content-addressed store, hash-verified

93
94// writeHeader writes the ZLIB header.
95func (z *Writer) writeHeader() (err error) {
96 z.wroteHeader = true
97 // ZLIB has a two-byte header (as documented in RFC 1950).
98 // The first four bits is the CINFO (compression info), which is 7 for the default deflate window size.
99 // The next four bits is the CM (compression method), which is 8 for deflate.
100 z.scratch[0] = 0x78
101 // The next two bits is the FLEVEL (compression level). The four values are:
102 // 0=fastest, 1=fast, 2=default, 3=best.
103 // The next bit, FDICT, is set if a dictionary is given.
104 // The final five FCHECK bits form a mod-31 checksum.
105 switch z.level {
106 case -2, 0, 1:
107 z.scratch[1] = 0 << 6
108 case 2, 3, 4, 5:
109 z.scratch[1] = 1 << 6
110 case 6, -1:
111 z.scratch[1] = 2 << 6
112 case 7, 8, 9:
113 z.scratch[1] = 3 << 6
114 default:
115 panic("unreachable")
116 }
117 if z.dict != nil {
118 z.scratch[1] |= 1 << 5
119 }
120 z.scratch[1] += uint8(31 - binary.BigEndian.Uint16(z.scratch[:2])%31)
121 if _, err = z.w.Write(z.scratch[0:2]); err != nil {
122 return err
123 }
124 if z.dict != nil {
125 // The next four bytes are the Adler-32 checksum of the dictionary.
126 binary.BigEndian.PutUint32(z.scratch[:], adler32.Checksum(z.dict))
127 if _, err = z.w.Write(z.scratch[0:4]); err != nil {
128 return err
129 }
130 }
131 if z.compressor == nil {
132 // Initialize deflater unless the Writer is being reused
133 // after a Reset call.
134 z.compressor, err = flate.NewWriterDict(z.w, z.level, z.dict)
135 if err != nil {
136 return err
137 }
138 z.digest = adler32.New()
139 }
140 return nil
141}
142
143// Write writes a compressed form of p to the underlying io.Writer. The
144// compressed bytes are not necessarily flushed until the Writer is closed or

Callers 3

WriteMethod · 0.95
FlushMethod · 0.95
CloseMethod · 0.95

Calls 2

NewWriterDictFunction · 0.92
WriteMethod · 0.65

Tested by

no test coverage detected