forked from tcalmant/python-javaobj
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathconstants.py
More file actions
175 lines (141 loc) · 4.15 KB
/
constants.py
File metadata and controls
175 lines (141 loc) · 4.15 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
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
#!/usr/bin/env python3
"""
Definition of the constants used in the deserialization process
:authors: Thomas Calmant
:license: Apache License 2.0
:version: 0.4.3
:status: Alpha
..
Copyright 2021 Thomas Calmant
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
"""
import enum
# ------------------------------------------------------------------------------
__all__ = (
"PRIMITIVE_TYPES",
"StreamConstants",
"TerminalCode",
"ClassDescFlags",
"TypeCode",
"StreamCodeDebug",
)
# Module version
__version_info__ = (0, 4, 3)
__version__ = ".".join(str(x) for x in __version_info__)
# Documentation strings format
__docformat__ = "restructuredtext en"
# ------------------------------------------------------------------------------
class StreamConstants(enum.IntEnum):
"""
Basic constants of the stream protocol
"""
# Magic bytes of any serialized files
STREAM_MAGIC = 0xACED
# Only protocol version supported by javaobj
STREAM_VERSION = 0x05
# Base index for handles
BASE_REFERENCE_IDX = 0x7E0000
class TerminalCode(enum.IntEnum):
"""
Stream type Codes
"""
TC_NULL = 0x70
TC_REFERENCE = 0x71
TC_CLASSDESC = 0x72
TC_OBJECT = 0x73
TC_STRING = 0x74
TC_ARRAY = 0x75
TC_CLASS = 0x76
TC_BLOCKDATA = 0x77
TC_ENDBLOCKDATA = 0x78
TC_RESET = 0x79
TC_BLOCKDATALONG = 0x7A
TC_EXCEPTION = 0x7B
TC_LONGSTRING = 0x7C
TC_PROXYCLASSDESC = 0x7D
TC_ENUM = 0x7E
# Ignore TC_MAX: we don't use it and it messes with TC_ENUM
# TC_MAX = 0x7E
class ClassDescFlags(enum.IntEnum):
"""
Class description flags
"""
SC_WRITE_METHOD = 0x01 # if SC_SERIALIZABLE
SC_BLOCK_DATA = 0x08 # if SC_EXTERNALIZABLE
SC_SERIALIZABLE = 0x02
SC_EXTERNALIZABLE = 0x04
SC_ENUM = 0x10
class TypeCode(enum.IntEnum):
"""
Type definition chars (typecode)
"""
# Primitive types
TYPE_BYTE = ord("B") # 0x42
TYPE_CHAR = ord("C") # 0x43
TYPE_DOUBLE = ord("D") # 0x44
TYPE_FLOAT = ord("F") # 0x46
TYPE_INTEGER = ord("I") # 0x49
TYPE_LONG = ord("J") # 0x4A
TYPE_SHORT = ord("S") # 0x53
TYPE_BOOLEAN = ord("Z") # 0x5A
# Object types
TYPE_OBJECT = ord("L") # 0x4C
TYPE_ARRAY = ord("[") # 0x5B
# List of the types defined as primitive
PRIMITIVE_TYPES = (
TypeCode.TYPE_BYTE,
TypeCode.TYPE_CHAR,
TypeCode.TYPE_DOUBLE,
TypeCode.TYPE_FLOAT,
TypeCode.TYPE_INTEGER,
TypeCode.TYPE_LONG,
TypeCode.TYPE_SHORT,
TypeCode.TYPE_BOOLEAN,
)
class StreamCodeDebug:
"""
Codes utility methods
"""
@staticmethod
def op_id(op_id):
# type: (int) -> str
"""
Returns the name of the given OP Code
:param op_id: OP Code
:return: Name of the OP Code
"""
try:
return TerminalCode(op_id).name
except ValueError:
return "<unknown TC:{0}>".format(op_id)
@staticmethod
def type_code(type_id):
# type: (int) -> str
"""
Returns the name of the given Type Code
:param type_id: Type code
:return: Name of the type code
"""
try:
return TypeCode(type_id).name
except ValueError:
return "<unknown TypeCode:{0}>".format(type_id)
@staticmethod
def flags(flags):
# type: (int) -> str
"""
Returns the names of the class description flags found in the given
integer
:param flags: A class description flag entry
:return: The flags names as a single string
"""
names = sorted(key.name for key in ClassDescFlags if key & flags)
return ", ".join(names)