| OLD | NEW |
| (Empty) |
| 1 // Copyright 2009 Google Inc. | |
| 2 // | |
| 3 // Licensed under the Apache License, Version 2.0 (the "License"); | |
| 4 // you may not use this file except in compliance with the License. | |
| 5 // You may obtain a copy of the License at | |
| 6 // | |
| 7 // http://www.apache.org/licenses/LICENSE-2.0 | |
| 8 // | |
| 9 // Unless required by applicable law or agreed to in writing, software | |
| 10 // distributed under the License is distributed on an "AS IS" BASIS, | |
| 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | |
| 12 // See the License for the specific language governing permissions and | |
| 13 // limitations under the License. | |
| 14 // ======================================================================== | |
| 15 // | |
| 16 // BCJ encodes a file to increase its compressibility. | |
| 17 | |
| 18 #include <windows.h> | |
| 19 #include <shellapi.h> | |
| 20 | |
| 21 #include <string> | |
| 22 | |
| 23 #include "base/basictypes.h" | |
| 24 #include "base/scoped_ptr.h" | |
| 25 #include "omaha/mi_exe_stub/x86_encoder/bcj2_encoder.h" | |
| 26 #include "third_party/smartany/scoped_any.h" | |
| 27 | |
| 28 int wmain(int argc, WCHAR* argv[], WCHAR* env[]) { | |
| 29 UNREFERENCED_PARAMETER(env); | |
| 30 | |
| 31 if (argc < 3) { | |
| 32 return 1; | |
| 33 } | |
| 34 | |
| 35 // argv[1] is the input file, argv[2] is the output file. | |
| 36 scoped_hfile file(::CreateFile(argv[1], GENERIC_READ, 0, | |
| 37 NULL, OPEN_EXISTING, 0, NULL)); | |
| 38 if (!valid(file)) { | |
| 39 return 2; | |
| 40 } | |
| 41 | |
| 42 LARGE_INTEGER file_size_data; | |
| 43 if (!::GetFileSizeEx(get(file), &file_size_data)) { | |
| 44 return 3; | |
| 45 } | |
| 46 | |
| 47 DWORD file_size = static_cast<DWORD>(file_size_data.QuadPart); | |
| 48 scoped_array<uint8> buffer(new uint8[file_size]); | |
| 49 DWORD bytes_read = 0; | |
| 50 if (!::ReadFile(get(file), buffer.get(), file_size, &bytes_read, NULL) || | |
| 51 bytes_read != file_size) { | |
| 52 return 4; | |
| 53 } | |
| 54 | |
| 55 std::string out1; | |
| 56 std::string out2; | |
| 57 std::string out3; | |
| 58 std::string out4; | |
| 59 if (!omaha::Bcj2Encode(std::string(reinterpret_cast<char*>(buffer.get()), | |
| 60 file_size), | |
| 61 &out1, &out2, &out3, &out4)) { | |
| 62 return 5; | |
| 63 } | |
| 64 | |
| 65 // The format of BCJ2 file is very primitive. | |
| 66 // Header is 5 32-bit ints, with the following information: | |
| 67 // unpacked size | |
| 68 // size of stream 1 | |
| 69 // size of stream 2 | |
| 70 // size of stream 3 | |
| 71 // size of stream 4 | |
| 72 const DWORD output_buffer_length = 5 * sizeof(uint32) + // NOLINT | |
| 73 out1.size() + out2.size() + out3.size() + | |
| 74 out4.size(); | |
| 75 DWORD buffer_remaining = output_buffer_length; | |
| 76 scoped_array<uint8> output_buffer(new uint8[output_buffer_length]); | |
| 77 uint8* p = output_buffer.get(); | |
| 78 *reinterpret_cast<uint32*>(p) = bytes_read; | |
| 79 p += sizeof(uint32); // NOLINT | |
| 80 buffer_remaining -= sizeof(uint32); // NOLINT | |
| 81 *reinterpret_cast<uint32*>(p) = out1.size(); | |
| 82 p += sizeof(uint32); // NOLINT | |
| 83 buffer_remaining -= sizeof(uint32); // NOLINT | |
| 84 *reinterpret_cast<uint32*>(p) = out2.size(); | |
| 85 p += sizeof(uint32); // NOLINT | |
| 86 buffer_remaining -= sizeof(uint32); // NOLINT | |
| 87 *reinterpret_cast<uint32*>(p) = out3.size(); | |
| 88 p += sizeof(uint32); // NOLINT | |
| 89 buffer_remaining -= sizeof(uint32); // NOLINT | |
| 90 *reinterpret_cast<uint32*>(p) = out4.size(); | |
| 91 p += sizeof(uint32); // NOLINT | |
| 92 buffer_remaining -= sizeof(uint32); // NOLINT | |
| 93 if (!out1.empty()) { | |
| 94 if (0 != memcpy_s(p, buffer_remaining, &out1[0], out1.size())) { | |
| 95 return 9; | |
| 96 } | |
| 97 p += out1.size(); | |
| 98 buffer_remaining -= out1.size(); | |
| 99 } | |
| 100 if (!out2.empty()) { | |
| 101 if (0 != memcpy_s(p, buffer_remaining, &out2[0], out2.size())) { | |
| 102 return 10; | |
| 103 } | |
| 104 p += out2.size(); | |
| 105 buffer_remaining -= out2.size(); | |
| 106 } | |
| 107 if (!out3.empty()) { | |
| 108 if (0 != memcpy_s(p, buffer_remaining, &out3[0], out3.size())) { | |
| 109 return 11; | |
| 110 } | |
| 111 p += out3.size(); | |
| 112 buffer_remaining -= out3.size(); | |
| 113 } | |
| 114 if (!out4.empty()) { | |
| 115 if (0 != memcpy_s(p, buffer_remaining, &out4[0], out4.size())) { | |
| 116 return 12; | |
| 117 } | |
| 118 p += out4.size(); | |
| 119 buffer_remaining -= out4.size(); | |
| 120 } | |
| 121 if (p != output_buffer.get() + output_buffer_length || | |
| 122 0 != buffer_remaining) { | |
| 123 return 8; | |
| 124 } | |
| 125 | |
| 126 reset(file, ::CreateFile(argv[2], GENERIC_WRITE, 0, NULL, CREATE_ALWAYS, 0, | |
| 127 NULL)); | |
| 128 if (!valid(file)) { | |
| 129 return 6; | |
| 130 } | |
| 131 | |
| 132 DWORD bytes_written = 0; | |
| 133 if (!::WriteFile(get(file), output_buffer.get(), output_buffer_length, | |
| 134 &bytes_written, NULL)) { | |
| 135 return 7; | |
| 136 } | |
| 137 | |
| 138 return 0; | |
| 139 } | |
| OLD | NEW |