@@ -24,6 +24,15 @@ type Ctx interface {
2424 // prevent allocation. If it is too small, or if nil is passed, a new buffer
2525 // will be allocated and returned.
2626 Decompress (dst , src []byte ) ([]byte , error )
27+
28+ // DecompressInto decompresses src into dst. Unlike Decompress, DecompressInto
29+ // requires that dst be sufficiently large to hold the decompressed payload.
30+ // DecompressInto may be used when the caller knows the size of the decompressed
31+ // payload before attempting decompression.
32+ //
33+ // It returns the number of bytes copied and an error if any is encountered. If
34+ // dst is too small, DecompressInto errors.
35+ DecompressInto (dst , src []byte ) (int , error )
2736}
2837
2938type ctx struct {
@@ -104,14 +113,7 @@ func (c *ctx) Decompress(dst, src []byte) ([]byte, error) {
104113 dst = make ([]byte , bound )
105114 }
106115
107- written := int (C .ZSTD_decompressDCtx (
108- c .dctx ,
109- unsafe .Pointer (& dst [0 ]),
110- C .size_t (len (dst )),
111- unsafe .Pointer (& src [0 ]),
112- C .size_t (len (src ))))
113-
114- err := getError (written )
116+ written , err := c .DecompressInto (dst , src )
115117 if err == nil {
116118 return dst [:written ], nil
117119 }
@@ -125,6 +127,17 @@ func (c *ctx) Decompress(dst, src []byte) ([]byte, error) {
125127 return ioutil .ReadAll (r )
126128}
127129
130+ func (c * ctx ) DecompressInto (dst , src []byte ) (int , error ) {
131+ written := int (C .ZSTD_decompressDCtx (
132+ c .dctx ,
133+ unsafe .Pointer (& dst [0 ]),
134+ C .size_t (len (dst )),
135+ unsafe .Pointer (& src [0 ]),
136+ C .size_t (len (src ))))
137+ err := getError (written )
138+ return written , err
139+ }
140+
128141func finalizeCtx (c * ctx ) {
129142 C .ZSTD_freeCCtx (c .cctx )
130143 C .ZSTD_freeDCtx (c .dctx )
0 commit comments