]> gitweb.hamatoma.de Git - reqt/commitdiff
dayly work
authorhama <hama@siduction.net>
Mon, 9 Nov 2015 22:53:36 +0000 (23:53 +0100)
committerhama <hama@siduction.net>
Mon, 9 Nov 2015 22:53:36 +0000 (23:53 +0100)
base/ReRandomizer.cpp
base/ReRandomizer.hpp
cunit/allTests.cpp
cunit/cuReCryptFileSystem.cpp
os/ReCryptFileSystem.cpp
os/ReCryptFileSystem.hpp

index 40db7fef66fc43ddb6136f162e8cbafa6221bfb5..237626600101cbae046b2984f7e73d80beef8574 100644 (file)
@@ -1079,16 +1079,17 @@ QString ReNameScrambler::decodeName(const QString& name) {
 }
 
 /**
- * Encodes a block of file content.
+ * Decodes a block of file content.
  *
  * @param source       the source block
  * @param target       OUT: the target block<br>
  *                                     Can be identical to <code>source</code> (in place replacement)
+ * @param offset       decoding start at this content
  * @return                     <code>true</code>: successful<br>
  *                                     <code>false</code>: otherwise
  */
 bool ReByteScrambler::decodeContent(const QByteArray& source,
-       QByteArray& target) {
+       QByteArray& target, int start) {
        bool rc = true;
        int offset = 0;
        target.resize(0);
@@ -1096,9 +1097,16 @@ bool ReByteScrambler::decodeContent(const QByteArray& source,
                target.resize(source.length() - offset);
                int64_t* trg = reinterpret_cast<int64_t*>(target.data());
                int count = (source.length() - offset) / sizeof(int64_t);
-               const int64_t* src = reinterpret_cast<const int64_t*>(source.constData() + offset);
+               const int64_t* src = reinterpret_cast<const int64_t*>(source.constData()
+                                                                                                                         + offset);
                int64_t last = ReRandomizer::m_primes64[0];
-               for (int ix = 0; ix < count; ix++) {
+               if (start > 0){
+                       memcpy(trg, src, start);
+                       start /= sizeof(int64_t);
+                       src += offset;
+                       trg += offset;
+               }
+               for (int ix = start; ix < count; ix++) {
                        int64_t last2 = last;
                        last = *src++;
                        int64_t rand = m_contentRandom.nextSeed64();
@@ -1131,16 +1139,23 @@ bool ReByteScrambler::decodeContent(const QByteArray& source,
  * @param source       the source buffer
  * @param target       OUT: the target buffer.
  *                                     May be identical to <code>source</code> (in place processing)
+ * @param start        the encryption begins at this index
  */
 void ReByteScrambler::encodeContent(const QByteArray& source,
-       QByteArray& target) {
+       QByteArray& target, int start) {
        target.resize(source.length());
        int64_t* trg = reinterpret_cast<int64_t*>(target.data());
        int count = source.length() / sizeof(int64_t);
        const int64_t* src = reinterpret_cast<const int64_t*>(source.constData());
        int64_t last = ReRandomizer::m_primes64[0];
 
-       for (int ix = 0; ix < count; ix++) {
+       if (start > 0){
+               memcpy(trg, src, start);
+               start /= sizeof(int64_t);
+               src += start;
+               trg += start;
+       }
+       for (int ix = start; ix < count; ix++) {
                int64_t rand = m_contentRandom.nextSeed64();
                IF_TRACE(int64_t last2 = last);
                last = *trg++ = *src++ ^ rand ^ last;
index 6cfec37e3831f577bdbe64f1e4cff71f8db5139c..c5411213060c70cb730dcf59e39ac4d0e3d4df17 100644 (file)
@@ -304,8 +304,10 @@ class ReByteScrambler{
 public:
        ReByteScrambler(ReRandomizer& contentRandom, ReLogger* logger);
 public:
-       bool decodeContent(const QByteArray& source, QByteArray& target);
-       void encodeContent(const QByteArray& source, QByteArray& target);
+       bool decodeContent(const QByteArray& source, QByteArray& target,
+                                          int offset = 0);
+       void encodeContent(const QByteArray& source, QByteArray& target,
+                                          int offset = 0);
        void initHeader(int metaInfoLength, int markerLength,
                                        const QByteArray& info = ReStringUtils::m_empty);
        bool initFromHeader(int metaInfoLength, int markerLength,
index 192e6a26c840925c3a22ade95952ccd6014289d6..661c4da01eeb9d1a5bc96f9c928a848c72d157c0 100644 (file)
@@ -94,8 +94,8 @@ static void testOs() {
        testReFileSystem();
 }
 void allTests() {
-       testBase();
        testOs();
+       testBase();
        testGui();
        if (s_allTest) {
                testBase();
index f163648068408ef90942e92b85be38894f6799fb..32dee6a4b1840295425edb7dcac0ab860c0b90a6 100644 (file)
@@ -75,7 +75,8 @@ protected:
        void init() {
                m_hostBase = ReFileUtils::tempDir("cryptfs");
                m_sourceBase = ReFileUtils::tempDir("sourcefs");
-               m_hostFs = new ReLocalFileSystem(m_hostBase, &m_logger);
+               m_hostFs = new ReLocalFileSystem("/", &m_logger);
+               m_hostFs->setDirectory(m_hostBase);
                m_cryptFs = new ReCryptFileSystem(*m_hostFs, m_contentRandom, &m_logger);
        }
        void destroy(){
@@ -99,6 +100,7 @@ protected:
        virtual void run() {
                init();
                testWriteRead();
+               destroy();
        }
 };
 void testReCryptFileSystem() {
index 8b4aa2f95438c46581f21d84cdb28b0793a87310..06f4a9444c92a96b95f091c3d15a88262cb1fd5d 100644 (file)
@@ -277,6 +277,7 @@ ReCryptDirectory::ReCryptDirectory(ReRandomizer& contentRandom,
        m_currentNode(),
        m_fileBuffer(),
        m_entryBuffer(),
+       m_smallBuffer(),
        m_blockSize(1024 * 1024),
        m_maxFileId(0)
 {
@@ -435,7 +436,8 @@ bool ReCryptDirectory::readMetaFile()
 {
        bool rc = true;
        m_list.clear();
-       QString fnMetaFile = m_parentFS->directory() + ReCryptFileSystem::NODE_META_DIR;
+       QString fnMetaFile = m_parentFS->host().directory()
+                       + ReCryptFileSystem::NODE_META_DIR;
        FILE* fp = fopen(fnMetaFile.toUtf8().constData(), "rb");
        m_maxFileId = 0;
        if (fp != NULL){
@@ -504,15 +506,16 @@ bool ReCryptDirectory::writeMetaFile()
                int length = node.length();
                meta2->m_size += length + (length < 256 ? 0 : 1);
        }
-       QString fnMetaFile = m_parentFS->directory() + ReCryptFileSystem::NODE_META_DIR;
-       FILE* fp = fopen(fnMetaFile.toUtf8().constData(), "rb");
+       QString fnMetaFile = m_parentFS->host().directory()
+                       + ReCryptFileSystem::NODE_META_DIR;
+       FILE* fp = fopen(fnMetaFile.toUtf8().constData(), "wb");
        if (fp == NULL){
                m_logger->logv(LOG_ERROR, LOC_WRITE_META_1, "cannot write (%d): %s",
                                           errno, fnMetaFile.constData());
        } else {
                m_fileBuffer.append(header);
                int ixList = 0;
-               while (ixList++ < m_list.length()){
+               while (ixList < m_list.length()){
                        const ReFileMetaData& file = m_list.at(ixList++);
                        FileEntry_t trg;
                        trg.m_created = file.m_created.toMSecsSinceEpoch();
@@ -529,13 +532,16 @@ bool ReCryptDirectory::writeMetaFile()
                        m_fileBuffer.append(node);
                        if (length >= 256)
                                m_fileBuffer.append('\0');
-                       if (m_fileBuffer.length() >= m_blockSize || ixList >= m_list.length()){
-                               m_entryBuffer.resize(0);
-                               if (m_fileBuffer.length() > m_blockSize){
-                                       m_entryBuffer = m_fileBuffer.mid(m_blockSize);
-                                       m_fileBuffer.resize(m_blockSize);
+                       int blockLength = m_fileBuffer.length();
+                       bool lastBlock = ixList >= m_list.length();
+                       if (lastBlock ||blockLength  >= m_blockSize - 512){
+                               if (! lastBlock && blockLength % sizeof(int64_t) != 0){
+                                       int newLength = blockLength - blockLength % sizeof(int64_t);
+                                       m_smallBuffer = m_fileBuffer.mid(newLength);
+                                       m_fileBuffer.resize(newLength);
                                }
-                               encodeContent(m_fileBuffer, m_fileBuffer);
+                               int offset = ixList <= 1 ? META_DIR_HEADER_LENGTH : 0;
+                               encodeContent(m_fileBuffer, m_fileBuffer, offset);
                                int nWritten = fwrite(m_fileBuffer.constData(), 1,
                                                                          m_fileBuffer.length(), fp);
                                if (nWritten != m_fileBuffer.length()){
@@ -543,8 +549,12 @@ bool ReCryptDirectory::writeMetaFile()
                                                                   "write error (%d): %s [%d/%d]", errno,
                                                                   nWritten, m_fileBuffer.length());
                                }
+                               m_fileBuffer.resize(0);
+                               if (! lastBlock && blockLength % sizeof(int64_t) != 0)
+                                       m_fileBuffer.append(m_smallBuffer);
                        }
                }
+               fclose(fp);
        }
        return rc;
 }
index 71a458476a71128082d615224b14d6e14527a83e..6a92c218cd20cc559e226b5b068b446912da6020 100644 (file)
@@ -105,6 +105,7 @@ protected:
        QString m_currentNode;
        QByteArray m_fileBuffer;
        QByteArray m_entryBuffer;
+       QByteArray m_smallBuffer;
        int m_blockSize;
        int m_maxFileId;
 };