embed.go 977 B

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. // License: GPLv3 Copyright: 2023, Kovid Goyal, <kovid at kovidgoyal.net>
  2. package utils
  3. import (
  4. "bytes"
  5. "compress/bzip2"
  6. "encoding/binary"
  7. "fmt"
  8. "io"
  9. )
  10. var _ = fmt.Print
  11. func ReadAll(r io.Reader, expected_size int) ([]byte, error) {
  12. b := make([]byte, 0, expected_size)
  13. for {
  14. if len(b) == cap(b) {
  15. // Add more capacity (let append pick how much).
  16. b = append(b, 0)[:len(b)]
  17. }
  18. n, err := r.Read(b[len(b):cap(b)])
  19. b = b[:len(b)+n]
  20. if err != nil {
  21. if err == io.EOF {
  22. err = nil
  23. }
  24. return b, err
  25. }
  26. }
  27. }
  28. func ReadCompressedEmbeddedData(raw string) []byte {
  29. compressed := UnsafeStringToBytes(raw)
  30. uncompressed_size := binary.LittleEndian.Uint32(compressed)
  31. r := bzip2.NewReader(bytes.NewReader(compressed[4:]))
  32. ans, err := ReadAll(r, int(uncompressed_size))
  33. if err != nil {
  34. panic(err)
  35. }
  36. return ans
  37. }
  38. func ReaderForCompressedEmbeddedData(raw string) io.Reader {
  39. return bzip2.NewReader(bytes.NewReader(UnsafeStringToBytes(raw)[4:]))
  40. }