|
4 | 4 | #include <fstream> |
5 | 5 | #include <streambuf> |
6 | 6 |
|
| 7 | +#include "../WindowInjection/img.h" |
| 8 | + |
7 | 9 | void print_usage(const char* exe) |
8 | 10 | { |
9 | | - printf("usage: %s <img file>\n", exe); |
| 11 | + printf("usage: %s <load|save> <img file>\n", exe); |
10 | 12 | } |
11 | 13 |
|
| 14 | +int load_img(const char* infile); |
| 15 | +int save_img(const char* outfile); |
| 16 | + |
12 | 17 | int write_imgdata(const std::vector<char>& data, const char* outfile); |
13 | 18 |
|
14 | 19 | int main(int argc, char* argv[]) |
15 | 20 | { |
16 | | - if (argc != 2) |
| 21 | + if (argc != 3) |
17 | 22 | { |
18 | 23 | print_usage(argv[0]); |
19 | 24 | return 0; |
20 | 25 | } |
21 | 26 |
|
22 | | - const char* infile = argv[1]; |
23 | | - const char* outfile = "../WindowInjection/img.cpp"; |
24 | | - |
25 | | - std::ifstream ifs(infile, std::ios::binary); |
26 | | - if (!ifs.is_open()) |
| 27 | + std::string cmd(argv[1]); |
| 28 | + if (cmd == "load") |
27 | 29 | { |
28 | | - printf("Failed to open %s\n", infile); |
29 | | - return 0; |
| 30 | + printf("begin load image\n"); |
| 31 | + return load_img(argv[2]); |
30 | 32 | } |
31 | | - |
32 | | - std::vector<char> imgdata( |
33 | | - (std::istreambuf_iterator<char>(ifs)), |
34 | | - std::istreambuf_iterator<char>()); |
35 | | - |
36 | | - if (0 != write_imgdata(imgdata, outfile)) |
| 33 | + else if (cmd == "save") |
37 | 34 | { |
38 | | - printf("Failed to write image data, %s -> %s\n", infile, outfile); |
| 35 | + printf("begin save image\n"); |
| 36 | + return save_img(argv[2]); |
39 | 37 | } |
40 | 38 | else |
41 | 39 | { |
42 | | - printf("Succeeded to write image data, %s -> %s\n", infile, outfile); |
| 40 | + print_usage(argv[0]); |
| 41 | + return 0; |
43 | 42 | } |
44 | | - return 0; |
45 | 43 | } |
46 | 44 |
|
47 | 45 | int write_imgdata(const std::vector<char>& data, const char* outfile) |
@@ -78,3 +76,43 @@ int write_imgdata(const std::vector<char>& data, const char* outfile) |
78 | 76 |
|
79 | 77 | return 0; |
80 | 78 | } |
| 79 | + |
| 80 | +int load_img(const char* infile) |
| 81 | +{ |
| 82 | + const char* outfile = "../WindowInjection/img.cpp"; |
| 83 | + |
| 84 | + std::ifstream ifs(infile, std::ios::binary); |
| 85 | + if (!ifs.is_open()) |
| 86 | + { |
| 87 | + printf("Failed to open %s\n", infile); |
| 88 | + return 0; |
| 89 | + } |
| 90 | + |
| 91 | + std::vector<char> imgdata( |
| 92 | + (std::istreambuf_iterator<char>(ifs)), |
| 93 | + std::istreambuf_iterator<char>()); |
| 94 | + |
| 95 | + if (0 != write_imgdata(imgdata, outfile)) |
| 96 | + { |
| 97 | + printf("Failed to write image data, %s -> %s\n", infile, outfile); |
| 98 | + } |
| 99 | + else |
| 100 | + { |
| 101 | + printf("Succeeded to write image data, %s -> %s\n", infile, outfile); |
| 102 | + } |
| 103 | + return 0; |
| 104 | +} |
| 105 | + |
| 106 | +int save_img(const char* outfile) |
| 107 | +{ |
| 108 | + std::ofstream ofs(outfile, std::ios::binary|std::ios::trunc); |
| 109 | + if (!ofs.is_open()) |
| 110 | + { |
| 111 | + printf("Failed to open %s\n", outfile); |
| 112 | + return 0; |
| 113 | + } |
| 114 | + |
| 115 | + std::copy(g_img.begin(), g_img.end(), std::ostreambuf_iterator<char>(ofs)); |
| 116 | + printf("Succeeded to write to %s\n", outfile); |
| 117 | + return 0; |
| 118 | +} |
0 commit comments