zip_parser.cpp 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  1. /**
  2. * ZuluSCSI™ - Copyright (c) 2024 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. #include "zip_parser.h"
  22. #define ZIP_PARSER_METHOD_DEFLATE_BYTE 0x08
  23. #define ZIP_PARSER_METHOD_UNCOMPRESSED_BYTE 0x00
  24. namespace zipparser
  25. {
  26. Parser::Parser()
  27. {
  28. filename_len = 0;
  29. Reset();
  30. }
  31. Parser::Parser(char const *filename, const size_t length)
  32. {
  33. Reset();
  34. SetMatchingFilename(filename, length);
  35. }
  36. void Parser::Reset()
  37. {
  38. target = parsing_target::signature;
  39. position = 0;
  40. filename_match = false;
  41. crc = 0;
  42. }
  43. void Parser::SetMatchingFilename(char const *filename, const size_t length)
  44. {
  45. if (filename[0] == '\0')
  46. filename_len = 0;
  47. else
  48. {
  49. this->filename = filename;
  50. filename_len = length;
  51. }
  52. }
  53. int32_t Parser::Parse(uint8_t const *buf, const size_t size)
  54. {
  55. if (filename_len == 0)
  56. return PARSE_ERROR;
  57. static bool matching = true;
  58. static bool central_dir = false;
  59. static bool local_file_header = false;
  60. for (size_t idx = 0; idx < size; idx++)
  61. {
  62. switch (target)
  63. {
  64. case parsing_target::signature:
  65. if (++position == 1 && buf[idx] == 'P')
  66. break;
  67. if (position == 2 && buf[idx] == 'K')
  68. {
  69. local_file_header = false;
  70. central_dir = false;
  71. break;
  72. }
  73. if (position == 3 && buf[idx] == 0x03)
  74. {
  75. local_file_header = true;
  76. break;
  77. }
  78. if (position == 3 && buf[idx] == 0x01)
  79. {
  80. central_dir = true;
  81. break;
  82. }
  83. if (central_dir && position == 4 && buf[idx] == 0x2)
  84. {
  85. return PARSE_CENTRAL_DIR;
  86. }
  87. if (local_file_header && position == 4 && buf[idx] == 0x04)
  88. {
  89. position = 0;
  90. target = parsing_target::version;
  91. break;
  92. }
  93. return PARSE_ERROR;
  94. break;
  95. case parsing_target::version:
  96. if (++position == 2)
  97. {
  98. position = 0;
  99. target = parsing_target::flag;
  100. }
  101. break;
  102. case parsing_target::flag:
  103. if (++position == 2)
  104. {
  105. position = 0;
  106. target = parsing_target::method;
  107. }
  108. break;
  109. case parsing_target::method:
  110. if (++position == 1)
  111. {
  112. // Currently only uncompresseed files in the zip package are supported
  113. if (!buf[idx] == ZIP_PARSER_METHOD_UNCOMPRESSED_BYTE)
  114. {
  115. return PARSE_UNSUPPORTED_COMPRESSION;
  116. }
  117. }
  118. if (position == 2)
  119. {
  120. if (buf[idx] == 0)
  121. {
  122. position = 0;
  123. target = parsing_target::modify_time;
  124. }
  125. else
  126. return PARSE_UNSUPPORTED_COMPRESSION;
  127. }
  128. break;
  129. case parsing_target::modify_time:
  130. if (++position == 2)
  131. {
  132. position = 0;
  133. target = parsing_target::modify_date;
  134. }
  135. break;
  136. case parsing_target::modify_date:
  137. if (++position == 2)
  138. {
  139. position = 0;
  140. target = parsing_target::crc;
  141. crc = 0;
  142. }
  143. break;
  144. case parsing_target::crc:
  145. crc |= buf[idx] << (8 * position++);
  146. if (position == 4)
  147. {
  148. target = parsing_target::size_compressed;
  149. compressed_data_size = 0;
  150. position = 0;
  151. }
  152. break;
  153. case parsing_target::size_compressed:
  154. compressed_data_size |= buf[idx] << (8 * position++);
  155. if (position == 4)
  156. {
  157. target = parsing_target::size_uncompressed;
  158. uncompressed_data_size = 0;
  159. position = 0;
  160. }
  161. break;
  162. case parsing_target::size_uncompressed:
  163. uncompressed_data_size |= buf[idx] << (8 * position++);
  164. if (position == 4)
  165. {
  166. target = parsing_target::filename_len;
  167. current_zip_filename_len = 0;
  168. position = 0;
  169. }
  170. break;
  171. case parsing_target::filename_len:
  172. current_zip_filename_len |= buf[idx] << (8 * position++);
  173. if (position == 2)
  174. {
  175. target = parsing_target::extra_field_len;
  176. extra_field_len = 0;
  177. position = 0;
  178. }
  179. break;
  180. case parsing_target::extra_field_len:
  181. extra_field_len |= buf[idx] << (8 * position++);
  182. if (position == 2)
  183. {
  184. target = parsing_target::filename;
  185. position = 0;
  186. filename_match = false;
  187. matching = true;
  188. }
  189. break;
  190. case parsing_target::filename:
  191. if (position <= current_zip_filename_len - 1)
  192. {
  193. if (matching && position < filename_len && filename[position] != buf[idx])
  194. matching = false;
  195. if (position == filename_len - 1 && matching)
  196. filename_match = true;
  197. if (position == current_zip_filename_len -1)
  198. {
  199. target = parsing_target::extra_field;
  200. matching = true;
  201. position = 0;
  202. if (extra_field_len == 0)
  203. {
  204. target = parsing_target::end;
  205. return idx + 1;
  206. }
  207. }
  208. else
  209. position++;
  210. }
  211. break;
  212. case parsing_target::extra_field:
  213. // extra_field_len should be at least 1 by this time
  214. if (++position == extra_field_len)
  215. {
  216. target = parsing_target::end;
  217. return idx + 1;
  218. }
  219. break;
  220. case parsing_target::end:
  221. return 0;
  222. break;
  223. }
  224. }
  225. return size;
  226. }
  227. bool Parser::FoundMatch()
  228. {
  229. return filename_match;
  230. }
  231. }