Skip to content

Commit 64617f4

Browse files
kokudamartinweismann
authored andcommitted
NC-Negate offset when ZIP_SOURCE_SEEK from SEEK_END (#74)
* Negate offset when ZIP_SOURCE_SEEK from SEEK_END custom_zip_source_callback from the zip library where the cmd is ZIP_SOURCE_SEEK and whence is SEEK_END specifies a negative offset. pImportStream takes an unsigned positive value to indicate the distance to seek back. The value must be negated between the two APIs. * Assume unsigned argument for seekFromEnd The API for CImportStream and CExportStream only allows unsigned input. This commit handles this consistently in all seekFromEnd-functions and adds some exceptions in custom_zip_source_callback. * Support zip seeking backward from the current position. * Update version
1 parent f287932 commit 64617f4

File tree

5 files changed

+10
-12
lines changed

5 files changed

+10
-12
lines changed

CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ project (lib3MF)
99
# Define Version
1010
set(LIB3MF_VERSION_MAJOR 1) # increase on every backward-compatibility breaking change of the API
1111
set(LIB3MF_VERSION_MINOR 3) # increase on every backward compatible change of the API
12-
set(LIB3MF_VERSION_MICRO 0) # increase on on every change that does not alter the API
12+
set(LIB3MF_VERSION_MICRO 1) # increase on on every change that does not alter the API
1313

1414
set(CMAKE_INSTALL_BINDIR bin CACHE PATH "directory for installing binary files")
1515
set(CMAKE_INSTALL_LIBDIR lib CACHE PATH "directory for installing library files")

Source/Common/OPC/NMR_OpcPackageReader.cpp

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -62,13 +62,16 @@ namespace NMR {
6262
argsSeek = * ((zip_source_args_seek *)data);
6363
if (argsSeek.whence == SEEK_SET)
6464
pImportStream->seekPosition(argsSeek.offset, true);
65-
else if (argsSeek.whence == SEEK_CUR)
66-
pImportStream->seekForward(argsSeek.offset, true);
65+
else if (argsSeek.whence == SEEK_CUR) {
66+
pImportStream->seekPosition(pImportStream->getPosition() + argsSeek.offset, true);
67+
}
6768
else if (argsSeek.whence == SEEK_END) {
68-
pImportStream->seekFromEnd(argsSeek.offset, true);
69+
if (argsSeek.offset > 0)
70+
throw CNMRException(NMR_ERROR_ZIPCALLBACK);
71+
pImportStream->seekFromEnd(-argsSeek.offset, true);
6972
}
7073
else
71-
throw CNMRException(0);
74+
throw CNMRException(NMR_ERROR_ZIPCALLBACK);
7275
return 0;
7376

7477
case ZIP_SOURCE_OPEN:

Source/Common/Platform/NMR_ExportStream_GCC_Native.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,7 @@ namespace NMR {
100100

101101
nfBool CExportStream_GCC_Native::seekFromEnd(_In_ nfUint64 bytes, _In_ nfBool bHasToSucceed)
102102
{
103-
std::streampos nStreamOffset = bytes;
103+
std::streampos nStreamOffset = 0-bytes;
104104
m_Stream.seekp(nStreamOffset, std::ios_base::end);
105105

106106
if (m_Stream.fail()) {

Source/Common/Platform/NMR_ImportStream_GCC_Native.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@ namespace NMR {
9696

9797
nfBool CImportStream_GCC_Native::seekFromEnd(_In_ nfUint64 bytes, _In_ nfBool bHasToSucceed)
9898
{
99-
std::streampos nStreamOffset = bytes;
99+
std::streampos nStreamOffset = 0-bytes;
100100
m_Stream.seekg(nStreamOffset, std::ios_base::end);
101101

102102
if (m_Stream.fail()) {

Source/Common/Platform/NMR_ImportStream_Memory.cpp

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -156,11 +156,6 @@ namespace NMR {
156156

157157
nfBool CImportStream_Memory::seekFromEnd(_In_ nfUint64 cbBytes, _In_ nfBool bHasToSucceed)
158158
{
159-
// all seekFromEnd functions follow the calling conventions of fseek.
160-
// fseek expects a negative number to seek from the end (SEEK_END).
161-
// The program logic in this function requires a positive offset from the end.
162-
cbBytes = 1+~cbBytes;
163-
164159
if (cbBytes > m_cbSize) {
165160
if (bHasToSucceed)
166161
throw CNMRException(NMR_ERROR_COULDNOTSEEKSTREAM);

0 commit comments

Comments
 (0)