doc.odin 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. /*
  2. A small `GZIP` unpacker.
  3. Example:
  4. import "core:bytes"
  5. import "core:os"
  6. import "core:compress"
  7. import "core:fmt"
  8. // Small GZIP file with fextra, fname and fcomment present.
  9. @private
  10. TEST: []u8 = {
  11. 0x1f, 0x8b, 0x08, 0x1c, 0xcb, 0x3b, 0x3a, 0x5a,
  12. 0x02, 0x03, 0x07, 0x00, 0x61, 0x62, 0x03, 0x00,
  13. 0x63, 0x64, 0x65, 0x66, 0x69, 0x6c, 0x65, 0x6e,
  14. 0x61, 0x6d, 0x65, 0x00, 0x54, 0x68, 0x69, 0x73,
  15. 0x20, 0x69, 0x73, 0x20, 0x61, 0x20, 0x63, 0x6f,
  16. 0x6d, 0x6d, 0x65, 0x6e, 0x74, 0x00, 0x2b, 0x48,
  17. 0xac, 0xcc, 0xc9, 0x4f, 0x4c, 0x01, 0x00, 0x15,
  18. 0x6a, 0x2c, 0x42, 0x07, 0x00, 0x00, 0x00,
  19. }
  20. main :: proc() {
  21. // Set up output buffer.
  22. buf := bytes.Buffer{}
  23. stdout :: proc(s: string) {
  24. os.write_string(os.stdout, s)
  25. }
  26. stderr :: proc(s: string) {
  27. os.write_string(os.stderr, s)
  28. }
  29. args := os.args
  30. if len(args) < 2 {
  31. stderr("No input file specified.\n")
  32. err := load(data=TEST, buf=&buf, known_gzip_size=len(TEST))
  33. if err == nil {
  34. stdout("Displaying test vector: ")
  35. stdout(bytes.buffer_to_string(&buf))
  36. stdout("\n")
  37. } else {
  38. fmt.printf("gzip.load returned %v\n", err)
  39. }
  40. bytes.buffer_destroy(&buf)
  41. os.exit(0)
  42. }
  43. // The rest are all files.
  44. args = args[1:]
  45. err: Error
  46. for file in args {
  47. if file == "-" {
  48. // Read from stdin
  49. s := os.stream_from_handle(os.stdin)
  50. ctx := &compress.Context_Stream_Input{
  51. input = s,
  52. }
  53. err = load(ctx, &buf)
  54. } else {
  55. err = load(file, &buf)
  56. }
  57. if err != nil {
  58. if err != E_General.File_Not_Found {
  59. stderr("File not found: ")
  60. stderr(file)
  61. stderr("\n")
  62. os.exit(1)
  63. }
  64. stderr("GZIP returned an error.\n")
  65. bytes.buffer_destroy(&buf)
  66. os.exit(2)
  67. }
  68. stdout(bytes.buffer_to_string(&buf))
  69. }
  70. bytes.buffer_destroy(&buf)
  71. }
  72. */
  73. package compress_gzip
  74. /*
  75. Copyright 2021 Jeroen van Rijn <[email protected]>.
  76. Made available under Odin's BSD-3 license.
  77. List of contributors:
  78. Jeroen van Rijn: Initial implementation.
  79. Ginger Bill: Cosmetic changes.
  80. A small GZIP implementation as an example.
  81. */