|
1 |
| -#include <iostream> |
2 |
| -#include <vector> |
3 | 1 | #include "zlib.h"
|
4 |
| -#include <cstdio> |
5 |
| -#include <cstring> |
6 | 2 |
|
7 |
| -int UnsafeInflate(int argc, char *argv[]) { |
8 |
| - // original string len = 36 |
9 |
| - char a[50] = "Hello Hello Hello Hello Hello Hello!"; |
10 |
| - // placeholder for the compressed (deflated) version of "a" |
11 |
| - char b[50]; |
12 |
| - // placeholder for the Uncompressed (inflated) version of "b" |
13 |
| - char c[50]; |
14 |
| - printf("Uncompressed size is: %lu\n", strlen(a)); |
15 |
| - printf("Uncompressed string is: %s\n", a); |
16 |
| - printf("\n----------\n\n"); |
17 |
| - |
18 |
| - // STEP 1. |
19 |
| - // zlib struct |
20 |
| - z_stream defstream; |
21 |
| - defstream.zalloc = Z_NULL; |
22 |
| - defstream.zfree = Z_NULL; |
23 |
| - defstream.opaque = Z_NULL; |
24 |
| - // setup "a" as the input and "b" as the compressed output |
25 |
| - defstream.avail_in = (uInt) strlen(a) + 1; // size of input, string + terminator |
26 |
| - defstream.next_in = (Bytef *) a; // input char array |
27 |
| - defstream.avail_out = (uInt) sizeof(b); // size of output |
28 |
| - defstream.next_out = (Bytef *) b; // output char array |
29 |
| - |
30 |
| - // the actual compression work. |
31 |
| - deflateInit(&defstream, Z_BEST_COMPRESSION); |
32 |
| - deflate(&defstream, Z_FINISH); |
33 |
| - deflateEnd(&defstream); |
34 |
| - |
35 |
| - // This is one way of getting the size of the output |
36 |
| - printf("Compressed size is: %lu\n", strlen(b)); |
37 |
| - printf("Compressed string is: %s\n", b); |
38 |
| - printf("\n----------\n\n"); |
39 |
| - // STEP 2. |
40 |
| - // inflate b into c |
41 |
| - // zlib struct |
42 |
| - z_stream infstream; |
43 |
| - infstream.zalloc = Z_NULL; |
44 |
| - infstream.zfree = Z_NULL; |
45 |
| - infstream.opaque = Z_NULL; |
46 |
| - // setup "b" as the input and "c" as the compressed output |
47 |
| - // TOTHINK: Here we can add additional step from Right operand to z_stream variable access |
48 |
| - infstream.avail_in = (uInt) ((char *) defstream.next_out - b); // size of input |
49 |
| - infstream.next_in = (Bytef *) b; // input char array |
50 |
| - infstream.avail_out = (uInt) sizeof(c); // size of output |
51 |
| - infstream.next_out = (Bytef *) c; // output char array |
52 |
| - |
53 |
| - // uLong total_out; /* total number of bytes output so far */ |
54 |
| - // the actual DE-compression work. |
55 |
| - inflateInit(&infstream); |
56 |
| - std::cout << infstream.total_out << std::endl; |
57 |
| - inflate(&infstream, Z_NO_FLUSH); |
58 |
| - std::cout << infstream.total_out << std::endl; |
59 |
| - inflateEnd(&infstream); |
60 |
| - |
61 |
| - printf("Uncompressed size is: %lu\n", strlen(c)); |
62 |
| - printf("Uncompressed string is: %s\n", c); |
63 |
| - return 0; |
64 |
| -} |
65 |
| - |
66 |
| -int UnsafeGzread() { |
67 |
| - std::cout << "enter compressed file name!\n" << std::endl; |
68 |
| - char fileName[100]; |
69 |
| - std::cin >> fileName; |
70 |
| - gzFile inFileZ = gzopen(fileName, "rb"); |
71 |
| - if (inFileZ == nullptr) { |
72 |
| - printf("Error: Failed to gzopen %s\n", fileName); |
73 |
| - exit(0); |
74 |
| - } |
75 |
| - unsigned char unzipBuffer[8192]; |
| 3 | +void UnsafeGzread(gzFile inFileZ) { |
| 4 | + const int BUFFER_SIZE = 8192; |
| 5 | + unsigned char unzipBuffer[BUFFER_SIZE]; |
76 | 6 | unsigned int unzippedBytes;
|
77 |
| - std::vector<unsigned char> unzippedData; |
78 | 7 | while (true) {
|
79 |
| - unzippedBytes = gzread(inFileZ, unzipBuffer, 8192); |
80 |
| - if (unzippedBytes > 0) { |
81 |
| - unzippedData.insert(unzippedData.end(), unzipBuffer, unzipBuffer + unzippedBytes); |
82 |
| - } else { |
| 8 | + unzippedBytes = gzread(inFileZ, unzipBuffer, BUFFER_SIZE); |
| 9 | + if (unzippedBytes <= 0) { |
83 | 10 | break;
|
84 | 11 | }
|
85 |
| - } |
86 |
| - for (auto &&i: unzippedData) |
87 |
| - std::cout << i; |
88 |
| - gzclose(inFileZ); |
89 |
| - return 0; |
90 |
| -} |
91 | 12 |
|
92 |
| -int UnsafeGzfread() { |
93 |
| - std::cout << "enter compressed file name!\n" << std::endl; |
94 |
| - char fileName[100]; |
95 |
| - std::cin >> fileName; |
96 |
| - gzFile inFileZ = gzopen(fileName, "rb"); |
97 |
| - if (inFileZ == nullptr) { |
98 |
| - printf("Error: Failed to gzopen %s\n", fileName); |
99 |
| - exit(0); |
100 |
| - } |
101 |
| - while (true) { |
102 |
| - char buffer[1000]; |
103 |
| - if (!gzfread(buffer, 999, 1, inFileZ)) { |
104 |
| - break; |
105 |
| - } |
106 |
| - } |
107 |
| - gzclose(inFileZ); |
108 |
| - return 0; |
109 |
| -} |
110 |
| - |
111 |
| -int UnsafeGzgets() { |
112 |
| - std::cout << "enter compressed file name!\n" << std::endl; |
113 |
| - char fileName[100]; |
114 |
| - std::cin >> fileName; |
115 |
| - gzFile inFileZ = gzopen(fileName, "rb"); |
116 |
| - if (inFileZ == nullptr) { |
117 |
| - printf("Error: Failed to gzopen %s\n", fileName); |
118 |
| - exit(0); |
119 |
| - } |
120 |
| - char *buffer = new char[4000000000]; |
121 |
| - char *result = gzgets(inFileZ, buffer, 1000000000); |
122 |
| - while (true) { |
123 |
| - result = gzgets(inFileZ, buffer, 1000000000); |
124 |
| - if (result == nullptr) { |
125 |
| - break; |
126 |
| - } |
| 13 | + // process buffer |
127 | 14 | }
|
128 |
| - return 0; |
129 | 15 | }
|
0 commit comments