-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathQuadric.cpp
209 lines (171 loc) · 5.54 KB
/
Quadric.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
/* Proof Of Concept algorithm implementation for base-2 binary to base-4 DNA-coded conversion
*
*
* Copyright 2020 Jomcraft Network
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissionsand
* limitations under the License.
*/
#ifdef __linux__
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#endif
#include <iostream>
#include <algorithm>
#include <cmath>
#include <vector>
#include <fstream>
#include "Utils.h"
bool encodeFile(const char* fileName) {
std::ifstream file(fileName, std::ifstream::binary);
const std::string fileName_s = fileName;
if (!fileExists(fileName))
{
std::cout << "No valid file provided" << "\n";
return false;
}
#ifdef _WIN32
struct _stat64 filestat;
_stat64(fileName, &filestat);
#else
struct stat64 filestat;
stat64(fileName, &filestat);
#endif
const size_t totalSize = filestat.st_size;
size_t chunkSize = 0;
chunkSize = 16 * 1024;
size_t chunks = totalSize / chunkSize;
size_t sizeLastChunk = totalSize % chunkSize;
if (sizeLastChunk != 0)
{
++chunks;
}
else
{
sizeLastChunk = chunkSize;
}
std::ofstream ofs;
ofs.open(fileName_s + ".qdc", std::ofstream::out | std::ofstream::trunc);
ofs.close();
std::ofstream readFile(fileName_s + ".qdc", std::fstream::in | std::fstream::out | std::fstream::app);
if (readFile.is_open() && file.is_open())
{
for (size_t chunk = 0; chunk < chunks; ++chunk)
{
const size_t currentChunkSize =
chunk == chunks - 1
? sizeLastChunk
: chunkSize;
std::vector<char> chunkData(currentChunkSize);
file.read(&chunkData[0], currentChunkSize);
for (const unsigned char c : chunkData)
{
readFile << ToQuadFile(c);
}
}
return true;
readFile.close();
file.close();
}
return false;
}
bool decodeFile(const char* fileName) {
std::ifstream file(fileName, std::ifstream::binary);
const std::string fileName_s = fileName;
if (!fileExists(fileName))
{
std::cout << "No valid file provided" << "\n";
return false;
}
#ifdef _WIN32
struct _stat64 filestat;
_stat64(fileName, &filestat);
#else
struct stat64 filestat;
stat64(fileName, &filestat);
#endif
const size_t totalSize = filestat.st_size;
size_t chunkSize = 0;
chunkSize = 16 * 1024;
size_t chunks = totalSize / chunkSize;
size_t sizeLastChunk = totalSize % chunkSize;
if (sizeLastChunk != 0)
{
++chunks;
}
else
{
sizeLastChunk = chunkSize;
}
std::ofstream ofs;
ofs.open(fileName_s.substr(0, fileName_s.length() - 4), std::ofstream::out | std::ofstream::trunc);
ofs.close();
std::ofstream readFile(fileName_s.substr(0, fileName_s.length() - 4), std::ios_base::binary | std::fstream::app);
if (readFile.is_open() && file.is_open())
{
for (size_t chunk = 0; chunk < chunks; ++chunk)
{
const size_t currentChunkSize =
chunk == chunks - 1
? sizeLastChunk
: chunkSize;
std::vector<char> chunkData(currentChunkSize);
file.read(&chunkData[0], currentChunkSize);
const std::string str(chunkData.begin(), chunkData.end());
std::string output = "";
const int length = (int)str.length();
const int segments = length / 4;
for (int i = 0; i < segments; i++) {
const int begin = i * 4;
const int binVal = fromBinary(str.substr(begin, 4));
readFile << (unsigned char)binVal;
}
}
readFile.close();
file.close();
return true;
}
return false;
}
int main(int argc, const char* argv[])
{
setlocale(LC_ALL, "en-US");
const std::string help = "--help";
const std::string enc = "-e";
const std::string dec = "-d";
if (argc < 2) {
std::cout << "Please use `quadric --help` for the usage guide" << "\n";
}
else if (argc < 3 && help.compare(argv[1]) == 0) {
#ifdef _WIN32
std::cout << " -- THIS DEMO HAS BEEN COMPILED FOR WINDOWS x86-64--" << "\n\n";
#else
std::cout << " -- THIS DEMO HAS BEEN COMPILED FOR LINUX / MAC--" << "\n\n";
#endif
std::cout << " -- Quadric v3 Demo Help --" << "\n\n";
std::cout << " -e <TEXT> | encode a specific file" << "\n";
std::cout << " -d <QUADv3> | decode a .qdc file" << "\n\n";
std::cout << " Copyright 2020 Jomcraft Network" << "\n";
}
else if (argc < 4) {
if (enc.compare(argv[1]) == 0) {
std::cout << (encodeFile(argv[2]) ? "File successfully wrapped!" : "Task failed!") << "\n";
}
else if (dec.compare(argv[1]) == 0) {
std::cout << (decodeFile(argv[2]) ? "File successfully unwrapped!" : "Task failed!") << "\n";
}
else {
std::cout << "Please use `quadric --help` for the usage guide" << "\n";
}
}
}