Example #1
0
  /**
   * EncryptedContentInfo ::= SEQUENCE { contentType ContentType, contentEncryptionAlgorithm
   * ContentEncryptionAlgorithmIdentifier, encryptedContent [0] IMPLICIT EncryptedContent OPTIONAL }
   *
   * <p>EncryptedContent ::= OCTET STRING
   */
  public static EncContent fromASN1(ASN1Encodable content) {
    ASN1Sequence sequence = (ASN1Sequence) content;
    ASN1ObjectIdentifier contentType = (ASN1ObjectIdentifier) (sequence.getObjectAt(0));
    int nid = ASN1Registry.obj2nid(contentType);

    EncContent ec = new EncContent();
    ec.setContentType(nid);
    ec.setAlgorithm(AlgorithmIdentifier.getInstance(sequence.getObjectAt(1)));
    if (sequence.size() > 2
        && sequence.getObjectAt(2) instanceof ASN1TaggedObject
        && ((ASN1TaggedObject) (sequence.getObjectAt(2))).getTagNo() == 0) {
      ASN1Encodable ee = ((ASN1TaggedObject) (sequence.getObjectAt(2))).getObject();
      if (ee instanceof ASN1Sequence && ((ASN1Sequence) ee).size() > 0) {
        ByteList combinedOctets = new ByteList();
        Enumeration enm = ((ASN1Sequence) ee).getObjects();
        while (enm.hasMoreElements()) {
          byte[] octets = ((ASN1OctetString) enm.nextElement()).getOctets();
          combinedOctets.append(octets);
        }
        ec.setEncData(new DEROctetString(combinedOctets.bytes()));
      } else {
        ec.setEncData((ASN1OctetString) ee);
      }
    }
    return ec;
  }
Example #2
0
    @JRubyMethod(name = "read", optional = 1)
    public IRubyObject read(IRubyObject[] args) throws IOException {
      if (args.length == 0 || args[0].isNil()) {
        ByteList val = new ByteList(10);
        byte[] buffer = new byte[BUFF_SIZE];
        int read = io.read(buffer);
        while (read != -1) {
          val.append(buffer, 0, read);
          read = io.read(buffer);
        }
        return RubyString.newString(getRuntime(), val);
      }

      int len = RubyNumeric.fix2int(args[0]);
      if (len < 0) {
        throw getRuntime().newArgumentError("negative length " + len + " given");
      } else if (len > 0) {
        byte[] buffer = new byte[len];
        int toRead = len;
        int offset = 0;
        int read = 0;
        while (toRead > 0) {
          read = io.read(buffer, offset, toRead);
          if (read == -1) {
            break;
          }
          toRead -= read;
          offset += read;
        } // hmm...
        return RubyString.newString(getRuntime(), new ByteList(buffer, 0, len - toRead, false));
      }

      return RubyString.newEmptyString(getRuntime());
    }
Example #3
0
  public final ByteList transform(byte[] src, final int off, final int size) {
    final int end = off + size;
    int i;

    if ((i = scan(src, off, end, bitmap)) >= end) {
      return null;
    }

    ByteList ob = new ByteList(HOUDINI_ESCAPED_SIZE(size));
    ob.append(src, off, i - off);

    while (i < end) {
      int org = i;
      if ((i = scan(src, i, end, bitmap)) > org) {
        ob.append(src, org, i - org);
        if (i >= end) {
          break;
        }
      }

      ob.append(entityTable[src[i] & 63]);
      i++;
    }

    return ob;
  }
Example #4
0
  @TruffleBoundary
  private byte[] encode(ByteList bytes) {
    // TODO CS 30-Mar-15 should write our own optimizable version of UU

    final ByteList output = new ByteList();
    Pack.encodeUM(null, bytes, length, ignoreStar, 'u', output);
    return output.bytes();
  }
Example #5
0
 private int nameToBackrefNumber(RubyString str) {
   ByteList value = str.getByteList();
   try {
     return pattern.nameToBackrefNumber(
         value.getUnsafeBytes(), value.getBegin(), value.getBegin() + value.getRealSize(), regs);
   } catch (JOniException je) {
     throw getRuntime().newIndexError(je.getMessage());
   }
 }
 private void swallowLF(ByteList list) {
   while (data.pos < list.getRealSize()) {
     if (list.get((int) data.pos) == '\n') {
       data.pos++;
     } else {
       break;
     }
   }
 }
Example #7
0
 @JRubyMethod(name = "inspect")
 public IRubyObject inspect(ThreadContext context) {
   ByteList bytes = new ByteList();
   bytes.append("#<Encoding:".getBytes());
   bytes.append(name);
   if (isDummy) bytes.append(" (dummy)".getBytes());
   bytes.append('>');
   return RubyString.newUsAsciiStringNoCopy(context.runtime, bytes);
 }
  private void ungetcCommon(int c) {
    data.internal.modify();
    data.pos--;

    ByteList bytes = data.internal.getByteList();

    if (isEndOfString()) bytes.length((int) data.pos + 1);

    bytes.set((int) data.pos, c);
  }
Example #9
0
 public static int getBackrefNumber(DynamicObject matchData, ByteList value) {
   assert RubyGuards.isRubyMatchData(matchData);
   return Layouts.REGEXP
       .getRegex(Layouts.MATCH_DATA.getFields(matchData).regexp)
       .nameToBackrefNumber(
           value.getUnsafeBytes(),
           value.getBegin(),
           value.getBegin() + value.getRealSize(),
           Layouts.MATCH_DATA.getFields(matchData).region);
 }
Example #10
0
 public SymbolNode(ISourcePosition position, ByteList value) {
   super(position, false);
   this.name = value.toString().intern();
   // FIXME: A full scan to determine whether we should back off to US-ASCII.  Lexer should just do
   // this properly.
   if (value.lengthEnc() == value.length()) {
     this.encoding = USASCIIEncoding.INSTANCE;
   } else {
     this.encoding = value.getEncoding();
   }
 }
Example #11
0
 @JRubyMethod(name = "ruby=", required = 1)
 public IRubyObject ruby_set(final ThreadContext ctx, IRubyObject arg) {
   RubyString string;
   try {
     string = arg.convertToString();
   } catch (RaiseException re) {
     throw newTypeError(ctx.runtime, arg.getMetaClass(), "String");
   }
   ByteList bytes = string.getByteList();
   value.set(bytes.getUnsafeBytes(), bytes.getBegin(), bytes.getRealSize());
   return arg;
 }
Example #12
0
 public static SpecialEncoding valueOf(ByteList name) {
   if (name.caseInsensitiveCmp(LOCALE_BL) == 0) {
     return LOCALE;
   } else if (name.caseInsensitiveCmp(EXTERNAL_BL) == 0) {
     return EXTERNAL;
   } else if (name.caseInsensitiveCmp(INTERNAL_BL) == 0) {
     return INTERNAL;
   } else if (name.caseInsensitiveCmp(FILESYSTEM_BL) == 0) {
     return FILESYSTEM;
   }
   return null;
 }
Example #13
0
  private RubyString strioSubstr(Ruby runtime, int pos, int len) {
    RubyString str = ptr.string;
    ByteList strByteList = str.getByteList();
    byte[] strBytes = strByteList.getUnsafeBytes();
    Encoding enc = str.getEncoding();
    int rlen = str.size() - pos;

    if (len > rlen) len = rlen;
    if (len < 0) len = 0;

    if (len == 0) return RubyString.newEmptyString(runtime);
    return RubyString.newStringShared(runtime, strBytes, strByteList.getBegin() + pos, len, enc);
  }
Example #14
0
 public final void marshal(
     ThreadContext context, InvocationBuffer buffer, IRubyObject parameter) {
   if (parameter instanceof RubyString) {
     Util.checkStringSafety(context.getRuntime(), parameter);
     ByteList bl = ((RubyString) parameter).getByteList();
     buffer.putArray(
         bl.unsafeBytes(), bl.begin(), bl.length(), ArrayFlags.IN | ArrayFlags.NULTERMINATE);
   } else if (parameter.isNil()) {
     buffer.putAddress(0);
   } else {
     throw context.getRuntime().newArgumentError("Invalid string parameter");
   }
 }
Example #15
0
  @JRubyMethod(name = "each_byte", frame = true)
  public IRubyObject each_byte(ThreadContext context, Block block) {
    checkReadable();
    Ruby runtime = context.getRuntime();
    ByteList bytes = internal.getByteList();

    // Check the length every iteration, since
    // the block can modify this string.
    while (pos < bytes.length()) {
      block.yield(context, runtime.newFixnum(bytes.get((int) pos++) & 0xFF));
    }
    return runtime.getNil();
  }
  @Override
  public IRubyObject each_byte(ThreadContext context, Block block) {
    checkReadable();
    Ruby runtime = context.runtime;
    ByteList bytes = data.internal.getByteList();

    // Check the length every iteration, since
    // the block can modify this string.
    while (data.pos < bytes.length()) {
      block.yield(context, runtime.newFixnum(bytes.get((int) data.pos++) & 0xFF));
    }
    return this;
  }
Example #17
0
    public final void marshal(
        ThreadContext context, InvocationBuffer buffer, IRubyObject parameter) {
      if (parameter instanceof Buffer) {
        addBufferParameter(buffer, parameter, flags);
      } else if (parameter instanceof Pointer) {
        buffer.putAddress(getAddress((Pointer) parameter));
      } else if (parameter instanceof Struct) {
        IRubyObject memory = ((Struct) parameter).getMemory();
        if (memory instanceof Buffer) {
          addBufferParameter(buffer, memory, flags);
        } else if (memory instanceof Pointer) {
          buffer.putAddress(getAddress((Pointer) memory));
        } else if (memory == null || memory.isNil()) {
          buffer.putAddress(0L);
        } else {
          throw context.getRuntime().newArgumentError("Invalid Struct memory");
        }
      } else if (parameter.isNil()) {
        buffer.putAddress(0L);
      } else if (parameter instanceof RubyString) {
        ByteList bl = ((RubyString) parameter).getByteList();
        buffer.putArray(
            bl.getUnsafeBytes(), bl.begin(), bl.length(), flags | ArrayFlags.NULTERMINATE);

      } else if (parameter.respondsTo("to_ptr")) {
        final int MAXRECURSE = 4;
        for (int depth = 0; depth < MAXRECURSE; ++depth) {
          IRubyObject ptr = parameter.callMethod(context, "to_ptr");
          if (ptr instanceof Pointer) {
            buffer.putAddress(getAddress((Pointer) ptr));
          } else if (ptr instanceof Buffer) {
            addBufferParameter(buffer, ptr, flags);
          } else if (ptr.isNil()) {
            buffer.putAddress(0L);
          } else if (depth < MAXRECURSE && ptr.respondsTo("to_ptr")) {
            parameter = ptr;
            continue;
          } else {
            throw context.getRuntime().newArgumentError("to_ptr returned an invalid pointer");
          }
          break;
        }

      } else if (parameter instanceof RubyInteger && acceptIntegerValues) {

        buffer.putAddress(((RubyInteger) parameter).getLongValue());

      } else {
        throw context.getRuntime().newArgumentError("Invalid buffer/pointer parameter");
      }
    }
Example #18
0
 public static final void checkStringSafety(Ruby runtime, IRubyObject value) {
   RubyString s = value.asString();
   if (runtime.getSafeLevel() > 0 && s.isTaint()) {
     throw runtime.newSecurityError("Unsafe string parameter");
   }
   ByteList bl = s.getByteList();
   final byte[] array = bl.unsafeBytes();
   final int end = bl.length();
   for (int i = bl.begin(); i < end; ++i) {
     if (array[i] == (byte) 0) {
       throw runtime.newSecurityError("string contains null byte");
     }
   }
 }
Example #19
0
 @JRubyMethod(name = "chr", compat = CompatVersion.RUBY1_9)
 public RubyString chr19(ThreadContext context, IRubyObject arg) {
   Ruby runtime = context.getRuntime();
   long value = getLongValue();
   Encoding enc = arg.convertToString().toEncoding(runtime);
   int n;
   if (value < 0 || (n = StringSupport.codeLength(runtime, enc, (int) value)) <= 0) {
     throw runtime.newRangeError(this.toString() + " out of char range");
   }
   ByteList bytes = new ByteList(n);
   enc.codeToMbc((int) value, bytes.getUnsafeBytes(), 0);
   bytes.setRealSize(n);
   return RubyString.newStringNoCopy(runtime, bytes, enc, 0);
 }
Example #20
0
  private void updateCharOffset() {
    if (charOffsetUpdated) return;

    ByteList value = str.getByteList();
    Encoding enc = value.getEncoding();

    if (regs == null) {
      updateCharOffsetOnlyOneReg(value, enc);
    } else {
      updateCharOffsetManyRegs(value, enc);
    }

    charOffsetUpdated = true;
  }
Example #21
0
  public static void updateCharOffset(DynamicObject matchData) {
    assert RubyGuards.isRubyMatchData(matchData);
    if (Layouts.MATCH_DATA.getFields(matchData).charOffsetUpdated) return;

    ByteList value = Layouts.STRING.getByteList(Layouts.MATCH_DATA.getFields(matchData).source);
    Encoding enc = value.getEncoding();

    if (Layouts.MATCH_DATA.getFields(matchData).region == null) {
      updateCharOffsetOnlyOneReg(matchData, value, enc);
    } else {
      updateCharOffsetManyRegs(matchData, value, enc);
    }

    Layouts.MATCH_DATA.getFields(matchData).charOffsetUpdated = true;
  }
Example #22
0
  private void ungetbyteCommon(int c) {
    ptr.string.modify();
    ptr.pos--;

    ByteList bytes = ptr.string.getByteList();

    if (isEndOfString()) bytes.length((int) ptr.pos + 1);

    if (ptr.pos == -1) {
      bytes.prepend((byte) c);
      ptr.pos = 0;
    } else {
      bytes.set((int) ptr.pos, c);
    }
  }
Example #23
0
 public static RubyFloat unmarshalFrom(UnmarshalStream input) throws java.io.IOException {
   ByteList value = input.unmarshalString();
   RubyFloat result;
   if (value.equals(NAN_BYTELIST)) {
     result = RubyFloat.newFloat(input.getRuntime(), RubyFloat.NAN);
   } else if (value.equals(NEGATIVE_INFINITY_BYTELIST)) {
     result = RubyFloat.newFloat(input.getRuntime(), Double.NEGATIVE_INFINITY);
   } else if (value.equals(INFINITY_BYTELIST)) {
     result = RubyFloat.newFloat(input.getRuntime(), Double.POSITIVE_INFINITY);
   } else {
     result = RubyFloat.newFloat(input.getRuntime(), ConvertDouble.byteListToDouble(value, false));
   }
   input.registerLinkTarget(result);
   return result;
 }
Example #24
0
  @JRubyMethod(name = {"each_byte", "bytes"})
  public IRubyObject each_byte(ThreadContext context, Block block) {
    if (!block.isGiven()) return enumeratorize(context.runtime, this, "each_byte");

    checkReadable();
    Ruby runtime = context.runtime;
    ByteList bytes = ptr.string.getByteList();

    // Check the length every iteration, since
    // the block can modify this string.
    while (ptr.pos < bytes.length()) {
      block.yield(context, runtime.newFixnum(bytes.get((int) ptr.pos++) & 0xFF));
    }
    return this;
  }
Example #25
0
  private ByteList fromEncodedBytes(Ruby runtime, Encoding enc, int value) {
    int n;
    try {
      n = value < 0 ? 0 : enc.codeToMbcLength(value);
    } catch (EncodingException ee) {
      n = 0;
    }

    if (n <= 0) throw runtime.newRangeError(this.toString() + " out of char range");

    ByteList bytes = new ByteList(n);
    enc.codeToMbc(value, bytes.getUnsafeBytes(), 0);
    bytes.setRealSize(n);
    return bytes;
  }
Example #26
0
  private void updatePairs(ByteList value, Encoding encoding, Pair[] pairs) {
    Arrays.sort(pairs);

    int length = pairs.length;
    byte[] bytes = value.getUnsafeBytes();
    int p = value.getBegin();
    int s = p;
    int c = 0;

    for (int i = 0; i < length; i++) {
      int q = s + pairs[i].bytePos;
      c += StringSupport.strLength(encoding, bytes, p, q);
      pairs[i].charPos = c;
      p = q;
    }
  }
Example #27
0
  @JRubyMethod(name = "crc32", optional = 2, module = true, visibility = Visibility.PRIVATE)
  public static IRubyObject crc32(IRubyObject recv, IRubyObject[] args) throws Exception {
    args = Arity.scanArgs(recv.getRuntime(), args, 0, 2);
    long crc = 0;
    ByteList bytes = null;

    if (!args[0].isNil()) bytes = args[0].convertToString().getByteList();
    if (!args[1].isNil()) crc = RubyNumeric.num2long(args[1]);

    CRC32Ext ext = new CRC32Ext((int) crc);
    if (bytes != null) {
      ext.update(bytes.unsafeBytes(), bytes.begin(), bytes.length());
    }

    return recv.getRuntime().newFixnum(ext.getValue());
  }
Example #28
0
 private IRubyObject internalSepGets(ByteList sep) throws IOException {
   ByteList result = new ByteList();
   int ce = io.read();
   while (ce != -1 && sep.indexOf(ce) == -1) {
     result.append((byte) ce);
     ce = io.read();
   }
   // io.available() only returns 0 after EOF is encountered
   // so we need to differentiate between the empty string and EOF
   if (0 == result.length() && -1 == ce) {
     return getRuntime().getNil();
   }
   line++;
   result.append(sep);
   return RubyString.newString(getRuntime(), result);
 }
Example #29
0
  @JRubyMethod(name = "adler32", optional = 2, module = true, visibility = Visibility.PRIVATE)
  public static IRubyObject adler32(IRubyObject recv, IRubyObject[] args) throws Exception {
    args = Arity.scanArgs(recv.getRuntime(), args, 0, 2);
    int adler = 1;
    ByteList bytes = null;
    if (!args[0].isNil()) bytes = args[0].convertToString().getByteList();
    if (!args[1].isNil()) adler = RubyNumeric.fix2int(args[1]);

    Adler32Ext ext = new Adler32Ext(adler);
    if (bytes != null) {
      ext.update(
          bytes.unsafeBytes(),
          bytes.begin(),
          bytes.length()); // it's safe since adler.update doesn't modify the array
    }
    return recv.getRuntime().newFixnum(ext.getValue());
  }
Example #30
0
  // Make string based on internal data encoding (which ironically is its
  // external encoding.  This seems messy and we should consider a more
  // uniform method for makeing strings (we have a slightly different variant
  // of this in RubyIO.
  private RubyString makeString(Ruby runtime, ByteList buf, boolean setEncoding) {
    if (runtime.is1_9() && setEncoding) buf.setEncoding(ptr.string.getEncoding());

    RubyString str = RubyString.newString(runtime, buf);
    str.setTaint(true);

    return str;
  }