1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302
|
/*-
* Copyright 2003,2004 Colin Percival
* All rights reserved
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted providing that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
* DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
* STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
* IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*
* Changelog:
* 2005-04-26 - Define the header as a C structure, add a CRC32 checksum to
* the header, and make all the types 32-bit.
* --Benjamin Smedberg <benjamin@smedbergs.us>
* 2007-11-14 - Added CalculateCrc() and ApplyBinaryPatch() methods.
* --Rahul Kuchhal
* 2016-07-27 - Improve validation of diffs.
* --Ricky Zhou
*/
#include "mbspatch.h"
#include <sys/stat.h>
#include <sys/types.h>
#include <stdlib.h>
#include <stdio.h>
#include <fcntl.h>
#include <string.h>
#include <limits.h>
#ifdef _WIN32
# include <io.h>
# include <winsock2.h>
#else
# include <unistd.h>
# include <arpa/inet.h>
#endif
extern "C" {
#include <7zCrc.h>
}
#ifndef SSIZE_MAX
# define SSIZE_MAX LONG_MAX
#endif
int
MBS_ReadHeader(int fd, MBSPatchHeader *header)
{
int s = read(fd, header, sizeof(MBSPatchHeader));
if (s != sizeof(MBSPatchHeader))
return READ_ERROR;
header->slen = ntohl(header->slen);
header->scrc32 = ntohl(header->scrc32);
header->dlen = ntohl(header->dlen);
header->cblen = ntohl(header->cblen);
header->difflen = ntohl(header->difflen);
header->extralen = ntohl(header->extralen);
struct stat hs;
s = fstat(fd, &hs);
if (s != 0)
return READ_ERROR;
if (memcmp(header->tag, "MBDIFF10", 8) != 0)
return UNEXPECTED_ERROR;
if (hs.st_size > INT_MAX)
return UNEXPECTED_ERROR;
size_t size = static_cast<size_t>(hs.st_size);
if (size < sizeof(MBSPatchHeader))
return UNEXPECTED_ERROR;
size -= sizeof(MBSPatchHeader);
if (size < header->cblen)
return UNEXPECTED_ERROR;
size -= header->cblen;
if (size < header->difflen)
return UNEXPECTED_ERROR;
size -= header->difflen;
if (size < header->extralen)
return UNEXPECTED_ERROR;
size -= header->extralen;
if (size != 0)
return UNEXPECTED_ERROR;
return OK;
}
int
MBS_ApplyPatch(const MBSPatchHeader *header, int patchfd,
unsigned char *fbuffer, int filefd)
{
unsigned char *fbufstart = fbuffer;
unsigned char *fbufend = fbuffer + header->slen;
unsigned char *buf = (unsigned char*) malloc(header->cblen +
header->difflen +
header->extralen);
if (!buf)
return MEM_ERROR;
int rv = OK;
int r = header->cblen + header->difflen + header->extralen;
unsigned char *wb = buf;
while (r) {
int c = read(patchfd, wb, (r > SSIZE_MAX) ? SSIZE_MAX : r);
if (c < 0) {
rv = READ_ERROR;
goto end;
}
r -= c;
wb += c;
if (c == 0 && r) {
rv = UNEXPECTED_ERROR;
goto end;
}
}
{
MBSPatchTriple *ctrlsrc = (MBSPatchTriple*) buf;
if (header->cblen % sizeof(MBSPatchTriple) != 0) {
rv = UNEXPECTED_ERROR;
goto end;
}
unsigned char *diffsrc = buf + header->cblen;
unsigned char *extrasrc = diffsrc + header->difflen;
MBSPatchTriple *ctrlend = (MBSPatchTriple*) diffsrc;
unsigned char *diffend = extrasrc;
unsigned char *extraend = extrasrc + header->extralen;
while (ctrlsrc < ctrlend) {
ctrlsrc->x = ntohl(ctrlsrc->x);
ctrlsrc->y = ntohl(ctrlsrc->y);
ctrlsrc->z = ntohl(ctrlsrc->z);
#ifdef DEBUG_bsmedberg
printf("Applying block:\n"
" x: %u\n"
" y: %u\n"
" z: %i\n",
ctrlsrc->x,
ctrlsrc->y,
ctrlsrc->z);
#endif
/* Add x bytes from oldfile to x bytes from the diff block */
if (ctrlsrc->x > static_cast<size_t>(fbufend - fbuffer) ||
ctrlsrc->x > static_cast<size_t>(diffend - diffsrc)) {
rv = UNEXPECTED_ERROR;
goto end;
}
for (unsigned int i = 0; i < ctrlsrc->x; ++i) {
diffsrc[i] += fbuffer[i];
}
if ((int) write(filefd, diffsrc, ctrlsrc->x) != ctrlsrc->x) {
rv = WRITE_ERROR;
goto end;
}
fbuffer += ctrlsrc->x;
diffsrc += ctrlsrc->x;
/* Copy y bytes from the extra block */
if (ctrlsrc->y > static_cast<size_t>(extraend - extrasrc)) {
rv = UNEXPECTED_ERROR;
goto end;
}
if ((int) write(filefd, extrasrc, ctrlsrc->y) != ctrlsrc->y) {
rv = WRITE_ERROR;
goto end;
}
extrasrc += ctrlsrc->y;
/* "seek" forwards in oldfile by z bytes */
if (ctrlsrc->z < fbufstart - fbuffer ||
ctrlsrc->z > fbufend - fbuffer) {
rv = UNEXPECTED_ERROR;
goto end;
}
fbuffer += ctrlsrc->z;
/* and on to the next control block */
++ctrlsrc;
}
}
end:
free(buf);
return rv;
}
int CalculateCrc(const unsigned char *buf, int size) {
CrcGenerateTable();
unsigned int crc = 0xffffffffL;
crc = ~CrcCalc(buf, size);
return crc;
}
/* _O_BINARY is a MSWindows open() mode flag. When absent, MSWindows
* open() translates CR+LF to LF; when present, it passes bytes
* through faithfully. Under *nix, we are always in binary mode, so
* the following #define turns this flag into a no-op w.r.t. bitwise
* OR. Note that this would be DANGEROUS AND UNSOUND if we used
* _O_BINARY other than as a bitwise OR mask (e.g., as a bitwise AND
* mask to check for binary mode), but it seems OK in the limited
* context of the following small function. */
#ifndef _O_BINARY
# define _O_BINARY 0
#endif
int ApplyBinaryPatch(const wchar_t *old_file, const wchar_t *patch_file,
const wchar_t *new_file) {
int ret = OK;
int ofd = -1;
int nfd = -1;
unsigned char *buf = NULL;
int pfd = _wopen(patch_file, O_RDONLY | _O_BINARY);
if (pfd < 0) return READ_ERROR;
do {
MBSPatchHeader header;
if ((ret = MBS_ReadHeader(pfd, &header)))
break;
ofd = _wopen(old_file, O_RDONLY | _O_BINARY);
if (ofd < 0) {
ret = READ_ERROR;
break;
}
struct stat os;
if ((ret = fstat(ofd, &os))) {
ret = READ_ERROR;
break;
}
if (os.st_size != header.slen) {
ret = UNEXPECTED_ERROR;
break;
}
buf = (unsigned char*) malloc(header.slen);
if (!buf) {
ret = MEM_ERROR;
break;
}
if (read(ofd, buf, header.slen) != header.slen) {
ret = READ_ERROR;
break;
}
if (CalculateCrc(buf, header.slen) != header.scrc32) {
ret = CRC_ERROR;
break;
}
nfd = _wopen(new_file, O_WRONLY | O_TRUNC | O_CREAT | _O_BINARY);
if (nfd < 0) {
ret = READ_ERROR;
break;
}
ret = MBS_ApplyPatch(&header, pfd, buf, nfd);
} while (0);
free(buf);
close(pfd);
if (ofd >= 0) close(ofd);
if (nfd >= 0) close(nfd);
return ret;
}
|