| 56 | } |
| 57 | |
| 58 | void ZipFile::Read(void *data, size_t size) { |
| 59 | if (state == Deflate) { |
| 60 | deflateEnd(&z); |
| 61 | state = Initial; |
| 62 | } |
| 63 | if (state != Inflate) { |
| 64 | if (inflateInit(&z) != Z_OK) |
| 65 | throw FFMS_Exception(FFMS_ERROR_PARSER, FFMS_ERROR_FILE_READ, "Failed to initialize zlib"); |
| 66 | state = Inflate; |
| 67 | } |
| 68 | |
| 69 | z.next_out = static_cast<unsigned char *>(data); |
| 70 | z.avail_out = size; |
| 71 | if (!z.avail_in && !is_file) { |
| 72 | z.next_in = reinterpret_cast<Bytef*>(&index_buffer[0]); |
| 73 | z.avail_in = index_buffer.size(); |
| 74 | } |
| 75 | do { |
| 76 | if (!z.avail_in && is_file) { |
| 77 | z.next_in = reinterpret_cast<Bytef*>(&buffer[0]); |
| 78 | z.avail_in = file.Read(&buffer[0], buffer.size()); |
| 79 | } |
| 80 | if (!z.avail_in) { |
| 81 | if (!is_file) |
| 82 | throw FFMS_Exception(FFMS_ERROR_PARSER, FFMS_ERROR_FILE_READ, "Failed to read data: Buffer is empty"); |
| 83 | else if (!file.Tell()) |
| 84 | throw FFMS_Exception(FFMS_ERROR_PARSER, FFMS_ERROR_FILE_READ, "Failed to read data: File is empty"); |
| 85 | } |
| 86 | |
| 87 | switch (inflate(&z, Z_SYNC_FLUSH)) { |
| 88 | case Z_NEED_DICT: |
| 89 | throw FFMS_Exception(FFMS_ERROR_PARSER, FFMS_ERROR_FILE_READ, "Failed to read data: Dictionary error."); |
| 90 | case Z_DATA_ERROR: |
| 91 | throw FFMS_Exception(FFMS_ERROR_PARSER, FFMS_ERROR_FILE_READ, "Failed to read data: Data error."); |
| 92 | case Z_MEM_ERROR: |
| 93 | throw FFMS_Exception(FFMS_ERROR_PARSER, FFMS_ERROR_FILE_READ, "Failed to read data: Memory error."); |
| 94 | case Z_STREAM_END: |
| 95 | if (z.avail_out > 0) |
| 96 | throw FFMS_Exception(FFMS_ERROR_PARSER, FFMS_ERROR_FILE_READ, "Failed to read data: Stream ended early"); |
| 97 | } |
| 98 | } while (z.avail_out); |
| 99 | } |
| 100 | |
| 101 | int ZipFile::Write(const void *data, size_t size) { |
| 102 | if (state == Inflate) { |
no test coverage detected