forked from Parchive/par2cmdline
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlibpar2.cpp
237 lines (216 loc) · 6.86 KB
/
libpar2.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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
/*
* par2cmdline() is a function identical to par2's main().
* It takes the exact same arguments and procudes the exact same outputs,
* has exactly the same functionality.
*
* The main difference is that after running ./compile.sh, you'll end up with a
* single file shared library that you can easily use in other projects. My main
* motivation for this is that I found no way to use the program as a library,
* and after a long and unpleasant struggle with automake an libtool decided to
* simply not use the existing makefile, nor bother with the old style of letting
* the linker sort out includes and symbol availability, but do it (nearly) the
* SQLite way and simply include all source into a single file.
*
* The code below is almost and exact copy of par2cmdline.cpp, with the
* most notable change the function name which is properly externed so you end up
* with a C library you can call from anywhere. The "work" I put in into this
* source file is figuring out the proper order of includes such that it resolves
* all symbols correctly, and fix a few things with what appears implicit 'using's
* and primary type renames.
*
* The /src dir is untouched from upstream, and will remain so in order to make it
* straightforward to track updates.
*
* Copyright Brent Huisman, par2cmdline authors
*
*******************************************************************************/
// PACKAGE and VERSION are set by configure in par2cmdline, in libpar2 below.
#define PACKAGE "libpar2"
#define VERSION "0.8.1"
#include <string>
using std::string;
#include <ostream>
using std::ostream;
#include <iostream>
using std::endl;
using std::flush;
using std::hex;
#include <iomanip>
using std::setfill;
using std::dec;
using std::setw;
using std::min;
#include <cstring>
#include <cassert>
#include "src/libpar2internal.h"
#include "src/libpar2.h" //for u64 like types
#include <stdint.h>
typedef uint8_t u8;
typedef int8_t i8;
typedef uint16_t u16;
typedef int16_t i16;
typedef uint32_t u32;
typedef int32_t i32;
//typedef uint64_t u64;
typedef int64_t i64;
#include "src/diskfile.h"
#include "src/md5.h"
#include "src/crc.h"
#include "src/galois.h"
#include "src/reedsolomon.cpp"
#include "src/reedsolomon.h"
#include "src/letype.h"
#include "src/par1fileformat.h"
#include "src/par2fileformat.h"
#include "src/criticalpacket.h"
#include "src/datablock.h"
#include "src/filechecksummer.h"
#include "src/par2creatorsourcefile.h"
#include "src/par2repairersourcefile.h"
#include "src/verificationhashtable.h"
#include "src/verificationpacket.h"
#include "src/recoverypacket.h"
#include "src/mainpacket.h"
#include "src/creatorpacket.h"
#include "src/descriptionpacket.h"
#include "src/par2creator.h"
#include "src/par1repairersourcefile.h"
#include "src/par2repairer.h"
#include "src/par1repairer.h"
#include "src/commandline.h"
#include "src/descriptionpacket.cpp"
#include "src/par2creator.cpp"
#include "src/par1fileformat.cpp"
#include "src/mainpacket.cpp"
#include "src/verificationhashtable.cpp"
#include "src/datablock.cpp"
#include "src/criticalpacket.cpp"
#include "src/par1repairersourcefile.cpp"
#include "src/galois.cpp"
#include "src/par2repairer.cpp"
#include "src/diskfile.cpp"
#include "src/par1repairer.cpp"
#include "src/commandline.cpp"
#include "src/recoverypacket.cpp"
#include "src/verificationpacket.cpp"
#include "src/par2creatorsourcefile.cpp"
#include "src/filechecksummer.cpp"
#include "src/creatorpacket.cpp"
#include "src/libpar2.cpp"
#include "src/par2fileformat.cpp"
#include "src/crc.cpp"
#include "src/md5.cpp"
#include "src/par2repairersourcefile.cpp"
// This is included here, so that cout and cerr are not used elsewhere.
#include <iostream>
#ifdef _MSC_VER
#ifdef _DEBUG
#undef THIS_FILE
static char THIS_FILE[]=__FILE__;
#define new DEBUG_NEW
#endif
#endif
#if defined _WIN32
#define LIB_PRE __declspec(dllexport)
#elif defined __unix__
#define LIB_PRE
#else
#define LIB_PRE __declspec(dllexport)
#endif
extern "C" LIB_PRE int par2cmdline(int argc_, char *argv_[])
{
int argc = argc_+1;
const char* argv[argc];
argv[0]="fake_executable_filename";
for (int i = 0; i < argc_; i++){
argv[i+1] = argv_[i];
}
#ifdef _MSC_VER
// Memory leak checking
_CrtSetDbgFlag(_CrtSetDbgFlag(_CRTDBG_REPORT_FLAG) | _CRTDBG_ALLOC_MEM_DF | /*_CRTDBG_CHECK_CRT_DF | */_CRTDBG_DELAY_FREE_MEM_DF | _CRTDBG_LEAK_CHECK_DF);
#endif
// check sizeof integers
static_assert(sizeof(u8) == 1 || sizeof(i8) == 1
|| sizeof(u16) == 2 || sizeof(i16) == 1
|| sizeof(u32) == 4 || sizeof(i32) == 1
|| sizeof(u64) == 8 || sizeof(i64) == 1,
"Error: the assumed sizes of integers is wrong!");
// Parse the command line
CommandLine *commandline = new CommandLine;
Result result = eInvalidCommandLineArguments;
if (commandline->Parse(argc, argv))
{
// Which operation was selected
switch (commandline->GetOperation())
{
case CommandLine::opCreate:
// Create recovery data
result = par2create(std::cout,
std::cerr,
commandline->GetNoiseLevel(),
commandline->GetMemoryLimit(),
commandline->GetBasePath(),
#ifdef _OPENMP
commandline->GetNumThreads(),
commandline->GetFileThreads(),
#endif
commandline->GetParFilename(),
commandline->GetExtraFiles(),
commandline->GetBlockSize(),
commandline->GetFirstRecoveryBlock(),
commandline->GetRecoveryFileScheme(),
commandline->GetRecoveryFileCount(),
commandline->GetRecoveryBlockCount()
);
break;
case CommandLine::opVerify:
case CommandLine::opRepair:
{
// Verify or Repair damaged files
switch (commandline->GetVersion())
{
case CommandLine::verPar1:
result = par1repair(std::cout,
std::cerr,
commandline->GetNoiseLevel(),
commandline->GetMemoryLimit(),
#ifdef _OPENMP
commandline->GetNumThreads(),
#endif
commandline->GetParFilename(),
commandline->GetExtraFiles(),
commandline->GetOperation() == CommandLine::opRepair,
commandline->GetPurgeFiles());
break;
case CommandLine::verPar2:
result = par2repair(std::cout,
std::cerr,
commandline->GetNoiseLevel(),
commandline->GetMemoryLimit(),
commandline->GetBasePath(),
#ifdef _OPENMP
commandline->GetNumThreads(),
commandline->GetFileThreads(),
#endif
commandline->GetParFilename(),
commandline->GetExtraFiles(),
commandline->GetOperation() == CommandLine::opRepair,
commandline->GetPurgeFiles(),
commandline->GetSkipData(),
commandline->GetSkipLeaway());
break;
default:
break;
}
}
break;
case CommandLine::opNone:
result = eSuccess;
break;
default:
break;
}
}
delete commandline;
return result;
}