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