// Inflate utility that decompresses a string using the flate algo func Inflate(deflated []byte) []byte { var b bytes.Buffer r := flate.NewReader(bytes.NewReader(deflated)) b.ReadFrom(r) r.Close() return b.Bytes() }
func (db *Database) Load(filename string) { var rows, columns uint32 f, _ := os.Open(filename) c := flate.NewReader(f) r := bufio.NewReader(c) binary.Read(r, binary.LittleEndian, &rows) binary.Read(r, binary.LittleEndian, &columns) ids := make(map[string]int) db.ids = ids keys := make([]string, rows) db.keys = keys for i := 0; i < int(rows); i++ { b, _ := r.ReadBytes('\x00') key := string(b[:len(b)-1]) keys[i] = key ids[key] = i } db.columns = make(map[string]*Column) for i := 0; i < int(columns); i++ { var id, count uint32 name, _ := r.ReadBytes('\x00') binary.Read(r, binary.LittleEndian, &count) column := db.getColumn(string(name[:len(name)-1])) for j := 0; j < int(count); j++ { binary.Read(r, binary.LittleEndian, &id) column.Set(int(id), true) } } c.Close() f.Close() }
func (parser *redirectRequestParser) Parse(request *http.Request) (loginReq *AuthnRequest, relayState string, err error) { err = request.ParseForm() if err != nil { return } relayState = request.Form.Get("RelayState") if len(relayState) > 80 { err = errors.New("RelayState cannot be longer than 80 characters.") return } samlReq := request.Form.Get("SAMLRequest") // URL decoding is already performed // remove base64 encoding reqBytes, err := base64.StdEncoding.DecodeString(samlReq) if err != nil { return } // Remove deflate req := flate.NewReader(bytes.NewReader(reqBytes)) // Read the XML decoder := xml.NewDecoder(req) loginReq = &AuthnRequest{} err = decoder.Decode(loginReq) return }
func NewConnection(nodeID string, reader io.Reader, writer io.Writer, receiver Model) Connection { cr := &countingReader{Reader: reader} cw := &countingWriter{Writer: writer} flrd := flate.NewReader(cr) flwr, err := flate.NewWriter(cw, flate.BestSpeed) if err != nil { panic(err) } wb := bufio.NewWriter(flwr) c := rawConnection{ id: nodeID, receiver: nativeModel{receiver}, reader: flrd, cr: cr, xr: xdr.NewReader(flrd), writer: flwr, cw: cw, wb: wb, xw: xdr.NewWriter(wb), awaiting: make([]chan asyncResult, 0x1000), indexSent: make(map[string]map[string][2]int64), outbox: make(chan []encodable), nextID: make(chan int), closed: make(chan struct{}), } go c.readerLoop() go c.writerLoop() go c.pingerLoop() go c.idGenerator() return wireFormatConnection{&c} }
// NewReaderDict is like NewReader but uses a preset dictionary. // NewReaderDict ignores the dictionary if the compressed data does not refer to it. func NewReaderDict(r io.Reader, dict []byte) (io.ReadCloser, os.Error) { z := new(reader) if fr, ok := r.(flate.Reader); ok { z.r = fr } else { z.r = bufio.NewReader(r) } _, err := io.ReadFull(z.r, z.scratch[0:2]) if err != nil { return nil, err } h := uint(z.scratch[0])<<8 | uint(z.scratch[1]) if (z.scratch[0]&0x0f != zlibDeflate) || (h%31 != 0) { return nil, HeaderError } if z.scratch[1]&0x20 != 0 { _, err = io.ReadFull(z.r, z.scratch[0:4]) if err != nil { return nil, err } checksum := uint32(z.scratch[0])<<24 | uint32(z.scratch[1])<<16 | uint32(z.scratch[2])<<8 | uint32(z.scratch[3]) if checksum != adler32.Checksum(dict) { return nil, DictionaryError } z.decompressor = flate.NewReaderDict(z.r, dict) } else { z.decompressor = flate.NewReader(z.r) } z.digest = adler32.New() return z, nil }
func Decrypt(key string, publickey PublicKey, rin io.Reader) (r io.Reader, length int64, sequence int64, e os.Error) { c, e := simpleCipher(key) if e != nil { e = os.NewError("Trouble reading the symmetric key: " + e.String()) return } pub, e := readPublicKey(publickey) if e != nil { e = os.NewError("Trouble reading the public key: " + e.String()) return } iv := make([]byte, c.BlockSize()) _, e = io.ReadFull(rin, iv) // read the iv first (it's not encrypted) if e != nil { e = os.NewError("Trouble reading the iv: " + e.String()) return } decrypter := cipher.NewCFBDecrypter(c, iv) rdec := flate.NewReader(cipher.StreamReader{decrypter, rin}) e = binary.Read(rdec, binary.LittleEndian, &length) if e != nil { e = os.NewError("Trouble reading the file length: " + e.String()) return } e = binary.Read(rdec, binary.LittleEndian, &sequence) if e != nil { e = os.NewError("Trouble reading the serial number: " + e.String()) return } return &hashReader{rdec, sha256.New(), pub, length}, length, sequence, nil }
func (dz *reader) get(start, size int64) ([]byte, error) { if size == 0 { return []byte{}, nil } if start < 0 || size < 0 { return nil, fmt.Errorf("Negative start or size") } if int(start/dz.blocksize) >= len(dz.offsets) { return nil, fmt.Errorf("Start passed end of archive") } start1 := dz.blocksize * (start / dz.blocksize) size1 := size + (start - start1) dz.lock.Lock() defer dz.lock.Unlock() _, err := dz.fp.Seek(dz.offsets[start/dz.blocksize], 0) if err != nil { return nil, err } rd := flate.NewReader(dz.fp) data := make([]byte, size1) _, err = io.ReadFull(rd, data) if err != nil { return nil, err } return data[start-start1:], nil }
func ExampleDecode() { // In this example, we read a PBM (black and white) "file" (really a // hard-wired string to make the example self-contained) into a PPM // (color) image. Because netpbm.Decode returns a netpbm.Image, not a // generic image.Image we can query its Netpbm format and maximum // value. We also output an arbitrary white pixel to show how B&W can // be promoted to color. const pbmRaw = "\n0\xe1RVp\x0eru\f\xf1\x0f\xb2Rp\xf7\xf4\rP\b\xf0\xf3Up\xcb\xcc)I-R\bK-*\xce\xcc\xcfS0\xd43\xe423Q02\xe1\xfa\x0f\x02\xff\xfe\xff\x06\xd3\x1f\xec\x1b\xc1\xf4a{\x060\xddo\xc3\x01\xa6\xe5l*\xc0\xca\xea,~\x80\xe8?\u007f\n\u007f\x82\xe8\x9f\xff\x1eC\xe8\xff\xcf?\x02\xa9\v\xc6\xff\xfa?\xff\xff\xdf\xc0\xfd|A\xff\xe3\xff\x17\xe2\xff\x1fc\x90?\xfe\x83\xf5\xff\x97\xbeB\xfb\xe3M\xff\xff2\xcc\u007fd/\xbf\xff/\x03\xb7\xfc\xa1:\xf9\xff\f\xfc\xff\xed\xfbj\xec\xff\x89\xff\a\xd2\x15\xf5 \xe3\xed\xe4\x12\xc0\xd6\xd8\xd81\x82i\x81\u007f\xec`\x9a\xf1\u007f?\xd8\x19`\x1e\x1a\x00\x04\x00\x00\xff\xff" r := flate.NewReader(bytes.NewBufferString(pbmRaw)) img, err := netpbm.Decode(r, &netpbm.DecodeOptions{ Target: netpbm.PPM, // Want to wind up with color Exact: false, // Can accept grayscale or B&W too PBMMaxValue: 42, // B&W white --> (42, 42, 42) }) if err != nil { panic(err) } r.Close() fmt.Printf("Image is of type %s.\n", img.Format()) fmt.Printf("Maximum channel value is %d.\n", img.MaxValue()) fmt.Printf("Color at (32, 20) is %#v.\n", img.At(32, 20)) // Output: // Image is of type PPM. // Maximum channel value is 42. // Color at (32, 20) is npcolor.RGBM{R:0x2a, G:0x2a, B:0x2a, M:0x2a}. }
func Decompress(in []byte) []byte { buf := new(bytes.Buffer) decompressor := flate.NewReader(bytes.NewReader(in)) io.Copy(buf, decompressor) decompressor.Close() return buf.Bytes() }
func main() { var file *os.File var err error if file, err = os.Open("output/sample.tar.flate"); err != nil { log.Fatalln(err) } defer file.Close() tr := tar.NewReader(flate.NewReader(file)) var header *tar.Header for { header, err = tr.Next() if err == io.EOF { // ファイルの最後 break } if err != nil { log.Fatalln(err) } buf := new(bytes.Buffer) if _, err = io.Copy(buf, tr); err != nil { log.Fatalln(err) } if err = ioutil.WriteFile("output/"+header.Name, buf.Bytes(), 0755); err != nil { log.Fatal(err) } } }
func (r *ZipScannerImpl) Reader() (io.Reader, error) { switch r.fh.Method { case zip.Deflate: if r.Debug { fmt.Println("inflating...") } r.fr = flate.NewReader(r.reader) case zip.Store: if r.Debug { fmt.Println("reading...") } if r.fh.UncompressedSize > 0 { r.fr = io.LimitReader(r.reader, int64(r.fh.UncompressedSize)) } else if r.fh.UncompressedSize == 0 && (r.fh.Flags&hasDataDescriptor) == 0 { // file of 0 bytes or directory ? r.fr = io.LimitReader(r.reader, 0) } else { return r.readError(NoUncompressedSize) } default: return r.readError(UnsupportedCompression) } r.err = nil return r.fr, r.err }
func DecompressString(in string) string { buf := new(bytes.Buffer) decompressor := flate.NewReader(strings.NewReader(in)) io.Copy(buf, decompressor) decompressor.Close() return buf.String() }
// BodyReader returns an io.ReadCloser that reads the HTTP request or response // body. If mv.skipBody was set the reader will immediately return io.EOF. // // If the Decode option is passed the body will be unchunked if // Transfer-Encoding is set to "chunked", and will decode the following // Content-Encodings: gzip, deflate. func (mv *MessageView) BodyReader(opts ...Option) (io.ReadCloser, error) { var r io.Reader conf := &config{} for _, o := range opts { o(conf) } br := bytes.NewReader(mv.message) r = io.NewSectionReader(br, mv.bodyoffset, mv.traileroffset-mv.bodyoffset) if !conf.decode { return ioutil.NopCloser(r), nil } if mv.chunked { r = httputil.NewChunkedReader(r) } switch mv.compress { case "gzip": gr, err := gzip.NewReader(r) if err != nil { return nil, err } return gr, nil case "deflate": return flate.NewReader(r), nil default: return ioutil.NopCloser(r), nil } }
//Read returns a channel which logentries are appended to //in sequential order func (l *Logger) Read() <-chan model.LogEntry { c := make(chan model.LogEntry) go func(c chan<- model.LogEntry) { defer close(c) rFile, err := os.OpenFile(l.wFile.Name(), os.O_RDONLY, 0644) if err != nil { log.Fatal(err) } defer rFile.Close() reader := flate.NewReader(rFile) defer reader.Close() scanner := bufio.NewScanner(reader) scanner.Split(ScanPayloadSplitFunc) for scanner.Scan() { entry := model.LogEntry(scanner.Bytes()) c <- entry } err = scanner.Err() if err != nil && err != io.ErrUnexpectedEOF { log.Fatal(err) } }(c) return c }
func NewCompressedReadWriteCloser(rwc io.ReadWriteCloser) io.ReadWriteCloser { w, _ := flate.NewWriter(rwc, 1) return &compressedRWC{ flate.NewReader(rwc), w, } }
func (self *Surf) Download(req Request) (resp *http.Response, err error) { param, err := NewParam(req) if err != nil { return nil, err } param.client = self.buildClient(param) resp, err = self.httpRequest(param) if err == nil { switch resp.Header.Get("Content-Encoding") { case "gzip": var gzipReader *gzip.Reader gzipReader, err = gzip.NewReader(resp.Body) if err == nil { resp.Body = gzipReader } case "deflate": resp.Body = flate.NewReader(resp.Body) case "zlib": var readCloser io.ReadCloser readCloser, err = zlib.NewReader(resp.Body) if err == nil { resp.Body = readCloser } } } resp = param.writeback(resp) return }
func (a *Article) Website() io.ReadCloser { if a.WebsiteRaw != nil { return flate.NewReader(bytes.NewReader(a.WebsiteRaw)) } return nil }
func main() { input, e := ioutil.ReadAll(os.Stdin) if e != nil { log.Fatal(e) } if isHttpPostBinding(input) { decoded, e := base64.StdEncoding.DecodeString(string(input)) if e != nil { log.Fatal(e) } fmt.Print(string(decoded)) } else { unescaped, e := url.QueryUnescape(string(input)) if e != nil { log.Fatal(e) } decoded, e := base64.StdEncoding.DecodeString(unescaped) if e != nil { log.Fatal(e) } inflater := flate.NewReader(bytes.NewBuffer(decoded)) io.Copy(os.Stdout, inflater) inflater.Close() } }
func readBody(resp *http.Response) ([]byte, error) { buffer := bytes.NewBuffer(make([]byte, 0, 1024)) var rd io.ReadCloser var err error switch resp.Header.Get("Content-Encoding") { case "gzip": rd, _ = gzip.NewReader(resp.Body) defer rd.Close() case "deflate": rd = flate.NewReader(resp.Body) defer rd.Close() default: rd = resp.Body } defer func() { e := recover() if e == nil { return } if panicErr, ok := e.(error); ok && panicErr == bytes.ErrTooLarge { err = panicErr } else { panic(e) } }() _, err = buffer.ReadFrom(rd) return buffer.Bytes(), err }
func (data *Data) Decompress() error { if !data.IsCompressed() { return nil } input := bytes.NewBuffer(data.Content) var reader io.Reader if data.ContentEncoding == ceDeflate { reader = flate.NewReader(input) } else if data.ContentEncoding == ceGzip { var err error if reader, err = gzip.NewReader(input); err != nil { return err } } else { return log.Error("unknown content-encoding", log.WithValue("content-encoding", data.ContentEncoding)) } writer := bytes.Buffer{} _, err := io.Copy(&writer, reader) if err != nil { return err } data.Content = writer.Bytes() data.ContentEncoding = "" data.UncompressedLength = len(data.Content) return nil }
// NewIdpAuthnRequest returns a new IdpAuthnRequest for the given HTTP request to the authorization // service. func NewIdpAuthnRequest(idp *IdentityProvider, r *http.Request) (*IdpAuthnRequest, error) { req := &IdpAuthnRequest{ IDP: idp, HTTPRequest: r, } switch r.Method { case "GET": compressedRequest, err := base64.StdEncoding.DecodeString(r.URL.Query().Get("SAMLRequest")) if err != nil { return nil, fmt.Errorf("cannot decode request: %s", err) } req.RequestBuffer, err = ioutil.ReadAll(flate.NewReader(bytes.NewReader(compressedRequest))) if err != nil { return nil, fmt.Errorf("cannot decompress request: %s", err) } req.RelayState = r.URL.Query().Get("RelayState") case "POST": if err := r.ParseForm(); err != nil { return nil, err } var err error req.RequestBuffer, err = base64.StdEncoding.DecodeString(r.PostForm.Get("SAMLRequest")) if err != nil { return nil, err } req.RelayState = r.PostForm.Get("RelayState") default: return nil, fmt.Errorf("method not allowed") } return req, nil }
func (f *FetchServer) decodeResponse(resp *http.Response) (resp1 *http.Response, err error) { if resp.StatusCode != 200 { return resp, nil } var hdrLen uint16 if err = binary.Read(resp.Body, binary.BigEndian, &hdrLen); err != nil { return } hdrBuf := make([]byte, hdrLen) if _, err = io.ReadFull(resp.Body, hdrBuf); err != nil { return } resp1, err = http.ReadResponse(bufio.NewReader(flate.NewReader(bytes.NewReader(hdrBuf))), resp.Request) if err != nil { return } data, _ := ioutil.ReadAll(resp.Body) resp1.Body = httpproxy.NewMultiReadCloser(bytes.NewReader(data), resp.Body) return }
// Open returns a ReadCloser that provides access to the File's contents. func (f *File) Open() (rc io.ReadCloser, err os.Error) { off := int64(f.headerOffset) size := int64(f.CompressedSize) if f.bodyOffset == 0 { r := io.NewSectionReader(f.zipr, off, f.zipsize-off) if err = readFileHeader(f, r); err != nil { return } if f.bodyOffset, err = r.Seek(0, os.SEEK_CUR); err != nil { return } if size == 0 { size = int64(f.CompressedSize) } } if f.hasDataDescriptor() && size == 0 { // permit SectionReader to see the rest of the file size = f.zipsize - (off + f.bodyOffset) } r := io.NewSectionReader(f.zipr, off+f.bodyOffset, size) switch f.Method { case Store: // (no compression) rc = ioutil.NopCloser(r) case Deflate: rc = flate.NewReader(r) default: err = UnsupportedMethod } if rc != nil { rc = &checksumReader{rc, crc32.NewIEEE(), f, r} } return }
func (f *FetchServer) decodeResponse(resp *http.Response) (resp1 *http.Response, err error) { if resp.StatusCode != 200 { return resp, nil } var hdrLen uint16 if err = binary.Read(resp.Body, binary.BigEndian, &hdrLen); err != nil { return } hdrBuf := make([]byte, hdrLen) if _, err = io.ReadFull(resp.Body, hdrBuf); err != nil { return } resp1, err = http.ReadResponse(bufio.NewReader(flate.NewReader(bytes.NewReader(hdrBuf))), resp.Request) if err != nil { return } const cookieKey string = "Set-Cookie" if cookie := resp1.Header.Get(cookieKey); cookie != "" { parts := strings.Split(cookie, ", ") parts1 := make([]string, 0) for i := 0; i < len(parts); i++ { c := parts[i] if i == 0 || strings.Contains(strings.Split(c, ";")[0], "=") { parts1 = append(parts1, c) } else { parts1[len(parts1)-1] = parts1[len(parts1)-1] + ", " + c } } resp1.Header.Del(cookieKey) for i := 0; i < len(parts1); i++ { resp1.Header.Add(cookieKey, parts1[i]) } } if resp1.StatusCode >= 400 { switch { case resp.Body == nil: break case resp1.Body == nil: resp1.Body = resp.Body default: b, _ := ioutil.ReadAll(resp1.Body) if b != nil && len(b) > 0 { resp1.Body = httpproxy.NewMultiReadCloser(bytes.NewReader(b), resp.Body) } else { resp1.Body = resp.Body } } } else { resp1.Body = resp.Body } return }
func (c *Conn) parseXzver() (result []MessageOverview, err error) { // XZVER is a yenc stream… yencStream := ¥cReader{r: c.r} defer yencStream.Close() // containing a DEFLATE stream… flateStream := flate.NewReader(yencStream) defer flateStream.Close() // containing an overview stream… result, err = parseOverview(bufio.NewReader(flateStream)) if err == nil { // …with a dot at the end flateStream.Close() yencStream.Close() var line string line, err = c.r.ReadString('\n') if err == nil && strings.TrimRight(line, "\r\n") != "." { return nil, fmt.Errorf("unexpected data after XZVER: %q", line) } } return }
// 识别输入(文件名/网址)并提供弹幕数据 func identify(par string) (r io.ReadCloser, name string, err error) { if biliz.MatchString(par) { err := func() error { resp, err := http.Get(par) if err != nil { return err } defer resp.Body.Close() data, err := ioutil.ReadAll(resp.Body) if err != nil { return err } matched := addrs.FindSubmatch(data) if len(matched) == 0 { return errNoXML } par = string(matched[1]) + `.xml` matched = title.FindSubmatch(data) if len(matched) == 0 { return errNoTitle } name = string(matched[1]) + `.ass` resp, err = http.Get(`http://comment.bilibili.com/` + par) if err != nil { return err } defer resp.Body.Close() if resp.Header.Get("Content-Encoding") == "deflate" { resp.Body = flate.NewReader(resp.Body) defer resp.Body.Close() } file, err := os.Create(par) if err != nil { return err } defer file.Close() xml, err := ioutil.ReadAll(resp.Body) if err != nil { return err } file.Write(xml) return nil }() if err != nil { return nil, "", err } } else { if n := strings.LastIndex(par, "."); n < 0 { name = par + ".ass" } else { name = par[:n] + ".ass" } } file, err := os.Open(par) if err != nil { return nil, "", err } return file, name, nil }
func DeCompress(src []byte, length int) []byte { realSrc := src[:length] b := bytes.NewReader(realSrc) var out bytes.Buffer r := flate.NewReader(b) io.Copy(&out, r) return out.Bytes() }
func Uncompress(stream []byte) ([]byte, error) { stream = append(Dictionary, stream...) uncompressed, err := ioutil.ReadAll(flate.NewReader(bytes.NewBuffer(stream))) if err != nil { return nil, err } return uncompressed, nil }
func (alg *Deflate) Decompress(compressedText []byte) []byte { enflated, _ := ioutil.ReadAll( flate.NewReader( bytes.NewReader(compressedText))) return enflated }
func inflate(src []byte) []byte { r := bytes.NewReader(src) b, err := ioutil.ReadAll(flate.NewReader(r)) if err != nil { panic(err) } return b }