@JRubyMethod(name = "ungetc", compat = CompatVersion.RUBY1_9)
  @Override
  public IRubyObject ungetc19(ThreadContext context, IRubyObject arg) {
    checkReadable();

    if (!arg.isNil()) {
      int c;
      if (arg instanceof RubyFixnum) {
        c = RubyNumeric.fix2int(arg);
      } else {
        RubyString str = arg.convertToString();
        c = str.getEncoding().mbcToCode(str.getBytes(), 0, 1);
      }

      ungetcCommon(c);
    }

    return getRuntime().getNil();
  }
Example #2
0
  @JRubyMethod(rest = true, visibility = Visibility.PRIVATE)
  public IRubyObject initialize(IRubyObject[] args) {
    IRubyObject arg;
    IRubyObject pass = null;
    char[] passwd = null;
    if (org.jruby.runtime.Arity.checkArgumentCount(getRuntime(), args, 0, 2) == 0) {
      privKey = null;
      pubKey = null;
    } else {
      arg = args[0];
      if (args.length > 1) {
        pass = args[1];
      }
      if (arg instanceof RubyFixnum) {
        int keysize = RubyNumeric.fix2int(arg);
        dsaGenerate(this, keysize);
      } else {
        if (pass != null && !pass.isNil()) {
          passwd = pass.toString().toCharArray();
        }
        arg = OpenSSLImpl.to_der_if_possible(arg);
        RubyString str = arg.convertToString();

        Object val = null;
        KeyFactory fact = null;
        try {
          fact = KeyFactory.getInstance("DSA");
        } catch (NoSuchAlgorithmException e) {
          throw getRuntime().newLoadError("unsupported key algorithm (DSA)");
        }
        // TODO: ugly NoClassDefFoundError catching for no BC env. How can we remove this?
        if (null == val) {
          // PEM_read_bio_DSAPrivateKey
          try {
            val = PEMInputOutput.readDSAPrivateKey(new StringReader(str.toString()), passwd);
          } catch (NoClassDefFoundError e) {
            val = null;
          } catch (Exception e) {
            val = null;
          }
        }
        if (null == val) {
          // PEM_read_bio_DSAPublicKey
          try {
            val = PEMInputOutput.readDSAPublicKey(new StringReader(str.toString()), passwd);
          } catch (NoClassDefFoundError e) {
            val = null;
          } catch (Exception e) {
            val = null;
          }
        }
        if (null == val) {
          // PEM_read_bio_DSA_PUBKEY
          try {
            val = PEMInputOutput.readDSAPubKey(new StringReader(str.toString()));
          } catch (NoClassDefFoundError e) {
            val = null;
          } catch (Exception e) {
            val = null;
          }
        }
        if (null == val) {
          // d2i_DSAPrivateKey_bio
          try {
            val = org.jruby.ext.openssl.impl.PKey.readDSAPrivateKey(str.getBytes());
          } catch (NoClassDefFoundError e) {
            val = null;
          } catch (Exception e) {
            val = null;
          }
        }
        if (null == val) {
          // d2i_DSA_PUBKEY_bio
          try {
            val = org.jruby.ext.openssl.impl.PKey.readDSAPublicKey(str.getBytes());
          } catch (NoClassDefFoundError e) {
            val = null;
          } catch (Exception e) {
            val = null;
          }
        }
        if (null == val) {
          try {
            val = fact.generatePrivate(new PKCS8EncodedKeySpec(str.getBytes()));
          } catch (Exception e) {
            val = null;
          }
        }
        if (null == val) {
          try {
            val = fact.generatePublic(new X509EncodedKeySpec(str.getBytes()));
          } catch (Exception e) {
            val = null;
          }
        }
        if (null == val) {
          throw newDSAError(getRuntime(), "Neither PUB key nor PRIV key:");
        }

        if (val instanceof KeyPair) {
          PrivateKey privateKey = ((KeyPair) val).getPrivate();
          PublicKey publicKey = ((KeyPair) val).getPublic();
          if (privateKey instanceof DSAPrivateKey) {
            privKey = (DSAPrivateKey) privateKey;
            pubKey = (DSAPublicKey) publicKey;
          } else {
            throw newDSAError(getRuntime(), "Neither PUB key nor PRIV key:");
          }
        } else if (val instanceof DSAPrivateKey) {
          privKey = (DSAPrivateKey) val;
        } else if (val instanceof DSAPublicKey) {
          pubKey = (DSAPublicKey) val;
          privKey = null;
        } else {
          throw newDSAError(getRuntime(), "Neither PUB key nor PRIV key:");
        }
      }
    }
    return this;
  }