Skip to content

Commit

Permalink
enh: Check for serializable version support
Browse files Browse the repository at this point in the history
  • Loading branch information
cyb3rko committed Oct 17, 2023
1 parent e962eba commit 9005550
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 7 deletions.
6 changes: 5 additions & 1 deletion app/src/main/kotlin/com/cyb3rko/pincredible/data/PinTable.kt
Original file line number Diff line number Diff line change
Expand Up @@ -74,10 +74,14 @@ internal class PinTable : Serializable() {
}

@Suppress("UNCHECKED_CAST")
override suspend fun loadFromBytes(bytes: ByteArray): Serializable {
override suspend fun loadFromBytes(bytes: ByteArray): Serializable? {
ByteArrayInputStream(bytes).use {
val version = it.read()
Log.d("PINcredible", "Found PinTable v$version")
if (version > getVersion()) {
Log.d("PINcredible", "PinTable version not supported")
return null
}
val buffer = ByteArray(307)
it.read(buffer)
data = ObjectSerializer.deserialize(buffer) as Array<IntArray>
Expand Down
37 changes: 32 additions & 5 deletions app/src/main/kotlin/com/cyb3rko/pincredible/utils/BackupHandler.kt
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ import com.cyb3rko.backpack.managers.StorageManager
import com.cyb3rko.backpack.modals.ErrorDialog
import com.cyb3rko.backpack.modals.PasswordDialog
import com.cyb3rko.backpack.modals.ProgressDialog
import com.cyb3rko.backpack.modals.VersionNotSupportedDialog
import com.cyb3rko.backpack.utils.ObjectSerializer
import com.cyb3rko.backpack.utils.dateNow
import com.cyb3rko.backpack.utils.lastN
Expand Down Expand Up @@ -292,7 +293,14 @@ internal object BackupHandler {

val backup = SingleBackupStructure().loadFromBytes(
bytes.withoutLastN(OVERHEAD_SIZE - 1)
) as SingleBackupStructure
) as SingleBackupStructure?
if (backup == null) {
withContext(Dispatchers.Main) {
progressDialog.dismiss()
VersionNotSupportedDialog.show(context)
}
return@launch
}
Log.d("PINcredible Backup", "Single backup version ${backup.getVersion()} found")
withContext(Dispatchers.Main) {
progressDialog.updateAbsolute(50)
Expand Down Expand Up @@ -372,7 +380,14 @@ internal object BackupHandler {

val backup = MultiBackupStructure().loadFromBytes(
bytes.withoutLastN(OVERHEAD_SIZE - 1)
) as MultiBackupStructure
) as MultiBackupStructure?
if (backup == null) {
withContext(Dispatchers.Main) {
progressDialog.dismiss()
VersionNotSupportedDialog.show(context)
}
return@launch
}
Log.d("PINcredible Backup", "Full backup version ${backup.getVersion()} found")
withContext(Dispatchers.Main) {
progressDialog.updateAbsolute(50)
Expand Down Expand Up @@ -455,10 +470,14 @@ internal object BackupHandler {
lateinit var pinTable: PinTable
lateinit var name: String

override suspend fun loadFromBytes(bytes: ByteArray): Serializable {
override suspend fun loadFromBytes(bytes: ByteArray): Serializable? {
ByteArrayInputStream(bytes).use {
val version = it.read()
Log.d("PINcredible", "Found SingleBackupStructure v$version")
if (version > getVersion()) {
Log.d("PINcredible", "SingleBackupStructure version not supported")
return null
}
val buffer = ByteArray(PinTable.SIZE)
it.read(buffer)
pinTable = PinTable().loadFromBytes(buffer) as PinTable
Expand Down Expand Up @@ -494,10 +513,14 @@ internal object BackupHandler {
lateinit var pinTable: PinTable
lateinit var fileName: String

override suspend fun loadFromBytes(bytes: ByteArray): Serializable {
override suspend fun loadFromBytes(bytes: ByteArray): Serializable? {
ByteArrayInputStream(bytes).use {
val version = it.read()
Log.d("PINcredible", "Found MultiBackupPinTable v$version")
if (version > getVersion()) {
Log.d("PINcredible", "MultiBackupPinTable version not supported")
return null
}
val buffer = ByteArray(PinTable.SIZE)
it.read(buffer)
pinTable = PinTable().loadFromBytes(buffer) as PinTable
Expand Down Expand Up @@ -534,10 +557,14 @@ internal object BackupHandler {
lateinit var names: Set<String>

@Suppress("UNCHECKED_CAST")
override suspend fun loadFromBytes(bytes: ByteArray): Serializable {
override suspend fun loadFromBytes(bytes: ByteArray): Serializable? {
ByteArrayInputStream(bytes).use { stream ->
val version = stream.read()
Log.d("PINcredible", "Found MultiBackupStrucutre v$version")
if (version > getVersion()) {
Log.d("PINcredible", "MultiBackupStrucutre version not supported")
return null
}
val pinCount = stream.read()
val pinBuffer = mutableListOf<MultiBackupPinTable>()
val pinSizeBytes = ByteArray(2)
Expand Down

0 comments on commit 9005550

Please sign in to comment.