-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathMakeMogg.cpp
More file actions
147 lines (123 loc) · 3.29 KB
/
MakeMogg.cpp
File metadata and controls
147 lines (123 loc) · 3.29 KB
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
// moggcrypt-cpp.cpp : Defines the entry point for the console application.
#include <iostream>
#include <cstring>
#include <fstream>
#include "VorbisEncrypter.h"
#include "OggMap.h"
#include "CCallbacks.h"
#define VERSION "1.0.0"
int usage(const char* name) {
char* usage = "Usage: \n"
"To encrypt a mogg: %s <input_mogg> -e <output_encrypted_mogg>\n"
"To create a mogg from an ogg: %s <input_ogg> -m <output_mogg>\n"
"To do both: %s <input_ogg> -em <output_encrypted_mogg>\n"
"\n\nVersion " VERSION "\n";
printf(usage, name, name, name);
return 0;
}
int fail(const char* msg) {
printf("Failed!\n\nMessage: %s\n", msg);
return 1;
}
int encryptOgg(const char* in, const char* out) {
std::ifstream infile(in, std::ios::in | std::ios::binary);
if (!infile.is_open()) {
return fail("Could not open input file");
}
std::ofstream outfile(out, std::ios::out | std::ios::binary);
if (!outfile.is_open()) {
return fail("Could not open output file");
}
try {
VorbisEncrypter ve(&infile, cppCallbacks);
char buf[8192];
size_t read = 0;
do {
read = ve.ReadRaw(buf, 1, 8192);
outfile.write(buf, read);
} while (read != 0);
} catch(std::exception& e) {
printf("Error: %s", e.what());
return 1;
}
return 0;
}
int mapAndEncryptOgg(const char* in, const char* out) {
std::ifstream infile(in, std::ios::in | std::ios::binary);
if (!infile.is_open()) {
return fail("Could not open input file");
}
std::ofstream outfile(out, std::ios::out | std::ios::binary);
if (!outfile.is_open()) {
return fail("Could not open output file");
}
try {
VorbisEncrypter ve(&infile, 0x10, cppCallbacks);
char buf[8192];
size_t read = 0;
do {
read = ve.ReadRaw(buf, 1, 8192);
outfile.write(buf, read);
} while (read != 0);
} catch(std::exception& e) {
printf("Error: %s", e.what());
return 1;
}
return 0;
}
int mapOgg(const char* in, const char* out)
{
int ret = 0;
std::ifstream infile(in, std::ios::in | std::ios::binary);
if (!infile.is_open()) {
return fail("Could not open input file");
}
std::ofstream outfile(out, std::ios::out | std::ios::binary);
if (!outfile.is_open()) {
return fail("Could not open output file");
}
auto result = OggMap::Create(&infile, cppCallbacks);
if (std::holds_alternative<std::string>(result)) {
printf("Error creating OggMap\n%s\n", std::get<std::string>(result).c_str());
return 1;
}
auto& map = std::get<OggMap>(result);
auto mapData = map.Serialize();
int oggVersion = 0xA;
int fileOffset = 8 + mapData.size();
outfile.write((char*)&oggVersion, sizeof(int));
outfile.write((char*)&fileOffset, sizeof(int));
outfile.write(mapData.data(), mapData.size());
// Copy the audio data
infile.clear();
infile.seekg(0);
size_t read = 0;
char copyBuf[8192];
do {
infile.read(copyBuf, 8192);
outfile.write(copyBuf, infile.gcount());
} while (infile.gcount() > 0);
return 0;
}
int main(int argc, char* argv[])
{
if (argc != 4 || argv[2][0] != '-') {
return usage(argv[0]);
}
char* infilename = argv[1];
char* mode = argv[2];
char* outfilename = argv[3];
if (!strcmp(mode, "-em")) {
return mapAndEncryptOgg(infilename, outfilename);
}
switch (mode[1]) {
case 'e':
return encryptOgg(infilename, outfilename);
break;
case 'm':
return mapOgg(infilename, outfilename);
break;
default:
return usage(argv[0]);
}
}