1 /* 2 * Copyright (C) 2009 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 // See imgdiff.c in this directory for a description of the patch file 18 // format. 19 20 #include <stdio.h> 21 #include <sys/stat.h> 22 #include <errno.h> 23 #include <unistd.h> 24 #include <string.h> 25 26 #include "zlib.h" 27 #include "mincrypt/sha.h" 28 #include "applypatch.h" 29 #include "imgdiff.h" 30 #include "utils.h" 31 32 /* 33 * Apply the patch given in 'patch_filename' to the source data given 34 * by (old_data, old_size). Write the patched output to the 'output' 35 * file, and update the SHA context with the output data as well. 36 * Return 0 on success. 37 */ 38 int ApplyImagePatch(const unsigned char* old_data, ssize_t old_size, 39 const Value* patch, 40 SinkFn sink, void* token, SHA_CTX* ctx, 41 const Value* bonus_data) { 42 ssize_t pos = 12; 43 char* header = patch->data; 44 if (patch->size < 12) { 45 printf("patch too short to contain header\n"); 46 return -1; 47 } 48 49 // IMGDIFF2 uses CHUNK_NORMAL, CHUNK_DEFLATE, and CHUNK_RAW. 50 // (IMGDIFF1, which is no longer supported, used CHUNK_NORMAL and 51 // CHUNK_GZIP.) 52 if (memcmp(header, "IMGDIFF2", 8) != 0) { 53 printf("corrupt patch file header (magic number)\n"); 54 return -1; 55 } 56 57 int num_chunks = Read4(header+8); 58 59 int i; 60 for (i = 0; i < num_chunks; ++i) { 61 // each chunk's header record starts with 4 bytes. 62 if (pos + 4 > patch->size) { 63 printf("failed to read chunk %d record\n", i); 64 return -1; 65 } 66 int type = Read4(patch->data + pos); 67 pos += 4; 68 69 if (type == CHUNK_NORMAL) { 70 char* normal_header = patch->data + pos; 71 pos += 24; 72 if (pos > patch->size) { 73 printf("failed to read chunk %d normal header data\n", i); 74 return -1; 75 } 76 77 size_t src_start = Read8(normal_header); 78 size_t src_len = Read8(normal_header+8); 79 size_t patch_offset = Read8(normal_header+16); 80 81 ApplyBSDiffPatch(old_data + src_start, src_len, 82 patch, patch_offset, sink, token, ctx); 83 } else if (type == CHUNK_RAW) { 84 char* raw_header = patch->data + pos; 85 pos += 4; 86 if (pos > patch->size) { 87 printf("failed to read chunk %d raw header data\n", i); 88 return -1; 89 } 90 91 ssize_t data_len = Read4(raw_header); 92 93 if (pos + data_len > patch->size) { 94 printf("failed to read chunk %d raw data\n", i); 95 return -1; 96 } 97 SHA_update(ctx, patch->data + pos, data_len); 98 if (sink((unsigned char*)patch->data + pos, 99 data_len, token) != data_len) { 100 printf("failed to write chunk %d raw data\n", i); 101 return -1; 102 } 103 pos += data_len; 104 } else if (type == CHUNK_DEFLATE) { 105 // deflate chunks have an additional 60 bytes in their chunk header. 106 char* deflate_header = patch->data + pos; 107 pos += 60; 108 if (pos > patch->size) { 109 printf("failed to read chunk %d deflate header data\n", i); 110 return -1; 111 } 112 113 size_t src_start = Read8(deflate_header); 114 size_t src_len = Read8(deflate_header+8); 115 size_t patch_offset = Read8(deflate_header+16); 116 size_t expanded_len = Read8(deflate_header+24); 117 size_t target_len = Read8(deflate_header+32); 118 int level = Read4(deflate_header+40); 119 int method = Read4(deflate_header+44); 120 int windowBits = Read4(deflate_header+48); 121 int memLevel = Read4(deflate_header+52); 122 int strategy = Read4(deflate_header+56); 123 124 // Decompress the source data; the chunk header tells us exactly 125 // how big we expect it to be when decompressed. 126 127 // Note: expanded_len will include the bonus data size if 128 // the patch was constructed with bonus data. The 129 // deflation will come up 'bonus_size' bytes short; these 130 // must be appended from the bonus_data value. 131 size_t bonus_size = (i == 1 && bonus_data != NULL) ? bonus_data->size : 0; 132 133 unsigned char* expanded_source = malloc(expanded_len); 134 if (expanded_source == NULL) { 135 printf("failed to allocate %d bytes for expanded_source\n", 136 expanded_len); 137 return -1; 138 } 139 140 z_stream strm; 141 strm.zalloc = Z_NULL; 142 strm.zfree = Z_NULL; 143 strm.opaque = Z_NULL; 144 strm.avail_in = src_len; 145 strm.next_in = (unsigned char*)(old_data + src_start); 146 strm.avail_out = expanded_len; 147 strm.next_out = expanded_source; 148 149 int ret; 150 ret = inflateInit2(&strm, -15); 151 if (ret != Z_OK) { 152 printf("failed to init source inflation: %d\n", ret); 153 return -1; 154 } 155 156 // Because we've provided enough room to accommodate the output 157 // data, we expect one call to inflate() to suffice. 158 ret = inflate(&strm, Z_SYNC_FLUSH); 159 if (ret != Z_STREAM_END) { 160 printf("source inflation returned %d\n", ret); 161 return -1; 162 } 163 // We should have filled the output buffer exactly, except 164 // for the bonus_size. 165 if (strm.avail_out != bonus_size) { 166 printf("source inflation short by %d bytes\n", strm.avail_out-bonus_size); 167 return -1; 168 } 169 inflateEnd(&strm); 170 171 if (bonus_size) { 172 memcpy(expanded_source + (expanded_len - bonus_size), 173 bonus_data->data, bonus_size); 174 } 175 176 // Next, apply the bsdiff patch (in memory) to the uncompressed 177 // data. 178 unsigned char* uncompressed_target_data; 179 ssize_t uncompressed_target_size; 180 if (ApplyBSDiffPatchMem(expanded_source, expanded_len, 181 patch, patch_offset, 182 &uncompressed_target_data, 183 &uncompressed_target_size) != 0) { 184 return -1; 185 } 186 187 // Now compress the target data and append it to the output. 188 189 // we're done with the expanded_source data buffer, so we'll 190 // reuse that memory to receive the output of deflate. 191 unsigned char* temp_data = expanded_source; 192 ssize_t temp_size = expanded_len; 193 if (temp_size < 32768) { 194 // ... unless the buffer is too small, in which case we'll 195 // allocate a fresh one. 196 free(temp_data); 197 temp_data = malloc(32768); 198 temp_size = 32768; 199 } 200 201 // now the deflate stream 202 strm.zalloc = Z_NULL; 203 strm.zfree = Z_NULL; 204 strm.opaque = Z_NULL; 205 strm.avail_in = uncompressed_target_size; 206 strm.next_in = uncompressed_target_data; 207 ret = deflateInit2(&strm, level, method, windowBits, memLevel, strategy); 208 do { 209 strm.avail_out = temp_size; 210 strm.next_out = temp_data; 211 ret = deflate(&strm, Z_FINISH); 212 ssize_t have = temp_size - strm.avail_out; 213 214 if (sink(temp_data, have, token) != have) { 215 printf("failed to write %ld compressed bytes to output\n", 216 (long)have); 217 return -1; 218 } 219 SHA_update(ctx, temp_data, have); 220 } while (ret != Z_STREAM_END); 221 deflateEnd(&strm); 222 223 free(temp_data); 224 free(uncompressed_target_data); 225 } else { 226 printf("patch chunk %d is unknown type %d\n", i, type); 227 return -1; 228 } 229 } 230 231 return 0; 232 } 233