Skip to content
Permalink

Comparing changes

Choose two branches to see what’s changed or to start a new pull request. If you need to, you can also or learn more about diff comparisons.

Open a pull request

Create a new pull request by comparing changes across two branches. If you need to, you can also . Learn more about diff comparisons here.
base repository: jruby/jruby
Failed to load repositories. Confirm that selected base ref is valid, then try again.
Loading
base: 03869f7d7fac
Choose a base ref
...
head repository: jruby/jruby
Failed to load repositories. Confirm that selected head ref is valid, then try again.
Loading
compare: f1f2c7292ac8
Choose a head ref
  • 2 commits
  • 1 file changed
  • 1 contributor

Commits on Apr 26, 2018

  1. Don't force the input value to an int. It's supposed to be long as th…

    …at's the way to simulate 32-bit unsigned ints in the JVM
    jmiettinen authored and kares committed Apr 26, 2018
    Copy the full SHA
    dcf3fb3 View commit details
  2. Don't hack the internals of java.util.zip.CRC32 as we can use com.jcr…

    …aft.jzlib.JZlib
    jmiettinen authored and kares committed Apr 26, 2018
    Copy the full SHA
    f1f2c72 View commit details
Showing with 13 additions and 33 deletions.
  1. +13 −33 core/src/main/java/org/jruby/ext/zlib/RubyZlib.java
46 changes: 13 additions & 33 deletions core/src/main/java/org/jruby/ext/zlib/RubyZlib.java
Original file line number Diff line number Diff line change
@@ -33,8 +33,6 @@

package org.jruby.ext.zlib;

import java.lang.reflect.Field;

import java.util.zip.CRC32;
import java.util.zip.Adler32;

@@ -70,21 +68,8 @@ public class RubyZlib {
public final static String ZLIB_VERSION = "1.2.3.3";
public final static String VERSION = "0.6.0";

private static final Field crc32InternalField = getCRC32InternalField();

private static final Field getCRC32InternalField() {
try {
Field field = CRC32.class.getDeclaredField("crc");
field.setAccessible(true);
field.setInt(new CRC32(), 1);
return field;
} catch(Exception e) {
return null;
}
}

/** Create the Zlib module and add it to the Ruby runtime.
*
*
*/
public static RubyModule createZlibModule(Ruby runtime) {
RubyModule mZlib = runtime.defineModule("Zlib");
@@ -204,26 +189,21 @@ public static IRubyObject zlib_version(IRubyObject recv) {
@JRubyMethod(name = "crc32", optional = 2, module = true, visibility = PRIVATE)
public static IRubyObject crc32(IRubyObject recv, IRubyObject[] args) {
args = Arity.scanArgs(recv.getRuntime(),args,0,2);
int start = 0;
long start = 0;
ByteList bytes = null;
if (!args[0].isNil()) bytes = args[0].convertToString().getByteList();
if (!args[1].isNil()) start = (int)RubyNumeric.num2long(args[1]);
if (!args[1].isNil()) start = RubyNumeric.num2long(args[1]);

CRC32 checksum = new CRC32();
boolean slow = crc32InternalField == null;
final boolean slowPath = start != 0;
final int bytesLength = bytes == null ? 0 : bytes.length();
long result = 0;
if (bytes != null) {
if (start != 0 && !slow) {
try {
crc32InternalField.setInt(checksum, start);
} catch (IllegalAccessException iae) {
slow = true;
}
}
checksum.update(bytes.getUnsafeBytes(), bytes.begin(), bytes.length());
CRC32 checksum = new CRC32();
checksum.update(bytes.getUnsafeBytes(), bytes.begin(), bytesLength);
result = checksum.getValue();
}
long result = checksum.getValue();
if (start != 0 && slow) {
result = JZlib.crc32_combine(start, result, bytes.length());
if (slowPath) {
result = JZlib.crc32_combine(start, result, bytesLength);
}
return recv.getRuntime().newFixnum(result);
}
@@ -342,10 +322,10 @@ static RaiseException newGzipFileError(Ruby runtime, String klass, String messag
excn.setInstanceVariable("@input", runtime.getNil());
return excn.toThrowable();
}

static int FIXNUMARG(IRubyObject obj, int ifnil) {
if (obj.isNil()) return ifnil;

return RubyNumeric.fix2int(obj);
}
}