Skip to content

Commit

Permalink
Fixed Zlib::Deflate algorithm to use the right header and footer arou…
Browse files Browse the repository at this point in the history
…nd System.IO.Compression.DeflateStream

Fixed bug in MutableStringStream where it was writing too many bytes
Added GitDiff.bat to use windiff for diffing, and to be able to get of files to diff from rather than having to go serially through all the files one by one
Adds a trivial README - github.com will automatically show the contents inline if anyone browses the repo.
  • Loading branch information
Shri Borde committed Mar 16, 2009
1 parent bc39b55 commit ce7438b
Show file tree
Hide file tree
Showing 11 changed files with 221 additions and 5 deletions.
@@ -1,5 +1,6 @@
require 'zlib'
require File.dirname(__FILE__) + '/../../../spec_helper'
require File.dirname(__FILE__) + '/fixtures/classes'

describe 'Zlib::Deflate#deflate' do

Expand All @@ -25,6 +26,13 @@
zipped.should == "x\234\355\301\001\001\000\000\000\200\220\376\257\356\b\n#{"\000" * 31}\030\200\000\000\001"
end

# This example is useful for implementations which do generate valid compressed output, but which
# do not compress to the exact same byte sequence as MRI
it 'deflates data to an inflatable format' do
zipped = @deflator.deflate DeflateSpecs::LoremIpsum, Zlib::FINISH
Zlib::Inflate.inflate(zipped).should == DeflateSpecs::LoremIpsum
end

end

describe 'Zlib::Deflate::deflate' do
Expand All @@ -44,6 +52,13 @@

zipped.should == "x\234\355\301\001\001\000\000\000\200\220\376\257\356\b\n#{"\000" * 31}\030\200\000\000\001"
end

# This example is useful for implementations which do generate valid compressed output, but which
# do not compress to the exact same byte sequence as MRI
it 'deflates data to an inflatable format' do
zipped = Zlib::Deflate.deflate DeflateSpecs::LoremIpsum
Zlib::Inflate.inflate(zipped).should == DeflateSpecs::LoremIpsum
end

end

@@ -0,0 +1,5 @@
module DeflateSpecs
LoremIpsum = "Ipsum is simply dummy text of the printing and typesetting industry. \nLorem Ipsum has been the industrys standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. \nIt has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. \nIt was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum."

DeflatedLoremIpsum = "x\234M\221AN\0031\fE\367\234\302\a@\250,@b\311\262\022H\\\301\223q;Q\023'\212\235\016s{\376\244 \272\213e\373\377\347\237c\265\236)\032Y\3145m4\367\2347r\371v*'\362E\250\266\250\036\365L\2543\371V\305\304G\035u\356\346m{\242\207\217\322$\323qh-l4\211\350X\376\233\201\276c\237\333|\357 Wi0\326 c\370\371\345p\260GZ\027,\263R\327\213\226Uo\000\030\364R.\304t\346\224d\et\200\031T\026\032\347)\311L\3211F\231/h\334\372V%\304\f\305\t\353@=\372 \264\336\256\361\212\r-\270Tq\371\t%\005Q\357-\n0\246\356\304\311\312@K\302\025\307@[\222\004oEc\270\017\343\221\020\000G\335s\0213\250D`n\270!,\254g\231o\316+\234k\251=q\213\266\343\336Rz~{=\030\255\321\227Q6x\260\311~\342\207x\303\323\311\026\0217\nE\375\327\346>\363\312f|\336\251\36782:\020\331o\001\302\220\235\305.^*\325>\245h\313\276o\345\344+c2E\244\365\236\360Q\364\005\221O\204\327@\026R\237\3679\374\221\305\2426p\376=\237~\000\317\321\320K"
end
@@ -1,5 +1,5 @@
module GzipReaderSpecs
UncompressedContents = "Ipsum is simply dummy text of the printing and typesetting industry. \nLorem Ipsum has been the industrys standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. \nIt has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. \nIt was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum."
LoremIpsum = "Ipsum is simply dummy text of the printing and typesetting industry. \nLorem Ipsum has been the industrys standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. \nIt has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. \nIt was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum."

GzippedContents = "\037\213\b\000\325\374\262I\000\vM\221AN\0031\fE\367\234\302\a@\250,@b\311\262\022H\\\301\223q;Q\023'\212\235\016s{\376\244 \272\213e\373\377\347\237c\265\236)\032Y\3145m4\367\2347r\371v*'\362E\250\266\250\036\365L\2543\371V\305\304G\035u\356\346m{\242\207\217\322$\323qh-l4\211\350X\376\233\201\276c\237\333|\357 Wi0\326 c\370\371\345p\260GZ\027,\263R\327\213\226Uo\000\030\364R.\304t\346\224d\et\200\031T\026\032\347)\311L\3211F\231/h\334\372V%\304\f\305\t\353@=\372 \264\336\256\361\212\r-\270Tq\371\t%\005Q\357-\n0\246\356\304\311\312@K\302\025\307@[\222\004oEc\270\017\343\221\020\000G\335s\0213\250D`n\270!,\254g\231o\316+\234k\251=q\213\266\343\336Rz~{=\030\255\321\227Q6x\260\311~\342\207x\303\323\311\026\0217\nE\375\327\346>\363\312f|\336\251\36782:\020\331o\001\302\220\235\305.^*\325>\245h\313\276o\345\344+c2E\244\365\236\360Q\364\005\221O\204\327@\026R\237\3679\374\221\305\2426p\376=\237~\000\003\357\321!:\002\000\000"
GzippedLoremIpsum = "\037\213\b\000\325\374\262I\000\vM\221AN\0031\fE\367\234\302\a@\250,@b\311\262\022H\\\301\223q;Q\023'\212\235\016s{\376\244 \272\213e\373\377\347\237c\265\236)\032Y\3145m4\367\2347r\371v*'\362E\250\266\250\036\365L\2543\371V\305\304G\035u\356\346m{\242\207\217\322$\323qh-l4\211\350X\376\233\201\276c\237\333|\357 Wi0\326 c\370\371\345p\260GZ\027,\263R\327\213\226Uo\000\030\364R.\304t\346\224d\et\200\031T\026\032\347)\311L\3211F\231/h\334\372V%\304\f\305\t\353@=\372 \264\336\256\361\212\r-\270Tq\371\t%\005Q\357-\n0\246\356\304\311\312@K\302\025\307@[\222\004oEc\270\017\343\221\020\000G\335s\0213\250D`n\270!,\254g\231o\316+\234k\251=q\213\266\343\336Rz~{=\030\255\321\227Q6x\260\311~\342\207x\303\323\311\026\0217\nE\375\327\346>\363\312f|\336\251\36782:\020\331o\001\302\220\235\305.^*\325>\245h\313\276o\345\344+c2E\244\365\236\360Q\364\005\221O\204\327@\026R\237\3679\374\221\305\2426p\376=\237~\000\003\357\321!:\002\000\000"
end
Expand Up @@ -5,7 +5,7 @@

describe "Zlib::GzipReader.wrap" do
before :each do
@io = ClassSpecs::StubReaderWithClose.new GzipReaderSpecs::GzippedContents
@io = ClassSpecs::StubReaderWithClose.new GzipReaderSpecs::GzippedLoremIpsum
end

it "can be called without a block" do
Expand Down Expand Up @@ -46,7 +46,7 @@

it "allows IO objects without a close method" do
io = mock("io")
io.should_receive(:read).any_number_of_times.and_return(GzipReaderSpecs::GzippedContents, nil)
io.should_receive(:read).any_number_of_times.and_return(GzipReaderSpecs::GzippedLoremIpsum, nil)
Zlib::GzipReader.wrap(io) { |gz| gz.read }
end

Expand Down
@@ -0,0 +1,5 @@
module InflateSpecs
LoremIpsum = "Ipsum is simply dummy text of the printing and typesetting industry. \nLorem Ipsum has been the industrys standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. \nIt has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. \nIt was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum."

DeflatedLoremIpsum = "x\234M\221AN\0031\fE\367\234\302\a@\250,@b\311\262\022H\\\301\223q;Q\023'\212\235\016s{\376\244 \272\213e\373\377\347\237c\265\236)\032Y\3145m4\367\2347r\371v*'\362E\250\266\250\036\365L\2543\371V\305\304G\035u\356\346m{\242\207\217\322$\323qh-l4\211\350X\376\233\201\276c\237\333|\357 Wi0\326 c\370\371\345p\260GZ\027,\263R\327\213\226Uo\000\030\364R.\304t\346\224d\et\200\031T\026\032\347)\311L\3211F\231/h\334\372V%\304\f\305\t\353@=\372 \264\336\256\361\212\r-\270Tq\371\t%\005Q\357-\n0\246\356\304\311\312@K\302\025\307@[\222\004oEc\270\017\343\221\020\000G\335s\0213\250D`n\270!,\254g\231o\316+\234k\251=q\213\266\343\336Rz~{=\030\255\321\227Q6x\260\311~\342\207x\303\323\311\026\0217\nE\375\327\346>\363\312f|\336\251\36782:\020\331o\001\302\220\235\305.^*\325>\245h\313\276o\345\344+c2E\244\365\236\360Q\364\005\221O\204\327@\026R\237\3679\374\221\305\2426p\376=\237~\000\317\321\320K"
end
@@ -1,5 +1,6 @@
require 'zlib'
require File.dirname(__FILE__) + '/../../../spec_helper'
require File.dirname(__FILE__) + '/fixtures/classes'

describe 'Zlib::Inflate#inflate' do

Expand All @@ -25,6 +26,10 @@
unzipped.should == "\000" * 32 * 1024
end

it 'inflates Lorem Ipsum' do
@inflator.inflate(InflateSpecs::DeflatedLoremIpsum).should == InflateSpecs::LoremIpsum
end

end

describe 'Zlib::Inflate::inflate' do
Expand All @@ -44,6 +49,10 @@

zipped.should == "\000" * 32 * 1024
end

it 'inflates Lorem Ipsum' do
Zlib::Inflate.inflate(InflateSpecs::DeflatedLoremIpsum).should == InflateSpecs::LoremIpsum
end

end

112 changes: 111 additions & 1 deletion Merlin/Main/Languages/Ruby/Libraries.LCA_RESTRICTED/Zlib/zlib.cs
Expand Up @@ -900,6 +900,116 @@ protected StreamError(System.Runtime.Serialization.SerializationInfo info, Syste
[RubyClass("Deflate", BuildConfig="!SILVERLIGHT")]
public class Deflate : ZStream {

/// <summary>
/// Adds a 2 byte header, and a 4 byte adler checksum footer.
/// </summary>
class ZDeflateStream : DeflateStream {
private long _size;
private uint _crc;
private bool _leaveOpen;
private Stream _output;

public ZDeflateStream(Stream output, bool leaveOpen)
: base(output, CompressionMode.Compress, true) {
_output = output;
_leaveOpen = leaveOpen;

// System.IO.Compression.DeflateStream uses a window size of 8K and FLEVEL is 2 (default algorithm).
byte[] header = { 0x58, 0x85 };
_output.Write(header, 0, header.Length);
}

public override IAsyncResult BeginWrite(byte[] array, int offset, int count, AsyncCallback asyncCallback, object asyncState) {
IAsyncResult result = base.BeginWrite(array, offset, count, asyncCallback, asyncState);
_size += count;
_crc = UpdateCrc(_crc, array, offset, count);
return result;
}

public override void Write(byte[] array, int offset, int count) {
base.Write(array, offset, count);
_size += count;
_crc = UpdateCrc(_crc, array, offset, count);
}

protected override void Dispose(bool disposing) {
base.Dispose(disposing);
if (disposing && _output != null) {
_output.WriteByte((byte)(_crc & 0xff));
_output.WriteByte((byte)((_crc >> 8) & 0xff));
_output.WriteByte((byte)((_crc >> 16) & 0xff));
_output.WriteByte((byte)((_crc >> 24) & 0xff));
if (!_leaveOpen) _output.Close();
_output = null;
}
}

// See RFC1950 for details. http://www.faqs.org/rfcs/rfc1950.html
private static uint UpdateCrc(uint crc, byte[] buffer, int offset, int length) {
crc ^= 0xffffffffU;
while (--length >= 0) {
crc = crcTable[(crc ^ buffer[offset++]) & 0xFF] ^ (crc >> 8);
}
crc ^= 0xffffffffU;
return crc;
}

private static readonly uint[] crcTable = new uint[] {
0x00000000u, 0x77073096u, 0xee0e612cu, 0x990951bau, 0x076dc419u,
0x706af48fu, 0xe963a535u, 0x9e6495a3u, 0x0edb8832u, 0x79dcb8a4u,
0xe0d5e91eu, 0x97d2d988u, 0x09b64c2bu, 0x7eb17cbdu, 0xe7b82d07u,
0x90bf1d91u, 0x1db71064u, 0x6ab020f2u, 0xf3b97148u, 0x84be41deu,
0x1adad47du, 0x6ddde4ebu, 0xf4d4b551u, 0x83d385c7u, 0x136c9856u,
0x646ba8c0u, 0xfd62f97au, 0x8a65c9ecu, 0x14015c4fu, 0x63066cd9u,
0xfa0f3d63u, 0x8d080df5u, 0x3b6e20c8u, 0x4c69105eu, 0xd56041e4u,
0xa2677172u, 0x3c03e4d1u, 0x4b04d447u, 0xd20d85fdu, 0xa50ab56bu,
0x35b5a8fau, 0x42b2986cu, 0xdbbbc9d6u, 0xacbcf940u, 0x32d86ce3u,
0x45df5c75u, 0xdcd60dcfu, 0xabd13d59u, 0x26d930acu, 0x51de003au,
0xc8d75180u, 0xbfd06116u, 0x21b4f4b5u, 0x56b3c423u, 0xcfba9599u,
0xb8bda50fu, 0x2802b89eu, 0x5f058808u, 0xc60cd9b2u, 0xb10be924u,
0x2f6f7c87u, 0x58684c11u, 0xc1611dabu, 0xb6662d3du, 0x76dc4190u,
0x01db7106u, 0x98d220bcu, 0xefd5102au, 0x71b18589u, 0x06b6b51fu,
0x9fbfe4a5u, 0xe8b8d433u, 0x7807c9a2u, 0x0f00f934u, 0x9609a88eu,
0xe10e9818u, 0x7f6a0dbbu, 0x086d3d2du, 0x91646c97u, 0xe6635c01u,
0x6b6b51f4u, 0x1c6c6162u, 0x856530d8u, 0xf262004eu, 0x6c0695edu,
0x1b01a57bu, 0x8208f4c1u, 0xf50fc457u, 0x65b0d9c6u, 0x12b7e950u,
0x8bbeb8eau, 0xfcb9887cu, 0x62dd1ddfu, 0x15da2d49u, 0x8cd37cf3u,
0xfbd44c65u, 0x4db26158u, 0x3ab551ceu, 0xa3bc0074u, 0xd4bb30e2u,
0x4adfa541u, 0x3dd895d7u, 0xa4d1c46du, 0xd3d6f4fbu, 0x4369e96au,
0x346ed9fcu, 0xad678846u, 0xda60b8d0u, 0x44042d73u, 0x33031de5u,
0xaa0a4c5fu, 0xdd0d7cc9u, 0x5005713cu, 0x270241aau, 0xbe0b1010u,
0xc90c2086u, 0x5768b525u, 0x206f85b3u, 0xb966d409u, 0xce61e49fu,
0x5edef90eu, 0x29d9c998u, 0xb0d09822u, 0xc7d7a8b4u, 0x59b33d17u,
0x2eb40d81u, 0xb7bd5c3bu, 0xc0ba6cadu, 0xedb88320u, 0x9abfb3b6u,
0x03b6e20cu, 0x74b1d29au, 0xead54739u, 0x9dd277afu, 0x04db2615u,
0x73dc1683u, 0xe3630b12u, 0x94643b84u, 0x0d6d6a3eu, 0x7a6a5aa8u,
0xe40ecf0bu, 0x9309ff9du, 0x0a00ae27u, 0x7d079eb1u, 0xf00f9344u,
0x8708a3d2u, 0x1e01f268u, 0x6906c2feu, 0xf762575du, 0x806567cbu,
0x196c3671u, 0x6e6b06e7u, 0xfed41b76u, 0x89d32be0u, 0x10da7a5au,
0x67dd4accu, 0xf9b9df6fu, 0x8ebeeff9u, 0x17b7be43u, 0x60b08ed5u,
0xd6d6a3e8u, 0xa1d1937eu, 0x38d8c2c4u, 0x4fdff252u, 0xd1bb67f1u,
0xa6bc5767u, 0x3fb506ddu, 0x48b2364bu, 0xd80d2bdau, 0xaf0a1b4cu,
0x36034af6u, 0x41047a60u, 0xdf60efc3u, 0xa867df55u, 0x316e8eefu,
0x4669be79u, 0xcb61b38cu, 0xbc66831au, 0x256fd2a0u, 0x5268e236u,
0xcc0c7795u, 0xbb0b4703u, 0x220216b9u, 0x5505262fu, 0xc5ba3bbeu,
0xb2bd0b28u, 0x2bb45a92u, 0x5cb36a04u, 0xc2d7ffa7u, 0xb5d0cf31u,
0x2cd99e8bu, 0x5bdeae1du, 0x9b64c2b0u, 0xec63f226u, 0x756aa39cu,
0x026d930au, 0x9c0906a9u, 0xeb0e363fu, 0x72076785u, 0x05005713u,
0x95bf4a82u, 0xe2b87a14u, 0x7bb12baeu, 0x0cb61b38u, 0x92d28e9bu,
0xe5d5be0du, 0x7cdcefb7u, 0x0bdbdf21u, 0x86d3d2d4u, 0xf1d4e242u,
0x68ddb3f8u, 0x1fda836eu, 0x81be16cdu, 0xf6b9265bu, 0x6fb077e1u,
0x18b74777u, 0x88085ae6u, 0xff0f6a70u, 0x66063bcau, 0x11010b5cu,
0x8f659effu, 0xf862ae69u, 0x616bffd3u, 0x166ccf45u, 0xa00ae278u,
0xd70dd2eeu, 0x4e048354u, 0x3903b3c2u, 0xa7672661u, 0xd06016f7u,
0x4969474du, 0x3e6e77dbu, 0xaed16a4au, 0xd9d65adcu, 0x40df0b66u,
0x37d83bf0u, 0xa9bcae53u, 0xdebb9ec5u, 0x47b2cf7fu, 0x30b5ffe9u,
0xbdbdf21cu, 0xcabac28au, 0x53b39330u, 0x24b4a3a6u, 0xbad03605u,
0xcdd70693u, 0x54de5729u, 0x23d967bfu, 0xb3667a2eu, 0xc4614ab8u,
0x5d681b02u, 0x2a6f2b94u, 0xb40bbe37u, 0xc30c8ea1u, 0x5a05df1bu,
0x2d02ef8du
};
}

public Deflate() {
}

Expand All @@ -915,7 +1025,7 @@ public class Deflate : ZStream {

MutableStringStream inputStream = new MutableStringStream(str);
MutableStringStream outputStream = new MutableStringStream();
DeflateStream compressedZipStream = new DeflateStream(outputStream, CompressionMode.Compress, true);
ZDeflateStream compressedZipStream = new ZDeflateStream(outputStream, true);

int remainingInputSize = str.Length;
byte[] inputDataBlock = new byte[Math.Min(0x1000, remainingInputSize)];
Expand Down
55 changes: 55 additions & 0 deletions Merlin/Main/Languages/Ruby/Scripts/GitDiff.bat
@@ -0,0 +1,55 @@
@echo off

REM There is some problem with environment variables because of the recursive inocation of the batch file.
REM The current workaround is to explicitly unset variables. However, setlocal should be used instead.
REM
REM setlocal

if "%1" == "-?" (
echo GitDiff - enables diffing of file lists, instead of having to serially
echo diff files without being able to go back to a previous file.
echo Command-line options are passed through to git diff.
echo If GIT_FOLDER_DIFF is set, it is used to diff the file lists. Default is windff.
goto END
)

if "%GIT_DIFF_COPY_FILES%"=="" (
rd /s /q %TEMP%\GitDiff
mkdir %TEMP%\GitDiff
mkdir %TEMP%\GitDiff\old
mkdir %TEMP%\GitDiff\new

REM This batch file will be called by git diff. This env var indicates whether it is
REM being called directly, or inside git diff
set GIT_DIFF_COPY_FILES=1

set GIT_DIFF_OLD_FILES=%TEMP%\GitDiff\old
set GIT_DIFF_NEW_FILES=%TEMP%\GitDiff\new

set OLD_GIT_EXTERNAL_DIFF=%GIT_EXTERNAL_DIFF%
set GIT_EXTERNAL_DIFF=%~dp0\GitDiff.bat
echo "Press q and wait (git requirement) ..."
git diff %*

set GIT_DIFF_COPY_FILES=

set OLD_GIT_FOLDER_DIFF=%GIT_FOLDER_DIFF%
if "%GIT_FOLDER_DIFF%"=="" (
set GIT_FOLDER_DIFF=windiff
if exist "%ProgramFiles%\Beyond Compare 2\BC2.exe" (
set GIT_FOLDER_DIFF="%ProgramFiles%\Beyond Compare 2\BC2.exe"
)
)

windiff %TEMP%\GitDiff\old %TEMP%\GitDiff\new

set GIT_FOLDER_DIFF=%OLD_GIT_FOLDER_DIFF%
set GIT_EXTERNAL_DIFF=%OLD_GIT_EXTERNAL_DIFF%
goto END
)

REM diff is called by git with 7 parameters:
REM path old-file old-hex old-mode new-file new-hex new-mode
%RUBY18_EXE% %~dp0\GitDiff.rb %1 %2 %5

:END
11 changes: 11 additions & 0 deletions Merlin/Main/Languages/Ruby/Scripts/GitDiff.rb
@@ -0,0 +1,11 @@
require "ftools"

path_of_file_in_branch = ARGV[0]
file_name = File.basename(path_of_file_in_branch)
old_file = ARGV[1]
new_file = ARGV[2]
old_files_dir = ENV["GIT_DIFF_OLD_FILES"]
new_files_dir = ENV["GIT_DIFF_NEW_FILES"]

File.copy(old_file, old_files_dir + "\\" + file_name) if old_file != "/dev/null"
File.copy(new_file, new_files_dir + "\\" + file_name) if new_file != "/dev/null"
2 changes: 2 additions & 0 deletions Merlin/Users/sborde/Dev.bat
@@ -0,0 +1,2 @@
path=%path%;c:\vsl\Merlin\Main\External\Tools
doskey n2=Notepad2.exe $*
4 changes: 4 additions & 0 deletions README
@@ -0,0 +1,4 @@
Please see http://wiki.github.com/ironruby/ironruby for information on:
- Setting up a development environment with easy access to utility scripts
- Building
- Running test

0 comments on commit ce7438b

Please sign in to comment.