| 212 | } |
| 213 | |
| 214 | size_t ZipArchive::readFile(void* data, size_t size) |
| 215 | { |
| 216 | if (m_curFile == INVALID_FILE) { return 0u; } |
| 217 | if (size == 0) { size = m_entries[m_curFile].length; } |
| 218 | |
| 219 | const size_t sizeToRead = std::min(size, m_entries[m_curFile].length); |
| 220 | // The fast path is to just read the entire entry into the provided memory, avoiding the extra memcopy. |
| 221 | // This is only done if we are reading the entire file and there is no offset. |
| 222 | if (m_fileOffset == 0 && sizeToRead == m_entries[m_curFile].length) |
| 223 | { |
| 224 | m_fileOffset += (s32)sizeToRead; |
| 225 | s64 actualSizeRead = zip_entry_noallocread((struct zip_t*)m_fileHandle, data, sizeToRead); |
| 226 | if (actualSizeRead <= 0) |
| 227 | { |
| 228 | return 0u; |
| 229 | } |
| 230 | return size_t(actualSizeRead); |
| 231 | } |
| 232 | |
| 233 | // Otherwise go through the slower path - a one time decompression and read, followed |
| 234 | // by memcopying the data into the output as needed. |
| 235 | if (!m_entryRead) |
| 236 | { |
| 237 | // Read the whole entry into temporary memory. |
| 238 | assert(m_tempBufferSize >= m_entries[m_curFile].length); |
| 239 | if (zip_entry_noallocread((struct zip_t*)m_fileHandle, m_tempBuffer, m_entries[m_curFile].length) <= 0) |
| 240 | { |
| 241 | return 0u; |
| 242 | } |
| 243 | m_entryRead = true; |
| 244 | } |
| 245 | // Then copy the section we want into the output. |
| 246 | memcpy(data, m_tempBuffer + m_fileOffset, sizeToRead); |
| 247 | m_fileOffset += (s32)sizeToRead; |
| 248 | return sizeToRead; |
| 249 | } |
| 250 | |
| 251 | bool ZipArchive::seekFile(s32 offset, s32 origin) |
| 252 | { |
no test coverage detected