gzip_header.h 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. // Copyright 2014 The Chromium Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style license that can be
  3. // found in the LICENSE file.
  4. // The GZipHeader class allows you to parse a gzip header, such as you
  5. // might find at the beginning of a file compressed by gzip (ie, a .gz
  6. // file), or at the beginning of an HTTP response that uses a gzip
  7. // Content-Encoding. See RFC 1952 for the specification for the gzip
  8. // header.
  9. //
  10. // The model is that you call ReadMore() for each chunk of bytes
  11. // you've read from a file or socket.
  12. //
  13. #ifndef NET_FILTER_GZIP_HEADER_H_
  14. #define NET_FILTER_GZIP_HEADER_H_
  15. #include <stddef.h>
  16. #include <stdint.h>
  17. #include "net/base/net_export.h"
  18. namespace net {
  19. class NET_EXPORT GZipHeader {
  20. public:
  21. enum Status {
  22. INCOMPLETE_HEADER, // don't have all the bits yet...
  23. COMPLETE_HEADER, // complete, valid header
  24. INVALID_HEADER, // found something invalid in the header
  25. };
  26. GZipHeader();
  27. GZipHeader(const GZipHeader&) = delete;
  28. GZipHeader& operator=(const GZipHeader&) = delete;
  29. ~GZipHeader();
  30. // Wipe the slate clean and start from scratch.
  31. void Reset();
  32. // Attempt to parse the given buffer as the next installment of
  33. // bytes from a gzip header. If the bytes we've seen so far do not
  34. // yet constitute a complete gzip header, return
  35. // INCOMPLETE_HEADER. If these bytes do not constitute a *valid*
  36. // gzip header, return INVALID_HEADER. When we've seen a complete
  37. // gzip header, return COMPLETE_HEADER and set the pointer pointed
  38. // to by header_end to the first byte beyond the gzip header.
  39. Status ReadMore(const char* inbuf, size_t inbuf_len, const char** header_end);
  40. private:
  41. enum { // flags (see RFC)
  42. FLAG_FTEXT = 0x01, // bit 0 set: file probably ascii text
  43. FLAG_FHCRC = 0x02, // bit 1 set: header CRC present
  44. FLAG_FEXTRA = 0x04, // bit 2 set: extra field present
  45. FLAG_FNAME = 0x08, // bit 3 set: original file name present
  46. FLAG_FCOMMENT = 0x10, // bit 4 set: file comment present
  47. FLAG_RESERVED = 0xE0, // bits 5..7: reserved
  48. };
  49. enum State {
  50. // The first 10 bytes are the fixed-size header:
  51. IN_HEADER_ID1,
  52. IN_HEADER_ID2,
  53. IN_HEADER_CM,
  54. IN_HEADER_FLG,
  55. IN_HEADER_MTIME_BYTE_0,
  56. IN_HEADER_MTIME_BYTE_1,
  57. IN_HEADER_MTIME_BYTE_2,
  58. IN_HEADER_MTIME_BYTE_3,
  59. IN_HEADER_XFL,
  60. IN_HEADER_OS,
  61. IN_XLEN_BYTE_0,
  62. IN_XLEN_BYTE_1,
  63. IN_FEXTRA,
  64. IN_FNAME,
  65. IN_FCOMMENT,
  66. IN_FHCRC_BYTE_0,
  67. IN_FHCRC_BYTE_1,
  68. IN_DONE,
  69. };
  70. static const uint8_t magic[]; // gzip magic header
  71. int state_; // our current State in the parsing FSM: an int so we can ++
  72. uint8_t flags_; // the flags byte of the header ("FLG" in the RFC)
  73. uint16_t extra_length_; // how much of the "extra field" we have yet to read
  74. };
  75. } // namespace net
  76. #endif // NET_FILTER_GZIP_HEADER_H_