Mark Sibly преди 7 години
родител
ревизия
d0879a211b
променени са 2 файла, в които са добавени 28 реда и са изтрити 8 реда
  1. 17 7
      modules/std/memory/databuffer.monkey2
  2. 11 1
      modules/zlib/zlib.monkey2

+ 17 - 7
modules/std/memory/databuffer.monkey2

@@ -602,26 +602,36 @@ Class DataBuffer Extends std.resource.Resource
 		dest.PokeUInt( 0,Length )
 		dest.PokeUInt( 4,~Length )
 		Local destLenOut:=destLen
-		zlib.compress2( dest.Data+8,Cast<zlib.z_uLong Ptr>( Varptr destLenOut ),Data,Length,compressionLevel )
+		Local r:=zlib.compress2( dest.Data+8,Cast<zlib.z_uLong Ptr>( Varptr destLenOut ),Data,Length,compressionLevel )
+		If r<>zlib.Z_OK
+			RuntimeError( "Error compressing data: zlib return code="+r)
+			dest.Discard()
+			Return Null
+		Endif
 		If destLenOut<>destLen
-			Local r:=New DataBuffer( destLenOut+8 )
-			dest.CopyTo( r,0,0,destLenOut+8 )
+			Local tmp:=New DataBuffer( destLenOut+8 )
+			dest.CopyTo( tmp,0,0,destLenOut+8 )
 			dest.Discard()
-			dest=r
+			dest=tmp
 		Endif
 		Return dest
 	End
 	
 	#rem monkeydoc Decompresses data buffer.
 	
-	The data buffer must have been originally compressed using the [[Compress]] method or a runtime error will occur.
+	If there was an error decompressing the data, null is returned. Erros can occur if the compressed data 
+	has somehow been corrupted.
 	
 	#end
 	Method Decompress:DataBuffer()
-		Assert( Length>8 And PeekUInt(0)=~PeekUInt(4),"DataBuffer does not appear to contain valid compressed data" )
+		If Length<8 Or PeekUInt( 0 )<>~PeekUInt( 4 ) Return Null
 		Local destLen:=PeekUInt( 0 )
 		Local dest:=New DataBuffer( destLen )
-		zlib.uncompress( dest.Data,Cast<zlib.z_uLong Ptr>( Varptr destLen ),Data+8,Length-8 )
+		Local r:=zlib.uncompress( dest.Data,Cast<zlib.z_uLong Ptr>( Varptr destLen ),Data+8,Length-8 )
+		If r<>zlib.Z_OK
+			dest.Discard()
+			Return Null
+		Endif
 		Return dest
 	End
 	

+ 11 - 1
modules/zlib/zlib.monkey2

@@ -15,7 +15,7 @@ Namespace zlib
 #Import "zlib-1.2.11/inftrees.c"
 #Import "zlib-1.2.11/trees.c"
 #Import "zlib-1.2.11/uncompr.c"
-'#Import "zlib-1.2.11/zutil.c"
+#Import "zlib-1.2.11/zutil.c"
 
 #Import "zlib-1.2.11/*.h"
 #Import "<zlib.h>"
@@ -26,6 +26,16 @@ Extern
 Struct z_uLong="uLong"
 End
 
+Const Z_OK:Int
+Const Z_STREAM_END:Int
+Const Z_NEED_DICT:Int
+Const Z_ERRNO:Int
+Const Z_STREAM_ERROR:Int
+Const Z_DATA_ERROR:Int
+Const Z_MEM_ERROR:Int
+Const Z_BUF_ERROR:Int
+Const Z_VERSION_ERROR:Int
+
 Function compress:Int( dest:UByte Ptr,destLen:z_uLong Ptr,source:UByte Ptr,sourceLen:UInt )
 Function compress2:Int( dest:UByte Ptr,destLen:z_uLong Ptr,source:UByte Ptr,sourceLen:UInt,level:Int )
 Function uncompress:Int( dest:UByte Ptr,destLen:z_uLong Ptr,source:UByte Ptr,sourceLen:UInt )