zip_parser.h 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. /**
  2. * ZuluSCSI™ - Copyright (c) 2024-2025 Rabbit Hole Computing™
  3. *
  4. * ZuluSCSI™ firmware is licensed under the GPL version 3 or any later version. 
  5. *
  6. * https://www.gnu.org/licenses/gpl-3.0.html
  7. * ----
  8. * This program is free software: you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License as published by
  10. * the Free Software Foundation, either version 3 of the License, or
  11. * (at your option) any later version. 
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  16. * GNU General Public License for more details. 
  17. *
  18. * You should have received a copy of the GNU General Public License
  19. * along with this program.  If not, see <https://www.gnu.org/licenses/>.
  20. **/
  21. #pragma once
  22. #include <stdint.h>
  23. #include <strings.h>
  24. namespace zipparser
  25. {
  26. enum class parsing_target {signature, version, flag, method, modify_time, modify_date,
  27. crc, size_compressed, size_uncompressed, filename_len,
  28. extra_field_len, filename, extra_field, end};
  29. class Parser
  30. {
  31. public:
  32. Parser();
  33. Parser(char const *filename, const size_t length, const size_t target_total_length);
  34. void SetMatchingFilename(char const *filename, const size_t length, const size_t target_total_length);
  35. void Reset();
  36. static const int32_t PARSE_ERROR = -1;
  37. static const int32_t PARSE_CENTRAL_DIR = -2;
  38. static const int32_t PARSE_UNSUPPORTED_COMPRESSION = -3;
  39. // A state machine that per byte processes the incoming buffer
  40. // \param buf a pointer to binary data to be processed
  41. // \param size of data to be processed, can by 1 for a single byte at a time
  42. // \returns the number of bytes processed or -1 if an error ocurred
  43. int32_t Parse(uint8_t const *buf, const size_t size);
  44. bool FoundMatch();
  45. inline uint32_t GetCompressedSize() {return compressed_data_size;}
  46. protected:
  47. bool filename_match;
  48. char const *filename;
  49. size_t filename_len;
  50. size_t current_zip_filename_len;
  51. size_t target_zip_filename_len;
  52. size_t extra_field_len;
  53. uint32_t compressed_data_size;
  54. uint32_t uncompressed_data_size;
  55. parsing_target target;
  56. size_t position;
  57. uint32_t crc;
  58. };
  59. }