-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathunpython.lisp
More file actions
142 lines (119 loc) · 4.55 KB
/
Copy pathunpython.lisp
File metadata and controls
142 lines (119 loc) · 4.55 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
;;;; unpython.lisp
(in-package #:unpython)
(defvar +highest-protocol+ 4)
(defvar *protocol* 0)
(defvar *meta-stack* '())
(defvar *stack* '())
(defvar *memo* (make-hash-table))
(defun reset ()
(setf *protocol* 0)
(setf *stack* '())
(setf *meta-stack* '())
(setf *protocol* 0)
(clrhash *memo*))
(defun load-pickle (file)
(reset)
(with-open-file (pickle file :element-type '(unsigned-byte 8))
(handler-case
(loop for op-code = (read-byte pickle nil nil)
while op-code
do (perform-op op-code pickle))
(stop (stop-value)
(clrhash *memo*)
(return-from load-pickle (return-val stop-value)))))
(error 'unpickling-error :code 'None
:error "Reached end of file before reading +STOP+ op code"))
(defun bytes->long (bytes &key from-end (size 8))
(flet ((big (int byte)
(logior (ash int size) byte))
(little (byte int)
(logior (ash int size) byte)))
(let ((f (if (null from-end)
#'big
#'little)))
(reduce f bytes :from-end from-end))))
(defun byte-sequence->utf8-codes (sequence)
(let ((len (1- (length sequence))))
(macrolet ((gen (byte mask num)
(let ((byte-array (gensym)))
`(let ((,byte-array (list
(logand ,byte ,mask)
,@(loop repeat num
collect `(logand
(aref sequence (incf i))
#x7f)))))
(bytes->long ,byte-array :size 6)))))
(loop for i to len
collect (let ((byte (aref sequence i)))
(cond
((<= byte #x7f)
byte)
((<= #xc0 byte #xdf)
(gen byte #x3f 1))
((<= #xe0 byte #xef)
(gen byte #x0f 2))
((<= #xf0 byte #xf7)
(gen byte #x07 3))
(t
(error (format nil "Invalide UTF-8: ~a~%" sequence)))))))))
(defun pop-mark ()
(let ((items *stack*))
(setf *stack* (pop *meta-stack*))
items))
(on-load +proto+ (stream)
(let ((protocol (read-byte stream)))
(unless (<= 0 protocol +highest-protocol+)
(error 'unpickling-error :code '+proto+
:error (format nil
"Protocol ~A not supported"
protocol)))
(setf *protocol* protocol)))
(on-load +empty-list+ ()
(push '() *stack*))
(on-load +empty-dict+ ()
(push (make-hash-table :test 'equal) *stack*))
(on-load +setitem+ ()
(let ((values (pop *stack*))
(key (pop *stack*))
(dict (first *stack*)))
(setf (gethash key dict) values)))
(on-load +setitems+ ()
;; Have to do the pop first so we actually have a dict
(let ((items (pop-mark))
(dict (first *stack*)))
(loop for (value key) on items by #'cddr
do (setf (gethash key dict) value))))
(on-load +binput+ (stream)
(let ((i (read-byte stream)))
(when (< i 0)
(error 'unpickling-error :code '+binput+
:error (format nil "Read byte less than 0 at pos ~A"
(- (file-position stream) 1))))
(setf (gethash i *memo*) (first *stack*))))
(on-load +long-binput+ (stream)
(let ((i (make-array 4 :element-type '(unsigned-byte 8))))
(read-sequence i stream)
(setf (gethash (bytes->long i :from-end t) *memo*) (first *stack*))))
(on-load +binget+ (stream)
(push (gethash (read-byte stream) *memo*) *stack*))
(on-load +long-binget+ (stream)
(let ((i (make-array 4 :element-type '(unsigned-byte 8))))
(read-sequence i stream)
(push (gethash (bytes->long i :from-end t) *memo*) *stack*)))
(on-load +mark+ ()
(push *stack* *meta-stack*)
(setf *stack* '()))
(on-load +binunicode+ (stream)
(let ((len (make-array 4 :element-type '(unsigned-byte 8))))
(read-sequence len stream)
(let ((data (make-array (bytes->long len :from-end t))))
(read-sequence data stream)
(push (map 'string 'code-char (byte-sequence->utf8-codes data))
*stack*))))
(on-load +append+ ()
(push (pop *stack*) (first *stack*)))
(on-load +appends+ ()
(loop for i in (pop-mark)
do (push i (first *stack*))))
(on-load +stop+ ()
(signal 'stop :return-val (pop *stack*)))