-
Notifications
You must be signed in to change notification settings - Fork 0
/
SewagePlant.cpp
165 lines (139 loc) · 3.9 KB
/
SewagePlant.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
/*
*
* Copyright (c) 2015. Lukas Dresel, Julius Lohmann, Benedikt Lorch, Burkhard Ringlein, Andreas Rupp, Yuriy Sulima, Carola Touchy
*
* This file is part of GarbageCollector.
*
* GarbageCollector is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* GarbageCollector is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with GarbageCollector. If not, see <http://www.gnu.org/licenses/>.
*
*/
#include "SewagePlant.h"
#include <iostream>
#include <errno.h>
#include <string.h>
#include <sstream>
using namespace std;
#define FRAGMENTSIZE (16*1024)
SewagePlant::SewagePlant(const string pathToOutputFolder, unsigned int initValue, uint64_t imageFileLength)
{
this->pathToOutputFolder = pathToOutputFolder;
this->pdfCount = initValue;
this->pngCount = initValue;
this->jpgCount= initValue;
this->sqliteCount = initValue;
this->imageFileLength = imageFileLength;
}
SewagePlant::~SewagePlant()
{
}
void SewagePlant::purify(uint64_t startOffset, uint64_t realLength, JobType fileType, FILE * imageFile)
{
if ((startOffset+realLength) > imageFileLength)
{
cerr << "file to purify is bigger then the image/disk" << endl;
return;
}
unsigned int fileNumber = 0;
string subdir = " ";
switch(fileType)
{
case pdf:
fileNumber = pdfCount++;
subdir = "pdf";
break;
case png:
fileNumber = pngCount++;
subdir = "png";
break;
case jpg:
fileNumber = jpgCount++;
subdir = "jpg";
break;
case sqlite:
fileNumber = sqliteCount++;
subdir = "sqlite";
break;
default:
cerr << "printOutput default fail" << endl;
}
int ret = fseek(imageFile, startOffset, SEEK_SET);
if(ret == -1 )
{
cerr << "fseek : " << strerror(errno) << endl;
return;
}
stringstream pathToNewFile;
pathToNewFile << pathToOutputFolder.c_str() << subdir << "/" << fileNumber << "." << subdir;
#ifdef DEBUG
cout << "pathToNewFile : " << pathToNewFile.str().c_str() << endl;
#endif
FILE* outputFile = fopen(pathToNewFile.str().c_str(), "w+");
if(outputFile == NULL)
{
cerr << "fopen : " << strerror(errno) << endl;
return;
}
unsigned int numberOfFragments= ((unsigned int) realLength/FRAGMENTSIZE);
size_t fragmentlength = 0;
size_t currentOffset = 0;
unsigned char buffer[FRAGMENTSIZE];
for(unsigned int i =0; i < (numberOfFragments + 1); i++)
{
#ifdef SPAM
cout << "printOutput: iteration started : " << i << endl;
#endif
if(i<numberOfFragments)
{
fragmentlength = FRAGMENTSIZE;
}else{
fragmentlength = realLength - currentOffset;
if(fragmentlength == 0 )
{
break;
}
}
size_t readen = fread(buffer, 1, fragmentlength, imageFile);
if(readen == 0)
{
if(feof(imageFile))
{
cerr << "feof imageFile" << endl;
}else if(ferror(imageFile))
{
cerr << "ferror imageFile" << endl;
}else{
cerr << "unspecified error in SewagePlant while reading imageFile" << endl;
}
break;
}
size_t written = fwrite(buffer, 1, fragmentlength , outputFile);
if(written == 0)
{
cerr << "unspecified error in SewagePlant while writing outputFile" << endl;
break;
}
currentOffset += fragmentlength;
}
fclose(outputFile);
}
void SewagePlant::condense()
{
cout << endl << dec << "Valid Files Found: " << endl;
cout << "================== " << endl;
cout << "\t PDF : " << pdfCount << endl;
cout << "\t PNG : " << pngCount << endl;
cout << "\t JPG : " << jpgCount << endl;
cout << "\t SQLite : " << sqliteCount << endl;
cout << "------------------ " << endl;
}