Skip to content

Commit 7ef939e

Browse files
mkristiankares
authored andcommitted
revert some changes back to master which came in through a conflict merge from PR
1 parent 894a5fe commit 7ef939e

File tree

1 file changed

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

1 file changed

+49
-85
lines changed

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

Lines changed: 49 additions & 85 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@
4343
import org.jruby.anno.JRubyMethod;
4444
import org.jruby.exceptions.RaiseException;
4545
import org.jruby.runtime.Arity;
46+
import org.jruby.runtime.ClassIndex;
4647
import org.jruby.runtime.ObjectAllocator;
4748
import org.jruby.runtime.ThreadContext;
4849
import org.jruby.runtime.builtin.IRubyObject;
@@ -247,32 +248,21 @@ public IRubyObject to_bn() {
247248
public IRubyObject coerce(IRubyObject other) {
248249
final Ruby runtime = getRuntime();
249250
IRubyObject self;
250-
// switch (other.getMetaClass().index) {
251-
// case ClassIndex.STRING:
252-
// self = runtime.newString(value.toString());
253-
// break;
254-
// case ClassIndex.FIXNUM:
255-
// case ClassIndex.BIGNUM:
256-
// // FIXME: s/b faster way to convert than going through RubyString
257-
// self = RubyNumeric.str2inum(runtime, runtime.newString(value.toString()), 10, true);
258-
// break;
259-
// default:
260-
// if (other instanceof BN) {
261-
// self = this;
262-
// } else {
263-
// throw runtime.newTypeError("Don't know how to coerce");
264-
// }
265-
if ( other instanceof RubyString ) {
266-
self = runtime.newString(value.toString());
267-
}
268-
else if ( other instanceof RubyInteger ) {
269-
self = to_i();
270-
}
271-
else if ( other instanceof BN ) {
272-
self = this;
273-
}
274-
else {
275-
throw runtime.newTypeError("don't know how to coerce to " + other.getMetaClass().getName());
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+
}
276266
}
277267
return runtime.newArray(other, self);
278268
}
@@ -376,43 +366,27 @@ public IRubyObject bn_exp(final ThreadContext context, IRubyObject other) {
376366
// exponent even approaching Integer.MAX_VALUE would be silly big, and
377367
// the value would take a very, very long time to calculate.)
378368
// we'll check for values < 0 (illegal) while we're at it
379-
// int exp;
380-
// switch(other.getMetaClass().index) {
381-
// case ClassIndex.FIXNUM: {
382-
// long val = ((RubyFixnum)other).getLongValue();
383-
// if (val >= 0 && val <= Integer.MAX_VALUE) {
384-
// exp = (int)val;
385-
// break;
386-
// }
387-
// }
388-
// case ClassIndex.BIGNUM:
389-
// // Bignum is inherently too big
390-
// throw newBNError(getRuntime(), "invalid exponent");
391-
// default: {
392-
// if (!(other instanceof BN)) {
393-
// throw getRuntime().newTypeError("Cannot convert into OpenSSL::BN");
394-
int exp = -1;
395-
396-
if ( other instanceof RubyInteger ) {
397-
long val = ((RubyInteger) other).getLongValue();
398-
if ( val >= 0 && val <= Integer.MAX_VALUE ) {
399-
exp = (int) val;
400-
}
401-
else if ( other instanceof RubyBignum ) { // inherently too big
402-
throw newBNError(context.runtime, "invalid exponent");
403-
}
404-
}
405-
406-
if ( exp == -1 ) {
407-
if ( ! (other instanceof BN) ) {
408-
throw context.runtime.newTypeError("Cannot convert into " + other.getMetaClass().getName());
409-
}
410-
BigInteger val = ((BN) other).value;
411-
if (val.compareTo(BigInteger.ZERO) < 0 || val.compareTo(MAX_INT) > 0) {
412-
throw newBNError(context.runtime, "invalid exponent");
413-
}
414-
exp = val.intValue();
415-
}
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();
416390

417391
try {
418392
return newBN(context.runtime, value.pow(exp));
@@ -822,27 +796,17 @@ public static RaiseException newBNError(Ruby runtime, String message) {
822796
return new RaiseException(runtime, runtime.getModule("OpenSSL").getClass("BNError"), message, true);
823797
}
824798

825-
// public static BigInteger getBigInteger(IRubyObject arg) {
826-
// if (arg.isNil()) return null;
827-
// switch(arg.getMetaClass().index) {
828-
// case ClassIndex.FIXNUM:
829-
// case ClassIndex.BIGNUM:
830-
// return new BigInteger(arg.toString());
831-
// default:
832-
// if (arg instanceof BN) {
833-
// return ((BN)arg).value;
834-
// }
835-
// throw arg.getRuntime().newTypeError("Cannot convert into OpenSSL::BN");
836-
public static BigInteger getBigInteger(final IRubyObject arg) {
837-
if ( arg.isNil() ) return null;
838-
839-
if ( arg instanceof RubyInteger ) {
840-
return ((RubyInteger) arg).getBigIntegerValue();
841-
}
842-
843-
if ( arg instanceof BN ) return ((BN) arg).value;
844-
845-
throw arg.getRuntime().newTypeError("Cannot convert into OpenSSL::BN");
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+
}
846811
}
847-
848812
}

0 commit comments

Comments
 (0)