Package org.jruby.util
Class Pack
java.lang.Object
org.jruby.util.Pack
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic classprivate static classprivate static classIntroduced to allow outlining cases in #packCommon that update both of these values.private static class -
Field Summary
FieldsModifier and TypeFieldDescriptionprivate static final org.jcodings.specific.ASCIIEncodingprivate static final byte[]static final int[]private static final charprivate static final Pack.Converter[]private static final Stringprivate static final intprivate static final charprivate static final Stringprivate static final StringNative pack type.private static final Stringprivate static final Stringstatic final byte[]private static final byte[]private static final byte[]private static final Stringprivate static final intprivate static final intUnpack modesprivate static final intprivate static final Stringprivate static final org.jcodings.specific.USASCIIEncodingprivate static final org.jcodings.specific.UTF8Encodingprivate static final long[]private static final byte[] -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionprivate static intadjustEncInfo(int type, int enc_info) private static voidappendOrYield(ThreadContext context, Block block, RubyArray result, IRubyObject item, int mode) private static IRubyObjectappendOrYield(ThreadContext context, Block block, RubyArray result, ByteList item, int mode, boolean taint) private static intcheckLimit(Ruby runtime, ByteBuffer encode, int limit) static voiddecode(ThreadContext context, Ruby runtime, ByteBuffer encode, int occurrences, RubyArray result, Block block, Pack.Converter converter) Deprecated.static IRubyObjectdecode(ThreadContext context, Ruby runtime, ByteBuffer encode, int occurrences, RubyArray result, Block block, Pack.Converter converter, int mode) private static doubledecodeDoubleBigEndian(ByteBuffer encode) Decode a double in big-endian from a packed valueprivate static doubledecodeDoubleLittleEndian(ByteBuffer encode) Decode a double from a packed valueprivate static floatdecodeFloatBigEndian(ByteBuffer encode) Decode a float in big-endian from a packed valueprivate static floatdecodeFloatLittleEndian(ByteBuffer encode) Decode a float in little-endian from a packed valueprivate static intdecodeIntBigEndian(ByteBuffer encode) Retrieve an encoded int in little endian starting at index in the string value.private static intdecodeIntLittleEndian(ByteBuffer encode) Retrieve an encoded int in little endian starting at index in the string value.private static longdecodeIntUnsignedBigEndian(ByteBuffer encode) Retrieve an encoded int in big endian starting at index in the string value.private static longRetrieve an encoded int in little endian starting at index in the string value.private static longdecodeLongBigEndian(ByteBuffer encode) Decode a long in big-endian format from a packed valueprivate static longdecodeLongLittleEndian(ByteBuffer encode) Decode a long in little-endian format from a packed valueprivate static shortdecodeShortBigEndian(ByteBuffer encode) Decode a short in big-endian from a packed valueprivate static intdecodeShortLittleEndian(ByteBuffer encode) Decode a short in little-endian from a packed valueprivate static intDecode a short in big-endian from a packed valueprivate static intDecode a short in little-endian from a packed valuestatic intencode(Ruby runtime, int occurrences, ByteList result, RubyArray list, int index, Pack.ConverterExecutor converter) private static voidencodeDoubleBigEndian(ByteList result, double d) Encode a double in big-endian format into a packed valueprivate static voidencodeDoubleLittleEndian(ByteList result, double d) Encode a double in little endian format into a packed valueprivate static voidencodeFloatBigEndian(ByteList result, float f) Encode a float in big-endian format into a packed valueprivate static voidencodeFloatLittleEndian(ByteList result, float f) Encode a float in little endian format into a packed valueprivate static voidencodeIntBigEndian(ByteList result, int s) Encode an int in big-endian format into a packed representation.private static voidencodeIntLittleEndian(ByteList result, int s) Encode an int in little endian format into a packed representation.private static voidencodeLongBigEndian(ByteList result, long l) Encode a long in big-endian format into a packed valueprivate static voidencodeLongLittleEndian(ByteList result, long l) Encode a long in little-endian format into a packed valueprivate static ByteListencodes(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.private static voidencodeShortBigEndian(ByteList result, int s) Encode an shortin big-endian format into a packed representation.private static voidencodeShortLittleEndian(ByteList result, int s) Encode an short in little endian format into a packed representation.static voidencodeUM(Ruby runtime, ByteList lCurElemString, int occurrences, boolean ignoreStar, char type, ByteList result) private static Pack.ConverterExecutorexecutor()private static final ByteListgrows a stringbuffer.private static longnum2quad(IRubyObject arg) private static doubleobj2dbl(Ruby runtime, IRubyObject o) private static floatobj2flt(Ruby runtime, IRubyObject o) static RubyStringSame as pack but defaults tainting of output to false.static RubyStringpack(ThreadContext context, RubyArray list, RubyString formatString, RubyString buffer) static RubyStringpack(ThreadContext context, Ruby runtime, RubyArray list, RubyString formatString) Deprecated.private static voidprivate static booleanpack_h(ThreadContext context, RubyArray list, ByteList result, boolean taintOutput, Pack.PackInts packInts, int type, int occurrences, boolean isStar) private static voidpack_h_aAZ(ByteList result, int type, ByteList lCurElemString, int occurrences) private static voidprivate static voidprivate static voidprivate static voidprivate static voidpack_h_inner(ByteList result, int type, ByteList lCurElemString, int occurrences) private static booleanpack_m(ThreadContext context, RubyArray list, ByteList result, boolean taintOutput, Pack.PackInts packInts, char type, int occurrences, boolean ignoreStar) private static booleanpack_M(ThreadContext context, RubyArray list, ByteList result, boolean taintOutput, Pack.PackInts packInts, int occurrences) private static voidpack_U(ThreadContext context, RubyArray list, ByteList result, Pack.PackInts packInts, int occurrences) private static voidpack_w(ThreadContext context, RubyArray list, ByteList result, Pack.PackInts packInts, int occurrences) private static voidpack_X(ThreadContext context, ByteList result, int occurrences) private static RubyStringpackCommon(ThreadContext context, RubyArray list, ByteList formatString, boolean tainted, Pack.ConverterExecutor executor, RubyString buffer) static ByteListstatic intsafeGet(ByteBuffer encode) private static intsafeGetIgnoreNull(ByteBuffer encode) private static final ByteListshrinks a stringbuffer.static RubyArraystatic RubyArrayunpack(ThreadContext context, RubyString encoded, ByteList formatString) private static IRubyObjectunpack_a(ThreadContext context, Block block, RubyArray result, boolean tainted, ByteBuffer encode, int occurrences, int mode) private static IRubyObjectunpack_A(ThreadContext context, Block block, RubyArray result, boolean tainted, ByteBuffer encode, int occurrences, int mode) private static voidunpack_at(Ruby runtime, ByteList encodedString, ByteBuffer encode, int occurrences) private static IRubyObjectunpack_b(ThreadContext context, Block block, RubyArray result, boolean tainted, ByteBuffer encode, int occurrences, int mode) private static IRubyObjectunpack_B(ThreadContext context, Block block, RubyArray result, boolean tainted, ByteBuffer encode, int occurrences, int mode) private static IRubyObjectunpack_h(ThreadContext context, Block block, RubyArray result, boolean tainted, ByteBuffer encode, int occurrences, int mode) private static IRubyObjectunpack_H(ThreadContext context, Block block, RubyArray result, boolean tainted, ByteBuffer encode, int occurrences, int mode) private static IRubyObjectunpack_m(ThreadContext context, Block block, Ruby runtime, RubyArray result, boolean tainted, ByteBuffer encode, int occurrences, int mode) private static IRubyObjectunpack_M(ThreadContext context, Block block, RubyArray result, boolean tainted, ByteBuffer encode, int mode) private static intunpack_m_nonzeroOccurrences(ByteBuffer encode, byte[] lElem, int a, int b, int c, int index) private static intunpack_m_zeroOccurrences(Ruby runtime, ByteBuffer encode, byte[] lElem, int a, int b, int c, int index, int s) private static IRubyObjectunpack_u(ThreadContext context, Block block, RubyArray result, boolean tainted, ByteBuffer encode, int mode) private static IRubyObjectunpack_U(ThreadContext context, Block block, Ruby runtime, RubyArray result, ByteBuffer encode, int occurrences, int mode) private static IRubyObjectunpack_w(ThreadContext context, Block block, Ruby runtime, RubyArray result, ByteBuffer encode, int occurrences, int mode) private static voidunpack_x(Ruby runtime, ByteBuffer encode, int occurrences) private static voidunpack_X(Ruby runtime, ByteBuffer encode, int occurrences) private static IRubyObjectunpack_Z(ThreadContext context, Block block, RubyArray result, boolean tainted, ByteBuffer encode, int occurrences, int mode) static IRubyObjectunpack1WithBlock(ThreadContext context, RubyString encoded, ByteList formatString, Block block) private static RubyStringunpackBase46Strict(Ruby runtime, ByteList input) static intunpackInt_i(ByteBuffer enc) private static IRubyObjectunpackInternal(ThreadContext context, RubyString encoded, ByteList formatString, int mode, Block block) static RubyArrayunpackWithBlock(ThreadContext context, Ruby runtime, ByteList encodedString, ByteList formatString, Block block) Deprecated.static RubyArrayunpackWithBlock(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.private static intutf8Decode(ByteBuffer buffer) utf8_to_uvstatic intutf8Decode(Ruby runtime, byte[] to, int p, int code) rb_uv_to_utf8
-
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
Native pack type.- See Also:
-
MAPPED_CODES
- See Also:
-
BE
private static final char BE- See Also:
-
LE
private static final char LE- See Also:
-
ENDIANESS_CODES
-
UNPACK_IGNORE_NULL_CODES
- See Also:
-
PACK_IGNORE_NULL_CODES
- See Also:
-
PACK_IGNORE_NULL_CODES_WITH_MODIFIERS
- See Also:
-
UNPACK_ARRAY
private static final int UNPACK_ARRAYUnpack modes- See Also:
-
UNPACK_BLOCK
private static final int UNPACK_BLOCK- See Also:
-
UNPACK_1
private static final int UNPACK_1- See Also:
-
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
-
utf8_limits
private static final long[] utf8_limits
-
-
Constructor Details
-
Pack
public Pack()
-
-
Method Details
-
num2quad
-
obj2flt
-
obj2dbl
-
unpackInt_i
-
packInt_i
-
encodeUM
-
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 resultcharsToEncode- The String to encodestartIndex-length- The max number of characters to encodecharCount-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
-
unpack
- 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 directivessSiIlLmay 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
1 May be modified by appending ``_'' to the directive.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. --- 
-
unpackBase46Strict
-
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
-
unpack_X
-
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
-
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
rb_uv_to_utf8 -
utf8Decode
utf8_to_uv -
safeGet
-
safeGetIgnoreNull
-
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
-
shrink
shrinks a stringbuffer. shrinks a stringbuffer by a number of characters.- Parameters:
i2Shrink- the stringbufferiLength- how much to shrink- Returns:
- the stringbuffer
-
grow
grows a stringbuffer. uses the Strings to pad the buffer for a certain length- Parameters:
i2Grow- the buffer to growiPads- the string used as paddingiLength- how much padding is needed- Returns:
- the padded buffer
-
pack
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
-
pack_X
-
pack_h_inner
-
pack_h_H
-
pack_h_h
-
pack_h_B
-
pack_h_b
-
pack_h_aAZ
-
adjustEncInfo
private static int adjustEncInfo(int type, int enc_info) -
decodeIntLittleEndian
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
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
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
Retrieve an encoded int in little endian starting at index in the string value.- Parameters:
encode- the encoded string- Returns:
- the decoded integer
-
encodeIntLittleEndian
Encode an int in little endian format into a packed representation.- Parameters:
result- to be appended tos- the integer to encode
-
encodeIntBigEndian
Encode an int in big-endian format into a packed representation.- Parameters:
result- to be appended tos- the integer to encode
-
decodeLongBigEndian
Decode a long in big-endian format from a packed value- Parameters:
encode- string to get int from- Returns:
- the long value
-
decodeLongLittleEndian
Decode a long in little-endian format from a packed value- Parameters:
encode- string to get int from- Returns:
- the long value
-
encodeLongLittleEndian
Encode a long in little-endian format into a packed value- Parameters:
result- to pack long intol- is the long to encode
-
encodeLongBigEndian
Encode a long in big-endian format into a packed value- Parameters:
result- to pack long intol- is the long to encode
-
decodeDoubleLittleEndian
Decode a double from a packed value- Parameters:
encode- string to get int from- Returns:
- the double value
-
decodeDoubleBigEndian
Decode a double in big-endian from a packed value- Parameters:
encode- string to get int from- Returns:
- the double value
-
encodeDoubleLittleEndian
Encode a double in little endian format into a packed value- Parameters:
result- to pack double intod- is the double to encode
-
encodeDoubleBigEndian
Encode a double in big-endian format into a packed value- Parameters:
result- to pack double intod- is the double to encode
-
decodeFloatBigEndian
Decode a float in big-endian from a packed value- Parameters:
encode- string to get int from- Returns:
- the double value
-
decodeFloatLittleEndian
Decode a float in little-endian from a packed value- Parameters:
encode- string to get int from- Returns:
- the double value
-
encodeFloatLittleEndian
Encode a float in little endian format into a packed value- Parameters:
result- to pack float intof- is the float to encode
-
encodeFloatBigEndian
Encode a float in big-endian format into a packed value- Parameters:
result- to pack float intof- is the float to encode
-
decodeShortUnsignedLittleEndian
Decode a short in little-endian from a packed value- Parameters:
encode- string to get int from- Returns:
- the short value
-
decodeShortUnsignedBigEndian
Decode a short in big-endian from a packed value- Parameters:
encode- string to get int from- Returns:
- the short value
-
decodeShortLittleEndian
Decode a short in little-endian from a packed value- Parameters:
encode- string to get int from- Returns:
- the short value
-
decodeShortBigEndian
Decode a short in big-endian from a packed value- Parameters:
encode- string to get int from- Returns:
- the short value
-
encodeShortLittleEndian
Encode an short in little endian format into a packed representation.- Parameters:
result- to be appended tos- the short to encode
-
encodeShortBigEndian
Encode an shortin big-endian format into a packed representation.- Parameters:
result- to be appended tos- the short to encode
-