| 1 | /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ |
| 2 | /* |
| 3 | * This file is part of the libcdr project. |
| 4 | * |
| 5 | * This Source Code Form is subject to the terms of the Mozilla Public |
| 6 | * License, v. 2.0. If a copy of the MPL was not distributed with this |
| 7 | * file, You can obtain one at http://mozilla.org/MPL/2.0/. |
| 8 | */ |
| 9 | |
| 10 | #include "CDRInternalStream.h" |
| 11 | |
| 12 | #include <zlib.h> |
| 13 | #include <string.h> // for memcpy |
| 14 | |
| 15 | |
| 16 | #define CHUNK 16384 |
| 17 | |
| 18 | libcdr::CDRInternalStream::CDRInternalStream(const std::vector<unsigned char> &buffer) : |
| 19 | librevenge::RVNGInputStream(), |
| 20 | m_offset(0), |
| 21 | m_buffer(buffer) |
| 22 | { |
| 23 | } |
| 24 | |
| 25 | libcdr::CDRInternalStream::CDRInternalStream(librevenge::RVNGInputStream *input, unsigned long size, bool compressed) : |
| 26 | librevenge::RVNGInputStream(), |
| 27 | m_offset(0), |
| 28 | m_buffer() |
| 29 | { |
| 30 | if (!size) |
| 31 | return; |
| 32 | |
| 33 | if (!compressed) |
| 34 | { |
| 35 | unsigned long tmpNumBytesRead = 0; |
| 36 | const unsigned char *tmpBuffer = input->read(size, tmpNumBytesRead); |
| 37 | |
| 38 | if (size != tmpNumBytesRead) |
| 39 | return; |
| 40 | |
| 41 | m_buffer = std::vector<unsigned char>(size); |
| 42 | memcpy(&m_buffer[0], tmpBuffer, size); |
| 43 | } |
| 44 | else |
| 45 | { |
| 46 | int ret; |
| 47 | z_stream strm; |
| 48 | unsigned char out[CHUNK]; |
| 49 | |
| 50 | /* allocate inflate state */ |
| 51 | strm.zalloc = Z_NULL; |
| 52 | strm.zfree = Z_NULL; |
| 53 | strm.opaque = Z_NULL; |
| 54 | strm.avail_in = 0; |
| 55 | strm.next_in = Z_NULL; |
| 56 | ret = inflateInit(&strm); |
| 57 | if (ret != Z_OK) |
| 58 | return; |
| 59 | |
| 60 | unsigned long tmpNumBytesRead = 0; |
| 61 | const unsigned char *tmpBuffer = input->read(size, tmpNumBytesRead); |
| 62 | |
| 63 | if (size != tmpNumBytesRead) |
| 64 | { |
| 65 | (void)inflateEnd(&strm); |
| 66 | return; |
| 67 | } |
| 68 | |
| 69 | strm.avail_in = (uInt)tmpNumBytesRead; |
| 70 | strm.next_in = const_cast<Bytef *>(tmpBuffer); |
| 71 | |
| 72 | do |
| 73 | { |
| 74 | strm.avail_out = CHUNK; |
| 75 | strm.next_out = out; |
| 76 | ret = inflate(&strm, Z_NO_FLUSH); |
| 77 | switch (ret) |
| 78 | { |
| 79 | case Z_NEED_DICT: |
| 80 | case Z_DATA_ERROR: |
| 81 | case Z_MEM_ERROR: |
| 82 | (void)inflateEnd(&strm); |
| 83 | m_buffer.clear(); |
| 84 | return; |
| 85 | default: |
| 86 | break; |
| 87 | } |
| 88 | |
| 89 | unsigned have = CHUNK - strm.avail_out; |
| 90 | |
| 91 | for (unsigned long i=0; i<have; i++) |
| 92 | m_buffer.push_back(out[i]); |
| 93 | |
| 94 | } |
| 95 | while (strm.avail_out == 0); |
| 96 | (void)inflateEnd(&strm); |
| 97 | } |
| 98 | } |
| 99 | |
| 100 | const unsigned char *libcdr::CDRInternalStream::read(unsigned long numBytes, unsigned long &numBytesRead) |
| 101 | { |
| 102 | numBytesRead = 0; |
| 103 | |
| 104 | if (numBytes == 0) |
| 105 | return nullptr; |
| 106 | |
| 107 | if (m_offset < 0) |
| 108 | return nullptr; |
| 109 | |
| 110 | const unsigned long bufSize = m_buffer.size(); |
| 111 | const unsigned long pos = static_cast<unsigned long>(m_offset); |
| 112 | const unsigned long remaining = pos < bufSize ? bufSize - pos : 0; |
| 113 | const unsigned long numBytesToRead = numBytes < remaining ? numBytes : remaining; |
| 114 | |
| 115 | numBytesRead = numBytesToRead; // about as paranoid as we can be.. |
| 116 | |
| 117 | if (numBytesToRead == 0) |
| 118 | return nullptr; |
| 119 | |
| 120 | long oldOffset = m_offset; |
| 121 | m_offset += numBytesToRead; |
| 122 | |
| 123 | return &m_buffer[oldOffset]; |
| 124 | } |
| 125 | |
| 126 | int libcdr::CDRInternalStream::seek(long offset, librevenge::RVNG_SEEK_TYPE seekType) |
| 127 | { |
| 128 | if (seekType == librevenge::RVNG_SEEK_CUR) |
| 129 | m_offset += offset; |
| 130 | else if (seekType == librevenge::RVNG_SEEK_SET) |
| 131 | m_offset = offset; |
| 132 | else if (seekType == librevenge::RVNG_SEEK_END) |
| 133 | m_offset = long(static_cast<unsigned long>(m_buffer.size())) + offset; |
| 134 | |
| 135 | if (m_offset < 0) |
| 136 | { |
| 137 | m_offset = 0; |
| 138 | return 1; |
| 139 | } |
| 140 | if ((long)m_offset > (long)m_buffer.size()) |
| 141 | { |
| 142 | m_offset = m_buffer.size(); |
| 143 | return 1; |
| 144 | } |
| 145 | |
| 146 | return 0; |
| 147 | } |
| 148 | |
| 149 | long libcdr::CDRInternalStream::tell() |
| 150 | { |
| 151 | return m_offset; |
| 152 | } |
| 153 | |
| 154 | bool libcdr::CDRInternalStream::isEnd() |
| 155 | { |
| 156 | if ((long)m_offset >= (long)m_buffer.size()) |
| 157 | return true; |
| 158 | |
| 159 | return false; |
| 160 | } |
| 161 | /* vim:set shiftwidth=2 softtabstop=2 expandtab: */ |