1 /*
2 * Copyright (C) 2008, Robin Rosenberg <robin.rosenberg@dewire.com>
3 * Copyright (C) 2006-2008, 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
45 package org.eclipse.jgit.lib;
46
47 import java.io.IOException;
48 import java.io.ObjectInputStream;
49 import java.io.ObjectOutputStream;
50 import java.io.Serializable;
51
52 import org.eclipse.jgit.annotations.Nullable;
53 import org.eclipse.jgit.errors.InvalidObjectIdException;
54 import org.eclipse.jgit.util.NB;
55 import org.eclipse.jgit.util.RawParseUtils;
56
57 /**
58 * A SHA-1 abstraction.
59 */
60 public class ObjectId extends AnyObjectId implements Serializable {
61 private static final long serialVersionUID = 1L;
62
63 private static final ObjectId ZEROID;
64
65 private static final String ZEROID_STR;
66
67 static {
68 ZEROID = new ObjectId(0, 0, 0, 0, 0);
69 ZEROID_STR = ZEROID.name();
70 }
71
72 /**
73 * Get the special all-null ObjectId.
74 *
75 * @return the all-null ObjectId, often used to stand-in for no object.
76 */
77 public static final ObjectId zeroId() {
78 return ZEROID;
79 }
80
81 /**
82 * Test a string of characters to verify it is a hex format.
83 * <p>
84 * If true the string can be parsed with {@link #fromString(String)}.
85 *
86 * @param id
87 * the string to test.
88 * @return true if the string can converted into an ObjectId.
89 */
90 public static final boolean isId(@Nullable String id) {
91 if (id == null) {
92 return false;
93 }
94 if (id.length() != Constants.OBJECT_ID_STRING_LENGTH)
95 return false;
96 try {
97 for (int i = 0; i < Constants.OBJECT_ID_STRING_LENGTH; i++) {
98 RawParseUtils.parseHexInt4((byte) id.charAt(i));
99 }
100 return true;
101 } catch (ArrayIndexOutOfBoundsException e) {
102 return false;
103 }
104 }
105
106 /**
107 * Convert an ObjectId into a hex string representation.
108 *
109 * @param i
110 * the id to convert. May be null.
111 * @return the hex string conversion of this id's content.
112 */
113 public static final String toString(ObjectId i) {
114 return i != null ? i.name() : ZEROID_STR;
115 }
116
117 /**
118 * Compare two object identifier byte sequences for equality.
119 *
120 * @param firstBuffer
121 * the first buffer to compare against. Must have at least 20
122 * bytes from position fi through the end of the buffer.
123 * @param fi
124 * first offset within firstBuffer to begin testing.
125 * @param secondBuffer
126 * the second buffer to compare against. Must have at least 20
127 * bytes from position si through the end of the buffer.
128 * @param si
129 * first offset within secondBuffer to begin testing.
130 * @return true if the two identifiers are the same.
131 */
132 public static boolean equals(final byte[] firstBuffer, final int fi,
133 final byte[] secondBuffer, final int si) {
134 return firstBuffer[fi] == secondBuffer[si]
135 && firstBuffer[fi + 1] == secondBuffer[si + 1]
136 && firstBuffer[fi + 2] == secondBuffer[si + 2]
137 && firstBuffer[fi + 3] == secondBuffer[si + 3]
138 && firstBuffer[fi + 4] == secondBuffer[si + 4]
139 && firstBuffer[fi + 5] == secondBuffer[si + 5]
140 && firstBuffer[fi + 6] == secondBuffer[si + 6]
141 && firstBuffer[fi + 7] == secondBuffer[si + 7]
142 && firstBuffer[fi + 8] == secondBuffer[si + 8]
143 && firstBuffer[fi + 9] == secondBuffer[si + 9]
144 && firstBuffer[fi + 10] == secondBuffer[si + 10]
145 && firstBuffer[fi + 11] == secondBuffer[si + 11]
146 && firstBuffer[fi + 12] == secondBuffer[si + 12]
147 && firstBuffer[fi + 13] == secondBuffer[si + 13]
148 && firstBuffer[fi + 14] == secondBuffer[si + 14]
149 && firstBuffer[fi + 15] == secondBuffer[si + 15]
150 && firstBuffer[fi + 16] == secondBuffer[si + 16]
151 && firstBuffer[fi + 17] == secondBuffer[si + 17]
152 && firstBuffer[fi + 18] == secondBuffer[si + 18]
153 && firstBuffer[fi + 19] == secondBuffer[si + 19];
154 }
155
156 /**
157 * Convert an ObjectId from raw binary representation.
158 *
159 * @param bs
160 * the raw byte buffer to read from. At least 20 bytes must be
161 * available within this byte array.
162 * @return the converted object id.
163 */
164 public static final ObjectId fromRaw(byte[] bs) {
165 return fromRaw(bs, 0);
166 }
167
168 /**
169 * Convert an ObjectId from raw binary representation.
170 *
171 * @param bs
172 * the raw byte buffer to read from. At least 20 bytes after p
173 * must be available within this byte array.
174 * @param p
175 * position to read the first byte of data from.
176 * @return the converted object id.
177 */
178 public static final ObjectId fromRaw(byte[] bs, int p) {
179 final int a = NB.decodeInt32(bs, p);
180 final int b = NB.decodeInt32(bs, p + 4);
181 final int c = NB.decodeInt32(bs, p + 8);
182 final int d = NB.decodeInt32(bs, p + 12);
183 final int e = NB.decodeInt32(bs, p + 16);
184 return new ObjectId(a, b, c, d, e);
185 }
186
187 /**
188 * Convert an ObjectId from raw binary representation.
189 *
190 * @param is
191 * the raw integers buffer to read from. At least 5 integers must
192 * be available within this int array.
193 * @return the converted object id.
194 */
195 public static final ObjectId fromRaw(int[] is) {
196 return fromRaw(is, 0);
197 }
198
199 /**
200 * Convert an ObjectId from raw binary representation.
201 *
202 * @param is
203 * the raw integers buffer to read from. At least 5 integers
204 * after p must be available within this int array.
205 * @param p
206 * position to read the first integer of data from.
207 * @return the converted object id.
208 */
209 public static final ObjectId fromRaw(int[] is, int p) {
210 return new ObjectId(is[p], is[p + 1], is[p + 2], is[p + 3], is[p + 4]);
211 }
212
213 /**
214 * Convert an ObjectId from hex characters (US-ASCII).
215 *
216 * @param buf
217 * the US-ASCII buffer to read from. At least 40 bytes after
218 * offset must be available within this byte array.
219 * @param offset
220 * position to read the first character from.
221 * @return the converted object id.
222 */
223 public static final ObjectId fromString(byte[] buf, int offset) {
224 return fromHexString(buf, offset);
225 }
226
227 /**
228 * Convert an ObjectId from hex characters.
229 *
230 * @param str
231 * the string to read from. Must be 40 characters long.
232 * @return the converted object id.
233 */
234 public static ObjectId fromString(String str) {
235 if (str.length() != Constants.OBJECT_ID_STRING_LENGTH) {
236 throw new InvalidObjectIdException(str);
237 }
238 return fromHexString(Constants.encodeASCII(str), 0);
239 }
240
241 private static final ObjectId fromHexString(byte[] bs, int p) {
242 try {
243 final int a = RawParseUtils.parseHexInt32(bs, p);
244 final int b = RawParseUtils.parseHexInt32(bs, p + 8);
245 final int c = RawParseUtils.parseHexInt32(bs, p + 16);
246 final int d = RawParseUtils.parseHexInt32(bs, p + 24);
247 final int e = RawParseUtils.parseHexInt32(bs, p + 32);
248 return new ObjectId(a, b, c, d, e);
249 } catch (ArrayIndexOutOfBoundsException e1) {
250 throw new InvalidObjectIdException(bs, p,
251 Constants.OBJECT_ID_STRING_LENGTH);
252 }
253 }
254
255 /**
256 * Construct an ObjectId from 160 bits provided in 5 words.
257 *
258 * @param new_1
259 * an int
260 * @param new_2
261 * an int
262 * @param new_3
263 * an int
264 * @param new_4
265 * an int
266 * @param new_5
267 * an int
268 * @since 4.7
269 */
270 public ObjectId(int new_1, int new_2, int new_3, int new_4, int new_5) {
271 w1 = new_1;
272 w2 = new_2;
273 w3 = new_3;
274 w4 = new_4;
275 w5 = new_5;
276 }
277
278 /**
279 * Initialize this instance by copying another existing ObjectId.
280 * <p>
281 * This constructor is mostly useful for subclasses who want to extend an
282 * ObjectId with more properties, but initialize from an existing ObjectId
283 * instance acquired by other means.
284 *
285 * @param src
286 * another already parsed ObjectId to copy the value out of.
287 */
288 protected ObjectId(AnyObjectId src) {
289 w1 = src.w1;
290 w2 = src.w2;
291 w3 = src.w3;
292 w4 = src.w4;
293 w5 = src.w5;
294 }
295
296 /** {@inheritDoc} */
297 @Override
298 public ObjectId toObjectId() {
299 return this;
300 }
301
302 private void writeObject(ObjectOutputStream os) throws IOException {
303 os.writeInt(w1);
304 os.writeInt(w2);
305 os.writeInt(w3);
306 os.writeInt(w4);
307 os.writeInt(w5);
308 }
309
310 private void readObject(ObjectInputStream ois) throws IOException {
311 w1 = ois.readInt();
312 w2 = ois.readInt();
313 w3 = ois.readInt();
314 w4 = ois.readInt();
315 w5 = ois.readInt();
316 }
317 }