Index: ObjFW.xcodeproj/project.pbxproj ================================================================== --- ObjFW.xcodeproj/project.pbxproj +++ ObjFW.xcodeproj/project.pbxproj @@ -300,12 +300,10 @@ 4B6025A419B76A5C00694BCC /* OFSHA512Hash.m in Sources */ = {isa = PBXBuildFile; fileRef = 4B60259E19B76A5C00694BCC /* OFSHA512Hash.m */; }; 4B6025A719B76B5000694BCC /* OFSHA384HashTests.m in Sources */ = {isa = PBXBuildFile; fileRef = 4B6025A519B76B5000694BCC /* OFSHA384HashTests.m */; }; 4B6025A819B76B5000694BCC /* OFSHA512HashTests.m in Sources */ = {isa = PBXBuildFile; fileRef = 4B6025A619B76B5000694BCC /* OFSHA512HashTests.m */; }; 4B62ED1518566FCA0004E0E3 /* OFCopyItemFailedException.h in Headers */ = {isa = PBXBuildFile; fileRef = 4B62ED1318566FCA0004E0E3 /* OFCopyItemFailedException.h */; settings = {ATTRIBUTES = (Public, ); }; }; 4B62ED1618566FCA0004E0E3 /* OFCopyItemFailedException.m in Sources */ = {isa = PBXBuildFile; fileRef = 4B62ED1418566FCA0004E0E3 /* OFCopyItemFailedException.m */; }; - 4B66B636182ADC45000A69E3 /* OFDeflate64Stream.h in Headers */ = {isa = PBXBuildFile; fileRef = 4B66B634182ADC45000A69E3 /* OFDeflate64Stream.h */; settings = {ATTRIBUTES = (Public, ); }; }; - 4B66B637182ADC45000A69E3 /* OFDeflate64Stream.m in Sources */ = {isa = PBXBuildFile; fileRef = 4B66B635182ADC45000A69E3 /* OFDeflate64Stream.m */; }; 4B6743F1163C384A00EB1E59 /* OFLockFailedException.h in Headers */ = {isa = PBXBuildFile; fileRef = 4B6743EB163C384A00EB1E59 /* OFLockFailedException.h */; settings = {ATTRIBUTES = (Public, ); }; }; 4B6743F2163C384A00EB1E59 /* OFLockFailedException.m in Sources */ = {isa = PBXBuildFile; fileRef = 4B6743EC163C384A00EB1E59 /* OFLockFailedException.m */; }; 4B6743F3163C384A00EB1E59 /* OFStillLockedException.h in Headers */ = {isa = PBXBuildFile; fileRef = 4B6743ED163C384A00EB1E59 /* OFStillLockedException.h */; settings = {ATTRIBUTES = (Public, ); }; }; 4B6743F4163C384A00EB1E59 /* OFStillLockedException.m in Sources */ = {isa = PBXBuildFile; fileRef = 4B6743EE163C384A00EB1E59 /* OFStillLockedException.m */; }; 4B6743F5163C384A00EB1E59 /* OFUnlockFailedException.h in Headers */ = {isa = PBXBuildFile; fileRef = 4B6743EF163C384A00EB1E59 /* OFUnlockFailedException.h */; settings = {ATTRIBUTES = (Public, ); }; }; @@ -321,10 +319,14 @@ 4B6C8AD917BD5C2E00B194F2 /* OFStream+Private.h in Headers */ = {isa = PBXBuildFile; fileRef = 4B6C8AD217BD5C2E00B194F2 /* OFStream+Private.h */; }; 4B6C8ADB17BD5C2E00B194F2 /* OFString_UTF8+Private.h in Headers */ = {isa = PBXBuildFile; fileRef = 4B6C8AD417BD5C2E00B194F2 /* OFString_UTF8+Private.h */; }; 4B6C8ADC17BD5C2E00B194F2 /* OFThread+Private.h in Headers */ = {isa = PBXBuildFile; fileRef = 4B6C8AD517BD5C2E00B194F2 /* OFThread+Private.h */; }; 4B6C8ADD17BD5C2E00B194F2 /* OFTimer+Private.h in Headers */ = {isa = PBXBuildFile; fileRef = 4B6C8AD617BD5C2E00B194F2 /* OFTimer+Private.h */; }; 4B6C8ADE17BD5C2E00B194F2 /* OFZIPArchiveEntry+Private.h in Headers */ = {isa = PBXBuildFile; fileRef = 4B6C8AD717BD5C2E00B194F2 /* OFZIPArchiveEntry+Private.h */; }; + 4B70A9921B07DEBF00CC5593 /* OFInflate64Stream.h in Headers */ = {isa = PBXBuildFile; fileRef = 4B70A9901B07DEBE00CC5593 /* OFInflate64Stream.h */; settings = {ATTRIBUTES = (Public, ); }; }; + 4B70A9931B07DEBF00CC5593 /* OFInflateStream.h in Headers */ = {isa = PBXBuildFile; fileRef = 4B70A9911B07DEBE00CC5593 /* OFInflateStream.h */; settings = {ATTRIBUTES = (Public, ); }; }; + 4B70A9961B07DF9700CC5593 /* OFInflate64Stream.m in Sources */ = {isa = PBXBuildFile; fileRef = 4B70A9941B07DF9700CC5593 /* OFInflate64Stream.m */; }; + 4B70A9971B07DF9700CC5593 /* OFInflateStream.m in Sources */ = {isa = PBXBuildFile; fileRef = 4B70A9951B07DF9700CC5593 /* OFInflateStream.m */; }; 4B7161AD17A6FC7600B74970 /* OFHTTPResponse.h in Headers */ = {isa = PBXBuildFile; fileRef = 4B7161AB17A6FC7600B74970 /* OFHTTPResponse.h */; settings = {ATTRIBUTES = (Public, ); }; }; 4B7161AE17A6FC7600B74970 /* OFHTTPResponse.m in Sources */ = {isa = PBXBuildFile; fileRef = 4B7161AC17A6FC7600B74970 /* OFHTTPResponse.m */; }; 4B72F7DE1AD9311B00CE253C /* OFStatItemFailedException.h in Headers */ = {isa = PBXBuildFile; fileRef = 4B72F7DC1AD9311B00CE253C /* OFStatItemFailedException.h */; settings = {ATTRIBUTES = (Public, ); }; }; 4B72F7DF1AD9311B00CE253C /* OFStatItemFailedException.m in Sources */ = {isa = PBXBuildFile; fileRef = 4B72F7DD1AD9311B00CE253C /* OFStatItemFailedException.m */; }; 4B745BA5168B25E600A6C20E /* OFSystemInfo.h in Headers */ = {isa = PBXBuildFile; fileRef = 4B745BA3168B25E600A6C20E /* OFSystemInfo.h */; settings = {ATTRIBUTES = (Public, ); }; }; @@ -343,12 +345,10 @@ 4B837D7A16829C5F007A3E83 /* instance.h in Headers */ = {isa = PBXBuildFile; fileRef = 4B837D7816829C5F007A3E83 /* instance.h */; settings = {ATTRIBUTES = (Public, ); }; }; 4B8385161951BF9500D5358A /* OFSettings_INIFile.h in Headers */ = {isa = PBXBuildFile; fileRef = 4B8385121951BF9500D5358A /* OFSettings_INIFile.h */; }; 4B8385171951BF9500D5358A /* OFSettings_INIFile.m in Sources */ = {isa = PBXBuildFile; fileRef = 4B8385131951BF9500D5358A /* OFSettings_INIFile.m */; }; 4B8385181951BF9500D5358A /* OFSettings.h in Headers */ = {isa = PBXBuildFile; fileRef = 4B8385141951BF9500D5358A /* OFSettings.h */; settings = {ATTRIBUTES = (Public, ); }; }; 4B8385191951BF9500D5358A /* OFSettings.m in Sources */ = {isa = PBXBuildFile; fileRef = 4B8385151951BF9500D5358A /* OFSettings.m */; }; - 4B86E7CB17F8B98200ACA680 /* OFDeflateStream.h in Headers */ = {isa = PBXBuildFile; fileRef = 4B86E7C917F8B98200ACA680 /* OFDeflateStream.h */; settings = {ATTRIBUTES = (Public, ); }; }; - 4B86E7CC17F8B98200ACA680 /* OFDeflateStream.m in Sources */ = {isa = PBXBuildFile; fileRef = 4B86E7CA17F8B98200ACA680 /* OFDeflateStream.m */; }; 4B879A8C177231F000EBCEA4 /* OFDataArray+MessagePackValue.h in Headers */ = {isa = PBXBuildFile; fileRef = 4B879A89177231F000EBCEA4 /* OFDataArray+MessagePackValue.h */; settings = {ATTRIBUTES = (Public, ); }; }; 4B879A8D177231F000EBCEA4 /* OFDataArray+MessagePackValue.m in Sources */ = {isa = PBXBuildFile; fileRef = 4B879A8A177231F000EBCEA4 /* OFDataArray+MessagePackValue.m */; }; 4B879A8E177231F000EBCEA4 /* OFMessagePackRepresentation.h in Headers */ = {isa = PBXBuildFile; fileRef = 4B879A8B177231F000EBCEA4 /* OFMessagePackRepresentation.h */; settings = {ATTRIBUTES = (Public, ); }; }; 4B8B025917BBA7C7009ED983 /* OFZIPArchiveEntry.h in Headers */ = {isa = PBXBuildFile; fileRef = 4B8B025717BBA7C7009ED983 /* OFZIPArchiveEntry.h */; settings = {ATTRIBUTES = (Public, ); }; }; 4B8B025A17BBA7C7009ED983 /* OFZIPArchiveEntry.m in Sources */ = {isa = PBXBuildFile; fileRef = 4B8B025817BBA7C7009ED983 /* OFZIPArchiveEntry.m */; }; @@ -736,12 +736,10 @@ 4B60259E19B76A5C00694BCC /* OFSHA512Hash.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; name = OFSHA512Hash.m; path = src/OFSHA512Hash.m; sourceTree = ""; }; 4B6025A519B76B5000694BCC /* OFSHA384HashTests.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; name = OFSHA384HashTests.m; path = tests/OFSHA384HashTests.m; sourceTree = ""; }; 4B6025A619B76B5000694BCC /* OFSHA512HashTests.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; name = OFSHA512HashTests.m; path = tests/OFSHA512HashTests.m; sourceTree = ""; }; 4B62ED1318566FCA0004E0E3 /* OFCopyItemFailedException.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = OFCopyItemFailedException.h; path = src/exceptions/OFCopyItemFailedException.h; sourceTree = ""; }; 4B62ED1418566FCA0004E0E3 /* OFCopyItemFailedException.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; name = OFCopyItemFailedException.m; path = src/exceptions/OFCopyItemFailedException.m; sourceTree = ""; }; - 4B66B634182ADC45000A69E3 /* OFDeflate64Stream.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = OFDeflate64Stream.h; path = src/OFDeflate64Stream.h; sourceTree = ""; }; - 4B66B635182ADC45000A69E3 /* OFDeflate64Stream.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; name = OFDeflate64Stream.m; path = src/OFDeflate64Stream.m; sourceTree = ""; }; 4B6743EB163C384A00EB1E59 /* OFLockFailedException.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = OFLockFailedException.h; path = src/exceptions/OFLockFailedException.h; sourceTree = ""; }; 4B6743EC163C384A00EB1E59 /* OFLockFailedException.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; name = OFLockFailedException.m; path = src/exceptions/OFLockFailedException.m; sourceTree = ""; }; 4B6743ED163C384A00EB1E59 /* OFStillLockedException.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = OFStillLockedException.h; path = src/exceptions/OFStillLockedException.h; sourceTree = ""; }; 4B6743EE163C384A00EB1E59 /* OFStillLockedException.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; name = OFStillLockedException.m; path = src/exceptions/OFStillLockedException.m; sourceTree = ""; }; 4B6743EF163C384A00EB1E59 /* OFUnlockFailedException.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = OFUnlockFailedException.h; path = src/exceptions/OFUnlockFailedException.h; sourceTree = ""; }; @@ -818,10 +816,14 @@ 4B6EF67F1235358D0076B512 /* PropertiesTests.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; name = PropertiesTests.m; path = tests/PropertiesTests.m; sourceTree = SOURCE_ROOT; }; 4B6EF6801235358D0076B512 /* TestsAppDelegate.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = TestsAppDelegate.h; path = tests/TestsAppDelegate.h; sourceTree = SOURCE_ROOT; }; 4B6EF6811235358D0076B512 /* TestsAppDelegate.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; name = TestsAppDelegate.m; path = tests/TestsAppDelegate.m; sourceTree = SOURCE_ROOT; }; 4B6EF684123535B60076B512 /* TestPlugin.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; name = TestPlugin.m; path = tests/plugin/TestPlugin.m; sourceTree = SOURCE_ROOT; }; 4B6EF685123535C80076B512 /* test.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; name = test.m; path = tests/objc_sync/test.m; sourceTree = SOURCE_ROOT; }; + 4B70A9901B07DEBE00CC5593 /* OFInflate64Stream.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = OFInflate64Stream.h; path = src/OFInflate64Stream.h; sourceTree = ""; }; + 4B70A9911B07DEBE00CC5593 /* OFInflateStream.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = OFInflateStream.h; path = src/OFInflateStream.h; sourceTree = ""; }; + 4B70A9941B07DF9700CC5593 /* OFInflate64Stream.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; name = OFInflate64Stream.m; path = src/OFInflate64Stream.m; sourceTree = ""; }; + 4B70A9951B07DF9700CC5593 /* OFInflateStream.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; name = OFInflateStream.m; path = src/OFInflateStream.m; sourceTree = ""; }; 4B7161AB17A6FC7600B74970 /* OFHTTPResponse.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = OFHTTPResponse.h; path = src/OFHTTPResponse.h; sourceTree = ""; }; 4B7161AC17A6FC7600B74970 /* OFHTTPResponse.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; name = OFHTTPResponse.m; path = src/OFHTTPResponse.m; sourceTree = ""; }; 4B72F7DC1AD9311B00CE253C /* OFStatItemFailedException.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = OFStatItemFailedException.h; path = src/exceptions/OFStatItemFailedException.h; sourceTree = ""; }; 4B72F7DD1AD9311B00CE253C /* OFStatItemFailedException.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; name = OFStatItemFailedException.m; path = src/exceptions/OFStatItemFailedException.m; sourceTree = ""; }; 4B745BA3168B25E600A6C20E /* OFSystemInfo.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = OFSystemInfo.h; path = src/OFSystemInfo.h; sourceTree = ""; }; @@ -841,12 +843,10 @@ 4B837D7816829C5F007A3E83 /* instance.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = instance.h; path = src/instance.h; sourceTree = ""; }; 4B8385121951BF9500D5358A /* OFSettings_INIFile.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = OFSettings_INIFile.h; path = src/OFSettings_INIFile.h; sourceTree = ""; }; 4B8385131951BF9500D5358A /* OFSettings_INIFile.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; name = OFSettings_INIFile.m; path = src/OFSettings_INIFile.m; sourceTree = ""; }; 4B8385141951BF9500D5358A /* OFSettings.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = OFSettings.h; path = src/OFSettings.h; sourceTree = ""; }; 4B8385151951BF9500D5358A /* OFSettings.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; name = OFSettings.m; path = src/OFSettings.m; sourceTree = ""; }; - 4B86E7C917F8B98200ACA680 /* OFDeflateStream.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = OFDeflateStream.h; path = src/OFDeflateStream.h; sourceTree = ""; }; - 4B86E7CA17F8B98200ACA680 /* OFDeflateStream.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; name = OFDeflateStream.m; path = src/OFDeflateStream.m; sourceTree = ""; }; 4B879A89177231F000EBCEA4 /* OFDataArray+MessagePackValue.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = "OFDataArray+MessagePackValue.h"; path = "src/OFDataArray+MessagePackValue.h"; sourceTree = ""; }; 4B879A8A177231F000EBCEA4 /* OFDataArray+MessagePackValue.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; name = "OFDataArray+MessagePackValue.m"; path = "src/OFDataArray+MessagePackValue.m"; sourceTree = ""; }; 4B879A8B177231F000EBCEA4 /* OFMessagePackRepresentation.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = OFMessagePackRepresentation.h; path = src/OFMessagePackRepresentation.h; sourceTree = ""; }; 4B8B025717BBA7C7009ED983 /* OFZIPArchiveEntry.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = OFZIPArchiveEntry.h; path = src/OFZIPArchiveEntry.h; sourceTree = ""; }; 4B8B025817BBA7C7009ED983 /* OFZIPArchiveEntry.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; name = OFZIPArchiveEntry.m; path = src/OFZIPArchiveEntry.m; sourceTree = ""; }; @@ -1289,14 +1289,10 @@ 4BE17ADA12FD746D002CEB0B /* OFDataArray+Hashing.m */, 4B879A89177231F000EBCEA4 /* OFDataArray+MessagePackValue.h */, 4B879A8A177231F000EBCEA4 /* OFDataArray+MessagePackValue.m */, 4BE5F0D912DF4225005C7A0C /* OFDate.h */, 4BE5F0DA12DF4225005C7A0C /* OFDate.m */, - 4B86E7C917F8B98200ACA680 /* OFDeflateStream.h */, - 4B86E7CA17F8B98200ACA680 /* OFDeflateStream.m */, - 4B66B634182ADC45000A69E3 /* OFDeflate64Stream.h */, - 4B66B635182ADC45000A69E3 /* OFDeflate64Stream.m */, 4B6799621099E7C50041064A /* OFDictionary.h */, 4B6799631099E7C50041064A /* OFDictionary.m */, 4B2B3E75140D430500EC2F7C /* OFDictionary_hashtable.h */, 4B2B3E76140D430500EC2F7C /* OFDictionary_hashtable.m */, 4B0108C910EB8C9300631877 /* OFEnumerator.h */, @@ -1310,10 +1306,14 @@ 4B99251012E0780000215DBE /* OFHTTPRequest.m */, 4B7161AB17A6FC7600B74970 /* OFHTTPResponse.h */, 4B7161AC17A6FC7600B74970 /* OFHTTPResponse.m */, 4BB4B54116775FF4002A2DCE /* OFHTTPServer.h */, 4BB4B54216775FF4002A2DCE /* OFHTTPServer.m */, + 4B70A9901B07DEBE00CC5593 /* OFInflate64Stream.h */, + 4B70A9941B07DF9700CC5593 /* OFInflate64Stream.m */, + 4B70A9911B07DEBE00CC5593 /* OFInflateStream.h */, + 4B70A9951B07DF9700CC5593 /* OFInflateStream.m */, 4B06855018B2AD3800FC731A /* OFINICategory.h */, 4B06855118B2AD3800FC731A /* OFINICategory.m */, 4B06855218B2AD3800FC731A /* OFINICategory+Private.h */, 4B5B02BC18B288A400CE6AE4 /* OFINIFile.h */, 4B5B02BD18B288A400CE6AE4 /* OFINIFile.m */, @@ -1634,20 +1634,20 @@ 4B3D23C21337FC8300DD29B8 /* OFDataArray.h in Headers */, 4B3D23C31337FC8300DD29B8 /* OFDataArray+Hashing.h in Headers */, 4B879A8C177231F000EBCEA4 /* OFDataArray+MessagePackValue.h in Headers */, 4B3D23C41337FC8300DD29B8 /* OFDate.h in Headers */, 4B3D23C51337FCB000DD29B8 /* OFDictionary.h in Headers */, - 4B86E7CB17F8B98200ACA680 /* OFDeflateStream.h in Headers */, - 4B66B636182ADC45000A69E3 /* OFDeflate64Stream.h in Headers */, 4B3D23C61337FCB000DD29B8 /* OFEnumerator.h in Headers */, 4B17FF74133A2AAB003E6DCD /* OFException.h in Headers */, 4B3D23C81337FCB000DD29B8 /* OFFile.h in Headers */, 4B3D23C91337FCB000DD29B8 /* OFHash.h in Headers */, 4BB4B54416775FF4002A2DCE /* OFHTTPClient.h in Headers */, 4B3D23CA1337FCB000DD29B8 /* OFHTTPRequest.h in Headers */, 4B7161AD17A6FC7600B74970 /* OFHTTPResponse.h in Headers */, 4BB4B54616775FF4002A2DCE /* OFHTTPServer.h in Headers */, + 4B70A9921B07DEBF00CC5593 /* OFInflate64Stream.h in Headers */, + 4B70A9931B07DEBF00CC5593 /* OFInflateStream.h in Headers */, 4B06855318B2AD3800FC731A /* OFINICategory.h in Headers */, 4B5B02BE18B288A400CE6AE4 /* OFINIFile.h in Headers */, 4BA49D9013DB113B00381CDB /* OFIntrospection.h in Headers */, 4BA02BA115041F5900002F84 /* OFJSONRepresentation.h in Headers */, 4B0EA9211898690E00F573A4 /* OFKernelEventObserver.h in Headers */, @@ -2012,20 +2012,20 @@ 4BA85BCB140ECCE800E91D51 /* OFCountedSet_hashtable.m in Sources */, 4B3D23901337FC0D00DD29B8 /* OFDataArray.m in Sources */, 4B3D23911337FC0D00DD29B8 /* OFDataArray+Hashing.m in Sources */, 4B879A8D177231F000EBCEA4 /* OFDataArray+MessagePackValue.m in Sources */, 4B3D23921337FC0D00DD29B8 /* OFDate.m in Sources */, - 4B86E7CC17F8B98200ACA680 /* OFDeflateStream.m in Sources */, - 4B66B637182ADC45000A69E3 /* OFDeflate64Stream.m in Sources */, 4B3D23931337FC0D00DD29B8 /* OFDictionary.m in Sources */, 4B2B3E80140D430500EC2F7C /* OFDictionary_hashtable.m in Sources */, 4B3D23941337FC0D00DD29B8 /* OFEnumerator.m in Sources */, 4B3D23961337FC0D00DD29B8 /* OFFile.m in Sources */, 4BB4B54516775FF4002A2DCE /* OFHTTPClient.m in Sources */, 4B3D23981337FC0D00DD29B8 /* OFHTTPRequest.m in Sources */, 4B7161AE17A6FC7600B74970 /* OFHTTPResponse.m in Sources */, 4BB4B54716775FF4002A2DCE /* OFHTTPServer.m in Sources */, + 4B70A9961B07DF9700CC5593 /* OFInflate64Stream.m in Sources */, + 4B70A9971B07DF9700CC5593 /* OFInflateStream.m in Sources */, 4B06855418B2AD3800FC731A /* OFINICategory.m in Sources */, 4B5B02BF18B288A400CE6AE4 /* OFINIFile.m in Sources */, 4BA49D9113DB113B00381CDB /* OFIntrospection.m in Sources */, 4B0EA9221898690E00F573A4 /* OFKernelEventObserver.m in Sources */, 4B0EA91C1898690E00F573A4 /* OFKernelEventObserver_kqueue.m in Sources */, Index: src/Makefile ================================================================== --- src/Makefile +++ src/Makefile @@ -19,14 +19,14 @@ OFCountedSet.m \ OFDataArray.m \ OFDataArray+Hashing.m \ OFDataArray+MessagePackValue.m \ OFDate.m \ - OFDeflateStream.m \ - OFDeflate64Stream.m \ OFDictionary.m \ OFEnumerator.m \ + OFInflateStream.m \ + OFInflate64Stream.m \ OFIntrospection.m \ OFList.m \ OFMapTable.m \ OFMD5Hash.m \ OFMessagePackExtension.m \ DELETED src/OFDeflate64Stream.h Index: src/OFDeflate64Stream.h ================================================================== --- src/OFDeflate64Stream.h +++ src/OFDeflate64Stream.h @@ -1,26 +0,0 @@ -/* - * Copyright (c) 2008, 2009, 2010, 2011, 2012, 2013, 2014, 2015 - * Jonathan Schleifer - * - * All rights reserved. - * - * This file is part of ObjFW. It may be distributed under the terms of the - * Q Public License 1.0, which can be found in the file LICENSE.QPL included in - * the packaging of this file. - * - * Alternatively, it may be distributed under the terms of the GNU General - * Public License, either version 2 or 3, which can be found in the file - * LICENSE.GPLv2 or LICENSE.GPLv3 respectively included in the packaging of this - * file. - */ - -#import "OFDeflateStream.h" - -/*! - * @class OFDeflate64Stream OFDeflate64Stream.h ObjFW/OFDeflate64Stream.h - * - * @brief A class for a stream that handles Deflate64 compression or - * decompression transparently for an underlying stream. - */ -@interface OFDeflate64Stream: OFDeflateStream -@end DELETED src/OFDeflate64Stream.m Index: src/OFDeflate64Stream.m ================================================================== --- src/OFDeflate64Stream.m +++ src/OFDeflate64Stream.m @@ -1,18 +0,0 @@ -/* - * Copyright (c) 2008, 2009, 2010, 2011, 2012, 2013, 2014, 2015 - * Jonathan Schleifer - * - * All rights reserved. - * - * This file is part of ObjFW. It may be distributed under the terms of the - * Q Public License 1.0, which can be found in the file LICENSE.QPL included in - * the packaging of this file. - * - * Alternatively, it may be distributed under the terms of the GNU General - * Public License, either version 2 or 3, which can be found in the file - * LICENSE.GPLv2 or LICENSE.GPLv3 respectively included in the packaging of this - * file. - */ - -#define DEFLATE64 -#include "OFDeflateStream.m" DELETED src/OFDeflateStream.h Index: src/OFDeflateStream.h ================================================================== --- src/OFDeflateStream.h +++ src/OFDeflateStream.h @@ -1,99 +0,0 @@ -/* - * Copyright (c) 2008, 2009, 2010, 2011, 2012, 2013, 2014, 2015 - * Jonathan Schleifer - * - * All rights reserved. - * - * This file is part of ObjFW. It may be distributed under the terms of the - * Q Public License 1.0, which can be found in the file LICENSE.QPL included in - * the packaging of this file. - * - * Alternatively, it may be distributed under the terms of the GNU General - * Public License, either version 2 or 3, which can be found in the file - * LICENSE.GPLv2 or LICENSE.GPLv3 respectively included in the packaging of this - * file. - */ - -#import "OFStream.h" - -#define OF_DEFLATE_STREAM_BUFFER_SIZE 4096 - -/*! - * @class OFDeflateStream OFDeflateStream.h ObjFW/OFDeflateStream.h - * - * @brief A class for a stream that handles Deflate compression or decompression - * transparently for an underlying stream. - */ -@interface OFDeflateStream: OFStream -{ -#ifdef OF_DEFLATE_STREAM_M -@public -#endif - OFStream *_stream; - uint8_t _buffer[OF_DEFLATE_STREAM_BUFFER_SIZE]; - uint_fast16_t _bufferIndex, _bufferLength; - uint8_t _byte; - uint_fast8_t _bitIndex, _savedBitsLength; - uint_fast16_t _savedBits; -@protected - uint8_t *_slidingWindow; - uint_fast16_t _slidingWindowIndex, _slidingWindowMask; - enum { - OF_DEFLATE_STREAM_BLOCK_HEADER, - OF_DEFLATE_STREAM_UNCOMPRESSED_BLOCK_HEADER, - OF_DEFLATE_STREAM_UNCOMPRESSED_BLOCK, - OF_DEFLATE_STREAM_HUFFMAN_TREE, - OF_DEFLATE_STREAM_HUFFMAN_BLOCK - } _state; - union { - struct { - uint_fast8_t position; - uint8_t length[4]; - } uncompressedHeader; - struct { - uint_fast16_t position, length; - } uncompressed; - struct { - struct huffman_tree *litLenTree, *distTree; - struct huffman_tree *codeLenTree, *treeIter; - uint8_t *lengths; - uint_fast16_t receivedCount; - uint_fast8_t value, litLenCodesCount, distCodesCount; - uint_fast8_t codeLenCodesCount; - } huffmanTree; - struct { - struct huffman_tree *litLenTree, *distTree, *treeIter; - enum { - OF_DEFLATE_STREAM_WRITE_VALUE, - OF_DEFLATE_STREAM_AWAIT_CODE, - OF_DEFLATE_STREAM_AWAIT_LENGTH_EXTRA_BITS, - OF_DEFLATE_STREAM_AWAIT_DISTANCE, - OF_DEFLATE_STREAM_AWAIT_DISTANCE_EXTRA_BITS, - OF_DEFLATE_STREAM_PROCESS_PAIR - } state; - uint_fast16_t value, length, distance; - uint_fast16_t extraBits; - } huffman; - } _context; - bool _inLastBlock, _atEndOfStream; -} - -/*! - * @brief Creates a new OFDeflateStream with the specified underlying stream. - * - * @param stream The underlying stream to which compressed data is written or - * from which compressed data is read - * @return A new, autoreleased OFDeflateStream - */ -+ (instancetype)streamWithStream: (OFStream*)stream; - -/*! - * @brief Initializes an already allocated OFDeflateStream with the specified - * underlying stream. - * - * @param stream The underlying stream to which compressed data is written or - * from which compressed data is read - * @return A initialized OFDeflateStream - */ -- initWithStream: (OFStream*)stream; -@end DELETED src/OFDeflateStream.m Index: src/OFDeflateStream.m ================================================================== --- src/OFDeflateStream.m +++ src/OFDeflateStream.m @@ -1,787 +0,0 @@ -/* - * Copyright (c) 2008, 2009, 2010, 2011, 2012, 2013, 2014, 2015 - * Jonathan Schleifer - * - * All rights reserved. - * - * This file is part of ObjFW. It may be distributed under the terms of the - * Q Public License 1.0, which can be found in the file LICENSE.QPL included in - * the packaging of this file. - * - * Alternatively, it may be distributed under the terms of the GNU General - * Public License, either version 2 or 3, which can be found in the file - * LICENSE.GPLv2 or LICENSE.GPLv3 respectively included in the packaging of this - * file. - */ - -#define OF_DEFLATE_STREAM_M - -#include "config.h" - -#include -#include - -#include - -#ifndef DEFLATE64 -# import "OFDeflateStream.h" -#else -# import "OFDeflate64Stream.h" -# define OFDeflateStream OFDeflate64Stream -#endif -#import "OFDataArray.h" - -#import "OFInitializationFailedException.h" -#import "OFInvalidFormatException.h" -#import "OFOutOfMemoryException.h" -#import "OFReadFailedException.h" - -#define BLOCK_HEADER OF_DEFLATE_STREAM_BLOCK_HEADER -#define UNCOMPRESSED_BLOCK_HEADER OF_DEFLATE_STREAM_UNCOMPRESSED_BLOCK_HEADER -#define UNCOMPRESSED_BLOCK OF_DEFLATE_STREAM_UNCOMPRESSED_BLOCK -#define HUFFMAN_BLOCK OF_DEFLATE_STREAM_HUFFMAN_BLOCK -#define HUFFMAN_TREE OF_DEFLATE_STREAM_HUFFMAN_TREE -#define CONSTRUCT_CODELEN_TREE OF_DEFLATE_STREAM_CONSTRUCT_CODELEN_TREE -#define CONSTRUCT_LITLEN_TREE OF_DEFLATE_STREAM_CONSTRUCT_LITLEN_TREE -#define CONSTRUCT_DIST_TREE OF_DEFLATE_STREAM_CONSTRUCT_DIST_TREE -#define AWAIT_CODE OF_DEFLATE_STREAM_AWAIT_CODE -#define WRITE_VALUE OF_DEFLATE_STREAM_WRITE_VALUE -#define AWAIT_LENGTH_EXTRA_BITS OF_DEFLATE_STREAM_AWAIT_LENGTH_EXTRA_BITS -#define AWAIT_DISTANCE OF_DEFLATE_STREAM_AWAIT_DISTANCE -#define AWAIT_DISTANCE_EXTRA_BITS OF_DEFLATE_STREAM_AWAIT_DISTANCE_EXTRA_BITS -#define PROCESS_PAIR OF_DEFLATE_STREAM_PROCESS_PAIR -#define BUFFER_SIZE OF_DEFLATE_STREAM_BUFFER_SIZE - -#define MAX_BITS 15 - -struct huffman_tree { - struct huffman_tree *leafs[2]; - uint16_t value; -}; - -#ifndef DEFLATE64 -static const uint_fast8_t numDistanceCodes = 30; -static const uint8_t lengthCodes[29] = { - /* indices are -257, values -3 */ - 0, 1, 2, 3, 4, 5, 6, 7, 8, 10, 12, 14, 16, 20, 24, 28, 32, 40, 48, 56, - 64, 80, 96, 112, 128, 160, 192, 224, 255 -}; -static const uint8_t lengthExtraBits[29] = { - 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 2, 2, 2, 2, 3, 3, 3, 3, 4, 4, 4, 4, - 5, 5, 5, 5, 0 -}; -static const uint16_t distanceCodes[30] = { - 1, 2, 3, 4, 5, 7, 9, 13, 17, 25, 33, 49, 65, 97, 129, 193, 257, 385, - 513, 769, 1025, 1537, 2049, 3073, 4097, 6145, 8193, 12289, 16385, 24577 -}; -static const uint8_t distanceExtraBits[30] = { - 0, 0, 0, 0, 1, 1, 2, 2, 3, 3, 4, 4, 5, 5, 6, 6, 7, 7, 8, 8, 9, 9, 10, - 10, 11, 11, 12, 12, 13, 13 -}; -#else -static const uint_fast8_t numDistanceCodes = 32; -static const uint8_t lengthCodes[29] = { - /* indices are -257, values -3 */ - 0, 1, 2, 3, 4, 5, 6, 7, 8, 10, 12, 14, 16, 20, 24, 28, 32, 40, 48, 56, - 64, 80, 96, 112, 128, 160, 192, 224, 0 -}; -static const uint8_t lengthExtraBits[29] = { - 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 2, 2, 2, 2, 3, 3, 3, 3, 4, 4, 4, 4, - 5, 5, 5, 5, 16 -}; -static const uint16_t distanceCodes[32] = { - 1, 2, 3, 4, 5, 7, 9, 13, 17, 25, 33, 49, 65, 97, 129, 193, 257, 385, - 513, 769, 1025, 1537, 2049, 3073, 4097, 6145, 8193, 12289, 16385, 24577, - 32769, 49153 -}; -static const uint8_t distanceExtraBits[32] = { - 0, 0, 0, 0, 1, 1, 2, 2, 3, 3, 4, 4, 5, 5, 6, 6, 7, 7, 8, 8, 9, 9, 10, - 10, 11, 11, 12, 12, 13, 13, 14, 14 -}; -#endif -static const uint8_t codeLengthsOrder[19] = { - 16, 17, 18, 0, 8, 7, 9, 6, 10, 5, 11, 4, 12, 3, 13, 2, 14, 1, 15 -}; -static struct huffman_tree *fixedLitLenTree, *fixedDistTree; - -static bool -tryReadBits(OFDeflateStream *stream, uint_fast16_t *bits, uint_fast8_t count) -{ - uint_fast16_t ret = stream->_savedBits; - uint_fast8_t i; - - assert(stream->_savedBitsLength < count); - - for (i = stream->_savedBitsLength; i < count; i++) { - if OF_UNLIKELY (stream->_bitIndex == 8) { - if (stream->_bufferIndex < stream->_bufferLength) - stream->_byte = - stream->_buffer[stream->_bufferIndex++]; - else { - size_t length = [stream->_stream - readIntoBuffer: stream->_buffer - length: BUFFER_SIZE]; - - if OF_UNLIKELY (length < 1) { - stream->_savedBits = ret; - stream->_savedBitsLength = i; - return false; - } - - stream->_byte = stream->_buffer[0]; - stream->_bufferIndex = 1; - stream->_bufferLength = (uint_fast16_t)length; - } - - stream->_bitIndex = 0; - } - - ret |= ((stream->_byte >> stream->_bitIndex++) & 1) << i; - } - - stream->_savedBits = 0; - stream->_savedBitsLength = 0; - *bits = ret; - - return true; -} - -static struct huffman_tree* -newTree(void) -{ - struct huffman_tree *tree; - - if ((tree = malloc(sizeof(*tree))) == NULL) - @throw [OFOutOfMemoryException - exceptionWithRequestedSize: sizeof(*tree)]; - - tree->leafs[0] = tree->leafs[1] = NULL; - tree->value = 0xFFFF; - - return tree; -} - -static void -treeInsert(struct huffman_tree *tree, uint_fast16_t code, uint_fast8_t length, - uint16_t value) -{ - while (length > 0) { - uint_fast8_t bit; - - length--; - bit = (code & (1 << length)) >> length; - - if (tree->leafs[bit] == NULL) - tree->leafs[bit] = newTree(); - - tree = tree->leafs[bit]; - } - - tree->value = value; -} - -static struct huffman_tree* -constructTree(uint8_t lengths[], uint_fast16_t count) -{ - struct huffman_tree *tree; - uint16_t lengthCount[MAX_BITS + 1] = { 0 }; - uint16_t code, maxCode = 0, nextCode[MAX_BITS + 1]; - uint_fast16_t i; - - for (i = 0; i < count; i++) { - uint8_t length = lengths[i]; - - if OF_UNLIKELY (length > MAX_BITS) - @throw [OFInvalidFormatException exception]; - - if (length > 0) { - lengthCount[length]++; - maxCode = i; - } - } - - code = 0; - for (i = 1; i <= MAX_BITS; i++) { - code = (code + lengthCount[i - 1]) << 1; - nextCode[i] = code; - } - - tree = newTree(); - - for (i = 0; i <= maxCode; i++) { - uint8_t length = lengths[i]; - - if (length > 0) - treeInsert(tree, nextCode[length]++, length, i); - } - - return tree; -} - -static bool -walkTree(OFDeflateStream *stream, struct huffman_tree **tree, uint16_t *value) -{ - struct huffman_tree *iter = *tree; - uint_fast16_t bits; - - while (iter->value == 0xFFFF) { - if OF_UNLIKELY (!tryReadBits(stream, &bits, 1)) { - *tree = iter; - return false; - } - - if OF_UNLIKELY (iter->leafs[bits] == NULL) - @throw [OFInvalidFormatException exception]; - - iter = iter->leafs[bits]; - } - - *value = iter->value; - return true; -} - -static void -releaseTree(struct huffman_tree *tree) -{ - uint_fast8_t i; - - for (i = 0; i < 2; i++) - if OF_LIKELY (tree->leafs[i] != NULL) - releaseTree(tree->leafs[i]); - - free(tree); -} - -@implementation OFDeflateStream -+ (void)initialize -{ - uint_fast16_t i; - uint8_t lengths[288]; - - if (self != [OFDeflateStream class]) - return; - - for (i = 0; i <= 143; i++) - lengths[i] = 8; - for (i = 144; i <= 255; i++) - lengths[i] = 9; - for (i = 256; i <= 279; i++) - lengths[i] = 7; - for (i = 280; i <= 287; i++) - lengths[i] = 8; - - fixedLitLenTree = constructTree(lengths, 288); - - for (i = 0; i <= 31; i++) - lengths[i] = 5; - - fixedDistTree = constructTree(lengths, 32); -} - -#ifndef DEFLATE64 -+ (instancetype)streamWithStream: (OFStream*)stream -{ - return [[[self alloc] initWithStream: stream] autorelease]; -} - -- init -{ - OF_INVALID_INIT_METHOD -} - -- initWithStream: (OFStream*)stream -{ - self = [super init]; - - _stream = [stream retain]; - _bitIndex = 8; /* 0-7 address the bit, 8 means fetch next byte */ - _slidingWindowMask = 0x7FFF; - - return self; -} - -- (void)dealloc -{ - [_stream release]; - - [super dealloc]; -} -#endif - -- (size_t)lowlevelReadIntoBuffer: (void*)buffer_ - length: (size_t)length -{ - uint8_t *buffer = buffer_; - uint_fast16_t bits, i, tmp; - uint16_t value; - size_t bytesWritten = 0; - uint8_t *slidingWindow; - uint_fast16_t slidingWindowIndex; - - if (_atEndOfStream) - @throw [OFReadFailedException exceptionWithObject: self - requestedLength: length]; - -start: - switch (_state) { - case BLOCK_HEADER: - if OF_UNLIKELY (_inLastBlock) { - [_stream unreadFromBuffer: _buffer + _bufferIndex - length: _bufferLength - - _bufferIndex]; - - _atEndOfStream = true; - return bytesWritten; - } - - if OF_UNLIKELY (!tryReadBits(self, &bits, 3)) - return bytesWritten; - - _inLastBlock = (bits & 1); - - switch (bits >> 1) { - case 0: /* No compression */ - _state = UNCOMPRESSED_BLOCK_HEADER; - _bitIndex = 8; - _context.uncompressedHeader.position = 0; - memset(_context.uncompressedHeader.length, 0, 4); - break; - case 1: /* Fixed Huffman */ - _state = HUFFMAN_BLOCK; - _context.huffman.state = AWAIT_CODE; - _context.huffman.litLenTree = fixedLitLenTree; - _context.huffman.distTree = fixedDistTree; - _context.huffman.treeIter = fixedLitLenTree; - break; - case 2: /* Dynamic Huffman */ - _state = HUFFMAN_TREE; - _context.huffmanTree.lengths = NULL; - _context.huffmanTree.receivedCount = 0; - _context.huffmanTree.value = 0xFE; - _context.huffmanTree.litLenCodesCount = 0xFF; - _context.huffmanTree.distCodesCount = 0xFF; - _context.huffmanTree.codeLenCodesCount = 0xFF; - break; - default: - @throw [OFInvalidFormatException exception]; - } - - goto start; - case UNCOMPRESSED_BLOCK_HEADER: -#define CTX _context.uncompressedHeader - /* FIXME: This can be done more efficiently than unreading */ - [_stream unreadFromBuffer: _buffer + _bufferIndex - length: _bufferLength - _bufferIndex]; - _bufferIndex = _bufferLength = 0; - - CTX.position += [_stream - readIntoBuffer: CTX.length + CTX.position - length: 4 - CTX.position]; - - if OF_UNLIKELY (CTX.position < 4) - return bytesWritten; - - if OF_UNLIKELY ((CTX.length[0] | (CTX.length[1] << 8)) != - (uint16_t)~(CTX.length[2] | (CTX.length[3] << 8))) - @throw [OFInvalidFormatException exception]; - - _state = UNCOMPRESSED_BLOCK; - - /* - * Do not reorder! _context.uncompressed.position and - * _context.uncompressedHeader.length overlap! - */ - _context.uncompressed.length = - CTX.length[0] | (CTX.length[1] << 8); - _context.uncompressed.position = 0; - - goto start; -#undef CTX - case UNCOMPRESSED_BLOCK: -#define CTX _context.uncompressed - if OF_UNLIKELY (length == 0) - return bytesWritten; - - tmp = (length < CTX.length - CTX.position - ? (uint_fast16_t)length : CTX.length - CTX.position); - - tmp = (uint_fast16_t)[_stream - readIntoBuffer: buffer + bytesWritten - length: tmp]; - - if OF_UNLIKELY (_slidingWindow == NULL) { - _slidingWindow = - [self allocMemoryWithSize: _slidingWindowMask + 1]; - /* Avoid leaking data */ - memset(_slidingWindow, 0, _slidingWindowMask + 1); - } - - slidingWindow = _slidingWindow; - slidingWindowIndex = _slidingWindowIndex; - for (i = 0; i < tmp; i++) { - slidingWindow[slidingWindowIndex] = - buffer[bytesWritten + i]; - slidingWindowIndex = (slidingWindowIndex + 1) & - _slidingWindowMask; - } - _slidingWindowIndex = slidingWindowIndex; - - length -= tmp; - bytesWritten += tmp; - - CTX.position += tmp; - if OF_UNLIKELY (CTX.position == CTX.length) - _state = BLOCK_HEADER; - - goto start; -#undef CTX - case HUFFMAN_TREE: -#define CTX _context.huffmanTree - if OF_LIKELY (CTX.value == 0xFE) { - if OF_LIKELY (CTX.litLenCodesCount == 0xFF) { - if OF_UNLIKELY (!tryReadBits(self, &bits, 5)) - return bytesWritten; - - if OF_UNLIKELY (bits > 29) - @throw [OFInvalidFormatException - exception]; - - CTX.litLenCodesCount = bits; - } - - if OF_LIKELY (CTX.distCodesCount == 0xFF) { - if OF_UNLIKELY (!tryReadBits(self, &bits, 5)) - return bytesWritten; - - CTX.distCodesCount = bits; - } - - if OF_LIKELY (CTX.codeLenCodesCount == 0xFF) { - if OF_UNLIKELY (!tryReadBits(self, &bits, 4)) - return bytesWritten; - - CTX.codeLenCodesCount = bits; - } - - if OF_LIKELY (CTX.lengths == NULL) { - CTX.lengths = [self allocMemoryWithSize: 19]; - memset(CTX.lengths, 0, 19); - } - - for (i = CTX.receivedCount; - i < CTX.codeLenCodesCount + 4; i++) { - if OF_UNLIKELY (!tryReadBits(self, &bits, 3)) { - CTX.receivedCount = i; - return bytesWritten; - } - - CTX.lengths[codeLengthsOrder[i]] = bits; - } - - CTX.codeLenTree = constructTree(CTX.lengths, 19); - CTX.treeIter = CTX.codeLenTree; - - [self freeMemory: CTX.lengths]; - CTX.lengths = NULL; - CTX.receivedCount = 0; - CTX.value = 0xFF; - } - - if OF_LIKELY (CTX.lengths == NULL) - CTX.lengths = [self allocMemoryWithSize: - CTX.litLenCodesCount + CTX.distCodesCount + 258]; - - for (i = CTX.receivedCount; - i < CTX.litLenCodesCount + CTX.distCodesCount + 258;) { - uint_fast8_t j, count; - - if OF_LIKELY (CTX.value == 0xFF) { - if OF_UNLIKELY (!walkTree(self, &CTX.treeIter, - &value)) { - CTX.receivedCount = i; - return bytesWritten; - } - - CTX.treeIter = CTX.codeLenTree; - - if (value < 16) { - CTX.lengths[i++] = value; - continue; - } - } else - value = CTX.value; - - switch (value) { - case 16: - if OF_UNLIKELY (i < 1) - @throw [OFInvalidFormatException - exception]; - - if OF_UNLIKELY (!tryReadBits(self, &bits, 2)) { - CTX.receivedCount = i; - CTX.value = value; - return bytesWritten; - } - - value = CTX.lengths[i - 1]; - count = bits + 3; - - break; - case 17: - if OF_UNLIKELY (!tryReadBits(self, &bits, 3)) { - CTX.receivedCount = i; - CTX.value = value; - return bytesWritten; - } - - value = 0; - count = bits + 3; - - break; - case 18: - if OF_UNLIKELY (!tryReadBits(self, &bits, 7)) { - CTX.receivedCount = i; - CTX.value = value; - return bytesWritten; - } - - value = 0; - count = bits + 11; - - break; - default: - @throw [OFInvalidFormatException exception]; - } - - if OF_UNLIKELY (i + count > - CTX.litLenCodesCount + CTX.distCodesCount + 258) - @throw [OFInvalidFormatException exception]; - - for (j = 0; j < count; j++) - CTX.lengths[i++] = value; - - CTX.value = 0xFF; - } - - releaseTree(CTX.codeLenTree); - - CTX.litLenTree = constructTree(CTX.lengths, - CTX.litLenCodesCount + 257); - CTX.distTree = constructTree( - CTX.lengths + CTX.litLenCodesCount + 257, - CTX.distCodesCount + 1); - - [self freeMemory: CTX.lengths]; - - /* - * litLenTree and distTree are at the same location in - * _context.huffman and _context.huffmanTree, thus no need to - * set them. - */ - _state = HUFFMAN_BLOCK; - _context.huffman.state = AWAIT_CODE; - _context.huffman.treeIter = CTX.litLenTree; - - goto start; -#undef CTX - case HUFFMAN_BLOCK: -#define CTX _context.huffman - for (;;) { - uint_fast8_t extraBits, lengthCodeIndex; - - if OF_UNLIKELY (CTX.state == WRITE_VALUE) { - if OF_UNLIKELY (length == 0) - return bytesWritten; - - buffer[bytesWritten++] = CTX.value; - length--; - - if (_slidingWindow == NULL) { - _slidingWindow = [self - allocMemoryWithSize: - _slidingWindowMask + 1]; - /* Avoid leaking data */ - memset(_slidingWindow, 0, - _slidingWindowMask + 1); - } - - _slidingWindow[_slidingWindowIndex] = CTX.value; - _slidingWindowIndex = - (_slidingWindowIndex + 1) & - _slidingWindowMask; - - CTX.state = AWAIT_CODE; - CTX.treeIter = CTX.litLenTree; - } - - if OF_UNLIKELY (CTX.state == AWAIT_LENGTH_EXTRA_BITS) { - if OF_UNLIKELY (!tryReadBits(self, &bits, - CTX.extraBits)) - return bytesWritten; - - CTX.length += bits; - - CTX.state = AWAIT_DISTANCE; - CTX.treeIter = CTX.distTree; - } - - /* Distance of length distance pair */ - if (CTX.state == AWAIT_DISTANCE) { - if OF_UNLIKELY (!walkTree(self, &CTX.treeIter, - &value)) - return bytesWritten; - - if OF_UNLIKELY (value >= numDistanceCodes) - @throw [OFInvalidFormatException - exception]; - - CTX.distance = distanceCodes[value]; - extraBits = distanceExtraBits[value]; - - if (extraBits > 0) { - if OF_UNLIKELY (!tryReadBits(self, - &bits, extraBits)) { - CTX.state = - AWAIT_DISTANCE_EXTRA_BITS; - CTX.extraBits = extraBits; - return bytesWritten; - } - - CTX.distance += bits; - } - - CTX.state = PROCESS_PAIR; - } else if (CTX.state == AWAIT_DISTANCE_EXTRA_BITS) { - if OF_UNLIKELY (!tryReadBits(self, &bits, - CTX.extraBits)) - return bytesWritten; - - CTX.distance += bits; - - CTX.state = PROCESS_PAIR; - } - - /* Length distance pair */ - if (CTX.state == PROCESS_PAIR) { - uint_fast16_t j; - - if OF_UNLIKELY (_slidingWindow == NULL) - @throw [OFInvalidFormatException - exception]; - - for (j = 0; j < CTX.length; j++) { - uint_fast16_t index; - - if OF_UNLIKELY (length == 0) { - CTX.length -= j; - return bytesWritten; - } - - index = (_slidingWindowIndex - - CTX.distance) & _slidingWindowMask; - value = _slidingWindow[index]; - - buffer[bytesWritten++] = value; - length--; - - _slidingWindow[_slidingWindowIndex] = - value; - _slidingWindowIndex = - (_slidingWindowIndex + 1) & - _slidingWindowMask; - } - - CTX.state = AWAIT_CODE; - CTX.treeIter = CTX.litLenTree; - } - - if OF_UNLIKELY (!walkTree(self, &CTX.treeIter, &value)) - return bytesWritten; - - /* End of block */ - if OF_UNLIKELY (value == 256) { - if (CTX.litLenTree != fixedLitLenTree) - releaseTree(CTX.litLenTree); - if (CTX.distTree != fixedDistTree) - releaseTree(CTX.distTree); - - _state = BLOCK_HEADER; - goto start; - } - - /* Literal byte */ - if (value < 256) { - if OF_UNLIKELY (length == 0) { - CTX.state = WRITE_VALUE; - CTX.value = value; - return bytesWritten; - } - - buffer[bytesWritten++] = value; - length--; - - if (_slidingWindow == NULL) { - _slidingWindow = [self - allocMemoryWithSize: - _slidingWindowMask + 1]; - /* Avoid leaking data */ - memset(_slidingWindow, 0, - _slidingWindowMask + 1); - } - - _slidingWindow[_slidingWindowIndex] = value; - _slidingWindowIndex = - (_slidingWindowIndex + 1) & - _slidingWindowMask; - - CTX.treeIter = CTX.litLenTree; - continue; - } - - if OF_UNLIKELY (value > 285) - @throw [OFInvalidFormatException exception]; - - /* Length of length distance pair */ - lengthCodeIndex = value - 257; - CTX.length = lengthCodes[lengthCodeIndex] + 3; - extraBits = lengthExtraBits[lengthCodeIndex]; - - if (extraBits > 0) { - if OF_UNLIKELY (!tryReadBits(self, &bits, - extraBits)) { - CTX.extraBits = extraBits; - CTX.state = AWAIT_LENGTH_EXTRA_BITS; - return bytesWritten; - } - - CTX.length += bits; - } - - CTX.treeIter = CTX.distTree; - CTX.state = AWAIT_DISTANCE; - } - - break; -#undef CTX - } - - OF_UNREACHABLE -} - -#ifndef DEFLATE64 -- (bool)lowlevelIsAtEndOfStream -{ - return _atEndOfStream; -} - -- (int)fileDescriptorForReading -{ - return [_stream fileDescriptorForReading]; -} - -- (bool)hasDataInReadBuffer -{ - return ([super hasDataInReadBuffer] || [_stream hasDataInReadBuffer]); -} -#endif -@end ADDED src/OFInflate64Stream.h Index: src/OFInflate64Stream.h ================================================================== --- src/OFInflate64Stream.h +++ src/OFInflate64Stream.h @@ -0,0 +1,26 @@ +/* + * Copyright (c) 2008, 2009, 2010, 2011, 2012, 2013, 2014, 2015 + * Jonathan Schleifer + * + * All rights reserved. + * + * This file is part of ObjFW. It may be distributed under the terms of the + * Q Public License 1.0, which can be found in the file LICENSE.QPL included in + * the packaging of this file. + * + * Alternatively, it may be distributed under the terms of the GNU General + * Public License, either version 2 or 3, which can be found in the file + * LICENSE.GPLv2 or LICENSE.GPLv3 respectively included in the packaging of this + * file. + */ + +#import "OFInflateStream.h" + +/*! + * @class OFInflate64Stream OFInflate64Stream.h ObjFW/OFInflate64Stream.h + * + * @brief A class that handles Deflate64 decompression transparently for an + * underlying stream. + */ +@interface OFInflate64Stream: OFInflateStream +@end ADDED src/OFInflate64Stream.m Index: src/OFInflate64Stream.m ================================================================== --- src/OFInflate64Stream.m +++ src/OFInflate64Stream.m @@ -0,0 +1,18 @@ +/* + * Copyright (c) 2008, 2009, 2010, 2011, 2012, 2013, 2014, 2015 + * Jonathan Schleifer + * + * All rights reserved. + * + * This file is part of ObjFW. It may be distributed under the terms of the + * Q Public License 1.0, which can be found in the file LICENSE.QPL included in + * the packaging of this file. + * + * Alternatively, it may be distributed under the terms of the GNU General + * Public License, either version 2 or 3, which can be found in the file + * LICENSE.GPLv2 or LICENSE.GPLv3 respectively included in the packaging of this + * file. + */ + +#define DEFLATE64 +#include "OFInflateStream.m" ADDED src/OFInflateStream.h Index: src/OFInflateStream.h ================================================================== --- src/OFInflateStream.h +++ src/OFInflateStream.h @@ -0,0 +1,99 @@ +/* + * Copyright (c) 2008, 2009, 2010, 2011, 2012, 2013, 2014, 2015 + * Jonathan Schleifer + * + * All rights reserved. + * + * This file is part of ObjFW. It may be distributed under the terms of the + * Q Public License 1.0, which can be found in the file LICENSE.QPL included in + * the packaging of this file. + * + * Alternatively, it may be distributed under the terms of the GNU General + * Public License, either version 2 or 3, which can be found in the file + * LICENSE.GPLv2 or LICENSE.GPLv3 respectively included in the packaging of this + * file. + */ + +#import "OFStream.h" + +#define OF_INFLATE_STREAM_BUFFER_SIZE 4096 + +/*! + * @class OFInflateStream OFInflateStream.h ObjFW/OFInflateStream.h + * + * @brief A class that handles Deflate decompression transparently for an + * underlying stream. + */ +@interface OFInflateStream: OFStream +{ +#ifdef OF_INFLATE_STREAM_M +@public +#endif + OFStream *_stream; + uint8_t _buffer[OF_INFLATE_STREAM_BUFFER_SIZE]; + uint_fast16_t _bufferIndex, _bufferLength; + uint8_t _byte; + uint_fast8_t _bitIndex, _savedBitsLength; + uint_fast16_t _savedBits; +@protected + uint8_t *_slidingWindow; + uint_fast16_t _slidingWindowIndex, _slidingWindowMask; + enum { + OF_INFLATE_STREAM_BLOCK_HEADER, + OF_INFLATE_STREAM_UNCOMPRESSED_BLOCK_HEADER, + OF_INFLATE_STREAM_UNCOMPRESSED_BLOCK, + OF_INFLATE_STREAM_HUFFMAN_TREE, + OF_INFLATE_STREAM_HUFFMAN_BLOCK + } _state; + union { + struct { + uint_fast8_t position; + uint8_t length[4]; + } uncompressedHeader; + struct { + uint_fast16_t position, length; + } uncompressed; + struct { + struct huffman_tree *litLenTree, *distTree; + struct huffman_tree *codeLenTree, *treeIter; + uint8_t *lengths; + uint_fast16_t receivedCount; + uint_fast8_t value, litLenCodesCount, distCodesCount; + uint_fast8_t codeLenCodesCount; + } huffmanTree; + struct { + struct huffman_tree *litLenTree, *distTree, *treeIter; + enum { + OF_INFLATE_STREAM_WRITE_VALUE, + OF_INFLATE_STREAM_AWAIT_CODE, + OF_INFLATE_STREAM_AWAIT_LENGTH_EXTRA_BITS, + OF_INFLATE_STREAM_AWAIT_DISTANCE, + OF_INFLATE_STREAM_AWAIT_DISTANCE_EXTRA_BITS, + OF_INFLATE_STREAM_PROCESS_PAIR + } state; + uint_fast16_t value, length, distance; + uint_fast16_t extraBits; + } huffman; + } _context; + bool _inLastBlock, _atEndOfStream; +} + +/*! + * @brief Creates a new OFInflateStream with the specified underlying stream. + * + * @param stream The underlying stream to which compressed data is written or + * from which compressed data is read + * @return A new, autoreleased OFInflateStream + */ ++ (instancetype)streamWithStream: (OFStream*)stream; + +/*! + * @brief Initializes an already allocated OFInflateStream with the specified + * underlying stream. + * + * @param stream The underlying stream to which compressed data is written or + * from which compressed data is read + * @return A initialized OFInflateStream + */ +- initWithStream: (OFStream*)stream; +@end ADDED src/OFInflateStream.m Index: src/OFInflateStream.m ================================================================== --- src/OFInflateStream.m +++ src/OFInflateStream.m @@ -0,0 +1,787 @@ +/* + * Copyright (c) 2008, 2009, 2010, 2011, 2012, 2013, 2014, 2015 + * Jonathan Schleifer + * + * All rights reserved. + * + * This file is part of ObjFW. It may be distributed under the terms of the + * Q Public License 1.0, which can be found in the file LICENSE.QPL included in + * the packaging of this file. + * + * Alternatively, it may be distributed under the terms of the GNU General + * Public License, either version 2 or 3, which can be found in the file + * LICENSE.GPLv2 or LICENSE.GPLv3 respectively included in the packaging of this + * file. + */ + +#define OF_INFLATE_STREAM_M + +#include "config.h" + +#include +#include + +#include + +#ifndef DEFLATE64 +# import "OFInflateStream.h" +#else +# import "OFInflate64Stream.h" +# define OFInflateStream OFInflate64Stream +#endif +#import "OFDataArray.h" + +#import "OFInitializationFailedException.h" +#import "OFInvalidFormatException.h" +#import "OFOutOfMemoryException.h" +#import "OFReadFailedException.h" + +#define BLOCK_HEADER OF_INFLATE_STREAM_BLOCK_HEADER +#define UNCOMPRESSED_BLOCK_HEADER OF_INFLATE_STREAM_UNCOMPRESSED_BLOCK_HEADER +#define UNCOMPRESSED_BLOCK OF_INFLATE_STREAM_UNCOMPRESSED_BLOCK +#define HUFFMAN_BLOCK OF_INFLATE_STREAM_HUFFMAN_BLOCK +#define HUFFMAN_TREE OF_INFLATE_STREAM_HUFFMAN_TREE +#define CONSTRUCT_CODELEN_TREE OF_INFLATE_STREAM_CONSTRUCT_CODELEN_TREE +#define CONSTRUCT_LITLEN_TREE OF_INFLATE_STREAM_CONSTRUCT_LITLEN_TREE +#define CONSTRUCT_DIST_TREE OF_INFLATE_STREAM_CONSTRUCT_DIST_TREE +#define AWAIT_CODE OF_INFLATE_STREAM_AWAIT_CODE +#define WRITE_VALUE OF_INFLATE_STREAM_WRITE_VALUE +#define AWAIT_LENGTH_EXTRA_BITS OF_INFLATE_STREAM_AWAIT_LENGTH_EXTRA_BITS +#define AWAIT_DISTANCE OF_INFLATE_STREAM_AWAIT_DISTANCE +#define AWAIT_DISTANCE_EXTRA_BITS OF_INFLATE_STREAM_AWAIT_DISTANCE_EXTRA_BITS +#define PROCESS_PAIR OF_INFLATE_STREAM_PROCESS_PAIR +#define BUFFER_SIZE OF_INFLATE_STREAM_BUFFER_SIZE + +#define MAX_BITS 15 + +struct huffman_tree { + struct huffman_tree *leafs[2]; + uint16_t value; +}; + +#ifndef DEFLATE64 +static const uint_fast8_t numDistanceCodes = 30; +static const uint8_t lengthCodes[29] = { + /* indices are -257, values -3 */ + 0, 1, 2, 3, 4, 5, 6, 7, 8, 10, 12, 14, 16, 20, 24, 28, 32, 40, 48, 56, + 64, 80, 96, 112, 128, 160, 192, 224, 255 +}; +static const uint8_t lengthExtraBits[29] = { + 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 2, 2, 2, 2, 3, 3, 3, 3, 4, 4, 4, 4, + 5, 5, 5, 5, 0 +}; +static const uint16_t distanceCodes[30] = { + 1, 2, 3, 4, 5, 7, 9, 13, 17, 25, 33, 49, 65, 97, 129, 193, 257, 385, + 513, 769, 1025, 1537, 2049, 3073, 4097, 6145, 8193, 12289, 16385, 24577 +}; +static const uint8_t distanceExtraBits[30] = { + 0, 0, 0, 0, 1, 1, 2, 2, 3, 3, 4, 4, 5, 5, 6, 6, 7, 7, 8, 8, 9, 9, 10, + 10, 11, 11, 12, 12, 13, 13 +}; +#else +static const uint_fast8_t numDistanceCodes = 32; +static const uint8_t lengthCodes[29] = { + /* indices are -257, values -3 */ + 0, 1, 2, 3, 4, 5, 6, 7, 8, 10, 12, 14, 16, 20, 24, 28, 32, 40, 48, 56, + 64, 80, 96, 112, 128, 160, 192, 224, 0 +}; +static const uint8_t lengthExtraBits[29] = { + 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 2, 2, 2, 2, 3, 3, 3, 3, 4, 4, 4, 4, + 5, 5, 5, 5, 16 +}; +static const uint16_t distanceCodes[32] = { + 1, 2, 3, 4, 5, 7, 9, 13, 17, 25, 33, 49, 65, 97, 129, 193, 257, 385, + 513, 769, 1025, 1537, 2049, 3073, 4097, 6145, 8193, 12289, 16385, 24577, + 32769, 49153 +}; +static const uint8_t distanceExtraBits[32] = { + 0, 0, 0, 0, 1, 1, 2, 2, 3, 3, 4, 4, 5, 5, 6, 6, 7, 7, 8, 8, 9, 9, 10, + 10, 11, 11, 12, 12, 13, 13, 14, 14 +}; +#endif +static const uint8_t codeLengthsOrder[19] = { + 16, 17, 18, 0, 8, 7, 9, 6, 10, 5, 11, 4, 12, 3, 13, 2, 14, 1, 15 +}; +static struct huffman_tree *fixedLitLenTree, *fixedDistTree; + +static bool +tryReadBits(OFInflateStream *stream, uint_fast16_t *bits, uint_fast8_t count) +{ + uint_fast16_t ret = stream->_savedBits; + uint_fast8_t i; + + assert(stream->_savedBitsLength < count); + + for (i = stream->_savedBitsLength; i < count; i++) { + if OF_UNLIKELY (stream->_bitIndex == 8) { + if (stream->_bufferIndex < stream->_bufferLength) + stream->_byte = + stream->_buffer[stream->_bufferIndex++]; + else { + size_t length = [stream->_stream + readIntoBuffer: stream->_buffer + length: BUFFER_SIZE]; + + if OF_UNLIKELY (length < 1) { + stream->_savedBits = ret; + stream->_savedBitsLength = i; + return false; + } + + stream->_byte = stream->_buffer[0]; + stream->_bufferIndex = 1; + stream->_bufferLength = (uint_fast16_t)length; + } + + stream->_bitIndex = 0; + } + + ret |= ((stream->_byte >> stream->_bitIndex++) & 1) << i; + } + + stream->_savedBits = 0; + stream->_savedBitsLength = 0; + *bits = ret; + + return true; +} + +static struct huffman_tree* +newTree(void) +{ + struct huffman_tree *tree; + + if ((tree = malloc(sizeof(*tree))) == NULL) + @throw [OFOutOfMemoryException + exceptionWithRequestedSize: sizeof(*tree)]; + + tree->leafs[0] = tree->leafs[1] = NULL; + tree->value = 0xFFFF; + + return tree; +} + +static void +treeInsert(struct huffman_tree *tree, uint_fast16_t code, uint_fast8_t length, + uint16_t value) +{ + while (length > 0) { + uint_fast8_t bit; + + length--; + bit = (code & (1 << length)) >> length; + + if (tree->leafs[bit] == NULL) + tree->leafs[bit] = newTree(); + + tree = tree->leafs[bit]; + } + + tree->value = value; +} + +static struct huffman_tree* +constructTree(uint8_t lengths[], uint_fast16_t count) +{ + struct huffman_tree *tree; + uint16_t lengthCount[MAX_BITS + 1] = { 0 }; + uint16_t code, maxCode = 0, nextCode[MAX_BITS + 1]; + uint_fast16_t i; + + for (i = 0; i < count; i++) { + uint8_t length = lengths[i]; + + if OF_UNLIKELY (length > MAX_BITS) + @throw [OFInvalidFormatException exception]; + + if (length > 0) { + lengthCount[length]++; + maxCode = i; + } + } + + code = 0; + for (i = 1; i <= MAX_BITS; i++) { + code = (code + lengthCount[i - 1]) << 1; + nextCode[i] = code; + } + + tree = newTree(); + + for (i = 0; i <= maxCode; i++) { + uint8_t length = lengths[i]; + + if (length > 0) + treeInsert(tree, nextCode[length]++, length, i); + } + + return tree; +} + +static bool +walkTree(OFInflateStream *stream, struct huffman_tree **tree, uint16_t *value) +{ + struct huffman_tree *iter = *tree; + uint_fast16_t bits; + + while (iter->value == 0xFFFF) { + if OF_UNLIKELY (!tryReadBits(stream, &bits, 1)) { + *tree = iter; + return false; + } + + if OF_UNLIKELY (iter->leafs[bits] == NULL) + @throw [OFInvalidFormatException exception]; + + iter = iter->leafs[bits]; + } + + *value = iter->value; + return true; +} + +static void +releaseTree(struct huffman_tree *tree) +{ + uint_fast8_t i; + + for (i = 0; i < 2; i++) + if OF_LIKELY (tree->leafs[i] != NULL) + releaseTree(tree->leafs[i]); + + free(tree); +} + +@implementation OFInflateStream ++ (void)initialize +{ + uint_fast16_t i; + uint8_t lengths[288]; + + if (self != [OFInflateStream class]) + return; + + for (i = 0; i <= 143; i++) + lengths[i] = 8; + for (i = 144; i <= 255; i++) + lengths[i] = 9; + for (i = 256; i <= 279; i++) + lengths[i] = 7; + for (i = 280; i <= 287; i++) + lengths[i] = 8; + + fixedLitLenTree = constructTree(lengths, 288); + + for (i = 0; i <= 31; i++) + lengths[i] = 5; + + fixedDistTree = constructTree(lengths, 32); +} + +#ifndef DEFLATE64 ++ (instancetype)streamWithStream: (OFStream*)stream +{ + return [[[self alloc] initWithStream: stream] autorelease]; +} + +- init +{ + OF_INVALID_INIT_METHOD +} + +- initWithStream: (OFStream*)stream +{ + self = [super init]; + + _stream = [stream retain]; + _bitIndex = 8; /* 0-7 address the bit, 8 means fetch next byte */ + _slidingWindowMask = 0x7FFF; + + return self; +} + +- (void)dealloc +{ + [_stream release]; + + [super dealloc]; +} +#endif + +- (size_t)lowlevelReadIntoBuffer: (void*)buffer_ + length: (size_t)length +{ + uint8_t *buffer = buffer_; + uint_fast16_t bits, i, tmp; + uint16_t value; + size_t bytesWritten = 0; + uint8_t *slidingWindow; + uint_fast16_t slidingWindowIndex; + + if (_atEndOfStream) + @throw [OFReadFailedException exceptionWithObject: self + requestedLength: length]; + +start: + switch (_state) { + case BLOCK_HEADER: + if OF_UNLIKELY (_inLastBlock) { + [_stream unreadFromBuffer: _buffer + _bufferIndex + length: _bufferLength - + _bufferIndex]; + + _atEndOfStream = true; + return bytesWritten; + } + + if OF_UNLIKELY (!tryReadBits(self, &bits, 3)) + return bytesWritten; + + _inLastBlock = (bits & 1); + + switch (bits >> 1) { + case 0: /* No compression */ + _state = UNCOMPRESSED_BLOCK_HEADER; + _bitIndex = 8; + _context.uncompressedHeader.position = 0; + memset(_context.uncompressedHeader.length, 0, 4); + break; + case 1: /* Fixed Huffman */ + _state = HUFFMAN_BLOCK; + _context.huffman.state = AWAIT_CODE; + _context.huffman.litLenTree = fixedLitLenTree; + _context.huffman.distTree = fixedDistTree; + _context.huffman.treeIter = fixedLitLenTree; + break; + case 2: /* Dynamic Huffman */ + _state = HUFFMAN_TREE; + _context.huffmanTree.lengths = NULL; + _context.huffmanTree.receivedCount = 0; + _context.huffmanTree.value = 0xFE; + _context.huffmanTree.litLenCodesCount = 0xFF; + _context.huffmanTree.distCodesCount = 0xFF; + _context.huffmanTree.codeLenCodesCount = 0xFF; + break; + default: + @throw [OFInvalidFormatException exception]; + } + + goto start; + case UNCOMPRESSED_BLOCK_HEADER: +#define CTX _context.uncompressedHeader + /* FIXME: This can be done more efficiently than unreading */ + [_stream unreadFromBuffer: _buffer + _bufferIndex + length: _bufferLength - _bufferIndex]; + _bufferIndex = _bufferLength = 0; + + CTX.position += [_stream + readIntoBuffer: CTX.length + CTX.position + length: 4 - CTX.position]; + + if OF_UNLIKELY (CTX.position < 4) + return bytesWritten; + + if OF_UNLIKELY ((CTX.length[0] | (CTX.length[1] << 8)) != + (uint16_t)~(CTX.length[2] | (CTX.length[3] << 8))) + @throw [OFInvalidFormatException exception]; + + _state = UNCOMPRESSED_BLOCK; + + /* + * Do not reorder! _context.uncompressed.position and + * _context.uncompressedHeader.length overlap! + */ + _context.uncompressed.length = + CTX.length[0] | (CTX.length[1] << 8); + _context.uncompressed.position = 0; + + goto start; +#undef CTX + case UNCOMPRESSED_BLOCK: +#define CTX _context.uncompressed + if OF_UNLIKELY (length == 0) + return bytesWritten; + + tmp = (length < CTX.length - CTX.position + ? (uint_fast16_t)length : CTX.length - CTX.position); + + tmp = (uint_fast16_t)[_stream + readIntoBuffer: buffer + bytesWritten + length: tmp]; + + if OF_UNLIKELY (_slidingWindow == NULL) { + _slidingWindow = + [self allocMemoryWithSize: _slidingWindowMask + 1]; + /* Avoid leaking data */ + memset(_slidingWindow, 0, _slidingWindowMask + 1); + } + + slidingWindow = _slidingWindow; + slidingWindowIndex = _slidingWindowIndex; + for (i = 0; i < tmp; i++) { + slidingWindow[slidingWindowIndex] = + buffer[bytesWritten + i]; + slidingWindowIndex = (slidingWindowIndex + 1) & + _slidingWindowMask; + } + _slidingWindowIndex = slidingWindowIndex; + + length -= tmp; + bytesWritten += tmp; + + CTX.position += tmp; + if OF_UNLIKELY (CTX.position == CTX.length) + _state = BLOCK_HEADER; + + goto start; +#undef CTX + case HUFFMAN_TREE: +#define CTX _context.huffmanTree + if OF_LIKELY (CTX.value == 0xFE) { + if OF_LIKELY (CTX.litLenCodesCount == 0xFF) { + if OF_UNLIKELY (!tryReadBits(self, &bits, 5)) + return bytesWritten; + + if OF_UNLIKELY (bits > 29) + @throw [OFInvalidFormatException + exception]; + + CTX.litLenCodesCount = bits; + } + + if OF_LIKELY (CTX.distCodesCount == 0xFF) { + if OF_UNLIKELY (!tryReadBits(self, &bits, 5)) + return bytesWritten; + + CTX.distCodesCount = bits; + } + + if OF_LIKELY (CTX.codeLenCodesCount == 0xFF) { + if OF_UNLIKELY (!tryReadBits(self, &bits, 4)) + return bytesWritten; + + CTX.codeLenCodesCount = bits; + } + + if OF_LIKELY (CTX.lengths == NULL) { + CTX.lengths = [self allocMemoryWithSize: 19]; + memset(CTX.lengths, 0, 19); + } + + for (i = CTX.receivedCount; + i < CTX.codeLenCodesCount + 4; i++) { + if OF_UNLIKELY (!tryReadBits(self, &bits, 3)) { + CTX.receivedCount = i; + return bytesWritten; + } + + CTX.lengths[codeLengthsOrder[i]] = bits; + } + + CTX.codeLenTree = constructTree(CTX.lengths, 19); + CTX.treeIter = CTX.codeLenTree; + + [self freeMemory: CTX.lengths]; + CTX.lengths = NULL; + CTX.receivedCount = 0; + CTX.value = 0xFF; + } + + if OF_LIKELY (CTX.lengths == NULL) + CTX.lengths = [self allocMemoryWithSize: + CTX.litLenCodesCount + CTX.distCodesCount + 258]; + + for (i = CTX.receivedCount; + i < CTX.litLenCodesCount + CTX.distCodesCount + 258;) { + uint_fast8_t j, count; + + if OF_LIKELY (CTX.value == 0xFF) { + if OF_UNLIKELY (!walkTree(self, &CTX.treeIter, + &value)) { + CTX.receivedCount = i; + return bytesWritten; + } + + CTX.treeIter = CTX.codeLenTree; + + if (value < 16) { + CTX.lengths[i++] = value; + continue; + } + } else + value = CTX.value; + + switch (value) { + case 16: + if OF_UNLIKELY (i < 1) + @throw [OFInvalidFormatException + exception]; + + if OF_UNLIKELY (!tryReadBits(self, &bits, 2)) { + CTX.receivedCount = i; + CTX.value = value; + return bytesWritten; + } + + value = CTX.lengths[i - 1]; + count = bits + 3; + + break; + case 17: + if OF_UNLIKELY (!tryReadBits(self, &bits, 3)) { + CTX.receivedCount = i; + CTX.value = value; + return bytesWritten; + } + + value = 0; + count = bits + 3; + + break; + case 18: + if OF_UNLIKELY (!tryReadBits(self, &bits, 7)) { + CTX.receivedCount = i; + CTX.value = value; + return bytesWritten; + } + + value = 0; + count = bits + 11; + + break; + default: + @throw [OFInvalidFormatException exception]; + } + + if OF_UNLIKELY (i + count > + CTX.litLenCodesCount + CTX.distCodesCount + 258) + @throw [OFInvalidFormatException exception]; + + for (j = 0; j < count; j++) + CTX.lengths[i++] = value; + + CTX.value = 0xFF; + } + + releaseTree(CTX.codeLenTree); + + CTX.litLenTree = constructTree(CTX.lengths, + CTX.litLenCodesCount + 257); + CTX.distTree = constructTree( + CTX.lengths + CTX.litLenCodesCount + 257, + CTX.distCodesCount + 1); + + [self freeMemory: CTX.lengths]; + + /* + * litLenTree and distTree are at the same location in + * _context.huffman and _context.huffmanTree, thus no need to + * set them. + */ + _state = HUFFMAN_BLOCK; + _context.huffman.state = AWAIT_CODE; + _context.huffman.treeIter = CTX.litLenTree; + + goto start; +#undef CTX + case HUFFMAN_BLOCK: +#define CTX _context.huffman + for (;;) { + uint_fast8_t extraBits, lengthCodeIndex; + + if OF_UNLIKELY (CTX.state == WRITE_VALUE) { + if OF_UNLIKELY (length == 0) + return bytesWritten; + + buffer[bytesWritten++] = CTX.value; + length--; + + if (_slidingWindow == NULL) { + _slidingWindow = [self + allocMemoryWithSize: + _slidingWindowMask + 1]; + /* Avoid leaking data */ + memset(_slidingWindow, 0, + _slidingWindowMask + 1); + } + + _slidingWindow[_slidingWindowIndex] = CTX.value; + _slidingWindowIndex = + (_slidingWindowIndex + 1) & + _slidingWindowMask; + + CTX.state = AWAIT_CODE; + CTX.treeIter = CTX.litLenTree; + } + + if OF_UNLIKELY (CTX.state == AWAIT_LENGTH_EXTRA_BITS) { + if OF_UNLIKELY (!tryReadBits(self, &bits, + CTX.extraBits)) + return bytesWritten; + + CTX.length += bits; + + CTX.state = AWAIT_DISTANCE; + CTX.treeIter = CTX.distTree; + } + + /* Distance of length distance pair */ + if (CTX.state == AWAIT_DISTANCE) { + if OF_UNLIKELY (!walkTree(self, &CTX.treeIter, + &value)) + return bytesWritten; + + if OF_UNLIKELY (value >= numDistanceCodes) + @throw [OFInvalidFormatException + exception]; + + CTX.distance = distanceCodes[value]; + extraBits = distanceExtraBits[value]; + + if (extraBits > 0) { + if OF_UNLIKELY (!tryReadBits(self, + &bits, extraBits)) { + CTX.state = + AWAIT_DISTANCE_EXTRA_BITS; + CTX.extraBits = extraBits; + return bytesWritten; + } + + CTX.distance += bits; + } + + CTX.state = PROCESS_PAIR; + } else if (CTX.state == AWAIT_DISTANCE_EXTRA_BITS) { + if OF_UNLIKELY (!tryReadBits(self, &bits, + CTX.extraBits)) + return bytesWritten; + + CTX.distance += bits; + + CTX.state = PROCESS_PAIR; + } + + /* Length distance pair */ + if (CTX.state == PROCESS_PAIR) { + uint_fast16_t j; + + if OF_UNLIKELY (_slidingWindow == NULL) + @throw [OFInvalidFormatException + exception]; + + for (j = 0; j < CTX.length; j++) { + uint_fast16_t index; + + if OF_UNLIKELY (length == 0) { + CTX.length -= j; + return bytesWritten; + } + + index = (_slidingWindowIndex - + CTX.distance) & _slidingWindowMask; + value = _slidingWindow[index]; + + buffer[bytesWritten++] = value; + length--; + + _slidingWindow[_slidingWindowIndex] = + value; + _slidingWindowIndex = + (_slidingWindowIndex + 1) & + _slidingWindowMask; + } + + CTX.state = AWAIT_CODE; + CTX.treeIter = CTX.litLenTree; + } + + if OF_UNLIKELY (!walkTree(self, &CTX.treeIter, &value)) + return bytesWritten; + + /* End of block */ + if OF_UNLIKELY (value == 256) { + if (CTX.litLenTree != fixedLitLenTree) + releaseTree(CTX.litLenTree); + if (CTX.distTree != fixedDistTree) + releaseTree(CTX.distTree); + + _state = BLOCK_HEADER; + goto start; + } + + /* Literal byte */ + if (value < 256) { + if OF_UNLIKELY (length == 0) { + CTX.state = WRITE_VALUE; + CTX.value = value; + return bytesWritten; + } + + buffer[bytesWritten++] = value; + length--; + + if (_slidingWindow == NULL) { + _slidingWindow = [self + allocMemoryWithSize: + _slidingWindowMask + 1]; + /* Avoid leaking data */ + memset(_slidingWindow, 0, + _slidingWindowMask + 1); + } + + _slidingWindow[_slidingWindowIndex] = value; + _slidingWindowIndex = + (_slidingWindowIndex + 1) & + _slidingWindowMask; + + CTX.treeIter = CTX.litLenTree; + continue; + } + + if OF_UNLIKELY (value > 285) + @throw [OFInvalidFormatException exception]; + + /* Length of length distance pair */ + lengthCodeIndex = value - 257; + CTX.length = lengthCodes[lengthCodeIndex] + 3; + extraBits = lengthExtraBits[lengthCodeIndex]; + + if (extraBits > 0) { + if OF_UNLIKELY (!tryReadBits(self, &bits, + extraBits)) { + CTX.extraBits = extraBits; + CTX.state = AWAIT_LENGTH_EXTRA_BITS; + return bytesWritten; + } + + CTX.length += bits; + } + + CTX.treeIter = CTX.distTree; + CTX.state = AWAIT_DISTANCE; + } + + break; +#undef CTX + } + + OF_UNREACHABLE +} + +#ifndef DEFLATE64 +- (bool)lowlevelIsAtEndOfStream +{ + return _atEndOfStream; +} + +- (int)fileDescriptorForReading +{ + return [_stream fileDescriptorForReading]; +} + +- (bool)hasDataInReadBuffer +{ + return ([super hasDataInReadBuffer] || [_stream hasDataInReadBuffer]); +} +#endif +@end Index: src/OFZIPArchive.m ================================================================== --- src/OFZIPArchive.m +++ src/OFZIPArchive.m @@ -23,12 +23,12 @@ #import "OFZIPArchiveEntry+Private.h" #import "OFDataArray.h" #import "OFArray.h" #import "OFDictionary.h" #import "OFFile.h" -#import "OFDeflateStream.h" -#import "OFDeflate64Stream.h" +#import "OFInflateStream.h" +#import "OFInflate64Stream.h" #import "OFChecksumFailedException.h" #import "OFInvalidArgumentException.h" #import "OFInvalidFormatException.h" #import "OFNotImplementedException.h" @@ -478,15 +478,15 @@ switch (localFileHeader->_compressionMethod) { case OF_ZIP_ARCHIVE_ENTRY_COMPRESSION_METHOD_NONE: _decompressedStream = [stream retain]; break; case OF_ZIP_ARCHIVE_ENTRY_COMPRESSION_METHOD_DEFLATE: - _decompressedStream = [[OFDeflateStream alloc] + _decompressedStream = [[OFInflateStream alloc] initWithStream: stream]; break; case OF_ZIP_ARCHIVE_ENTRY_COMPRESSION_METHOD_DEFLATE64: - _decompressedStream = [[OFDeflate64Stream alloc] + _decompressedStream = [[OFInflate64Stream alloc] initWithStream: stream]; break; default: @throw [OFNotImplementedException exceptionWithSelector: _cmd Index: src/ObjFW.h ================================================================== --- src/ObjFW.h +++ src/ObjFW.h @@ -45,11 +45,12 @@ #import "OFDate.h" #import "OFURL.h" #import "OFStream.h" #import "OFStdIOStream.h" -#import "OFDeflateStream.h" +#import "OFInflateStream.h" +#import "OFInflate64Stream.h" #ifdef OF_HAVE_FILES # import "OFFile.h" # import "OFINIFile.h" # import "OFSettings.h" # import "OFZIPArchive.h"