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