macbinary.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451
  1. /*
  2. Copyright (c) 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. // Code for handling MacBinary headers.
  17. //
  18. // Classic Mac OS attaches more metadata to files than other operating
  19. // systems. For example, each file has a file type that is used to
  20. // determine the application to open it with. Files can also have both
  21. // a data fork and a resource fork. Because of this design, when
  22. // transferring a file between computers (eg. over a network), all of
  23. // the data associated with the file must be bundled up together to
  24. // preserve the file.
  25. //
  26. // MacLHA uses the MacBinary container format to do this. Within the
  27. // compressed data, the file contents are preceded by a 128 byte
  28. // header that contains the metadata. The data from the data fork can
  29. // also be followed by the data from the resource fork.
  30. //
  31. // Because this is incompatible with .lzh archives from other operating
  32. // systems, MacLHA has two menu items to create new archives - one
  33. // creates a "Mac" archive, while the other creates a "non-Mac"
  34. // (standard) archive that contains just the file contents. This quote
  35. // from the documentation (MacLHAE.doc) describes what is stored when
  36. // the latter option is used:
  37. //
  38. // > If a file has only either Data Fork or Resource Fork, it's stored
  39. // > into archives. In case a file has both Data Fork and Resource Fork,
  40. // > only the Data Fork is stored.
  41. //
  42. // --
  43. //
  44. // Mac OS X has essentially abandoned this practise of using filesystem
  45. // metadata and other systems do not use it, either. It is therefore
  46. // sensible and desirable to strip off the MacBinary header (if present)
  47. // and extract just the normal file contents. It makes sense to use the
  48. // same strategy quoted above.
  49. //
  50. // The possible presence of a MacBinary header can be inferred using the
  51. // OS type field from the LHA header - a value of 'm' indicates that it
  52. // was generated by MacLHA. However, there are some issues with this:
  53. //
  54. // 1. This type is set regardless of whether a MacBinary header is
  55. // attached or not. There is no other field to indicate the
  56. // difference, and MacBinary headers do not have a magic number, so
  57. // the presence of one must be determined heuristically.
  58. // Realistically, though, this can be done without too much
  59. // difficulty, by strictly checking all the fields in the MacBinary
  60. // header. If an invalid header is seen, it can be rejected and
  61. // assumed to be a normal file.
  62. //
  63. // 2. MacBinary is a standard container format for transferring files
  64. // between Macs and not used just by MacLHA. Therefore, it is
  65. // plausible that a .lzh archive might "deliberately" contain a
  66. // MacBinary file, in which case it would be a mistake to strip
  67. // off the header.
  68. //
  69. // This is an unlikely but still a plausible scenario. It can be
  70. // mitigated by comparing the MacBinary header values against the
  71. // values from the .lzh header. A header added by MacLHA will have
  72. // a filename that matches the .lzh header's filename (MacBinary
  73. // files usually have a .bin extension appended, so the filenames
  74. // would not match. Also, the modification timestamp should match
  75. // the timestamp from the .lzh header.
  76. //
  77. // 3. Comparing the MacBinary header modification timestamp with the
  78. // .lzh header modification timestamp is complicated by the fact
  79. // that the former is stored as a Macintosh 1904-based timestamp
  80. // in the local timezone, while the latter is stored as a Unix
  81. // timestamp in UTC time. Although converting timestamp formats
  82. // is trivial, the two do not compare exactly due to the timezone
  83. // offset.
  84. //
  85. // --
  86. //
  87. // Summary of MacBinary header fields and policy for each
  88. // (Z = check zero, C = check value, I = ignore):
  89. //
  90. // 0x00 - Z - "Old version number", must be zero for compatibility
  91. // 0x01 - C - Filename length, must match .lzh header filename.
  92. // 0x02-0x40 - C - Filename, must match .lzh header filename.
  93. // Z - Remainder following filename contents must be zero
  94. // 0x41-0x44 - I - File type
  95. // 0x45-0x48 - I - File creator
  96. // 0x49 - I - Finder flags
  97. // 0x4a - Z - "Must be zero for compatibility"
  98. // 0x4b-0x4c - I - Icon vertical position
  99. // 0x4d-0x4e - I - Icon horizonal position
  100. // 0x4f-0x50 - I - Window ID
  101. // 0x51 - I - "Protected" flag
  102. // 0x52 - Z - "Must be zero for compatibility"
  103. // 0x53-0x56 - C - Data fork length }- added together, equal uncompressed
  104. // 0x57-0x5a - C - Resource fork length }- data length rounded up to 256
  105. // 0x5b-0x5e - I - File creation date
  106. // 0x5f-0x62 - C - File modification date - should match .lzh header
  107. // 0x63-0x64 - Z - Finder "Get Info" comment length - unused by MacLHA
  108. // 0x65-0x7f - Z - MacBinary II data - unused by MacLHA
  109. #include <stdlib.h>
  110. #include <string.h>
  111. #include "lha_decoder.h"
  112. #include "lha_endian.h"
  113. #include "lha_file_header.h"
  114. #define OUTPUT_BUFFER_SIZE 4096 /* bytes */
  115. // Classic Mac OS represents time in seconds since 1904, instead of
  116. // Unix time's 1970 epoch. This is the difference between the two.
  117. #define MAC_TIME_OFFSET 2082844800 /* seconds */
  118. // Size of the MacBinary header.
  119. #define MBHDR_SIZE 128 /* bytes */
  120. // Offsets of fields in MacBinary header (and their sizes):
  121. #define MBHDR_OFF_VERSION 0x00
  122. #define MBHDR_OFF_FILENAME_LEN 0x01
  123. #define MBHDR_OFF_FILENAME 0x02
  124. #define MBHDR_LEN_FILENAME 63
  125. #define MBHDR_OFF_ZERO_COMPAT1 0x4a
  126. #define MBHDR_OFF_ZERO_COMPAT2 0x52
  127. #define MBHDR_OFF_DATA_FORK_LEN 0x53
  128. #define MBHDR_OFF_RES_FORK_LEN 0x57
  129. #define MBHDR_OFF_FILE_MOD_DATE 0x5f
  130. #define MBHDR_OFF_COMMENT_LEN 0x63
  131. #define MBHDR_OFF_MACBINARY2_DATA 0x65
  132. #define MBHDR_LEN_MACBINARY2_DATA (MBHDR_SIZE - MBHDR_OFF_MACBINARY2_DATA)
  133. // Check that the given block of data contains only zero bytes.
  134. static int block_is_zero(uint8_t *data, size_t data_len)
  135. {
  136. unsigned int i;
  137. for (i = 0; i < data_len; ++i) {
  138. if (data[i] != 0) {
  139. return 0;
  140. }
  141. }
  142. return 1;
  143. }
  144. // Check that the specified modification time matches the modification
  145. // time from the file header.
  146. static int check_modification_time(unsigned int mod_time,
  147. LHAFileHeader *header)
  148. {
  149. unsigned int time_diff;
  150. // In an ideal world, mod_time should match header->timestamp
  151. // exactly. However, there's an additional complication
  152. // because mod_time is local time, not UTC time, so there is
  153. // a timezone difference.
  154. if (header->timestamp > mod_time) {
  155. time_diff = header->timestamp - mod_time;
  156. } else {
  157. time_diff = mod_time - header->timestamp;
  158. }
  159. // The maximum UTC timezone difference is UTC+14, used in
  160. // New Zealand and some other islands in the Pacific.
  161. if (time_diff > 14 * 60 * 60) {
  162. return 0;
  163. }
  164. // If the world was simpler, all time zones would be exact
  165. // hour offsets, but in fact, some regions use half or
  166. // quarter hour offsets. So the difference should be a
  167. // multiple of 15 minutes. Actually, the control panel in
  168. // Mac OS allows any minute offset to be configured, but if
  169. // people are crazy enough to do that, they deserve the
  170. // brokenness they get as a result. It's preferable to use
  171. // a 15 minute check rather than a 1 minute check, because
  172. // this allows MacLHA-added MacBinary headers to be
  173. // distinguished from archived MacBinary files more reliably.
  174. //return (time_diff % (15 * 60)) == 0;
  175. // It turns out the assumption above doesn't hold, and MacLHA
  176. // does generate archives where the timestamps don't always
  177. // exactly match. Oh well.
  178. return 1;
  179. }
  180. // Given the specified data buffer, check whether it has a MacBinary
  181. // header with contents that match the specified .lzh header.
  182. static int is_macbinary_header(uint8_t *data, LHAFileHeader *header)
  183. {
  184. unsigned int filename_len;
  185. unsigned int data_fork_len, res_fork_len, expected_len;
  186. unsigned int mod_time;
  187. // Check fields in the header that should be zero.
  188. if (data[MBHDR_OFF_VERSION] != 0
  189. || data[MBHDR_OFF_ZERO_COMPAT1] != 0
  190. || data[MBHDR_OFF_ZERO_COMPAT2] != 0
  191. || !block_is_zero(&data[MBHDR_OFF_COMMENT_LEN], 2)
  192. || !block_is_zero(&data[MBHDR_OFF_MACBINARY2_DATA],
  193. MBHDR_LEN_MACBINARY2_DATA)) {
  194. return 0;
  195. }
  196. // Check that the filename matches the filename from the
  197. // lzh header.
  198. filename_len = data[MBHDR_OFF_FILENAME_LEN];
  199. if (filename_len > MBHDR_LEN_FILENAME
  200. || filename_len != strlen(header->filename)
  201. || memcmp(&data[MBHDR_OFF_FILENAME],
  202. header->filename, filename_len) != 0) {
  203. return 0;
  204. }
  205. // Data following the filename must be zero as well.
  206. if (!block_is_zero(data + MBHDR_OFF_FILENAME + filename_len,
  207. MBHDR_LEN_FILENAME - filename_len)) {
  208. return 0;
  209. }
  210. // Decode data fork / resource fork lengths. Their combined
  211. // lengths, plus the MacBinary header, should match the
  212. // compressed data length (rounded up to the nearest 128).
  213. data_fork_len = lha_decode_be_uint32(&data[MBHDR_OFF_DATA_FORK_LEN]);
  214. res_fork_len = lha_decode_be_uint32(&data[MBHDR_OFF_RES_FORK_LEN]);
  215. expected_len = (data_fork_len + res_fork_len + MBHDR_SIZE);
  216. if (header->length != ((expected_len + 0x7f) & ~0x7f)) {
  217. return 0;
  218. }
  219. // Check modification time.
  220. mod_time = lha_decode_be_uint32(&data[MBHDR_OFF_FILE_MOD_DATE]);
  221. if (mod_time < MAC_TIME_OFFSET
  222. || !check_modification_time(mod_time - MAC_TIME_OFFSET, header)) {
  223. return 0;
  224. }
  225. return 1;
  226. }
  227. //
  228. // MacBinary "decoder". This reuses the LHADecoder framework to provide
  229. // a "pass-through" decoder that detects and strips the MacBinary header.
  230. //
  231. typedef struct {
  232. // When the decoder is initialized, the first 128 bytes of
  233. // data are read into this buffer and analysed. If it is
  234. // not a MacBinary header, the data must be kept so that it
  235. // can be returned in the first call to .read().
  236. // mb_header_bytes contains the number of bytes still to read.
  237. uint8_t mb_header[MBHDR_SIZE];
  238. size_t mb_header_bytes;
  239. // The "inner" decoder used to read the compressed data.
  240. LHADecoder *decoder;
  241. // Number of bytes still to read before decode should be
  242. // terminated.
  243. size_t stream_remaining;
  244. } MacBinaryDecoder;
  245. // Structure used when initializing a MacBinaryDecoder.
  246. typedef struct {
  247. LHADecoder *decoder;
  248. LHAFileHeader *header;
  249. } MacBinaryDecoderClosure;
  250. static int read_macbinary_header(MacBinaryDecoder *decoder,
  251. LHAFileHeader *header)
  252. {
  253. unsigned int data_fork_len, res_fork_len;
  254. size_t n, bytes;
  255. bytes = 0;
  256. while (bytes < MBHDR_SIZE) {
  257. n = lha_decoder_read(decoder->decoder,
  258. decoder->mb_header + bytes,
  259. MBHDR_SIZE - bytes);
  260. // Unexpected EOF?
  261. if (n == 0) {
  262. return 0;
  263. }
  264. bytes += n;
  265. }
  266. // Check if the data that was read corresponds to a MacBinary
  267. // header that matches the .lzh header. If not, just decode it
  268. // as a normal stream.
  269. if (!is_macbinary_header(decoder->mb_header, header)) {
  270. decoder->mb_header_bytes = bytes;
  271. return 1;
  272. }
  273. // We have a MacBinary header, so skip over it. Decide how
  274. // long the data stream is (see policy in comment at start
  275. // of file).
  276. decoder->mb_header_bytes = 0;
  277. data_fork_len = lha_decode_be_uint32(
  278. &decoder->mb_header[MBHDR_OFF_DATA_FORK_LEN]);
  279. res_fork_len = lha_decode_be_uint32(
  280. &decoder->mb_header[MBHDR_OFF_RES_FORK_LEN]);
  281. if (data_fork_len > 0) {
  282. decoder->stream_remaining = data_fork_len;
  283. } else {
  284. decoder->stream_remaining = res_fork_len;
  285. }
  286. return 1;
  287. }
  288. static int macbinary_decoder_init(void *_decoder,
  289. LHADecoderCallback callback,
  290. void *_closure)
  291. {
  292. MacBinaryDecoder *decoder = _decoder;
  293. MacBinaryDecoderClosure *closure = _closure;
  294. decoder->decoder = closure->decoder;
  295. decoder->mb_header_bytes = 0;
  296. decoder->stream_remaining = closure->header->length;
  297. if (closure->header->length >= MBHDR_SIZE
  298. && !read_macbinary_header(decoder, closure->header)) {
  299. return 0;
  300. }
  301. return 1;
  302. }
  303. static void decode_to_end(LHADecoder *decoder)
  304. {
  305. uint8_t buf[128];
  306. size_t n;
  307. do {
  308. n = lha_decoder_read(decoder, buf, sizeof(buf));
  309. } while (n > 0);
  310. }
  311. static size_t macbinary_decoder_read(void *_decoder, uint8_t *buf)
  312. {
  313. MacBinaryDecoder *decoder = _decoder;
  314. size_t result;
  315. size_t to_read;
  316. size_t n;
  317. result = 0;
  318. // If there is data from the mb_header buffer waiting to be
  319. // read, add it first.
  320. if (decoder->mb_header_bytes > 0) {
  321. memcpy(buf, decoder->mb_header, decoder->mb_header_bytes);
  322. result = decoder->mb_header_bytes;
  323. decoder->mb_header_bytes = 0;
  324. }
  325. // Read further data, if there is some in the stream still to read.
  326. to_read = OUTPUT_BUFFER_SIZE - result;
  327. if (to_read > decoder->stream_remaining) {
  328. to_read = decoder->stream_remaining;
  329. }
  330. n = lha_decoder_read(decoder->decoder, buf + result, to_read);
  331. decoder->stream_remaining -= n;
  332. result += n;
  333. // Once the end of the stream is reached, there may still be
  334. // data from the inner decoder to decompress. When this happens,
  335. // run the decoder until the end.
  336. if (decoder->stream_remaining == 0) {
  337. decode_to_end(decoder->decoder);
  338. }
  339. return result;
  340. }
  341. static LHADecoderType macbinary_decoder_type = {
  342. macbinary_decoder_init,
  343. NULL,
  344. macbinary_decoder_read,
  345. sizeof(MacBinaryDecoder),
  346. OUTPUT_BUFFER_SIZE,
  347. 0,
  348. };
  349. LHADecoder *lha_macbinary_passthrough(LHADecoder *decoder,
  350. LHAFileHeader *header)
  351. {
  352. MacBinaryDecoderClosure closure;
  353. LHADecoder *result;
  354. closure.decoder = decoder;
  355. closure.header = header;
  356. result = lha_decoder_new(&macbinary_decoder_type, NULL,
  357. &closure, header->length);
  358. return result;
  359. }