org.telegram.tgnet.NativeByteBuffer Java Examples

The following examples show how to use org.telegram.tgnet.NativeByteBuffer. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: SQLiteCursor.java    From Telegram-FOSS with GNU General Public License v2.0 5 votes vote down vote up
public NativeByteBuffer byteBufferValue(int columnIndex) throws SQLiteException {
	checkRow();
	long ptr = columnByteBufferValue(preparedStatement.getStatementHandle(), columnIndex);
	if (ptr != 0) {
		return NativeByteBuffer.wrap(ptr);
	}
	return null;
}
 
Example #2
Source File: LocationController.java    From Telegram with GNU General Public License v2.0 5 votes vote down vote up
private void saveSharingLocation(final SharingLocationInfo info, final int remove) {
    getMessagesStorage().getStorageQueue().postRunnable(() -> {
        try {
            if (remove == 2) {
                getMessagesStorage().getDatabase().executeFast("DELETE FROM sharing_locations WHERE 1").stepThis().dispose();
            } else if (remove == 1) {
                if (info == null) {
                    return;
                }
                getMessagesStorage().getDatabase().executeFast("DELETE FROM sharing_locations WHERE uid = " + info.did).stepThis().dispose();
            } else {
                if (info == null) {
                    return;
                }
                SQLitePreparedStatement state = getMessagesStorage().getDatabase().executeFast("REPLACE INTO sharing_locations VALUES(?, ?, ?, ?, ?)");
                state.requery();

                NativeByteBuffer data = new NativeByteBuffer(info.messageObject.messageOwner.getObjectSize());
                info.messageObject.messageOwner.serializeToStream(data);

                state.bindLong(1, info.did);
                state.bindInteger(2, info.mid);
                state.bindInteger(3, info.stopTime);
                state.bindInteger(4, info.period);
                state.bindByteBuffer(5, data);

                state.step();
                state.dispose();
                data.reuse();
            }
        } catch (Exception e) {
            FileLog.e(e);
        }
    });
}
 
Example #3
Source File: FileLoadOperation.java    From Telegram with GNU General Public License v2.0 5 votes vote down vote up
private int findNextPreloadDownloadOffset(int atomOffset, int partOffset, NativeByteBuffer partBuffer) {
    int partSize = partBuffer.limit();
    while (true) {
        if (atomOffset < partOffset - (preloadTempBuffer != null ? 16 : 0) || atomOffset >= partOffset + partSize) {
            return 0;
        }
        if (atomOffset >= partOffset + partSize - 16) {
            preloadTempBufferCount = partOffset + partSize - atomOffset;
            partBuffer.position(partBuffer.limit() - preloadTempBufferCount);
            partBuffer.readBytes(preloadTempBuffer, 0, preloadTempBufferCount, false);
            return partOffset + partSize;
        }
        if (preloadTempBufferCount != 0) {
            partBuffer.position(0);
            partBuffer.readBytes(preloadTempBuffer, preloadTempBufferCount, 16 - preloadTempBufferCount, false);
            preloadTempBufferCount = 0;
        } else {
            partBuffer.position(atomOffset - partOffset);
            partBuffer.readBytes(preloadTempBuffer, 0, 16, false);
        }
        int atomSize = (((int) preloadTempBuffer[0] & 0xFF) << 24) + (((int) preloadTempBuffer[1] & 0xFF) << 16) + (((int) preloadTempBuffer[2] & 0xFF) << 8) + ((int) preloadTempBuffer[3] & 0xFF);
        if (atomSize == 0) {
            return 0;
        } else if (atomSize == 1) {
            atomSize = (((int) preloadTempBuffer[12] & 0xFF) << 24) + (((int) preloadTempBuffer[13] & 0xFF) << 16) + (((int) preloadTempBuffer[14] & 0xFF) << 8) + ((int) preloadTempBuffer[15] & 0xFF);
        }
        if (preloadTempBuffer[4] == 'm' && preloadTempBuffer[5] == 'o' && preloadTempBuffer[6] == 'o' && preloadTempBuffer[7] == 'v') {
            return -atomSize;
        }
        if (atomSize + atomOffset >= partOffset + partSize) {
            return atomSize + atomOffset;
        }
        atomOffset += atomSize;
    }
}
 
Example #4
Source File: SQLiteCursor.java    From Telegram with GNU General Public License v2.0 5 votes vote down vote up
public NativeByteBuffer byteBufferValue(int columnIndex) throws SQLiteException {
	checkRow();
	long ptr = columnByteBufferValue(preparedStatement.getStatementHandle(), columnIndex);
	if (ptr != 0) {
		return NativeByteBuffer.wrap(ptr);
	}
	return null;
}
 
Example #5
Source File: LocationController.java    From Telegram-FOSS with GNU General Public License v2.0 5 votes vote down vote up
private void saveSharingLocation(final SharingLocationInfo info, final int remove) {
    getMessagesStorage().getStorageQueue().postRunnable(() -> {
        try {
            if (remove == 2) {
                getMessagesStorage().getDatabase().executeFast("DELETE FROM sharing_locations WHERE 1").stepThis().dispose();
            } else if (remove == 1) {
                if (info == null) {
                    return;
                }
                getMessagesStorage().getDatabase().executeFast("DELETE FROM sharing_locations WHERE uid = " + info.did).stepThis().dispose();
            } else {
                if (info == null) {
                    return;
                }
                SQLitePreparedStatement state = getMessagesStorage().getDatabase().executeFast("REPLACE INTO sharing_locations VALUES(?, ?, ?, ?, ?)");
                state.requery();

                NativeByteBuffer data = new NativeByteBuffer(info.messageObject.messageOwner.getObjectSize());
                info.messageObject.messageOwner.serializeToStream(data);

                state.bindLong(1, info.did);
                state.bindInteger(2, info.mid);
                state.bindInteger(3, info.stopTime);
                state.bindInteger(4, info.period);
                state.bindByteBuffer(5, data);

                state.step();
                state.dispose();
                data.reuse();
            }
        } catch (Exception e) {
            FileLog.e(e);
        }
    });
}
 
Example #6
Source File: FileLoadOperation.java    From Telegram-FOSS with GNU General Public License v2.0 5 votes vote down vote up
private int findNextPreloadDownloadOffset(int atomOffset, int partOffset, NativeByteBuffer partBuffer) {
    int partSize = partBuffer.limit();
    while (true) {
        if (atomOffset < partOffset - (preloadTempBuffer != null ? 16 : 0) || atomOffset >= partOffset + partSize) {
            return 0;
        }
        if (atomOffset >= partOffset + partSize - 16) {
            preloadTempBufferCount = partOffset + partSize - atomOffset;
            partBuffer.position(partBuffer.limit() - preloadTempBufferCount);
            partBuffer.readBytes(preloadTempBuffer, 0, preloadTempBufferCount, false);
            return partOffset + partSize;
        }
        if (preloadTempBufferCount != 0) {
            partBuffer.position(0);
            partBuffer.readBytes(preloadTempBuffer, preloadTempBufferCount, 16 - preloadTempBufferCount, false);
            preloadTempBufferCount = 0;
        } else {
            partBuffer.position(atomOffset - partOffset);
            partBuffer.readBytes(preloadTempBuffer, 0, 16, false);
        }
        int atomSize = (((int) preloadTempBuffer[0] & 0xFF) << 24) + (((int) preloadTempBuffer[1] & 0xFF) << 16) + (((int) preloadTempBuffer[2] & 0xFF) << 8) + ((int) preloadTempBuffer[3] & 0xFF);
        if (atomSize == 0) {
            return 0;
        } else if (atomSize == 1) {
            atomSize = (((int) preloadTempBuffer[12] & 0xFF) << 24) + (((int) preloadTempBuffer[13] & 0xFF) << 16) + (((int) preloadTempBuffer[14] & 0xFF) << 8) + ((int) preloadTempBuffer[15] & 0xFF);
        }
        if (preloadTempBuffer[4] == 'm' && preloadTempBuffer[5] == 'o' && preloadTempBuffer[6] == 'o' && preloadTempBuffer[7] == 'v') {
            return -atomSize;
        }
        if (atomSize + atomOffset >= partOffset + partSize) {
            return atomSize + atomOffset;
        }
        atomOffset += atomSize;
    }
}
 
Example #7
Source File: LocationController.java    From TelePlus-Android with GNU General Public License v2.0 5 votes vote down vote up
private void saveSharingLocation(final SharingLocationInfo info, final int remove) {
    MessagesStorage.getInstance(currentAccount).getStorageQueue().postRunnable(new Runnable() {
        @Override
        public void run() {
            try {
                if (remove == 2) {
                    MessagesStorage.getInstance(currentAccount).getDatabase().executeFast("DELETE FROM sharing_locations WHERE 1").stepThis().dispose();
                } else if (remove == 1) {
                    if (info == null) {
                        return;
                    }
                    MessagesStorage.getInstance(currentAccount).getDatabase().executeFast("DELETE FROM sharing_locations WHERE uid = " + info.did).stepThis().dispose();
                } else {
                    if (info == null) {
                        return;
                    }
                    SQLitePreparedStatement state = MessagesStorage.getInstance(currentAccount).getDatabase().executeFast("REPLACE INTO sharing_locations VALUES(?, ?, ?, ?, ?)");
                    state.requery();

                    NativeByteBuffer data = new NativeByteBuffer(info.messageObject.messageOwner.getObjectSize());
                    info.messageObject.messageOwner.serializeToStream(data);

                    state.bindLong(1, info.did);
                    state.bindInteger(2, info.mid);
                    state.bindInteger(3, info.stopTime);
                    state.bindInteger(4, info.period);
                    state.bindByteBuffer(5, data);

                    state.step();
                    state.dispose();
                    data.reuse();
                }
            } catch (Exception e) {
                FileLog.e(e);
            }
        }
    });
}
 
Example #8
Source File: SQLiteCursor.java    From TelePlus-Android with GNU General Public License v2.0 5 votes vote down vote up
public NativeByteBuffer byteBufferValue(int columnIndex) throws SQLiteException {
	checkRow();
	long ptr = columnByteBufferValue(preparedStatement.getStatementHandle(), columnIndex);
	if (ptr != 0) {
		return NativeByteBuffer.wrap(ptr);
	}
	return null;
}
 
Example #9
Source File: LocationController.java    From TelePlus-Android with GNU General Public License v2.0 5 votes vote down vote up
private void saveSharingLocation(final SharingLocationInfo info, final int remove) {
    MessagesStorage.getInstance(currentAccount).getStorageQueue().postRunnable(new Runnable() {
        @Override
        public void run() {
            try {
                if (remove == 2) {
                    MessagesStorage.getInstance(currentAccount).getDatabase().executeFast("DELETE FROM sharing_locations WHERE 1").stepThis().dispose();
                } else if (remove == 1) {
                    if (info == null) {
                        return;
                    }
                    MessagesStorage.getInstance(currentAccount).getDatabase().executeFast("DELETE FROM sharing_locations WHERE uid = " + info.did).stepThis().dispose();
                } else {
                    if (info == null) {
                        return;
                    }
                    SQLitePreparedStatement state = MessagesStorage.getInstance(currentAccount).getDatabase().executeFast("REPLACE INTO sharing_locations VALUES(?, ?, ?, ?, ?)");
                    state.requery();

                    NativeByteBuffer data = new NativeByteBuffer(info.messageObject.messageOwner.getObjectSize());
                    info.messageObject.messageOwner.serializeToStream(data);

                    state.bindLong(1, info.did);
                    state.bindInteger(2, info.mid);
                    state.bindInteger(3, info.stopTime);
                    state.bindInteger(4, info.period);
                    state.bindByteBuffer(5, data);

                    state.step();
                    state.dispose();
                    data.reuse();
                }
            } catch (Exception e) {
                FileLog.e(e);
            }
        }
    });
}
 
Example #10
Source File: SQLiteCursor.java    From TelePlus-Android with GNU General Public License v2.0 5 votes vote down vote up
public NativeByteBuffer byteBufferValue(int columnIndex) throws SQLiteException {
	checkRow();
	long ptr = columnByteBufferValue(preparedStatement.getStatementHandle(), columnIndex);
	if (ptr != 0) {
		return NativeByteBuffer.wrap(ptr);
	}
	return null;
}
 
Example #11
Source File: SecretChatHelper.java    From TelePlus-Android with GNU General Public License v2.0 4 votes vote down vote up
private boolean decryptWithMtProtoVersion(NativeByteBuffer is, byte[] keyToDecrypt, byte[] messageKey, int version, boolean incoming, boolean encryptOnError) {
    if (version == 1) {
        incoming = false;
    }
    MessageKeyData keyData = MessageKeyData.generateMessageKeyData(keyToDecrypt, messageKey, incoming, version);
    Utilities.aesIgeEncryption(is.buffer, keyData.aesKey, keyData.aesIv, false, false, 24, is.limit() - 24);

    int len = is.readInt32(false);
    byte[] messageKeyFull;
    if (version == 2) {
        messageKeyFull = Utilities.computeSHA256(keyToDecrypt, 88 + (incoming ? 8 : 0), 32, is.buffer, 24, is.buffer.limit());
        if (!Utilities.arraysEquals(messageKey, 0, messageKeyFull, 8)) {
            if (encryptOnError) {
                Utilities.aesIgeEncryption(is.buffer, keyData.aesKey, keyData.aesIv, true, false, 24, is.limit() - 24);
                is.position(24);
            }
            return false;
        }
    } else {
        int l = len + 28;
        if (l < is.buffer.limit() - 15 || l > is.buffer.limit()) {
            l = is.buffer.limit();
        }
        messageKeyFull = Utilities.computeSHA1(is.buffer, 24, l);
        if (!Utilities.arraysEquals(messageKey, 0, messageKeyFull, messageKeyFull.length - 16)) {
            if (encryptOnError) {
                Utilities.aesIgeEncryption(is.buffer, keyData.aesKey, keyData.aesIv, true, false, 24, is.limit() - 24);
                is.position(24);
            }
            return false;
        }
    }
    if (len <= 0 || len > is.limit() - 28) {
        return false;
    }
    int padding = is.limit() - 28 - len;
    if (version == 2 && (padding < 12 || padding > 1024) || version == 1 && padding > 15) {
        return false;
    }
    //
    return true;
}
 
Example #12
Source File: SQLitePreparedStatement.java    From Telegram-FOSS with GNU General Public License v2.0 4 votes vote down vote up
public void bindByteBuffer(int index, NativeByteBuffer value) throws SQLiteException {
    bindByteBuffer(sqliteStatementHandle, index, value.buffer, value.limit());
}
 
Example #13
Source File: SQLitePreparedStatement.java    From TelePlus-Android with GNU General Public License v2.0 4 votes vote down vote up
public void bindByteBuffer(int index, NativeByteBuffer value) throws SQLiteException {
    bindByteBuffer(sqliteStatementHandle, index, value.buffer, value.limit());
}
 
Example #14
Source File: SecretChatHelper.java    From Telegram-FOSS with GNU General Public License v2.0 4 votes vote down vote up
private boolean decryptWithMtProtoVersion(NativeByteBuffer is, byte[] keyToDecrypt, byte[] messageKey, int version, boolean incoming, boolean encryptOnError) {
    if (version == 1) {
        incoming = false;
    }
    MessageKeyData keyData = MessageKeyData.generateMessageKeyData(keyToDecrypt, messageKey, incoming, version);
    Utilities.aesIgeEncryption(is.buffer, keyData.aesKey, keyData.aesIv, false, false, 24, is.limit() - 24);

    int len = is.readInt32(false);
    byte[] messageKeyFull;
    if (version == 2) {
        messageKeyFull = Utilities.computeSHA256(keyToDecrypt, 88 + (incoming ? 8 : 0), 32, is.buffer, 24, is.buffer.limit());
        if (!Utilities.arraysEquals(messageKey, 0, messageKeyFull, 8)) {
            if (encryptOnError) {
                Utilities.aesIgeEncryption(is.buffer, keyData.aesKey, keyData.aesIv, true, false, 24, is.limit() - 24);
                is.position(24);
            }
            return false;
        }
    } else {
        int l = len + 28;
        if (l < is.buffer.limit() - 15 || l > is.buffer.limit()) {
            l = is.buffer.limit();
        }
        messageKeyFull = Utilities.computeSHA1(is.buffer, 24, l);
        if (!Utilities.arraysEquals(messageKey, 0, messageKeyFull, messageKeyFull.length - 16)) {
            if (encryptOnError) {
                Utilities.aesIgeEncryption(is.buffer, keyData.aesKey, keyData.aesIv, true, false, 24, is.limit() - 24);
                is.position(24);
            }
            return false;
        }
    }
    if (len <= 0 || len > is.limit() - 28) {
        return false;
    }
    int padding = is.limit() - 28 - len;
    if (version == 2 && (padding < 12 || padding > 1024) || version == 1 && padding > 15) {
        return false;
    }
    //
    return true;
}
 
Example #15
Source File: SQLitePreparedStatement.java    From TelePlus-Android with GNU General Public License v2.0 4 votes vote down vote up
public void bindByteBuffer(int index, NativeByteBuffer value) throws SQLiteException {
    bindByteBuffer(sqliteStatementHandle, index, value.buffer, value.limit());
}
 
Example #16
Source File: SQLitePreparedStatement.java    From Telegram with GNU General Public License v2.0 4 votes vote down vote up
public void bindByteBuffer(int index, NativeByteBuffer value) throws SQLiteException {
    bindByteBuffer(sqliteStatementHandle, index, value.buffer, value.limit());
}
 
Example #17
Source File: SecretChatHelper.java    From Telegram with GNU General Public License v2.0 4 votes vote down vote up
private boolean decryptWithMtProtoVersion(NativeByteBuffer is, byte[] keyToDecrypt, byte[] messageKey, int version, boolean incoming, boolean encryptOnError) {
    if (version == 1) {
        incoming = false;
    }
    MessageKeyData keyData = MessageKeyData.generateMessageKeyData(keyToDecrypt, messageKey, incoming, version);
    Utilities.aesIgeEncryption(is.buffer, keyData.aesKey, keyData.aesIv, false, false, 24, is.limit() - 24);

    int len = is.readInt32(false);
    byte[] messageKeyFull;
    if (version == 2) {
        messageKeyFull = Utilities.computeSHA256(keyToDecrypt, 88 + (incoming ? 8 : 0), 32, is.buffer, 24, is.buffer.limit());
        if (!Utilities.arraysEquals(messageKey, 0, messageKeyFull, 8)) {
            if (encryptOnError) {
                Utilities.aesIgeEncryption(is.buffer, keyData.aesKey, keyData.aesIv, true, false, 24, is.limit() - 24);
                is.position(24);
            }
            return false;
        }
    } else {
        int l = len + 28;
        if (l < is.buffer.limit() - 15 || l > is.buffer.limit()) {
            l = is.buffer.limit();
        }
        messageKeyFull = Utilities.computeSHA1(is.buffer, 24, l);
        if (!Utilities.arraysEquals(messageKey, 0, messageKeyFull, messageKeyFull.length - 16)) {
            if (encryptOnError) {
                Utilities.aesIgeEncryption(is.buffer, keyData.aesKey, keyData.aesIv, true, false, 24, is.limit() - 24);
                is.position(24);
            }
            return false;
        }
    }
    if (len <= 0 || len > is.limit() - 28) {
        return false;
    }
    int padding = is.limit() - 28 - len;
    if (version == 2 && (padding < 12 || padding > 1024) || version == 1 && padding > 15) {
        return false;
    }
    //
    return true;
}
 
Example #18
Source File: SecretChatHelper.java    From TelePlus-Android with GNU General Public License v2.0 4 votes vote down vote up
private boolean decryptWithMtProtoVersion(NativeByteBuffer is, byte[] keyToDecrypt, byte[] messageKey, int version, boolean incoming, boolean encryptOnError) {
    if (version == 1) {
        incoming = false;
    }
    MessageKeyData keyData = MessageKeyData.generateMessageKeyData(keyToDecrypt, messageKey, incoming, version);
    Utilities.aesIgeEncryption(is.buffer, keyData.aesKey, keyData.aesIv, false, false, 24, is.limit() - 24);

    int len = is.readInt32(false);
    byte[] messageKeyFull;
    if (version == 2) {
        messageKeyFull = Utilities.computeSHA256(keyToDecrypt, 88 + (incoming ? 8 : 0), 32, is.buffer, 24, is.buffer.limit());
        if (!Utilities.arraysEquals(messageKey, 0, messageKeyFull, 8)) {
            if (encryptOnError) {
                Utilities.aesIgeEncryption(is.buffer, keyData.aesKey, keyData.aesIv, true, false, 24, is.limit() - 24);
                is.position(24);
            }
            return false;
        }
    } else {
        int l = len + 28;
        if (l < is.buffer.limit() - 15 || l > is.buffer.limit()) {
            l = is.buffer.limit();
        }
        messageKeyFull = Utilities.computeSHA1(is.buffer, 24, l);
        if (!Utilities.arraysEquals(messageKey, 0, messageKeyFull, messageKeyFull.length - 16)) {
            if (encryptOnError) {
                Utilities.aesIgeEncryption(is.buffer, keyData.aesKey, keyData.aesIv, true, false, 24, is.limit() - 24);
                is.position(24);
            }
            return false;
        }
    }
    if (len <= 0 || len > is.limit() - 28) {
        return false;
    }
    int padding = is.limit() - 28 - len;
    if (version == 2 && (padding < 12 || padding > 1024) || version == 1 && padding > 15) {
        return false;
    }
    //
    return true;
}