• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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>
6 #include <fcntl.h>
7 #include <getopt.h>
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 
17 #include <iostream>
18 #include <limits>
19 
20 #include "bsdiff/bsdiff.h"
21 #include "bsdiff/bsdiff_arguments.h"
22 #include "bsdiff/constants.h"
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.
MapFile(const char * filename,size_t * filesize)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|
57 // file with options in |arguments|. Store the resulting patch in a new
58 // |patch_filename| file. Returns 0 on success.
GenerateBsdiffFromFiles(const char * old_filename,const char * new_filename,const char * patch_filename,const bsdiff::BsdiffArguments & arguments)59 int GenerateBsdiffFromFiles(const char* old_filename,
60                             const char* new_filename,
61                             const char* patch_filename,
62                             const bsdiff::BsdiffArguments& arguments) {
63   size_t oldsize;
64   uint8_t* old_buf = static_cast<uint8_t*>(MapFile(old_filename, &oldsize));
65   if (!old_buf) {
66     return 1;
67   }
68 
69   size_t newsize;
70   uint8_t* new_buf = static_cast<uint8_t*>(MapFile(new_filename, &newsize));
71   if (!new_buf) {
72     munmap(old_buf, oldsize);
73     return 1;
74   }
75 
76   std::unique_ptr<bsdiff::PatchWriterInterface> patch_writer;
77   std::vector<uint8_t> raw_data;
78 
79   if (arguments.format() == bsdiff::BsdiffFormat::kLegacy) {
80     patch_writer = bsdiff::CreateBsdiffPatchWriter(patch_filename);
81   } else if (arguments.format() == bsdiff::BsdiffFormat::kBsdf2) {
82     patch_writer = bsdiff::CreateBSDF2PatchWriter(
83         patch_filename, arguments.compressor_type(),
84         arguments.compression_quality());
85   } else if (arguments.format() == bsdiff::BsdiffFormat::kEndsley) {
86     patch_writer =
87         bsdiff::CreateEndsleyPatchWriter(&raw_data, arguments.compressor_type(),
88                                          arguments.compression_quality());
89   } else {
90     std::cerr << "unexpected bsdiff format." << std::endl;
91     return 1;
92   }
93 
94   int ret = bsdiff::bsdiff(old_buf, oldsize, new_buf, newsize,
95                            arguments.min_length(), patch_writer.get(), nullptr);
96 
97   munmap(old_buf, oldsize);
98   munmap(new_buf, newsize);
99 
100   if (!ret && arguments.format() == bsdiff::BsdiffFormat::kEndsley) {
101     // Store the raw_data on disk.
102     FILE* fp = fopen(patch_filename, "wb");
103     if (!fp) {
104       perror("Opening the patch file");
105       return 1;
106     }
107     if (raw_data.size() != fwrite(raw_data.data(), 1, raw_data.size(), fp)) {
108       perror("Writing to the patch file");
109       ret = 1;
110     }
111     fclose(fp);
112   }
113   return ret;
114 }
115 
PrintUsage(const std::string & proc_name)116 void PrintUsage(const std::string& proc_name) {
117   std::cerr << "usage: " << proc_name
118             << " [options] oldfile newfile patchfile\n";
119   std::cerr << "  --format <legacy|bsdiff40|bsdf2|endsley>  The format of the"
120                " bsdiff patch.\n"
121             << "  --minlen LEN                       The minimum match length "
122                "required to consider a match in the algorithm.\n"
123             << "  --type <bz2|brotli|nocompression>  The algorithm to compress "
124                "the patch, bsdf2 format only.\n"
125             << "  --quality                          Quality of the patch "
126                "compression, brotli only.\n";
127 }
128 
129 }  // namespace
130 
main(int argc,char * argv[])131 int main(int argc, char* argv[]) {
132   bsdiff::BsdiffArguments arguments;
133 
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);
148 }
149