reader.go 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. package brotli
  2. import (
  3. "errors"
  4. "io"
  5. )
  6. type decodeError int
  7. func (err decodeError) Error() string {
  8. return "brotli: " + string(decoderErrorString(int(err)))
  9. }
  10. var errExcessiveInput = errors.New("brotli: excessive input")
  11. var errInvalidState = errors.New("brotli: invalid state")
  12. // readBufSize is a "good" buffer size that avoids excessive round-trips
  13. // between C and Go but doesn't waste too much memory on buffering.
  14. // It is arbitrarily chosen to be equal to the constant used in io.Copy.
  15. const readBufSize = 32 * 1024
  16. // NewReader creates a new Reader reading the given reader.
  17. func NewReader(src io.Reader) *Reader {
  18. r := new(Reader)
  19. r.Reset(src)
  20. return r
  21. }
  22. // Reset discards the Reader's state and makes it equivalent to the result of
  23. // its original state from NewReader, but reading from src instead.
  24. // This permits reusing a Reader rather than allocating a new one.
  25. // Error is always nil
  26. func (r *Reader) Reset(src io.Reader) error {
  27. if r.error_code < 0 {
  28. // There was an unrecoverable error, leaving the Reader's state
  29. // undefined. Clear out everything but the buffer.
  30. *r = Reader{buf: r.buf}
  31. }
  32. decoderStateInit(r)
  33. r.src = src
  34. if r.buf == nil {
  35. r.buf = make([]byte, readBufSize)
  36. }
  37. return nil
  38. }
  39. func (r *Reader) Read(p []byte) (n int, err error) {
  40. if !decoderHasMoreOutput(r) && len(r.in) == 0 {
  41. m, readErr := r.src.Read(r.buf)
  42. if m == 0 {
  43. // If readErr is `nil`, we just proxy underlying stream behavior.
  44. return 0, readErr
  45. }
  46. r.in = r.buf[:m]
  47. }
  48. if len(p) == 0 {
  49. return 0, nil
  50. }
  51. for {
  52. var written uint
  53. in_len := uint(len(r.in))
  54. out_len := uint(len(p))
  55. in_remaining := in_len
  56. out_remaining := out_len
  57. result := decoderDecompressStream(r, &in_remaining, &r.in, &out_remaining, &p)
  58. written = out_len - out_remaining
  59. n = int(written)
  60. switch result {
  61. case decoderResultSuccess:
  62. if len(r.in) > 0 {
  63. return n, errExcessiveInput
  64. }
  65. return n, nil
  66. case decoderResultError:
  67. return n, decodeError(decoderGetErrorCode(r))
  68. case decoderResultNeedsMoreOutput:
  69. if n == 0 {
  70. return 0, io.ErrShortBuffer
  71. }
  72. return n, nil
  73. case decoderNeedsMoreInput:
  74. }
  75. if len(r.in) != 0 {
  76. return 0, errInvalidState
  77. }
  78. // Calling r.src.Read may block. Don't block if we have data to return.
  79. if n > 0 {
  80. return n, nil
  81. }
  82. // Top off the buffer.
  83. encN, err := r.src.Read(r.buf)
  84. if encN == 0 {
  85. // Not enough data to complete decoding.
  86. if err == io.EOF {
  87. return 0, io.ErrUnexpectedEOF
  88. }
  89. return 0, err
  90. }
  91. r.in = r.buf[:encN]
  92. }
  93. }