mirror of
https://github.com/Kunzisoft/KeePassDX.git
synced 2026-11-30 22:02:57 -08:00
Add custom data objects
This commit is contained in:
1 parent
80c4a3c06d
commit
23e899042d
10 files changed
+182
-42
No files matched your search
@@ -0,0 +1,66 @@
|
||||
package com.kunzisoft.keepass.database.element
|
||||
|
||||
import android.os.Parcel
|
||||
import android.os.Parcelable
|
||||
import com.kunzisoft.keepass.utils.ParcelableUtil
|
||||
import java.util.*
|
||||
|
||||
class CustomData : Parcelable {
|
||||
|
||||
private val mCustomDataItems = HashMap<String, CustomDataItem>()
|
||||
|
||||
constructor()
|
||||
|
||||
constructor(toCopy: CustomData) {
|
||||
mCustomDataItems.clear()
|
||||
mCustomDataItems.putAll(toCopy.mCustomDataItems)
|
||||
}
|
||||
|
||||
constructor(parcel: Parcel) {
|
||||
ParcelableUtil.readStringParcelableMap(parcel, CustomData::class.java)
|
||||
}
|
||||
|
||||
fun get(key: String): CustomDataItem? {
|
||||
return mCustomDataItems[key]
|
||||
}
|
||||
|
||||
fun put(customDataItem: CustomDataItem) {
|
||||
mCustomDataItems[customDataItem.key] = customDataItem
|
||||
}
|
||||
|
||||
fun containsItemWithValue(value: String): Boolean {
|
||||
return mCustomDataItems.any { mapEntry -> mapEntry.value.value.equals(value, true) }
|
||||
}
|
||||
|
||||
fun containsItemWithLastModificationTime(): Boolean {
|
||||
return mCustomDataItems.any { mapEntry -> mapEntry.value.lastModificationTime != null }
|
||||
}
|
||||
|
||||
fun isNotEmpty(): Boolean {
|
||||
return mCustomDataItems.isNotEmpty()
|
||||
}
|
||||
|
||||
fun doForEachItems(action: (CustomDataItem) -> Unit) {
|
||||
for ((_, value) in mCustomDataItems) {
|
||||
action.invoke(value)
|
||||
}
|
||||
}
|
||||
|
||||
override fun writeToParcel(parcel: Parcel, flags: Int) {
|
||||
ParcelableUtil.writeStringParcelableMap(parcel, flags, mCustomDataItems)
|
||||
}
|
||||
|
||||
override fun describeContents(): Int {
|
||||
return 0
|
||||
}
|
||||
|
||||
companion object CREATOR : Parcelable.Creator<CustomData> {
|
||||
override fun createFromParcel(parcel: Parcel): CustomData {
|
||||
return CustomData(parcel)
|
||||
}
|
||||
|
||||
override fun newArray(size: Int): Array<CustomData?> {
|
||||
return arrayOfNulls(size)
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,43 @@
|
||||
package com.kunzisoft.keepass.database.element
|
||||
|
||||
import android.os.Parcel
|
||||
import android.os.Parcelable
|
||||
|
||||
class CustomDataItem : Parcelable {
|
||||
|
||||
val key: String
|
||||
var value: String
|
||||
var lastModificationTime: DateInstant? = null
|
||||
|
||||
constructor(parcel: Parcel) {
|
||||
key = parcel.readString() ?: ""
|
||||
value = parcel.readString() ?: ""
|
||||
lastModificationTime = parcel.readParcelable(DateInstant::class.java.classLoader)
|
||||
}
|
||||
|
||||
constructor(key: String, value: String, lastModificationTime: DateInstant? = null) {
|
||||
this.key = key
|
||||
this.value = value
|
||||
this.lastModificationTime = lastModificationTime
|
||||
}
|
||||
|
||||
override fun writeToParcel(parcel: Parcel, flags: Int) {
|
||||
parcel.writeString(key)
|
||||
parcel.writeString(value)
|
||||
parcel.writeParcelable(lastModificationTime, flags)
|
||||
}
|
||||
|
||||
override fun describeContents(): Int {
|
||||
return 0
|
||||
}
|
||||
|
||||
companion object CREATOR : Parcelable.Creator<CustomDataItem> {
|
||||
override fun createFromParcel(parcel: Parcel): CustomDataItem {
|
||||
return CustomDataItem(parcel)
|
||||
}
|
||||
|
||||
override fun newArray(size: Int): Array<CustomDataItem?> {
|
||||
return arrayOfNulls(size)
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -32,6 +32,8 @@ import com.kunzisoft.keepass.database.crypto.VariantDictionary
|
||||
import com.kunzisoft.keepass.database.crypto.kdf.KdfEngine
|
||||
import com.kunzisoft.keepass.database.crypto.kdf.KdfFactory
|
||||
import com.kunzisoft.keepass.database.crypto.kdf.KdfParameters
|
||||
import com.kunzisoft.keepass.database.element.CustomData
|
||||
import com.kunzisoft.keepass.database.element.CustomDataItem
|
||||
import com.kunzisoft.keepass.database.element.DateInstant
|
||||
import com.kunzisoft.keepass.database.element.DeletedObject
|
||||
import com.kunzisoft.keepass.database.element.binary.BinaryData
|
||||
@@ -112,7 +114,7 @@ class DatabaseKDBX : DatabaseVersioned<UUID, UUID, GroupKDBX, EntryKDBX> {
|
||||
var lastTopVisibleGroupUUID = UUID_ZERO
|
||||
var memoryProtection = MemoryProtectionConfig()
|
||||
val deletedObjects = ArrayList<DeletedObject>()
|
||||
val customData = HashMap<String, String>()
|
||||
val customData = CustomData()
|
||||
|
||||
var localizedAppName = "KeePassDX"
|
||||
|
||||
@@ -331,17 +333,20 @@ class DatabaseKDBX : DatabaseVersioned<UUID, UUID, GroupKDBX, EntryKDBX> {
|
||||
return this.iconsManager.getIcon(iconUuid)
|
||||
}
|
||||
|
||||
fun putCustomData(label: String, value: String) {
|
||||
this.customData[label] = value
|
||||
fun putCustomData(label: String, value: String, lastModificationTime: DateInstant?) {
|
||||
this.customData.put(CustomDataItem(label, value, lastModificationTime))
|
||||
}
|
||||
|
||||
override fun containsCustomData(): Boolean {
|
||||
return customData.isNotEmpty()
|
||||
}
|
||||
|
||||
/**
|
||||
* To perform a search in entry custom data
|
||||
*/
|
||||
fun getEntryByCustomData(customDataValue: String): EntryKDBX? {
|
||||
return entryIndexes.values.find { entry ->
|
||||
entry.customData.containsValue(customDataValue)
|
||||
entry.customData.containsItemWithValue(customDataValue)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -23,6 +23,8 @@ import android.os.Parcel
|
||||
import android.os.Parcelable
|
||||
import com.kunzisoft.keepass.utils.UnsignedLong
|
||||
import com.kunzisoft.keepass.database.element.Attachment
|
||||
import com.kunzisoft.keepass.database.element.CustomData
|
||||
import com.kunzisoft.keepass.database.element.CustomDataItem
|
||||
import com.kunzisoft.keepass.database.element.DateInstant
|
||||
import com.kunzisoft.keepass.database.element.binary.AttachmentPool
|
||||
import com.kunzisoft.keepass.database.element.database.DatabaseKDBX
|
||||
@@ -45,7 +47,7 @@ class EntryKDBX : EntryVersioned<UUID, UUID, GroupKDBX, EntryKDBX>, NodeKDBXInte
|
||||
@Transient
|
||||
private var mDecodeRef = false
|
||||
|
||||
var customData = LinkedHashMap<String, String>()
|
||||
var customData = CustomData()
|
||||
var fields = LinkedHashMap<String, ProtectedString>()
|
||||
var binaries = LinkedHashMap<String, Int>() // Map<Label, PoolId>
|
||||
var foregroundColor = ""
|
||||
@@ -63,7 +65,7 @@ class EntryKDBX : EntryVersioned<UUID, UUID, GroupKDBX, EntryKDBX>, NodeKDBXInte
|
||||
constructor(parcel: Parcel) : super(parcel) {
|
||||
usageCount = UnsignedLong(parcel.readLong())
|
||||
locationChanged = parcel.readParcelable(DateInstant::class.java.classLoader) ?: locationChanged
|
||||
customData = ParcelableUtil.readStringParcelableMap(parcel)
|
||||
customData = parcel.readParcelable(CustomData::class.java.classLoader) ?: CustomData()
|
||||
fields = ParcelableUtil.readStringParcelableMap(parcel, ProtectedString::class.java)
|
||||
binaries = ParcelableUtil.readStringIntMap(parcel)
|
||||
foregroundColor = parcel.readString() ?: foregroundColor
|
||||
@@ -87,7 +89,7 @@ class EntryKDBX : EntryVersioned<UUID, UUID, GroupKDBX, EntryKDBX>, NodeKDBXInte
|
||||
super.writeToParcel(dest, flags)
|
||||
dest.writeLong(usageCount.toKotlinLong())
|
||||
dest.writeParcelable(locationChanged, flags)
|
||||
ParcelableUtil.writeStringParcelableMap(dest, customData)
|
||||
dest.writeParcelable(customData, flags)
|
||||
ParcelableUtil.writeStringParcelableMap(dest, flags, fields)
|
||||
ParcelableUtil.writeStringIntMap(dest, binaries)
|
||||
dest.writeString(foregroundColor)
|
||||
@@ -107,9 +109,7 @@ class EntryKDBX : EntryVersioned<UUID, UUID, GroupKDBX, EntryKDBX>, NodeKDBXInte
|
||||
super.updateWith(source)
|
||||
usageCount = source.usageCount
|
||||
locationChanged = DateInstant(source.locationChanged)
|
||||
// Add all custom elements in map
|
||||
customData.clear()
|
||||
customData.putAll(source.customData)
|
||||
customData = CustomData(source.customData)
|
||||
fields.clear()
|
||||
fields.putAll(source.fields)
|
||||
binaries.clear()
|
||||
@@ -320,13 +320,17 @@ class EntryKDBX : EntryVersioned<UUID, UUID, GroupKDBX, EntryKDBX>, NodeKDBXInte
|
||||
}
|
||||
|
||||
override fun putCustomData(key: String, value: String) {
|
||||
customData[key] = value
|
||||
customData.put(CustomDataItem(key, value))
|
||||
}
|
||||
|
||||
override fun containsCustomData(): Boolean {
|
||||
return customData.isNotEmpty()
|
||||
}
|
||||
|
||||
override fun containsCustomDataWithLastModificationTime(): Boolean {
|
||||
return customData.containsItemWithLastModificationTime()
|
||||
}
|
||||
|
||||
override fun containsCustomIconWithNameOrLastModificationTime(): Boolean {
|
||||
return icon.custom.name.isNotEmpty() || icon.custom.lastModificationTime != null
|
||||
}
|
||||
|
||||
@@ -21,6 +21,8 @@ package com.kunzisoft.keepass.database.element.group
|
||||
|
||||
import android.os.Parcel
|
||||
import android.os.Parcelable
|
||||
import com.kunzisoft.keepass.database.element.CustomData
|
||||
import com.kunzisoft.keepass.database.element.CustomDataItem
|
||||
import com.kunzisoft.keepass.database.element.DateInstant
|
||||
import com.kunzisoft.keepass.database.element.database.DatabaseVersioned
|
||||
import com.kunzisoft.keepass.database.element.entry.EntryKDBX
|
||||
@@ -33,9 +35,8 @@ import java.util.*
|
||||
|
||||
class GroupKDBX : GroupVersioned<UUID, UUID, GroupKDBX, EntryKDBX>, NodeKDBXInterface {
|
||||
|
||||
private val customData = HashMap<String, String>()
|
||||
var customData = CustomData()
|
||||
var notes = ""
|
||||
|
||||
var isExpanded = true
|
||||
var defaultAutoTypeSequence = ""
|
||||
var enableAutoType: Boolean? = null
|
||||
@@ -60,7 +61,7 @@ class GroupKDBX : GroupVersioned<UUID, UUID, GroupKDBX, EntryKDBX>, NodeKDBXInte
|
||||
constructor(parcel: Parcel) : super(parcel) {
|
||||
usageCount = UnsignedLong(parcel.readLong())
|
||||
locationChanged = parcel.readParcelable(DateInstant::class.java.classLoader) ?: locationChanged
|
||||
// TODO customData = ParcelableUtil.readStringParcelableMap(parcel);
|
||||
customData = parcel.readParcelable(CustomData::class.java.classLoader) ?: CustomData()
|
||||
notes = parcel.readString() ?: notes
|
||||
isExpanded = parcel.readByte().toInt() != 0
|
||||
defaultAutoTypeSequence = parcel.readString() ?: defaultAutoTypeSequence
|
||||
@@ -83,7 +84,7 @@ class GroupKDBX : GroupVersioned<UUID, UUID, GroupKDBX, EntryKDBX>, NodeKDBXInte
|
||||
super.writeToParcel(dest, flags)
|
||||
dest.writeLong(usageCount.toKotlinLong())
|
||||
dest.writeParcelable(locationChanged, flags)
|
||||
// TODO ParcelableUtil.writeStringParcelableMap(dest, customData);
|
||||
dest.writeParcelable(customData, flags)
|
||||
dest.writeString(notes)
|
||||
dest.writeByte((if (isExpanded) 1 else 0).toByte())
|
||||
dest.writeString(defaultAutoTypeSequence)
|
||||
@@ -97,10 +98,7 @@ class GroupKDBX : GroupVersioned<UUID, UUID, GroupKDBX, EntryKDBX>, NodeKDBXInte
|
||||
usageCount = source.usageCount
|
||||
locationChanged = DateInstant(source.locationChanged)
|
||||
// Add all custom elements in map
|
||||
customData.clear()
|
||||
for ((key, value) in source.customData) {
|
||||
customData[key] = value
|
||||
}
|
||||
customData = CustomData(source.customData)
|
||||
notes = source.notes
|
||||
isExpanded = source.isExpanded
|
||||
defaultAutoTypeSequence = source.defaultAutoTypeSequence
|
||||
@@ -118,13 +116,17 @@ class GroupKDBX : GroupVersioned<UUID, UUID, GroupKDBX, EntryKDBX>, NodeKDBXInte
|
||||
}
|
||||
|
||||
override fun putCustomData(key: String, value: String) {
|
||||
customData[key] = value
|
||||
customData.put(CustomDataItem(key, value))
|
||||
}
|
||||
|
||||
override fun containsCustomData(): Boolean {
|
||||
return customData.isNotEmpty()
|
||||
}
|
||||
|
||||
override fun containsCustomDataWithLastModificationTime(): Boolean {
|
||||
return customData.containsItemWithLastModificationTime()
|
||||
}
|
||||
|
||||
override fun containsCustomIconWithNameOrLastModificationTime(): Boolean {
|
||||
return icon.custom.name.isNotEmpty() || icon.custom.lastModificationTime != null
|
||||
}
|
||||
|
||||
@@ -29,8 +29,8 @@ interface NodeKDBXInterface : NodeTimeInterface {
|
||||
var locationChanged: DateInstant
|
||||
|
||||
fun putCustomData(key: String, value: String)
|
||||
|
||||
fun containsCustomData(): Boolean
|
||||
fun containsCustomDataWithLastModificationTime(): Boolean
|
||||
|
||||
fun containsCustomIconWithNameOrLastModificationTime() : Boolean
|
||||
|
||||
|
||||
@@ -101,7 +101,9 @@ class DatabaseHeaderKDBX(private val databaseV4: DatabaseKDBX) : DatabaseHeader(
|
||||
}
|
||||
if (node.containsCustomData()) {
|
||||
containsCustomData = true
|
||||
// TODO Data modification time
|
||||
if (node.containsCustomDataWithLastModificationTime()) {
|
||||
containsCustomDataWithLastModificationTime = true
|
||||
}
|
||||
}
|
||||
return true
|
||||
}
|
||||
@@ -142,7 +144,7 @@ class DatabaseHeaderKDBX(private val databaseV4: DatabaseKDBX) : DatabaseHeader(
|
||||
// https://keepass.info/help/kb/kdbx_4.html
|
||||
// If AES is not use, it's at least 4.0
|
||||
val kdfIsNotAes = databaseKDBX.kdfParameters?.uuid != AesKdf.CIPHER_UUID
|
||||
val containsHeaderCustomData = databaseKDBX.containsCustomData()
|
||||
val containsHeaderCustomData = databaseKDBX.customData.isNotEmpty()
|
||||
val containsNodeCustomData = entryHandler.containsCustomData || groupHandler.containsCustomData
|
||||
|
||||
// Check each condition to determine version
|
||||
|
||||
@@ -94,6 +94,7 @@ class DatabaseInputKDBX(cacheDirectory: File,
|
||||
private var customIconData: ByteArray? = null
|
||||
private var customDataKey: String? = null
|
||||
private var customDataValue: String? = null
|
||||
private var customDataLastModificationTime: DateInstant? = null
|
||||
private var groupCustomDataKey: String? = null
|
||||
private var groupCustomDataValue: String? = null
|
||||
private var entryCustomDataKey: String? = null
|
||||
@@ -495,6 +496,8 @@ class DatabaseInputKDBX(cacheDirectory: File,
|
||||
customDataKey = readString(xpp)
|
||||
} else if (name.equals(DatabaseKDBXXML.ElemValue, ignoreCase = true)) {
|
||||
customDataValue = readString(xpp)
|
||||
} else if (name.equals(DatabaseKDBXXML.ElemLastModTime, ignoreCase = true)) {
|
||||
customDataLastModificationTime = readPwTime(xpp)
|
||||
} else {
|
||||
readUnknown(xpp)
|
||||
}
|
||||
@@ -744,13 +747,14 @@ class DatabaseInputKDBX(cacheDirectory: File,
|
||||
} else if (ctx == KdbContext.CustomData && name.equals(DatabaseKDBXXML.ElemCustomData, ignoreCase = true)) {
|
||||
return KdbContext.Meta
|
||||
} else if (ctx == KdbContext.CustomDataItem && name.equals(DatabaseKDBXXML.ElemStringDictExItem, ignoreCase = true)) {
|
||||
if (customDataKey != null && customDataValue != null) {
|
||||
mDatabase.putCustomData(customDataKey!!, customDataValue!!)
|
||||
customDataKey?.let { dataKey ->
|
||||
customDataValue?.let { dataValue ->
|
||||
mDatabase.putCustomData(dataKey, dataValue, customDataLastModificationTime)
|
||||
}
|
||||
}
|
||||
|
||||
customDataKey = null
|
||||
customDataValue = null
|
||||
|
||||
customDataLastModificationTime = null
|
||||
return KdbContext.CustomData
|
||||
} else if (ctx == KdbContext.Group && name.equals(DatabaseKDBXXML.ElemGroup, ignoreCase = true)) {
|
||||
if (ctxGroup != null && ctxGroup?.id == DatabaseVersioned.UUID_ZERO) {
|
||||
|
||||
+28
-14
@@ -28,6 +28,8 @@ import com.kunzisoft.keepass.database.action.node.NodeHandler
|
||||
import com.kunzisoft.keepass.database.crypto.CipherEngine
|
||||
import com.kunzisoft.keepass.database.crypto.EncryptionAlgorithm
|
||||
import com.kunzisoft.keepass.database.crypto.kdf.KdfFactory
|
||||
import com.kunzisoft.keepass.database.element.CustomData
|
||||
import com.kunzisoft.keepass.database.element.CustomDataItem
|
||||
import com.kunzisoft.keepass.database.element.DeletedObject
|
||||
import com.kunzisoft.keepass.database.element.Tags
|
||||
import com.kunzisoft.keepass.database.element.database.CompressionAlgorithm
|
||||
@@ -400,9 +402,7 @@ class DatabaseOutputKDBX(private val mDatabaseKDBX: DatabaseKDBX,
|
||||
writeTimes(entry)
|
||||
writeFields(entry.fields)
|
||||
writeEntryBinaries(entry.binaries)
|
||||
if (entry.containsCustomData()) {
|
||||
writeCustomData(entry.customData)
|
||||
}
|
||||
writeCustomData(entry.customData)
|
||||
writeAutoType(entry.autoType)
|
||||
|
||||
if (!isHistory) {
|
||||
@@ -647,20 +647,34 @@ class DatabaseOutputKDBX(private val mDatabaseKDBX: DatabaseKDBX,
|
||||
}
|
||||
|
||||
@Throws(IllegalArgumentException::class, IllegalStateException::class, IOException::class)
|
||||
private fun writeCustomData(customData: Map<String, String>) {
|
||||
xml.startTag(null, DatabaseKDBXXML.ElemCustomData)
|
||||
private fun writeCustomData(customData: CustomData) {
|
||||
if (customData.isNotEmpty()) {
|
||||
xml.startTag(null, DatabaseKDBXXML.ElemCustomData)
|
||||
|
||||
for ((key, value) in customData) {
|
||||
writeObject(
|
||||
DatabaseKDBXXML.ElemStringDictExItem,
|
||||
DatabaseKDBXXML.ElemKey,
|
||||
key,
|
||||
DatabaseKDBXXML.ElemValue,
|
||||
value
|
||||
)
|
||||
customData.doForEachItems { customDataItem ->
|
||||
writeCustomDataItem(customDataItem)
|
||||
}
|
||||
|
||||
xml.endTag(null, DatabaseKDBXXML.ElemCustomData)
|
||||
}
|
||||
}
|
||||
|
||||
private fun writeCustomDataItem(customDataItem: CustomDataItem) {
|
||||
xml.startTag(null, DatabaseKDBXXML.ElemStringDictExItem)
|
||||
|
||||
xml.startTag(null, DatabaseKDBXXML.ElemKey)
|
||||
xml.text(safeXmlString(customDataItem.key))
|
||||
xml.endTag(null, DatabaseKDBXXML.ElemKey)
|
||||
|
||||
xml.startTag(null, DatabaseKDBXXML.ElemValue)
|
||||
xml.text(safeXmlString(customDataItem.value))
|
||||
xml.endTag(null, DatabaseKDBXXML.ElemValue)
|
||||
|
||||
customDataItem.lastModificationTime?.let { lastModificationTime ->
|
||||
writeObject(DatabaseKDBXXML.ElemLastAccessTime, lastModificationTime.date)
|
||||
}
|
||||
|
||||
xml.endTag(null, DatabaseKDBXXML.ElemCustomData)
|
||||
xml.endTag(null, DatabaseKDBXXML.ElemStringDictExItem)
|
||||
}
|
||||
|
||||
@Throws(IllegalArgumentException::class, IllegalStateException::class, IOException::class)
|
||||
|
||||
@@ -52,7 +52,7 @@ object ParcelableUtil {
|
||||
|
||||
// For writing map with string key to a Parcel
|
||||
fun <V : Parcelable> writeStringParcelableMap(
|
||||
parcel: Parcel, flags: Int, map: LinkedHashMap<String, V>) {
|
||||
parcel: Parcel, flags: Int, map: HashMap<String, V>) {
|
||||
parcel.writeInt(map.size)
|
||||
for ((key, value) in map) {
|
||||
parcel.writeString(key)
|
||||
|
||||
Reference in new issue
Block a user