lib/python/pickle/protocol3.rb in python-pickle-0.1.1 vs lib/python/pickle/protocol3.rb in python-pickle-0.2.0
- old
+ new
@@ -3,19 +3,25 @@
require 'python/pickle/instructions/short_bin_bytes'
module Python
module Pickle
class Protocol3 < Protocol2
- # Opcodes for Pickle protocol version 2.
+ # The `BINBYTES` opcode.
#
- # @see http://formats.kaitai.io/python_pickle/ruby.html
- OPCODES = Protocol2::OPCODES + Set[
- 66, # BINBYTES
- 67 # SHORT_BINBYTES
- ]
+ # @since 0.2.0
+ #
+ # @see https://github.com/python/cpython/blob/v3.10.9/Lib/pickle.py#L175
+ BINBYTES = 66
+ # The `SHORT_BINBYTES` opcode.
#
+ # @since 0.2.0
+ #
+ # @see https://github.com/python/cpython/blob/v3.10.9/Lib/pickle.py#L176
+ SHORT_BINBYTES = 67
+
+ #
# Reads an instruction from the pickle stream.
#
# @return [Instruction]
# The decoded instruction.
#
@@ -25,138 +31,101 @@
def read_instruction
case (opcode = @io.getbyte)
#
# Protocol 0 instructions
#
- when 40 # MARK
- Instructions::MARK
- when 46 # STOP
- Instructions::STOP
- when 48 # POP
- Instructions::POP
- when 49 # POP_MARK
- Instructions::POP_MARK
- when 50 # DUP
- Instructions::DUP
- when 70 # FLOAT
- Instructions::Float.new(read_float)
- when 73 # INT
- Instructions::Int.new(read_int)
- when 76 # LONG
- Instructions::Long.new(read_long)
- when 78 # NONE
- Instructions::NONE
- when 82 # REDUCE
- Instructions::REDUCE
- when 83 # STRING
- Instructions::String.new(read_string)
- when 86 # UNICODE
- Instructions::String.new(read_unicode_string)
- when 97 # APPEND
- Instructions::APPEND
- when 98 # BUILD
- Instructions::BUILD
- when 99 # GLOBAL
- Instructions::Global.new(read_nl_string,read_nl_string)
- when 100 # DICT
- Instructions::DICT
- when 103 # GET
- Instructions::Get.new(read_int)
- when 108 # LIST
- Instructions::LIST
- when 112 # PUT
- Instructions::Put.new(read_int)
- when 115 # SETITEM
- Instructions::SETITEM
- when 116 # TUPLE
- Instructions::TUPLE
+ when MARK then Instructions::MARK
+ when STOP then Instructions::STOP
+ when POP then Instructions::POP
+ when POP_MARK then Instructions::POP_MARK
+ when DUP then Instructions::DUP
+ when FLOAT then read_float_instruction
+ when INT then read_int_instruction
+ when LONG then read_long_instruction
+ when NONE then Instructions::NONE
+ when REDUCE then Instructions::REDUCE
+ when STRING then read_string_instruction
+ when UNICODE then read_unicode_instruction
+ when APPEND then Instructions::APPEND
+ when BUILD then Instructions::BUILD
+ when GLOBAL then read_global_instruction
+ when DICT then Instructions::DICT
+ when GET then read_get_instruction
+ when LIST then Instructions::LIST
+ when PUT then read_put_instruction
+ when SETITEM then Instructions::SETITEM
+ when TUPLE then Instructions::TUPLE
+ when INST then read_inst_instruction
+ when OBJ then Instructions::OBJ
+ when PERSID then read_persid_instruction
+ when BINPERSID then Instructions::BINPERSID
#
# Protocol 1 instructions
#
- when 41 # EMPTY_TUPLE
- Instructions::EMPTY_TUPLE
- when 71 # BINFLOAT
- Instructions::BinFloat.new(read_float64_be)
- when 75 # BININT1
- Instructions::BinInt1.new(read_uint8)
- when 84 # BINSTRING
- length = read_uint32_le
- string = @io.read(length)
-
- Instructions::BinString.new(length,string)
- when 85 # SHORT_BINSTRING
- length = read_uint8
- string = @io.read(length)
-
- Instructions::ShortBinString.new(length,string)
- when 88 # BINUNICODE
- length = read_uint32_le
- string = @io.read(length).force_encoding(Encoding::UTF_8)
-
- Instructions::BinUnicode.new(length,string)
- when 93 # EMPTY_LIST
- Instructions::EMPTY_LIST
- when 101 # APPENDS
- Instructions::APPENDS
- when 104 # BINGET
- Instructions::BinGet.new(read_uint8)
- when 106 # LONG_BINGET
- Instructions::LongBinGet.new(read_uint32_le)
- when 113 # BINPUT
- Instructions::BinPut.new(read_uint8)
- when 117 # SETITEMS
- Instructions::SETITEMS
- when 125 # EMPTY_DICT
- Instructions::EMPTY_DICT
+ when EMPTY_TUPLE then Instructions::EMPTY_TUPLE
+ when BINFLOAT then read_binfloat_instruction
+ when BININT1 then read_binint1_instruction
+ when BINSTRING then read_binstring_instruction
+ when SHORT_BINSTRING then read_short_binstring_instruction
+ when BINUNICODE then read_binunicode_instruction
+ when EMPTY_LIST then Instructions::EMPTY_LIST
+ when APPENDS then Instructions::APPENDS
+ when BINGET then read_binget_instruction
+ when LONG_BINGET then read_long_binget_instruction
+ when BINPUT then read_binput_instruction
+ when SETITEMS then Instructions::SETITEMS
+ when EMPTY_DICT then Instructions::EMPTY_DICT
#
# Protocol 2 instructions
#
- when 128 # PROT
- Instructions::Proto.new(read_uint8)
- when 129 # NEWOBJ
- Instructions::NEWOBJ
- when 130 # EXT1
- Instructions::Ext1.new(read_uint8)
- when 131 # EXT2
- Instructions::Ext2.new(read_uint16_le)
- when 132 # EXT4
- Instructions::Ext4.new(read_uint32_le)
- when 133 # TUPLE1
- Instructions::TUPLE1
- when 134 # TUPLE2
- Instructions::TUPLE2
- when 135 # TUPLE3
- Instructions::TUPLE3
- when 136 # NEWTRUE
- Instructions::NEWTRUE
- when 137 # NEWFALSE
- Instructions::NEWFALSE
- when 138 # LONG1
- length = read_uint8
- long = read_int_le(length)
-
- Instructions::Long1.new(length,long)
- when 139 # LONG4
- length = read_uint32_le
- long = read_int_le(length)
-
- Instructions::Long4.new(length,long)
+ when PROTO then read_proto_instruction
+ when NEWOBJ then Instructions::NEWOBJ
+ when EXT1 then read_ext1_instruction
+ when EXT2 then read_ext2_instruction
+ when EXT4 then read_ext4_instruction
+ when TUPLE1 then Instructions::TUPLE1
+ when TUPLE2 then Instructions::TUPLE2
+ when TUPLE3 then Instructions::TUPLE3
+ when NEWTRUE then Instructions::NEWTRUE
+ when NEWFALSE then Instructions::NEWFALSE
+ when LONG1 then read_long1_instruction
+ when LONG4 then read_long4_instruction
#
# Protocol 3 instructions
#
- when 66 # BINBYTES
- length = read_uint32_le
- bytes = @io.read(length)
-
- Instructions::BinBytes.new(length,bytes)
- when 67 # SHORT_BINBYTES
- length = read_uint8
- bytes = @io.read(length)
-
- Instructions::ShortBinBytes.new(length,bytes)
+ when BINBYTES then read_binbytes_instruction
+ when SHORT_BINBYTES then read_short_binbytes_instruction
else
raise(InvalidFormat,"invalid opcode (#{opcode.inspect}) for protocol 3")
end
+ end
+
+ #
+ # Reads a `BINBYTES` instruction.
+ #
+ # @return [Instructions::BinBytes]
+ #
+ # @since 0.2.0
+ #
+ def read_binbytes_instruction
+ length = read_uint32_le
+ bytes = @io.read(length)
+
+ Instructions::BinBytes.new(length,bytes)
+ end
+
+ #
+ # Reads a `SHORT_BINBYTES` instruction.
+ #
+ # @return [Instructions::ShortBinBytes]
+ #
+ # @since 0.2.0
+ #
+ def read_short_binbytes_instruction
+ length = read_uint8
+ bytes = @io.read(length)
+
+ Instructions::ShortBinBytes.new(length,bytes)
end
end
end
end