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 export to standalone executable
7
////////////////////////////////////////////////////////////////////////////
9
#include "FormatIOExecutable.h"
10
#include "FormatIOEncHtml.h"
11
#include "DocumentIterator.h"
13
#include "../config.h"
14
#include "../support.h"
15
#include <stdio.h> //fopen
23
#define strcasecmp stricmp
26
#include <sys/types.h> //chmod
30
std::string GetAppPath();
32
FormatIO_Executable::FormatIO_Executable()
34
m_nCurParentID = -1; //TOFIX use Idx for faster operation
36
m_nLastNodeLevel = -1;
38
m_nCoversionFailuresCnt = 0;
41
FormatIO_Executable::~FormatIO_Executable()
45
int FormatIO_Executable::Load(const char *szFile, NoteDocument &doc)
47
std::string strSrcExe = GetAppPath();
48
FILE *pExe = fopen(strSrcExe.c_str(), "rb");
51
//check "magic" marker adt the end of the file
52
fseek(pExe, -8, SEEK_END);
55
nRead = fread(szBuffer, 8, 1, pExe);
57
if(0 != strcmp("NOTECASE", szBuffer)){
59
return DOC_LOAD_FORMAT_ERROR;
62
//marker is correct, read document start offset
63
INT64 nStandaloneOffset = -1;
65
fseek(pExe, -16, SEEK_END);
66
int nEndOffset = ftell(pExe);
68
nRead = fread(&nStandaloneOffset, sizeof(INT64), 1, pExe);
69
//convert from network byte order (big-endian) to processor default
70
if(G_BYTE_ORDER == G_LITTLE_ENDIAN)
71
nStandaloneOffset = GINT64_FROM_BE(nStandaloneOffset);
74
//now load the document
75
FormatIO_Base *pLoader = new FormatIO_EncHTML;
76
((FormatIO_EncHTML *)pLoader)->SetLoadEmbedded(nStandaloneOffset, (INT64)nEndOffset);
77
((FormatIO_EncHTML *)pLoader)->SetPassword(doc.GetPassword());
78
int nRes = pLoader->Load(strSrcExe.c_str(), doc);
80
//copy data from the real loader
81
m_strGeneratorApp = ((FormatIO_EncHTML *)pLoader)->m_strGeneratorApp;
82
m_strGeneratorVer = ((FormatIO_EncHTML *)pLoader)->m_strGeneratorVer;
88
return DOC_LOAD_ERROR;
91
int FormatIO_Executable::Save(const char *szFile, NoteDocument &doc)
94
// first create a copy of current Notecase application file
96
std::string strSrcExe = GetAppPath();
97
FILE *pExe = fopen(strSrcExe.c_str(), "rb");
99
return DOC_SAVE_ERR_EXE_OPEN;
101
//check if this exe has already embedded document (that is not being copied)
102
// - check "magic" marker at the end of the file
103
INT64 nStandaloneOffset = -1;
104
fseek(pExe, -8, SEEK_END);
107
nRead = fread(szBuffer1, 8, 1, pExe);
109
if(0 == strcmp("NOTECASE", szBuffer1)){
110
//marker is correct, read document start offset
111
fseek(pExe, -16, SEEK_END);
112
nRead = fread(&nStandaloneOffset, sizeof(INT64), 1, pExe);
113
//convert from network byte order (big-endian) to processor default
114
if(G_BYTE_ORDER == G_LITTLE_ENDIAN)
115
nStandaloneOffset = GINT64_FROM_BE(nStandaloneOffset);
117
fseek(pExe, 0, SEEK_SET);
119
FILE *pOut = fopen(szFile, "wb");
122
return DOC_SAVE_ERR_FILE_OPEN;
126
char szBuffer[10000];
128
while((nRead = fread(szBuffer, 1, sizeof(szBuffer), pExe)) > 0){
129
if(nStandaloneOffset >= 0 && (nTotal + nRead) > nStandaloneOffset){
130
nWritten = fwrite(szBuffer, 1, (size_t)(nStandaloneOffset-nTotal), pOut);
134
nWritten = fwrite(szBuffer, 1, nRead, pOut);
141
// next append the document
143
INT64 nDocStartPos = ftell(pOut);
146
//Document is always in stored in EncHtml format (because it is compressed)
147
//if user does not have a password we use default one "NOTECASE"
148
std::string strPass = doc.GetPassword();
149
if(strPass.size() == 0)
150
strPass = "NOTECASE";
152
FormatIO_EncHTML exporter;
153
exporter.SetPassword(strPass.c_str());
154
int nRes = exporter.Save(szFile, doc, true);
155
if(DOC_SAVE_OK != nRes){
156
remove(szFile); //remove output file on error
160
//now write a header to recognize document embedded in the exe
161
pOut = fopen(szFile, "ab");
163
remove(szFile); //remove output file on error
164
return DOC_SAVE_ERR_FILE_OPEN;
167
//write offset of the begining of the document
168
//convert from processor default to network byte order (big-endian)
169
if(G_BYTE_ORDER == G_LITTLE_ENDIAN)
170
nDocStartPos = GINT64_TO_BE(nDocStartPos);
171
nWritten = fwrite(&nDocStartPos, sizeof(INT64), 1, pOut);
173
//write "magic" letters to mark embedded document
174
nWritten = fwrite("NOTECASE", strlen("NOTECASE"), 1, pOut);
178
//set default file permission (note that chmod(755) would faild for me)
179
mode_t mode = S_IRUSR|S_IWUSR|S_IXUSR|S_IRGRP|S_IROTH; //0x0744
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 export to standalone executable
7
////////////////////////////////////////////////////////////////////////////
9
#include "FormatIOExecutable.h"
10
#include "FormatIOEncHtml.h"
11
#include "DocumentIterator.h"
13
#include "../config.h"
14
#include "../support.h"
15
#include <stdio.h> //fopen
23
#define strcasecmp stricmp
26
#include <sys/types.h> //chmod
30
std::string GetAppPath();
32
FormatIO_Executable::FormatIO_Executable()
34
m_nCurParentID = -1; //TOFIX use Idx for faster operation
36
m_nLastNodeLevel = -1;
38
m_nCoversionFailuresCnt = 0;
41
FormatIO_Executable::~FormatIO_Executable()
45
int FormatIO_Executable::Load(const char *szFile, NoteDocument &doc)
47
std::string strSrcExe = GetAppPath();
48
FILE *pExe = fopen(strSrcExe.c_str(), "rb");
51
//check "magic" marker adt the end of the file
52
fseek(pExe, -8, SEEK_END);
55
nRead = fread(szBuffer, 8, 1, pExe);
57
if(0 != strcmp("NOTECASE", szBuffer)){
59
return DOC_LOAD_FORMAT_ERROR;
62
//marker is correct, read document start offset
63
INT64 nStandaloneOffset = -1;
65
fseek(pExe, -16, SEEK_END);
66
int nEndOffset = ftell(pExe);
68
nRead = fread(&nStandaloneOffset, sizeof(INT64), 1, pExe);
69
//convert from network byte order (big-endian) to processor default
70
if(G_BYTE_ORDER == G_LITTLE_ENDIAN)
71
nStandaloneOffset = GINT64_FROM_BE(nStandaloneOffset);
74
//now load the document
75
FormatIO_Base *pLoader = new FormatIO_EncHTML;
76
((FormatIO_EncHTML *)pLoader)->SetLoadEmbedded(nStandaloneOffset, (INT64)nEndOffset);
77
((FormatIO_EncHTML *)pLoader)->SetPassword(doc.GetPassword());
78
int nRes = pLoader->Load(strSrcExe.c_str(), doc);
80
//copy data from the real loader
81
m_strGeneratorApp = ((FormatIO_EncHTML *)pLoader)->m_strGeneratorApp;
82
m_strGeneratorVer = ((FormatIO_EncHTML *)pLoader)->m_strGeneratorVer;
88
return DOC_LOAD_ERROR;
91
int FormatIO_Executable::Save(const char *szFile, NoteDocument &doc)
94
// first create a copy of current Notecase application file
96
std::string strSrcExe = GetAppPath();
97
FILE *pExe = fopen(strSrcExe.c_str(), "rb");
99
return DOC_SAVE_ERR_EXE_OPEN;
101
//check if this exe has already embedded document (that is not being copied)
102
// - check "magic" marker at the end of the file
103
INT64 nStandaloneOffset = -1;
104
fseek(pExe, -8, SEEK_END);
107
nRead = fread(szBuffer1, 8, 1, pExe);
109
if(0 == strcmp("NOTECASE", szBuffer1)){
110
//marker is correct, read document start offset
111
fseek(pExe, -16, SEEK_END);
112
nRead = fread(&nStandaloneOffset, sizeof(INT64), 1, pExe);
113
//convert from network byte order (big-endian) to processor default
114
if(G_BYTE_ORDER == G_LITTLE_ENDIAN)
115
nStandaloneOffset = GINT64_FROM_BE(nStandaloneOffset);
117
fseek(pExe, 0, SEEK_SET);
119
FILE *pOut = fopen(szFile, "wb");
122
return DOC_SAVE_ERR_FILE_OPEN;
126
char szBuffer[10000];
128
while((nRead = fread(szBuffer, 1, sizeof(szBuffer), pExe)) > 0){
129
if(nStandaloneOffset >= 0 && (nTotal + nRead) > nStandaloneOffset){
130
nWritten = fwrite(szBuffer, 1, (size_t)(nStandaloneOffset-nTotal), pOut);
134
nWritten = fwrite(szBuffer, 1, nRead, pOut);
141
// next append the document
143
INT64 nDocStartPos = ftell(pOut);
146
//Document is always in stored in EncHtml format (because it is compressed)
147
//if user does not have a password we use default one "NOTECASE"
148
std::string strPass = doc.GetPassword();
149
if(strPass.size() == 0)
150
strPass = "NOTECASE";
152
FormatIO_EncHTML exporter;
153
exporter.SetPassword(strPass.c_str());
154
int nRes = exporter.Save(szFile, doc, true);
155
if(DOC_SAVE_OK != nRes){
156
remove(szFile); //remove output file on error
160
//now write a header to recognize document embedded in the exe
161
pOut = fopen(szFile, "ab");
163
remove(szFile); //remove output file on error
164
return DOC_SAVE_ERR_FILE_OPEN;
167
//write offset of the begining of the document
168
//convert from processor default to network byte order (big-endian)
169
if(G_BYTE_ORDER == G_LITTLE_ENDIAN)
170
nDocStartPos = GINT64_TO_BE(nDocStartPos);
171
nWritten = fwrite(&nDocStartPos, sizeof(INT64), 1, pOut);
173
//write "magic" letters to mark embedded document
174
nWritten = fwrite("NOTECASE", strlen("NOTECASE"), 1, pOut);
178
//set default file permission (note that chmod(755) would faild for me)
179
mode_t mode = S_IRUSR|S_IWUSR|S_IXUSR|S_IRGRP|S_IROTH; //0x0744