OLD | NEW |
1 /*- | 1 /*- |
2 * Copyright 2003,2004 Colin Percival | 2 * Copyright 2003,2004 Colin Percival |
3 * All rights reserved | 3 * All rights reserved |
4 * | 4 * |
5 * Redistribution and use in source and binary forms, with or without | 5 * Redistribution and use in source and binary forms, with or without |
6 * modification, are permitted providing that the following conditions | 6 * modification, are permitted providing that the following conditions |
7 * are met: | 7 * are met: |
8 * 1. Redistributions of source code must retain the above copyright | 8 * 1. Redistributions of source code must retain the above copyright |
9 * notice, this list of conditions and the following disclaimer. | 9 * notice, this list of conditions and the following disclaimer. |
10 * 2. Redistributions in binary form must reproduce the above copyright | 10 * 2. Redistributions in binary form must reproduce the above copyright |
(...skipping 26 matching lines...) Expand all Loading... |
37 #include "courgette/crc.h" | 37 #include "courgette/crc.h" |
38 #include "courgette/streams.h" | 38 #include "courgette/streams.h" |
39 | 39 |
40 namespace courgette { | 40 namespace courgette { |
41 | 41 |
42 BSDiffStatus MBS_ReadHeader(SourceStream* stream, MBSPatchHeader* header) { | 42 BSDiffStatus MBS_ReadHeader(SourceStream* stream, MBSPatchHeader* header) { |
43 if (!stream->Read(header->tag, sizeof(header->tag))) return READ_ERROR; | 43 if (!stream->Read(header->tag, sizeof(header->tag))) return READ_ERROR; |
44 if (!stream->ReadVarint32(&header->slen)) return READ_ERROR; | 44 if (!stream->ReadVarint32(&header->slen)) return READ_ERROR; |
45 if (!stream->ReadVarint32(&header->scrc32)) return READ_ERROR; | 45 if (!stream->ReadVarint32(&header->scrc32)) return READ_ERROR; |
46 if (!stream->ReadVarint32(&header->dlen)) return READ_ERROR; | 46 if (!stream->ReadVarint32(&header->dlen)) return READ_ERROR; |
47 if (!stream->ReadVarint32(&header->cblen)) return READ_ERROR; | |
48 if (!stream->ReadVarint32(&header->difflen)) return READ_ERROR; | |
49 if (!stream->ReadVarint32(&header->extralen)) return READ_ERROR; | |
50 | 47 |
51 // The string will have a NUL terminator that we don't use, hence '-1'. | 48 // The string will have a NUL terminator that we don't use, hence '-1'. |
52 COMPILE_ASSERT(sizeof(MBS_PATCH_HEADER_TAG) - 1 == sizeof(header->tag), | 49 COMPILE_ASSERT(sizeof(MBS_PATCH_HEADER_TAG) - 1 == sizeof(header->tag), |
53 MBS_PATCH_HEADER_TAG_must_match_header_field_size); | 50 MBS_PATCH_HEADER_TAG_must_match_header_field_size); |
54 if (memcmp(header->tag, MBS_PATCH_HEADER_TAG, 8) != 0) | 51 if (memcmp(header->tag, MBS_PATCH_HEADER_TAG, 8) != 0) |
55 return UNEXPECTED_ERROR; | 52 return UNEXPECTED_ERROR; |
56 | 53 |
57 size_t bytes_remaining = stream->Remaining(); | |
58 if (header->cblen + | |
59 header->difflen + | |
60 header->extralen != bytes_remaining) | |
61 return UNEXPECTED_ERROR; | |
62 | |
63 return OK; | 54 return OK; |
64 } | 55 } |
65 | 56 |
66 BSDiffStatus MBS_ApplyPatch(const MBSPatchHeader *header, | 57 BSDiffStatus MBS_ApplyPatch(const MBSPatchHeader *header, |
67 SourceStream* patch_stream, | 58 SourceStream* patch_stream, |
68 const uint8* old_start, size_t old_size, | 59 const uint8* old_start, size_t old_size, |
69 SinkStream* new_stream) { | 60 SinkStream* new_stream) { |
70 const uint8* old_end = old_start + old_size; | 61 const uint8* old_end = old_start + old_size; |
71 | 62 |
72 SourceStream control_stream; | 63 SourceStreamSet patch_streams; |
73 | 64 if (!patch_streams.Init(patch_stream)) |
74 const uint8* control_start = patch_stream->Buffer(); | |
75 if (!patch_stream->ReadSubstream(header->cblen, &control_stream)) | |
76 return READ_ERROR; | |
77 if (!patch_stream->Skip(header->difflen + header->extralen)) | |
78 return READ_ERROR; | |
79 if (!patch_stream->Empty()) | |
80 return READ_ERROR; | 65 return READ_ERROR; |
81 | 66 |
82 const uint8* diff_start = control_start + header->cblen; | 67 SourceStream* control_stream_copy_counts = patch_streams.stream(0); |
83 const uint8* diff_end = diff_start + header->difflen; | 68 SourceStream* control_stream_extra_counts = patch_streams.stream(1); |
84 const uint8* extra_start = diff_end; | 69 SourceStream* control_stream_seeks = patch_streams.stream(2); |
85 const uint8* extra_end = extra_start + header->extralen; | 70 SourceStream* diff_skips = patch_streams.stream(3); |
| 71 SourceStream* diff_bytes = patch_streams.stream(4); |
| 72 SourceStream* extra_bytes = patch_streams.stream(5); |
| 73 |
| 74 const uint8* extra_start = extra_bytes->Buffer(); |
| 75 const uint8* extra_end = extra_start + extra_bytes->Remaining(); |
| 76 const uint8* extra_position = extra_start; |
86 | 77 |
87 const uint8* old_position = old_start; | 78 const uint8* old_position = old_start; |
88 const uint8* diff_position = diff_start; | |
89 const uint8* extra_position = extra_start; | |
90 | 79 |
91 new_stream->Reserve(header->dlen); | 80 new_stream->Reserve(header->dlen); |
92 | 81 |
93 while (!control_stream.Empty()) { | 82 uint32 pending_diff_zeros = 0; |
| 83 if (!diff_skips->ReadVarint32(&pending_diff_zeros)) |
| 84 return UNEXPECTED_ERROR; |
| 85 |
| 86 while (!control_stream_copy_counts->Empty()) { |
94 uint32 copy_count, extra_count; | 87 uint32 copy_count, extra_count; |
95 int32 seek_adjustment; | 88 int32 seek_adjustment; |
96 if (!control_stream.ReadVarint32(©_count)) | 89 if (!control_stream_copy_counts->ReadVarint32(©_count)) |
97 return UNEXPECTED_ERROR; | 90 return UNEXPECTED_ERROR; |
98 if (!control_stream.ReadVarint32(&extra_count)) | 91 if (!control_stream_extra_counts->ReadVarint32(&extra_count)) |
99 return UNEXPECTED_ERROR; | 92 return UNEXPECTED_ERROR; |
100 if (!control_stream.ReadVarint32Signed(&seek_adjustment)) | 93 if (!control_stream_seeks->ReadVarint32Signed(&seek_adjustment)) |
101 return UNEXPECTED_ERROR; | 94 return UNEXPECTED_ERROR; |
102 | 95 |
103 #ifdef DEBUG_bsmedberg | 96 #ifdef DEBUG_bsmedberg |
104 printf("Applying block: copy: %-8u extra: %-8u seek: %+i\n", | 97 printf("Applying block: copy: %-8u extra: %-8u seek: %+i\n", |
105 copy_count, extra_count, seek_adjustment); | 98 copy_count, extra_count, seek_adjustment); |
106 #endif | 99 #endif |
107 // Byte-wise arithmetically add bytes from old file to bytes from the diff | 100 // Byte-wise arithmetically add bytes from old file to bytes from the diff |
108 // block. | 101 // block. |
109 if (copy_count > static_cast<size_t>(old_end - old_position)) | 102 if (copy_count > static_cast<size_t>(old_end - old_position)) |
110 return UNEXPECTED_ERROR; | 103 return UNEXPECTED_ERROR; |
111 if (copy_count > static_cast<size_t>(diff_end - diff_position)) | |
112 return UNEXPECTED_ERROR; | |
113 | 104 |
114 // Add together bytes from the 'old' file and the 'diff' stream. | 105 // Add together bytes from the 'old' file and the 'diff' stream. |
115 for (size_t i = 0; i < copy_count; ++i) { | 106 for (size_t i = 0; i < copy_count; ++i) { |
116 uint8 byte = old_position[i] + diff_position[i]; | 107 uint8 diff_byte = 0; |
| 108 if (pending_diff_zeros) { |
| 109 --pending_diff_zeros; |
| 110 } else { |
| 111 if (!diff_skips->ReadVarint32(&pending_diff_zeros)) |
| 112 return UNEXPECTED_ERROR; |
| 113 if (!diff_bytes->Read(&diff_byte, 1)) |
| 114 return UNEXPECTED_ERROR; |
| 115 } |
| 116 uint8 byte = old_position[i] + diff_byte; |
117 new_stream->Write(&byte, 1); | 117 new_stream->Write(&byte, 1); |
118 } | 118 } |
119 old_position += copy_count; | 119 old_position += copy_count; |
120 diff_position += copy_count; | |
121 | 120 |
122 // Copy bytes from the extra block. | 121 // Copy bytes from the extra block. |
123 if (extra_count > static_cast<size_t>(extra_end - extra_position)) | 122 if (extra_count > static_cast<size_t>(extra_end - extra_position)) |
124 return UNEXPECTED_ERROR; | 123 return UNEXPECTED_ERROR; |
125 | 124 |
126 new_stream->Write(extra_position, extra_count); | 125 new_stream->Write(extra_position, extra_count); |
127 extra_position += extra_count; | 126 extra_position += extra_count; |
128 | 127 |
129 // "seek" forwards (or backwards) in oldfile. | 128 // "seek" forwards (or backwards) in oldfile. |
130 if (old_position + seek_adjustment < old_start || | 129 if (old_position + seek_adjustment < old_start || |
131 old_position + seek_adjustment > old_end) | 130 old_position + seek_adjustment > old_end) |
132 return UNEXPECTED_ERROR; | 131 return UNEXPECTED_ERROR; |
133 | 132 |
134 old_position += seek_adjustment; | 133 old_position += seek_adjustment; |
135 } | 134 } |
136 | 135 |
137 if (diff_position != diff_end) | 136 if (!control_stream_copy_counts->Empty() || |
138 return UNEXPECTED_ERROR; | 137 !control_stream_extra_counts->Empty() || |
139 if (extra_position != extra_end) | 138 !control_stream_seeks->Empty() || |
| 139 !diff_skips->Empty() || |
| 140 !diff_bytes->Empty() || |
| 141 !extra_bytes->Empty()) |
140 return UNEXPECTED_ERROR; | 142 return UNEXPECTED_ERROR; |
141 | 143 |
142 return OK; | 144 return OK; |
143 } | 145 } |
144 | 146 |
145 BSDiffStatus ApplyBinaryPatch(SourceStream* old_stream, | 147 BSDiffStatus ApplyBinaryPatch(SourceStream* old_stream, |
146 SourceStream* patch_stream, | 148 SourceStream* patch_stream, |
147 SinkStream* new_stream) { | 149 SinkStream* new_stream) { |
148 MBSPatchHeader header; | 150 MBSPatchHeader header; |
149 BSDiffStatus ret = MBS_ReadHeader(patch_stream, &header); | 151 BSDiffStatus ret = MBS_ReadHeader(patch_stream, &header); |
150 if (ret != OK) return ret; | 152 if (ret != OK) return ret; |
151 | 153 |
152 const uint8* old_start = old_stream->Buffer(); | 154 const uint8* old_start = old_stream->Buffer(); |
153 size_t old_size = old_stream->Remaining(); | 155 size_t old_size = old_stream->Remaining(); |
154 | 156 |
155 if (old_size != header.slen) return UNEXPECTED_ERROR; | 157 if (old_size != header.slen) return UNEXPECTED_ERROR; |
156 | 158 |
157 if (CalculateCrc(old_start, old_size) != header.scrc32) | 159 if (CalculateCrc(old_start, old_size) != header.scrc32) |
158 return CRC_ERROR; | 160 return CRC_ERROR; |
159 | 161 |
160 MBS_ApplyPatch(&header, patch_stream, old_start, old_size, new_stream); | 162 MBS_ApplyPatch(&header, patch_stream, old_start, old_size, new_stream); |
161 | 163 |
162 return OK; | 164 return OK; |
163 } | 165 } |
164 | 166 |
165 } // namespace | 167 } // namespace |
OLD | NEW |