View Javadoc
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  
44  package org.eclipse.jgit.util;
45  
46  /** Conversion utilities for network byte order handling. */
47  public final class NB {
48  	/**
49  	 * Compare a 32 bit unsigned integer stored in a 32 bit signed integer.
50  	 * <p>
51  	 * This function performs an unsigned compare operation, even though Java
52  	 * does not natively support unsigned integer values. Negative numbers are
53  	 * treated as larger than positive ones.
54  	 *
55  	 * @param a
56  	 *            the first value to compare.
57  	 * @param b
58  	 *            the second value to compare.
59  	 * @return &lt; 0 if a &lt; b; 0 if a == b; &gt; 0 if a &gt; b.
60  	 */
61  	public static int compareUInt32(final int a, final int b) {
62  		final int cmp = (a >>> 1) - (b >>> 1);
63  		if (cmp != 0)
64  			return cmp;
65  		return (a & 1) - (b & 1);
66  	}
67  
68  	/**
69  	 * Compare a 64 bit unsigned integer stored in a 64 bit signed integer.
70  	 * <p>
71  	 * This function performs an unsigned compare operation, even though Java
72  	 * does not natively support unsigned integer values. Negative numbers are
73  	 * treated as larger than positive ones.
74  	 *
75  	 * @param a
76  	 *            the first value to compare.
77  	 * @param b
78  	 *            the second value to compare.
79  	 * @return &lt; 0 if a &lt; b; 0 if a == b; &gt; 0 if a &gt; b.
80  	 * @since 4.3
81  	 */
82  	public static int compareUInt64(final long a, final long b) {
83  		long cmp = (a >>> 1) - (b >>> 1);
84  		if (cmp > 0) {
85  			return 1;
86  		} else if (cmp < 0) {
87  			return -1;
88  		}
89  		cmp = ((a & 1) - (b & 1));
90  		if (cmp > 0) {
91  			return 1;
92  		} else if (cmp < 0) {
93  			return -1;
94  		} else {
95  			return 0;
96  		}
97  	}
98  
99  	/**
100 	 * Convert sequence of 2 bytes (network byte order) into unsigned value.
101 	 *
102 	 * @param intbuf
103 	 *            buffer to acquire the 2 bytes of data from.
104 	 * @param offset
105 	 *            position within the buffer to begin reading from. This
106 	 *            position and the next byte after it (for a total of 2 bytes)
107 	 *            will be read.
108 	 * @return unsigned integer value that matches the 16 bits read.
109 	 */
110 	public static int decodeUInt16(final byte[] intbuf, final int offset) {
111 		int r = (intbuf[offset] & 0xff) << 8;
112 		return r | (intbuf[offset + 1] & 0xff);
113 	}
114 
115 	/**
116 	 * Convert sequence of 4 bytes (network byte order) into signed value.
117 	 *
118 	 * @param intbuf
119 	 *            buffer to acquire the 4 bytes of data from.
120 	 * @param offset
121 	 *            position within the buffer to begin reading from. This
122 	 *            position and the next 3 bytes after it (for a total of 4
123 	 *            bytes) will be read.
124 	 * @return signed integer value that matches the 32 bits read.
125 	 */
126 	public static int decodeInt32(final byte[] intbuf, final int offset) {
127 		int r = intbuf[offset] << 8;
128 
129 		r |= intbuf[offset + 1] & 0xff;
130 		r <<= 8;
131 
132 		r |= intbuf[offset + 2] & 0xff;
133 		return (r << 8) | (intbuf[offset + 3] & 0xff);
134 	}
135 
136 	/**
137 	 * Convert sequence of 8 bytes (network byte order) into signed value.
138 	 *
139 	 * @param intbuf
140 	 *            buffer to acquire the 8 bytes of data from.
141 	 * @param offset
142 	 *            position within the buffer to begin reading from. This
143 	 *            position and the next 7 bytes after it (for a total of 8
144 	 *            bytes) will be read.
145 	 * @return signed integer value that matches the 64 bits read.
146 	 * @since 3.0
147 	 */
148 	public static long decodeInt64(final byte[] intbuf, final int offset) {
149 		long r = intbuf[offset] << 8;
150 
151 		r |= intbuf[offset + 1] & 0xff;
152 		r <<= 8;
153 
154 		r |= intbuf[offset + 2] & 0xff;
155 		r <<= 8;
156 
157 		r |= intbuf[offset + 3] & 0xff;
158 		r <<= 8;
159 
160 		r |= intbuf[offset + 4] & 0xff;
161 		r <<= 8;
162 
163 		r |= intbuf[offset + 5] & 0xff;
164 		r <<= 8;
165 
166 		r |= intbuf[offset + 6] & 0xff;
167 		return (r << 8) | (intbuf[offset + 7] & 0xff);
168 	}
169 
170 	/**
171 	 * Convert sequence of 4 bytes (network byte order) into unsigned value.
172 	 *
173 	 * @param intbuf
174 	 *            buffer to acquire the 4 bytes of data from.
175 	 * @param offset
176 	 *            position within the buffer to begin reading from. This
177 	 *            position and the next 3 bytes after it (for a total of 4
178 	 *            bytes) will be read.
179 	 * @return unsigned integer value that matches the 32 bits read.
180 	 */
181 	public static long decodeUInt32(final byte[] intbuf, final int offset) {
182 		int low = (intbuf[offset + 1] & 0xff) << 8;
183 		low |= (intbuf[offset + 2] & 0xff);
184 		low <<= 8;
185 
186 		low |= (intbuf[offset + 3] & 0xff);
187 		return ((long) (intbuf[offset] & 0xff)) << 24 | low;
188 	}
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 	/**
207 	 * Write a 16 bit integer as a sequence of 2 bytes (network byte order).
208 	 *
209 	 * @param intbuf
210 	 *            buffer to write the 2 bytes of data into.
211 	 * @param offset
212 	 *            position within the buffer to begin writing to. This position
213 	 *            and the next byte after it (for a total of 2 bytes) will be
214 	 *            replaced.
215 	 * @param v
216 	 *            the value to write.
217 	 */
218 	public static void encodeInt16(final byte[] intbuf, final int offset, int v) {
219 		intbuf[offset + 1] = (byte) v;
220 		v >>>= 8;
221 
222 		intbuf[offset] = (byte) v;
223 	}
224 
225 	/**
226 	 * Write a 32 bit integer as a sequence of 4 bytes (network byte order).
227 	 *
228 	 * @param intbuf
229 	 *            buffer to write the 4 bytes of data into.
230 	 * @param offset
231 	 *            position within the buffer to begin writing to. This position
232 	 *            and the next 3 bytes after it (for a total of 4 bytes) will be
233 	 *            replaced.
234 	 * @param v
235 	 *            the value to write.
236 	 */
237 	public static void encodeInt32(final byte[] intbuf, final int offset, int v) {
238 		intbuf[offset + 3] = (byte) v;
239 		v >>>= 8;
240 
241 		intbuf[offset + 2] = (byte) v;
242 		v >>>= 8;
243 
244 		intbuf[offset + 1] = (byte) v;
245 		v >>>= 8;
246 
247 		intbuf[offset] = (byte) v;
248 	}
249 
250 	/**
251 	 * Write a 64 bit integer as a sequence of 8 bytes (network byte order).
252 	 *
253 	 * @param intbuf
254 	 *            buffer to write the 8 bytes of data into.
255 	 * @param offset
256 	 *            position within the buffer to begin writing to. This position
257 	 *            and the next 7 bytes after it (for a total of 8 bytes) will be
258 	 *            replaced.
259 	 * @param v
260 	 *            the value to write.
261 	 */
262 	public static void encodeInt64(final byte[] intbuf, final int offset, long v) {
263 		intbuf[offset + 7] = (byte) v;
264 		v >>>= 8;
265 
266 		intbuf[offset + 6] = (byte) v;
267 		v >>>= 8;
268 
269 		intbuf[offset + 5] = (byte) v;
270 		v >>>= 8;
271 
272 		intbuf[offset + 4] = (byte) v;
273 		v >>>= 8;
274 
275 		intbuf[offset + 3] = (byte) v;
276 		v >>>= 8;
277 
278 		intbuf[offset + 2] = (byte) v;
279 		v >>>= 8;
280 
281 		intbuf[offset + 1] = (byte) v;
282 		v >>>= 8;
283 
284 		intbuf[offset] = (byte) v;
285 	}
286 
287 	private NB() {
288 		// Don't create instances of a static only utility.
289 	}
290 }