lha_basic_reader.c 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  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 <stdio.h>
  17. #include <stdlib.h>
  18. #include <string.h>
  19. #include "crc16.h"
  20. #include "lha_decoder.h"
  21. #include "lha_basic_reader.h"
  22. struct _LHABasicReader {
  23. LHAInputStream *stream;
  24. LHAFileHeader *curr_file;
  25. size_t curr_file_remaining;
  26. int eof;
  27. };
  28. LHABasicReader *lha_basic_reader_new(LHAInputStream *stream)
  29. {
  30. LHABasicReader *reader;
  31. reader = calloc(1, sizeof(LHABasicReader));
  32. if (reader == NULL) {
  33. return NULL;
  34. }
  35. reader->stream = stream;
  36. reader->curr_file = NULL;
  37. reader->curr_file_remaining = 0;
  38. reader->eof = 0;
  39. return reader;
  40. }
  41. void lha_basic_reader_free(LHABasicReader *reader)
  42. {
  43. if (reader->curr_file != NULL) {
  44. lha_file_header_free(reader->curr_file);
  45. }
  46. free(reader);
  47. }
  48. LHAFileHeader *lha_basic_reader_curr_file(LHABasicReader *reader)
  49. {
  50. return reader->curr_file;
  51. }
  52. LHAFileHeader *lha_basic_reader_next_file(LHABasicReader *reader)
  53. {
  54. // Free the current file header and skip over any remaining
  55. // compressed data that hasn't been read yet.
  56. if (reader->curr_file != NULL) {
  57. lha_file_header_free(reader->curr_file);
  58. reader->curr_file = NULL;
  59. if (!lha_input_stream_skip(reader->stream,
  60. reader->curr_file_remaining)) {
  61. reader->eof = 1;
  62. }
  63. }
  64. if (reader->eof) {
  65. return NULL;
  66. }
  67. // Read the header for the next file.
  68. reader->curr_file = lha_file_header_read(reader->stream);
  69. if (reader->curr_file == NULL) {
  70. reader->eof = 1;
  71. return NULL;
  72. }
  73. reader->curr_file_remaining = reader->curr_file->compressed_length;
  74. return reader->curr_file;
  75. }
  76. size_t lha_basic_reader_read_compressed(LHABasicReader *reader, void *buf,
  77. size_t buf_len)
  78. {
  79. size_t bytes;
  80. if (reader->eof || reader->curr_file_remaining == 0) {
  81. return 0;
  82. }
  83. // Read up to the number of bytes of compressed data remaining.
  84. if (buf_len > reader->curr_file_remaining) {
  85. bytes = reader->curr_file_remaining;
  86. } else {
  87. bytes = buf_len;
  88. }
  89. if (!lha_input_stream_read(reader->stream, buf, bytes)) {
  90. reader->eof = 1;
  91. return 0;
  92. }
  93. // Update counter and return success.
  94. reader->curr_file_remaining -= bytes;
  95. return bytes;
  96. }
  97. static size_t decoder_callback(void *buf, size_t buf_len, void *user_data)
  98. {
  99. return lha_basic_reader_read_compressed(user_data, buf, buf_len);
  100. }
  101. // Create the decoder structure to decode the current file.
  102. LHADecoder *lha_basic_reader_decode(LHABasicReader *reader)
  103. {
  104. LHADecoderType *dtype;
  105. if (reader->curr_file == NULL) {
  106. return NULL;
  107. }
  108. // Look up the decoder to use for this compression method.
  109. dtype = lha_decoder_for_name(reader->curr_file->compress_method);
  110. if (dtype == NULL) {
  111. return NULL;
  112. }
  113. // Create decoder.
  114. return lha_decoder_new(dtype, decoder_callback, reader,
  115. reader->curr_file->length);
  116. }