Chromium Code Reviews| 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 193 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 204 * OR. Note that this would be DANGEROUS AND UNSOUND if we used | 204 * OR. Note that this would be DANGEROUS AND UNSOUND if we used |
| 205 * _O_BINARY other than as a bitwise OR mask (e.g., as a bitwise AND | 205 * _O_BINARY other than as a bitwise OR mask (e.g., as a bitwise AND |
| 206 * mask to check for binary mode), but it seems OK in the limited | 206 * mask to check for binary mode), but it seems OK in the limited |
| 207 * context of the following small function. */ | 207 * context of the following small function. */ |
| 208 #ifndef _O_BINARY | 208 #ifndef _O_BINARY |
| 209 # define _O_BINARY 0 | 209 # define _O_BINARY 0 |
| 210 #endif | 210 #endif |
| 211 | 211 |
| 212 int ApplyBinaryPatch(const wchar_t *old_file, const wchar_t *patch_file, | 212 int ApplyBinaryPatch(const wchar_t *old_file, const wchar_t *patch_file, |
| 213 const wchar_t *new_file) { | 213 const wchar_t *new_file) { |
| 214 int ret = 0; | 214 int ret = OK; |
| 215 int ofd = -1; | |
| 216 int nfd = -1; | |
| 217 unsigned char *buf = NULL; | |
| 218 | |
| 215 int pfd = _wopen(patch_file, O_RDONLY | _O_BINARY); | 219 int pfd = _wopen(patch_file, O_RDONLY | _O_BINARY); |
| 216 if (pfd < 0) return READ_ERROR; | 220 if (pfd < 0) return READ_ERROR; |
| 217 | 221 |
| 218 MBSPatchHeader header; | 222 do { |
| 219 if (ret = MBS_ReadHeader(pfd, &header)) return ret; | 223 MBSPatchHeader header; |
| 224 if ((ret = MBS_ReadHeader(pfd, &header))) | |
| 225 break; | |
| 220 | 226 |
| 221 int ofd = _wopen(old_file, O_RDONLY | _O_BINARY); | 227 ofd = _wopen(old_file, O_RDONLY | _O_BINARY); |
| 222 if (ofd < 0) return READ_ERROR; | 228 if (ofd < 0) { |
| 229 ret = READ_ERROR; | |
| 230 break; | |
| 231 } | |
| 223 | 232 |
| 224 struct stat os; | 233 struct stat os; |
| 225 if (ret = fstat(ofd, &os)) return READ_ERROR; | 234 if ((ret = fstat(ofd, &os))) { |
| 226 if (os.st_size != header.slen) return UNEXPECTED_ERROR; | 235 ret = READ_ERROR; |
| 236 break; | |
| 237 } | |
| 227 | 238 |
| 228 unsigned char *buf = (unsigned char*) malloc(header.slen); | 239 if (os.st_size != header.slen) { |
| 229 if (!buf) return MEM_ERROR; | 240 ret = UNEXPECTED_ERROR; |
| 241 break; | |
| 242 } | |
| 230 | 243 |
| 231 if (read(ofd, buf, header.slen) != header.slen) return READ_ERROR; | 244 buf = (unsigned char*) malloc(header.slen); |
| 232 if (CalculateCrc(buf, header.slen) != header.scrc32) | 245 if (!buf) { |
| 233 return CRC_ERROR; | 246 ret = MEM_ERROR; |
| 247 break; | |
| 248 } | |
| 234 | 249 |
| 235 int nfd = _wopen(new_file, O_WRONLY | O_TRUNC | O_CREAT | _O_BINARY); | 250 if (read(ofd, buf, header.slen) != header.slen) { |
| 236 if (nfd < 0) return READ_ERROR; | 251 ret = READ_ERROR; |
| 252 break; | |
| 253 } | |
| 237 | 254 |
| 238 MBS_ApplyPatch(&header, pfd, buf, nfd); | 255 if (CalculateCrc(buf, header.slen) != header.scrc32) { |
| 256 ret = CRC_ERROR; | |
| 257 break; | |
| 258 } | |
| 259 | |
| 260 nfd = _wopen(new_file, O_WRONLY | O_TRUNC | O_CREAT | _O_BINARY); | |
| 261 if (nfd < 0) { | |
| 262 ret = READ_ERROR; | |
| 263 break; | |
| 264 } | |
| 265 | |
| 266 MBS_ApplyPatch(&header, pfd, buf, nfd); | |
| 267 } while (0); | |
|
Avi (use Gerrit)
2013/07/29 14:46:21
Huh. I don't think I've seen this style anywhere i
| |
| 239 | 268 |
| 240 free(buf); | 269 free(buf); |
| 241 close(pfd); | 270 close(pfd); |
| 242 close(ofd); | 271 if (ofd >= 0) close(ofd); |
| 243 close(nfd); | 272 if (nfd >= 0) close(nfd); |
| 244 return OK; | 273 return ret; |
| 245 } | 274 } |
| OLD | NEW |