mbspatch.cc 7.8 KB

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