Skip to content

Instantly share code, notes, and snippets.

@osa1
Created January 30, 2012 19:41
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save osa1/1706229 to your computer and use it in GitHub Desktop.
Save osa1/1706229 to your computer and use it in GitHub Desktop.
parsing java class files with defbinstruct
(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))))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment