lha_input_stream.c 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403
  1. /*
  2. Copyright (c) 2011, 2012, Simon Howard
  3. Permission to use, copy, modify, and/or distribute this software
  4. for any purpose with or without fee is hereby granted, provided
  5. that the above copyright notice and this permission notice appear
  6. in all copies.
  7. THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL
  8. WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED
  9. WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE
  10. AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR
  11. CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
  12. LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT,
  13. NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
  14. CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  15. */
  16. #include <stdlib.h>
  17. #include <string.h>
  18. #include <ctype.h>
  19. #include <errno.h>
  20. #include "lha_arch.h"
  21. #include "lha_input_stream.h"
  22. // Maximum length of the self-extractor header.
  23. // If we don't find an LHA file header after this many bytes, give up.
  24. #define MAX_SFX_HEADER_LEN 65536
  25. // Size of the lead-in buffer used to skip the self-extractor.
  26. #define LEADIN_BUFFER_LEN 24
  27. // Magic string to detect an Amiga LhASFX self-extracting file.
  28. // This type of self-extractor is special because the program itself
  29. // contains a mini-LHA file that must be skipped over to get to
  30. // the real one.
  31. #define AMIGA_LHASFX_ID "LhASFX V1.2,"
  32. typedef enum {
  33. LHA_INPUT_STREAM_INIT,
  34. LHA_INPUT_STREAM_READING,
  35. LHA_INPUT_STREAM_FAIL
  36. } LHAInputStreamState;
  37. struct _LHAInputStream {
  38. const LHAInputStreamType *type;
  39. void *handle;
  40. LHAInputStreamState state;
  41. uint8_t leadin[LEADIN_BUFFER_LEN];
  42. size_t leadin_len;
  43. };
  44. LHAInputStream *lha_input_stream_new(const LHAInputStreamType *type,
  45. void *handle)
  46. {
  47. LHAInputStream *result;
  48. result = calloc(1, sizeof(LHAInputStream));
  49. if (result == NULL) {
  50. return NULL;
  51. }
  52. result->type = type;
  53. result->handle = handle;
  54. result->leadin_len = 0;
  55. result->state = LHA_INPUT_STREAM_INIT;
  56. return result;
  57. }
  58. void lha_input_stream_free(LHAInputStream *stream)
  59. {
  60. // Close the input stream.
  61. if (stream->type->close != NULL) {
  62. stream->type->close(stream->handle);
  63. }
  64. free(stream);
  65. }
  66. // Check if the specified buffer is the start of a file header.
  67. static int file_header_match(uint8_t *buf)
  68. {
  69. if (buf[2] != '-' || buf[6] != '-') {
  70. return 0;
  71. }
  72. // LHA algorithm?
  73. if (buf[3] == 'l' && buf[4] == 'h') {
  74. return 1;
  75. }
  76. // LArc algorithm (lz4, lz5, lzs)?
  77. if (buf[3] == 'l' && buf[4] == 'z'
  78. && (buf[5] == '4' || buf[5] == '5' || buf[5] == 's')) {
  79. return 1;
  80. }
  81. // PMarc algorithm? (pm0, pm2)
  82. // Note: PMarc SFX archives have a -pms- string in them that must
  83. // be ignored.
  84. if (buf[3] == 'p' && buf[4] == 'm' && buf[5] != 's') {
  85. return 1;
  86. }
  87. return 0;
  88. }
  89. // Empty some of the bytes from the start of the lead-in buffer.
  90. static void empty_leadin(LHAInputStream *stream, size_t bytes)
  91. {
  92. memmove(stream->leadin, stream->leadin + bytes,
  93. stream->leadin_len - bytes);
  94. stream->leadin_len -= bytes;
  95. }
  96. // Read bytes from the input stream into the specified buffer.
  97. static int do_read(LHAInputStream *stream, void *buf, size_t buf_len)
  98. {
  99. return stream->type->read(stream->handle, buf, buf_len);
  100. }
  101. // Skip the self-extractor header at the start of the file.
  102. // Returns non-zero if a header was found.
  103. static int skip_sfx(LHAInputStream *stream)
  104. {
  105. size_t filepos;
  106. unsigned int i;
  107. int skip_files;
  108. int read;
  109. filepos = 0;
  110. skip_files = 0;
  111. while (filepos < MAX_SFX_HEADER_LEN) {
  112. // Add some more bytes to the lead-in buffer:
  113. read = do_read(stream, stream->leadin + stream->leadin_len,
  114. LEADIN_BUFFER_LEN - stream->leadin_len);
  115. if (read <= 0) {
  116. break;
  117. }
  118. stream->leadin_len += (unsigned int) read;
  119. // Check the lead-in buffer for a file header.
  120. for (i = 0; i + 12 < stream->leadin_len; ++i) {
  121. if (file_header_match(stream->leadin + i)) {
  122. if (skip_files == 0) {
  123. empty_leadin(stream, i);
  124. return 1;
  125. } else {
  126. --skip_files;
  127. }
  128. }
  129. // Detect Amiga self-extractor.
  130. if (!memcmp(stream->leadin + i, AMIGA_LHASFX_ID,
  131. strlen(AMIGA_LHASFX_ID))) {
  132. skip_files = 1;
  133. }
  134. }
  135. empty_leadin(stream, i);
  136. filepos += i;
  137. }
  138. return 0;
  139. }
  140. int lha_input_stream_read(LHAInputStream *stream, void *buf, size_t buf_len)
  141. {
  142. size_t total_bytes, n;
  143. int result;
  144. // Start of the stream? Skip self-extract header, if there is one.
  145. if (stream->state == LHA_INPUT_STREAM_INIT) {
  146. if (skip_sfx(stream)) {
  147. stream->state = LHA_INPUT_STREAM_READING;
  148. } else {
  149. stream->state = LHA_INPUT_STREAM_FAIL;
  150. }
  151. }
  152. if (stream->state == LHA_INPUT_STREAM_FAIL) {
  153. return 0;
  154. }
  155. // Now fill the result buffer. Start by emptying the lead-in buffer.
  156. total_bytes = 0;
  157. if (stream->leadin_len > 0) {
  158. if (buf_len < stream->leadin_len) {
  159. n = buf_len;
  160. } else {
  161. n = stream->leadin_len;
  162. }
  163. memcpy(buf, stream->leadin, n);
  164. empty_leadin(stream, n);
  165. total_bytes += n;
  166. }
  167. // Read from the input stream.
  168. if (total_bytes < buf_len) {
  169. result = do_read(stream, (uint8_t *) buf + total_bytes,
  170. buf_len - total_bytes);
  171. if (result > 0) {
  172. total_bytes += (unsigned int) result;
  173. }
  174. }
  175. // Only successful if the complete buffer is filled.
  176. return total_bytes == buf_len;
  177. }
  178. int lha_input_stream_skip(LHAInputStream *stream, size_t bytes)
  179. {
  180. // If we have a dedicated skip function, use it; otherwise,
  181. // the read function can be used to perform a skip.
  182. if (stream->type->skip != NULL) {
  183. return stream->type->skip(stream->handle, bytes);
  184. } else {
  185. uint8_t data[32];
  186. unsigned int len;
  187. int result;
  188. while (bytes > 0) {
  189. // Read as many bytes left as possible to fit in
  190. // the buffer:
  191. if (bytes > sizeof(data)) {
  192. len = sizeof(data);
  193. } else {
  194. len = bytes;
  195. }
  196. result = do_read(stream, data, len);
  197. if (result < 0) {
  198. return 0;
  199. }
  200. bytes -= (unsigned int) result;
  201. }
  202. return 1;
  203. }
  204. }
  205. // Read data from a FILE * source.
  206. static int file_source_read(void *handle, void *buf, size_t buf_len)
  207. {
  208. size_t bytes_read;
  209. FILE *fh = handle;
  210. bytes_read = fread(buf, 1, buf_len, fh);
  211. // If an error occurs, zero is returned; however, it may also
  212. // indicate end of file.
  213. if (bytes_read == 0 && !feof(fh)) {
  214. return -1;
  215. }
  216. return (int) bytes_read;
  217. }
  218. // "Fallback" skip for file source that uses fread(), for unseekable
  219. // streams.
  220. static int file_source_skip_fallback(FILE *handle, size_t bytes)
  221. {
  222. uint8_t data[32];
  223. unsigned int len;
  224. int result;
  225. while (bytes > 0) {
  226. if (bytes > sizeof(data)) {
  227. len = sizeof(data);
  228. } else {
  229. len = bytes;
  230. }
  231. result = fread(data, 1, len, handle);
  232. if (result != (int) len) {
  233. return 0;
  234. }
  235. bytes -= len;
  236. }
  237. return 1;
  238. }
  239. // Seek forward in a FILE * input stream.
  240. static int file_source_skip(void *handle, size_t bytes)
  241. {
  242. int result;
  243. // If this is an unseekable stream of some kind, always use the
  244. // fallback behavior, as at least this is guaranteed to work.
  245. // This is to work around problems on Windows, where fseek() can
  246. // seek half-way on a stream and *then* fail, leaving us in an
  247. // unworkable situation.
  248. if (ftell(handle) < 0) {
  249. return file_source_skip_fallback(handle, bytes);
  250. }
  251. result = fseek(handle, (long) bytes, SEEK_CUR);
  252. if (result < 0) {
  253. if (errno == EBADF || errno == ESPIPE) {
  254. return file_source_skip_fallback(handle, bytes);
  255. } else {
  256. return 0;
  257. }
  258. }
  259. return 1;
  260. }
  261. // Close a FILE * input stream.
  262. static void file_source_close(void *handle)
  263. {
  264. fclose(handle);
  265. }
  266. // "Owned" file source - the stream will be closed when the input
  267. // stream is freed.
  268. static const LHAInputStreamType file_source_owned = {
  269. file_source_read,
  270. file_source_skip,
  271. file_source_close
  272. };
  273. // "Unowned" file source - the stream is owned by the calling code.
  274. static const LHAInputStreamType file_source_unowned = {
  275. file_source_read,
  276. file_source_skip,
  277. NULL
  278. };
  279. LHAInputStream *lha_input_stream_from(char *filename)
  280. {
  281. LHAInputStream *result;
  282. FILE *fstream;
  283. fstream = fopen(filename, "rb");
  284. if (fstream == NULL) {
  285. return NULL;
  286. }
  287. result = lha_input_stream_new(&file_source_owned, fstream);
  288. if (result == NULL) {
  289. fclose(fstream);
  290. }
  291. return result;
  292. }
  293. LHAInputStream *lha_input_stream_from_FILE(FILE *stream)
  294. {
  295. lha_arch_set_binary(stream);
  296. return lha_input_stream_new(&file_source_unowned, stream);
  297. }