THIS IS A TEST INSTANCE ONLY! REPOSITORIES CAN BE DELETED AT ANY TIME!

Git Source Code Mirror - This is a publish-only repository and all pull requests are ignored. Please follow Documentation/SubmittingPatches procedure for any of your improvements.
git
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

95 lines
2.4KB

  1. /*
  2. * patch-delta.c:
  3. * recreate a buffer from a source and the delta produced by diff-delta.c
  4. *
  5. * (C) 2005 Nicolas Pitre <nico@fluxnic.net>
  6. *
  7. * This code is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License version 2 as
  9. * published by the Free Software Foundation.
  10. */
  11. #include "git-compat-util.h"
  12. #include "delta.h"
  13. void *patch_delta(const void *src_buf, unsigned long src_size,
  14. const void *delta_buf, unsigned long delta_size,
  15. unsigned long *dst_size)
  16. {
  17. const unsigned char *data, *top;
  18. unsigned char *dst_buf, *out, cmd;
  19. unsigned long size;
  20. if (delta_size < DELTA_SIZE_MIN)
  21. return NULL;
  22. data = delta_buf;
  23. top = (const unsigned char *) delta_buf + delta_size;
  24. /* make sure the orig file size matches what we expect */
  25. size = get_delta_hdr_size(&data, top);
  26. if (size != src_size)
  27. return NULL;
  28. /* now the result size */
  29. size = get_delta_hdr_size(&data, top);
  30. dst_buf = xmallocz(size);
  31. out = dst_buf;
  32. while (data < top) {
  33. cmd = *data++;
  34. if (cmd & 0x80) {
  35. unsigned long cp_off = 0, cp_size = 0;
  36. #define PARSE_CP_PARAM(bit, var, shift) do { \
  37. if (cmd & (bit)) { \
  38. if (data >= top) \
  39. goto bad_length; \
  40. var |= ((unsigned) *data++ << (shift)); \
  41. } } while (0)
  42. PARSE_CP_PARAM(0x01, cp_off, 0);
  43. PARSE_CP_PARAM(0x02, cp_off, 8);
  44. PARSE_CP_PARAM(0x04, cp_off, 16);
  45. PARSE_CP_PARAM(0x08, cp_off, 24);
  46. PARSE_CP_PARAM(0x10, cp_size, 0);
  47. PARSE_CP_PARAM(0x20, cp_size, 8);
  48. PARSE_CP_PARAM(0x40, cp_size, 16);
  49. #undef PARSE_CP_PARAM
  50. if (cp_size == 0) cp_size = 0x10000;
  51. if (unsigned_add_overflows(cp_off, cp_size) ||
  52. cp_off + cp_size > src_size ||
  53. cp_size > size)
  54. goto bad_length;
  55. memcpy(out, (char *) src_buf + cp_off, cp_size);
  56. out += cp_size;
  57. size -= cp_size;
  58. } else if (cmd) {
  59. if (cmd > size || cmd > top - data)
  60. goto bad_length;
  61. memcpy(out, data, cmd);
  62. out += cmd;
  63. data += cmd;
  64. size -= cmd;
  65. } else {
  66. /*
  67. * cmd == 0 is reserved for future encoding
  68. * extensions. In the mean time we must fail when
  69. * encountering them (might be data corruption).
  70. */
  71. error("unexpected delta opcode 0");
  72. goto bad;
  73. }
  74. }
  75. /* sanity check */
  76. if (data != top || size != 0) {
  77. bad_length:
  78. error("delta replay has gone wild");
  79. bad:
  80. free(dst_buf);
  81. return NULL;
  82. }
  83. *dst_size = out - dst_buf;
  84. return dst_buf;
  85. }