diff --git a/Zip.xcodeproj/project.pbxproj b/Zip.xcodeproj/project.pbxproj index a1edad5d35666da33168eefa19ebcb060ff8cb91..43283b91a6dec3e2839028723c57cf8e6d9c4444 100644 --- a/Zip.xcodeproj/project.pbxproj +++ b/Zip.xcodeproj/project.pbxproj @@ -7,6 +7,28 @@ objects = { /* Begin PBXBuildFile section */ + 3430BB261C484A65001143B5 /* aes.h in Headers */ = {isa = PBXBuildFile; fileRef = 3430BB101C484A65001143B5 /* aes.h */; }; + 3430BB271C484A65001143B5 /* aes_via_ace.h in Headers */ = {isa = PBXBuildFile; fileRef = 3430BB111C484A65001143B5 /* aes_via_ace.h */; }; + 3430BB281C484A65001143B5 /* aescrypt.c in Sources */ = {isa = PBXBuildFile; fileRef = 3430BB121C484A65001143B5 /* aescrypt.c */; }; + 3430BB291C484A65001143B5 /* aeskey.c in Sources */ = {isa = PBXBuildFile; fileRef = 3430BB131C484A65001143B5 /* aeskey.c */; }; + 3430BB2A1C484A65001143B5 /* aesopt.h in Headers */ = {isa = PBXBuildFile; fileRef = 3430BB141C484A65001143B5 /* aesopt.h */; }; + 3430BB2B1C484A65001143B5 /* aestab.c in Sources */ = {isa = PBXBuildFile; fileRef = 3430BB151C484A65001143B5 /* aestab.c */; }; + 3430BB2C1C484A65001143B5 /* aestab.h in Headers */ = {isa = PBXBuildFile; fileRef = 3430BB161C484A65001143B5 /* aestab.h */; }; + 3430BB2D1C484A65001143B5 /* brg_endian.h in Headers */ = {isa = PBXBuildFile; fileRef = 3430BB171C484A65001143B5 /* brg_endian.h */; }; + 3430BB2E1C484A65001143B5 /* brg_types.h in Headers */ = {isa = PBXBuildFile; fileRef = 3430BB181C484A65001143B5 /* brg_types.h */; }; + 3430BB2F1C484A65001143B5 /* entropy.c in Sources */ = {isa = PBXBuildFile; fileRef = 3430BB191C484A65001143B5 /* entropy.c */; }; + 3430BB301C484A65001143B5 /* entropy.h in Headers */ = {isa = PBXBuildFile; fileRef = 3430BB1A1C484A65001143B5 /* entropy.h */; }; + 3430BB311C484A65001143B5 /* fileenc.c in Sources */ = {isa = PBXBuildFile; fileRef = 3430BB1B1C484A65001143B5 /* fileenc.c */; }; + 3430BB321C484A65001143B5 /* fileenc.h in Headers */ = {isa = PBXBuildFile; fileRef = 3430BB1C1C484A65001143B5 /* fileenc.h */; }; + 3430BB331C484A65001143B5 /* hmac.c in Sources */ = {isa = PBXBuildFile; fileRef = 3430BB1D1C484A65001143B5 /* hmac.c */; }; + 3430BB341C484A65001143B5 /* hmac.h in Headers */ = {isa = PBXBuildFile; fileRef = 3430BB1E1C484A65001143B5 /* hmac.h */; }; + 3430BB351C484A65001143B5 /* Makefile in Sources */ = {isa = PBXBuildFile; fileRef = 3430BB1F1C484A65001143B5 /* Makefile */; }; + 3430BB361C484A65001143B5 /* prng.c in Sources */ = {isa = PBXBuildFile; fileRef = 3430BB201C484A65001143B5 /* prng.c */; }; + 3430BB371C484A65001143B5 /* prng.h in Headers */ = {isa = PBXBuildFile; fileRef = 3430BB211C484A65001143B5 /* prng.h */; }; + 3430BB381C484A65001143B5 /* pwd2key.c in Sources */ = {isa = PBXBuildFile; fileRef = 3430BB221C484A65001143B5 /* pwd2key.c */; }; + 3430BB391C484A65001143B5 /* pwd2key.h in Headers */ = {isa = PBXBuildFile; fileRef = 3430BB231C484A65001143B5 /* pwd2key.h */; }; + 3430BB3A1C484A65001143B5 /* sha1.c in Sources */ = {isa = PBXBuildFile; fileRef = 3430BB241C484A65001143B5 /* sha1.c */; }; + 3430BB3B1C484A65001143B5 /* sha1.h in Headers */ = {isa = PBXBuildFile; fileRef = 3430BB251C484A65001143B5 /* sha1.h */; }; 3430F6201C45C805007473A6 /* libz.tbd in Frameworks */ = {isa = PBXBuildFile; fileRef = 3430F61F1C45C805007473A6 /* libz.tbd */; }; 3430F62D1C45C851007473A6 /* crypt.h in Headers */ = {isa = PBXBuildFile; fileRef = 3430F6221C45C851007473A6 /* crypt.h */; }; 3430F62E1C45C851007473A6 /* include.h in Headers */ = {isa = PBXBuildFile; fileRef = 3430F6231C45C851007473A6 /* include.h */; }; @@ -19,27 +41,6 @@ 3430F6351C45C851007473A6 /* zip.c in Sources */ = {isa = PBXBuildFile; fileRef = 3430F62B1C45C851007473A6 /* zip.c */; }; 3430F6361C45C851007473A6 /* zip.h in Headers */ = {isa = PBXBuildFile; fileRef = 3430F62C1C45C851007473A6 /* zip.h */; }; 3430F6381C45C89A007473A6 /* Common.h in Headers */ = {isa = PBXBuildFile; fileRef = 3430F6371C45C89A007473A6 /* Common.h */; }; - 3430F64F1C45C8AD007473A6 /* aes.h in Headers */ = {isa = PBXBuildFile; fileRef = 3430F63A1C45C8AD007473A6 /* aes.h */; }; - 3430F6501C45C8AD007473A6 /* aes_via_ace.h in Headers */ = {isa = PBXBuildFile; fileRef = 3430F63B1C45C8AD007473A6 /* aes_via_ace.h */; }; - 3430F6511C45C8AD007473A6 /* aescrypt.c in Sources */ = {isa = PBXBuildFile; fileRef = 3430F63C1C45C8AD007473A6 /* aescrypt.c */; }; - 3430F6521C45C8AD007473A6 /* aeskey.c in Sources */ = {isa = PBXBuildFile; fileRef = 3430F63D1C45C8AD007473A6 /* aeskey.c */; }; - 3430F6531C45C8AD007473A6 /* aesopt.h in Headers */ = {isa = PBXBuildFile; fileRef = 3430F63E1C45C8AD007473A6 /* aesopt.h */; }; - 3430F6541C45C8AD007473A6 /* aestab.c in Sources */ = {isa = PBXBuildFile; fileRef = 3430F63F1C45C8AD007473A6 /* aestab.c */; }; - 3430F6551C45C8AD007473A6 /* aestab.h in Headers */ = {isa = PBXBuildFile; fileRef = 3430F6401C45C8AD007473A6 /* aestab.h */; }; - 3430F6561C45C8AD007473A6 /* brg_endian.h in Headers */ = {isa = PBXBuildFile; fileRef = 3430F6411C45C8AD007473A6 /* brg_endian.h */; }; - 3430F6571C45C8AD007473A6 /* brg_types.h in Headers */ = {isa = PBXBuildFile; fileRef = 3430F6421C45C8AD007473A6 /* brg_types.h */; }; - 3430F6581C45C8AD007473A6 /* entropy.c in Sources */ = {isa = PBXBuildFile; fileRef = 3430F6431C45C8AD007473A6 /* entropy.c */; }; - 3430F6591C45C8AD007473A6 /* entropy.h in Headers */ = {isa = PBXBuildFile; fileRef = 3430F6441C45C8AD007473A6 /* entropy.h */; }; - 3430F65A1C45C8AD007473A6 /* fileenc.c in Sources */ = {isa = PBXBuildFile; fileRef = 3430F6451C45C8AD007473A6 /* fileenc.c */; }; - 3430F65B1C45C8AD007473A6 /* fileenc.h in Headers */ = {isa = PBXBuildFile; fileRef = 3430F6461C45C8AD007473A6 /* fileenc.h */; }; - 3430F65C1C45C8AD007473A6 /* hmac.c in Sources */ = {isa = PBXBuildFile; fileRef = 3430F6471C45C8AD007473A6 /* hmac.c */; }; - 3430F65D1C45C8AD007473A6 /* hmac.h in Headers */ = {isa = PBXBuildFile; fileRef = 3430F6481C45C8AD007473A6 /* hmac.h */; }; - 3430F65E1C45C8AD007473A6 /* prng.c in Sources */ = {isa = PBXBuildFile; fileRef = 3430F6491C45C8AD007473A6 /* prng.c */; }; - 3430F65F1C45C8AD007473A6 /* prng.h in Headers */ = {isa = PBXBuildFile; fileRef = 3430F64A1C45C8AD007473A6 /* prng.h */; }; - 3430F6601C45C8AD007473A6 /* pwd2key.c in Sources */ = {isa = PBXBuildFile; fileRef = 3430F64B1C45C8AD007473A6 /* pwd2key.c */; }; - 3430F6611C45C8AD007473A6 /* pwd2key.h in Headers */ = {isa = PBXBuildFile; fileRef = 3430F64C1C45C8AD007473A6 /* pwd2key.h */; }; - 3430F6621C45C8AD007473A6 /* sha1.c in Sources */ = {isa = PBXBuildFile; fileRef = 3430F64D1C45C8AD007473A6 /* sha1.c */; }; - 3430F6631C45C8AD007473A6 /* sha1.h in Headers */ = {isa = PBXBuildFile; fileRef = 3430F64E1C45C8AD007473A6 /* sha1.h */; }; 347E3A781C1DFFB500A11FD3 /* Zip.h in Headers */ = {isa = PBXBuildFile; fileRef = 347E3A771C1DFFB500A11FD3 /* Zip.h */; settings = {ATTRIBUTES = (Public, ); }; }; 347E3A7F1C1DFFB500A11FD3 /* Zip.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 347E3A741C1DFFB500A11FD3 /* Zip.framework */; }; 347E3A841C1DFFB500A11FD3 /* ZipTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 347E3A831C1DFFB500A11FD3 /* ZipTests.swift */; }; @@ -57,6 +58,28 @@ /* End PBXContainerItemProxy section */ /* Begin PBXFileReference section */ + 3430BB101C484A65001143B5 /* aes.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = aes.h; sourceTree = ""; }; + 3430BB111C484A65001143B5 /* aes_via_ace.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = aes_via_ace.h; sourceTree = ""; }; + 3430BB121C484A65001143B5 /* aescrypt.c */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.c; path = aescrypt.c; sourceTree = ""; }; + 3430BB131C484A65001143B5 /* aeskey.c */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.c; path = aeskey.c; sourceTree = ""; }; + 3430BB141C484A65001143B5 /* aesopt.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = aesopt.h; sourceTree = ""; }; + 3430BB151C484A65001143B5 /* aestab.c */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.c; path = aestab.c; sourceTree = ""; }; + 3430BB161C484A65001143B5 /* aestab.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = aestab.h; sourceTree = ""; }; + 3430BB171C484A65001143B5 /* brg_endian.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = brg_endian.h; sourceTree = ""; }; + 3430BB181C484A65001143B5 /* brg_types.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = brg_types.h; sourceTree = ""; }; + 3430BB191C484A65001143B5 /* entropy.c */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.c; path = entropy.c; sourceTree = ""; }; + 3430BB1A1C484A65001143B5 /* entropy.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = entropy.h; sourceTree = ""; }; + 3430BB1B1C484A65001143B5 /* fileenc.c */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.c; path = fileenc.c; sourceTree = ""; }; + 3430BB1C1C484A65001143B5 /* fileenc.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = fileenc.h; sourceTree = ""; }; + 3430BB1D1C484A65001143B5 /* hmac.c */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.c; path = hmac.c; sourceTree = ""; }; + 3430BB1E1C484A65001143B5 /* hmac.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = hmac.h; sourceTree = ""; }; + 3430BB1F1C484A65001143B5 /* Makefile */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.make; path = Makefile; sourceTree = ""; }; + 3430BB201C484A65001143B5 /* prng.c */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.c; path = prng.c; sourceTree = ""; }; + 3430BB211C484A65001143B5 /* prng.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = prng.h; sourceTree = ""; }; + 3430BB221C484A65001143B5 /* pwd2key.c */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.c; path = pwd2key.c; sourceTree = ""; }; + 3430BB231C484A65001143B5 /* pwd2key.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = pwd2key.h; sourceTree = ""; }; + 3430BB241C484A65001143B5 /* sha1.c */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.c; path = sha1.c; sourceTree = ""; }; + 3430BB251C484A65001143B5 /* sha1.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = sha1.h; sourceTree = ""; }; 3430F61F1C45C805007473A6 /* libz.tbd */ = {isa = PBXFileReference; lastKnownFileType = "sourcecode.text-based-dylib-definition"; name = libz.tbd; path = usr/lib/libz.tbd; sourceTree = SDKROOT; }; 3430F6221C45C851007473A6 /* crypt.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = crypt.h; sourceTree = ""; }; 3430F6231C45C851007473A6 /* include.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = include.h; sourceTree = ""; }; @@ -70,27 +93,6 @@ 3430F62B1C45C851007473A6 /* zip.c */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.c; path = zip.c; sourceTree = ""; }; 3430F62C1C45C851007473A6 /* zip.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = zip.h; sourceTree = ""; }; 3430F6371C45C89A007473A6 /* Common.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = Common.h; sourceTree = ""; }; - 3430F63A1C45C8AD007473A6 /* aes.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = aes.h; sourceTree = ""; }; - 3430F63B1C45C8AD007473A6 /* aes_via_ace.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = aes_via_ace.h; sourceTree = ""; }; - 3430F63C1C45C8AD007473A6 /* aescrypt.c */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.c; path = aescrypt.c; sourceTree = ""; }; - 3430F63D1C45C8AD007473A6 /* aeskey.c */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.c; path = aeskey.c; sourceTree = ""; }; - 3430F63E1C45C8AD007473A6 /* aesopt.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = aesopt.h; sourceTree = ""; }; - 3430F63F1C45C8AD007473A6 /* aestab.c */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.c; path = aestab.c; sourceTree = ""; }; - 3430F6401C45C8AD007473A6 /* aestab.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = aestab.h; sourceTree = ""; }; - 3430F6411C45C8AD007473A6 /* brg_endian.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = brg_endian.h; sourceTree = ""; }; - 3430F6421C45C8AD007473A6 /* brg_types.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = brg_types.h; sourceTree = ""; }; - 3430F6431C45C8AD007473A6 /* entropy.c */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.c; path = entropy.c; sourceTree = ""; }; - 3430F6441C45C8AD007473A6 /* entropy.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = entropy.h; sourceTree = ""; }; - 3430F6451C45C8AD007473A6 /* fileenc.c */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.c; path = fileenc.c; sourceTree = ""; }; - 3430F6461C45C8AD007473A6 /* fileenc.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = fileenc.h; sourceTree = ""; }; - 3430F6471C45C8AD007473A6 /* hmac.c */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.c; path = hmac.c; sourceTree = ""; }; - 3430F6481C45C8AD007473A6 /* hmac.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = hmac.h; sourceTree = ""; }; - 3430F6491C45C8AD007473A6 /* prng.c */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.c; path = prng.c; sourceTree = ""; }; - 3430F64A1C45C8AD007473A6 /* prng.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = prng.h; sourceTree = ""; }; - 3430F64B1C45C8AD007473A6 /* pwd2key.c */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.c; path = pwd2key.c; sourceTree = ""; }; - 3430F64C1C45C8AD007473A6 /* pwd2key.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = pwd2key.h; sourceTree = ""; }; - 3430F64D1C45C8AD007473A6 /* sha1.c */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.c; path = sha1.c; sourceTree = ""; }; - 3430F64E1C45C8AD007473A6 /* sha1.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = sha1.h; sourceTree = ""; }; 347E3A741C1DFFB500A11FD3 /* Zip.framework */ = {isa = PBXFileReference; explicitFileType = wrapper.framework; includeInIndex = 0; path = Zip.framework; sourceTree = BUILT_PRODUCTS_DIR; }; 347E3A771C1DFFB500A11FD3 /* Zip.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = Zip.h; sourceTree = ""; }; 347E3A791C1DFFB500A11FD3 /* Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = ""; }; @@ -120,9 +122,39 @@ /* End PBXFrameworksBuildPhase section */ /* Begin PBXGroup section */ + 3430BB0F1C484A65001143B5 /* aes */ = { + isa = PBXGroup; + children = ( + 3430BB101C484A65001143B5 /* aes.h */, + 3430BB111C484A65001143B5 /* aes_via_ace.h */, + 3430BB121C484A65001143B5 /* aescrypt.c */, + 3430BB131C484A65001143B5 /* aeskey.c */, + 3430BB141C484A65001143B5 /* aesopt.h */, + 3430BB151C484A65001143B5 /* aestab.c */, + 3430BB161C484A65001143B5 /* aestab.h */, + 3430BB171C484A65001143B5 /* brg_endian.h */, + 3430BB181C484A65001143B5 /* brg_types.h */, + 3430BB191C484A65001143B5 /* entropy.c */, + 3430BB1A1C484A65001143B5 /* entropy.h */, + 3430BB1B1C484A65001143B5 /* fileenc.c */, + 3430BB1C1C484A65001143B5 /* fileenc.h */, + 3430BB1D1C484A65001143B5 /* hmac.c */, + 3430BB1E1C484A65001143B5 /* hmac.h */, + 3430BB1F1C484A65001143B5 /* Makefile */, + 3430BB201C484A65001143B5 /* prng.c */, + 3430BB211C484A65001143B5 /* prng.h */, + 3430BB221C484A65001143B5 /* pwd2key.c */, + 3430BB231C484A65001143B5 /* pwd2key.h */, + 3430BB241C484A65001143B5 /* sha1.c */, + 3430BB251C484A65001143B5 /* sha1.h */, + ); + path = aes; + sourceTree = ""; + }; 3430F6211C45C83F007473A6 /* minizip */ = { isa = PBXGroup; children = ( + 3430BB0F1C484A65001143B5 /* aes */, 3430F6371C45C89A007473A6 /* Common.h */, 3430F6221C45C851007473A6 /* crypt.h */, 3430F6231C45C851007473A6 /* include.h */, @@ -139,34 +171,6 @@ path = minizip; sourceTree = ""; }; - 3430F6391C45C8AD007473A6 /* aes */ = { - isa = PBXGroup; - children = ( - 3430F63A1C45C8AD007473A6 /* aes.h */, - 3430F63B1C45C8AD007473A6 /* aes_via_ace.h */, - 3430F63C1C45C8AD007473A6 /* aescrypt.c */, - 3430F63D1C45C8AD007473A6 /* aeskey.c */, - 3430F63E1C45C8AD007473A6 /* aesopt.h */, - 3430F63F1C45C8AD007473A6 /* aestab.c */, - 3430F6401C45C8AD007473A6 /* aestab.h */, - 3430F6411C45C8AD007473A6 /* brg_endian.h */, - 3430F6421C45C8AD007473A6 /* brg_types.h */, - 3430F6431C45C8AD007473A6 /* entropy.c */, - 3430F6441C45C8AD007473A6 /* entropy.h */, - 3430F6451C45C8AD007473A6 /* fileenc.c */, - 3430F6461C45C8AD007473A6 /* fileenc.h */, - 3430F6471C45C8AD007473A6 /* hmac.c */, - 3430F6481C45C8AD007473A6 /* hmac.h */, - 3430F6491C45C8AD007473A6 /* prng.c */, - 3430F64A1C45C8AD007473A6 /* prng.h */, - 3430F64B1C45C8AD007473A6 /* pwd2key.c */, - 3430F64C1C45C8AD007473A6 /* pwd2key.h */, - 3430F64D1C45C8AD007473A6 /* sha1.c */, - 3430F64E1C45C8AD007473A6 /* sha1.h */, - ); - path = aes; - sourceTree = ""; - }; 347E3A6A1C1DFFB500A11FD3 = { isa = PBXGroup; children = ( @@ -189,7 +193,6 @@ 347E3A761C1DFFB500A11FD3 /* Zip */ = { isa = PBXGroup; children = ( - 3430F6391C45C8AD007473A6 /* aes */, 3430F6211C45C83F007473A6 /* minizip */, 347E3A771C1DFFB500A11FD3 /* Zip.h */, 347E3A791C1DFFB500A11FD3 /* Info.plist */, @@ -214,25 +217,25 @@ isa = PBXHeadersBuildPhase; buildActionMask = 2147483647; files = ( - 3430F65D1C45C8AD007473A6 /* hmac.h in Headers */, - 3430F65F1C45C8AD007473A6 /* prng.h in Headers */, - 3430F6561C45C8AD007473A6 /* brg_endian.h in Headers */, + 3430BB341C484A65001143B5 /* hmac.h in Headers */, + 3430BB371C484A65001143B5 /* prng.h in Headers */, + 3430BB2D1C484A65001143B5 /* brg_endian.h in Headers */, 347E3A781C1DFFB500A11FD3 /* Zip.h in Headers */, 3430F6361C45C851007473A6 /* zip.h in Headers */, - 3430F6631C45C8AD007473A6 /* sha1.h in Headers */, - 3430F6551C45C8AD007473A6 /* aestab.h in Headers */, + 3430BB3B1C484A65001143B5 /* sha1.h in Headers */, + 3430BB2C1C484A65001143B5 /* aestab.h in Headers */, 3430F62D1C45C851007473A6 /* crypt.h in Headers */, - 3430F65B1C45C8AD007473A6 /* fileenc.h in Headers */, + 3430BB321C484A65001143B5 /* fileenc.h in Headers */, 3430F62E1C45C851007473A6 /* include.h in Headers */, 3430F6341C45C851007473A6 /* unzip.h in Headers */, - 3430F6531C45C8AD007473A6 /* aesopt.h in Headers */, - 3430F6591C45C8AD007473A6 /* entropy.h in Headers */, + 3430BB2A1C484A65001143B5 /* aesopt.h in Headers */, + 3430BB301C484A65001143B5 /* entropy.h in Headers */, 3430F6301C45C851007473A6 /* ioapi.h in Headers */, - 3430F64F1C45C8AD007473A6 /* aes.h in Headers */, - 3430F6501C45C8AD007473A6 /* aes_via_ace.h in Headers */, - 3430F6611C45C8AD007473A6 /* pwd2key.h in Headers */, + 3430BB261C484A65001143B5 /* aes.h in Headers */, + 3430BB271C484A65001143B5 /* aes_via_ace.h in Headers */, + 3430BB391C484A65001143B5 /* pwd2key.h in Headers */, 3430F6321C45C851007473A6 /* mztools.h in Headers */, - 3430F6571C45C8AD007473A6 /* brg_types.h in Headers */, + 3430BB2E1C484A65001143B5 /* brg_types.h in Headers */, 3430F6381C45C89A007473A6 /* Common.h in Headers */, ); runOnlyForDeploymentPostprocessing = 0; @@ -334,20 +337,21 @@ isa = PBXSourcesBuildPhase; buildActionMask = 2147483647; files = ( - 3430F6621C45C8AD007473A6 /* sha1.c in Sources */, - 3430F65E1C45C8AD007473A6 /* prng.c in Sources */, - 3430F65C1C45C8AD007473A6 /* hmac.c in Sources */, + 3430BB3A1C484A65001143B5 /* sha1.c in Sources */, + 3430BB361C484A65001143B5 /* prng.c in Sources */, + 3430BB291C484A65001143B5 /* aeskey.c in Sources */, + 3430BB2B1C484A65001143B5 /* aestab.c in Sources */, 3430F62F1C45C851007473A6 /* ioapi.c in Sources */, 3430F6331C45C851007473A6 /* unzip.c in Sources */, 347E3AD81C1E04C900A11FD3 /* Zip.swift in Sources */, - 3430F6581C45C8AD007473A6 /* entropy.c in Sources */, - 3430F6521C45C8AD007473A6 /* aeskey.c in Sources */, - 3430F6541C45C8AD007473A6 /* aestab.c in Sources */, + 3430BB331C484A65001143B5 /* hmac.c in Sources */, + 3430BB351C484A65001143B5 /* Makefile in Sources */, + 3430BB2F1C484A65001143B5 /* entropy.c in Sources */, 3430F6351C45C851007473A6 /* zip.c in Sources */, + 3430BB281C484A65001143B5 /* aescrypt.c in Sources */, + 3430BB311C484A65001143B5 /* fileenc.c in Sources */, + 3430BB381C484A65001143B5 /* pwd2key.c in Sources */, 3430F6311C45C851007473A6 /* mztools.c in Sources */, - 3430F6511C45C8AD007473A6 /* aescrypt.c in Sources */, - 3430F6601C45C8AD007473A6 /* pwd2key.c in Sources */, - 3430F65A1C45C8AD007473A6 /* fileenc.c in Sources */, ); runOnlyForDeploymentPostprocessing = 0; }; diff --git a/Zip/minizip/ChangeLog b/Zip/minizip/ChangeLog new file mode 100755 index 0000000000000000000000000000000000000000..7cdefb5ae05124a25e7b80a7f19c454e21b01d35 --- /dev/null +++ b/Zip/minizip/ChangeLog @@ -0,0 +1,140 @@ +More changes since 1.1 +- Added support for AE-2 zip files +- Fixed error in AE-1 not testing CRC32 + + Credits + + Steven Christy + +Changes since 1.1 +- Added PKZIP spanning support +- Added AES encryption support +- Added I/O buffering +- Clean up & changed unzLocateFile to support custom comparison function +- Clean up, removed zipRemoveExtraInfoBlock + + Credits + + Nathan Moinvaziri + +Change in 1.1 +- Added ZIP64 support for unzip ( by Even Rouault ) +- Added ZIP64 support for zip ( by Mathias Svensson ) +- Reverted some changed that Even Rouault did. +- Bunch of patches received from Gulles Vollant that he received for MiniZip from various users. +- Added unzip patch for BZIP Compression method (patch create by Daniel Borca) +- Added BZIP Compress method for zip +- Did some refactoring and code cleanup + + Credits + + Gilles Vollant - Original MiniZip author + Even Rouault - ZIP64 unzip Support + + 2007-2008 + - Addition of cpl_unzGetCurrentFileZStreamPos + - Decoration of symbol names unz* -> cpl_unz* + - Remove old C style function prototypes + - Add unzip support for ZIP64 + + Daniel Borca - BZip Compression method support in unzip + Mathias Svensson - ZIP64 zip support ( http://result42.com ) + + Oct-2009 unzip.c + - Removed cpl_* from symbol names + - Fixed problem if uncompressed size was > 4G and compressed size was <4G + should only read the compressed/uncompressed size from the Zip64 format if + the size from normal header was 0xffffffff + - Applied some bug fixes from patches received from Gilles Vollant + - Applied support to unzip files with compression method BZIP2 (bzip2 lib is required) + Patch created by Daniel Borca + + Oct-2009 zip.c + - Remove old C style function prototypes + - Added Zip64 Support when creating new file archives + - Did some code cleanup and refactoring to get better overview of some functions. + - Added zipRemoveExtraInfoBlock to strip extra field data from its ZIP64 data + It is used when recreting zip archive with RAW when deleting items from a zip. + ZIP64 data is automaticly added to items that needs it, and existing ZIP64 + data need to be removed. + - Added support for BZIP2 as compression mode (bzip2 lib is required) + + Oct-2009 ioapi.x + - Defined ZPOS64_T to fpos_t on windows and u_int64_t on linux. + - Change to fseeko64, ftello64 and fopen64 so large files would work on linux. + More if/def section may be needed to support other platforms + - Defined fxxxx64 calls to normal fopen/ftell/fseek so they would compile on windows. + (but you should use iowin32.c for windows instead) + +Change in 1.01e (12 feb 05) +- Fix in zipOpen2 for globalcomment (Rolf Kalbermatter) +- Fix possible memory leak in unzip.c (Zoran Stevanovic) + +Change in 1.01b (20 may 04) +- Integrate patch from Debian package (Mark Brown) +- Add tools mztools from Xavier Roche + +Change in 1.01 (8 may 04) +- Fix buffer overrun risk in unzip.c (Xavier Roche) +- Fix a minor buffer insecurity in minizip.c (Mike Whittaker) + +Change in 1.00: (10 sept 03) +- Rename to 1.00 +- Cosmetic code change + +Change in 0.22: (19 May 03) +- Crypting support (unless you define NOCRYPT) +- Append file in existing zipfile + +Change in 0.21: (10 Mar 03) +- Bug fixes + +Change in 0.17: (27 Jan 02) +- Bug fixes + +Change in 0.16: (19 Jan 02) +- Support of ioapi for virtualize zip file access + +Change in 0.15: (19 Mar 98) +- Fix memory leak in minizip.c + +Change in 0.14: (10 Mar 98) +- Fix bugs in minizip.c sample for zipping big file +- Fix problem in month in date handling +- Fix bug in unzlocal_GetCurrentFileInfoInternal in unzip.c for comment handling + +Change in 0.13: (6 Mar 98) +- Fix bugs in zip.c +- Add real minizip sample + +Change in 0.12: (4 Mar 98) +- Add zip.c and zip.h for creates .zip file +- Fix change_file_date in miniunz.c for Unix (Jean-loup Gailly) +- Fix miniunz.c for file without specific record for directory + +Change in 0.11: (3 Mar 98) +- Fix bug in unzGetCurrentFileInfo for get extra field and comment +- Enhance miniunz sample, remove the bad unztst.c sample + +Change in 0.10: (2 Mar 98) +- Fix bug in unzReadCurrentFile +- Rename unzip* to unz* function and structure +- Remove Windows-like hungary notation variable name +- Modify some structure in unzip.h +- Add somes comment in source +- Remove unzipGetcCurrentFile function +- Replace ZUNZEXPORT by ZEXPORT +- Add unzGetLocalExtrafield for get the local extrafield info +- Add a new sample, miniunz.c + +Change in 0.4: (25 Feb 98) +- Suppress the type unzipFileInZip. + Only on file in the zipfile can be open at the same time +- Fix somes typo in code +- Added tm_unz structure in unzip_file_info (date/time in readable format) + +Change unknown? +- Added unzGetOffset (RX '2004) +- Added unzGetFilePos & unzGoToFilePos (Ryan Haksi) +- Added unzGetCurrentFileZStreamPos64 (GDAL) + diff --git a/Zip/minizip/LICENSE b/Zip/minizip/LICENSE new file mode 100755 index 0000000000000000000000000000000000000000..086295a2b2af4260a4b8bd067c411b468b46268b --- /dev/null +++ b/Zip/minizip/LICENSE @@ -0,0 +1,17 @@ +Condition of use and distribution are the same as zlib: + +This software is provided 'as-is', without any express or implied +warranty. In no event will the authors be held liable for any damages +arising from the use of this software. + +Permission is granted to anyone to use this software for any purpose, +including commercial applications, and to alter it and redistribute it +freely, subject to the following restrictions: + +1. The origin of this software must not be misrepresented; you must not + claim that you wrote the original software. If you use this software + in a product, an acknowledgement in the product documentation would be + appreciated but is not required. +2. Altered source versions must be plainly marked as such, and must not be + misrepresented as being the original software. +3. This notice may not be removed or altered from any source distribution. \ No newline at end of file diff --git a/Zip/minizip/Makefile b/Zip/minizip/Makefile new file mode 100755 index 0000000000000000000000000000000000000000..876686135c64c3af7348817b2c96d3902c632d76 --- /dev/null +++ b/Zip/minizip/Makefile @@ -0,0 +1,37 @@ +CC=cc +CFLAGS=-O -I../.. -DHAVE_AES + +UNZ_OBJS = miniunz.o unzip.o ioapi.o ../../libz.a libaes.a +ZIP_OBJS = minizip.o zip.o ioapi.o ../../libz.a libaes.a +TEST_FILES = test.zip readme.old readme.txt + +.c.o: + $(CC) -c $(CFLAGS) $*.c + +all: miniunz minizip + +libaes.a: + cd aes; $(MAKE) $(MFLAGS) + +miniunz: $(UNZ_OBJS) libaes.a + $(CC) $(CFLAGS) -o $@ $(UNZ_OBJS) + +minizip: $(ZIP_OBJS) libaes.a + $(CC) $(CFLAGS) -o $@ $(ZIP_OBJS) + +.PHONY: test clean + +test: miniunz minizip + @rm -f $(TEST_FILES) + @cp README.md readme.txt + @touch -t 200712301223.44 readme.txt + ./minizip test.zip readme.txt + ./miniunz -l test.zip + mv readme.txt readme.old + ./miniunz test.zip + @diff -q README.md readme.txt || echo "Test failed: files differ" + @[[ "$$(stat -c %Y readme.txt)" = "$$(stat -c %Y readme.old)" ]] || echo "Test failed: timestamp not preserved" + @rm -f $(TEST_FILES) + +clean: + /bin/rm -f *.o *~ minizip miniunz $(TEST_FILES) diff --git a/Zip/minizip/Makefile.am b/Zip/minizip/Makefile.am new file mode 100755 index 0000000000000000000000000000000000000000..d83fee7d5b5e3b79542be2eb62ef2c37bcc2e95a --- /dev/null +++ b/Zip/minizip/Makefile.am @@ -0,0 +1,43 @@ +lib_LTLIBRARIES = libminizip.la + +if COND_DEMOS +bin_PROGRAMS = miniunzip minizip +endif + +zlib_top_srcdir = $(top_srcdir)/../.. +zlib_top_builddir = $(top_builddir)/../.. + +AM_CPPFLAGS = -I$(zlib_top_srcdir) +AM_LDFLAGS = -L$(zlib_top_builddir) + +if WIN32 +iowin32_src = iowin32.c +iowin32_h = iowin32.h +endif + +libminizip_la_SOURCES = \ + ioapi.c \ + unzip.c \ + zip.c \ + ${iowin32_src} + +libminizip_la_LDFLAGS = $(AM_LDFLAGS) -version-info 1:0:0 -lz + +minizip_includedir = $(includedir)/minizip +minizip_include_HEADERS = \ + crypt.h \ + ioapi.h \ + unzip.h \ + zip.h \ + ${iowin32_h} + +pkgconfigdir = $(libdir)/pkgconfig +pkgconfig_DATA = minizip.pc + +EXTRA_PROGRAMS = miniunzip minizip + +miniunzip_SOURCES = miniunz.c +miniunzip_LDADD = libminizip.la + +minizip_SOURCES = minizip.c +minizip_LDADD = libminizip.la -lz diff --git a/Zip/minizip/README.md b/Zip/minizip/README.md new file mode 100755 index 0000000000000000000000000000000000000000..b44c1779e8e65eea3899066df86189b7bcbc38bd --- /dev/null +++ b/Zip/minizip/README.md @@ -0,0 +1,81 @@ +Minizip zlib contribution that includes: + +- AES encryption +- I/O buffering +- PKWARE disk spanning +- Visual Studio 2008 project files + +It also has the latest bug fixes that having been found all over the internet including the minizip forum and zlib developer's mailing list. + +*AES Encryption* + ++ Requires #define HAVE_AES ++ Requires AES library files + +When zipping with a password it will always use AES 256-bit encryption. +When unzipping it will use AES decryption only if necessary. + +*I/O Buffering* + +Improves I/O performance by buffering read and write operations. +``` +zlib_filefunc64_def filefunc64 = {0}; +ourbuffer_t buffered = {0}; + +fill_win32_filefunc64(&buffered->filefunc64); +fill_buffer_filefunc64(&filefunc64, buffered); + +unzOpen2_64(filename, &filefunc64) +``` + +*PKWARE disk spanning* + +To create an archive with multiple disks use zipOpen3_64 supplying a disk_size value in bytes. + +``` +extern zipFile ZEXPORT zipOpen3_64 OF((const void *pathname, int append, + ZPOS64_T disk_size, zipcharpc* globalcomment, zlib_filefunc64_def* pzlib_filefunc_def)); +``` +The central directory is the only data stored in the .zip and doesn't follow disk_size restrictions. + +When unzipping it will automatically determine when in needs to span disks. + +*I/O Memory* + +To unzip from a zip file in memory use fill_memory_filefunc and supply a proper ourmemory_t structure. +``` +zlib_filefunc_def filefunc32 = {0}; +ourmemory_t unzmem = {0}; + +unzmem.size = bufsize; +unzmem.base = (char *)malloc(unzmem.size); +memcpy(unzmem.base, buffer, unzmem.size); + +fill_memory_filefunc(&filefunc32, &unzmem); + +unzOpen2("__notused__", &filefunc32); +``` + +To create a zip file in memory use fill_memory_filefunc and supply a proper ourmemory_t structure. It is important +not to forget to free zipmem->base when finished. If grow is set, zipmem->base will expand to fit the size of the zip. +If grow is not set be sure to fill out zipmem.base and zipmem.size. + +``` +zlib_filefunc_def filefunc32 = {0}; +ourmemory_t zipmem = {0}; + +zipmem.grow = 1; + +fill_memory_filefunc(&filefunc32, &zipmem); + +zipOpen3("__notused__", APPEND_STATUS_CREATE, 0, 0, &filefunc32); +``` + +*BZIP2* + ++ Requires #define HAVE_BZIP2 ++ Requires BZIP2 library + +*Windows RT* + ++ Requires #define IOWIN32_USING_WINRT_API diff --git a/Zip/minizip/aes/Makefile b/Zip/minizip/aes/Makefile new file mode 100755 index 0000000000000000000000000000000000000000..d7e3234bbf505de80d7fa9e24f9949e16658a03f --- /dev/null +++ b/Zip/minizip/aes/Makefile @@ -0,0 +1,19 @@ +CC=cc +CFLAGS=-O -DHAVE_AES +OBJS=aescrypt.o aeskey.o aestab.o entropy.o fileenc.o hmac.o prng.o pwd2key.o sha1.o +ARFLAGS=rv + +.c.o: + $(CC) -c $(CFLAGS) $*.c + +libaes.a: $(OBJS) + $(ECHO) $(AR) $(ARFLAGS) ../libaes.a $? + $(AR) $(ARFLAGS) ../libaes.a $? + $(RANLIB) ../libaes.a + +all: libaes.a + +.PHONY: clean + +clean: + rm *.o *.a \ No newline at end of file diff --git a/Zip/aes/aes.h b/Zip/minizip/aes/aes.h similarity index 97% rename from Zip/aes/aes.h rename to Zip/minizip/aes/aes.h index 44682c39116de9d9b404076ddc82e0816bd4891a..40927fb7eb3799a01304de0e4296255b49076c73 100755 --- a/Zip/aes/aes.h +++ b/Zip/minizip/aes/aes.h @@ -1,198 +1,198 @@ -/* ---------------------------------------------------------------------------- -Copyright (c) 1998-2010, Brian Gladman, Worcester, UK. All rights reserved. - -The redistribution and use of this software (with or without changes) -is allowed without the payment of fees or royalties provided that: - - source code distributions include the above copyright notice, this - list of conditions and the following disclaimer; - - binary distributions include the above copyright notice, this list - of conditions and the following disclaimer in their documentation. - -This software is provided 'as is' with no explicit or implied warranties -in respect of its operation, including, but not limited to, correctness -and fitness for purpose. ---------------------------------------------------------------------------- -Issue Date: 20/12/2007 - - This file contains the definitions required to use AES in C. See aesopt.h - for optimisation details. -*/ - -#ifndef _AES_H -#define _AES_H - -#include - -/* This include is used to find 8 & 32 bit unsigned integer types */ -#include "brg_types.h" - -#if defined(__cplusplus) -extern "C" -{ -#endif - -#define AES_128 /* if a fast 128 bit key scheduler is needed */ -#define AES_192 /* if a fast 192 bit key scheduler is needed */ -#define AES_256 /* if a fast 256 bit key scheduler is needed */ -#define AES_VAR /* if variable key size scheduler is needed */ -#define AES_MODES /* if support is needed for modes */ - -/* The following must also be set in assembler files if being used */ - -#define AES_ENCRYPT /* if support for encryption is needed */ -#define AES_DECRYPT /* if support for decryption is needed */ -#define AES_REV_DKS /* define to reverse decryption key schedule */ - -#define AES_BLOCK_SIZE 16 /* the AES block size in bytes */ -#define N_COLS 4 /* the number of columns in the state */ - -/* The key schedule length is 11, 13 or 15 16-byte blocks for 128, */ -/* 192 or 256-bit keys respectively. That is 176, 208 or 240 bytes */ -/* or 44, 52 or 60 32-bit words. */ - -#if defined( AES_VAR ) || defined( AES_256 ) -#define KS_LENGTH 60 -#elif defined( AES_192 ) -#define KS_LENGTH 52 -#else -#define KS_LENGTH 44 -#endif - -#define AES_RETURN INT_RETURN - -/* the character array 'inf' in the following structures is used */ -/* to hold AES context information. This AES code uses cx->inf.b[0] */ -/* to hold the number of rounds multiplied by 16. The other three */ -/* elements can be used by code that implements additional modes */ - -typedef union -{ uint_32t l; - uint_8t b[4]; -} aes_inf; - -typedef struct -{ uint_32t ks[KS_LENGTH]; - aes_inf inf; -} aes_encrypt_ctx; - -typedef struct -{ uint_32t ks[KS_LENGTH]; - aes_inf inf; -} aes_decrypt_ctx; - -/* This routine must be called before first use if non-static */ -/* tables are being used */ - -AES_RETURN aes_init(void); - -/* Key lengths in the range 16 <= key_len <= 32 are given in bytes, */ -/* those in the range 128 <= key_len <= 256 are given in bits */ - -#if defined( AES_ENCRYPT ) - -#if defined( AES_128 ) || defined( AES_VAR) -AES_RETURN aes_encrypt_key128(const unsigned char *key, aes_encrypt_ctx cx[1]); -#endif - -#if defined( AES_192 ) || defined( AES_VAR) -AES_RETURN aes_encrypt_key192(const unsigned char *key, aes_encrypt_ctx cx[1]); -#endif - -#if defined( AES_256 ) || defined( AES_VAR) -AES_RETURN aes_encrypt_key256(const unsigned char *key, aes_encrypt_ctx cx[1]); -#endif - -#if defined( AES_VAR ) -AES_RETURN aes_encrypt_key(const unsigned char *key, int key_len, aes_encrypt_ctx cx[1]); -#endif - -AES_RETURN aes_encrypt(const unsigned char *in, unsigned char *out, const aes_encrypt_ctx cx[1]); - -#endif - -#if defined( AES_DECRYPT ) - -#if defined( AES_128 ) || defined( AES_VAR) -AES_RETURN aes_decrypt_key128(const unsigned char *key, aes_decrypt_ctx cx[1]); -#endif - -#if defined( AES_192 ) || defined( AES_VAR) -AES_RETURN aes_decrypt_key192(const unsigned char *key, aes_decrypt_ctx cx[1]); -#endif - -#if defined( AES_256 ) || defined( AES_VAR) -AES_RETURN aes_decrypt_key256(const unsigned char *key, aes_decrypt_ctx cx[1]); -#endif - -#if defined( AES_VAR ) -AES_RETURN aes_decrypt_key(const unsigned char *key, int key_len, aes_decrypt_ctx cx[1]); -#endif - -AES_RETURN aes_decrypt(const unsigned char *in, unsigned char *out, const aes_decrypt_ctx cx[1]); - -#endif - -#if defined( AES_MODES ) - -/* Multiple calls to the following subroutines for multiple block */ -/* ECB, CBC, CFB, OFB and CTR mode encryption can be used to handle */ -/* long messages incremantally provided that the context AND the iv */ -/* are preserved between all such calls. For the ECB and CBC modes */ -/* each individual call within a series of incremental calls must */ -/* process only full blocks (i.e. len must be a multiple of 16) but */ -/* the CFB, OFB and CTR mode calls can handle multiple incremental */ -/* calls of any length. Each mode is reset when a new AES key is */ -/* set but ECB and CBC operations can be reset without setting a */ -/* new key by setting a new IV value. To reset CFB, OFB and CTR */ -/* without setting the key, aes_mode_reset() must be called and the */ -/* IV must be set. NOTE: All these calls update the IV on exit so */ -/* this has to be reset if a new operation with the same IV as the */ -/* previous one is required (or decryption follows encryption with */ -/* the same IV array). */ - -AES_RETURN aes_test_alignment_detection(unsigned int n); - -AES_RETURN aes_ecb_encrypt(const unsigned char *ibuf, unsigned char *obuf, - int len, const aes_encrypt_ctx cx[1]); - -AES_RETURN aes_ecb_decrypt(const unsigned char *ibuf, unsigned char *obuf, - int len, const aes_decrypt_ctx cx[1]); - -AES_RETURN aes_cbc_encrypt(const unsigned char *ibuf, unsigned char *obuf, - int len, unsigned char *iv, const aes_encrypt_ctx cx[1]); - -AES_RETURN aes_cbc_decrypt(const unsigned char *ibuf, unsigned char *obuf, - int len, unsigned char *iv, const aes_decrypt_ctx cx[1]); - -AES_RETURN aes_mode_reset(aes_encrypt_ctx cx[1]); - -AES_RETURN aes_cfb_encrypt(const unsigned char *ibuf, unsigned char *obuf, - int len, unsigned char *iv, aes_encrypt_ctx cx[1]); - -AES_RETURN aes_cfb_decrypt(const unsigned char *ibuf, unsigned char *obuf, - int len, unsigned char *iv, aes_encrypt_ctx cx[1]); - -#define aes_ofb_encrypt aes_ofb_crypt -#define aes_ofb_decrypt aes_ofb_crypt - -AES_RETURN aes_ofb_crypt(const unsigned char *ibuf, unsigned char *obuf, - int len, unsigned char *iv, aes_encrypt_ctx cx[1]); - -typedef void cbuf_inc(unsigned char *cbuf); - -#define aes_ctr_encrypt aes_ctr_crypt -#define aes_ctr_decrypt aes_ctr_crypt - -AES_RETURN aes_ctr_crypt(const unsigned char *ibuf, unsigned char *obuf, - int len, unsigned char *cbuf, cbuf_inc ctr_inc, aes_encrypt_ctx cx[1]); - -#endif - -#if defined(__cplusplus) -} -#endif - -#endif +/* +--------------------------------------------------------------------------- +Copyright (c) 1998-2010, Brian Gladman, Worcester, UK. All rights reserved. + +The redistribution and use of this software (with or without changes) +is allowed without the payment of fees or royalties provided that: + + source code distributions include the above copyright notice, this + list of conditions and the following disclaimer; + + binary distributions include the above copyright notice, this list + of conditions and the following disclaimer in their documentation. + +This software is provided 'as is' with no explicit or implied warranties +in respect of its operation, including, but not limited to, correctness +and fitness for purpose. +--------------------------------------------------------------------------- +Issue Date: 20/12/2007 + + This file contains the definitions required to use AES in C. See aesopt.h + for optimisation details. +*/ + +#ifndef _AES_H +#define _AES_H + +#include + +/* This include is used to find 8 & 32 bit unsigned integer types */ +#include "brg_types.h" + +#if defined(__cplusplus) +extern "C" +{ +#endif + +#define AES_128 /* if a fast 128 bit key scheduler is needed */ +#define AES_192 /* if a fast 192 bit key scheduler is needed */ +#define AES_256 /* if a fast 256 bit key scheduler is needed */ +#define AES_VAR /* if variable key size scheduler is needed */ +#define AES_MODES /* if support is needed for modes */ + +/* The following must also be set in assembler files if being used */ + +#define AES_ENCRYPT /* if support for encryption is needed */ +#define AES_DECRYPT /* if support for decryption is needed */ +#define AES_REV_DKS /* define to reverse decryption key schedule */ + +#define AES_BLOCK_SIZE 16 /* the AES block size in bytes */ +#define N_COLS 4 /* the number of columns in the state */ + +/* The key schedule length is 11, 13 or 15 16-byte blocks for 128, */ +/* 192 or 256-bit keys respectively. That is 176, 208 or 240 bytes */ +/* or 44, 52 or 60 32-bit words. */ + +#if defined( AES_VAR ) || defined( AES_256 ) +#define KS_LENGTH 60 +#elif defined( AES_192 ) +#define KS_LENGTH 52 +#else +#define KS_LENGTH 44 +#endif + +#define AES_RETURN INT_RETURN + +/* the character array 'inf' in the following structures is used */ +/* to hold AES context information. This AES code uses cx->inf.b[0] */ +/* to hold the number of rounds multiplied by 16. The other three */ +/* elements can be used by code that implements additional modes */ + +typedef union +{ uint_32t l; + uint_8t b[4]; +} aes_inf; + +typedef struct +{ uint_32t ks[KS_LENGTH]; + aes_inf inf; +} aes_encrypt_ctx; + +typedef struct +{ uint_32t ks[KS_LENGTH]; + aes_inf inf; +} aes_decrypt_ctx; + +/* This routine must be called before first use if non-static */ +/* tables are being used */ + +AES_RETURN aes_init(void); + +/* Key lengths in the range 16 <= key_len <= 32 are given in bytes, */ +/* those in the range 128 <= key_len <= 256 are given in bits */ + +#if defined( AES_ENCRYPT ) + +#if defined( AES_128 ) || defined( AES_VAR) +AES_RETURN aes_encrypt_key128(const unsigned char *key, aes_encrypt_ctx cx[1]); +#endif + +#if defined( AES_192 ) || defined( AES_VAR) +AES_RETURN aes_encrypt_key192(const unsigned char *key, aes_encrypt_ctx cx[1]); +#endif + +#if defined( AES_256 ) || defined( AES_VAR) +AES_RETURN aes_encrypt_key256(const unsigned char *key, aes_encrypt_ctx cx[1]); +#endif + +#if defined( AES_VAR ) +AES_RETURN aes_encrypt_key(const unsigned char *key, int key_len, aes_encrypt_ctx cx[1]); +#endif + +AES_RETURN aes_encrypt(const unsigned char *in, unsigned char *out, const aes_encrypt_ctx cx[1]); + +#endif + +#if defined( AES_DECRYPT ) + +#if defined( AES_128 ) || defined( AES_VAR) +AES_RETURN aes_decrypt_key128(const unsigned char *key, aes_decrypt_ctx cx[1]); +#endif + +#if defined( AES_192 ) || defined( AES_VAR) +AES_RETURN aes_decrypt_key192(const unsigned char *key, aes_decrypt_ctx cx[1]); +#endif + +#if defined( AES_256 ) || defined( AES_VAR) +AES_RETURN aes_decrypt_key256(const unsigned char *key, aes_decrypt_ctx cx[1]); +#endif + +#if defined( AES_VAR ) +AES_RETURN aes_decrypt_key(const unsigned char *key, int key_len, aes_decrypt_ctx cx[1]); +#endif + +AES_RETURN aes_decrypt(const unsigned char *in, unsigned char *out, const aes_decrypt_ctx cx[1]); + +#endif + +#if defined( AES_MODES ) + +/* Multiple calls to the following subroutines for multiple block */ +/* ECB, CBC, CFB, OFB and CTR mode encryption can be used to handle */ +/* long messages incremantally provided that the context AND the iv */ +/* are preserved between all such calls. For the ECB and CBC modes */ +/* each individual call within a series of incremental calls must */ +/* process only full blocks (i.e. len must be a multiple of 16) but */ +/* the CFB, OFB and CTR mode calls can handle multiple incremental */ +/* calls of any length. Each mode is reset when a new AES key is */ +/* set but ECB and CBC operations can be reset without setting a */ +/* new key by setting a new IV value. To reset CFB, OFB and CTR */ +/* without setting the key, aes_mode_reset() must be called and the */ +/* IV must be set. NOTE: All these calls update the IV on exit so */ +/* this has to be reset if a new operation with the same IV as the */ +/* previous one is required (or decryption follows encryption with */ +/* the same IV array). */ + +AES_RETURN aes_test_alignment_detection(unsigned int n); + +AES_RETURN aes_ecb_encrypt(const unsigned char *ibuf, unsigned char *obuf, + int len, const aes_encrypt_ctx cx[1]); + +AES_RETURN aes_ecb_decrypt(const unsigned char *ibuf, unsigned char *obuf, + int len, const aes_decrypt_ctx cx[1]); + +AES_RETURN aes_cbc_encrypt(const unsigned char *ibuf, unsigned char *obuf, + int len, unsigned char *iv, const aes_encrypt_ctx cx[1]); + +AES_RETURN aes_cbc_decrypt(const unsigned char *ibuf, unsigned char *obuf, + int len, unsigned char *iv, const aes_decrypt_ctx cx[1]); + +AES_RETURN aes_mode_reset(aes_encrypt_ctx cx[1]); + +AES_RETURN aes_cfb_encrypt(const unsigned char *ibuf, unsigned char *obuf, + int len, unsigned char *iv, aes_encrypt_ctx cx[1]); + +AES_RETURN aes_cfb_decrypt(const unsigned char *ibuf, unsigned char *obuf, + int len, unsigned char *iv, aes_encrypt_ctx cx[1]); + +#define aes_ofb_encrypt aes_ofb_crypt +#define aes_ofb_decrypt aes_ofb_crypt + +AES_RETURN aes_ofb_crypt(const unsigned char *ibuf, unsigned char *obuf, + int len, unsigned char *iv, aes_encrypt_ctx cx[1]); + +typedef void cbuf_inc(unsigned char *cbuf); + +#define aes_ctr_encrypt aes_ctr_crypt +#define aes_ctr_decrypt aes_ctr_crypt + +AES_RETURN aes_ctr_crypt(const unsigned char *ibuf, unsigned char *obuf, + int len, unsigned char *cbuf, cbuf_inc ctr_inc, aes_encrypt_ctx cx[1]); + +#endif + +#if defined(__cplusplus) +} +#endif + +#endif diff --git a/Zip/aes/aes_via_ace.h b/Zip/minizip/aes/aes_via_ace.h similarity index 96% rename from Zip/aes/aes_via_ace.h rename to Zip/minizip/aes/aes_via_ace.h index cb2aa1babf469a56fef208b63e5749021ddcac5a..bb7a9cf4dd19c67a5b8473b3afffe053285d5528 100755 --- a/Zip/aes/aes_via_ace.h +++ b/Zip/minizip/aes/aes_via_ace.h @@ -1,541 +1,541 @@ -/* -Copyright (c) 1998-2010, Brian Gladman, Worcester, UK. All rights reserved. - -The redistribution and use of this software (with or without changes) -is allowed without the payment of fees or royalties provided that: - - source code distributions include the above copyright notice, this - list of conditions and the following disclaimer; - - binary distributions include the above copyright notice, this list - of conditions and the following disclaimer in their documentation. - -This software is provided 'as is' with no explicit or implied warranties -in respect of its operation, including, but not limited to, correctness -and fitness for purpose. ---------------------------------------------------------------------------- -Issue Date: 20/12/2007 -*/ - -#ifndef AES_VIA_ACE_H -#define AES_VIA_ACE_H - -#if defined( _MSC_VER ) -# define INLINE __inline -#elif defined( __GNUC__ ) -# define INLINE static inline -#else -# error VIA ACE requires Microsoft or GNU C -#endif - -#define NEH_GENERATE 1 -#define NEH_LOAD 2 -#define NEH_HYBRID 3 - -#define MAX_READ_ATTEMPTS 1000 - -/* VIA Nehemiah RNG and ACE Feature Mask Values */ - -#define NEH_CPU_IS_VIA 0x00000001 -#define NEH_CPU_READ 0x00000010 -#define NEH_CPU_MASK 0x00000011 - -#define NEH_RNG_PRESENT 0x00000004 -#define NEH_RNG_ENABLED 0x00000008 -#define NEH_ACE_PRESENT 0x00000040 -#define NEH_ACE_ENABLED 0x00000080 -#define NEH_RNG_FLAGS (NEH_RNG_PRESENT | NEH_RNG_ENABLED) -#define NEH_ACE_FLAGS (NEH_ACE_PRESENT | NEH_ACE_ENABLED) -#define NEH_FLAGS_MASK (NEH_RNG_FLAGS | NEH_ACE_FLAGS) - -/* VIA Nehemiah Advanced Cryptography Engine (ACE) Control Word Values */ - -#define NEH_GEN_KEY 0x00000000 /* generate key schedule */ -#define NEH_LOAD_KEY 0x00000080 /* load schedule from memory */ -#define NEH_ENCRYPT 0x00000000 /* encryption */ -#define NEH_DECRYPT 0x00000200 /* decryption */ -#define NEH_KEY128 0x00000000+0x0a /* 128 bit key */ -#define NEH_KEY192 0x00000400+0x0c /* 192 bit key */ -#define NEH_KEY256 0x00000800+0x0e /* 256 bit key */ - -#define NEH_ENC_GEN (NEH_ENCRYPT | NEH_GEN_KEY) -#define NEH_DEC_GEN (NEH_DECRYPT | NEH_GEN_KEY) -#define NEH_ENC_LOAD (NEH_ENCRYPT | NEH_LOAD_KEY) -#define NEH_DEC_LOAD (NEH_DECRYPT | NEH_LOAD_KEY) - -#define NEH_ENC_GEN_DATA {\ - NEH_ENC_GEN | NEH_KEY128, 0, 0, 0,\ - NEH_ENC_GEN | NEH_KEY192, 0, 0, 0,\ - NEH_ENC_GEN | NEH_KEY256, 0, 0, 0 } - -#define NEH_ENC_LOAD_DATA {\ - NEH_ENC_LOAD | NEH_KEY128, 0, 0, 0,\ - NEH_ENC_LOAD | NEH_KEY192, 0, 0, 0,\ - NEH_ENC_LOAD | NEH_KEY256, 0, 0, 0 } - -#define NEH_ENC_HYBRID_DATA {\ - NEH_ENC_GEN | NEH_KEY128, 0, 0, 0,\ - NEH_ENC_LOAD | NEH_KEY192, 0, 0, 0,\ - NEH_ENC_LOAD | NEH_KEY256, 0, 0, 0 } - -#define NEH_DEC_GEN_DATA {\ - NEH_DEC_GEN | NEH_KEY128, 0, 0, 0,\ - NEH_DEC_GEN | NEH_KEY192, 0, 0, 0,\ - NEH_DEC_GEN | NEH_KEY256, 0, 0, 0 } - -#define NEH_DEC_LOAD_DATA {\ - NEH_DEC_LOAD | NEH_KEY128, 0, 0, 0,\ - NEH_DEC_LOAD | NEH_KEY192, 0, 0, 0,\ - NEH_DEC_LOAD | NEH_KEY256, 0, 0, 0 } - -#define NEH_DEC_HYBRID_DATA {\ - NEH_DEC_GEN | NEH_KEY128, 0, 0, 0,\ - NEH_DEC_LOAD | NEH_KEY192, 0, 0, 0,\ - NEH_DEC_LOAD | NEH_KEY256, 0, 0, 0 } - -#define neh_enc_gen_key(x) ((x) == 128 ? (NEH_ENC_GEN | NEH_KEY128) : \ - (x) == 192 ? (NEH_ENC_GEN | NEH_KEY192) : (NEH_ENC_GEN | NEH_KEY256)) - -#define neh_enc_load_key(x) ((x) == 128 ? (NEH_ENC_LOAD | NEH_KEY128) : \ - (x) == 192 ? (NEH_ENC_LOAD | NEH_KEY192) : (NEH_ENC_LOAD | NEH_KEY256)) - -#define neh_enc_hybrid_key(x) ((x) == 128 ? (NEH_ENC_GEN | NEH_KEY128) : \ - (x) == 192 ? (NEH_ENC_LOAD | NEH_KEY192) : (NEH_ENC_LOAD | NEH_KEY256)) - -#define neh_dec_gen_key(x) ((x) == 128 ? (NEH_DEC_GEN | NEH_KEY128) : \ - (x) == 192 ? (NEH_DEC_GEN | NEH_KEY192) : (NEH_DEC_GEN | NEH_KEY256)) - -#define neh_dec_load_key(x) ((x) == 128 ? (NEH_DEC_LOAD | NEH_KEY128) : \ - (x) == 192 ? (NEH_DEC_LOAD | NEH_KEY192) : (NEH_DEC_LOAD | NEH_KEY256)) - -#define neh_dec_hybrid_key(x) ((x) == 128 ? (NEH_DEC_GEN | NEH_KEY128) : \ - (x) == 192 ? (NEH_DEC_LOAD | NEH_KEY192) : (NEH_DEC_LOAD | NEH_KEY256)) - -#if defined( _MSC_VER ) && ( _MSC_VER > 1200 ) -#define aligned_auto(type, name, no, stride) __declspec(align(stride)) type name[no] -#else -#define aligned_auto(type, name, no, stride) \ - unsigned char _##name[no * sizeof(type) + stride]; \ - type *name = (type*)(16 * ((((unsigned long)(_##name)) + stride - 1) / stride)) -#endif - -#if defined( _MSC_VER ) && ( _MSC_VER > 1200 ) -#define aligned_array(type, name, no, stride) __declspec(align(stride)) type name[no] -#elif defined( __GNUC__ ) -#define aligned_array(type, name, no, stride) type name[no] __attribute__ ((aligned(stride))) -#else -#define aligned_array(type, name, no, stride) type name[no] -#endif - -/* VIA ACE codeword */ - -static unsigned char via_flags = 0; - -#if defined ( _MSC_VER ) && ( _MSC_VER > 800 ) - -#define NEH_REKEY __asm pushfd __asm popfd -#define NEH_AES __asm _emit 0xf3 __asm _emit 0x0f __asm _emit 0xa7 -#define NEH_ECB NEH_AES __asm _emit 0xc8 -#define NEH_CBC NEH_AES __asm _emit 0xd0 -#define NEH_CFB NEH_AES __asm _emit 0xe0 -#define NEH_OFB NEH_AES __asm _emit 0xe8 -#define NEH_RNG __asm _emit 0x0f __asm _emit 0xa7 __asm _emit 0xc0 - -INLINE int has_cpuid(void) -{ char ret_value; - __asm - { pushfd /* save EFLAGS register */ - mov eax,[esp] /* copy it to eax */ - mov edx,0x00200000 /* CPUID bit position */ - xor eax,edx /* toggle the CPUID bit */ - push eax /* attempt to set EFLAGS to */ - popfd /* the new value */ - pushfd /* get the new EFLAGS value */ - pop eax /* into eax */ - xor eax,[esp] /* xor with original value */ - and eax,edx /* has CPUID bit changed? */ - setne al /* set to 1 if we have been */ - mov ret_value,al /* able to change it */ - popfd /* restore original EFLAGS */ - } - return (int)ret_value; -} - -INLINE int is_via_cpu(void) -{ char ret_value; - __asm - { push ebx - xor eax,eax /* use CPUID to get vendor */ - cpuid /* identity string */ - xor eax,eax /* is it "CentaurHauls" ? */ - sub ebx,0x746e6543 /* 'Cent' */ - or eax,ebx - sub edx,0x48727561 /* 'aurH' */ - or eax,edx - sub ecx,0x736c7561 /* 'auls' */ - or eax,ecx - sete al /* set to 1 if it is VIA ID */ - mov dl,NEH_CPU_READ /* mark CPU type as read */ - or dl,al /* & store result in flags */ - mov [via_flags],dl /* set VIA detected flag */ - mov ret_value,al /* able to change it */ - pop ebx - } - return (int)ret_value; -} - -INLINE int read_via_flags(void) -{ char ret_value = 0; - __asm - { mov eax,0xC0000000 /* Centaur extended CPUID */ - cpuid - mov edx,0xc0000001 /* >= 0xc0000001 if support */ - cmp eax,edx /* for VIA extended feature */ - jnae no_rng /* flags is available */ - mov eax,edx /* read Centaur extended */ - cpuid /* feature flags */ - mov eax,NEH_FLAGS_MASK /* mask out and save */ - and eax,edx /* the RNG and ACE flags */ - or [via_flags],al /* present & enabled flags */ - mov ret_value,al /* able to change it */ -no_rng: - } - return (int)ret_value; -} - -INLINE unsigned int via_rng_in(void *buf) -{ char ret_value = 0x1f; - __asm - { push edi - mov edi,buf /* input buffer address */ - xor edx,edx /* try to fetch 8 bytes */ - NEH_RNG /* do RNG read operation */ - and ret_value,al /* count of bytes returned */ - pop edi - } - return (int)ret_value; -} - -INLINE void via_ecb_op5( - const void *k, const void *c, const void *s, void *d, int l) -{ __asm - { push ebx - NEH_REKEY - mov ebx, (k) - mov edx, (c) - mov esi, (s) - mov edi, (d) - mov ecx, (l) - NEH_ECB - pop ebx - } -} - -INLINE void via_cbc_op6( - const void *k, const void *c, const void *s, void *d, int l, void *v) -{ __asm - { push ebx - NEH_REKEY - mov ebx, (k) - mov edx, (c) - mov esi, (s) - mov edi, (d) - mov ecx, (l) - mov eax, (v) - NEH_CBC - pop ebx - } -} - -INLINE void via_cbc_op7( - const void *k, const void *c, const void *s, void *d, int l, void *v, void *w) -{ __asm - { push ebx - NEH_REKEY - mov ebx, (k) - mov edx, (c) - mov esi, (s) - mov edi, (d) - mov ecx, (l) - mov eax, (v) - NEH_CBC - mov esi, eax - mov edi, (w) - movsd - movsd - movsd - movsd - pop ebx - } -} - -INLINE void via_cfb_op6( - const void *k, const void *c, const void *s, void *d, int l, void *v) -{ __asm - { push ebx - NEH_REKEY - mov ebx, (k) - mov edx, (c) - mov esi, (s) - mov edi, (d) - mov ecx, (l) - mov eax, (v) - NEH_CFB - pop ebx - } -} - -INLINE void via_cfb_op7( - const void *k, const void *c, const void *s, void *d, int l, void *v, void *w) -{ __asm - { push ebx - NEH_REKEY - mov ebx, (k) - mov edx, (c) - mov esi, (s) - mov edi, (d) - mov ecx, (l) - mov eax, (v) - NEH_CFB - mov esi, eax - mov edi, (w) - movsd - movsd - movsd - movsd - pop ebx - } -} - -INLINE void via_ofb_op6( - const void *k, const void *c, const void *s, void *d, int l, void *v) -{ __asm - { push ebx - NEH_REKEY - mov ebx, (k) - mov edx, (c) - mov esi, (s) - mov edi, (d) - mov ecx, (l) - mov eax, (v) - NEH_OFB - pop ebx - } -} - -#elif defined( __GNUC__ ) - -#define NEH_REKEY asm("pushfl\n popfl\n\t") -#define NEH_ECB asm(".byte 0xf3, 0x0f, 0xa7, 0xc8\n\t") -#define NEH_CBC asm(".byte 0xf3, 0x0f, 0xa7, 0xd0\n\t") -#define NEH_CFB asm(".byte 0xf3, 0x0f, 0xa7, 0xe0\n\t") -#define NEH_OFB asm(".byte 0xf3, 0x0f, 0xa7, 0xe8\n\t") -#define NEH_RNG asm(".byte 0x0f, 0xa7, 0xc0\n\t"); - -INLINE int has_cpuid(void) -{ int val; - asm("pushfl\n\t"); - asm("movl 0(%esp),%eax\n\t"); - asm("xor $0x00200000,%eax\n\t"); - asm("pushl %eax\n\t"); - asm("popfl\n\t"); - asm("pushfl\n\t"); - asm("popl %eax\n\t"); - asm("xorl 0(%esp),%edx\n\t"); - asm("andl $0x00200000,%eax\n\t"); - asm("movl %%eax,%0\n\t" : "=m" (val)); - asm("popfl\n\t"); - return val ? 1 : 0; -} - -INLINE int is_via_cpu(void) -{ int val; - asm("pushl %ebx\n\t"); - asm("xorl %eax,%eax\n\t"); - asm("cpuid\n\t"); - asm("xorl %eax,%eax\n\t"); - asm("subl $0x746e6543,%ebx\n\t"); - asm("orl %ebx,%eax\n\t"); - asm("subl $0x48727561,%edx\n\t"); - asm("orl %edx,%eax\n\t"); - asm("subl $0x736c7561,%ecx\n\t"); - asm("orl %ecx,%eax\n\t"); - asm("movl %%eax,%0\n\t" : "=m" (val)); - asm("popl %ebx\n\t"); - val = (val ? 0 : 1); - via_flags = (val | NEH_CPU_READ); - return val; -} - -INLINE int read_via_flags(void) -{ unsigned char val; - asm("movl $0xc0000000,%eax\n\t"); - asm("cpuid\n\t"); - asm("movl $0xc0000001,%edx\n\t"); - asm("cmpl %edx,%eax\n\t"); - asm("setae %al\n\t"); - asm("movb %%al,%0\n\t" : "=m" (val)); - if(!val) return 0; - asm("movl $0xc0000001,%eax\n\t"); - asm("cpuid\n\t"); - asm("movb %%dl,%0\n\t" : "=m" (val)); - val &= NEH_FLAGS_MASK; - via_flags |= val; - return (int) val; -} - -INLINE int via_rng_in(void *buf) -{ int val; - asm("pushl %edi\n\t"); - asm("movl %0,%%edi\n\t" : : "m" (buf)); - asm("xorl %edx,%edx\n\t"); - NEH_RNG - asm("andl $0x0000001f,%eax\n\t"); - asm("movl %%eax,%0\n\t" : "=m" (val)); - asm("popl %edi\n\t"); - return val; -} - -INLINE volatile void via_ecb_op5( - const void *k, const void *c, const void *s, void *d, int l) -{ - asm("pushl %ebx\n\t"); - NEH_REKEY; - asm("movl %0, %%ebx\n\t" : : "m" (k)); - asm("movl %0, %%edx\n\t" : : "m" (c)); - asm("movl %0, %%esi\n\t" : : "m" (s)); - asm("movl %0, %%edi\n\t" : : "m" (d)); - asm("movl %0, %%ecx\n\t" : : "m" (l)); - NEH_ECB; - asm("popl %ebx\n\t"); -} - -INLINE volatile void via_cbc_op6( - const void *k, const void *c, const void *s, void *d, int l, void *v) -{ - asm("pushl %ebx\n\t"); - NEH_REKEY; - asm("movl %0, %%ebx\n\t" : : "m" (k)); - asm("movl %0, %%edx\n\t" : : "m" (c)); - asm("movl %0, %%esi\n\t" : : "m" (s)); - asm("movl %0, %%edi\n\t" : : "m" (d)); - asm("movl %0, %%ecx\n\t" : : "m" (l)); - asm("movl %0, %%eax\n\t" : : "m" (v)); - NEH_CBC; - asm("popl %ebx\n\t"); -} - -INLINE volatile void via_cbc_op7( - const void *k, const void *c, const void *s, void *d, int l, void *v, void *w) -{ - asm("pushl %ebx\n\t"); - NEH_REKEY; - asm("movl %0, %%ebx\n\t" : : "m" (k)); - asm("movl %0, %%edx\n\t" : : "m" (c)); - asm("movl %0, %%esi\n\t" : : "m" (s)); - asm("movl %0, %%edi\n\t" : : "m" (d)); - asm("movl %0, %%ecx\n\t" : : "m" (l)); - asm("movl %0, %%eax\n\t" : : "m" (v)); - NEH_CBC; - asm("movl %eax,%esi\n\t"); - asm("movl %0, %%edi\n\t" : : "m" (w)); - asm("movsl; movsl; movsl; movsl\n\t"); - asm("popl %ebx\n\t"); -} - -INLINE volatile void via_cfb_op6( - const void *k, const void *c, const void *s, void *d, int l, void *v) -{ - asm("pushl %ebx\n\t"); - NEH_REKEY; - asm("movl %0, %%ebx\n\t" : : "m" (k)); - asm("movl %0, %%edx\n\t" : : "m" (c)); - asm("movl %0, %%esi\n\t" : : "m" (s)); - asm("movl %0, %%edi\n\t" : : "m" (d)); - asm("movl %0, %%ecx\n\t" : : "m" (l)); - asm("movl %0, %%eax\n\t" : : "m" (v)); - NEH_CFB; - asm("popl %ebx\n\t"); -} - -INLINE volatile void via_cfb_op7( - const void *k, const void *c, const void *s, void *d, int l, void *v, void *w) -{ - asm("pushl %ebx\n\t"); - NEH_REKEY; - asm("movl %0, %%ebx\n\t" : : "m" (k)); - asm("movl %0, %%edx\n\t" : : "m" (c)); - asm("movl %0, %%esi\n\t" : : "m" (s)); - asm("movl %0, %%edi\n\t" : : "m" (d)); - asm("movl %0, %%ecx\n\t" : : "m" (l)); - asm("movl %0, %%eax\n\t" : : "m" (v)); - NEH_CFB; - asm("movl %eax,%esi\n\t"); - asm("movl %0, %%edi\n\t" : : "m" (w)); - asm("movsl; movsl; movsl; movsl\n\t"); - asm("popl %ebx\n\t"); -} - -INLINE volatile void via_ofb_op6( - const void *k, const void *c, const void *s, void *d, int l, void *v) -{ - asm("pushl %ebx\n\t"); - NEH_REKEY; - asm("movl %0, %%ebx\n\t" : : "m" (k)); - asm("movl %0, %%edx\n\t" : : "m" (c)); - asm("movl %0, %%esi\n\t" : : "m" (s)); - asm("movl %0, %%edi\n\t" : : "m" (d)); - asm("movl %0, %%ecx\n\t" : : "m" (l)); - asm("movl %0, %%eax\n\t" : : "m" (v)); - NEH_OFB; - asm("popl %ebx\n\t"); -} - -#else -#error VIA ACE is not available with this compiler -#endif - -INLINE int via_ace_test(void) -{ - return has_cpuid() && is_via_cpu() && ((read_via_flags() & NEH_ACE_FLAGS) == NEH_ACE_FLAGS); -} - -#define VIA_ACE_AVAILABLE (((via_flags & NEH_ACE_FLAGS) == NEH_ACE_FLAGS) \ - || (via_flags & NEH_CPU_READ) && (via_flags & NEH_CPU_IS_VIA) || via_ace_test()) - -INLINE int via_rng_test(void) -{ - return has_cpuid() && is_via_cpu() && ((read_via_flags() & NEH_RNG_FLAGS) == NEH_RNG_FLAGS); -} - -#define VIA_RNG_AVAILABLE (((via_flags & NEH_RNG_FLAGS) == NEH_RNG_FLAGS) \ - || (via_flags & NEH_CPU_READ) && (via_flags & NEH_CPU_IS_VIA) || via_rng_test()) - -INLINE int read_via_rng(void *buf, int count) -{ int nbr, max_reads, lcnt = count; - unsigned char *p, *q; - aligned_auto(unsigned char, bp, 64, 16); - - if(!VIA_RNG_AVAILABLE) - return 0; - - do - { - max_reads = MAX_READ_ATTEMPTS; - do - nbr = via_rng_in(bp); - while - (nbr == 0 && --max_reads); - - lcnt -= nbr; - p = (unsigned char*)buf; q = bp; - while(nbr--) - *p++ = *q++; - } - while - (lcnt && max_reads); - - return count - lcnt; -} - -#endif +/* +Copyright (c) 1998-2010, Brian Gladman, Worcester, UK. All rights reserved. + +The redistribution and use of this software (with or without changes) +is allowed without the payment of fees or royalties provided that: + + source code distributions include the above copyright notice, this + list of conditions and the following disclaimer; + + binary distributions include the above copyright notice, this list + of conditions and the following disclaimer in their documentation. + +This software is provided 'as is' with no explicit or implied warranties +in respect of its operation, including, but not limited to, correctness +and fitness for purpose. +--------------------------------------------------------------------------- +Issue Date: 20/12/2007 +*/ + +#ifndef AES_VIA_ACE_H +#define AES_VIA_ACE_H + +#if defined( _MSC_VER ) +# define INLINE __inline +#elif defined( __GNUC__ ) +# define INLINE static inline +#else +# error VIA ACE requires Microsoft or GNU C +#endif + +#define NEH_GENERATE 1 +#define NEH_LOAD 2 +#define NEH_HYBRID 3 + +#define MAX_READ_ATTEMPTS 1000 + +/* VIA Nehemiah RNG and ACE Feature Mask Values */ + +#define NEH_CPU_IS_VIA 0x00000001 +#define NEH_CPU_READ 0x00000010 +#define NEH_CPU_MASK 0x00000011 + +#define NEH_RNG_PRESENT 0x00000004 +#define NEH_RNG_ENABLED 0x00000008 +#define NEH_ACE_PRESENT 0x00000040 +#define NEH_ACE_ENABLED 0x00000080 +#define NEH_RNG_FLAGS (NEH_RNG_PRESENT | NEH_RNG_ENABLED) +#define NEH_ACE_FLAGS (NEH_ACE_PRESENT | NEH_ACE_ENABLED) +#define NEH_FLAGS_MASK (NEH_RNG_FLAGS | NEH_ACE_FLAGS) + +/* VIA Nehemiah Advanced Cryptography Engine (ACE) Control Word Values */ + +#define NEH_GEN_KEY 0x00000000 /* generate key schedule */ +#define NEH_LOAD_KEY 0x00000080 /* load schedule from memory */ +#define NEH_ENCRYPT 0x00000000 /* encryption */ +#define NEH_DECRYPT 0x00000200 /* decryption */ +#define NEH_KEY128 0x00000000+0x0a /* 128 bit key */ +#define NEH_KEY192 0x00000400+0x0c /* 192 bit key */ +#define NEH_KEY256 0x00000800+0x0e /* 256 bit key */ + +#define NEH_ENC_GEN (NEH_ENCRYPT | NEH_GEN_KEY) +#define NEH_DEC_GEN (NEH_DECRYPT | NEH_GEN_KEY) +#define NEH_ENC_LOAD (NEH_ENCRYPT | NEH_LOAD_KEY) +#define NEH_DEC_LOAD (NEH_DECRYPT | NEH_LOAD_KEY) + +#define NEH_ENC_GEN_DATA {\ + NEH_ENC_GEN | NEH_KEY128, 0, 0, 0,\ + NEH_ENC_GEN | NEH_KEY192, 0, 0, 0,\ + NEH_ENC_GEN | NEH_KEY256, 0, 0, 0 } + +#define NEH_ENC_LOAD_DATA {\ + NEH_ENC_LOAD | NEH_KEY128, 0, 0, 0,\ + NEH_ENC_LOAD | NEH_KEY192, 0, 0, 0,\ + NEH_ENC_LOAD | NEH_KEY256, 0, 0, 0 } + +#define NEH_ENC_HYBRID_DATA {\ + NEH_ENC_GEN | NEH_KEY128, 0, 0, 0,\ + NEH_ENC_LOAD | NEH_KEY192, 0, 0, 0,\ + NEH_ENC_LOAD | NEH_KEY256, 0, 0, 0 } + +#define NEH_DEC_GEN_DATA {\ + NEH_DEC_GEN | NEH_KEY128, 0, 0, 0,\ + NEH_DEC_GEN | NEH_KEY192, 0, 0, 0,\ + NEH_DEC_GEN | NEH_KEY256, 0, 0, 0 } + +#define NEH_DEC_LOAD_DATA {\ + NEH_DEC_LOAD | NEH_KEY128, 0, 0, 0,\ + NEH_DEC_LOAD | NEH_KEY192, 0, 0, 0,\ + NEH_DEC_LOAD | NEH_KEY256, 0, 0, 0 } + +#define NEH_DEC_HYBRID_DATA {\ + NEH_DEC_GEN | NEH_KEY128, 0, 0, 0,\ + NEH_DEC_LOAD | NEH_KEY192, 0, 0, 0,\ + NEH_DEC_LOAD | NEH_KEY256, 0, 0, 0 } + +#define neh_enc_gen_key(x) ((x) == 128 ? (NEH_ENC_GEN | NEH_KEY128) : \ + (x) == 192 ? (NEH_ENC_GEN | NEH_KEY192) : (NEH_ENC_GEN | NEH_KEY256)) + +#define neh_enc_load_key(x) ((x) == 128 ? (NEH_ENC_LOAD | NEH_KEY128) : \ + (x) == 192 ? (NEH_ENC_LOAD | NEH_KEY192) : (NEH_ENC_LOAD | NEH_KEY256)) + +#define neh_enc_hybrid_key(x) ((x) == 128 ? (NEH_ENC_GEN | NEH_KEY128) : \ + (x) == 192 ? (NEH_ENC_LOAD | NEH_KEY192) : (NEH_ENC_LOAD | NEH_KEY256)) + +#define neh_dec_gen_key(x) ((x) == 128 ? (NEH_DEC_GEN | NEH_KEY128) : \ + (x) == 192 ? (NEH_DEC_GEN | NEH_KEY192) : (NEH_DEC_GEN | NEH_KEY256)) + +#define neh_dec_load_key(x) ((x) == 128 ? (NEH_DEC_LOAD | NEH_KEY128) : \ + (x) == 192 ? (NEH_DEC_LOAD | NEH_KEY192) : (NEH_DEC_LOAD | NEH_KEY256)) + +#define neh_dec_hybrid_key(x) ((x) == 128 ? (NEH_DEC_GEN | NEH_KEY128) : \ + (x) == 192 ? (NEH_DEC_LOAD | NEH_KEY192) : (NEH_DEC_LOAD | NEH_KEY256)) + +#if defined( _MSC_VER ) && ( _MSC_VER > 1200 ) +#define aligned_auto(type, name, no, stride) __declspec(align(stride)) type name[no] +#else +#define aligned_auto(type, name, no, stride) \ + unsigned char _##name[no * sizeof(type) + stride]; \ + type *name = (type*)(16 * ((((unsigned long)(_##name)) + stride - 1) / stride)) +#endif + +#if defined( _MSC_VER ) && ( _MSC_VER > 1200 ) +#define aligned_array(type, name, no, stride) __declspec(align(stride)) type name[no] +#elif defined( __GNUC__ ) +#define aligned_array(type, name, no, stride) type name[no] __attribute__ ((aligned(stride))) +#else +#define aligned_array(type, name, no, stride) type name[no] +#endif + +/* VIA ACE codeword */ + +static unsigned char via_flags = 0; + +#if defined ( _MSC_VER ) && ( _MSC_VER > 800 ) + +#define NEH_REKEY __asm pushfd __asm popfd +#define NEH_AES __asm _emit 0xf3 __asm _emit 0x0f __asm _emit 0xa7 +#define NEH_ECB NEH_AES __asm _emit 0xc8 +#define NEH_CBC NEH_AES __asm _emit 0xd0 +#define NEH_CFB NEH_AES __asm _emit 0xe0 +#define NEH_OFB NEH_AES __asm _emit 0xe8 +#define NEH_RNG __asm _emit 0x0f __asm _emit 0xa7 __asm _emit 0xc0 + +INLINE int has_cpuid(void) +{ char ret_value; + __asm + { pushfd /* save EFLAGS register */ + mov eax,[esp] /* copy it to eax */ + mov edx,0x00200000 /* CPUID bit position */ + xor eax,edx /* toggle the CPUID bit */ + push eax /* attempt to set EFLAGS to */ + popfd /* the new value */ + pushfd /* get the new EFLAGS value */ + pop eax /* into eax */ + xor eax,[esp] /* xor with original value */ + and eax,edx /* has CPUID bit changed? */ + setne al /* set to 1 if we have been */ + mov ret_value,al /* able to change it */ + popfd /* restore original EFLAGS */ + } + return (int)ret_value; +} + +INLINE int is_via_cpu(void) +{ char ret_value; + __asm + { push ebx + xor eax,eax /* use CPUID to get vendor */ + cpuid /* identity string */ + xor eax,eax /* is it "CentaurHauls" ? */ + sub ebx,0x746e6543 /* 'Cent' */ + or eax,ebx + sub edx,0x48727561 /* 'aurH' */ + or eax,edx + sub ecx,0x736c7561 /* 'auls' */ + or eax,ecx + sete al /* set to 1 if it is VIA ID */ + mov dl,NEH_CPU_READ /* mark CPU type as read */ + or dl,al /* & store result in flags */ + mov [via_flags],dl /* set VIA detected flag */ + mov ret_value,al /* able to change it */ + pop ebx + } + return (int)ret_value; +} + +INLINE int read_via_flags(void) +{ char ret_value = 0; + __asm + { mov eax,0xC0000000 /* Centaur extended CPUID */ + cpuid + mov edx,0xc0000001 /* >= 0xc0000001 if support */ + cmp eax,edx /* for VIA extended feature */ + jnae no_rng /* flags is available */ + mov eax,edx /* read Centaur extended */ + cpuid /* feature flags */ + mov eax,NEH_FLAGS_MASK /* mask out and save */ + and eax,edx /* the RNG and ACE flags */ + or [via_flags],al /* present & enabled flags */ + mov ret_value,al /* able to change it */ +no_rng: + } + return (int)ret_value; +} + +INLINE unsigned int via_rng_in(void *buf) +{ char ret_value = 0x1f; + __asm + { push edi + mov edi,buf /* input buffer address */ + xor edx,edx /* try to fetch 8 bytes */ + NEH_RNG /* do RNG read operation */ + and ret_value,al /* count of bytes returned */ + pop edi + } + return (int)ret_value; +} + +INLINE void via_ecb_op5( + const void *k, const void *c, const void *s, void *d, int l) +{ __asm + { push ebx + NEH_REKEY + mov ebx, (k) + mov edx, (c) + mov esi, (s) + mov edi, (d) + mov ecx, (l) + NEH_ECB + pop ebx + } +} + +INLINE void via_cbc_op6( + const void *k, const void *c, const void *s, void *d, int l, void *v) +{ __asm + { push ebx + NEH_REKEY + mov ebx, (k) + mov edx, (c) + mov esi, (s) + mov edi, (d) + mov ecx, (l) + mov eax, (v) + NEH_CBC + pop ebx + } +} + +INLINE void via_cbc_op7( + const void *k, const void *c, const void *s, void *d, int l, void *v, void *w) +{ __asm + { push ebx + NEH_REKEY + mov ebx, (k) + mov edx, (c) + mov esi, (s) + mov edi, (d) + mov ecx, (l) + mov eax, (v) + NEH_CBC + mov esi, eax + mov edi, (w) + movsd + movsd + movsd + movsd + pop ebx + } +} + +INLINE void via_cfb_op6( + const void *k, const void *c, const void *s, void *d, int l, void *v) +{ __asm + { push ebx + NEH_REKEY + mov ebx, (k) + mov edx, (c) + mov esi, (s) + mov edi, (d) + mov ecx, (l) + mov eax, (v) + NEH_CFB + pop ebx + } +} + +INLINE void via_cfb_op7( + const void *k, const void *c, const void *s, void *d, int l, void *v, void *w) +{ __asm + { push ebx + NEH_REKEY + mov ebx, (k) + mov edx, (c) + mov esi, (s) + mov edi, (d) + mov ecx, (l) + mov eax, (v) + NEH_CFB + mov esi, eax + mov edi, (w) + movsd + movsd + movsd + movsd + pop ebx + } +} + +INLINE void via_ofb_op6( + const void *k, const void *c, const void *s, void *d, int l, void *v) +{ __asm + { push ebx + NEH_REKEY + mov ebx, (k) + mov edx, (c) + mov esi, (s) + mov edi, (d) + mov ecx, (l) + mov eax, (v) + NEH_OFB + pop ebx + } +} + +#elif defined( __GNUC__ ) + +#define NEH_REKEY asm("pushfl\n popfl\n\t") +#define NEH_ECB asm(".byte 0xf3, 0x0f, 0xa7, 0xc8\n\t") +#define NEH_CBC asm(".byte 0xf3, 0x0f, 0xa7, 0xd0\n\t") +#define NEH_CFB asm(".byte 0xf3, 0x0f, 0xa7, 0xe0\n\t") +#define NEH_OFB asm(".byte 0xf3, 0x0f, 0xa7, 0xe8\n\t") +#define NEH_RNG asm(".byte 0x0f, 0xa7, 0xc0\n\t"); + +INLINE int has_cpuid(void) +{ int val; + asm("pushfl\n\t"); + asm("movl 0(%esp),%eax\n\t"); + asm("xor $0x00200000,%eax\n\t"); + asm("pushl %eax\n\t"); + asm("popfl\n\t"); + asm("pushfl\n\t"); + asm("popl %eax\n\t"); + asm("xorl 0(%esp),%edx\n\t"); + asm("andl $0x00200000,%eax\n\t"); + asm("movl %%eax,%0\n\t" : "=m" (val)); + asm("popfl\n\t"); + return val ? 1 : 0; +} + +INLINE int is_via_cpu(void) +{ int val; + asm("pushl %ebx\n\t"); + asm("xorl %eax,%eax\n\t"); + asm("cpuid\n\t"); + asm("xorl %eax,%eax\n\t"); + asm("subl $0x746e6543,%ebx\n\t"); + asm("orl %ebx,%eax\n\t"); + asm("subl $0x48727561,%edx\n\t"); + asm("orl %edx,%eax\n\t"); + asm("subl $0x736c7561,%ecx\n\t"); + asm("orl %ecx,%eax\n\t"); + asm("movl %%eax,%0\n\t" : "=m" (val)); + asm("popl %ebx\n\t"); + val = (val ? 0 : 1); + via_flags = (val | NEH_CPU_READ); + return val; +} + +INLINE int read_via_flags(void) +{ unsigned char val; + asm("movl $0xc0000000,%eax\n\t"); + asm("cpuid\n\t"); + asm("movl $0xc0000001,%edx\n\t"); + asm("cmpl %edx,%eax\n\t"); + asm("setae %al\n\t"); + asm("movb %%al,%0\n\t" : "=m" (val)); + if(!val) return 0; + asm("movl $0xc0000001,%eax\n\t"); + asm("cpuid\n\t"); + asm("movb %%dl,%0\n\t" : "=m" (val)); + val &= NEH_FLAGS_MASK; + via_flags |= val; + return (int) val; +} + +INLINE int via_rng_in(void *buf) +{ int val; + asm("pushl %edi\n\t"); + asm("movl %0,%%edi\n\t" : : "m" (buf)); + asm("xorl %edx,%edx\n\t"); + NEH_RNG + asm("andl $0x0000001f,%eax\n\t"); + asm("movl %%eax,%0\n\t" : "=m" (val)); + asm("popl %edi\n\t"); + return val; +} + +INLINE volatile void via_ecb_op5( + const void *k, const void *c, const void *s, void *d, int l) +{ + asm("pushl %ebx\n\t"); + NEH_REKEY; + asm("movl %0, %%ebx\n\t" : : "m" (k)); + asm("movl %0, %%edx\n\t" : : "m" (c)); + asm("movl %0, %%esi\n\t" : : "m" (s)); + asm("movl %0, %%edi\n\t" : : "m" (d)); + asm("movl %0, %%ecx\n\t" : : "m" (l)); + NEH_ECB; + asm("popl %ebx\n\t"); +} + +INLINE volatile void via_cbc_op6( + const void *k, const void *c, const void *s, void *d, int l, void *v) +{ + asm("pushl %ebx\n\t"); + NEH_REKEY; + asm("movl %0, %%ebx\n\t" : : "m" (k)); + asm("movl %0, %%edx\n\t" : : "m" (c)); + asm("movl %0, %%esi\n\t" : : "m" (s)); + asm("movl %0, %%edi\n\t" : : "m" (d)); + asm("movl %0, %%ecx\n\t" : : "m" (l)); + asm("movl %0, %%eax\n\t" : : "m" (v)); + NEH_CBC; + asm("popl %ebx\n\t"); +} + +INLINE volatile void via_cbc_op7( + const void *k, const void *c, const void *s, void *d, int l, void *v, void *w) +{ + asm("pushl %ebx\n\t"); + NEH_REKEY; + asm("movl %0, %%ebx\n\t" : : "m" (k)); + asm("movl %0, %%edx\n\t" : : "m" (c)); + asm("movl %0, %%esi\n\t" : : "m" (s)); + asm("movl %0, %%edi\n\t" : : "m" (d)); + asm("movl %0, %%ecx\n\t" : : "m" (l)); + asm("movl %0, %%eax\n\t" : : "m" (v)); + NEH_CBC; + asm("movl %eax,%esi\n\t"); + asm("movl %0, %%edi\n\t" : : "m" (w)); + asm("movsl; movsl; movsl; movsl\n\t"); + asm("popl %ebx\n\t"); +} + +INLINE volatile void via_cfb_op6( + const void *k, const void *c, const void *s, void *d, int l, void *v) +{ + asm("pushl %ebx\n\t"); + NEH_REKEY; + asm("movl %0, %%ebx\n\t" : : "m" (k)); + asm("movl %0, %%edx\n\t" : : "m" (c)); + asm("movl %0, %%esi\n\t" : : "m" (s)); + asm("movl %0, %%edi\n\t" : : "m" (d)); + asm("movl %0, %%ecx\n\t" : : "m" (l)); + asm("movl %0, %%eax\n\t" : : "m" (v)); + NEH_CFB; + asm("popl %ebx\n\t"); +} + +INLINE volatile void via_cfb_op7( + const void *k, const void *c, const void *s, void *d, int l, void *v, void *w) +{ + asm("pushl %ebx\n\t"); + NEH_REKEY; + asm("movl %0, %%ebx\n\t" : : "m" (k)); + asm("movl %0, %%edx\n\t" : : "m" (c)); + asm("movl %0, %%esi\n\t" : : "m" (s)); + asm("movl %0, %%edi\n\t" : : "m" (d)); + asm("movl %0, %%ecx\n\t" : : "m" (l)); + asm("movl %0, %%eax\n\t" : : "m" (v)); + NEH_CFB; + asm("movl %eax,%esi\n\t"); + asm("movl %0, %%edi\n\t" : : "m" (w)); + asm("movsl; movsl; movsl; movsl\n\t"); + asm("popl %ebx\n\t"); +} + +INLINE volatile void via_ofb_op6( + const void *k, const void *c, const void *s, void *d, int l, void *v) +{ + asm("pushl %ebx\n\t"); + NEH_REKEY; + asm("movl %0, %%ebx\n\t" : : "m" (k)); + asm("movl %0, %%edx\n\t" : : "m" (c)); + asm("movl %0, %%esi\n\t" : : "m" (s)); + asm("movl %0, %%edi\n\t" : : "m" (d)); + asm("movl %0, %%ecx\n\t" : : "m" (l)); + asm("movl %0, %%eax\n\t" : : "m" (v)); + NEH_OFB; + asm("popl %ebx\n\t"); +} + +#else +#error VIA ACE is not available with this compiler +#endif + +INLINE int via_ace_test(void) +{ + return has_cpuid() && is_via_cpu() && ((read_via_flags() & NEH_ACE_FLAGS) == NEH_ACE_FLAGS); +} + +#define VIA_ACE_AVAILABLE (((via_flags & NEH_ACE_FLAGS) == NEH_ACE_FLAGS) \ + || (via_flags & NEH_CPU_READ) && (via_flags & NEH_CPU_IS_VIA) || via_ace_test()) + +INLINE int via_rng_test(void) +{ + return has_cpuid() && is_via_cpu() && ((read_via_flags() & NEH_RNG_FLAGS) == NEH_RNG_FLAGS); +} + +#define VIA_RNG_AVAILABLE (((via_flags & NEH_RNG_FLAGS) == NEH_RNG_FLAGS) \ + || (via_flags & NEH_CPU_READ) && (via_flags & NEH_CPU_IS_VIA) || via_rng_test()) + +INLINE int read_via_rng(void *buf, int count) +{ int nbr, max_reads, lcnt = count; + unsigned char *p, *q; + aligned_auto(unsigned char, bp, 64, 16); + + if(!VIA_RNG_AVAILABLE) + return 0; + + do + { + max_reads = MAX_READ_ATTEMPTS; + do + nbr = via_rng_in(bp); + while + (nbr == 0 && --max_reads); + + lcnt -= nbr; + p = (unsigned char*)buf; q = bp; + while(nbr--) + *p++ = *q++; + } + while + (lcnt && max_reads); + + return count - lcnt; +} + +#endif diff --git a/Zip/aes/aescrypt.c b/Zip/minizip/aes/aescrypt.c similarity index 97% rename from Zip/aes/aescrypt.c rename to Zip/minizip/aes/aescrypt.c index 99141cf86d8fa6dfe833660ca7ded74b3eec2aa7..6095f41a6816531b6cb20d69664bf8de3774c116 100755 --- a/Zip/aes/aescrypt.c +++ b/Zip/minizip/aes/aescrypt.c @@ -1,294 +1,294 @@ -/* ---------------------------------------------------------------------------- -Copyright (c) 1998-2010, Brian Gladman, Worcester, UK. All rights reserved. - -The redistribution and use of this software (with or without changes) -is allowed without the payment of fees or royalties provided that: - - source code distributions include the above copyright notice, this - list of conditions and the following disclaimer; - - binary distributions include the above copyright notice, this list - of conditions and the following disclaimer in their documentation. - -This software is provided 'as is' with no explicit or implied warranties -in respect of its operation, including, but not limited to, correctness -and fitness for purpose. ---------------------------------------------------------------------------- -Issue Date: 20/12/2007 -*/ - -#include "aesopt.h" -#include "aestab.h" - -#if defined(__cplusplus) -extern "C" -{ -#endif - -#define si(y,x,k,c) (s(y,c) = word_in(x, c) ^ (k)[c]) -#define so(y,x,c) word_out(y, c, s(x,c)) - -#if defined(ARRAYS) -#define locals(y,x) x[4],y[4] -#else -#define locals(y,x) x##0,x##1,x##2,x##3,y##0,y##1,y##2,y##3 -#endif - -#define l_copy(y, x) s(y,0) = s(x,0); s(y,1) = s(x,1); \ - s(y,2) = s(x,2); s(y,3) = s(x,3); -#define state_in(y,x,k) si(y,x,k,0); si(y,x,k,1); si(y,x,k,2); si(y,x,k,3) -#define state_out(y,x) so(y,x,0); so(y,x,1); so(y,x,2); so(y,x,3) -#define round(rm,y,x,k) rm(y,x,k,0); rm(y,x,k,1); rm(y,x,k,2); rm(y,x,k,3) - -#if ( FUNCS_IN_C & ENCRYPTION_IN_C ) - -/* Visual C++ .Net v7.1 provides the fastest encryption code when using - Pentium optimiation with small code but this is poor for decryption - so we need to control this with the following VC++ pragmas -*/ - -#if defined( _MSC_VER ) && !defined( _WIN64 ) -#pragma optimize( "s", on ) -#endif - -/* Given the column (c) of the output state variable, the following - macros give the input state variables which are needed in its - computation for each row (r) of the state. All the alternative - macros give the same end values but expand into different ways - of calculating these values. In particular the complex macro - used for dynamically variable block sizes is designed to expand - to a compile time constant whenever possible but will expand to - conditional clauses on some branches (I am grateful to Frank - Yellin for this construction) -*/ - -#define fwd_var(x,r,c)\ - ( r == 0 ? ( c == 0 ? s(x,0) : c == 1 ? s(x,1) : c == 2 ? s(x,2) : s(x,3))\ - : r == 1 ? ( c == 0 ? s(x,1) : c == 1 ? s(x,2) : c == 2 ? s(x,3) : s(x,0))\ - : r == 2 ? ( c == 0 ? s(x,2) : c == 1 ? s(x,3) : c == 2 ? s(x,0) : s(x,1))\ - : ( c == 0 ? s(x,3) : c == 1 ? s(x,0) : c == 2 ? s(x,1) : s(x,2))) - -#if defined(FT4_SET) -#undef dec_fmvars -#define fwd_rnd(y,x,k,c) (s(y,c) = (k)[c] ^ four_tables(x,t_use(f,n),fwd_var,rf1,c)) -#elif defined(FT1_SET) -#undef dec_fmvars -#define fwd_rnd(y,x,k,c) (s(y,c) = (k)[c] ^ one_table(x,upr,t_use(f,n),fwd_var,rf1,c)) -#else -#define fwd_rnd(y,x,k,c) (s(y,c) = (k)[c] ^ fwd_mcol(no_table(x,t_use(s,box),fwd_var,rf1,c))) -#endif - -#if defined(FL4_SET) -#define fwd_lrnd(y,x,k,c) (s(y,c) = (k)[c] ^ four_tables(x,t_use(f,l),fwd_var,rf1,c)) -#elif defined(FL1_SET) -#define fwd_lrnd(y,x,k,c) (s(y,c) = (k)[c] ^ one_table(x,ups,t_use(f,l),fwd_var,rf1,c)) -#else -#define fwd_lrnd(y,x,k,c) (s(y,c) = (k)[c] ^ no_table(x,t_use(s,box),fwd_var,rf1,c)) -#endif - -AES_RETURN aes_encrypt(const unsigned char *in, unsigned char *out, const aes_encrypt_ctx cx[1]) -{ uint_32t locals(b0, b1); - const uint_32t *kp; -#if defined( dec_fmvars ) - dec_fmvars; /* declare variables for fwd_mcol() if needed */ -#endif - - if( cx->inf.b[0] != 10 * 16 && cx->inf.b[0] != 12 * 16 && cx->inf.b[0] != 14 * 16 ) - return EXIT_FAILURE; - - kp = cx->ks; - state_in(b0, in, kp); - -#if (ENC_UNROLL == FULL) - - switch(cx->inf.b[0]) - { - case 14 * 16: - round(fwd_rnd, b1, b0, kp + 1 * N_COLS); - round(fwd_rnd, b0, b1, kp + 2 * N_COLS); - kp += 2 * N_COLS; - case 12 * 16: - round(fwd_rnd, b1, b0, kp + 1 * N_COLS); - round(fwd_rnd, b0, b1, kp + 2 * N_COLS); - kp += 2 * N_COLS; - case 10 * 16: - round(fwd_rnd, b1, b0, kp + 1 * N_COLS); - round(fwd_rnd, b0, b1, kp + 2 * N_COLS); - round(fwd_rnd, b1, b0, kp + 3 * N_COLS); - round(fwd_rnd, b0, b1, kp + 4 * N_COLS); - round(fwd_rnd, b1, b0, kp + 5 * N_COLS); - round(fwd_rnd, b0, b1, kp + 6 * N_COLS); - round(fwd_rnd, b1, b0, kp + 7 * N_COLS); - round(fwd_rnd, b0, b1, kp + 8 * N_COLS); - round(fwd_rnd, b1, b0, kp + 9 * N_COLS); - round(fwd_lrnd, b0, b1, kp +10 * N_COLS); - } - -#else - -#if (ENC_UNROLL == PARTIAL) - { uint_32t rnd; - for(rnd = 0; rnd < (cx->inf.b[0] >> 5) - 1; ++rnd) - { - kp += N_COLS; - round(fwd_rnd, b1, b0, kp); - kp += N_COLS; - round(fwd_rnd, b0, b1, kp); - } - kp += N_COLS; - round(fwd_rnd, b1, b0, kp); -#else - { uint_32t rnd; - for(rnd = 0; rnd < (cx->inf.b[0] >> 4) - 1; ++rnd) - { - kp += N_COLS; - round(fwd_rnd, b1, b0, kp); - l_copy(b0, b1); - } -#endif - kp += N_COLS; - round(fwd_lrnd, b0, b1, kp); - } -#endif - - state_out(out, b0); - return EXIT_SUCCESS; -} - -#endif - -#if ( FUNCS_IN_C & DECRYPTION_IN_C) - -/* Visual C++ .Net v7.1 provides the fastest encryption code when using - Pentium optimiation with small code but this is poor for decryption - so we need to control this with the following VC++ pragmas -*/ - -#if defined( _MSC_VER ) && !defined( _WIN64 ) -#pragma optimize( "t", on ) -#endif - -/* Given the column (c) of the output state variable, the following - macros give the input state variables which are needed in its - computation for each row (r) of the state. All the alternative - macros give the same end values but expand into different ways - of calculating these values. In particular the complex macro - used for dynamically variable block sizes is designed to expand - to a compile time constant whenever possible but will expand to - conditional clauses on some branches (I am grateful to Frank - Yellin for this construction) -*/ - -#define inv_var(x,r,c)\ - ( r == 0 ? ( c == 0 ? s(x,0) : c == 1 ? s(x,1) : c == 2 ? s(x,2) : s(x,3))\ - : r == 1 ? ( c == 0 ? s(x,3) : c == 1 ? s(x,0) : c == 2 ? s(x,1) : s(x,2))\ - : r == 2 ? ( c == 0 ? s(x,2) : c == 1 ? s(x,3) : c == 2 ? s(x,0) : s(x,1))\ - : ( c == 0 ? s(x,1) : c == 1 ? s(x,2) : c == 2 ? s(x,3) : s(x,0))) - -#if defined(IT4_SET) -#undef dec_imvars -#define inv_rnd(y,x,k,c) (s(y,c) = (k)[c] ^ four_tables(x,t_use(i,n),inv_var,rf1,c)) -#elif defined(IT1_SET) -#undef dec_imvars -#define inv_rnd(y,x,k,c) (s(y,c) = (k)[c] ^ one_table(x,upr,t_use(i,n),inv_var,rf1,c)) -#else -#define inv_rnd(y,x,k,c) (s(y,c) = inv_mcol((k)[c] ^ no_table(x,t_use(i,box),inv_var,rf1,c))) -#endif - -#if defined(IL4_SET) -#define inv_lrnd(y,x,k,c) (s(y,c) = (k)[c] ^ four_tables(x,t_use(i,l),inv_var,rf1,c)) -#elif defined(IL1_SET) -#define inv_lrnd(y,x,k,c) (s(y,c) = (k)[c] ^ one_table(x,ups,t_use(i,l),inv_var,rf1,c)) -#else -#define inv_lrnd(y,x,k,c) (s(y,c) = (k)[c] ^ no_table(x,t_use(i,box),inv_var,rf1,c)) -#endif - -/* This code can work with the decryption key schedule in the */ -/* order that is used for encrytpion (where the 1st decryption */ -/* round key is at the high end ot the schedule) or with a key */ -/* schedule that has been reversed to put the 1st decryption */ -/* round key at the low end of the schedule in memory (when */ -/* AES_REV_DKS is defined) */ - -#ifdef AES_REV_DKS -#define key_ofs 0 -#define rnd_key(n) (kp + n * N_COLS) -#else -#define key_ofs 1 -#define rnd_key(n) (kp - n * N_COLS) -#endif - -AES_RETURN aes_decrypt(const unsigned char *in, unsigned char *out, const aes_decrypt_ctx cx[1]) -{ uint_32t locals(b0, b1); -#if defined( dec_imvars ) - dec_imvars; /* declare variables for inv_mcol() if needed */ -#endif - const uint_32t *kp; - - if( cx->inf.b[0] != 10 * 16 && cx->inf.b[0] != 12 * 16 && cx->inf.b[0] != 14 * 16 ) - return EXIT_FAILURE; - - kp = cx->ks + (key_ofs ? (cx->inf.b[0] >> 2) : 0); - state_in(b0, in, kp); - -#if (DEC_UNROLL == FULL) - - kp = cx->ks + (key_ofs ? 0 : (cx->inf.b[0] >> 2)); - switch(cx->inf.b[0]) - { - case 14 * 16: - round(inv_rnd, b1, b0, rnd_key(-13)); - round(inv_rnd, b0, b1, rnd_key(-12)); - case 12 * 16: - round(inv_rnd, b1, b0, rnd_key(-11)); - round(inv_rnd, b0, b1, rnd_key(-10)); - case 10 * 16: - round(inv_rnd, b1, b0, rnd_key(-9)); - round(inv_rnd, b0, b1, rnd_key(-8)); - round(inv_rnd, b1, b0, rnd_key(-7)); - round(inv_rnd, b0, b1, rnd_key(-6)); - round(inv_rnd, b1, b0, rnd_key(-5)); - round(inv_rnd, b0, b1, rnd_key(-4)); - round(inv_rnd, b1, b0, rnd_key(-3)); - round(inv_rnd, b0, b1, rnd_key(-2)); - round(inv_rnd, b1, b0, rnd_key(-1)); - round(inv_lrnd, b0, b1, rnd_key( 0)); - } - -#else - -#if (DEC_UNROLL == PARTIAL) - { uint_32t rnd; - for(rnd = 0; rnd < (cx->inf.b[0] >> 5) - 1; ++rnd) - { - kp = rnd_key(1); - round(inv_rnd, b1, b0, kp); - kp = rnd_key(1); - round(inv_rnd, b0, b1, kp); - } - kp = rnd_key(1); - round(inv_rnd, b1, b0, kp); -#else - { uint_32t rnd; - for(rnd = 0; rnd < (cx->inf.b[0] >> 4) - 1; ++rnd) - { - kp = rnd_key(1); - round(inv_rnd, b1, b0, kp); - l_copy(b0, b1); - } -#endif - kp = rnd_key(1); - round(inv_lrnd, b0, b1, kp); - } -#endif - - state_out(out, b0); - return EXIT_SUCCESS; -} - -#endif - -#if defined(__cplusplus) -} -#endif +/* +--------------------------------------------------------------------------- +Copyright (c) 1998-2010, Brian Gladman, Worcester, UK. All rights reserved. + +The redistribution and use of this software (with or without changes) +is allowed without the payment of fees or royalties provided that: + + source code distributions include the above copyright notice, this + list of conditions and the following disclaimer; + + binary distributions include the above copyright notice, this list + of conditions and the following disclaimer in their documentation. + +This software is provided 'as is' with no explicit or implied warranties +in respect of its operation, including, but not limited to, correctness +and fitness for purpose. +--------------------------------------------------------------------------- +Issue Date: 20/12/2007 +*/ + +#include "aesopt.h" +#include "aestab.h" + +#if defined(__cplusplus) +extern "C" +{ +#endif + +#define si(y,x,k,c) (s(y,c) = word_in(x, c) ^ (k)[c]) +#define so(y,x,c) word_out(y, c, s(x,c)) + +#if defined(ARRAYS) +#define locals(y,x) x[4],y[4] +#else +#define locals(y,x) x##0,x##1,x##2,x##3,y##0,y##1,y##2,y##3 +#endif + +#define l_copy(y, x) s(y,0) = s(x,0); s(y,1) = s(x,1); \ + s(y,2) = s(x,2); s(y,3) = s(x,3); +#define state_in(y,x,k) si(y,x,k,0); si(y,x,k,1); si(y,x,k,2); si(y,x,k,3) +#define state_out(y,x) so(y,x,0); so(y,x,1); so(y,x,2); so(y,x,3) +#define round(rm,y,x,k) rm(y,x,k,0); rm(y,x,k,1); rm(y,x,k,2); rm(y,x,k,3) + +#if ( FUNCS_IN_C & ENCRYPTION_IN_C ) + +/* Visual C++ .Net v7.1 provides the fastest encryption code when using + Pentium optimiation with small code but this is poor for decryption + so we need to control this with the following VC++ pragmas +*/ + +#if defined( _MSC_VER ) && !defined( _WIN64 ) +#pragma optimize( "s", on ) +#endif + +/* Given the column (c) of the output state variable, the following + macros give the input state variables which are needed in its + computation for each row (r) of the state. All the alternative + macros give the same end values but expand into different ways + of calculating these values. In particular the complex macro + used for dynamically variable block sizes is designed to expand + to a compile time constant whenever possible but will expand to + conditional clauses on some branches (I am grateful to Frank + Yellin for this construction) +*/ + +#define fwd_var(x,r,c)\ + ( r == 0 ? ( c == 0 ? s(x,0) : c == 1 ? s(x,1) : c == 2 ? s(x,2) : s(x,3))\ + : r == 1 ? ( c == 0 ? s(x,1) : c == 1 ? s(x,2) : c == 2 ? s(x,3) : s(x,0))\ + : r == 2 ? ( c == 0 ? s(x,2) : c == 1 ? s(x,3) : c == 2 ? s(x,0) : s(x,1))\ + : ( c == 0 ? s(x,3) : c == 1 ? s(x,0) : c == 2 ? s(x,1) : s(x,2))) + +#if defined(FT4_SET) +#undef dec_fmvars +#define fwd_rnd(y,x,k,c) (s(y,c) = (k)[c] ^ four_tables(x,t_use(f,n),fwd_var,rf1,c)) +#elif defined(FT1_SET) +#undef dec_fmvars +#define fwd_rnd(y,x,k,c) (s(y,c) = (k)[c] ^ one_table(x,upr,t_use(f,n),fwd_var,rf1,c)) +#else +#define fwd_rnd(y,x,k,c) (s(y,c) = (k)[c] ^ fwd_mcol(no_table(x,t_use(s,box),fwd_var,rf1,c))) +#endif + +#if defined(FL4_SET) +#define fwd_lrnd(y,x,k,c) (s(y,c) = (k)[c] ^ four_tables(x,t_use(f,l),fwd_var,rf1,c)) +#elif defined(FL1_SET) +#define fwd_lrnd(y,x,k,c) (s(y,c) = (k)[c] ^ one_table(x,ups,t_use(f,l),fwd_var,rf1,c)) +#else +#define fwd_lrnd(y,x,k,c) (s(y,c) = (k)[c] ^ no_table(x,t_use(s,box),fwd_var,rf1,c)) +#endif + +AES_RETURN aes_encrypt(const unsigned char *in, unsigned char *out, const aes_encrypt_ctx cx[1]) +{ uint_32t locals(b0, b1); + const uint_32t *kp; +#if defined( dec_fmvars ) + dec_fmvars; /* declare variables for fwd_mcol() if needed */ +#endif + + if( cx->inf.b[0] != 10 * 16 && cx->inf.b[0] != 12 * 16 && cx->inf.b[0] != 14 * 16 ) + return EXIT_FAILURE; + + kp = cx->ks; + state_in(b0, in, kp); + +#if (ENC_UNROLL == FULL) + + switch(cx->inf.b[0]) + { + case 14 * 16: + round(fwd_rnd, b1, b0, kp + 1 * N_COLS); + round(fwd_rnd, b0, b1, kp + 2 * N_COLS); + kp += 2 * N_COLS; + case 12 * 16: + round(fwd_rnd, b1, b0, kp + 1 * N_COLS); + round(fwd_rnd, b0, b1, kp + 2 * N_COLS); + kp += 2 * N_COLS; + case 10 * 16: + round(fwd_rnd, b1, b0, kp + 1 * N_COLS); + round(fwd_rnd, b0, b1, kp + 2 * N_COLS); + round(fwd_rnd, b1, b0, kp + 3 * N_COLS); + round(fwd_rnd, b0, b1, kp + 4 * N_COLS); + round(fwd_rnd, b1, b0, kp + 5 * N_COLS); + round(fwd_rnd, b0, b1, kp + 6 * N_COLS); + round(fwd_rnd, b1, b0, kp + 7 * N_COLS); + round(fwd_rnd, b0, b1, kp + 8 * N_COLS); + round(fwd_rnd, b1, b0, kp + 9 * N_COLS); + round(fwd_lrnd, b0, b1, kp +10 * N_COLS); + } + +#else + +#if (ENC_UNROLL == PARTIAL) + { uint_32t rnd; + for(rnd = 0; rnd < (cx->inf.b[0] >> 5) - 1; ++rnd) + { + kp += N_COLS; + round(fwd_rnd, b1, b0, kp); + kp += N_COLS; + round(fwd_rnd, b0, b1, kp); + } + kp += N_COLS; + round(fwd_rnd, b1, b0, kp); +#else + { uint_32t rnd; + for(rnd = 0; rnd < (cx->inf.b[0] >> 4) - 1; ++rnd) + { + kp += N_COLS; + round(fwd_rnd, b1, b0, kp); + l_copy(b0, b1); + } +#endif + kp += N_COLS; + round(fwd_lrnd, b0, b1, kp); + } +#endif + + state_out(out, b0); + return EXIT_SUCCESS; +} + +#endif + +#if ( FUNCS_IN_C & DECRYPTION_IN_C) + +/* Visual C++ .Net v7.1 provides the fastest encryption code when using + Pentium optimiation with small code but this is poor for decryption + so we need to control this with the following VC++ pragmas +*/ + +#if defined( _MSC_VER ) && !defined( _WIN64 ) +#pragma optimize( "t", on ) +#endif + +/* Given the column (c) of the output state variable, the following + macros give the input state variables which are needed in its + computation for each row (r) of the state. All the alternative + macros give the same end values but expand into different ways + of calculating these values. In particular the complex macro + used for dynamically variable block sizes is designed to expand + to a compile time constant whenever possible but will expand to + conditional clauses on some branches (I am grateful to Frank + Yellin for this construction) +*/ + +#define inv_var(x,r,c)\ + ( r == 0 ? ( c == 0 ? s(x,0) : c == 1 ? s(x,1) : c == 2 ? s(x,2) : s(x,3))\ + : r == 1 ? ( c == 0 ? s(x,3) : c == 1 ? s(x,0) : c == 2 ? s(x,1) : s(x,2))\ + : r == 2 ? ( c == 0 ? s(x,2) : c == 1 ? s(x,3) : c == 2 ? s(x,0) : s(x,1))\ + : ( c == 0 ? s(x,1) : c == 1 ? s(x,2) : c == 2 ? s(x,3) : s(x,0))) + +#if defined(IT4_SET) +#undef dec_imvars +#define inv_rnd(y,x,k,c) (s(y,c) = (k)[c] ^ four_tables(x,t_use(i,n),inv_var,rf1,c)) +#elif defined(IT1_SET) +#undef dec_imvars +#define inv_rnd(y,x,k,c) (s(y,c) = (k)[c] ^ one_table(x,upr,t_use(i,n),inv_var,rf1,c)) +#else +#define inv_rnd(y,x,k,c) (s(y,c) = inv_mcol((k)[c] ^ no_table(x,t_use(i,box),inv_var,rf1,c))) +#endif + +#if defined(IL4_SET) +#define inv_lrnd(y,x,k,c) (s(y,c) = (k)[c] ^ four_tables(x,t_use(i,l),inv_var,rf1,c)) +#elif defined(IL1_SET) +#define inv_lrnd(y,x,k,c) (s(y,c) = (k)[c] ^ one_table(x,ups,t_use(i,l),inv_var,rf1,c)) +#else +#define inv_lrnd(y,x,k,c) (s(y,c) = (k)[c] ^ no_table(x,t_use(i,box),inv_var,rf1,c)) +#endif + +/* This code can work with the decryption key schedule in the */ +/* order that is used for encrytpion (where the 1st decryption */ +/* round key is at the high end ot the schedule) or with a key */ +/* schedule that has been reversed to put the 1st decryption */ +/* round key at the low end of the schedule in memory (when */ +/* AES_REV_DKS is defined) */ + +#ifdef AES_REV_DKS +#define key_ofs 0 +#define rnd_key(n) (kp + n * N_COLS) +#else +#define key_ofs 1 +#define rnd_key(n) (kp - n * N_COLS) +#endif + +AES_RETURN aes_decrypt(const unsigned char *in, unsigned char *out, const aes_decrypt_ctx cx[1]) +{ uint_32t locals(b0, b1); +#if defined( dec_imvars ) + dec_imvars; /* declare variables for inv_mcol() if needed */ +#endif + const uint_32t *kp; + + if( cx->inf.b[0] != 10 * 16 && cx->inf.b[0] != 12 * 16 && cx->inf.b[0] != 14 * 16 ) + return EXIT_FAILURE; + + kp = cx->ks + (key_ofs ? (cx->inf.b[0] >> 2) : 0); + state_in(b0, in, kp); + +#if (DEC_UNROLL == FULL) + + kp = cx->ks + (key_ofs ? 0 : (cx->inf.b[0] >> 2)); + switch(cx->inf.b[0]) + { + case 14 * 16: + round(inv_rnd, b1, b0, rnd_key(-13)); + round(inv_rnd, b0, b1, rnd_key(-12)); + case 12 * 16: + round(inv_rnd, b1, b0, rnd_key(-11)); + round(inv_rnd, b0, b1, rnd_key(-10)); + case 10 * 16: + round(inv_rnd, b1, b0, rnd_key(-9)); + round(inv_rnd, b0, b1, rnd_key(-8)); + round(inv_rnd, b1, b0, rnd_key(-7)); + round(inv_rnd, b0, b1, rnd_key(-6)); + round(inv_rnd, b1, b0, rnd_key(-5)); + round(inv_rnd, b0, b1, rnd_key(-4)); + round(inv_rnd, b1, b0, rnd_key(-3)); + round(inv_rnd, b0, b1, rnd_key(-2)); + round(inv_rnd, b1, b0, rnd_key(-1)); + round(inv_lrnd, b0, b1, rnd_key( 0)); + } + +#else + +#if (DEC_UNROLL == PARTIAL) + { uint_32t rnd; + for(rnd = 0; rnd < (cx->inf.b[0] >> 5) - 1; ++rnd) + { + kp = rnd_key(1); + round(inv_rnd, b1, b0, kp); + kp = rnd_key(1); + round(inv_rnd, b0, b1, kp); + } + kp = rnd_key(1); + round(inv_rnd, b1, b0, kp); +#else + { uint_32t rnd; + for(rnd = 0; rnd < (cx->inf.b[0] >> 4) - 1; ++rnd) + { + kp = rnd_key(1); + round(inv_rnd, b1, b0, kp); + l_copy(b0, b1); + } +#endif + kp = rnd_key(1); + round(inv_lrnd, b0, b1, kp); + } +#endif + + state_out(out, b0); + return EXIT_SUCCESS; +} + +#endif + +#if defined(__cplusplus) +} +#endif diff --git a/Zip/aes/aeskey.c b/Zip/minizip/aes/aeskey.c similarity index 96% rename from Zip/aes/aeskey.c rename to Zip/minizip/aes/aeskey.c index 0378f0cf69fed32095a701da65e747632bcc36ba..3633641a30d8a17ef1e726794b923e4cc63f8dee 100755 --- a/Zip/aes/aeskey.c +++ b/Zip/minizip/aes/aeskey.c @@ -1,548 +1,548 @@ -/* ---------------------------------------------------------------------------- -Copyright (c) 1998-2010, Brian Gladman, Worcester, UK. All rights reserved. - -The redistribution and use of this software (with or without changes) -is allowed without the payment of fees or royalties provided that: - - source code distributions include the above copyright notice, this - list of conditions and the following disclaimer; - - binary distributions include the above copyright notice, this list - of conditions and the following disclaimer in their documentation. - -This software is provided 'as is' with no explicit or implied warranties -in respect of its operation, including, but not limited to, correctness -and fitness for purpose. ---------------------------------------------------------------------------- -Issue Date: 20/12/2007 -*/ - -#include "aesopt.h" -#include "aestab.h" - -#ifdef USE_VIA_ACE_IF_PRESENT -# include "aes_via_ace.h" -#endif - -#if defined(__cplusplus) -extern "C" -{ -#endif - -/* Initialise the key schedule from the user supplied key. The key - length can be specified in bytes, with legal values of 16, 24 - and 32, or in bits, with legal values of 128, 192 and 256. These - values correspond with Nk values of 4, 6 and 8 respectively. - - The following macros implement a single cycle in the key - schedule generation process. The number of cycles needed - for each cx->n_col and nk value is: - - nk = 4 5 6 7 8 - ------------------------------ - cx->n_col = 4 10 9 8 7 7 - cx->n_col = 5 14 11 10 9 9 - cx->n_col = 6 19 15 12 11 11 - cx->n_col = 7 21 19 16 13 14 - cx->n_col = 8 29 23 19 17 14 -*/ - -#if defined( REDUCE_CODE_SIZE ) -# define ls_box ls_sub - uint_32t ls_sub(const uint_32t t, const uint_32t n); -# define inv_mcol im_sub - uint_32t im_sub(const uint_32t x); -# ifdef ENC_KS_UNROLL -# undef ENC_KS_UNROLL -# endif -# ifdef DEC_KS_UNROLL -# undef DEC_KS_UNROLL -# endif -#endif - -#if (FUNCS_IN_C & ENC_KEYING_IN_C) - -#if defined(AES_128) || defined( AES_VAR ) - -#define ke4(k,i) \ -{ k[4*(i)+4] = ss[0] ^= ls_box(ss[3],3) ^ t_use(r,c)[i]; \ - k[4*(i)+5] = ss[1] ^= ss[0]; \ - k[4*(i)+6] = ss[2] ^= ss[1]; \ - k[4*(i)+7] = ss[3] ^= ss[2]; \ -} - -AES_RETURN aes_encrypt_key128(const unsigned char *key, aes_encrypt_ctx cx[1]) -{ uint_32t ss[4]; - - cx->ks[0] = ss[0] = word_in(key, 0); - cx->ks[1] = ss[1] = word_in(key, 1); - cx->ks[2] = ss[2] = word_in(key, 2); - cx->ks[3] = ss[3] = word_in(key, 3); - -#ifdef ENC_KS_UNROLL - ke4(cx->ks, 0); ke4(cx->ks, 1); - ke4(cx->ks, 2); ke4(cx->ks, 3); - ke4(cx->ks, 4); ke4(cx->ks, 5); - ke4(cx->ks, 6); ke4(cx->ks, 7); - ke4(cx->ks, 8); -#else - { uint_32t i; - for(i = 0; i < 9; ++i) - ke4(cx->ks, i); - } -#endif - ke4(cx->ks, 9); - cx->inf.l = 0; - cx->inf.b[0] = 10 * 16; - -#ifdef USE_VIA_ACE_IF_PRESENT - if(VIA_ACE_AVAILABLE) - cx->inf.b[1] = 0xff; -#endif - return EXIT_SUCCESS; -} - -#endif - -#if defined(AES_192) || defined( AES_VAR ) - -#define kef6(k,i) \ -{ k[6*(i)+ 6] = ss[0] ^= ls_box(ss[5],3) ^ t_use(r,c)[i]; \ - k[6*(i)+ 7] = ss[1] ^= ss[0]; \ - k[6*(i)+ 8] = ss[2] ^= ss[1]; \ - k[6*(i)+ 9] = ss[3] ^= ss[2]; \ -} - -#define ke6(k,i) \ -{ kef6(k,i); \ - k[6*(i)+10] = ss[4] ^= ss[3]; \ - k[6*(i)+11] = ss[5] ^= ss[4]; \ -} - -AES_RETURN aes_encrypt_key192(const unsigned char *key, aes_encrypt_ctx cx[1]) -{ uint_32t ss[6]; - - cx->ks[0] = ss[0] = word_in(key, 0); - cx->ks[1] = ss[1] = word_in(key, 1); - cx->ks[2] = ss[2] = word_in(key, 2); - cx->ks[3] = ss[3] = word_in(key, 3); - cx->ks[4] = ss[4] = word_in(key, 4); - cx->ks[5] = ss[5] = word_in(key, 5); - -#ifdef ENC_KS_UNROLL - ke6(cx->ks, 0); ke6(cx->ks, 1); - ke6(cx->ks, 2); ke6(cx->ks, 3); - ke6(cx->ks, 4); ke6(cx->ks, 5); - ke6(cx->ks, 6); -#else - { uint_32t i; - for(i = 0; i < 7; ++i) - ke6(cx->ks, i); - } -#endif - kef6(cx->ks, 7); - cx->inf.l = 0; - cx->inf.b[0] = 12 * 16; - -#ifdef USE_VIA_ACE_IF_PRESENT - if(VIA_ACE_AVAILABLE) - cx->inf.b[1] = 0xff; -#endif - return EXIT_SUCCESS; -} - -#endif - -#if defined(AES_256) || defined( AES_VAR ) - -#define kef8(k,i) \ -{ k[8*(i)+ 8] = ss[0] ^= ls_box(ss[7],3) ^ t_use(r,c)[i]; \ - k[8*(i)+ 9] = ss[1] ^= ss[0]; \ - k[8*(i)+10] = ss[2] ^= ss[1]; \ - k[8*(i)+11] = ss[3] ^= ss[2]; \ -} - -#define ke8(k,i) \ -{ kef8(k,i); \ - k[8*(i)+12] = ss[4] ^= ls_box(ss[3],0); \ - k[8*(i)+13] = ss[5] ^= ss[4]; \ - k[8*(i)+14] = ss[6] ^= ss[5]; \ - k[8*(i)+15] = ss[7] ^= ss[6]; \ -} - -AES_RETURN aes_encrypt_key256(const unsigned char *key, aes_encrypt_ctx cx[1]) -{ uint_32t ss[8]; - - cx->ks[0] = ss[0] = word_in(key, 0); - cx->ks[1] = ss[1] = word_in(key, 1); - cx->ks[2] = ss[2] = word_in(key, 2); - cx->ks[3] = ss[3] = word_in(key, 3); - cx->ks[4] = ss[4] = word_in(key, 4); - cx->ks[5] = ss[5] = word_in(key, 5); - cx->ks[6] = ss[6] = word_in(key, 6); - cx->ks[7] = ss[7] = word_in(key, 7); - -#ifdef ENC_KS_UNROLL - ke8(cx->ks, 0); ke8(cx->ks, 1); - ke8(cx->ks, 2); ke8(cx->ks, 3); - ke8(cx->ks, 4); ke8(cx->ks, 5); -#else - { uint_32t i; - for(i = 0; i < 6; ++i) - ke8(cx->ks, i); - } -#endif - kef8(cx->ks, 6); - cx->inf.l = 0; - cx->inf.b[0] = 14 * 16; - -#ifdef USE_VIA_ACE_IF_PRESENT - if(VIA_ACE_AVAILABLE) - cx->inf.b[1] = 0xff; -#endif - return EXIT_SUCCESS; -} - -#endif - -#if defined( AES_VAR ) - -AES_RETURN aes_encrypt_key(const unsigned char *key, int key_len, aes_encrypt_ctx cx[1]) -{ - switch(key_len) - { - case 16: case 128: return aes_encrypt_key128(key, cx); - case 24: case 192: return aes_encrypt_key192(key, cx); - case 32: case 256: return aes_encrypt_key256(key, cx); - default: return EXIT_FAILURE; - } -} - -#endif - -#endif - -#if (FUNCS_IN_C & DEC_KEYING_IN_C) - -/* this is used to store the decryption round keys */ -/* in forward or reverse order */ - -#ifdef AES_REV_DKS -#define v(n,i) ((n) - (i) + 2 * ((i) & 3)) -#else -#define v(n,i) (i) -#endif - -#if DEC_ROUND == NO_TABLES -#define ff(x) (x) -#else -#define ff(x) inv_mcol(x) -#if defined( dec_imvars ) -#define d_vars dec_imvars -#endif -#endif - -#if defined(AES_128) || defined( AES_VAR ) - -#define k4e(k,i) \ -{ k[v(40,(4*(i))+4)] = ss[0] ^= ls_box(ss[3],3) ^ t_use(r,c)[i]; \ - k[v(40,(4*(i))+5)] = ss[1] ^= ss[0]; \ - k[v(40,(4*(i))+6)] = ss[2] ^= ss[1]; \ - k[v(40,(4*(i))+7)] = ss[3] ^= ss[2]; \ -} - -#if 1 - -#define kdf4(k,i) \ -{ ss[0] = ss[0] ^ ss[2] ^ ss[1] ^ ss[3]; \ - ss[1] = ss[1] ^ ss[3]; \ - ss[2] = ss[2] ^ ss[3]; \ - ss[4] = ls_box(ss[(i+3) % 4], 3) ^ t_use(r,c)[i]; \ - ss[i % 4] ^= ss[4]; \ - ss[4] ^= k[v(40,(4*(i)))]; k[v(40,(4*(i))+4)] = ff(ss[4]); \ - ss[4] ^= k[v(40,(4*(i))+1)]; k[v(40,(4*(i))+5)] = ff(ss[4]); \ - ss[4] ^= k[v(40,(4*(i))+2)]; k[v(40,(4*(i))+6)] = ff(ss[4]); \ - ss[4] ^= k[v(40,(4*(i))+3)]; k[v(40,(4*(i))+7)] = ff(ss[4]); \ -} - -#define kd4(k,i) \ -{ ss[4] = ls_box(ss[(i+3) % 4], 3) ^ t_use(r,c)[i]; \ - ss[i % 4] ^= ss[4]; ss[4] = ff(ss[4]); \ - k[v(40,(4*(i))+4)] = ss[4] ^= k[v(40,(4*(i)))]; \ - k[v(40,(4*(i))+5)] = ss[4] ^= k[v(40,(4*(i))+1)]; \ - k[v(40,(4*(i))+6)] = ss[4] ^= k[v(40,(4*(i))+2)]; \ - k[v(40,(4*(i))+7)] = ss[4] ^= k[v(40,(4*(i))+3)]; \ -} - -#define kdl4(k,i) \ -{ ss[4] = ls_box(ss[(i+3) % 4], 3) ^ t_use(r,c)[i]; ss[i % 4] ^= ss[4]; \ - k[v(40,(4*(i))+4)] = (ss[0] ^= ss[1]) ^ ss[2] ^ ss[3]; \ - k[v(40,(4*(i))+5)] = ss[1] ^ ss[3]; \ - k[v(40,(4*(i))+6)] = ss[0]; \ - k[v(40,(4*(i))+7)] = ss[1]; \ -} - -#else - -#define kdf4(k,i) \ -{ ss[0] ^= ls_box(ss[3],3) ^ t_use(r,c)[i]; k[v(40,(4*(i))+ 4)] = ff(ss[0]); \ - ss[1] ^= ss[0]; k[v(40,(4*(i))+ 5)] = ff(ss[1]); \ - ss[2] ^= ss[1]; k[v(40,(4*(i))+ 6)] = ff(ss[2]); \ - ss[3] ^= ss[2]; k[v(40,(4*(i))+ 7)] = ff(ss[3]); \ -} - -#define kd4(k,i) \ -{ ss[4] = ls_box(ss[3],3) ^ t_use(r,c)[i]; \ - ss[0] ^= ss[4]; ss[4] = ff(ss[4]); k[v(40,(4*(i))+ 4)] = ss[4] ^= k[v(40,(4*(i)))]; \ - ss[1] ^= ss[0]; k[v(40,(4*(i))+ 5)] = ss[4] ^= k[v(40,(4*(i))+ 1)]; \ - ss[2] ^= ss[1]; k[v(40,(4*(i))+ 6)] = ss[4] ^= k[v(40,(4*(i))+ 2)]; \ - ss[3] ^= ss[2]; k[v(40,(4*(i))+ 7)] = ss[4] ^= k[v(40,(4*(i))+ 3)]; \ -} - -#define kdl4(k,i) \ -{ ss[0] ^= ls_box(ss[3],3) ^ t_use(r,c)[i]; k[v(40,(4*(i))+ 4)] = ss[0]; \ - ss[1] ^= ss[0]; k[v(40,(4*(i))+ 5)] = ss[1]; \ - ss[2] ^= ss[1]; k[v(40,(4*(i))+ 6)] = ss[2]; \ - ss[3] ^= ss[2]; k[v(40,(4*(i))+ 7)] = ss[3]; \ -} - -#endif - -AES_RETURN aes_decrypt_key128(const unsigned char *key, aes_decrypt_ctx cx[1]) -{ uint_32t ss[5]; -#if defined( d_vars ) - d_vars; -#endif - cx->ks[v(40,(0))] = ss[0] = word_in(key, 0); - cx->ks[v(40,(1))] = ss[1] = word_in(key, 1); - cx->ks[v(40,(2))] = ss[2] = word_in(key, 2); - cx->ks[v(40,(3))] = ss[3] = word_in(key, 3); - -#ifdef DEC_KS_UNROLL - kdf4(cx->ks, 0); kd4(cx->ks, 1); - kd4(cx->ks, 2); kd4(cx->ks, 3); - kd4(cx->ks, 4); kd4(cx->ks, 5); - kd4(cx->ks, 6); kd4(cx->ks, 7); - kd4(cx->ks, 8); kdl4(cx->ks, 9); -#else - { uint_32t i; - for(i = 0; i < 10; ++i) - k4e(cx->ks, i); -#if !(DEC_ROUND == NO_TABLES) - for(i = N_COLS; i < 10 * N_COLS; ++i) - cx->ks[i] = inv_mcol(cx->ks[i]); -#endif - } -#endif - cx->inf.l = 0; - cx->inf.b[0] = 10 * 16; - -#ifdef USE_VIA_ACE_IF_PRESENT - if(VIA_ACE_AVAILABLE) - cx->inf.b[1] = 0xff; -#endif - return EXIT_SUCCESS; -} - -#endif - -#if defined(AES_192) || defined( AES_VAR ) - -#define k6ef(k,i) \ -{ k[v(48,(6*(i))+ 6)] = ss[0] ^= ls_box(ss[5],3) ^ t_use(r,c)[i]; \ - k[v(48,(6*(i))+ 7)] = ss[1] ^= ss[0]; \ - k[v(48,(6*(i))+ 8)] = ss[2] ^= ss[1]; \ - k[v(48,(6*(i))+ 9)] = ss[3] ^= ss[2]; \ -} - -#define k6e(k,i) \ -{ k6ef(k,i); \ - k[v(48,(6*(i))+10)] = ss[4] ^= ss[3]; \ - k[v(48,(6*(i))+11)] = ss[5] ^= ss[4]; \ -} - -#define kdf6(k,i) \ -{ ss[0] ^= ls_box(ss[5],3) ^ t_use(r,c)[i]; k[v(48,(6*(i))+ 6)] = ff(ss[0]); \ - ss[1] ^= ss[0]; k[v(48,(6*(i))+ 7)] = ff(ss[1]); \ - ss[2] ^= ss[1]; k[v(48,(6*(i))+ 8)] = ff(ss[2]); \ - ss[3] ^= ss[2]; k[v(48,(6*(i))+ 9)] = ff(ss[3]); \ - ss[4] ^= ss[3]; k[v(48,(6*(i))+10)] = ff(ss[4]); \ - ss[5] ^= ss[4]; k[v(48,(6*(i))+11)] = ff(ss[5]); \ -} - -#define kd6(k,i) \ -{ ss[6] = ls_box(ss[5],3) ^ t_use(r,c)[i]; \ - ss[0] ^= ss[6]; ss[6] = ff(ss[6]); k[v(48,(6*(i))+ 6)] = ss[6] ^= k[v(48,(6*(i)))]; \ - ss[1] ^= ss[0]; k[v(48,(6*(i))+ 7)] = ss[6] ^= k[v(48,(6*(i))+ 1)]; \ - ss[2] ^= ss[1]; k[v(48,(6*(i))+ 8)] = ss[6] ^= k[v(48,(6*(i))+ 2)]; \ - ss[3] ^= ss[2]; k[v(48,(6*(i))+ 9)] = ss[6] ^= k[v(48,(6*(i))+ 3)]; \ - ss[4] ^= ss[3]; k[v(48,(6*(i))+10)] = ss[6] ^= k[v(48,(6*(i))+ 4)]; \ - ss[5] ^= ss[4]; k[v(48,(6*(i))+11)] = ss[6] ^= k[v(48,(6*(i))+ 5)]; \ -} - -#define kdl6(k,i) \ -{ ss[0] ^= ls_box(ss[5],3) ^ t_use(r,c)[i]; k[v(48,(6*(i))+ 6)] = ss[0]; \ - ss[1] ^= ss[0]; k[v(48,(6*(i))+ 7)] = ss[1]; \ - ss[2] ^= ss[1]; k[v(48,(6*(i))+ 8)] = ss[2]; \ - ss[3] ^= ss[2]; k[v(48,(6*(i))+ 9)] = ss[3]; \ -} - -AES_RETURN aes_decrypt_key192(const unsigned char *key, aes_decrypt_ctx cx[1]) -{ uint_32t ss[7]; -#if defined( d_vars ) - d_vars; -#endif - cx->ks[v(48,(0))] = ss[0] = word_in(key, 0); - cx->ks[v(48,(1))] = ss[1] = word_in(key, 1); - cx->ks[v(48,(2))] = ss[2] = word_in(key, 2); - cx->ks[v(48,(3))] = ss[3] = word_in(key, 3); - -#ifdef DEC_KS_UNROLL - cx->ks[v(48,(4))] = ff(ss[4] = word_in(key, 4)); - cx->ks[v(48,(5))] = ff(ss[5] = word_in(key, 5)); - kdf6(cx->ks, 0); kd6(cx->ks, 1); - kd6(cx->ks, 2); kd6(cx->ks, 3); - kd6(cx->ks, 4); kd6(cx->ks, 5); - kd6(cx->ks, 6); kdl6(cx->ks, 7); -#else - cx->ks[v(48,(4))] = ss[4] = word_in(key, 4); - cx->ks[v(48,(5))] = ss[5] = word_in(key, 5); - { uint_32t i; - - for(i = 0; i < 7; ++i) - k6e(cx->ks, i); - k6ef(cx->ks, 7); -#if !(DEC_ROUND == NO_TABLES) - for(i = N_COLS; i < 12 * N_COLS; ++i) - cx->ks[i] = inv_mcol(cx->ks[i]); -#endif - } -#endif - cx->inf.l = 0; - cx->inf.b[0] = 12 * 16; - -#ifdef USE_VIA_ACE_IF_PRESENT - if(VIA_ACE_AVAILABLE) - cx->inf.b[1] = 0xff; -#endif - return EXIT_SUCCESS; -} - -#endif - -#if defined(AES_256) || defined( AES_VAR ) - -#define k8ef(k,i) \ -{ k[v(56,(8*(i))+ 8)] = ss[0] ^= ls_box(ss[7],3) ^ t_use(r,c)[i]; \ - k[v(56,(8*(i))+ 9)] = ss[1] ^= ss[0]; \ - k[v(56,(8*(i))+10)] = ss[2] ^= ss[1]; \ - k[v(56,(8*(i))+11)] = ss[3] ^= ss[2]; \ -} - -#define k8e(k,i) \ -{ k8ef(k,i); \ - k[v(56,(8*(i))+12)] = ss[4] ^= ls_box(ss[3],0); \ - k[v(56,(8*(i))+13)] = ss[5] ^= ss[4]; \ - k[v(56,(8*(i))+14)] = ss[6] ^= ss[5]; \ - k[v(56,(8*(i))+15)] = ss[7] ^= ss[6]; \ -} - -#define kdf8(k,i) \ -{ ss[0] ^= ls_box(ss[7],3) ^ t_use(r,c)[i]; k[v(56,(8*(i))+ 8)] = ff(ss[0]); \ - ss[1] ^= ss[0]; k[v(56,(8*(i))+ 9)] = ff(ss[1]); \ - ss[2] ^= ss[1]; k[v(56,(8*(i))+10)] = ff(ss[2]); \ - ss[3] ^= ss[2]; k[v(56,(8*(i))+11)] = ff(ss[3]); \ - ss[4] ^= ls_box(ss[3],0); k[v(56,(8*(i))+12)] = ff(ss[4]); \ - ss[5] ^= ss[4]; k[v(56,(8*(i))+13)] = ff(ss[5]); \ - ss[6] ^= ss[5]; k[v(56,(8*(i))+14)] = ff(ss[6]); \ - ss[7] ^= ss[6]; k[v(56,(8*(i))+15)] = ff(ss[7]); \ -} - -#define kd8(k,i) \ -{ ss[8] = ls_box(ss[7],3) ^ t_use(r,c)[i]; \ - ss[0] ^= ss[8]; ss[8] = ff(ss[8]); k[v(56,(8*(i))+ 8)] = ss[8] ^= k[v(56,(8*(i)))]; \ - ss[1] ^= ss[0]; k[v(56,(8*(i))+ 9)] = ss[8] ^= k[v(56,(8*(i))+ 1)]; \ - ss[2] ^= ss[1]; k[v(56,(8*(i))+10)] = ss[8] ^= k[v(56,(8*(i))+ 2)]; \ - ss[3] ^= ss[2]; k[v(56,(8*(i))+11)] = ss[8] ^= k[v(56,(8*(i))+ 3)]; \ - ss[8] = ls_box(ss[3],0); \ - ss[4] ^= ss[8]; ss[8] = ff(ss[8]); k[v(56,(8*(i))+12)] = ss[8] ^= k[v(56,(8*(i))+ 4)]; \ - ss[5] ^= ss[4]; k[v(56,(8*(i))+13)] = ss[8] ^= k[v(56,(8*(i))+ 5)]; \ - ss[6] ^= ss[5]; k[v(56,(8*(i))+14)] = ss[8] ^= k[v(56,(8*(i))+ 6)]; \ - ss[7] ^= ss[6]; k[v(56,(8*(i))+15)] = ss[8] ^= k[v(56,(8*(i))+ 7)]; \ -} - -#define kdl8(k,i) \ -{ ss[0] ^= ls_box(ss[7],3) ^ t_use(r,c)[i]; k[v(56,(8*(i))+ 8)] = ss[0]; \ - ss[1] ^= ss[0]; k[v(56,(8*(i))+ 9)] = ss[1]; \ - ss[2] ^= ss[1]; k[v(56,(8*(i))+10)] = ss[2]; \ - ss[3] ^= ss[2]; k[v(56,(8*(i))+11)] = ss[3]; \ -} - -AES_RETURN aes_decrypt_key256(const unsigned char *key, aes_decrypt_ctx cx[1]) -{ uint_32t ss[9]; -#if defined( d_vars ) - d_vars; -#endif - cx->ks[v(56,(0))] = ss[0] = word_in(key, 0); - cx->ks[v(56,(1))] = ss[1] = word_in(key, 1); - cx->ks[v(56,(2))] = ss[2] = word_in(key, 2); - cx->ks[v(56,(3))] = ss[3] = word_in(key, 3); - -#ifdef DEC_KS_UNROLL - cx->ks[v(56,(4))] = ff(ss[4] = word_in(key, 4)); - cx->ks[v(56,(5))] = ff(ss[5] = word_in(key, 5)); - cx->ks[v(56,(6))] = ff(ss[6] = word_in(key, 6)); - cx->ks[v(56,(7))] = ff(ss[7] = word_in(key, 7)); - kdf8(cx->ks, 0); kd8(cx->ks, 1); - kd8(cx->ks, 2); kd8(cx->ks, 3); - kd8(cx->ks, 4); kd8(cx->ks, 5); - kdl8(cx->ks, 6); -#else - cx->ks[v(56,(4))] = ss[4] = word_in(key, 4); - cx->ks[v(56,(5))] = ss[5] = word_in(key, 5); - cx->ks[v(56,(6))] = ss[6] = word_in(key, 6); - cx->ks[v(56,(7))] = ss[7] = word_in(key, 7); - { uint_32t i; - - for(i = 0; i < 6; ++i) - k8e(cx->ks, i); - k8ef(cx->ks, 6); -#if !(DEC_ROUND == NO_TABLES) - for(i = N_COLS; i < 14 * N_COLS; ++i) - cx->ks[i] = inv_mcol(cx->ks[i]); -#endif - } -#endif - cx->inf.l = 0; - cx->inf.b[0] = 14 * 16; - -#ifdef USE_VIA_ACE_IF_PRESENT - if(VIA_ACE_AVAILABLE) - cx->inf.b[1] = 0xff; -#endif - return EXIT_SUCCESS; -} - -#endif - -#if defined( AES_VAR ) - -AES_RETURN aes_decrypt_key(const unsigned char *key, int key_len, aes_decrypt_ctx cx[1]) -{ - switch(key_len) - { - case 16: case 128: return aes_decrypt_key128(key, cx); - case 24: case 192: return aes_decrypt_key192(key, cx); - case 32: case 256: return aes_decrypt_key256(key, cx); - default: return EXIT_FAILURE; - } -} - -#endif - -#endif - -#if defined(__cplusplus) -} -#endif +/* +--------------------------------------------------------------------------- +Copyright (c) 1998-2010, Brian Gladman, Worcester, UK. All rights reserved. + +The redistribution and use of this software (with or without changes) +is allowed without the payment of fees or royalties provided that: + + source code distributions include the above copyright notice, this + list of conditions and the following disclaimer; + + binary distributions include the above copyright notice, this list + of conditions and the following disclaimer in their documentation. + +This software is provided 'as is' with no explicit or implied warranties +in respect of its operation, including, but not limited to, correctness +and fitness for purpose. +--------------------------------------------------------------------------- +Issue Date: 20/12/2007 +*/ + +#include "aesopt.h" +#include "aestab.h" + +#ifdef USE_VIA_ACE_IF_PRESENT +# include "aes_via_ace.h" +#endif + +#if defined(__cplusplus) +extern "C" +{ +#endif + +/* Initialise the key schedule from the user supplied key. The key + length can be specified in bytes, with legal values of 16, 24 + and 32, or in bits, with legal values of 128, 192 and 256. These + values correspond with Nk values of 4, 6 and 8 respectively. + + The following macros implement a single cycle in the key + schedule generation process. The number of cycles needed + for each cx->n_col and nk value is: + + nk = 4 5 6 7 8 + ------------------------------ + cx->n_col = 4 10 9 8 7 7 + cx->n_col = 5 14 11 10 9 9 + cx->n_col = 6 19 15 12 11 11 + cx->n_col = 7 21 19 16 13 14 + cx->n_col = 8 29 23 19 17 14 +*/ + +#if defined( REDUCE_CODE_SIZE ) +# define ls_box ls_sub + uint_32t ls_sub(const uint_32t t, const uint_32t n); +# define inv_mcol im_sub + uint_32t im_sub(const uint_32t x); +# ifdef ENC_KS_UNROLL +# undef ENC_KS_UNROLL +# endif +# ifdef DEC_KS_UNROLL +# undef DEC_KS_UNROLL +# endif +#endif + +#if (FUNCS_IN_C & ENC_KEYING_IN_C) + +#if defined(AES_128) || defined( AES_VAR ) + +#define ke4(k,i) \ +{ k[4*(i)+4] = ss[0] ^= ls_box(ss[3],3) ^ t_use(r,c)[i]; \ + k[4*(i)+5] = ss[1] ^= ss[0]; \ + k[4*(i)+6] = ss[2] ^= ss[1]; \ + k[4*(i)+7] = ss[3] ^= ss[2]; \ +} + +AES_RETURN aes_encrypt_key128(const unsigned char *key, aes_encrypt_ctx cx[1]) +{ uint_32t ss[4]; + + cx->ks[0] = ss[0] = word_in(key, 0); + cx->ks[1] = ss[1] = word_in(key, 1); + cx->ks[2] = ss[2] = word_in(key, 2); + cx->ks[3] = ss[3] = word_in(key, 3); + +#ifdef ENC_KS_UNROLL + ke4(cx->ks, 0); ke4(cx->ks, 1); + ke4(cx->ks, 2); ke4(cx->ks, 3); + ke4(cx->ks, 4); ke4(cx->ks, 5); + ke4(cx->ks, 6); ke4(cx->ks, 7); + ke4(cx->ks, 8); +#else + { uint_32t i; + for(i = 0; i < 9; ++i) + ke4(cx->ks, i); + } +#endif + ke4(cx->ks, 9); + cx->inf.l = 0; + cx->inf.b[0] = 10 * 16; + +#ifdef USE_VIA_ACE_IF_PRESENT + if(VIA_ACE_AVAILABLE) + cx->inf.b[1] = 0xff; +#endif + return EXIT_SUCCESS; +} + +#endif + +#if defined(AES_192) || defined( AES_VAR ) + +#define kef6(k,i) \ +{ k[6*(i)+ 6] = ss[0] ^= ls_box(ss[5],3) ^ t_use(r,c)[i]; \ + k[6*(i)+ 7] = ss[1] ^= ss[0]; \ + k[6*(i)+ 8] = ss[2] ^= ss[1]; \ + k[6*(i)+ 9] = ss[3] ^= ss[2]; \ +} + +#define ke6(k,i) \ +{ kef6(k,i); \ + k[6*(i)+10] = ss[4] ^= ss[3]; \ + k[6*(i)+11] = ss[5] ^= ss[4]; \ +} + +AES_RETURN aes_encrypt_key192(const unsigned char *key, aes_encrypt_ctx cx[1]) +{ uint_32t ss[6]; + + cx->ks[0] = ss[0] = word_in(key, 0); + cx->ks[1] = ss[1] = word_in(key, 1); + cx->ks[2] = ss[2] = word_in(key, 2); + cx->ks[3] = ss[3] = word_in(key, 3); + cx->ks[4] = ss[4] = word_in(key, 4); + cx->ks[5] = ss[5] = word_in(key, 5); + +#ifdef ENC_KS_UNROLL + ke6(cx->ks, 0); ke6(cx->ks, 1); + ke6(cx->ks, 2); ke6(cx->ks, 3); + ke6(cx->ks, 4); ke6(cx->ks, 5); + ke6(cx->ks, 6); +#else + { uint_32t i; + for(i = 0; i < 7; ++i) + ke6(cx->ks, i); + } +#endif + kef6(cx->ks, 7); + cx->inf.l = 0; + cx->inf.b[0] = 12 * 16; + +#ifdef USE_VIA_ACE_IF_PRESENT + if(VIA_ACE_AVAILABLE) + cx->inf.b[1] = 0xff; +#endif + return EXIT_SUCCESS; +} + +#endif + +#if defined(AES_256) || defined( AES_VAR ) + +#define kef8(k,i) \ +{ k[8*(i)+ 8] = ss[0] ^= ls_box(ss[7],3) ^ t_use(r,c)[i]; \ + k[8*(i)+ 9] = ss[1] ^= ss[0]; \ + k[8*(i)+10] = ss[2] ^= ss[1]; \ + k[8*(i)+11] = ss[3] ^= ss[2]; \ +} + +#define ke8(k,i) \ +{ kef8(k,i); \ + k[8*(i)+12] = ss[4] ^= ls_box(ss[3],0); \ + k[8*(i)+13] = ss[5] ^= ss[4]; \ + k[8*(i)+14] = ss[6] ^= ss[5]; \ + k[8*(i)+15] = ss[7] ^= ss[6]; \ +} + +AES_RETURN aes_encrypt_key256(const unsigned char *key, aes_encrypt_ctx cx[1]) +{ uint_32t ss[8]; + + cx->ks[0] = ss[0] = word_in(key, 0); + cx->ks[1] = ss[1] = word_in(key, 1); + cx->ks[2] = ss[2] = word_in(key, 2); + cx->ks[3] = ss[3] = word_in(key, 3); + cx->ks[4] = ss[4] = word_in(key, 4); + cx->ks[5] = ss[5] = word_in(key, 5); + cx->ks[6] = ss[6] = word_in(key, 6); + cx->ks[7] = ss[7] = word_in(key, 7); + +#ifdef ENC_KS_UNROLL + ke8(cx->ks, 0); ke8(cx->ks, 1); + ke8(cx->ks, 2); ke8(cx->ks, 3); + ke8(cx->ks, 4); ke8(cx->ks, 5); +#else + { uint_32t i; + for(i = 0; i < 6; ++i) + ke8(cx->ks, i); + } +#endif + kef8(cx->ks, 6); + cx->inf.l = 0; + cx->inf.b[0] = 14 * 16; + +#ifdef USE_VIA_ACE_IF_PRESENT + if(VIA_ACE_AVAILABLE) + cx->inf.b[1] = 0xff; +#endif + return EXIT_SUCCESS; +} + +#endif + +#if defined( AES_VAR ) + +AES_RETURN aes_encrypt_key(const unsigned char *key, int key_len, aes_encrypt_ctx cx[1]) +{ + switch(key_len) + { + case 16: case 128: return aes_encrypt_key128(key, cx); + case 24: case 192: return aes_encrypt_key192(key, cx); + case 32: case 256: return aes_encrypt_key256(key, cx); + default: return EXIT_FAILURE; + } +} + +#endif + +#endif + +#if (FUNCS_IN_C & DEC_KEYING_IN_C) + +/* this is used to store the decryption round keys */ +/* in forward or reverse order */ + +#ifdef AES_REV_DKS +#define v(n,i) ((n) - (i) + 2 * ((i) & 3)) +#else +#define v(n,i) (i) +#endif + +#if DEC_ROUND == NO_TABLES +#define ff(x) (x) +#else +#define ff(x) inv_mcol(x) +#if defined( dec_imvars ) +#define d_vars dec_imvars +#endif +#endif + +#if defined(AES_128) || defined( AES_VAR ) + +#define k4e(k,i) \ +{ k[v(40,(4*(i))+4)] = ss[0] ^= ls_box(ss[3],3) ^ t_use(r,c)[i]; \ + k[v(40,(4*(i))+5)] = ss[1] ^= ss[0]; \ + k[v(40,(4*(i))+6)] = ss[2] ^= ss[1]; \ + k[v(40,(4*(i))+7)] = ss[3] ^= ss[2]; \ +} + +#if 1 + +#define kdf4(k,i) \ +{ ss[0] = ss[0] ^ ss[2] ^ ss[1] ^ ss[3]; \ + ss[1] = ss[1] ^ ss[3]; \ + ss[2] = ss[2] ^ ss[3]; \ + ss[4] = ls_box(ss[(i+3) % 4], 3) ^ t_use(r,c)[i]; \ + ss[i % 4] ^= ss[4]; \ + ss[4] ^= k[v(40,(4*(i)))]; k[v(40,(4*(i))+4)] = ff(ss[4]); \ + ss[4] ^= k[v(40,(4*(i))+1)]; k[v(40,(4*(i))+5)] = ff(ss[4]); \ + ss[4] ^= k[v(40,(4*(i))+2)]; k[v(40,(4*(i))+6)] = ff(ss[4]); \ + ss[4] ^= k[v(40,(4*(i))+3)]; k[v(40,(4*(i))+7)] = ff(ss[4]); \ +} + +#define kd4(k,i) \ +{ ss[4] = ls_box(ss[(i+3) % 4], 3) ^ t_use(r,c)[i]; \ + ss[i % 4] ^= ss[4]; ss[4] = ff(ss[4]); \ + k[v(40,(4*(i))+4)] = ss[4] ^= k[v(40,(4*(i)))]; \ + k[v(40,(4*(i))+5)] = ss[4] ^= k[v(40,(4*(i))+1)]; \ + k[v(40,(4*(i))+6)] = ss[4] ^= k[v(40,(4*(i))+2)]; \ + k[v(40,(4*(i))+7)] = ss[4] ^= k[v(40,(4*(i))+3)]; \ +} + +#define kdl4(k,i) \ +{ ss[4] = ls_box(ss[(i+3) % 4], 3) ^ t_use(r,c)[i]; ss[i % 4] ^= ss[4]; \ + k[v(40,(4*(i))+4)] = (ss[0] ^= ss[1]) ^ ss[2] ^ ss[3]; \ + k[v(40,(4*(i))+5)] = ss[1] ^ ss[3]; \ + k[v(40,(4*(i))+6)] = ss[0]; \ + k[v(40,(4*(i))+7)] = ss[1]; \ +} + +#else + +#define kdf4(k,i) \ +{ ss[0] ^= ls_box(ss[3],3) ^ t_use(r,c)[i]; k[v(40,(4*(i))+ 4)] = ff(ss[0]); \ + ss[1] ^= ss[0]; k[v(40,(4*(i))+ 5)] = ff(ss[1]); \ + ss[2] ^= ss[1]; k[v(40,(4*(i))+ 6)] = ff(ss[2]); \ + ss[3] ^= ss[2]; k[v(40,(4*(i))+ 7)] = ff(ss[3]); \ +} + +#define kd4(k,i) \ +{ ss[4] = ls_box(ss[3],3) ^ t_use(r,c)[i]; \ + ss[0] ^= ss[4]; ss[4] = ff(ss[4]); k[v(40,(4*(i))+ 4)] = ss[4] ^= k[v(40,(4*(i)))]; \ + ss[1] ^= ss[0]; k[v(40,(4*(i))+ 5)] = ss[4] ^= k[v(40,(4*(i))+ 1)]; \ + ss[2] ^= ss[1]; k[v(40,(4*(i))+ 6)] = ss[4] ^= k[v(40,(4*(i))+ 2)]; \ + ss[3] ^= ss[2]; k[v(40,(4*(i))+ 7)] = ss[4] ^= k[v(40,(4*(i))+ 3)]; \ +} + +#define kdl4(k,i) \ +{ ss[0] ^= ls_box(ss[3],3) ^ t_use(r,c)[i]; k[v(40,(4*(i))+ 4)] = ss[0]; \ + ss[1] ^= ss[0]; k[v(40,(4*(i))+ 5)] = ss[1]; \ + ss[2] ^= ss[1]; k[v(40,(4*(i))+ 6)] = ss[2]; \ + ss[3] ^= ss[2]; k[v(40,(4*(i))+ 7)] = ss[3]; \ +} + +#endif + +AES_RETURN aes_decrypt_key128(const unsigned char *key, aes_decrypt_ctx cx[1]) +{ uint_32t ss[5]; +#if defined( d_vars ) + d_vars; +#endif + cx->ks[v(40,(0))] = ss[0] = word_in(key, 0); + cx->ks[v(40,(1))] = ss[1] = word_in(key, 1); + cx->ks[v(40,(2))] = ss[2] = word_in(key, 2); + cx->ks[v(40,(3))] = ss[3] = word_in(key, 3); + +#ifdef DEC_KS_UNROLL + kdf4(cx->ks, 0); kd4(cx->ks, 1); + kd4(cx->ks, 2); kd4(cx->ks, 3); + kd4(cx->ks, 4); kd4(cx->ks, 5); + kd4(cx->ks, 6); kd4(cx->ks, 7); + kd4(cx->ks, 8); kdl4(cx->ks, 9); +#else + { uint_32t i; + for(i = 0; i < 10; ++i) + k4e(cx->ks, i); +#if !(DEC_ROUND == NO_TABLES) + for(i = N_COLS; i < 10 * N_COLS; ++i) + cx->ks[i] = inv_mcol(cx->ks[i]); +#endif + } +#endif + cx->inf.l = 0; + cx->inf.b[0] = 10 * 16; + +#ifdef USE_VIA_ACE_IF_PRESENT + if(VIA_ACE_AVAILABLE) + cx->inf.b[1] = 0xff; +#endif + return EXIT_SUCCESS; +} + +#endif + +#if defined(AES_192) || defined( AES_VAR ) + +#define k6ef(k,i) \ +{ k[v(48,(6*(i))+ 6)] = ss[0] ^= ls_box(ss[5],3) ^ t_use(r,c)[i]; \ + k[v(48,(6*(i))+ 7)] = ss[1] ^= ss[0]; \ + k[v(48,(6*(i))+ 8)] = ss[2] ^= ss[1]; \ + k[v(48,(6*(i))+ 9)] = ss[3] ^= ss[2]; \ +} + +#define k6e(k,i) \ +{ k6ef(k,i); \ + k[v(48,(6*(i))+10)] = ss[4] ^= ss[3]; \ + k[v(48,(6*(i))+11)] = ss[5] ^= ss[4]; \ +} + +#define kdf6(k,i) \ +{ ss[0] ^= ls_box(ss[5],3) ^ t_use(r,c)[i]; k[v(48,(6*(i))+ 6)] = ff(ss[0]); \ + ss[1] ^= ss[0]; k[v(48,(6*(i))+ 7)] = ff(ss[1]); \ + ss[2] ^= ss[1]; k[v(48,(6*(i))+ 8)] = ff(ss[2]); \ + ss[3] ^= ss[2]; k[v(48,(6*(i))+ 9)] = ff(ss[3]); \ + ss[4] ^= ss[3]; k[v(48,(6*(i))+10)] = ff(ss[4]); \ + ss[5] ^= ss[4]; k[v(48,(6*(i))+11)] = ff(ss[5]); \ +} + +#define kd6(k,i) \ +{ ss[6] = ls_box(ss[5],3) ^ t_use(r,c)[i]; \ + ss[0] ^= ss[6]; ss[6] = ff(ss[6]); k[v(48,(6*(i))+ 6)] = ss[6] ^= k[v(48,(6*(i)))]; \ + ss[1] ^= ss[0]; k[v(48,(6*(i))+ 7)] = ss[6] ^= k[v(48,(6*(i))+ 1)]; \ + ss[2] ^= ss[1]; k[v(48,(6*(i))+ 8)] = ss[6] ^= k[v(48,(6*(i))+ 2)]; \ + ss[3] ^= ss[2]; k[v(48,(6*(i))+ 9)] = ss[6] ^= k[v(48,(6*(i))+ 3)]; \ + ss[4] ^= ss[3]; k[v(48,(6*(i))+10)] = ss[6] ^= k[v(48,(6*(i))+ 4)]; \ + ss[5] ^= ss[4]; k[v(48,(6*(i))+11)] = ss[6] ^= k[v(48,(6*(i))+ 5)]; \ +} + +#define kdl6(k,i) \ +{ ss[0] ^= ls_box(ss[5],3) ^ t_use(r,c)[i]; k[v(48,(6*(i))+ 6)] = ss[0]; \ + ss[1] ^= ss[0]; k[v(48,(6*(i))+ 7)] = ss[1]; \ + ss[2] ^= ss[1]; k[v(48,(6*(i))+ 8)] = ss[2]; \ + ss[3] ^= ss[2]; k[v(48,(6*(i))+ 9)] = ss[3]; \ +} + +AES_RETURN aes_decrypt_key192(const unsigned char *key, aes_decrypt_ctx cx[1]) +{ uint_32t ss[7]; +#if defined( d_vars ) + d_vars; +#endif + cx->ks[v(48,(0))] = ss[0] = word_in(key, 0); + cx->ks[v(48,(1))] = ss[1] = word_in(key, 1); + cx->ks[v(48,(2))] = ss[2] = word_in(key, 2); + cx->ks[v(48,(3))] = ss[3] = word_in(key, 3); + +#ifdef DEC_KS_UNROLL + cx->ks[v(48,(4))] = ff(ss[4] = word_in(key, 4)); + cx->ks[v(48,(5))] = ff(ss[5] = word_in(key, 5)); + kdf6(cx->ks, 0); kd6(cx->ks, 1); + kd6(cx->ks, 2); kd6(cx->ks, 3); + kd6(cx->ks, 4); kd6(cx->ks, 5); + kd6(cx->ks, 6); kdl6(cx->ks, 7); +#else + cx->ks[v(48,(4))] = ss[4] = word_in(key, 4); + cx->ks[v(48,(5))] = ss[5] = word_in(key, 5); + { uint_32t i; + + for(i = 0; i < 7; ++i) + k6e(cx->ks, i); + k6ef(cx->ks, 7); +#if !(DEC_ROUND == NO_TABLES) + for(i = N_COLS; i < 12 * N_COLS; ++i) + cx->ks[i] = inv_mcol(cx->ks[i]); +#endif + } +#endif + cx->inf.l = 0; + cx->inf.b[0] = 12 * 16; + +#ifdef USE_VIA_ACE_IF_PRESENT + if(VIA_ACE_AVAILABLE) + cx->inf.b[1] = 0xff; +#endif + return EXIT_SUCCESS; +} + +#endif + +#if defined(AES_256) || defined( AES_VAR ) + +#define k8ef(k,i) \ +{ k[v(56,(8*(i))+ 8)] = ss[0] ^= ls_box(ss[7],3) ^ t_use(r,c)[i]; \ + k[v(56,(8*(i))+ 9)] = ss[1] ^= ss[0]; \ + k[v(56,(8*(i))+10)] = ss[2] ^= ss[1]; \ + k[v(56,(8*(i))+11)] = ss[3] ^= ss[2]; \ +} + +#define k8e(k,i) \ +{ k8ef(k,i); \ + k[v(56,(8*(i))+12)] = ss[4] ^= ls_box(ss[3],0); \ + k[v(56,(8*(i))+13)] = ss[5] ^= ss[4]; \ + k[v(56,(8*(i))+14)] = ss[6] ^= ss[5]; \ + k[v(56,(8*(i))+15)] = ss[7] ^= ss[6]; \ +} + +#define kdf8(k,i) \ +{ ss[0] ^= ls_box(ss[7],3) ^ t_use(r,c)[i]; k[v(56,(8*(i))+ 8)] = ff(ss[0]); \ + ss[1] ^= ss[0]; k[v(56,(8*(i))+ 9)] = ff(ss[1]); \ + ss[2] ^= ss[1]; k[v(56,(8*(i))+10)] = ff(ss[2]); \ + ss[3] ^= ss[2]; k[v(56,(8*(i))+11)] = ff(ss[3]); \ + ss[4] ^= ls_box(ss[3],0); k[v(56,(8*(i))+12)] = ff(ss[4]); \ + ss[5] ^= ss[4]; k[v(56,(8*(i))+13)] = ff(ss[5]); \ + ss[6] ^= ss[5]; k[v(56,(8*(i))+14)] = ff(ss[6]); \ + ss[7] ^= ss[6]; k[v(56,(8*(i))+15)] = ff(ss[7]); \ +} + +#define kd8(k,i) \ +{ ss[8] = ls_box(ss[7],3) ^ t_use(r,c)[i]; \ + ss[0] ^= ss[8]; ss[8] = ff(ss[8]); k[v(56,(8*(i))+ 8)] = ss[8] ^= k[v(56,(8*(i)))]; \ + ss[1] ^= ss[0]; k[v(56,(8*(i))+ 9)] = ss[8] ^= k[v(56,(8*(i))+ 1)]; \ + ss[2] ^= ss[1]; k[v(56,(8*(i))+10)] = ss[8] ^= k[v(56,(8*(i))+ 2)]; \ + ss[3] ^= ss[2]; k[v(56,(8*(i))+11)] = ss[8] ^= k[v(56,(8*(i))+ 3)]; \ + ss[8] = ls_box(ss[3],0); \ + ss[4] ^= ss[8]; ss[8] = ff(ss[8]); k[v(56,(8*(i))+12)] = ss[8] ^= k[v(56,(8*(i))+ 4)]; \ + ss[5] ^= ss[4]; k[v(56,(8*(i))+13)] = ss[8] ^= k[v(56,(8*(i))+ 5)]; \ + ss[6] ^= ss[5]; k[v(56,(8*(i))+14)] = ss[8] ^= k[v(56,(8*(i))+ 6)]; \ + ss[7] ^= ss[6]; k[v(56,(8*(i))+15)] = ss[8] ^= k[v(56,(8*(i))+ 7)]; \ +} + +#define kdl8(k,i) \ +{ ss[0] ^= ls_box(ss[7],3) ^ t_use(r,c)[i]; k[v(56,(8*(i))+ 8)] = ss[0]; \ + ss[1] ^= ss[0]; k[v(56,(8*(i))+ 9)] = ss[1]; \ + ss[2] ^= ss[1]; k[v(56,(8*(i))+10)] = ss[2]; \ + ss[3] ^= ss[2]; k[v(56,(8*(i))+11)] = ss[3]; \ +} + +AES_RETURN aes_decrypt_key256(const unsigned char *key, aes_decrypt_ctx cx[1]) +{ uint_32t ss[9]; +#if defined( d_vars ) + d_vars; +#endif + cx->ks[v(56,(0))] = ss[0] = word_in(key, 0); + cx->ks[v(56,(1))] = ss[1] = word_in(key, 1); + cx->ks[v(56,(2))] = ss[2] = word_in(key, 2); + cx->ks[v(56,(3))] = ss[3] = word_in(key, 3); + +#ifdef DEC_KS_UNROLL + cx->ks[v(56,(4))] = ff(ss[4] = word_in(key, 4)); + cx->ks[v(56,(5))] = ff(ss[5] = word_in(key, 5)); + cx->ks[v(56,(6))] = ff(ss[6] = word_in(key, 6)); + cx->ks[v(56,(7))] = ff(ss[7] = word_in(key, 7)); + kdf8(cx->ks, 0); kd8(cx->ks, 1); + kd8(cx->ks, 2); kd8(cx->ks, 3); + kd8(cx->ks, 4); kd8(cx->ks, 5); + kdl8(cx->ks, 6); +#else + cx->ks[v(56,(4))] = ss[4] = word_in(key, 4); + cx->ks[v(56,(5))] = ss[5] = word_in(key, 5); + cx->ks[v(56,(6))] = ss[6] = word_in(key, 6); + cx->ks[v(56,(7))] = ss[7] = word_in(key, 7); + { uint_32t i; + + for(i = 0; i < 6; ++i) + k8e(cx->ks, i); + k8ef(cx->ks, 6); +#if !(DEC_ROUND == NO_TABLES) + for(i = N_COLS; i < 14 * N_COLS; ++i) + cx->ks[i] = inv_mcol(cx->ks[i]); +#endif + } +#endif + cx->inf.l = 0; + cx->inf.b[0] = 14 * 16; + +#ifdef USE_VIA_ACE_IF_PRESENT + if(VIA_ACE_AVAILABLE) + cx->inf.b[1] = 0xff; +#endif + return EXIT_SUCCESS; +} + +#endif + +#if defined( AES_VAR ) + +AES_RETURN aes_decrypt_key(const unsigned char *key, int key_len, aes_decrypt_ctx cx[1]) +{ + switch(key_len) + { + case 16: case 128: return aes_decrypt_key128(key, cx); + case 24: case 192: return aes_decrypt_key192(key, cx); + case 32: case 256: return aes_decrypt_key256(key, cx); + default: return EXIT_FAILURE; + } +} + +#endif + +#endif + +#if defined(__cplusplus) +} +#endif diff --git a/Zip/aes/aesopt.h b/Zip/minizip/aes/aesopt.h similarity index 97% rename from Zip/aes/aesopt.h rename to Zip/minizip/aes/aesopt.h index 8851425e1b876fe8c691c48bfde6c86eb29e2ef8..fd8db2ec883a97db1953527a242f85e37a997015 100755 --- a/Zip/aes/aesopt.h +++ b/Zip/minizip/aes/aesopt.h @@ -1,739 +1,739 @@ -/* ---------------------------------------------------------------------------- -Copyright (c) 1998-2010, Brian Gladman, Worcester, UK. All rights reserved. - -The redistribution and use of this software (with or without changes) -is allowed without the payment of fees or royalties provided that: - - source code distributions include the above copyright notice, this - list of conditions and the following disclaimer; - - binary distributions include the above copyright notice, this list - of conditions and the following disclaimer in their documentation. - -This software is provided 'as is' with no explicit or implied warranties -in respect of its operation, including, but not limited to, correctness -and fitness for purpose. ---------------------------------------------------------------------------- -Issue Date: 20/12/2007 - - This file contains the compilation options for AES (Rijndael) and code - that is common across encryption, key scheduling and table generation. - - OPERATION - - These source code files implement the AES algorithm Rijndael designed by - Joan Daemen and Vincent Rijmen. This version is designed for the standard - block size of 16 bytes and for key sizes of 128, 192 and 256 bits (16, 24 - and 32 bytes). - - This version is designed for flexibility and speed using operations on - 32-bit words rather than operations on bytes. It can be compiled with - either big or little endian internal byte order but is faster when the - native byte order for the processor is used. - - THE CIPHER INTERFACE - - The cipher interface is implemented as an array of bytes in which lower - AES bit sequence indexes map to higher numeric significance within bytes. - - uint_8t (an unsigned 8-bit type) - uint_32t (an unsigned 32-bit type) - struct aes_encrypt_ctx (structure for the cipher encryption context) - struct aes_decrypt_ctx (structure for the cipher decryption context) - AES_RETURN the function return type - - C subroutine calls: - - AES_RETURN aes_encrypt_key128(const unsigned char *key, aes_encrypt_ctx cx[1]); - AES_RETURN aes_encrypt_key192(const unsigned char *key, aes_encrypt_ctx cx[1]); - AES_RETURN aes_encrypt_key256(const unsigned char *key, aes_encrypt_ctx cx[1]); - AES_RETURN aes_encrypt(const unsigned char *in, unsigned char *out, - const aes_encrypt_ctx cx[1]); - - AES_RETURN aes_decrypt_key128(const unsigned char *key, aes_decrypt_ctx cx[1]); - AES_RETURN aes_decrypt_key192(const unsigned char *key, aes_decrypt_ctx cx[1]); - AES_RETURN aes_decrypt_key256(const unsigned char *key, aes_decrypt_ctx cx[1]); - AES_RETURN aes_decrypt(const unsigned char *in, unsigned char *out, - const aes_decrypt_ctx cx[1]); - - IMPORTANT NOTE: If you are using this C interface with dynamic tables make sure that - you call aes_init() before AES is used so that the tables are initialised. - - C++ aes class subroutines: - - Class AESencrypt for encryption - - Construtors: - AESencrypt(void) - AESencrypt(const unsigned char *key) - 128 bit key - Members: - AES_RETURN key128(const unsigned char *key) - AES_RETURN key192(const unsigned char *key) - AES_RETURN key256(const unsigned char *key) - AES_RETURN encrypt(const unsigned char *in, unsigned char *out) const - - Class AESdecrypt for encryption - Construtors: - AESdecrypt(void) - AESdecrypt(const unsigned char *key) - 128 bit key - Members: - AES_RETURN key128(const unsigned char *key) - AES_RETURN key192(const unsigned char *key) - AES_RETURN key256(const unsigned char *key) - AES_RETURN decrypt(const unsigned char *in, unsigned char *out) const -*/ - -#if !defined( _AESOPT_H ) -#define _AESOPT_H - -#if defined( __cplusplus ) -#include "aescpp.h" -#else -#include "aes.h" -#endif - -/* PLATFORM SPECIFIC INCLUDES */ - -#include "brg_endian.h" - -/* CONFIGURATION - THE USE OF DEFINES - - Later in this section there are a number of defines that control the - operation of the code. In each section, the purpose of each define is - explained so that the relevant form can be included or excluded by - setting either 1's or 0's respectively on the branches of the related - #if clauses. The following local defines should not be changed. -*/ - -#define ENCRYPTION_IN_C 1 -#define DECRYPTION_IN_C 2 -#define ENC_KEYING_IN_C 4 -#define DEC_KEYING_IN_C 8 - -#define NO_TABLES 0 -#define ONE_TABLE 1 -#define FOUR_TABLES 4 -#define NONE 0 -#define PARTIAL 1 -#define FULL 2 - -/* --- START OF USER CONFIGURED OPTIONS --- */ - -/* 1. BYTE ORDER WITHIN 32 BIT WORDS - - The fundamental data processing units in Rijndael are 8-bit bytes. The - input, output and key input are all enumerated arrays of bytes in which - bytes are numbered starting at zero and increasing to one less than the - number of bytes in the array in question. This enumeration is only used - for naming bytes and does not imply any adjacency or order relationship - from one byte to another. When these inputs and outputs are considered - as bit sequences, bits 8*n to 8*n+7 of the bit sequence are mapped to - byte[n] with bit 8n+i in the sequence mapped to bit 7-i within the byte. - In this implementation bits are numbered from 0 to 7 starting at the - numerically least significant end of each byte (bit n represents 2^n). - - However, Rijndael can be implemented more efficiently using 32-bit - words by packing bytes into words so that bytes 4*n to 4*n+3 are placed - into word[n]. While in principle these bytes can be assembled into words - in any positions, this implementation only supports the two formats in - which bytes in adjacent positions within words also have adjacent byte - numbers. This order is called big-endian if the lowest numbered bytes - in words have the highest numeric significance and little-endian if the - opposite applies. - - This code can work in either order irrespective of the order used by the - machine on which it runs. Normally the internal byte order will be set - to the order of the processor on which the code is to be run but this - define can be used to reverse this in special situations - - WARNING: Assembler code versions rely on PLATFORM_BYTE_ORDER being set. - This define will hence be redefined later (in section 4) if necessary -*/ - -#if 1 -# define ALGORITHM_BYTE_ORDER PLATFORM_BYTE_ORDER -#elif 0 -# define ALGORITHM_BYTE_ORDER IS_LITTLE_ENDIAN -#elif 0 -# define ALGORITHM_BYTE_ORDER IS_BIG_ENDIAN -#else -# error The algorithm byte order is not defined -#endif - -/* 2. VIA ACE SUPPORT */ - -#if !defined(__APPLE__) && defined( __GNUC__ ) && defined( __i386__ ) \ - || defined( _WIN32 ) && defined( _M_IX86 ) \ - && !(defined( _WIN64 ) || defined( _WIN32_WCE ) || defined( _MSC_VER ) && ( _MSC_VER <= 800 )) -# define VIA_ACE_POSSIBLE -#endif - -/* Define this option if support for the VIA ACE is required. This uses - inline assembler instructions and is only implemented for the Microsoft, - Intel and GCC compilers. If VIA ACE is known to be present, then defining - ASSUME_VIA_ACE_PRESENT will remove the ordinary encryption/decryption - code. If USE_VIA_ACE_IF_PRESENT is defined then VIA ACE will be used if - it is detected (both present and enabled) but the normal AES code will - also be present. - - When VIA ACE is to be used, all AES encryption contexts MUST be 16 byte - aligned; other input/output buffers do not need to be 16 byte aligned - but there are very large performance gains if this can be arranged. - VIA ACE also requires the decryption key schedule to be in reverse - order (which later checks below ensure). -*/ - -#if 1 && defined( VIA_ACE_POSSIBLE ) && !defined( USE_VIA_ACE_IF_PRESENT ) -# define USE_VIA_ACE_IF_PRESENT -#endif - -#if 0 && defined( VIA_ACE_POSSIBLE ) && !defined( ASSUME_VIA_ACE_PRESENT ) -# define ASSUME_VIA_ACE_PRESENT -# endif - -/* 3. ASSEMBLER SUPPORT - - This define (which can be on the command line) enables the use of the - assembler code routines for encryption, decryption and key scheduling - as follows: - - ASM_X86_V1C uses the assembler (aes_x86_v1.asm) with large tables for - encryption and decryption and but with key scheduling in C - ASM_X86_V2 uses assembler (aes_x86_v2.asm) with compressed tables for - encryption, decryption and key scheduling - ASM_X86_V2C uses assembler (aes_x86_v2.asm) with compressed tables for - encryption and decryption and but with key scheduling in C - ASM_AMD64_C uses assembler (aes_amd64.asm) with compressed tables for - encryption and decryption and but with key scheduling in C - - Change one 'if 0' below to 'if 1' to select the version or define - as a compilation option. -*/ - -#if 0 && !defined( ASM_X86_V1C ) -# define ASM_X86_V1C -#elif 0 && !defined( ASM_X86_V2 ) -# define ASM_X86_V2 -#elif 0 && !defined( ASM_X86_V2C ) -# define ASM_X86_V2C -#elif 0 && !defined( ASM_AMD64_C ) -# define ASM_AMD64_C -#endif - -#if (defined ( ASM_X86_V1C ) || defined( ASM_X86_V2 ) || defined( ASM_X86_V2C )) \ - && !defined( _M_IX86 ) || defined( ASM_AMD64_C ) && !defined( _M_X64 ) -# error Assembler code is only available for x86 and AMD64 systems -#endif - -/* 4. FAST INPUT/OUTPUT OPERATIONS. - - On some machines it is possible to improve speed by transferring the - bytes in the input and output arrays to and from the internal 32-bit - variables by addressing these arrays as if they are arrays of 32-bit - words. On some machines this will always be possible but there may - be a large performance penalty if the byte arrays are not aligned on - the normal word boundaries. On other machines this technique will - lead to memory access errors when such 32-bit word accesses are not - properly aligned. The option SAFE_IO avoids such problems but will - often be slower on those machines that support misaligned access - (especially so if care is taken to align the input and output byte - arrays on 32-bit word boundaries). If SAFE_IO is not defined it is - assumed that access to byte arrays as if they are arrays of 32-bit - words will not cause problems when such accesses are misaligned. -*/ -#if 1 && !defined( _MSC_VER ) -# define SAFE_IO -#endif - -/* 5. LOOP UNROLLING - - The code for encryption and decrytpion cycles through a number of rounds - that can be implemented either in a loop or by expanding the code into a - long sequence of instructions, the latter producing a larger program but - one that will often be much faster. The latter is called loop unrolling. - There are also potential speed advantages in expanding two iterations in - a loop with half the number of iterations, which is called partial loop - unrolling. The following options allow partial or full loop unrolling - to be set independently for encryption and decryption -*/ -#if 1 -# define ENC_UNROLL FULL -#elif 0 -# define ENC_UNROLL PARTIAL -#else -# define ENC_UNROLL NONE -#endif - -#if 1 -# define DEC_UNROLL FULL -#elif 0 -# define DEC_UNROLL PARTIAL -#else -# define DEC_UNROLL NONE -#endif - -#if 1 -# define ENC_KS_UNROLL -#endif - -#if 1 -# define DEC_KS_UNROLL -#endif - -/* 6. FAST FINITE FIELD OPERATIONS - - If this section is included, tables are used to provide faster finite - field arithmetic (this has no effect if FIXED_TABLES is defined). -*/ -#if 1 -# define FF_TABLES -#endif - -/* 7. INTERNAL STATE VARIABLE FORMAT - - The internal state of Rijndael is stored in a number of local 32-bit - word varaibles which can be defined either as an array or as individual - names variables. Include this section if you want to store these local - varaibles in arrays. Otherwise individual local variables will be used. -*/ -#if 1 -# define ARRAYS -#endif - -/* 8. FIXED OR DYNAMIC TABLES - - When this section is included the tables used by the code are compiled - statically into the binary file. Otherwise the subroutine aes_init() - must be called to compute them before the code is first used. -*/ -#if 1 && !(defined( _MSC_VER ) && ( _MSC_VER <= 800 )) -# define FIXED_TABLES -#endif - -/* 9. MASKING OR CASTING FROM LONGER VALUES TO BYTES - - In some systems it is better to mask longer values to extract bytes - rather than using a cast. This option allows this choice. -*/ -#if 0 -# define to_byte(x) ((uint_8t)(x)) -#else -# define to_byte(x) ((x) & 0xff) -#endif - -/* 10. TABLE ALIGNMENT - - On some sytsems speed will be improved by aligning the AES large lookup - tables on particular boundaries. This define should be set to a power of - two giving the desired alignment. It can be left undefined if alignment - is not needed. This option is specific to the Microsft VC++ compiler - - it seems to sometimes cause trouble for the VC++ version 6 compiler. -*/ - -#if 1 && defined( _MSC_VER ) && ( _MSC_VER >= 1300 ) -# define TABLE_ALIGN 32 -#endif - -/* 11. REDUCE CODE AND TABLE SIZE - - This replaces some expanded macros with function calls if AES_ASM_V2 or - AES_ASM_V2C are defined -*/ - -#if 1 && (defined( ASM_X86_V2 ) || defined( ASM_X86_V2C )) -# define REDUCE_CODE_SIZE -#endif - -/* 12. TABLE OPTIONS - - This cipher proceeds by repeating in a number of cycles known as 'rounds' - which are implemented by a round function which can optionally be speeded - up using tables. The basic tables are each 256 32-bit words, with either - one or four tables being required for each round function depending on - how much speed is required. The encryption and decryption round functions - are different and the last encryption and decrytpion round functions are - different again making four different round functions in all. - - This means that: - 1. Normal encryption and decryption rounds can each use either 0, 1 - or 4 tables and table spaces of 0, 1024 or 4096 bytes each. - 2. The last encryption and decryption rounds can also use either 0, 1 - or 4 tables and table spaces of 0, 1024 or 4096 bytes each. - - Include or exclude the appropriate definitions below to set the number - of tables used by this implementation. -*/ - -#if 1 /* set tables for the normal encryption round */ -# define ENC_ROUND FOUR_TABLES -#elif 0 -# define ENC_ROUND ONE_TABLE -#else -# define ENC_ROUND NO_TABLES -#endif - -#if 1 /* set tables for the last encryption round */ -# define LAST_ENC_ROUND FOUR_TABLES -#elif 0 -# define LAST_ENC_ROUND ONE_TABLE -#else -# define LAST_ENC_ROUND NO_TABLES -#endif - -#if 1 /* set tables for the normal decryption round */ -# define DEC_ROUND FOUR_TABLES -#elif 0 -# define DEC_ROUND ONE_TABLE -#else -# define DEC_ROUND NO_TABLES -#endif - -#if 1 /* set tables for the last decryption round */ -# define LAST_DEC_ROUND FOUR_TABLES -#elif 0 -# define LAST_DEC_ROUND ONE_TABLE -#else -# define LAST_DEC_ROUND NO_TABLES -#endif - -/* The decryption key schedule can be speeded up with tables in the same - way that the round functions can. Include or exclude the following - defines to set this requirement. -*/ -#if 1 -# define KEY_SCHED FOUR_TABLES -#elif 0 -# define KEY_SCHED ONE_TABLE -#else -# define KEY_SCHED NO_TABLES -#endif - -/* ---- END OF USER CONFIGURED OPTIONS ---- */ - -/* VIA ACE support is only available for VC++ and GCC */ - -#if !defined( _MSC_VER ) && !defined( __GNUC__ ) -# if defined( ASSUME_VIA_ACE_PRESENT ) -# undef ASSUME_VIA_ACE_PRESENT -# endif -# if defined( USE_VIA_ACE_IF_PRESENT ) -# undef USE_VIA_ACE_IF_PRESENT -# endif -#endif - -#if defined( ASSUME_VIA_ACE_PRESENT ) && !defined( USE_VIA_ACE_IF_PRESENT ) -# define USE_VIA_ACE_IF_PRESENT -#endif - -#if defined( USE_VIA_ACE_IF_PRESENT ) && !defined ( AES_REV_DKS ) -# define AES_REV_DKS -#endif - -/* Assembler support requires the use of platform byte order */ - -#if ( defined( ASM_X86_V1C ) || defined( ASM_X86_V2C ) || defined( ASM_AMD64_C ) ) \ - && (ALGORITHM_BYTE_ORDER != PLATFORM_BYTE_ORDER) -# undef ALGORITHM_BYTE_ORDER -# define ALGORITHM_BYTE_ORDER PLATFORM_BYTE_ORDER -#endif - -/* In this implementation the columns of the state array are each held in - 32-bit words. The state array can be held in various ways: in an array - of words, in a number of individual word variables or in a number of - processor registers. The following define maps a variable name x and - a column number c to the way the state array variable is to be held. - The first define below maps the state into an array x[c] whereas the - second form maps the state into a number of individual variables x0, - x1, etc. Another form could map individual state colums to machine - register names. -*/ - -#if defined( ARRAYS ) -# define s(x,c) x[c] -#else -# define s(x,c) x##c -#endif - -/* This implementation provides subroutines for encryption, decryption - and for setting the three key lengths (separately) for encryption - and decryption. Since not all functions are needed, masks are set - up here to determine which will be implemented in C -*/ - -#if !defined( AES_ENCRYPT ) -# define EFUNCS_IN_C 0 -#elif defined( ASSUME_VIA_ACE_PRESENT ) || defined( ASM_X86_V1C ) \ - || defined( ASM_X86_V2C ) || defined( ASM_AMD64_C ) -# define EFUNCS_IN_C ENC_KEYING_IN_C -#elif !defined( ASM_X86_V2 ) -# define EFUNCS_IN_C ( ENCRYPTION_IN_C | ENC_KEYING_IN_C ) -#else -# define EFUNCS_IN_C 0 -#endif - -#if !defined( AES_DECRYPT ) -# define DFUNCS_IN_C 0 -#elif defined( ASSUME_VIA_ACE_PRESENT ) || defined( ASM_X86_V1C ) \ - || defined( ASM_X86_V2C ) || defined( ASM_AMD64_C ) -# define DFUNCS_IN_C DEC_KEYING_IN_C -#elif !defined( ASM_X86_V2 ) -# define DFUNCS_IN_C ( DECRYPTION_IN_C | DEC_KEYING_IN_C ) -#else -# define DFUNCS_IN_C 0 -#endif - -#define FUNCS_IN_C ( EFUNCS_IN_C | DFUNCS_IN_C ) - -/* END OF CONFIGURATION OPTIONS */ - -#define RC_LENGTH (5 * (AES_BLOCK_SIZE / 4 - 2)) - -/* Disable or report errors on some combinations of options */ - -#if ENC_ROUND == NO_TABLES && LAST_ENC_ROUND != NO_TABLES -# undef LAST_ENC_ROUND -# define LAST_ENC_ROUND NO_TABLES -#elif ENC_ROUND == ONE_TABLE && LAST_ENC_ROUND == FOUR_TABLES -# undef LAST_ENC_ROUND -# define LAST_ENC_ROUND ONE_TABLE -#endif - -#if ENC_ROUND == NO_TABLES && ENC_UNROLL != NONE -# undef ENC_UNROLL -# define ENC_UNROLL NONE -#endif - -#if DEC_ROUND == NO_TABLES && LAST_DEC_ROUND != NO_TABLES -# undef LAST_DEC_ROUND -# define LAST_DEC_ROUND NO_TABLES -#elif DEC_ROUND == ONE_TABLE && LAST_DEC_ROUND == FOUR_TABLES -# undef LAST_DEC_ROUND -# define LAST_DEC_ROUND ONE_TABLE -#endif - -#if DEC_ROUND == NO_TABLES && DEC_UNROLL != NONE -# undef DEC_UNROLL -# define DEC_UNROLL NONE -#endif - -#if defined( bswap32 ) -# define aes_sw32 bswap32 -#elif defined( bswap_32 ) -# define aes_sw32 bswap_32 -#else -# define brot(x,n) (((uint_32t)(x) << n) | ((uint_32t)(x) >> (32 - n))) -# define aes_sw32(x) ((brot((x),8) & 0x00ff00ff) | (brot((x),24) & 0xff00ff00)) -#endif - -/* upr(x,n): rotates bytes within words by n positions, moving bytes to - higher index positions with wrap around into low positions - ups(x,n): moves bytes by n positions to higher index positions in - words but without wrap around - bval(x,n): extracts a byte from a word - - WARNING: The definitions given here are intended only for use with - unsigned variables and with shift counts that are compile - time constants -*/ - -#if ( ALGORITHM_BYTE_ORDER == IS_LITTLE_ENDIAN ) -# define upr(x,n) (((uint_32t)(x) << (8 * (n))) | ((uint_32t)(x) >> (32 - 8 * (n)))) -# define ups(x,n) ((uint_32t) (x) << (8 * (n))) -# define bval(x,n) to_byte((x) >> (8 * (n))) -# define bytes2word(b0, b1, b2, b3) \ - (((uint_32t)(b3) << 24) | ((uint_32t)(b2) << 16) | ((uint_32t)(b1) << 8) | (b0)) -#endif - -#if ( ALGORITHM_BYTE_ORDER == IS_BIG_ENDIAN ) -# define upr(x,n) (((uint_32t)(x) >> (8 * (n))) | ((uint_32t)(x) << (32 - 8 * (n)))) -# define ups(x,n) ((uint_32t) (x) >> (8 * (n))) -# define bval(x,n) to_byte((x) >> (24 - 8 * (n))) -# define bytes2word(b0, b1, b2, b3) \ - (((uint_32t)(b0) << 24) | ((uint_32t)(b1) << 16) | ((uint_32t)(b2) << 8) | (b3)) -#endif - -#if defined( SAFE_IO ) -# define word_in(x,c) bytes2word(((const uint_8t*)(x)+4*c)[0], ((const uint_8t*)(x)+4*c)[1], \ - ((const uint_8t*)(x)+4*c)[2], ((const uint_8t*)(x)+4*c)[3]) -# define word_out(x,c,v) { ((uint_8t*)(x)+4*c)[0] = bval(v,0); ((uint_8t*)(x)+4*c)[1] = bval(v,1); \ - ((uint_8t*)(x)+4*c)[2] = bval(v,2); ((uint_8t*)(x)+4*c)[3] = bval(v,3); } -#elif ( ALGORITHM_BYTE_ORDER == PLATFORM_BYTE_ORDER ) -# define word_in(x,c) (*((uint_32t*)(x)+(c))) -# define word_out(x,c,v) (*((uint_32t*)(x)+(c)) = (v)) -#else -# define word_in(x,c) aes_sw32(*((uint_32t*)(x)+(c))) -# define word_out(x,c,v) (*((uint_32t*)(x)+(c)) = aes_sw32(v)) -#endif - -/* the finite field modular polynomial and elements */ - -#define WPOLY 0x011b -#define BPOLY 0x1b - -/* multiply four bytes in GF(2^8) by 'x' {02} in parallel */ - -#define gf_c1 0x80808080 -#define gf_c2 0x7f7f7f7f -#define gf_mulx(x) ((((x) & gf_c2) << 1) ^ ((((x) & gf_c1) >> 7) * BPOLY)) - -/* The following defines provide alternative definitions of gf_mulx that might - give improved performance if a fast 32-bit multiply is not available. Note - that a temporary variable u needs to be defined where gf_mulx is used. - -#define gf_mulx(x) (u = (x) & gf_c1, u |= (u >> 1), ((x) & gf_c2) << 1) ^ ((u >> 3) | (u >> 6)) -#define gf_c4 (0x01010101 * BPOLY) -#define gf_mulx(x) (u = (x) & gf_c1, ((x) & gf_c2) << 1) ^ ((u - (u >> 7)) & gf_c4) -*/ - -/* Work out which tables are needed for the different options */ - -#if defined( ASM_X86_V1C ) -# if defined( ENC_ROUND ) -# undef ENC_ROUND -# endif -# define ENC_ROUND FOUR_TABLES -# if defined( LAST_ENC_ROUND ) -# undef LAST_ENC_ROUND -# endif -# define LAST_ENC_ROUND FOUR_TABLES -# if defined( DEC_ROUND ) -# undef DEC_ROUND -# endif -# define DEC_ROUND FOUR_TABLES -# if defined( LAST_DEC_ROUND ) -# undef LAST_DEC_ROUND -# endif -# define LAST_DEC_ROUND FOUR_TABLES -# if defined( KEY_SCHED ) -# undef KEY_SCHED -# define KEY_SCHED FOUR_TABLES -# endif -#endif - -#if ( FUNCS_IN_C & ENCRYPTION_IN_C ) || defined( ASM_X86_V1C ) -# if ENC_ROUND == ONE_TABLE -# define FT1_SET -# elif ENC_ROUND == FOUR_TABLES -# define FT4_SET -# else -# define SBX_SET -# endif -# if LAST_ENC_ROUND == ONE_TABLE -# define FL1_SET -# elif LAST_ENC_ROUND == FOUR_TABLES -# define FL4_SET -# elif !defined( SBX_SET ) -# define SBX_SET -# endif -#endif - -#if ( FUNCS_IN_C & DECRYPTION_IN_C ) || defined( ASM_X86_V1C ) -# if DEC_ROUND == ONE_TABLE -# define IT1_SET -# elif DEC_ROUND == FOUR_TABLES -# define IT4_SET -# else -# define ISB_SET -# endif -# if LAST_DEC_ROUND == ONE_TABLE -# define IL1_SET -# elif LAST_DEC_ROUND == FOUR_TABLES -# define IL4_SET -# elif !defined(ISB_SET) -# define ISB_SET -# endif -#endif - -#if !(defined( REDUCE_CODE_SIZE ) && (defined( ASM_X86_V2 ) || defined( ASM_X86_V2C ))) -# if ((FUNCS_IN_C & ENC_KEYING_IN_C) || (FUNCS_IN_C & DEC_KEYING_IN_C)) -# if KEY_SCHED == ONE_TABLE -# if !defined( FL1_SET ) && !defined( FL4_SET ) -# define LS1_SET -# endif -# elif KEY_SCHED == FOUR_TABLES -# if !defined( FL4_SET ) -# define LS4_SET -# endif -# elif !defined( SBX_SET ) -# define SBX_SET -# endif -# endif -# if (FUNCS_IN_C & DEC_KEYING_IN_C) -# if KEY_SCHED == ONE_TABLE -# define IM1_SET -# elif KEY_SCHED == FOUR_TABLES -# define IM4_SET -# elif !defined( SBX_SET ) -# define SBX_SET -# endif -# endif -#endif - -/* generic definitions of Rijndael macros that use tables */ - -#define no_table(x,box,vf,rf,c) bytes2word( \ - box[bval(vf(x,0,c),rf(0,c))], \ - box[bval(vf(x,1,c),rf(1,c))], \ - box[bval(vf(x,2,c),rf(2,c))], \ - box[bval(vf(x,3,c),rf(3,c))]) - -#define one_table(x,op,tab,vf,rf,c) \ - ( tab[bval(vf(x,0,c),rf(0,c))] \ - ^ op(tab[bval(vf(x,1,c),rf(1,c))],1) \ - ^ op(tab[bval(vf(x,2,c),rf(2,c))],2) \ - ^ op(tab[bval(vf(x,3,c),rf(3,c))],3)) - -#define four_tables(x,tab,vf,rf,c) \ - ( tab[0][bval(vf(x,0,c),rf(0,c))] \ - ^ tab[1][bval(vf(x,1,c),rf(1,c))] \ - ^ tab[2][bval(vf(x,2,c),rf(2,c))] \ - ^ tab[3][bval(vf(x,3,c),rf(3,c))]) - -#define vf1(x,r,c) (x) -#define rf1(r,c) (r) -#define rf2(r,c) ((8+r-c)&3) - -/* perform forward and inverse column mix operation on four bytes in long word x in */ -/* parallel. NOTE: x must be a simple variable, NOT an expression in these macros. */ - -#if !(defined( REDUCE_CODE_SIZE ) && (defined( ASM_X86_V2 ) || defined( ASM_X86_V2C ))) - -#if defined( FM4_SET ) /* not currently used */ -# define fwd_mcol(x) four_tables(x,t_use(f,m),vf1,rf1,0) -#elif defined( FM1_SET ) /* not currently used */ -# define fwd_mcol(x) one_table(x,upr,t_use(f,m),vf1,rf1,0) -#else -# define dec_fmvars uint_32t g2 -# define fwd_mcol(x) (g2 = gf_mulx(x), g2 ^ upr((x) ^ g2, 3) ^ upr((x), 2) ^ upr((x), 1)) -#endif - -#if defined( IM4_SET ) -# define inv_mcol(x) four_tables(x,t_use(i,m),vf1,rf1,0) -#elif defined( IM1_SET ) -# define inv_mcol(x) one_table(x,upr,t_use(i,m),vf1,rf1,0) -#else -# define dec_imvars uint_32t g2, g4, g9 -# define inv_mcol(x) (g2 = gf_mulx(x), g4 = gf_mulx(g2), g9 = (x) ^ gf_mulx(g4), g4 ^= g9, \ - (x) ^ g2 ^ g4 ^ upr(g2 ^ g9, 3) ^ upr(g4, 2) ^ upr(g9, 1)) -#endif - -#if defined( FL4_SET ) -# define ls_box(x,c) four_tables(x,t_use(f,l),vf1,rf2,c) -#elif defined( LS4_SET ) -# define ls_box(x,c) four_tables(x,t_use(l,s),vf1,rf2,c) -#elif defined( FL1_SET ) -# define ls_box(x,c) one_table(x,upr,t_use(f,l),vf1,rf2,c) -#elif defined( LS1_SET ) -# define ls_box(x,c) one_table(x,upr,t_use(l,s),vf1,rf2,c) -#else -# define ls_box(x,c) no_table(x,t_use(s,box),vf1,rf2,c) -#endif - -#endif - -#if defined( ASM_X86_V1C ) && defined( AES_DECRYPT ) && !defined( ISB_SET ) -# define ISB_SET -#endif - -#endif +/* +--------------------------------------------------------------------------- +Copyright (c) 1998-2010, Brian Gladman, Worcester, UK. All rights reserved. + +The redistribution and use of this software (with or without changes) +is allowed without the payment of fees or royalties provided that: + + source code distributions include the above copyright notice, this + list of conditions and the following disclaimer; + + binary distributions include the above copyright notice, this list + of conditions and the following disclaimer in their documentation. + +This software is provided 'as is' with no explicit or implied warranties +in respect of its operation, including, but not limited to, correctness +and fitness for purpose. +--------------------------------------------------------------------------- +Issue Date: 20/12/2007 + + This file contains the compilation options for AES (Rijndael) and code + that is common across encryption, key scheduling and table generation. + + OPERATION + + These source code files implement the AES algorithm Rijndael designed by + Joan Daemen and Vincent Rijmen. This version is designed for the standard + block size of 16 bytes and for key sizes of 128, 192 and 256 bits (16, 24 + and 32 bytes). + + This version is designed for flexibility and speed using operations on + 32-bit words rather than operations on bytes. It can be compiled with + either big or little endian internal byte order but is faster when the + native byte order for the processor is used. + + THE CIPHER INTERFACE + + The cipher interface is implemented as an array of bytes in which lower + AES bit sequence indexes map to higher numeric significance within bytes. + + uint_8t (an unsigned 8-bit type) + uint_32t (an unsigned 32-bit type) + struct aes_encrypt_ctx (structure for the cipher encryption context) + struct aes_decrypt_ctx (structure for the cipher decryption context) + AES_RETURN the function return type + + C subroutine calls: + + AES_RETURN aes_encrypt_key128(const unsigned char *key, aes_encrypt_ctx cx[1]); + AES_RETURN aes_encrypt_key192(const unsigned char *key, aes_encrypt_ctx cx[1]); + AES_RETURN aes_encrypt_key256(const unsigned char *key, aes_encrypt_ctx cx[1]); + AES_RETURN aes_encrypt(const unsigned char *in, unsigned char *out, + const aes_encrypt_ctx cx[1]); + + AES_RETURN aes_decrypt_key128(const unsigned char *key, aes_decrypt_ctx cx[1]); + AES_RETURN aes_decrypt_key192(const unsigned char *key, aes_decrypt_ctx cx[1]); + AES_RETURN aes_decrypt_key256(const unsigned char *key, aes_decrypt_ctx cx[1]); + AES_RETURN aes_decrypt(const unsigned char *in, unsigned char *out, + const aes_decrypt_ctx cx[1]); + + IMPORTANT NOTE: If you are using this C interface with dynamic tables make sure that + you call aes_init() before AES is used so that the tables are initialised. + + C++ aes class subroutines: + + Class AESencrypt for encryption + + Construtors: + AESencrypt(void) + AESencrypt(const unsigned char *key) - 128 bit key + Members: + AES_RETURN key128(const unsigned char *key) + AES_RETURN key192(const unsigned char *key) + AES_RETURN key256(const unsigned char *key) + AES_RETURN encrypt(const unsigned char *in, unsigned char *out) const + + Class AESdecrypt for encryption + Construtors: + AESdecrypt(void) + AESdecrypt(const unsigned char *key) - 128 bit key + Members: + AES_RETURN key128(const unsigned char *key) + AES_RETURN key192(const unsigned char *key) + AES_RETURN key256(const unsigned char *key) + AES_RETURN decrypt(const unsigned char *in, unsigned char *out) const +*/ + +#if !defined( _AESOPT_H ) +#define _AESOPT_H + +#if defined( __cplusplus ) +#include "aescpp.h" +#else +#include "aes.h" +#endif + +/* PLATFORM SPECIFIC INCLUDES */ + +#include "brg_endian.h" + +/* CONFIGURATION - THE USE OF DEFINES + + Later in this section there are a number of defines that control the + operation of the code. In each section, the purpose of each define is + explained so that the relevant form can be included or excluded by + setting either 1's or 0's respectively on the branches of the related + #if clauses. The following local defines should not be changed. +*/ + +#define ENCRYPTION_IN_C 1 +#define DECRYPTION_IN_C 2 +#define ENC_KEYING_IN_C 4 +#define DEC_KEYING_IN_C 8 + +#define NO_TABLES 0 +#define ONE_TABLE 1 +#define FOUR_TABLES 4 +#define NONE 0 +#define PARTIAL 1 +#define FULL 2 + +/* --- START OF USER CONFIGURED OPTIONS --- */ + +/* 1. BYTE ORDER WITHIN 32 BIT WORDS + + The fundamental data processing units in Rijndael are 8-bit bytes. The + input, output and key input are all enumerated arrays of bytes in which + bytes are numbered starting at zero and increasing to one less than the + number of bytes in the array in question. This enumeration is only used + for naming bytes and does not imply any adjacency or order relationship + from one byte to another. When these inputs and outputs are considered + as bit sequences, bits 8*n to 8*n+7 of the bit sequence are mapped to + byte[n] with bit 8n+i in the sequence mapped to bit 7-i within the byte. + In this implementation bits are numbered from 0 to 7 starting at the + numerically least significant end of each byte (bit n represents 2^n). + + However, Rijndael can be implemented more efficiently using 32-bit + words by packing bytes into words so that bytes 4*n to 4*n+3 are placed + into word[n]. While in principle these bytes can be assembled into words + in any positions, this implementation only supports the two formats in + which bytes in adjacent positions within words also have adjacent byte + numbers. This order is called big-endian if the lowest numbered bytes + in words have the highest numeric significance and little-endian if the + opposite applies. + + This code can work in either order irrespective of the order used by the + machine on which it runs. Normally the internal byte order will be set + to the order of the processor on which the code is to be run but this + define can be used to reverse this in special situations + + WARNING: Assembler code versions rely on PLATFORM_BYTE_ORDER being set. + This define will hence be redefined later (in section 4) if necessary +*/ + +#if 1 +# define ALGORITHM_BYTE_ORDER PLATFORM_BYTE_ORDER +#elif 0 +# define ALGORITHM_BYTE_ORDER IS_LITTLE_ENDIAN +#elif 0 +# define ALGORITHM_BYTE_ORDER IS_BIG_ENDIAN +#else +# error The algorithm byte order is not defined +#endif + +/* 2. VIA ACE SUPPORT */ + +#if !defined(__APPLE__) && defined( __GNUC__ ) && defined( __i386__ ) \ + || defined( _WIN32 ) && defined( _M_IX86 ) \ + && !(defined( _WIN64 ) || defined( _WIN32_WCE ) || defined( _MSC_VER ) && ( _MSC_VER <= 800 )) +# define VIA_ACE_POSSIBLE +#endif + +/* Define this option if support for the VIA ACE is required. This uses + inline assembler instructions and is only implemented for the Microsoft, + Intel and GCC compilers. If VIA ACE is known to be present, then defining + ASSUME_VIA_ACE_PRESENT will remove the ordinary encryption/decryption + code. If USE_VIA_ACE_IF_PRESENT is defined then VIA ACE will be used if + it is detected (both present and enabled) but the normal AES code will + also be present. + + When VIA ACE is to be used, all AES encryption contexts MUST be 16 byte + aligned; other input/output buffers do not need to be 16 byte aligned + but there are very large performance gains if this can be arranged. + VIA ACE also requires the decryption key schedule to be in reverse + order (which later checks below ensure). +*/ + +#if 1 && defined( VIA_ACE_POSSIBLE ) && !defined( USE_VIA_ACE_IF_PRESENT ) +# define USE_VIA_ACE_IF_PRESENT +#endif + +#if 0 && defined( VIA_ACE_POSSIBLE ) && !defined( ASSUME_VIA_ACE_PRESENT ) +# define ASSUME_VIA_ACE_PRESENT +# endif + +/* 3. ASSEMBLER SUPPORT + + This define (which can be on the command line) enables the use of the + assembler code routines for encryption, decryption and key scheduling + as follows: + + ASM_X86_V1C uses the assembler (aes_x86_v1.asm) with large tables for + encryption and decryption and but with key scheduling in C + ASM_X86_V2 uses assembler (aes_x86_v2.asm) with compressed tables for + encryption, decryption and key scheduling + ASM_X86_V2C uses assembler (aes_x86_v2.asm) with compressed tables for + encryption and decryption and but with key scheduling in C + ASM_AMD64_C uses assembler (aes_amd64.asm) with compressed tables for + encryption and decryption and but with key scheduling in C + + Change one 'if 0' below to 'if 1' to select the version or define + as a compilation option. +*/ + +#if 0 && !defined( ASM_X86_V1C ) +# define ASM_X86_V1C +#elif 0 && !defined( ASM_X86_V2 ) +# define ASM_X86_V2 +#elif 0 && !defined( ASM_X86_V2C ) +# define ASM_X86_V2C +#elif 0 && !defined( ASM_AMD64_C ) +# define ASM_AMD64_C +#endif + +#if (defined ( ASM_X86_V1C ) || defined( ASM_X86_V2 ) || defined( ASM_X86_V2C )) \ + && !defined( _M_IX86 ) || defined( ASM_AMD64_C ) && !defined( _M_X64 ) +# error Assembler code is only available for x86 and AMD64 systems +#endif + +/* 4. FAST INPUT/OUTPUT OPERATIONS. + + On some machines it is possible to improve speed by transferring the + bytes in the input and output arrays to and from the internal 32-bit + variables by addressing these arrays as if they are arrays of 32-bit + words. On some machines this will always be possible but there may + be a large performance penalty if the byte arrays are not aligned on + the normal word boundaries. On other machines this technique will + lead to memory access errors when such 32-bit word accesses are not + properly aligned. The option SAFE_IO avoids such problems but will + often be slower on those machines that support misaligned access + (especially so if care is taken to align the input and output byte + arrays on 32-bit word boundaries). If SAFE_IO is not defined it is + assumed that access to byte arrays as if they are arrays of 32-bit + words will not cause problems when such accesses are misaligned. +*/ +#if 1 && !defined( _MSC_VER ) +# define SAFE_IO +#endif + +/* 5. LOOP UNROLLING + + The code for encryption and decrytpion cycles through a number of rounds + that can be implemented either in a loop or by expanding the code into a + long sequence of instructions, the latter producing a larger program but + one that will often be much faster. The latter is called loop unrolling. + There are also potential speed advantages in expanding two iterations in + a loop with half the number of iterations, which is called partial loop + unrolling. The following options allow partial or full loop unrolling + to be set independently for encryption and decryption +*/ +#if 1 +# define ENC_UNROLL FULL +#elif 0 +# define ENC_UNROLL PARTIAL +#else +# define ENC_UNROLL NONE +#endif + +#if 1 +# define DEC_UNROLL FULL +#elif 0 +# define DEC_UNROLL PARTIAL +#else +# define DEC_UNROLL NONE +#endif + +#if 1 +# define ENC_KS_UNROLL +#endif + +#if 1 +# define DEC_KS_UNROLL +#endif + +/* 6. FAST FINITE FIELD OPERATIONS + + If this section is included, tables are used to provide faster finite + field arithmetic (this has no effect if FIXED_TABLES is defined). +*/ +#if 1 +# define FF_TABLES +#endif + +/* 7. INTERNAL STATE VARIABLE FORMAT + + The internal state of Rijndael is stored in a number of local 32-bit + word varaibles which can be defined either as an array or as individual + names variables. Include this section if you want to store these local + varaibles in arrays. Otherwise individual local variables will be used. +*/ +#if 1 +# define ARRAYS +#endif + +/* 8. FIXED OR DYNAMIC TABLES + + When this section is included the tables used by the code are compiled + statically into the binary file. Otherwise the subroutine aes_init() + must be called to compute them before the code is first used. +*/ +#if 1 && !(defined( _MSC_VER ) && ( _MSC_VER <= 800 )) +# define FIXED_TABLES +#endif + +/* 9. MASKING OR CASTING FROM LONGER VALUES TO BYTES + + In some systems it is better to mask longer values to extract bytes + rather than using a cast. This option allows this choice. +*/ +#if 0 +# define to_byte(x) ((uint_8t)(x)) +#else +# define to_byte(x) ((x) & 0xff) +#endif + +/* 10. TABLE ALIGNMENT + + On some sytsems speed will be improved by aligning the AES large lookup + tables on particular boundaries. This define should be set to a power of + two giving the desired alignment. It can be left undefined if alignment + is not needed. This option is specific to the Microsft VC++ compiler - + it seems to sometimes cause trouble for the VC++ version 6 compiler. +*/ + +#if 1 && defined( _MSC_VER ) && ( _MSC_VER >= 1300 ) +# define TABLE_ALIGN 32 +#endif + +/* 11. REDUCE CODE AND TABLE SIZE + + This replaces some expanded macros with function calls if AES_ASM_V2 or + AES_ASM_V2C are defined +*/ + +#if 1 && (defined( ASM_X86_V2 ) || defined( ASM_X86_V2C )) +# define REDUCE_CODE_SIZE +#endif + +/* 12. TABLE OPTIONS + + This cipher proceeds by repeating in a number of cycles known as 'rounds' + which are implemented by a round function which can optionally be speeded + up using tables. The basic tables are each 256 32-bit words, with either + one or four tables being required for each round function depending on + how much speed is required. The encryption and decryption round functions + are different and the last encryption and decrytpion round functions are + different again making four different round functions in all. + + This means that: + 1. Normal encryption and decryption rounds can each use either 0, 1 + or 4 tables and table spaces of 0, 1024 or 4096 bytes each. + 2. The last encryption and decryption rounds can also use either 0, 1 + or 4 tables and table spaces of 0, 1024 or 4096 bytes each. + + Include or exclude the appropriate definitions below to set the number + of tables used by this implementation. +*/ + +#if 1 /* set tables for the normal encryption round */ +# define ENC_ROUND FOUR_TABLES +#elif 0 +# define ENC_ROUND ONE_TABLE +#else +# define ENC_ROUND NO_TABLES +#endif + +#if 1 /* set tables for the last encryption round */ +# define LAST_ENC_ROUND FOUR_TABLES +#elif 0 +# define LAST_ENC_ROUND ONE_TABLE +#else +# define LAST_ENC_ROUND NO_TABLES +#endif + +#if 1 /* set tables for the normal decryption round */ +# define DEC_ROUND FOUR_TABLES +#elif 0 +# define DEC_ROUND ONE_TABLE +#else +# define DEC_ROUND NO_TABLES +#endif + +#if 1 /* set tables for the last decryption round */ +# define LAST_DEC_ROUND FOUR_TABLES +#elif 0 +# define LAST_DEC_ROUND ONE_TABLE +#else +# define LAST_DEC_ROUND NO_TABLES +#endif + +/* The decryption key schedule can be speeded up with tables in the same + way that the round functions can. Include or exclude the following + defines to set this requirement. +*/ +#if 1 +# define KEY_SCHED FOUR_TABLES +#elif 0 +# define KEY_SCHED ONE_TABLE +#else +# define KEY_SCHED NO_TABLES +#endif + +/* ---- END OF USER CONFIGURED OPTIONS ---- */ + +/* VIA ACE support is only available for VC++ and GCC */ + +#if !defined( _MSC_VER ) && !defined( __GNUC__ ) +# if defined( ASSUME_VIA_ACE_PRESENT ) +# undef ASSUME_VIA_ACE_PRESENT +# endif +# if defined( USE_VIA_ACE_IF_PRESENT ) +# undef USE_VIA_ACE_IF_PRESENT +# endif +#endif + +#if defined( ASSUME_VIA_ACE_PRESENT ) && !defined( USE_VIA_ACE_IF_PRESENT ) +# define USE_VIA_ACE_IF_PRESENT +#endif + +#if defined( USE_VIA_ACE_IF_PRESENT ) && !defined ( AES_REV_DKS ) +# define AES_REV_DKS +#endif + +/* Assembler support requires the use of platform byte order */ + +#if ( defined( ASM_X86_V1C ) || defined( ASM_X86_V2C ) || defined( ASM_AMD64_C ) ) \ + && (ALGORITHM_BYTE_ORDER != PLATFORM_BYTE_ORDER) +# undef ALGORITHM_BYTE_ORDER +# define ALGORITHM_BYTE_ORDER PLATFORM_BYTE_ORDER +#endif + +/* In this implementation the columns of the state array are each held in + 32-bit words. The state array can be held in various ways: in an array + of words, in a number of individual word variables or in a number of + processor registers. The following define maps a variable name x and + a column number c to the way the state array variable is to be held. + The first define below maps the state into an array x[c] whereas the + second form maps the state into a number of individual variables x0, + x1, etc. Another form could map individual state colums to machine + register names. +*/ + +#if defined( ARRAYS ) +# define s(x,c) x[c] +#else +# define s(x,c) x##c +#endif + +/* This implementation provides subroutines for encryption, decryption + and for setting the three key lengths (separately) for encryption + and decryption. Since not all functions are needed, masks are set + up here to determine which will be implemented in C +*/ + +#if !defined( AES_ENCRYPT ) +# define EFUNCS_IN_C 0 +#elif defined( ASSUME_VIA_ACE_PRESENT ) || defined( ASM_X86_V1C ) \ + || defined( ASM_X86_V2C ) || defined( ASM_AMD64_C ) +# define EFUNCS_IN_C ENC_KEYING_IN_C +#elif !defined( ASM_X86_V2 ) +# define EFUNCS_IN_C ( ENCRYPTION_IN_C | ENC_KEYING_IN_C ) +#else +# define EFUNCS_IN_C 0 +#endif + +#if !defined( AES_DECRYPT ) +# define DFUNCS_IN_C 0 +#elif defined( ASSUME_VIA_ACE_PRESENT ) || defined( ASM_X86_V1C ) \ + || defined( ASM_X86_V2C ) || defined( ASM_AMD64_C ) +# define DFUNCS_IN_C DEC_KEYING_IN_C +#elif !defined( ASM_X86_V2 ) +# define DFUNCS_IN_C ( DECRYPTION_IN_C | DEC_KEYING_IN_C ) +#else +# define DFUNCS_IN_C 0 +#endif + +#define FUNCS_IN_C ( EFUNCS_IN_C | DFUNCS_IN_C ) + +/* END OF CONFIGURATION OPTIONS */ + +#define RC_LENGTH (5 * (AES_BLOCK_SIZE / 4 - 2)) + +/* Disable or report errors on some combinations of options */ + +#if ENC_ROUND == NO_TABLES && LAST_ENC_ROUND != NO_TABLES +# undef LAST_ENC_ROUND +# define LAST_ENC_ROUND NO_TABLES +#elif ENC_ROUND == ONE_TABLE && LAST_ENC_ROUND == FOUR_TABLES +# undef LAST_ENC_ROUND +# define LAST_ENC_ROUND ONE_TABLE +#endif + +#if ENC_ROUND == NO_TABLES && ENC_UNROLL != NONE +# undef ENC_UNROLL +# define ENC_UNROLL NONE +#endif + +#if DEC_ROUND == NO_TABLES && LAST_DEC_ROUND != NO_TABLES +# undef LAST_DEC_ROUND +# define LAST_DEC_ROUND NO_TABLES +#elif DEC_ROUND == ONE_TABLE && LAST_DEC_ROUND == FOUR_TABLES +# undef LAST_DEC_ROUND +# define LAST_DEC_ROUND ONE_TABLE +#endif + +#if DEC_ROUND == NO_TABLES && DEC_UNROLL != NONE +# undef DEC_UNROLL +# define DEC_UNROLL NONE +#endif + +#if defined( bswap32 ) +# define aes_sw32 bswap32 +#elif defined( bswap_32 ) +# define aes_sw32 bswap_32 +#else +# define brot(x,n) (((uint_32t)(x) << n) | ((uint_32t)(x) >> (32 - n))) +# define aes_sw32(x) ((brot((x),8) & 0x00ff00ff) | (brot((x),24) & 0xff00ff00)) +#endif + +/* upr(x,n): rotates bytes within words by n positions, moving bytes to + higher index positions with wrap around into low positions + ups(x,n): moves bytes by n positions to higher index positions in + words but without wrap around + bval(x,n): extracts a byte from a word + + WARNING: The definitions given here are intended only for use with + unsigned variables and with shift counts that are compile + time constants +*/ + +#if ( ALGORITHM_BYTE_ORDER == IS_LITTLE_ENDIAN ) +# define upr(x,n) (((uint_32t)(x) << (8 * (n))) | ((uint_32t)(x) >> (32 - 8 * (n)))) +# define ups(x,n) ((uint_32t) (x) << (8 * (n))) +# define bval(x,n) to_byte((x) >> (8 * (n))) +# define bytes2word(b0, b1, b2, b3) \ + (((uint_32t)(b3) << 24) | ((uint_32t)(b2) << 16) | ((uint_32t)(b1) << 8) | (b0)) +#endif + +#if ( ALGORITHM_BYTE_ORDER == IS_BIG_ENDIAN ) +# define upr(x,n) (((uint_32t)(x) >> (8 * (n))) | ((uint_32t)(x) << (32 - 8 * (n)))) +# define ups(x,n) ((uint_32t) (x) >> (8 * (n))) +# define bval(x,n) to_byte((x) >> (24 - 8 * (n))) +# define bytes2word(b0, b1, b2, b3) \ + (((uint_32t)(b0) << 24) | ((uint_32t)(b1) << 16) | ((uint_32t)(b2) << 8) | (b3)) +#endif + +#if defined( SAFE_IO ) +# define word_in(x,c) bytes2word(((const uint_8t*)(x)+4*c)[0], ((const uint_8t*)(x)+4*c)[1], \ + ((const uint_8t*)(x)+4*c)[2], ((const uint_8t*)(x)+4*c)[3]) +# define word_out(x,c,v) { ((uint_8t*)(x)+4*c)[0] = bval(v,0); ((uint_8t*)(x)+4*c)[1] = bval(v,1); \ + ((uint_8t*)(x)+4*c)[2] = bval(v,2); ((uint_8t*)(x)+4*c)[3] = bval(v,3); } +#elif ( ALGORITHM_BYTE_ORDER == PLATFORM_BYTE_ORDER ) +# define word_in(x,c) (*((uint_32t*)(x)+(c))) +# define word_out(x,c,v) (*((uint_32t*)(x)+(c)) = (v)) +#else +# define word_in(x,c) aes_sw32(*((uint_32t*)(x)+(c))) +# define word_out(x,c,v) (*((uint_32t*)(x)+(c)) = aes_sw32(v)) +#endif + +/* the finite field modular polynomial and elements */ + +#define WPOLY 0x011b +#define BPOLY 0x1b + +/* multiply four bytes in GF(2^8) by 'x' {02} in parallel */ + +#define gf_c1 0x80808080 +#define gf_c2 0x7f7f7f7f +#define gf_mulx(x) ((((x) & gf_c2) << 1) ^ ((((x) & gf_c1) >> 7) * BPOLY)) + +/* The following defines provide alternative definitions of gf_mulx that might + give improved performance if a fast 32-bit multiply is not available. Note + that a temporary variable u needs to be defined where gf_mulx is used. + +#define gf_mulx(x) (u = (x) & gf_c1, u |= (u >> 1), ((x) & gf_c2) << 1) ^ ((u >> 3) | (u >> 6)) +#define gf_c4 (0x01010101 * BPOLY) +#define gf_mulx(x) (u = (x) & gf_c1, ((x) & gf_c2) << 1) ^ ((u - (u >> 7)) & gf_c4) +*/ + +/* Work out which tables are needed for the different options */ + +#if defined( ASM_X86_V1C ) +# if defined( ENC_ROUND ) +# undef ENC_ROUND +# endif +# define ENC_ROUND FOUR_TABLES +# if defined( LAST_ENC_ROUND ) +# undef LAST_ENC_ROUND +# endif +# define LAST_ENC_ROUND FOUR_TABLES +# if defined( DEC_ROUND ) +# undef DEC_ROUND +# endif +# define DEC_ROUND FOUR_TABLES +# if defined( LAST_DEC_ROUND ) +# undef LAST_DEC_ROUND +# endif +# define LAST_DEC_ROUND FOUR_TABLES +# if defined( KEY_SCHED ) +# undef KEY_SCHED +# define KEY_SCHED FOUR_TABLES +# endif +#endif + +#if ( FUNCS_IN_C & ENCRYPTION_IN_C ) || defined( ASM_X86_V1C ) +# if ENC_ROUND == ONE_TABLE +# define FT1_SET +# elif ENC_ROUND == FOUR_TABLES +# define FT4_SET +# else +# define SBX_SET +# endif +# if LAST_ENC_ROUND == ONE_TABLE +# define FL1_SET +# elif LAST_ENC_ROUND == FOUR_TABLES +# define FL4_SET +# elif !defined( SBX_SET ) +# define SBX_SET +# endif +#endif + +#if ( FUNCS_IN_C & DECRYPTION_IN_C ) || defined( ASM_X86_V1C ) +# if DEC_ROUND == ONE_TABLE +# define IT1_SET +# elif DEC_ROUND == FOUR_TABLES +# define IT4_SET +# else +# define ISB_SET +# endif +# if LAST_DEC_ROUND == ONE_TABLE +# define IL1_SET +# elif LAST_DEC_ROUND == FOUR_TABLES +# define IL4_SET +# elif !defined(ISB_SET) +# define ISB_SET +# endif +#endif + +#if !(defined( REDUCE_CODE_SIZE ) && (defined( ASM_X86_V2 ) || defined( ASM_X86_V2C ))) +# if ((FUNCS_IN_C & ENC_KEYING_IN_C) || (FUNCS_IN_C & DEC_KEYING_IN_C)) +# if KEY_SCHED == ONE_TABLE +# if !defined( FL1_SET ) && !defined( FL4_SET ) +# define LS1_SET +# endif +# elif KEY_SCHED == FOUR_TABLES +# if !defined( FL4_SET ) +# define LS4_SET +# endif +# elif !defined( SBX_SET ) +# define SBX_SET +# endif +# endif +# if (FUNCS_IN_C & DEC_KEYING_IN_C) +# if KEY_SCHED == ONE_TABLE +# define IM1_SET +# elif KEY_SCHED == FOUR_TABLES +# define IM4_SET +# elif !defined( SBX_SET ) +# define SBX_SET +# endif +# endif +#endif + +/* generic definitions of Rijndael macros that use tables */ + +#define no_table(x,box,vf,rf,c) bytes2word( \ + box[bval(vf(x,0,c),rf(0,c))], \ + box[bval(vf(x,1,c),rf(1,c))], \ + box[bval(vf(x,2,c),rf(2,c))], \ + box[bval(vf(x,3,c),rf(3,c))]) + +#define one_table(x,op,tab,vf,rf,c) \ + ( tab[bval(vf(x,0,c),rf(0,c))] \ + ^ op(tab[bval(vf(x,1,c),rf(1,c))],1) \ + ^ op(tab[bval(vf(x,2,c),rf(2,c))],2) \ + ^ op(tab[bval(vf(x,3,c),rf(3,c))],3)) + +#define four_tables(x,tab,vf,rf,c) \ + ( tab[0][bval(vf(x,0,c),rf(0,c))] \ + ^ tab[1][bval(vf(x,1,c),rf(1,c))] \ + ^ tab[2][bval(vf(x,2,c),rf(2,c))] \ + ^ tab[3][bval(vf(x,3,c),rf(3,c))]) + +#define vf1(x,r,c) (x) +#define rf1(r,c) (r) +#define rf2(r,c) ((8+r-c)&3) + +/* perform forward and inverse column mix operation on four bytes in long word x in */ +/* parallel. NOTE: x must be a simple variable, NOT an expression in these macros. */ + +#if !(defined( REDUCE_CODE_SIZE ) && (defined( ASM_X86_V2 ) || defined( ASM_X86_V2C ))) + +#if defined( FM4_SET ) /* not currently used */ +# define fwd_mcol(x) four_tables(x,t_use(f,m),vf1,rf1,0) +#elif defined( FM1_SET ) /* not currently used */ +# define fwd_mcol(x) one_table(x,upr,t_use(f,m),vf1,rf1,0) +#else +# define dec_fmvars uint_32t g2 +# define fwd_mcol(x) (g2 = gf_mulx(x), g2 ^ upr((x) ^ g2, 3) ^ upr((x), 2) ^ upr((x), 1)) +#endif + +#if defined( IM4_SET ) +# define inv_mcol(x) four_tables(x,t_use(i,m),vf1,rf1,0) +#elif defined( IM1_SET ) +# define inv_mcol(x) one_table(x,upr,t_use(i,m),vf1,rf1,0) +#else +# define dec_imvars uint_32t g2, g4, g9 +# define inv_mcol(x) (g2 = gf_mulx(x), g4 = gf_mulx(g2), g9 = (x) ^ gf_mulx(g4), g4 ^= g9, \ + (x) ^ g2 ^ g4 ^ upr(g2 ^ g9, 3) ^ upr(g4, 2) ^ upr(g9, 1)) +#endif + +#if defined( FL4_SET ) +# define ls_box(x,c) four_tables(x,t_use(f,l),vf1,rf2,c) +#elif defined( LS4_SET ) +# define ls_box(x,c) four_tables(x,t_use(l,s),vf1,rf2,c) +#elif defined( FL1_SET ) +# define ls_box(x,c) one_table(x,upr,t_use(f,l),vf1,rf2,c) +#elif defined( LS1_SET ) +# define ls_box(x,c) one_table(x,upr,t_use(l,s),vf1,rf2,c) +#else +# define ls_box(x,c) no_table(x,t_use(s,box),vf1,rf2,c) +#endif + +#endif + +#if defined( ASM_X86_V1C ) && defined( AES_DECRYPT ) && !defined( ISB_SET ) +# define ISB_SET +#endif + +#endif diff --git a/Zip/aes/aestab.c b/Zip/minizip/aes/aestab.c similarity index 95% rename from Zip/aes/aestab.c rename to Zip/minizip/aes/aestab.c index 6d193aff26e1e352302c91e226916b420835b6da..9671a7d87ac5ea50e6455f402f36648b969e61ca 100755 --- a/Zip/aes/aestab.c +++ b/Zip/minizip/aes/aestab.c @@ -1,391 +1,391 @@ -/* ---------------------------------------------------------------------------- -Copyright (c) 1998-2010, Brian Gladman, Worcester, UK. All rights reserved. - -The redistribution and use of this software (with or without changes) -is allowed without the payment of fees or royalties provided that: - - source code distributions include the above copyright notice, this - list of conditions and the following disclaimer; - - binary distributions include the above copyright notice, this list - of conditions and the following disclaimer in their documentation. - -This software is provided 'as is' with no explicit or implied warranties -in respect of its operation, including, but not limited to, correctness -and fitness for purpose. ---------------------------------------------------------------------------- -Issue Date: 20/12/2007 -*/ - -#define DO_TABLES - -#include "aes.h" -#include "aesopt.h" - -#if defined(FIXED_TABLES) - -#define sb_data(w) {\ - w(0x63), w(0x7c), w(0x77), w(0x7b), w(0xf2), w(0x6b), w(0x6f), w(0xc5),\ - w(0x30), w(0x01), w(0x67), w(0x2b), w(0xfe), w(0xd7), w(0xab), w(0x76),\ - w(0xca), w(0x82), w(0xc9), w(0x7d), w(0xfa), w(0x59), w(0x47), w(0xf0),\ - w(0xad), w(0xd4), w(0xa2), w(0xaf), w(0x9c), w(0xa4), w(0x72), w(0xc0),\ - w(0xb7), w(0xfd), w(0x93), w(0x26), w(0x36), w(0x3f), w(0xf7), w(0xcc),\ - w(0x34), w(0xa5), w(0xe5), w(0xf1), w(0x71), w(0xd8), w(0x31), w(0x15),\ - w(0x04), w(0xc7), w(0x23), w(0xc3), w(0x18), w(0x96), w(0x05), w(0x9a),\ - w(0x07), w(0x12), w(0x80), w(0xe2), w(0xeb), w(0x27), w(0xb2), w(0x75),\ - w(0x09), w(0x83), w(0x2c), w(0x1a), w(0x1b), w(0x6e), w(0x5a), w(0xa0),\ - w(0x52), w(0x3b), w(0xd6), w(0xb3), w(0x29), w(0xe3), w(0x2f), w(0x84),\ - w(0x53), w(0xd1), w(0x00), w(0xed), w(0x20), w(0xfc), w(0xb1), w(0x5b),\ - w(0x6a), w(0xcb), w(0xbe), w(0x39), w(0x4a), w(0x4c), w(0x58), w(0xcf),\ - w(0xd0), w(0xef), w(0xaa), w(0xfb), w(0x43), w(0x4d), w(0x33), w(0x85),\ - w(0x45), w(0xf9), w(0x02), w(0x7f), w(0x50), w(0x3c), w(0x9f), w(0xa8),\ - w(0x51), w(0xa3), w(0x40), w(0x8f), w(0x92), w(0x9d), w(0x38), w(0xf5),\ - w(0xbc), w(0xb6), w(0xda), w(0x21), w(0x10), w(0xff), w(0xf3), w(0xd2),\ - w(0xcd), w(0x0c), w(0x13), w(0xec), w(0x5f), w(0x97), w(0x44), w(0x17),\ - w(0xc4), w(0xa7), w(0x7e), w(0x3d), w(0x64), w(0x5d), w(0x19), w(0x73),\ - w(0x60), w(0x81), w(0x4f), w(0xdc), w(0x22), w(0x2a), w(0x90), w(0x88),\ - w(0x46), w(0xee), w(0xb8), w(0x14), w(0xde), w(0x5e), w(0x0b), w(0xdb),\ - w(0xe0), w(0x32), w(0x3a), w(0x0a), w(0x49), w(0x06), w(0x24), w(0x5c),\ - w(0xc2), w(0xd3), w(0xac), w(0x62), w(0x91), w(0x95), w(0xe4), w(0x79),\ - w(0xe7), w(0xc8), w(0x37), w(0x6d), w(0x8d), w(0xd5), w(0x4e), w(0xa9),\ - w(0x6c), w(0x56), w(0xf4), w(0xea), w(0x65), w(0x7a), w(0xae), w(0x08),\ - w(0xba), w(0x78), w(0x25), w(0x2e), w(0x1c), w(0xa6), w(0xb4), w(0xc6),\ - w(0xe8), w(0xdd), w(0x74), w(0x1f), w(0x4b), w(0xbd), w(0x8b), w(0x8a),\ - w(0x70), w(0x3e), w(0xb5), w(0x66), w(0x48), w(0x03), w(0xf6), w(0x0e),\ - w(0x61), w(0x35), w(0x57), w(0xb9), w(0x86), w(0xc1), w(0x1d), w(0x9e),\ - w(0xe1), w(0xf8), w(0x98), w(0x11), w(0x69), w(0xd9), w(0x8e), w(0x94),\ - w(0x9b), w(0x1e), w(0x87), w(0xe9), w(0xce), w(0x55), w(0x28), w(0xdf),\ - w(0x8c), w(0xa1), w(0x89), w(0x0d), w(0xbf), w(0xe6), w(0x42), w(0x68),\ - w(0x41), w(0x99), w(0x2d), w(0x0f), w(0xb0), w(0x54), w(0xbb), w(0x16) } - -#define isb_data(w) {\ - w(0x52), w(0x09), w(0x6a), w(0xd5), w(0x30), w(0x36), w(0xa5), w(0x38),\ - w(0xbf), w(0x40), w(0xa3), w(0x9e), w(0x81), w(0xf3), w(0xd7), w(0xfb),\ - w(0x7c), w(0xe3), w(0x39), w(0x82), w(0x9b), w(0x2f), w(0xff), w(0x87),\ - w(0x34), w(0x8e), w(0x43), w(0x44), w(0xc4), w(0xde), w(0xe9), w(0xcb),\ - w(0x54), w(0x7b), w(0x94), w(0x32), w(0xa6), w(0xc2), w(0x23), w(0x3d),\ - w(0xee), w(0x4c), w(0x95), w(0x0b), w(0x42), w(0xfa), w(0xc3), w(0x4e),\ - w(0x08), w(0x2e), w(0xa1), w(0x66), w(0x28), w(0xd9), w(0x24), w(0xb2),\ - w(0x76), w(0x5b), w(0xa2), w(0x49), w(0x6d), w(0x8b), w(0xd1), w(0x25),\ - w(0x72), w(0xf8), w(0xf6), w(0x64), w(0x86), w(0x68), w(0x98), w(0x16),\ - w(0xd4), w(0xa4), w(0x5c), w(0xcc), w(0x5d), w(0x65), w(0xb6), w(0x92),\ - w(0x6c), w(0x70), w(0x48), w(0x50), w(0xfd), w(0xed), w(0xb9), w(0xda),\ - w(0x5e), w(0x15), w(0x46), w(0x57), w(0xa7), w(0x8d), w(0x9d), w(0x84),\ - w(0x90), w(0xd8), w(0xab), w(0x00), w(0x8c), w(0xbc), w(0xd3), w(0x0a),\ - w(0xf7), w(0xe4), w(0x58), w(0x05), w(0xb8), w(0xb3), w(0x45), w(0x06),\ - w(0xd0), w(0x2c), w(0x1e), w(0x8f), w(0xca), w(0x3f), w(0x0f), w(0x02),\ - w(0xc1), w(0xaf), w(0xbd), w(0x03), w(0x01), w(0x13), w(0x8a), w(0x6b),\ - w(0x3a), w(0x91), w(0x11), w(0x41), w(0x4f), w(0x67), w(0xdc), w(0xea),\ - w(0x97), w(0xf2), w(0xcf), w(0xce), w(0xf0), w(0xb4), w(0xe6), w(0x73),\ - w(0x96), w(0xac), w(0x74), w(0x22), w(0xe7), w(0xad), w(0x35), w(0x85),\ - w(0xe2), w(0xf9), w(0x37), w(0xe8), w(0x1c), w(0x75), w(0xdf), w(0x6e),\ - w(0x47), w(0xf1), w(0x1a), w(0x71), w(0x1d), w(0x29), w(0xc5), w(0x89),\ - w(0x6f), w(0xb7), w(0x62), w(0x0e), w(0xaa), w(0x18), w(0xbe), w(0x1b),\ - w(0xfc), w(0x56), w(0x3e), w(0x4b), w(0xc6), w(0xd2), w(0x79), w(0x20),\ - w(0x9a), w(0xdb), w(0xc0), w(0xfe), w(0x78), w(0xcd), w(0x5a), w(0xf4),\ - w(0x1f), w(0xdd), w(0xa8), w(0x33), w(0x88), w(0x07), w(0xc7), w(0x31),\ - w(0xb1), w(0x12), w(0x10), w(0x59), w(0x27), w(0x80), w(0xec), w(0x5f),\ - w(0x60), w(0x51), w(0x7f), w(0xa9), w(0x19), w(0xb5), w(0x4a), w(0x0d),\ - w(0x2d), w(0xe5), w(0x7a), w(0x9f), w(0x93), w(0xc9), w(0x9c), w(0xef),\ - w(0xa0), w(0xe0), w(0x3b), w(0x4d), w(0xae), w(0x2a), w(0xf5), w(0xb0),\ - w(0xc8), w(0xeb), w(0xbb), w(0x3c), w(0x83), w(0x53), w(0x99), w(0x61),\ - w(0x17), w(0x2b), w(0x04), w(0x7e), w(0xba), w(0x77), w(0xd6), w(0x26),\ - w(0xe1), w(0x69), w(0x14), w(0x63), w(0x55), w(0x21), w(0x0c), w(0x7d) } - -#define mm_data(w) {\ - w(0x00), w(0x01), w(0x02), w(0x03), w(0x04), w(0x05), w(0x06), w(0x07),\ - w(0x08), w(0x09), w(0x0a), w(0x0b), w(0x0c), w(0x0d), w(0x0e), w(0x0f),\ - w(0x10), w(0x11), w(0x12), w(0x13), w(0x14), w(0x15), w(0x16), w(0x17),\ - w(0x18), w(0x19), w(0x1a), w(0x1b), w(0x1c), w(0x1d), w(0x1e), w(0x1f),\ - w(0x20), w(0x21), w(0x22), w(0x23), w(0x24), w(0x25), w(0x26), w(0x27),\ - w(0x28), w(0x29), w(0x2a), w(0x2b), w(0x2c), w(0x2d), w(0x2e), w(0x2f),\ - w(0x30), w(0x31), w(0x32), w(0x33), w(0x34), w(0x35), w(0x36), w(0x37),\ - w(0x38), w(0x39), w(0x3a), w(0x3b), w(0x3c), w(0x3d), w(0x3e), w(0x3f),\ - w(0x40), w(0x41), w(0x42), w(0x43), w(0x44), w(0x45), w(0x46), w(0x47),\ - w(0x48), w(0x49), w(0x4a), w(0x4b), w(0x4c), w(0x4d), w(0x4e), w(0x4f),\ - w(0x50), w(0x51), w(0x52), w(0x53), w(0x54), w(0x55), w(0x56), w(0x57),\ - w(0x58), w(0x59), w(0x5a), w(0x5b), w(0x5c), w(0x5d), w(0x5e), w(0x5f),\ - w(0x60), w(0x61), w(0x62), w(0x63), w(0x64), w(0x65), w(0x66), w(0x67),\ - w(0x68), w(0x69), w(0x6a), w(0x6b), w(0x6c), w(0x6d), w(0x6e), w(0x6f),\ - w(0x70), w(0x71), w(0x72), w(0x73), w(0x74), w(0x75), w(0x76), w(0x77),\ - w(0x78), w(0x79), w(0x7a), w(0x7b), w(0x7c), w(0x7d), w(0x7e), w(0x7f),\ - w(0x80), w(0x81), w(0x82), w(0x83), w(0x84), w(0x85), w(0x86), w(0x87),\ - w(0x88), w(0x89), w(0x8a), w(0x8b), w(0x8c), w(0x8d), w(0x8e), w(0x8f),\ - w(0x90), w(0x91), w(0x92), w(0x93), w(0x94), w(0x95), w(0x96), w(0x97),\ - w(0x98), w(0x99), w(0x9a), w(0x9b), w(0x9c), w(0x9d), w(0x9e), w(0x9f),\ - w(0xa0), w(0xa1), w(0xa2), w(0xa3), w(0xa4), w(0xa5), w(0xa6), w(0xa7),\ - w(0xa8), w(0xa9), w(0xaa), w(0xab), w(0xac), w(0xad), w(0xae), w(0xaf),\ - w(0xb0), w(0xb1), w(0xb2), w(0xb3), w(0xb4), w(0xb5), w(0xb6), w(0xb7),\ - w(0xb8), w(0xb9), w(0xba), w(0xbb), w(0xbc), w(0xbd), w(0xbe), w(0xbf),\ - w(0xc0), w(0xc1), w(0xc2), w(0xc3), w(0xc4), w(0xc5), w(0xc6), w(0xc7),\ - w(0xc8), w(0xc9), w(0xca), w(0xcb), w(0xcc), w(0xcd), w(0xce), w(0xcf),\ - w(0xd0), w(0xd1), w(0xd2), w(0xd3), w(0xd4), w(0xd5), w(0xd6), w(0xd7),\ - w(0xd8), w(0xd9), w(0xda), w(0xdb), w(0xdc), w(0xdd), w(0xde), w(0xdf),\ - w(0xe0), w(0xe1), w(0xe2), w(0xe3), w(0xe4), w(0xe5), w(0xe6), w(0xe7),\ - w(0xe8), w(0xe9), w(0xea), w(0xeb), w(0xec), w(0xed), w(0xee), w(0xef),\ - w(0xf0), w(0xf1), w(0xf2), w(0xf3), w(0xf4), w(0xf5), w(0xf6), w(0xf7),\ - w(0xf8), w(0xf9), w(0xfa), w(0xfb), w(0xfc), w(0xfd), w(0xfe), w(0xff) } - -#define rc_data(w) {\ - w(0x01), w(0x02), w(0x04), w(0x08), w(0x10),w(0x20), w(0x40), w(0x80),\ - w(0x1b), w(0x36) } - -#define h0(x) (x) - -#define w0(p) bytes2word(p, 0, 0, 0) -#define w1(p) bytes2word(0, p, 0, 0) -#define w2(p) bytes2word(0, 0, p, 0) -#define w3(p) bytes2word(0, 0, 0, p) - -#define u0(p) bytes2word(f2(p), p, p, f3(p)) -#define u1(p) bytes2word(f3(p), f2(p), p, p) -#define u2(p) bytes2word(p, f3(p), f2(p), p) -#define u3(p) bytes2word(p, p, f3(p), f2(p)) - -#define v0(p) bytes2word(fe(p), f9(p), fd(p), fb(p)) -#define v1(p) bytes2word(fb(p), fe(p), f9(p), fd(p)) -#define v2(p) bytes2word(fd(p), fb(p), fe(p), f9(p)) -#define v3(p) bytes2word(f9(p), fd(p), fb(p), fe(p)) - -#endif - -#if defined(FIXED_TABLES) || !defined(FF_TABLES) - -#define f2(x) ((x<<1) ^ (((x>>7) & 1) * WPOLY)) -#define f4(x) ((x<<2) ^ (((x>>6) & 1) * WPOLY) ^ (((x>>6) & 2) * WPOLY)) -#define f8(x) ((x<<3) ^ (((x>>5) & 1) * WPOLY) ^ (((x>>5) & 2) * WPOLY) \ - ^ (((x>>5) & 4) * WPOLY)) -#define f3(x) (f2(x) ^ x) -#define f9(x) (f8(x) ^ x) -#define fb(x) (f8(x) ^ f2(x) ^ x) -#define fd(x) (f8(x) ^ f4(x) ^ x) -#define fe(x) (f8(x) ^ f4(x) ^ f2(x)) - -#else - -#define f2(x) ((x) ? pow[log[x] + 0x19] : 0) -#define f3(x) ((x) ? pow[log[x] + 0x01] : 0) -#define f9(x) ((x) ? pow[log[x] + 0xc7] : 0) -#define fb(x) ((x) ? pow[log[x] + 0x68] : 0) -#define fd(x) ((x) ? pow[log[x] + 0xee] : 0) -#define fe(x) ((x) ? pow[log[x] + 0xdf] : 0) - -#endif - -#include "aestab.h" - -#if defined(__cplusplus) -extern "C" -{ -#endif - -#if defined(FIXED_TABLES) - -/* implemented in case of wrong call for fixed tables */ - -AES_RETURN aes_init(void) -{ - return EXIT_SUCCESS; -} - -#else /* Generate the tables for the dynamic table option */ - -#if defined(FF_TABLES) - -#define gf_inv(x) ((x) ? pow[ 255 - log[x]] : 0) - -#else - -/* It will generally be sensible to use tables to compute finite - field multiplies and inverses but where memory is scarse this - code might sometimes be better. But it only has effect during - initialisation so its pretty unimportant in overall terms. -*/ - -/* return 2 ^ (n - 1) where n is the bit number of the highest bit - set in x with x in the range 1 < x < 0x00000200. This form is - used so that locals within fi can be bytes rather than words -*/ - -static uint_8t hibit(const uint_32t x) -{ uint_8t r = (uint_8t)((x >> 1) | (x >> 2)); - - r |= (r >> 2); - r |= (r >> 4); - return (r + 1) >> 1; -} - -/* return the inverse of the finite field element x */ - -static uint_8t gf_inv(const uint_8t x) -{ uint_8t p1 = x, p2 = BPOLY, n1 = hibit(x), n2 = 0x80, v1 = 1, v2 = 0; - - if(x < 2) - return x; - - for( ; ; ) - { - if(n1) - while(n2 >= n1) /* divide polynomial p2 by p1 */ - { - n2 /= n1; /* shift smaller polynomial left */ - p2 ^= (p1 * n2) & 0xff; /* and remove from larger one */ - v2 ^= v1 * n2; /* shift accumulated value and */ - n2 = hibit(p2); /* add into result */ - } - else - return v1; - - if(n2) /* repeat with values swapped */ - while(n1 >= n2) - { - n1 /= n2; - p1 ^= p2 * n1; - v1 ^= v2 * n1; - n1 = hibit(p1); - } - else - return v2; - } -} - -#endif - -/* The forward and inverse affine transformations used in the S-box */ -uint_8t fwd_affine(const uint_8t x) -{ uint_32t w = x; - w ^= (w << 1) ^ (w << 2) ^ (w << 3) ^ (w << 4); - return 0x63 ^ ((w ^ (w >> 8)) & 0xff); -} - -uint_8t inv_affine(const uint_8t x) -{ uint_32t w = x; - w = (w << 1) ^ (w << 3) ^ (w << 6); - return 0x05 ^ ((w ^ (w >> 8)) & 0xff); -} - -static int init = 0; - -AES_RETURN aes_init(void) -{ uint_32t i, w; - -#if defined(FF_TABLES) - - uint_8t pow[512], log[256]; - - if(init) - return EXIT_SUCCESS; - /* log and power tables for GF(2^8) finite field with - WPOLY as modular polynomial - the simplest primitive - root is 0x03, used here to generate the tables - */ - - i = 0; w = 1; - do - { - pow[i] = (uint_8t)w; - pow[i + 255] = (uint_8t)w; - log[w] = (uint_8t)i++; - w ^= (w << 1) ^ (w & 0x80 ? WPOLY : 0); - } - while (w != 1); - -#else - if(init) - return EXIT_SUCCESS; -#endif - - for(i = 0, w = 1; i < RC_LENGTH; ++i) - { - t_set(r,c)[i] = bytes2word(w, 0, 0, 0); - w = f2(w); - } - - for(i = 0; i < 256; ++i) - { uint_8t b; - - b = fwd_affine(gf_inv((uint_8t)i)); - w = bytes2word(f2(b), b, b, f3(b)); - -#if defined( SBX_SET ) - t_set(s,box)[i] = b; -#endif - -#if defined( FT1_SET ) /* tables for a normal encryption round */ - t_set(f,n)[i] = w; -#endif -#if defined( FT4_SET ) - t_set(f,n)[0][i] = w; - t_set(f,n)[1][i] = upr(w,1); - t_set(f,n)[2][i] = upr(w,2); - t_set(f,n)[3][i] = upr(w,3); -#endif - w = bytes2word(b, 0, 0, 0); - -#if defined( FL1_SET ) /* tables for last encryption round (may also */ - t_set(f,l)[i] = w; /* be used in the key schedule) */ -#endif -#if defined( FL4_SET ) - t_set(f,l)[0][i] = w; - t_set(f,l)[1][i] = upr(w,1); - t_set(f,l)[2][i] = upr(w,2); - t_set(f,l)[3][i] = upr(w,3); -#endif - -#if defined( LS1_SET ) /* table for key schedule if t_set(f,l) above is*/ - t_set(l,s)[i] = w; /* not of the required form */ -#endif -#if defined( LS4_SET ) - t_set(l,s)[0][i] = w; - t_set(l,s)[1][i] = upr(w,1); - t_set(l,s)[2][i] = upr(w,2); - t_set(l,s)[3][i] = upr(w,3); -#endif - - b = gf_inv(inv_affine((uint_8t)i)); - w = bytes2word(fe(b), f9(b), fd(b), fb(b)); - -#if defined( IM1_SET ) /* tables for the inverse mix column operation */ - t_set(i,m)[b] = w; -#endif -#if defined( IM4_SET ) - t_set(i,m)[0][b] = w; - t_set(i,m)[1][b] = upr(w,1); - t_set(i,m)[2][b] = upr(w,2); - t_set(i,m)[3][b] = upr(w,3); -#endif - -#if defined( ISB_SET ) - t_set(i,box)[i] = b; -#endif -#if defined( IT1_SET ) /* tables for a normal decryption round */ - t_set(i,n)[i] = w; -#endif -#if defined( IT4_SET ) - t_set(i,n)[0][i] = w; - t_set(i,n)[1][i] = upr(w,1); - t_set(i,n)[2][i] = upr(w,2); - t_set(i,n)[3][i] = upr(w,3); -#endif - w = bytes2word(b, 0, 0, 0); -#if defined( IL1_SET ) /* tables for last decryption round */ - t_set(i,l)[i] = w; -#endif -#if defined( IL4_SET ) - t_set(i,l)[0][i] = w; - t_set(i,l)[1][i] = upr(w,1); - t_set(i,l)[2][i] = upr(w,2); - t_set(i,l)[3][i] = upr(w,3); -#endif - } - init = 1; - return EXIT_SUCCESS; -} - -#endif - -#if defined(__cplusplus) -} -#endif - +/* +--------------------------------------------------------------------------- +Copyright (c) 1998-2010, Brian Gladman, Worcester, UK. All rights reserved. + +The redistribution and use of this software (with or without changes) +is allowed without the payment of fees or royalties provided that: + + source code distributions include the above copyright notice, this + list of conditions and the following disclaimer; + + binary distributions include the above copyright notice, this list + of conditions and the following disclaimer in their documentation. + +This software is provided 'as is' with no explicit or implied warranties +in respect of its operation, including, but not limited to, correctness +and fitness for purpose. +--------------------------------------------------------------------------- +Issue Date: 20/12/2007 +*/ + +#define DO_TABLES + +#include "aes.h" +#include "aesopt.h" + +#if defined(FIXED_TABLES) + +#define sb_data(w) {\ + w(0x63), w(0x7c), w(0x77), w(0x7b), w(0xf2), w(0x6b), w(0x6f), w(0xc5),\ + w(0x30), w(0x01), w(0x67), w(0x2b), w(0xfe), w(0xd7), w(0xab), w(0x76),\ + w(0xca), w(0x82), w(0xc9), w(0x7d), w(0xfa), w(0x59), w(0x47), w(0xf0),\ + w(0xad), w(0xd4), w(0xa2), w(0xaf), w(0x9c), w(0xa4), w(0x72), w(0xc0),\ + w(0xb7), w(0xfd), w(0x93), w(0x26), w(0x36), w(0x3f), w(0xf7), w(0xcc),\ + w(0x34), w(0xa5), w(0xe5), w(0xf1), w(0x71), w(0xd8), w(0x31), w(0x15),\ + w(0x04), w(0xc7), w(0x23), w(0xc3), w(0x18), w(0x96), w(0x05), w(0x9a),\ + w(0x07), w(0x12), w(0x80), w(0xe2), w(0xeb), w(0x27), w(0xb2), w(0x75),\ + w(0x09), w(0x83), w(0x2c), w(0x1a), w(0x1b), w(0x6e), w(0x5a), w(0xa0),\ + w(0x52), w(0x3b), w(0xd6), w(0xb3), w(0x29), w(0xe3), w(0x2f), w(0x84),\ + w(0x53), w(0xd1), w(0x00), w(0xed), w(0x20), w(0xfc), w(0xb1), w(0x5b),\ + w(0x6a), w(0xcb), w(0xbe), w(0x39), w(0x4a), w(0x4c), w(0x58), w(0xcf),\ + w(0xd0), w(0xef), w(0xaa), w(0xfb), w(0x43), w(0x4d), w(0x33), w(0x85),\ + w(0x45), w(0xf9), w(0x02), w(0x7f), w(0x50), w(0x3c), w(0x9f), w(0xa8),\ + w(0x51), w(0xa3), w(0x40), w(0x8f), w(0x92), w(0x9d), w(0x38), w(0xf5),\ + w(0xbc), w(0xb6), w(0xda), w(0x21), w(0x10), w(0xff), w(0xf3), w(0xd2),\ + w(0xcd), w(0x0c), w(0x13), w(0xec), w(0x5f), w(0x97), w(0x44), w(0x17),\ + w(0xc4), w(0xa7), w(0x7e), w(0x3d), w(0x64), w(0x5d), w(0x19), w(0x73),\ + w(0x60), w(0x81), w(0x4f), w(0xdc), w(0x22), w(0x2a), w(0x90), w(0x88),\ + w(0x46), w(0xee), w(0xb8), w(0x14), w(0xde), w(0x5e), w(0x0b), w(0xdb),\ + w(0xe0), w(0x32), w(0x3a), w(0x0a), w(0x49), w(0x06), w(0x24), w(0x5c),\ + w(0xc2), w(0xd3), w(0xac), w(0x62), w(0x91), w(0x95), w(0xe4), w(0x79),\ + w(0xe7), w(0xc8), w(0x37), w(0x6d), w(0x8d), w(0xd5), w(0x4e), w(0xa9),\ + w(0x6c), w(0x56), w(0xf4), w(0xea), w(0x65), w(0x7a), w(0xae), w(0x08),\ + w(0xba), w(0x78), w(0x25), w(0x2e), w(0x1c), w(0xa6), w(0xb4), w(0xc6),\ + w(0xe8), w(0xdd), w(0x74), w(0x1f), w(0x4b), w(0xbd), w(0x8b), w(0x8a),\ + w(0x70), w(0x3e), w(0xb5), w(0x66), w(0x48), w(0x03), w(0xf6), w(0x0e),\ + w(0x61), w(0x35), w(0x57), w(0xb9), w(0x86), w(0xc1), w(0x1d), w(0x9e),\ + w(0xe1), w(0xf8), w(0x98), w(0x11), w(0x69), w(0xd9), w(0x8e), w(0x94),\ + w(0x9b), w(0x1e), w(0x87), w(0xe9), w(0xce), w(0x55), w(0x28), w(0xdf),\ + w(0x8c), w(0xa1), w(0x89), w(0x0d), w(0xbf), w(0xe6), w(0x42), w(0x68),\ + w(0x41), w(0x99), w(0x2d), w(0x0f), w(0xb0), w(0x54), w(0xbb), w(0x16) } + +#define isb_data(w) {\ + w(0x52), w(0x09), w(0x6a), w(0xd5), w(0x30), w(0x36), w(0xa5), w(0x38),\ + w(0xbf), w(0x40), w(0xa3), w(0x9e), w(0x81), w(0xf3), w(0xd7), w(0xfb),\ + w(0x7c), w(0xe3), w(0x39), w(0x82), w(0x9b), w(0x2f), w(0xff), w(0x87),\ + w(0x34), w(0x8e), w(0x43), w(0x44), w(0xc4), w(0xde), w(0xe9), w(0xcb),\ + w(0x54), w(0x7b), w(0x94), w(0x32), w(0xa6), w(0xc2), w(0x23), w(0x3d),\ + w(0xee), w(0x4c), w(0x95), w(0x0b), w(0x42), w(0xfa), w(0xc3), w(0x4e),\ + w(0x08), w(0x2e), w(0xa1), w(0x66), w(0x28), w(0xd9), w(0x24), w(0xb2),\ + w(0x76), w(0x5b), w(0xa2), w(0x49), w(0x6d), w(0x8b), w(0xd1), w(0x25),\ + w(0x72), w(0xf8), w(0xf6), w(0x64), w(0x86), w(0x68), w(0x98), w(0x16),\ + w(0xd4), w(0xa4), w(0x5c), w(0xcc), w(0x5d), w(0x65), w(0xb6), w(0x92),\ + w(0x6c), w(0x70), w(0x48), w(0x50), w(0xfd), w(0xed), w(0xb9), w(0xda),\ + w(0x5e), w(0x15), w(0x46), w(0x57), w(0xa7), w(0x8d), w(0x9d), w(0x84),\ + w(0x90), w(0xd8), w(0xab), w(0x00), w(0x8c), w(0xbc), w(0xd3), w(0x0a),\ + w(0xf7), w(0xe4), w(0x58), w(0x05), w(0xb8), w(0xb3), w(0x45), w(0x06),\ + w(0xd0), w(0x2c), w(0x1e), w(0x8f), w(0xca), w(0x3f), w(0x0f), w(0x02),\ + w(0xc1), w(0xaf), w(0xbd), w(0x03), w(0x01), w(0x13), w(0x8a), w(0x6b),\ + w(0x3a), w(0x91), w(0x11), w(0x41), w(0x4f), w(0x67), w(0xdc), w(0xea),\ + w(0x97), w(0xf2), w(0xcf), w(0xce), w(0xf0), w(0xb4), w(0xe6), w(0x73),\ + w(0x96), w(0xac), w(0x74), w(0x22), w(0xe7), w(0xad), w(0x35), w(0x85),\ + w(0xe2), w(0xf9), w(0x37), w(0xe8), w(0x1c), w(0x75), w(0xdf), w(0x6e),\ + w(0x47), w(0xf1), w(0x1a), w(0x71), w(0x1d), w(0x29), w(0xc5), w(0x89),\ + w(0x6f), w(0xb7), w(0x62), w(0x0e), w(0xaa), w(0x18), w(0xbe), w(0x1b),\ + w(0xfc), w(0x56), w(0x3e), w(0x4b), w(0xc6), w(0xd2), w(0x79), w(0x20),\ + w(0x9a), w(0xdb), w(0xc0), w(0xfe), w(0x78), w(0xcd), w(0x5a), w(0xf4),\ + w(0x1f), w(0xdd), w(0xa8), w(0x33), w(0x88), w(0x07), w(0xc7), w(0x31),\ + w(0xb1), w(0x12), w(0x10), w(0x59), w(0x27), w(0x80), w(0xec), w(0x5f),\ + w(0x60), w(0x51), w(0x7f), w(0xa9), w(0x19), w(0xb5), w(0x4a), w(0x0d),\ + w(0x2d), w(0xe5), w(0x7a), w(0x9f), w(0x93), w(0xc9), w(0x9c), w(0xef),\ + w(0xa0), w(0xe0), w(0x3b), w(0x4d), w(0xae), w(0x2a), w(0xf5), w(0xb0),\ + w(0xc8), w(0xeb), w(0xbb), w(0x3c), w(0x83), w(0x53), w(0x99), w(0x61),\ + w(0x17), w(0x2b), w(0x04), w(0x7e), w(0xba), w(0x77), w(0xd6), w(0x26),\ + w(0xe1), w(0x69), w(0x14), w(0x63), w(0x55), w(0x21), w(0x0c), w(0x7d) } + +#define mm_data(w) {\ + w(0x00), w(0x01), w(0x02), w(0x03), w(0x04), w(0x05), w(0x06), w(0x07),\ + w(0x08), w(0x09), w(0x0a), w(0x0b), w(0x0c), w(0x0d), w(0x0e), w(0x0f),\ + w(0x10), w(0x11), w(0x12), w(0x13), w(0x14), w(0x15), w(0x16), w(0x17),\ + w(0x18), w(0x19), w(0x1a), w(0x1b), w(0x1c), w(0x1d), w(0x1e), w(0x1f),\ + w(0x20), w(0x21), w(0x22), w(0x23), w(0x24), w(0x25), w(0x26), w(0x27),\ + w(0x28), w(0x29), w(0x2a), w(0x2b), w(0x2c), w(0x2d), w(0x2e), w(0x2f),\ + w(0x30), w(0x31), w(0x32), w(0x33), w(0x34), w(0x35), w(0x36), w(0x37),\ + w(0x38), w(0x39), w(0x3a), w(0x3b), w(0x3c), w(0x3d), w(0x3e), w(0x3f),\ + w(0x40), w(0x41), w(0x42), w(0x43), w(0x44), w(0x45), w(0x46), w(0x47),\ + w(0x48), w(0x49), w(0x4a), w(0x4b), w(0x4c), w(0x4d), w(0x4e), w(0x4f),\ + w(0x50), w(0x51), w(0x52), w(0x53), w(0x54), w(0x55), w(0x56), w(0x57),\ + w(0x58), w(0x59), w(0x5a), w(0x5b), w(0x5c), w(0x5d), w(0x5e), w(0x5f),\ + w(0x60), w(0x61), w(0x62), w(0x63), w(0x64), w(0x65), w(0x66), w(0x67),\ + w(0x68), w(0x69), w(0x6a), w(0x6b), w(0x6c), w(0x6d), w(0x6e), w(0x6f),\ + w(0x70), w(0x71), w(0x72), w(0x73), w(0x74), w(0x75), w(0x76), w(0x77),\ + w(0x78), w(0x79), w(0x7a), w(0x7b), w(0x7c), w(0x7d), w(0x7e), w(0x7f),\ + w(0x80), w(0x81), w(0x82), w(0x83), w(0x84), w(0x85), w(0x86), w(0x87),\ + w(0x88), w(0x89), w(0x8a), w(0x8b), w(0x8c), w(0x8d), w(0x8e), w(0x8f),\ + w(0x90), w(0x91), w(0x92), w(0x93), w(0x94), w(0x95), w(0x96), w(0x97),\ + w(0x98), w(0x99), w(0x9a), w(0x9b), w(0x9c), w(0x9d), w(0x9e), w(0x9f),\ + w(0xa0), w(0xa1), w(0xa2), w(0xa3), w(0xa4), w(0xa5), w(0xa6), w(0xa7),\ + w(0xa8), w(0xa9), w(0xaa), w(0xab), w(0xac), w(0xad), w(0xae), w(0xaf),\ + w(0xb0), w(0xb1), w(0xb2), w(0xb3), w(0xb4), w(0xb5), w(0xb6), w(0xb7),\ + w(0xb8), w(0xb9), w(0xba), w(0xbb), w(0xbc), w(0xbd), w(0xbe), w(0xbf),\ + w(0xc0), w(0xc1), w(0xc2), w(0xc3), w(0xc4), w(0xc5), w(0xc6), w(0xc7),\ + w(0xc8), w(0xc9), w(0xca), w(0xcb), w(0xcc), w(0xcd), w(0xce), w(0xcf),\ + w(0xd0), w(0xd1), w(0xd2), w(0xd3), w(0xd4), w(0xd5), w(0xd6), w(0xd7),\ + w(0xd8), w(0xd9), w(0xda), w(0xdb), w(0xdc), w(0xdd), w(0xde), w(0xdf),\ + w(0xe0), w(0xe1), w(0xe2), w(0xe3), w(0xe4), w(0xe5), w(0xe6), w(0xe7),\ + w(0xe8), w(0xe9), w(0xea), w(0xeb), w(0xec), w(0xed), w(0xee), w(0xef),\ + w(0xf0), w(0xf1), w(0xf2), w(0xf3), w(0xf4), w(0xf5), w(0xf6), w(0xf7),\ + w(0xf8), w(0xf9), w(0xfa), w(0xfb), w(0xfc), w(0xfd), w(0xfe), w(0xff) } + +#define rc_data(w) {\ + w(0x01), w(0x02), w(0x04), w(0x08), w(0x10),w(0x20), w(0x40), w(0x80),\ + w(0x1b), w(0x36) } + +#define h0(x) (x) + +#define w0(p) bytes2word(p, 0, 0, 0) +#define w1(p) bytes2word(0, p, 0, 0) +#define w2(p) bytes2word(0, 0, p, 0) +#define w3(p) bytes2word(0, 0, 0, p) + +#define u0(p) bytes2word(f2(p), p, p, f3(p)) +#define u1(p) bytes2word(f3(p), f2(p), p, p) +#define u2(p) bytes2word(p, f3(p), f2(p), p) +#define u3(p) bytes2word(p, p, f3(p), f2(p)) + +#define v0(p) bytes2word(fe(p), f9(p), fd(p), fb(p)) +#define v1(p) bytes2word(fb(p), fe(p), f9(p), fd(p)) +#define v2(p) bytes2word(fd(p), fb(p), fe(p), f9(p)) +#define v3(p) bytes2word(f9(p), fd(p), fb(p), fe(p)) + +#endif + +#if defined(FIXED_TABLES) || !defined(FF_TABLES) + +#define f2(x) ((x<<1) ^ (((x>>7) & 1) * WPOLY)) +#define f4(x) ((x<<2) ^ (((x>>6) & 1) * WPOLY) ^ (((x>>6) & 2) * WPOLY)) +#define f8(x) ((x<<3) ^ (((x>>5) & 1) * WPOLY) ^ (((x>>5) & 2) * WPOLY) \ + ^ (((x>>5) & 4) * WPOLY)) +#define f3(x) (f2(x) ^ x) +#define f9(x) (f8(x) ^ x) +#define fb(x) (f8(x) ^ f2(x) ^ x) +#define fd(x) (f8(x) ^ f4(x) ^ x) +#define fe(x) (f8(x) ^ f4(x) ^ f2(x)) + +#else + +#define f2(x) ((x) ? pow[log[x] + 0x19] : 0) +#define f3(x) ((x) ? pow[log[x] + 0x01] : 0) +#define f9(x) ((x) ? pow[log[x] + 0xc7] : 0) +#define fb(x) ((x) ? pow[log[x] + 0x68] : 0) +#define fd(x) ((x) ? pow[log[x] + 0xee] : 0) +#define fe(x) ((x) ? pow[log[x] + 0xdf] : 0) + +#endif + +#include "aestab.h" + +#if defined(__cplusplus) +extern "C" +{ +#endif + +#if defined(FIXED_TABLES) + +/* implemented in case of wrong call for fixed tables */ + +AES_RETURN aes_init(void) +{ + return EXIT_SUCCESS; +} + +#else /* Generate the tables for the dynamic table option */ + +#if defined(FF_TABLES) + +#define gf_inv(x) ((x) ? pow[ 255 - log[x]] : 0) + +#else + +/* It will generally be sensible to use tables to compute finite + field multiplies and inverses but where memory is scarse this + code might sometimes be better. But it only has effect during + initialisation so its pretty unimportant in overall terms. +*/ + +/* return 2 ^ (n - 1) where n is the bit number of the highest bit + set in x with x in the range 1 < x < 0x00000200. This form is + used so that locals within fi can be bytes rather than words +*/ + +static uint_8t hibit(const uint_32t x) +{ uint_8t r = (uint_8t)((x >> 1) | (x >> 2)); + + r |= (r >> 2); + r |= (r >> 4); + return (r + 1) >> 1; +} + +/* return the inverse of the finite field element x */ + +static uint_8t gf_inv(const uint_8t x) +{ uint_8t p1 = x, p2 = BPOLY, n1 = hibit(x), n2 = 0x80, v1 = 1, v2 = 0; + + if(x < 2) + return x; + + for( ; ; ) + { + if(n1) + while(n2 >= n1) /* divide polynomial p2 by p1 */ + { + n2 /= n1; /* shift smaller polynomial left */ + p2 ^= (p1 * n2) & 0xff; /* and remove from larger one */ + v2 ^= v1 * n2; /* shift accumulated value and */ + n2 = hibit(p2); /* add into result */ + } + else + return v1; + + if(n2) /* repeat with values swapped */ + while(n1 >= n2) + { + n1 /= n2; + p1 ^= p2 * n1; + v1 ^= v2 * n1; + n1 = hibit(p1); + } + else + return v2; + } +} + +#endif + +/* The forward and inverse affine transformations used in the S-box */ +uint_8t fwd_affine(const uint_8t x) +{ uint_32t w = x; + w ^= (w << 1) ^ (w << 2) ^ (w << 3) ^ (w << 4); + return 0x63 ^ ((w ^ (w >> 8)) & 0xff); +} + +uint_8t inv_affine(const uint_8t x) +{ uint_32t w = x; + w = (w << 1) ^ (w << 3) ^ (w << 6); + return 0x05 ^ ((w ^ (w >> 8)) & 0xff); +} + +static int init = 0; + +AES_RETURN aes_init(void) +{ uint_32t i, w; + +#if defined(FF_TABLES) + + uint_8t pow[512], log[256]; + + if(init) + return EXIT_SUCCESS; + /* log and power tables for GF(2^8) finite field with + WPOLY as modular polynomial - the simplest primitive + root is 0x03, used here to generate the tables + */ + + i = 0; w = 1; + do + { + pow[i] = (uint_8t)w; + pow[i + 255] = (uint_8t)w; + log[w] = (uint_8t)i++; + w ^= (w << 1) ^ (w & 0x80 ? WPOLY : 0); + } + while (w != 1); + +#else + if(init) + return EXIT_SUCCESS; +#endif + + for(i = 0, w = 1; i < RC_LENGTH; ++i) + { + t_set(r,c)[i] = bytes2word(w, 0, 0, 0); + w = f2(w); + } + + for(i = 0; i < 256; ++i) + { uint_8t b; + + b = fwd_affine(gf_inv((uint_8t)i)); + w = bytes2word(f2(b), b, b, f3(b)); + +#if defined( SBX_SET ) + t_set(s,box)[i] = b; +#endif + +#if defined( FT1_SET ) /* tables for a normal encryption round */ + t_set(f,n)[i] = w; +#endif +#if defined( FT4_SET ) + t_set(f,n)[0][i] = w; + t_set(f,n)[1][i] = upr(w,1); + t_set(f,n)[2][i] = upr(w,2); + t_set(f,n)[3][i] = upr(w,3); +#endif + w = bytes2word(b, 0, 0, 0); + +#if defined( FL1_SET ) /* tables for last encryption round (may also */ + t_set(f,l)[i] = w; /* be used in the key schedule) */ +#endif +#if defined( FL4_SET ) + t_set(f,l)[0][i] = w; + t_set(f,l)[1][i] = upr(w,1); + t_set(f,l)[2][i] = upr(w,2); + t_set(f,l)[3][i] = upr(w,3); +#endif + +#if defined( LS1_SET ) /* table for key schedule if t_set(f,l) above is*/ + t_set(l,s)[i] = w; /* not of the required form */ +#endif +#if defined( LS4_SET ) + t_set(l,s)[0][i] = w; + t_set(l,s)[1][i] = upr(w,1); + t_set(l,s)[2][i] = upr(w,2); + t_set(l,s)[3][i] = upr(w,3); +#endif + + b = gf_inv(inv_affine((uint_8t)i)); + w = bytes2word(fe(b), f9(b), fd(b), fb(b)); + +#if defined( IM1_SET ) /* tables for the inverse mix column operation */ + t_set(i,m)[b] = w; +#endif +#if defined( IM4_SET ) + t_set(i,m)[0][b] = w; + t_set(i,m)[1][b] = upr(w,1); + t_set(i,m)[2][b] = upr(w,2); + t_set(i,m)[3][b] = upr(w,3); +#endif + +#if defined( ISB_SET ) + t_set(i,box)[i] = b; +#endif +#if defined( IT1_SET ) /* tables for a normal decryption round */ + t_set(i,n)[i] = w; +#endif +#if defined( IT4_SET ) + t_set(i,n)[0][i] = w; + t_set(i,n)[1][i] = upr(w,1); + t_set(i,n)[2][i] = upr(w,2); + t_set(i,n)[3][i] = upr(w,3); +#endif + w = bytes2word(b, 0, 0, 0); +#if defined( IL1_SET ) /* tables for last decryption round */ + t_set(i,l)[i] = w; +#endif +#if defined( IL4_SET ) + t_set(i,l)[0][i] = w; + t_set(i,l)[1][i] = upr(w,1); + t_set(i,l)[2][i] = upr(w,2); + t_set(i,l)[3][i] = upr(w,3); +#endif + } + init = 1; + return EXIT_SUCCESS; +} + +#endif + +#if defined(__cplusplus) +} +#endif + diff --git a/Zip/aes/aestab.h b/Zip/minizip/aes/aestab.h similarity index 96% rename from Zip/aes/aestab.h rename to Zip/minizip/aes/aestab.h index 21fc7361618d9ab940e76c49318f080969471d5e..de685679dc06adc692dbeb507fe9399107b4f9d1 100755 --- a/Zip/aes/aestab.h +++ b/Zip/minizip/aes/aestab.h @@ -1,173 +1,173 @@ -/* ---------------------------------------------------------------------------- -Copyright (c) 1998-2010, Brian Gladman, Worcester, UK. All rights reserved. - -The redistribution and use of this software (with or without changes) -is allowed without the payment of fees or royalties provided that: - - source code distributions include the above copyright notice, this - list of conditions and the following disclaimer; - - binary distributions include the above copyright notice, this list - of conditions and the following disclaimer in their documentation. - -This software is provided 'as is' with no explicit or implied warranties -in respect of its operation, including, but not limited to, correctness -and fitness for purpose. ---------------------------------------------------------------------------- -Issue Date: 20/12/2007 - - This file contains the code for declaring the tables needed to implement - AES. The file aesopt.h is assumed to be included before this header file. - If there are no global variables, the definitions here can be used to put - the AES tables in a structure so that a pointer can then be added to the - AES context to pass them to the AES routines that need them. If this - facility is used, the calling program has to ensure that this pointer is - managed appropriately. In particular, the value of the t_dec(in,it) item - in the table structure must be set to zero in order to ensure that the - tables are initialised. In practice the three code sequences in aeskey.c - that control the calls to aes_init() and the aes_init() routine itself will - have to be changed for a specific implementation. If global variables are - available it will generally be preferable to use them with the precomputed - FIXED_TABLES option that uses static global tables. - - The following defines can be used to control the way the tables - are defined, initialised and used in embedded environments that - require special features for these purposes - - the 't_dec' construction is used to declare fixed table arrays - the 't_set' construction is used to set fixed table values - the 't_use' construction is used to access fixed table values - - 256 byte tables: - - t_xxx(s,box) => forward S box - t_xxx(i,box) => inverse S box - - 256 32-bit word OR 4 x 256 32-bit word tables: - - t_xxx(f,n) => forward normal round - t_xxx(f,l) => forward last round - t_xxx(i,n) => inverse normal round - t_xxx(i,l) => inverse last round - t_xxx(l,s) => key schedule table - t_xxx(i,m) => key schedule table - - Other variables and tables: - - t_xxx(r,c) => the rcon table -*/ - -#if !defined( _AESTAB_H ) -#define _AESTAB_H - -#if defined(__cplusplus) -extern "C" { -#endif - -#define t_dec(m,n) t_##m##n -#define t_set(m,n) t_##m##n -#define t_use(m,n) t_##m##n - -#if defined(FIXED_TABLES) -# if !defined( __GNUC__ ) && (defined( __MSDOS__ ) || defined( __WIN16__ )) -/* make tables far data to avoid using too much DGROUP space (PG) */ -# define CONST const far -# else -# define CONST const -# endif -#else -# define CONST -#endif - -#if defined(DO_TABLES) -# define EXTERN -#else -# define EXTERN extern -#endif - -#if defined(_MSC_VER) && defined(TABLE_ALIGN) -#define ALIGN __declspec(align(TABLE_ALIGN)) -#else -#define ALIGN -#endif - -#if defined( __WATCOMC__ ) && ( __WATCOMC__ >= 1100 ) -# define XP_DIR __cdecl -#else -# define XP_DIR -#endif - -#if defined(DO_TABLES) && defined(FIXED_TABLES) -#define d_1(t,n,b,e) EXTERN ALIGN CONST XP_DIR t n[256] = b(e) -#define d_4(t,n,b,e,f,g,h) EXTERN ALIGN CONST XP_DIR t n[4][256] = { b(e), b(f), b(g), b(h) } -EXTERN ALIGN CONST uint_32t t_dec(r,c)[RC_LENGTH] = rc_data(w0); -#else -#define d_1(t,n,b,e) EXTERN ALIGN CONST XP_DIR t n[256] -#define d_4(t,n,b,e,f,g,h) EXTERN ALIGN CONST XP_DIR t n[4][256] -EXTERN ALIGN CONST uint_32t t_dec(r,c)[RC_LENGTH]; -#endif - -#if defined( SBX_SET ) - d_1(uint_8t, t_dec(s,box), sb_data, h0); -#endif -#if defined( ISB_SET ) - d_1(uint_8t, t_dec(i,box), isb_data, h0); -#endif - -#if defined( FT1_SET ) - d_1(uint_32t, t_dec(f,n), sb_data, u0); -#endif -#if defined( FT4_SET ) - d_4(uint_32t, t_dec(f,n), sb_data, u0, u1, u2, u3); -#endif - -#if defined( FL1_SET ) - d_1(uint_32t, t_dec(f,l), sb_data, w0); -#endif -#if defined( FL4_SET ) - d_4(uint_32t, t_dec(f,l), sb_data, w0, w1, w2, w3); -#endif - -#if defined( IT1_SET ) - d_1(uint_32t, t_dec(i,n), isb_data, v0); -#endif -#if defined( IT4_SET ) - d_4(uint_32t, t_dec(i,n), isb_data, v0, v1, v2, v3); -#endif - -#if defined( IL1_SET ) - d_1(uint_32t, t_dec(i,l), isb_data, w0); -#endif -#if defined( IL4_SET ) - d_4(uint_32t, t_dec(i,l), isb_data, w0, w1, w2, w3); -#endif - -#if defined( LS1_SET ) -#if defined( FL1_SET ) -#undef LS1_SET -#else - d_1(uint_32t, t_dec(l,s), sb_data, w0); -#endif -#endif - -#if defined( LS4_SET ) -#if defined( FL4_SET ) -#undef LS4_SET -#else - d_4(uint_32t, t_dec(l,s), sb_data, w0, w1, w2, w3); -#endif -#endif - -#if defined( IM1_SET ) - d_1(uint_32t, t_dec(i,m), mm_data, v0); -#endif -#if defined( IM4_SET ) - d_4(uint_32t, t_dec(i,m), mm_data, v0, v1, v2, v3); -#endif - -#if defined(__cplusplus) -} -#endif - -#endif +/* +--------------------------------------------------------------------------- +Copyright (c) 1998-2010, Brian Gladman, Worcester, UK. All rights reserved. + +The redistribution and use of this software (with or without changes) +is allowed without the payment of fees or royalties provided that: + + source code distributions include the above copyright notice, this + list of conditions and the following disclaimer; + + binary distributions include the above copyright notice, this list + of conditions and the following disclaimer in their documentation. + +This software is provided 'as is' with no explicit or implied warranties +in respect of its operation, including, but not limited to, correctness +and fitness for purpose. +--------------------------------------------------------------------------- +Issue Date: 20/12/2007 + + This file contains the code for declaring the tables needed to implement + AES. The file aesopt.h is assumed to be included before this header file. + If there are no global variables, the definitions here can be used to put + the AES tables in a structure so that a pointer can then be added to the + AES context to pass them to the AES routines that need them. If this + facility is used, the calling program has to ensure that this pointer is + managed appropriately. In particular, the value of the t_dec(in,it) item + in the table structure must be set to zero in order to ensure that the + tables are initialised. In practice the three code sequences in aeskey.c + that control the calls to aes_init() and the aes_init() routine itself will + have to be changed for a specific implementation. If global variables are + available it will generally be preferable to use them with the precomputed + FIXED_TABLES option that uses static global tables. + + The following defines can be used to control the way the tables + are defined, initialised and used in embedded environments that + require special features for these purposes + + the 't_dec' construction is used to declare fixed table arrays + the 't_set' construction is used to set fixed table values + the 't_use' construction is used to access fixed table values + + 256 byte tables: + + t_xxx(s,box) => forward S box + t_xxx(i,box) => inverse S box + + 256 32-bit word OR 4 x 256 32-bit word tables: + + t_xxx(f,n) => forward normal round + t_xxx(f,l) => forward last round + t_xxx(i,n) => inverse normal round + t_xxx(i,l) => inverse last round + t_xxx(l,s) => key schedule table + t_xxx(i,m) => key schedule table + + Other variables and tables: + + t_xxx(r,c) => the rcon table +*/ + +#if !defined( _AESTAB_H ) +#define _AESTAB_H + +#if defined(__cplusplus) +extern "C" { +#endif + +#define t_dec(m,n) t_##m##n +#define t_set(m,n) t_##m##n +#define t_use(m,n) t_##m##n + +#if defined(FIXED_TABLES) +# if !defined( __GNUC__ ) && (defined( __MSDOS__ ) || defined( __WIN16__ )) +/* make tables far data to avoid using too much DGROUP space (PG) */ +# define CONST const far +# else +# define CONST const +# endif +#else +# define CONST +#endif + +#if defined(DO_TABLES) +# define EXTERN +#else +# define EXTERN extern +#endif + +#if defined(_MSC_VER) && defined(TABLE_ALIGN) +#define ALIGN __declspec(align(TABLE_ALIGN)) +#else +#define ALIGN +#endif + +#if defined( __WATCOMC__ ) && ( __WATCOMC__ >= 1100 ) +# define XP_DIR __cdecl +#else +# define XP_DIR +#endif + +#if defined(DO_TABLES) && defined(FIXED_TABLES) +#define d_1(t,n,b,e) EXTERN ALIGN CONST XP_DIR t n[256] = b(e) +#define d_4(t,n,b,e,f,g,h) EXTERN ALIGN CONST XP_DIR t n[4][256] = { b(e), b(f), b(g), b(h) } +EXTERN ALIGN CONST uint_32t t_dec(r,c)[RC_LENGTH] = rc_data(w0); +#else +#define d_1(t,n,b,e) EXTERN ALIGN CONST XP_DIR t n[256] +#define d_4(t,n,b,e,f,g,h) EXTERN ALIGN CONST XP_DIR t n[4][256] +EXTERN ALIGN CONST uint_32t t_dec(r,c)[RC_LENGTH]; +#endif + +#if defined( SBX_SET ) + d_1(uint_8t, t_dec(s,box), sb_data, h0); +#endif +#if defined( ISB_SET ) + d_1(uint_8t, t_dec(i,box), isb_data, h0); +#endif + +#if defined( FT1_SET ) + d_1(uint_32t, t_dec(f,n), sb_data, u0); +#endif +#if defined( FT4_SET ) + d_4(uint_32t, t_dec(f,n), sb_data, u0, u1, u2, u3); +#endif + +#if defined( FL1_SET ) + d_1(uint_32t, t_dec(f,l), sb_data, w0); +#endif +#if defined( FL4_SET ) + d_4(uint_32t, t_dec(f,l), sb_data, w0, w1, w2, w3); +#endif + +#if defined( IT1_SET ) + d_1(uint_32t, t_dec(i,n), isb_data, v0); +#endif +#if defined( IT4_SET ) + d_4(uint_32t, t_dec(i,n), isb_data, v0, v1, v2, v3); +#endif + +#if defined( IL1_SET ) + d_1(uint_32t, t_dec(i,l), isb_data, w0); +#endif +#if defined( IL4_SET ) + d_4(uint_32t, t_dec(i,l), isb_data, w0, w1, w2, w3); +#endif + +#if defined( LS1_SET ) +#if defined( FL1_SET ) +#undef LS1_SET +#else + d_1(uint_32t, t_dec(l,s), sb_data, w0); +#endif +#endif + +#if defined( LS4_SET ) +#if defined( FL4_SET ) +#undef LS4_SET +#else + d_4(uint_32t, t_dec(l,s), sb_data, w0, w1, w2, w3); +#endif +#endif + +#if defined( IM1_SET ) + d_1(uint_32t, t_dec(i,m), mm_data, v0); +#endif +#if defined( IM4_SET ) + d_4(uint_32t, t_dec(i,m), mm_data, v0, v1, v2, v3); +#endif + +#if defined(__cplusplus) +} +#endif + +#endif diff --git a/Zip/aes/brg_endian.h b/Zip/minizip/aes/brg_endian.h similarity index 97% rename from Zip/aes/brg_endian.h rename to Zip/minizip/aes/brg_endian.h index 82e48f0bc48d236105fee108d32ef0ea089240b4..0f12fbbf2902423d2671ec6ecd00db72d084aa44 100755 --- a/Zip/aes/brg_endian.h +++ b/Zip/minizip/aes/brg_endian.h @@ -1,126 +1,126 @@ -/* ---------------------------------------------------------------------------- -Copyright (c) 1998-2010, Brian Gladman, Worcester, UK. All rights reserved. - -The redistribution and use of this software (with or without changes) -is allowed without the payment of fees or royalties provided that: - - source code distributions include the above copyright notice, this - list of conditions and the following disclaimer; - - binary distributions include the above copyright notice, this list - of conditions and the following disclaimer in their documentation. - -This software is provided 'as is' with no explicit or implied warranties -in respect of its operation, including, but not limited to, correctness -and fitness for purpose. ---------------------------------------------------------------------------- -Issue Date: 20/12/2007 -*/ - -#ifndef _BRG_ENDIAN_H -#define _BRG_ENDIAN_H - -#define IS_BIG_ENDIAN 4321 /* byte 0 is most significant (mc68k) */ -#define IS_LITTLE_ENDIAN 1234 /* byte 0 is least significant (i386) */ - -/* Include files where endian defines and byteswap functions may reside */ -#if defined( __sun ) -# include -#elif defined( __FreeBSD__ ) || defined( __OpenBSD__ ) || defined( __NetBSD__ ) -# include -#elif defined( BSD ) && ( BSD >= 199103 ) || defined( __APPLE__ ) || \ - defined( __CYGWIN32__ ) || defined( __DJGPP__ ) || defined( __osf__ ) -# include -#elif defined( __linux__ ) || defined( __GNUC__ ) || defined( __GNU_LIBRARY__ ) -# if !defined( __MINGW32__ ) && !defined( _AIX ) -# include -# if !defined( __BEOS__ ) -# include -# endif -# endif -#endif - -/* Now attempt to set the define for platform byte order using any */ -/* of the four forms SYMBOL, _SYMBOL, __SYMBOL & __SYMBOL__, which */ -/* seem to encompass most endian symbol definitions */ - -#if defined( BIG_ENDIAN ) && defined( LITTLE_ENDIAN ) -# if defined( BYTE_ORDER ) && BYTE_ORDER == BIG_ENDIAN -# define PLATFORM_BYTE_ORDER IS_BIG_ENDIAN -# elif defined( BYTE_ORDER ) && BYTE_ORDER == LITTLE_ENDIAN -# define PLATFORM_BYTE_ORDER IS_LITTLE_ENDIAN -# endif -#elif defined( BIG_ENDIAN ) -# define PLATFORM_BYTE_ORDER IS_BIG_ENDIAN -#elif defined( LITTLE_ENDIAN ) -# define PLATFORM_BYTE_ORDER IS_LITTLE_ENDIAN -#endif - -#if defined( _BIG_ENDIAN ) && defined( _LITTLE_ENDIAN ) -# if defined( _BYTE_ORDER ) && _BYTE_ORDER == _BIG_ENDIAN -# define PLATFORM_BYTE_ORDER IS_BIG_ENDIAN -# elif defined( _BYTE_ORDER ) && _BYTE_ORDER == _LITTLE_ENDIAN -# define PLATFORM_BYTE_ORDER IS_LITTLE_ENDIAN -# endif -#elif defined( _BIG_ENDIAN ) -# define PLATFORM_BYTE_ORDER IS_BIG_ENDIAN -#elif defined( _LITTLE_ENDIAN ) -# define PLATFORM_BYTE_ORDER IS_LITTLE_ENDIAN -#endif - -#if defined( __BIG_ENDIAN ) && defined( __LITTLE_ENDIAN ) -# if defined( __BYTE_ORDER ) && __BYTE_ORDER == __BIG_ENDIAN -# define PLATFORM_BYTE_ORDER IS_BIG_ENDIAN -# elif defined( __BYTE_ORDER ) && __BYTE_ORDER == __LITTLE_ENDIAN -# define PLATFORM_BYTE_ORDER IS_LITTLE_ENDIAN -# endif -#elif defined( __BIG_ENDIAN ) -# define PLATFORM_BYTE_ORDER IS_BIG_ENDIAN -#elif defined( __LITTLE_ENDIAN ) -# define PLATFORM_BYTE_ORDER IS_LITTLE_ENDIAN -#endif - -#if defined( __BIG_ENDIAN__ ) && defined( __LITTLE_ENDIAN__ ) -# if defined( __BYTE_ORDER__ ) && __BYTE_ORDER__ == __BIG_ENDIAN__ -# define PLATFORM_BYTE_ORDER IS_BIG_ENDIAN -# elif defined( __BYTE_ORDER__ ) && __BYTE_ORDER__ == __LITTLE_ENDIAN__ -# define PLATFORM_BYTE_ORDER IS_LITTLE_ENDIAN -# endif -#elif defined( __BIG_ENDIAN__ ) -# define PLATFORM_BYTE_ORDER IS_BIG_ENDIAN -#elif defined( __LITTLE_ENDIAN__ ) -# define PLATFORM_BYTE_ORDER IS_LITTLE_ENDIAN -#endif - -/* if the platform byte order could not be determined, then try to */ -/* set this define using common machine defines */ -#if !defined(PLATFORM_BYTE_ORDER) - -#if defined( __alpha__ ) || defined( __alpha ) || defined( i386 ) || \ - defined( __i386__ ) || defined( _M_I86 ) || defined( _M_IX86 ) || \ - defined( __OS2__ ) || defined( sun386 ) || defined( __TURBOC__ ) || \ - defined( vax ) || defined( vms ) || defined( VMS ) || \ - defined( __VMS ) || defined( _M_X64 ) -# define PLATFORM_BYTE_ORDER IS_LITTLE_ENDIAN - -#elif defined( AMIGA ) || defined( applec ) || defined( __AS400__ ) || \ - defined( _CRAY ) || defined( __hppa ) || defined( __hp9000 ) || \ - defined( ibm370 ) || defined( mc68000 ) || defined( m68k ) || \ - defined( __MRC__ ) || defined( __MVS__ ) || defined( __MWERKS__ ) || \ - defined( sparc ) || defined( __sparc) || defined( SYMANTEC_C ) || \ - defined( __VOS__ ) || defined( __TIGCC__ ) || defined( __TANDEM ) || \ - defined( THINK_C ) || defined( __VMCMS__ ) || defined( _AIX ) -# define PLATFORM_BYTE_ORDER IS_BIG_ENDIAN - -#elif 0 /* **** EDIT HERE IF NECESSARY **** */ -# define PLATFORM_BYTE_ORDER IS_LITTLE_ENDIAN -#elif 0 /* **** EDIT HERE IF NECESSARY **** */ -# define PLATFORM_BYTE_ORDER IS_BIG_ENDIAN -#else -# error Please edit lines 126 or 128 in brg_endian.h to set the platform byte order -#endif - -#endif - -#endif +/* +--------------------------------------------------------------------------- +Copyright (c) 1998-2010, Brian Gladman, Worcester, UK. All rights reserved. + +The redistribution and use of this software (with or without changes) +is allowed without the payment of fees or royalties provided that: + + source code distributions include the above copyright notice, this + list of conditions and the following disclaimer; + + binary distributions include the above copyright notice, this list + of conditions and the following disclaimer in their documentation. + +This software is provided 'as is' with no explicit or implied warranties +in respect of its operation, including, but not limited to, correctness +and fitness for purpose. +--------------------------------------------------------------------------- +Issue Date: 20/12/2007 +*/ + +#ifndef _BRG_ENDIAN_H +#define _BRG_ENDIAN_H + +#define IS_BIG_ENDIAN 4321 /* byte 0 is most significant (mc68k) */ +#define IS_LITTLE_ENDIAN 1234 /* byte 0 is least significant (i386) */ + +/* Include files where endian defines and byteswap functions may reside */ +#if defined( __sun ) +# include +#elif defined( __FreeBSD__ ) || defined( __OpenBSD__ ) || defined( __NetBSD__ ) +# include +#elif defined( BSD ) && ( BSD >= 199103 ) || defined( __APPLE__ ) || \ + defined( __CYGWIN32__ ) || defined( __DJGPP__ ) || defined( __osf__ ) +# include +#elif defined( __linux__ ) || defined( __GNUC__ ) || defined( __GNU_LIBRARY__ ) +# if !defined( __MINGW32__ ) && !defined( _AIX ) +# include +# if !defined( __BEOS__ ) +# include +# endif +# endif +#endif + +/* Now attempt to set the define for platform byte order using any */ +/* of the four forms SYMBOL, _SYMBOL, __SYMBOL & __SYMBOL__, which */ +/* seem to encompass most endian symbol definitions */ + +#if defined( BIG_ENDIAN ) && defined( LITTLE_ENDIAN ) +# if defined( BYTE_ORDER ) && BYTE_ORDER == BIG_ENDIAN +# define PLATFORM_BYTE_ORDER IS_BIG_ENDIAN +# elif defined( BYTE_ORDER ) && BYTE_ORDER == LITTLE_ENDIAN +# define PLATFORM_BYTE_ORDER IS_LITTLE_ENDIAN +# endif +#elif defined( BIG_ENDIAN ) +# define PLATFORM_BYTE_ORDER IS_BIG_ENDIAN +#elif defined( LITTLE_ENDIAN ) +# define PLATFORM_BYTE_ORDER IS_LITTLE_ENDIAN +#endif + +#if defined( _BIG_ENDIAN ) && defined( _LITTLE_ENDIAN ) +# if defined( _BYTE_ORDER ) && _BYTE_ORDER == _BIG_ENDIAN +# define PLATFORM_BYTE_ORDER IS_BIG_ENDIAN +# elif defined( _BYTE_ORDER ) && _BYTE_ORDER == _LITTLE_ENDIAN +# define PLATFORM_BYTE_ORDER IS_LITTLE_ENDIAN +# endif +#elif defined( _BIG_ENDIAN ) +# define PLATFORM_BYTE_ORDER IS_BIG_ENDIAN +#elif defined( _LITTLE_ENDIAN ) +# define PLATFORM_BYTE_ORDER IS_LITTLE_ENDIAN +#endif + +#if defined( __BIG_ENDIAN ) && defined( __LITTLE_ENDIAN ) +# if defined( __BYTE_ORDER ) && __BYTE_ORDER == __BIG_ENDIAN +# define PLATFORM_BYTE_ORDER IS_BIG_ENDIAN +# elif defined( __BYTE_ORDER ) && __BYTE_ORDER == __LITTLE_ENDIAN +# define PLATFORM_BYTE_ORDER IS_LITTLE_ENDIAN +# endif +#elif defined( __BIG_ENDIAN ) +# define PLATFORM_BYTE_ORDER IS_BIG_ENDIAN +#elif defined( __LITTLE_ENDIAN ) +# define PLATFORM_BYTE_ORDER IS_LITTLE_ENDIAN +#endif + +#if defined( __BIG_ENDIAN__ ) && defined( __LITTLE_ENDIAN__ ) +# if defined( __BYTE_ORDER__ ) && __BYTE_ORDER__ == __BIG_ENDIAN__ +# define PLATFORM_BYTE_ORDER IS_BIG_ENDIAN +# elif defined( __BYTE_ORDER__ ) && __BYTE_ORDER__ == __LITTLE_ENDIAN__ +# define PLATFORM_BYTE_ORDER IS_LITTLE_ENDIAN +# endif +#elif defined( __BIG_ENDIAN__ ) +# define PLATFORM_BYTE_ORDER IS_BIG_ENDIAN +#elif defined( __LITTLE_ENDIAN__ ) +# define PLATFORM_BYTE_ORDER IS_LITTLE_ENDIAN +#endif + +/* if the platform byte order could not be determined, then try to */ +/* set this define using common machine defines */ +#if !defined(PLATFORM_BYTE_ORDER) + +#if defined( __alpha__ ) || defined( __alpha ) || defined( i386 ) || \ + defined( __i386__ ) || defined( _M_I86 ) || defined( _M_IX86 ) || \ + defined( __OS2__ ) || defined( sun386 ) || defined( __TURBOC__ ) || \ + defined( vax ) || defined( vms ) || defined( VMS ) || \ + defined( __VMS ) || defined( _M_X64 ) +# define PLATFORM_BYTE_ORDER IS_LITTLE_ENDIAN + +#elif defined( AMIGA ) || defined( applec ) || defined( __AS400__ ) || \ + defined( _CRAY ) || defined( __hppa ) || defined( __hp9000 ) || \ + defined( ibm370 ) || defined( mc68000 ) || defined( m68k ) || \ + defined( __MRC__ ) || defined( __MVS__ ) || defined( __MWERKS__ ) || \ + defined( sparc ) || defined( __sparc) || defined( SYMANTEC_C ) || \ + defined( __VOS__ ) || defined( __TIGCC__ ) || defined( __TANDEM ) || \ + defined( THINK_C ) || defined( __VMCMS__ ) || defined( _AIX ) +# define PLATFORM_BYTE_ORDER IS_BIG_ENDIAN + +#elif 0 /* **** EDIT HERE IF NECESSARY **** */ +# define PLATFORM_BYTE_ORDER IS_LITTLE_ENDIAN +#elif 0 /* **** EDIT HERE IF NECESSARY **** */ +# define PLATFORM_BYTE_ORDER IS_BIG_ENDIAN +#else +# error Please edit lines 126 or 128 in brg_endian.h to set the platform byte order +#endif + +#endif + +#endif diff --git a/Zip/aes/brg_types.h b/Zip/minizip/aes/brg_types.h similarity index 97% rename from Zip/aes/brg_types.h rename to Zip/minizip/aes/brg_types.h index 40d4af5bd5b592c5740c5ae3ebc669d45914c329..a1d7483a4c8a67b448dd9127ccc7f37785e4a6ff 100755 --- a/Zip/aes/brg_types.h +++ b/Zip/minizip/aes/brg_types.h @@ -1,219 +1,219 @@ -/* ---------------------------------------------------------------------------- -Copyright (c) 1998-2010, Brian Gladman, Worcester, UK. All rights reserved. - -The redistribution and use of this software (with or without changes) -is allowed without the payment of fees or royalties provided that: - - source code distributions include the above copyright notice, this - list of conditions and the following disclaimer; - - binary distributions include the above copyright notice, this list - of conditions and the following disclaimer in their documentation. - -This software is provided 'as is' with no explicit or implied warranties -in respect of its operation, including, but not limited to, correctness -and fitness for purpose. ---------------------------------------------------------------------------- -Issue Date: 20/12/2007 - - The unsigned integer types defined here are of the form uint_t where - is the length of the type; for example, the unsigned 32-bit type is - 'uint_32t'. These are NOT the same as the 'C99 integer types' that are - defined in the inttypes.h and stdint.h headers since attempts to use these - types have shown that support for them is still highly variable. However, - since the latter are of the form uint_t, a regular expression search - and replace (in VC++ search on 'uint_{:z}t' and replace with 'uint\1_t') - can be used to convert the types used here to the C99 standard types. -*/ - -#ifndef _BRG_TYPES_H -#define _BRG_TYPES_H - -#if defined(__cplusplus) -extern "C" { -#endif - -#include - -#if defined( _MSC_VER ) && ( _MSC_VER >= 1300 ) -# include -# define ptrint_t intptr_t -#elif defined( __ECOS__ ) -# define intptr_t unsigned int -# define ptrint_t intptr_t -#elif defined( __GNUC__ ) && ( __GNUC__ >= 3 ) -# include -# define ptrint_t intptr_t -#else -# define ptrint_t int -#endif - -#ifndef BRG_UI8 -# define BRG_UI8 -# if UCHAR_MAX == 255u - typedef unsigned char uint_8t; -# else -# error Please define uint_8t as an 8-bit unsigned integer type in brg_types.h -# endif -#endif - -#ifndef BRG_UI16 -# define BRG_UI16 -# if USHRT_MAX == 65535u - typedef unsigned short uint_16t; -# else -# error Please define uint_16t as a 16-bit unsigned short type in brg_types.h -# endif -#endif - -#ifndef BRG_UI32 -# define BRG_UI32 -# if UINT_MAX == 4294967295u -# define li_32(h) 0x##h##u - typedef unsigned int uint_32t; -# elif ULONG_MAX == 4294967295u -# define li_32(h) 0x##h##ul - typedef unsigned long uint_32t; -# elif defined( _CRAY ) -# error This code needs 32-bit data types, which Cray machines do not provide -# else -# error Please define uint_32t as a 32-bit unsigned integer type in brg_types.h -# endif -#endif - -#ifndef BRG_UI64 -# if defined( __BORLANDC__ ) && !defined( __MSDOS__ ) -# define BRG_UI64 -# define li_64(h) 0x##h##ui64 - typedef unsigned __int64 uint_64t; -# elif defined( _MSC_VER ) && ( _MSC_VER < 1300 ) /* 1300 == VC++ 7.0 */ -# define BRG_UI64 -# define li_64(h) 0x##h##ui64 - typedef unsigned __int64 uint_64t; -# elif defined( __sun ) && defined( ULONG_MAX ) && ULONG_MAX == 0xfffffffful -# define BRG_UI64 -# define li_64(h) 0x##h##ull - typedef unsigned long long uint_64t; -# elif defined( __MVS__ ) -# define BRG_UI64 -# define li_64(h) 0x##h##ull - typedef unsigned int long long uint_64t; -# elif defined( UINT_MAX ) && UINT_MAX > 4294967295u -# if UINT_MAX == 18446744073709551615u -# define BRG_UI64 -# define li_64(h) 0x##h##u - typedef unsigned int uint_64t; -# endif -# elif defined( ULONG_MAX ) && ULONG_MAX > 4294967295u -# if ULONG_MAX == 18446744073709551615ul -# define BRG_UI64 -# define li_64(h) 0x##h##ul - typedef unsigned long uint_64t; -# endif -# elif defined( ULLONG_MAX ) && ULLONG_MAX > 4294967295u -# if ULLONG_MAX == 18446744073709551615ull -# define BRG_UI64 -# define li_64(h) 0x##h##ull - typedef unsigned long long uint_64t; -# endif -# elif defined( ULONG_LONG_MAX ) && ULONG_LONG_MAX > 4294967295u -# if ULONG_LONG_MAX == 18446744073709551615ull -# define BRG_UI64 -# define li_64(h) 0x##h##ull - typedef unsigned long long uint_64t; -# endif -# endif -#endif - -#if !defined( BRG_UI64 ) -# if defined( NEED_UINT_64T ) -# error Please define uint_64t as an unsigned 64 bit type in brg_types.h -# endif -#endif - -#ifndef RETURN_VALUES -# define RETURN_VALUES -# if defined( DLL_EXPORT ) -# if defined( _MSC_VER ) || defined ( __INTEL_COMPILER ) -# define VOID_RETURN __declspec( dllexport ) void __stdcall -# define INT_RETURN __declspec( dllexport ) int __stdcall -# elif defined( __GNUC__ ) -# define VOID_RETURN __declspec( __dllexport__ ) void -# define INT_RETURN __declspec( __dllexport__ ) int -# else -# error Use of the DLL is only available on the Microsoft, Intel and GCC compilers -# endif -# elif defined( DLL_IMPORT ) -# if defined( _MSC_VER ) || defined ( __INTEL_COMPILER ) -# define VOID_RETURN __declspec( dllimport ) void __stdcall -# define INT_RETURN __declspec( dllimport ) int __stdcall -# elif defined( __GNUC__ ) -# define VOID_RETURN __declspec( __dllimport__ ) void -# define INT_RETURN __declspec( __dllimport__ ) int -# else -# error Use of the DLL is only available on the Microsoft, Intel and GCC compilers -# endif -# elif defined( __WATCOMC__ ) -# define VOID_RETURN void __cdecl -# define INT_RETURN int __cdecl -# else -# define VOID_RETURN void -# define INT_RETURN int -# endif -#endif - -/* These defines are used to detect and set the memory alignment of pointers. - Note that offsets are in bytes. - - ALIGN_OFFSET(x,n) return the positive or zero offset of - the memory addressed by the pointer 'x' - from an address that is aligned on an - 'n' byte boundary ('n' is a power of 2) - - ALIGN_FLOOR(x,n) return a pointer that points to memory - that is aligned on an 'n' byte boundary - and is not higher than the memory address - pointed to by 'x' ('n' is a power of 2) - - ALIGN_CEIL(x,n) return a pointer that points to memory - that is aligned on an 'n' byte boundary - and is not lower than the memory address - pointed to by 'x' ('n' is a power of 2) -*/ - -#define ALIGN_OFFSET(x,n) (((ptrint_t)(x)) & ((n) - 1)) -#define ALIGN_FLOOR(x,n) ((uint_8t*)(x) - ( ((ptrint_t)(x)) & ((n) - 1))) -#define ALIGN_CEIL(x,n) ((uint_8t*)(x) + (-((ptrint_t)(x)) & ((n) - 1))) - -/* These defines are used to declare buffers in a way that allows - faster operations on longer variables to be used. In all these - defines 'size' must be a power of 2 and >= 8. NOTE that the - buffer size is in bytes but the type length is in bits - - UNIT_TYPEDEF(x,size) declares a variable 'x' of length - 'size' bits - - BUFR_TYPEDEF(x,size,bsize) declares a buffer 'x' of length 'bsize' - bytes defined as an array of variables - each of 'size' bits (bsize must be a - multiple of size / 8) - - UNIT_CAST(x,size) casts a variable to a type of - length 'size' bits - - UPTR_CAST(x,size) casts a pointer to a pointer to a - varaiable of length 'size' bits -*/ - -#define UI_TYPE(size) uint_##size##t -#define UNIT_TYPEDEF(x,size) typedef UI_TYPE(size) x -#define BUFR_TYPEDEF(x,size,bsize) typedef UI_TYPE(size) x[bsize / (size >> 3)] -#define UNIT_CAST(x,size) ((UI_TYPE(size) )(x)) -#define UPTR_CAST(x,size) ((UI_TYPE(size)*)(x)) - -#if defined(__cplusplus) -} -#endif - -#endif +/* +--------------------------------------------------------------------------- +Copyright (c) 1998-2010, Brian Gladman, Worcester, UK. All rights reserved. + +The redistribution and use of this software (with or without changes) +is allowed without the payment of fees or royalties provided that: + + source code distributions include the above copyright notice, this + list of conditions and the following disclaimer; + + binary distributions include the above copyright notice, this list + of conditions and the following disclaimer in their documentation. + +This software is provided 'as is' with no explicit or implied warranties +in respect of its operation, including, but not limited to, correctness +and fitness for purpose. +--------------------------------------------------------------------------- +Issue Date: 20/12/2007 + + The unsigned integer types defined here are of the form uint_t where + is the length of the type; for example, the unsigned 32-bit type is + 'uint_32t'. These are NOT the same as the 'C99 integer types' that are + defined in the inttypes.h and stdint.h headers since attempts to use these + types have shown that support for them is still highly variable. However, + since the latter are of the form uint_t, a regular expression search + and replace (in VC++ search on 'uint_{:z}t' and replace with 'uint\1_t') + can be used to convert the types used here to the C99 standard types. +*/ + +#ifndef _BRG_TYPES_H +#define _BRG_TYPES_H + +#if defined(__cplusplus) +extern "C" { +#endif + +#include + +#if defined( _MSC_VER ) && ( _MSC_VER >= 1300 ) +# include +# define ptrint_t intptr_t +#elif defined( __ECOS__ ) +# define intptr_t unsigned int +# define ptrint_t intptr_t +#elif defined( __GNUC__ ) && ( __GNUC__ >= 3 ) +# include +# define ptrint_t intptr_t +#else +# define ptrint_t int +#endif + +#ifndef BRG_UI8 +# define BRG_UI8 +# if UCHAR_MAX == 255u + typedef unsigned char uint_8t; +# else +# error Please define uint_8t as an 8-bit unsigned integer type in brg_types.h +# endif +#endif + +#ifndef BRG_UI16 +# define BRG_UI16 +# if USHRT_MAX == 65535u + typedef unsigned short uint_16t; +# else +# error Please define uint_16t as a 16-bit unsigned short type in brg_types.h +# endif +#endif + +#ifndef BRG_UI32 +# define BRG_UI32 +# if UINT_MAX == 4294967295u +# define li_32(h) 0x##h##u + typedef unsigned int uint_32t; +# elif ULONG_MAX == 4294967295u +# define li_32(h) 0x##h##ul + typedef unsigned long uint_32t; +# elif defined( _CRAY ) +# error This code needs 32-bit data types, which Cray machines do not provide +# else +# error Please define uint_32t as a 32-bit unsigned integer type in brg_types.h +# endif +#endif + +#ifndef BRG_UI64 +# if defined( __BORLANDC__ ) && !defined( __MSDOS__ ) +# define BRG_UI64 +# define li_64(h) 0x##h##ui64 + typedef unsigned __int64 uint_64t; +# elif defined( _MSC_VER ) && ( _MSC_VER < 1300 ) /* 1300 == VC++ 7.0 */ +# define BRG_UI64 +# define li_64(h) 0x##h##ui64 + typedef unsigned __int64 uint_64t; +# elif defined( __sun ) && defined( ULONG_MAX ) && ULONG_MAX == 0xfffffffful +# define BRG_UI64 +# define li_64(h) 0x##h##ull + typedef unsigned long long uint_64t; +# elif defined( __MVS__ ) +# define BRG_UI64 +# define li_64(h) 0x##h##ull + typedef unsigned int long long uint_64t; +# elif defined( UINT_MAX ) && UINT_MAX > 4294967295u +# if UINT_MAX == 18446744073709551615u +# define BRG_UI64 +# define li_64(h) 0x##h##u + typedef unsigned int uint_64t; +# endif +# elif defined( ULONG_MAX ) && ULONG_MAX > 4294967295u +# if ULONG_MAX == 18446744073709551615ul +# define BRG_UI64 +# define li_64(h) 0x##h##ul + typedef unsigned long uint_64t; +# endif +# elif defined( ULLONG_MAX ) && ULLONG_MAX > 4294967295u +# if ULLONG_MAX == 18446744073709551615ull +# define BRG_UI64 +# define li_64(h) 0x##h##ull + typedef unsigned long long uint_64t; +# endif +# elif defined( ULONG_LONG_MAX ) && ULONG_LONG_MAX > 4294967295u +# if ULONG_LONG_MAX == 18446744073709551615ull +# define BRG_UI64 +# define li_64(h) 0x##h##ull + typedef unsigned long long uint_64t; +# endif +# endif +#endif + +#if !defined( BRG_UI64 ) +# if defined( NEED_UINT_64T ) +# error Please define uint_64t as an unsigned 64 bit type in brg_types.h +# endif +#endif + +#ifndef RETURN_VALUES +# define RETURN_VALUES +# if defined( DLL_EXPORT ) +# if defined( _MSC_VER ) || defined ( __INTEL_COMPILER ) +# define VOID_RETURN __declspec( dllexport ) void __stdcall +# define INT_RETURN __declspec( dllexport ) int __stdcall +# elif defined( __GNUC__ ) +# define VOID_RETURN __declspec( __dllexport__ ) void +# define INT_RETURN __declspec( __dllexport__ ) int +# else +# error Use of the DLL is only available on the Microsoft, Intel and GCC compilers +# endif +# elif defined( DLL_IMPORT ) +# if defined( _MSC_VER ) || defined ( __INTEL_COMPILER ) +# define VOID_RETURN __declspec( dllimport ) void __stdcall +# define INT_RETURN __declspec( dllimport ) int __stdcall +# elif defined( __GNUC__ ) +# define VOID_RETURN __declspec( __dllimport__ ) void +# define INT_RETURN __declspec( __dllimport__ ) int +# else +# error Use of the DLL is only available on the Microsoft, Intel and GCC compilers +# endif +# elif defined( __WATCOMC__ ) +# define VOID_RETURN void __cdecl +# define INT_RETURN int __cdecl +# else +# define VOID_RETURN void +# define INT_RETURN int +# endif +#endif + +/* These defines are used to detect and set the memory alignment of pointers. + Note that offsets are in bytes. + + ALIGN_OFFSET(x,n) return the positive or zero offset of + the memory addressed by the pointer 'x' + from an address that is aligned on an + 'n' byte boundary ('n' is a power of 2) + + ALIGN_FLOOR(x,n) return a pointer that points to memory + that is aligned on an 'n' byte boundary + and is not higher than the memory address + pointed to by 'x' ('n' is a power of 2) + + ALIGN_CEIL(x,n) return a pointer that points to memory + that is aligned on an 'n' byte boundary + and is not lower than the memory address + pointed to by 'x' ('n' is a power of 2) +*/ + +#define ALIGN_OFFSET(x,n) (((ptrint_t)(x)) & ((n) - 1)) +#define ALIGN_FLOOR(x,n) ((uint_8t*)(x) - ( ((ptrint_t)(x)) & ((n) - 1))) +#define ALIGN_CEIL(x,n) ((uint_8t*)(x) + (-((ptrint_t)(x)) & ((n) - 1))) + +/* These defines are used to declare buffers in a way that allows + faster operations on longer variables to be used. In all these + defines 'size' must be a power of 2 and >= 8. NOTE that the + buffer size is in bytes but the type length is in bits + + UNIT_TYPEDEF(x,size) declares a variable 'x' of length + 'size' bits + + BUFR_TYPEDEF(x,size,bsize) declares a buffer 'x' of length 'bsize' + bytes defined as an array of variables + each of 'size' bits (bsize must be a + multiple of size / 8) + + UNIT_CAST(x,size) casts a variable to a type of + length 'size' bits + + UPTR_CAST(x,size) casts a pointer to a pointer to a + varaiable of length 'size' bits +*/ + +#define UI_TYPE(size) uint_##size##t +#define UNIT_TYPEDEF(x,size) typedef UI_TYPE(size) x +#define BUFR_TYPEDEF(x,size,bsize) typedef UI_TYPE(size) x[bsize / (size >> 3)] +#define UNIT_CAST(x,size) ((UI_TYPE(size) )(x)) +#define UPTR_CAST(x,size) ((UI_TYPE(size)*)(x)) + +#if defined(__cplusplus) +} +#endif + +#endif diff --git a/Zip/aes/entropy.c b/Zip/minizip/aes/entropy.c similarity index 95% rename from Zip/aes/entropy.c rename to Zip/minizip/aes/entropy.c index 5840a97256a0d9f121dd5362b5382503823ec419..d065a4c19780db68a2a5e953cbeb894423f89c8b 100755 --- a/Zip/aes/entropy.c +++ b/Zip/minizip/aes/entropy.c @@ -42,7 +42,7 @@ int entropy_fun(unsigned char buf[], unsigned int len) int rlen = 0; if (frand != -1) { - rlen = (int)read(frand, buf, len); + rlen = read(frand, buf, len); close(frand); } return rlen; diff --git a/Zip/aes/entropy.h b/Zip/minizip/aes/entropy.h similarity index 92% rename from Zip/aes/entropy.h rename to Zip/minizip/aes/entropy.h index 306620c81039445b75f41b5f044ea0950f11ae58..34d59d2bc7d8b5f8c3a5575cb3791e51e12a5425 100755 --- a/Zip/aes/entropy.h +++ b/Zip/minizip/aes/entropy.h @@ -1,16 +1,16 @@ - -#ifndef _ENTROPY_FUN_H -#define _ENTROPY_FUN_H - -#if defined(__cplusplus) -extern "C" -{ -#endif - + +#ifndef _ENTROPY_FUN_H +#define _ENTROPY_FUN_H + +#if defined(__cplusplus) +extern "C" +{ +#endif + int entropy_fun(unsigned char buf[], unsigned int len); - -#if defined(__cplusplus) -} -#endif - -#endif + +#if defined(__cplusplus) +} +#endif + +#endif diff --git a/Zip/aes/fileenc.c b/Zip/minizip/aes/fileenc.c similarity index 66% rename from Zip/aes/fileenc.c rename to Zip/minizip/aes/fileenc.c index 505703683d2d064b42c8e8b5c7ef1064a83aa4be..1533dd0889bd009d1b54addfd3833a9fb3d8ce63 100755 --- a/Zip/aes/fileenc.c +++ b/Zip/minizip/aes/fileenc.c @@ -1,144 +1,143 @@ -/* - --------------------------------------------------------------------------- - Copyright (c) 2002, Dr Brian Gladman < >, Worcester, UK. - All rights reserved. - - LICENSE TERMS - - The free distribution and use of this software in both source and binary - form is allowed (with or without changes) provided that: - - 1. distributions of this source code include the above copyright - notice, this list of conditions and the following disclaimer; - - 2. distributions in binary form include the above copyright - notice, this list of conditions and the following disclaimer - in the documentation and/or other associated materials; - - 3. the copyright holder's name is not used to endorse products - built using this software without specific written permission. - - ALTERNATIVELY, provided that this notice is retained in full, this product - may be distributed under the terms of the GNU General Public License (GPL), - in which case the provisions of the GPL apply INSTEAD OF those given above. - - DISCLAIMER - - This software is provided 'as is' with no explicit or implied warranties - in respect of its properties, including, but not limited to, correctness - and/or fitness for purpose. - ------------------------------------------------------------------------- - Issue Date: 24/01/2003 - - This file implements password based file encryption and authentication - using AES in CTR mode, HMAC-SHA1 authentication and RFC2898 password - based key derivation. - - */ - -#include - -#include "fileenc.h" - -#if defined(__cplusplus) -extern "C" -{ -#endif - -/* subroutine for data encryption/decryption */ -/* this could be speeded up a lot by aligning */ -/* buffers and using 32 bit operations */ - -static void encr_data(unsigned char data[], unsigned long d_len, fcrypt_ctx cx[1]) -{ - unsigned long i = 0, pos = cx->encr_pos; - - while (i < d_len) { - if (pos == AES_BLOCK_SIZE) { - unsigned int j = 0; - /* increment encryption nonce */ - while (j < 8 && !++cx->nonce[j]) - ++j; - /* encrypt the nonce to form next xor buffer */ - aes_encrypt(cx->nonce, cx->encr_bfr, cx->encr_ctx); - pos = 0; - } - - data[i++] ^= cx->encr_bfr[pos++]; - } - - cx->encr_pos = (unsigned int)pos; -} - -int fcrypt_init( - int mode, /* the mode to be used (input) */ - const unsigned char pwd[], /* the user specified password (input) */ - unsigned int pwd_len, /* the length of the password (input) */ - const unsigned char salt[], /* the salt (input) */ -#ifdef PASSWORD_VERIFIER - unsigned char pwd_ver[PWD_VER_LENGTH], /* 2 byte password verifier (output) */ -#endif - fcrypt_ctx cx[1]) /* the file encryption context (output) */ -{ - unsigned char kbuf[2 * MAX_KEY_LENGTH + PWD_VER_LENGTH]; - - if (pwd_len > MAX_PWD_LENGTH) - return PASSWORD_TOO_LONG; - - if (mode < 1 || mode > 3) - return BAD_MODE; - - cx->mode = mode; - cx->pwd_len = pwd_len; - - /* derive the encryption and authentication keys and the password verifier */ - derive_key(pwd, pwd_len, salt, SALT_LENGTH(mode), KEYING_ITERATIONS, - kbuf, 2 * KEY_LENGTH(mode) + PWD_VER_LENGTH); - - /* initialise the encryption nonce and buffer pos */ - cx->encr_pos = AES_BLOCK_SIZE; - /* if we need a random component in the encryption */ - /* nonce, this is where it would have to be set */ - memset(cx->nonce, 0, AES_BLOCK_SIZE * sizeof(unsigned char)); - - /* initialise for encryption using key 1 */ - aes_encrypt_key(kbuf, KEY_LENGTH(mode), cx->encr_ctx); - - /* initialise for authentication using key 2 */ - hmac_sha_begin(cx->auth_ctx); - hmac_sha_key(kbuf + KEY_LENGTH(mode), KEY_LENGTH(mode), cx->auth_ctx); - -#ifdef PASSWORD_VERIFIER - memcpy(pwd_ver, kbuf + 2 * KEY_LENGTH(mode), PWD_VER_LENGTH); -#endif - - return GOOD_RETURN; -} - -/* perform 'in place' encryption and authentication */ - -void fcrypt_encrypt(unsigned char data[], unsigned int data_len, fcrypt_ctx cx[1]) -{ - encr_data(data, data_len, cx); - hmac_sha_data(data, data_len, cx->auth_ctx); -} - -/* perform 'in place' authentication and decryption */ - -void fcrypt_decrypt(unsigned char data[], unsigned int data_len, fcrypt_ctx cx[1]) -{ - hmac_sha_data(data, data_len, cx->auth_ctx); - encr_data(data, data_len, cx); -} - -/* close encryption/decryption and return the MAC value */ - -int fcrypt_end(unsigned char mac[], fcrypt_ctx cx[1]) -{ - hmac_sha_end(mac, MAC_LENGTH(cx->mode), cx->auth_ctx); - return MAC_LENGTH(cx->mode); /* return MAC length in bytes */ -} - -#if defined(__cplusplus) -} -#endif +/* + --------------------------------------------------------------------------- + Copyright (c) 2002, Dr Brian Gladman < >, Worcester, UK. + All rights reserved. + + LICENSE TERMS + + The free distribution and use of this software in both source and binary + form is allowed (with or without changes) provided that: + + 1. distributions of this source code include the above copyright + notice, this list of conditions and the following disclaimer; + + 2. distributions in binary form include the above copyright + notice, this list of conditions and the following disclaimer + in the documentation and/or other associated materials; + + 3. the copyright holder's name is not used to endorse products + built using this software without specific written permission. + + ALTERNATIVELY, provided that this notice is retained in full, this product + may be distributed under the terms of the GNU General Public License (GPL), + in which case the provisions of the GPL apply INSTEAD OF those given above. + + DISCLAIMER + + This software is provided 'as is' with no explicit or implied warranties + in respect of its properties, including, but not limited to, correctness + and/or fitness for purpose. + ------------------------------------------------------------------------- + Issue Date: 24/01/2003 + + This file implements password based file encryption and authentication + using AES in CTR mode, HMAC-SHA1 authentication and RFC2898 password + based key derivation. + +*/ + +#include + +#include "fileenc.h" + +#if defined(__cplusplus) +extern "C" +{ +#endif + +/* subroutine for data encryption/decryption */ +/* this could be speeded up a lot by aligning */ +/* buffers and using 32 bit operations */ + +static void encr_data(unsigned char data[], unsigned long d_len, fcrypt_ctx cx[1]) +{ unsigned long i = 0, pos = cx->encr_pos; + + while(i < d_len) + { + if(pos == AES_BLOCK_SIZE) + { unsigned int j = 0; + /* increment encryption nonce */ + while(j < 8 && !++cx->nonce[j]) + ++j; + /* encrypt the nonce to form next xor buffer */ + aes_encrypt(cx->nonce, cx->encr_bfr, cx->encr_ctx); + pos = 0; + } + + data[i++] ^= cx->encr_bfr[pos++]; + } + + cx->encr_pos = pos; +} + +int fcrypt_init( + int mode, /* the mode to be used (input) */ + const unsigned char pwd[], /* the user specified password (input) */ + unsigned int pwd_len, /* the length of the password (input) */ + const unsigned char salt[], /* the salt (input) */ +#ifdef PASSWORD_VERIFIER + unsigned char pwd_ver[PWD_VER_LENGTH], /* 2 byte password verifier (output) */ +#endif + fcrypt_ctx cx[1]) /* the file encryption context (output) */ +{ unsigned char kbuf[2 * MAX_KEY_LENGTH + PWD_VER_LENGTH]; + + if(pwd_len > MAX_PWD_LENGTH) + return PASSWORD_TOO_LONG; + + if(mode < 1 || mode > 3) + return BAD_MODE; + + cx->mode = mode; + cx->pwd_len = pwd_len; + + /* derive the encryption and authentication keys and the password verifier */ + derive_key(pwd, pwd_len, salt, SALT_LENGTH(mode), KEYING_ITERATIONS, + kbuf, 2 * KEY_LENGTH(mode) + PWD_VER_LENGTH); + + /* initialise the encryption nonce and buffer pos */ + cx->encr_pos = AES_BLOCK_SIZE; + /* if we need a random component in the encryption */ + /* nonce, this is where it would have to be set */ + memset(cx->nonce, 0, AES_BLOCK_SIZE * sizeof(unsigned char)); + + /* initialise for encryption using key 1 */ + aes_encrypt_key(kbuf, KEY_LENGTH(mode), cx->encr_ctx); + + /* initialise for authentication using key 2 */ + hmac_sha_begin(cx->auth_ctx); + hmac_sha_key(kbuf + KEY_LENGTH(mode), KEY_LENGTH(mode), cx->auth_ctx); + +#ifdef PASSWORD_VERIFIER + memcpy(pwd_ver, kbuf + 2 * KEY_LENGTH(mode), PWD_VER_LENGTH); +#endif + + return GOOD_RETURN; +} + +/* perform 'in place' encryption and authentication */ + +void fcrypt_encrypt(unsigned char data[], unsigned int data_len, fcrypt_ctx cx[1]) +{ + encr_data(data, data_len, cx); + hmac_sha_data(data, data_len, cx->auth_ctx); +} + +/* perform 'in place' authentication and decryption */ + +void fcrypt_decrypt(unsigned char data[], unsigned int data_len, fcrypt_ctx cx[1]) +{ + hmac_sha_data(data, data_len, cx->auth_ctx); + encr_data(data, data_len, cx); +} + +/* close encryption/decryption and return the MAC value */ + +int fcrypt_end(unsigned char mac[], fcrypt_ctx cx[1]) +{ + hmac_sha_end(mac, MAC_LENGTH(cx->mode), cx->auth_ctx); + return MAC_LENGTH(cx->mode); /* return MAC length in bytes */ +} + +#if defined(__cplusplus) +} +#endif diff --git a/Zip/aes/fileenc.h b/Zip/minizip/aes/fileenc.h similarity index 97% rename from Zip/aes/fileenc.h rename to Zip/minizip/aes/fileenc.h index ba64a7c3266f4db9bf2d7e410ff653719665ccc8..9c0250edb149d281ace248bfe415ddb465f31ed0 100755 --- a/Zip/aes/fileenc.h +++ b/Zip/minizip/aes/fileenc.h @@ -1,121 +1,121 @@ -/* - --------------------------------------------------------------------------- - Copyright (c) 2002, Dr Brian Gladman < >, Worcester, UK. - All rights reserved. - - LICENSE TERMS - - The free distribution and use of this software in both source and binary - form is allowed (with or without changes) provided that: - - 1. distributions of this source code include the above copyright - notice, this list of conditions and the following disclaimer; - - 2. distributions in binary form include the above copyright - notice, this list of conditions and the following disclaimer - in the documentation and/or other associated materials; - - 3. the copyright holder's name is not used to endorse products - built using this software without specific written permission. - - ALTERNATIVELY, provided that this notice is retained in full, this product - may be distributed under the terms of the GNU General Public License (GPL), - in which case the provisions of the GPL apply INSTEAD OF those given above. - - DISCLAIMER - - This software is provided 'as is' with no explicit or implied warranties - in respect of its properties, including, but not limited to, correctness - and/or fitness for purpose. - --------------------------------------------------------------------------- - Issue Date: 24/01/2003 - - This file contains the header file for fileenc.c, which implements password - based file encryption and authentication using AES in CTR mode, HMAC-SHA1 - authentication and RFC2898 password based key derivation. -*/ - -#ifndef _FENC_H -#define _FENC_H - -#include "aes.h" -#include "hmac.h" -#include "pwd2key.h" - -#define PASSWORD_VERIFIER - -#define MAX_KEY_LENGTH 32 -#define MAX_PWD_LENGTH 128 -#define MAX_SALT_LENGTH 16 -#define KEYING_ITERATIONS 1000 - -#ifdef PASSWORD_VERIFIER -#define PWD_VER_LENGTH 2 -#else -#define PWD_VER_LENGTH 0 -#endif - -#define GOOD_RETURN 0 -#define PASSWORD_TOO_LONG -100 -#define BAD_MODE -101 - -/* - Field lengths (in bytes) versus File Encryption Mode (0 < mode < 4) - - Mode Key Salt MAC Overhead - 1 16 8 10 18 - 2 24 12 10 22 - 3 32 16 10 26 - - The following macros assume that the mode value is correct. -*/ - -#define KEY_LENGTH(mode) (8 * (mode & 3) + 8) -#define SALT_LENGTH(mode) (4 * (mode & 3) + 4) -#define MAC_LENGTH(mode) (10) - -/* the context for file encryption */ - -#if defined(__cplusplus) -extern "C" -{ -#endif - -typedef struct -{ unsigned char nonce[AES_BLOCK_SIZE]; /* the CTR nonce */ - unsigned char encr_bfr[AES_BLOCK_SIZE]; /* encrypt buffer */ - aes_encrypt_ctx encr_ctx[1]; /* encryption context */ - hmac_ctx auth_ctx[1]; /* authentication context */ - unsigned int encr_pos; /* block position (enc) */ - unsigned int pwd_len; /* password length */ - unsigned int mode; /* File encryption mode */ -} fcrypt_ctx; - -/* initialise file encryption or decryption */ - -int fcrypt_init( - int mode, /* the mode to be used (input) */ - const unsigned char pwd[], /* the user specified password (input) */ - unsigned int pwd_len, /* the length of the password (input) */ - const unsigned char salt[], /* the salt (input) */ -#ifdef PASSWORD_VERIFIER - unsigned char pwd_ver[PWD_VER_LENGTH], /* 2 byte password verifier (output) */ -#endif - fcrypt_ctx cx[1]); /* the file encryption context (output) */ - -/* perform 'in place' encryption or decryption and authentication */ - -void fcrypt_encrypt(unsigned char data[], unsigned int data_len, fcrypt_ctx cx[1]); -void fcrypt_decrypt(unsigned char data[], unsigned int data_len, fcrypt_ctx cx[1]); - -/* close encryption/decryption and return the MAC value */ -/* the return value is the length of the MAC */ - -int fcrypt_end(unsigned char mac[], /* the MAC value (output) */ - fcrypt_ctx cx[1]); /* the context (input) */ - -#if defined(__cplusplus) -} -#endif - -#endif +/* + --------------------------------------------------------------------------- + Copyright (c) 2002, Dr Brian Gladman < >, Worcester, UK. + All rights reserved. + + LICENSE TERMS + + The free distribution and use of this software in both source and binary + form is allowed (with or without changes) provided that: + + 1. distributions of this source code include the above copyright + notice, this list of conditions and the following disclaimer; + + 2. distributions in binary form include the above copyright + notice, this list of conditions and the following disclaimer + in the documentation and/or other associated materials; + + 3. the copyright holder's name is not used to endorse products + built using this software without specific written permission. + + ALTERNATIVELY, provided that this notice is retained in full, this product + may be distributed under the terms of the GNU General Public License (GPL), + in which case the provisions of the GPL apply INSTEAD OF those given above. + + DISCLAIMER + + This software is provided 'as is' with no explicit or implied warranties + in respect of its properties, including, but not limited to, correctness + and/or fitness for purpose. + --------------------------------------------------------------------------- + Issue Date: 24/01/2003 + + This file contains the header file for fileenc.c, which implements password + based file encryption and authentication using AES in CTR mode, HMAC-SHA1 + authentication and RFC2898 password based key derivation. +*/ + +#ifndef _FENC_H +#define _FENC_H + +#include "aes.h" +#include "hmac.h" +#include "pwd2key.h" + +#define PASSWORD_VERIFIER + +#define MAX_KEY_LENGTH 32 +#define MAX_PWD_LENGTH 128 +#define MAX_SALT_LENGTH 16 +#define KEYING_ITERATIONS 1000 + +#ifdef PASSWORD_VERIFIER +#define PWD_VER_LENGTH 2 +#else +#define PWD_VER_LENGTH 0 +#endif + +#define GOOD_RETURN 0 +#define PASSWORD_TOO_LONG -100 +#define BAD_MODE -101 + +/* + Field lengths (in bytes) versus File Encryption Mode (0 < mode < 4) + + Mode Key Salt MAC Overhead + 1 16 8 10 18 + 2 24 12 10 22 + 3 32 16 10 26 + + The following macros assume that the mode value is correct. +*/ + +#define KEY_LENGTH(mode) (8 * (mode & 3) + 8) +#define SALT_LENGTH(mode) (4 * (mode & 3) + 4) +#define MAC_LENGTH(mode) (10) + +/* the context for file encryption */ + +#if defined(__cplusplus) +extern "C" +{ +#endif + +typedef struct +{ unsigned char nonce[AES_BLOCK_SIZE]; /* the CTR nonce */ + unsigned char encr_bfr[AES_BLOCK_SIZE]; /* encrypt buffer */ + aes_encrypt_ctx encr_ctx[1]; /* encryption context */ + hmac_ctx auth_ctx[1]; /* authentication context */ + unsigned int encr_pos; /* block position (enc) */ + unsigned int pwd_len; /* password length */ + unsigned int mode; /* File encryption mode */ +} fcrypt_ctx; + +/* initialise file encryption or decryption */ + +int fcrypt_init( + int mode, /* the mode to be used (input) */ + const unsigned char pwd[], /* the user specified password (input) */ + unsigned int pwd_len, /* the length of the password (input) */ + const unsigned char salt[], /* the salt (input) */ +#ifdef PASSWORD_VERIFIER + unsigned char pwd_ver[PWD_VER_LENGTH], /* 2 byte password verifier (output) */ +#endif + fcrypt_ctx cx[1]); /* the file encryption context (output) */ + +/* perform 'in place' encryption or decryption and authentication */ + +void fcrypt_encrypt(unsigned char data[], unsigned int data_len, fcrypt_ctx cx[1]); +void fcrypt_decrypt(unsigned char data[], unsigned int data_len, fcrypt_ctx cx[1]); + +/* close encryption/decryption and return the MAC value */ +/* the return value is the length of the MAC */ + +int fcrypt_end(unsigned char mac[], /* the MAC value (output) */ + fcrypt_ctx cx[1]); /* the context (input) */ + +#if defined(__cplusplus) +} +#endif + +#endif diff --git a/Zip/aes/hmac.c b/Zip/minizip/aes/hmac.c similarity index 97% rename from Zip/aes/hmac.c rename to Zip/minizip/aes/hmac.c index c71b14ea4086b711d0d328b987234141fc675985..7600a4e491fc012485e0dba32e1ee8a9553b0363 100755 --- a/Zip/aes/hmac.c +++ b/Zip/minizip/aes/hmac.c @@ -1,145 +1,145 @@ -/* - --------------------------------------------------------------------------- - Copyright (c) 2002, Dr Brian Gladman, Worcester, UK. All rights reserved. - - LICENSE TERMS - - The free distribution and use of this software in both source and binary - form is allowed (with or without changes) provided that: - - 1. distributions of this source code include the above copyright - notice, this list of conditions and the following disclaimer; - - 2. distributions in binary form include the above copyright - notice, this list of conditions and the following disclaimer - in the documentation and/or other associated materials; - - 3. the copyright holder's name is not used to endorse products - built using this software without specific written permission. - - ALTERNATIVELY, provided that this notice is retained in full, this product - may be distributed under the terms of the GNU General Public License (GPL), - in which case the provisions of the GPL apply INSTEAD OF those given above. - - DISCLAIMER - - This software is provided 'as is' with no explicit or implied warranties - in respect of its properties, including, but not limited to, correctness - and/or fitness for purpose. - --------------------------------------------------------------------------- - Issue Date: 26/08/2003 - - This is an implementation of HMAC, the FIPS standard keyed hash function -*/ - -#include "hmac.h" -#include "brg_types.h" - -#if defined(__cplusplus) -extern "C" -{ -#endif - -/* initialise the HMAC context to zero */ -void hmac_sha_begin(hmac_ctx cx[1]) -{ - memset(cx, 0, sizeof(hmac_ctx)); -} - -/* input the HMAC key (can be called multiple times) */ -int hmac_sha_key(const unsigned char key[], unsigned long key_len, hmac_ctx cx[1]) -{ - if(cx->klen == HMAC_IN_DATA) /* error if further key input */ - return HMAC_BAD_MODE; /* is attempted in data mode */ - - if(cx->klen + key_len > HASH_INPUT_SIZE) /* if the key has to be hashed */ - { - if(cx->klen <= HASH_INPUT_SIZE) /* if the hash has not yet been */ - { /* started, initialise it and */ - sha_begin(cx->ctx); /* hash stored key characters */ - sha_hash(cx->key, cx->klen, cx->ctx); - } - - sha_hash(key, key_len, cx->ctx); /* hash long key data into hash */ - } - else /* otherwise store key data */ - memcpy(cx->key + cx->klen, key, key_len); - - cx->klen += key_len; /* update the key length count */ - return HMAC_OK; -} - -/* input the HMAC data (can be called multiple times) - */ -/* note that this call terminates the key input phase */ -void hmac_sha_data(const unsigned char data[], unsigned long data_len, hmac_ctx cx[1]) -{ unsigned int i; - - if(cx->klen != HMAC_IN_DATA) /* if not yet in data phase */ - { - if(cx->klen > HASH_INPUT_SIZE) /* if key is being hashed */ - { /* complete the hash and */ - sha_end(cx->key, cx->ctx); /* store the result as the */ - cx->klen = HASH_OUTPUT_SIZE; /* key and set new length */ - } - - /* pad the key if necessary */ - memset(cx->key + cx->klen, 0, HASH_INPUT_SIZE - cx->klen); - - /* xor ipad into key value */ - for(i = 0; i < (HASH_INPUT_SIZE >> 2); ++i) - ((uint_32t*)cx->key)[i] ^= 0x36363636; - - /* and start hash operation */ - sha_begin(cx->ctx); - sha_hash(cx->key, HASH_INPUT_SIZE, cx->ctx); - - /* mark as now in data mode */ - cx->klen = HMAC_IN_DATA; - } - - /* hash the data (if any) */ - if(data_len) - sha_hash(data, data_len, cx->ctx); -} - -/* compute and output the MAC value */ -void hmac_sha_end(unsigned char mac[], unsigned long mac_len, hmac_ctx cx[1]) -{ unsigned char dig[HASH_OUTPUT_SIZE]; - unsigned int i; - - /* if no data has been entered perform a null data phase */ - if(cx->klen != HMAC_IN_DATA) - hmac_sha_data((const unsigned char*)0, 0, cx); - - sha_end(dig, cx->ctx); /* complete the inner hash */ - - /* set outer key value using opad and removing ipad */ - for(i = 0; i < (HASH_INPUT_SIZE >> 2); ++i) - ((uint_32t*)cx->key)[i] ^= 0x36363636 ^ 0x5c5c5c5c; - - /* perform the outer hash operation */ - sha_begin(cx->ctx); - sha_hash(cx->key, HASH_INPUT_SIZE, cx->ctx); - sha_hash(dig, HASH_OUTPUT_SIZE, cx->ctx); - sha_end(dig, cx->ctx); - - /* output the hash value */ - for(i = 0; i < mac_len; ++i) - mac[i] = dig[i]; -} - -/* 'do it all in one go' subroutine */ -void hmac_sha(const unsigned char key[], unsigned long key_len, - const unsigned char data[], unsigned long data_len, - unsigned char mac[], unsigned long mac_len) -{ hmac_ctx cx[1]; - - hmac_sha_begin(cx); - hmac_sha_key(key, key_len, cx); - hmac_sha_data(data, data_len, cx); - hmac_sha_end(mac, mac_len, cx); -} - -#if defined(__cplusplus) -} -#endif +/* + --------------------------------------------------------------------------- + Copyright (c) 2002, Dr Brian Gladman, Worcester, UK. All rights reserved. + + LICENSE TERMS + + The free distribution and use of this software in both source and binary + form is allowed (with or without changes) provided that: + + 1. distributions of this source code include the above copyright + notice, this list of conditions and the following disclaimer; + + 2. distributions in binary form include the above copyright + notice, this list of conditions and the following disclaimer + in the documentation and/or other associated materials; + + 3. the copyright holder's name is not used to endorse products + built using this software without specific written permission. + + ALTERNATIVELY, provided that this notice is retained in full, this product + may be distributed under the terms of the GNU General Public License (GPL), + in which case the provisions of the GPL apply INSTEAD OF those given above. + + DISCLAIMER + + This software is provided 'as is' with no explicit or implied warranties + in respect of its properties, including, but not limited to, correctness + and/or fitness for purpose. + --------------------------------------------------------------------------- + Issue Date: 26/08/2003 + + This is an implementation of HMAC, the FIPS standard keyed hash function +*/ + +#include "hmac.h" +#include "brg_types.h" + +#if defined(__cplusplus) +extern "C" +{ +#endif + +/* initialise the HMAC context to zero */ +void hmac_sha_begin(hmac_ctx cx[1]) +{ + memset(cx, 0, sizeof(hmac_ctx)); +} + +/* input the HMAC key (can be called multiple times) */ +int hmac_sha_key(const unsigned char key[], unsigned long key_len, hmac_ctx cx[1]) +{ + if(cx->klen == HMAC_IN_DATA) /* error if further key input */ + return HMAC_BAD_MODE; /* is attempted in data mode */ + + if(cx->klen + key_len > HASH_INPUT_SIZE) /* if the key has to be hashed */ + { + if(cx->klen <= HASH_INPUT_SIZE) /* if the hash has not yet been */ + { /* started, initialise it and */ + sha_begin(cx->ctx); /* hash stored key characters */ + sha_hash(cx->key, cx->klen, cx->ctx); + } + + sha_hash(key, key_len, cx->ctx); /* hash long key data into hash */ + } + else /* otherwise store key data */ + memcpy(cx->key + cx->klen, key, key_len); + + cx->klen += key_len; /* update the key length count */ + return HMAC_OK; +} + +/* input the HMAC data (can be called multiple times) - */ +/* note that this call terminates the key input phase */ +void hmac_sha_data(const unsigned char data[], unsigned long data_len, hmac_ctx cx[1]) +{ unsigned int i; + + if(cx->klen != HMAC_IN_DATA) /* if not yet in data phase */ + { + if(cx->klen > HASH_INPUT_SIZE) /* if key is being hashed */ + { /* complete the hash and */ + sha_end(cx->key, cx->ctx); /* store the result as the */ + cx->klen = HASH_OUTPUT_SIZE; /* key and set new length */ + } + + /* pad the key if necessary */ + memset(cx->key + cx->klen, 0, HASH_INPUT_SIZE - cx->klen); + + /* xor ipad into key value */ + for(i = 0; i < (HASH_INPUT_SIZE >> 2); ++i) + ((uint_32t*)cx->key)[i] ^= 0x36363636; + + /* and start hash operation */ + sha_begin(cx->ctx); + sha_hash(cx->key, HASH_INPUT_SIZE, cx->ctx); + + /* mark as now in data mode */ + cx->klen = HMAC_IN_DATA; + } + + /* hash the data (if any) */ + if(data_len) + sha_hash(data, data_len, cx->ctx); +} + +/* compute and output the MAC value */ +void hmac_sha_end(unsigned char mac[], unsigned long mac_len, hmac_ctx cx[1]) +{ unsigned char dig[HASH_OUTPUT_SIZE]; + unsigned int i; + + /* if no data has been entered perform a null data phase */ + if(cx->klen != HMAC_IN_DATA) + hmac_sha_data((const unsigned char*)0, 0, cx); + + sha_end(dig, cx->ctx); /* complete the inner hash */ + + /* set outer key value using opad and removing ipad */ + for(i = 0; i < (HASH_INPUT_SIZE >> 2); ++i) + ((uint_32t*)cx->key)[i] ^= 0x36363636 ^ 0x5c5c5c5c; + + /* perform the outer hash operation */ + sha_begin(cx->ctx); + sha_hash(cx->key, HASH_INPUT_SIZE, cx->ctx); + sha_hash(dig, HASH_OUTPUT_SIZE, cx->ctx); + sha_end(dig, cx->ctx); + + /* output the hash value */ + for(i = 0; i < mac_len; ++i) + mac[i] = dig[i]; +} + +/* 'do it all in one go' subroutine */ +void hmac_sha(const unsigned char key[], unsigned long key_len, + const unsigned char data[], unsigned long data_len, + unsigned char mac[], unsigned long mac_len) +{ hmac_ctx cx[1]; + + hmac_sha_begin(cx); + hmac_sha_key(key, key_len, cx); + hmac_sha_data(data, data_len, cx); + hmac_sha_end(mac, mac_len, cx); +} + +#if defined(__cplusplus) +} +#endif diff --git a/Zip/aes/hmac.h b/Zip/minizip/aes/hmac.h similarity index 96% rename from Zip/aes/hmac.h rename to Zip/minizip/aes/hmac.h index 643037c922a0aeb2f9ea61f393c668c72f2c6fec..419cc15f44ebd8e416e9e3e60dae6877df7c1a1e 100755 --- a/Zip/aes/hmac.h +++ b/Zip/minizip/aes/hmac.h @@ -1,103 +1,103 @@ -/* - --------------------------------------------------------------------------- - Copyright (c) 2002, Dr Brian Gladman, Worcester, UK. All rights reserved. - - LICENSE TERMS - - The free distribution and use of this software in both source and binary - form is allowed (with or without changes) provided that: - - 1. distributions of this source code include the above copyright - notice, this list of conditions and the following disclaimer; - - 2. distributions in binary form include the above copyright - notice, this list of conditions and the following disclaimer - in the documentation and/or other associated materials; - - 3. the copyright holder's name is not used to endorse products - built using this software without specific written permission. - - ALTERNATIVELY, provided that this notice is retained in full, this product - may be distributed under the terms of the GNU General Public License (GPL), - in which case the provisions of the GPL apply INSTEAD OF those given above. - - DISCLAIMER - - This software is provided 'as is' with no explicit or implied warranties - in respect of its properties, including, but not limited to, correctness - and/or fitness for purpose. - --------------------------------------------------------------------------- - Issue Date: 26/08/2003 - - This is an implementation of HMAC, the FIPS standard keyed hash function -*/ - -#ifndef _HMAC_H -#define _HMAC_H - -#include - -#if defined(__cplusplus) -extern "C" -{ -#endif - -#define USE_SHA1 - -#if !defined(USE_SHA1) && !defined(USE_SHA256) -#error define USE_SHA1 or USE_SHA256 to set the HMAC hash algorithm -#endif - -#ifdef USE_SHA1 - -#include "sha1.h" - -#define HASH_INPUT_SIZE SHA1_BLOCK_SIZE -#define HASH_OUTPUT_SIZE SHA1_DIGEST_SIZE -#define sha_ctx sha1_ctx -#define sha_begin sha1_begin -#define sha_hash sha1_hash -#define sha_end sha1_end - -#endif - -#ifdef USE_SHA256 - -#include "sha2.h" - -#define HASH_INPUT_SIZE SHA256_BLOCK_SIZE -#define HASH_OUTPUT_SIZE SHA256_DIGEST_SIZE -#define sha_ctx sha256_ctx -#define sha_begin sha256_begin -#define sha_hash sha256_hash -#define sha_end sha256_end - -#endif - -#define HMAC_OK 0 -#define HMAC_BAD_MODE -1 -#define HMAC_IN_DATA 0xffffffff - -typedef struct -{ unsigned char key[HASH_INPUT_SIZE]; - sha_ctx ctx[1]; - unsigned long klen; -} hmac_ctx; - -void hmac_sha_begin(hmac_ctx cx[1]); - -int hmac_sha_key(const unsigned char key[], unsigned long key_len, hmac_ctx cx[1]); - -void hmac_sha_data(const unsigned char data[], unsigned long data_len, hmac_ctx cx[1]); - -void hmac_sha_end(unsigned char mac[], unsigned long mac_len, hmac_ctx cx[1]); - -void hmac_sha(const unsigned char key[], unsigned long key_len, - const unsigned char data[], unsigned long data_len, - unsigned char mac[], unsigned long mac_len); - -#if defined(__cplusplus) -} -#endif - -#endif +/* + --------------------------------------------------------------------------- + Copyright (c) 2002, Dr Brian Gladman, Worcester, UK. All rights reserved. + + LICENSE TERMS + + The free distribution and use of this software in both source and binary + form is allowed (with or without changes) provided that: + + 1. distributions of this source code include the above copyright + notice, this list of conditions and the following disclaimer; + + 2. distributions in binary form include the above copyright + notice, this list of conditions and the following disclaimer + in the documentation and/or other associated materials; + + 3. the copyright holder's name is not used to endorse products + built using this software without specific written permission. + + ALTERNATIVELY, provided that this notice is retained in full, this product + may be distributed under the terms of the GNU General Public License (GPL), + in which case the provisions of the GPL apply INSTEAD OF those given above. + + DISCLAIMER + + This software is provided 'as is' with no explicit or implied warranties + in respect of its properties, including, but not limited to, correctness + and/or fitness for purpose. + --------------------------------------------------------------------------- + Issue Date: 26/08/2003 + + This is an implementation of HMAC, the FIPS standard keyed hash function +*/ + +#ifndef _HMAC_H +#define _HMAC_H + +#include + +#if defined(__cplusplus) +extern "C" +{ +#endif + +#define USE_SHA1 + +#if !defined(USE_SHA1) && !defined(USE_SHA256) +#error define USE_SHA1 or USE_SHA256 to set the HMAC hash algorithm +#endif + +#ifdef USE_SHA1 + +#include "sha1.h" + +#define HASH_INPUT_SIZE SHA1_BLOCK_SIZE +#define HASH_OUTPUT_SIZE SHA1_DIGEST_SIZE +#define sha_ctx sha1_ctx +#define sha_begin sha1_begin +#define sha_hash sha1_hash +#define sha_end sha1_end + +#endif + +#ifdef USE_SHA256 + +#include "sha2.h" + +#define HASH_INPUT_SIZE SHA256_BLOCK_SIZE +#define HASH_OUTPUT_SIZE SHA256_DIGEST_SIZE +#define sha_ctx sha256_ctx +#define sha_begin sha256_begin +#define sha_hash sha256_hash +#define sha_end sha256_end + +#endif + +#define HMAC_OK 0 +#define HMAC_BAD_MODE -1 +#define HMAC_IN_DATA 0xffffffff + +typedef struct +{ unsigned char key[HASH_INPUT_SIZE]; + sha_ctx ctx[1]; + unsigned long klen; +} hmac_ctx; + +void hmac_sha_begin(hmac_ctx cx[1]); + +int hmac_sha_key(const unsigned char key[], unsigned long key_len, hmac_ctx cx[1]); + +void hmac_sha_data(const unsigned char data[], unsigned long data_len, hmac_ctx cx[1]); + +void hmac_sha_end(unsigned char mac[], unsigned long mac_len, hmac_ctx cx[1]); + +void hmac_sha(const unsigned char key[], unsigned long key_len, + const unsigned char data[], unsigned long data_len, + unsigned char mac[], unsigned long mac_len); + +#if defined(__cplusplus) +} +#endif + +#endif diff --git a/Zip/aes/prng.c b/Zip/minizip/aes/prng.c similarity index 97% rename from Zip/aes/prng.c rename to Zip/minizip/aes/prng.c index 2f910907c3729f5775cd96f6b4025780e0c68c6b..14fc09d4eff29aa8242dc88923a9c67924202dd1 100755 --- a/Zip/aes/prng.c +++ b/Zip/minizip/aes/prng.c @@ -1,155 +1,155 @@ -/* - --------------------------------------------------------------------------- - Copyright (c) 2002, Dr Brian Gladman < >, Worcester, UK. - All rights reserved. - - LICENSE TERMS - - The free distribution and use of this software in both source and binary - form is allowed (with or without changes) provided that: - - 1. distributions of this source code include the above copyright - notice, this list of conditions and the following disclaimer; - - 2. distributions in binary form include the above copyright - notice, this list of conditions and the following disclaimer - in the documentation and/or other associated materials; - - 3. the copyright holder's name is not used to endorse products - built using this software without specific written permission. - - ALTERNATIVELY, provided that this notice is retained in full, this product - may be distributed under the terms of the GNU General Public License (GPL), - in which case the provisions of the GPL apply INSTEAD OF those given above. - - DISCLAIMER - - This software is provided 'as is' with no explicit or implied warranties - in respect of its properties, including, but not limited to, correctness - and/or fitness for purpose. - --------------------------------------------------------------------------- - Issue Date: 24/01/2003 - - This file implements a random data pool based on the use of an external - entropy function. It is based on the ideas advocated by Peter Gutmann in - his work on pseudo random sequence generators. It is not a 'paranoid' - random sequence generator and no attempt is made to protect the pool - from prying eyes either by memory locking or by techniques to obscure - its location in memory. -*/ - -#include -#include "prng.h" - -#if defined(__cplusplus) -extern "C" -{ -#endif - -/* mix a random data pool using the SHA1 compression function (as */ -/* suggested by Peter Gutmann in his paper on random pools) */ - -static void prng_mix(unsigned char buf[]) -{ unsigned int i, len; - sha1_ctx ctx[1]; - - /*lint -e{663} unusual array to pointer conversion */ - for(i = 0; i < PRNG_POOL_SIZE; i += SHA1_DIGEST_SIZE) - { - /* copy digest size pool block into SHA1 hash block */ - memcpy(ctx->hash, buf + (i ? i : PRNG_POOL_SIZE) - - SHA1_DIGEST_SIZE, SHA1_DIGEST_SIZE); - - /* copy data from pool into the SHA1 data buffer */ - len = PRNG_POOL_SIZE - i; - memcpy(ctx->wbuf, buf + i, (len > SHA1_BLOCK_SIZE ? SHA1_BLOCK_SIZE : len)); - - if(len < SHA1_BLOCK_SIZE) - memcpy(((char*)ctx->wbuf) + len, buf, SHA1_BLOCK_SIZE - len); - - /* compress using the SHA1 compression function */ - sha1_compile(ctx); - - /* put digest size block back into the random pool */ - memcpy(buf + i, ctx->hash, SHA1_DIGEST_SIZE); - } -} - -/* refresh the output buffer and update the random pool by adding */ -/* entropy and remixing */ - -static void update_pool(prng_ctx ctx[1]) -{ unsigned int i = 0; - - /* transfer random pool data to the output buffer */ - memcpy(ctx->obuf, ctx->rbuf, PRNG_POOL_SIZE); - - /* enter entropy data into the pool */ - while(i < PRNG_POOL_SIZE) - i += ctx->entropy(ctx->rbuf + i, PRNG_POOL_SIZE - i); - - /* invert and xor the original pool data into the pool */ - for(i = 0; i < PRNG_POOL_SIZE; ++i) - ctx->rbuf[i] ^= ~ctx->obuf[i]; - - /* mix the pool and the output buffer */ - prng_mix(ctx->rbuf); - prng_mix(ctx->obuf); -} - -void prng_init(prng_entropy_fn fun, prng_ctx ctx[1]) -{ int i; - - /* clear the buffers and the counter in the context */ - memset(ctx, 0, sizeof(prng_ctx)); - - /* set the pointer to the entropy collection function */ - ctx->entropy = fun; - - /* initialise the random data pool */ - update_pool(ctx); - - /* mix the pool a minimum number of times */ - for(i = 0; i < PRNG_MIN_MIX; ++i) - prng_mix(ctx->rbuf); - - /* update the pool to prime the pool output buffer */ - update_pool(ctx); -} - -/* provide random bytes from the random data pool */ - -void prng_rand(unsigned char data[], unsigned int data_len, prng_ctx ctx[1]) -{ unsigned char *rp = data; - unsigned int len, pos = ctx->pos; - - while(data_len) - { - /* transfer 'data_len' bytes (or the number of bytes remaining */ - /* the pool output buffer if less) into the output */ - len = (data_len < PRNG_POOL_SIZE - pos ? data_len : PRNG_POOL_SIZE - pos); - memcpy(rp, ctx->obuf + pos, len); - rp += len; /* update ouput buffer position pointer */ - pos += len; /* update pool output buffer pointer */ - data_len -= len; /* update the remaining data count */ - - /* refresh the random pool if necessary */ - if(pos == PRNG_POOL_SIZE) - { - update_pool(ctx); pos = 0; - } - } - - ctx->pos = pos; -} - -void prng_end(prng_ctx ctx[1]) -{ - /* ensure the data in the context is destroyed */ - memset(ctx, 0, sizeof(prng_ctx)); -} - -#if defined(__cplusplus) -} -#endif - +/* + --------------------------------------------------------------------------- + Copyright (c) 2002, Dr Brian Gladman < >, Worcester, UK. + All rights reserved. + + LICENSE TERMS + + The free distribution and use of this software in both source and binary + form is allowed (with or without changes) provided that: + + 1. distributions of this source code include the above copyright + notice, this list of conditions and the following disclaimer; + + 2. distributions in binary form include the above copyright + notice, this list of conditions and the following disclaimer + in the documentation and/or other associated materials; + + 3. the copyright holder's name is not used to endorse products + built using this software without specific written permission. + + ALTERNATIVELY, provided that this notice is retained in full, this product + may be distributed under the terms of the GNU General Public License (GPL), + in which case the provisions of the GPL apply INSTEAD OF those given above. + + DISCLAIMER + + This software is provided 'as is' with no explicit or implied warranties + in respect of its properties, including, but not limited to, correctness + and/or fitness for purpose. + --------------------------------------------------------------------------- + Issue Date: 24/01/2003 + + This file implements a random data pool based on the use of an external + entropy function. It is based on the ideas advocated by Peter Gutmann in + his work on pseudo random sequence generators. It is not a 'paranoid' + random sequence generator and no attempt is made to protect the pool + from prying eyes either by memory locking or by techniques to obscure + its location in memory. +*/ + +#include +#include "prng.h" + +#if defined(__cplusplus) +extern "C" +{ +#endif + +/* mix a random data pool using the SHA1 compression function (as */ +/* suggested by Peter Gutmann in his paper on random pools) */ + +static void prng_mix(unsigned char buf[]) +{ unsigned int i, len; + sha1_ctx ctx[1]; + + /*lint -e{663} unusual array to pointer conversion */ + for(i = 0; i < PRNG_POOL_SIZE; i += SHA1_DIGEST_SIZE) + { + /* copy digest size pool block into SHA1 hash block */ + memcpy(ctx->hash, buf + (i ? i : PRNG_POOL_SIZE) + - SHA1_DIGEST_SIZE, SHA1_DIGEST_SIZE); + + /* copy data from pool into the SHA1 data buffer */ + len = PRNG_POOL_SIZE - i; + memcpy(ctx->wbuf, buf + i, (len > SHA1_BLOCK_SIZE ? SHA1_BLOCK_SIZE : len)); + + if(len < SHA1_BLOCK_SIZE) + memcpy(((char*)ctx->wbuf) + len, buf, SHA1_BLOCK_SIZE - len); + + /* compress using the SHA1 compression function */ + sha1_compile(ctx); + + /* put digest size block back into the random pool */ + memcpy(buf + i, ctx->hash, SHA1_DIGEST_SIZE); + } +} + +/* refresh the output buffer and update the random pool by adding */ +/* entropy and remixing */ + +static void update_pool(prng_ctx ctx[1]) +{ unsigned int i = 0; + + /* transfer random pool data to the output buffer */ + memcpy(ctx->obuf, ctx->rbuf, PRNG_POOL_SIZE); + + /* enter entropy data into the pool */ + while(i < PRNG_POOL_SIZE) + i += ctx->entropy(ctx->rbuf + i, PRNG_POOL_SIZE - i); + + /* invert and xor the original pool data into the pool */ + for(i = 0; i < PRNG_POOL_SIZE; ++i) + ctx->rbuf[i] ^= ~ctx->obuf[i]; + + /* mix the pool and the output buffer */ + prng_mix(ctx->rbuf); + prng_mix(ctx->obuf); +} + +void prng_init(prng_entropy_fn fun, prng_ctx ctx[1]) +{ int i; + + /* clear the buffers and the counter in the context */ + memset(ctx, 0, sizeof(prng_ctx)); + + /* set the pointer to the entropy collection function */ + ctx->entropy = fun; + + /* initialise the random data pool */ + update_pool(ctx); + + /* mix the pool a minimum number of times */ + for(i = 0; i < PRNG_MIN_MIX; ++i) + prng_mix(ctx->rbuf); + + /* update the pool to prime the pool output buffer */ + update_pool(ctx); +} + +/* provide random bytes from the random data pool */ + +void prng_rand(unsigned char data[], unsigned int data_len, prng_ctx ctx[1]) +{ unsigned char *rp = data; + unsigned int len, pos = ctx->pos; + + while(data_len) + { + /* transfer 'data_len' bytes (or the number of bytes remaining */ + /* the pool output buffer if less) into the output */ + len = (data_len < PRNG_POOL_SIZE - pos ? data_len : PRNG_POOL_SIZE - pos); + memcpy(rp, ctx->obuf + pos, len); + rp += len; /* update ouput buffer position pointer */ + pos += len; /* update pool output buffer pointer */ + data_len -= len; /* update the remaining data count */ + + /* refresh the random pool if necessary */ + if(pos == PRNG_POOL_SIZE) + { + update_pool(ctx); pos = 0; + } + } + + ctx->pos = pos; +} + +void prng_end(prng_ctx ctx[1]) +{ + /* ensure the data in the context is destroyed */ + memset(ctx, 0, sizeof(prng_ctx)); +} + +#if defined(__cplusplus) +} +#endif + diff --git a/Zip/aes/prng.h b/Zip/minizip/aes/prng.h similarity index 97% rename from Zip/aes/prng.h rename to Zip/minizip/aes/prng.h index f934b51921d99d6bde88363ce7d5d7aeba8c5a3a..9a7742675a6b1420993781fcbe6cace0001e53ec 100755 --- a/Zip/aes/prng.h +++ b/Zip/minizip/aes/prng.h @@ -1,82 +1,82 @@ -/* - --------------------------------------------------------------------------- - Copyright (c) 2002, Dr Brian Gladman < >, Worcester, UK. - All rights reserved. - - LICENSE TERMS - - The free distribution and use of this software in both source and binary - form is allowed (with or without changes) provided that: - - 1. distributions of this source code include the above copyright - notice, this list of conditions and the following disclaimer; - - 2. distributions in binary form include the above copyright - notice, this list of conditions and the following disclaimer - in the documentation and/or other associated materials; - - 3. the copyright holder's name is not used to endorse products - built using this software without specific written permission. - - ALTERNATIVELY, provided that this notice is retained in full, this product - may be distributed under the terms of the GNU General Public License (GPL), - in which case the provisions of the GPL apply INSTEAD OF those given above. - - DISCLAIMER - - This software is provided 'as is' with no explicit or implied warranties - in respect of its properties, including, but not limited to, correctness - and/or fitness for purpose. - --------------------------------------------------------------------------- - Issue Date: 24/01/2003 - - This is the header file for an implementation of a random data pool based on - the use of an external entropy function (inspired by Peter Gutmann's work). -*/ - -#ifndef _PRNG_H -#define _PRNG_H - -#include "sha1.h" - -#define PRNG_POOL_LEN 256 /* minimum random pool size */ -#define PRNG_MIN_MIX 20 /* min initial pool mixing iterations */ - -/* ensure that pool length is a multiple of the SHA1 digest size */ - -#define PRNG_POOL_SIZE (SHA1_DIGEST_SIZE * (1 + (PRNG_POOL_LEN - 1) / SHA1_DIGEST_SIZE)) - -#if defined(__cplusplus) -extern "C" -{ -#endif - -/* A function for providing entropy is a parameter in the prng_init() */ -/* call. This function has the following form and returns a maximum */ -/* of 'len' bytes of pseudo random data in the buffer 'buf'. It can */ -/* return less than 'len' bytes but will be repeatedly called for more */ -/* data in this case. */ - -typedef int (*prng_entropy_fn)(unsigned char buf[], unsigned int len); - -typedef struct -{ unsigned char rbuf[PRNG_POOL_SIZE]; /* the random pool */ - unsigned char obuf[PRNG_POOL_SIZE]; /* pool output buffer */ - unsigned int pos; /* output buffer position */ - prng_entropy_fn entropy; /* entropy function pointer */ -} prng_ctx; - -/* initialise the random stream generator */ -void prng_init(prng_entropy_fn fun, prng_ctx ctx[1]); - -/* obtain random bytes from the generator */ -void prng_rand(unsigned char data[], unsigned int data_len, prng_ctx ctx[1]); - -/* close the random stream generator */ -void prng_end(prng_ctx ctx[1]); - -#if defined(__cplusplus) -} -#endif - -#endif +/* + --------------------------------------------------------------------------- + Copyright (c) 2002, Dr Brian Gladman < >, Worcester, UK. + All rights reserved. + + LICENSE TERMS + + The free distribution and use of this software in both source and binary + form is allowed (with or without changes) provided that: + + 1. distributions of this source code include the above copyright + notice, this list of conditions and the following disclaimer; + + 2. distributions in binary form include the above copyright + notice, this list of conditions and the following disclaimer + in the documentation and/or other associated materials; + + 3. the copyright holder's name is not used to endorse products + built using this software without specific written permission. + + ALTERNATIVELY, provided that this notice is retained in full, this product + may be distributed under the terms of the GNU General Public License (GPL), + in which case the provisions of the GPL apply INSTEAD OF those given above. + + DISCLAIMER + + This software is provided 'as is' with no explicit or implied warranties + in respect of its properties, including, but not limited to, correctness + and/or fitness for purpose. + --------------------------------------------------------------------------- + Issue Date: 24/01/2003 + + This is the header file for an implementation of a random data pool based on + the use of an external entropy function (inspired by Peter Gutmann's work). +*/ + +#ifndef _PRNG_H +#define _PRNG_H + +#include "sha1.h" + +#define PRNG_POOL_LEN 256 /* minimum random pool size */ +#define PRNG_MIN_MIX 20 /* min initial pool mixing iterations */ + +/* ensure that pool length is a multiple of the SHA1 digest size */ + +#define PRNG_POOL_SIZE (SHA1_DIGEST_SIZE * (1 + (PRNG_POOL_LEN - 1) / SHA1_DIGEST_SIZE)) + +#if defined(__cplusplus) +extern "C" +{ +#endif + +/* A function for providing entropy is a parameter in the prng_init() */ +/* call. This function has the following form and returns a maximum */ +/* of 'len' bytes of pseudo random data in the buffer 'buf'. It can */ +/* return less than 'len' bytes but will be repeatedly called for more */ +/* data in this case. */ + +typedef int (*prng_entropy_fn)(unsigned char buf[], unsigned int len); + +typedef struct +{ unsigned char rbuf[PRNG_POOL_SIZE]; /* the random pool */ + unsigned char obuf[PRNG_POOL_SIZE]; /* pool output buffer */ + unsigned int pos; /* output buffer position */ + prng_entropy_fn entropy; /* entropy function pointer */ +} prng_ctx; + +/* initialise the random stream generator */ +void prng_init(prng_entropy_fn fun, prng_ctx ctx[1]); + +/* obtain random bytes from the generator */ +void prng_rand(unsigned char data[], unsigned int data_len, prng_ctx ctx[1]); + +/* close the random stream generator */ +void prng_end(prng_ctx ctx[1]); + +#if defined(__cplusplus) +} +#endif + +#endif diff --git a/Zip/aes/pwd2key.c b/Zip/minizip/aes/pwd2key.c similarity index 96% rename from Zip/aes/pwd2key.c rename to Zip/minizip/aes/pwd2key.c index 80a47603d8b7bc191f6cb8d6654535bab19a9768..d3c6abbf414dd05f7d6852d31285222bc68a9165 100755 --- a/Zip/aes/pwd2key.c +++ b/Zip/minizip/aes/pwd2key.c @@ -1,193 +1,193 @@ -/* - --------------------------------------------------------------------------- - Copyright (c) 2002, Dr Brian Gladman, Worcester, UK. All rights reserved. - - LICENSE TERMS - - The free distribution and use of this software in both source and binary - form is allowed (with or without changes) provided that: - - 1. distributions of this source code include the above copyright - notice, this list of conditions and the following disclaimer; - - 2. distributions in binary form include the above copyright - notice, this list of conditions and the following disclaimer - in the documentation and/or other associated materials; - - 3. the copyright holder's name is not used to endorse products - built using this software without specific written permission. - - ALTERNATIVELY, provided that this notice is retained in full, this product - may be distributed under the terms of the GNU General Public License (GPL), - in which case the provisions of the GPL apply INSTEAD OF those given above. - - DISCLAIMER - - This software is provided 'as is' with no explicit or implied warranties - in respect of its properties, including, but not limited to, correctness - and/or fitness for purpose. - --------------------------------------------------------------------------- - Issue Date: 26/08/2003 - - This is an implementation of RFC2898, which specifies key derivation from - a password and a salt value. -*/ - -#include -#include "hmac.h" - -#if defined(__cplusplus) -extern "C" -{ -#endif - -void derive_key(const unsigned char pwd[], /* the PASSWORD */ - unsigned int pwd_len, /* and its length */ - const unsigned char salt[], /* the SALT and its */ - unsigned int salt_len, /* length */ - unsigned int iter, /* the number of iterations */ - unsigned char key[], /* space for the output key */ - unsigned int key_len)/* and its required length */ -{ - unsigned int i, j, k, n_blk; - unsigned char uu[HASH_OUTPUT_SIZE], ux[HASH_OUTPUT_SIZE]; - hmac_ctx c1[1], c2[1], c3[1]; - - /* set HMAC context (c1) for password */ - hmac_sha_begin(c1); - hmac_sha_key(pwd, pwd_len, c1); - - /* set HMAC context (c2) for password and salt */ - memcpy(c2, c1, sizeof(hmac_ctx)); - hmac_sha_data(salt, salt_len, c2); - - /* find the number of SHA blocks in the key */ - n_blk = 1 + (key_len - 1) / HASH_OUTPUT_SIZE; - - for(i = 0; i < n_blk; ++i) /* for each block in key */ - { - /* ux[] holds the running xor value */ - memset(ux, 0, HASH_OUTPUT_SIZE); - - /* set HMAC context (c3) for password and salt */ - memcpy(c3, c2, sizeof(hmac_ctx)); - - /* enter additional data for 1st block into uu */ - uu[0] = (unsigned char)((i + 1) >> 24); - uu[1] = (unsigned char)((i + 1) >> 16); - uu[2] = (unsigned char)((i + 1) >> 8); - uu[3] = (unsigned char)(i + 1); - - /* this is the key mixing iteration */ - for(j = 0, k = 4; j < iter; ++j) - { - /* add previous round data to HMAC */ - hmac_sha_data(uu, k, c3); - - /* obtain HMAC for uu[] */ - hmac_sha_end(uu, HASH_OUTPUT_SIZE, c3); - - /* xor into the running xor block */ - for(k = 0; k < HASH_OUTPUT_SIZE; ++k) - ux[k] ^= uu[k]; - - /* set HMAC context (c3) for password */ - memcpy(c3, c1, sizeof(hmac_ctx)); - } - - /* compile key blocks into the key output */ - j = 0; k = i * HASH_OUTPUT_SIZE; - while(j < HASH_OUTPUT_SIZE && k < key_len) - key[k++] = ux[j++]; - } -} - -#ifdef TEST - -#include - -struct -{ unsigned int pwd_len; - unsigned int salt_len; - unsigned int it_count; - unsigned char *pwd; - unsigned char salt[32]; - unsigned char key[32]; -} tests[] = -{ - { 8, 4, 5, (unsigned char*)"password", - { - 0x12, 0x34, 0x56, 0x78 - }, - { - 0x5c, 0x75, 0xce, 0xf0, 0x1a, 0x96, 0x0d, 0xf7, - 0x4c, 0xb6, 0xb4, 0x9b, 0x9e, 0x38, 0xe6, 0xb5 - } - }, - { 8, 8, 5, (unsigned char*)"password", - { - 0x12, 0x34, 0x56, 0x78, 0x78, 0x56, 0x34, 0x12 - }, - { - 0xd1, 0xda, 0xa7, 0x86, 0x15, 0xf2, 0x87, 0xe6, - 0xa1, 0xc8, 0xb1, 0x20, 0xd7, 0x06, 0x2a, 0x49 - } - }, - { 8, 21, 1, (unsigned char*)"password", - { - "ATHENA.MIT.EDUraeburn" - }, - { - 0xcd, 0xed, 0xb5, 0x28, 0x1b, 0xb2, 0xf8, 0x01, - 0x56, 0x5a, 0x11, 0x22, 0xb2, 0x56, 0x35, 0x15 - } - }, - { 8, 21, 2, (unsigned char*)"password", - { - "ATHENA.MIT.EDUraeburn" - }, - { - 0x01, 0xdb, 0xee, 0x7f, 0x4a, 0x9e, 0x24, 0x3e, - 0x98, 0x8b, 0x62, 0xc7, 0x3c, 0xda, 0x93, 0x5d - } - }, - { 8, 21, 1200, (unsigned char*)"password", - { - "ATHENA.MIT.EDUraeburn" - }, - { - 0x5c, 0x08, 0xeb, 0x61, 0xfd, 0xf7, 0x1e, 0x4e, - 0x4e, 0xc3, 0xcf, 0x6b, 0xa1, 0xf5, 0x51, 0x2b - } - } -}; - -int main() -{ unsigned int i, j, key_len = 256; - unsigned char key[256]; - - printf("\nTest of RFC2898 Password Based Key Derivation"); - for(i = 0; i < 5; ++i) - { - derive_key(tests[i].pwd, tests[i].pwd_len, tests[i].salt, - tests[i].salt_len, tests[i].it_count, key, key_len); - - printf("\ntest %i: ", i + 1); - printf("key %s", memcmp(tests[i].key, key, 16) ? "is bad" : "is good"); - for(j = 0; j < key_len && j < 64; j += 4) - { - if(j % 16 == 0) - printf("\n"); - printf("0x%02x%02x%02x%02x ", key[j], key[j + 1], key[j + 2], key[j + 3]); - } - printf(j < key_len ? " ... \n" : "\n"); - } - printf("\n"); - return 0; -} - -#if defined(__cplusplus) -} -#endif - -#endif +/* + --------------------------------------------------------------------------- + Copyright (c) 2002, Dr Brian Gladman, Worcester, UK. All rights reserved. + + LICENSE TERMS + + The free distribution and use of this software in both source and binary + form is allowed (with or without changes) provided that: + + 1. distributions of this source code include the above copyright + notice, this list of conditions and the following disclaimer; + + 2. distributions in binary form include the above copyright + notice, this list of conditions and the following disclaimer + in the documentation and/or other associated materials; + + 3. the copyright holder's name is not used to endorse products + built using this software without specific written permission. + + ALTERNATIVELY, provided that this notice is retained in full, this product + may be distributed under the terms of the GNU General Public License (GPL), + in which case the provisions of the GPL apply INSTEAD OF those given above. + + DISCLAIMER + + This software is provided 'as is' with no explicit or implied warranties + in respect of its properties, including, but not limited to, correctness + and/or fitness for purpose. + --------------------------------------------------------------------------- + Issue Date: 26/08/2003 + + This is an implementation of RFC2898, which specifies key derivation from + a password and a salt value. +*/ + +#include +#include "hmac.h" + +#if defined(__cplusplus) +extern "C" +{ +#endif + +void derive_key(const unsigned char pwd[], /* the PASSWORD */ + unsigned int pwd_len, /* and its length */ + const unsigned char salt[], /* the SALT and its */ + unsigned int salt_len, /* length */ + unsigned int iter, /* the number of iterations */ + unsigned char key[], /* space for the output key */ + unsigned int key_len)/* and its required length */ +{ + unsigned int i, j, k, n_blk; + unsigned char uu[HASH_OUTPUT_SIZE], ux[HASH_OUTPUT_SIZE]; + hmac_ctx c1[1], c2[1], c3[1]; + + /* set HMAC context (c1) for password */ + hmac_sha_begin(c1); + hmac_sha_key(pwd, pwd_len, c1); + + /* set HMAC context (c2) for password and salt */ + memcpy(c2, c1, sizeof(hmac_ctx)); + hmac_sha_data(salt, salt_len, c2); + + /* find the number of SHA blocks in the key */ + n_blk = 1 + (key_len - 1) / HASH_OUTPUT_SIZE; + + for(i = 0; i < n_blk; ++i) /* for each block in key */ + { + /* ux[] holds the running xor value */ + memset(ux, 0, HASH_OUTPUT_SIZE); + + /* set HMAC context (c3) for password and salt */ + memcpy(c3, c2, sizeof(hmac_ctx)); + + /* enter additional data for 1st block into uu */ + uu[0] = (unsigned char)((i + 1) >> 24); + uu[1] = (unsigned char)((i + 1) >> 16); + uu[2] = (unsigned char)((i + 1) >> 8); + uu[3] = (unsigned char)(i + 1); + + /* this is the key mixing iteration */ + for(j = 0, k = 4; j < iter; ++j) + { + /* add previous round data to HMAC */ + hmac_sha_data(uu, k, c3); + + /* obtain HMAC for uu[] */ + hmac_sha_end(uu, HASH_OUTPUT_SIZE, c3); + + /* xor into the running xor block */ + for(k = 0; k < HASH_OUTPUT_SIZE; ++k) + ux[k] ^= uu[k]; + + /* set HMAC context (c3) for password */ + memcpy(c3, c1, sizeof(hmac_ctx)); + } + + /* compile key blocks into the key output */ + j = 0; k = i * HASH_OUTPUT_SIZE; + while(j < HASH_OUTPUT_SIZE && k < key_len) + key[k++] = ux[j++]; + } +} + +#ifdef TEST + +#include + +struct +{ unsigned int pwd_len; + unsigned int salt_len; + unsigned int it_count; + unsigned char *pwd; + unsigned char salt[32]; + unsigned char key[32]; +} tests[] = +{ + { 8, 4, 5, (unsigned char*)"password", + { + 0x12, 0x34, 0x56, 0x78 + }, + { + 0x5c, 0x75, 0xce, 0xf0, 0x1a, 0x96, 0x0d, 0xf7, + 0x4c, 0xb6, 0xb4, 0x9b, 0x9e, 0x38, 0xe6, 0xb5 + } + }, + { 8, 8, 5, (unsigned char*)"password", + { + 0x12, 0x34, 0x56, 0x78, 0x78, 0x56, 0x34, 0x12 + }, + { + 0xd1, 0xda, 0xa7, 0x86, 0x15, 0xf2, 0x87, 0xe6, + 0xa1, 0xc8, 0xb1, 0x20, 0xd7, 0x06, 0x2a, 0x49 + } + }, + { 8, 21, 1, (unsigned char*)"password", + { + "ATHENA.MIT.EDUraeburn" + }, + { + 0xcd, 0xed, 0xb5, 0x28, 0x1b, 0xb2, 0xf8, 0x01, + 0x56, 0x5a, 0x11, 0x22, 0xb2, 0x56, 0x35, 0x15 + } + }, + { 8, 21, 2, (unsigned char*)"password", + { + "ATHENA.MIT.EDUraeburn" + }, + { + 0x01, 0xdb, 0xee, 0x7f, 0x4a, 0x9e, 0x24, 0x3e, + 0x98, 0x8b, 0x62, 0xc7, 0x3c, 0xda, 0x93, 0x5d + } + }, + { 8, 21, 1200, (unsigned char*)"password", + { + "ATHENA.MIT.EDUraeburn" + }, + { + 0x5c, 0x08, 0xeb, 0x61, 0xfd, 0xf7, 0x1e, 0x4e, + 0x4e, 0xc3, 0xcf, 0x6b, 0xa1, 0xf5, 0x51, 0x2b + } + } +}; + +int main() +{ unsigned int i, j, key_len = 256; + unsigned char key[256]; + + printf("\nTest of RFC2898 Password Based Key Derivation"); + for(i = 0; i < 5; ++i) + { + derive_key(tests[i].pwd, tests[i].pwd_len, tests[i].salt, + tests[i].salt_len, tests[i].it_count, key, key_len); + + printf("\ntest %i: ", i + 1); + printf("key %s", memcmp(tests[i].key, key, 16) ? "is bad" : "is good"); + for(j = 0; j < key_len && j < 64; j += 4) + { + if(j % 16 == 0) + printf("\n"); + printf("0x%02x%02x%02x%02x ", key[j], key[j + 1], key[j + 2], key[j + 3]); + } + printf(j < key_len ? " ... \n" : "\n"); + } + printf("\n"); + return 0; +} + +#if defined(__cplusplus) +} +#endif + +#endif diff --git a/Zip/aes/pwd2key.h b/Zip/minizip/aes/pwd2key.h similarity index 97% rename from Zip/aes/pwd2key.h rename to Zip/minizip/aes/pwd2key.h index 99c1ecc183b1caf3911b0f0d88f69d359d2f2abf..d95a5b9fb5c82fd61b29e5b08e85a52d93ed2086 100755 --- a/Zip/aes/pwd2key.h +++ b/Zip/minizip/aes/pwd2key.h @@ -1,57 +1,57 @@ -/* - --------------------------------------------------------------------------- - Copyright (c) 2002, Dr Brian Gladman, Worcester, UK. All rights reserved. - - LICENSE TERMS - - The free distribution and use of this software in both source and binary - form is allowed (with or without changes) provided that: - - 1. distributions of this source code include the above copyright - notice, this list of conditions and the following disclaimer; - - 2. distributions in binary form include the above copyright - notice, this list of conditions and the following disclaimer - in the documentation and/or other associated materials; - - 3. the copyright holder's name is not used to endorse products - built using this software without specific written permission. - - ALTERNATIVELY, provided that this notice is retained in full, this product - may be distributed under the terms of the GNU General Public License (GPL), - in which case the provisions of the GPL apply INSTEAD OF those given above. - - DISCLAIMER - - This software is provided 'as is' with no explicit or implied warranties - in respect of its properties, including, but not limited to, correctness - and/or fitness for purpose. - --------------------------------------------------------------------------- - Issue Date: 26/08/2003 - - This is an implementation of RFC2898, which specifies key derivation from - a password and a salt value. -*/ - -#ifndef PWD2KEY_H -#define PWD2KEY_H - -#if defined(__cplusplus) -extern "C" -{ -#endif - -void derive_key( - const unsigned char pwd[], /* the PASSWORD, and */ - unsigned int pwd_len, /* its length */ - const unsigned char salt[], /* the SALT and its */ - unsigned int salt_len, /* length */ - unsigned int iter, /* the number of iterations */ - unsigned char key[], /* space for the output key */ - unsigned int key_len); /* and its required length */ - -#if defined(__cplusplus) -} -#endif - -#endif +/* + --------------------------------------------------------------------------- + Copyright (c) 2002, Dr Brian Gladman, Worcester, UK. All rights reserved. + + LICENSE TERMS + + The free distribution and use of this software in both source and binary + form is allowed (with or without changes) provided that: + + 1. distributions of this source code include the above copyright + notice, this list of conditions and the following disclaimer; + + 2. distributions in binary form include the above copyright + notice, this list of conditions and the following disclaimer + in the documentation and/or other associated materials; + + 3. the copyright holder's name is not used to endorse products + built using this software without specific written permission. + + ALTERNATIVELY, provided that this notice is retained in full, this product + may be distributed under the terms of the GNU General Public License (GPL), + in which case the provisions of the GPL apply INSTEAD OF those given above. + + DISCLAIMER + + This software is provided 'as is' with no explicit or implied warranties + in respect of its properties, including, but not limited to, correctness + and/or fitness for purpose. + --------------------------------------------------------------------------- + Issue Date: 26/08/2003 + + This is an implementation of RFC2898, which specifies key derivation from + a password and a salt value. +*/ + +#ifndef PWD2KEY_H +#define PWD2KEY_H + +#if defined(__cplusplus) +extern "C" +{ +#endif + +void derive_key( + const unsigned char pwd[], /* the PASSWORD, and */ + unsigned int pwd_len, /* its length */ + const unsigned char salt[], /* the SALT and its */ + unsigned int salt_len, /* length */ + unsigned int iter, /* the number of iterations */ + unsigned char key[], /* space for the output key */ + unsigned int key_len); /* and its required length */ + +#if defined(__cplusplus) +} +#endif + +#endif diff --git a/Zip/aes/sha1.c b/Zip/minizip/aes/sha1.c similarity index 97% rename from Zip/aes/sha1.c rename to Zip/minizip/aes/sha1.c index 0fbf05e6fabf462114db43f806252f5fbdb1153a..bb5474287bb4c7a3ddea224167eca38e34723856 100755 --- a/Zip/aes/sha1.c +++ b/Zip/minizip/aes/sha1.c @@ -1,258 +1,258 @@ -/* - --------------------------------------------------------------------------- - Copyright (c) 2002, Dr Brian Gladman, Worcester, UK. All rights reserved. - - LICENSE TERMS - - The free distribution and use of this software in both source and binary - form is allowed (with or without changes) provided that: - - 1. distributions of this source code include the above copyright - notice, this list of conditions and the following disclaimer; - - 2. distributions in binary form include the above copyright - notice, this list of conditions and the following disclaimer - in the documentation and/or other associated materials; - - 3. the copyright holder's name is not used to endorse products - built using this software without specific written permission. - - ALTERNATIVELY, provided that this notice is retained in full, this product - may be distributed under the terms of the GNU General Public License (GPL), - in which case the provisions of the GPL apply INSTEAD OF those given above. - - DISCLAIMER - - This software is provided 'as is' with no explicit or implied warranties - in respect of its properties, including, but not limited to, correctness - and/or fitness for purpose. - --------------------------------------------------------------------------- - Issue Date: 01/08/2005 - - This is a byte oriented version of SHA1 that operates on arrays of bytes - stored in memory. -*/ - -#include /* for memcpy() etc. */ - -#include "sha1.h" -#include "brg_endian.h" - -#if defined(__cplusplus) -extern "C" -{ -#endif - -#if defined( _MSC_VER ) && ( _MSC_VER > 800 ) -#pragma intrinsic(memcpy) -#endif - -#if 0 && defined(_MSC_VER) -#define rotl32 _lrotl -#define rotr32 _lrotr -#else -#define rotl32(x,n) (((x) << n) | ((x) >> (32 - n))) -#define rotr32(x,n) (((x) >> n) | ((x) << (32 - n))) -#endif - -#if !defined(bswap_32) -#define bswap_32(x) ((rotr32((x), 24) & 0x00ff00ff) | (rotr32((x), 8) & 0xff00ff00)) -#endif - -#if (PLATFORM_BYTE_ORDER == IS_LITTLE_ENDIAN) -#define SWAP_BYTES -#else -#undef SWAP_BYTES -#endif - -#if defined(SWAP_BYTES) -#define bsw_32(p,n) \ - { int _i = (n); while(_i--) ((uint_32t*)p)[_i] = bswap_32(((uint_32t*)p)[_i]); } -#else -#define bsw_32(p,n) -#endif - -#define SHA1_MASK (SHA1_BLOCK_SIZE - 1) - -#if 0 - -#define ch(x,y,z) (((x) & (y)) ^ (~(x) & (z))) -#define parity(x,y,z) ((x) ^ (y) ^ (z)) -#define maj(x,y,z) (((x) & (y)) ^ ((x) & (z)) ^ ((y) & (z))) - -#else /* Discovered by Rich Schroeppel and Colin Plumb */ - -#define ch(x,y,z) ((z) ^ ((x) & ((y) ^ (z)))) -#define parity(x,y,z) ((x) ^ (y) ^ (z)) -#define maj(x,y,z) (((x) & (y)) | ((z) & ((x) ^ (y)))) - -#endif - -/* Compile 64 bytes of hash data into SHA1 context. Note */ -/* that this routine assumes that the byte order in the */ -/* ctx->wbuf[] at this point is in such an order that low */ -/* address bytes in the ORIGINAL byte stream will go in */ -/* this buffer to the high end of 32-bit words on BOTH big */ -/* and little endian systems */ - -#ifdef ARRAY -#define q(v,n) v[n] -#else -#define q(v,n) v##n -#endif - -#define one_cycle(v,a,b,c,d,e,f,k,h) \ - q(v,e) += rotr32(q(v,a),27) + \ - f(q(v,b),q(v,c),q(v,d)) + k + h; \ - q(v,b) = rotr32(q(v,b), 2) - -#define five_cycle(v,f,k,i) \ - one_cycle(v, 0,1,2,3,4, f,k,hf(i )); \ - one_cycle(v, 4,0,1,2,3, f,k,hf(i+1)); \ - one_cycle(v, 3,4,0,1,2, f,k,hf(i+2)); \ - one_cycle(v, 2,3,4,0,1, f,k,hf(i+3)); \ - one_cycle(v, 1,2,3,4,0, f,k,hf(i+4)) - -VOID_RETURN sha1_compile(sha1_ctx ctx[1]) -{ uint_32t *w = ctx->wbuf; - -#ifdef ARRAY - uint_32t v[5]; - memcpy(v, ctx->hash, 5 * sizeof(uint_32t)); -#else - uint_32t v0, v1, v2, v3, v4; - v0 = ctx->hash[0]; v1 = ctx->hash[1]; - v2 = ctx->hash[2]; v3 = ctx->hash[3]; - v4 = ctx->hash[4]; -#endif - -#define hf(i) w[i] - - five_cycle(v, ch, 0x5a827999, 0); - five_cycle(v, ch, 0x5a827999, 5); - five_cycle(v, ch, 0x5a827999, 10); - one_cycle(v,0,1,2,3,4, ch, 0x5a827999, hf(15)); \ - -#undef hf -#define hf(i) (w[(i) & 15] = rotl32( \ - w[((i) + 13) & 15] ^ w[((i) + 8) & 15] \ - ^ w[((i) + 2) & 15] ^ w[(i) & 15], 1)) - - one_cycle(v,4,0,1,2,3, ch, 0x5a827999, hf(16)); - one_cycle(v,3,4,0,1,2, ch, 0x5a827999, hf(17)); - one_cycle(v,2,3,4,0,1, ch, 0x5a827999, hf(18)); - one_cycle(v,1,2,3,4,0, ch, 0x5a827999, hf(19)); - - five_cycle(v, parity, 0x6ed9eba1, 20); - five_cycle(v, parity, 0x6ed9eba1, 25); - five_cycle(v, parity, 0x6ed9eba1, 30); - five_cycle(v, parity, 0x6ed9eba1, 35); - - five_cycle(v, maj, 0x8f1bbcdc, 40); - five_cycle(v, maj, 0x8f1bbcdc, 45); - five_cycle(v, maj, 0x8f1bbcdc, 50); - five_cycle(v, maj, 0x8f1bbcdc, 55); - - five_cycle(v, parity, 0xca62c1d6, 60); - five_cycle(v, parity, 0xca62c1d6, 65); - five_cycle(v, parity, 0xca62c1d6, 70); - five_cycle(v, parity, 0xca62c1d6, 75); - -#ifdef ARRAY - ctx->hash[0] += v[0]; ctx->hash[1] += v[1]; - ctx->hash[2] += v[2]; ctx->hash[3] += v[3]; - ctx->hash[4] += v[4]; -#else - ctx->hash[0] += v0; ctx->hash[1] += v1; - ctx->hash[2] += v2; ctx->hash[3] += v3; - ctx->hash[4] += v4; -#endif -} - -VOID_RETURN sha1_begin(sha1_ctx ctx[1]) -{ - ctx->count[0] = ctx->count[1] = 0; - ctx->hash[0] = 0x67452301; - ctx->hash[1] = 0xefcdab89; - ctx->hash[2] = 0x98badcfe; - ctx->hash[3] = 0x10325476; - ctx->hash[4] = 0xc3d2e1f0; -} - -/* SHA1 hash data in an array of bytes into hash buffer and */ -/* call the hash_compile function as required. */ - -VOID_RETURN sha1_hash(const unsigned char data[], unsigned long len, sha1_ctx ctx[1]) -{ uint_32t pos = (uint_32t)(ctx->count[0] & SHA1_MASK), - space = SHA1_BLOCK_SIZE - pos; - const unsigned char *sp = data; - - if((ctx->count[0] += len) < len) - ++(ctx->count[1]); - - while(len >= space) /* tranfer whole blocks if possible */ - { - memcpy(((unsigned char*)ctx->wbuf) + pos, sp, space); - sp += space; len -= space; space = SHA1_BLOCK_SIZE; pos = 0; - bsw_32(ctx->wbuf, SHA1_BLOCK_SIZE >> 2); - sha1_compile(ctx); - } - - memcpy(((unsigned char*)ctx->wbuf) + pos, sp, len); -} - -/* SHA1 final padding and digest calculation */ - -VOID_RETURN sha1_end(unsigned char hval[], sha1_ctx ctx[1]) -{ uint_32t i = (uint_32t)(ctx->count[0] & SHA1_MASK); - - /* put bytes in the buffer in an order in which references to */ - /* 32-bit words will put bytes with lower addresses into the */ - /* top of 32 bit words on BOTH big and little endian machines */ - bsw_32(ctx->wbuf, (i + 3) >> 2); - - /* we now need to mask valid bytes and add the padding which is */ - /* a single 1 bit and as many zero bits as necessary. Note that */ - /* we can always add the first padding byte here because the */ - /* buffer always has at least one empty slot */ - ctx->wbuf[i >> 2] &= 0xffffff80 << 8 * (~i & 3); - ctx->wbuf[i >> 2] |= 0x00000080 << 8 * (~i & 3); - - /* we need 9 or more empty positions, one for the padding byte */ - /* (above) and eight for the length count. If there is not */ - /* enough space, pad and empty the buffer */ - if(i > SHA1_BLOCK_SIZE - 9) - { - if(i < 60) ctx->wbuf[15] = 0; - sha1_compile(ctx); - i = 0; - } - else /* compute a word index for the empty buffer positions */ - i = (i >> 2) + 1; - - while(i < 14) /* and zero pad all but last two positions */ - ctx->wbuf[i++] = 0; - - /* the following 32-bit length fields are assembled in the */ - /* wrong byte order on little endian machines but this is */ - /* corrected later since they are only ever used as 32-bit */ - /* word values. */ - ctx->wbuf[14] = (ctx->count[1] << 3) | (ctx->count[0] >> 29); - ctx->wbuf[15] = ctx->count[0] << 3; - sha1_compile(ctx); - - /* extract the hash value as bytes in case the hash buffer is */ - /* misaligned for 32-bit words */ - for(i = 0; i < SHA1_DIGEST_SIZE; ++i) - hval[i] = (unsigned char)(ctx->hash[i >> 2] >> (8 * (~i & 3))); -} - -VOID_RETURN sha1(unsigned char hval[], const unsigned char data[], unsigned long len) -{ sha1_ctx cx[1]; - - sha1_begin(cx); sha1_hash(data, len, cx); sha1_end(hval, cx); -} - -#if defined(__cplusplus) -} -#endif +/* + --------------------------------------------------------------------------- + Copyright (c) 2002, Dr Brian Gladman, Worcester, UK. All rights reserved. + + LICENSE TERMS + + The free distribution and use of this software in both source and binary + form is allowed (with or without changes) provided that: + + 1. distributions of this source code include the above copyright + notice, this list of conditions and the following disclaimer; + + 2. distributions in binary form include the above copyright + notice, this list of conditions and the following disclaimer + in the documentation and/or other associated materials; + + 3. the copyright holder's name is not used to endorse products + built using this software without specific written permission. + + ALTERNATIVELY, provided that this notice is retained in full, this product + may be distributed under the terms of the GNU General Public License (GPL), + in which case the provisions of the GPL apply INSTEAD OF those given above. + + DISCLAIMER + + This software is provided 'as is' with no explicit or implied warranties + in respect of its properties, including, but not limited to, correctness + and/or fitness for purpose. + --------------------------------------------------------------------------- + Issue Date: 01/08/2005 + + This is a byte oriented version of SHA1 that operates on arrays of bytes + stored in memory. +*/ + +#include /* for memcpy() etc. */ + +#include "sha1.h" +#include "brg_endian.h" + +#if defined(__cplusplus) +extern "C" +{ +#endif + +#if defined( _MSC_VER ) && ( _MSC_VER > 800 ) +#pragma intrinsic(memcpy) +#endif + +#if 0 && defined(_MSC_VER) +#define rotl32 _lrotl +#define rotr32 _lrotr +#else +#define rotl32(x,n) (((x) << n) | ((x) >> (32 - n))) +#define rotr32(x,n) (((x) >> n) | ((x) << (32 - n))) +#endif + +#if !defined(bswap_32) +#define bswap_32(x) ((rotr32((x), 24) & 0x00ff00ff) | (rotr32((x), 8) & 0xff00ff00)) +#endif + +#if (PLATFORM_BYTE_ORDER == IS_LITTLE_ENDIAN) +#define SWAP_BYTES +#else +#undef SWAP_BYTES +#endif + +#if defined(SWAP_BYTES) +#define bsw_32(p,n) \ + { int _i = (n); while(_i--) ((uint_32t*)p)[_i] = bswap_32(((uint_32t*)p)[_i]); } +#else +#define bsw_32(p,n) +#endif + +#define SHA1_MASK (SHA1_BLOCK_SIZE - 1) + +#if 0 + +#define ch(x,y,z) (((x) & (y)) ^ (~(x) & (z))) +#define parity(x,y,z) ((x) ^ (y) ^ (z)) +#define maj(x,y,z) (((x) & (y)) ^ ((x) & (z)) ^ ((y) & (z))) + +#else /* Discovered by Rich Schroeppel and Colin Plumb */ + +#define ch(x,y,z) ((z) ^ ((x) & ((y) ^ (z)))) +#define parity(x,y,z) ((x) ^ (y) ^ (z)) +#define maj(x,y,z) (((x) & (y)) | ((z) & ((x) ^ (y)))) + +#endif + +/* Compile 64 bytes of hash data into SHA1 context. Note */ +/* that this routine assumes that the byte order in the */ +/* ctx->wbuf[] at this point is in such an order that low */ +/* address bytes in the ORIGINAL byte stream will go in */ +/* this buffer to the high end of 32-bit words on BOTH big */ +/* and little endian systems */ + +#ifdef ARRAY +#define q(v,n) v[n] +#else +#define q(v,n) v##n +#endif + +#define one_cycle(v,a,b,c,d,e,f,k,h) \ + q(v,e) += rotr32(q(v,a),27) + \ + f(q(v,b),q(v,c),q(v,d)) + k + h; \ + q(v,b) = rotr32(q(v,b), 2) + +#define five_cycle(v,f,k,i) \ + one_cycle(v, 0,1,2,3,4, f,k,hf(i )); \ + one_cycle(v, 4,0,1,2,3, f,k,hf(i+1)); \ + one_cycle(v, 3,4,0,1,2, f,k,hf(i+2)); \ + one_cycle(v, 2,3,4,0,1, f,k,hf(i+3)); \ + one_cycle(v, 1,2,3,4,0, f,k,hf(i+4)) + +VOID_RETURN sha1_compile(sha1_ctx ctx[1]) +{ uint_32t *w = ctx->wbuf; + +#ifdef ARRAY + uint_32t v[5]; + memcpy(v, ctx->hash, 5 * sizeof(uint_32t)); +#else + uint_32t v0, v1, v2, v3, v4; + v0 = ctx->hash[0]; v1 = ctx->hash[1]; + v2 = ctx->hash[2]; v3 = ctx->hash[3]; + v4 = ctx->hash[4]; +#endif + +#define hf(i) w[i] + + five_cycle(v, ch, 0x5a827999, 0); + five_cycle(v, ch, 0x5a827999, 5); + five_cycle(v, ch, 0x5a827999, 10); + one_cycle(v,0,1,2,3,4, ch, 0x5a827999, hf(15)); \ + +#undef hf +#define hf(i) (w[(i) & 15] = rotl32( \ + w[((i) + 13) & 15] ^ w[((i) + 8) & 15] \ + ^ w[((i) + 2) & 15] ^ w[(i) & 15], 1)) + + one_cycle(v,4,0,1,2,3, ch, 0x5a827999, hf(16)); + one_cycle(v,3,4,0,1,2, ch, 0x5a827999, hf(17)); + one_cycle(v,2,3,4,0,1, ch, 0x5a827999, hf(18)); + one_cycle(v,1,2,3,4,0, ch, 0x5a827999, hf(19)); + + five_cycle(v, parity, 0x6ed9eba1, 20); + five_cycle(v, parity, 0x6ed9eba1, 25); + five_cycle(v, parity, 0x6ed9eba1, 30); + five_cycle(v, parity, 0x6ed9eba1, 35); + + five_cycle(v, maj, 0x8f1bbcdc, 40); + five_cycle(v, maj, 0x8f1bbcdc, 45); + five_cycle(v, maj, 0x8f1bbcdc, 50); + five_cycle(v, maj, 0x8f1bbcdc, 55); + + five_cycle(v, parity, 0xca62c1d6, 60); + five_cycle(v, parity, 0xca62c1d6, 65); + five_cycle(v, parity, 0xca62c1d6, 70); + five_cycle(v, parity, 0xca62c1d6, 75); + +#ifdef ARRAY + ctx->hash[0] += v[0]; ctx->hash[1] += v[1]; + ctx->hash[2] += v[2]; ctx->hash[3] += v[3]; + ctx->hash[4] += v[4]; +#else + ctx->hash[0] += v0; ctx->hash[1] += v1; + ctx->hash[2] += v2; ctx->hash[3] += v3; + ctx->hash[4] += v4; +#endif +} + +VOID_RETURN sha1_begin(sha1_ctx ctx[1]) +{ + ctx->count[0] = ctx->count[1] = 0; + ctx->hash[0] = 0x67452301; + ctx->hash[1] = 0xefcdab89; + ctx->hash[2] = 0x98badcfe; + ctx->hash[3] = 0x10325476; + ctx->hash[4] = 0xc3d2e1f0; +} + +/* SHA1 hash data in an array of bytes into hash buffer and */ +/* call the hash_compile function as required. */ + +VOID_RETURN sha1_hash(const unsigned char data[], unsigned long len, sha1_ctx ctx[1]) +{ uint_32t pos = (uint_32t)(ctx->count[0] & SHA1_MASK), + space = SHA1_BLOCK_SIZE - pos; + const unsigned char *sp = data; + + if((ctx->count[0] += len) < len) + ++(ctx->count[1]); + + while(len >= space) /* tranfer whole blocks if possible */ + { + memcpy(((unsigned char*)ctx->wbuf) + pos, sp, space); + sp += space; len -= space; space = SHA1_BLOCK_SIZE; pos = 0; + bsw_32(ctx->wbuf, SHA1_BLOCK_SIZE >> 2); + sha1_compile(ctx); + } + + memcpy(((unsigned char*)ctx->wbuf) + pos, sp, len); +} + +/* SHA1 final padding and digest calculation */ + +VOID_RETURN sha1_end(unsigned char hval[], sha1_ctx ctx[1]) +{ uint_32t i = (uint_32t)(ctx->count[0] & SHA1_MASK); + + /* put bytes in the buffer in an order in which references to */ + /* 32-bit words will put bytes with lower addresses into the */ + /* top of 32 bit words on BOTH big and little endian machines */ + bsw_32(ctx->wbuf, (i + 3) >> 2); + + /* we now need to mask valid bytes and add the padding which is */ + /* a single 1 bit and as many zero bits as necessary. Note that */ + /* we can always add the first padding byte here because the */ + /* buffer always has at least one empty slot */ + ctx->wbuf[i >> 2] &= 0xffffff80 << 8 * (~i & 3); + ctx->wbuf[i >> 2] |= 0x00000080 << 8 * (~i & 3); + + /* we need 9 or more empty positions, one for the padding byte */ + /* (above) and eight for the length count. If there is not */ + /* enough space, pad and empty the buffer */ + if(i > SHA1_BLOCK_SIZE - 9) + { + if(i < 60) ctx->wbuf[15] = 0; + sha1_compile(ctx); + i = 0; + } + else /* compute a word index for the empty buffer positions */ + i = (i >> 2) + 1; + + while(i < 14) /* and zero pad all but last two positions */ + ctx->wbuf[i++] = 0; + + /* the following 32-bit length fields are assembled in the */ + /* wrong byte order on little endian machines but this is */ + /* corrected later since they are only ever used as 32-bit */ + /* word values. */ + ctx->wbuf[14] = (ctx->count[1] << 3) | (ctx->count[0] >> 29); + ctx->wbuf[15] = ctx->count[0] << 3; + sha1_compile(ctx); + + /* extract the hash value as bytes in case the hash buffer is */ + /* misaligned for 32-bit words */ + for(i = 0; i < SHA1_DIGEST_SIZE; ++i) + hval[i] = (unsigned char)(ctx->hash[i >> 2] >> (8 * (~i & 3))); +} + +VOID_RETURN sha1(unsigned char hval[], const unsigned char data[], unsigned long len) +{ sha1_ctx cx[1]; + + sha1_begin(cx); sha1_hash(data, len, cx); sha1_end(hval, cx); +} + +#if defined(__cplusplus) +} +#endif diff --git a/Zip/aes/sha1.h b/Zip/minizip/aes/sha1.h similarity index 96% rename from Zip/aes/sha1.h rename to Zip/minizip/aes/sha1.h index bace6afe97c426c7aeac3a9ebb34d4632aea9ea9..65ee6d39e4acc8918dff46fc6d81e792e144b92b 100755 --- a/Zip/aes/sha1.h +++ b/Zip/minizip/aes/sha1.h @@ -1,73 +1,73 @@ -/* - --------------------------------------------------------------------------- - Copyright (c) 2002, Dr Brian Gladman, Worcester, UK. All rights reserved. - - LICENSE TERMS - - The free distribution and use of this software in both source and binary - form is allowed (with or without changes) provided that: - - 1. distributions of this source code include the above copyright - notice, this list of conditions and the following disclaimer; - - 2. distributions in binary form include the above copyright - notice, this list of conditions and the following disclaimer - in the documentation and/or other associated materials; - - 3. the copyright holder's name is not used to endorse products - built using this software without specific written permission. - - ALTERNATIVELY, provided that this notice is retained in full, this product - may be distributed under the terms of the GNU General Public License (GPL), - in which case the provisions of the GPL apply INSTEAD OF those given above. - - DISCLAIMER - - This software is provided 'as is' with no explicit or implied warranties - in respect of its properties, including, but not limited to, correctness - and/or fitness for purpose. - --------------------------------------------------------------------------- - Issue Date: 01/08/2005 -*/ - -#ifndef _SHA1_H -#define _SHA1_H - -#include -#include "brg_types.h" - -#define SHA1_BLOCK_SIZE 64 -#define SHA1_DIGEST_SIZE 20 - -#if defined(__cplusplus) -extern "C" -{ -#endif - -/* type to hold the SHA256 context */ - -typedef struct -{ uint_32t count[2]; - uint_32t hash[5]; - uint_32t wbuf[16]; -} sha1_ctx; - -/* Note that these prototypes are the same for both bit and */ -/* byte oriented implementations. However the length fields */ -/* are in bytes or bits as appropriate for the version used */ -/* and bit sequences are input as arrays of bytes in which */ -/* bit sequences run from the most to the least significant */ -/* end of each byte */ - -VOID_RETURN sha1_compile(sha1_ctx ctx[1]); - -VOID_RETURN sha1_begin(sha1_ctx ctx[1]); -VOID_RETURN sha1_hash(const unsigned char data[], unsigned long len, sha1_ctx ctx[1]); -VOID_RETURN sha1_end(unsigned char hval[], sha1_ctx ctx[1]); -VOID_RETURN sha1(unsigned char hval[], const unsigned char data[], unsigned long len); - -#if defined(__cplusplus) -} -#endif - -#endif +/* + --------------------------------------------------------------------------- + Copyright (c) 2002, Dr Brian Gladman, Worcester, UK. All rights reserved. + + LICENSE TERMS + + The free distribution and use of this software in both source and binary + form is allowed (with or without changes) provided that: + + 1. distributions of this source code include the above copyright + notice, this list of conditions and the following disclaimer; + + 2. distributions in binary form include the above copyright + notice, this list of conditions and the following disclaimer + in the documentation and/or other associated materials; + + 3. the copyright holder's name is not used to endorse products + built using this software without specific written permission. + + ALTERNATIVELY, provided that this notice is retained in full, this product + may be distributed under the terms of the GNU General Public License (GPL), + in which case the provisions of the GPL apply INSTEAD OF those given above. + + DISCLAIMER + + This software is provided 'as is' with no explicit or implied warranties + in respect of its properties, including, but not limited to, correctness + and/or fitness for purpose. + --------------------------------------------------------------------------- + Issue Date: 01/08/2005 +*/ + +#ifndef _SHA1_H +#define _SHA1_H + +#include +#include "brg_types.h" + +#define SHA1_BLOCK_SIZE 64 +#define SHA1_DIGEST_SIZE 20 + +#if defined(__cplusplus) +extern "C" +{ +#endif + +/* type to hold the SHA256 context */ + +typedef struct +{ uint_32t count[2]; + uint_32t hash[5]; + uint_32t wbuf[16]; +} sha1_ctx; + +/* Note that these prototypes are the same for both bit and */ +/* byte oriented implementations. However the length fields */ +/* are in bytes or bits as appropriate for the version used */ +/* and bit sequences are input as arrays of bytes in which */ +/* bit sequences run from the most to the least significant */ +/* end of each byte */ + +VOID_RETURN sha1_compile(sha1_ctx ctx[1]); + +VOID_RETURN sha1_begin(sha1_ctx ctx[1]); +VOID_RETURN sha1_hash(const unsigned char data[], unsigned long len, sha1_ctx ctx[1]); +VOID_RETURN sha1_end(unsigned char hval[], sha1_ctx ctx[1]); +VOID_RETURN sha1(unsigned char hval[], const unsigned char data[], unsigned long len); + +#if defined(__cplusplus) +} +#endif + +#endif diff --git a/Zip/minizip/configure.ac b/Zip/minizip/configure.ac new file mode 100755 index 0000000000000000000000000000000000000000..827a4e05770f87127cf1333b4b85d24503d6e532 --- /dev/null +++ b/Zip/minizip/configure.ac @@ -0,0 +1,32 @@ +# -*- Autoconf -*- +# Process this file with autoconf to produce a configure script. + +AC_INIT([minizip], [1.2.8], [bugzilla.redhat.com]) +AC_CONFIG_SRCDIR([minizip.c]) +AM_INIT_AUTOMAKE([foreign]) +LT_INIT + +AC_MSG_CHECKING([whether to build example programs]) +AC_ARG_ENABLE([demos], AC_HELP_STRING([--enable-demos], [build example programs])) +AM_CONDITIONAL([COND_DEMOS], [test "$enable_demos" = yes]) +if test "$enable_demos" = yes +then + AC_MSG_RESULT([yes]) +else + AC_MSG_RESULT([no]) +fi + +case "${host}" in + *-mingw* | mingw*) + WIN32="yes" + ;; + *) + ;; +esac +AM_CONDITIONAL([WIN32], [test "${WIN32}" = "yes"]) + + +AC_SUBST([HAVE_UNISTD_H], [0]) +AC_CHECK_HEADER([unistd.h], [HAVE_UNISTD_H=1], []) +AC_CONFIG_FILES([Makefile minizip.pc]) +AC_OUTPUT diff --git a/Zip/minizip/crypt.h b/Zip/minizip/crypt.h index 46c63fb3e4a007d93b8aa377b71aa426b673fe1a..8705208eabd810ba3c2b4581f7ebd8fa90cc39ef 100755 --- a/Zip/minizip/crypt.h +++ b/Zip/minizip/crypt.h @@ -31,7 +31,7 @@ /*********************************************************************** * Return the next byte in the pseudo-random sequence */ -static int decrypt_byte(unsigned long* pkeys, const unsigned long* pcrc_32_tab) +static int decrypt_byte(unsigned long* pkeys) { unsigned temp; /* POTENTIAL BUG: temp*(temp^1) may overflow in an * unpredictable manner on 16-bit systems; not a problem @@ -44,14 +44,14 @@ static int decrypt_byte(unsigned long* pkeys, const unsigned long* pcrc_32_tab) /*********************************************************************** * Update the encryption keys with the next byte of plain text */ -static int update_keys(unsigned long* pkeys,const unsigned long* pcrc_32_tab,int c) +static int update_keys(unsigned long* pkeys, const unsigned long* pcrc_32_tab, int c) { (*(pkeys+0)) = CRC32((*(pkeys+0)), c); (*(pkeys+1)) += (*(pkeys+0)) & 0xff; (*(pkeys+1)) = (*(pkeys+1)) * 134775813L + 1; { - register int keyshift = (int)((*(pkeys+1)) >> 24); - (*(pkeys+2)) = CRC32((*(pkeys+2)), keyshift); + register int keyshift = (int)((*(pkeys+1)) >> 24); + (*(pkeys+2)) = CRC32((*(pkeys+2)), keyshift); } return c; } @@ -61,22 +61,23 @@ static int update_keys(unsigned long* pkeys,const unsigned long* pcrc_32_tab,int * Initialize the encryption keys and the random header according to * the given password. */ -static void init_keys(const char* passwd,unsigned long* pkeys,const unsigned long* pcrc_32_tab) +static void init_keys(const char* passwd, unsigned long* pkeys, const unsigned long* pcrc_32_tab) { *(pkeys+0) = 305419896L; *(pkeys+1) = 591751049L; *(pkeys+2) = 878082192L; - while (*passwd != 0) { + while (*passwd != 0) + { update_keys(pkeys,pcrc_32_tab,(int)*passwd); passwd++; } } #define zdecode(pkeys,pcrc_32_tab,c) \ - (update_keys(pkeys,pcrc_32_tab,c ^= decrypt_byte(pkeys,pcrc_32_tab))) + (update_keys(pkeys,pcrc_32_tab,c ^= decrypt_byte(pkeys))) #define zencode(pkeys,pcrc_32_tab,c,t) \ - (t=decrypt_byte(pkeys,pcrc_32_tab), update_keys(pkeys,pcrc_32_tab,c), t^(c)) + (t=decrypt_byte(pkeys), update_keys(pkeys,pcrc_32_tab,c), t^(c)) #ifdef INCLUDECRYPTINGCODE_IFCRYPTALLOWED @@ -99,8 +100,8 @@ static int crypthead(const char* passwd, /* password string */ unsigned char header[RAND_HEAD_LEN-2]; /* random header */ static unsigned calls = 0; /* ensure different random header each time */ - if (bufSizefile = file; - ioposix->filenameLength = (int)strlen(filename) + 1; + ioposix->filenameLength = strlen(filename) + 1; ioposix->filename = (char*)malloc(ioposix->filenameLength * sizeof(char)); strncpy(ioposix->filename, filename, ioposix->filenameLength); return (voidpf)ioposix; @@ -176,7 +176,7 @@ static voidpf ZCALLBACK fopendisk64_file_func (voidpf opaque, voidpf stream, int strncpy(diskFilename, ioposix->filename, ioposix->filenameLength); for (i = ioposix->filenameLength - 1; i >= 0; i -= 1) { - if (diskFilename[i] != '.') + if (diskFilename[i] != '.') continue; snprintf(&diskFilename[i], ioposix->filenameLength - i, ".z%02d", number_disk + 1); break; @@ -201,7 +201,7 @@ static voidpf ZCALLBACK fopendisk_file_func (voidpf opaque, voidpf stream, int n strncpy(diskFilename, ioposix->filename, ioposix->filenameLength); for (i = ioposix->filenameLength - 1; i >= 0; i -= 1) { - if (diskFilename[i] != '.') + if (diskFilename[i] != '.') continue; snprintf(&diskFilename[i], ioposix->filenameLength - i, ".z%02d", number_disk + 1); break; diff --git a/Zip/minizip/ioapi.h b/Zip/minizip/ioapi.h index 742fae5026a175d938c310b2979f7550fc4654e9..f0edcacab2d1b7cd8f44fe03e1aef81f0c13da98 100755 --- a/Zip/minizip/ioapi.h +++ b/Zip/minizip/ioapi.h @@ -59,7 +59,7 @@ #ifdef HAVE_64BIT_INT_CUSTOM typedef 64BIT_INT_CUSTOM_TYPE ZPOS64_T; #else -# ifdef HAS_STDINT_H +# ifdef HAVE_STDINT_H # include "stdint.h" typedef uint64_t ZPOS64_T; # else diff --git a/Zip/minizip/ioapi_buf.c b/Zip/minizip/ioapi_buf.c new file mode 100755 index 0000000000000000000000000000000000000000..521a6d9d811b0884a6b4c0b8ab4e0740fe2a9b63 --- /dev/null +++ b/Zip/minizip/ioapi_buf.c @@ -0,0 +1,514 @@ +/* ioapi_buf.h -- IO base function header for compress/uncompress .zip + files using zlib + zip or unzip API + + This version of ioapi is designed to buffer IO. + + Copyright (C) 1998-2003 Gilles Vollant + (C) 2012-2014 Nathan Moinvaziri + + This program is distributed under the terms of the same license as zlib. + See the accompanying LICENSE file for the full text of the license. +*/ + + +#include +#include +#include +#include + +#include "zlib.h" +#include "ioapi.h" + +#include "ioapi_buf.h" + +#if defined(_WIN32) +# include +# define PRINTF _cprintf +# define VPRINTF _vcprintf +#else +# define PRINTF printf +# define VPRINTF vprintf +#endif + +//#define IOBUF_VERBOSE + +#ifdef __GNUC__ +#ifndef max +#define max(x,y) ({ \ +const typeof(x) _x = (x); \ +const typeof(y) _y = (y); \ +(void) (&_x == &_y); \ +_x > _y ? _x : _y; }) +#endif /* __GNUC__ */ + +#ifndef min +#define min(x,y) ({ \ +const typeof(x) _x = (x); \ +const typeof(y) _y = (y); \ +(void) (&_x == &_y); \ +_x < _y ? _x : _y; }) +#endif +#endif + +typedef struct ourstream_s { + char readBuffer[IOBUF_BUFFERSIZE]; + uInt readBufferLength; + uInt readBufferPos; + uInt readBufferHits; + uInt readBufferMisses; + char writeBuffer[IOBUF_BUFFERSIZE]; + uInt writeBufferLength; + uInt writeBufferPos; + uInt writeBufferHits; + uInt writeBufferMisses; + ZPOS64_T position; + voidpf stream; +} ourstream_t; + +#if defined(IOBUF_VERBOSE) +# define print_buf(o,s,f,...) print_buf_internal(o,s,f,__VA_ARGS__); +#else +# define print_buf(o,s,f,...) +#endif + +void print_buf_internal(voidpf opaque, voidpf stream, char *format, ...) +{ + ourstream_t *streamio = (ourstream_t *)stream; + va_list arglist; + PRINTF("Buf stream %p - ", streamio); + va_start(arglist, format); + VPRINTF(format, arglist); + va_end(arglist); +} + +voidpf fopen_buf_internal_func (opaque, stream, number_disk, mode) + voidpf opaque; + voidpf stream; + int number_disk; + int mode; +{ + ourstream_t *streamio = NULL; + if (stream == NULL) + return NULL; + streamio = (ourstream_t *)malloc(sizeof(ourstream_t)); + if (streamio == NULL) + return NULL; + memset(streamio, 0, sizeof(ourstream_t)); + streamio->stream = stream; + print_buf(opaque, streamio, "open [num %d mode %d]\n", number_disk, mode); + return streamio; +} + +voidpf ZCALLBACK fopen_buf_func (opaque, filename, mode) + voidpf opaque; + const char* filename; + int mode; +{ + ourbuffer_t *bufio = (ourbuffer_t *)opaque; + voidpf stream = bufio->filefunc.zopen_file(bufio->filefunc.opaque, filename, mode); + return fopen_buf_internal_func(opaque, stream, 0, mode); +} + +voidpf ZCALLBACK fopen64_buf_func (opaque, filename, mode) + voidpf opaque; + const char* filename; + int mode; +{ + ourbuffer_t *bufio = (ourbuffer_t *)opaque; + voidpf stream = bufio->filefunc64.zopen64_file(bufio->filefunc64.opaque, filename, mode); + return fopen_buf_internal_func(opaque, stream, 0, mode); +} + +voidpf ZCALLBACK fopendisk_buf_func (opaque, stream_cd, number_disk, mode) + voidpf opaque; + voidpf stream_cd; + int number_disk; + int mode; +{ + ourbuffer_t *bufio = (ourbuffer_t *)opaque; + ourstream_t *streamio = (ourstream_t *)stream_cd; + voidpf *stream = bufio->filefunc.zopendisk_file(bufio->filefunc.opaque, streamio->stream, number_disk, mode); + return fopen_buf_internal_func(opaque, stream, number_disk, mode); +} + +voidpf ZCALLBACK fopendisk64_buf_func (opaque, stream_cd, number_disk, mode) + voidpf opaque; + voidpf stream_cd; + int number_disk; + int mode; +{ + ourbuffer_t *bufio = (ourbuffer_t *)opaque; + ourstream_t *streamio = (ourstream_t *)stream_cd; + voidpf stream = bufio->filefunc64.zopendisk64_file(bufio->filefunc64.opaque, streamio->stream, number_disk, mode); + return fopen_buf_internal_func(opaque, stream, number_disk, mode); +} + +long fflush_buf OF((voidpf opaque, voidpf stream)); +long fflush_buf (opaque, stream) + voidpf opaque; + voidpf stream; +{ + ourbuffer_t *bufio = (ourbuffer_t *)opaque; + ourstream_t *streamio = (ourstream_t *)stream; + uInt totalBytesWritten = 0; + uInt bytesToWrite = streamio->writeBufferLength; + uInt bytesLeftToWrite = streamio->writeBufferLength; + int bytesWritten = 0; + + while (bytesLeftToWrite > 0) + { + if (bufio->filefunc64.zwrite_file != NULL) + bytesWritten = bufio->filefunc64.zwrite_file(bufio->filefunc64.opaque, streamio->stream, streamio->writeBuffer + (bytesToWrite - bytesLeftToWrite), bytesLeftToWrite); + else + bytesWritten = bufio->filefunc.zwrite_file(bufio->filefunc.opaque, streamio->stream, streamio->writeBuffer + (bytesToWrite - bytesLeftToWrite), bytesLeftToWrite); + + streamio->writeBufferMisses += 1; + + print_buf(opaque, stream, "write flush [%d:%d len %d]\n", bytesToWrite, bytesLeftToWrite, streamio->writeBufferLength); + + if (bytesWritten < 0) + return bytesWritten; + + totalBytesWritten += bytesWritten; + bytesLeftToWrite -= bytesWritten; + streamio->position += bytesWritten; + } + streamio->writeBufferLength = 0; + streamio->writeBufferPos = 0; + return totalBytesWritten; +} + +uLong ZCALLBACK fread_buf_func (opaque, stream, buf, size) + voidpf opaque; + voidpf stream; + void* buf; + uLong size; +{ + ourbuffer_t *bufio = (ourbuffer_t *)opaque; + ourstream_t *streamio = (ourstream_t *)stream; + uInt bytesToRead = 0; + uInt bufLength = 0; + uInt bytesToCopy = 0; + uInt bytesLeftToRead = size; + uInt bytesRead = -1; + + print_buf(opaque, stream, "read [size %ld pos %lld]\n", size, streamio->position); + + if (streamio->writeBufferLength > 0) + { + print_buf(opaque, stream, "switch from write to read, not yet supported [%lld]\n", streamio->position); + } + + while (bytesLeftToRead > 0) + { + if ((streamio->readBufferLength == 0) || (streamio->readBufferPos == streamio->readBufferLength)) + { + if (streamio->readBufferLength == IOBUF_BUFFERSIZE) + { + streamio->readBufferPos = 0; + streamio->readBufferLength = 0; + } + + bytesToRead = IOBUF_BUFFERSIZE -(streamio->readBufferLength - streamio->readBufferPos); + + if (bufio->filefunc64.zread_file != NULL) + bytesRead = bufio->filefunc64.zread_file(bufio->filefunc64.opaque, streamio->stream, streamio->readBuffer + streamio->readBufferPos, bytesToRead); + else + bytesRead = bufio->filefunc.zread_file(bufio->filefunc.opaque, streamio->stream, streamio->readBuffer + streamio->readBufferPos, bytesToRead); + + streamio->readBufferMisses += 1; + streamio->readBufferLength += bytesRead; + streamio->position += bytesRead; + + print_buf(opaque, stream, "filled [read %d/%d buf %d:%d pos %lld]\n", bytesRead, bytesToRead, streamio->readBufferPos, streamio->readBufferLength, streamio->position); + + if (bytesRead == 0) + break; + } + + if ((streamio->readBufferLength - streamio->readBufferPos) > 0) + { + bytesToCopy = min(bytesLeftToRead, (streamio->readBufferLength - streamio->readBufferPos)); + memcpy((char *)buf + bufLength, streamio->readBuffer + streamio->readBufferPos, bytesToCopy); + + bufLength += bytesToCopy; + bytesLeftToRead -= bytesToCopy; + + streamio->readBufferHits += 1; + streamio->readBufferPos += bytesToCopy; + + print_buf(opaque, stream, "emptied [copied %d remaining %d buf %d:%d pos %lld]\n", bytesToCopy, bytesLeftToRead, streamio->readBufferPos, streamio->readBufferLength, streamio->position); + } + } + + return size - bytesLeftToRead; +} + +uLong ZCALLBACK fwrite_buf_func (opaque, stream, buf, size) + voidpf opaque; + voidpf stream; + const void* buf; + uLong size; +{ + ourbuffer_t *bufio = (ourbuffer_t *)opaque; + ourstream_t *streamio = (ourstream_t *)stream; + uInt bytesToWrite = size; + uInt bytesLeftToWrite = size; + uInt bytesToCopy = 0; + int retVal = 0; + + print_buf(opaque, stream, "write [size %ld len %d pos %lld]\n", size, streamio->writeBufferLength, streamio->position); + + if (streamio->readBufferLength > 0) + { + streamio->position -= streamio->readBufferLength; + streamio->position += streamio->readBufferPos; + + streamio->readBufferLength = 0; + streamio->readBufferPos = 0; + + print_buf(opaque, stream, "switch from read to write [%lld]\n", streamio->position); + + if (bufio->filefunc64.zseek64_file != NULL) + retVal = bufio->filefunc64.zseek64_file(bufio->filefunc64.opaque, streamio->stream, streamio->position, ZLIB_FILEFUNC_SEEK_SET); + else + retVal = bufio->filefunc.zseek_file(bufio->filefunc.opaque, streamio->stream, (uLong)streamio->position, ZLIB_FILEFUNC_SEEK_SET); + + if (retVal != 0) + return -1; + } + + while (bytesLeftToWrite > 0) + { + bytesToCopy = min(bytesLeftToWrite, (IOBUF_BUFFERSIZE - min(streamio->writeBufferLength, streamio->writeBufferPos))); + + if (bytesToCopy == 0) + { + if (fflush_buf(opaque, stream) <= 0) + return 0; + + continue; + } + + memcpy(streamio->writeBuffer + streamio->writeBufferPos, (char *)buf + (bytesToWrite - bytesLeftToWrite), bytesToCopy); + + print_buf(opaque, stream, "write copy [remaining %d write %d:%d len %d]\n", bytesToCopy, bytesToWrite, bytesLeftToWrite, streamio->writeBufferLength); + + bytesLeftToWrite -= bytesToCopy; + + streamio->writeBufferPos += bytesToCopy; + streamio->writeBufferHits += 1; + if (streamio->writeBufferPos > streamio->writeBufferLength) + streamio->writeBufferLength += streamio->writeBufferPos - streamio->writeBufferLength; + } + + return size - bytesLeftToWrite; +} + +ZPOS64_T ftell_buf_internal_func (opaque, stream, position) + voidpf opaque; + voidpf stream; + ZPOS64_T position; +{ + ourstream_t *streamio = (ourstream_t *)stream; + streamio->position = position; + print_buf(opaque, stream, "tell [pos %llu readpos %d writepos %d err %d]\n", streamio->position, streamio->readBufferPos, streamio->writeBufferPos, errno); + if (streamio->readBufferLength > 0) + position -= (streamio->readBufferLength - streamio->readBufferPos); + if (streamio->writeBufferLength > 0) + position += streamio->writeBufferPos; + return position; +} + +long ZCALLBACK ftell_buf_func (opaque, stream) + voidpf opaque; + voidpf stream; +{ + ourbuffer_t *bufio = (ourbuffer_t *)opaque; + ourstream_t *streamio = (ourstream_t *)stream; + ZPOS64_T position = bufio->filefunc.ztell_file(bufio->filefunc.opaque, streamio->stream); + return (long)ftell_buf_internal_func(opaque, stream, position); +} + +ZPOS64_T ZCALLBACK ftell64_buf_func (opaque, stream) + voidpf opaque; + voidpf stream; +{ + ourbuffer_t *bufio = (ourbuffer_t *)opaque; + ourstream_t *streamio = (ourstream_t *)stream; + ZPOS64_T position = bufio->filefunc64.ztell64_file(bufio->filefunc64.opaque, streamio->stream); + return ftell_buf_internal_func(opaque, stream, position); +} + +int fseek_buf_internal_func (opaque, stream, offset, origin) + voidpf opaque; + voidpf stream; + ZPOS64_T offset; + int origin; +{ + ourstream_t *streamio = (ourstream_t *)stream; + + print_buf(opaque, stream, "seek [origin %d offset %llu pos %lld]\n", origin, offset, streamio->position); + + switch (origin) + { + case ZLIB_FILEFUNC_SEEK_SET: + + if (streamio->writeBufferLength > 0) + { + if ((offset >= streamio->position) && (offset <= streamio->position + streamio->writeBufferLength)) + { + streamio->writeBufferPos = (uLong)(offset - streamio->position); + return 0; + } + } + if ((streamio->readBufferLength > 0) && (offset < streamio->position) && (offset >= streamio->position - streamio->readBufferLength)) + { + streamio->readBufferPos = (uLong)(offset - (streamio->position - streamio->readBufferLength)); + return 0; + } + if (fflush_buf(opaque, stream) < 0) + return -1; + streamio->position = offset; + break; + + case ZLIB_FILEFUNC_SEEK_CUR: + + if (streamio->readBufferLength > 0) + { + if (offset <= (streamio->readBufferLength - streamio->readBufferPos)) + { + streamio->readBufferPos += (uLong)offset; + return 0; + } + offset -= (streamio->readBufferLength - streamio->readBufferPos); + streamio->position += offset; + } + if (streamio->writeBufferLength > 0) + { + if (offset <= (streamio->writeBufferLength - streamio->writeBufferPos)) + { + streamio->writeBufferPos += (uLong)offset; + return 0; + } + offset -= (streamio->writeBufferLength - streamio->writeBufferPos); + } + + if (fflush_buf(opaque, stream) < 0) + return -1; + + break; + + case ZLIB_FILEFUNC_SEEK_END: + + if (streamio->writeBufferLength > 0) + { + streamio->writeBufferPos = streamio->writeBufferLength; + return 0; + } + break; + } + + streamio->readBufferLength = 0; + streamio->readBufferPos = 0; + streamio->writeBufferLength = 0; + streamio->writeBufferPos = 0; + return 1; +} + +long ZCALLBACK fseek_buf_func (opaque, stream, offset, origin) + voidpf opaque; + voidpf stream; + uLong offset; + int origin; +{ + ourbuffer_t *bufio = (ourbuffer_t *)opaque; + ourstream_t *streamio = (ourstream_t *)stream; + int retVal = -1; + if (bufio->filefunc.zseek_file == NULL) + return retVal; + retVal = fseek_buf_internal_func(opaque, stream, offset, origin); + if (retVal == 1) + retVal = bufio->filefunc.zseek_file(bufio->filefunc.opaque, streamio->stream, offset, origin); + return retVal; +} + +long ZCALLBACK fseek64_buf_func (opaque, stream, offset, origin) + voidpf opaque; + voidpf stream; + ZPOS64_T offset; + int origin; +{ + ourbuffer_t *bufio = (ourbuffer_t *)opaque; + ourstream_t *streamio = (ourstream_t *)stream; + int retVal = -1; + if (bufio->filefunc64.zseek64_file == NULL) + return retVal; + retVal = fseek_buf_internal_func(opaque, stream, offset, origin); + if (retVal == 1) + retVal = bufio->filefunc64.zseek64_file(bufio->filefunc64.opaque, streamio->stream, offset, origin); + return retVal; +} + +int ZCALLBACK fclose_buf_func (opaque, stream) + voidpf opaque; + voidpf stream; +{ + ourbuffer_t *bufio = (ourbuffer_t *)opaque; + ourstream_t *streamio = (ourstream_t *)stream; + int retVal = 0; + fflush_buf(opaque, stream); + print_buf(opaque, stream, "close\n"); + if (streamio->readBufferHits + streamio->readBufferMisses > 0) + print_buf(opaque, stream, "read efficency %.02f%%\n", (streamio->readBufferHits / ((float)streamio->readBufferHits + streamio->readBufferMisses)) * 100); + if (streamio->writeBufferHits + streamio->writeBufferMisses > 0) + print_buf(opaque, stream, "write efficency %.02f%%\n", (streamio->writeBufferHits / ((float)streamio->writeBufferHits + streamio->writeBufferMisses)) * 100); + if (bufio->filefunc64.zclose_file != NULL) + retVal = bufio->filefunc64.zclose_file(bufio->filefunc64.opaque, streamio->stream); + else + retVal = bufio->filefunc.zclose_file(bufio->filefunc.opaque, streamio->stream); + free(streamio); + return retVal; +} + +int ZCALLBACK ferror_buf_func (opaque, stream) + voidpf opaque; + voidpf stream; +{ + ourbuffer_t *bufio = (ourbuffer_t *)opaque; + ourstream_t *streamio = (ourstream_t *)stream; + if (bufio->filefunc64.zerror_file != NULL) + return bufio->filefunc64.zerror_file(bufio->filefunc64.opaque, streamio->stream); + return bufio->filefunc.zerror_file(bufio->filefunc.opaque, streamio->stream); +} + + +void fill_buffer_filefunc (pzlib_filefunc_def, ourbuf) + zlib_filefunc_def* pzlib_filefunc_def; + ourbuffer_t *ourbuf; +{ + pzlib_filefunc_def->zopen_file = fopen_buf_func; + pzlib_filefunc_def->zopendisk_file = fopendisk_buf_func; + pzlib_filefunc_def->zread_file = fread_buf_func; + pzlib_filefunc_def->zwrite_file = fwrite_buf_func; + pzlib_filefunc_def->ztell_file = ftell_buf_func; + pzlib_filefunc_def->zseek_file = fseek_buf_func; + pzlib_filefunc_def->zclose_file = fclose_buf_func; + pzlib_filefunc_def->zerror_file = ferror_buf_func; + pzlib_filefunc_def->opaque = ourbuf; +} + +void fill_buffer_filefunc64 (pzlib_filefunc_def, ourbuf) + zlib_filefunc64_def* pzlib_filefunc_def; + ourbuffer_t *ourbuf; +{ + pzlib_filefunc_def->zopen64_file = fopen64_buf_func; + pzlib_filefunc_def->zopendisk64_file = fopendisk64_buf_func; + pzlib_filefunc_def->zread_file = fread_buf_func; + pzlib_filefunc_def->zwrite_file = fwrite_buf_func; + pzlib_filefunc_def->ztell64_file = ftell64_buf_func; + pzlib_filefunc_def->zseek64_file = fseek64_buf_func; + pzlib_filefunc_def->zclose_file = fclose_buf_func; + pzlib_filefunc_def->zerror_file = ferror_buf_func; + pzlib_filefunc_def->opaque = ourbuf; +} diff --git a/Zip/minizip/ioapi_buf.h b/Zip/minizip/ioapi_buf.h new file mode 100755 index 0000000000000000000000000000000000000000..2feb777b61e1c45f9b48dadd7fafcb68af6c8076 --- /dev/null +++ b/Zip/minizip/ioapi_buf.h @@ -0,0 +1,54 @@ +/* ioapi_buf.h -- IO base function header for compress/uncompress .zip + files using zlib + zip or unzip API + + This version of ioapi is designed to buffer IO. + + Copyright (C) 1998-2003 Gilles Vollant + (C) 2012-2014 Nathan Moinvaziri + + This program is distributed under the terms of the same license as zlib. + See the accompanying LICENSE file for the full text of the license. +*/ + +#ifndef _IOAPI_BUF_H +#define _IOAPI_BUF_H + +#include +#include +#include + +#include "zlib.h" +#include "ioapi.h" + +#define IOBUF_BUFFERSIZE (64 * 1024) + +#ifdef __cplusplus +extern "C" { +#endif + +voidpf ZCALLBACK fopen_buf_func OF((voidpf opaque,const char* filename,int mode)); +voidpf ZCALLBACK fopen64_buf_func OF((voidpf opaque,const char* filename,int mode)); +voidpf ZCALLBACK fopendisk_buf_func OF((voidpf opaque, voidpf stream_cd, int number_disk, int mode)); +voidpf ZCALLBACK fopendisk64_buf_func OF((voidpf opaque, voidpf stream_cd, int number_disk, int mode)); +uLong ZCALLBACK fread_buf_func OF((voidpf opaque,voidpf stream,void* buf,uLong size)); +uLong ZCALLBACK fwrite_buf_func OF((voidpf opaque,voidpf stream,const void* buf,uLong size)); +long ZCALLBACK ftell_buf_func OF((voidpf opaque,voidpf stream)); +ZPOS64_T ZCALLBACK ftell64_buf_func OF((voidpf opaque, voidpf stream)); +long ZCALLBACK fseek_buf_func OF((voidpf opaque,voidpf stream,uLong offset,int origin)); +long ZCALLBACK fseek64_buf_func OF((voidpf opaque, voidpf stream, ZPOS64_T offset, int origin)); +int ZCALLBACK fclose_buf_func OF((voidpf opaque,voidpf stream)); +int ZCALLBACK ferror_buf_func OF((voidpf opaque,voidpf stream)); + +typedef struct ourbuffer_s { + zlib_filefunc_def filefunc; + zlib_filefunc64_def filefunc64; +} ourbuffer_t; + +void fill_buffer_filefunc OF((zlib_filefunc_def* pzlib_filefunc_def, ourbuffer_t *ourbuf)); +void fill_buffer_filefunc64 OF((zlib_filefunc64_def* pzlib_filefunc_def, ourbuffer_t *ourbuf)); + +#ifdef __cplusplus +} +#endif + +#endif diff --git a/Zip/minizip/ioapi_mem.c b/Zip/minizip/ioapi_mem.c new file mode 100755 index 0000000000000000000000000000000000000000..aaea039e3519a7de61229d1f0d3bbd97400ab64c --- /dev/null +++ b/Zip/minizip/ioapi_mem.c @@ -0,0 +1,192 @@ +/* ioapi_mem.h -- IO base function header for compress/uncompress .zip + files using zlib + zip or unzip API + + This version of ioapi is designed to access memory rather than files. + We do use a region of memory to put data in to and take it out of. We do + not have auto-extending buffers and do not inform anyone else that the + data has been written. It is really intended for accessing a zip archive + embedded in an application such that I can write an installer with no + external files. Creation of archives has not been attempted, although + parts of the framework are present. + + Based on Unzip ioapi.c version 0.22, May 19th, 2003 + + Copyright (C) 1998-2003 Gilles Vollant + (C) 2003 Justin Fletcher + + This file is under the same license as the Unzip tool it is distributed + with. +*/ + + +#include +#include +#include + +#include "zlib.h" +#include "ioapi.h" + +#include "ioapi_mem.h" + +#ifndef IOMEM_BUFFERSIZE +# define IOMEM_BUFFERSIZE (64 * 1024) +#endif + +voidpf ZCALLBACK fopen_mem_func (opaque, filename, mode) + voidpf opaque; + const char* filename; + int mode; +{ + ourmemory_t *mem = (ourmemory_t *)opaque; + if (mem == NULL) + return NULL; /* Mem structure passed in was null */ + + if (mode & ZLIB_FILEFUNC_MODE_CREATE) + { + if (mem->grow) + { + mem->size = IOMEM_BUFFERSIZE; + mem->base = (char *)malloc(mem->size); + } + + mem->limit = 0; /* When writing we start with 0 bytes written */ + } + else + mem->limit = mem->size; + + mem->cur_offset = 0; + + return mem; +} + +voidpf ZCALLBACK fopendisk_mem_func (opaque, stream, number_disk, mode) + voidpf opaque; + voidpf stream; + int number_disk; + int mode; +{ + /* Not used */ + return NULL; +} + +uLong ZCALLBACK fread_mem_func (opaque, stream, buf, size) + voidpf opaque; + voidpf stream; + void* buf; + uLong size; +{ + ourmemory_t *mem = (ourmemory_t *)stream; + + if (size > mem->size - mem->cur_offset) + size = mem->size - mem->cur_offset; + + memcpy(buf, mem->base + mem->cur_offset, size); + mem->cur_offset += size; + + return size; +} + + +uLong ZCALLBACK fwrite_mem_func (opaque, stream, buf, size) + voidpf opaque; + voidpf stream; + const void* buf; + uLong size; +{ + ourmemory_t *mem = (ourmemory_t *)stream; + char *newbase = NULL; + uLong newmemsize = 0; + + if (size > mem->size - mem->cur_offset) + { + if (mem->grow) + { + newmemsize = mem->size; + if (size < IOMEM_BUFFERSIZE) + newmemsize += IOMEM_BUFFERSIZE; + else + newmemsize += size; + newbase = (char *)malloc(newmemsize); + memcpy(newbase, mem->base, mem->size); + free(mem->base); + mem->base = newbase; + mem->size = newmemsize; + } + else + size = mem->size - mem->cur_offset; + } + memcpy(mem->base + mem->cur_offset, buf, size); + mem->cur_offset += size; + if (mem->cur_offset > mem->limit) + mem->limit = mem->cur_offset; + + return size; +} + +long ZCALLBACK ftell_mem_func (opaque, stream) + voidpf opaque; + voidpf stream; +{ + ourmemory_t *mem = (ourmemory_t *)stream; + return mem->cur_offset; +} + +long ZCALLBACK fseek_mem_func (opaque, stream, offset, origin) + voidpf opaque; + voidpf stream; + uLong offset; + int origin; +{ + ourmemory_t *mem = (ourmemory_t *)stream; + uLong new_pos; + switch (origin) + { + case ZLIB_FILEFUNC_SEEK_CUR: + new_pos = mem->cur_offset + offset; + break; + case ZLIB_FILEFUNC_SEEK_END: + new_pos = mem->limit + offset; + break; + case ZLIB_FILEFUNC_SEEK_SET: + new_pos = offset; + break; + default: + return -1; + } + + if (new_pos > mem->size) + return 1; /* Failed to seek that far */ + mem->cur_offset = new_pos; + return 0; +} + +int ZCALLBACK fclose_mem_func (opaque, stream) + voidpf opaque; + voidpf stream; +{ + /* Even with grow = 1, caller must always free() memory */ + return 0; +} + +int ZCALLBACK ferror_mem_func (opaque, stream) + voidpf opaque; + voidpf stream; +{ + /* We never return errors */ + return 0; +} + +void fill_memory_filefunc (pzlib_filefunc_def, ourmem) + zlib_filefunc_def* pzlib_filefunc_def; + ourmemory_t *ourmem; +{ + pzlib_filefunc_def->zopen_file = fopen_mem_func; + pzlib_filefunc_def->zopendisk_file = fopendisk_mem_func; + pzlib_filefunc_def->zread_file = fread_mem_func; + pzlib_filefunc_def->zwrite_file = fwrite_mem_func; + pzlib_filefunc_def->ztell_file = ftell_mem_func; + pzlib_filefunc_def->zseek_file = fseek_mem_func; + pzlib_filefunc_def->zclose_file = fclose_mem_func; + pzlib_filefunc_def->zerror_file = ferror_mem_func; + pzlib_filefunc_def->opaque = ourmem; +} diff --git a/Zip/minizip/ioapi_mem.h b/Zip/minizip/ioapi_mem.h new file mode 100755 index 0000000000000000000000000000000000000000..fdbcd1ec9e5b513a8d60b8446124a5a67f9b5022 --- /dev/null +++ b/Zip/minizip/ioapi_mem.h @@ -0,0 +1,51 @@ +/* ioapi_mem.h -- IO base function header for compress/uncompress .zip + files using zlib + zip or unzip API + + This version of ioapi is designed to access memory rather than files. + We do use a region of memory to put data in to and take it out of. + + Copyright (C) 1998-2003 Gilles Vollant + (C) 2003 Justin Fletcher + + This program is distributed under the terms of the same license as zlib. + See the accompanying LICENSE file for the full text of the license. +*/ + +#ifndef _IOAPI_MEM_H +#define _IOAPI_MEM_H + +#include +#include +#include + +#include "zlib.h" +#include "ioapi.h" + +#ifdef __cplusplus +extern "C" { +#endif + +voidpf ZCALLBACK fopen_mem_func OF((voidpf opaque,const char* filename,int mode)); +voidpf ZCALLBACK fopendisk_mem_func OF((voidpf opaque, voidpf stream, int number_disk, int mode)); +uLong ZCALLBACK fread_mem_func OF((voidpf opaque,voidpf stream,void* buf,uLong size)); +uLong ZCALLBACK fwrite_mem_func OF((voidpf opaque,voidpf stream,const void* buf,uLong size)); +long ZCALLBACK ftell_mem_func OF((voidpf opaque,voidpf stream)); +long ZCALLBACK fseek_mem_func OF((voidpf opaque,voidpf stream,uLong offset,int origin)); +int ZCALLBACK fclose_mem_func OF((voidpf opaque,voidpf stream)); +int ZCALLBACK ferror_mem_func OF((voidpf opaque,voidpf stream)); + +typedef struct ourmemory_s { + char *base; /* Base of the region of memory we're using */ + uLong size; /* Size of the region of memory we're using */ + uLong limit; /* Furthest we've written */ + uLong cur_offset; /* Current offset in the area */ + int grow; /* Growable memory buffer */ +} ourmemory_t; + +void fill_memory_filefunc OF((zlib_filefunc_def* pzlib_filefunc_def, ourmemory_t *ourmem)); + +#ifdef __cplusplus +} +#endif + +#endif diff --git a/Zip/minizip/iowin32.c b/Zip/minizip/iowin32.c new file mode 100755 index 0000000000000000000000000000000000000000..59cd420f25458724034bd76b954ebb75c81082fc --- /dev/null +++ b/Zip/minizip/iowin32.c @@ -0,0 +1,585 @@ +/* iowin32.c -- IO base function header for compress/uncompress .zip + Version 1.1, February 14h, 2010 + part of the MiniZip project + + Copyright (C) 1998-2010 Gilles Vollant + http://www.winimage.com/zLibDll/minizip.html + Modifications for Zip64 support + Copyright (C) 2009-2010 Mathias Svensson + http://result42.com + + This program is distributed under the terms of the same license as zlib. + See the accompanying LICENSE file for the full text of the license. +*/ + +#include +#include + +#include "zlib.h" +#include "ioapi.h" +#include "iowin32.h" + +#ifndef INVALID_HANDLE_VALUE +# define INVALID_HANDLE_VALUE (0xFFFFFFFF) +#endif + +#ifndef INVALID_SET_FILE_POINTER +# define INVALID_SET_FILE_POINTER ((DWORD)-1) +#endif + +#if defined(WINAPI_FAMILY_PARTITION) && (!(defined(IOWIN32_USING_WINRT_API))) +# if !WINAPI_FAMILY_PARTITION(WINAPI_PARTITION_DESKTOP) +# define IOWIN32_USING_WINRT_API 1 +# endif +#endif + +voidpf ZCALLBACK win32_open_file_func OF((voidpf opaque, const char* filename, int mode)); +uLong ZCALLBACK win32_read_file_func OF((voidpf opaque, voidpf stream, void* buf, uLong size)); +uLong ZCALLBACK win32_write_file_func OF((voidpf opaque, voidpf stream, const void* buf, uLong size)); +ZPOS64_T ZCALLBACK win32_tell64_file_func OF((voidpf opaque, voidpf stream)); +long ZCALLBACK win32_seek64_file_func OF((voidpf opaque, voidpf stream, ZPOS64_T offset, int origin)); +int ZCALLBACK win32_close_file_func OF((voidpf opaque, voidpf stream)); +int ZCALLBACK win32_error_file_func OF((voidpf opaque, voidpf stream)); + +typedef struct +{ + HANDLE hf; + int error; + void *filename; + int filenameLength; +} WIN32FILE_IOWIN; + + +static void win32_translate_open_mode(int mode, + DWORD* lpdwDesiredAccess, + DWORD* lpdwCreationDisposition, + DWORD* lpdwShareMode, + DWORD* lpdwFlagsAndAttributes) +{ + *lpdwDesiredAccess = *lpdwShareMode = *lpdwFlagsAndAttributes = *lpdwCreationDisposition = 0; + + if ((mode & ZLIB_FILEFUNC_MODE_READWRITEFILTER) == ZLIB_FILEFUNC_MODE_READ) + { + *lpdwDesiredAccess = GENERIC_READ; + *lpdwCreationDisposition = OPEN_EXISTING; + *lpdwShareMode = FILE_SHARE_READ; + } + else if (mode & ZLIB_FILEFUNC_MODE_EXISTING) + { + *lpdwDesiredAccess = GENERIC_WRITE | GENERIC_READ; + *lpdwCreationDisposition = OPEN_EXISTING; + } + else if (mode & ZLIB_FILEFUNC_MODE_CREATE) + { + *lpdwDesiredAccess = GENERIC_WRITE | GENERIC_READ; + *lpdwCreationDisposition = CREATE_ALWAYS; + } +} + +static voidpf win32_build_iowin(HANDLE hFile) +{ + WIN32FILE_IOWIN *iowin = NULL; + + if ((hFile != NULL) && (hFile != INVALID_HANDLE_VALUE)) + { + iowin = (WIN32FILE_IOWIN *)malloc(sizeof(WIN32FILE_IOWIN)); + if (iowin == NULL) + { + CloseHandle(hFile); + return NULL; + } + memset(iowin, 0, sizeof(WIN32FILE_IOWIN)); + iowin->hf = hFile; + } + return (voidpf)iowin; +} + +voidpf ZCALLBACK win32_open64_file_func (voidpf opaque, const void* filename, int mode) +{ + DWORD dwDesiredAccess,dwCreationDisposition,dwShareMode,dwFlagsAndAttributes ; + HANDLE hFile = NULL; + WIN32FILE_IOWIN *iowin = NULL; + + win32_translate_open_mode(mode, &dwDesiredAccess, &dwCreationDisposition, &dwShareMode, &dwFlagsAndAttributes); + + if ((filename != NULL) && (dwDesiredAccess != 0)) + { +#ifdef IOWIN32_USING_WINRT_API +#ifdef UNICODE + hFile = CreateFile2((LPCTSTR)filename, dwDesiredAccess, dwShareMode, dwCreationDisposition, NULL); +#else + WCHAR filenameW[FILENAME_MAX + 0x200 + 1]; + MultiByteToWideChar(CP_ACP, 0, (const char*)filename, -1, filenameW, FILENAME_MAX + 0x200); + hFile = CreateFile2(filenameW, dwDesiredAccess, dwShareMode, dwCreationDisposition, NULL); +#endif +#else + hFile = CreateFile((LPCTSTR)filename, dwDesiredAccess, dwShareMode, NULL, dwCreationDisposition, dwFlagsAndAttributes, NULL); +#endif + } + + iowin = win32_build_iowin(hFile); + if (iowin == NULL) + return NULL; + iowin->filenameLength = _tcslen(filename) + 1; + iowin->filename = (void*)malloc(iowin->filenameLength * sizeof(TCHAR)); + _tcsncpy(iowin->filename, filename, iowin->filenameLength); + return iowin; +} + + +voidpf ZCALLBACK win32_open64_file_funcA (voidpf opaque, const void* filename, int mode) +{ + DWORD dwDesiredAccess, dwCreationDisposition, dwShareMode, dwFlagsAndAttributes ; + HANDLE hFile = NULL; + WIN32FILE_IOWIN *iowin = NULL; + + win32_translate_open_mode(mode, &dwDesiredAccess, &dwCreationDisposition, &dwShareMode, &dwFlagsAndAttributes); + + if ((filename != NULL) && (dwDesiredAccess != 0)) + { +#ifdef IOWIN32_USING_WINRT_API + WCHAR filenameW[FILENAME_MAX + 0x200 + 1]; + MultiByteToWideChar(CP_ACP, 0, (const char*)filename, -1, filenameW, FILENAME_MAX + 0x200); + hFile = CreateFile2(filenameW, dwDesiredAccess, dwShareMode, dwCreationDisposition, NULL); +#else + hFile = CreateFileA((LPCSTR)filename, dwDesiredAccess, dwShareMode, NULL, dwCreationDisposition, dwFlagsAndAttributes, NULL); +#endif + } + + iowin = win32_build_iowin(hFile); + if (iowin == NULL) + return NULL; + iowin->filenameLength = strlen(filename) + 1; + iowin->filename = (void*)malloc(iowin->filenameLength * sizeof(char)); + strncpy(iowin->filename, filename, iowin->filenameLength); + return iowin; +} + + +voidpf ZCALLBACK win32_open64_file_funcW (voidpf opaque,const void* filename,int mode) +{ + DWORD dwDesiredAccess, dwCreationDisposition, dwShareMode, dwFlagsAndAttributes; + HANDLE hFile = NULL; + WIN32FILE_IOWIN *iowin = NULL; + + win32_translate_open_mode(mode, &dwDesiredAccess, &dwCreationDisposition, &dwShareMode, &dwFlagsAndAttributes); + + if ((filename != NULL) && (dwDesiredAccess != 0)) + { +#ifdef IOWIN32_USING_WINRT_API + hFile = CreateFile2((LPCWSTR)filename, dwDesiredAccess, dwShareMode, dwCreationDisposition, NULL); +#else + hFile = CreateFileW((LPCWSTR)filename, dwDesiredAccess, dwShareMode, NULL, dwCreationDisposition, dwFlagsAndAttributes, NULL); +#endif + } + + iowin = win32_build_iowin(hFile); + if (iowin == NULL) + return NULL; + if (iowin->filename == NULL) + { + iowin->filenameLength = wcslen(filename) + 1; + iowin->filename = (void*)malloc(iowin->filenameLength * sizeof(WCHAR)); + wcsncpy(iowin->filename, filename, iowin->filenameLength); + } + return iowin; +} + +voidpf ZCALLBACK win32_open_file_func (voidpf opaque,const char* filename,int mode) +{ + DWORD dwDesiredAccess, dwCreationDisposition, dwShareMode, dwFlagsAndAttributes ; + HANDLE hFile = NULL; + WIN32FILE_IOWIN *iowin = NULL; + + win32_translate_open_mode(mode, &dwDesiredAccess, &dwCreationDisposition, &dwShareMode, &dwFlagsAndAttributes); + + if ((filename != NULL) && (dwDesiredAccess != 0)) + { +#ifdef IOWIN32_USING_WINRT_API +#ifdef UNICODE + hFile = CreateFile2((LPCTSTR)filename, dwDesiredAccess, dwShareMode, dwCreationDisposition, NULL); +#else + WCHAR filenameW[FILENAME_MAX + 0x200 + 1]; + MultiByteToWideChar(CP_ACP, 0, (const char*)filename, -1, filenameW, FILENAME_MAX + 0x200); + hFile = CreateFile2(filenameW, dwDesiredAccess, dwShareMode, dwCreationDisposition, NULL); +#endif +#else + hFile = CreateFile((LPCTSTR)filename, dwDesiredAccess, dwShareMode, NULL, dwCreationDisposition, dwFlagsAndAttributes, NULL); +#endif + } + + iowin = win32_build_iowin(hFile); + if (iowin == NULL) + return NULL; + iowin->filenameLength = _tcslen((TCHAR*)filename) + 1; + iowin->filename = (void*)malloc(iowin->filenameLength * sizeof(TCHAR)); + _tcsncpy(iowin->filename, (TCHAR*)filename, iowin->filenameLength); + return iowin; +} + +voidpf ZCALLBACK win32_opendisk64_file_func (voidpf opaque, voidpf stream, int number_disk, int mode) +{ + WIN32FILE_IOWIN *iowin = NULL; + TCHAR *diskFilename = NULL; + voidpf ret = NULL; + int i = 0; + + if (stream == NULL) + return NULL; + iowin = (WIN32FILE_IOWIN*)stream; + diskFilename = (TCHAR*)malloc(iowin->filenameLength * sizeof(TCHAR)); + _tcsncpy(diskFilename, iowin->filename, iowin->filenameLength); + for (i = iowin->filenameLength - 1; i >= 0; i -= 1) + { + if (diskFilename[i] != _T('.')) + continue; + _sntprintf(&diskFilename[i], iowin->filenameLength - i, _T(".z%02d"), number_disk + 1); + break; + } + if (i >= 0) + ret = win32_open64_file_func(opaque, (char*)diskFilename, mode); + free(diskFilename); + return ret; +} + +voidpf ZCALLBACK win32_opendisk64_file_funcW (voidpf opaque, voidpf stream, int number_disk, int mode) +{ + WIN32FILE_IOWIN *iowin = NULL; + WCHAR *diskFilename = NULL; + voidpf ret = NULL; + int i = 0; + + if (stream == NULL) + return NULL; + iowin = (WIN32FILE_IOWIN*)stream; + diskFilename = (WCHAR*)malloc((iowin->filenameLength + 10) * sizeof(WCHAR)); + wcsncpy(diskFilename, iowin->filename, iowin->filenameLength); + for (i = iowin->filenameLength - 1; i >= 0; i -= 1) + { + if (diskFilename[i] != L'.') + continue; + _snwprintf(&diskFilename[i], (iowin->filenameLength + 10) - i, L".z%02d", number_disk + 1); + break; + } + if (i >= 0) + ret = win32_open64_file_funcW(opaque, diskFilename, mode); + free(diskFilename); + return ret; +} + +voidpf ZCALLBACK win32_opendisk64_file_funcA (voidpf opaque, voidpf stream, int number_disk, int mode) +{ + WIN32FILE_IOWIN *iowin = NULL; + char *diskFilename = NULL; + voidpf ret = NULL; + int i = 0; + + if (stream == NULL) + return NULL; + iowin = (WIN32FILE_IOWIN*)stream; + diskFilename = (char*)malloc(iowin->filenameLength * sizeof(char)); + strncpy(diskFilename, iowin->filename, iowin->filenameLength); + for (i = iowin->filenameLength - 1; i >= 0; i -= 1) + { + if (diskFilename[i] != '.') + continue; + _snprintf(&diskFilename[i], iowin->filenameLength - i, ".z%02d", number_disk + 1); + break; + } + if (i >= 0) + ret = win32_open64_file_funcA(opaque, diskFilename, mode); + free(diskFilename); + return ret; +} + +voidpf ZCALLBACK win32_opendisk_file_func (voidpf opaque, voidpf stream, int number_disk, int mode) +{ + WIN32FILE_IOWIN *iowin = NULL; + TCHAR *diskFilename = NULL; + voidpf ret = NULL; + int i = 0; + + if (stream == NULL) + return NULL; + iowin = (WIN32FILE_IOWIN*)stream; + diskFilename = (TCHAR*)malloc(iowin->filenameLength * sizeof(TCHAR)); + _tcsncpy(diskFilename, iowin->filename, iowin->filenameLength); + for (i = iowin->filenameLength - 1; i >= 0; i -= 1) + { + if (diskFilename[i] != _T('.')) + continue; + _sntprintf(&diskFilename[i], iowin->filenameLength - i, _T(".z%02d"), number_disk + 1); + break; + } + if (i >= 0) + ret = win32_open_file_func(opaque, (char*)diskFilename, mode); + free(diskFilename); + return ret; +} + +uLong ZCALLBACK win32_read_file_func (voidpf opaque, voidpf stream, void* buf,uLong size) +{ + uLong ret = 0; + HANDLE hFile = NULL; + if (stream != NULL) + hFile = ((WIN32FILE_IOWIN*)stream)->hf; + + if (hFile != NULL) + { + if (!ReadFile(hFile, buf, size, &ret, NULL)) + { + DWORD dwErr = GetLastError(); + if (dwErr == ERROR_HANDLE_EOF) + dwErr = 0; + ((WIN32FILE_IOWIN*)stream)->error = (int)dwErr; + } + } + + return ret; +} + +uLong ZCALLBACK win32_write_file_func (voidpf opaque,voidpf stream,const void* buf,uLong size) +{ + uLong ret = 0; + HANDLE hFile = NULL; + if (stream != NULL) + hFile = ((WIN32FILE_IOWIN*)stream)->hf; + + if (hFile != NULL) + { + if (!WriteFile(hFile, buf, size, &ret, NULL)) + { + DWORD dwErr = GetLastError(); + if (dwErr == ERROR_HANDLE_EOF) + dwErr = 0; + ((WIN32FILE_IOWIN*)stream)->error = (int)dwErr; + } + } + + return ret; +} + +static BOOL win32_setfilepointer_internal(HANDLE hFile, LARGE_INTEGER pos, LARGE_INTEGER *newPos, DWORD dwMoveMethod) +{ +#ifdef IOWIN32_USING_WINRT_API + return SetFilePointerEx(hFile, pos, newPos, dwMoveMethod); +#else + LONG lHigh = pos.HighPart; + BOOL ret = TRUE; + DWORD dwNewPos = SetFilePointer(hFile, pos.LowPart, &lHigh, dwMoveMethod); + if ((dwNewPos == INVALID_SET_FILE_POINTER) && (GetLastError() != NO_ERROR)) + ret = FALSE; + if ((newPos != NULL) && (ret)) + { + newPos->LowPart = dwNewPos; + newPos->HighPart = lHigh; + } + return ret; +#endif +} + +long ZCALLBACK win32_tell_file_func (voidpf opaque,voidpf stream) +{ + long ret = -1; + HANDLE hFile = NULL; + if (stream != NULL) + hFile = ((WIN32FILE_IOWIN*)stream)->hf; + if (hFile != NULL) + { + LARGE_INTEGER pos; + pos.QuadPart = 0; + if (!win32_setfilepointer_internal(hFile, pos, &pos, FILE_CURRENT)) + { + DWORD dwErr = GetLastError(); + ((WIN32FILE_IOWIN*)stream)->error = (int)dwErr; + ret = -1; + } + else + ret = (long)pos.LowPart; + } + return ret; +} + +ZPOS64_T ZCALLBACK win32_tell64_file_func (voidpf opaque, voidpf stream) +{ + ZPOS64_T ret = (ZPOS64_T)-1; + HANDLE hFile = NULL; + if (stream != NULL) + hFile = ((WIN32FILE_IOWIN*)stream)->hf; + + if (hFile) + { + LARGE_INTEGER pos; + pos.QuadPart = 0; + if (!win32_setfilepointer_internal(hFile, pos, &pos, FILE_CURRENT)) + { + DWORD dwErr = GetLastError(); + ((WIN32FILE_IOWIN*)stream)->error = (int)dwErr; + ret = (ZPOS64_T)-1; + } + else + ret = pos.QuadPart; + } + return ret; +} + + +long ZCALLBACK win32_seek_file_func (voidpf opaque,voidpf stream,uLong offset,int origin) +{ + DWORD dwMoveMethod = 0xFFFFFFFF; + HANDLE hFile = NULL; + long ret = -1; + + if (stream != NULL) + hFile = ((WIN32FILE_IOWIN*)stream)->hf; + + switch (origin) + { + case ZLIB_FILEFUNC_SEEK_CUR: + dwMoveMethod = FILE_CURRENT; + break; + case ZLIB_FILEFUNC_SEEK_END: + dwMoveMethod = FILE_END; + break; + case ZLIB_FILEFUNC_SEEK_SET: + dwMoveMethod = FILE_BEGIN; + break; + default: + return -1; + } + + if (hFile != NULL) + { + LARGE_INTEGER pos; + pos.QuadPart = offset; + if (!win32_setfilepointer_internal(hFile, pos, NULL, dwMoveMethod)) + { + DWORD dwErr = GetLastError(); + ((WIN32FILE_IOWIN*)stream)->error = (int)dwErr; + ret = -1; + } + else + ret = 0; + } + return ret; +} + +long ZCALLBACK win32_seek64_file_func (voidpf opaque, voidpf stream,ZPOS64_T offset,int origin) +{ + DWORD dwMoveMethod = 0xFFFFFFFF; + HANDLE hFile = NULL; + long ret = -1; + + if (stream != NULL) + hFile = ((WIN32FILE_IOWIN*)stream)->hf; + + switch (origin) + { + case ZLIB_FILEFUNC_SEEK_CUR: + dwMoveMethod = FILE_CURRENT; + break; + case ZLIB_FILEFUNC_SEEK_END: + dwMoveMethod = FILE_END; + break; + case ZLIB_FILEFUNC_SEEK_SET: + dwMoveMethod = FILE_BEGIN; + break; + default: + return -1; + } + + if (hFile) + { + LARGE_INTEGER pos; + pos.QuadPart = offset; + if (!win32_setfilepointer_internal(hFile, pos, NULL, dwMoveMethod)) + { + DWORD dwErr = GetLastError(); + ((WIN32FILE_IOWIN*)stream)->error = (int)dwErr; + ret = -1; + } + else + ret = 0; + } + return ret; +} + +int ZCALLBACK win32_close_file_func (voidpf opaque, voidpf stream) +{ + WIN32FILE_IOWIN* iowin = NULL; + int ret = -1; + + if (stream == NULL) + return ret; + iowin = ((WIN32FILE_IOWIN*)stream); + if (iowin->filename != NULL) + free(iowin->filename); + if (iowin->hf != NULL) + { + CloseHandle(iowin->hf); + ret=0; + } + free(stream); + return ret; +} + +int ZCALLBACK win32_error_file_func (voidpf opaque, voidpf stream) +{ + int ret = -1; + if (stream == NULL) + return ret; + ret = ((WIN32FILE_IOWIN*)stream)->error; + return ret; +} + +void fill_win32_filefunc (zlib_filefunc_def* pzlib_filefunc_def) +{ + pzlib_filefunc_def->zopen_file = win32_open_file_func; + pzlib_filefunc_def->zopendisk_file = win32_opendisk_file_func; + pzlib_filefunc_def->zread_file = win32_read_file_func; + pzlib_filefunc_def->zwrite_file = win32_write_file_func; + pzlib_filefunc_def->ztell_file = win32_tell_file_func; + pzlib_filefunc_def->zseek_file = win32_seek_file_func; + pzlib_filefunc_def->zclose_file = win32_close_file_func; + pzlib_filefunc_def->zerror_file = win32_error_file_func; + pzlib_filefunc_def->opaque = NULL; +} + +void fill_win32_filefunc64(zlib_filefunc64_def* pzlib_filefunc_def) +{ + pzlib_filefunc_def->zopen64_file = win32_open64_file_func; + pzlib_filefunc_def->zopendisk64_file = win32_opendisk64_file_func; + pzlib_filefunc_def->zread_file = win32_read_file_func; + pzlib_filefunc_def->zwrite_file = win32_write_file_func; + pzlib_filefunc_def->ztell64_file = win32_tell64_file_func; + pzlib_filefunc_def->zseek64_file = win32_seek64_file_func; + pzlib_filefunc_def->zclose_file = win32_close_file_func; + pzlib_filefunc_def->zerror_file = win32_error_file_func; + pzlib_filefunc_def->opaque = NULL; +} + +void fill_win32_filefunc64A(zlib_filefunc64_def* pzlib_filefunc_def) +{ + pzlib_filefunc_def->zopen64_file = win32_open64_file_funcA; + pzlib_filefunc_def->zopendisk64_file = win32_opendisk64_file_funcA; + pzlib_filefunc_def->zread_file = win32_read_file_func; + pzlib_filefunc_def->zwrite_file = win32_write_file_func; + pzlib_filefunc_def->ztell64_file = win32_tell64_file_func; + pzlib_filefunc_def->zseek64_file = win32_seek64_file_func; + pzlib_filefunc_def->zclose_file = win32_close_file_func; + pzlib_filefunc_def->zerror_file = win32_error_file_func; + pzlib_filefunc_def->opaque = NULL; +} + +void fill_win32_filefunc64W(zlib_filefunc64_def* pzlib_filefunc_def) +{ + pzlib_filefunc_def->zopen64_file = win32_open64_file_funcW; + pzlib_filefunc_def->zopendisk64_file = win32_opendisk64_file_funcW; + pzlib_filefunc_def->zread_file = win32_read_file_func; + pzlib_filefunc_def->zwrite_file = win32_write_file_func; + pzlib_filefunc_def->ztell64_file = win32_tell64_file_func; + pzlib_filefunc_def->zseek64_file = win32_seek64_file_func; + pzlib_filefunc_def->zclose_file = win32_close_file_func; + pzlib_filefunc_def->zerror_file = win32_error_file_func; + pzlib_filefunc_def->opaque = NULL; +} diff --git a/Zip/minizip/iowin32.h b/Zip/minizip/iowin32.h new file mode 100755 index 0000000000000000000000000000000000000000..466e1d2f72e7c12ab99756948ee2707923f4b679 --- /dev/null +++ b/Zip/minizip/iowin32.h @@ -0,0 +1,33 @@ +/* iowin32.h -- IO base function header for compress/uncompress .zip + Version 1.1, February 14h, 2010 + part of the MiniZip project + + Copyright (C) 1998-2010 Gilles Vollant + http://www.winimage.com/zLibDll/minizip.html + Modifications for Zip64 support + Copyright (C) 2009-2010 Mathias Svensson + http://result42.com + + This program is distributed under the terms of the same license as zlib. + See the accompanying LICENSE file for the full text of the license. +*/ + +#ifndef _IOWIN32_H +#define _IOWIN32_H + +#include + +#ifdef __cplusplus +extern "C" { +#endif + +void fill_win32_filefunc OF((zlib_filefunc_def* pzlib_filefunc_def)); +void fill_win32_filefunc64 OF((zlib_filefunc64_def* pzlib_filefunc_def)); +void fill_win32_filefunc64A OF((zlib_filefunc64_def* pzlib_filefunc_def)); +void fill_win32_filefunc64W OF((zlib_filefunc64_def* pzlib_filefunc_def)); + +#ifdef __cplusplus +} +#endif + +#endif diff --git a/Zip/minizip/miniunz.c b/Zip/minizip/miniunz.c new file mode 100755 index 0000000000000000000000000000000000000000..9a9e98226774e4fa127c192967f80fd9b546886c --- /dev/null +++ b/Zip/minizip/miniunz.c @@ -0,0 +1,573 @@ +/* miniunz.c + Version 1.1, February 14h, 2010 + sample part of the MiniZip project + + Copyright (C) 1998-2010 Gilles Vollant + http://www.winimage.com/zLibDll/minizip.html + Modifications of Unzip for Zip64 + Copyright (C) 2007-2008 Even Rouault + Modifications for Zip64 support + Copyright (C) 2009-2010 Mathias Svensson + http://result42.com + + This program is distributed under the terms of the same license as zlib. + See the accompanying LICENSE file for the full text of the license. +*/ + +#if (!defined(_WIN32)) && (!defined(WIN32)) && (!defined(__APPLE__)) +# ifndef __USE_FILE_OFFSET64 +# define __USE_FILE_OFFSET64 +# endif +# ifndef __USE_LARGEFILE64 +# define __USE_LARGEFILE64 +# endif +# ifndef _LARGEFILE64_SOURCE +# define _LARGEFILE64_SOURCE +# endif +# ifndef _FILE_OFFSET_BIT +# define _FILE_OFFSET_BIT 64 +# endif +#endif + +#ifdef __APPLE__ +/* In darwin and perhaps other BSD variants off_t is a 64 bit value, hence no need for specific 64 bit functions */ +# define FOPEN_FUNC(filename, mode) fopen(filename, mode) +# define FTELLO_FUNC(stream) ftello(stream) +# define FSEEKO_FUNC(stream, offset, origin) fseeko(stream, offset, origin) +#else +# define FOPEN_FUNC(filename, mode) fopen64(filename, mode) +# define FTELLO_FUNC(stream) ftello64(stream) +# define FSEEKO_FUNC(stream, offset, origin) fseeko64(stream, offset, origin) +#endif + +#include +#include +#include +#include +#include +#include + +#ifdef _WIN32 +# include +# include +#else +# include +# include +# include +#endif + +#ifdef _WIN32 +# define MKDIR(d) _mkdir(d) +# define CHDIR(d) _chdir(d) +#else +# define MKDIR(d) mkdir(d, 0775) +# define CHDIR(d) chdir(d) +#endif + +#include "unzip.h" + +#define WRITEBUFFERSIZE (8192) +#define MAXFILENAME (256) + +#ifdef _WIN32 +# define USEWIN32IOAPI +# include "iowin32.h" +#endif + +void change_file_date(const char *filename, uLong dosdate, tm_unz tmu_date) +{ +#ifdef _WIN32 + HANDLE hFile; + FILETIME ftm, ftLocal, ftCreate, ftLastAcc, ftLastWrite; + + hFile = CreateFileA(filename, GENERIC_READ | GENERIC_WRITE, 0, NULL, OPEN_EXISTING, 0, NULL); + if (hFile != INVALID_HANDLE_VALUE) + { + GetFileTime(hFile, &ftCreate, &ftLastAcc, &ftLastWrite); + DosDateTimeToFileTime((WORD)(dosdate>>16),(WORD)dosdate, &ftLocal); + LocalFileTimeToFileTime(&ftLocal, &ftm); + SetFileTime(hFile, &ftm, &ftLastAcc, &ftm); + CloseHandle(hFile); + } +#else +#if defined unix || defined __APPLE__ + struct utimbuf ut; + struct tm newdate; + + newdate.tm_sec = tmu_date.tm_sec; + newdate.tm_min = tmu_date.tm_min; + newdate.tm_hour = tmu_date.tm_hour; + newdate.tm_mday = tmu_date.tm_mday; + newdate.tm_mon = tmu_date.tm_mon; + if (tmu_date.tm_year > 1900) + newdate.tm_year = tmu_date.tm_year - 1900; + else + newdate.tm_year = tmu_date.tm_year ; + newdate.tm_isdst = -1; + + ut.actime = ut.modtime = mktime(&newdate); + utime(filename,&ut); +#endif +#endif +} + +int check_file_exists(const char* filename) +{ + FILE* ftestexist = FOPEN_FUNC(filename,"rb"); + if (ftestexist == NULL) + return 0; + fclose(ftestexist); + return 1; +} + +int makedir(const char *newdir) +{ + char *buffer = NULL; + char *p = NULL; + int len = (int)strlen(newdir); + + if (len <= 0) + return 0; + + buffer = (char*)malloc(len+1); + if (buffer == NULL) + { + printf("Error allocating memory\n"); + return UNZ_INTERNALERROR; + } + + strcpy(buffer, newdir); + + if (buffer[len-1] == '/') + buffer[len-1] = 0; + + if (MKDIR(buffer) == 0) + { + free(buffer); + return 1; + } + + p = buffer + 1; + while (1) + { + char hold; + while(*p && *p != '\\' && *p != '/') + p++; + hold = *p; + *p = 0; + + if ((MKDIR(buffer) == -1) && (errno == ENOENT)) + { + printf("couldn't create directory %s (%d)\n", buffer, errno); + free(buffer); + return 0; + } + + if (hold == 0) + break; + + *p++ = hold; + } + + free(buffer); + return 1; +} + +void display_zpos64(ZPOS64_T n, int size_char) +{ + /* To avoid compatibility problem we do here the conversion */ + char number[21] = {0}; + int offset = 19; + int pos_string = 19; + int size_display_string = 19; + + while (1) + { + number[offset] = (char)((n%10) + '0'); + if (number[offset] != '0') + pos_string = offset; + n /= 10; + if (offset == 0) + break; + offset--; + } + + size_display_string -= pos_string; + while (size_char-- > size_display_string) + printf(" "); + printf("%s",&number[pos_string]); +} + +void do_banner() +{ + printf("MiniUnz 1.01b, demo of zLib + Unz package written by Gilles Vollant\n"); + printf("more info at http://www.winimage.com/zLibDll/minizip.html\n\n"); +} + +void do_help() +{ + printf("Usage : miniunz [-e] [-x] [-v] [-l] [-o] [-p password] file.zip [file_to_extr.] [-d extractdir]\n\n" \ + " -e Extract without pathname (junk paths)\n" \ + " -x Extract with pathname\n" \ + " -v list files\n" \ + " -l list files\n" \ + " -d directory to extract into\n" \ + " -o overwrite files without prompting\n" \ + " -p extract crypted file using password\n\n"); +} + +int do_list(unzFile uf) +{ + int err = unzGoToFirstFile(uf); + if (err != UNZ_OK) + { + printf("error %d with zipfile in unzGoToFirstFile\n", err); + return 1; + } + + printf(" Length Method Size Ratio Date Time CRC-32 Name\n"); + printf(" ------ ------ ---- ----- ---- ---- ------ ----\n"); + + do + { + char filename_inzip[256] = {0}; + unz_file_info64 file_info = {0}; + uLong ratio = 0; + const char *string_method = NULL; + char charCrypt = ' '; + + err = unzGetCurrentFileInfo64(uf, &file_info, filename_inzip, sizeof(filename_inzip), NULL, 0, NULL, 0); + if (err != UNZ_OK) + { + printf("error %d with zipfile in unzGetCurrentFileInfo\n", err); + break; + } + + if (file_info.uncompressed_size > 0) + ratio = (uLong)((file_info.compressed_size*100) / file_info.uncompressed_size); + + /* Display a '*' if the file is encrypted */ + if ((file_info.flag & 1) != 0) + charCrypt = '*'; + + if (file_info.compression_method == 0) + string_method = "Stored"; + else if (file_info.compression_method == Z_DEFLATED) + { + uInt iLevel = (uInt)((file_info.flag & 0x6) / 2); + if (iLevel == 0) + string_method = "Defl:N"; + else if (iLevel == 1) + string_method = "Defl:X"; + else if ((iLevel == 2) || (iLevel == 3)) + string_method = "Defl:F"; /* 2:fast , 3 : extra fast*/ + } + else if (file_info.compression_method == Z_BZIP2ED) + { + string_method = "BZip2 "; + } + else + string_method = "Unkn. "; + + display_zpos64(file_info.uncompressed_size, 7); + printf(" %6s%c", string_method, charCrypt); + display_zpos64(file_info.compressed_size, 7); + printf(" %3lu%% %2.2lu-%2.2lu-%2.2lu %2.2lu:%2.2lu %8.8lx %s\n", ratio, + (uLong)file_info.tmu_date.tm_mon + 1, (uLong)file_info.tmu_date.tm_mday, + (uLong)file_info.tmu_date.tm_year % 100, + (uLong)file_info.tmu_date.tm_hour, (uLong)file_info.tmu_date.tm_min, + (uLong)file_info.crc, filename_inzip); + + err = unzGoToNextFile(uf); + } + while (err == UNZ_OK); + + if (err != UNZ_END_OF_LIST_OF_FILE && err != UNZ_OK) { + printf("error %d with zipfile in unzGoToNextFile\n", err); + return err; + } + + return 0; +} + +int do_extract_currentfile(unzFile uf, int opt_extract_without_path, int* popt_overwrite, const char *password) +{ + unz_file_info64 file_info = {0}; + FILE* fout = NULL; + void* buf = NULL; + uInt size_buf = WRITEBUFFERSIZE; + int err = UNZ_OK; + int errclose = UNZ_OK; + int skip = 0; + char filename_inzip[256] = {0}; + char* filename_withoutpath = NULL; + const char* write_filename = NULL; + char* p = NULL; + + err = unzGetCurrentFileInfo64(uf, &file_info, filename_inzip, sizeof(filename_inzip), NULL, 0, NULL, 0); + if (err != UNZ_OK) + { + printf("error %d with zipfile in unzGetCurrentFileInfo\n",err); + return err; + } + + p = filename_withoutpath = filename_inzip; + while (*p != 0) + { + if ((*p == '/') || (*p == '\\')) + filename_withoutpath = p+1; + p++; + } + + /* If zip entry is a directory then create it on disk */ + if (*filename_withoutpath == 0) + { + if (opt_extract_without_path == 0) + { + printf("creating directory: %s\n", filename_inzip); + MKDIR(filename_inzip); + } + return err; + } + + buf = (void*)malloc(size_buf); + if (buf == NULL) + { + printf("Error allocating memory\n"); + return UNZ_INTERNALERROR; + } + + err = unzOpenCurrentFilePassword(uf, password); + if (err != UNZ_OK) + printf("error %d with zipfile in unzOpenCurrentFilePassword\n", err); + + if (opt_extract_without_path) + write_filename = filename_withoutpath; + else + write_filename = filename_inzip; + + /* Determine if the file should be overwritten or not and ask the user if needed */ + if ((err == UNZ_OK) && (*popt_overwrite == 0) && (check_file_exists(write_filename))) + { + char rep = 0; + do + { + char answer[128]; + printf("The file %s exists. Overwrite ? [y]es, [n]o, [A]ll: ", write_filename); + if (scanf("%1s", answer) != 1) + exit(EXIT_FAILURE); + rep = answer[0]; + if ((rep >= 'a') && (rep <= 'z')) + rep -= 0x20; + } + while ((rep != 'Y') && (rep != 'N') && (rep != 'A')); + + if (rep == 'N') + skip = 1; + if (rep == 'A') + *popt_overwrite = 1; + } + + /* Create the file on disk so we can unzip to it */ + if ((skip == 0) && (err == UNZ_OK)) + { + fout = FOPEN_FUNC(write_filename, "wb"); + /* Some zips don't contain directory alone before file */ + if ((fout == NULL) && (opt_extract_without_path == 0) && + (filename_withoutpath != (char*)filename_inzip)) + { + char c = *(filename_withoutpath-1); + *(filename_withoutpath-1) = 0; + makedir(write_filename); + *(filename_withoutpath-1) = c; + fout = FOPEN_FUNC(write_filename, "wb"); + } + if (fout == NULL) + printf("error opening %s\n", write_filename); + } + + /* Read from the zip, unzip to buffer, and write to disk */ + if (fout != NULL) + { + printf(" extracting: %s\n", write_filename); + + do + { + err = unzReadCurrentFile(uf, buf, size_buf); + if (err < 0) + { + printf("error %d with zipfile in unzReadCurrentFile\n", err); + break; + } + if (err == 0) + break; + if (fwrite(buf, err, 1, fout) != 1) + { + printf("error %d in writing extracted file\n", errno); + err = UNZ_ERRNO; + break; + } + } + while (err > 0); + + if (fout) + fclose(fout); + + /* Set the time of the file that has been unzipped */ + if (err == 0) + change_file_date(write_filename,file_info.dosDate, file_info.tmu_date); + } + + errclose = unzCloseCurrentFile(uf); + if (errclose != UNZ_OK) + printf("error %d with zipfile in unzCloseCurrentFile\n", errclose); + + free(buf); + return err; +} + +int do_extract_all(unzFile uf, int opt_extract_without_path, int opt_overwrite, const char *password) +{ + int err = unzGoToFirstFile(uf); + if (err != UNZ_OK) + { + printf("error %d with zipfile in unzGoToFirstFile\n", err); + return 1; + } + + do + { + err = do_extract_currentfile(uf, opt_extract_without_path, &opt_overwrite, password); + if (err != UNZ_OK) + break; + err = unzGoToNextFile(uf); + } + while (err == UNZ_OK); + + if (err != UNZ_END_OF_LIST_OF_FILE) + { + printf("error %d with zipfile in unzGoToNextFile\n", err); + return 1; + } + return 0; +} + +int do_extract_onefile(unzFile uf, const char* filename, int opt_extract_without_path, int opt_overwrite, + const char* password) +{ + if (unzLocateFile(uf, filename, NULL) != UNZ_OK) + { + printf("file %s not found in the zipfile\n", filename); + return 2; + } + if (do_extract_currentfile(uf, opt_extract_without_path, &opt_overwrite, password) == UNZ_OK) + return 0; + return 1; +} + +int main(int argc, const char *argv[]) +{ + const char *zipfilename = NULL; + const char *filename_to_extract = NULL; + const char *password = NULL; + int i = 0; + int ret = 0; + int opt_do_list = 0; + int opt_do_extract = 1; + int opt_do_extract_withoutpath = 0; + int opt_overwrite = 0; + int opt_extractdir = 0; + const char *dirname = NULL; + unzFile uf = NULL; + + do_banner(); + if (argc == 1) + { + do_help(); + return 0; + } + + /* Parse command line options */ + for (i = 1; i < argc; i++) + { + if ((*argv[i]) == '-') + { + const char *p = argv[i]+1; + + while (*p != 0) + { + char c = *(p++); + if ((c == 'l') || (c == 'L')) + opt_do_list = 1; + if ((c == 'v') || (c == 'V')) + opt_do_list = 1; + if ((c == 'x') || (c == 'X')) + opt_do_extract = 1; + if ((c == 'e') || (c == 'E')) + opt_do_extract = opt_do_extract_withoutpath = 1; + if ((c == 'o') || (c == 'O')) + opt_overwrite=1; + if ((c == 'd') || (c == 'D')) + { + opt_extractdir = 1; + dirname = argv[i+1]; + } + + if (((c == 'p') || (c == 'P')) && (i+1 < argc)) + { + password = argv[i+1]; + i++; + } + } + } + else + { + if (zipfilename == NULL) + zipfilename = argv[i]; + else if ((filename_to_extract == NULL) && (!opt_extractdir)) + filename_to_extract = argv[i]; + } + } + + /* Open zip file */ + if (zipfilename != NULL) + { +#ifdef USEWIN32IOAPI + zlib_filefunc64_def ffunc; + fill_win32_filefunc64A(&ffunc); + uf = unzOpen2_64(zipfilename, &ffunc); +#else + uf = unzOpen64(zipfilename); +#endif + } + + if (uf == NULL) + { + printf("Cannot open %s\n", zipfilename); + return 1; + } + + printf("%s opened\n", zipfilename); + + /* Process command line options */ + if (opt_do_list == 1) + { + ret = do_list(uf); + } + else if (opt_do_extract == 1) + { + if (opt_extractdir && CHDIR(dirname)) + { + printf("Error changing into %s, aborting\n", dirname); + exit(-1); + } + + if (filename_to_extract == NULL) + ret = do_extract_all(uf, opt_do_extract_withoutpath, opt_overwrite, password); + else + ret = do_extract_onefile(uf, filename_to_extract, opt_do_extract_withoutpath, opt_overwrite, password); + } + + unzClose(uf); + return ret; +} diff --git a/Zip/minizip/miniunz.vcproj b/Zip/minizip/miniunz.vcproj new file mode 100755 index 0000000000000000000000000000000000000000..fd52a1811e7877573715a3161a336230ff207b78 --- /dev/null +++ b/Zip/minizip/miniunz.vcproj @@ -0,0 +1,394 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/Zip/minizip/minizip.c b/Zip/minizip/minizip.c new file mode 100755 index 0000000000000000000000000000000000000000..0f8bb2a30c8e1fab1df6cd4ddac9317d551b8357 --- /dev/null +++ b/Zip/minizip/minizip.c @@ -0,0 +1,439 @@ +/* minizip.c + Version 1.1, February 14h, 2010 + sample part of the MiniZip project + + Copyright (C) 1998-2010 Gilles Vollant + http://www.winimage.com/zLibDll/minizip.html + Modifications of Unzip for Zip64 + Copyright (C) 2007-2008 Even Rouault + Modifications for Zip64 support + Copyright (C) 2009-2010 Mathias Svensson + http://result42.com + + This program is distributed under the terms of the same license as zlib. + See the accompanying LICENSE file for the full text of the license. +*/ + +#if (!defined(_WIN32)) && (!defined(WIN32)) && (!defined(__APPLE__)) +# ifndef __USE_FILE_OFFSET64 +# define __USE_FILE_OFFSET64 +# endif +# ifndef __USE_LARGEFILE64 +# define __USE_LARGEFILE64 +# endif +# ifndef _LARGEFILE64_SOURCE +# define _LARGEFILE64_SOURCE +# endif +# ifndef _FILE_OFFSET_BIT +# define _FILE_OFFSET_BIT 64 +# endif +#endif + +#ifdef __APPLE__ +/* In darwin and perhaps other BSD variants off_t is a 64 bit value, hence no need for specific 64 bit functions */ +# define FOPEN_FUNC(filename, mode) fopen(filename, mode) +# define FTELLO_FUNC(stream) ftello(stream) +# define FSEEKO_FUNC(stream, offset, origin) fseeko(stream, offset, origin) +#else +# define FOPEN_FUNC(filename, mode) fopen64(filename, mode) +# define FTELLO_FUNC(stream) ftello64(stream) +# define FSEEKO_FUNC(stream, offset, origin) fseeko64(stream, offset, origin) +#endif + +#include +#include +#include +#include +#include +#include + +#ifdef _WIN32 +# include +# include +#else +# include +# include +# include +# include +#endif + +#include "zip.h" + +#ifdef _WIN32 +# define USEWIN32IOAPI +# include "iowin32.h" +#endif + +#define WRITEBUFFERSIZE (16384) +#define MAXFILENAME (256) + +uLong filetime(const char *filename, tm_zip *tmzip, uLong *dostime) +{ + int ret = 0; +#ifdef _WIN32 + FILETIME ftLocal; + HANDLE hFind; + WIN32_FIND_DATAA ff32; + + hFind = FindFirstFileA(filename, &ff32); + if (hFind != INVALID_HANDLE_VALUE) + { + FileTimeToLocalFileTime(&(ff32.ftLastWriteTime), &ftLocal); + FileTimeToDosDateTime(&ftLocal,((LPWORD)dostime)+1,((LPWORD)dostime)+0); + FindClose(hFind); + ret = 1; + } +#else +#if defined unix || defined __APPLE__ + struct stat s = {0}; + struct tm* filedate; + time_t tm_t = 0; + + if (strcmp(filename,"-") != 0) + { + char name[MAXFILENAME+1]; + int len = strlen(filename); + if (len > MAXFILENAME) + len = MAXFILENAME; + + strncpy(name, filename, MAXFILENAME - 1); + name[MAXFILENAME] = 0; + + if (name[len - 1] == '/') + name[len - 1] = 0; + + /* not all systems allow stat'ing a file with / appended */ + if (stat(name,&s) == 0) + { + tm_t = s.st_mtime; + ret = 1; + } + } + + filedate = localtime(&tm_t); + + tmzip->tm_sec = filedate->tm_sec; + tmzip->tm_min = filedate->tm_min; + tmzip->tm_hour = filedate->tm_hour; + tmzip->tm_mday = filedate->tm_mday; + tmzip->tm_mon = filedate->tm_mon ; + tmzip->tm_year = filedate->tm_year; +#endif +#endif + return ret; +} + +int check_file_exists(const char* filename) +{ + FILE* ftestexist = FOPEN_FUNC(filename, "rb"); + if (ftestexist == NULL) + return 0; + fclose(ftestexist); + return 1; +} + +int is_large_file(const char* filename) +{ + ZPOS64_T pos = 0; + FILE* pFile = FOPEN_FUNC(filename, "rb"); + + if (pFile == NULL) + return 0; + + FSEEKO_FUNC(pFile, 0, SEEK_END); + pos = FTELLO_FUNC(pFile); + fclose(pFile); + + printf("File : %s is %lld bytes\n", filename, pos); + + return (pos >= 0xffffffff); +} + +/* Calculate the CRC32 of a file, because to encrypt a file, we need known the CRC32 of the file before */ +int get_file_crc(const char* filenameinzip, void *buf, unsigned long size_buf, unsigned long* result_crc) +{ + FILE *fin = NULL; + unsigned long calculate_crc = 0; + unsigned long size_read = 0; + int err = ZIP_OK; + + fin = FOPEN_FUNC(filenameinzip,"rb"); + if (fin == NULL) + err = ZIP_ERRNO; + else + { + do + { + size_read = (int)fread(buf,1,size_buf,fin); + + if ((size_read < size_buf) && (feof(fin) == 0)) + { + printf("error in reading %s\n",filenameinzip); + err = ZIP_ERRNO; + } + + if (size_read > 0) + calculate_crc = crc32(calculate_crc,buf,size_read); + } + while ((err == ZIP_OK) && (size_read > 0)); + } + + if (fin) + fclose(fin); + + printf("file %s crc %lx\n", filenameinzip, calculate_crc); + *result_crc = calculate_crc; + return err; +} + +void do_banner() +{ + printf("MiniZip 1.1, demo of zLib + MiniZip64 package, written by Gilles Vollant\n"); + printf("more info on MiniZip at http://www.winimage.com/zLibDll/minizip.html\n\n"); +} + +void do_help() +{ + printf("Usage : minizip [-o] [-a] [-0 to -9] [-p password] [-j] file.zip [files_to_add]\n\n" \ + " -o Overwrite existing file.zip\n" \ + " -a Append to existing file.zip\n" \ + " -0 Store only\n" \ + " -1 Compress faster\n" \ + " -9 Compress better\n\n" \ + " -j exclude path. store only the file name.\n\n"); +} + +int main(int argc, char *argv[]) +{ + zipFile zf = NULL; +#ifdef USEWIN32IOAPI + zlib_filefunc64_def ffunc = {0}; +#endif + char *zipfilename = NULL; + const char* password = NULL; + void* buf = NULL; + int size_buf = WRITEBUFFERSIZE; + int zipfilenamearg = 0; + int errclose = 0; + int err = 0; + int i = 0; + int opt_overwrite = APPEND_STATUS_CREATE; + int opt_compress_level = Z_DEFAULT_COMPRESSION; + int opt_exclude_path = 0; + + do_banner(); + if (argc == 1) + { + do_help(); + return 0; + } + + /* Parse command line options */ + for (i = 1; i < argc; i++) + { + if ((*argv[i]) == '-') + { + const char *p = argv[i]+1; + + while ((*p) != '\0') + { + char c = *(p++);; + if ((c == 'o') || (c == 'O')) + opt_overwrite = APPEND_STATUS_CREATEAFTER; + if ((c == 'a') || (c == 'A')) + opt_overwrite = APPEND_STATUS_ADDINZIP; + if ((c >= '0') && (c <= '9')) + opt_compress_level = (c - '0'); + if ((c == 'j') || (c == 'J')) + opt_exclude_path = 1; + + if (((c == 'p') || (c == 'P')) && (i+1 < argc)) + { + password=argv[i+1]; + i++; + } + } + } + else + { + if (zipfilenamearg == 0) + zipfilenamearg = i; + } + } + + if (zipfilenamearg == 0) + { + do_help(); + return 0; + } + zipfilename = argv[zipfilenamearg]; + + buf = (void*)malloc(size_buf); + if (buf == NULL) + { + printf("Error allocating memory\n"); + return ZIP_INTERNALERROR; + } + + if (opt_overwrite == 2) + { + /* If the file don't exist, we not append file */ + if (check_file_exists(zipfilename) == 0) + opt_overwrite = 1; + } + else if (opt_overwrite == 0) + { + /* If ask the user what to do because append and overwrite args not set */ + if (check_file_exists(zipfilename) != 0) + { + char rep = 0; + do + { + char answer[128]; + printf("The file %s exists. Overwrite ? [y]es, [n]o, [a]ppend : ", zipfilename); + if (scanf("%1s", answer) != 1) + exit(EXIT_FAILURE); + rep = answer[0]; + + if ((rep >= 'a') && (rep <= 'z')) + rep -= 0x20; + } + while ((rep != 'Y') && (rep != 'N') && (rep != 'A')); + + if (rep == 'A') + opt_overwrite = 2; + else if (rep == 'N') + { + do_help(); + free(buf); + return 0; + } + } + } + +#ifdef USEWIN32IOAPI + fill_win32_filefunc64A(&ffunc); + zf = zipOpen2_64(zipfilename, opt_overwrite, NULL, &ffunc); +#else + zf = zipOpen64(zipfilename, opt_overwrite); +#endif + + if (zf == NULL) + { + printf("error opening %s\n", zipfilename); + err = ZIP_ERRNO; + } + else + printf("creating %s\n", zipfilename); + + /* Go through command line args looking for files to add to zip */ + for (i = zipfilenamearg + 1; (i < argc) && (err == ZIP_OK); i++) + { + FILE *fin = NULL; + int size_read = 0; + const char* filenameinzip = argv[i]; + const char *savefilenameinzip; + zip_fileinfo zi = {0}; + unsigned long crcFile = 0; + int zip64 = 0; + + /* Skip command line options */ + if ((((*(argv[i])) == '-') || ((*(argv[i])) == '/')) && (strlen(argv[i]) == 2) && + ((argv[i][1] == 'o') || (argv[i][1] == 'O') || (argv[i][1] == 'a') || (argv[i][1] == 'A') || + (argv[i][1] == 'p') || (argv[i][1] == 'P') || ((argv[i][1] >= '0') && (argv[i][1] <= '9')))) + continue; + + /* Get information about the file on disk so we can store it in zip */ + filetime(filenameinzip, &zi.tmz_date, &zi.dosDate); + + if ((password != NULL) && (err == ZIP_OK)) + err = get_file_crc(filenameinzip, buf, size_buf, &crcFile); + + zip64 = is_large_file(filenameinzip); + + /* Construct the filename that our file will be stored in the zip as. + The path name saved, should not include a leading slash. + If it did, windows/xp and dynazip couldn't read the zip file. */ + + savefilenameinzip = filenameinzip; + while (savefilenameinzip[0] == '\\' || savefilenameinzip[0] == '/') + savefilenameinzip++; + + /* Should the file be stored with any path info at all? */ + if (opt_exclude_path) + { + const char *tmpptr = NULL; + const char *lastslash = 0; + + for (tmpptr = savefilenameinzip; *tmpptr; tmpptr++) + { + if (*tmpptr == '\\' || *tmpptr == '/') + lastslash = tmpptr; + } + + if (lastslash != NULL) + savefilenameinzip = lastslash + 1; /* base filename follows last slash. */ + } + + /* Add to zip file */ + err = zipOpenNewFileInZip3_64(zf, savefilenameinzip, &zi, + NULL, 0, NULL, 0, NULL /* comment*/, + (opt_compress_level != 0) ? Z_DEFLATED : 0, + opt_compress_level,0, + -MAX_WBITS, DEF_MEM_LEVEL, Z_DEFAULT_STRATEGY, + password, crcFile, zip64); + + if (err != ZIP_OK) + printf("error in opening %s in zipfile (%d)\n", filenameinzip, err); + else + { + fin = FOPEN_FUNC(filenameinzip, "rb"); + if (fin == NULL) + { + err = ZIP_ERRNO; + printf("error in opening %s for reading\n", filenameinzip); + } + } + + if (err == ZIP_OK) + { + /* Read contents of file and write it to zip */ + do + { + size_read = (int)fread(buf, 1, size_buf, fin); + if ((size_read < size_buf) && (feof(fin) == 0)) + { + printf("error in reading %s\n",filenameinzip); + err = ZIP_ERRNO; + } + + if (size_read > 0) + { + err = zipWriteInFileInZip(zf, buf, size_read); + if (err < 0) + printf("error in writing %s in the zipfile (%d)\n", filenameinzip, err); + } + } + while ((err == ZIP_OK) && (size_read > 0)); + } + + if (fin) + fclose(fin); + + if (err < 0) + err = ZIP_ERRNO; + else + { + err = zipCloseFileInZip(zf); + if (err != ZIP_OK) + printf("error in closing %s in the zipfile (%d)\n", filenameinzip, err); + } + } + + errclose = zipClose(zf, NULL); + if (errclose != ZIP_OK) + printf("error in closing %s (%d)\n", zipfilename, errclose); + + free(buf); + return err; +} diff --git a/Zip/minizip/minizip.pc.in b/Zip/minizip/minizip.pc.in new file mode 100755 index 0000000000000000000000000000000000000000..69b5b7fdcb3b4ae67ac7f9ee23733a59dac7e665 --- /dev/null +++ b/Zip/minizip/minizip.pc.in @@ -0,0 +1,12 @@ +prefix=@prefix@ +exec_prefix=@exec_prefix@ +libdir=@libdir@ +includedir=@includedir@/minizip + +Name: minizip +Description: Minizip zip file manipulation library +Requires: +Version: @PACKAGE_VERSION@ +Libs: -L${libdir} -lminizip +Libs.private: -lz +Cflags: -I${includedir} diff --git a/Zip/minizip/minizip.sln b/Zip/minizip/minizip.sln new file mode 100755 index 0000000000000000000000000000000000000000..98f7ac62e18a8bebb459b687dce948cbea246ad5 --- /dev/null +++ b/Zip/minizip/minizip.sln @@ -0,0 +1,26 @@ + +Microsoft Visual Studio Solution File, Format Version 10.00 +# Visual Studio 2008 +Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "miniunz", "miniunz.vcproj", "{8D770B0A-A853-4FB7-8E71-5BDDC16A8C11}" +EndProject +Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "minizip", "minizip.vcproj", "{9C3AFF9E-022F-4A42-BCDC-C705AEE00DEB}" +EndProject +Global + GlobalSection(SolutionConfigurationPlatforms) = preSolution + Debug|Win32 = Debug|Win32 + Release|Win32 = Release|Win32 + EndGlobalSection + GlobalSection(ProjectConfigurationPlatforms) = postSolution + {8D770B0A-A853-4FB7-8E71-5BDDC16A8C11}.Debug|Win32.ActiveCfg = Debug|Win32 + {8D770B0A-A853-4FB7-8E71-5BDDC16A8C11}.Debug|Win32.Build.0 = Debug|Win32 + {8D770B0A-A853-4FB7-8E71-5BDDC16A8C11}.Release|Win32.ActiveCfg = Release|Win32 + {8D770B0A-A853-4FB7-8E71-5BDDC16A8C11}.Release|Win32.Build.0 = Release|Win32 + {9C3AFF9E-022F-4A42-BCDC-C705AEE00DEB}.Debug|Win32.ActiveCfg = Debug|Win32 + {9C3AFF9E-022F-4A42-BCDC-C705AEE00DEB}.Debug|Win32.Build.0 = Debug|Win32 + {9C3AFF9E-022F-4A42-BCDC-C705AEE00DEB}.Release|Win32.ActiveCfg = Release|Win32 + {9C3AFF9E-022F-4A42-BCDC-C705AEE00DEB}.Release|Win32.Build.0 = Release|Win32 + EndGlobalSection + GlobalSection(SolutionProperties) = preSolution + HideSolutionNode = FALSE + EndGlobalSection +EndGlobal diff --git a/Zip/minizip/minizip.vcproj b/Zip/minizip/minizip.vcproj new file mode 100755 index 0000000000000000000000000000000000000000..75790e1ffbc0574ad4cc671e013c8ed01c68333d --- /dev/null +++ b/Zip/minizip/minizip.vcproj @@ -0,0 +1,394 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/Zip/minizip/unzip.c b/Zip/minizip/unzip.c index 09d13d17804f69fc6ebcac581f8bab195becd604..a3b98346f8e7f36afb3fd2d8111d78c23f41536e 100755 --- a/Zip/minizip/unzip.c +++ b/Zip/minizip/unzip.c @@ -14,29 +14,26 @@ This program is distributed under the terms of the same license as zlib. See the accompanying LICENSE file for the full text of the license. - */ - +*/ #include #include #include /*#ifndef NOUNCRYPT - # define NOUNCRYPT - #endif*/ +# define NOUNCRYPT +#endif*/ #include "zlib.h" #include "unzip.h" -#include "Common.h" - #ifdef STDC # include # include # include #endif #ifdef NO_ERRNO_H -extern int errno; + extern int errno; #else # include #endif @@ -49,8 +46,8 @@ extern int errno; # define AES_HEADERSIZE (11) # define AES_KEYSIZE(mode) (64 + (mode * 64)) -# include "aes.h" -# include "fileenc.h" +# include "aes/aes.h" +# include "aes/fileenc.h" #endif #ifndef NOUNCRYPT # include "crypt.h" @@ -87,14 +84,15 @@ extern int errno; # define ALLOC(size) (malloc(size)) #endif #ifndef TRYFREE -# define TRYFREE(p) {if (p) free(p); } +# define TRYFREE(p) {if (p) free(p);} #endif const char unz_copyright[] = - " unzip 1.01 Copyright 1998-2004 Gilles Vollant - http://www.winimage.com/zLibDll"; + " unzip 1.01 Copyright 1998-2004 Gilles Vollant - http://www.winimage.com/zLibDll"; /* unz_file_info_interntal contain internal info about a file in zipfile*/ -typedef struct unz_file_info64_internal_s { +typedef struct unz_file_info64_internal_s +{ ZPOS64_T offset_curfile; /* relative offset of local header 8 bytes */ ZPOS64_T byte_before_the_zipfile; /* byte before the zipfile, (>0 for sfx) */ #ifdef HAVE_AES @@ -105,7 +103,8 @@ typedef struct unz_file_info64_internal_s { } unz_file_info64_internal; /* file_in_zip_read_info_s contain internal information about a file in zipfile */ -typedef struct { +typedef struct +{ Bytef *read_buffer; /* internal buffer for compressed data */ z_stream stream; /* zLib stream structure for inflate */ @@ -138,7 +137,8 @@ typedef struct { } file_in_zip64_read_info_s; /* unz64_s contain internal information about the zipfile */ -typedef struct { +typedef struct +{ zlib_filefunc64_32_def z_filefunc; voidpf filestream; /* io structure of the current zipfile */ voidpf filestream_with_CD; /* io structure of the disk with the central directory */ @@ -155,27 +155,27 @@ typedef struct { unz_file_info64 cur_file_info; /* public info about the current file in zip*/ unz_file_info64_internal cur_file_info_internal; - /* private info about it*/ - file_in_zip64_read_info_s *pfile_in_zip_read; - /* structure about the current file if we are decompressing it */ + /* private info about it*/ + file_in_zip64_read_info_s* pfile_in_zip_read; + /* structure about the current file if we are decompressing it */ int isZip64; /* is the current file zip64 */ #ifndef NOUNCRYPT unsigned long keys[3]; /* keys defining the pseudo-random sequence */ - const unsigned long *pcrc_32_tab; + const unsigned long* pcrc_32_tab; #endif } unz64_s; /* Translate date/time from Dos format to tm_unz (readable more easily) */ -local void unz64local_DosDateToTmuDate(ZPOS64_T ulDosDate, tm_unz *ptm) +local void unz64local_DosDateToTmuDate (ZPOS64_T ulDosDate, tm_unz* ptm) { - ZPOS64_T uDate = (ZPOS64_T)(ulDosDate >> 16); + ZPOS64_T uDate = (ZPOS64_T)(ulDosDate>>16); - ptm->tm_mday = (uInt)(uDate & 0x1f); - ptm->tm_mon = (uInt)((((uDate) & 0x1E0) / 0x20) - 1); - ptm->tm_year = (uInt)(((uDate & 0x0FE00) / 0x0200) + 1980); - ptm->tm_hour = (uInt)((ulDosDate & 0xF800) / 0x800); - ptm->tm_min = (uInt)((ulDosDate & 0x7E0) / 0x20); - ptm->tm_sec = (uInt)(2 * (ulDosDate & 0x1f)); + ptm->tm_mday = (uInt)(uDate&0x1f); + ptm->tm_mon = (uInt)((((uDate)&0x1E0)/0x20)-1); + ptm->tm_year = (uInt)(((uDate&0x0FE00)/0x0200)+1980); + ptm->tm_hour = (uInt)((ulDosDate &0xF800)/0x800); + ptm->tm_min = (uInt)((ulDosDate&0x7E0)/0x20); + ptm->tm_sec = (uInt)(2*(ulDosDate&0x1f)); #define unz64local_in_range(min, max, value) ((min) <= (value) && (value) <= (max)) if (!unz64local_in_range(0, 11, ptm->tm_mon) || @@ -183,27 +183,30 @@ local void unz64local_DosDateToTmuDate(ZPOS64_T ulDosDate, tm_unz *ptm) !unz64local_in_range(0, 23, ptm->tm_hour) || !unz64local_in_range(0, 59, ptm->tm_min) || !unz64local_in_range(0, 59, ptm->tm_sec)) - /* Invalid date stored, so don't return it. */ - memset(ptm, 0, sizeof(tm_unz)); + /* Invalid date stored, so don't return it. */ + memset(ptm, 0, sizeof(tm_unz)); #undef unz64local_in_range } /* Read a byte from a gz_stream; Return EOF for end of file. */ -local int unz64local_getByte(const zlib_filefunc64_32_def *pzlib_filefunc_def, voidpf filestream, int *pi) +local int unz64local_getByte OF((const zlib_filefunc64_32_def* pzlib_filefunc_def, voidpf filestream, int *pi)); +local int unz64local_getByte(const zlib_filefunc64_32_def* pzlib_filefunc_def, voidpf filestream, int *pi) { unsigned char c; int err = (int)ZREAD64(*pzlib_filefunc_def, filestream, &c, 1); - if (err == 1) { + if (err == 1) + { *pi = (int)c; return UNZ_OK; } + *pi = 0; if (ZERROR64(*pzlib_filefunc_def, filestream)) return UNZ_ERRNO; return UNZ_EOF; } -local int unz64local_getShort OF((const zlib_filefunc64_32_def * pzlib_filefunc_def, voidpf filestream, uLong * pX)); -local int unz64local_getShort(const zlib_filefunc64_32_def *pzlib_filefunc_def, voidpf filestream, uLong *pX) +local int unz64local_getShort OF((const zlib_filefunc64_32_def* pzlib_filefunc_def, voidpf filestream, uLong *pX)); +local int unz64local_getShort (const zlib_filefunc64_32_def* pzlib_filefunc_def, voidpf filestream, uLong *pX) { uLong x; int i = 0; @@ -213,7 +216,7 @@ local int unz64local_getShort(const zlib_filefunc64_32_def *pzlib_filefunc_def, x = (uLong)i; if (err == UNZ_OK) err = unz64local_getByte(pzlib_filefunc_def, filestream, &i); - x |= ((uLong)i) << 8; + x |= ((uLong)i)<<8; if (err == UNZ_OK) *pX = x; @@ -222,8 +225,8 @@ local int unz64local_getShort(const zlib_filefunc64_32_def *pzlib_filefunc_def, return err; } -local int unz64local_getLong OF((const zlib_filefunc64_32_def * pzlib_filefunc_def, voidpf filestream, uLong * pX)); -local int unz64local_getLong(const zlib_filefunc64_32_def *pzlib_filefunc_def, voidpf filestream, uLong *pX) +local int unz64local_getLong OF((const zlib_filefunc64_32_def* pzlib_filefunc_def, voidpf filestream, uLong *pX)); +local int unz64local_getLong (const zlib_filefunc64_32_def* pzlib_filefunc_def, voidpf filestream, uLong *pX) { uLong x; int i = 0; @@ -233,13 +236,13 @@ local int unz64local_getLong(const zlib_filefunc64_32_def *pzlib_filefunc_def, v x = (uLong)i; if (err == UNZ_OK) err = unz64local_getByte(pzlib_filefunc_def, filestream, &i); - x |= ((uLong)i) << 8; + x |= ((uLong)i)<<8; if (err == UNZ_OK) err = unz64local_getByte(pzlib_filefunc_def, filestream, &i); - x |= ((uLong)i) << 16; + x |= ((uLong)i)<<16; if (err == UNZ_OK) err = unz64local_getByte(pzlib_filefunc_def, filestream, &i); - x += ((uLong)i) << 24; + x += ((uLong)i)<<24; if (err == UNZ_OK) *pX = x; @@ -248,8 +251,8 @@ local int unz64local_getLong(const zlib_filefunc64_32_def *pzlib_filefunc_def, v return err; } -local int unz64local_getLong64 OF((const zlib_filefunc64_32_def * pzlib_filefunc_def, voidpf filestream, ZPOS64_T * pX)); -local int unz64local_getLong64(const zlib_filefunc64_32_def *pzlib_filefunc_def, voidpf filestream, ZPOS64_T *pX) +local int unz64local_getLong64 OF((const zlib_filefunc64_32_def* pzlib_filefunc_def, voidpf filestream, ZPOS64_T *pX)); +local int unz64local_getLong64 (const zlib_filefunc64_32_def* pzlib_filefunc_def, voidpf filestream, ZPOS64_T *pX) { ZPOS64_T x; int i = 0; @@ -259,25 +262,25 @@ local int unz64local_getLong64(const zlib_filefunc64_32_def *pzlib_filefunc_def, x = (ZPOS64_T)i; if (err == UNZ_OK) err = unz64local_getByte(pzlib_filefunc_def, filestream, &i); - x |= ((ZPOS64_T)i) << 8; + x |= ((ZPOS64_T)i)<<8; if (err == UNZ_OK) err = unz64local_getByte(pzlib_filefunc_def, filestream, &i); - x |= ((ZPOS64_T)i) << 16; + x |= ((ZPOS64_T)i)<<16; if (err == UNZ_OK) err = unz64local_getByte(pzlib_filefunc_def, filestream, &i); - x |= ((ZPOS64_T)i) << 24; + x |= ((ZPOS64_T)i)<<24; if (err == UNZ_OK) err = unz64local_getByte(pzlib_filefunc_def, filestream, &i); - x |= ((ZPOS64_T)i) << 32; + x |= ((ZPOS64_T)i)<<32; if (err == UNZ_OK) err = unz64local_getByte(pzlib_filefunc_def, filestream, &i); - x |= ((ZPOS64_T)i) << 40; + x |= ((ZPOS64_T)i)<<40; if (err == UNZ_OK) err = unz64local_getByte(pzlib_filefunc_def, filestream, &i); - x |= ((ZPOS64_T)i) << 48; + x |= ((ZPOS64_T)i)<<48; if (err == UNZ_OK) err = unz64local_getByte(pzlib_filefunc_def, filestream, &i); - x |= ((ZPOS64_T)i) << 56; + x |= ((ZPOS64_T)i)<<56; if (err == UNZ_OK) *pX = x; @@ -287,10 +290,10 @@ local int unz64local_getLong64(const zlib_filefunc64_32_def *pzlib_filefunc_def, } /* Locate the Central directory of a zip file (at the end, just before the global comment) */ -local ZPOS64_T unz64local_SearchCentralDir OF((const zlib_filefunc64_32_def * pzlib_filefunc_def, voidpf filestream)); -local ZPOS64_T unz64local_SearchCentralDir(const zlib_filefunc64_32_def *pzlib_filefunc_def, voidpf filestream) +local ZPOS64_T unz64local_SearchCentralDir OF((const zlib_filefunc64_32_def* pzlib_filefunc_def, voidpf filestream)); +local ZPOS64_T unz64local_SearchCentralDir(const zlib_filefunc64_32_def* pzlib_filefunc_def, voidpf filestream) { - unsigned char *buf; + unsigned char* buf; ZPOS64_T file_size; ZPOS64_T back_read = 4; ZPOS64_T max_back = 0xffff; /* maximum size of global comment */ @@ -299,11 +302,12 @@ local ZPOS64_T unz64local_SearchCentralDir(const zlib_filefunc64_32_def *pzlib_f ZPOS64_T read_pos; int i; - buf = (unsigned char *)ALLOC(BUFREADCOMMENT + 4); + buf = (unsigned char*)ALLOC(BUFREADCOMMENT + 4); if (buf == NULL) return 0; - if (ZSEEK64(*pzlib_filefunc_def, filestream, 0, ZLIB_FILEFUNC_SEEK_END) != 0) { + if (ZSEEK64(*pzlib_filefunc_def, filestream, 0, ZLIB_FILEFUNC_SEEK_END) != 0) + { TRYFREE(buf); return 0; } @@ -313,7 +317,8 @@ local ZPOS64_T unz64local_SearchCentralDir(const zlib_filefunc64_32_def *pzlib_f if (max_back > file_size) max_back = file_size; - while (back_read < max_back) { + while (back_read < max_back) + { if (back_read + BUFREADCOMMENT > max_back) back_read = max_back; else @@ -321,19 +326,20 @@ local ZPOS64_T unz64local_SearchCentralDir(const zlib_filefunc64_32_def *pzlib_f read_pos = file_size - back_read; read_size = ((BUFREADCOMMENT + 4) < (file_size - read_pos)) ? - (BUFREADCOMMENT + 4) : (uLong)(file_size - read_pos); + (BUFREADCOMMENT + 4) : (uLong)(file_size - read_pos); if (ZSEEK64(*pzlib_filefunc_def, filestream, read_pos, ZLIB_FILEFUNC_SEEK_SET) != 0) break; if (ZREAD64(*pzlib_filefunc_def, filestream, buf, read_size) != read_size) break; - for (i = (int)read_size - 3; (i--) > 0; ) - if (((*(buf + i)) == (ENDHEADERMAGIC & 0xff)) && - ((*(buf + i + 1)) == (ENDHEADERMAGIC >> 8 & 0xff)) && - ((*(buf + i + 2)) == (ENDHEADERMAGIC >> 16 & 0xff)) && - ((*(buf + i + 3)) == (ENDHEADERMAGIC >> 24 & 0xff))) { - pos_found = read_pos + i; + for (i = (int)read_size-3; (i--) > 0;) + if (((*(buf+i)) == (ENDHEADERMAGIC & 0xff)) && + ((*(buf+i+1)) == (ENDHEADERMAGIC >> 8 & 0xff)) && + ((*(buf+i+2)) == (ENDHEADERMAGIC >> 16 & 0xff)) && + ((*(buf+i+3)) == (ENDHEADERMAGIC >> 24 & 0xff))) + { + pos_found = read_pos+i; break; } @@ -345,10 +351,10 @@ local ZPOS64_T unz64local_SearchCentralDir(const zlib_filefunc64_32_def *pzlib_f } /* Locate the Central directory 64 of a zipfile (at the end, just before the global comment) */ -local ZPOS64_T unz64local_SearchCentralDir64 OF((const zlib_filefunc64_32_def * pzlib_filefunc_def, voidpf filestream, - const ZPOS64_T endcentraloffset)); -local ZPOS64_T unz64local_SearchCentralDir64(const zlib_filefunc64_32_def *pzlib_filefunc_def, voidpf filestream, - const ZPOS64_T endcentraloffset) +local ZPOS64_T unz64local_SearchCentralDir64 OF((const zlib_filefunc64_32_def* pzlib_filefunc_def, voidpf filestream, + const ZPOS64_T endcentraloffset)); +local ZPOS64_T unz64local_SearchCentralDir64(const zlib_filefunc64_32_def* pzlib_filefunc_def, voidpf filestream, + const ZPOS64_T endcentraloffset) { ZPOS64_T offset; uLong uL; @@ -374,7 +380,7 @@ local ZPOS64_T unz64local_SearchCentralDir64(const zlib_filefunc64_32_def *pzlib /* Goto end of central directory record */ if (ZSEEK64(*pzlib_filefunc_def, filestream, offset, ZLIB_FILEFUNC_SEEK_SET) != 0) return 0; - /* the signature */ + /* the signature */ if (unz64local_getLong(pzlib_filefunc_def, filestream, &uL) != UNZ_OK) return 0; if (uL != ZIP64ENDHEADERMAGIC) @@ -383,7 +389,7 @@ local ZPOS64_T unz64local_SearchCentralDir64(const zlib_filefunc64_32_def *pzlib return offset; } -local unzFile unzOpenInternal(const void *path, zlib_filefunc64_32_def *pzlib_filefunc64_32_def) +local unzFile unzOpenInternal(const void *path, zlib_filefunc64_32_def* pzlib_filefunc64_32_def) { unz64_s us; unz64_s *s; @@ -393,7 +399,7 @@ local unzFile unzOpenInternal(const void *path, zlib_filefunc64_32_def *pzlib_fi ZPOS64_T number_entry_CD; int err = UNZ_OK; - if (unz_copyright[0] != ' ') + if (unz_copyright[0]!=' ') return NULL; us.filestream = NULL; @@ -416,7 +422,8 @@ local unzFile unzOpenInternal(const void *path, zlib_filefunc64_32_def *pzlib_fi /* Use unz64local_SearchCentralDir first. Only based on the result is it necessary to locate the unz64local_SearchCentralDir64 */ central_pos = unz64local_SearchCentralDir(&us.z_filefunc, us.filestream); - if (central_pos) { + if (central_pos) + { if (ZSEEK64(us.z_filefunc, us.filestream, central_pos, ZLIB_FILEFUNC_SEEK_SET) != 0) err = UNZ_ERRNO; @@ -428,7 +435,7 @@ local unzFile unzOpenInternal(const void *path, zlib_filefunc64_32_def *pzlib_fi err = UNZ_ERRNO; us.number_disk = uL; /* number of the disk with the start of the central directory */ - if (unz64local_getShort(&us.z_filefunc, us.filestream, &uL) != UNZ_OK) + if (unz64local_getShort(&us.z_filefunc, us.filestream,& uL) != UNZ_OK) err = UNZ_ERRNO; us.gi.number_disk_with_CD = uL; /* total number of entries in the central directory on this disk */ @@ -454,10 +461,12 @@ local unzFile unzOpenInternal(const void *path, zlib_filefunc64_32_def *pzlib_fi err = UNZ_ERRNO; if ((err == UNZ_OK) && - ((us.gi.number_entry == 0xffff) || (us.size_central_dir == 0xffff) || (us.offset_central_dir == 0xffffffff))) { + ((us.gi.number_entry == 0xffff) || (us.size_central_dir == 0xffff) || (us.offset_central_dir == 0xffffffff))) + { /* Format should be Zip64, as the central directory or file size is too large */ central_pos = unz64local_SearchCentralDir64(&us.z_filefunc, us.filestream, central_pos); - if (central_pos) { + if (central_pos) + { ZPOS64_T uL64; us.isZip64 = 1; @@ -497,21 +506,25 @@ local unzFile unzOpenInternal(const void *path, zlib_filefunc64_32_def *pzlib_fi /* offset of start of central directory with respect to the starting disk number */ if (unz64local_getLong64(&us.z_filefunc, us.filestream, &us.offset_central_dir) != UNZ_OK) err = UNZ_ERRNO; - } else + } + else err = UNZ_BADZIPFILE; } - } else + } + else err = UNZ_ERRNO; if ((err == UNZ_OK) && (central_pos < us.offset_central_dir + us.size_central_dir)) err = UNZ_BADZIPFILE; - if (err != UNZ_OK) { + if (err != UNZ_OK) + { ZCLOSE64(us.z_filefunc, us.filestream); return NULL; } - if (us.gi.number_disk_with_CD == 0) { + if (us.gi.number_disk_with_CD == 0) + { /* If there is only one disk open another stream so we don't have to seek between the CD and the file headers constantly */ filestream = ZOPEN64(us.z_filefunc, path, ZLIB_FILEFUNC_MODE_READ | ZLIB_FILEFUNC_MODE_EXISTING); @@ -520,24 +533,26 @@ local unzFile unzOpenInternal(const void *path, zlib_filefunc64_32_def *pzlib_fi } /* Hack for zip files that have no respect for zip64 - if ((central_pos > 0xffffffff) && (us.offset_central_dir < 0xffffffff)) + if ((central_pos > 0xffffffff) && (us.offset_central_dir < 0xffffffff)) us.offset_central_dir = central_pos - us.size_central_dir;*/ us.byte_before_the_zipfile = central_pos - (us.offset_central_dir + us.size_central_dir); us.central_pos = central_pos; us.pfile_in_zip_read = NULL; - s = (unz64_s *)ALLOC(sizeof(unz64_s)); - if (s != NULL) { + s = (unz64_s*)ALLOC(sizeof(unz64_s)); + if (s != NULL) + { *s = us; unzGoToFirstFile((unzFile)s); } return (unzFile)s; } -extern unzFile ZEXPORT unzOpen2(const char *path, zlib_filefunc_def *pzlib_filefunc32_def) +extern unzFile ZEXPORT unzOpen2(const char *path, zlib_filefunc_def* pzlib_filefunc32_def) { - if (pzlib_filefunc32_def != NULL) { + if (pzlib_filefunc32_def != NULL) + { zlib_filefunc64_32_def zlib_filefunc64_32_def_fill; fill_zlib_filefunc64_32_def_from_filefunc32(&zlib_filefunc64_32_def_fill, pzlib_filefunc32_def); return unzOpenInternal(path, &zlib_filefunc64_32_def_fill); @@ -545,9 +560,10 @@ extern unzFile ZEXPORT unzOpen2(const char *path, zlib_filefunc_def *pzlib_filef return unzOpenInternal(path, NULL); } -extern unzFile ZEXPORT unzOpen2_64(const void *path, zlib_filefunc64_def *pzlib_filefunc_def) +extern unzFile ZEXPORT unzOpen2_64(const void *path, zlib_filefunc64_def* pzlib_filefunc_def) { - if (pzlib_filefunc_def != NULL) { + if (pzlib_filefunc_def != NULL) + { zlib_filefunc64_32_def zlib_filefunc64_32_def_fill; zlib_filefunc64_32_def_fill.zfile_func64 = *pzlib_filefunc_def; zlib_filefunc64_32_def_fill.ztell32_file = NULL; @@ -569,10 +585,10 @@ extern unzFile ZEXPORT unzOpen64(const void *path) extern int ZEXPORT unzClose(unzFile file) { - unz64_s *s; + unz64_s* s; if (file == NULL) return UNZ_PARAMERROR; - s = (unz64_s *)file; + s = (unz64_s*)file; if (s->pfile_in_zip_read != NULL) unzCloseCurrentFile(file); @@ -592,33 +608,35 @@ extern int ZEXPORT unzClose(unzFile file) local int unzGoToNextDisk OF((unzFile file)); local int unzGoToNextDisk(unzFile file) { - unz64_s *s; - file_in_zip64_read_info_s *pfile_in_zip_read_info; + unz64_s* s; uLong number_disk_next = 0; - s = (unz64_s *)file; + s = (unz64_s*)file; if (s == NULL) return UNZ_PARAMERROR; - pfile_in_zip_read_info = s->pfile_in_zip_read; number_disk_next = s->number_disk; - if ((pfile_in_zip_read_info != NULL) && (pfile_in_zip_read_info->rest_read_uncompressed > 0)) + if ((s->pfile_in_zip_read != NULL) && (s->pfile_in_zip_read->rest_read_uncompressed > 0)) /* We are currently reading a file and we need the next sequential disk */ number_disk_next += 1; else /* Goto the disk for the current file */ number_disk_next = s->cur_file_info.disk_num_start; - if (number_disk_next != s->number_disk) { + if (number_disk_next != s->number_disk) + { /* Switch disks */ if ((s->filestream != NULL) && (s->filestream != s->filestream_with_CD)) ZCLOSE64(s->z_filefunc, s->filestream); - if (number_disk_next == s->gi.number_disk_with_CD) { + if (number_disk_next == s->gi.number_disk_with_CD) + { s->filestream = s->filestream_with_CD; - } else { - s->filestream = ZOPENDISK64(s->z_filefunc, s->filestream_with_CD, (unsigned int)number_disk_next, - ZLIB_FILEFUNC_MODE_READ | ZLIB_FILEFUNC_MODE_EXISTING); + } + else + { + s->filestream = ZOPENDISK64(s->z_filefunc, s->filestream_with_CD, number_disk_next, + ZLIB_FILEFUNC_MODE_READ | ZLIB_FILEFUNC_MODE_EXISTING); } if (s->filestream == NULL) @@ -630,12 +648,12 @@ local int unzGoToNextDisk(unzFile file) return UNZ_OK; } -extern int ZEXPORT unzGetGlobalInfo(unzFile file, unz_global_info *pglobal_info32) +extern int ZEXPORT unzGetGlobalInfo(unzFile file, unz_global_info* pglobal_info32) { - unz64_s *s; + unz64_s* s; if (file == NULL) return UNZ_PARAMERROR; - s = (unz64_s *)file; + s = (unz64_s*)file; /* to do : check if number_entry is not truncated */ pglobal_info32->number_entry = (uLong)s->gi.number_entry; pglobal_info32->size_comment = s->gi.size_comment; @@ -643,23 +661,23 @@ extern int ZEXPORT unzGetGlobalInfo(unzFile file, unz_global_info *pglobal_info3 return UNZ_OK; } -extern int ZEXPORT unzGetGlobalInfo64(unzFile file, unz_global_info64 *pglobal_info) +extern int ZEXPORT unzGetGlobalInfo64(unzFile file, unz_global_info64* pglobal_info) { - unz64_s *s; + unz64_s* s; if (file == NULL) return UNZ_PARAMERROR; - s = (unz64_s *)file; + s = (unz64_s*)file; *pglobal_info = s->gi; return UNZ_OK; } extern int ZEXPORT unzGetGlobalComment(unzFile file, char *comment, uLong comment_size) { - unz64_s *s; + unz64_s* s; uLong bytes_to_read = comment_size; if (file == NULL) return (int)UNZ_PARAMERROR; - s = (unz64_s *)file; + s = (unz64_s*)file; if (bytes_to_read > s->gi.size_comment) bytes_to_read = s->gi.size_comment; @@ -667,23 +685,24 @@ extern int ZEXPORT unzGetGlobalComment(unzFile file, char *comment, uLong commen if (ZSEEK64(s->z_filefunc, s->filestream_with_CD, s->central_pos + 22, ZLIB_FILEFUNC_SEEK_SET) != 0) return UNZ_ERRNO; - if (bytes_to_read > 0) { + if (bytes_to_read>0) + { *comment = 0; if (ZREAD64(s->z_filefunc, s->filestream_with_CD, comment, bytes_to_read) != bytes_to_read) return UNZ_ERRNO; } if ((comment != NULL) && (comment_size > s->gi.size_comment)) - *(comment + s->gi.size_comment) = 0; + *(comment+s->gi.size_comment) = 0; return (int)bytes_to_read; } /* Get Info about the current file in the zipfile, with internal only info */ local int unz64local_GetCurrentFileInfoInternal(unzFile file, unz_file_info64 *pfile_info, - unz_file_info64_internal *pfile_info_internal, char *filename, uLong filename_size, void *extrafield, - uLong extrafield_size, char *comment, uLong comment_size) + unz_file_info64_internal *pfile_info_internal, char *filename, uLong filename_size, void *extrafield, + uLong extrafield_size, char *comment, uLong comment_size) { - unz64_s *s; + unz64_s* s; unz_file_info64 file_info; unz_file_info64_internal file_info_internal; ZPOS64_T bytes_to_read; @@ -697,14 +716,15 @@ local int unz64local_GetCurrentFileInfoInternal(unzFile file, unz_file_info64 *p if (file == NULL) return UNZ_PARAMERROR; - s = (unz64_s *)file; + s = (unz64_s*)file; if (ZSEEK64(s->z_filefunc, s->filestream_with_CD, - s->pos_in_central_dir + s->byte_before_the_zipfile, ZLIB_FILEFUNC_SEEK_SET) != 0) + s->pos_in_central_dir + s->byte_before_the_zipfile, ZLIB_FILEFUNC_SEEK_SET) != 0) err = UNZ_ERRNO; /* Check the magic */ - if (err == UNZ_OK) { + if (err == UNZ_OK) + { if (unz64local_getLong(&s->z_filefunc, s->filestream_with_CD, &uMagic) != UNZ_OK) err = UNZ_ERRNO; else if (uMagic != CENTRALHEADERMAGIC) @@ -758,29 +778,34 @@ local int unz64local_GetCurrentFileInfoInternal(unzFile file, unz_file_info64 *p lSeek += file_info.size_filename; - if ((err == UNZ_OK) && (filename != NULL)) { - if (file_info.size_filename < filename_size) { - *(filename + file_info.size_filename) = 0; + if ((err == UNZ_OK) && (filename != NULL)) + { + if (file_info.size_filename < filename_size) + { + *(filename+file_info.size_filename) = 0; bytes_to_read = file_info.size_filename; - } else + } + else bytes_to_read = filename_size; if ((file_info.size_filename > 0) && (filename_size > 0)) - if (ZREAD64(s->z_filefunc, s->filestream_with_CD, filename, (uLong)bytes_to_read) != bytes_to_read) + if (ZREAD64(s->z_filefunc, s->filestream_with_CD,filename, (uLong)bytes_to_read) != bytes_to_read) err = UNZ_ERRNO; lSeek -= (uLong)bytes_to_read; } /* Read extrafield */ - if ((err == UNZ_OK) && (extrafield != NULL)) { + if ((err == UNZ_OK) && (extrafield != NULL)) + { if (file_info.size_file_extra < extrafield_size) bytes_to_read = file_info.size_file_extra; else bytes_to_read = extrafield_size; - if (lSeek != 0) { + if (lSeek != 0) + { if (ZSEEK64(s->z_filefunc, s->filestream_with_CD, lSeek, ZLIB_FILEFUNC_SEEK_CUR) == 0) - lSeek = 0; + lSeek=0; else err = UNZ_ERRNO; } @@ -789,13 +814,16 @@ local int unz64local_GetCurrentFileInfoInternal(unzFile file, unz_file_info64 *p if (ZREAD64(s->z_filefunc, s->filestream_with_CD, extrafield, (uLong)bytes_to_read) != bytes_to_read) err = UNZ_ERRNO; lSeek += file_info.size_file_extra - (uLong)bytes_to_read; - } else + } + else lSeek += file_info.size_file_extra; - if ((err == UNZ_OK) && (file_info.size_file_extra != 0)) { - if (lSeek != 0) { + if ((err == UNZ_OK) && (file_info.size_file_extra != 0)) + { + if (lSeek != 0) + { if (ZSEEK64(s->z_filefunc, s->filestream_with_CD, lSeek, ZLIB_FILEFUNC_SEEK_CUR) == 0) - lSeek = 0; + lSeek=0; else err = UNZ_ERRNO; } @@ -808,7 +836,8 @@ local int unz64local_GetCurrentFileInfoInternal(unzFile file, unz_file_info64 *p if (ZSEEK64(s->z_filefunc, s->filestream_with_CD, current_pos, ZLIB_FILEFUNC_SEEK_SET) != 0) err = UNZ_ERRNO; - while ((err != UNZ_ERRNO) && (acc < file_info.size_file_extra)) { + while((err != UNZ_ERRNO) && (acc < file_info.size_file_extra)) + { uLong headerid; uLong datasize; @@ -818,26 +847,31 @@ local int unz64local_GetCurrentFileInfoInternal(unzFile file, unz_file_info64 *p err = UNZ_ERRNO; /* ZIP64 extra fields */ - if (headerid == 0x0001) { + if (headerid == 0x0001) + { /* Subtract size of ZIP64 field, since ZIP64 is handled internally */ file_info.size_file_extra_internal += 2 + 2 + datasize; - if (file_info.uncompressed_size == 0xffffffff) { + if (file_info.uncompressed_size == 0xffffffff) + { if (unz64local_getLong64(&s->z_filefunc, s->filestream_with_CD, &file_info.uncompressed_size) != UNZ_OK) err = UNZ_ERRNO; } - if (file_info.compressed_size == 0xffffffff) { + if (file_info.compressed_size == 0xffffffff) + { if (unz64local_getLong64(&s->z_filefunc, s->filestream_with_CD, &file_info.compressed_size) != UNZ_OK) err = UNZ_ERRNO; } - if (file_info_internal.offset_curfile == 0xffffffff) { + if (file_info_internal.offset_curfile == 0xffffffff) + { /* Relative Header offset */ if (unz64local_getLong64(&s->z_filefunc, s->filestream_with_CD, &uL64) != UNZ_OK) err = UNZ_ERRNO; file_info_internal.offset_curfile = uL64; file_info.disk_offset = uL64; } - if (file_info.disk_num_start == 0xffffffff) { + if (file_info.disk_num_start == 0xffffffff) + { /* Disk Start Number */ if (unz64local_getLong(&s->z_filefunc, s->filestream_with_CD, &file_info.disk_num_start) != UNZ_OK) err = UNZ_ERRNO; @@ -845,7 +879,8 @@ local int unz64local_GetCurrentFileInfoInternal(unzFile file, unz_file_info64 *p } #ifdef HAVE_AES /* AES header */ - else if (headerid == 0x9901) { + else if (headerid == 0x9901) + { /* Subtract size of AES field, since AES is handled internally */ file_info.size_file_extra_internal += 2 + 2 + datasize; @@ -856,16 +891,16 @@ local int unz64local_GetCurrentFileInfoInternal(unzFile file, unz_file_info64 *p if (uL != 1 && uL != 2) err = UNZ_ERRNO; file_info_internal.aes_version = uL; - if (unz64local_getByte(&s->z_filefunc, s->filestream_with_CD, (int *)&uL) != UNZ_OK) + if (unz64local_getByte(&s->z_filefunc, s->filestream_with_CD, &uL) != UNZ_OK) err = UNZ_ERRNO; if ((char)uL != 'A') err = UNZ_ERRNO; - if (unz64local_getByte(&s->z_filefunc, s->filestream_with_CD, (int *)&uL) != UNZ_OK) + if (unz64local_getByte(&s->z_filefunc, s->filestream_with_CD, &uL) != UNZ_OK) err = UNZ_ERRNO; if ((char)uL != 'E') err = UNZ_ERRNO; /* Get AES encryption strength and actual compression method */ - if (unz64local_getByte(&s->z_filefunc, s->filestream_with_CD, (int *)&uL) != UNZ_OK) + if (unz64local_getByte(&s->z_filefunc, s->filestream_with_CD, &uL) != UNZ_OK) err = UNZ_ERRNO; file_info_internal.aes_encryption_mode = uL; if (unz64local_getShort(&s->z_filefunc, s->filestream_with_CD, &uL) != UNZ_OK) @@ -873,8 +908,9 @@ local int unz64local_GetCurrentFileInfoInternal(unzFile file, unz_file_info64 *p file_info_internal.aes_compression_method = uL; } #endif - else { - if (ZSEEK64(s->z_filefunc, s->filestream_with_CD, datasize, ZLIB_FILEFUNC_SEEK_CUR) != 0) + else + { + if (ZSEEK64(s->z_filefunc, s->filestream_with_CD,datasize, ZLIB_FILEFUNC_SEEK_CUR) != 0) err = UNZ_ERRNO; } @@ -887,14 +923,18 @@ local int unz64local_GetCurrentFileInfoInternal(unzFile file, unz_file_info64 *p else file_info_internal.byte_before_the_zipfile = 0; - if ((err == UNZ_OK) && (comment != NULL)) { - if (file_info.size_file_comment < comment_size) { - *(comment + file_info.size_file_comment) = 0; + if ((err == UNZ_OK) && (comment != NULL)) + { + if (file_info.size_file_comment < comment_size) + { + *(comment+file_info.size_file_comment) = 0; bytes_to_read = file_info.size_file_comment; - } else + } + else bytes_to_read = comment_size; - if (lSeek != 0) { + if (lSeek != 0) + { if (ZSEEK64(s->z_filefunc, s->filestream_with_CD, lSeek, ZLIB_FILEFUNC_SEEK_CUR) != 0) err = UNZ_ERRNO; } @@ -903,7 +943,8 @@ local int unz64local_GetCurrentFileInfoInternal(unzFile file, unz_file_info64 *p if (ZREAD64(s->z_filefunc, s->filestream_with_CD, comment, (uLong)bytes_to_read) != bytes_to_read) err = UNZ_ERRNO; lSeek += file_info.size_file_comment - (uLong)bytes_to_read; - } else + } + else lSeek += file_info.size_file_comment; if ((err == UNZ_OK) && (pfile_info != NULL)) @@ -915,16 +956,17 @@ local int unz64local_GetCurrentFileInfoInternal(unzFile file, unz_file_info64 *p return err; } -extern int ZEXPORT unzGetCurrentFileInfo(unzFile file, unz_file_info *pfile_info, char *filename, - uLong filename_size, void *extrafield, uLong extrafield_size, char *comment, uLong comment_size) +extern int ZEXPORT unzGetCurrentFileInfo(unzFile file, unz_file_info * pfile_info, char *filename, + uLong filename_size, void *extrafield, uLong extrafield_size, char* comment, uLong comment_size) { unz_file_info64 file_info64; int err; err = unz64local_GetCurrentFileInfoInternal(file, &file_info64, NULL, filename, filename_size, - extrafield, extrafield_size, comment, comment_size); + extrafield, extrafield_size, comment, comment_size); - if ((err == UNZ_OK) && (pfile_info != NULL)) { + if ((err == UNZ_OK) && (pfile_info != NULL)) + { pfile_info->version = file_info64.version; pfile_info->version_needed = file_info64.version_needed; pfile_info->flag = file_info64.flag; @@ -949,18 +991,18 @@ extern int ZEXPORT unzGetCurrentFileInfo(unzFile file, unz_file_info *pfile_info return err; } -extern int ZEXPORT unzGetCurrentFileInfo64(unzFile file, unz_file_info64 *pfile_info, char *filename, - uLong filename_size, void *extrafield, uLong extrafield_size, char *comment, uLong comment_size) +extern int ZEXPORT unzGetCurrentFileInfo64(unzFile file, unz_file_info64 * pfile_info, char *filename, + uLong filename_size, void *extrafield, uLong extrafield_size, char* comment, uLong comment_size) { return unz64local_GetCurrentFileInfoInternal(file, pfile_info, NULL, filename, filename_size, - extrafield, extrafield_size, comment, comment_size); + extrafield, extrafield_size, comment,comment_size); } /* Read the local header of the current zipfile. Check the coherency of the local header and info in the end of central directory about this file store in *piSizeVar the size of extra info in local header (filename and size of extra field data) */ -local int unz64local_CheckCurrentFileCoherencyHeader(unz64_s *s, uInt *piSizeVar, ZPOS64_T *poffset_local_extrafield, - uInt *psize_local_extrafield) +local int unz64local_CheckCurrentFileCoherencyHeader(unz64_s* s, uInt* piSizeVar, ZPOS64_T *poffset_local_extrafield, + uInt *psize_local_extrafield) { uLong uMagic, uL, uFlags; uLong size_filename; @@ -977,10 +1019,11 @@ local int unz64local_CheckCurrentFileCoherencyHeader(unz64_s *s, uInt *piSizeVar return err; if (ZSEEK64(s->z_filefunc, s->filestream, s->cur_file_info_internal.offset_curfile + - s->cur_file_info_internal.byte_before_the_zipfile, ZLIB_FILEFUNC_SEEK_SET) != 0) + s->cur_file_info_internal.byte_before_the_zipfile, ZLIB_FILEFUNC_SEEK_SET) != 0) return UNZ_ERRNO; - if (err == UNZ_OK) { + if (err == UNZ_OK) + { if (unz64local_getLong(&s->z_filefunc, s->filestream, &uMagic) != UNZ_OK) err = UNZ_ERRNO; else if (uMagic != LOCALHEADERMAGIC) @@ -1041,18 +1084,18 @@ local int unz64local_CheckCurrentFileCoherencyHeader(unz64_s *s, uInt *piSizeVar } /* - Open for reading data the current file in the zipfile. - If there is no error and the file is opened, the return value is UNZ_OK. - */ -extern int ZEXPORT unzOpenCurrentFile3(unzFile file, int *method, int *level, int raw, const char *password) + Open for reading data the current file in the zipfile. + If there is no error and the file is opened, the return value is UNZ_OK. +*/ +extern int ZEXPORT unzOpenCurrentFile3(unzFile file, int* method, int* level, int raw, const char* password) { int err = UNZ_OK; int compression_method; uInt iSizeVar; - unz64_s *s; - file_in_zip64_read_info_s *pfile_in_zip_read_info; + unz64_s* s; + file_in_zip64_read_info_s* pfile_in_zip_read_info; ZPOS64_T offset_local_extrafield; - uInt size_local_extrafield; + uInt size_local_extrafield; #ifndef NOUNCRYPT char source[12]; #else @@ -1061,7 +1104,7 @@ extern int ZEXPORT unzOpenCurrentFile3(unzFile file, int *method, int *level, in #endif if (file == NULL) return UNZ_PARAMERROR; - s = (unz64_s *)file; + s = (unz64_s*)file; if (!s->current_file_ok) return UNZ_PARAMERROR; @@ -1071,17 +1114,18 @@ extern int ZEXPORT unzOpenCurrentFile3(unzFile file, int *method, int *level, in if (unz64local_CheckCurrentFileCoherencyHeader(s, &iSizeVar, &offset_local_extrafield, &size_local_extrafield) != UNZ_OK) return UNZ_BADZIPFILE; - pfile_in_zip_read_info = (file_in_zip64_read_info_s *)ALLOC(sizeof(file_in_zip64_read_info_s)); + pfile_in_zip_read_info = (file_in_zip64_read_info_s*)ALLOC(sizeof(file_in_zip64_read_info_s)); if (pfile_in_zip_read_info == NULL) return UNZ_INTERNALERROR; - pfile_in_zip_read_info->read_buffer = (Bytef *)ALLOC(UNZ_BUFSIZE); + pfile_in_zip_read_info->read_buffer = (Bytef*)ALLOC(UNZ_BUFSIZE); pfile_in_zip_read_info->offset_local_extrafield = offset_local_extrafield; pfile_in_zip_read_info->size_local_extrafield = size_local_extrafield; pfile_in_zip_read_info->pos_local_extrafield = 0; pfile_in_zip_read_info->raw = raw; - if (pfile_in_zip_read_info->read_buffer == NULL) { + if (pfile_in_zip_read_info->read_buffer == NULL) + { TRYFREE(pfile_in_zip_read_info); return UNZ_INTERNALERROR; } @@ -1097,12 +1141,14 @@ extern int ZEXPORT unzOpenCurrentFile3(unzFile file, int *method, int *level, in if (method != NULL) *method = compression_method; - if (level != NULL) { + if (level != NULL) + { *level = 6; - switch (s->cur_file_info.flag & 0x06) { - case 6: *level = 1; break; - case 4: *level = 2; break; - case 2: *level = 9; break; + switch (s->cur_file_info.flag & 0x06) + { + case 6 : *level = 1; break; + case 4 : *level = 2; break; + case 2 : *level = 9; break; } } @@ -1127,10 +1173,12 @@ extern int ZEXPORT unzOpenCurrentFile3(unzFile file, int *method, int *level, in pfile_in_zip_read_info->stream.total_in = 0; pfile_in_zip_read_info->stream.next_in = NULL; - if (!raw) { - if (compression_method == Z_BZIP2ED) { + if (!raw) + { + if (compression_method == Z_BZIP2ED) + { #ifdef HAVE_BZIP2 - pfile_in_zip_read_info->bstream.bzalloc = (void *(*)(void *, int, int)) 0; + pfile_in_zip_read_info->bstream.bzalloc = (void *(*) (void *, int, int))0; pfile_in_zip_read_info->bstream.bzfree = (free_func)0; pfile_in_zip_read_info->bstream.opaque = (voidpf)0; pfile_in_zip_read_info->bstream.state = (voidpf)0; @@ -1143,15 +1191,18 @@ extern int ZEXPORT unzOpenCurrentFile3(unzFile file, int *method, int *level, in err = BZ2_bzDecompressInit(&pfile_in_zip_read_info->bstream, 0, 0); if (err == Z_OK) - pfile_in_zip_read_info->stream_initialised = Z_BZIP2ED; - else { + pfile_in_zip_read_info->stream_initialised=Z_BZIP2ED; + else + { TRYFREE(pfile_in_zip_read_info); return err; } #else pfile_in_zip_read_info->raw = 1; #endif - } else if (compression_method == Z_DEFLATED) { + } + else if (compression_method == Z_DEFLATED) + { pfile_in_zip_read_info->stream.zalloc = (alloc_func)0; pfile_in_zip_read_info->stream.zfree = (free_func)0; pfile_in_zip_read_info->stream.opaque = (voidpf)s; @@ -1161,7 +1212,8 @@ extern int ZEXPORT unzOpenCurrentFile3(unzFile file, int *method, int *level, in err = inflateInit2(&pfile_in_zip_read_info->stream, -MAX_WBITS); if (err == Z_OK) pfile_in_zip_read_info->stream_initialised = Z_DEFLATED; - else { + else + { TRYFREE(pfile_in_zip_read_info); return err; } @@ -1181,15 +1233,18 @@ extern int ZEXPORT unzOpenCurrentFile3(unzFile file, int *method, int *level, in pfile_in_zip_read_info->stream.avail_in = (uInt)0; s->pfile_in_zip_read = pfile_in_zip_read_info; + s->pcrc_32_tab = NULL; #ifndef NOUNCRYPT - if ((password != NULL) && ((s->cur_file_info.flag & 1) != 0)) { + if ((password != NULL) && ((s->cur_file_info.flag & 1) != 0)) + { if (ZSEEK64(s->z_filefunc, s->filestream, - s->pfile_in_zip_read->pos_in_zipfile + s->pfile_in_zip_read->byte_before_the_zipfile, - ZLIB_FILEFUNC_SEEK_SET) != 0) + s->pfile_in_zip_read->pos_in_zipfile + s->pfile_in_zip_read->byte_before_the_zipfile, + ZLIB_FILEFUNC_SEEK_SET) != 0) return UNZ_INTERNALERROR; #ifdef HAVE_AES - if (s->cur_file_info.compression_method == AES_METHOD) { + if (s->cur_file_info.compression_method == AES_METHOD) + { unsigned char passverify[AES_PWVERIFYSIZE]; unsigned char saltvalue[AES_MAXSALTLENGTH]; uInt saltlength; @@ -1205,18 +1260,19 @@ extern int ZEXPORT unzOpenCurrentFile3(unzFile file, int *method, int *level, in if (ZREAD64(s->z_filefunc, s->filestream, passverify, AES_PWVERIFYSIZE) != AES_PWVERIFYSIZE) return UNZ_INTERNALERROR; - fcrypt_init((int)s->cur_file_info_internal.aes_encryption_mode, (unsigned char *)password, (unsigned int)strlen(password), saltvalue, - passverify, &s->pfile_in_zip_read->aes_ctx); + fcrypt_init(s->cur_file_info_internal.aes_encryption_mode, password, strlen(password), saltvalue, + passverify, &s->pfile_in_zip_read->aes_ctx); - pfile_in_zip_read_info->rest_read_compressed -= saltlength + AES_PWVERIFYSIZE; - pfile_in_zip_read_info->rest_read_compressed -= AES_AUTHCODESIZE; + s->pfile_in_zip_read->rest_read_compressed -= saltlength + AES_PWVERIFYSIZE; + s->pfile_in_zip_read->rest_read_compressed -= AES_AUTHCODESIZE; s->pfile_in_zip_read->pos_in_zipfile += saltlength + AES_PWVERIFYSIZE; - } else + } + else #endif { int i; - s->pcrc_32_tab = (const unsigned long *)get_crc_table(); + s->pcrc_32_tab = (const unsigned long*)get_crc_table(); init_keys(password, s->keys, s->pcrc_32_tab); if (ZREAD64(s->z_filefunc, s->filestream, source, 12) < 12) @@ -1225,7 +1281,7 @@ extern int ZEXPORT unzOpenCurrentFile3(unzFile file, int *method, int *level, in for (i = 0; i < 12; i++) zdecode(s->keys, s->pcrc_32_tab, source[i]); - pfile_in_zip_read_info->rest_read_compressed -= 12; + s->pfile_in_zip_read->rest_read_compressed -= 12; s->pfile_in_zip_read->pos_in_zipfile += 12; } @@ -1240,12 +1296,12 @@ extern int ZEXPORT unzOpenCurrentFile(unzFile file) return unzOpenCurrentFile3(file, NULL, NULL, 0, NULL); } -extern int ZEXPORT unzOpenCurrentFilePassword(unzFile file, const char *password) +extern int ZEXPORT unzOpenCurrentFilePassword(unzFile file, const char* password) { return unzOpenCurrentFile3(file, NULL, NULL, 0, password); } -extern int ZEXPORT unzOpenCurrentFile2(unzFile file, int *method, int *level, int raw) +extern int ZEXPORT unzOpenCurrentFile2(unzFile file, int* method, int* level, int raw) { return unzOpenCurrentFile3(file, method, level, raw, NULL); } @@ -1261,212 +1317,208 @@ extern int ZEXPORT unzReadCurrentFile(unzFile file, voidp buf, unsigned len) { int err = UNZ_OK; uInt read = 0; - unz64_s *s; - file_in_zip64_read_info_s *pfile_in_zip_read_info; + unz64_s* s; if (file == NULL) return UNZ_PARAMERROR; - s = (unz64_s *)file; - pfile_in_zip_read_info = s->pfile_in_zip_read; + s = (unz64_s*)file; - if (pfile_in_zip_read_info == NULL) + if (s->pfile_in_zip_read == NULL) return UNZ_PARAMERROR; - if (pfile_in_zip_read_info->read_buffer == NULL) + if (s->pfile_in_zip_read->read_buffer == NULL) return UNZ_END_OF_LIST_OF_FILE; if (len == 0) return 0; - pfile_in_zip_read_info->stream.next_out = (Bytef *)buf; - pfile_in_zip_read_info->stream.avail_out = (uInt)len; - - if (pfile_in_zip_read_info->raw) { - if (len > pfile_in_zip_read_info->rest_read_compressed + pfile_in_zip_read_info->stream.avail_in) - pfile_in_zip_read_info->stream.avail_out = (uInt)pfile_in_zip_read_info->rest_read_compressed + - pfile_in_zip_read_info->stream.avail_in; - } else { - - // NOTE: - // This bit of code seems to try to set the amount of space in the output buffer based on the - // value stored in the headers stored in the .zip file. However, if those values are incorrect - // it may result in a loss of data when uncompresssing that file. The compressed data is still - // legit and will deflate without knowing the uncompressed code so this tidbit is unnecessary and - // may cause issues for some .zip files. - // - // It's removed in here to fix those issues. - // - // See: https://github.com/ZipArchive/ziparchive/issues/16 - // - - /* - - - FIXME: Upgrading to minizip 1.1 caused issues here, Uncommented the code that was commented before. 11/24/2015 - */ - - if (len > pfile_in_zip_read_info->rest_read_uncompressed) - pfile_in_zip_read_info->stream.avail_out = (uInt)pfile_in_zip_read_info->rest_read_uncompressed; - - - - } + s->pfile_in_zip_read->stream.next_out = (Bytef*)buf; + s->pfile_in_zip_read->stream.avail_out = (uInt)len; - while (pfile_in_zip_read_info->stream.avail_out > 0) { - if (pfile_in_zip_read_info->stream.avail_in == 0) { - uInt bytes_to_read = UNZ_BUFSIZE; - uInt bytes_not_read = 0; - uInt bytes_read = 0; - uInt total_bytes_read = 0; + if (s->pfile_in_zip_read->raw) + { + if (len > s->pfile_in_zip_read->rest_read_compressed + s->pfile_in_zip_read->stream.avail_in) + s->pfile_in_zip_read->stream.avail_out = (uInt)s->pfile_in_zip_read->rest_read_compressed + + s->pfile_in_zip_read->stream.avail_in; + } + else + { + if (len > s->pfile_in_zip_read->rest_read_uncompressed) + s->pfile_in_zip_read->stream.avail_out = (uInt)s->pfile_in_zip_read->rest_read_uncompressed; + } - if (pfile_in_zip_read_info->stream.next_in != NULL) - bytes_not_read = (uInt)(pfile_in_zip_read_info->read_buffer + UNZ_BUFSIZE - - pfile_in_zip_read_info->stream.next_in); + while (s->pfile_in_zip_read->stream.avail_out > 0) + { + if (s->pfile_in_zip_read->stream.avail_in == 0) + { + uLong bytes_to_read = UNZ_BUFSIZE; + uLong bytes_not_read = 0; + uLong bytes_read = 0; + uLong total_bytes_read = 0; + + if (s->pfile_in_zip_read->stream.next_in != NULL) + bytes_not_read = s->pfile_in_zip_read->read_buffer + UNZ_BUFSIZE - + s->pfile_in_zip_read->stream.next_in; bytes_to_read -= bytes_not_read; if (bytes_not_read > 0) - memcpy(pfile_in_zip_read_info->read_buffer, pfile_in_zip_read_info->stream.next_in, bytes_not_read); - if (pfile_in_zip_read_info->rest_read_compressed < bytes_to_read) - bytes_to_read = (uInt)pfile_in_zip_read_info->rest_read_compressed; - - while (total_bytes_read != bytes_to_read) { - if (ZSEEK64(pfile_in_zip_read_info->z_filefunc, pfile_in_zip_read_info->filestream, - pfile_in_zip_read_info->pos_in_zipfile + pfile_in_zip_read_info->byte_before_the_zipfile, - ZLIB_FILEFUNC_SEEK_SET) != 0) + memcpy(s->pfile_in_zip_read->read_buffer, s->pfile_in_zip_read->stream.next_in, bytes_not_read); + if (s->pfile_in_zip_read->rest_read_compressed < bytes_to_read) + bytes_to_read = (uInt)s->pfile_in_zip_read->rest_read_compressed; + + while (total_bytes_read != bytes_to_read) + { + if (ZSEEK64(s->pfile_in_zip_read->z_filefunc, s->pfile_in_zip_read->filestream, + s->pfile_in_zip_read->pos_in_zipfile + s->pfile_in_zip_read->byte_before_the_zipfile, + ZLIB_FILEFUNC_SEEK_SET) != 0) return UNZ_ERRNO; - bytes_read = (int)ZREAD64(pfile_in_zip_read_info->z_filefunc, pfile_in_zip_read_info->filestream, - pfile_in_zip_read_info->read_buffer + bytes_not_read + total_bytes_read, - bytes_to_read - total_bytes_read); + bytes_read = ZREAD64(s->pfile_in_zip_read->z_filefunc, s->pfile_in_zip_read->filestream, + s->pfile_in_zip_read->read_buffer + bytes_not_read + total_bytes_read, + bytes_to_read - total_bytes_read); total_bytes_read += bytes_read; - pfile_in_zip_read_info->pos_in_zipfile += bytes_read; + s->pfile_in_zip_read->pos_in_zipfile += bytes_read; - if (bytes_read == 0) { - if (ZERROR64(pfile_in_zip_read_info->z_filefunc, pfile_in_zip_read_info->filestream)) + if (bytes_read == 0) + { + if (ZERROR64(s->pfile_in_zip_read->z_filefunc, s->pfile_in_zip_read->filestream)) return UNZ_ERRNO; err = unzGoToNextDisk(file); if (err != UNZ_OK) return err; - pfile_in_zip_read_info->pos_in_zipfile = 0; - pfile_in_zip_read_info->filestream = s->filestream; + s->pfile_in_zip_read->pos_in_zipfile = 0; + s->pfile_in_zip_read->filestream = s->filestream; } } #ifndef NOUNCRYPT - if ((s->cur_file_info.flag & 1) != 0) { + if ((s->cur_file_info.flag & 1) != 0) + { #ifdef HAVE_AES - if (s->cur_file_info.compression_method == AES_METHOD) { - fcrypt_decrypt(pfile_in_zip_read_info->read_buffer, bytes_to_read, &s->pfile_in_zip_read->aes_ctx); - } else + if (s->cur_file_info.compression_method == AES_METHOD) + { + fcrypt_decrypt(s->pfile_in_zip_read->read_buffer, bytes_to_read, &s->pfile_in_zip_read->aes_ctx); + } + else #endif + if (s->pcrc_32_tab != NULL) { uInt i; - for (i = 0; i < total_bytes_read; i++) - pfile_in_zip_read_info->read_buffer[i] = - zdecode(s->keys, s->pcrc_32_tab, pfile_in_zip_read_info->read_buffer[i]); + for(i = 0; i < total_bytes_read; i++) + s->pfile_in_zip_read->read_buffer[i] = + zdecode(s->keys, s->pcrc_32_tab, s->pfile_in_zip_read->read_buffer[i]); } } #endif - pfile_in_zip_read_info->rest_read_compressed -= total_bytes_read; - pfile_in_zip_read_info->stream.next_in = (Bytef *)pfile_in_zip_read_info->read_buffer; - pfile_in_zip_read_info->stream.avail_in = (uInt)bytes_not_read + total_bytes_read; + s->pfile_in_zip_read->rest_read_compressed -= total_bytes_read; + s->pfile_in_zip_read->stream.next_in = (Bytef*)s->pfile_in_zip_read->read_buffer; + s->pfile_in_zip_read->stream.avail_in = (uInt)(bytes_not_read + total_bytes_read); } - if ((pfile_in_zip_read_info->compression_method == 0) || (pfile_in_zip_read_info->raw)) { + if ((s->pfile_in_zip_read->compression_method == 0) || (s->pfile_in_zip_read->raw)) + { uInt copy, i; - if ((pfile_in_zip_read_info->stream.avail_in == 0) && - (pfile_in_zip_read_info->rest_read_compressed == 0)) + if ((s->pfile_in_zip_read->stream.avail_in == 0) && + (s->pfile_in_zip_read->rest_read_compressed == 0)) return (read == 0) ? UNZ_EOF : read; - if (pfile_in_zip_read_info->stream.avail_out < pfile_in_zip_read_info->stream.avail_in) - copy = pfile_in_zip_read_info->stream.avail_out; + if (s->pfile_in_zip_read->stream.avail_out < s->pfile_in_zip_read->stream.avail_in) + copy = s->pfile_in_zip_read->stream.avail_out; else - copy = pfile_in_zip_read_info->stream.avail_in; + copy = s->pfile_in_zip_read->stream.avail_in; for (i = 0; i < copy; i++) - *(pfile_in_zip_read_info->stream.next_out + i) = - *(pfile_in_zip_read_info->stream.next_in + i); - - pfile_in_zip_read_info->total_out_64 = pfile_in_zip_read_info->total_out_64 + copy; - pfile_in_zip_read_info->rest_read_uncompressed -= copy; - pfile_in_zip_read_info->crc32 = crc32(pfile_in_zip_read_info->crc32, - pfile_in_zip_read_info->stream.next_out, copy); - - pfile_in_zip_read_info->stream.avail_in -= copy; - pfile_in_zip_read_info->stream.avail_out -= copy; - pfile_in_zip_read_info->stream.next_out += copy; - pfile_in_zip_read_info->stream.next_in += copy; - pfile_in_zip_read_info->stream.total_out += copy; + *(s->pfile_in_zip_read->stream.next_out+i) = + *(s->pfile_in_zip_read->stream.next_in+i); + + s->pfile_in_zip_read->total_out_64 = s->pfile_in_zip_read->total_out_64 + copy; + s->pfile_in_zip_read->rest_read_uncompressed -= copy; + s->pfile_in_zip_read->crc32 = crc32(s->pfile_in_zip_read->crc32, + s->pfile_in_zip_read->stream.next_out, copy); + + s->pfile_in_zip_read->stream.avail_in -= copy; + s->pfile_in_zip_read->stream.avail_out -= copy; + s->pfile_in_zip_read->stream.next_out += copy; + s->pfile_in_zip_read->stream.next_in += copy; + s->pfile_in_zip_read->stream.total_out += copy; read += copy; - } else if (pfile_in_zip_read_info->compression_method == Z_BZIP2ED) { + } + else if (s->pfile_in_zip_read->compression_method == Z_BZIP2ED) + { #ifdef HAVE_BZIP2 uLong total_out_before, total_out_after; const Bytef *buf_before; uLong out_bytes; - pfile_in_zip_read_info->bstream.next_in = (char *)pfile_in_zip_read_info->stream.next_in; - pfile_in_zip_read_info->bstream.avail_in = pfile_in_zip_read_info->stream.avail_in; - pfile_in_zip_read_info->bstream.total_in_lo32 = pfile_in_zip_read_info->stream.total_in; - pfile_in_zip_read_info->bstream.total_in_hi32 = 0; - pfile_in_zip_read_info->bstream.next_out = (char *)pfile_in_zip_read_info->stream.next_out; - pfile_in_zip_read_info->bstream.avail_out = pfile_in_zip_read_info->stream.avail_out; - pfile_in_zip_read_info->bstream.total_out_lo32 = pfile_in_zip_read_info->stream.total_out; - pfile_in_zip_read_info->bstream.total_out_hi32 = 0; + s->pfile_in_zip_read->bstream.next_in = (char*)s->pfile_in_zip_read->stream.next_in; + s->pfile_in_zip_read->bstream.avail_in = s->pfile_in_zip_read->stream.avail_in; + s->pfile_in_zip_read->bstream.total_in_lo32 = (uInt)s->pfile_in_zip_read->stream.total_in; + s->pfile_in_zip_read->bstream.total_in_hi32 = s->pfile_in_zip_read->stream.total_in >> 32; + + s->pfile_in_zip_read->bstream.next_out = (char*)s->pfile_in_zip_read->stream.next_out; + s->pfile_in_zip_read->bstream.avail_out = s->pfile_in_zip_read->stream.avail_out; + s->pfile_in_zip_read->bstream.total_out_lo32 = (uInt)s->pfile_in_zip_read->stream.total_out; + s->pfile_in_zip_read->bstream.total_out_hi32 = s->pfile_in_zip_read->stream.total_out >> 32; + + total_out_before = s->pfile_in_zip_read->bstream.total_out_lo32 + + (((uLong)s->pfile_in_zip_read->bstream.total_out_hi32) << 32); + buf_before = (const Bytef *)s->pfile_in_zip_read->bstream.next_out; - total_out_before = pfile_in_zip_read_info->bstream.total_out_lo32; - buf_before = (const Bytef *)pfile_in_zip_read_info->bstream.next_out; + err = BZ2_bzDecompress(&s->pfile_in_zip_read->bstream); - err = BZ2_bzDecompress(&pfile_in_zip_read_info->bstream); + total_out_after = s->pfile_in_zip_read->bstream.total_out_lo32 + + (((uLong)s->pfile_in_zip_read->bstream.total_out_hi32) << 32); - total_out_after = pfile_in_zip_read_info->bstream.total_out_lo32; - out_bytes = total_out_after - total_out_before; + out_bytes = total_out_after-total_out_before; - pfile_in_zip_read_info->total_out_64 = pfile_in_zip_read_info->total_out_64 + out_bytes; - pfile_in_zip_read_info->rest_read_uncompressed -= out_bytes; - pfile_in_zip_read_info->crc32 = crc32(pfile_in_zip_read_info->crc32, buf_before, (uInt)(out_bytes)); + s->pfile_in_zip_read->total_out_64 = s->pfile_in_zip_read->total_out_64 + out_bytes; + s->pfile_in_zip_read->rest_read_uncompressed -= out_bytes; + s->pfile_in_zip_read->crc32 = crc32(s->pfile_in_zip_read->crc32,buf_before, (uInt)(out_bytes)); read += (uInt)(total_out_after - total_out_before); - pfile_in_zip_read_info->stream.next_in = (Bytef *)pfile_in_zip_read_info->bstream.next_in; - pfile_in_zip_read_info->stream.avail_in = pfile_in_zip_read_info->bstream.avail_in; - pfile_in_zip_read_info->stream.total_in = pfile_in_zip_read_info->bstream.total_in_lo32; - pfile_in_zip_read_info->stream.next_out = (Bytef *)pfile_in_zip_read_info->bstream.next_out; - pfile_in_zip_read_info->stream.avail_out = pfile_in_zip_read_info->bstream.avail_out; - pfile_in_zip_read_info->stream.total_out = pfile_in_zip_read_info->bstream.total_out_lo32; + s->pfile_in_zip_read->stream.next_in = (Bytef*)s->pfile_in_zip_read->bstream.next_in; + s->pfile_in_zip_read->stream.avail_in = s->pfile_in_zip_read->bstream.avail_in; + s->pfile_in_zip_read->stream.total_in = s->pfile_in_zip_read->bstream.total_in_lo32; + s->pfile_in_zip_read->stream.next_out = (Bytef*)s->pfile_in_zip_read->bstream.next_out; + s->pfile_in_zip_read->stream.avail_out = s->pfile_in_zip_read->bstream.avail_out; + s->pfile_in_zip_read->stream.total_out = s->pfile_in_zip_read->bstream.total_out_lo32; if (err == BZ_STREAM_END) return (read == 0) ? UNZ_EOF : read; if (err != BZ_OK) break; #endif - } else { + } + else + { ZPOS64_T total_out_before, total_out_after; const Bytef *buf_before; ZPOS64_T out_bytes; - int flush = Z_SYNC_FLUSH; + int flush=Z_SYNC_FLUSH; - total_out_before = pfile_in_zip_read_info->stream.total_out; - buf_before = pfile_in_zip_read_info->stream.next_out; + total_out_before = s->pfile_in_zip_read->stream.total_out; + buf_before = s->pfile_in_zip_read->stream.next_out; /* - if ((pfile_in_zip_read_info->rest_read_uncompressed == + if ((pfile_in_zip_read_info->rest_read_uncompressed == pfile_in_zip_read_info->stream.avail_out) && (pfile_in_zip_read_info->rest_read_compressed == 0)) flush = Z_FINISH; - */ - err = inflate(&pfile_in_zip_read_info->stream, flush); + */ + err = inflate(&s->pfile_in_zip_read->stream,flush); - if ((err >= 0) && (pfile_in_zip_read_info->stream.msg != NULL)) + if ((err >= 0) && (s->pfile_in_zip_read->stream.msg != NULL)) err = Z_DATA_ERROR; - total_out_after = pfile_in_zip_read_info->stream.total_out; - out_bytes = total_out_after - total_out_before; + total_out_after = s->pfile_in_zip_read->stream.total_out; + out_bytes = total_out_after-total_out_before; - pfile_in_zip_read_info->total_out_64 += out_bytes; - pfile_in_zip_read_info->rest_read_uncompressed -= out_bytes; - pfile_in_zip_read_info->crc32 = - crc32(pfile_in_zip_read_info->crc32, buf_before, (uInt)(out_bytes)); + s->pfile_in_zip_read->total_out_64 += out_bytes; + s->pfile_in_zip_read->rest_read_uncompressed -= out_bytes; + s->pfile_in_zip_read->crc32 = + crc32(s->pfile_in_zip_read->crc32,buf_before, (uInt)(out_bytes)); read += (uInt)(total_out_after - total_out_before); @@ -1484,33 +1536,28 @@ extern int ZEXPORT unzReadCurrentFile(unzFile file, voidp buf, unsigned len) extern ZPOS64_T ZEXPORT unzGetCurrentFileZStreamPos64(unzFile file) { - unz64_s *s; - file_in_zip64_read_info_s *pfile_in_zip_read_info; - s = (unz64_s *)file; + unz64_s* s; + s = (unz64_s*)file; if (file == NULL) return 0; /* UNZ_PARAMERROR */ - pfile_in_zip_read_info = s->pfile_in_zip_read; - if (pfile_in_zip_read_info == NULL) + if (s->pfile_in_zip_read == NULL) return 0; /* UNZ_PARAMERROR */ - return pfile_in_zip_read_info->pos_in_zipfile + pfile_in_zip_read_info->byte_before_the_zipfile; + return s->pfile_in_zip_read->pos_in_zipfile + s->pfile_in_zip_read->byte_before_the_zipfile; } extern int ZEXPORT unzGetLocalExtrafield(unzFile file, voidp buf, unsigned len) { - unz64_s *s; - file_in_zip64_read_info_s *pfile_in_zip_read_info; + unz64_s* s; uInt read_now; ZPOS64_T size_to_read; if (file == NULL) return UNZ_PARAMERROR; - s = (unz64_s *)file; - pfile_in_zip_read_info = s->pfile_in_zip_read; - - if (pfile_in_zip_read_info == NULL) + s = (unz64_s*)file; + if (s->pfile_in_zip_read == NULL) return UNZ_PARAMERROR; - size_to_read = pfile_in_zip_read_info->size_local_extrafield - pfile_in_zip_read_info->pos_local_extrafield; + size_to_read = s->pfile_in_zip_read->size_local_extrafield - s->pfile_in_zip_read->pos_local_extrafield; if (buf == NULL) return (int)size_to_read; @@ -1518,17 +1565,17 @@ extern int ZEXPORT unzGetLocalExtrafield(unzFile file, voidp buf, unsigned len) if (len > size_to_read) read_now = (uInt)size_to_read; else - read_now = (uInt)len; + read_now = (uInt)len ; if (read_now == 0) return 0; - if (ZSEEK64(pfile_in_zip_read_info->z_filefunc, pfile_in_zip_read_info->filestream, - pfile_in_zip_read_info->offset_local_extrafield + pfile_in_zip_read_info->pos_local_extrafield, - ZLIB_FILEFUNC_SEEK_SET) != 0) + if (ZSEEK64(s->pfile_in_zip_read->z_filefunc, s->pfile_in_zip_read->filestream, + s->pfile_in_zip_read->offset_local_extrafield + s->pfile_in_zip_read->pos_local_extrafield, + ZLIB_FILEFUNC_SEEK_SET) != 0) return UNZ_ERRNO; - if (ZREAD64(pfile_in_zip_read_info->z_filefunc, pfile_in_zip_read_info->filestream, buf, read_now) != read_now) + if (ZREAD64(s->pfile_in_zip_read->z_filefunc, s->pfile_in_zip_read->filestream, buf, read_now) != read_now) return UNZ_ERRNO; return (int)read_now; @@ -1538,18 +1585,19 @@ extern int ZEXPORT unzCloseCurrentFile(unzFile file) { int err = UNZ_OK; - unz64_s *s; - file_in_zip64_read_info_s *pfile_in_zip_read_info; + unz64_s* s; + file_in_zip64_read_info_s* pfile_in_zip_read_info; if (file == NULL) return UNZ_PARAMERROR; - s = (unz64_s *)file; - pfile_in_zip_read_info = s->pfile_in_zip_read; + s = (unz64_s*)file; + pfile_in_zip_read_info=s->pfile_in_zip_read; if (pfile_in_zip_read_info == NULL) return UNZ_PARAMERROR; #ifdef HAVE_AES - if (s->cur_file_info.compression_method == AES_METHOD) { + if (s->cur_file_info.compression_method == AES_METHOD) + { unsigned char authcode[AES_AUTHCODESIZE]; unsigned char rauthcode[AES_AUTHCODESIZE]; @@ -1567,7 +1615,8 @@ extern int ZEXPORT unzCloseCurrentFile(unzFile file) #endif { if ((pfile_in_zip_read_info->rest_read_uncompressed == 0) && - (!pfile_in_zip_read_info->raw)) { + (!pfile_in_zip_read_info->raw)) + { if (pfile_in_zip_read_info->crc32 != pfile_in_zip_read_info->crc32_wait) err = UNZ_CRCERROR; } @@ -1591,17 +1640,17 @@ extern int ZEXPORT unzCloseCurrentFile(unzFile file) } extern int ZEXPORT unzGoToFirstFile2(unzFile file, unz_file_info64 *pfile_info, char *filename, - uLong filename_size, void *extrafield, uLong extrafield_size, char *comment, uLong comment_size) + uLong filename_size, void *extrafield, uLong extrafield_size, char *comment, uLong comment_size) { int err = UNZ_OK; - unz64_s *s; + unz64_s* s; if (file == NULL) return UNZ_PARAMERROR; - s = (unz64_s *)file; + s = (unz64_s*)file; s->pos_in_central_dir = s->offset_central_dir; s->num_file = 0; err = unz64local_GetCurrentFileInfoInternal(file, &s->cur_file_info, &s->cur_file_info_internal, - filename, filename_size, extrafield, extrafield_size, comment, comment_size); + filename,filename_size, extrafield,extrafield_size, comment,comment_size); s->current_file_ok = (err == UNZ_OK); if ((err == UNZ_OK) && (pfile_info != NULL)) memcpy(pfile_info, &s->cur_file_info, sizeof(unz_file_info64)); @@ -1614,24 +1663,24 @@ extern int ZEXPORT unzGoToFirstFile(unzFile file) } extern int ZEXPORT unzGoToNextFile2(unzFile file, unz_file_info64 *pfile_info, char *filename, - uLong filename_size, void *extrafield, uLong extrafield_size, char *comment, uLong comment_size) + uLong filename_size, void *extrafield, uLong extrafield_size, char *comment, uLong comment_size) { - unz64_s *s; + unz64_s* s; int err; if (file == NULL) return UNZ_PARAMERROR; - s = (unz64_s *)file; + s = (unz64_s*)file; if (!s->current_file_ok) return UNZ_END_OF_LIST_OF_FILE; if (s->gi.number_entry != 0xffff) /* 2^16 files overflow hack */ - if (s->num_file + 1 == s->gi.number_entry) - return UNZ_END_OF_LIST_OF_FILE; + if (s->num_file+1 == s->gi.number_entry) + return UNZ_END_OF_LIST_OF_FILE; s->pos_in_central_dir += SIZECENTRALDIRITEM + s->cur_file_info.size_filename + - s->cur_file_info.size_file_extra + s->cur_file_info.size_file_comment; + s->cur_file_info.size_file_extra + s->cur_file_info.size_file_comment; s->num_file++; - err = unz64local_GetCurrentFileInfoInternal(file, &s->cur_file_info, &s->cur_file_info_internal, - filename, filename_size, extrafield, extrafield_size, comment, comment_size); + err = unz64local_GetCurrentFileInfoInternal(file,&s->cur_file_info, &s->cur_file_info_internal, + filename, filename_size, extrafield,extrafield_size, comment,comment_size); s->current_file_ok = (err == UNZ_OK); if ((err == UNZ_OK) && (pfile_info != NULL)) memcpy(pfile_info, &s->cur_file_info, sizeof(unz_file_info64)); @@ -1645,19 +1694,19 @@ extern int ZEXPORT unzGoToNextFile(unzFile file) extern int ZEXPORT unzLocateFile(unzFile file, const char *filename, unzFileNameComparer filename_compare_func) { - unz64_s *s; + unz64_s* s; int err; unz_file_info64 cur_file_info_saved; unz_file_info64_internal cur_file_info_internal_saved; ZPOS64_T num_file_saved; ZPOS64_T pos_in_central_dir_saved; - char current_filename[UNZ_MAXFILENAMEINZIP + 1]; + char current_filename[UNZ_MAXFILENAMEINZIP+1]; if (file == NULL) return UNZ_PARAMERROR; if (strlen(filename) >= UNZ_MAXFILENAMEINZIP) return UNZ_PARAMERROR; - s = (unz64_s *)file; + s = (unz64_s*)file; if (!s->current_file_ok) return UNZ_END_OF_LIST_OF_FILE; @@ -1667,16 +1716,17 @@ extern int ZEXPORT unzLocateFile(unzFile file, const char *filename, unzFileName cur_file_info_saved = s->cur_file_info; cur_file_info_internal_saved = s->cur_file_info_internal; - err = unzGoToFirstFile2(file, NULL, current_filename, sizeof(current_filename) - 1, NULL, 0, NULL, 0); + err = unzGoToFirstFile2(file, NULL, current_filename, sizeof(current_filename)-1, NULL, 0, NULL, 0); - while (err == UNZ_OK) { + while (err == UNZ_OK) + { if (filename_compare_func != NULL) err = filename_compare_func(file, current_filename, filename); else err = strcmp(current_filename, filename); if (err == 0) return UNZ_OK; - err = unzGoToNextFile2(file, NULL, current_filename, sizeof(current_filename) - 1, NULL, 0, NULL, 0); + err = unzGoToNextFile2(file, NULL, current_filename, sizeof(current_filename)-1, NULL, 0, NULL, 0); } /* We failed, so restore the state of the 'current file' to where we were. */ @@ -1687,18 +1737,19 @@ extern int ZEXPORT unzLocateFile(unzFile file, const char *filename, unzFileName return err; } -extern int ZEXPORT unzGetFilePos(unzFile file, unz_file_pos *file_pos) +extern int ZEXPORT unzGetFilePos(unzFile file, unz_file_pos* file_pos) { unz64_file_pos file_pos64; - int err = unzGetFilePos64(file, &file_pos64); - if (err == UNZ_OK) { + int err = unzGetFilePos64(file,&file_pos64); + if (err == UNZ_OK) + { file_pos->pos_in_zip_directory = (uLong)file_pos64.pos_in_zip_directory; file_pos->num_of_file = (uLong)file_pos64.num_of_file; } return err; } -extern int ZEXPORT unzGoToFilePos(unzFile file, unz_file_pos *file_pos) +extern int ZEXPORT unzGoToFilePos(unzFile file, unz_file_pos* file_pos) { unz64_file_pos file_pos64; @@ -1706,40 +1757,40 @@ extern int ZEXPORT unzGoToFilePos(unzFile file, unz_file_pos *file_pos) return UNZ_PARAMERROR; file_pos64.pos_in_zip_directory = file_pos->pos_in_zip_directory; file_pos64.num_of_file = file_pos->num_of_file; - return unzGoToFilePos64(file, &file_pos64); + return unzGoToFilePos64(file,&file_pos64); } -extern int ZEXPORT unzGetFilePos64(unzFile file, unz64_file_pos *file_pos) +extern int ZEXPORT unzGetFilePos64(unzFile file, unz64_file_pos* file_pos) { - unz64_s *s; + unz64_s* s; if (file == NULL || file_pos == NULL) return UNZ_PARAMERROR; - s = (unz64_s *)file; + s = (unz64_s*)file; if (!s->current_file_ok) return UNZ_END_OF_LIST_OF_FILE; - file_pos->pos_in_zip_directory = s->pos_in_central_dir; + file_pos->pos_in_zip_directory = s->pos_in_central_dir; file_pos->num_of_file = s->num_file; return UNZ_OK; } -extern int ZEXPORT unzGoToFilePos64(unzFile file, const unz64_file_pos *file_pos) +extern int ZEXPORT unzGoToFilePos64(unzFile file, const unz64_file_pos* file_pos) { - unz64_s *s; + unz64_s* s; int err; if (file == NULL || file_pos == NULL) return UNZ_PARAMERROR; - s = (unz64_s *)file; + s = (unz64_s*)file; /* jump to the right spot */ s->pos_in_central_dir = file_pos->pos_in_zip_directory; s->num_file = file_pos->num_of_file; /* set the current file */ - err = unz64local_GetCurrentFileInfoInternal(file, &s->cur_file_info, &s->cur_file_info_internal, NULL, 0, NULL, 0, NULL, 0); + err = unz64local_GetCurrentFileInfoInternal(file,&s->cur_file_info, &s->cur_file_info_internal,NULL,0,NULL,0,NULL,0); /* return results */ s->current_file_ok = (err == UNZ_OK); return err; @@ -1757,11 +1808,11 @@ extern uLong ZEXPORT unzGetOffset(unzFile file) extern ZPOS64_T ZEXPORT unzGetOffset64(unzFile file) { - unz64_s *s; + unz64_s* s; if (file == NULL) return 0; /* UNZ_PARAMERROR; */ - s = (unz64_s *)file; + s = (unz64_s*)file; if (!s->current_file_ok) return 0; if (s->gi.number_entry != 0 && s->gi.number_entry != 0xffff) @@ -1777,63 +1828,120 @@ extern int ZEXPORT unzSetOffset(unzFile file, uLong pos) extern int ZEXPORT unzSetOffset64(unzFile file, ZPOS64_T pos) { - unz64_s *s; + unz64_s* s; int err; if (file == NULL) return UNZ_PARAMERROR; - s = (unz64_s *)file; - + s = (unz64_s*)file; s->pos_in_central_dir = pos; s->num_file = s->gi.number_entry; /* hack */ + err = unz64local_GetCurrentFileInfoInternal(file, &s->cur_file_info, &s->cur_file_info_internal, NULL, 0, NULL, 0, NULL, 0); + s->current_file_ok = (err == UNZ_OK); return err; } extern z_off_t ZEXPORT unztell(unzFile file) { - unz64_s *s; - file_in_zip64_read_info_s *pfile_in_zip_read_info; + unz64_s* s; if (file == NULL) return UNZ_PARAMERROR; - s = (unz64_s *)file; - pfile_in_zip_read_info = s->pfile_in_zip_read; - if (pfile_in_zip_read_info == NULL) + s = (unz64_s*)file; + if (s->pfile_in_zip_read == NULL) return UNZ_PARAMERROR; - return (z_off_t)pfile_in_zip_read_info->stream.total_out; + return (z_off_t)s->pfile_in_zip_read->stream.total_out; } extern ZPOS64_T ZEXPORT unztell64(unzFile file) { - - unz64_s *s; - file_in_zip64_read_info_s *pfile_in_zip_read_info; + unz64_s* s; if (file == NULL) return (ZPOS64_T)-1; - s = (unz64_s *)file; - pfile_in_zip_read_info = s->pfile_in_zip_read; - - if (pfile_in_zip_read_info == NULL) + s = (unz64_s*)file; + if (s->pfile_in_zip_read == NULL) return (ZPOS64_T)-1; + return s->pfile_in_zip_read->total_out_64; +} - return pfile_in_zip_read_info->total_out_64; +extern int ZEXPORT unzseek(unzFile file, z_off_t offset, int origin) +{ + return unzseek64(file, (ZPOS64_T)offset, origin); } -extern int ZEXPORT unzeof(unzFile file) +extern int ZEXPORT unzseek64(unzFile file, ZPOS64_T offset, int origin) { - unz64_s *s; - file_in_zip64_read_info_s *pfile_in_zip_read_info; + unz64_s* s; + ZPOS64_T stream_pos_begin; + ZPOS64_T stream_pos_end; + int isWithinBuffer; + ZPOS64_T position; + if (file == NULL) return UNZ_PARAMERROR; - s = (unz64_s *)file; - pfile_in_zip_read_info = s->pfile_in_zip_read; - if (pfile_in_zip_read_info == NULL) + s = (unz64_s*)file; + + if (s->pfile_in_zip_read == NULL) + return UNZ_ERRNO; + if (s->pfile_in_zip_read->compression_method != 0) + return UNZ_ERRNO; + + if (origin == SEEK_SET) + position = offset; + else if (origin == SEEK_CUR) + position = s->pfile_in_zip_read->total_out_64 + offset; + else if (origin == SEEK_END) + position = s->cur_file_info.compressed_size + offset; + else return UNZ_PARAMERROR; - if (pfile_in_zip_read_info->rest_read_uncompressed == 0) + if (position > s->cur_file_info.compressed_size) + return UNZ_PARAMERROR; + + stream_pos_end = s->pfile_in_zip_read->pos_in_zipfile; + stream_pos_begin = stream_pos_end; + + if (stream_pos_begin > UNZ_BUFSIZE) + stream_pos_begin -= UNZ_BUFSIZE; + else + stream_pos_begin = 0; + + isWithinBuffer = s->pfile_in_zip_read->stream.avail_in != 0 && + (s->pfile_in_zip_read->rest_read_compressed != 0 || s->cur_file_info.compressed_size < UNZ_BUFSIZE) && + position >= stream_pos_begin && position < stream_pos_end; + + if (isWithinBuffer) + { + s->pfile_in_zip_read->stream.next_in += position - s->pfile_in_zip_read->total_out_64; + s->pfile_in_zip_read->stream.avail_in = (uInt)(stream_pos_end - position); + } + else + { + s->pfile_in_zip_read->stream.avail_in = 0; + s->pfile_in_zip_read->stream.next_in = 0; + + s->pfile_in_zip_read->pos_in_zipfile = s->pfile_in_zip_read->offset_local_extrafield + position; + s->pfile_in_zip_read->rest_read_compressed = s->cur_file_info.compressed_size - position; + } + + s->pfile_in_zip_read->rest_read_uncompressed -= (position - s->pfile_in_zip_read->total_out_64); + s->pfile_in_zip_read->stream.total_out = (uLong)position; + s->pfile_in_zip_read->total_out_64 = position; + + return UNZ_OK; +} + +extern int ZEXPORT unzeof(unzFile file) +{ + unz64_s* s; + if (file == NULL) + return UNZ_PARAMERROR; + s = (unz64_s*)file; + if (s->pfile_in_zip_read == NULL) + return UNZ_PARAMERROR; + if (s->pfile_in_zip_read->rest_read_uncompressed == 0) return 1; return 0; } - diff --git a/Zip/minizip/unzip.h b/Zip/minizip/unzip.h index 02c4c58193c186cdfe9e3ebae589d71a9f3cd80f..217c20297f609c273bac748d73ba041917523042 100755 --- a/Zip/minizip/unzip.h +++ b/Zip/minizip/unzip.h @@ -14,13 +14,9 @@ See the accompanying LICENSE file for the full text of the license. */ -#include "Common.h" - #ifndef _UNZ_H #define _UNZ_H -#define HAVE_AES - #ifdef __cplusplus extern "C" { #endif @@ -29,7 +25,7 @@ extern "C" { #include "zlib.h" #endif -#ifndef _ZLIBIOAPI_H +#ifndef _ZLIBIOAPI_H #include "ioapi.h" #endif @@ -48,7 +44,6 @@ typedef unzFile__ *unzFile; typedef voidp unzFile; #endif - #define UNZ_OK (0) #define UNZ_END_OF_LIST_OF_FILE (-100) #define UNZ_ERRNO (Z_ERRNO) @@ -58,6 +53,78 @@ typedef voidp unzFile; #define UNZ_INTERNALERROR (-104) #define UNZ_CRCERROR (-105) +/* tm_unz contain date/time info */ +typedef struct tm_unz_s +{ + uInt tm_sec; /* seconds after the minute - [0,59] */ + uInt tm_min; /* minutes after the hour - [0,59] */ + uInt tm_hour; /* hours since midnight - [0,23] */ + uInt tm_mday; /* day of the month - [1,31] */ + uInt tm_mon; /* months since January - [0,11] */ + uInt tm_year; /* years - [1980..2044] */ +} tm_unz; + +/* unz_global_info structure contain global data about the ZIPfile + These data comes from the end of central dir */ +typedef struct unz_global_info64_s +{ + ZPOS64_T number_entry; /* total number of entries in the central dir on this disk */ + uLong number_disk_with_CD; /* number the the disk with central dir, used for spanning ZIP*/ + uLong size_comment; /* size of the global comment of the zipfile */ +} unz_global_info64; + +typedef struct unz_global_info_s +{ + uLong number_entry; /* total number of entries in the central dir on this disk */ + uLong number_disk_with_CD; /* number the the disk with central dir, used for spanning ZIP*/ + uLong size_comment; /* size of the global comment of the zipfile */ +} unz_global_info; + +/* unz_file_info contain information about a file in the zipfile */ +typedef struct unz_file_info64_s +{ + uLong version; /* version made by 2 bytes */ + uLong version_needed; /* version needed to extract 2 bytes */ + uLong flag; /* general purpose bit flag 2 bytes */ + uLong compression_method; /* compression method 2 bytes */ + uLong dosDate; /* last mod file date in Dos fmt 4 bytes */ + uLong crc; /* crc-32 4 bytes */ + ZPOS64_T compressed_size; /* compressed size 8 bytes */ + ZPOS64_T uncompressed_size; /* uncompressed size 8 bytes */ + uLong size_filename; /* filename length 2 bytes */ + uLong size_file_extra; /* extra field length 2 bytes */ + uLong size_file_comment; /* file comment length 2 bytes */ + + uLong disk_num_start; /* disk number start 2 bytes */ + uLong internal_fa; /* internal file attributes 2 bytes */ + uLong external_fa; /* external file attributes 4 bytes */ + + tm_unz tmu_date; + ZPOS64_T disk_offset; + uLong size_file_extra_internal; +} unz_file_info64; + +typedef struct unz_file_info_s +{ + uLong version; /* version made by 2 bytes */ + uLong version_needed; /* version needed to extract 2 bytes */ + uLong flag; /* general purpose bit flag 2 bytes */ + uLong compression_method; /* compression method 2 bytes */ + uLong dosDate; /* last mod file date in Dos fmt 4 bytes */ + uLong crc; /* crc-32 4 bytes */ + uLong compressed_size; /* compressed size 4 bytes */ + uLong uncompressed_size; /* uncompressed size 4 bytes */ + uLong size_filename; /* filename length 2 bytes */ + uLong size_file_extra; /* extra field length 2 bytes */ + uLong size_file_comment; /* file comment length 2 bytes */ + + uLong disk_num_start; /* disk number start 2 bytes */ + uLong internal_fa; /* internal file attributes 2 bytes */ + uLong external_fa; /* external file attributes 4 bytes */ + + tm_unz tmu_date; + uLong disk_offset; +} unz_file_info; /***************************************************************************/ /* Opening and close a zip file */ @@ -236,6 +303,10 @@ extern z_off_t ZEXPORT unztell OF((unzFile file)); extern ZPOS64_T ZEXPORT unztell64 OF((unzFile file)); /* return current position in uncompressed data */ +extern int ZEXPORT unzseek OF((unzFile file, z_off_t offset, int origin)); +extern int ZEXPORT unzseek64 OF((unzFile file, ZPOS64_T offset, int origin)); +/* Seek within the uncompressed data if compression method is storage */ + extern int ZEXPORT unzeof OF((unzFile file)); /* return 1 if the end of file was reached, 0 elsewhere */ diff --git a/Zip/minizip/zip.c b/Zip/minizip/zip.c index b88bd88ddf790d115c3336a5403ee2d2c2b642c5..53fdf5a6c5f0e8d543c14b7fc542574f8fad4b53 100755 --- a/Zip/minizip/zip.c +++ b/Zip/minizip/zip.c @@ -12,7 +12,7 @@ This program is distributed under the terms of the same license as zlib. See the accompanying LICENSE file for the full text of the license. - */ +*/ #include #include @@ -27,7 +27,7 @@ # include #endif #ifdef NO_ERRNO_H -extern int errno; + extern int errno; #else # include #endif @@ -40,10 +40,10 @@ extern int errno; # define AES_VERSION (0x0001) # define AES_ENCRYPTIONMODE (0x03) -# include "aes.h" -# include "fileenc.h" -# include "prng.h" -# include "entropy.h" +# include "aes/aes.h" +# include "aes/fileenc.h" +# include "aes/prng.h" +# include "aes/entropy.h" #endif #ifndef NOCRYPT @@ -56,7 +56,7 @@ extern int errno; #endif /* compile with -Dlocal if your debugger can't find static symbols */ -#define SIZEDATA_INDATABLOCK (4096 - (4 * 4)) +#define SIZEDATA_INDATABLOCK (4096-(4*4)) #define DISKHEADERMAGIC (0x08074b50) #define LOCALHEADERMAGIC (0x04034b50) @@ -81,7 +81,7 @@ extern int errno; #endif #ifndef Z_BUFSIZE -# define Z_BUFSIZE (64 * 1024) +# define Z_BUFSIZE (64*1024) #endif #ifndef Z_MAXFILENAMEINZIP # define Z_MAXFILENAMEINZIP (256) @@ -91,7 +91,7 @@ extern int errno; # define ALLOC(size) (malloc(size)) #endif #ifndef TRYFREE -# define TRYFREE(p) {if (p) free(p); } +# define TRYFREE(p) {if (p) free(p);} #endif /* NOT sure that this work on ALL platform */ @@ -107,20 +107,23 @@ extern int errno; const char zip_copyright[] = " zip 1.01 Copyright 1998-2004 Gilles Vollant - http://www.winimage.com/zLibDll"; -typedef struct linkedlist_datablock_internal_s { - struct linkedlist_datablock_internal_s *next_datablock; - uLong avail_in_this_block; - uLong filled_in_this_block; - uLong unused; /* for future use and alignment */ +typedef struct linkedlist_datablock_internal_s +{ + struct linkedlist_datablock_internal_s* next_datablock; + uLong avail_in_this_block; + uLong filled_in_this_block; + uLong unused; /* for future use and alignment */ unsigned char data[SIZEDATA_INDATABLOCK]; } linkedlist_datablock_internal; -typedef struct linkedlist_data_s { - linkedlist_datablock_internal *first_block; - linkedlist_datablock_internal *last_block; +typedef struct linkedlist_data_s +{ + linkedlist_datablock_internal* first_block; + linkedlist_datablock_internal* last_block; } linkedlist_data; -typedef struct { +typedef struct +{ z_stream stream; /* zLib stream structure for inflate */ #ifdef HAVE_BZIP2 bz_stream bstream; /* bzLib stream structure for bziped */ @@ -129,36 +132,37 @@ typedef struct { fcrypt_ctx aes_ctx; prng_ctx aes_rng[1]; #endif - int stream_initialised; /* 1 is stream is initialized */ + int stream_initialised; /* 1 is stream is initialized */ uInt pos_in_buffered_data; /* last written byte in buffered_data */ ZPOS64_T pos_local_header; /* offset of the local header of the file currently writing */ - char *central_header; /* central header data for the current file */ + char* central_header; /* central header data for the current file */ uLong size_centralextra; uLong size_centralheader; /* size of the central header for cur file */ uLong size_centralextrafree; /* Extra bytes allocated to the central header but that are not used */ uLong size_comment; uLong flag; /* flag of the file currently writing */ - int method; /* compression method written to file.*/ - int compression_method; /* compression method to use */ - int raw; /* 1 for directly writing raw data */ + int method; /* compression method written to file.*/ + int compression_method; /* compression method to use */ + int raw; /* 1 for directly writing raw data */ Byte buffered_data[Z_BUFSIZE]; /* buffer contain compressed data to be writ*/ uLong dosDate; uLong crc32; - int zip64; /* Add ZIP64 extended information in the extra field */ + int zip64; /* Add ZIP64 extended information in the extra field */ uLong number_disk; /* number of current disk used for spanning ZIP */ ZPOS64_T pos_zip64extrainfo; ZPOS64_T total_compressed; ZPOS64_T total_uncompressed; #ifndef NOCRYPT unsigned long keys[3]; /* keys defining the pseudo-random sequence */ - const unsigned long *pcrc_32_tab; + const unsigned long* pcrc_32_tab; int crypt_header_size; #endif } curfile64_info; -typedef struct { +typedef struct +{ zlib_filefunc64_32_def z_filefunc; voidpf filestream; /* io structure of the zipfile */ voidpf filestream_with_CD; /* io structure of the zipfile with the central dir */ @@ -179,14 +183,15 @@ typedef struct { } zip64_internal; /* Allocate a new data block */ -local linkedlist_datablock_internal *allocate_new_datablock OF(()); -local linkedlist_datablock_internal *allocate_new_datablock() +local linkedlist_datablock_internal* allocate_new_datablock OF(()); +local linkedlist_datablock_internal* allocate_new_datablock() { - linkedlist_datablock_internal *ldi; + linkedlist_datablock_internal* ldi; - ldi = (linkedlist_datablock_internal *)ALLOC(sizeof(linkedlist_datablock_internal)); + ldi = (linkedlist_datablock_internal*)ALLOC(sizeof(linkedlist_datablock_internal)); - if (ldi != NULL) { + if (ldi != NULL) + { ldi->next_datablock = NULL; ldi->filled_in_this_block = 0; ldi->avail_in_this_block = SIZEDATA_INDATABLOCK; @@ -195,60 +200,64 @@ local linkedlist_datablock_internal *allocate_new_datablock() } /* Free data block in linked list */ -local void free_datablock OF((linkedlist_datablock_internal * ldi)); -local void free_datablock(linkedlist_datablock_internal *ldi) +local void free_datablock OF((linkedlist_datablock_internal* ldi)); +local void free_datablock(linkedlist_datablock_internal* ldi) { - while (ldi != NULL) { - linkedlist_datablock_internal *ldinext = ldi->next_datablock; + while (ldi != NULL) + { + linkedlist_datablock_internal* ldinext = ldi->next_datablock; TRYFREE(ldi); ldi = ldinext; } } /* Initialize linked list */ -local void init_linkedlist OF((linkedlist_data * ll)); -local void init_linkedlist(linkedlist_data *ll) +local void init_linkedlist OF((linkedlist_data* ll)); +local void init_linkedlist(linkedlist_data* ll) { ll->first_block = ll->last_block = NULL; } /* Free entire linked list and all data blocks */ -local void free_linkedlist OF((linkedlist_data * ll)); -local void free_linkedlist(linkedlist_data *ll) +local void free_linkedlist OF((linkedlist_data* ll)); +local void free_linkedlist(linkedlist_data* ll) { free_datablock(ll->first_block); ll->first_block = ll->last_block = NULL; } /* Add data to linked list data block */ -local int add_data_in_datablock OF((linkedlist_data * ll, const void *buf, uLong len)); -local int add_data_in_datablock(linkedlist_data *ll, const void *buf, uLong len) +local int add_data_in_datablock OF((linkedlist_data* ll, const void* buf, uLong len)); +local int add_data_in_datablock(linkedlist_data* ll, const void* buf, uLong len) { - linkedlist_datablock_internal *ldi; - const unsigned char *from_copy; + linkedlist_datablock_internal* ldi; + const unsigned char* from_copy; if (ll == NULL) return ZIP_INTERNALERROR; - if (ll->last_block == NULL) { + if (ll->last_block == NULL) + { ll->first_block = ll->last_block = allocate_new_datablock(); if (ll->first_block == NULL) return ZIP_INTERNALERROR; } ldi = ll->last_block; - from_copy = (unsigned char *)buf; + from_copy = (unsigned char*)buf; - while (len > 0) { + while (len > 0) + { uInt copy_this; uInt i; - unsigned char *to_copy; + unsigned char* to_copy; - if (ldi->avail_in_this_block == 0) { + if (ldi->avail_in_this_block == 0) + { ldi->next_datablock = allocate_new_datablock(); if (ldi->next_datablock == NULL) return ZIP_INTERNALERROR; - ldi = ldi->next_datablock; + ldi = ldi->next_datablock ; ll->last_block = ldi; } @@ -260,7 +269,7 @@ local int add_data_in_datablock(linkedlist_data *ll, const void *buf, uLong len) to_copy = &(ldi->data[ldi->filled_in_this_block]); for (i = 0; i < copy_this; i++) - *(to_copy + i) = *(from_copy + i); + *(to_copy+i) = *(from_copy+i); ldi->filled_in_this_block += copy_this; ldi->avail_in_this_block -= copy_this; @@ -270,25 +279,25 @@ local int add_data_in_datablock(linkedlist_data *ll, const void *buf, uLong len) return ZIP_OK; } -local uLong zip64local_TmzDateToDosDate OF((const tm_zip * ptm)); -local uLong zip64local_TmzDateToDosDate(const tm_zip *ptm) +local uLong zip64local_TmzDateToDosDate OF((const tm_zip* ptm)); +local uLong zip64local_TmzDateToDosDate(const tm_zip* ptm) { uLong year; #define zip64local_in_range(min, max, value) ((min) <= (value) && (value) <= (max)) /* Years supported: - * [00, 79] (assumed to be between 2000 and 2079) - * [80, 207] (assumed to be between 1980 and 2107, typical output of old + * [00, 79] (assumed to be between 2000 and 2079) + * [80, 207] (assumed to be between 1980 and 2107, typical output of old software that does 'year-1900' to get a double digit year) - * [1980, 2107] + * [1980, 2107] Due to the date format limitations, only years between 1980 and 2107 can be stored. - */ + */ if (!(zip64local_in_range(1980, 2107, ptm->tm_year) || zip64local_in_range(0, 207, ptm->tm_year)) || !zip64local_in_range(0, 11, ptm->tm_mon) || !zip64local_in_range(1, 31, ptm->tm_mday) || !zip64local_in_range(0, 23, ptm->tm_hour) || !zip64local_in_range(0, 59, ptm->tm_min) || !zip64local_in_range(0, 59, ptm->tm_sec)) - return 0; + return 0; #undef zip64local_in_range year = (uLong)ptm->tm_year; @@ -299,60 +308,65 @@ local uLong zip64local_TmzDateToDosDate(const tm_zip *ptm) else /* range [00, 79] */ year += 20; - return - (uLong)(((ptm->tm_mday) + (32 * (ptm->tm_mon + 1)) + (512 * year)) << 16) | + return (uLong)(((ptm->tm_mday) + (32 * (ptm->tm_mon+1)) + (512 * year)) << 16) | ((ptm->tm_sec / 2) + (32 * ptm->tm_min) + (2048 * (uLong)ptm->tm_hour)); } /* Inputs a long in LSB order to the given file: nbByte == 1, 2 ,4 or 8 (byte, short or long, ZPOS64_T) */ -local int zip64local_putValue OF((const zlib_filefunc64_32_def * pzlib_filefunc_def, voidpf filestream, - ZPOS64_T x, int nbByte)); -local int zip64local_putValue(const zlib_filefunc64_32_def *pzlib_filefunc_def, voidpf filestream, - ZPOS64_T x, int nbByte) +local int zip64local_putValue OF((const zlib_filefunc64_32_def* pzlib_filefunc_def, voidpf filestream, + ZPOS64_T x, int nbByte)); +local int zip64local_putValue (const zlib_filefunc64_32_def* pzlib_filefunc_def, voidpf filestream, + ZPOS64_T x, int nbByte) { unsigned char buf[8]; int n; - for (n = 0; n < nbByte; n++) { + for (n = 0; n < nbByte; n++) + { buf[n] = (unsigned char)(x & 0xff); x >>= 8; } - if (x != 0) { - /* data overflow - hack for ZIP64 (X Roche) */ - for (n = 0; n < nbByte; n++) { + if (x != 0) + { + /* Data overflow - hack for ZIP64 (X Roche) */ + for (n = 0; n < nbByte; n++) + { buf[n] = 0xff; } } - if (ZWRITE64(*pzlib_filefunc_def, filestream, buf, nbByte) != (uLong)nbByte) + if (ZWRITE64(*pzlib_filefunc_def,filestream,buf,nbByte)!=(uLong)nbByte) return ZIP_ERRNO; return ZIP_OK; } -local void zip64local_putValue_inmemory OF((void *dest, ZPOS64_T x, int nbByte)); -local void zip64local_putValue_inmemory(void *dest, ZPOS64_T x, int nbByte) +local void zip64local_putValue_inmemory OF((void* dest, ZPOS64_T x, int nbByte)); +local void zip64local_putValue_inmemory (void* dest, ZPOS64_T x, int nbByte) { - unsigned char *buf = (unsigned char *)dest; + unsigned char* buf =(unsigned char*)dest; int n; for (n = 0; n < nbByte; n++) { buf[n] = (unsigned char)(x & 0xff); x >>= 8; } - if (x != 0) { - /* data overflow - hack for ZIP64 */ - for (n = 0; n < nbByte; n++) { - buf[n] = 0xff; - } + if (x != 0) + { + /* data overflow - hack for ZIP64 */ + for (n = 0; n < nbByte; n++) + { + buf[n] = 0xff; + } } } -local int zip64local_getByte OF((const zlib_filefunc64_32_def * pzlib_filefunc_def, voidpf filestream, int *pi)); -local int zip64local_getByte(const zlib_filefunc64_32_def *pzlib_filefunc_def, voidpf filestream, int *pi) +local int zip64local_getByte OF((const zlib_filefunc64_32_def* pzlib_filefunc_def, voidpf filestream, int *pi)); +local int zip64local_getByte(const zlib_filefunc64_32_def* pzlib_filefunc_def,voidpf filestream,int* pi) { unsigned char c; - int err = (int)ZREAD64(*pzlib_filefunc_def, filestream, &c, 1); - if (err == 1) { + int err = (int)ZREAD64(*pzlib_filefunc_def, filestream, &c,1); + if (err == 1) + { *pi = (int)c; return ZIP_OK; } @@ -361,8 +375,8 @@ local int zip64local_getByte(const zlib_filefunc64_32_def *pzlib_filefunc_def, v return ZIP_EOF; } -local int zip64local_getShort OF((const zlib_filefunc64_32_def * pzlib_filefunc_def, voidpf filestream, uLong * pX)); -local int zip64local_getShort(const zlib_filefunc64_32_def *pzlib_filefunc_def, voidpf filestream, uLong *pX) +local int zip64local_getShort OF((const zlib_filefunc64_32_def* pzlib_filefunc_def, voidpf filestream, uLong *pX)); +local int zip64local_getShort (const zlib_filefunc64_32_def* pzlib_filefunc_def, voidpf filestream, uLong* pX) { uLong x; int i = 0; @@ -381,8 +395,8 @@ local int zip64local_getShort(const zlib_filefunc64_32_def *pzlib_filefunc_def, return err; } -local int zip64local_getLong OF((const zlib_filefunc64_32_def * pzlib_filefunc_def, voidpf filestream, uLong * pX)); -local int zip64local_getLong(const zlib_filefunc64_32_def *pzlib_filefunc_def, voidpf filestream, uLong *pX) +local int zip64local_getLong OF((const zlib_filefunc64_32_def* pzlib_filefunc_def, voidpf filestream, uLong *pX)); +local int zip64local_getLong (const zlib_filefunc64_32_def* pzlib_filefunc_def, voidpf filestream, uLong* pX) { uLong x; int i = 0; @@ -407,8 +421,8 @@ local int zip64local_getLong(const zlib_filefunc64_32_def *pzlib_filefunc_def, v return err; } -local int zip64local_getLong64 OF((const zlib_filefunc64_32_def * pzlib_filefunc_def, voidpf filestream, ZPOS64_T * pX)); -local int zip64local_getLong64(const zlib_filefunc64_32_def *pzlib_filefunc_def, voidpf filestream, ZPOS64_T *pX) +local int zip64local_getLong64 OF((const zlib_filefunc64_32_def* pzlib_filefunc_def, voidpf filestream, ZPOS64_T *pX)); +local int zip64local_getLong64 (const zlib_filefunc64_32_def* pzlib_filefunc_def, voidpf filestream, ZPOS64_T *pX) { ZPOS64_T x; int i = 0; @@ -447,13 +461,13 @@ local int zip64local_getLong64(const zlib_filefunc64_32_def *pzlib_filefunc_def, } /* Gets the amount of bytes left to write to the current disk for spanning archives */ -local int zipGetDiskSizeAvailable OF((zipFile file, ZPOS64_T * size_available)); +local int zipGetDiskSizeAvailable OF((zipFile file, ZPOS64_T *size_available)); local int zipGetDiskSizeAvailable(zipFile file, ZPOS64_T *size_available) { - zip64_internal *zi; + zip64_internal* zi; ZPOS64_T current_disk_size; - zi = (zip64_internal *)file; + zi = (zip64_internal*)file; ZSEEK64(zi->z_filefunc, zi->filestream, 0, ZLIB_FILEFUNC_SEEK_END); current_disk_size = ZTELL64(zi->z_filefunc, zi->filestream); *size_available = zi->disk_size - current_disk_size; @@ -464,10 +478,10 @@ local int zipGetDiskSizeAvailable(zipFile file, ZPOS64_T *size_available) local int zipGoToSpecificDisk OF((zipFile file, int number_disk, int open_existing)); local int zipGoToSpecificDisk(zipFile file, int number_disk, int open_existing) { - zip64_internal *zi; + zip64_internal* zi; int err = ZIP_OK; - zi = (zip64_internal *)file; + zi = (zip64_internal*)file; if (zi->disk_size == 0) return err; @@ -475,8 +489,8 @@ local int zipGoToSpecificDisk(zipFile file, int number_disk, int open_existing) ZCLOSE64(zi->z_filefunc, zi->filestream); zi->filestream = ZOPENDISK64(zi->z_filefunc, zi->filestream_with_CD, number_disk, (open_existing == 1) ? - (ZLIB_FILEFUNC_MODE_READ | ZLIB_FILEFUNC_MODE_WRITE | ZLIB_FILEFUNC_MODE_EXISTING) : - (ZLIB_FILEFUNC_MODE_READ | ZLIB_FILEFUNC_MODE_WRITE | ZLIB_FILEFUNC_MODE_CREATE)); + (ZLIB_FILEFUNC_MODE_READ | ZLIB_FILEFUNC_MODE_WRITE | ZLIB_FILEFUNC_MODE_EXISTING) : + (ZLIB_FILEFUNC_MODE_READ | ZLIB_FILEFUNC_MODE_WRITE | ZLIB_FILEFUNC_MODE_CREATE)); if (zi->filestream == NULL) err = ZIP_ERRNO; @@ -488,17 +502,17 @@ local int zipGoToSpecificDisk(zipFile file, int number_disk, int open_existing) local int zipGoToFirstDisk OF((zipFile file)); local int zipGoToFirstDisk(zipFile file) { - zip64_internal *zi; + zip64_internal* zi; int number_disk_next; int err = ZIP_OK; - zi = (zip64_internal *)file; + zi = (zip64_internal*)file; if (zi->disk_size == 0) return err; number_disk_next = 0; if (zi->number_disk_with_CD > 0) - number_disk_next = (int)zi->number_disk_with_CD - 1; + number_disk_next = zi->number_disk_with_CD - 1; err = zipGoToSpecificDisk(file, number_disk_next, (zi->append == APPEND_STATUS_ADDINZIP)); if ((err == ZIP_ERRNO) && (zi->append == APPEND_STATUS_ADDINZIP)) err = zipGoToSpecificDisk(file, number_disk_next, 0); @@ -512,19 +526,20 @@ local int zipGoToFirstDisk(zipFile file) local int zipGoToNextDisk OF((zipFile file)); local int zipGoToNextDisk(zipFile file) { - zip64_internal *zi; + zip64_internal* zi; ZPOS64_T size_available_in_disk; int err = ZIP_OK; int number_disk_next; - zi = (zip64_internal *)file; + zi = (zip64_internal*)file; if (zi->disk_size == 0) return err; - number_disk_next = (int)zi->number_disk + 1; + number_disk_next = zi->number_disk + 1; - do { + do + { err = zipGoToSpecificDisk(file, number_disk_next, (zi->append == APPEND_STATUS_ADDINZIP)); if ((err == ZIP_ERRNO) && (zi->append == APPEND_STATUS_ADDINZIP)) err = zipGoToSpecificDisk(file, number_disk_next, 0); @@ -537,29 +552,31 @@ local int zipGoToNextDisk(zipFile file) zi->number_disk_with_CD = zi->number_disk + 1; number_disk_next += 1; - } while (size_available_in_disk <= 0); + } + while (size_available_in_disk <= 0); return err; } /* Locate the Central directory of a zipfile (at the end, just before the global comment) */ -local ZPOS64_T zip64local_SearchCentralDir OF((const zlib_filefunc64_32_def * pzlib_filefunc_def, voidpf filestream)); -local ZPOS64_T zip64local_SearchCentralDir(const zlib_filefunc64_32_def *pzlib_filefunc_def, voidpf filestream) +local ZPOS64_T zip64local_SearchCentralDir OF((const zlib_filefunc64_32_def* pzlib_filefunc_def, voidpf filestream)); +local ZPOS64_T zip64local_SearchCentralDir(const zlib_filefunc64_32_def* pzlib_filefunc_def, voidpf filestream) { - unsigned char *buf; + unsigned char* buf; ZPOS64_T file_size; ZPOS64_T back_read = 4; - ZPOS64_T max_back = 0xffff; /* maximum size of global comment */ - ZPOS64_T pos_found = 0; + ZPOS64_T max_back=0xffff; /* maximum size of global comment */ + ZPOS64_T pos_found=0; uLong read_size; ZPOS64_T read_pos; int i; - buf = (unsigned char *)ALLOC(BUFREADCOMMENT + 4); + buf = (unsigned char*)ALLOC(BUFREADCOMMENT+4); if (buf == NULL) return 0; - if (ZSEEK64(*pzlib_filefunc_def, filestream, 0, ZLIB_FILEFUNC_SEEK_END) != 0) { + if (ZSEEK64(*pzlib_filefunc_def, filestream, 0, ZLIB_FILEFUNC_SEEK_END) != 0) + { TRYFREE(buf); return 0; } @@ -569,27 +586,29 @@ local ZPOS64_T zip64local_SearchCentralDir(const zlib_filefunc64_32_def *pzlib_f if (max_back > file_size) max_back = file_size; - while (back_read < max_back) { + while (back_read < max_back) + { if (back_read + BUFREADCOMMENT > max_back) back_read = max_back; else back_read += BUFREADCOMMENT; - read_pos = file_size - back_read; - read_size = ((BUFREADCOMMENT + 4) < (file_size - read_pos)) ? - (BUFREADCOMMENT + 4) : (uLong)(file_size - read_pos); + read_pos = file_size-back_read; + read_size = ((BUFREADCOMMENT+4) < (file_size-read_pos)) ? + (BUFREADCOMMENT+4) : (uLong)(file_size-read_pos); if (ZSEEK64(*pzlib_filefunc_def, filestream, read_pos, ZLIB_FILEFUNC_SEEK_SET) != 0) break; if (ZREAD64(*pzlib_filefunc_def, filestream, buf, read_size) != read_size) break; - for (i = (int)read_size - 3; (i--) > 0; ) - if ((*(buf + i)) == (ENDHEADERMAGIC & 0xff) && - (*(buf + i + 1)) == (ENDHEADERMAGIC >> 8 & 0xff) && - (*(buf + i + 2)) == (ENDHEADERMAGIC >> 16 & 0xff) && - (*(buf + i + 3)) == (ENDHEADERMAGIC >> 24 & 0xff)) { - pos_found = read_pos + i; + for (i = (int)read_size-3; (i--) > 0;) + if ((*(buf+i)) == (ENDHEADERMAGIC & 0xff) && + (*(buf+i+1)) == (ENDHEADERMAGIC >> 8 & 0xff) && + (*(buf+i+2)) == (ENDHEADERMAGIC >> 16 & 0xff) && + (*(buf+i+3)) == (ENDHEADERMAGIC >> 24 & 0xff)) + { + pos_found = read_pos+i; break; } @@ -601,10 +620,10 @@ local ZPOS64_T zip64local_SearchCentralDir(const zlib_filefunc64_32_def *pzlib_f } /* Locate the Central directory 64 of a zipfile (at the end, just before the global comment) */ -local ZPOS64_T zip64local_SearchCentralDir64 OF((const zlib_filefunc64_32_def * pzlib_filefunc_def, voidpf filestream, - const ZPOS64_T endcentraloffset)); -local ZPOS64_T zip64local_SearchCentralDir64(const zlib_filefunc64_32_def *pzlib_filefunc_def, voidpf filestream, - const ZPOS64_T endcentraloffset) +local ZPOS64_T zip64local_SearchCentralDir64 OF((const zlib_filefunc64_32_def* pzlib_filefunc_def, voidpf filestream, + const ZPOS64_T endcentraloffset)); +local ZPOS64_T zip64local_SearchCentralDir64(const zlib_filefunc64_32_def* pzlib_filefunc_def, voidpf filestream, + const ZPOS64_T endcentraloffset) { ZPOS64_T offset; uLong uL; @@ -613,24 +632,24 @@ local ZPOS64_T zip64local_SearchCentralDir64(const zlib_filefunc64_32_def *pzlib if (ZSEEK64(*pzlib_filefunc_def, filestream, endcentraloffset - SIZECENTRALHEADERLOCATOR, ZLIB_FILEFUNC_SEEK_SET) != 0) return 0; - /* read locator signature */ + /* Read locator signature */ if (zip64local_getLong(pzlib_filefunc_def, filestream, &uL) != ZIP_OK) return 0; if (uL != ZIP64ENDLOCHEADERMAGIC) return 0; - /* number of the disk with the start of the zip64 end of central directory */ + /* Number of the disk with the start of the zip64 end of central directory */ if (zip64local_getLong(pzlib_filefunc_def, filestream, &uL) != ZIP_OK) return 0; - /* relative offset of the zip64 end of central directory record */ + /* Relative offset of the zip64 end of central directory record */ if (zip64local_getLong64(pzlib_filefunc_def, filestream, &offset) != ZIP_OK) return 0; - /* total number of disks */ + /* Total number of disks */ if (zip64local_getLong(pzlib_filefunc_def, filestream, &uL) != ZIP_OK) return 0; /* Goto end of central directory record */ - if (ZSEEK64(*pzlib_filefunc_def, filestream, offset, ZLIB_FILEFUNC_SEEK_SET) != 0) + if (ZSEEK64(*pzlib_filefunc_def,filestream, offset, ZLIB_FILEFUNC_SEEK_SET) != 0) return 0; - /* the signature */ + /* The signature */ if (zip64local_getLong(pzlib_filefunc_def, filestream, &uL) != ZIP_OK) return 0; if (uL != ZIP64ENDHEADERMAGIC) @@ -639,23 +658,23 @@ local ZPOS64_T zip64local_SearchCentralDir64(const zlib_filefunc64_32_def *pzlib return offset; } -extern zipFile ZEXPORT zipOpen4(const void *pathname, int append, ZPOS64_T disk_size, zipcharpc *globalcomment, - zlib_filefunc64_32_def *pzlib_filefunc64_32_def) +extern zipFile ZEXPORT zipOpen4(const void *pathname, int append, ZPOS64_T disk_size, const char ** globalcomment, + zlib_filefunc64_32_def* pzlib_filefunc64_32_def) { zip64_internal ziinit; - zip64_internal *zi; + zip64_internal* zi; #ifndef NO_ADDFILEINEXISTINGZIP ZPOS64_T byte_before_the_zipfile; /* byte before the zipfile, (>0 for sfx)*/ - ZPOS64_T size_central_dir = 0; /* size of the central directory */ - ZPOS64_T offset_central_dir = 0; /* offset of start of central directory */ - ZPOS64_T number_entry_CD = 0; /* total number of entries in the central dir */ + ZPOS64_T size_central_dir; /* size of the central directory */ + ZPOS64_T offset_central_dir; /* offset of start of central directory */ + ZPOS64_T number_entry_CD; /* total number of entries in the central dir */ ZPOS64_T number_entry; ZPOS64_T central_pos; ZPOS64_T size_central_dir_to_read; uLong uL; - uLong size_comment = 0; + uLong size_comment; size_t buf_size = SIZEDATA_INDATABLOCK; - void *buf_read; + void* buf_read; #endif int err = ZIP_OK; int mode; @@ -676,12 +695,13 @@ extern zipFile ZEXPORT zipOpen4(const void *pathname, int append, ZPOS64_T disk_ if (ziinit.filestream == NULL) return NULL; - if (append == APPEND_STATUS_CREATEAFTER) { + if (append == APPEND_STATUS_CREATEAFTER) + { /* Don't support spanning ZIP with APPEND_STATUS_CREATEAFTER */ if (disk_size > 0) return NULL; - ZSEEK64(ziinit.z_filefunc, ziinit.filestream, 0, SEEK_END); + ZSEEK64(ziinit.z_filefunc,ziinit.filestream,0,SEEK_END); } ziinit.filestream_with_CD = ziinit.filestream; @@ -689,153 +709,161 @@ extern zipFile ZEXPORT zipOpen4(const void *pathname, int append, ZPOS64_T disk_ ziinit.number_disk = 0; ziinit.number_disk_with_CD = 0; ziinit.disk_size = disk_size; - ziinit.begin_pos = ZTELL64(ziinit.z_filefunc, ziinit.filestream); + ziinit.begin_pos = ZTELL64(ziinit.z_filefunc,ziinit.filestream); ziinit.in_opened_file_inzip = 0; ziinit.ci.stream_initialised = 0; ziinit.number_entry = 0; ziinit.add_position_when_writting_offset = 0; init_linkedlist(&(ziinit.central_dir)); - zi = (zip64_internal *)ALLOC(sizeof(zip64_internal)); - if (zi == NULL) { - ZCLOSE64(ziinit.z_filefunc, ziinit.filestream); + zi = (zip64_internal*)ALLOC(sizeof(zip64_internal)); + if (zi == NULL) + { + ZCLOSE64(ziinit.z_filefunc,ziinit.filestream); return NULL; } #ifndef NO_ADDFILEINEXISTINGZIP /* Add file in a zipfile */ ziinit.globalcomment = NULL; - if (append == APPEND_STATUS_ADDINZIP) { + if (append == APPEND_STATUS_ADDINZIP) + { /* Read and Cache Central Directory Records */ - central_pos = zip64local_SearchCentralDir(&ziinit.z_filefunc, ziinit.filestream); - /* disable to allow appending to empty ZIP archive (must be standard zip, not zip64) + central_pos = zip64local_SearchCentralDir(&ziinit.z_filefunc,ziinit.filestream); + /* Disable to allow appending to empty ZIP archive (must be standard zip, not zip64) if (central_pos == 0) err = ZIP_ERRNO; - */ + */ - if (err == ZIP_OK) { - /* read end of central directory info */ - if (ZSEEK64(ziinit.z_filefunc, ziinit.filestream, central_pos, ZLIB_FILEFUNC_SEEK_SET) != 0) + if (err == ZIP_OK) + { + /* Read end of central directory info */ + if (ZSEEK64(ziinit.z_filefunc, ziinit.filestream, central_pos,ZLIB_FILEFUNC_SEEK_SET) != 0) err = ZIP_ERRNO; - /* the signature, already checked */ + /* The signature, already checked */ if (zip64local_getLong(&ziinit.z_filefunc, ziinit.filestream, &uL) != ZIP_OK) err = ZIP_ERRNO; - /* number of this disk */ + /* Number of this disk */ if (zip64local_getShort(&ziinit.z_filefunc, ziinit.filestream, &ziinit.number_disk) != ZIP_OK) err = ZIP_ERRNO; - /* number of the disk with the start of the central directory */ + /* Number of the disk with the start of the central directory */ if (zip64local_getShort(&ziinit.z_filefunc, ziinit.filestream, &ziinit.number_disk_with_CD) != ZIP_OK) err = ZIP_ERRNO; - /* total number of entries in the central dir on this disk */ + /* Total number of entries in the central dir on this disk */ number_entry = 0; if (zip64local_getShort(&ziinit.z_filefunc, ziinit.filestream, &uL) != ZIP_OK) err = ZIP_ERRNO; else number_entry = uL; - /* total number of entries in the central dir */ + /* Total number of entries in the central dir */ number_entry_CD = 0; if (zip64local_getShort(&ziinit.z_filefunc, ziinit.filestream, &uL) != ZIP_OK) err = ZIP_ERRNO; else number_entry_CD = uL; - if (number_entry_CD != number_entry) + if (number_entry_CD!=number_entry) err = ZIP_BADZIPFILE; - /* size of the central directory */ + /* Size of the central directory */ size_central_dir = 0; if (zip64local_getLong(&ziinit.z_filefunc, ziinit.filestream, &uL) != ZIP_OK) err = ZIP_ERRNO; else size_central_dir = uL; - /* offset of start of central directory with respect to the starting disk number */ + /* Offset of start of central directory with respect to the starting disk number */ offset_central_dir = 0; if (zip64local_getLong(&ziinit.z_filefunc, ziinit.filestream, &uL) != ZIP_OK) err = ZIP_ERRNO; else offset_central_dir = uL; - /* zipfile global comment length */ + /* Zipfile global comment length */ if (zip64local_getShort(&ziinit.z_filefunc, ziinit.filestream, &size_comment) != ZIP_OK) err = ZIP_ERRNO; - if ((err == ZIP_OK) && ((number_entry_CD == 0xffff) || (offset_central_dir == 0xffffffff))) { + if ((err == ZIP_OK) && ((number_entry_CD == 0xffff) || (offset_central_dir == 0xffffffff))) + { /* Format should be Zip64, as the central directory or file size is too large */ central_pos = zip64local_SearchCentralDir64(&ziinit.z_filefunc, ziinit.filestream, central_pos); - if (central_pos) { + if (central_pos) + { ZPOS64_T sizeEndOfCentralDirectory; if (ZSEEK64(ziinit.z_filefunc, ziinit.filestream, central_pos, ZLIB_FILEFUNC_SEEK_SET) != 0) err = ZIP_ERRNO; - /* the signature, already checked */ + /* The signature, already checked */ if (zip64local_getLong(&ziinit.z_filefunc, ziinit.filestream, &uL) != ZIP_OK) err = ZIP_ERRNO; - /* size of zip64 end of central directory record */ + /* Size of zip64 end of central directory record */ if (zip64local_getLong64(&ziinit.z_filefunc, ziinit.filestream, &sizeEndOfCentralDirectory) != ZIP_OK) err = ZIP_ERRNO; - /* version made by */ + /* Version made by */ if (zip64local_getShort(&ziinit.z_filefunc, ziinit.filestream, &uL) != ZIP_OK) err = ZIP_ERRNO; - /* version needed to extract */ + /* Version needed to extract */ if (zip64local_getShort(&ziinit.z_filefunc, ziinit.filestream, &uL) != ZIP_OK) err = ZIP_ERRNO; - /* number of this disk */ + /* Number of this disk */ if (zip64local_getLong(&ziinit.z_filefunc, ziinit.filestream, &ziinit.number_disk) != ZIP_OK) err = ZIP_ERRNO; - /* number of the disk with the start of the central directory */ + /* Number of the disk with the start of the central directory */ if (zip64local_getLong(&ziinit.z_filefunc, ziinit.filestream, &ziinit.number_disk_with_CD) != ZIP_OK) err = ZIP_ERRNO; - /* total number of entries in the central directory on this disk */ + /* Total number of entries in the central directory on this disk */ if (zip64local_getLong64(&ziinit.z_filefunc, ziinit.filestream, &number_entry) != ZIP_OK) err = ZIP_ERRNO; - /* total number of entries in the central directory */ + /* Total number of entries in the central directory */ if (zip64local_getLong64(&ziinit.z_filefunc, ziinit.filestream, &number_entry_CD) != ZIP_OK) err = ZIP_ERRNO; - if (number_entry_CD != number_entry) + if (number_entry_CD!=number_entry) err = ZIP_BADZIPFILE; - /* size of the central directory */ + /* Size of the central directory */ if (zip64local_getLong64(&ziinit.z_filefunc, ziinit.filestream, &size_central_dir) != ZIP_OK) err = ZIP_ERRNO; - /* offset of start of central directory with respect to the starting disk number */ + /* Offset of start of central directory with respect to the starting disk number */ if (zip64local_getLong64(&ziinit.z_filefunc, ziinit.filestream, &offset_central_dir) != ZIP_OK) err = ZIP_ERRNO; - } else + } + else err = ZIP_BADZIPFILE; - } + } } - if ((err == ZIP_OK) && (central_pos < offset_central_dir + size_central_dir)) + if ((err == ZIP_OK) && (central_pos 0) { - ziinit.globalcomment = (char *)ALLOC(size_comment + 1); - if (ziinit.globalcomment) { + if (size_comment > 0) + { + ziinit.globalcomment = (char*)ALLOC(size_comment+1); + if (ziinit.globalcomment) + { size_comment = ZREAD64(ziinit.z_filefunc, ziinit.filestream, ziinit.globalcomment, size_comment); ziinit.globalcomment[size_comment] = 0; } } - byte_before_the_zipfile = central_pos - (offset_central_dir + size_central_dir); + byte_before_the_zipfile = central_pos - (offset_central_dir+size_central_dir); ziinit.add_position_when_writting_offset = byte_before_the_zipfile; /* Store central directory in memory */ size_central_dir_to_read = size_central_dir; buf_size = SIZEDATA_INDATABLOCK; - buf_read = (void *)ALLOC(buf_size); - if (buf_read == NULL) - err = ZIP_INTERNALERROR; + buf_read = (void*)ALLOC(buf_size); if (ZSEEK64(ziinit.z_filefunc, ziinit.filestream, - offset_central_dir + byte_before_the_zipfile, ZLIB_FILEFUNC_SEEK_SET) != 0) + offset_central_dir + byte_before_the_zipfile, ZLIB_FILEFUNC_SEEK_SET) != 0) err = ZIP_ERRNO; - while ((size_central_dir_to_read > 0) && (err == ZIP_OK)) { + while ((size_central_dir_to_read > 0) && (err == ZIP_OK)) + { ZPOS64_T read_this = SIZEDATA_INDATABLOCK; if (read_this > size_central_dir_to_read) read_this = size_central_dir_to_read; @@ -854,7 +882,7 @@ extern zipFile ZEXPORT zipOpen4(const void *pathname, int append, ZPOS64_T disk_ ziinit.number_entry = number_entry_CD; if (ZSEEK64(ziinit.z_filefunc, ziinit.filestream, - offset_central_dir + byte_before_the_zipfile, ZLIB_FILEFUNC_SEEK_SET) != 0) + offset_central_dir+byte_before_the_zipfile, ZLIB_FILEFUNC_SEEK_SET) != 0) err = ZIP_ERRNO; } @@ -862,7 +890,8 @@ extern zipFile ZEXPORT zipOpen4(const void *pathname, int append, ZPOS64_T disk_ *globalcomment = ziinit.globalcomment; #endif - if (err != ZIP_OK) { + if (err != ZIP_OK) + { #ifndef NO_ADDFILEINEXISTINGZIP TRYFREE(ziinit.globalcomment); #endif @@ -872,24 +901,26 @@ extern zipFile ZEXPORT zipOpen4(const void *pathname, int append, ZPOS64_T disk_ *zi = ziinit; zipGoToFirstDisk((zipFile)zi); - return (zipFile)zi; + return(zipFile)zi; } -extern zipFile ZEXPORT zipOpen2(const char *pathname, int append, zipcharpc *globalcomment, - zlib_filefunc_def *pzlib_filefunc32_def) +extern zipFile ZEXPORT zipOpen2(const char *pathname, int append, const char ** globalcomment, + zlib_filefunc_def* pzlib_filefunc32_def) { - if (pzlib_filefunc32_def != NULL) { + if (pzlib_filefunc32_def != NULL) + { zlib_filefunc64_32_def zlib_filefunc64_32_def_fill; - fill_zlib_filefunc64_32_def_from_filefunc32(&zlib_filefunc64_32_def_fill, pzlib_filefunc32_def); + fill_zlib_filefunc64_32_def_from_filefunc32(&zlib_filefunc64_32_def_fill,pzlib_filefunc32_def); return zipOpen4(pathname, append, 0, globalcomment, &zlib_filefunc64_32_def_fill); } return zipOpen4(pathname, append, 0, globalcomment, NULL); } -extern zipFile ZEXPORT zipOpen2_64(const void *pathname, int append, zipcharpc *globalcomment, - zlib_filefunc64_def *pzlib_filefunc_def) +extern zipFile ZEXPORT zipOpen2_64(const void *pathname, int append, const char ** globalcomment, + zlib_filefunc64_def* pzlib_filefunc_def) { - if (pzlib_filefunc_def != NULL) { + if (pzlib_filefunc_def != NULL) + { zlib_filefunc64_32_def zlib_filefunc64_32_def_fill; zlib_filefunc64_32_def_fill.zfile_func64 = *pzlib_filefunc_def; zlib_filefunc64_32_def_fill.ztell32_file = NULL; @@ -899,21 +930,23 @@ extern zipFile ZEXPORT zipOpen2_64(const void *pathname, int append, zipcharpc * return zipOpen4(pathname, append, 0, globalcomment, NULL); } -extern zipFile ZEXPORT zipOpen3(const char *pathname, int append, ZPOS64_T disk_size, zipcharpc *globalcomment, - zlib_filefunc_def *pzlib_filefunc32_def) +extern zipFile ZEXPORT zipOpen3(const char *pathname, int append, ZPOS64_T disk_size, const char ** globalcomment, + zlib_filefunc_def* pzlib_filefunc32_def) { - if (pzlib_filefunc32_def != NULL) { + if (pzlib_filefunc32_def != NULL) + { zlib_filefunc64_32_def zlib_filefunc64_32_def_fill; - fill_zlib_filefunc64_32_def_from_filefunc32(&zlib_filefunc64_32_def_fill, pzlib_filefunc32_def); + fill_zlib_filefunc64_32_def_from_filefunc32(&zlib_filefunc64_32_def_fill,pzlib_filefunc32_def); return zipOpen4(pathname, append, disk_size, globalcomment, &zlib_filefunc64_32_def_fill); } return zipOpen4(pathname, append, disk_size, globalcomment, NULL); } -extern zipFile ZEXPORT zipOpen3_64(const void *pathname, int append, ZPOS64_T disk_size, zipcharpc *globalcomment, - zlib_filefunc64_def *pzlib_filefunc_def) +extern zipFile ZEXPORT zipOpen3_64(const void *pathname, int append, ZPOS64_T disk_size, const char ** globalcomment, + zlib_filefunc64_def* pzlib_filefunc_def) { - if (pzlib_filefunc_def != NULL) { + if (pzlib_filefunc_def != NULL) + { zlib_filefunc64_32_def zlib_filefunc64_32_def_fill; zlib_filefunc64_32_def_fill.zfile_func64 = *pzlib_filefunc_def; zlib_filefunc64_32_def_fill.ztell32_file = NULL; @@ -923,22 +956,22 @@ extern zipFile ZEXPORT zipOpen3_64(const void *pathname, int append, ZPOS64_T di return zipOpen4(pathname, append, disk_size, globalcomment, NULL); } -extern zipFile ZEXPORT zipOpen(const char *pathname, int append) +extern zipFile ZEXPORT zipOpen(const char* pathname, int append) { - return zipOpen3((const void *)pathname, append, 0, NULL, NULL); + return zipOpen3((const void*)pathname,append,0,NULL,NULL); } -extern zipFile ZEXPORT zipOpen64(const void *pathname, int append) +extern zipFile ZEXPORT zipOpen64(const void* pathname, int append) { - return zipOpen3(pathname, append, 0, NULL, NULL); + return zipOpen3(pathname,append,0,NULL,NULL); } -extern int ZEXPORT zipOpenNewFileInZip4_64(zipFile file, const char *filename, const zip_fileinfo *zipfi, - const void *extrafield_local, uInt size_extrafield_local, const void *extrafield_global, - uInt size_extrafield_global, const char *comment, int method, int level, int raw, int windowBits, int memLevel, - int strategy, const char *password, uLong crcForCrypting, uLong versionMadeBy, uLong flagBase, int zip64) +extern int ZEXPORT zipOpenNewFileInZip4_64(zipFile file, const char* filename, const zip_fileinfo* zipfi, + const void* extrafield_local, uInt size_extrafield_local, const void* extrafield_global, + uInt size_extrafield_global, const char* comment, int method, int level, int raw, int windowBits, int memLevel, + int strategy, const char* password, uLong crcForCrypting, uLong versionMadeBy, uLong flagBase, int zip64) { - zip64_internal *zi; + zip64_internal* zi; uInt size_filename; uInt size_comment = 0; uInt i; @@ -962,10 +995,11 @@ extern int ZEXPORT zipOpenNewFileInZip4_64(zipFile file, const char *filename, c (method != Z_DEFLATED)) return ZIP_PARAMERROR; - zi = (zip64_internal *)file; + zi = (zip64_internal*)file; - if (zi->in_opened_file_inzip == 1) { - err = zipCloseFileInZip(file); + if (zi->in_opened_file_inzip == 1) + { + err = zipCloseFileInZip (file); if (err != ZIP_OK) return err; } @@ -979,7 +1013,8 @@ extern int ZEXPORT zipOpenNewFileInZip4_64(zipFile file, const char *filename, c if (zipfi == NULL) zi->ci.dosDate = 0; - else { + else + { if (zipfi->dosDate != 0) zi->ci.dosDate = zipfi->dosDate; else @@ -999,14 +1034,16 @@ extern int ZEXPORT zipOpenNewFileInZip4_64(zipFile file, const char *filename, c zi->ci.flag |= 4; if (level == 1) zi->ci.flag |= 6; - if (password != NULL) { + if (password != NULL) + { zi->ci.flag |= 1; #ifdef HAVE_AES zi->ci.method = AES_METHOD; #endif } - if (zi->disk_size > 0) { + if (zi->disk_size > 0) + { if ((zi->number_disk == 0) && (zi->number_entry == 0)) err = zip64local_putValue(&zi->z_filefunc, zi->filestream, (uLong)DISKHEADERMAGIC, 4); @@ -1032,50 +1069,47 @@ extern int ZEXPORT zipOpenNewFileInZip4_64(zipFile file, const char *filename, c if (zi->ci.method == AES_METHOD) zi->ci.size_centralextrafree += 11; /* Extra space reserved for AES extra info */ #endif - zi->ci.central_header = (char *)ALLOC((uInt)zi->ci.size_centralheader + zi->ci.size_centralextrafree + size_comment); - if (zi->ci.central_header == NULL) - return ZIP_INTERNALERROR; - + zi->ci.central_header = (char*)ALLOC((uInt)zi->ci.size_centralheader + zi->ci.size_centralextrafree + size_comment); zi->ci.number_disk = zi->number_disk; /* Write central directory header */ zip64local_putValue_inmemory(zi->ci.central_header, (uLong)CENTRALHEADERMAGIC, 4); - zip64local_putValue_inmemory(zi->ci.central_header + 4, (uLong)versionMadeBy, 2); - zip64local_putValue_inmemory(zi->ci.central_header + 6, (uLong)20, 2); - zip64local_putValue_inmemory(zi->ci.central_header + 8, (uLong)zi->ci.flag, 2); - zip64local_putValue_inmemory(zi->ci.central_header + 10, (uLong)zi->ci.method, 2); - zip64local_putValue_inmemory(zi->ci.central_header + 12, (uLong)zi->ci.dosDate, 4); - zip64local_putValue_inmemory(zi->ci.central_header + 16, (uLong)0, 4); /*crc*/ - zip64local_putValue_inmemory(zi->ci.central_header + 20, (uLong)0, 4); /*compr size*/ - zip64local_putValue_inmemory(zi->ci.central_header + 24, (uLong)0, 4); /*uncompr size*/ - zip64local_putValue_inmemory(zi->ci.central_header + 28, (uLong)size_filename, 2); - zip64local_putValue_inmemory(zi->ci.central_header + 30, (uLong)size_extrafield_global, 2); - zip64local_putValue_inmemory(zi->ci.central_header + 32, (uLong)size_comment, 2); - zip64local_putValue_inmemory(zi->ci.central_header + 34, (uLong)zi->ci.number_disk, 2); /*disk nm start*/ + zip64local_putValue_inmemory(zi->ci.central_header+4, (uLong)versionMadeBy, 2); + zip64local_putValue_inmemory(zi->ci.central_header+6, (uLong)20, 2); + zip64local_putValue_inmemory(zi->ci.central_header+8, (uLong)zi->ci.flag, 2); + zip64local_putValue_inmemory(zi->ci.central_header+10, (uLong)zi->ci.method, 2); + zip64local_putValue_inmemory(zi->ci.central_header+12, (uLong)zi->ci.dosDate, 4); + zip64local_putValue_inmemory(zi->ci.central_header+16, (uLong)0, 4); /*crc*/ + zip64local_putValue_inmemory(zi->ci.central_header+20, (uLong)0, 4); /*compr size*/ + zip64local_putValue_inmemory(zi->ci.central_header+24, (uLong)0, 4); /*uncompr size*/ + zip64local_putValue_inmemory(zi->ci.central_header+28, (uLong)size_filename, 2); + zip64local_putValue_inmemory(zi->ci.central_header+30, (uLong)size_extrafield_global, 2); + zip64local_putValue_inmemory(zi->ci.central_header+32, (uLong)size_comment, 2); + zip64local_putValue_inmemory(zi->ci.central_header+34, (uLong)zi->ci.number_disk, 2); /*disk nm start*/ if (zipfi == NULL) - zip64local_putValue_inmemory(zi->ci.central_header + 36, (uLong)0, 2); + zip64local_putValue_inmemory(zi->ci.central_header+36, (uLong)0, 2); else - zip64local_putValue_inmemory(zi->ci.central_header + 36, (uLong)zipfi->internal_fa, 2); + zip64local_putValue_inmemory(zi->ci.central_header+36, (uLong)zipfi->internal_fa, 2); if (zipfi == NULL) - zip64local_putValue_inmemory(zi->ci.central_header + 38, (uLong)0, 4); + zip64local_putValue_inmemory(zi->ci.central_header+38, (uLong)0, 4); else - zip64local_putValue_inmemory(zi->ci.central_header + 38, (uLong)zipfi->external_fa, 4); + zip64local_putValue_inmemory(zi->ci.central_header+38, (uLong)zipfi->external_fa, 4); if (zi->ci.pos_local_header >= 0xffffffff) - zip64local_putValue_inmemory(zi->ci.central_header + 42, (uLong)0xffffffff, 4); + zip64local_putValue_inmemory(zi->ci.central_header+42, (uLong)0xffffffff, 4); else - zip64local_putValue_inmemory(zi->ci.central_header + 42, - (uLong)zi->ci.pos_local_header - zi->add_position_when_writting_offset, 4); + zip64local_putValue_inmemory(zi->ci.central_header+42, + (uLong)zi->ci.pos_local_header - zi->add_position_when_writting_offset, 4); for (i = 0; i < size_filename; i++) - zi->ci.central_header[SIZECENTRALHEADER + i] = filename[i]; + zi->ci.central_header[SIZECENTRALHEADER+i] = filename[i]; for (i = 0; i < size_extrafield_global; i++) - zi->ci.central_header[SIZECENTRALHEADER + size_filename + i] = - ((const char *)extrafield_global)[i]; + zi->ci.central_header[SIZECENTRALHEADER+size_filename+i] = + ((const char*)extrafield_global)[i]; /* Store comment at the end for later repositioning */ for (i = 0; i < size_comment; i++) - zi->ci.central_header[zi->ci.size_centralheader + - zi->ci.size_centralextrafree + i] = comment[i]; + zi->ci.central_header[zi->ci.size_centralheader+ + zi->ci.size_centralextrafree+i] = comment[i]; if (zi->ci.central_header == NULL) return ZIP_INTERNALERROR; @@ -1088,7 +1122,8 @@ extern int ZEXPORT zipOpenNewFileInZip4_64(zipFile file, const char *filename, c /* Write the local header */ err = zip64local_putValue(&zi->z_filefunc, zi->filestream, (uLong)LOCALHEADERMAGIC, 4); - if (err == ZIP_OK) { + if (err == ZIP_OK) + { if (zi->ci.zip64) err = zip64local_putValue(&zi->z_filefunc, zi->filestream, (uLong)45, 2); /* version needed to extract */ else @@ -1105,13 +1140,15 @@ extern int ZEXPORT zipOpenNewFileInZip4_64(zipFile file, const char *filename, c if (err == ZIP_OK) err = zip64local_putValue(&zi->z_filefunc, zi->filestream, (uLong)0, 4); /* crc 32, unknown */ - if (err == ZIP_OK) { + if (err == ZIP_OK) + { if (zi->ci.zip64) err = zip64local_putValue(&zi->z_filefunc, zi->filestream, (uLong)0xFFFFFFFF, 4); /* compressed size, unknown */ else err = zip64local_putValue(&zi->z_filefunc, zi->filestream, (uLong)0, 4); /* compressed size, unknown */ } - if (err == ZIP_OK) { + if (err == ZIP_OK) + { if (zi->ci.zip64) /* uncompressed size, unknown */ err = zip64local_putValue(&zi->z_filefunc, zi->filestream, (uLong)0xFFFFFFFF, 4); else /* uncompressed size, unknown */ @@ -1119,7 +1156,8 @@ extern int ZEXPORT zipOpenNewFileInZip4_64(zipFile file, const char *filename, c } if (err == ZIP_OK) err = zip64local_putValue(&zi->z_filefunc, zi->filestream, (uLong)size_filename, 2); - if (err == ZIP_OK) { + if (err == ZIP_OK) + { ZPOS64_T size_extrafield = size_extrafield_local; if (zi->ci.zip64) size_extrafield += 20; @@ -1127,19 +1165,22 @@ extern int ZEXPORT zipOpenNewFileInZip4_64(zipFile file, const char *filename, c if (zi->ci.method == AES_METHOD) size_extrafield += 11; #endif - err = zip64local_putValue(&zi->z_filefunc, zi->filestream, (uLong)size_extrafield, 2); + err = zip64local_putValue(&zi->z_filefunc, zi->filestream, (uLong)size_extrafield,2); } - if ((err == ZIP_OK) && (size_filename > 0)) { + if ((err == ZIP_OK) && (size_filename > 0)) + { if (ZWRITE64(zi->z_filefunc, zi->filestream, filename, size_filename) != size_filename) err = ZIP_ERRNO; } - if ((err == ZIP_OK) && (size_extrafield_local > 0)) { + if ((err == ZIP_OK) && (size_extrafield_local > 0)) + { if (ZWRITE64(zi->z_filefunc, zi->filestream, extrafield_local, size_extrafield_local) != size_extrafield_local) err = ZIP_ERRNO; } /* Write the Zip64 extended info */ - if ((err == ZIP_OK) && (zi->ci.zip64)) { + if ((err == ZIP_OK) && (zi->ci.zip64)) + { short headerid = 1; short datasize = 16; ZPOS64_T compressed_size = 0; @@ -1157,7 +1198,8 @@ extern int ZEXPORT zipOpenNewFileInZip4_64(zipFile file, const char *filename, c } #ifdef HAVE_AES /* Write the AES extended info */ - if ((err == ZIP_OK) && (zi->ci.method == AES_METHOD)) { + if ((err == ZIP_OK) && (zi->ci.method == AES_METHOD)) + { int headerid = 0x9901; short datasize = 7; @@ -1175,7 +1217,7 @@ extern int ZEXPORT zipOpenNewFileInZip4_64(zipFile file, const char *filename, c #ifdef HAVE_BZIP2 zi->ci.bstream.avail_in = (uInt)0; zi->ci.bstream.avail_out = (uInt)Z_BUFSIZE; - zi->ci.bstream.next_out = (char *)zi->ci.buffered_data; + zi->ci.bstream.next_out = (char*)zi->ci.buffered_data; zi->ci.bstream.total_in_hi32 = 0; zi->ci.bstream.total_in_lo32 = 0; zi->ci.bstream.total_out_hi32 = 0; @@ -1189,8 +1231,10 @@ extern int ZEXPORT zipOpenNewFileInZip4_64(zipFile file, const char *filename, c zi->ci.stream.total_out = 0; zi->ci.stream.data_type = Z_BINARY; - if ((err == ZIP_OK) && (!zi->ci.raw)) { - if (method == Z_DEFLATED) { + if ((err == ZIP_OK) && (!zi->ci.raw)) + { + if (method == Z_DEFLATED) + { zi->ci.stream.zalloc = (alloc_func)0; zi->ci.stream.zfree = (free_func)0; zi->ci.stream.opaque = (voidpf)zi; @@ -1202,7 +1246,9 @@ extern int ZEXPORT zipOpenNewFileInZip4_64(zipFile file, const char *filename, c if (err == Z_OK) zi->ci.stream_initialised = Z_DEFLATED; - } else if (method == Z_BZIP2ED) { + } + else if (method == Z_BZIP2ED) + { #ifdef HAVE_BZIP2 zi->ci.bstream.bzalloc = 0; zi->ci.bstream.bzfree = 0; @@ -1217,9 +1263,11 @@ extern int ZEXPORT zipOpenNewFileInZip4_64(zipFile file, const char *filename, c #ifndef NOCRYPT zi->ci.crypt_header_size = 0; - if ((err == Z_OK) && ((zi->ci.flag & 1) != 0)) { + if ((err == Z_OK) && ((zi->ci.flag & 1) != 0)) + { #ifdef HAVE_AES - if (zi->ci.method == AES_METHOD) { + if (zi->ci.method == AES_METHOD) + { unsigned char passverify[AES_PWVERIFYSIZE]; unsigned char saltvalue[AES_MAXSALTLENGTH]; uInt saltlength; @@ -1233,7 +1281,7 @@ extern int ZEXPORT zipOpenNewFileInZip4_64(zipFile file, const char *filename, c prng_rand(saltvalue, saltlength, zi->ci.aes_rng); prng_end(zi->ci.aes_rng); - fcrypt_init(AES_ENCRYPTIONMODE, (unsigned char *)password, (unsigned int)strlen(password), saltvalue, passverify, &zi->ci.aes_ctx); + fcrypt_init(AES_ENCRYPTIONMODE, password, strlen(password), saltvalue, passverify, &zi->ci.aes_ctx); if (ZWRITE64(zi->z_filefunc, zi->filestream, saltvalue, saltlength) != saltlength) err = ZIP_ERRNO; @@ -1241,7 +1289,8 @@ extern int ZEXPORT zipOpenNewFileInZip4_64(zipFile file, const char *filename, c err = ZIP_ERRNO; zi->ci.crypt_header_size = saltlength + AES_PWVERIFYSIZE + AES_AUTHCODESIZE; - } else + } + else #endif { unsigned char bufHead[RAND_HEAD_LEN]; @@ -1264,75 +1313,75 @@ extern int ZEXPORT zipOpenNewFileInZip4_64(zipFile file, const char *filename, c return err; } -extern int ZEXPORT zipOpenNewFileInZip4(zipFile file, const char *filename, const zip_fileinfo *zipfi, - const void *extrafield_local, uInt size_extrafield_local, const void *extrafield_global, - uInt size_extrafield_global, const char *comment, int method, int level, int raw, int windowBits, - int memLevel, int strategy, const char *password, uLong crcForCrypting, uLong versionMadeBy, uLong flagBase) +extern int ZEXPORT zipOpenNewFileInZip4(zipFile file, const char* filename, const zip_fileinfo* zipfi, + const void* extrafield_local, uInt size_extrafield_local, const void* extrafield_global, + uInt size_extrafield_global, const char* comment, int method, int level, int raw, int windowBits, + int memLevel, int strategy, const char* password, uLong crcForCrypting, uLong versionMadeBy, uLong flagBase) { return zipOpenNewFileInZip4_64(file, filename, zipfi, extrafield_local, size_extrafield_local, - extrafield_global, size_extrafield_global, comment, method, level, raw, windowBits, memLevel, - strategy, password, crcForCrypting, versionMadeBy, flagBase, 0); + extrafield_global, size_extrafield_global, comment, method, level, raw, windowBits, memLevel, + strategy, password, crcForCrypting, versionMadeBy, flagBase, 0); } -extern int ZEXPORT zipOpenNewFileInZip3(zipFile file, const char *filename, const zip_fileinfo *zipfi, - const void *extrafield_local, uInt size_extrafield_local, const void *extrafield_global, - uInt size_extrafield_global, const char *comment, int method, int level, int raw, int windowBits, - int memLevel, int strategy, const char *password, uLong crcForCrypting) +extern int ZEXPORT zipOpenNewFileInZip3(zipFile file, const char* filename, const zip_fileinfo* zipfi, + const void* extrafield_local, uInt size_extrafield_local, const void* extrafield_global, + uInt size_extrafield_global, const char* comment, int method, int level, int raw, int windowBits, + int memLevel, int strategy, const char* password, uLong crcForCrypting) { return zipOpenNewFileInZip4_64(file, filename, zipfi, extrafield_local, size_extrafield_local, - extrafield_global, size_extrafield_global, comment, method, level, raw, windowBits, memLevel, - strategy, password, crcForCrypting, VERSIONMADEBY, 0, 0); + extrafield_global, size_extrafield_global, comment, method, level, raw, windowBits, memLevel, + strategy, password, crcForCrypting, VERSIONMADEBY, 0, 0); } -extern int ZEXPORT zipOpenNewFileInZip3_64(zipFile file, const char *filename, const zip_fileinfo *zipfi, - const void *extrafield_local, uInt size_extrafield_local, const void *extrafield_global, - uInt size_extrafield_global, const char *comment, int method, int level, int raw, int windowBits, - int memLevel, int strategy, const char *password, uLong crcForCrypting, int zip64) +extern int ZEXPORT zipOpenNewFileInZip3_64(zipFile file, const char* filename, const zip_fileinfo* zipfi, + const void* extrafield_local, uInt size_extrafield_local, const void* extrafield_global, + uInt size_extrafield_global, const char* comment, int method, int level, int raw, int windowBits, + int memLevel, int strategy, const char* password, uLong crcForCrypting, int zip64) { return zipOpenNewFileInZip4_64(file, filename, zipfi, extrafield_local, size_extrafield_local, - extrafield_global, size_extrafield_global, comment, method, level, raw, windowBits, memLevel, strategy, - password, crcForCrypting, VERSIONMADEBY, 0, zip64); + extrafield_global, size_extrafield_global, comment, method, level, raw, windowBits, memLevel, strategy, + password, crcForCrypting, VERSIONMADEBY, 0, zip64); } -extern int ZEXPORT zipOpenNewFileInZip2(zipFile file, const char *filename, const zip_fileinfo *zipfi, - const void *extrafield_local, uInt size_extrafield_local, const void *extrafield_global, - uInt size_extrafield_global, const char *comment, int method, int level, int raw) +extern int ZEXPORT zipOpenNewFileInZip2(zipFile file, const char* filename, const zip_fileinfo* zipfi, + const void* extrafield_local, uInt size_extrafield_local, const void* extrafield_global, + uInt size_extrafield_global, const char* comment, int method, int level, int raw) { return zipOpenNewFileInZip4_64(file, filename, zipfi, extrafield_local, size_extrafield_local, - extrafield_global, size_extrafield_global, comment, method, level, raw, -MAX_WBITS, DEF_MEM_LEVEL, - Z_DEFAULT_STRATEGY, NULL, 0, VERSIONMADEBY, 0, 0); + extrafield_global, size_extrafield_global, comment, method, level, raw, -MAX_WBITS, DEF_MEM_LEVEL, + Z_DEFAULT_STRATEGY, NULL, 0, VERSIONMADEBY, 0, 0); } -extern int ZEXPORT zipOpenNewFileInZip2_64(zipFile file, const char *filename, const zip_fileinfo *zipfi, - const void *extrafield_local, uInt size_extrafield_local, const void *extrafield_global, - uInt size_extrafield_global, const char *comment, int method, int level, int raw, int zip64) +extern int ZEXPORT zipOpenNewFileInZip2_64(zipFile file, const char* filename, const zip_fileinfo* zipfi, + const void* extrafield_local, uInt size_extrafield_local, const void* extrafield_global, + uInt size_extrafield_global, const char* comment, int method, int level, int raw, int zip64) { return zipOpenNewFileInZip4_64(file, filename, zipfi, extrafield_local, size_extrafield_local, - extrafield_global, size_extrafield_global, comment, method, level, raw, -MAX_WBITS, DEF_MEM_LEVEL, - Z_DEFAULT_STRATEGY, NULL, 0, VERSIONMADEBY, 0, zip64); + extrafield_global, size_extrafield_global, comment, method, level, raw, -MAX_WBITS, DEF_MEM_LEVEL, + Z_DEFAULT_STRATEGY, NULL, 0, VERSIONMADEBY, 0, zip64); } -extern int ZEXPORT zipOpenNewFileInZip64(zipFile file, const char *filename, const zip_fileinfo *zipfi, - const void *extrafield_local, uInt size_extrafield_local, const void *extrafield_global, - uInt size_extrafield_global, const char *comment, int method, int level, int zip64) +extern int ZEXPORT zipOpenNewFileInZip64(zipFile file, const char* filename, const zip_fileinfo* zipfi, + const void* extrafield_local, uInt size_extrafield_local, const void*extrafield_global, + uInt size_extrafield_global, const char* comment, int method, int level, int zip64) { return zipOpenNewFileInZip4_64(file, filename, zipfi, extrafield_local, size_extrafield_local, - extrafield_global, size_extrafield_global, comment, method, level, 0, -MAX_WBITS, DEF_MEM_LEVEL, - Z_DEFAULT_STRATEGY, NULL, 0, VERSIONMADEBY, 0, zip64); + extrafield_global, size_extrafield_global, comment, method, level, 0, -MAX_WBITS, DEF_MEM_LEVEL, + Z_DEFAULT_STRATEGY, NULL, 0, VERSIONMADEBY, 0, zip64); } -extern int ZEXPORT zipOpenNewFileInZip(zipFile file, const char *filename, const zip_fileinfo *zipfi, - const void *extrafield_local, uInt size_extrafield_local, const void *extrafield_global, - uInt size_extrafield_global, const char *comment, int method, int level) +extern int ZEXPORT zipOpenNewFileInZip(zipFile file, const char* filename, const zip_fileinfo* zipfi, + const void* extrafield_local, uInt size_extrafield_local, const void*extrafield_global, + uInt size_extrafield_global, const char* comment, int method, int level) { return zipOpenNewFileInZip4_64(file, filename, zipfi, extrafield_local, size_extrafield_local, - extrafield_global, size_extrafield_global, comment, method, level, 0, -MAX_WBITS, DEF_MEM_LEVEL, - Z_DEFAULT_STRATEGY, NULL, 0, VERSIONMADEBY, 0, 0); + extrafield_global, size_extrafield_global, comment, method, level, 0, -MAX_WBITS, DEF_MEM_LEVEL, + Z_DEFAULT_STRATEGY, NULL, 0, VERSIONMADEBY, 0, 0); } /* Flushes the write buffer to disk */ -local int zip64FlushWriteBuffer OF((zip64_internal * zi)); -local int zip64FlushWriteBuffer(zip64_internal *zi) +local int zip64FlushWriteBuffer OF((zip64_internal* zi)); +local int zip64FlushWriteBuffer(zip64_internal* zi) { int err = ZIP_OK; uInt written = 0; @@ -1341,33 +1390,39 @@ local int zip64FlushWriteBuffer(zip64_internal *zi) uInt max_write = 0; ZPOS64_T size_available = 0; - if ((zi->ci.flag & 1) != 0) { + if ((zi->ci.flag & 1) != 0) + { #ifndef NOCRYPT #ifdef HAVE_AES - if (zi->ci.method == AES_METHOD) { + if (zi->ci.method == AES_METHOD) + { fcrypt_encrypt(zi->ci.buffered_data, zi->ci.pos_in_buffered_data, &zi->ci.aes_ctx); - } else + } + else #endif { uInt i; int t; - for (i = 0; i < zi->ci.pos_in_buffered_data; i++) - zi->ci.buffered_data[i] = zencode(zi->ci.keys, zi->ci.pcrc_32_tab, zi->ci.buffered_data[i], t); + for (i = 0;i < zi->ci.pos_in_buffered_data; i++) + zi->ci.buffered_data[i] = zencode(zi->ci.keys, zi->ci.pcrc_32_tab, zi->ci.buffered_data[i],t); } #endif } write = zi->ci.pos_in_buffered_data; - do { + do + { max_write = write; - if (zi->disk_size > 0) { + if (zi->disk_size > 0) + { err = zipGetDiskSizeAvailable((zipFile)zi, &size_available); if (err != ZIP_OK) return err; - if (size_available == 0) { + if (size_available == 0) + { err = zipGoToNextDisk((zipFile)zi); if (err != ZIP_OK) return err; @@ -1377,25 +1432,29 @@ local int zip64FlushWriteBuffer(zip64_internal *zi) max_write = (uInt)size_available; } - written = (unsigned int)ZWRITE64(zi->z_filefunc, zi->filestream, zi->ci.buffered_data + total_written, max_write); + written = ZWRITE64(zi->z_filefunc, zi->filestream, zi->ci.buffered_data + total_written, max_write); - if (ZERROR64(zi->z_filefunc, zi->filestream)) { + if (ZERROR64(zi->z_filefunc, zi->filestream)) + { err = ZIP_ERRNO; break; } total_written += written; write -= written; - } while (write > 0); + } + while (write > 0); zi->ci.total_compressed += zi->ci.pos_in_buffered_data; #ifdef HAVE_BZIP2 - if (zi->ci.compression_method == Z_BZIP2ED) { + if (zi->ci.compression_method == Z_BZIP2ED) + { zi->ci.total_uncompressed += zi->ci.bstream.total_in_lo32; zi->ci.bstream.total_in_lo32 = 0; zi->ci.bstream.total_in_hi32 = 0; - } else + } + else #endif { zi->ci.total_uncompressed += zi->ci.stream.total_in; @@ -1407,14 +1466,14 @@ local int zip64FlushWriteBuffer(zip64_internal *zi) return err; } -extern int ZEXPORT zipWriteInFileInZip(zipFile file, const void *buf, unsigned int len) +extern int ZEXPORT zipWriteInFileInZip(zipFile file,const void* buf,unsigned int len) { - zip64_internal *zi; + zip64_internal* zi; int err = ZIP_OK; if (file == NULL) return ZIP_PARAMERROR; - zi = (zip64_internal *)file; + zi = (zip64_internal*)file; if (zi->in_opened_file_inzip == 0) return ZIP_PARAMERROR; @@ -1422,18 +1481,23 @@ extern int ZEXPORT zipWriteInFileInZip(zipFile file, const void *buf, unsigned i zi->ci.crc32 = crc32(zi->ci.crc32, buf, (uInt)len); #ifdef HAVE_BZIP2 - if ((zi->ci.compression_method == Z_BZIP2ED) && (!zi->ci.raw)) { - zi->ci.bstream.next_in = (void *)buf; + if ((zi->ci.compression_method == Z_BZIP2ED) && (!zi->ci.raw)) + { + zi->ci.bstream.next_in = (void*)buf; zi->ci.bstream.avail_in = len; err = BZ_RUN_OK; - while ((err == BZ_RUN_OK) && (zi->ci.bstream.avail_in > 0)) { - if (zi->ci.bstream.avail_out == 0) { + while ((err == BZ_RUN_OK) && (zi->ci.bstream.avail_in > 0)) + { + if (zi->ci.bstream.avail_out == 0) + { if (zip64FlushWriteBuffer(zi) == ZIP_ERRNO) err = ZIP_ERRNO; zi->ci.bstream.avail_out = (uInt)Z_BUFSIZE; - zi->ci.bstream.next_out = (char *)zi->ci.buffered_data; - } else { + zi->ci.bstream.next_out = (char*)zi->ci.buffered_data; + } + else + { uLong uTotalOutBefore_lo = zi->ci.bstream.total_out_lo32; uLong uTotalOutBefore_hi = zi->ci.bstream.total_out_hi32; @@ -1445,14 +1509,17 @@ extern int ZEXPORT zipWriteInFileInZip(zipFile file, const void *buf, unsigned i if (err == BZ_RUN_OK) err = ZIP_OK; - } else + } + else #endif { - zi->ci.stream.next_in = (Bytef *)buf; + zi->ci.stream.next_in = (Bytef*)buf; zi->ci.stream.avail_in = len; - while ((err == ZIP_OK) && (zi->ci.stream.avail_in > 0)) { - if (zi->ci.stream.avail_out == 0) { + while ((err == ZIP_OK) && (zi->ci.stream.avail_in > 0)) + { + if (zi->ci.stream.avail_out == 0) + { if (zip64FlushWriteBuffer(zi) == ZIP_ERRNO) err = ZIP_ERRNO; zi->ci.stream.avail_out = (uInt)Z_BUFSIZE; @@ -1462,22 +1529,25 @@ extern int ZEXPORT zipWriteInFileInZip(zipFile file, const void *buf, unsigned i if (err != ZIP_OK) break; - if ((zi->ci.compression_method == Z_DEFLATED) && (!zi->ci.raw)) { + if ((zi->ci.compression_method == Z_DEFLATED) && (!zi->ci.raw)) + { uLong total_out_before = zi->ci.stream.total_out; err = deflate(&zi->ci.stream, Z_NO_FLUSH); zi->ci.pos_in_buffered_data += (uInt)(zi->ci.stream.total_out - total_out_before); - } else { - uInt copy_this, i; + } + else + { + uInt copy_this,i; if (zi->ci.stream.avail_in < zi->ci.stream.avail_out) copy_this = zi->ci.stream.avail_in; else copy_this = zi->ci.stream.avail_out; for (i = 0; i < copy_this; i++) - *(((char *)zi->ci.stream.next_out) + i) = - *(((const char *)zi->ci.stream.next_in) + i); + *(((char*)zi->ci.stream.next_out)+i) = + *(((const char*)zi->ci.stream.next_in)+i); - zi->ci.stream.avail_in -= copy_this; + zi->ci.stream.avail_in -= copy_this; zi->ci.stream.avail_out -= copy_this; zi->ci.stream.next_in += copy_this; zi->ci.stream.next_out += copy_this; @@ -1493,12 +1563,12 @@ extern int ZEXPORT zipWriteInFileInZip(zipFile file, const void *buf, unsigned i extern int ZEXPORT zipCloseFileInZipRaw(zipFile file, uLong uncompressed_size, uLong crc32) { - return zipCloseFileInZipRaw64(file, uncompressed_size, crc32); + return zipCloseFileInZipRaw64 (file, uncompressed_size, crc32); } extern int ZEXPORT zipCloseFileInZipRaw64(zipFile file, ZPOS64_T uncompressed_size, uLong crc32) { - zip64_internal *zi; + zip64_internal* zi; ZPOS64_T compressed_size; uLong invalidValue = 0xffffffff; uLong i = 0; @@ -1507,17 +1577,21 @@ extern int ZEXPORT zipCloseFileInZipRaw64(zipFile file, ZPOS64_T uncompressed_si if (file == NULL) return ZIP_PARAMERROR; - zi = (zip64_internal *)file; + zi = (zip64_internal*)file; if (zi->in_opened_file_inzip == 0) return ZIP_PARAMERROR; zi->ci.stream.avail_in = 0; - if (!zi->ci.raw) { - if (zi->ci.compression_method == Z_DEFLATED) { - while (err == ZIP_OK) { + if (!zi->ci.raw) + { + if (zi->ci.compression_method == Z_DEFLATED) + { + while (err == ZIP_OK) + { uLong total_out_before; - if (zi->ci.stream.avail_out == 0) { + if (zi->ci.stream.avail_out == 0) + { if (zip64FlushWriteBuffer(zi) == ZIP_ERRNO) err = ZIP_ERRNO; zi->ci.stream.avail_out = (uInt)Z_BUFSIZE; @@ -1527,16 +1601,20 @@ extern int ZEXPORT zipCloseFileInZipRaw64(zipFile file, ZPOS64_T uncompressed_si err = deflate(&zi->ci.stream, Z_FINISH); zi->ci.pos_in_buffered_data += (uInt)(zi->ci.stream.total_out - total_out_before); } - } else if (zi->ci.compression_method == Z_BZIP2ED) { + } + else if (zi->ci.compression_method == Z_BZIP2ED) + { #ifdef HAVE_BZIP2 err = BZ_FINISH_OK; - while (err == BZ_FINISH_OK) { + while (err == BZ_FINISH_OK) + { uLong total_out_before; - if (zi->ci.bstream.avail_out == 0) { + if (zi->ci.bstream.avail_out == 0) + { if (zip64FlushWriteBuffer(zi) == ZIP_ERRNO) err = ZIP_ERRNO; zi->ci.bstream.avail_out = (uInt)Z_BUFSIZE; - zi->ci.bstream.next_out = (char *)zi->ci.buffered_data; + zi->ci.bstream.next_out = (char*)zi->ci.buffered_data; } total_out_before = zi->ci.bstream.total_out_lo32; err = BZ2_bzCompress(&zi->ci.bstream, BZ_FINISH); @@ -1554,13 +1632,15 @@ extern int ZEXPORT zipCloseFileInZipRaw64(zipFile file, ZPOS64_T uncompressed_si if (err == Z_STREAM_END) err = ZIP_OK; /* this is normal */ - if ((zi->ci.pos_in_buffered_data > 0) && (err == ZIP_OK)) { + if ((zi->ci.pos_in_buffered_data > 0) && (err == ZIP_OK)) + { if (zip64FlushWriteBuffer(zi) == ZIP_ERRNO) err = ZIP_ERRNO; } #ifdef HAVE_AES - if (zi->ci.method == AES_METHOD) { + if (zi->ci.method == AES_METHOD) + { unsigned char authcode[AES_AUTHCODESIZE]; fcrypt_end(authcode, &zi->ci.aes_ctx); @@ -1570,15 +1650,18 @@ extern int ZEXPORT zipCloseFileInZipRaw64(zipFile file, ZPOS64_T uncompressed_si } #endif - if (!zi->ci.raw) { - if (zi->ci.compression_method == Z_DEFLATED) { + if (!zi->ci.raw) + { + if (zi->ci.compression_method == Z_DEFLATED) + { int tmp_err = deflateEnd(&zi->ci.stream); if (err == ZIP_OK) err = tmp_err; zi->ci.stream_initialised = 0; } #ifdef HAVE_BZIP2 - else if (zi->ci.compression_method == Z_BZIP2ED) { + else if (zi->ci.compression_method == Z_BZIP2ED) + { int tmperr = BZ2_bzCompressEnd(&zi->ci.bstream); if (err == ZIP_OK) err = tmperr; @@ -1596,21 +1679,22 @@ extern int ZEXPORT zipCloseFileInZipRaw64(zipFile file, ZPOS64_T uncompressed_si #endif /* Update current item crc and sizes */ - if (compressed_size >= 0xffffffff || uncompressed_size >= 0xffffffff || zi->ci.pos_local_header >= 0xffffffff) { - zip64local_putValue_inmemory(zi->ci.central_header + 4, (uLong)45, 2); /* version made by */ - zip64local_putValue_inmemory(zi->ci.central_header + 6, (uLong)45, 2); /* version needed */ + if (compressed_size >= 0xffffffff || uncompressed_size >= 0xffffffff || zi->ci.pos_local_header >= 0xffffffff) + { + zip64local_putValue_inmemory(zi->ci.central_header+4, (uLong)45, 2); /* version made by */ + zip64local_putValue_inmemory(zi->ci.central_header+6, (uLong)45, 2); /* version needed */ } - zip64local_putValue_inmemory(zi->ci.central_header + 16, crc32, 4); /* crc */ + zip64local_putValue_inmemory(zi->ci.central_header+16, crc32, 4); /* crc */ if (compressed_size >= 0xffffffff) - zip64local_putValue_inmemory(zi->ci.central_header + 20, invalidValue, 4); /* compr size */ + zip64local_putValue_inmemory(zi->ci.central_header+20, invalidValue, 4); /* compr size */ else - zip64local_putValue_inmemory(zi->ci.central_header + 20, compressed_size, 4); /* compr size */ + zip64local_putValue_inmemory(zi->ci.central_header+20, compressed_size, 4); /* compr size */ if (zi->ci.stream.data_type == Z_ASCII) - zip64local_putValue_inmemory(zi->ci.central_header + 36, (uLong)Z_ASCII, 2); /* internal file attrib */ + zip64local_putValue_inmemory(zi->ci.central_header+36, (uLong)Z_ASCII, 2); /* internal file attrib */ if (uncompressed_size >= 0xffffffff) - zip64local_putValue_inmemory(zi->ci.central_header + 24, invalidValue, 4); /* uncompr size */ + zip64local_putValue_inmemory(zi->ci.central_header+24, invalidValue, 4); /* uncompr size */ else - zip64local_putValue_inmemory(zi->ci.central_header + 24, uncompressed_size, 4); /* uncompr size */ + zip64local_putValue_inmemory(zi->ci.central_header+24, uncompressed_size, 4); /* uncompr size */ /* Add ZIP64 extra info field for uncompressed size */ if (uncompressed_size >= 0xffffffff) @@ -1623,8 +1707,9 @@ extern int ZEXPORT zipCloseFileInZipRaw64(zipFile file, ZPOS64_T uncompressed_si datasize += 8; /* Add Extra Information Header for 'ZIP64 information' */ - if (datasize > 0) { - char *p = zi->ci.central_header + zi->ci.size_centralheader; + if (datasize > 0) + { + char* p = zi->ci.central_header + zi->ci.size_centralheader; if ((uLong)(datasize + 4) > zi->ci.size_centralextrafree) return ZIP_BADZIPFILE; @@ -1634,15 +1719,18 @@ extern int ZEXPORT zipCloseFileInZipRaw64(zipFile file, ZPOS64_T uncompressed_si zip64local_putValue_inmemory(p, datasize, 2); p += 2; - if (uncompressed_size >= 0xffffffff) { + if (uncompressed_size >= 0xffffffff) + { zip64local_putValue_inmemory(p, uncompressed_size, 8); p += 8; } - if (compressed_size >= 0xffffffff) { + if (compressed_size >= 0xffffffff) + { zip64local_putValue_inmemory(p, compressed_size, 8); p += 8; } - if (zi->ci.pos_local_header >= 0xffffffff) { + if (zi->ci.pos_local_header >= 0xffffffff) + { zip64local_putValue_inmemory(p, zi->ci.pos_local_header, 8); p += 8; } @@ -1651,13 +1739,14 @@ extern int ZEXPORT zipCloseFileInZipRaw64(zipFile file, ZPOS64_T uncompressed_si zi->ci.size_centralheader += datasize + 4; zi->ci.size_centralextra += datasize + 4; - zip64local_putValue_inmemory(zi->ci.central_header + 30, (uLong)zi->ci.size_centralextra, 2); + zip64local_putValue_inmemory(zi->ci.central_header+30, (uLong)zi->ci.size_centralextra, 2); } #ifdef HAVE_AES /* Write the AES extended info */ - if (zi->ci.method == AES_METHOD) { - char *p = zi->ci.central_header + zi->ci.size_centralheader; + if (zi->ci.method == AES_METHOD) + { + char* p = zi->ci.central_header + zi->ci.size_centralheader; datasize = 7; @@ -1683,13 +1772,13 @@ extern int ZEXPORT zipCloseFileInZipRaw64(zipFile file, ZPOS64_T uncompressed_si zi->ci.size_centralheader += datasize + 4; zi->ci.size_centralextra += datasize + 4; - zip64local_putValue_inmemory(zi->ci.central_header + 30, (uLong)zi->ci.size_centralextra, 2); + zip64local_putValue_inmemory(zi->ci.central_header+30, (uLong)zi->ci.size_centralextra, 2); } #endif /* Restore comment to correct position */ for (i = 0; i < zi->ci.size_comment; i++) - zi->ci.central_header[zi->ci.size_centralheader + i] = - zi->ci.central_header[zi->ci.size_centralheader + zi->ci.size_centralextrafree + i]; + zi->ci.central_header[zi->ci.size_centralheader+i] = + zi->ci.central_header[zi->ci.size_centralheader+zi->ci.size_centralextrafree+i]; zi->ci.size_centralheader += zi->ci.size_comment; if (err == ZIP_OK) @@ -1697,22 +1786,25 @@ extern int ZEXPORT zipCloseFileInZipRaw64(zipFile file, ZPOS64_T uncompressed_si free(zi->ci.central_header); - if (err == ZIP_OK) { + if (err == ZIP_OK) + { /* Update the LocalFileHeader with the new values. */ ZPOS64_T cur_pos_inzip = ZTELL64(zi->z_filefunc, zi->filestream); uLong cur_number_disk = zi->number_disk; /* Local file header is stored on previous disk, switch to make edits */ if (zi->ci.number_disk != cur_number_disk) - err = zipGoToSpecificDisk(file, (int)zi->ci.number_disk, 1); + err = zipGoToSpecificDisk(file, zi->ci.number_disk, 1); if (ZSEEK64(zi->z_filefunc, zi->filestream, zi->ci.pos_local_header + 14, ZLIB_FILEFUNC_SEEK_SET) != 0) err = ZIP_ERRNO; if (err == ZIP_OK) - err = zip64local_putValue(&zi->z_filefunc, zi->filestream, crc32, 4); /* crc 32, unknown */ + err = zip64local_putValue(&zi->z_filefunc, zi->filestream,crc32,4); /* crc 32, unknown */ - if (uncompressed_size >= 0xffffffff || compressed_size >= 0xffffffff) { - if (zi->ci.pos_zip64extrainfo > 0) { + if (uncompressed_size >= 0xffffffff || compressed_size >= 0xffffffff) + { + if (zi->ci.pos_zip64extrainfo > 0) + { /* Update the size in the ZIP64 extended field. */ if (ZSEEK64(zi->z_filefunc, zi->filestream, zi->ci.pos_zip64extrainfo + 4, ZLIB_FILEFUNC_SEEK_SET) != 0) err = ZIP_ERRNO; @@ -1721,18 +1813,21 @@ extern int ZEXPORT zipCloseFileInZipRaw64(zipFile file, ZPOS64_T uncompressed_si err = zip64local_putValue(&zi->z_filefunc, zi->filestream, uncompressed_size, 8); if (err == ZIP_OK) /* uncompressed size, unknown */ err = zip64local_putValue(&zi->z_filefunc, zi->filestream, compressed_size, 8); - } else + } + else err = ZIP_BADZIPFILE; /* Caller passed zip64 = 0, so no room for zip64 info -> fatal */ - } else { - if (err == ZIP_OK) /* compressed size, unknown */ - err = zip64local_putValue(&zi->z_filefunc, zi->filestream, compressed_size, 4); - if (err == ZIP_OK) /* uncompressed size, unknown */ - err = zip64local_putValue(&zi->z_filefunc, zi->filestream, uncompressed_size, 4); + } + else + { + if (err == ZIP_OK) /* compressed size, unknown */ + err = zip64local_putValue(&zi->z_filefunc, zi->filestream,compressed_size, 4); + if (err == ZIP_OK) /* uncompressed size, unknown */ + err = zip64local_putValue(&zi->z_filefunc, zi->filestream,uncompressed_size, 4); } /* Now switch back again to the disk we were on before */ if (zi->ci.number_disk != cur_number_disk) - err = zipGoToSpecificDisk(file, (int)cur_number_disk, 1); + err = zipGoToSpecificDisk(file, cur_number_disk, 1); if (ZSEEK64(zi->z_filefunc, zi->filestream, cur_pos_inzip, ZLIB_FILEFUNC_SEEK_SET) != 0) err = ZIP_ERRNO; @@ -1749,9 +1844,9 @@ extern int ZEXPORT zipCloseFileInZip(zipFile file) return zipCloseFileInZipRaw(file, 0, 0); } -extern int ZEXPORT zipClose(zipFile file, const char *global_comment) +extern int ZEXPORT zipClose(zipFile file, const char* global_comment) { - zip64_internal *zi; + zip64_internal* zi; int err = 0; uLong size_centraldir = 0; uInt size_global_comment = 0; @@ -1762,7 +1857,7 @@ extern int ZEXPORT zipClose(zipFile file, const char *global_comment) if (file == NULL) return ZIP_PARAMERROR; - zi = (zip64_internal *)file; + zi = (zip64_internal*)file; if (zi->in_opened_file_inzip == 1) err = zipCloseFileInZip(file); @@ -1772,7 +1867,8 @@ extern int ZEXPORT zipClose(zipFile file, const char *global_comment) global_comment = zi->globalcomment; #endif - if (zi->filestream != zi->filestream_with_CD) { + if (zi->filestream != zi->filestream_with_CD) + { if (ZCLOSE64(zi->z_filefunc, zi->filestream) != 0) if (err == ZIP_OK) err = ZIP_ERRNO; @@ -1783,10 +1879,13 @@ extern int ZEXPORT zipClose(zipFile file, const char *global_comment) centraldir_pos_inzip = ZTELL64(zi->z_filefunc, zi->filestream); - if (err == ZIP_OK) { - linkedlist_datablock_internal *ldi = zi->central_dir.first_block; - while (ldi != NULL) { - if ((err == ZIP_OK) && (ldi->filled_in_this_block > 0)) { + if (err == ZIP_OK) + { + linkedlist_datablock_internal* ldi = zi->central_dir.first_block; + while (ldi!= NULL) + { + if ((err == ZIP_OK) && (ldi->filled_in_this_block > 0)) + { write = ZWRITE64(zi->z_filefunc, zi->filestream, ldi->data, ldi->filled_in_this_block); if (write != ldi->filled_in_this_block) err = ZIP_ERRNO; @@ -1802,88 +1901,94 @@ extern int ZEXPORT zipClose(zipFile file, const char *global_comment) pos = centraldir_pos_inzip - zi->add_position_when_writting_offset; /* Write the ZIP64 central directory header */ - if (pos >= 0xffffffff || zi->number_entry > 0xffff) { + if (pos >= 0xffffffff || zi->number_entry > 0xffff) + { ZPOS64_T zip64eocd_pos_inzip = ZTELL64(zi->z_filefunc, zi->filestream); uLong zip64datasize = 44; err = zip64local_putValue(&zi->z_filefunc, zi->filestream, (uLong)ZIP64ENDHEADERMAGIC, 4); - /* size of this 'zip64 end of central directory' */ + /* Size of this 'zip64 end of central directory' */ if (err == ZIP_OK) err = zip64local_putValue(&zi->z_filefunc, zi->filestream, (ZPOS64_T)zip64datasize, 8); - /* version made by */ + /* Version made by */ if (err == ZIP_OK) err = zip64local_putValue(&zi->z_filefunc, zi->filestream, (uLong)45, 2); - /* version needed */ + /* Version needed */ if (err == ZIP_OK) err = zip64local_putValue(&zi->z_filefunc, zi->filestream, (uLong)45, 2); - /* number of this disk */ + /* Number of this disk */ if (err == ZIP_OK) err = zip64local_putValue(&zi->z_filefunc, zi->filestream, (uLong)zi->number_disk_with_CD, 4); - /* number of the disk with the start of the central directory */ + /* Number of the disk with the start of the central directory */ if (err == ZIP_OK) err = zip64local_putValue(&zi->z_filefunc, zi->filestream, (uLong)zi->number_disk_with_CD, 4); - /* total number of entries in the central dir on this disk */ + /* Total number of entries in the central dir on this disk */ if (err == ZIP_OK) err = zip64local_putValue(&zi->z_filefunc, zi->filestream, zi->number_entry, 8); - /* total number of entries in the central dir */ + /* Total number of entries in the central dir */ if (err == ZIP_OK) err = zip64local_putValue(&zi->z_filefunc, zi->filestream, zi->number_entry, 8); - /* size of the central directory */ + /* Size of the central directory */ if (err == ZIP_OK) err = zip64local_putValue(&zi->z_filefunc, zi->filestream, (ZPOS64_T)size_centraldir, 8); - if (err == ZIP_OK) { - /* offset of start of central directory with respect to the starting disk number */ + if (err == ZIP_OK) + { + /* Offset of start of central directory with respect to the starting disk number */ ZPOS64_T pos = centraldir_pos_inzip - zi->add_position_when_writting_offset; err = zip64local_putValue(&zi->z_filefunc, zi->filestream, (ZPOS64_T)pos, 8); } if (err == ZIP_OK) err = zip64local_putValue(&zi->z_filefunc, zi->filestream, (uLong)ZIP64ENDLOCHEADERMAGIC, 4); - /* number of the disk with the start of the central directory */ + /* Number of the disk with the start of the central directory */ if (err == ZIP_OK) err = zip64local_putValue(&zi->z_filefunc, zi->filestream, (uLong)zi->number_disk_with_CD, 4); - /*relative offset to the Zip64EndOfCentralDirectory */ - if (err == ZIP_OK) { + /* Relative offset to the Zip64EndOfCentralDirectory */ + if (err == ZIP_OK) + { ZPOS64_T pos = zip64eocd_pos_inzip - zi->add_position_when_writting_offset; err = zip64local_putValue(&zi->z_filefunc, zi->filestream, pos, 8); } - /* number of the disk with the start of the central directory */ + /* Number of the disk with the start of the central directory */ if (err == ZIP_OK) - err = zip64local_putValue(&zi->z_filefunc, zi->filestream, (uLong)zi->number_disk_with_CD + 1, 4); + err = zip64local_putValue(&zi->z_filefunc, zi->filestream, (uLong)zi->number_disk_with_CD+1, 4); } /* Write the central directory header */ - /* signature */ + /* Signature */ if (err == ZIP_OK) err = zip64local_putValue(&zi->z_filefunc, zi->filestream, (uLong)ENDHEADERMAGIC, 4); - /* number of this disk */ + /* Number of this disk */ if (err == ZIP_OK) err = zip64local_putValue(&zi->z_filefunc, zi->filestream, (uLong)zi->number_disk_with_CD, 2); - /* number of the disk with the start of the central directory */ + /* Number of the disk with the start of the central directory */ if (err == ZIP_OK) err = zip64local_putValue(&zi->z_filefunc, zi->filestream, (uLong)zi->number_disk_with_CD, 2); - /* total number of entries in the central dir on this disk */ - if (err == ZIP_OK) { + /* Total number of entries in the central dir on this disk */ + if (err == ZIP_OK) + { if (zi->number_entry >= 0xffff) err = zip64local_putValue(&zi->z_filefunc, zi->filestream, (uLong)0xffff, 2); /* use value in ZIP64 record */ else err = zip64local_putValue(&zi->z_filefunc, zi->filestream, (uLong)zi->number_entry, 2); } - /* total number of entries in the central dir */ - if (err == ZIP_OK) { + /* Total number of entries in the central dir */ + if (err == ZIP_OK) + { if (zi->number_entry >= 0xffff) err = zip64local_putValue(&zi->z_filefunc, zi->filestream, (uLong)0xffff, 2); /* use value in ZIP64 record */ else err = zip64local_putValue(&zi->z_filefunc, zi->filestream, (uLong)zi->number_entry, 2); } - /* size of the central directory */ + /* Size of the central directory */ if (err == ZIP_OK) - err = zip64local_putValue(&zi->z_filefunc, zi->filestream, (uLong)size_centraldir, 4); - /* offset of start of central directory with respect to the starting disk number */ - if (err == ZIP_OK) { + err = zip64local_putValue(&zi->z_filefunc, zi->filestream, (uLong)size_centraldir,4); + /* Offset of start of central directory with respect to the starting disk number */ + if (err == ZIP_OK) + { ZPOS64_T pos = centraldir_pos_inzip - zi->add_position_when_writting_offset; if (pos >= 0xffffffff) err = zip64local_putValue(&zi->z_filefunc, zi->filestream, (uLong)0xffffffff, 4); @@ -1897,7 +2002,8 @@ extern int ZEXPORT zipClose(zipFile file, const char *global_comment) size_global_comment = (uInt)strlen(global_comment); if (err == ZIP_OK) err = zip64local_putValue(&zi->z_filefunc, zi->filestream, (uLong)size_global_comment, 2); - if (err == ZIP_OK && size_global_comment > 0) { + if (err == ZIP_OK && size_global_comment > 0) + { if (ZWRITE64(zi->z_filefunc, zi->filestream, global_comment, size_global_comment) != size_global_comment) err = ZIP_ERRNO; } @@ -1912,4 +2018,3 @@ extern int ZEXPORT zipClose(zipFile file, const char *global_comment) return err; } - diff --git a/Zip/minizip/zip.h b/Zip/minizip/zip.h index 10bbf26c2c84b7c6286da227b23adacf5ffae4e9..60208a359756b6b032af6d4e8b71955044ff94a2 100755 --- a/Zip/minizip/zip.h +++ b/Zip/minizip/zip.h @@ -15,8 +15,6 @@ #ifndef _ZIP_H #define _ZIP_H -#define HAVE_AES - #ifdef __cplusplus extern "C" { #endif @@ -79,8 +77,6 @@ typedef struct uLong external_fa; /* external file attributes 4 bytes */ } zip_fileinfo; -typedef const char* zipcharpc; - #define APPEND_STATUS_CREATE (0) #define APPEND_STATUS_CREATEAFTER (1) #define APPEND_STATUS_ADDINZIP (2) @@ -107,18 +103,18 @@ extern zipFile ZEXPORT zipOpen64 OF((const void *pathname, int append)); you must open a zipfile, and create another. Of course, you can use RAW reading and writing to copy the file you did not want delete. */ -extern zipFile ZEXPORT zipOpen2 OF((const char *pathname, int append, zipcharpc* globalcomment, +extern zipFile ZEXPORT zipOpen2 OF((const char *pathname, int append, const char ** globalcomment, zlib_filefunc_def* pzlib_filefunc_def)); -extern zipFile ZEXPORT zipOpen2_64 OF((const void *pathname, int append, zipcharpc* globalcomment, +extern zipFile ZEXPORT zipOpen2_64 OF((const void *pathname, int append, const char ** globalcomment, zlib_filefunc64_def* pzlib_filefunc_def)); extern zipFile ZEXPORT zipOpen3 OF((const char *pathname, int append, ZPOS64_T disk_size, - zipcharpc* globalcomment, zlib_filefunc_def* pzlib_filefunc_def)); + const char ** globalcomment, zlib_filefunc_def* pzlib_filefunc_def)); /* Same as zipOpen2 but allows specification of spanned zip size */ extern zipFile ZEXPORT zipOpen3_64 OF((const void *pathname, int append, ZPOS64_T disk_size, - zipcharpc* globalcomment, zlib_filefunc64_def* pzlib_filefunc_def)); + const char ** globalcomment, zlib_filefunc64_def* pzlib_filefunc_def)); extern int ZEXPORT zipOpenNewFileInZip OF((zipFile file, const char* filename, const zip_fileinfo* zipfi, const void* extrafield_local, uInt size_extrafield_local, const void* extrafield_global, diff --git a/examples/Sample/Sample/master.zip b/examples/Sample/Sample/master.zip index 1548e6f02f06c89b8f11e3fb8a1d5c104e66962c..96e2d453dd67d621da39661bb46496df30a6db63 100644 Binary files a/examples/Sample/Sample/master.zip and b/examples/Sample/Sample/master.zip differ