Class: Unpickle::PickleMachine

Inherits:
Object
  • Object
show all
Defined in:
lib/unpickle.rb

Overview

:nodoc: all

Instance Method Summary collapse

Constructor Details

#initialize(input) ⇒ PickleMachine

Returns a new instance of PickleMachine.



19
20
21
22
23
24
# File 'lib/unpickle.rb', line 19

def initialize(input)
    @stack = []
    @memo = {}
    @input = input
    @idx = 0
end

Instance Method Details

#at_end?Boolean

Returns:

  • (Boolean)


26
27
28
# File 'lib/unpickle.rb', line 26

def at_end?
    @idx >= @input.length
end

#markerObject

Raises:



56
57
58
59
60
61
62
63
64
65
# File 'lib/unpickle.rb', line 56

def marker
    idx = @stack.length-1
    while idx >= 0
        if @stack[idx].is_a?(Mark)
            return idx
        end
        idx -= 1
    end
    raise UnpickleException, "Couldn't find Mark"
end

#next_charObject



30
31
32
33
34
# File 'lib/unpickle.rb', line 30

def next_char
    rv = @input[@idx..@idx]
    @idx += 1
    return rv
end

#peek_charObject



36
37
38
# File 'lib/unpickle.rb', line 36

def peek_char
    @input[@idx..@idx]
end

#read_intObject



40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/unpickle.rb', line 40

def read_int
    strout = ""
    while peek_char != "\n"
        strout += next_char
    end
    next_char
    case strout
    when '00'
        return false
    when '01'
        return true
    else
        return strout.to_i
    end
end

#read_stringObject

read from the input stream to read the python string.

returns the value.



70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
# File 'lib/unpickle.rb', line 70

def read_string
    strout = ''
    if next_char != '\''
        raise UnpickleException, "Couldn't find leading quote for string"
    end
    while not at_end?
        c = next_char
        case c
        when "\\"
            opt = next_char
            case opt
            when 'x'
                num = ''
                while peek_char.match(/[\dA-Fa-f]/)
                    num += next_char
                    if num.length >= 2
                        break
                    end
                end
                unless (1..2).include?(num.length)
                    raise UnpickleException, "Bad \\x sequence in string"
                end
                strout += num.to_i(16).chr
            when '0'
                num = ''
                while peek_char.match(/[0-7]/)
                    num += next_char
                    if num.length >= 3
                        break
                    end
                end
                unless (1..3).include?(num.length)
                    raise UnpickleException, "Bad \\0 sequence in string"
                end
                strout += num.to_i(8).chr
            when 'n'
                strout += "\n"
            when "\\"
                strout += "\\"
            when 't'
                strout += "\t"
            when "'"
                strout += "'"
            else
                raise UnpickleException, "Unexpected \\ escape: \\#{opt}"
            end
        when "'"
            # valid end of string...
            break
        else
            strout += c
        end
    end
    if next_char != "\n"
        raise UnpickleException, "Expected \\n after string"
    end
    return strout
end

#unpickleObject

Raises:



129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
# File 'lib/unpickle.rb', line 129

def unpickle
    while not at_end?
        op = next_char
        case op
        when '(' # MARK
            @stack.push(Mark.new)
        when 'd' # DICT
            newdict = {}
            while true
                if @stack.empty?
                    raise UnpickleException, "Stack empty during 'd'"
                end
                v = @stack.pop
                if v.is_a?(Mark)
                    break
                end
                if @stack.empty?
                    raise UnpickleException, "Stack empty during 'd'"
                end
                k = stack.pop
                if k.is_a?(Mark)
                    raise UnpickleException, "Odd number of elements during 'd' stack walk"
                end
                newdict[k] = v
            end
            @stack.push(newdict)
        when 'S' # STRING
            newstr = read_string
            @stack.push(newstr)
        when 'p' # PUT (string)
            index = read_int
            @memo[index] = @stack[-1]
        when 'g' # GET (string)
            index = read_int
            @stack.push(@memo[index])
        when 'I' # INT
            intarg = read_int
            @stack.push(intarg)
        when 's' # SETITEM
            value = @stack.pop
            key = @stack.pop
            dict = @stack[-1]
            dict[key] = value
        when 't' # TUPLE
            midx = marker
            tuple = @stack[midx+1..-1]
            @stack = @stack[0...midx]
            tuple.freeze
            @stack.push(tuple)
        when 'l' # LIST
            midx = marker
            list = @stack[midx+1..-1]
            @stack = @stack[0...midx]
            @stack.push(list)
        when 'N' # NONE
            @stack.push(nil)
        when 'a' # APPEND
            e = @stack.pop
            @stack[-1].push(e)
        when '0' # POP
            @stack.pop
        when '2' # DUP
            @stack.push(@stack[-1])
        when '.' # STOP
            return @stack.pop
        else
            raise UnpickleException, "Unsupported unpickle operation '#{op}'"
        end
    end
    raise UnpickleException, "Hit end of input stream"
end