Get to the point where I can decode the database down to plaintext XML.

This commit is contained in:
Brian Pellin committed 2010-05-09 21:36:25 -05:00
1 parent a76b4e1976
commit 1bfc2bd29a
17 files changed
+560 -211

No files matched your search

@@ -68,7 +68,7 @@ public class CipherFactory {
*/
public static Cipher getInstance(UUID uuid, byte[] key, byte[] IV) throws NoSuchAlgorithmException, NoSuchPaddingException, InvalidKeyException, InvalidAlgorithmParameterException {
if ( uuid.equals(AES_CIPHER) ) {
Cipher cipher = CipherFactory.getInstance("AES/CBC/PKCS5Padding");
Cipher cipher = CipherFactory.getInstance("AES/CBC/PKCS5Padding", true);
cipher.init(Cipher.DECRYPT_MODE, new SecretKeySpec(key, "AES"), new IvParameterSpec(IV));
+12 -20
View File
@@ -33,7 +33,7 @@ import com.keepassdroid.crypto.finalkey.FinalKeyFactory;
import com.keepassdroid.database.exception.InvalidKeyFileException;
import com.keepassdroid.stream.NullOutputStream;
public class PwDatabase {
public abstract class PwDatabase {
public byte masterKey[] = new byte[32];
public byte[] finalKey;
@@ -69,21 +69,10 @@ public class PwDatabase {
}
public static byte[] getMasterKey(String key, String keyFileName)
throws InvalidKeyFileException, IOException {
assert( key != null && keyFileName != null );
if ( key.length() > 0 && keyFileName.length() > 0 ) {
return getCompositeKey(key, keyFileName);
} else if ( key.length() > 0 ) {
return getPasswordKey(key);
} else if ( keyFileName.length() > 0 ) {
return getFileKey(keyFileName);
} else {
throw new IllegalArgumentException( "Key cannot be empty." );
}
}
public abstract byte[] getMasterKey(String key, String keyFileName) throws InvalidKeyFileException, IOException;
// TODO: This needs to work differently for KDB4. It always produces a composite key.
// So even if there is just a password it gets SHA-256'ed twice.
public void setMasterKey(String key, String keyFileName)
throws InvalidKeyFileException, IOException {
@@ -92,7 +81,7 @@ public class PwDatabase {
masterKey = getMasterKey(key, keyFileName);
}
private static byte[] getCompositeKey(String key, String keyFileName)
protected byte[] getCompositeKey(String key, String keyFileName)
throws InvalidKeyFileException, IOException {
assert(key != null && keyFileName != null);
@@ -112,7 +101,7 @@ public class PwDatabase {
return md.digest(fileKey);
}
private static byte[] getFileKey(String fileName)
protected static byte[] getFileKey(String fileName)
throws InvalidKeyFileException, IOException {
assert(fileName != null);
@@ -186,7 +175,7 @@ public class PwDatabase {
return data;
}
private static byte[] getPasswordKey(String key) throws IOException {
protected byte[] getPasswordKey(String key, String encoding) throws IOException {
assert(key!=null);
if ( key.length() == 0 )
@@ -201,7 +190,8 @@ public class PwDatabase {
byte[] bKey;
try {
bKey = key.getBytes("ISO-8859-1");
// TODO: Need to make this UTF-8 in kdb4
bKey = key.getBytes(encoding);
} catch (UnsupportedEncodingException e) {
assert false;
bKey = key.getBytes();
@@ -210,5 +200,7 @@ public class PwDatabase {
return md.digest();
}
public abstract byte[] getPasswordKey(String key) throws IOException;
}
@@ -26,11 +26,14 @@ Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
package com.keepassdroid.database;
// Java
import java.io.IOException;
import java.util.Calendar;
import java.util.Date;
import java.util.Random;
import java.util.Vector;
import com.keepassdroid.database.exception.InvalidKeyFileException;
/**
@@ -239,4 +242,25 @@ public class PwDatabaseV3 extends PwDatabase {
return false;
}
public byte[] getMasterKey(String key, String keyFileName)
throws InvalidKeyFileException, IOException {
assert( key != null && keyFileName != null );
if ( key.length() > 0 && keyFileName.length() > 0 ) {
return getCompositeKey(key, keyFileName);
} else if ( key.length() > 0 ) {
return getPasswordKey(key);
} else if ( keyFileName.length() > 0 ) {
return getFileKey(keyFileName);
} else {
throw new IllegalArgumentException( "Key cannot be empty." );
}
}
public byte[] getPasswordKey(String key) throws IOException {
return getPasswordKey(key, "ISO-8859-1");
}
}
@@ -19,8 +19,13 @@
*/
package com.keepassdroid.database;
import java.io.IOException;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.UUID;
import com.keepassdroid.database.exception.InvalidKeyFileException;
public class PwDatabaseV4 extends PwDatabase {
@@ -28,4 +33,36 @@ public class PwDatabaseV4 extends PwDatabase {
public PwCompressionAlgorithm compressionAlgorithm;
public long numKeyEncRounds;
@Override
public byte[] getMasterKey(String key, String keyFileName)
throws InvalidKeyFileException, IOException {
assert( key != null && keyFileName != null );
byte[] fKey;
if ( key.length() > 0 && keyFileName.length() > 0 ) {
return getCompositeKey(key, keyFileName);
} else if ( key.length() > 0 ) {
fKey = getPasswordKey(key);
} else if ( keyFileName.length() > 0 ) {
fKey = getFileKey(keyFileName);
} else {
throw new IllegalArgumentException( "Key cannot be empty." );
}
MessageDigest md;
try {
md = MessageDigest.getInstance("SHA-256");
} catch (NoSuchAlgorithmException e) {
throw new IOException("No SHA-256 implementation");
}
return md.digest(fKey);
}
@Override
public byte[] getPasswordKey(String key) throws IOException {
return getPasswordKey(key, "UTF-8");
}
}
@@ -26,7 +26,7 @@ package com.keepassdroid.database;
import java.io.IOException;
import com.keepassdroid.utils.Types;
import com.keepassdroid.stream.LEDataInputStream;
public class PwDbHeaderV3 extends PwDbHeader {
@@ -69,21 +69,21 @@ public class PwDbHeaderV3 extends PwDbHeader {
* @throws IOException
*/
public void loadFromFile( byte buf[], int offset ) throws IOException {
signature1 = Types.readInt( buf, offset + 0 );
signature2 = Types.readInt( buf, offset + 4 );
flags = Types.readInt( buf, offset + 8 );
version = Types.readInt( buf, offset + 12 );
signature1 = LEDataInputStream.readInt( buf, offset + 0 );
signature2 = LEDataInputStream.readInt( buf, offset + 4 );
flags = LEDataInputStream.readInt( buf, offset + 8 );
version = LEDataInputStream.readInt( buf, offset + 12 );
System.arraycopy( buf, offset + 16, masterSeed, 0, 16 );
System.arraycopy( buf, offset + 32, encryptionIV, 0, 16 );
numGroups = Types.readInt( buf, offset + 48 );
numEntries = Types.readInt( buf, offset + 52 );
numGroups = LEDataInputStream.readInt( buf, offset + 48 );
numEntries = LEDataInputStream.readInt( buf, offset + 52 );
System.arraycopy( buf, offset + 56, contentsHash, 0, 32 );
System.arraycopy( buf, offset + 88, transformSeed, 0, 32 );
numKeyEncRounds = Types.readInt( buf, offset + 120 );
numKeyEncRounds = LEDataInputStream.readInt( buf, offset + 120 );
if ( numKeyEncRounds < 0 ) {
// TODO: Really treat this like an unsigned integer
throw new IOException("Does not support more than " + Integer.MAX_VALUE + " rounds.");
@@ -23,6 +23,7 @@ import java.io.IOException;
import java.io.InputStream;
import com.keepassdroid.database.exception.InvalidDBVersionException;
import com.keepassdroid.stream.LEDataInputStream;
import com.keepassdroid.utils.Types;
public class PwDbHeaderV4 extends PwDbHeader {
@@ -64,14 +65,14 @@ public class PwDbHeaderV4 extends PwDbHeader {
*/
public void loadFromFile(InputStream is) throws IOException, InvalidDBVersionException {
int sig1 = Types.readInt(is);
int sig2 = Types.readInt(is);
int sig1 = LEDataInputStream.readInt(is);
int sig2 = LEDataInputStream.readInt(is);
if ( ! matchesHeader(sig1, sig2) ) {
throw new InvalidDBVersionException();
}
long version = Types.readUInt(is);
long version = LEDataInputStream.readUInt(is);
if ( ! validVersion(version) ) {
throw new InvalidDBVersionException();
}
@@ -158,7 +159,7 @@ public class PwDbHeaderV4 extends PwDbHeader {
throw new IOException("Invalid compression flags.");
}
int flag = Types.readInt(pbFlags, 0);
int flag = LEDataInputStream.readInt(pbFlags, 0);
if ( flag < 0 || flag >= PwCompressionAlgorithm.count ) {
throw new IOException("Unrecognized compression flag.");
}
@@ -172,7 +173,7 @@ public class PwDbHeaderV4 extends PwDbHeader {
throw new IOException("Invalid rounds.");
}
long rnd = Types.readLong(rounds, 0);
long rnd = LEDataInputStream.readLong(rounds, 0);
if ( rnd < 0 || rnd > Integer.MAX_VALUE ) {
//TODO: Actually support really large numbers
@@ -188,7 +189,7 @@ public class PwDbHeaderV4 extends PwDbHeader {
throw new IOException("Invalid stream id.");
}
int id = Types.readInt(streamID, 0);
int id = LEDataInputStream.readInt(streamID, 0);
if ( id < 0 || id >= CrsAlgorithm.Count ) {
throw new IOException("Invalid stream id.");
}
@@ -17,20 +17,16 @@
* along with KeePassDroid. If not, see <http://www.gnu.org/licenses/>.
*
*/
package com.keepassdroid.stream;
package com.keepassdroid.database.exception;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.OutputStream;
public class InvalidDBVersionException extends Exception {
/**
*
*/
private static final long serialVersionUID = -4260650987856400586L;
public class BetterDataOutputStream extends DataOutputStream {
public BetterDataOutputStream(OutputStream out) {
super(out);
}
public void writeUInt(long uint) throws IOException {
writeInt((int)uint);
public InvalidDBVersionException() {
super();
}
}
}
@@ -26,7 +26,7 @@ import com.keepassdroid.database.PwDbHeaderV3;
import com.keepassdroid.database.PwDbHeaderV4;
import com.keepassdroid.database.exception.InvalidDBSignatureException;
import com.keepassdroid.database.exception.Kdb4Exception;
import com.keepassdroid.utils.Types;
import com.keepassdroid.stream.LEDataInputStream;
public class ImporterFactory {
public static Importer createImporter(InputStream is) throws Kdb4Exception, InvalidDBSignatureException, IOException
@@ -36,8 +36,8 @@ public class ImporterFactory {
public static Importer createImporter(InputStream is, boolean debug) throws Kdb4Exception, InvalidDBSignatureException, IOException
{
int sig1 = Types.readInt(is);
int sig2 = Types.readInt(is);
int sig1 = LEDataInputStream.readInt(is);
int sig2 = LEDataInputStream.readInt(is);
if ( PwDbHeaderV3.matchesHeader(sig1, sig2) ) {
return new ImporterV3(debug);
@@ -58,6 +58,8 @@ import com.keepassdroid.database.exception.InvalidDBSignatureException;
import com.keepassdroid.database.exception.InvalidDBVersionException;
import com.keepassdroid.database.exception.InvalidKeyFileException;
import com.keepassdroid.database.exception.InvalidPasswordException;
import com.keepassdroid.stream.LEDataInputStream;
import com.keepassdroid.stream.LEDataOutputStream;
import com.keepassdroid.stream.NullOutputStream;
import com.keepassdroid.utils.Types;
@@ -234,7 +236,7 @@ public class ImporterV3 extends Importer {
for( int i = 0; i < hdr.numGroups; ) {
int fieldType = Types.readShort( filebuf, pos );
pos += 2;
int fieldSize = Types.readInt( filebuf, pos );
int fieldSize = LEDataInputStream.readInt( filebuf, pos );
pos += 4;
if( fieldType == 0xFFFF ) {
@@ -254,7 +256,7 @@ public class ImporterV3 extends Importer {
PwEntryV3 newEnt = new PwEntryV3();
for( int i = 0; i < hdr.numEntries; ) {
int fieldType = Types.readShort( filebuf, pos );
int fieldSize = Types.readInt( filebuf, pos + 2 );
int fieldSize = LEDataInputStream.readInt( filebuf, pos + 2 );
if( fieldType == 0xFFFF ) {
// End-Group record. Save group and count it.
@@ -295,10 +297,10 @@ public class ImporterV3 extends Importer {
// write length*8 to end of final block
int ix = thisblk + nextblk - 8;
Types.writeInt( data.length>>29, pad, ix );
LEDataOutputStream.writeInt( data.length>>29, pad, ix );
bsw32( pad, ix );
ix += 4;
Types.writeInt( data.length<<3, pad, ix );
LEDataOutputStream.writeInt( data.length<<3, pad, ix );
bsw32( pad, ix );
return pad;
@@ -327,7 +329,7 @@ public class ImporterV3 extends Importer {
// Ignore field
break;
case 0x0001 :
grp.groupId = Types.readInt(buf, offset);
grp.groupId = LEDataInputStream.readInt(buf, offset);
break;
case 0x0002 :
grp.name = Types.readCString(buf, offset);
@@ -345,13 +347,13 @@ public class ImporterV3 extends Importer {
grp.tExpire = new PwDate(buf, offset);
break;
case 0x0007 :
grp.imageId = Types.readInt(buf, offset);
grp.imageId = LEDataInputStream.readInt(buf, offset);
break;
case 0x0008 :
grp.level = Types.readShort(buf, offset);
break;
case 0x0009 :
grp.flags = Types.readInt(buf, offset);
grp.flags = LEDataInputStream.readInt(buf, offset);
break;
}
}
@@ -363,7 +365,7 @@ public class ImporterV3 extends Importer {
{
int fieldType = Types.readShort(buf, offset);
offset += 2;
int fieldSize = Types.readInt(buf, offset);
int fieldSize = LEDataInputStream.readInt(buf, offset);
offset += 4;
switch( fieldType ) {
@@ -374,10 +376,10 @@ public class ImporterV3 extends Importer {
System.arraycopy(buf, offset, ent.uuid, 0, 16);
break;
case 0x0002 :
ent.groupId = Types.readInt(buf, offset);
ent.groupId = LEDataInputStream.readInt(buf, offset);
break;
case 0x0003 :
ent.imageId = Types.readInt(buf, offset);
ent.imageId = LEDataInputStream.readInt(buf, offset);
break;
case 0x0004 :
ent.title = Types.readCString(buf, offset);
@@ -22,11 +22,10 @@ package com.keepassdroid.database.load;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.security.DigestInputStream;
import java.security.InvalidAlgorithmParameterException;
import java.security.InvalidKeyException;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.Arrays;
import java.util.zip.GZIPInputStream;
import javax.crypto.Cipher;
@@ -42,6 +41,8 @@ import com.keepassdroid.database.exception.InvalidDBSignatureException;
import com.keepassdroid.database.exception.InvalidDBVersionException;
import com.keepassdroid.database.exception.InvalidKeyFileException;
import com.keepassdroid.database.exception.InvalidPasswordException;
import com.keepassdroid.stream.LEDataInputStream;
import com.keepassdroid.stream.HashedBlockInputStream;
public class ImporterV4 extends Importer {
@@ -84,15 +85,19 @@ public class ImporterV4 extends Importer {
}
InputStream decrypted = new CipherInputStream(inStream, cipher);
MessageDigest md = null;
try {
md = MessageDigest.getInstance("SHA-256");
} catch (NoSuchAlgorithmException e) {
throw new IOException("SHA-256 not implemented here.");
LEDataInputStream dataDecrypted = new LEDataInputStream(decrypted);
byte[] storedStartBytes = dataDecrypted.readBytes(32);
if ( storedStartBytes == null || storedStartBytes.length != 32 ) {
throw new IOException("Invalid data.");
}
InputStream hashed = new DigestInputStream(decrypted, md);
if ( ! Arrays.equals(storedStartBytes, header.streamStartBytes) ) {
// TODO: Probably need a special error here. This would probably indicate
// an incorrect password/key
throw new IOException("Bad database key");
}
HashedBlockInputStream hashed = new HashedBlockInputStream(decrypted);
InputStream decompressed;
if ( db.compressionAlgorithm == PwCompressionAlgorithm.Gzip ) {
@@ -101,11 +106,11 @@ public class ImporterV4 extends Importer {
decompressed = hashed;
}
FileOutputStream fos = new FileOutputStream("output.xml");
// TODO: output into database instead of file
FileOutputStream fos = new FileOutputStream("/sdcard/output.xml");
byte[] buf = new byte[1024];
int byteReads;
while (( byteReads = decompressed.read(buf)) != -1 ) {
while ( decompressed.read(buf) != -1 ) {
fos.write(buf);
}
@@ -1,64 +0,0 @@
/*
* Copyright 2010 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 <http://www.gnu.org/licenses/>.
*
*/
package com.keepassdroid.stream;
import java.io.DataInputStream;
import java.io.IOException;
import java.io.InputStream;
public class BetterDataInputStream extends DataInputStream {
public static final long INT_TO_LONG_MASK = 0xffffffffL;
public BetterDataInputStream(InputStream in) {
super(in);
}
/** Read a 32-bit value and return it as a long, so that it can
* be interpreted as an unsigned integer.
* @return
* @throws IOException
*/
public long readUInt() throws IOException {
return (readInt() & INT_TO_LONG_MASK);
}
public byte[] readBytes(int length) throws IOException {
byte[] buf = new byte[length];
int count = 0;
while ( count < length ) {
int read = read(buf, count, length - count);
// Reached end
if ( read == -1 ) {
// Stop early
byte[] early = new byte[count];
System.arraycopy(buf, 0, early, 0, count);
return early;
}
count += read;
}
return buf;
}
}
@@ -25,11 +25,12 @@ import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.Arrays;
public class HashedBlockInputStream extends InputStream {
private final static int HASH_SIZE = 32;
private BetterDataInputStream baseStream;
private LEDataInputStream baseStream;
private int bufferPos = 0;
private byte[] buffer = new byte[0];
private long bufferIndex = 0;
@@ -42,7 +43,7 @@ public class HashedBlockInputStream extends InputStream {
}
public HashedBlockInputStream(InputStream is) {
baseStream = new BetterDataInputStream(is);
baseStream = new LEDataInputStream(is);
}
@Override
@@ -89,12 +90,12 @@ public class HashedBlockInputStream extends InputStream {
}
bufferIndex++;
byte[] storedHash = baseStream.readBytes(HASH_SIZE);
byte[] storedHash = baseStream.readBytes(32);
if ( storedHash == null || storedHash.length != HASH_SIZE) {
throw new IOException("Invalid data format");
}
int bufferSize = baseStream.readInt();
int bufferSize = LEDataInputStream.readInt(baseStream);
if ( bufferSize < 0 ) {
throw new IOException("Invalid data format");
}
@@ -0,0 +1,147 @@
/*
* Copyright 2010 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 <http://www.gnu.org/licenses/>.
*
*/
package com.keepassdroid.stream;
import java.io.IOException;
import java.io.OutputStream;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
public class HashedBlockOutputStream extends OutputStream {
private final static int DEFAULT_BUFFER_SIZE = 1024 * 1024;
private LEDataOutputStream baseStream;
private int bufferPos = 0;
private byte[] buffer;
private long bufferIndex = 0;
public HashedBlockOutputStream(OutputStream os) {
init(os, DEFAULT_BUFFER_SIZE);
}
public HashedBlockOutputStream(OutputStream os, int bufferSize) {
if ( bufferSize <= 0 ) {
bufferSize = DEFAULT_BUFFER_SIZE;
}
init(os, bufferSize);
}
private void init(OutputStream os, int bufferSize) {
baseStream = new LEDataOutputStream(os);
buffer = new byte[bufferSize];
}
@Override
public void write(int oneByte) throws IOException {
byte[] buf = new byte[1];
write(buf, 0, 1);
}
@Override
public void close() throws IOException {
if ( bufferPos != 0 ) {
// Write remaining buffered amount
WriteHashedBlock();
}
// Write terminating block
WriteHashedBlock();
flush();
baseStream.close();
}
@Override
public void flush() throws IOException {
baseStream.flush();
}
@Override
public void write(byte[] b, int offset, int count) throws IOException {
while ( count > 0 ) {
if ( bufferPos == buffer.length ) {
WriteHashedBlock();
}
int copyLen = Math.min(buffer.length - bufferPos, count);
System.arraycopy(b, offset, buffer, bufferPos, copyLen);
offset += copyLen;
bufferPos += copyLen;
count -= copyLen;
}
}
private void WriteHashedBlock() throws IOException {
baseStream.writeUInt(bufferIndex);
bufferIndex++;
if ( bufferPos > 0 ) {
MessageDigest md = null;
try {
md = MessageDigest.getInstance("SHA-256");
} catch (NoSuchAlgorithmException e) {
throw new IOException("SHA-256 not implemented here.");
}
byte[] hash;
md.update(buffer, 0, bufferPos);
hash = md.digest();
/*
if ( bufferPos == buffer.length) {
hash = md.digest(buffer);
} else {
byte[] b = new byte[bufferPos];
System.arraycopy(buffer, 0, b, 0, bufferPos);
hash = md.digest(b);
}
*/
baseStream.write(hash);
} else {
// Write 32-bits of zeros
baseStream.writeLong(0L);
baseStream.writeLong(0L);
baseStream.writeLong(0L);
baseStream.writeLong(0L);
}
baseStream.writeInt(bufferPos);
if ( bufferPos > 0 ) {
baseStream.write(buffer, 0, bufferPos);
}
bufferPos = 0;
}
@Override
public void write(byte[] buffer) throws IOException {
write(buffer, 0, buffer.length);
}
}
@@ -0,0 +1,161 @@
/*
* Copyright 2010 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 <http://www.gnu.org/licenses/>.
*
*/
package com.keepassdroid.stream;
import java.io.IOException;
import java.io.InputStream;
/** Little endian version of the DataInputStream
* @author bpellin
*
*/
public class LEDataInputStream extends InputStream {
public static final long INT_TO_LONG_MASK = 0xffffffffL;
private InputStream baseStream;
public LEDataInputStream(InputStream in) {
baseStream = in;
}
/** Read a 32-bit value and return it as a long, so that it can
* be interpreted as an unsigned integer.
* @return
* @throws IOException
*/
public long readUInt() throws IOException {
return readUInt(baseStream);
}
public int readInt() throws IOException {
return readInt(baseStream);
}
public long readLong() throws IOException {
byte[] buf = readBytes(8);
return readLong(buf, 0);
}
@Override
public int available() throws IOException {
return baseStream.available();
}
@Override
public void close() throws IOException {
baseStream.close();
}
@Override
public void mark(int readlimit) {
baseStream.mark(readlimit);
}
@Override
public boolean markSupported() {
return baseStream.markSupported();
}
@Override
public int read() throws IOException {
return baseStream.read();
}
@Override
public int read(byte[] b, int offset, int length) throws IOException {
return baseStream.read(b, offset, length);
}
@Override
public int read(byte[] b) throws IOException {
// TODO Auto-generated method stub
return super.read(b);
}
@Override
public synchronized void reset() throws IOException {
baseStream.reset();
}
@Override
public long skip(long n) throws IOException {
return baseStream.skip(n);
}
public byte[] readBytes(int length) throws IOException {
byte[] buf = new byte[length];
int count = 0;
while ( count < length ) {
int read = read(buf, count, length - count);
// Reached end
if ( read == -1 ) {
// Stop early
byte[] early = new byte[count];
System.arraycopy(buf, 0, early, 0, count);
return early;
}
count += read;
}
return buf;
}
public static long readLong( byte buf[], int offset ) {
return ((long)buf[offset + 0] & 0xFF) + (((long)buf[offset + 1] & 0xFF) << 8)
+ (((long)buf[offset + 2] & 0xFF) << 16) + (((long)buf[offset + 3] & 0xFF) << 24)
+ (((long)buf[offset + 4] & 0xFF) << 32) + (((long)buf[offset + 5] & 0xFF) << 40)
+ (((long)buf[offset + 6] & 0xFF) << 48) + (((long)buf[offset + 7] & 0xFF) << 56);
}
public static long readUInt( byte buf[], int offset ) {
return (readInt(buf, offset) & INT_TO_LONG_MASK);
}
public static int readInt(InputStream is) throws IOException {
byte[] buf = new byte[4];
is.read(buf, 0, 4);
return readInt(buf, 0);
}
public static long readUInt(InputStream is) throws IOException {
return (readInt(is) & INT_TO_LONG_MASK);
}
/**
* Read a 32-bit value.
*
* @param buf
* @param offset
* @return
*/
public static int readInt( byte buf[], int offset ) {
return (buf[offset + 0] & 0xFF) + ((buf[offset + 1] & 0xFF) << 8) + ((buf[offset + 2] & 0xFF) << 16)
+ ((buf[offset + 3] & 0xFF) << 24);
}
}
@@ -0,0 +1,107 @@
/*
* Copyright 2010 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 <http://www.gnu.org/licenses/>.
*
*/
package com.keepassdroid.stream;
import java.io.IOException;
import java.io.OutputStream;
import com.keepassdroid.utils.Types;
/** Little Endian version of the DataOutputStream
* @author bpellin
*
*/
public class LEDataOutputStream extends OutputStream {
private OutputStream baseStream;
public LEDataOutputStream(OutputStream out) {
baseStream = out;
}
public void writeUInt(long uint) throws IOException {
baseStream.write(Types.writeInt((int) uint));
}
@Override
public void close() throws IOException {
baseStream.close();
}
@Override
public void flush() throws IOException {
baseStream.flush();
}
@Override
public void write(byte[] buffer, int offset, int count) throws IOException {
baseStream.write(buffer, offset, count);
}
@Override
public void write(byte[] buffer) throws IOException {
baseStream.write(buffer);
}
@Override
public void write(int oneByte) throws IOException {
baseStream.write(oneByte);
}
public void writeLong(long val) throws IOException {
byte[] buf = new byte[8];
writeLong(val, buf, 0);
baseStream.write(buf);
}
public void writeInt(int val) throws IOException {
byte[] buf = new byte[4];
writeInt(val, buf, 0);
baseStream.write(buf);
}
/**
* Write a 32-bit value.
*
* @param val
* @param buf
* @param offset
*/
public static void writeInt( int val, byte[] buf, int offset ) {
buf[offset + 0] = (byte)(val & 0xFF);
buf[offset + 1] = (byte)((val >>> 8) & 0xFF);
buf[offset + 2] = (byte)((val >>> 16) & 0xFF);
buf[offset + 3] = (byte)((val >>> 24) & 0xFF);
}
public static void writeLong( long val, byte[] buf, int offset ) {
buf[offset + 0] = (byte)(val & 0xFF);
buf[offset + 1] = (byte)((val >>> 8) & 0xFF);
buf[offset + 2] = (byte)((val >>> 16) & 0xFF);
buf[offset + 3] = (byte)((val >>> 24) & 0xFF);
buf[offset + 4] = (byte)((val >>> 32) & 0xFF);
buf[offset + 5] = (byte)((val >>> 40) & 0xFF);
buf[offset + 6] = (byte)((val >>> 48) & 0xFF);
buf[offset + 7] = (byte)((val >>> 56) & 0xFF);
}
}
+4 -66
View File
@@ -30,7 +30,8 @@ import java.io.OutputStream;
import java.io.UnsupportedEncodingException;
import java.util.UUID;
import com.keepassdroid.stream.BetterDataInputStream;
import com.keepassdroid.stream.LEDataOutputStream;
/**
* Tools for slicing and dicing Java and KeePass data types.
@@ -39,72 +40,9 @@ import com.keepassdroid.stream.BetterDataInputStream;
*/
public class Types {
public static long readLong( byte buf[], int offset ) {
return ((long)buf[offset + 0] & 0xFF) + (((long)buf[offset + 1] & 0xFF) << 8)
+ (((long)buf[offset + 2] & 0xFF) << 16) + (((long)buf[offset + 3] & 0xFF) << 24)
+ (((long)buf[offset + 4] & 0xFF) << 32) + (((long)buf[offset + 5] & 0xFF) << 40)
+ (((long)buf[offset + 6] & 0xFF) << 48) + (((long)buf[offset + 7] & 0xFF) << 56);
}
public static void writeLong( long val, byte[] buf, int offset ) {
buf[offset + 0] = (byte)(val & 0xFF);
buf[offset + 1] = (byte)((val >>> 8) & 0xFF);
buf[offset + 2] = (byte)((val >>> 16) & 0xFF);
buf[offset + 3] = (byte)((val >>> 24) & 0xFF);
buf[offset + 4] = (byte)((val >>> 32) & 0xFF);
buf[offset + 5] = (byte)((val >>> 40) & 0xFF);
buf[offset + 6] = (byte)((val >>> 48) & 0xFF);
buf[offset + 7] = (byte)((val >>> 56) & 0xFF);
}
/**
* Read a 32-bit value.
*
* @param buf
* @param offset
* @return
*/
public static int readInt( byte buf[], int offset ) {
return (buf[offset + 0] & 0xFF) + ((buf[offset + 1] & 0xFF) << 8) + ((buf[offset + 2] & 0xFF) << 16)
+ ((buf[offset + 3] & 0xFF) << 24);
}
public static long readUInt( byte buf[], int offset ) {
return (readInt(buf, offset) & BetterDataInputStream.INT_TO_LONG_MASK);
}
public static int readInt(InputStream is) throws IOException {
public static byte[] writeInt(int val) {
byte[] buf = new byte[4];
is.read(buf, 0, 4);
return Types.readInt(buf, 0);
}
public static long readUInt(InputStream is) throws IOException {
return (readInt(is) & BetterDataInputStream.INT_TO_LONG_MASK);
}
/**
* Write a 32-bit value.
*
* @param val
* @param buf
* @param offset
*/
public static void writeInt( int val, byte[] buf, int offset ) {
buf[offset + 0] = (byte)(val & 0xFF);
buf[offset + 1] = (byte)((val >>> 8) & 0xFF);
buf[offset + 2] = (byte)((val >>> 16) & 0xFF);
buf[offset + 3] = (byte)((val >>> 24) & 0xFF);
}
public static byte[] writeInt(int val) {
byte[] buf = new byte[4];
writeInt(val, buf, 0);
LEDataOutputStream.writeInt(val, buf, 0);
return buf;
}
@@ -28,6 +28,8 @@ import junit.framework.TestCase;
import com.keepassdroid.database.PwDate;
import com.keepassdroid.stream.LEDataInputStream;
import com.keepassdroid.stream.LEDataOutputStream;
import com.keepassdroid.utils.Types;
public class TypesTest extends TestCase {
@@ -58,8 +60,8 @@ public class TypesTest extends TestCase {
setArray(orig, value, 0, 8);
long one = Types.readLong(orig, 0);
Types.writeLong(one, dest, 0);
long one = LEDataInputStream.readLong(orig, 0);
LEDataOutputStream.writeLong(one, dest, 0);
assertArrayEquals(orig, dest);
@@ -87,9 +89,9 @@ public class TypesTest extends TestCase {
setArray(orig, value, 0, 4);
int one = Types.readInt(orig, 0);
int one = LEDataInputStream.readInt(orig, 0);
Types.writeInt(one, dest, 0);
LEDataOutputStream.writeInt(one, dest, 0);
assertArrayEquals(orig, dest);