From 46e042f6e25e6d80219bc11bef6709aec06b8db3 Mon Sep 17 00:00:00 2001 From: Brian Pellin Date: Fri, 18 Dec 2009 23:41:43 -0600 Subject: [PATCH] Implement lazy date conversion. --- src/com/keepassdroid/Database.java | 10 +- src/com/keepassdroid/EntryActivity.java | 11 +- src/com/keepassdroid/EntryEditActivity.java | 5 +- src/com/keepassdroid/keepasslib/PwDate.java | 165 ++++++++++++++++++ .../keepasslib/PwEntryOutput.java | 13 +- .../keepasslib/PwGroupOutput.java | 13 +- .../keepasslib/PwManagerOutput.java | 12 +- src/org/phoneid/keepassj2me/ImporterV3.java | 25 ++- src/org/phoneid/keepassj2me/PwEntry.java | 25 +-- src/org/phoneid/keepassj2me/PwGroup.java | 10 +- src/org/phoneid/keepassj2me/PwManager.java | 9 +- src/org/phoneid/keepassj2me/Types.java | 63 ------- .../com/keepassdroid/tests/PwEntryTest.java | 2 +- .../src/com/keepassdroid/tests/TypesTest.java | 10 +- .../tests/output/PwManagerOutputTest.java | 13 +- 15 files changed, 238 insertions(+), 148 deletions(-) create mode 100644 src/com/keepassdroid/keepasslib/PwDate.java diff --git a/src/com/keepassdroid/Database.java b/src/com/keepassdroid/Database.java index 3cecc99d4..c69873149 100644 --- a/src/com/keepassdroid/Database.java +++ b/src/com/keepassdroid/Database.java @@ -38,7 +38,6 @@ import org.phoneid.keepassj2me.Types; import android.content.Context; -import com.keepassdroid.app.App; import com.keepassdroid.keepasslib.InvalidKeyFileException; import com.keepassdroid.keepasslib.InvalidPasswordException; import com.keepassdroid.keepasslib.PwManagerOutput; @@ -94,18 +93,13 @@ public class Database { } public void LoadData(Context ctx, InputStream is, String password, String keyfile, UpdateStatus status, boolean debug) throws IOException, InvalidKeyFileException, InvalidPasswordException { - ImporterV3 Importer = new ImporterV3(App.getCalendar(), debug); + ImporterV3 Importer = new ImporterV3(debug); - //Debug.startMethodTracing("load"); mPM = Importer.openDatabase(is, password, keyfile, status); if ( mPM != null ) { mPM.constructTree(null); populateGlobals(null); } - - //status.updateMessage(R.string.building_search_idx); - //buildSearchIndex(); - //Debug.stopMethodTracing(); loaded = true; } @@ -151,7 +145,7 @@ public class Database { //BufferedOutputStream bos = new BufferedOutputStream(fos); //PwManagerOutput pmo = new PwManagerOutput(mPM, bos, App.getCalendar()); - PwManagerOutput pmo = new PwManagerOutput(mPM, fos, App.getCalendar()); + PwManagerOutput pmo = new PwManagerOutput(mPM, fos); pmo.output(); //bos.flush(); //bos.close(); diff --git a/src/com/keepassdroid/EntryActivity.java b/src/com/keepassdroid/EntryActivity.java index 2a87ecb5f..167cde91e 100644 --- a/src/com/keepassdroid/EntryActivity.java +++ b/src/com/keepassdroid/EntryActivity.java @@ -54,6 +54,7 @@ import com.android.keepass.KeePass; import com.android.keepass.R; import com.keepassdroid.app.App; import com.keepassdroid.intents.Intents; +import com.keepassdroid.keepasslib.PwDate; public class EntryActivity extends LockCloseActivity { public static final String KEY_ENTRY = "entry"; @@ -112,7 +113,7 @@ public class EntryActivity extends LockCloseActivity { // Update last access time. Calendar cal = Calendar.getInstance(); - mEntry.tLastAccess = cal.getTime(); + mEntry.tLastAccess = new PwDate(cal.getTime()); fillData(); View scroll = findViewById(R.id.entry_scroll); @@ -195,11 +196,11 @@ public class EntryActivity extends LockCloseActivity { setPasswordStyle(); DateFormat df = DateFormat.getInstance(); - populateText(R.id.entry_created, df.format(mEntry.tCreation)); - populateText(R.id.entry_modified, df.format(mEntry.tLastMod)); - populateText(R.id.entry_accessed, df.format(mEntry.tLastAccess)); + populateText(R.id.entry_created, df.format(mEntry.tCreation.getJDate())); + populateText(R.id.entry_modified, df.format(mEntry.tLastMod.getJDate())); + populateText(R.id.entry_accessed, df.format(mEntry.tLastAccess.getJDate())); - if ( PwEntry.IsNever(mEntry.tExpire) ) { + if ( PwEntry.IsNever(mEntry.tExpire.getJDate()) ) { populateText(R.id.entry_expires, R.string.never); } else { populateText(R.id.entry_expires, df.format(mEntry.tExpire)); diff --git a/src/com/keepassdroid/EntryEditActivity.java b/src/com/keepassdroid/EntryEditActivity.java index 64b809527..78340c5f9 100644 --- a/src/com/keepassdroid/EntryEditActivity.java +++ b/src/com/keepassdroid/EntryEditActivity.java @@ -48,6 +48,7 @@ import com.keepassdroid.database.AddEntry; import com.keepassdroid.database.OnFinish; import com.keepassdroid.database.RunnableOnFinish; import com.keepassdroid.database.UpdateEntry; +import com.keepassdroid.keepasslib.PwDate; public class EntryEditActivity extends LockCloseActivity { public static final String KEY_ENTRY = "entry"; @@ -146,8 +147,8 @@ public class EntryEditActivity extends LockCloseActivity { newEntry.uuid = mEntry.uuid; Date now = Calendar.getInstance().getTime(); - newEntry.tLastAccess = now; - newEntry.tLastMod = now; + newEntry.tLastAccess = new PwDate(now); + newEntry.tLastMod = new PwDate(now); byte[] binaryData = mEntry.getBinaryData(); if ( binaryData != null ) { diff --git a/src/com/keepassdroid/keepasslib/PwDate.java b/src/com/keepassdroid/keepasslib/PwDate.java new file mode 100644 index 000000000..8b80b1e4a --- /dev/null +++ b/src/com/keepassdroid/keepasslib/PwDate.java @@ -0,0 +1,165 @@ +/* + * Copyright 2009 Brian Pellin. + * + * This file is part of KeePassDroid. + * + * KeePassDroid 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 2 of the License, or + * (at your option) any later version. + * + * KeePassDroid 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 KeePassDroid. If not, see . + * + */ +package com.keepassdroid.keepasslib; + +import java.util.Calendar; +import java.util.Date; + +import org.phoneid.keepassj2me.Types; + +import com.keepassdroid.app.App; + +/** Converting from the C Date format to the Java data format is + * expensive when done for every record at once. I use this class to + * allow lazy conversions between the formats. + * @author bpellin + * + */ +public class PwDate implements Cloneable { + + private static final int DATE_SIZE = 5; + + private boolean cDateBuilt = false; + private boolean jDateBuilt = false; + + private Date jDate; + private byte[] cDate; + + public PwDate(byte[] buf, int offset) { + cDate = new byte[DATE_SIZE]; + System.arraycopy(buf, offset, cDate, 0, DATE_SIZE); + cDateBuilt = true; + } + + public PwDate(Date date) { + jDate = date; + jDateBuilt = true; + } + + public PwDate(long millis) { + jDate = new Date(millis); + jDateBuilt = true; + } + + private PwDate() { + + } + + @Override + public Object clone() { + PwDate copy = new PwDate(); + + if ( cDateBuilt ) { + byte[] newC = new byte[DATE_SIZE]; + System.arraycopy(cDate, 0, newC, 0, DATE_SIZE); + copy.cDate = newC; + copy.cDateBuilt = true; + } + + if ( jDateBuilt ) { + copy.jDate = (Date) jDate.clone(); + copy.jDateBuilt = true; + } + + return copy; + } + + + + public Date getJDate() { + if ( ! jDateBuilt ) { + jDate = readTime(cDate, 0, App.getCalendar()); + jDateBuilt = true; + } + + return jDate; + } + + public byte[] getCDate() { + if ( ! cDateBuilt ) { + cDate = writeTime(jDate, App.getCalendar()); + cDateBuilt = true; + } + + return cDate; + } + + + /** + * Unpack date from 5 byte format. The five bytes at 'offset' are unpacked + * to a java.util.Date instance. + */ + public static Date readTime(byte[] buf, int offset, Calendar time) { + int dw1 = Types.readUByte(buf, offset); + int dw2 = Types.readUByte(buf, offset + 1); + int dw3 = Types.readUByte(buf, offset + 2); + int dw4 = Types.readUByte(buf, offset + 3); + int dw5 = Types.readUByte(buf, offset + 4); + + // Unpack 5 byte structure to date and time + int year = (dw1 << 6) | (dw2 >> 2); + int month = ((dw2 & 0x00000003) << 2) | (dw3 >> 6); + + int day = (dw3 >> 1) & 0x0000001F; + int hour = ((dw3 & 0x00000001) << 4) | (dw4 >> 4); + int minute = ((dw4 & 0x0000000F) << 2) | (dw5 >> 6); + int second = dw5 & 0x0000003F; + + if (time == null) { + time = Calendar.getInstance(); + } + // File format is a 1 based month, java Calendar uses a zero based month + time.set(year, month - 1, day, hour, minute, second); + + return time.getTime(); + + } + + public static byte[] writeTime(Date date, Calendar cal) { + if (date == null) { + return null; + } + + byte[] buf = new byte[5]; + if (cal == null) { + cal = Calendar.getInstance(); + } + cal.setTime(date); + + int year = cal.get(Calendar.YEAR); + // File format is a 1 based month, java Calendar uses a zero based month + int month = cal.get(Calendar.MONTH) + 1; + int day = cal.get(Calendar.DAY_OF_MONTH); + int hour = cal.get(Calendar.HOUR_OF_DAY); + int minute = cal.get(Calendar.MINUTE); + int second = cal.get(Calendar.SECOND); + + buf[0] = Types.writeUByte(((year >> 6) & 0x0000003F)); + buf[1] = Types.writeUByte(((year & 0x0000003F) << 2) + | ((month >> 2) & 0x00000003)); + buf[2] = (byte) (((month & 0x00000003) << 6) + | ((day & 0x0000001F) << 1) | ((hour >> 4) & 0x00000001)); + buf[3] = (byte) (((hour & 0x0000000F) << 4) | ((minute >> 2) & 0x0000000F)); + buf[4] = (byte) (((minute & 0x00000003) << 6) | (second & 0x0000003F)); + + return buf; + } + +} diff --git a/src/com/keepassdroid/keepasslib/PwEntryOutput.java b/src/com/keepassdroid/keepasslib/PwEntryOutput.java index 50b93786b..8682aa8d8 100644 --- a/src/com/keepassdroid/keepasslib/PwEntryOutput.java +++ b/src/com/keepassdroid/keepasslib/PwEntryOutput.java @@ -21,7 +21,6 @@ package com.keepassdroid.keepasslib; import java.io.IOException; import java.io.OutputStream; -import java.util.Calendar; import org.phoneid.keepassj2me.PwEntry; import org.phoneid.keepassj2me.Types; @@ -56,16 +55,14 @@ public class PwEntryOutput { private OutputStream mOS; private PwEntry mPE; private long outputBytes = 0; - private Calendar mCal; /** Output the PwGroup to the stream * @param pe * @param os */ - public PwEntryOutput(PwEntry pe, OutputStream os, Calendar cal) { + public PwEntryOutput(PwEntry pe, OutputStream os) { mPE = pe; mOS = os; - mCal = cal; } //NOTE: Need be to careful about using ints. The actual type written to file is a unsigned int @@ -118,16 +115,16 @@ public class PwEntryOutput { outputBytes += addlLen; // Create date - writeDate(CREATE_FIELD_TYPE, Types.writeTime(mPE.tCreation, mCal)); + writeDate(CREATE_FIELD_TYPE, mPE.tCreation.getCDate()); // Modification date - writeDate(MOD_FIELD_TYPE, Types.writeTime(mPE.tLastMod, mCal)); + writeDate(MOD_FIELD_TYPE, mPE.tLastMod.getCDate()); // Access date - writeDate(ACCESS_FIELD_TYPE, Types.writeTime(mPE.tLastAccess, mCal)); + writeDate(ACCESS_FIELD_TYPE, mPE.tLastAccess.getCDate()); // Expiration date - writeDate(EXPIRE_FIELD_TYPE, Types.writeTime(mPE.tExpire, mCal)); + writeDate(EXPIRE_FIELD_TYPE, mPE.tExpire.getCDate()); // Binary desc mOS.write(BINARY_DESC_FIELD_TYPE); diff --git a/src/com/keepassdroid/keepasslib/PwGroupOutput.java b/src/com/keepassdroid/keepasslib/PwGroupOutput.java index 3acefc445..4b831b42e 100644 --- a/src/com/keepassdroid/keepasslib/PwGroupOutput.java +++ b/src/com/keepassdroid/keepasslib/PwGroupOutput.java @@ -21,7 +21,6 @@ package com.keepassdroid.keepasslib; import java.io.IOException; import java.io.OutputStream; -import java.util.Calendar; import org.phoneid.keepassj2me.PwGroup; import org.phoneid.keepassj2me.Types; @@ -48,16 +47,14 @@ public class PwGroupOutput { private OutputStream mOS; private PwGroup mPG; - private Calendar mCal; /** Output the PwGroup to the stream * @param pg * @param os */ - public PwGroupOutput(PwGroup pg, OutputStream os, Calendar cal) { + public PwGroupOutput(PwGroup pg, OutputStream os) { mPG = pg; mOS = os; - mCal = cal; } public void output() throws IOException { @@ -75,22 +72,22 @@ public class PwGroupOutput { // Create date mOS.write(CREATE_FIELD_TYPE); mOS.write(DATE_FIELD_SIZE); - mOS.write(Types.writeTime(mPG.tCreation, mCal)); + mOS.write(mPG.tCreation.getCDate()); // Modification date mOS.write(MOD_FIELD_TYPE); mOS.write(DATE_FIELD_SIZE); - mOS.write(Types.writeTime(mPG.tLastMod, mCal)); + mOS.write(mPG.tLastMod.getCDate()); // Access date mOS.write(ACCESS_FIELD_TYPE); mOS.write(DATE_FIELD_SIZE); - mOS.write(Types.writeTime(mPG.tLastAccess, mCal)); + mOS.write(mPG.tLastAccess.getCDate()); // Expiration date mOS.write(EXPIRE_FIELD_TYPE); mOS.write(DATE_FIELD_SIZE); - mOS.write(Types.writeTime(mPG.tExpire, mCal)); + mOS.write(mPG.tExpire.getCDate()); // Image ID mOS.write(IMAGEID_FIELD_TYPE); diff --git a/src/com/keepassdroid/keepasslib/PwManagerOutput.java b/src/com/keepassdroid/keepasslib/PwManagerOutput.java index e993b6af6..6ace8ac09 100644 --- a/src/com/keepassdroid/keepasslib/PwManagerOutput.java +++ b/src/com/keepassdroid/keepasslib/PwManagerOutput.java @@ -28,7 +28,6 @@ import java.security.InvalidKeyException; import java.security.MessageDigest; import java.security.NoSuchAlgorithmException; import java.security.SecureRandom; -import java.util.Calendar; import java.util.Vector; import javax.crypto.Cipher; @@ -49,19 +48,16 @@ public class PwManagerOutput { private OutputStream mOS; private final boolean mDebug; public static final boolean DEBUG = true; - private Calendar mCal; - public PwManagerOutput(PwManager pm, OutputStream os, Calendar cal) { + public PwManagerOutput(PwManager pm, OutputStream os) { mPM = pm; mOS = os; - mCal = cal; mDebug = false; } - public PwManagerOutput(PwManager pm, OutputStream os, Calendar cal, boolean debug) { + public PwManagerOutput(PwManager pm, OutputStream os, boolean debug) { mPM = pm; mOS = os; - mCal = cal; mDebug = debug; } @@ -191,7 +187,7 @@ public class PwManagerOutput { // Groups for ( int i = 0; i < mPM.groups.size(); i++ ) { PwGroup pg = mPM.groups.get(i); - PwGroupOutput pgo = new PwGroupOutput(pg, os, mCal); + PwGroupOutput pgo = new PwGroupOutput(pg, os); try { pgo.output(); } catch (IOException e) { @@ -202,7 +198,7 @@ public class PwManagerOutput { // Entries for (int i = 0; i < mPM.entries.size(); i++ ) { PwEntry pe = mPM.entries.get(i); - PwEntryOutput peo = new PwEntryOutput(pe, os, mCal); + PwEntryOutput peo = new PwEntryOutput(pe, os); try { peo.output(); } catch (IOException e) { diff --git a/src/org/phoneid/keepassj2me/ImporterV3.java b/src/org/phoneid/keepassj2me/ImporterV3.java index 56daab6c5..cd0aaf388 100644 --- a/src/org/phoneid/keepassj2me/ImporterV3.java +++ b/src/org/phoneid/keepassj2me/ImporterV3.java @@ -33,7 +33,6 @@ import java.security.InvalidKeyException; import java.security.MessageDigest; import java.security.NoSuchAlgorithmException; import java.util.Arrays; -import java.util.Calendar; import javax.crypto.BadPaddingException; import javax.crypto.Cipher; @@ -53,6 +52,7 @@ import com.keepassdroid.crypto.finalkey.FinalKeyFactory; import com.keepassdroid.keepasslib.InvalidKeyFileException; import com.keepassdroid.keepasslib.InvalidPasswordException; import com.keepassdroid.keepasslib.NullOutputStream; +import com.keepassdroid.keepasslib.PwDate; /** * Load a v3 database file. @@ -65,16 +65,13 @@ public class ImporterV3 { public static final boolean DEBUG = true; private final boolean mDebug; - private Calendar mCal; - public ImporterV3(Calendar cal) { + public ImporterV3() { mDebug = false; - mCal = cal; } - public ImporterV3(Calendar cal, boolean debug) { + public ImporterV3(boolean debug) { mDebug = debug; - mCal = cal; } @@ -362,16 +359,16 @@ public class ImporterV3 { grp.name = new String( buf, offset, Types.strlen( buf, offset ) ); break; case 0x0003 : - grp.tCreation = Types.readTime( buf, offset, mCal ); + grp.tCreation = new PwDate(buf, offset); break; case 0x0004 : - grp.tLastMod = Types.readTime( buf, offset, mCal ); + grp.tLastMod = new PwDate(buf, offset); break; case 0x0005 : - grp.tLastAccess = Types.readTime( buf, offset, mCal ); + grp.tLastAccess = new PwDate(buf, offset); break; case 0x0006 : - grp.tExpire = Types.readTime( buf, offset, mCal ); + grp.tExpire = new PwDate(buf, offset); break; case 0x0007 : grp.imageId = Types.readInt( buf, offset ); @@ -424,16 +421,16 @@ public class ImporterV3 { ent.additional = new String( buf, offset, Types.strlen( buf, offset ), "UTF-8" ); break; case 0x0009 : - ent.tCreation = Types.readTime( buf, offset, mCal ); + ent.tCreation = new PwDate(buf, offset); break; case 0x000A : - ent.tLastMod = Types.readTime( buf, offset, mCal ); + ent.tLastMod = new PwDate(buf, offset); break; case 0x000B : - ent.tLastAccess = Types.readTime( buf, offset, mCal ); + ent.tLastAccess = new PwDate(buf, offset); break; case 0x000C : - ent.tExpire = Types.readTime( buf, offset, mCal ); + ent.tExpire = new PwDate(buf, offset); break; case 0x000D : ent.binaryDesc = new String( buf, offset, Types.strlen( buf, offset ), "UTF-8" ); diff --git a/src/org/phoneid/keepassj2me/PwEntry.java b/src/org/phoneid/keepassj2me/PwEntry.java index ec0a7e1ee..085444c87 100644 --- a/src/org/phoneid/keepassj2me/PwEntry.java +++ b/src/org/phoneid/keepassj2me/PwEntry.java @@ -31,6 +31,7 @@ import java.util.Date; import java.util.Random; import com.keepassdroid.Database; +import com.keepassdroid.keepasslib.PwDate; /** @@ -110,10 +111,10 @@ public class PwEntry { Calendar cal = Calendar.getInstance(); Date now = cal.getTime(); - tCreation = now; - tLastAccess = now; - tLastMod = now; - tExpire = NEVER_EXPIRE; + tCreation = new PwDate(now); + tLastAccess = new PwDate(now); + tLastMod = new PwDate(now); + tExpire = new PwDate(NEVER_EXPIRE); } @@ -195,10 +196,10 @@ public class PwEntry { System.arraycopy(source.password, 0, password, 0, passLen); additional = source.additional; - tCreation = new Date(source.tCreation.getTime()); - tLastMod = new Date(source.tLastMod.getTime()); - tLastAccess = new Date(source.tLastAccess.getTime()); - tExpire = new Date(source.tExpire.getTime()); + tCreation = (PwDate) source.tCreation.clone(); + tLastMod = (PwDate) source.tLastMod.clone(); + tLastAccess = (PwDate) source.tLastAccess.clone(); + tExpire = (PwDate) source.tExpire.clone(); binaryDesc = source.binaryDesc; @@ -231,10 +232,10 @@ public class PwEntry { public String additional; - public Date tCreation; - public Date tLastMod; - public Date tLastAccess; - public Date tExpire; + public PwDate tCreation; + public PwDate tLastMod; + public PwDate tLastAccess; + public PwDate tExpire; /** A string describing what is in pBinaryData */ public String binaryDesc; diff --git a/src/org/phoneid/keepassj2me/PwGroup.java b/src/org/phoneid/keepassj2me/PwGroup.java index 995877616..e3ff637e6 100644 --- a/src/org/phoneid/keepassj2me/PwGroup.java +++ b/src/org/phoneid/keepassj2me/PwGroup.java @@ -35,6 +35,8 @@ import java.util.Comparator; import java.util.Date; import java.util.Vector; +import com.keepassdroid.keepasslib.PwDate; + /** * @author Brian Pellin @@ -64,10 +66,10 @@ public class PwGroup { public int imageId; public String name; - public Date tCreation; - public Date tLastMod; - public Date tLastAccess; - public Date tExpire; + public PwDate tCreation; + public PwDate tLastMod; + public PwDate tLastAccess; + public PwDate tExpire; public int level; // short diff --git a/src/org/phoneid/keepassj2me/PwManager.java b/src/org/phoneid/keepassj2me/PwManager.java index 7232ab3a8..b4bf6f70e 100644 --- a/src/org/phoneid/keepassj2me/PwManager.java +++ b/src/org/phoneid/keepassj2me/PwManager.java @@ -39,6 +39,7 @@ import java.util.Random; import java.util.Vector; import com.keepassdroid.keepasslib.InvalidKeyFileException; +import com.keepassdroid.keepasslib.PwDate; /** * @author Naomaru Itoi @@ -365,10 +366,10 @@ public class PwManager { group.name = name; Date now = Calendar.getInstance().getTime(); - group.tCreation = now; - group.tLastAccess = now; - group.tLastMod = now; - group.tExpire = PwGroup.NEVER_EXPIRE; + group.tCreation = new PwDate(now); + group.tLastAccess = new PwDate(now); + group.tLastMod = new PwDate(now); + group.tExpire = new PwDate(PwGroup.NEVER_EXPIRE); group.level = parent.level + 1; diff --git a/src/org/phoneid/keepassj2me/Types.java b/src/org/phoneid/keepassj2me/Types.java index c82c8e81b..c650d2cab 100644 --- a/src/org/phoneid/keepassj2me/Types.java +++ b/src/org/phoneid/keepassj2me/Types.java @@ -26,8 +26,6 @@ package org.phoneid.keepassj2me; import java.io.IOException; import java.io.OutputStream; -import java.util.Calendar; -import java.util.Date; import java.util.UUID; /** @@ -188,67 +186,6 @@ public class Types { return b2; } - - - /** - * Unpack date from 5 byte format. - * The five bytes at 'offset' are unpacked to a java.util.Date instance. - */ - public static Date readTime( byte[] buf, int offset, Calendar time ) { - int dw1 = readUByte( buf, offset ); - int dw2 = readUByte( buf, offset + 1 ); - int dw3 = readUByte( buf, offset + 2 ); - int dw4 = readUByte( buf, offset + 3 ); - int dw5 = readUByte( buf, offset + 4 ); - - // Unpack 5 byte structure to date and time - int year = (dw1 << 6) | (dw2 >> 2); - int month = ((dw2 & 0x00000003) << 2) | (dw3 >> 6); - - int day = (dw3 >> 1) & 0x0000001F; - int hour = ((dw3 & 0x00000001) << 4) | (dw4 >> 4); - int minute = ((dw4 & 0x0000000F) << 2) | (dw5 >> 6); - int second = dw5 & 0x0000003F; - - if ( time == null ) { - time = Calendar.getInstance(); - } - // File format is a 1 based month, java Calendar uses a zero based month - time.set( year, month-1, day, hour, minute, second ); - - return time.getTime(); - - //return null; - } - - public static byte[] writeTime(Date date, Calendar cal) { - if ( date == null ) { - return null; - } - - byte[] buf = new byte[5]; - if ( cal == null ) { - cal = Calendar.getInstance(); - } - cal.setTime(date); - - int year = cal.get(Calendar.YEAR); - // File format is a 1 based month, java Calendar uses a zero based month - int month = cal.get(Calendar.MONTH)+1; - int day = cal.get(Calendar.DAY_OF_MONTH); - int hour = cal.get(Calendar.HOUR_OF_DAY); - int minute = cal.get(Calendar.MINUTE); - int second = cal.get(Calendar.SECOND); - - buf[0] = writeUByte(((year >> 6) & 0x0000003F)); - buf[1] = writeUByte(((year & 0x0000003F) << 2) | ((month >> 2) & 0x00000003) ); - buf[2] = (byte)(((month & 0x00000003) << 6) | ((day & 0x0000001F) << 1) | ((hour >> 4) & 0x00000001)); - buf[3] = (byte)(((hour & 0x0000000F) << 4) | ((minute >> 2) & 0x0000000F)); - buf[4] = (byte)(((minute & 0x00000003) << 6) | (second & 0x0000003F)); - - return buf; - } - public static int writeCString(String str, OutputStream os) throws IOException { if ( str == null ) { os.write(writeInt(0)); diff --git a/tests/src/com/keepassdroid/tests/PwEntryTest.java b/tests/src/com/keepassdroid/tests/PwEntryTest.java index 269664482..450a65a0a 100644 --- a/tests/src/com/keepassdroid/tests/PwEntryTest.java +++ b/tests/src/com/keepassdroid/tests/PwEntryTest.java @@ -54,7 +54,7 @@ public class PwEntryTest extends AndroidTestCase { public void testCreation() { Calendar cal = Calendar.getInstance(); - cal.setTime(mPE.tCreation); + cal.setTime(mPE.tCreation.getJDate()); assertEquals("Incorrect year.", cal.get(Calendar.YEAR), 2009); assertEquals("Incorrect month.", cal.get(Calendar.MONTH), 3); diff --git a/tests/src/com/keepassdroid/tests/TypesTest.java b/tests/src/com/keepassdroid/tests/TypesTest.java index ee024c624..c2c2f392f 100644 --- a/tests/src/com/keepassdroid/tests/TypesTest.java +++ b/tests/src/com/keepassdroid/tests/TypesTest.java @@ -19,11 +19,15 @@ */ package com.keepassdroid.tests; +import static org.junit.Assert.assertArrayEquals; + import java.util.Calendar; import junit.framework.TestCase; + import org.phoneid.keepassj2me.Types; -import static org.junit.Assert.*; + +import com.keepassdroid.keepasslib.PwDate; public class TypesTest extends TestCase { @@ -123,9 +127,9 @@ public class TypesTest extends TestCase { Calendar expected = Calendar.getInstance(); expected.set(2008, 1, 2, 3, 4, 5); - byte[] buf = Types.writeTime(expected.getTime(), cal); + byte[] buf = PwDate.writeTime(expected.getTime(), cal); Calendar actual = Calendar.getInstance(); - actual.setTime(Types.readTime(buf, 0, cal)); + actual.setTime(PwDate.readTime(buf, 0, cal)); assertEquals("Year mismatch: ", 2008, actual.get(Calendar.YEAR)); assertEquals("Month mismatch: ", 1, actual.get(Calendar.MONTH)); diff --git a/tests/src/com/keepassdroid/tests/output/PwManagerOutputTest.java b/tests/src/com/keepassdroid/tests/output/PwManagerOutputTest.java index 8ad0a199e..0d19763ea 100644 --- a/tests/src/com/keepassdroid/tests/output/PwManagerOutputTest.java +++ b/tests/src/com/keepassdroid/tests/output/PwManagerOutputTest.java @@ -28,7 +28,6 @@ import java.io.InputStream; import java.security.DigestOutputStream; import java.security.MessageDigest; import java.security.NoSuchAlgorithmException; -import java.util.Calendar; import org.phoneid.keepassj2me.PwDbHeader; import org.phoneid.keepassj2me.PwManager; @@ -44,20 +43,18 @@ import com.keepassdroid.tests.database.TestData; public class PwManagerOutputTest extends AndroidTestCase { PwManager mPM; - Calendar mCal; @Override protected void setUp() throws Exception { super.setUp(); mPM = TestData.GetTest1(getContext()); - mCal = Calendar.getInstance(); } public void testPlainContent() throws IOException, PwManagerOutputException { ByteArrayOutputStream bos = new ByteArrayOutputStream(); - PwManagerOutput pos = new PwManagerOutput(mPM, bos, mCal, PwManagerOutput.DEBUG); + PwManagerOutput pos = new PwManagerOutput(mPM, bos, PwManagerOutput.DEBUG); pos.outputPlanGroupAndEntries(bos); assertTrue("No output", bos.toByteArray().length > 0); @@ -72,7 +69,7 @@ public class PwManagerOutputTest extends AndroidTestCase { DigestOutputStream dos = new DigestOutputStream(nos, md); - PwManagerOutput pos = new PwManagerOutput(mPM, dos, mCal, PwManagerOutput.DEBUG); + PwManagerOutput pos = new PwManagerOutput(mPM, dos, PwManagerOutput.DEBUG); pos.outputPlanGroupAndEntries(dos); dos.close(); @@ -97,7 +94,7 @@ public class PwManagerOutputTest extends AndroidTestCase { public void testHeader() throws PwManagerOutputException, IOException { ByteArrayOutputStream bActual = new ByteArrayOutputStream(); - PwManagerOutput pActual = new PwManagerOutput(mPM, bActual, mCal, PwManagerOutput.DEBUG); + PwManagerOutput pActual = new PwManagerOutput(mPM, bActual, PwManagerOutput.DEBUG); PwDbHeader header = pActual.outputHeader(bActual); ByteArrayOutputStream bExpected = new ByteArrayOutputStream(); @@ -111,7 +108,7 @@ public class PwManagerOutputTest extends AndroidTestCase { public void testFinalKey() throws PwManagerOutputException { ByteArrayOutputStream bActual = new ByteArrayOutputStream(); - PwManagerOutput pActual = new PwManagerOutput(mPM, bActual, mCal, PwManagerOutput.DEBUG); + PwManagerOutput pActual = new PwManagerOutput(mPM, bActual, PwManagerOutput.DEBUG); PwDbHeader hActual = pActual.outputHeader(bActual); byte[] finalKey = pActual.getFinalKey2(hActual); @@ -134,7 +131,7 @@ public class PwManagerOutputTest extends AndroidTestCase { } ByteArrayOutputStream bActual = new ByteArrayOutputStream(); - PwManagerOutput pActual = new PwManagerOutput(mPM, bActual, mCal, PwManagerOutput.DEBUG); + PwManagerOutput pActual = new PwManagerOutput(mPM, bActual, PwManagerOutput.DEBUG); pActual.output(); //pActual.close();