Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Updates to match new libzip version + potential fix #125

Merged
merged 3 commits into from
Feb 8, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -459,11 +459,19 @@ else()
add_library(
${PROJECT_NAME}
SHARED
native/sizes.cc
native/values.cc
native/version.cc
)

add_dependencies(${PROJECT_NAME} zip)

target_include_directories(
${PROJECT_NAME}
PRIVATE
"external/libzip/lib"
)

target_compile_definitions(
${PROJECT_NAME}
PRIVATE
Expand Down
2 changes: 1 addition & 1 deletion LibZipSharp.props
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<Project DefaultTargets="Build" ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<PropertyGroup>
<_LibZipSharpAssemblyVersion>2.0.8</_LibZipSharpAssemblyVersion>
<_LibZipSharpAssemblyVersion>2.1.0</_LibZipSharpAssemblyVersion>
<!--
Nuget Version. You can append things like -alpha-1 etc to this value.
But always leave the $(_LibZipSharpAssemblyVersion) value at the start.
Expand Down
62 changes: 47 additions & 15 deletions LibZipSharp/Xamarin.Tools.Zip/EncryptionMethod.cs
Original file line number Diff line number Diff line change
Expand Up @@ -45,62 +45,94 @@ public enum EncryptionMethod : ushort

/// <summary>
/// Strong encryption: DES
/// <remarks>Not supported by the native libzip yet (as of v1.0.1)</remarks>
/// <remarks>Not supported by the native libzip yet (as of v1.9.2)</remarks>
/// </summary>
DES = 0x6601,

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

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

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

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

/// <summary>
/// Strong encryption: AES (192-bit key)
/// <remarks>Not supported by the native libzip yet (as of v1.0.1)</remarks>
/// Deprecated alias for <see cref="AES_128"/>
/// </summary>
AES_192 = 0x660f,
[Obsolete ("Use EncryptionMethod.PKZIP_AES_128")]
AES_128 = PKZIP_AES_128,

/// <summary>
/// Strong encryption: PKZIP AES (192-bit key)
/// <remarks>Not supported by the native libzip yet (as of v1.9.2)</remarks>
/// </summary>
PKZIP_AES_192 = 0x660f,

/// <summary>
/// Deprecated alias for <see cref="AES_192"/>
/// </summary>
[Obsolete ("Use EncryptionMethod.PKZIP_AES_192")]
AES_192 = PKZIP_AES_192,

/// <summary>
/// Strong encryption: AES (256-bit key)
/// <remarks>Not supported by the native libzip yet (as of v1.0.1)</remarks>
/// <remarks>Not supported by the native libzip yet (as of v1.9.2)</remarks>
/// </summary>
AES_256 = 0x6610,
PKZIP_AES_256 = 0x6610,

/// <summary>
/// Deprecated alias for <see cref="AES_256"/>
/// </summary>
[Obsolete ("Use EncryptionMethod.PKZIP_AES_256")]
AES_256 = PKZIP_AES_256,

/// <summary>
/// Strong encryption: RC2, version >= 5.2
/// <remarks>Not supported by the native libzip yet (as of v1.0.1)</remarks>
/// <remarks>Not supported by the native libzip yet (as of v1.9.2)</remarks>
/// </summary>
RC2 = 0x6702,

/// <summary>
/// Strong encryption: RC4
/// <remarks>Not supported by the native libzip yet (as of v1.0.1)</remarks>
/// <remarks>Not supported by the native libzip yet (as of v1.9.2)</remarks>
/// </summary>
RC4 = 0x6801,

/// <summary>
/// WinZIP AES encryption (128-bit key)
/// </summary>
WINZIP_AES_128 = 0x0101,

/// <summary>
/// WinZIP AES encryption (192-bit key)
/// </summary>
WINZIP_AES_192 = 0x0102,

/// <summary>
/// WinZIP AES encryption (256-bit key)
/// </summary>
WINZIP_AES_256 = 0x0103,

/// <summary>
/// Unknown algorithm
/// </summary>
Unknown = 0xffff,
}
}

3 changes: 1 addition & 2 deletions LibZipSharp/Xamarin.Tools.Zip/EntryPermissions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,11 @@
using System;
namespace Xamarin.Tools.Zip
{
[Flags]
/// <summary>
/// ZIP (as opposed to filesystem) entry permission bits. The bits are used only
/// on the Unix systems.
/// </summary>
[Flags]
public enum EntryPermissions : uint
{
Default = 0,
Expand All @@ -55,4 +55,3 @@ public enum EntryPermissions : uint
SetDirectoryPermissionControl = UnixExternalPermissions.ISVTX,
}
}

10 changes: 10 additions & 0 deletions LibZipSharp/Xamarin.Tools.Zip/ErrorCode.cs
Original file line number Diff line number Diff line change
Expand Up @@ -192,5 +192,15 @@ public enum ErrorCode
/// Tell error
/// </summary>
Tell = 30,

/// <summary>
/// Compressed data invalid
/// </summary>
CompressedData = 31,

/// <summary>
/// Operation cancelled
/// </summary>
Cancelled = 32,
}
}
6 changes: 5 additions & 1 deletion LibZipSharp/Xamarin.Tools.Zip/ErrorType.cs
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,10 @@ public enum ErrorType
/// Error code field is zlib error code
/// </summary>
Zlib = 2,

/// <summary>
/// Error code field is libzip error code
/// </summary>
Libzip = 3,
}
}

5 changes: 3 additions & 2 deletions LibZipSharp/Xamarin.Tools.Zip/IPlatformServices.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
using System;
using System.Collections.Generic;

namespace Xamarin.Tools.Zip
Expand All @@ -37,8 +36,10 @@ public interface IPlatformServices
/// Checks whether the filesystem location identified by <paramref name="path"/> is a regular
/// file. Irregular files include device nodes, sockets, character or block devices on Unix systems etc.
/// </summary>
/// <returns><c>true</c> if <paramref name="path"/> points to regular file</returns>
/// <returns><c>true</c> if operation was successful, <c>false</c> otherwise</returns>
/// <param name="archive">ZipArchive to operate on</param>
/// <param name="path">Path to the filesystem location</param>
/// <param name="result"><c>true</c> if <paramref name="path"/> points to regular file</param>
bool IsRegularFile (ZipArchive archive, string path, out bool result);
bool IsDirectory (ZipArchive archive, string path, out bool result);
bool GetFilesystemPermissions (ZipArchive archive, string path, out EntryPermissions permissions);
Expand Down
109 changes: 88 additions & 21 deletions LibZipSharp/Xamarin.Tools.Zip/Native.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
using System;
using System.IO;
using System.Runtime.InteropServices;

[assembly: DefaultDllImportSearchPathsAttribute(DllImportSearchPath.SafeDirectories | DllImportSearchPath.AssemblyDirectory)]
Expand All @@ -33,6 +34,11 @@ namespace Xamarin.Tools.Zip
{
internal class Native
{
const UInt32 LZS_SEEK_SET = 0;
const UInt32 LZS_SEEK_CUR = 1;
const UInt32 LZS_SEEK_END = 2;
const UInt32 LZS_SEEK_INVALID = 0xDEADBEEFu;

[StructLayout (LayoutKind.Sequential)]
public struct LZSVersions
{
Expand All @@ -53,6 +59,7 @@ public struct zip_error_t
public IntPtr str; /* string representation or NULL */
};

[StructLayout (LayoutKind.Sequential)]
public struct zip_source_args_seek_t
{
public UInt64 offset;
Expand Down Expand Up @@ -92,16 +99,49 @@ public static int ZipSourceMakeCommandBitmask (SourceCommand cmd)
return 1 << (int)cmd;
}

public static T ZipSourceGetArgs<T> (IntPtr data, UInt64 len)
public unsafe static bool ZipSourceGetArgs<T> (IntPtr data, UInt64 len, out T ret) where T : unmanaged
{
return (T)Marshal.PtrToStructure (data, typeof (T));
ret = default(T);
if (data == IntPtr.Zero) {
return false;
}

if (len < (ulong)sizeof (T)) {
return false;
}

ret = (T)Marshal.PtrToStructure (data, typeof (T));
return true;
}

const string ZIP_LIBNAME = "libZipSharpNative";

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

[DllImport (ZIP_LIBNAME, CallingConvention = CallingConvention.Cdecl)]
public static extern UInt64 lzs_get_size_zip_source_args_seek ();

[DllImport (ZIP_LIBNAME, CallingConvention = CallingConvention.Cdecl)]
public static extern UInt32 lzs_convert_whence_value (Int32 whence);

public static SeekOrigin ConvertWhence (Int32 whence)
{
switch (lzs_convert_whence_value (whence)) {
case LZS_SEEK_SET:
return SeekOrigin.Begin;

case LZS_SEEK_CUR:
return SeekOrigin.Current;

case LZS_SEEK_END:
return SeekOrigin.End;

default:
throw new InvalidOperationException ($"Invalid whence value: {whence}");
}
}

public static Versions get_versions ()
{
lzs_get_versions (out LZSVersions ret);
Expand Down Expand Up @@ -208,20 +248,60 @@ public static int zip_stat (IntPtr archive, string fname, OperationFlags flags,
[DllImport (ZIP_LIBNAME, CallingConvention = CallingConvention.Cdecl)]
public static extern int zip_stat_index (IntPtr archive, UInt64 index, OperationFlags flags, out zip_stat_t sb);

static void StringToComment (string comment, out IntPtr utfString, out UInt16 len)
{
if (comment == null) {
utfString = IntPtr.Zero;
len = 0;
return;
}

utfString = Utilities.StringToUtf8StringPtr (comment, out int count);
len = count > UInt16.MaxValue ? UInt16.MaxValue : (UInt16)count;
}

[DllImport (ZIP_LIBNAME, CallingConvention = CallingConvention.Cdecl)]
public static extern int zip_file_set_comment (IntPtr archive, UInt64 index, IntPtr comment, UInt16 len, OperationFlags flags);

public static int zip_file_set_comment (IntPtr archive, UInt64 index, string comment)
{
StringToComment (comment, out IntPtr utfComment, out UInt16 len);

try {
return zip_file_set_comment (archive, index, utfComment, len, OperationFlags.Enc_UTF_8);
} finally {
Utilities.FreeUtf8StringPtr (utfComment);
}
}

[DllImport (ZIP_LIBNAME, CallingConvention = CallingConvention.Cdecl, EntryPoint="zip_file_get_comment")]
public static extern IntPtr zip_file_get_comment_ptr (IntPtr archive, UInt64 index, out UInt32 lenp, OperationFlags flags);

public static string zip_file_get_comment (IntPtr archive, UInt64 index, out UInt32 lenp, OperationFlags flags)
public static string zip_file_get_comment (IntPtr archive, UInt64 index, out UInt32 lenp, OperationFlags flags = OperationFlags.Enc_Guess)
{
return Utilities.Utf8StringPtrToString (zip_file_get_comment_ptr (archive, index, out lenp, flags));
}

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

public static string zip_get_archive_comment (IntPtr archive, out int lenp, OperationFlags flags)
public static string zip_get_archive_comment (IntPtr archive, OperationFlags flags = OperationFlags.Enc_Guess)
{
return Utilities.Utf8StringPtrToString (zip_get_archive_comment_ptr (archive, out lenp, flags));
return Utilities.Utf8StringPtrToString (zip_get_archive_comment_ptr (archive, out int _, flags));
}

[DllImport (ZIP_LIBNAME, CallingConvention = CallingConvention.Cdecl)]
public static extern int zip_set_archive_comment (IntPtr archive, IntPtr comment, UInt16 len);

public static int zip_set_archive_comment (IntPtr archive, string comment)
{
StringToComment (comment, out IntPtr utfComment, out UInt16 len);

try {
return zip_set_archive_comment (archive, utfComment, len);
} finally {
Utilities.FreeUtf8StringPtr (utfComment);
}
}

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

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

public static int zip_rename (IntPtr archive, UInt64 index, string name)
public static int zip_file_rename (IntPtr archive, UInt64 index, string name)
{
var utfName = Utilities.StringToUtf8StringPtr (name);
try {
return zip_rename (archive, index, utfName);
return zip_file_rename (archive, index, utfName, OperationFlags.Enc_UTF_8);
} finally {
Utilities.FreeUtf8StringPtr (utfName);
}
Expand Down Expand Up @@ -387,19 +467,6 @@ public static Int64 zip_file_add (IntPtr archive, string name, IntPtr source, Op
[DllImport (ZIP_LIBNAME, CallingConvention = CallingConvention.Cdecl)]
public static extern int zip_file_replace (IntPtr archive, UInt64 index, IntPtr source, OperationFlags flags);

[DllImport (ZIP_LIBNAME, CallingConvention = CallingConvention.Cdecl)]
public static extern int zip_set_file_comment (IntPtr archive, UInt64 index, IntPtr comment, UInt16 len, OperationFlags flags);

public static int zip_set_file_comment (IntPtr archive, UInt64 index, string comment, UInt16 len, OperationFlags flags)
{
IntPtr utfComment = Utilities.StringToUtf8StringPtr (comment);
try {
return zip_set_file_comment (archive, index, utfComment, len, flags);
} finally {
Utilities.FreeUtf8StringPtr (utfComment);
}
}

[DllImport (ZIP_LIBNAME, CallingConvention = CallingConvention.Cdecl)]
public static extern int zip_set_file_compression (IntPtr archive, UInt64 index, CompressionMethod comp, UInt32 comp_flags);

Expand Down
Loading