|
11 | 11 |
|
12 | 12 | int32_t |
13 | 13 | main ( |
14 | | - int32_t ArgumentCount, |
15 | | - char* Arguments[] |
16 | | - ) |
| 14 | + int32_t ArgumentCount, |
| 15 | + char* Arguments[] |
| 16 | + ) |
17 | 17 | { |
18 | | - FILE* inputFile; |
19 | | - FILE* outputFile; |
20 | | - size_t fileSize; |
21 | | - size_t sizeRead; |
22 | | - uint32_t size; |
23 | | - uint8_t* inputBuffer; |
24 | | - uint8_t* outputBuffer; |
25 | | - struct stat stat; |
26 | | - bool decodeResult; |
27 | | - |
28 | | - inputFile = NULL; |
29 | | - outputFile = NULL; |
30 | | - inputBuffer = NULL; |
31 | | - outputBuffer = NULL; |
32 | | - |
33 | | - printf("minlzdec v.1.0.0 -- http://ionescu007.github.io/minlzma\n"); |
34 | | - printf("Copyright(c) 2020 Alex Ionescu (@aionescu)\n\n"); |
35 | | - if (ArgumentCount != 3) |
36 | | - { |
37 | | - printf("Usage: minlzdec [INPUT FILE] [OUTPUT FILE]\n"); |
38 | | - printf("Decompress INPUT FILE in the .xz format into OUTPUT FILE.\n"); |
39 | | - errno = EINVAL; |
40 | | - goto Cleanup; |
41 | | - } |
42 | | - |
43 | | - inputFile = fopen(Arguments[1], "rb"); |
44 | | - if (inputFile == 0) |
45 | | - { |
46 | | - printf("Failed to open input file: %s\n", Arguments[1]); |
47 | | - goto Cleanup; |
48 | | - } |
49 | | - |
50 | | - fstat(fileno(inputFile), &stat); |
51 | | - fileSize = stat.st_size; |
52 | | - printf("Input file size: %zd\n", fileSize); |
53 | | - |
54 | | - inputBuffer = malloc(fileSize); |
55 | | - if (inputBuffer == NULL) |
56 | | - { |
57 | | - printf("Out of memory for allocating input buffer\n"); |
58 | | - goto Cleanup; |
59 | | - } |
60 | | - |
61 | | - sizeRead = fread(inputBuffer, 1, fileSize, inputFile); |
62 | | - if (sizeRead != fileSize) |
63 | | - { |
64 | | - printf("File read failed (%zd vs %zd bytes\n", sizeRead, fileSize); |
65 | | - goto Cleanup; |
66 | | - } |
67 | | - |
68 | | - size = (uint32_t)fileSize * 16; |
69 | | - |
70 | | - outputBuffer = malloc(size); |
71 | | - if (outputBuffer == NULL) |
72 | | - { |
73 | | - printf("Out of memory for allocating output buffer\n"); |
74 | | - goto Cleanup; |
75 | | - } |
76 | | - |
77 | | - decodeResult = XzDecode(inputBuffer, size, outputBuffer, &size); |
78 | | - if (decodeResult == false) |
79 | | - { |
80 | | - printf("Decoding failed after %d bytes\n", size); |
81 | | - errno = ENOTSUP; |
82 | | - goto Cleanup; |
83 | | - } |
84 | | - |
85 | | - printf("Decompressed %d bytes\n", size); |
86 | | - |
87 | | - outputFile = fopen(Arguments[2], "wb"); |
88 | | - if (outputFile == 0) |
89 | | - { |
90 | | - printf("Failed to open output file: %s\n", Arguments[1]); |
91 | | - goto Cleanup; |
92 | | - } |
93 | | - |
94 | | - fileSize = fwrite(outputBuffer, 1, size, outputFile); |
95 | | - if (fileSize != size) |
96 | | - { |
97 | | - printf("File write failed (%zd vs %d bytes\n", fileSize, size); |
98 | | - goto Cleanup; |
99 | | - } |
100 | | - errno = 0; |
| 18 | + FILE* inputFile; |
| 19 | + FILE* outputFile; |
| 20 | + size_t fileSize; |
| 21 | + size_t sizeRead; |
| 22 | + uint32_t inputSize, outputSize; |
| 23 | + uint8_t* inputBuffer; |
| 24 | + uint8_t* outputBuffer; |
| 25 | + struct stat stat; |
| 26 | + bool decodeResult; |
| 27 | + |
| 28 | + inputFile = NULL; |
| 29 | + outputFile = NULL; |
| 30 | + inputBuffer = NULL; |
| 31 | + outputBuffer = NULL; |
| 32 | + |
| 33 | + printf("minlzdec v.1.0.5 -- http://ionescu007.github.io/minlzma\n"); |
| 34 | + printf("Copyright(c) 2020 Alex Ionescu (@aionescu)\n\n"); |
| 35 | + if (ArgumentCount != 3) |
| 36 | + { |
| 37 | + printf("Usage: minlzdec [INPUT FILE] [OUTPUT FILE]\n"); |
| 38 | + printf("Decompress INPUT FILE in the .xz format into OUTPUT FILE.\n"); |
| 39 | + errno = EINVAL; |
| 40 | + goto Cleanup; |
| 41 | + } |
| 42 | + |
| 43 | + inputFile = fopen(Arguments[1], "rb"); |
| 44 | + if (inputFile == 0) |
| 45 | + { |
| 46 | + printf("Failed to open input file: %s\n", Arguments[1]); |
| 47 | + goto Cleanup; |
| 48 | + } |
| 49 | + |
| 50 | + fstat(fileno(inputFile), &stat); |
| 51 | + fileSize = stat.st_size; |
| 52 | + printf("Input file size: %zd\n", fileSize); |
| 53 | + |
| 54 | + inputBuffer = malloc(fileSize); |
| 55 | + if (inputBuffer == NULL) |
| 56 | + { |
| 57 | + printf("Out of memory for allocating input buffer\n"); |
| 58 | + goto Cleanup; |
| 59 | + } |
| 60 | + |
| 61 | + sizeRead = fread(inputBuffer, 1, fileSize, inputFile); |
| 62 | + if (sizeRead != fileSize) |
| 63 | + { |
| 64 | + printf("File read failed (%zd vs %zd bytes\n", sizeRead, fileSize); |
| 65 | + goto Cleanup; |
| 66 | + } |
| 67 | + |
| 68 | + inputSize = (uint32_t)fileSize; |
| 69 | + decodeResult = XzDecode(inputBuffer, inputSize, outputBuffer, &outputSize); |
| 70 | + if (decodeResult == false) |
| 71 | + { |
| 72 | + printf("Decoding failed after %d bytes\n", outputSize); |
| 73 | + errno = ENOTSUP; |
| 74 | + goto Cleanup; |
| 75 | + } |
| 76 | + |
| 77 | + printf("Decompressed file will be %d bytes (%f%% ratio)\n", |
| 78 | + outputSize, (double)inputSize / (double)outputSize); |
| 79 | + |
| 80 | + outputBuffer = malloc(outputSize); |
| 81 | + if (outputBuffer == NULL) |
| 82 | + { |
| 83 | + printf("Out of memory for allocating output buffer\n"); |
| 84 | + goto Cleanup; |
| 85 | + } |
| 86 | + |
| 87 | + decodeResult = XzDecode(inputBuffer, inputSize, outputBuffer, &outputSize); |
| 88 | + if (decodeResult == false) |
| 89 | + { |
| 90 | + printf("Decoding failed after %d bytes\n", outputSize); |
| 91 | + errno = ENOTSUP; |
| 92 | + goto Cleanup; |
| 93 | + } |
| 94 | + |
| 95 | + printf("Decompressed %d bytes\n", outputSize); |
| 96 | + |
| 97 | + outputFile = fopen(Arguments[2], "wb"); |
| 98 | + if (outputFile == 0) |
| 99 | + { |
| 100 | + printf("Failed to open output file: %s\n", Arguments[1]); |
| 101 | + goto Cleanup; |
| 102 | + } |
| 103 | + |
| 104 | + fileSize = fwrite(outputBuffer, 1, outputSize, outputFile); |
| 105 | + if (fileSize != outputSize) |
| 106 | + { |
| 107 | + printf("File write failed (%zd vs %d bytes)\n", fileSize, outputSize); |
| 108 | + goto Cleanup; |
| 109 | + } |
| 110 | + errno = 0; |
101 | 111 |
|
102 | 112 | Cleanup: |
103 | | - if (outputBuffer != NULL) |
104 | | - { |
105 | | - free(outputBuffer); |
106 | | - } |
107 | | - if (inputBuffer != NULL) |
108 | | - { |
109 | | - free(inputBuffer); |
110 | | - } |
111 | | - |
112 | | - if (outputFile != 0) |
113 | | - { |
114 | | - fflush(outputFile); |
115 | | - fclose(outputFile); |
116 | | - } |
117 | | - |
118 | | - if (inputFile != 0) |
119 | | - { |
120 | | - fclose(inputFile); |
121 | | - } |
122 | | - |
123 | | - return errno; |
| 113 | + if (outputBuffer != NULL) |
| 114 | + { |
| 115 | + free(outputBuffer); |
| 116 | + } |
| 117 | + if (inputBuffer != NULL) |
| 118 | + { |
| 119 | + free(inputBuffer); |
| 120 | + } |
| 121 | + |
| 122 | + if (outputFile != 0) |
| 123 | + { |
| 124 | + fflush(outputFile); |
| 125 | + fclose(outputFile); |
| 126 | + } |
| 127 | + |
| 128 | + if (inputFile != 0) |
| 129 | + { |
| 130 | + fclose(inputFile); |
| 131 | + } |
| 132 | + |
| 133 | + return errno; |
124 | 134 | } |
0 commit comments