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.

BinaryDelta.java 9.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318
  1. /*
  2. * Copyright (C) 2007, Robin Rosenberg <robin.rosenberg@dewire.com>
  3. * Copyright (C) 2006-2007, Shawn O. Pearce <spearce@spearce.org>
  4. * and other copyright owners as documented in the project's IP log.
  5. *
  6. * This program and the accompanying materials are made available
  7. * under the terms of the Eclipse Distribution License v1.0 which
  8. * accompanies this distribution, is reproduced below, and is
  9. * available at http://www.eclipse.org/org/documents/edl-v10.php
  10. *
  11. * All rights reserved.
  12. *
  13. * Redistribution and use in source and binary forms, with or
  14. * without modification, are permitted provided that the following
  15. * conditions are met:
  16. *
  17. * - Redistributions of source code must retain the above copyright
  18. * notice, this list of conditions and the following disclaimer.
  19. *
  20. * - Redistributions in binary form must reproduce the above
  21. * copyright notice, this list of conditions and the following
  22. * disclaimer in the documentation and/or other materials provided
  23. * with the distribution.
  24. *
  25. * - Neither the name of the Eclipse Foundation, Inc. nor the
  26. * names of its contributors may be used to endorse or promote
  27. * products derived from this software without specific prior
  28. * written permission.
  29. *
  30. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
  31. * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
  32. * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  33. * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  34. * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
  35. * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  36. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  37. * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  38. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  39. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
  40. * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  41. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
  42. * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  43. */
  44. package org.eclipse.jgit.internal.storage.pack;
  45. import org.eclipse.jgit.internal.JGitText;
  46. import org.eclipse.jgit.util.QuotedString;
  47. import org.eclipse.jgit.util.RawParseUtils;
  48. /**
  49. * Recreate a stream from a base stream and a GIT pack delta.
  50. * <p>
  51. * This entire class is heavily cribbed from <code>patch-delta.c</code> in the
  52. * GIT project. The original delta patching code was written by Nicolas Pitre
  53. * (&lt;nico@cam.org&gt;).
  54. * </p>
  55. */
  56. public class BinaryDelta {
  57. /**
  58. * Length of the base object in the delta stream.
  59. *
  60. * @param delta
  61. * the delta stream, or at least the header of it.
  62. * @return the base object's size.
  63. */
  64. public static long getBaseSize(byte[] delta) {
  65. int p = 0;
  66. long baseLen = 0;
  67. int c, shift = 0;
  68. do {
  69. c = delta[p++] & 0xff;
  70. baseLen |= ((long) (c & 0x7f)) << shift;
  71. shift += 7;
  72. } while ((c & 0x80) != 0);
  73. return baseLen;
  74. }
  75. /**
  76. * Length of the resulting object in the delta stream.
  77. *
  78. * @param delta
  79. * the delta stream, or at least the header of it.
  80. * @return the resulting object's size.
  81. */
  82. public static long getResultSize(byte[] delta) {
  83. int p = 0;
  84. // Skip length of the base object.
  85. //
  86. int c;
  87. do {
  88. c = delta[p++] & 0xff;
  89. } while ((c & 0x80) != 0);
  90. long resLen = 0;
  91. int shift = 0;
  92. do {
  93. c = delta[p++] & 0xff;
  94. resLen |= ((long) (c & 0x7f)) << shift;
  95. shift += 7;
  96. } while ((c & 0x80) != 0);
  97. return resLen;
  98. }
  99. /**
  100. * Apply the changes defined by delta to the data in base, yielding a new
  101. * array of bytes.
  102. *
  103. * @param base
  104. * some byte representing an object of some kind.
  105. * @param delta
  106. * a git pack delta defining the transform from one version to
  107. * another.
  108. * @return patched base
  109. */
  110. public static final byte[] apply(byte[] base, byte[] delta) {
  111. return apply(base, delta, null);
  112. }
  113. /**
  114. * Apply the changes defined by delta to the data in base, yielding a new
  115. * array of bytes.
  116. *
  117. * @param base
  118. * some byte representing an object of some kind.
  119. * @param delta
  120. * a git pack delta defining the transform from one version to
  121. * another.
  122. * @param result
  123. * array to store the result into. If null the result will be
  124. * allocated and returned.
  125. * @return either {@code result}, or the result array allocated.
  126. */
  127. public static final byte[] apply(final byte[] base, final byte[] delta,
  128. byte[] result) {
  129. int deltaPtr = 0;
  130. // Length of the base object (a variable length int).
  131. //
  132. int baseLen = 0;
  133. int c, shift = 0;
  134. do {
  135. c = delta[deltaPtr++] & 0xff;
  136. baseLen |= (c & 0x7f) << shift;
  137. shift += 7;
  138. } while ((c & 0x80) != 0);
  139. if (base.length != baseLen)
  140. throw new IllegalArgumentException(
  141. JGitText.get().baseLengthIncorrect);
  142. // Length of the resulting object (a variable length int).
  143. //
  144. int resLen = 0;
  145. shift = 0;
  146. do {
  147. c = delta[deltaPtr++] & 0xff;
  148. resLen |= (c & 0x7f) << shift;
  149. shift += 7;
  150. } while ((c & 0x80) != 0);
  151. if (result == null)
  152. result = new byte[resLen];
  153. else if (result.length != resLen)
  154. throw new IllegalArgumentException(
  155. JGitText.get().resultLengthIncorrect);
  156. int resultPtr = 0;
  157. while (deltaPtr < delta.length) {
  158. final int cmd = delta[deltaPtr++] & 0xff;
  159. if ((cmd & 0x80) != 0) {
  160. // Determine the segment of the base which should
  161. // be copied into the output. The segment is given
  162. // as an offset and a length.
  163. //
  164. int copyOffset = 0;
  165. if ((cmd & 0x01) != 0)
  166. copyOffset = delta[deltaPtr++] & 0xff;
  167. if ((cmd & 0x02) != 0)
  168. copyOffset |= (delta[deltaPtr++] & 0xff) << 8;
  169. if ((cmd & 0x04) != 0)
  170. copyOffset |= (delta[deltaPtr++] & 0xff) << 16;
  171. if ((cmd & 0x08) != 0)
  172. copyOffset |= (delta[deltaPtr++] & 0xff) << 24;
  173. int copySize = 0;
  174. if ((cmd & 0x10) != 0)
  175. copySize = delta[deltaPtr++] & 0xff;
  176. if ((cmd & 0x20) != 0)
  177. copySize |= (delta[deltaPtr++] & 0xff) << 8;
  178. if ((cmd & 0x40) != 0)
  179. copySize |= (delta[deltaPtr++] & 0xff) << 16;
  180. if (copySize == 0)
  181. copySize = 0x10000;
  182. System.arraycopy(base, copyOffset, result, resultPtr, copySize);
  183. resultPtr += copySize;
  184. } else if (cmd != 0) {
  185. // Anything else the data is literal within the delta
  186. // itself.
  187. //
  188. System.arraycopy(delta, deltaPtr, result, resultPtr, cmd);
  189. deltaPtr += cmd;
  190. resultPtr += cmd;
  191. } else {
  192. // cmd == 0 has been reserved for future encoding but
  193. // for now its not acceptable.
  194. //
  195. throw new IllegalArgumentException(
  196. JGitText.get().unsupportedCommand0);
  197. }
  198. }
  199. return result;
  200. }
  201. /**
  202. * Format this delta as a human readable string.
  203. *
  204. * @param delta
  205. * the delta instruction sequence to format.
  206. * @return the formatted delta.
  207. */
  208. public static String format(byte[] delta) {
  209. return format(delta, true);
  210. }
  211. /**
  212. * Format this delta as a human readable string.
  213. *
  214. * @param delta
  215. * the delta instruction sequence to format.
  216. * @param includeHeader
  217. * true if the header (base size and result size) should be
  218. * included in the formatting.
  219. * @return the formatted delta.
  220. */
  221. public static String format(byte[] delta, boolean includeHeader) {
  222. StringBuilder r = new StringBuilder();
  223. int deltaPtr = 0;
  224. long baseLen = 0;
  225. int c, shift = 0;
  226. do {
  227. c = delta[deltaPtr++] & 0xff;
  228. baseLen |= ((long) (c & 0x7f)) << shift;
  229. shift += 7;
  230. } while ((c & 0x80) != 0);
  231. long resLen = 0;
  232. shift = 0;
  233. do {
  234. c = delta[deltaPtr++] & 0xff;
  235. resLen |= ((long) (c & 0x7f)) << shift;
  236. shift += 7;
  237. } while ((c & 0x80) != 0);
  238. if (includeHeader) {
  239. r.append("DELTA( BASE="); //$NON-NLS-1$
  240. r.append(baseLen);
  241. r.append(" RESULT="); //$NON-NLS-1$
  242. r.append(resLen);
  243. r.append(" )\n"); //$NON-NLS-1$
  244. }
  245. while (deltaPtr < delta.length) {
  246. final int cmd = delta[deltaPtr++] & 0xff;
  247. if ((cmd & 0x80) != 0) {
  248. // Determine the segment of the base which should
  249. // be copied into the output. The segment is given
  250. // as an offset and a length.
  251. //
  252. int copyOffset = 0;
  253. if ((cmd & 0x01) != 0)
  254. copyOffset = delta[deltaPtr++] & 0xff;
  255. if ((cmd & 0x02) != 0)
  256. copyOffset |= (delta[deltaPtr++] & 0xff) << 8;
  257. if ((cmd & 0x04) != 0)
  258. copyOffset |= (delta[deltaPtr++] & 0xff) << 16;
  259. if ((cmd & 0x08) != 0)
  260. copyOffset |= (delta[deltaPtr++] & 0xff) << 24;
  261. int copySize = 0;
  262. if ((cmd & 0x10) != 0)
  263. copySize = delta[deltaPtr++] & 0xff;
  264. if ((cmd & 0x20) != 0)
  265. copySize |= (delta[deltaPtr++] & 0xff) << 8;
  266. if ((cmd & 0x40) != 0)
  267. copySize |= (delta[deltaPtr++] & 0xff) << 16;
  268. if (copySize == 0)
  269. copySize = 0x10000;
  270. r.append(" COPY ("); //$NON-NLS-1$
  271. r.append(copyOffset);
  272. r.append(", "); //$NON-NLS-1$
  273. r.append(copySize);
  274. r.append(")\n"); //$NON-NLS-1$
  275. } else if (cmd != 0) {
  276. // Anything else the data is literal within the delta
  277. // itself.
  278. //
  279. r.append(" INSERT("); //$NON-NLS-1$
  280. r.append(QuotedString.GIT_PATH.quote(RawParseUtils.decode(
  281. delta, deltaPtr, deltaPtr + cmd)));
  282. r.append(")\n"); //$NON-NLS-1$
  283. deltaPtr += cmd;
  284. } else {
  285. // cmd == 0 has been reserved for future encoding but
  286. // for now its not acceptable.
  287. //
  288. throw new IllegalArgumentException(
  289. JGitText.get().unsupportedCommand0);
  290. }
  291. }
  292. return r.toString();
  293. }
  294. }