~ubuntu-branches/ubuntu/oneiric/notecase/oneiric

« back to all changes in this revision

Viewing changes to src/lib/FormatEncHtml.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: Implements Blowfish encrypted NoteCase HTML format I/O
7
 
////////////////////////////////////////////////////////////////////////////
8
 
 
9
 
#include "FormatEncHtml.h"
10
 
#include "File64Enc.h"
11
 
#include "DocumentIterator.h"
12
 
#include "SHA1.h"
13
 
#include <stdio.h>      //sprintf
14
 
 
15
 
void WriteLevel(DocumentIterator &it, File64 &file, int nParentID);
16
 
void DumpDocument(NoteDocument &doc, const char *szFile);
17
 
 
18
 
FormatEncHTML::FormatEncHTML()
19
 
{
20
 
        m_nOrigSize = 0;
21
 
}
22
 
 
23
 
FormatEncHTML::~FormatEncHTML()
24
 
{
25
 
}
26
 
 
27
 
void FormatEncHTML::SetPassword(const char *szPass)
28
 
{
29
 
        m_strPass = szPass;
30
 
}
31
 
 
32
 
int FormatEncHTML::Load(const char *szFile, NoteDocument &doc)
33
 
{
34
 
        if(m_strPass.size()==0)
35
 
                return DOC_LOAD_WRONG_PASSWORD; //password not set
36
 
 
37
 
        File64Enc objFile;      
38
 
        char szBuffer[1032];
39
 
        int nRead;
40
 
 
41
 
        m_objDoc = &doc;  //store pointer
42
 
 
43
 
        //clear old document contents
44
 
        doc.Clear();
45
 
 
46
 
        if(!objFile.Open(szFile, F64_READ|F64_SHARE_READ|F64_OPEN_EXISTING))
47
 
                return DOC_LOAD_NOT_FOUND;
48
 
 
49
 
        objFile.SetPassword(m_strPass.c_str());
50
 
 
51
 
        //
52
 
        // read encrypted format header
53
 
        //
54
 
        
55
 
        // 1. format signature text "NOTECASE" (8 bytes)
56
 
        char szFormatCode[9];
57
 
        objFile.File64::Read(szFormatCode, 8);  // "explicit qualification"
58
 
        szFormatCode[8] = '\0';
59
 
        if(0 != strcmp(szFormatCode, "NOTECASE"))
60
 
                return DOC_LOAD_FORMAT_ERROR;   //
61
 
        
62
 
        // 2. SHA-1 password hash value (20 bytes)
63
 
        char szHashRead[20] = "";
64
 
        objFile.File64::Read(szHashRead, 20);
65
 
        CSHA1 hash;
66
 
        hash.Update((BYTE *)m_strPass.c_str(), m_strPass.size());
67
 
        hash.Final();
68
 
        if(0 != memcmp(szHashRead, hash.m_digest, 20))
69
 
                return DOC_LOAD_WRONG_PASSWORD; //invalid password
70
 
        
71
 
        // 3. original (unencrpyted) file size (6 bytes)
72
 
        objFile.File64::Read((char *)&m_nOrigSize, sizeof(m_nOrigSize));
73
 
 
74
 
                        
75
 
        //TOFIX error handling
76
 
        while((nRead = objFile.Read(szBuffer, 1024)) > 0)
77
 
                Parse(szBuffer, nRead);
78
 
 
79
 
        //TOFIX use m_nOrigSize
80
 
        return DOC_LOAD_OK;
81
 
}
82
 
 
83
 
bool FormatEncHTML::Save(const char *szFile, NoteDocument &doc)
84
 
{
85
 
        if(m_strPass.size()==0)
86
 
                return false;   //password not set
87
 
 
88
 
        File64Enc objFile;      
89
 
        if(!objFile.Open(szFile, F64_WRITE|F64_SHARE_READ|F64_OPEN_NEW))
90
 
                return false;
91
 
 
92
 
        objFile.SetPassword(m_strPass.c_str());
93
 
 
94
 
        //
95
 
        // write encrypted format header
96
 
        //
97
 
        
98
 
        // 1. format signature text "NOTECASE" (8 bytes)
99
 
        objFile.File64::Write("NOTECASE", 8);
100
 
        
101
 
        // 2. SHA-1 password hash value (20 bytes)
102
 
        CSHA1 hash;
103
 
        hash.Update((BYTE *)m_strPass.c_str(), m_strPass.size());
104
 
        hash.Final();
105
 
        objFile.File64::Write((char *)hash.m_digest, 20);
106
 
                
107
 
        // 3. original (unencrpyted) file size (6 bytes)
108
 
        objFile.File64::Write((char *)&m_nOrigSize, sizeof(m_nOrigSize));//TOFIX update at the end
109
 
        
110
 
        //write header
111
 
        objFile.WriteString("<!DOCTYPE NoteCenter-File>\r\n"); //TOFIX
112
 
 
113
 
        //calc and write selected node "index"
114
 
        char szLastNote[100];
115
 
        sprintf(szLastNote, "<!--LastNote:%d-->\r\n", doc.m_nCurrentNode);
116
 
        objFile.WriteString(szLastNote);
117
 
        
118
 
        objFile.WriteString("<HTML>\r\n<HEAD>\r\n<TITLE>");
119
 
        //TOFIX write file title as document title
120
 
        objFile.WriteString("</TITLE>\r\n</HEAD>\r\n<BODY BGCOLOR=\"#FFFFFF\">\r\n");
121
 
 
122
 
        //recursively iterate through document node tree
123
 
        DocumentIterator it(doc);
124
 
        WriteLevel(it, objFile, -1);
125
 
 
126
 
        //write footer
127
 
        objFile.WriteString("</BODY>\r\n</HTML>\r\n");
128
 
        objFile.WriteFinal();
129
 
        
130
 
        //TOFIX update m_nOrigSize
131
 
 
132
 
#ifdef _DEBUG
133
 
        //DumpDocument(doc, "D:\\dump.txt");//debug code
134
 
#endif
135
 
        return true;
136
 
}
137