Class Pack

java.lang.Object
org.jruby.util.Pack

public class Pack extends Object
  • Field Details

    • sSp10

      private static final byte[] sSp10
    • sNil10

      private static final byte[] sNil10
    • IS_STAR

      private static final int IS_STAR
      See Also:
    • ASCII

      private static final org.jcodings.specific.ASCIIEncoding ASCII
    • USASCII

      private static final org.jcodings.specific.USASCIIEncoding USASCII
    • UTF8

      private static final org.jcodings.specific.UTF8Encoding UTF8
    • NATIVE_CODES

      private static final String NATIVE_CODES
      Native pack type.
      See Also:
    • MAPPED_CODES

      private static final String MAPPED_CODES
      See Also:
    • BE

      private static final char BE
      See Also:
    • LE

      private static final char LE
      See Also:
    • ENDIANESS_CODES

      private static final String ENDIANESS_CODES
    • UNPACK_IGNORE_NULL_CODES

      private static final String UNPACK_IGNORE_NULL_CODES
      See Also:
    • PACK_IGNORE_NULL_CODES

      private static final String PACK_IGNORE_NULL_CODES
      See Also:
    • PACK_IGNORE_NULL_CODES_WITH_MODIFIERS

      private static final String PACK_IGNORE_NULL_CODES_WITH_MODIFIERS
      See Also:
    • UNPACK_ARRAY

      private static final int UNPACK_ARRAY
      Unpack modes
      See Also:
    • UNPACK_BLOCK

      private static final int UNPACK_BLOCK
      See Also:
    • UNPACK_1

      private static final int UNPACK_1
      See Also:
    • sTooFew

      private static final String sTooFew
      See Also:
    • uu_table

      private static final byte[] uu_table
    • b64_table

      private static final byte[] b64_table
    • sHexDigits

      public static final byte[] sHexDigits
    • b64_xtable

      public static final int[] b64_xtable
    • converters

      private static final Pack.Converter[] converters
    • utf8_limits

      private static final long[] utf8_limits
  • Constructor Details

    • Pack

      public Pack()
  • Method Details

    • num2quad

      private static long num2quad(IRubyObject arg)
    • obj2flt

      private static float obj2flt(Ruby runtime, IRubyObject o)
    • obj2dbl

      private static double obj2dbl(Ruby runtime, IRubyObject o)
    • unpackInt_i

      public static int unpackInt_i(ByteBuffer enc)
    • packInt_i

      public static ByteList packInt_i(ByteList result, int s)
    • encodeUM

      public static void encodeUM(Ruby runtime, ByteList lCurElemString, int occurrences, boolean ignoreStar, char type, ByteList result)
    • encodes

      private static ByteList encodes(Ruby runtime, ByteList io2Append, byte[] charsToEncode, int startIndex, int length, int charCount, byte encodingType, boolean tailLf)
      encodes a String in base64 or its uuencode variant. appends the result of the encoding in a StringBuffer
      Parameters:
      io2Append - The StringBuffer which should receive the result
      charsToEncode - The String to encode
      startIndex -
      length - The max number of characters to encode
      charCount -
      encodingType - the type of encoding required (this is the same type as used by the pack method)
      tailLf - true if the traililng "\n" is needed
      Returns:
      the io2Append buffer
    • unpack

      public static RubyArray unpack(Ruby runtime, ByteList encodedString, ByteList formatString)
    • unpack

      public static RubyArray unpack(ThreadContext context, RubyString encoded, ByteList formatString)
      Parameters:
      context -
      encoded -
      formatString -
      Returns:
      unpacked array
      See Also:
    • unpackWithBlock

      public static RubyArray unpackWithBlock(ThreadContext context, RubyString encoded, ByteList formatString, Block block)
      Decodes str (which may contain binary data) according to the format string, returning an array of each value extracted. The format string consists of a sequence of single-character directives.
      Each directive may be followed by a number, indicating the number of times to repeat with this directive. An asterisk (``*'') will use up all remaining elements.
      Note that if passed a block, this method will return null and instead yield results to the block. The directives sSiIlL may each be followed by an underscore (``_'') to use the underlying platform's native size for the specified type; otherwise, it uses a platform-independent consistent size.
      Spaces are ignored in the format string.

      Directives for String#unpack
      Format Function Returns
      A String with trailing nulls and spaces removed. String
      a String. String
      B Extract bits from each character (msb first). String
      b Extract bits from each character (lsb first). String
      C Extract a character as an unsigned integer. Fixnum
      c Extract a character as an integer. Fixnum
      d Treat sizeof(double) characters as a native double. Float
      E Treat sizeof(double) characters as a double in little-endian byte order. Float
      e Treat sizeof(float) characters as a float in little-endian byte order. Float
      f Treat sizeof(float) characters as a native float. Float
      G Treat sizeof(double) characters as a double in network byte order. Float
      g Treat sizeof(float) characters as a float in network byte order. Float
      H Extract hex nibbles from each character (most significant first). String
      h Extract hex nibbles from each character (least significant first). String
      I Treat sizeof(int) 1 successive characters as an unsigned native integer. Integer
      i Treat sizeof(int) 1 successive characters as a signed native integer. Integer
      L Treat four1 successive characters as an unsigned native long integer. Integer
      l Treat four1 successive characters as a signed native long integer. Integer
      M Extract a quoted-printable string. String
      m Extract a base64 encoded string. String
      N Treat four characters as an unsigned long in network byte order. Fixnum
      n Treat two characters as an unsigned short in network byte order. Fixnum
      P Treat sizeof(char *) characters as a pointer, and return len characters from the referenced location. String
      p Treat sizeof(char *) characters as a pointer to a null-terminated string. String
      S Treat two1 successive characters as an unsigned short in native byte order. Fixnum
      s Treat two1 successive characters as a signed short in native byte order. Fixnum
      U Extract UTF-8 characters as unsigned integers. Integer
      u Extract a UU-encoded string. String
      V Treat four characters as an unsigned long in little-endian byte order. Fixnum
      v Treat two characters as an unsigned short in little-endian byte order. Fixnum
      X Skip backward one character. ---
      x Skip forward one character. ---
      Z String with trailing nulls removed. String
      @ Skip to the offset given by the length argument. ---

      1 May be modified by appending ``_'' to the directive.

      See Also:
    • unpackBase46Strict

      private static RubyString unpackBase46Strict(Ruby runtime, ByteList input)
    • unpack1WithBlock

      public static IRubyObject unpack1WithBlock(ThreadContext context, RubyString encoded, ByteList formatString, Block block)
    • unpackInternal

      private static IRubyObject unpackInternal(ThreadContext context, RubyString encoded, ByteList formatString, int mode, Block block)
    • unpack_w

      private static IRubyObject unpack_w(ThreadContext context, Block block, Ruby runtime, RubyArray result, ByteBuffer encode, int occurrences, int mode)
    • unpack_x

      private static void unpack_x(Ruby runtime, ByteBuffer encode, int occurrences)
    • unpack_X

      private static void unpack_X(Ruby runtime, ByteBuffer encode, int occurrences)
    • unpack_U

      private static IRubyObject unpack_U(ThreadContext context, Block block, Ruby runtime, RubyArray result, ByteBuffer encode, int occurrences, int mode)
    • unpack_M

      private static IRubyObject unpack_M(ThreadContext context, Block block, RubyArray result, boolean tainted, ByteBuffer encode, int mode)
    • unpack_m

      private static IRubyObject unpack_m(ThreadContext context, Block block, Ruby runtime, RubyArray result, boolean tainted, ByteBuffer encode, int occurrences, int mode)
    • unpack_m_nonzeroOccurrences

      private static int unpack_m_nonzeroOccurrences(ByteBuffer encode, byte[] lElem, int a, int b, int c, int index)
    • unpack_m_zeroOccurrences

      private static int unpack_m_zeroOccurrences(Ruby runtime, ByteBuffer encode, byte[] lElem, int a, int b, int c, int index, int s)
    • unpack_u

      private static IRubyObject unpack_u(ThreadContext context, Block block, RubyArray result, boolean tainted, ByteBuffer encode, int mode)
    • unpack_H

      private static IRubyObject unpack_H(ThreadContext context, Block block, RubyArray result, boolean tainted, ByteBuffer encode, int occurrences, int mode)
    • unpack_h

      private static IRubyObject unpack_h(ThreadContext context, Block block, RubyArray result, boolean tainted, ByteBuffer encode, int occurrences, int mode)
    • unpack_B

      private static IRubyObject unpack_B(ThreadContext context, Block block, RubyArray result, boolean tainted, ByteBuffer encode, int occurrences, int mode)
    • unpack_b

      private static IRubyObject unpack_b(ThreadContext context, Block block, RubyArray result, boolean tainted, ByteBuffer encode, int occurrences, int mode)
    • unpack_a

      private static IRubyObject unpack_a(ThreadContext context, Block block, RubyArray result, boolean tainted, ByteBuffer encode, int occurrences, int mode)
    • unpack_Z

      private static IRubyObject unpack_Z(ThreadContext context, Block block, RubyArray result, boolean tainted, ByteBuffer encode, int occurrences, int mode)
    • unpack_A

      private static IRubyObject unpack_A(ThreadContext context, Block block, RubyArray result, boolean tainted, ByteBuffer encode, int occurrences, int mode)
    • unpack_at

      private static void unpack_at(Ruby runtime, ByteList encodedString, ByteBuffer encode, int occurrences)
    • checkLimit

      private static int checkLimit(Ruby runtime, ByteBuffer encode, int limit)
    • unpackWithBlock

      @Deprecated public static RubyArray unpackWithBlock(ThreadContext context, Ruby runtime, ByteList encodedString, ByteList formatString, Block block)
      Deprecated.
    • appendOrYield

      private static void appendOrYield(ThreadContext context, Block block, RubyArray result, IRubyObject item, int mode)
    • appendOrYield

      private static IRubyObject appendOrYield(ThreadContext context, Block block, RubyArray result, ByteList item, int mode, boolean taint)
    • utf8Decode

      public static int utf8Decode(Ruby runtime, byte[] to, int p, int code)
      rb_uv_to_utf8
    • utf8Decode

      private static int utf8Decode(ByteBuffer buffer)
      utf8_to_uv
    • safeGet

      public static int safeGet(ByteBuffer encode)
    • safeGetIgnoreNull

      private static int safeGetIgnoreNull(ByteBuffer encode)
    • decode

      public static IRubyObject decode(ThreadContext context, Ruby runtime, ByteBuffer encode, int occurrences, RubyArray result, Block block, Pack.Converter converter, int mode)
    • encode

      public static int encode(Ruby runtime, int occurrences, ByteList result, RubyArray list, int index, Pack.ConverterExecutor converter)
    • executor

      private static Pack.ConverterExecutor executor()
    • shrink

      private static final ByteList shrink(ByteList i2Shrink, int iLength)
      shrinks a stringbuffer. shrinks a stringbuffer by a number of characters.
      Parameters:
      i2Shrink - the stringbuffer
      iLength - how much to shrink
      Returns:
      the stringbuffer
    • grow

      private static final ByteList grow(ByteList i2Grow, byte[] iPads, int iLength)
      grows a stringbuffer. uses the Strings to pad the buffer for a certain length
      Parameters:
      i2Grow - the buffer to grow
      iPads - the string used as padding
      iLength - how much padding is needed
      Returns:
      the padded buffer
    • pack

      public static RubyString pack(Ruby runtime, RubyArray list, ByteList formatString)
      Same as pack but defaults tainting of output to false.
    • pack

      @Deprecated public static RubyString pack(ThreadContext context, Ruby runtime, RubyArray list, RubyString formatString)
      Deprecated.
    • decode

      @Deprecated public static void decode(ThreadContext context, Ruby runtime, ByteBuffer encode, int occurrences, RubyArray result, Block block, Pack.Converter converter)
      Deprecated.
    • pack

      public static RubyString pack(ThreadContext context, RubyArray list, RubyString formatString, RubyString buffer)
    • packCommon

      private static RubyString packCommon(ThreadContext context, RubyArray list, ByteList formatString, boolean tainted, Pack.ConverterExecutor executor, RubyString buffer)
    • pack_w

      private static void pack_w(ThreadContext context, RubyArray list, ByteList result, Pack.PackInts packInts, int occurrences)
    • pack_U

      private static void pack_U(ThreadContext context, RubyArray list, ByteList result, Pack.PackInts packInts, int occurrences)
    • pack_M

      private static boolean pack_M(ThreadContext context, RubyArray list, ByteList result, boolean taintOutput, Pack.PackInts packInts, int occurrences)
    • pack_h

      private static boolean pack_h(ThreadContext context, RubyArray list, ByteList result, boolean taintOutput, Pack.PackInts packInts, int type, int occurrences, boolean isStar)
    • pack_m

      private static boolean pack_m(ThreadContext context, RubyArray list, ByteList result, boolean taintOutput, Pack.PackInts packInts, char type, int occurrences, boolean ignoreStar)
    • pack_at

      private static void pack_at(ByteList result, int occurrences)
    • pack_X

      private static void pack_X(ThreadContext context, ByteList result, int occurrences)
    • pack_h_inner

      private static void pack_h_inner(ByteList result, int type, ByteList lCurElemString, int occurrences)
    • pack_h_H

      private static void pack_h_H(ByteList result, ByteList lCurElemString, int occurrences)
    • pack_h_h

      private static void pack_h_h(ByteList result, ByteList lCurElemString, int occurrences)
    • pack_h_B

      private static void pack_h_B(ByteList result, ByteList lCurElemString, int occurrences)
    • pack_h_b

      private static void pack_h_b(ByteList result, ByteList lCurElemString, int occurrences)
    • pack_h_aAZ

      private static void pack_h_aAZ(ByteList result, int type, ByteList lCurElemString, int occurrences)
    • adjustEncInfo

      private static int adjustEncInfo(int type, int enc_info)
    • decodeIntLittleEndian

      private static int decodeIntLittleEndian(ByteBuffer encode)
      Retrieve an encoded int in little endian starting at index in the string value.
      Parameters:
      encode - string to get int from
      Returns:
      the decoded integer
    • decodeIntBigEndian

      private static int decodeIntBigEndian(ByteBuffer encode)
      Retrieve an encoded int in little endian starting at index in the string value.
      Parameters:
      encode - string to get int from
      Returns:
      the decoded integer
    • decodeIntUnsignedBigEndian

      private static long decodeIntUnsignedBigEndian(ByteBuffer encode)
      Retrieve an encoded int in big endian starting at index in the string value.
      Parameters:
      encode - string to get int from
      Returns:
      the decoded integer
    • decodeIntUnsignedLittleEndian

      private static long decodeIntUnsignedLittleEndian(ByteBuffer encode)
      Retrieve an encoded int in little endian starting at index in the string value.
      Parameters:
      encode - the encoded string
      Returns:
      the decoded integer
    • encodeIntLittleEndian

      private static void encodeIntLittleEndian(ByteList result, int s)
      Encode an int in little endian format into a packed representation.
      Parameters:
      result - to be appended to
      s - the integer to encode
    • encodeIntBigEndian

      private static void encodeIntBigEndian(ByteList result, int s)
      Encode an int in big-endian format into a packed representation.
      Parameters:
      result - to be appended to
      s - the integer to encode
    • decodeLongBigEndian

      private static long decodeLongBigEndian(ByteBuffer encode)
      Decode a long in big-endian format from a packed value
      Parameters:
      encode - string to get int from
      Returns:
      the long value
    • decodeLongLittleEndian

      private static long decodeLongLittleEndian(ByteBuffer encode)
      Decode a long in little-endian format from a packed value
      Parameters:
      encode - string to get int from
      Returns:
      the long value
    • encodeLongLittleEndian

      private static void encodeLongLittleEndian(ByteList result, long l)
      Encode a long in little-endian format into a packed value
      Parameters:
      result - to pack long into
      l - is the long to encode
    • encodeLongBigEndian

      private static void encodeLongBigEndian(ByteList result, long l)
      Encode a long in big-endian format into a packed value
      Parameters:
      result - to pack long into
      l - is the long to encode
    • decodeDoubleLittleEndian

      private static double decodeDoubleLittleEndian(ByteBuffer encode)
      Decode a double from a packed value
      Parameters:
      encode - string to get int from
      Returns:
      the double value
    • decodeDoubleBigEndian

      private static double decodeDoubleBigEndian(ByteBuffer encode)
      Decode a double in big-endian from a packed value
      Parameters:
      encode - string to get int from
      Returns:
      the double value
    • encodeDoubleLittleEndian

      private static void encodeDoubleLittleEndian(ByteList result, double d)
      Encode a double in little endian format into a packed value
      Parameters:
      result - to pack double into
      d - is the double to encode
    • encodeDoubleBigEndian

      private static void encodeDoubleBigEndian(ByteList result, double d)
      Encode a double in big-endian format into a packed value
      Parameters:
      result - to pack double into
      d - is the double to encode
    • decodeFloatBigEndian

      private static float decodeFloatBigEndian(ByteBuffer encode)
      Decode a float in big-endian from a packed value
      Parameters:
      encode - string to get int from
      Returns:
      the double value
    • decodeFloatLittleEndian

      private static float decodeFloatLittleEndian(ByteBuffer encode)
      Decode a float in little-endian from a packed value
      Parameters:
      encode - string to get int from
      Returns:
      the double value
    • encodeFloatLittleEndian

      private static void encodeFloatLittleEndian(ByteList result, float f)
      Encode a float in little endian format into a packed value
      Parameters:
      result - to pack float into
      f - is the float to encode
    • encodeFloatBigEndian

      private static void encodeFloatBigEndian(ByteList result, float f)
      Encode a float in big-endian format into a packed value
      Parameters:
      result - to pack float into
      f - is the float to encode
    • decodeShortUnsignedLittleEndian

      private static int decodeShortUnsignedLittleEndian(ByteBuffer encode)
      Decode a short in little-endian from a packed value
      Parameters:
      encode - string to get int from
      Returns:
      the short value
    • decodeShortUnsignedBigEndian

      private static int decodeShortUnsignedBigEndian(ByteBuffer encode)
      Decode a short in big-endian from a packed value
      Parameters:
      encode - string to get int from
      Returns:
      the short value
    • decodeShortLittleEndian

      private static int decodeShortLittleEndian(ByteBuffer encode)
      Decode a short in little-endian from a packed value
      Parameters:
      encode - string to get int from
      Returns:
      the short value
    • decodeShortBigEndian

      private static short decodeShortBigEndian(ByteBuffer encode)
      Decode a short in big-endian from a packed value
      Parameters:
      encode - string to get int from
      Returns:
      the short value
    • encodeShortLittleEndian

      private static void encodeShortLittleEndian(ByteList result, int s)
      Encode an short in little endian format into a packed representation.
      Parameters:
      result - to be appended to
      s - the short to encode
    • encodeShortBigEndian

      private static void encodeShortBigEndian(ByteList result, int s)
      Encode an shortin big-endian format into a packed representation.
      Parameters:
      result - to be appended to
      s - the short to encode