mirror of
https://github.com/Kunzisoft/KeePassDX.git
synced 2026-11-29 21:33:07 -08:00
Encapsulate binary methods
This commit is contained in:
1 parent
9a91be7e36
commit
1eebc72b21
7 files changed
+47
-119
No files matched your search
@@ -442,7 +442,8 @@ class EntryEditActivity : LockingActivity(),
|
||||
}
|
||||
|
||||
private fun buildNewAttachment(attachmentToUploadUri: Uri, fileName: String) {
|
||||
mDatabase?.buildNewAttachment(applicationContext.filesDir, fileName)?.let { entryAttachment ->
|
||||
mDatabase?.buildNewBinary(applicationContext.filesDir)?.let { binaryAttachment ->
|
||||
val entryAttachment = EntryAttachment(fileName, binaryAttachment)
|
||||
// Ask to replace the current attachment
|
||||
if ((mDatabase?.allowMultipleAttachments != true && entryEditContentsView?.containsAttachment() == true) ||
|
||||
entryEditContentsView?.containsAttachment(EntryAttachmentState(entryAttachment, StreamDirection.UPLOAD)) == true) {
|
||||
|
||||
@@ -32,6 +32,7 @@ import com.kunzisoft.keepass.database.element.icon.IconImageFactory
|
||||
import com.kunzisoft.keepass.database.element.node.NodeId
|
||||
import com.kunzisoft.keepass.database.element.node.NodeIdInt
|
||||
import com.kunzisoft.keepass.database.element.node.NodeIdUUID
|
||||
import com.kunzisoft.keepass.database.element.security.BinaryAttachment
|
||||
import com.kunzisoft.keepass.database.element.security.EncryptionAlgorithm
|
||||
import com.kunzisoft.keepass.database.exception.DatabaseOutputException
|
||||
import com.kunzisoft.keepass.database.exception.FileNotFoundDatabaseException
|
||||
@@ -438,9 +439,9 @@ class Database {
|
||||
return false
|
||||
}
|
||||
|
||||
fun buildNewAttachment(cacheDirectory: File, fileName: String): EntryAttachment? {
|
||||
return mDatabaseKDB?.buildNewAttachment(cacheDirectory, fileName)
|
||||
?: mDatabaseKDBX?.buildNewAttachment(cacheDirectory, fileName)
|
||||
fun buildNewBinary(cacheDirectory: File): BinaryAttachment? {
|
||||
return mDatabaseKDB?.buildNewBinary(cacheDirectory)
|
||||
?: mDatabaseKDBX?.buildNewBinary(cacheDirectory)
|
||||
}
|
||||
|
||||
fun removeAttachmentIfNotUsed(attachment: EntryAttachment) {
|
||||
|
||||
@@ -28,7 +28,6 @@ import com.kunzisoft.keepass.database.element.node.NodeIdInt
|
||||
import com.kunzisoft.keepass.database.element.node.NodeIdUUID
|
||||
import com.kunzisoft.keepass.database.element.security.BinaryAttachment
|
||||
import com.kunzisoft.keepass.database.element.security.EncryptionAlgorithm
|
||||
import com.kunzisoft.keepass.database.element.EntryAttachment
|
||||
import com.kunzisoft.keepass.stream.NullOutputStream
|
||||
import java.io.File
|
||||
import java.io.IOException
|
||||
@@ -252,15 +251,10 @@ class DatabaseKDB : DatabaseVersioned<Int, UUID, GroupKDB, EntryKDB>() {
|
||||
addEntryTo(entry, origParent)
|
||||
}
|
||||
|
||||
fun getUnusedCacheFileName(): String {
|
||||
fun buildNewBinary(cacheDirectory: File): BinaryAttachment {
|
||||
// Generate an unique new file with timestamp
|
||||
return System.currentTimeMillis().toString()
|
||||
}
|
||||
|
||||
fun buildNewAttachment(cacheDirectory: File, fileName: String): EntryAttachment? {
|
||||
val fileInCache = File(cacheDirectory, getUnusedCacheFileName())
|
||||
val binaryAttachment = BinaryAttachment(fileInCache)
|
||||
return EntryAttachment(fileName, binaryAttachment)
|
||||
val fileInCache = File(cacheDirectory, System.currentTimeMillis().toString())
|
||||
return BinaryAttachment(fileInCache)
|
||||
}
|
||||
|
||||
companion object {
|
||||
|
||||
@@ -32,6 +32,7 @@ import com.kunzisoft.keepass.crypto.keyDerivation.KdfParameters
|
||||
import com.kunzisoft.keepass.database.action.node.NodeHandler
|
||||
import com.kunzisoft.keepass.database.element.DateInstant
|
||||
import com.kunzisoft.keepass.database.element.DeletedObject
|
||||
import com.kunzisoft.keepass.database.element.EntryAttachment
|
||||
import com.kunzisoft.keepass.database.element.database.DatabaseKDB.Companion.BACKUP_FOLDER_TITLE
|
||||
import com.kunzisoft.keepass.database.element.entry.EntryKDBX
|
||||
import com.kunzisoft.keepass.database.element.group.GroupKDBX
|
||||
@@ -44,7 +45,6 @@ import com.kunzisoft.keepass.database.element.security.MemoryProtectionConfig
|
||||
import com.kunzisoft.keepass.database.exception.UnknownKDF
|
||||
import com.kunzisoft.keepass.database.file.DatabaseHeaderKDBX.Companion.FILE_VERSION_32_3
|
||||
import com.kunzisoft.keepass.database.file.DatabaseHeaderKDBX.Companion.FILE_VERSION_32_4
|
||||
import com.kunzisoft.keepass.database.element.EntryAttachment
|
||||
import com.kunzisoft.keepass.utils.UnsignedInt
|
||||
import com.kunzisoft.keepass.utils.VariantDictionary
|
||||
import org.w3c.dom.Node
|
||||
@@ -540,20 +540,18 @@ class DatabaseKDBX : DatabaseVersioned<UUID, UUID, GroupKDBX, EntryKDBX> {
|
||||
return publicCustomData.size() > 0
|
||||
}
|
||||
|
||||
// TODO encapsulate
|
||||
fun getUnusedCacheFileName(): String {
|
||||
return binaryPool.findUnusedKey().toString()
|
||||
}
|
||||
|
||||
fun buildNewAttachment(cacheDirectory: File, fileName: String): EntryAttachment {
|
||||
val cacheId = getUnusedCacheFileName()
|
||||
val fileInCache = File(cacheDirectory, cacheId)
|
||||
// TODO protection?
|
||||
val compression = compressionAlgorithm == CompressionAlgorithm.GZip
|
||||
val binaryAttachment = BinaryAttachment(fileInCache, false, compression)
|
||||
fun buildNewBinary(cacheDirectory: File,
|
||||
protection: Boolean = false,
|
||||
compression: Boolean? = null,
|
||||
cacheId: String? = null): BinaryAttachment {
|
||||
// Unused cache key if needed
|
||||
val binaryId = cacheId ?: binaryPool.findUnusedKey().toString()
|
||||
val fileInCache = File(cacheDirectory, binaryId)
|
||||
val compressionBinary = compression ?: (compressionAlgorithm == CompressionAlgorithm.GZip)
|
||||
val binaryAttachment = BinaryAttachment(fileInCache, protection, compressionBinary)
|
||||
// add attachment to pool
|
||||
binaryPool.put(cacheId.toInt(), binaryAttachment)
|
||||
return EntryAttachment(fileName, binaryAttachment)
|
||||
binaryPool.put(binaryId.toInt(), binaryAttachment)
|
||||
return binaryAttachment
|
||||
}
|
||||
|
||||
fun removeAttachmentIfNotUsed(attachment: EntryAttachment) {
|
||||
|
||||
@@ -27,7 +27,6 @@ import com.kunzisoft.keepass.database.element.entry.EntryKDB
|
||||
import com.kunzisoft.keepass.database.element.group.GroupKDB
|
||||
import com.kunzisoft.keepass.database.element.node.NodeIdInt
|
||||
import com.kunzisoft.keepass.database.element.node.NodeIdUUID
|
||||
import com.kunzisoft.keepass.database.element.security.BinaryAttachment
|
||||
import com.kunzisoft.keepass.database.element.security.EncryptionAlgorithm
|
||||
import com.kunzisoft.keepass.database.exception.*
|
||||
import com.kunzisoft.keepass.database.file.DatabaseHeader
|
||||
@@ -281,10 +280,9 @@ class DatabaseInputKDB(cacheDirectory: File,
|
||||
0x000E -> {
|
||||
newEntry?.let { entry ->
|
||||
if (fieldSize > 0) {
|
||||
val binaryFile = File(cacheDirectory,
|
||||
mDatabaseToOpen.getUnusedCacheFileName())
|
||||
entry.binaryData = BinaryAttachment(binaryFile)
|
||||
BufferedOutputStream(FileOutputStream(binaryFile)).use { outputStream ->
|
||||
val binaryAttachment = mDatabaseToOpen.buildNewBinary(cacheDirectory)
|
||||
entry.binaryData = binaryAttachment
|
||||
BufferedOutputStream(binaryAttachment.getOutputDataStream()).use { outputStream ->
|
||||
cipherInputStream.readBytes(fieldSize,
|
||||
DatabaseKDB.BUFFER_SIZE_BYTES) { buffer ->
|
||||
outputStream.write(buffer)
|
||||
|
||||
@@ -26,6 +26,7 @@ import com.kunzisoft.keepass.crypto.StreamCipherFactory
|
||||
import com.kunzisoft.keepass.crypto.engine.CipherEngine
|
||||
import com.kunzisoft.keepass.database.element.DateInstant
|
||||
import com.kunzisoft.keepass.database.element.DeletedObject
|
||||
import com.kunzisoft.keepass.database.element.EntryAttachment
|
||||
import com.kunzisoft.keepass.database.element.database.CompressionAlgorithm
|
||||
import com.kunzisoft.keepass.database.element.database.DatabaseKDBX
|
||||
import com.kunzisoft.keepass.database.element.database.DatabaseKDBX.Companion.BASE_64_FLAG
|
||||
@@ -41,7 +42,6 @@ import com.kunzisoft.keepass.database.exception.*
|
||||
import com.kunzisoft.keepass.database.file.DatabaseHeaderKDBX
|
||||
import com.kunzisoft.keepass.database.file.DatabaseKDBXXML
|
||||
import com.kunzisoft.keepass.database.file.DateKDBXUtil
|
||||
import com.kunzisoft.keepass.database.element.EntryAttachment
|
||||
import com.kunzisoft.keepass.stream.*
|
||||
import com.kunzisoft.keepass.tasks.ProgressTaskUpdater
|
||||
import com.kunzisoft.keepass.utils.UnsignedInt
|
||||
@@ -50,7 +50,10 @@ import org.bouncycastle.crypto.StreamCipher
|
||||
import org.xmlpull.v1.XmlPullParser
|
||||
import org.xmlpull.v1.XmlPullParserException
|
||||
import org.xmlpull.v1.XmlPullParserFactory
|
||||
import java.io.*
|
||||
import java.io.File
|
||||
import java.io.IOException
|
||||
import java.io.InputStream
|
||||
import java.io.UnsupportedEncodingException
|
||||
import java.nio.charset.Charset
|
||||
import java.text.ParseException
|
||||
import java.util.*
|
||||
@@ -247,18 +250,15 @@ class DatabaseInputKDBX(cacheDirectory: File,
|
||||
header.innerRandomStreamKey = data
|
||||
}
|
||||
DatabaseHeaderKDBX.PwDbInnerHeaderV4Fields.Binary -> {
|
||||
val protectedFlag = dataInputStream.readBytes(1)[0].toInt() != 0
|
||||
val byteLength = size - 1
|
||||
// Read in a file
|
||||
val cacheId = mDatabase.getUnusedCacheFileName()
|
||||
val fileInCache = File(cacheDirectory, cacheId)
|
||||
FileOutputStream(fileInCache).use { outputStream ->
|
||||
val protectedFlag = dataInputStream.readBytes(1)[0].toInt() != 0
|
||||
val protectedBinary = mDatabase.buildNewBinary(cacheDirectory, protectedFlag)
|
||||
protectedBinary.getOutputDataStream().use { outputStream ->
|
||||
dataInputStream.readBytes(byteLength, DatabaseKDBX.BUFFER_SIZE_BYTES) { buffer ->
|
||||
outputStream.write(buffer)
|
||||
}
|
||||
}
|
||||
val protectedBinary = BinaryAttachment(fileInCache, protectedFlag)
|
||||
mDatabase.binaryPool.put(cacheId.toInt(), protectedBinary)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -946,28 +946,18 @@ class DatabaseInputKDBX(cacheDirectory: File,
|
||||
}
|
||||
|
||||
val key = xpp.getAttributeValue(null, DatabaseKDBXXML.AttrId)
|
||||
if (key != null) {
|
||||
val binaryId = Integer.parseInt(key)
|
||||
createBinary(binaryId.toString(), xpp)?.let {
|
||||
mDatabase.binaryPool.put(binaryId, it)
|
||||
return it
|
||||
}
|
||||
return if (key != null) {
|
||||
createBinary(key, xpp)
|
||||
}
|
||||
|
||||
// New binary to retrieve
|
||||
else {
|
||||
val binaryId = mDatabase.getUnusedCacheFileName()
|
||||
createBinary(binaryId, xpp)?.let {
|
||||
mDatabase.binaryPool.put(binaryId.toInt(), it)
|
||||
return it
|
||||
}
|
||||
createBinary(null, xpp)
|
||||
}
|
||||
|
||||
return null
|
||||
}
|
||||
|
||||
@Throws(IOException::class, XmlPullParserException::class)
|
||||
private fun createBinary(binaryId: String, xpp: XmlPullParser): BinaryAttachment? {
|
||||
private fun createBinary(binaryId: String?, xpp: XmlPullParser): BinaryAttachment? {
|
||||
var compressed = false
|
||||
var protected = false
|
||||
|
||||
@@ -988,18 +978,23 @@ class DatabaseInputKDBX(cacheDirectory: File,
|
||||
return null
|
||||
val data = Base64.decode(base64, BASE_64_FLAG)
|
||||
|
||||
val file = File(cacheDirectory, binaryId)
|
||||
return FileOutputStream(file).use { outputStream ->
|
||||
val effectivelyCompressed = if (mDatabase.compressionAlgorithm == CompressionAlgorithm.GZip
|
||||
&& !compressed) {
|
||||
true
|
||||
} else {
|
||||
compressed
|
||||
}
|
||||
|
||||
val binaryAttachment = mDatabase.buildNewBinary(cacheDirectory, protected, effectivelyCompressed, binaryId)
|
||||
binaryAttachment.getOutputDataStream().use { outputStream ->
|
||||
// Force compression in this specific case
|
||||
if (mDatabase.compressionAlgorithm == CompressionAlgorithm.GZip
|
||||
&& !compressed) {
|
||||
if (effectivelyCompressed) {
|
||||
GZIPOutputStream(outputStream).write(data)
|
||||
BinaryAttachment(file, protected, true)
|
||||
} else {
|
||||
outputStream.write(data)
|
||||
BinaryAttachment(file, protected, compressed)
|
||||
}
|
||||
}
|
||||
return binaryAttachment
|
||||
}
|
||||
|
||||
@Throws(IOException::class, XmlPullParserException::class)
|
||||
|
||||
@@ -1,59 +0,0 @@
|
||||
/*
|
||||
* Copyright 2017 Brian Pellin, Jeremy Jamet / Kunzisoft.
|
||||
*
|
||||
* This file is part of KeePassDX.
|
||||
*
|
||||
* KeePassDX is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* KeePassDX is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with KeePassDX. If not, see <http://www.gnu.org/licenses/>.
|
||||
*
|
||||
*/
|
||||
package com.kunzisoft.keepass.stream
|
||||
|
||||
import java.io.IOException
|
||||
import java.io.OutputStream
|
||||
import java.io.RandomAccessFile
|
||||
|
||||
class RandomFileOutputStream internal constructor(private val mFile: RandomAccessFile) : OutputStream() {
|
||||
|
||||
@Throws(IOException::class)
|
||||
override fun close() {
|
||||
super.close()
|
||||
|
||||
mFile.close()
|
||||
}
|
||||
|
||||
@Throws(IOException::class)
|
||||
override fun write(buffer: ByteArray, offset: Int, count: Int) {
|
||||
super.write(buffer, offset, count)
|
||||
|
||||
mFile.write(buffer, offset, count)
|
||||
}
|
||||
|
||||
@Throws(IOException::class)
|
||||
override fun write(buffer: ByteArray) {
|
||||
super.write(buffer)
|
||||
|
||||
mFile.write(buffer)
|
||||
}
|
||||
|
||||
@Throws(IOException::class)
|
||||
override fun write(oneByte: Int) {
|
||||
mFile.write(oneByte)
|
||||
}
|
||||
|
||||
@Throws(IOException::class)
|
||||
fun seek(pos: Long) {
|
||||
mFile.seek(pos)
|
||||
}
|
||||
|
||||
}
|
||||
Reference in new issue
Block a user