Skip to content

Commit daca4b9

Browse files
mkristiankares
authored andcommitted
Revert "revert some changes back to master which came in through a conflict merge from PR"
This reverts commit bd519e73d374588985259e4171362089d062f626.
1 parent 7ef939e commit daca4b9

File tree

1 file changed

+44
-49
lines changed
  • src/main/java/org/jruby/ext/openssl

1 file changed

+44
-49
lines changed

src/main/java/org/jruby/ext/openssl/BN.java

Lines changed: 44 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,6 @@
4343
import org.jruby.anno.JRubyMethod;
4444
import org.jruby.exceptions.RaiseException;
4545
import org.jruby.runtime.Arity;
46-
import org.jruby.runtime.ClassIndex;
4746
import org.jruby.runtime.ObjectAllocator;
4847
import org.jruby.runtime.ThreadContext;
4948
import org.jruby.runtime.builtin.IRubyObject;
@@ -248,21 +247,17 @@ public IRubyObject to_bn() {
248247
public IRubyObject coerce(IRubyObject other) {
249248
final Ruby runtime = getRuntime();
250249
IRubyObject self;
251-
switch (other.getMetaClass().index) {
252-
case ClassIndex.STRING:
253-
self = runtime.newString(value.toString());
254-
break;
255-
case ClassIndex.FIXNUM:
256-
case ClassIndex.BIGNUM:
257-
// FIXME: s/b faster way to convert than going through RubyString
258-
self = RubyNumeric.str2inum(runtime, runtime.newString(value.toString()), 10, true);
259-
break;
260-
default:
261-
if (other instanceof BN) {
262-
self = this;
263-
} else {
264-
throw runtime.newTypeError("Don't know how to coerce");
265-
}
250+
if ( other instanceof RubyString ) {
251+
self = runtime.newString(value.toString());
252+
}
253+
else if ( other instanceof RubyInteger ) {
254+
self = to_i();
255+
}
256+
else if ( other instanceof BN ) {
257+
self = this;
258+
}
259+
else {
260+
throw runtime.newTypeError("don't know how to coerce to " + other.getMetaClass().getName());
266261
}
267262
return runtime.newArray(other, self);
268263
}
@@ -366,27 +361,28 @@ public IRubyObject bn_exp(final ThreadContext context, IRubyObject other) {
366361
// exponent even approaching Integer.MAX_VALUE would be silly big, and
367362
// the value would take a very, very long time to calculate.)
368363
// we'll check for values < 0 (illegal) while we're at it
369-
int exp;
370-
switch(other.getMetaClass().index) {
371-
case ClassIndex.FIXNUM:
372-
long val = ((RubyFixnum)other).getLongValue();
373-
if (val >= 0 && val <= Integer.MAX_VALUE) {
374-
exp = (int)val;
375-
break;
376-
}
377-
case ClassIndex.BIGNUM:
378-
// Bignum is inherently too big
379-
throw newBNError(getRuntime(), "invalid exponent");
380-
default:
381-
if (!(other instanceof BN)) {
382-
throw getRuntime().newTypeError("Cannot convert into OpenSSL::BN");
383-
}
384-
}
385-
BigInteger val = ((BN) other).value;
386-
if (val.compareTo(BigInteger.ZERO) < 0 || val.compareTo(MAX_INT) > 0) {
387-
throw newBNError(context.runtime, "invalid exponent");
388-
}
389-
exp = val.intValue();
364+
int exp = -1;
365+
366+
if ( other instanceof RubyInteger ) {
367+
long val = ((RubyInteger) other).getLongValue();
368+
if ( val >= 0 && val <= Integer.MAX_VALUE ) {
369+
exp = (int) val;
370+
}
371+
else if ( other instanceof RubyBignum ) { // inherently too big
372+
throw newBNError(context.runtime, "invalid exponent");
373+
}
374+
}
375+
376+
if ( exp == -1 ) {
377+
if ( ! (other instanceof BN) ) {
378+
throw context.runtime.newTypeError("Cannot convert into " + other.getMetaClass().getName());
379+
}
380+
BigInteger val = ((BN) other).value;
381+
if (val.compareTo(BigInteger.ZERO) < 0 || val.compareTo(MAX_INT) > 0) {
382+
throw newBNError(context.runtime, "invalid exponent");
383+
}
384+
exp = val.intValue();
385+
}
390386

391387
try {
392388
return newBN(context.runtime, value.pow(exp));
@@ -796,17 +792,16 @@ public static RaiseException newBNError(Ruby runtime, String message) {
796792
return new RaiseException(runtime, runtime.getModule("OpenSSL").getClass("BNError"), message, true);
797793
}
798794

799-
public static BigInteger getBigInteger(IRubyObject arg) {
800-
if (arg.isNil()) return null;
801-
switch(arg.getMetaClass().index) {
802-
case ClassIndex.FIXNUM:
803-
case ClassIndex.BIGNUM:
804-
return new BigInteger(arg.toString());
805-
default:
806-
if (arg instanceof BN) {
807-
return ((BN)arg).value;
808-
}
809-
throw arg.getRuntime().newTypeError("Cannot convert into OpenSSL::BN");
810-
}
795+
public static BigInteger getBigInteger(final IRubyObject arg) {
796+
if ( arg.isNil() ) return null;
797+
798+
if ( arg instanceof RubyInteger ) {
799+
return ((RubyInteger) arg).getBigIntegerValue();
800+
}
801+
802+
if ( arg instanceof BN ) return ((BN) arg).value;
803+
804+
throw arg.getRuntime().newTypeError("Cannot convert into OpenSSL::BN");
811805
}
806+
812807
}

0 commit comments

Comments
 (0)