public
Last active

parsing java class files with defbinstruct

  • Download Gist
gistfile1.lisp
Common Lisp
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 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 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429
(ql:quickload 'babel)
 
(defbinstruct class-file
(magic 4) ;; values are bytes when types aren't specified
(minor-version 2)
(major-version 2)
(constant-pool (:struct constant-pool))
(access-flags 2)
(this-class 2)
(super-class 2)
(:temp (interfaces-count 2))
(interfaces (:list 2 interfaces-count))
;; temp values are used by some other slots, and won't appear
;; in final structure
(:temp (fields-count 2))
(fields (:list (:struct field) fields-count))
(:temp (jmethods-count 2))
(methods (:list (:struct jmethod) jmethods-count))
(:temp (attributes-count 2))
(attributes (:list (:struct attribute) attributes-count)))
 
;; constants ----------------------------------------------------------
(defbinstruct class-ref
(value 2))
 
(defbinstruct field-ref
(class-index 2)
(name-and-type-index 2))
 
(defbinstruct method-ref
(class-index 2)
(name-and-type-index 2))
 
(defbinstruct interface-method-ref
(class-index 2)
(name-and-type-index 2))
 
(defbinstruct descriptor
(name-index 2)
(descriptor-index 2))
 
(defbinstruct jdouble
(high-bytes 4)
(low-bytes 4))
 
(defbinstruct jlong
(high-bytes 4)
(low-bytes 4))
 
(defbinstruct utf-8
(:temp (length 2))
(value (:vector 1 length)))
 
(defbinstruct string-ref
(value 2))
 
(defbinstruct jinteger
(value 4))
 
(defbinstruct jfloat
(value 4))
 
;; --------------------------------------------------------------------
 
(defbinstruct constant-pool
:custom
(constants)
(let* ((constant-pool-count (1- (read-bytes 2 stream)))
(constants (make-array constant-pool-count)))
(loop for i from 0 to (1- constant-pool-count) do
(let ((tag (read-bytes 1 stream)))
(if (or (= tag 5) (= tag 6))
(let ((constant (if (= tag 5) (read-jlong stream) (read-jdouble stream))))
(setf (elt constants i) constant
(elt constants (1+ i)) constant)
(incf i))
(setf (elt constants i)
(funcall
(case tag
(1 #'read-utf-8)
(8 #'read-string-ref)
(3 #'read-jinteger)
(4 #'read-jfloat)
(7 #'read-class-ref)
(9 #'read-field-ref)
(10 #'read-method-ref)
(11 #'read-interface-method-ref)
(12 #'read-descriptor))
stream)))))
(make-constant-pool :constants constants)))
(defbinstruct attribute
(attribute-name-index 2)
(:temp (attribute-length 4))
(info (:list 1 attribute-length)))
 
(defbinstruct field
(access-flags 2)
(name-index 2)
(descriptor-index 2)
(:temp (attributes-count 2))
(attributes (:list (:struct attribute) attributes-count)))
 
(defbinstruct jmethod
(access-flags 2)
(name-index 2)
(descriptor-index 2)
(:temp (attributes-count 2))
(attributes (:list (:struct attribute) attributes-count)))
 
 
;; example --------------------------------------------------
 
 
CL-USER> (with-open-file (stream #P"/home/sinan/eclipseworkspace/OBSS/bin/hashmap/ExtendedHashMap.class"
:direction :input
:element-type '(unsigned-byte 8))
(read-class-file stream))
#S(CLASS-FILE
:MAGIC 3405691582
:MINOR-VERSION 0
:MAJOR-VERSION 50
:CONSTANT-POOL #S(CONSTANT-POOL
:CONSTANTS #(#S(CLASS-REF :VALUE 2)
#S(UTF-8
:VALUE #(104 97 115 104 109 97 112 47 69
120 116 101 110 100 101 100 72 97
115 104 77 97 112))
#S(CLASS-REF :VALUE 4)
#S(UTF-8
:VALUE #(106 97 118 97 47 108 97 110 103
47 79 98 106 101 99 116))
#S(UTF-8 :VALUE #(109 97 112))
#S(UTF-8
:VALUE #(76 106 97 118 97 47 117 116 105
108 47 72 97 115 104 77 97 112
59))
#S(UTF-8
:VALUE #(83 105 103 110 97 116 117 114
101))
#S(UTF-8
:VALUE #(76 106 97 118 97 47 117 116 105
108 47 72 97 115 104 77 97 112 60
76 106 97 118 97 47 108 97 110
103 47 83 116 114 105 110 103 59
76 106 97 118 97 47 117 116 105
108 47 72 97 115 104 77 97 112 60
76 106 97 118 97 47 108 97 110
103 47 83 116 114 105 110 103 59
76 106 97 118 97 47 108 97 110
103 47 79 98 106 101 99 116 59 62
59 62 59))
#S(UTF-8 :VALUE #(108 111 108 122))
#S(UTF-8 :VALUE #(73))
#S(UTF-8 :VALUE #(60 105 110 105 116 62))
#S(UTF-8 :VALUE #(40 41 86))
#S(UTF-8 :VALUE #(67 111 100 101))
#S(METHOD-REF
:CLASS-INDEX 3
:NAME-AND-TYPE-INDEX 15)
#S(DESCRIPTOR
:NAME-INDEX 11
:DESCRIPTOR-INDEX 12)
#S(CLASS-REF :VALUE 17)
#S(UTF-8
:VALUE #(106 97 118 97 47 117 116 105 108
47 72 97 115 104 77 97 112))
#S(METHOD-REF
:CLASS-INDEX 16
:NAME-AND-TYPE-INDEX 15)
#S(FIELD-REF
:CLASS-INDEX 1
:NAME-AND-TYPE-INDEX 20)
#S(DESCRIPTOR
:NAME-INDEX 5
:DESCRIPTOR-INDEX 6)
#S(UTF-8
:VALUE #(76 105 110 101 78 117 109 98 101
114 84 97 98 108 101))
#S(UTF-8
:VALUE #(76 111 99 97 108 86 97 114 105 97
98 108 101 84 97 98 108 101))
#S(UTF-8 :VALUE #(116 104 105 115))
#S(UTF-8
:VALUE #(76 104 97 115 104 109 97 112 47
69 120 116 101 110 100 101 100 72
97 115 104 77 97 112 59))
#S(UTF-8 :VALUE #(112 117 116))
#S(UTF-8
:VALUE #(40 76 106 97 118 97 47 108 97 110
103 47 83 116 114 105 110 103 59
76 106 97 118 97 47 108 97 110
103 47 83 116 114 105 110 103 59
76 106 97 118 97 47 108 97 110
103 47 79 98 106 101 99 116 59 41
86))
#S(METHOD-REF
:CLASS-INDEX 16
:NAME-AND-TYPE-INDEX 28)
#S(DESCRIPTOR
:NAME-INDEX 29
:DESCRIPTOR-INDEX 30)
#S(UTF-8 :VALUE #(103 101 116))
#S(UTF-8
:VALUE #(40 76 106 97 118 97 47 108 97 110
103 47 79 98 106 101 99 116 59 41
76 106 97 118 97 47 108 97 110
103 47 79 98 106 101 99 116 59))
#S(METHOD-REF
:CLASS-INDEX 16
:NAME-AND-TYPE-INDEX 32)
#S(DESCRIPTOR
:NAME-INDEX 25
:DESCRIPTOR-INDEX 33)
#S(UTF-8
:VALUE #(40 76 106 97 118 97 47 108 97 110
103 47 79 98 106 101 99 116 59 76
106 97 118 97 47 108 97 110 103
47 79 98 106 101 99 116 59 41 76
106 97 118 97 47 108 97 110 103
47 79 98 106 101 99 116 59))
#S(UTF-8 :VALUE #(107 101 121 49))
#S(UTF-8
:VALUE #(76 106 97 118 97 47 108 97 110
103 47 83 116 114 105 110 103
59))
#S(UTF-8 :VALUE #(107 101 121 50))
#S(UTF-8 :VALUE #(118 97 108))
#S(UTF-8
:VALUE #(76 106 97 118 97 47 108 97 110
103 47 79 98 106 101 99 116 59))
#S(UTF-8 :VALUE #(108 49))
#S(UTF-8
:VALUE #(76 111 99 97 108 86 97 114 105 97
98 108 101 84 121 112 101 84 97
98 108 101))
#S(UTF-8
:VALUE #(76 106 97 118 97 47 117 116 105
108 47 72 97 115 104 77 97 112 60
76 106 97 118 97 47 108 97 110
103 47 83 116 114 105 110 103 59
76 106 97 118 97 47 108 97 110
103 47 79 98 106 101 99 116 59 62
59))
#S(UTF-8
:VALUE #(83 116 97 99 107 77 97 112 84 97
98 108 101))
#S(UTF-8
:VALUE #(40 76 106 97 118 97 47 108 97 110
103 47 83 116 114 105 110 103 59
76 106 97 118 97 47 108 97 110
103 47 83 116 114 105 110 103 59
41 76 106 97 118 97 47 108 97 110
103 47 79 98 106 101 99 116 59))
#S(UTF-8 :VALUE #(109 97 105 110))
#S(UTF-8
:VALUE #(40 91 76 106 97 118 97 47 108 97
110 103 47 83 116 114 105 110 103
59 41 86))
#S(METHOD-REF
:CLASS-INDEX 1
:NAME-AND-TYPE-INDEX 15)
#S(STRING-REF :VALUE 48)
#S(UTF-8 :VALUE #(82 111 119 49))
#S(STRING-REF :VALUE 50)
#S(UTF-8 :VALUE #(67 111 108 117 109 110 49))
#S(STRING-REF :VALUE 52)
#S(UTF-8
:VALUE #(84 117 114 107 105 115 104))
#S(METHOD-REF
:CLASS-INDEX 1
:NAME-AND-TYPE-INDEX 54)
#S(DESCRIPTOR
:NAME-INDEX 25
:DESCRIPTOR-INDEX 26)
#S(STRING-REF :VALUE 56)
#S(UTF-8 :VALUE #(67 111 108 117 109 110 50))
#S(STRING-REF :VALUE 58)
#S(UTF-8
:VALUE #(69 110 103 108 105 115 104))
#S(STRING-REF :VALUE 60)
#S(UTF-8 :VALUE #(67 111 108 117 109 110 51))
#S(STRING-REF :VALUE 62)
#S(UTF-8 :VALUE #(83 112 97 110 105 115 104))
#S(STRING-REF :VALUE 64)
#S(UTF-8 :VALUE #(82 111 119 50))
#S(STRING-REF :VALUE 66)
#S(UTF-8
:VALUE #(72 117 115 101 121 105 110))
#S(STRING-REF :VALUE 68)
#S(UTF-8 :VALUE #(79 103 117 122))
#S(STRING-REF :VALUE 70)
#S(UTF-8 :VALUE #(82 111 119 51))
#S(STRING-REF :VALUE 72)
#S(UTF-8 :VALUE #(75 101 115 107 105 110))
#S(FIELD-REF
:CLASS-INDEX 74
:NAME-AND-TYPE-INDEX 76)
#S(CLASS-REF :VALUE 75)
#S(UTF-8
:VALUE #(106 97 118 97 47 108 97 110 103
47 83 121 115 116 101 109))
#S(DESCRIPTOR
:NAME-INDEX 77
:DESCRIPTOR-INDEX 78)
#S(UTF-8 :VALUE #(111 117 116))
#S(UTF-8
:VALUE #(76 106 97 118 97 47 105 111 47 80
114 105 110 116 83 116 114 101 97
109 59))
#S(METHOD-REF
:CLASS-INDEX 1
:NAME-AND-TYPE-INDEX 80)
#S(DESCRIPTOR
:NAME-INDEX 29
:DESCRIPTOR-INDEX 43)
#S(CLASS-REF :VALUE 82)
#S(UTF-8
:VALUE #(106 97 118 97 47 108 97 110 103
47 83 116 114 105 110 103))
#S(METHOD-REF
:CLASS-INDEX 84
:NAME-AND-TYPE-INDEX 86)
#S(CLASS-REF :VALUE 85)
#S(UTF-8
:VALUE #(106 97 118 97 47 105 111 47 80
114 105 110 116 83 116 114 101 97
109))
#S(DESCRIPTOR
:NAME-INDEX 87
:DESCRIPTOR-INDEX 88)
#S(UTF-8
:VALUE #(112 114 105 110 116 108 110))
#S(UTF-8
:VALUE #(40 76 106 97 118 97 47 108 97 110
103 47 83 116 114 105 110 103 59
41 86))
#S(UTF-8 :VALUE #(97 114 103 115))
#S(UTF-8
:VALUE #(91 76 106 97 118 97 47 108 97 110
103 47 83 116 114 105 110 103
59))
#S(UTF-8
:VALUE #(101 120 116 101 110 100 101 100
77 97 112))
#S(UTF-8
:VALUE #(83 111 117 114 99 101 70 105 108
101))
#S(UTF-8
:VALUE #(69 120 116 101 110 100 101 100 72
97 115 104 77 97 112 46 106 97
118 97))))
:ACCESS-FLAGS 33
:THIS-CLASS 1
:SUPER-CLASS 3
:INTERFACES NIL
:FIELDS (#S(FIELD
:ACCESS-FLAGS 2
:NAME-INDEX 5
:DESCRIPTOR-INDEX 6
:ATTRIBUTES (#S(ATTRIBUTE :ATTRIBUTE-NAME-INDEX 7 :INFO (0 8))))
#S(FIELD
:ACCESS-FLAGS 2
:NAME-INDEX 9
:DESCRIPTOR-INDEX 10
:ATTRIBUTES NIL))
:METHODS (#S(JMETHOD
:ACCESS-FLAGS 1
:NAME-INDEX 11
:DESCRIPTOR-INDEX 12
:ATTRIBUTES (#S(ATTRIBUTE
:ATTRIBUTE-NAME-INDEX 13
:INFO (0 3 0 1 0 0 0 16 42 183 0 14 42 187 0 16
89 183 0 18 181 0 19 177 0 0 0 2 0 21 0
0 0 14 0 3 0 0 0 10 0 4 0 11 0 15 0 12 0
22 0 0 0 12 0 1 0 0 0 16 0 23 0 24 0
0))))
#S(JMETHOD
:ACCESS-FLAGS 1
:NAME-INDEX 25
:DESCRIPTOR-INDEX 26
:ATTRIBUTES (#S(ATTRIBUTE
:ATTRIBUTE-NAME-INDEX 13
:INFO (0 3 0 5 0 0 0 47 42 180 0 19 43 182 0 27
192 0 16 58 4 25 4 199 0 23 187 0 16 89
183 0 18 58 4 42 180 0 19 43 25 4 182 0
31 87 25 4 44 45 182 0 31 87 177 0 0 0 4
0 21 0 0 0 26 0 6 0 0 0 15 0 13 0 16 0
18 0 17 0 27 0 18 0 38 0 20 0 46 0 21 0
22 0 0 0 52 0 5 0 0 0 47 0 23 0 24 0 0 0
0 0 47 0 34 0 35 0 1 0 0 0 47 0 36 0 35
0 2 0 0 0 47 0 37 0 38 0 3 0 13 0 34 0
39 0 6 0 4 0 40 0 0 0 12 0 1 0 13 0 34 0
39 0 41 0 4 0 42 0 0 0 8 0 1 252 0 38 7
0 16))))
#S(JMETHOD
:ACCESS-FLAGS 1
:NAME-INDEX 29
:DESCRIPTOR-INDEX 43
:ATTRIBUTES (#S(ATTRIBUTE
:ATTRIBUTE-NAME-INDEX 13
:INFO (0 2 0 4 0 0 0 24 42 180 0 19 43 182 0 27
192 0 16 78 45 199 0 5 1 176 45 44 182 0
27 176 0 0 0 4 0 21 0 0 0 18 0 4 0 0 0
24 0 12 0 25 0 16 0 26 0 18 0 27 0 22 0
0 0 42 0 4 0 0 0 24 0 23 0 24 0 0 0 0 0
24 0 34 0 35 0 1 0 0 0 24 0 36 0 35 0 2
0 12 0 12 0 39 0 6 0 3 0 40 0 0 0 12 0 1
0 12 0 12 0 39 0 41 0 3 0 42 0 0 0 8 0 1
252 0 18 7 0 16))))
#S(JMETHOD
:ACCESS-FLAGS 9
:NAME-INDEX 44
:DESCRIPTOR-INDEX 45
:ATTRIBUTES (#S(ATTRIBUTE
:ATTRIBUTE-NAME-INDEX 13
:INFO (0 4 0 2 0 0 0 86 187 0 1 89 183 0 46 76
43 18 47 18 49 18 51 182 0 53 43 18 47
18 55 18 57 182 0 53 43 18 47 18 59 18
61 182 0 53 43 18 63 18 49 18 65 182 0
53 43 18 63 18 55 18 67 182 0 53 43 18
69 18 59 18 71 182 0 53 178 0 73 43 18
63 18 55 182 0 79 192 0 81 182 0 83 177
0 0 0 2 0 21 0 0 0 38 0 9 0 0 0 31 0 8 0
32 0 18 0 33 0 28 0 34 0 38 0 35 0 48 0
36 0 58 0 37 0 68 0 39 0 85 0 40 0 22 0
0 0 22 0 2 0 0 0 86 0 89 0 90 0 0 0 8 0
78 0 91 0 24 0 1)))))
:ATTRIBUTES (#S(ATTRIBUTE :ATTRIBUTE-NAME-INDEX 92 :INFO (0 93))))

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.