1
////////////////////////////////////////////////////////////////////////////
2
// NoteCase notes manager project <http://notecase.sf.net>
4
// This code is licensed under BSD license.See "license.txt" for more details.
6
// File: Implements Blowfish encrypted NoteCase HTML format I/O
7
////////////////////////////////////////////////////////////////////////////
9
#include "FormatEncHtml.h"
10
#include "File64Enc.h"
11
#include "DocumentIterator.h"
13
#include <stdio.h> //sprintf
15
void WriteLevel(DocumentIterator &it, File64 &file, int nParentID);
16
void DumpDocument(NoteDocument &doc, const char *szFile);
18
FormatEncHTML::FormatEncHTML()
23
FormatEncHTML::~FormatEncHTML()
27
void FormatEncHTML::SetPassword(const char *szPass)
32
int FormatEncHTML::Load(const char *szFile, NoteDocument &doc)
34
if(m_strPass.size()==0)
35
return DOC_LOAD_WRONG_PASSWORD; //password not set
41
m_objDoc = &doc; //store pointer
43
//clear old document contents
46
if(!objFile.Open(szFile, F64_READ|F64_SHARE_READ|F64_OPEN_EXISTING))
47
return DOC_LOAD_NOT_FOUND;
49
objFile.SetPassword(m_strPass.c_str());
52
// read encrypted format header
55
// 1. format signature text "NOTECASE" (8 bytes)
57
objFile.File64::Read(szFormatCode, 8); // "explicit qualification"
58
szFormatCode[8] = '\0';
59
if(0 != strcmp(szFormatCode, "NOTECASE"))
60
return DOC_LOAD_FORMAT_ERROR; //
62
// 2. SHA-1 password hash value (20 bytes)
63
char szHashRead[20] = "";
64
objFile.File64::Read(szHashRead, 20);
66
hash.Update((BYTE *)m_strPass.c_str(), m_strPass.size());
68
if(0 != memcmp(szHashRead, hash.m_digest, 20))
69
return DOC_LOAD_WRONG_PASSWORD; //invalid password
71
// 3. original (unencrpyted) file size (6 bytes)
72
objFile.File64::Read((char *)&m_nOrigSize, sizeof(m_nOrigSize));
75
//TOFIX error handling
76
while((nRead = objFile.Read(szBuffer, 1024)) > 0)
77
Parse(szBuffer, nRead);
79
//TOFIX use m_nOrigSize
83
bool FormatEncHTML::Save(const char *szFile, NoteDocument &doc)
85
if(m_strPass.size()==0)
86
return false; //password not set
89
if(!objFile.Open(szFile, F64_WRITE|F64_SHARE_READ|F64_OPEN_NEW))
92
objFile.SetPassword(m_strPass.c_str());
95
// write encrypted format header
98
// 1. format signature text "NOTECASE" (8 bytes)
99
objFile.File64::Write("NOTECASE", 8);
101
// 2. SHA-1 password hash value (20 bytes)
103
hash.Update((BYTE *)m_strPass.c_str(), m_strPass.size());
105
objFile.File64::Write((char *)hash.m_digest, 20);
107
// 3. original (unencrpyted) file size (6 bytes)
108
objFile.File64::Write((char *)&m_nOrigSize, sizeof(m_nOrigSize));//TOFIX update at the end
111
objFile.WriteString("<!DOCTYPE NoteCenter-File>\r\n"); //TOFIX
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);
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");
122
//recursively iterate through document node tree
123
DocumentIterator it(doc);
124
WriteLevel(it, objFile, -1);
127
objFile.WriteString("</BODY>\r\n</HTML>\r\n");
128
objFile.WriteFinal();
130
//TOFIX update m_nOrigSize
133
//DumpDocument(doc, "D:\\dump.txt");//debug code