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.

NB.java 10KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331
  1. /*
  2. * Copyright (C) 2008, 2015 Shawn O. Pearce <spearce@spearce.org>
  3. * and other copyright owners as documented in the project's IP log.
  4. *
  5. * This program and the accompanying materials are made available
  6. * under the terms of the Eclipse Distribution License v1.0 which
  7. * accompanies this distribution, is reproduced below, and is
  8. * available at http://www.eclipse.org/org/documents/edl-v10.php
  9. *
  10. * All rights reserved.
  11. *
  12. * Redistribution and use in source and binary forms, with or
  13. * without modification, are permitted provided that the following
  14. * conditions are met:
  15. *
  16. * - Redistributions of source code must retain the above copyright
  17. * notice, this list of conditions and the following disclaimer.
  18. *
  19. * - Redistributions in binary form must reproduce the above
  20. * copyright notice, this list of conditions and the following
  21. * disclaimer in the documentation and/or other materials provided
  22. * with the distribution.
  23. *
  24. * - Neither the name of the Eclipse Foundation, Inc. nor the
  25. * names of its contributors may be used to endorse or promote
  26. * products derived from this software without specific prior
  27. * written permission.
  28. *
  29. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
  30. * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
  31. * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  32. * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  33. * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
  34. * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  35. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  36. * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  37. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  38. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
  39. * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  40. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
  41. * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  42. */
  43. package org.eclipse.jgit.util;
  44. /** Conversion utilities for network byte order handling. */
  45. public final class NB {
  46. /**
  47. * Compare a 32 bit unsigned integer stored in a 32 bit signed integer.
  48. * <p>
  49. * This function performs an unsigned compare operation, even though Java
  50. * does not natively support unsigned integer values. Negative numbers are
  51. * treated as larger than positive ones.
  52. *
  53. * @param a
  54. * the first value to compare.
  55. * @param b
  56. * the second value to compare.
  57. * @return &lt; 0 if a &lt; b; 0 if a == b; &gt; 0 if a &gt; b.
  58. */
  59. public static int compareUInt32(final int a, final int b) {
  60. final int cmp = (a >>> 1) - (b >>> 1);
  61. if (cmp != 0)
  62. return cmp;
  63. return (a & 1) - (b & 1);
  64. }
  65. /**
  66. * Compare a 64 bit unsigned integer stored in a 64 bit signed integer.
  67. * <p>
  68. * This function performs an unsigned compare operation, even though Java
  69. * does not natively support unsigned integer values. Negative numbers are
  70. * treated as larger than positive ones.
  71. *
  72. * @param a
  73. * the first value to compare.
  74. * @param b
  75. * the second value to compare.
  76. * @return &lt; 0 if a &lt; b; 0 if a == b; &gt; 0 if a &gt; b.
  77. * @since 4.3
  78. */
  79. public static int compareUInt64(final long a, final long b) {
  80. long cmp = (a >>> 1) - (b >>> 1);
  81. if (cmp > 0) {
  82. return 1;
  83. } else if (cmp < 0) {
  84. return -1;
  85. }
  86. cmp = ((a & 1) - (b & 1));
  87. if (cmp > 0) {
  88. return 1;
  89. } else if (cmp < 0) {
  90. return -1;
  91. } else {
  92. return 0;
  93. }
  94. }
  95. /**
  96. * Convert sequence of 2 bytes (network byte order) into unsigned value.
  97. *
  98. * @param intbuf
  99. * buffer to acquire the 2 bytes of data from.
  100. * @param offset
  101. * position within the buffer to begin reading from. This
  102. * position and the next byte after it (for a total of 2 bytes)
  103. * will be read.
  104. * @return unsigned integer value that matches the 16 bits read.
  105. */
  106. public static int decodeUInt16(final byte[] intbuf, final int offset) {
  107. int r = (intbuf[offset] & 0xff) << 8;
  108. return r | (intbuf[offset + 1] & 0xff);
  109. }
  110. /**
  111. * Convert sequence of 3 bytes (network byte order) into unsigned value.
  112. *
  113. * @param intbuf
  114. * buffer to acquire the 3 bytes of data from.
  115. * @param offset
  116. * position within the buffer to begin reading from. This
  117. * position and the next 2 bytes after it (for a total of 3
  118. * bytes) will be read.
  119. * @return signed integer value that matches the 24 bits read.
  120. * @since 4.9
  121. */
  122. public static int decodeUInt24(byte[] intbuf, int offset) {
  123. int r = (intbuf[offset] & 0xff) << 8;
  124. r |= intbuf[offset + 1] & 0xff;
  125. return (r << 8) | (intbuf[offset + 2] & 0xff);
  126. }
  127. /**
  128. * Convert sequence of 4 bytes (network byte order) into signed value.
  129. *
  130. * @param intbuf
  131. * buffer to acquire the 4 bytes of data from.
  132. * @param offset
  133. * position within the buffer to begin reading from. This
  134. * position and the next 3 bytes after it (for a total of 4
  135. * bytes) will be read.
  136. * @return signed integer value that matches the 32 bits read.
  137. */
  138. public static int decodeInt32(final byte[] intbuf, final int offset) {
  139. int r = intbuf[offset] << 8;
  140. r |= intbuf[offset + 1] & 0xff;
  141. r <<= 8;
  142. r |= intbuf[offset + 2] & 0xff;
  143. return (r << 8) | (intbuf[offset + 3] & 0xff);
  144. }
  145. /**
  146. * Convert sequence of 8 bytes (network byte order) into signed value.
  147. *
  148. * @param intbuf
  149. * buffer to acquire the 8 bytes of data from.
  150. * @param offset
  151. * position within the buffer to begin reading from. This
  152. * position and the next 7 bytes after it (for a total of 8
  153. * bytes) will be read.
  154. * @return signed integer value that matches the 64 bits read.
  155. * @since 3.0
  156. */
  157. public static long decodeInt64(final byte[] intbuf, final int offset) {
  158. long r = intbuf[offset] << 8;
  159. r |= intbuf[offset + 1] & 0xff;
  160. r <<= 8;
  161. r |= intbuf[offset + 2] & 0xff;
  162. r <<= 8;
  163. r |= intbuf[offset + 3] & 0xff;
  164. r <<= 8;
  165. r |= intbuf[offset + 4] & 0xff;
  166. r <<= 8;
  167. r |= intbuf[offset + 5] & 0xff;
  168. r <<= 8;
  169. r |= intbuf[offset + 6] & 0xff;
  170. return (r << 8) | (intbuf[offset + 7] & 0xff);
  171. }
  172. /**
  173. * Convert sequence of 4 bytes (network byte order) into unsigned value.
  174. *
  175. * @param intbuf
  176. * buffer to acquire the 4 bytes of data from.
  177. * @param offset
  178. * position within the buffer to begin reading from. This
  179. * position and the next 3 bytes after it (for a total of 4
  180. * bytes) will be read.
  181. * @return unsigned integer value that matches the 32 bits read.
  182. */
  183. public static long decodeUInt32(final byte[] intbuf, final int offset) {
  184. int low = (intbuf[offset + 1] & 0xff) << 8;
  185. low |= (intbuf[offset + 2] & 0xff);
  186. low <<= 8;
  187. low |= (intbuf[offset + 3] & 0xff);
  188. return ((long) (intbuf[offset] & 0xff)) << 24 | low;
  189. }
  190. /**
  191. * Convert sequence of 8 bytes (network byte order) into unsigned value.
  192. *
  193. * @param intbuf
  194. * buffer to acquire the 8 bytes of data from.
  195. * @param offset
  196. * position within the buffer to begin reading from. This
  197. * position and the next 7 bytes after it (for a total of 8
  198. * bytes) will be read.
  199. * @return unsigned integer value that matches the 64 bits read.
  200. */
  201. public static long decodeUInt64(final byte[] intbuf, final int offset) {
  202. return (decodeUInt32(intbuf, offset) << 32)
  203. | decodeUInt32(intbuf, offset + 4);
  204. }
  205. /**
  206. * Write a 16 bit integer as a sequence of 2 bytes (network byte order).
  207. *
  208. * @param intbuf
  209. * buffer to write the 2 bytes of data into.
  210. * @param offset
  211. * position within the buffer to begin writing to. This position
  212. * and the next byte after it (for a total of 2 bytes) will be
  213. * replaced.
  214. * @param v
  215. * the value to write.
  216. */
  217. public static void encodeInt16(final byte[] intbuf, final int offset, int v) {
  218. intbuf[offset + 1] = (byte) v;
  219. v >>>= 8;
  220. intbuf[offset] = (byte) v;
  221. }
  222. /**
  223. * Write a 24 bit integer as a sequence of 3 bytes (network byte order).
  224. *
  225. * @param intbuf
  226. * buffer to write the 3 bytes of data into.
  227. * @param offset
  228. * position within the buffer to begin writing to. This position
  229. * and the next 2 bytes after it (for a total of 3 bytes) will be
  230. * replaced.
  231. * @param v
  232. * the value to write.
  233. * @since 4.9
  234. */
  235. public static void encodeInt24(byte[] intbuf, int offset, int v) {
  236. intbuf[offset + 2] = (byte) v;
  237. v >>>= 8;
  238. intbuf[offset + 1] = (byte) v;
  239. v >>>= 8;
  240. intbuf[offset] = (byte) v;
  241. }
  242. /**
  243. * Write a 32 bit integer as a sequence of 4 bytes (network byte order).
  244. *
  245. * @param intbuf
  246. * buffer to write the 4 bytes of data into.
  247. * @param offset
  248. * position within the buffer to begin writing to. This position
  249. * and the next 3 bytes after it (for a total of 4 bytes) will be
  250. * replaced.
  251. * @param v
  252. * the value to write.
  253. */
  254. public static void encodeInt32(final byte[] intbuf, final int offset, int v) {
  255. intbuf[offset + 3] = (byte) v;
  256. v >>>= 8;
  257. intbuf[offset + 2] = (byte) v;
  258. v >>>= 8;
  259. intbuf[offset + 1] = (byte) v;
  260. v >>>= 8;
  261. intbuf[offset] = (byte) v;
  262. }
  263. /**
  264. * Write a 64 bit integer as a sequence of 8 bytes (network byte order).
  265. *
  266. * @param intbuf
  267. * buffer to write the 8 bytes of data into.
  268. * @param offset
  269. * position within the buffer to begin writing to. This position
  270. * and the next 7 bytes after it (for a total of 8 bytes) will be
  271. * replaced.
  272. * @param v
  273. * the value to write.
  274. */
  275. public static void encodeInt64(final byte[] intbuf, final int offset, long v) {
  276. intbuf[offset + 7] = (byte) v;
  277. v >>>= 8;
  278. intbuf[offset + 6] = (byte) v;
  279. v >>>= 8;
  280. intbuf[offset + 5] = (byte) v;
  281. v >>>= 8;
  282. intbuf[offset + 4] = (byte) v;
  283. v >>>= 8;
  284. intbuf[offset + 3] = (byte) v;
  285. v >>>= 8;
  286. intbuf[offset + 2] = (byte) v;
  287. v >>>= 8;
  288. intbuf[offset + 1] = (byte) v;
  289. v >>>= 8;
  290. intbuf[offset] = (byte) v;
  291. }
  292. private NB() {
  293. // Don't create instances of a static only utility.
  294. }
  295. }