/* * Copyright 2017 Brian Pellin, Jeremy Jamet / Kunzisoft. * * This file is part of KeePass DX. * * KeePass DX 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. * * KeePass DX 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 KeePass DX. If not, see . * This file was derived from Copyright 2007 Naomaru Itoi This file was derived from Java clone of KeePass - A KeePass file viewer for Java Copyright 2006 Bill Zwicky This program 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; version 2 This program 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 this program; if not, write to the Free Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA */ package com.kunzisoft.keepass.database; import android.os.Parcel; import java.io.UnsupportedEncodingException; import java.util.UUID; /** * Structure containing information about one entry. * *
 * One entry: [FIELDTYPE(FT)][FIELDSIZE(FS)][FIELDDATA(FD)]
 *            [FT+FS+(FD)][FT+FS+(FD)][FT+FS+(FD)][FT+FS+(FD)][FT+FS+(FD)]...
 *            
 * [ 2 bytes] FIELDTYPE
 * [ 4 bytes] FIELDSIZE, size of FIELDDATA in bytes
 * [ n bytes] FIELDDATA, n = FIELDSIZE
 * 
 * Notes:
 *  - Strings are stored in UTF-8 encoded form and are null-terminated.
 *  - FIELDTYPE can be one of the FT_ constants.
 * 
* * @author Naomaru Itoi * @author Bill Zwicky * @author Dominik Reichl * @author Jeremy Jamet */ public class PwEntryV3 extends PwEntry { /** Size of byte buffer needed to hold this struct. */ private static final String PMS_ID_BINDESC = "bin-stream"; private static final String PMS_ID_TITLE = "Meta-Info"; private static final String PMS_ID_USER = "SYSTEM"; private static final String PMS_ID_URL = "$"; // TODO Parent ID to remove private int groupId; private String title; private String username; private byte[] password; private String url; private String additional; /** A string describing what is in pBinaryData */ private String binaryDesc; private byte[] binaryData; public PwEntryV3() { super(); } public PwEntryV3(PwGroupV3 p) { construct(p); groupId = ((PwGroupIdV3) this.parent.getId()).getId(); // TODO remove } public PwEntryV3(Parcel in) { super(in); groupId = in.readInt(); title = in.readString(); username = in.readString(); in.readByteArray(password); url = in.readString(); additional = in.readString(); binaryDesc = in.readString(); in.readByteArray(binaryData); } @Override public void writeToParcel(Parcel dest, int flags) { super.writeToParcel(dest, flags); dest.writeInt(groupId); dest.writeString(title); dest.writeString(username); dest.writeByteArray(password); dest.writeString(url); dest.writeString(additional); dest.writeString(binaryDesc); dest.writeByteArray(binaryData); } public static final Creator CREATOR = new Creator() { @Override public PwEntryV3 createFromParcel(Parcel in) { return new PwEntryV3(in); } @Override public PwEntryV3[] newArray(int size) { return new PwEntryV3[size]; } }; protected void updateWith(PwEntryV3 source) { super.assign(source); groupId = source.groupId; title = source.title; username = source.username; int passLen = source.password.length; password = new byte[passLen]; System.arraycopy(source.password, 0, password, 0, passLen); url = source.url; additional = source.additional; binaryDesc = source.binaryDesc; if ( source.binaryData != null ) { int descLen = source.binaryData.length; binaryData = new byte[descLen]; System.arraycopy(source.binaryData, 0, binaryData, 0, descLen); } } @Override public PwEntryV3 clone() { // Attributes in parent PwEntryV3 newEntry = (PwEntryV3) super.clone(); // Attributes here // newEntry.parent stay the same in copy // newEntry.groupId stay the same in copy // newEntry.title stay the same in copy // newEntry.username stay the same in copy if (password != null) { int passLen = password.length; password = new byte[passLen]; System.arraycopy(password, 0, newEntry.password, 0, passLen); } // newEntry.url stay the same in copy // newEntry.additional stay the same in copy // newEntry.binaryDesc stay the same in copy if ( binaryData != null ) { int descLen = binaryData.length; newEntry.binaryData = new byte[descLen]; System.arraycopy(binaryData, 0, newEntry.binaryData, 0, descLen); } return newEntry; } public int getGroupId() { return groupId; } public void setGroupId(int groupId) { this.groupId = groupId; } @Override public String getUsername() { if (username == null) { return ""; } return username; } @Override public void setUsername(String user) { username = user; } @Override public String getTitle() { return title; } @Override public void setTitle(String title) { this.title = title; } @Override public String getNotes() { return additional; } @Override public void setNotes(String notes) { additional = notes; } @Override public String getUrl() { return url; } @Override public void setUrl(String url) { this.url = url; } public void populateBlankFields(PwDatabaseV3 db) { // TODO verify and remove if (icon == null) { icon = db.getIconFactory().getKeyIcon(); } if (username == null) { username = ""; } if (password == null) { password = new byte[0]; } if (uuid == null) { uuid = UUID.randomUUID(); } if (title == null) { title = ""; } if (url == null) { url = ""; } if (additional == null) { additional = ""; } if (binaryDesc == null) { binaryDesc = ""; } if (binaryData == null) { binaryData = new byte[0]; } } /** * @return the actual password byte array. */ @Override public String getPassword() { if (password == null) { return ""; } return new String(password); } public byte[] getPasswordBytes() { return password; } /** * fill byte array */ private static void fill(byte[] array, byte value) { for (int i=0; i