Add compression support

This commit is contained in:
2020-07-03 16:28:26 +02:00
parent a8e6c9e99b
commit b1b4ad3030
7 changed files with 342 additions and 13 deletions

View File

@@ -1,12 +1,19 @@
package com.github.mondei1.offpass
import android.content.Context
import android.util.Log
import com.github.mondei1.offpass.entities.Compression
import com.github.mondei1.offpass.entities.CompressionHelper
import kotlinx.coroutines.GlobalScope
import kotlinx.coroutines.async
import kotlinx.coroutines.channels.broadcast
import kotlinx.coroutines.runBlocking
import java.lang.Error
import java.util.regex.Matcher
import java.util.regex.Pattern
class QRSchema {
private var db: CompressionHelper? = null
var raw: String = ""
var decrypted_raw: String = ""
@@ -20,14 +27,34 @@ class QRSchema {
var custom: HashMap<String, String> = HashMap() // All defined custom/optional fields
var question_awnser: HashMap<String, String> = HashMap() // Used for security questions
constructor() {}
constructor(context: Context) {
this.db = CompressionHelper(context, null)
}
/**
* This function takes a key `db_key` and tries to resolve it.
*/
private suspend fun resolve(key: String, context: Context): String {
val to_resolve = key.replace("$", "")
Log.i("QR-Code schema", "Try to resolve $to_resolve")
val database = db
return GlobalScope.async<String> {
var cursor = database!!.get(session_key, to_resolve)
if (cursor != null) {
cursor.toString()
} else {
""
}
}.await()
}
/**
* This function will take the already set raw content and tries to parse it.
*
* @return It will return `true` if the operation was successful and `false` if otherwise.
*/
fun parse(): Boolean {
fun parse(context: Context): Boolean {
if (decrypted_raw == "") {
throw Error("Tried to parse QR-Code schema but raw content must be first decrypted! " +
"Set raw content first and then use decrypt()")
@@ -42,9 +69,9 @@ class QRSchema {
// Check if there is a session key
if (fields[0].startsWith("%")) {
fields[0] = fields[0].replace("%", "")
session_key = fields[0].substring(0, 9) // Get first 10 chars, which are the
session_key = fields[0].substring(0, 10) // Get first 10 chars, which are the
// session key
this.title = fields[0].substring(10, fields[0].length)
this.title = fields[0].substring(11, fields[0].length)
} else {
this.title = fields[0]
}
@@ -53,15 +80,35 @@ class QRSchema {
this.password = fields[2]
this.email = fields[3]
this.website_url = fields[4]
// Look if website url is compressed
if (this.website_url.startsWith("$")) {
var that_class = this
runBlocking {
that_class.website_url = resolve(that_class.website_url, context)
}
}
}
// Here are optional/custom fields
if (i > 4) {
if (fields[i].startsWith("(")) {
if (fields[i].startsWith("(") && fields[i] != "") {
var closingBracket = fields[i].indexOf(")")
var key = fields[i].substring(1, closingBracket)
var value = fields[i].substring(closingBracket+1, fields[i].length)
// Check if key and or value are compressed
if (key.startsWith("$")) {
runBlocking {
key = resolve(key, context)
}
}
if (value.startsWith("$")) {
runBlocking {
value = resolve(value, context)
}
}
// We got a security question/awnser
if (key == "") {
var qa = value.split("%")
@@ -69,15 +116,23 @@ class QRSchema {
} else {
custom.put(key, value)
}
Log.i("QR-Code schema", custom.toString())
} else {
throw Error("Custom/optional field should start with an open bracket: "
+ fields[i].toString())
}
}
// Final step is to resolve any compressed values
}
Log.i("QR-Code schema", "Found: $session_key, $title, $username, $password, $email, $website_url. ${custom.toString()} and ${question_awnser.toString()}")
Log.i("QR-Code schema", "Found: $session_key, $title, $username, $password, " +
"$email, $website_url. ${custom.toString()} and ${question_awnser.toString()}")
return true
}
/**
* This function will take all defined variables and converts them into the QR-Code schema
*/
fun build() {
}