102#define CDIR_ENTRY_SIG 0x02014b50
103#define LOC_FILE_HEADER_SIG 0x04034b50
104#define CDIR_END_SIG 0x06054b50
113 unsigned short tmpBuf;
114 unsigned long numBytesRead =
stream->sread(
115 reinterpret_cast<unsigned char *
>( &tmpBuf ), 2);
116 if (numBytesRead != 2)
123 if (!stream ||
stream->stell() == -1)
126 unsigned long numBytesRead =
stream->sread(
127 reinterpret_cast<unsigned char *
>( &tmpBuf ), 4);
128 if (numBytesRead != 4)
135 if (!stream ||
stream->stell() == -1)
137 auto tmp = std::make_unique<unsigned char[]>(size);
138 unsigned long numBytesRead =
stream->sread(tmp.get(), size);
139 if (numBytesRead != size)
144 std::string
aStr(
reinterpret_cast<char *
>(tmp.get()), size);
152 unsigned signature = readInt(stream);
156 end.disk_num = readShort(stream);
157 end.cdir_disk = readShort(stream);
158 end.disk_entries = readShort(stream);
159 end.cdir_entries = readShort(stream);
160 end.cdir_size = readInt(stream);
161 end.cdir_offset = readInt(stream);
162 end.comment_size = readShort(stream);
176 unsigned signature = readInt(stream);
186 entry.
crc32 = readInt(stream);
195 entry.
offset = readInt(stream);
211 unsigned signature = readInt(stream);
215 header.min_version = readShort(stream);
216 header.general_flag = readShort(stream);
217 header.compression = readShort(stream);
218 header.lastmod_time = readShort(stream);
219 header.lastmod_date = readShort(stream);
220 header.crc32 = readInt(stream);
221 header.compressed_size = readInt(stream);
222 header.uncompressed_size = readInt(stream);
223 header.filename_size = readShort(stream);
224 header.extra_field_size = readShort(stream);
243 if (!(
header.general_flag & 0x08))
255#define BLOCK_SIZE 0x800
257bool findSignatureAtOffset(
StreamInterface *stream,
unsigned long nOffset)
262 stream->sseek(nOffset, SEEK_SET);
266 for (
long n = nBytesRead - 4;
n >= 0;
n--)
271 stream->sseek(nOffset +
n, SEEK_SET);
284 stream->sseek(0,SEEK_END);
295 if (findSignatureAtOffset(stream, nOffset))
298 return findSignatureAtOffset(stream, 0);
308 if (!findCentralDirectoryEnd(stream))
311 if (!readCentralDirectoryEnd(stream, end))
313 stream->sseek(
end.cdir_offset, SEEK_SET);
315 if (!readCentralDirectoryEntry(stream, entry))
319 if (!readLocalFileHeader(stream, header))
321 if (!areHeadersConsistent(header, entry))
336 explicit stricmp(
const std::string &str) :
str_(str)
339 bool operator() (
const std::string &other)
341 return (0 == _stricmp(
str_.c_str(), other.c_str()));
369bool ZipFile::IsZipFile(
void* )
390bool ZipFile::IsValidZipFileVersionNumber(
const Filepath_t& )
395bool ZipFile::IsValidZipFileVersionNumber(
void* )
418 if (!isZipStream(m_pStream))
429 if (!isZipStream(stream))
438 if (m_pStream && m_bShouldFree)
447void ZipFile::GetUncompressedContent(
448 const std::string &ContentName, ZipContentBuffer_t &ContentBuffer)
450 if (!findCentralDirectoryEnd(m_pStream))
453 if (!readCentralDirectoryEnd(m_pStream, end))
455 m_pStream->
sseek(
end.cdir_offset, SEEK_SET);
459 if (!readCentralDirectoryEntry(m_pStream, entry))
461 if (ContentName.length() == entry.
filename_size && !_stricmp(entry.
filename.c_str(), ContentName.c_str()))
466 if (_stricmp(entry.
filename.c_str(), ContentName.c_str()))
470 if (!readLocalFileHeader(m_pStream, header))
472 if (!areHeadersConsistent(header, entry))
474 ContentBuffer.clear();
484 strm.zalloc = Z_NULL;
486 strm.opaque = Z_NULL;
488 strm.next_in = Z_NULL;
489 ret = inflateInit2(&strm,-MAX_WBITS);
498 strm.next_in =
reinterpret_cast<Bytef *
>(tmpBuffer.data());
501 strm.next_out =
reinterpret_cast<Bytef *
>(ContentBuffer.data());
502 ret = inflate(&strm, Z_FINISH);
508 (void)inflateEnd(&strm);
509 ContentBuffer.clear();
512 (void)inflateEnd(&strm);
519ZipFile::DirectoryPtr_t ZipFile::GetDirectory()
const
521 DirectoryPtr_t dir(
new Directory_t());
522 if (!findCentralDirectoryEnd(m_pStream))
525 if (!readCentralDirectoryEnd(m_pStream, end))
527 m_pStream->
sseek(
end.cdir_offset, SEEK_SET);
531 if (!readCentralDirectoryEntry(m_pStream, entry))
542bool ZipFile::HasContent(
const std::string &ContentName)
const
547 DirectoryPtr_t dir = GetDirectory();
549 return std::any_of(dir->begin(), dir->end(), internal::stricmp(ContentName));
constexpr sal_Int8 header[]
virtual unsigned long sread(unsigned char *vuf, unsigned long size)=0
virtual long sseek(long offset, int origin)=0
ZipFile(rtl::Reference< comphelper::RefCountedMutex > aMutexHolder, css::uno::Reference< css::io::XInputStream > const &xInput, css::uno::Reference< css::uno::XComponentContext > xContext, bool bInitialise)
Reference< XOutputStream > stream
GtkMediaStream * m_pStream
constexpr std::enable_if_t< std::is_signed_v< T >, std::make_unsigned_t< T > > make_unsigned(T value)
sal_uInt32 readString(const sal_uInt8 *buffer, sal_Unicode *v, sal_uInt32 maxSize)
unsigned short disk_entries
unsigned short cdir_entries
unsigned short comment_size
unsigned short creator_version
unsigned short min_version
unsigned short lastmod_date
unsigned short internal_attr
unsigned short extra_field_size
unsigned short filename_size
unsigned short lastmod_time
unsigned short compression
unsigned short file_comment_size
unsigned uncompressed_size
unsigned short general_flag
std::unique_ptr< char[]> aBuffer
#define LOC_FILE_HEADER_SIG