Skip to content

Commit f7dd2fb

Browse files
authored
Updates to match new libzip version + potential fix (#125)
* [WIP] Updates for the newer libzip version Also some additions to the API as well as fixed a handful of compile warnings * Possible fix for the "unable to open as zip archive" error Let's see if it breaks any tests... * Bump version to 2.1.0 New minor version because of the new versions of native libraries
1 parent f9ae5aa commit f7dd2fb

19 files changed

+352
-88
lines changed

CMakeLists.txt

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -459,11 +459,19 @@ else()
459459
add_library(
460460
${PROJECT_NAME}
461461
SHARED
462+
native/sizes.cc
463+
native/values.cc
462464
native/version.cc
463465
)
464466

465467
add_dependencies(${PROJECT_NAME} zip)
466468

469+
target_include_directories(
470+
${PROJECT_NAME}
471+
PRIVATE
472+
"external/libzip/lib"
473+
)
474+
467475
target_compile_definitions(
468476
${PROJECT_NAME}
469477
PRIVATE

LibZipSharp.props

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<Project DefaultTargets="Build" ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
22
<PropertyGroup>
3-
<_LibZipSharpAssemblyVersion>2.0.8</_LibZipSharpAssemblyVersion>
3+
<_LibZipSharpAssemblyVersion>2.1.0</_LibZipSharpAssemblyVersion>
44
<!--
55
Nuget Version. You can append things like -alpha-1 etc to this value.
66
But always leave the $(_LibZipSharpAssemblyVersion) value at the start.

LibZipSharp/Xamarin.Tools.Zip/EncryptionMethod.cs

Lines changed: 47 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -45,62 +45,94 @@ public enum EncryptionMethod : ushort
4545

4646
/// <summary>
4747
/// Strong encryption: DES
48-
/// <remarks>Not supported by the native libzip yet (as of v1.0.1)</remarks>
48+
/// <remarks>Not supported by the native libzip yet (as of v1.9.2)</remarks>
4949
/// </summary>
5050
DES = 0x6601,
5151

5252
/// <summary>
5353
/// Strong encryption: RC2, version &lt; 5.2
54-
/// <remarks>Not supported by the native libzip yet (as of v1.0.1)</remarks>
54+
/// <remarks>Not supported by the native libzip yet (as of v1.9.2)</remarks>
5555
/// </summary>
5656
RC2_Old = 0x6602,
5757

5858
/// <summary>
5959
/// Strong encryption: 3DES (168-bit key)
60-
/// <remarks>Not supported by the native libzip yet (as of v1.0.1)</remarks>
60+
/// <remarks>Not supported by the native libzip yet (as of v1.9.2)</remarks>
6161
/// </summary>
6262
Three_DES_168 = 0x6603,
6363

6464
/// <summary>
6565
/// Strong encryption: 3DES (112-bit key)
66-
/// <remarks>Not supported by the native libzip yet (as of v1.0.1)</remarks>
66+
/// <remarks>Not supported by the native libzip yet (as of v1.9.2)</remarks>
6767
/// </summary>
6868
Three_DES_112 = 0x6609,
6969

7070
/// <summary>
71-
/// Strong encryption: AES (128-bit key)
72-
/// <remarks>Not supported by the native libzip yet (as of v1.0.1)</remarks>
71+
/// Strong encryption: PKZIP AES (128-bit key)
72+
/// <remarks>Not supported by the native libzip yet (as of v1.9.2)</remarks>
7373
/// </summary>
74-
AES_128 = 0x660e,
74+
PKZIP_AES_128 = 0x660e,
7575

7676
/// <summary>
77-
/// Strong encryption: AES (192-bit key)
78-
/// <remarks>Not supported by the native libzip yet (as of v1.0.1)</remarks>
77+
/// Deprecated alias for <see cref="AES_128"/>
7978
/// </summary>
80-
AES_192 = 0x660f,
79+
[Obsolete ("Use EncryptionMethod.PKZIP_AES_128")]
80+
AES_128 = PKZIP_AES_128,
81+
82+
/// <summary>
83+
/// Strong encryption: PKZIP AES (192-bit key)
84+
/// <remarks>Not supported by the native libzip yet (as of v1.9.2)</remarks>
85+
/// </summary>
86+
PKZIP_AES_192 = 0x660f,
87+
88+
/// <summary>
89+
/// Deprecated alias for <see cref="AES_192"/>
90+
/// </summary>
91+
[Obsolete ("Use EncryptionMethod.PKZIP_AES_192")]
92+
AES_192 = PKZIP_AES_192,
8193

8294
/// <summary>
8395
/// Strong encryption: AES (256-bit key)
84-
/// <remarks>Not supported by the native libzip yet (as of v1.0.1)</remarks>
96+
/// <remarks>Not supported by the native libzip yet (as of v1.9.2)</remarks>
8597
/// </summary>
86-
AES_256 = 0x6610,
98+
PKZIP_AES_256 = 0x6610,
99+
100+
/// <summary>
101+
/// Deprecated alias for <see cref="AES_256"/>
102+
/// </summary>
103+
[Obsolete ("Use EncryptionMethod.PKZIP_AES_256")]
104+
AES_256 = PKZIP_AES_256,
87105

88106
/// <summary>
89107
/// Strong encryption: RC2, version >= 5.2
90-
/// <remarks>Not supported by the native libzip yet (as of v1.0.1)</remarks>
108+
/// <remarks>Not supported by the native libzip yet (as of v1.9.2)</remarks>
91109
/// </summary>
92110
RC2 = 0x6702,
93111

94112
/// <summary>
95113
/// Strong encryption: RC4
96-
/// <remarks>Not supported by the native libzip yet (as of v1.0.1)</remarks>
114+
/// <remarks>Not supported by the native libzip yet (as of v1.9.2)</remarks>
97115
/// </summary>
98116
RC4 = 0x6801,
99117

118+
/// <summary>
119+
/// WinZIP AES encryption (128-bit key)
120+
/// </summary>
121+
WINZIP_AES_128 = 0x0101,
122+
123+
/// <summary>
124+
/// WinZIP AES encryption (192-bit key)
125+
/// </summary>
126+
WINZIP_AES_192 = 0x0102,
127+
128+
/// <summary>
129+
/// WinZIP AES encryption (256-bit key)
130+
/// </summary>
131+
WINZIP_AES_256 = 0x0103,
132+
100133
/// <summary>
101134
/// Unknown algorithm
102135
/// </summary>
103136
Unknown = 0xffff,
104137
}
105138
}
106-

LibZipSharp/Xamarin.Tools.Zip/EntryPermissions.cs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,11 +26,11 @@
2626
using System;
2727
namespace Xamarin.Tools.Zip
2828
{
29-
[Flags]
3029
/// <summary>
3130
/// ZIP (as opposed to filesystem) entry permission bits. The bits are used only
3231
/// on the Unix systems.
3332
/// </summary>
33+
[Flags]
3434
public enum EntryPermissions : uint
3535
{
3636
Default = 0,
@@ -55,4 +55,3 @@ public enum EntryPermissions : uint
5555
SetDirectoryPermissionControl = UnixExternalPermissions.ISVTX,
5656
}
5757
}
58-

LibZipSharp/Xamarin.Tools.Zip/ErrorCode.cs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -192,5 +192,15 @@ public enum ErrorCode
192192
/// Tell error
193193
/// </summary>
194194
Tell = 30,
195+
196+
/// <summary>
197+
/// Compressed data invalid
198+
/// </summary>
199+
CompressedData = 31,
200+
201+
/// <summary>
202+
/// Operation cancelled
203+
/// </summary>
204+
Cancelled = 32,
195205
}
196206
}

LibZipSharp/Xamarin.Tools.Zip/ErrorType.cs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,10 @@ public enum ErrorType
4646
/// Error code field is zlib error code
4747
/// </summary>
4848
Zlib = 2,
49+
50+
/// <summary>
51+
/// Error code field is libzip error code
52+
/// </summary>
53+
Libzip = 3,
4954
}
5055
}
51-

LibZipSharp/Xamarin.Tools.Zip/IPlatformServices.cs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@
2323
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
2424
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
2525
// THE SOFTWARE.
26-
using System;
2726
using System.Collections.Generic;
2827

2928
namespace Xamarin.Tools.Zip
@@ -37,8 +36,10 @@ public interface IPlatformServices
3736
/// Checks whether the filesystem location identified by <paramref name="path"/> is a regular
3837
/// file. Irregular files include device nodes, sockets, character or block devices on Unix systems etc.
3938
/// </summary>
40-
/// <returns><c>true</c> if <paramref name="path"/> points to regular file</returns>
39+
/// <returns><c>true</c> if operation was successful, <c>false</c> otherwise</returns>
40+
/// <param name="archive">ZipArchive to operate on</param>
4141
/// <param name="path">Path to the filesystem location</param>
42+
/// <param name="result"><c>true</c> if <paramref name="path"/> points to regular file</param>
4243
bool IsRegularFile (ZipArchive archive, string path, out bool result);
4344
bool IsDirectory (ZipArchive archive, string path, out bool result);
4445
bool GetFilesystemPermissions (ZipArchive archive, string path, out EntryPermissions permissions);

LibZipSharp/Xamarin.Tools.Zip/Native.cs

Lines changed: 88 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
2626
// THE SOFTWARE.
2727
using System;
28+
using System.IO;
2829
using System.Runtime.InteropServices;
2930

3031
[assembly: DefaultDllImportSearchPathsAttribute(DllImportSearchPath.SafeDirectories | DllImportSearchPath.AssemblyDirectory)]
@@ -33,6 +34,11 @@ namespace Xamarin.Tools.Zip
3334
{
3435
internal class Native
3536
{
37+
const UInt32 LZS_SEEK_SET = 0;
38+
const UInt32 LZS_SEEK_CUR = 1;
39+
const UInt32 LZS_SEEK_END = 2;
40+
const UInt32 LZS_SEEK_INVALID = 0xDEADBEEFu;
41+
3642
[StructLayout (LayoutKind.Sequential)]
3743
public struct LZSVersions
3844
{
@@ -53,6 +59,7 @@ public struct zip_error_t
5359
public IntPtr str; /* string representation or NULL */
5460
};
5561

62+
[StructLayout (LayoutKind.Sequential)]
5663
public struct zip_source_args_seek_t
5764
{
5865
public UInt64 offset;
@@ -92,16 +99,49 @@ public static int ZipSourceMakeCommandBitmask (SourceCommand cmd)
9299
return 1 << (int)cmd;
93100
}
94101

95-
public static T ZipSourceGetArgs<T> (IntPtr data, UInt64 len)
102+
public unsafe static bool ZipSourceGetArgs<T> (IntPtr data, UInt64 len, out T ret) where T : unmanaged
96103
{
97-
return (T)Marshal.PtrToStructure (data, typeof (T));
104+
ret = default(T);
105+
if (data == IntPtr.Zero) {
106+
return false;
107+
}
108+
109+
if (len < (ulong)sizeof (T)) {
110+
return false;
111+
}
112+
113+
ret = (T)Marshal.PtrToStructure (data, typeof (T));
114+
return true;
98115
}
99116

100117
const string ZIP_LIBNAME = "libZipSharpNative";
101118

102119
[DllImport (ZIP_LIBNAME, CallingConvention = CallingConvention.Cdecl)]
103120
static extern void lzs_get_versions (out LZSVersions versions);
104121

122+
[DllImport (ZIP_LIBNAME, CallingConvention = CallingConvention.Cdecl)]
123+
public static extern UInt64 lzs_get_size_zip_source_args_seek ();
124+
125+
[DllImport (ZIP_LIBNAME, CallingConvention = CallingConvention.Cdecl)]
126+
public static extern UInt32 lzs_convert_whence_value (Int32 whence);
127+
128+
public static SeekOrigin ConvertWhence (Int32 whence)
129+
{
130+
switch (lzs_convert_whence_value (whence)) {
131+
case LZS_SEEK_SET:
132+
return SeekOrigin.Begin;
133+
134+
case LZS_SEEK_CUR:
135+
return SeekOrigin.Current;
136+
137+
case LZS_SEEK_END:
138+
return SeekOrigin.End;
139+
140+
default:
141+
throw new InvalidOperationException ($"Invalid whence value: {whence}");
142+
}
143+
}
144+
105145
public static Versions get_versions ()
106146
{
107147
lzs_get_versions (out LZSVersions ret);
@@ -208,20 +248,60 @@ public static int zip_stat (IntPtr archive, string fname, OperationFlags flags,
208248
[DllImport (ZIP_LIBNAME, CallingConvention = CallingConvention.Cdecl)]
209249
public static extern int zip_stat_index (IntPtr archive, UInt64 index, OperationFlags flags, out zip_stat_t sb);
210250

251+
static void StringToComment (string comment, out IntPtr utfString, out UInt16 len)
252+
{
253+
if (comment == null) {
254+
utfString = IntPtr.Zero;
255+
len = 0;
256+
return;
257+
}
258+
259+
utfString = Utilities.StringToUtf8StringPtr (comment, out int count);
260+
len = count > UInt16.MaxValue ? UInt16.MaxValue : (UInt16)count;
261+
}
262+
263+
[DllImport (ZIP_LIBNAME, CallingConvention = CallingConvention.Cdecl)]
264+
public static extern int zip_file_set_comment (IntPtr archive, UInt64 index, IntPtr comment, UInt16 len, OperationFlags flags);
265+
266+
public static int zip_file_set_comment (IntPtr archive, UInt64 index, string comment)
267+
{
268+
StringToComment (comment, out IntPtr utfComment, out UInt16 len);
269+
270+
try {
271+
return zip_file_set_comment (archive, index, utfComment, len, OperationFlags.Enc_UTF_8);
272+
} finally {
273+
Utilities.FreeUtf8StringPtr (utfComment);
274+
}
275+
}
276+
211277
[DllImport (ZIP_LIBNAME, CallingConvention = CallingConvention.Cdecl, EntryPoint="zip_file_get_comment")]
212278
public static extern IntPtr zip_file_get_comment_ptr (IntPtr archive, UInt64 index, out UInt32 lenp, OperationFlags flags);
213279

214-
public static string zip_file_get_comment (IntPtr archive, UInt64 index, out UInt32 lenp, OperationFlags flags)
280+
public static string zip_file_get_comment (IntPtr archive, UInt64 index, out UInt32 lenp, OperationFlags flags = OperationFlags.Enc_Guess)
215281
{
216282
return Utilities.Utf8StringPtrToString (zip_file_get_comment_ptr (archive, index, out lenp, flags));
217283
}
218284

219285
[DllImport (ZIP_LIBNAME, CallingConvention = CallingConvention.Cdecl, EntryPoint="zip_get_archive_comment")]
220286
public static extern IntPtr zip_get_archive_comment_ptr (IntPtr archive, out int lenp, OperationFlags flags);
221287

222-
public static string zip_get_archive_comment (IntPtr archive, out int lenp, OperationFlags flags)
288+
public static string zip_get_archive_comment (IntPtr archive, OperationFlags flags = OperationFlags.Enc_Guess)
223289
{
224-
return Utilities.Utf8StringPtrToString (zip_get_archive_comment_ptr (archive, out lenp, flags));
290+
return Utilities.Utf8StringPtrToString (zip_get_archive_comment_ptr (archive, out int _, flags));
291+
}
292+
293+
[DllImport (ZIP_LIBNAME, CallingConvention = CallingConvention.Cdecl)]
294+
public static extern int zip_set_archive_comment (IntPtr archive, IntPtr comment, UInt16 len);
295+
296+
public static int zip_set_archive_comment (IntPtr archive, string comment)
297+
{
298+
StringToComment (comment, out IntPtr utfComment, out UInt16 len);
299+
300+
try {
301+
return zip_set_archive_comment (archive, utfComment, len);
302+
} finally {
303+
Utilities.FreeUtf8StringPtr (utfComment);
304+
}
225305
}
226306

227307
[DllImport (ZIP_LIBNAME, CallingConvention = CallingConvention.Cdecl)]
@@ -242,13 +322,13 @@ public static string zip_get_name (IntPtr archive, UInt64 index, OperationFlags
242322
public static extern int zip_set_default_password (IntPtr archive, string password);
243323

244324
[DllImport (ZIP_LIBNAME, CallingConvention = CallingConvention.Cdecl)]
245-
public static extern int zip_rename (IntPtr archive, UInt64 index, IntPtr name);
325+
public static extern int zip_file_rename (IntPtr archive, UInt64 index, IntPtr name, OperationFlags flags);
246326

247-
public static int zip_rename (IntPtr archive, UInt64 index, string name)
327+
public static int zip_file_rename (IntPtr archive, UInt64 index, string name)
248328
{
249329
var utfName = Utilities.StringToUtf8StringPtr (name);
250330
try {
251-
return zip_rename (archive, index, utfName);
331+
return zip_file_rename (archive, index, utfName, OperationFlags.Enc_UTF_8);
252332
} finally {
253333
Utilities.FreeUtf8StringPtr (utfName);
254334
}
@@ -387,19 +467,6 @@ public static Int64 zip_file_add (IntPtr archive, string name, IntPtr source, Op
387467
[DllImport (ZIP_LIBNAME, CallingConvention = CallingConvention.Cdecl)]
388468
public static extern int zip_file_replace (IntPtr archive, UInt64 index, IntPtr source, OperationFlags flags);
389469

390-
[DllImport (ZIP_LIBNAME, CallingConvention = CallingConvention.Cdecl)]
391-
public static extern int zip_set_file_comment (IntPtr archive, UInt64 index, IntPtr comment, UInt16 len, OperationFlags flags);
392-
393-
public static int zip_set_file_comment (IntPtr archive, UInt64 index, string comment, UInt16 len, OperationFlags flags)
394-
{
395-
IntPtr utfComment = Utilities.StringToUtf8StringPtr (comment);
396-
try {
397-
return zip_set_file_comment (archive, index, utfComment, len, flags);
398-
} finally {
399-
Utilities.FreeUtf8StringPtr (utfComment);
400-
}
401-
}
402-
403470
[DllImport (ZIP_LIBNAME, CallingConvention = CallingConvention.Cdecl)]
404471
public static extern int zip_set_file_compression (IntPtr archive, UInt64 index, CompressionMethod comp, UInt32 comp_flags);
405472

0 commit comments

Comments
 (0)