2013-10-25 15:19:00 +00:00
|
|
|
/*
|
2013-12-20 19:25:49 +00:00
|
|
|
* This is the source code of Telegram for Android v. 1.3.2.
|
2013-10-25 15:19:00 +00:00
|
|
|
* It is licensed under GNU GPL v. 2 or later.
|
|
|
|
* You should have received a copy of the license in this archive (see LICENSE).
|
|
|
|
*
|
|
|
|
* Copyright Nikolai Kudashov, 2013.
|
|
|
|
*/
|
|
|
|
|
|
|
|
package org.telegram.messenger;
|
|
|
|
|
|
|
|
import java.io.ByteArrayInputStream;
|
|
|
|
import java.io.ByteArrayOutputStream;
|
|
|
|
import java.io.DataInputStream;
|
|
|
|
import java.io.DataOutputStream;
|
|
|
|
import java.io.File;
|
|
|
|
import java.io.FileInputStream;
|
|
|
|
import java.io.IOException;
|
|
|
|
|
|
|
|
public class SerializedData {
|
|
|
|
protected boolean isOut = true;
|
|
|
|
private ByteArrayOutputStream outbuf;
|
|
|
|
private DataOutputStream out;
|
|
|
|
private ByteArrayInputStream inbuf;
|
|
|
|
private DataInputStream in;
|
2014-02-11 14:32:09 +00:00
|
|
|
private boolean justCalc = false;
|
|
|
|
private int len;
|
2013-10-25 15:19:00 +00:00
|
|
|
|
|
|
|
public SerializedData() {
|
|
|
|
outbuf = new ByteArrayOutputStream();
|
|
|
|
out = new DataOutputStream(outbuf);
|
|
|
|
}
|
|
|
|
|
2014-02-11 14:32:09 +00:00
|
|
|
public SerializedData(boolean calculate) {
|
|
|
|
if (!calculate) {
|
|
|
|
outbuf = new ByteArrayOutputStream();
|
|
|
|
out = new DataOutputStream(outbuf);
|
|
|
|
}
|
|
|
|
justCalc = calculate;
|
|
|
|
len = 0;
|
|
|
|
}
|
|
|
|
|
2013-12-20 19:25:49 +00:00
|
|
|
public SerializedData(int size) {
|
|
|
|
outbuf = new ByteArrayOutputStream(size);
|
|
|
|
out = new DataOutputStream(outbuf);
|
|
|
|
}
|
|
|
|
|
2013-10-25 15:19:00 +00:00
|
|
|
public SerializedData(byte[] data){
|
|
|
|
isOut = false;
|
|
|
|
inbuf = new ByteArrayInputStream(data);
|
|
|
|
in = new DataInputStream(inbuf);
|
|
|
|
}
|
|
|
|
|
|
|
|
public SerializedData(File file) throws IOException {
|
|
|
|
FileInputStream is = new FileInputStream(file);
|
|
|
|
byte[] data = new byte[(int)file.length()];
|
|
|
|
new DataInputStream(is).readFully(data);
|
|
|
|
is.close();
|
|
|
|
|
|
|
|
isOut = false;
|
|
|
|
inbuf = new ByteArrayInputStream(data);
|
|
|
|
in = new DataInputStream(inbuf);
|
|
|
|
}
|
|
|
|
|
2014-02-11 14:32:09 +00:00
|
|
|
public void writeInt32(int x) {
|
|
|
|
if (!justCalc) {
|
|
|
|
writeInt32(x, out);
|
|
|
|
} else {
|
|
|
|
len += 4;
|
|
|
|
}
|
2013-10-25 15:19:00 +00:00
|
|
|
}
|
|
|
|
|
2014-02-11 14:32:09 +00:00
|
|
|
private void writeInt32(int x, DataOutputStream out) {
|
2013-10-25 15:19:00 +00:00
|
|
|
try {
|
2014-02-11 14:32:09 +00:00
|
|
|
for(int i = 0; i < 4; i++) {
|
2013-10-25 15:19:00 +00:00
|
|
|
out.write(x >> (i * 8));
|
|
|
|
}
|
|
|
|
} catch(IOException gfdsgd) {
|
2013-12-20 19:25:49 +00:00
|
|
|
FileLog.e("tmessages", "write int32 error");
|
2013-10-25 15:19:00 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public void writeInt64(long i) {
|
2014-02-11 14:32:09 +00:00
|
|
|
if (!justCalc) {
|
|
|
|
writeInt64(i, out);
|
|
|
|
} else {
|
|
|
|
len += 8;
|
|
|
|
}
|
2013-10-25 15:19:00 +00:00
|
|
|
}
|
|
|
|
|
2014-02-11 14:32:09 +00:00
|
|
|
private void writeInt64(long x, DataOutputStream out) {
|
2013-10-25 15:19:00 +00:00
|
|
|
try {
|
|
|
|
for(int i = 0; i < 8; i++){
|
|
|
|
out.write((int)(x >> (i * 8)));
|
|
|
|
}
|
|
|
|
} catch(IOException gfdsgd) {
|
2013-12-20 19:25:49 +00:00
|
|
|
FileLog.e("tmessages", "write int64 error");
|
2013-10-25 15:19:00 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public boolean readBool() {
|
|
|
|
int consructor = readInt32();
|
|
|
|
if (consructor == 0x997275b5) {
|
|
|
|
return true;
|
|
|
|
} else if (consructor == 0xbc799737) {
|
|
|
|
return false;
|
|
|
|
}
|
2013-12-20 19:25:49 +00:00
|
|
|
FileLog.e("tmessages", "Not bool value!");
|
2013-10-25 15:19:00 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
public void writeBool(boolean value) {
|
2014-02-11 14:32:09 +00:00
|
|
|
if (!justCalc) {
|
|
|
|
if (value) {
|
|
|
|
writeInt32(0x997275b5);
|
|
|
|
} else {
|
|
|
|
writeInt32(0xbc799737);
|
|
|
|
}
|
2013-10-25 15:19:00 +00:00
|
|
|
} else {
|
2014-02-11 14:32:09 +00:00
|
|
|
len += 4;
|
2013-10-25 15:19:00 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-12-20 19:25:49 +00:00
|
|
|
public int readInt32() {
|
|
|
|
return readInt32(null);
|
|
|
|
}
|
|
|
|
|
|
|
|
public int readInt32(boolean[] error) {
|
2013-10-25 15:19:00 +00:00
|
|
|
try {
|
|
|
|
int i = 0;
|
|
|
|
for(int j = 0; j < 4; j++){
|
|
|
|
i |= (in.read() << (j * 8));
|
|
|
|
}
|
2013-12-20 19:25:49 +00:00
|
|
|
if (error != null) {
|
|
|
|
error[0] = false;
|
|
|
|
}
|
2013-10-25 15:19:00 +00:00
|
|
|
return i;
|
|
|
|
} catch(IOException x) {
|
2013-12-20 19:25:49 +00:00
|
|
|
if (error != null) {
|
|
|
|
error[0] = true;
|
|
|
|
}
|
|
|
|
FileLog.e("tmessages", "read int32 error");
|
2013-10-25 15:19:00 +00:00
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2013-12-20 19:25:49 +00:00
|
|
|
public long readInt64() {
|
|
|
|
return readInt64(null);
|
|
|
|
}
|
|
|
|
|
|
|
|
public long readInt64(boolean[] error) {
|
2013-10-25 15:19:00 +00:00
|
|
|
try {
|
|
|
|
long i = 0;
|
|
|
|
for(int j = 0; j < 8; j++){
|
|
|
|
i |= ((long)in.read() << (j * 8));
|
|
|
|
}
|
2013-12-20 19:25:49 +00:00
|
|
|
if (error != null) {
|
|
|
|
error[0] = false;
|
|
|
|
}
|
2013-10-25 15:19:00 +00:00
|
|
|
return i;
|
|
|
|
} catch(IOException x) {
|
2013-12-20 19:25:49 +00:00
|
|
|
if (error != null) {
|
|
|
|
error[0] = true;
|
|
|
|
}
|
|
|
|
FileLog.e("tmessages", "read int64 error");
|
2013-10-25 15:19:00 +00:00
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2014-02-11 14:32:09 +00:00
|
|
|
public void writeRaw(byte[] b) {
|
2013-10-25 15:19:00 +00:00
|
|
|
try {
|
2014-02-11 14:32:09 +00:00
|
|
|
if (!justCalc) {
|
|
|
|
out.write(b);
|
|
|
|
} else {
|
|
|
|
len += b.length;
|
|
|
|
}
|
2013-10-25 15:19:00 +00:00
|
|
|
} catch(Exception x) {
|
2013-12-20 19:25:49 +00:00
|
|
|
FileLog.e("tmessages", "write raw error");
|
2013-10-25 15:19:00 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public void writeRaw(byte[] b, int offset, int count) {
|
|
|
|
try {
|
2014-02-11 14:32:09 +00:00
|
|
|
if (!justCalc) {
|
|
|
|
out.write(b, offset, count);
|
|
|
|
} else {
|
|
|
|
len += count;
|
|
|
|
}
|
2013-10-25 15:19:00 +00:00
|
|
|
} catch(Exception x) {
|
2013-12-20 19:25:49 +00:00
|
|
|
FileLog.e("tmessages", "write raw error");
|
2013-10-25 15:19:00 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public void writeByte(int i) {
|
|
|
|
try {
|
2014-02-11 14:32:09 +00:00
|
|
|
if (!justCalc) {
|
|
|
|
out.writeByte((byte)i);
|
|
|
|
} else {
|
|
|
|
len += 1;
|
|
|
|
}
|
2013-10-25 15:19:00 +00:00
|
|
|
} catch (Exception e) {
|
2013-12-20 19:25:49 +00:00
|
|
|
FileLog.e("tmessages", "write byte error");
|
2013-10-25 15:19:00 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public void writeByte(byte b) {
|
|
|
|
try {
|
2014-02-11 14:32:09 +00:00
|
|
|
if (!justCalc) {
|
|
|
|
out.writeByte(b);
|
|
|
|
} else {
|
|
|
|
len += 1;
|
|
|
|
}
|
2013-10-25 15:19:00 +00:00
|
|
|
} catch (Exception e) {
|
2013-12-20 19:25:49 +00:00
|
|
|
FileLog.e("tmessages", "write byte error");
|
2013-10-25 15:19:00 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-02-11 14:32:09 +00:00
|
|
|
public void readRaw(byte[] b) {
|
2013-10-25 15:19:00 +00:00
|
|
|
try {
|
|
|
|
in.read(b);
|
|
|
|
} catch(Exception x) {
|
2013-12-20 19:25:49 +00:00
|
|
|
FileLog.e("tmessages", "read raw error");
|
2013-10-25 15:19:00 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public byte[] readData(int count) {
|
|
|
|
byte[] arr = new byte[count];
|
|
|
|
readRaw(arr);
|
|
|
|
return arr;
|
|
|
|
}
|
|
|
|
|
2014-02-11 14:32:09 +00:00
|
|
|
public String readString() {
|
2013-10-25 15:19:00 +00:00
|
|
|
try {
|
|
|
|
int sl = 1;
|
|
|
|
int l = in.read();
|
|
|
|
if(l >= 254){
|
|
|
|
l = in.read() | (in.read() << 8) | (in.read() << 16);
|
|
|
|
sl = 4;
|
|
|
|
}
|
|
|
|
byte[] b = new byte[l];
|
|
|
|
in.read(b);
|
|
|
|
int i=sl;
|
|
|
|
while((l + i) % 4 != 0) {
|
|
|
|
in.read();
|
|
|
|
i++;
|
|
|
|
}
|
|
|
|
return new String(b, "UTF-8");
|
|
|
|
} catch(Exception x) {
|
2013-12-20 19:25:49 +00:00
|
|
|
FileLog.e("tmessages", "read string error");
|
2013-10-25 15:19:00 +00:00
|
|
|
}
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
public byte[] readByteArray() {
|
|
|
|
try {
|
|
|
|
int sl = 1;
|
|
|
|
int l = in.read();
|
|
|
|
if (l >= 254){
|
|
|
|
l = in.read() | (in.read() << 8) | (in.read() << 16);
|
|
|
|
sl = 4;
|
|
|
|
}
|
|
|
|
byte[] b = new byte[l];
|
|
|
|
in.read(b);
|
|
|
|
int i = sl;
|
|
|
|
while((l + i) % 4 != 0){
|
|
|
|
in.read();
|
|
|
|
i++;
|
|
|
|
}
|
|
|
|
return b;
|
|
|
|
} catch(Exception x) {
|
2013-12-20 19:25:49 +00:00
|
|
|
FileLog.e("tmessages", "read byte array error");
|
2013-10-25 15:19:00 +00:00
|
|
|
}
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2014-02-11 14:32:09 +00:00
|
|
|
public void writeByteArray(byte[] b) {
|
2013-10-25 15:19:00 +00:00
|
|
|
try {
|
|
|
|
if (b.length <= 253){
|
2014-02-11 14:32:09 +00:00
|
|
|
if (!justCalc) {
|
|
|
|
out.write(b.length);
|
|
|
|
} else {
|
|
|
|
len += 1;
|
|
|
|
}
|
2013-10-25 15:19:00 +00:00
|
|
|
} else {
|
2014-02-11 14:32:09 +00:00
|
|
|
if (!justCalc) {
|
|
|
|
out.write(254);
|
|
|
|
out.write(b.length);
|
|
|
|
out.write(b.length >> 8);
|
|
|
|
out.write(b.length >> 16);
|
|
|
|
} else {
|
|
|
|
len += 4;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (!justCalc) {
|
|
|
|
out.write(b);
|
|
|
|
} else {
|
|
|
|
len += b.length;
|
2013-10-25 15:19:00 +00:00
|
|
|
}
|
|
|
|
int i = b.length <= 253 ? 1 : 4;
|
|
|
|
while((b.length + i) % 4 != 0){
|
2014-02-11 14:32:09 +00:00
|
|
|
if (!justCalc) {
|
|
|
|
out.write(0);
|
|
|
|
} else {
|
|
|
|
len += 1;
|
|
|
|
}
|
2013-10-25 15:19:00 +00:00
|
|
|
i++;
|
|
|
|
}
|
2013-12-20 19:25:49 +00:00
|
|
|
} catch(Exception x) {
|
|
|
|
FileLog.e("tmessages", "write byte array error");
|
2013-10-25 15:19:00 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public void writeString(String s){
|
|
|
|
try {
|
|
|
|
writeByteArray(s.getBytes("UTF-8"));
|
|
|
|
} catch(Exception x) {
|
2013-12-20 19:25:49 +00:00
|
|
|
FileLog.e("tmessages", "write string error");
|
2013-10-25 15:19:00 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public void writeByteArray(byte[] b, int offset, int count) {
|
|
|
|
try {
|
|
|
|
if(count <= 253){
|
2014-02-11 14:32:09 +00:00
|
|
|
if (!justCalc) {
|
|
|
|
out.write(count);
|
|
|
|
} else {
|
|
|
|
len += 1;
|
|
|
|
}
|
2013-10-25 15:19:00 +00:00
|
|
|
} else {
|
2014-02-11 14:32:09 +00:00
|
|
|
if (!justCalc) {
|
|
|
|
out.write(254);
|
|
|
|
out.write(count);
|
|
|
|
out.write(count >> 8);
|
|
|
|
out.write(count >> 16);
|
|
|
|
} else {
|
|
|
|
len += 4;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (!justCalc) {
|
|
|
|
out.write(b, offset, count);
|
|
|
|
} else {
|
|
|
|
len += count;
|
2013-10-25 15:19:00 +00:00
|
|
|
}
|
|
|
|
int i = count <= 253 ? 1 : 4;
|
|
|
|
while ((count + i) % 4 != 0){
|
2014-02-11 14:32:09 +00:00
|
|
|
if (!justCalc) {
|
|
|
|
out.write(0);
|
|
|
|
} else {
|
|
|
|
len += 1;
|
|
|
|
}
|
2013-10-25 15:19:00 +00:00
|
|
|
i++;
|
|
|
|
}
|
|
|
|
} catch(Exception x) {
|
2013-12-20 19:25:49 +00:00
|
|
|
FileLog.e("tmessages", "write byte array error");
|
2013-10-25 15:19:00 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-12-20 19:25:49 +00:00
|
|
|
public double readDouble() {
|
2013-10-25 15:19:00 +00:00
|
|
|
try {
|
|
|
|
return Double.longBitsToDouble(readInt64());
|
|
|
|
} catch(Exception x) {
|
2013-12-20 19:25:49 +00:00
|
|
|
FileLog.e("tmessages", "read double error");
|
2013-10-25 15:19:00 +00:00
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2014-02-11 14:32:09 +00:00
|
|
|
public void writeDouble(double d) {
|
2013-10-25 15:19:00 +00:00
|
|
|
try {
|
|
|
|
writeInt64(Double.doubleToRawLongBits(d));
|
|
|
|
} catch(Exception x) {
|
2013-12-20 19:25:49 +00:00
|
|
|
FileLog.e("tmessages", "write double error");
|
2013-10-25 15:19:00 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public int length() {
|
2014-02-11 14:32:09 +00:00
|
|
|
if (!justCalc) {
|
|
|
|
return isOut ? outbuf.size() : inbuf.available();
|
|
|
|
}
|
|
|
|
return len;
|
2013-10-25 15:19:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
protected void set(byte[] newData) {
|
|
|
|
isOut = false;
|
|
|
|
inbuf = new ByteArrayInputStream(newData);
|
|
|
|
in = new DataInputStream(inbuf);
|
|
|
|
}
|
|
|
|
|
|
|
|
public byte[] toByteArray() {
|
|
|
|
return outbuf.toByteArray();
|
|
|
|
}
|
|
|
|
}
|