Simplify encoder interface.

This commit is contained in:
Andy Balholm 2019-03-09 16:06:37 -08:00
parent d6c7de28d1
commit 1ce1c68433
2 changed files with 6 additions and 56 deletions

View File

@ -136,29 +136,6 @@ func BrotliDecoderSetParameter(state *Reader, p int, value uint32) bool {
} }
} }
func BrotliDecoderCreateInstance() *Reader {
var state *Reader
state = new(Reader)
if state == nil {
return nil
}
if !BrotliDecoderStateInit(state) {
return nil
}
return state
}
/* Deinitializes and frees BrotliDecoderState instance. */
func BrotliDecoderDestroyInstance(state *Reader) {
if state == nil {
return
} else {
BrotliDecoderStateCleanup(state)
}
}
/* Saves error code and converts it to BrotliDecoderResult. */ /* Saves error code and converts it to BrotliDecoderResult. */
func SaveErrorCode(s *Reader, e int) int { func SaveErrorCode(s *Reader, e int) int {
s.error_code = int(e) s.error_code = int(e)
@ -2135,28 +2112,6 @@ func BrotliMaxDistanceSymbol(ndirect uint32, npostfix uint32) uint32 {
} }
} }
func BrotliDecoderDecompress(encoded_size uint, encoded_buffer []byte, decoded_size *uint, decoded_buffer []byte) int {
var s Reader
var result int
var total_out uint = 0
var available_in uint = encoded_size
var next_in []byte = encoded_buffer
var available_out uint = *decoded_size
var next_out []byte = decoded_buffer
if !BrotliDecoderStateInit(&s) {
return BROTLI_DECODER_RESULT_ERROR
}
result = BrotliDecoderDecompressStream(&s, &available_in, &next_in, &available_out, &next_out, &total_out)
*decoded_size = total_out
BrotliDecoderStateCleanup(&s)
if result != BROTLI_DECODER_RESULT_SUCCESS {
result = BROTLI_DECODER_RESULT_ERROR
}
return result
}
/* Invariant: input stream is never overconsumed: /* Invariant: input stream is never overconsumed:
- invalid input implies that the whole stream is invalid -> any amount of - invalid input implies that the whole stream is invalid -> any amount of
input could be read and discarded input could be read and discarded
@ -2168,15 +2123,10 @@ func BrotliDecoderDecompress(encoded_size uint, encoded_buffer []byte, decoded_s
buffer ahead of time buffer ahead of time
- when result is "success" decoder MUST return all unused data back to input - when result is "success" decoder MUST return all unused data back to input
buffer; this is possible because the invariant is held on enter */ buffer; this is possible because the invariant is held on enter */
func BrotliDecoderDecompressStream(s *Reader, available_in *uint, next_in *[]byte, available_out *uint, next_out *[]byte, total_out *uint) int { func BrotliDecoderDecompressStream(s *Reader, available_in *uint, next_in *[]byte, available_out *uint, next_out *[]byte) int {
var result int = BROTLI_DECODER_SUCCESS var result int = BROTLI_DECODER_SUCCESS
var br *BrotliBitReader = &s.br var br *BrotliBitReader = &s.br
/* Ensure that |total_out| is set, even if no data will ever be pushed out. */
if total_out != nil {
*total_out = s.partial_pos_out
}
/* Do not try to process further in a case of unrecoverable error. */ /* Do not try to process further in a case of unrecoverable error. */
if int(s.error_code) < 0 { if int(s.error_code) < 0 {
return BROTLI_DECODER_RESULT_ERROR return BROTLI_DECODER_RESULT_ERROR
@ -2209,7 +2159,7 @@ func BrotliDecoderDecompressStream(s *Reader, available_in *uint, next_in *[]byt
/* Error, needs more input/output. */ /* Error, needs more input/output. */
if result == BROTLI_DECODER_NEEDS_MORE_INPUT { if result == BROTLI_DECODER_NEEDS_MORE_INPUT {
if s.ringbuffer != nil { /* Pro-actively push output. */ if s.ringbuffer != nil { /* Pro-actively push output. */
var intermediate_result int = WriteRingBuffer(s, available_out, next_out, total_out, true) var intermediate_result int = WriteRingBuffer(s, available_out, next_out, nil, true)
/* WriteRingBuffer checks s->meta_block_remaining_len validity. */ /* WriteRingBuffer checks s->meta_block_remaining_len validity. */
if int(intermediate_result) < 0 { if int(intermediate_result) < 0 {
@ -2381,7 +2331,7 @@ func BrotliDecoderDecompressStream(s *Reader, available_in *uint, next_in *[]byt
s.state = BROTLI_STATE_HUFFMAN_CODE_0 s.state = BROTLI_STATE_HUFFMAN_CODE_0
case BROTLI_STATE_UNCOMPRESSED: case BROTLI_STATE_UNCOMPRESSED:
{ {
result = CopyUncompressedBlockToOutput(available_out, next_out, total_out, s) result = CopyUncompressedBlockToOutput(available_out, next_out, nil, s)
if result != BROTLI_DECODER_SUCCESS { if result != BROTLI_DECODER_SUCCESS {
break break
} }
@ -2609,7 +2559,7 @@ func BrotliDecoderDecompressStream(s *Reader, available_in *uint, next_in *[]byt
/* Fall through. */ /* Fall through. */
BROTLI_STATE_COMMAND_POST_WRITE_2: BROTLI_STATE_COMMAND_POST_WRITE_2:
result = WriteRingBuffer(s, available_out, next_out, total_out, false) result = WriteRingBuffer(s, available_out, next_out, nil, false)
if result != BROTLI_DECODER_SUCCESS { if result != BROTLI_DECODER_SUCCESS {
break break
@ -2674,7 +2624,7 @@ func BrotliDecoderDecompressStream(s *Reader, available_in *uint, next_in *[]byt
/* Fall through. */ /* Fall through. */
case BROTLI_STATE_DONE: case BROTLI_STATE_DONE:
if s.ringbuffer != nil { if s.ringbuffer != nil {
result = WriteRingBuffer(s, available_out, next_out, total_out, true) result = WriteRingBuffer(s, available_out, next_out, nil, true)
if result != BROTLI_DECODER_SUCCESS { if result != BROTLI_DECODER_SUCCESS {
break break
} }

View File

@ -49,7 +49,7 @@ func (r *Reader) Read(p []byte) (n int, err error) {
out_len := uint(len(p)) out_len := uint(len(p))
in_remaining := in_len in_remaining := in_len
out_remaining := out_len out_remaining := out_len
result := BrotliDecoderDecompressStream(r, &in_remaining, &r.in, &out_remaining, &p, nil) result := BrotliDecoderDecompressStream(r, &in_remaining, &r.in, &out_remaining, &p)
written = out_len - out_remaining written = out_len - out_remaining
n = int(written) n = int(written)