From a3a009e03dc6577a3d02d2a5431274dfaf7b9565 Mon Sep 17 00:00:00 2001 From: James Ahlborn Date: Sat, 27 Nov 2010 04:31:32 +0000 Subject: [PATCH] minor refactors git-svn-id: https://svn.code.sf.net/p/jackcess/code/jackcess/trunk@500 f203690c-595d-4dc9-a70b-905162fa7fd2 --- .../jackcess/Database.java | 59 +++++++++++-------- .../jackcess/JetFormat.java | 4 +- 2 files changed, 36 insertions(+), 27 deletions(-) diff --git a/src/java/com/healthmarketscience/jackcess/Database.java b/src/java/com/healthmarketscience/jackcess/Database.java index c15977b..b8dcc94 100644 --- a/src/java/com/healthmarketscience/jackcess/Database.java +++ b/src/java/com/healthmarketscience/jackcess/Database.java @@ -1129,8 +1129,15 @@ public class Database _buffer.position(_format.OFFSET_PASSWORD); _buffer.get(pwdBytes); - // de-mask password - applyPasswordMask(pwdBytes); + // de-mask password using extra password mask if necessary (the extra + // password mask is generated from the database creation date stored in + // the header) + byte[] pwdMask = getPasswordMask(_buffer, _format); + if(pwdMask != null) { + for(int i = 0; i < pwdBytes.length; ++i) { + pwdBytes[i] ^= pwdMask[i % pwdMask.length]; + } + } boolean hasPassword = false; for(int i = 0; i < pwdBytes.length; ++i) { @@ -1155,30 +1162,6 @@ public class Database return pwd; } - /** - * Applies an extra mask to the password if required for the current - * JetFormat. - */ - private void applyPasswordMask(byte[] pwdBytes) - { - // apply extra password mask if necessary (the extra password mask is - // generated from the database creation date stored in the header) - int pwdMaskPos = _format.OFFSET_HEADER_DATE; - if(pwdMaskPos >= 0) { - - _buffer.position(pwdMaskPos); - double dateVal = Double.longBitsToDouble(_buffer.getLong()); - - byte[] pwdMask = new byte[4]; - ByteBuffer.wrap(pwdMask).order(PageChannel.DEFAULT_BYTE_ORDER) - .putInt((int)dateVal); - - for(int i = 0; i < pwdBytes.length; ++i) { - pwdBytes[i] ^= pwdMask[i % pwdMask.length]; - } - } - } - /** * Finds the relationships matching the given from and to tables from the * given cursor and adds them to the given list. @@ -1635,6 +1618,30 @@ public class Database } } + /** + * Returns the password mask retrieved from the given header page and + * format, or {@code null} if this format does not use a password mask. + */ + static byte[] getPasswordMask(ByteBuffer buffer, JetFormat format) + { + // get extra password mask if necessary (the extra password mask is + // generated from the database creation date stored in the header) + int pwdMaskPos = format.OFFSET_HEADER_DATE; + if(pwdMaskPos < 0) { + return null; + } + + buffer.position(pwdMaskPos); + double dateVal = Double.longBitsToDouble(buffer.getLong()); + + byte[] pwdMask = new byte[4]; + ByteBuffer.wrap(pwdMask).order(PageChannel.DEFAULT_BYTE_ORDER) + .putInt((int)dateVal); + + return pwdMask; + } + + /** * Utility class for storing table page number and actual name. */ diff --git a/src/java/com/healthmarketscience/jackcess/JetFormat.java b/src/java/com/healthmarketscience/jackcess/JetFormat.java index 0922ab0..909b8be 100644 --- a/src/java/com/healthmarketscience/jackcess/JetFormat.java +++ b/src/java/com/healthmarketscience/jackcess/JetFormat.java @@ -64,7 +64,9 @@ public abstract class JetFormat { private static final byte CODE_VERSION_5 = 0x2; /** location of the engine name in the header */ - private static final int OFFSET_ENGINE_NAME = 0x4; + static final int OFFSET_ENGINE_NAME = 0x4; + /** length of the engine name in the header */ + static final int LENGTH_ENGINE_NAME = 0xF; /** amount of initial data to be read to determine database type */ private static final int HEADER_LENGTH = 21; -- 2.39.5