Alex Deymo | 20891f9 | 2015-10-12 17:28:04 -0700 | [diff] [blame] | 1 | // Copyright 2015 The Chromium OS Authors. All rights reserved. |
| 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
| 5 | #include <err.h> |
Alex Deymo | e445830 | 2017-10-26 16:40:01 +0200 | [diff] [blame] | 6 | #include <fcntl.h> |
Tianjie Xu | 1f1cdb2 | 2017-11-20 11:05:55 -0800 | [diff] [blame] | 7 | #include <getopt.h> |
Alex Deymo | e445830 | 2017-10-26 16:40:01 +0200 | [diff] [blame] | 8 | #include <sys/mman.h> |
| 9 | #include <sys/stat.h> |
| 10 | #include <sys/types.h> |
| 11 | #include <unistd.h> |
| 12 | |
| 13 | #include <stddef.h> |
| 14 | #include <stdio.h> |
| 15 | #include <stdlib.h> |
| 16 | |
Tianjie Xu | 1f1cdb2 | 2017-11-20 11:05:55 -0800 | [diff] [blame] | 17 | #include <iostream> |
Alex Deymo | e445830 | 2017-10-26 16:40:01 +0200 | [diff] [blame] | 18 | #include <limits> |
Alex Deymo | 20891f9 | 2015-10-12 17:28:04 -0700 | [diff] [blame] | 19 | |
Alex Deymo | ddf9db5 | 2017-03-02 16:10:41 -0800 | [diff] [blame] | 20 | #include "bsdiff/bsdiff.h" |
Tianjie Xu | 1f1cdb2 | 2017-11-20 11:05:55 -0800 | [diff] [blame] | 21 | #include "bsdiff/bsdiff_arguments.h" |
| 22 | #include "bsdiff/constants.h" |
Alex Deymo | e445830 | 2017-10-26 16:40:01 +0200 | [diff] [blame] | 23 | #include "bsdiff/patch_writer_factory.h" |
| 24 | |
| 25 | namespace { |
| 26 | |
| 27 | // mmap() the passed |filename| to read-only memory and store in |filesize| the |
| 28 | // size of the file. To release the memory, call munmap with the returned |
| 29 | // pointer and filesize. In case of error returns nullptr. |
| 30 | void* MapFile(const char* filename, size_t* filesize) { |
| 31 | int fd = open(filename, O_RDONLY); |
| 32 | if (fd < 0) { |
| 33 | perror("open()"); |
| 34 | return nullptr; |
| 35 | } |
| 36 | |
| 37 | struct stat st; |
| 38 | fstat(fd, &st); |
| 39 | if (static_cast<uint64_t>(st.st_size) > std::numeric_limits<size_t>::max()) { |
| 40 | fprintf(stderr, "File too big\n"); |
| 41 | close(fd); |
| 42 | return nullptr; |
| 43 | } |
| 44 | *filesize = st.st_size; |
| 45 | |
| 46 | void* ret = mmap(nullptr, st.st_size, PROT_READ, MAP_PRIVATE, fd, 0); |
| 47 | if (ret == MAP_FAILED) { |
| 48 | perror("mmap()"); |
| 49 | close(fd); |
| 50 | return nullptr; |
| 51 | } |
| 52 | close(fd); |
| 53 | return ret; |
| 54 | } |
| 55 | |
| 56 | // Generate bsdiff patch from the |old_filename| file to the |new_filename| |
Tianjie Xu | 1f1cdb2 | 2017-11-20 11:05:55 -0800 | [diff] [blame] | 57 | // file with options in |arguments|. Store the resulting patch in a new |
| 58 | // |patch_filename| file. Returns 0 on success. |
Alex Deymo | e445830 | 2017-10-26 16:40:01 +0200 | [diff] [blame] | 59 | int GenerateBsdiffFromFiles(const char* old_filename, |
| 60 | const char* new_filename, |
Tianjie Xu | 1f1cdb2 | 2017-11-20 11:05:55 -0800 | [diff] [blame] | 61 | const char* patch_filename, |
| 62 | const bsdiff::BsdiffArguments& arguments) { |
| 63 | size_t oldsize; |
Alex Deymo | e445830 | 2017-10-26 16:40:01 +0200 | [diff] [blame] | 64 | uint8_t* old_buf = static_cast<uint8_t*>(MapFile(old_filename, &oldsize)); |
Tianjie Xu | 1f1cdb2 | 2017-11-20 11:05:55 -0800 | [diff] [blame] | 65 | if (!old_buf) { |
| 66 | return 1; |
Alex Deymo | e445830 | 2017-10-26 16:40:01 +0200 | [diff] [blame] | 67 | } |
| 68 | |
Tianjie Xu | 1f1cdb2 | 2017-11-20 11:05:55 -0800 | [diff] [blame] | 69 | size_t newsize; |
| 70 | uint8_t* new_buf = static_cast<uint8_t*>(MapFile(new_filename, &newsize)); |
| 71 | if (!new_buf) { |
Alex Deymo | e445830 | 2017-10-26 16:40:01 +0200 | [diff] [blame] | 72 | munmap(old_buf, oldsize); |
Tianjie Xu | 1f1cdb2 | 2017-11-20 11:05:55 -0800 | [diff] [blame] | 73 | return 1; |
| 74 | } |
Alex Deymo | e445830 | 2017-10-26 16:40:01 +0200 | [diff] [blame] | 75 | |
Tianjie Xu | 1f1cdb2 | 2017-11-20 11:05:55 -0800 | [diff] [blame] | 76 | std::unique_ptr<bsdiff::PatchWriterInterface> patch_writer; |
Alex Deymo | e790a3b | 2017-11-09 18:09:11 +0100 | [diff] [blame] | 77 | std::vector<uint8_t> raw_data; |
| 78 | |
Tianjie Xu | 1f1cdb2 | 2017-11-20 11:05:55 -0800 | [diff] [blame] | 79 | if (arguments.format() == bsdiff::BsdiffFormat::kLegacy) { |
| 80 | patch_writer = bsdiff::CreateBsdiffPatchWriter(patch_filename); |
| 81 | } else if (arguments.format() == bsdiff::BsdiffFormat::kBsdf2) { |
Tianjie Xu | 2e70b55 | 2018-03-02 16:22:10 -0800 | [diff] [blame] | 82 | patch_writer = bsdiff::CreateBSDF2PatchWriter(patch_filename, |
Tianjie Xu | 77833b6 | 2018-03-07 18:13:47 -0800 | [diff] [blame] | 83 | arguments.compressor_types(), |
Tianjie Xu | 2e70b55 | 2018-03-02 16:22:10 -0800 | [diff] [blame] | 84 | arguments.brotli_quality()); |
Alex Deymo | e790a3b | 2017-11-09 18:09:11 +0100 | [diff] [blame] | 85 | } else if (arguments.format() == bsdiff::BsdiffFormat::kEndsley) { |
Tianjie Xu | 2e70b55 | 2018-03-02 16:22:10 -0800 | [diff] [blame] | 86 | patch_writer = bsdiff::CreateEndsleyPatchWriter( |
Tianjie Xu | 77833b6 | 2018-03-07 18:13:47 -0800 | [diff] [blame] | 87 | &raw_data, arguments.compressor_types()[0], arguments.brotli_quality()); |
Tianjie Xu | 1f1cdb2 | 2017-11-20 11:05:55 -0800 | [diff] [blame] | 88 | } else { |
| 89 | std::cerr << "unexpected bsdiff format." << std::endl; |
| 90 | return 1; |
| 91 | } |
| 92 | |
Alex Deymo | cdd45ca | 2018-02-14 19:18:49 +0100 | [diff] [blame] | 93 | int ret = bsdiff::bsdiff(old_buf, oldsize, new_buf, newsize, |
| 94 | arguments.min_length(), patch_writer.get(), nullptr); |
| 95 | |
| 96 | munmap(old_buf, oldsize); |
| 97 | munmap(new_buf, newsize); |
| 98 | |
Alex Deymo | e790a3b | 2017-11-09 18:09:11 +0100 | [diff] [blame] | 99 | if (!ret && arguments.format() == bsdiff::BsdiffFormat::kEndsley) { |
| 100 | // Store the raw_data on disk. |
| 101 | FILE* fp = fopen(patch_filename, "wb"); |
| 102 | if (!fp) { |
| 103 | perror("Opening the patch file"); |
| 104 | return 1; |
| 105 | } |
| 106 | if (raw_data.size() != fwrite(raw_data.data(), 1, raw_data.size(), fp)) { |
| 107 | perror("Writing to the patch file"); |
| 108 | ret = 1; |
| 109 | } |
| 110 | fclose(fp); |
| 111 | } |
Alex Deymo | cdd45ca | 2018-02-14 19:18:49 +0100 | [diff] [blame] | 112 | return ret; |
Tianjie Xu | 1f1cdb2 | 2017-11-20 11:05:55 -0800 | [diff] [blame] | 113 | } |
| 114 | |
| 115 | void PrintUsage(const std::string& proc_name) { |
| 116 | std::cerr << "usage: " << proc_name |
| 117 | << " [options] oldfile newfile patchfile\n"; |
Alex Deymo | e790a3b | 2017-11-09 18:09:11 +0100 | [diff] [blame] | 118 | std::cerr << " --format <legacy|bsdiff40|bsdf2|endsley> The format of the" |
| 119 | " bsdiff patch.\n" |
| 120 | << " --minlen LEN The minimum match length " |
| 121 | "required to consider a match in the algorithm.\n" |
| 122 | << " --type <bz2|brotli|nocompression> The algorithm to compress " |
Tianjie Xu | 77833b6 | 2018-03-07 18:13:47 -0800 | [diff] [blame] | 123 | "the patch, bsdf2 format only. Multiple supported compressors " |
| 124 | "should be split by ':', e.g. bz2:brotli.\n" |
Tianjie Xu | 2e70b55 | 2018-03-02 16:22:10 -0800 | [diff] [blame] | 125 | << " --brotli_quality Quality of the brotli " |
| 126 | "compressor.\n"; |
Alex Deymo | e445830 | 2017-10-26 16:40:01 +0200 | [diff] [blame] | 127 | } |
| 128 | |
| 129 | } // namespace |
Alex Deymo | 20891f9 | 2015-10-12 17:28:04 -0700 | [diff] [blame] | 130 | |
| 131 | int main(int argc, char* argv[]) { |
Tianjie Xu | 1f1cdb2 | 2017-11-20 11:05:55 -0800 | [diff] [blame] | 132 | bsdiff::BsdiffArguments arguments; |
Alex Deymo | 20891f9 | 2015-10-12 17:28:04 -0700 | [diff] [blame] | 133 | |
Tianjie Xu | 1f1cdb2 | 2017-11-20 11:05:55 -0800 | [diff] [blame] | 134 | if (!arguments.ParseCommandLine(argc, argv)) { |
| 135 | PrintUsage(argv[0]); |
| 136 | return 1; |
| 137 | } |
| 138 | |
| 139 | // The optind will be updated in ParseCommandLine to parse the options; and |
| 140 | // we expect the rest of the arguments to be oldfile, newfile, patchfile. |
| 141 | if (!arguments.IsValid() || argc - optind != 3) { |
| 142 | PrintUsage(argv[0]); |
| 143 | return 1; |
| 144 | } |
| 145 | |
| 146 | return GenerateBsdiffFromFiles(argv[optind], argv[optind + 1], |
| 147 | argv[optind + 2], arguments); |
Alex Deymo | 20891f9 | 2015-10-12 17:28:04 -0700 | [diff] [blame] | 148 | } |