Panda3D
patchfile.h
Go to the documentation of this file.
1 /**
2  * PANDA 3D SOFTWARE
3  * Copyright (c) Carnegie Mellon University. All rights reserved.
4  *
5  * All use of this software is subject to the terms of the revised BSD
6  * license. You should have received a copy of this license along
7  * with this source code in a file named "LICENSE."
8  *
9  * @file patchfile.h
10  * @author darren, mike
11  * @date 1997-01-09
12  */
13 
14 #ifndef PATCHFILE_H
15 #define PATCHFILE_H
16 
17 #include "pandabase.h"
18 
19 #ifdef HAVE_OPENSSL
20 
21 #include "typedef.h"
22 #include "pnotify.h"
23 #include "filename.h"
24 #include "plist.h"
25 #include "datagram.h"
26 #include "datagramIterator.h"
27 #include "buffer.h"
28 #include "pointerTo.h"
29 #include "hashVal.h" // MD5 stuff
30 #include "ordered_vector.h"
31 #include "streamWrapper.h"
32 
33 #include <algorithm>
34 
35 
36 /**
37  *
38  */
39 class EXPCL_PANDA_EXPRESS Patchfile {
40 PUBLISHED:
41  Patchfile();
42  explicit Patchfile(PT(Buffer) buffer);
43  ~Patchfile();
44 
45  bool build(Filename file_orig, Filename file_new, Filename patch_name);
46  int read_header(const Filename &patch_file);
47 
48  int initiate(const Filename &patch_file, const Filename &file);
49  int initiate(const Filename &patch_file, const Filename &orig_file,
50  const Filename &target_file);
51  int run();
52 
53  bool apply(Filename &patch_file, Filename &file);
54  bool apply(Filename &patch_file, Filename &orig_file,
55  const Filename &target_file);
56 
57  INLINE PN_stdfloat get_progress() const;
58  MAKE_PROPERTY(progress, get_progress);
59 
60  INLINE void set_allow_multifile(bool allow_multifile);
61  INLINE bool get_allow_multifile();
62  MAKE_PROPERTY(allow_multifile, get_allow_multifile, set_allow_multifile);
63 
64  INLINE void set_footprint_length(int length);
65  INLINE int get_footprint_length();
66  INLINE void reset_footprint_length();
67  MAKE_PROPERTY(footprint_length, get_footprint_length, set_footprint_length);
68 
69  INLINE bool has_source_hash() const;
70  INLINE const HashVal &get_source_hash() const;
71  INLINE const HashVal &get_result_hash() const;
72  MAKE_PROPERTY2(source_hash, has_source_hash, get_source_hash);
73  MAKE_PROPERTY(result_hash, get_result_hash);
74 
75 private:
76  int internal_read_header(const Filename &patch_file);
77  void init(PT(Buffer) buffer);
78  void cleanup();
79 
80 private:
81  // stuff for the build operation
82  void build_hash_link_tables(const char *buffer_orig, uint32_t length_orig,
83  uint32_t *hash_table, uint32_t *link_table);
84  uint32_t calc_hash(const char *buffer);
85  void find_longest_match(uint32_t new_pos, uint32_t &copy_pos, uint16_t &copy_length,
86  uint32_t *hash_table, uint32_t *link_table, const char* buffer_orig,
87  uint32_t length_orig, const char* buffer_new, uint32_t length_new);
88  uint32_t calc_match_length(const char* buf1, const char* buf2, uint32_t max_length,
89  uint32_t min_length);
90 
91  void emit_ADD(std::ostream &write_stream, uint32_t length, const char* buffer);
92  void emit_COPY(std::ostream &write_stream, uint32_t length, uint32_t COPY_pos);
93  void emit_add_and_copy(std::ostream &write_stream,
94  uint32_t add_length, const char *add_buffer,
95  uint32_t copy_length, uint32_t copy_pos);
96  void cache_add_and_copy(std::ostream &write_stream,
97  uint32_t add_length, const char *add_buffer,
98  uint32_t copy_length, uint32_t copy_pos);
99  void cache_flush(std::ostream &write_stream);
100 
101  void write_header(std::ostream &write_stream,
102  std::istream &stream_orig, std::istream &stream_new);
103  void write_terminator(std::ostream &write_stream);
104 
105  bool compute_file_patches(std::ostream &write_stream,
106  uint32_t offset_orig, uint32_t offset_new,
107  std::istream &stream_orig, std::istream &stream_new);
108  bool compute_mf_patches(std::ostream &write_stream,
109  uint32_t offset_orig, uint32_t offset_new,
110  std::istream &stream_orig, std::istream &stream_new);
111 #ifdef HAVE_TAR
112  class TarSubfile {
113  public:
114  inline bool operator < (const TarSubfile &other) const {
115  return _name < other._name;
116  }
117  std::string _name;
118  std::streampos _header_start;
119  std::streampos _data_start;
120  std::streampos _data_end;
121  std::streampos _end;
122  };
123  typedef ov_set<TarSubfile> TarDef;
124 
125  bool read_tar(TarDef &tar, std::istream &stream);
126  bool compute_tar_patches(std::ostream &write_stream,
127  uint32_t offset_orig, uint32_t offset_new,
128  std::istream &stream_orig, std::istream &stream_new,
129  TarDef &tar_orig, TarDef &tar_new);
130 
131  // Because this is static, we can only call read_tar() one at a time--no
132  // threads, please.
133  static std::istream *_tar_istream;
134 
135  static int tar_openfunc(const char *filename, int oflags, ...);
136  static int tar_closefunc(int fd);
137  static ssize_t tar_readfunc(int fd, void *buffer, size_t nbytes);
138  static ssize_t tar_writefunc(int fd, const void *buffer, size_t nbytes);
139 #endif // HAVE_TAR
140 
141  bool do_compute_patches(const Filename &file_orig, const Filename &file_new,
142  std::ostream &write_stream,
143  uint32_t offset_orig, uint32_t offset_new,
144  std::istream &stream_orig, std::istream &stream_new);
145 
146  bool patch_subfile(std::ostream &write_stream,
147  uint32_t offset_orig, uint32_t offset_new,
148  const Filename &filename,
149  IStreamWrapper &stream_orig, std::streampos orig_start, std::streampos orig_end,
150  IStreamWrapper &stream_new, std::streampos new_start, std::streampos new_end);
151 
152  static const uint32_t _HASH_BITS;
153  static const uint32_t _HASHTABLESIZE;
154  static const uint32_t _DEFAULT_FOOTPRINT_LENGTH;
155  static const uint32_t _NULL_VALUE;
156  static const uint32_t _MAX_RUN_LENGTH;
157  static const uint32_t _HASH_MASK;
158 
159  bool _allow_multifile;
160  uint32_t _footprint_length;
161 
162  uint32_t *_hash_table;
163 
164  uint32_t _add_pos;
165  uint32_t _last_copy_pos;
166 
167  std::string _cache_add_data;
168  uint32_t _cache_copy_start;
169  uint32_t _cache_copy_length;
170 
171 private:
172  PT(Buffer) _buffer; // this is the work buffer for apply -- used to prevent virtual memory swapping
173 
174  // async patch apply state variables
175  bool _initiated;
176 
177  uint16_t _version_number;
178 
179  HashVal _MD5_ofSource;
180 
181  HashVal _MD5_ofResult;
182 
183  uint32_t _total_bytes_to_process;
184  uint32_t _total_bytes_processed;
185 
186  std::istream *_patch_stream;
187  pofstream _write_stream;
188  std::istream *_origfile_stream;
189 
190  Filename _patch_file;
191  Filename _orig_file;
192  Filename _output_file;
193  bool _rename_output_to_orig;
194  bool _delete_patchfile;
195 
196  static const uint32_t _v0_magic_number;
197  static const uint32_t _magic_number;
198  static const uint16_t _current_version;
199 };
200 
201 #include "patchfile.I"
202 
203 #endif // HAVE_OPENSSL
204 
205 #endif
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
Stores a 128-bit value that represents the hashed contents (typically MD5) of a file or buffer.
Definition: hashVal.h:31
A specialization of ordered_vector that emulates a standard STL set: one copy of each element is allo...
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
Definition: buffer.h:24
The name of a file, such as a texture file or an Egg file.
Definition: filename.h:39
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
This class provides a locking wrapper around an arbitrary istream pointer.
Definition: streamWrapper.h:59
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.