Ipp.java
9.59 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
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
/*
* Decompiled with CFR 0_118.
*/
package com.scene7.is.ipp.messages;
import com.scene7.is.ipp.messages.IppProp;
import com.scene7.is.ipp.messages.IppPropDate;
import com.scene7.is.ipp.messages.IppPropDouble;
import com.scene7.is.ipp.messages.IppPropInt;
import com.scene7.is.ipp.messages.IppPropLongString;
import com.scene7.is.ipp.messages.IppPropString;
import com.scene7.is.ipp.messages.Offset;
import java.io.IOException;
import java.io.PrintStream;
import java.io.UnsupportedEncodingException;
import java.io.Writer;
public class Ipp {
public static void Assert(boolean expr) {
assert (expr);
}
public static void Assert(boolean expr, String sexpr) {
assert (expr);
}
public static int StuffIntDirect(byte[] buffer, int offset, int intToStuff) {
buffer[offset + 0] = (byte)(intToStuff & 255);
buffer[offset + 1] = (byte)(intToStuff >> 8 & 255);
buffer[offset + 2] = (byte)(intToStuff >> 16 & 255);
buffer[offset + 3] = (byte)(intToStuff >> 24 & 255);
return 4;
}
public static void StuffInt(byte[] buffer, Offset offset, int intToStuff) {
assert ((offset.val & 3) == 0);
int pos = offset.val;
offset.val += 4;
Ipp.StuffIntDirect(buffer, pos, intToStuff);
}
public static void StuffOffset(byte[] buffer, Offset offset, Offset offToStuff, int alignment) {
assert ((alignment & alignment - 1) == 0);
offToStuff.val = offToStuff.val + alignment - 1 & ~ (alignment - 1);
assert (offToStuff.val >= 0);
Ipp.StuffInt(buffer, offset, offToStuff.val);
}
public static void StuffNullBytes(byte[] buffer, Offset offset, int count) {
int i = offset.val + count;
while (offset.val < i) {
buffer[offset.val] = 0;
++offset.val;
}
}
public static void StuffHeader(byte[] buffer, int clientId, int tag, int MessageNumber, int length) {
Ipp.StuffIntDirect(buffer, 0, clientId);
Ipp.StuffIntDirect(buffer, 4, tag);
Ipp.StuffIntDirect(buffer, 8, MessageNumber);
Ipp.StuffIntDirect(buffer, 12, length);
}
public static int StuffInt64Direct(byte[] buffer, int offset, long longToStuff) {
assert ((offset & 7) == 0);
buffer[offset + 0] = (byte)(longToStuff & 255);
buffer[offset + 1] = (byte)(longToStuff >> 8 & 255);
buffer[offset + 2] = (byte)(longToStuff >> 16 & 255);
buffer[offset + 3] = (byte)(longToStuff >> 24 & 255);
buffer[offset + 4] = (byte)(longToStuff >> 32 & 255);
buffer[offset + 5] = (byte)(longToStuff >> 40 & 255);
buffer[offset + 6] = (byte)(longToStuff >> 48 & 255);
buffer[offset + 7] = (byte)(longToStuff >> 56 & 255);
return 8;
}
public static void StuffInt64(byte[] buffer, Offset offset, long longToStuff) {
int pos = offset.val;
offset.val += 8;
Ipp.StuffInt64Direct(buffer, pos, longToStuff);
}
public static void StuffDouble(byte[] buffer, Offset offset, double v) {
int pos = offset.val;
offset.val += 8;
Ipp.StuffInt64Direct(buffer, pos, Double.doubleToLongBits(v));
}
public static void StuffString(byte[] buffer, Offset offset, Offset varOffset, String string) {
byte[] stringBytes = new byte[]{};
try {
stringBytes = string.getBytes("UTF-8");
}
catch (UnsupportedEncodingException e) {
throw new AssertionError();
}
Ipp.StuffOffset(buffer, offset, varOffset, 1);
System.arraycopy(stringBytes, 0, buffer, varOffset.val, stringBytes.length);
varOffset.val += stringBytes.length;
buffer[varOffset.val] = 0;
++varOffset.val;
}
public static void StuffStringArray(byte[] buffer, Offset offset, Offset varOffset, String[] str) {
Ipp.StuffInt(buffer, offset, str.length);
Ipp.StuffInt(buffer, offset, 4);
Ipp.StuffOffset(buffer, offset, varOffset, 4);
Offset varvarOffset = new Offset(varOffset.val + 4 * str.length);
for (int i = 0; i < str.length; ++i) {
Ipp.StuffString(buffer, varOffset, varvarOffset, str[i]);
}
}
public static void StuffLongString(byte[] buffer, Offset offset, Offset varOffset, String string) {
byte[] stringBytes = string.getBytes();
Ipp.StuffOffset(buffer, offset, varOffset, 1);
System.arraycopy(stringBytes, 0, buffer, varOffset.val, stringBytes.length);
varOffset.val += stringBytes.length;
buffer[varOffset.val] = 0;
++varOffset.val;
}
public static void StuffByteArray(byte[] buffer, Offset offset, Offset varOffset, byte[] bytes) {
Ipp.StuffInt(buffer, offset, bytes.length);
Ipp.StuffOffset(buffer, offset, varOffset, 1);
System.arraycopy(bytes, 0, buffer, varOffset.val, bytes.length);
varOffset.val += bytes.length;
}
public static void StuffByteFixedArray(byte[] buffer, Offset offset, int length, byte[] bytes) {
assert (bytes.length == length);
System.arraycopy(bytes, 0, buffer, offset.val, length);
offset.val += length;
}
public static int UnstuffTriplet(byte[] array, int arrayPos) {
int returnVal = (array[arrayPos + 2] & 255) << 16;
returnVal |= (array[arrayPos + 1] & 255) << 8;
return returnVal |= array[arrayPos] & 255;
}
public static int UnstuffInt(byte[] array, int arrayPos) {
assert ((arrayPos & 3) == 0);
int returnVal = (array[arrayPos + 3] & 255) << 24;
returnVal |= (array[arrayPos + 2] & 255) << 16;
returnVal |= (array[arrayPos + 1] & 255) << 8;
return returnVal |= array[arrayPos] & 255;
}
public static double UnstuffDouble(byte[] array, int arrayPos) {
return Double.longBitsToDouble(Ipp.UnstuffInt64(array, arrayPos));
}
public static long UnstuffInt64(byte[] array, int pos) {
assert ((pos & 7) == 0);
long returnVal = ((long)array[pos + 7] & 255) << 56;
returnVal |= ((long)array[pos + 6] & 255) << 48;
returnVal |= ((long)array[pos + 5] & 255) << 40;
returnVal |= ((long)array[pos + 4] & 255) << 32;
returnVal |= ((long)array[pos + 3] & 255) << 24;
returnVal |= ((long)array[pos + 2] & 255) << 16;
returnVal |= ((long)array[pos + 1] & 255) << 8;
return returnVal |= (long)array[pos + 0] & 255;
}
public static String UnstuffLongString(byte[] array, int pos) {
int epos = pos = Ipp.UnstuffInt(array, pos);
while (array[epos] != 0) {
++epos;
}
return new String(array, pos, epos - pos);
}
public static synchronized void WriteHex(byte[] buffer, int len, int width, boolean bare) {
for (int index = 0; index < len; index += width) {
int subind = 0;
for (subind = 0; subind < width && subind + index < len; ++subind) {
if (subind + index >= len) continue;
byte[] tempbuf = new byte[]{buffer[index + subind], 0, 0, 0};
int tempint = Ipp.UnstuffInt(tempbuf, 0);
if (tempint >= 0 && tempint < 16) {
System.out.print("0");
}
System.out.print(Integer.toString(tempint, 16));
if (bare) continue;
System.out.print(" ");
}
if (!bare) {
for (int i = 0; i < width - subind; ++i) {
System.out.print(" ");
}
System.out.print(" '" + new String(buffer, index, subind) + "'");
}
System.out.println("");
}
System.out.flush();
}
public static void PrintTag(Writer tf, String label, int it) throws IOException {
tf.write(label + " ");
Ipp.PrintHex(tf, it);
tf.write("\n");
}
public static void PrintHex(Writer tf, String label, int it) throws IOException {
tf.write(label);
Ipp.PrintHex(tf, it);
tf.write("\n");
}
public static void PrintHex(Writer tf, int it) throws IOException {
String hex = "0123456789ABCDEF";
char[] d = new char[8];
for (int i = 0; i < 8; ++i) {
d[i] = hex.charAt(it >> (7 - i) * 4 & 15);
}
tf.write(new String(d));
}
public static String LookupString(IppProp[] props, int id) {
for (int i = 0; i < props.length; ++i) {
if (props[i].id() != id) continue;
if (props[i].type() == 65540) {
IppPropString p = (IppPropString)props[i];
return p.value();
}
if (props[i].type() != 65541) continue;
IppPropLongString p = (IppPropLongString)props[i];
return p.value();
}
return null;
}
public static double LookupDouble(IppProp[] props, int id) {
for (int i = 0; i < props.length; ++i) {
if (props[i].id() != id || props[i].type() != 65538) continue;
IppPropDouble p = (IppPropDouble)props[i];
return p.value();
}
return 0.0;
}
public static int LookupInt(IppProp[] props, int id) {
for (int i = 0; i < props.length; ++i) {
if (props[i].id() != id || props[i].type() != 65537) continue;
IppPropInt p = (IppPropInt)props[i];
return p.value();
}
return -1;
}
public static long LookupDate(IppProp[] props, int id) {
for (int i = 0; i < props.length; ++i) {
if (props[i].id() != id || props[i].type() != 65539) continue;
IppPropDate p = (IppPropDate)props[i];
return p.value();
}
return -1;
}
}