2525// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
2626// THE SOFTWARE.
2727using System ;
28+ using System . IO ;
2829using 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