~ubuntu-branches/ubuntu/vivid/notecase/vivid

« back to all changes in this revision

Viewing changes to src/lib/File64Enc.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Vijay(Vijay)
  • Date: 2007-06-14 00:13:48 UTC
  • mfrom: (1.1.1 upstream)
  • Revision ID: james.westby@ubuntu.com-20070614001348-z9e2vbgtenb9nhoo
Tags: 1.5.6-0ubuntu1
* New Upstream release 
*  The libgnomevfs2-dev is also added to Build-Depends 

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
////////////////////////////////////////////////////////////////////////////
2
 
// NoteCase notes manager project <http://notecase.sf.net>
3
 
//
4
 
// This code is licensed under BSD license.See "license.txt" for more details.
5
 
//
6
 
// File: Overrides large file access class to add reading/writing Blowfish encrypted data
7
 
////////////////////////////////////////////////////////////////////////////
8
 
 
9
 
#include "File64Enc.h"
10
 
 
11
 
#ifndef min
12
 
 #define min(x,y) (((x)<(y))?(x):(y))
13
 
#endif
14
 
 
15
 
File64Enc::File64Enc()
16
 
{
17
 
        m_nDataSize = 0;
18
 
}
19
 
 
20
 
File64Enc::~File64Enc()
21
 
{
22
 
 
23
 
}
24
 
 
25
 
void File64Enc::SetPassword(const char *szPass)
26
 
{
27
 
        m_crypt.Initialize((BYTE *)szPass, strlen(szPass));
28
 
}
29
 
 
30
 
int File64Enc::Read(char *szBuffer, int nLen)
31
 
{
32
 
        int nRead = File64::Read(szBuffer, nLen);
33
 
        m_crypt.Decode((BYTE *)szBuffer, (BYTE *)szBuffer, nRead);
34
 
        szBuffer[nRead] = '\0';
35
 
        return nRead;
36
 
}
37
 
 
38
 
int File64Enc::Write(const char *szBuffer, int nLen)
39
 
{
40
 
        int nPos = 0;
41
 
        int nTotalSize = 0;
42
 
        int nAvailSpace = ENC_BUFFER_SIZE-m_nDataSize;
43
 
        while(nLen > 0)
44
 
        {
45
 
                //prepare new buffer (append new data to its end)
46
 
                int nAdd = min(nLen, nAvailSpace);
47
 
                memcpy(m_szBuffer+m_nDataSize, szBuffer+nPos, nAdd);
48
 
 
49
 
                //refresh variables
50
 
                m_nDataSize += nAdd;
51
 
                nLen -= nAdd;
52
 
                nPos += nAdd;
53
 
                nAvailSpace = ENC_BUFFER_SIZE-m_nDataSize;
54
 
 
55
 
                //flush data if buffer full
56
 
                if(0 == nAvailSpace)
57
 
                        nTotalSize += WriteBuffer();
58
 
        }
59
 
 
60
 
        return nTotalSize;
61
 
}
62
 
 
63
 
int File64Enc::WriteFinal()
64
 
{
65
 
        //write anything left in the buffer
66
 
        return WriteBuffer();
67
 
}
68
 
 
69
 
int File64Enc::WriteBuffer()
70
 
{
71
 
        //encode and write buffer into the file
72
 
        if(m_nDataSize > 0)
73
 
        {
74
 
                int nOrigSize = m_nDataSize;
75
 
                int nEncSize  = m_crypt.Encode((BYTE *)m_szBuffer, (BYTE *)m_szBuffer, nOrigSize);
76
 
                File64::Write(m_szBuffer, nEncSize);
77
 
                m_nDataSize = 0;
78
 
                return nOrigSize;
79
 
        }
80
 
        return 0;
81
 
}
82