// Path: src/org/jruby/ext/krypt/Errors.java
// public class Errors {
//    
//     private Errors() { }
//     
//     public static RaiseException newKryptError(Ruby rt, String message) {
//         return newError(rt, "Krypt::Error", message);
//     }
//     
//     public static RaiseException newParseError(Ruby rt, String message) {
//         return newError(rt, "Krypt::ASN1::ParseError", message);
//     }
//     
//     public static RaiseException newSerializeError(Ruby rt, String message) {
//         return newError(rt, "Krypt::ASN1::SerializeError", message);
//     }
//     
//     public static RaiseException newASN1Error(Ruby rt, String message) {
//         return newError(rt, "Krypt::ASN1::ASN1Error", message);
//     }
//     
//     public static RaiseException newPEMError(Ruby rt, String message) {
//         return newError(rt, "Krypt::PEM::PEMError", message);
//     }
//     
//     public static RaiseException newHexError(Ruby rt, String message) {
//         return newError(rt, "Krypt::Hex::HexError", message);
//     }
//     
//     public static RaiseException newBase64Error(Ruby rt, String message) {
//         return newError(rt, "Krypt::Base64::Base64Error", message);
//     }
//     
//     public static RaiseException newDigestError(Ruby rt, String message) {
//         return newError(rt, "Krypt::Digest::DigestError", message);
//     }
//     
//     public static RaiseException newError(Ruby rt, String path, String message) {
//         return new RaiseException(rt, getClassFromPath(rt, path), message, true);
//     }
//     
//     public static RubyClass getClassFromPath(Ruby rt, String path) {
//         return (RubyClass)rt.getClassFromPath(path);
//     }
// }


import org.jruby.Ruby;
import org.jruby.RubyModule;
import org.jruby.RubyNumeric;
import org.jruby.anno.JRubyMethod;
import org.jruby.ext.krypt.Errors;
import org.jruby.runtime.ThreadContext;
import org.jruby.runtime.builtin.IRubyObject;
import org.jruby.util.ByteList;
/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
/*
 * krypt-core API - Java version
 *
 * Copyright (c) 2011-2013
 * Hiroshi Nakamura <nahi@ruby-lang.org>
 * Martin Bosslet <martin.bosslet@gmail.com>
 * All rights reserved.
 *
 * Permission is hereby granted, free of charge, to any person obtaining
 * a copy of this software and associated documentation files (the
 * "Software"), to deal in the Software without restriction, including
 * without limitation the rights to use, copy, modify, merge, publish,
 * distribute, sublicense, and/or sell copies of the Software, and to
 * permit persons to whom the Software is furnished to do so, subject to
 * the following conditions:
 *
 * The above copyright notice and this permission notice shall be
 * included in all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
 * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
 * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
 * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 */
package org.jruby.ext.krypt.helper;


/**
 * 
 * @author <a href="mailto:Martin.Bosslet@gmail.com">Martin Bosslet</a>
 */
public class RubyHelper {

    private RubyHelper() {}
    
    public static class RubyString {
    
        @JRubyMethod(meta = true)
        public static IRubyObject buffer(ThreadContext ctx, IRubyObject recv, IRubyObject size) {
            return ctx.getRuntime().newString(new ByteList(new byte[RubyNumeric.num2int(size)], false));
        }
        
        @JRubyMethod(meta = true)
        public static IRubyObject xor(ThreadContext ctx, IRubyObject recv, IRubyObject s1, IRubyObject s2) {
            ByteList tmp1 = s1.asString().getByteList();
            ByteList tmp2 = s2.asString().getByteList();
            byte[] src1 = tmp1.getUnsafeBytes();
            byte[] src2 = tmp2.getUnsafeBytes();
            
            int begin1 = tmp1.getBegin();
            int end1 = tmp1.getRealSize();
            int begin2 = tmp2.getBegin();
            int end2 = tmp2.getRealSize();
            int len = end1 - begin1;
            
            if ((end2 - begin2) != len)
// Path: src/org/jruby/ext/krypt/Errors.java
// public class Errors {
//    
//     private Errors() { }
//     
//     public static RaiseException newKryptError(Ruby rt, String message) {
//         return newError(rt, "Krypt::Error", message);
//     }
//     
//     public static RaiseException newParseError(Ruby rt, String message) {
//         return newError(rt, "Krypt::ASN1::ParseError", message);
//     }
//     
//     public static RaiseException newSerializeError(Ruby rt, String message) {
//         return newError(rt, "Krypt::ASN1::SerializeError", message);
//     }
//     
//     public static RaiseException newASN1Error(Ruby rt, String message) {
//         return newError(rt, "Krypt::ASN1::ASN1Error", message);
//     }
//     
//     public static RaiseException newPEMError(Ruby rt, String message) {
//         return newError(rt, "Krypt::PEM::PEMError", message);
//     }
//     
//     public static RaiseException newHexError(Ruby rt, String message) {
//         return newError(rt, "Krypt::Hex::HexError", message);
//     }
//     
//     public static RaiseException newBase64Error(Ruby rt, String message) {
//         return newError(rt, "Krypt::Base64::Base64Error", message);
//     }
//     
//     public static RaiseException newDigestError(Ruby rt, String message) {
//         return newError(rt, "Krypt::Digest::DigestError", message);
//     }
//     
//     public static RaiseException newError(Ruby rt, String path, String message) {
//         return new RaiseException(rt, getClassFromPath(rt, path), message, true);
//     }
//     
//     public static RubyClass getClassFromPath(Ruby rt, String path) {
//         return (RubyClass)rt.getClassFromPath(path);
//     }
// }


// Path: src/org/jruby/ext/krypt/helper/RubyHelper.java
import org.jruby.Ruby;
import org.jruby.RubyModule;
import org.jruby.RubyNumeric;
import org.jruby.anno.JRubyMethod;
import org.jruby.ext.krypt.Errors;
import org.jruby.runtime.ThreadContext;
import org.jruby.runtime.builtin.IRubyObject;
import org.jruby.util.ByteList;

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
/*
 * krypt-core API - Java version
 *
 * Copyright (c) 2011-2013
 * Hiroshi Nakamura <nahi@ruby-lang.org>
 * Martin Bosslet <martin.bosslet@gmail.com>
 * All rights reserved.
 *
 * Permission is hereby granted, free of charge, to any person obtaining
 * a copy of this software and associated documentation files (the
 * "Software"), to deal in the Software without restriction, including
 * without limitation the rights to use, copy, modify, merge, publish,
 * distribute, sublicense, and/or sell copies of the Software, and to
 * permit persons to whom the Software is furnished to do so, subject to
 * the following conditions:
 *
 * The above copyright notice and this permission notice shall be
 * included in all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
 * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
 * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
 * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 */
package org.jruby.ext.krypt.helper;


/**
 * 
 * @author <a href="mailto:Martin.Bosslet@gmail.com">Martin Bosslet</a>
 */
public class RubyHelper {

    private RubyHelper() {}
    
    public static class RubyString {
    
        @JRubyMethod(meta = true)
        public static IRubyObject buffer(ThreadContext ctx, IRubyObject recv, IRubyObject size) {
            return ctx.getRuntime().newString(new ByteList(new byte[RubyNumeric.num2int(size)], false));
        }
        
        @JRubyMethod(meta = true)
        public static IRubyObject xor(ThreadContext ctx, IRubyObject recv, IRubyObject s1, IRubyObject s2) {
            ByteList tmp1 = s1.asString().getByteList();
            ByteList tmp2 = s2.asString().getByteList();
            byte[] src1 = tmp1.getUnsafeBytes();
            byte[] src2 = tmp2.getUnsafeBytes();
            
            int begin1 = tmp1.getBegin();
            int end1 = tmp1.getRealSize();
            int begin2 = tmp2.getBegin();
            int end2 = tmp2.getRealSize();
            int len = end1 - begin1;
            
            if ((end2 - begin2) != len)
                throw Errors.newKryptError(ctx.getRuntime(), "String sizes don't match");