Skip to content

Commit

Permalink
Arity-split Point#initialize
Browse files Browse the repository at this point in the history
  • Loading branch information
headius committed Jun 11, 2024
1 parent b32a4fa commit aa1db88
Showing 1 changed file with 49 additions and 26 deletions.
75 changes: 49 additions & 26 deletions src/main/java/org/jruby/ext/openssl/PKeyEC.java
Original file line number Diff line number Diff line change
Expand Up @@ -931,44 +931,53 @@ private static RaiseException newError(final Ruby runtime, final String message)
return Utils.newError(runtime, Error, message);
}

@JRubyMethod(rest = true, visibility = Visibility.PRIVATE)
public IRubyObject initialize(final ThreadContext context, final IRubyObject[] args) {
final Ruby runtime = context.runtime;
@JRubyMethod(visibility = Visibility.PRIVATE)
public IRubyObject initialize(final ThreadContext context, final IRubyObject groupOrPoint) {
getPointAndGroup(context, groupOrPoint);

final int argc = Arity.checkArgumentCount(runtime, args, 1, 2);
final IRubyObject arg = args[0];
return this;
}

if ( arg instanceof Point ) {
this.group = ((Point) arg).group;
this.point = ((Point) arg).point;
@JRubyMethod(visibility = Visibility.PRIVATE)
public IRubyObject initialize(final ThreadContext context, final IRubyObject groupOrPoint, final IRubyObject bn) {
if (getPointAndGroup(context, groupOrPoint)) {
return this;
}

if ( arg instanceof Group ) {
this.group = (Group) arg;
final byte[] encoded;
if (bn instanceof BN) {
encoded = ((BN) bn).getValue().abs().toByteArray();
} else {
throw runtime.newTypeError(arg, _EC(runtime).getClass("Group"));
encoded = bn.convertToString().getBytes();
}

if ( argc == 2 ) { // (group, bn)
final byte[] encoded;
if (args[1] instanceof BN) {
encoded = ((BN) args[1]).getValue().abs().toByteArray();
} else {
encoded = args[1].convertToString().getBytes();
}
try {
this.point = ECPointUtil.decodePoint(group.getCurve(), encoded);
}
catch (IllegalArgumentException ex) {
// MRI: OpenSSL::PKey::EC::Point::Error: invalid encoding
throw newError(context.runtime, ex.getMessage());
}
try {
this.point = ECPointUtil.decodePoint(group.getCurve(), encoded);
}
catch (IllegalArgumentException ex) {
// MRI: OpenSSL::PKey::EC::Point::Error: invalid encoding
throw newError(context.runtime, ex.getMessage());
}

return this;
}

private boolean getPointAndGroup(ThreadContext context, IRubyObject groupOrPoint) {
final Ruby runtime = context.runtime;

if ( groupOrPoint instanceof Point) {
this.group = ((Point) groupOrPoint).group;
this.point = ((Point) groupOrPoint).point;
return true;
}

if ( groupOrPoint instanceof Group) {
this.group = (Group) groupOrPoint;
} else {
throw runtime.newTypeError(groupOrPoint, _EC(runtime).getClass("Group"));
}
return false;
}

@Override
@JRubyMethod(name = { "==", "eql?" })
public IRubyObject op_equal(final ThreadContext context, final IRubyObject obj) {
Expand Down Expand Up @@ -1059,6 +1068,20 @@ public IRubyObject inspect() {
return ObjectSupport.inspect(this, (List) Collections.singletonList(entry));
}

@Deprecated
public IRubyObject initialize(final ThreadContext context, final IRubyObject[] args) {
final int argc = Arity.checkArgumentCount(context.runtime, args, 1, 2);

switch (argc) {
case 1:
return initialize(context, args[0]);
case 2:
return initialize(context, args[0], args[1]);
default:
throw context.runtime.newArgumentError(args.length, 1);
}
}

}

static byte[] encode(final ECPublicKey pubKey) {
Expand Down

0 comments on commit aa1db88

Please sign in to comment.