1 /*
2 * Copyright (C) 2015, Matthias Sohn <matthias.sohn@sap.com>
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.lfs.lib;
45
46 import java.io.IOException;
47 import java.io.ObjectInputStream;
48 import java.io.ObjectOutputStream;
49 import java.io.Serializable;
50
51 import org.eclipse.jgit.lfs.errors.InvalidLongObjectIdException;
52 import org.eclipse.jgit.util.NB;
53 import org.eclipse.jgit.util.RawParseUtils;
54
55 /**
56 * A SHA-256 abstraction.
57 *
58 * Ported to SHA-256 from {@link org.eclipse.jgit.lib.ObjectId}
59 *
60 * @since 4.3
61 */
62 public class LongObjectId extends AnyLongObjectId implements Serializable {
63 private static final long serialVersionUID = 1L;
64
65 private static final LongObjectId ZEROID;
66
67 private static final String ZEROID_STR;
68
69 static {
70 ZEROID = new LongObjectId(0L, 0L, 0L, 0L);
71 ZEROID_STR = ZEROID.name();
72 }
73
74 /**
75 * Get the special all-zero LongObjectId.
76 *
77 * @return the all-zero LongObjectId, often used to stand-in for no object.
78 */
79 public static final LongObjectId zeroId() {
80 return ZEROID;
81 }
82
83 /**
84 * Test a string of characters to verify that it can be interpreted as
85 * LongObjectId.
86 * <p>
87 * If true the string can be parsed with {@link #fromString(String)}.
88 *
89 * @param id
90 * the string to test.
91 * @return true if the string can converted into an LongObjectId.
92 */
93 public static final boolean isId(String id) {
94 if (id.length() != Constants.LONG_OBJECT_ID_STRING_LENGTH)
95 return false;
96 try {
97 for (int i = 0; i < Constants.LONG_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 a LongObjectId 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(LongObjectId 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 32
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 32
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 && firstBuffer[fi + 20] == secondBuffer[si + 20]
155 && firstBuffer[fi + 21] == secondBuffer[si + 21]
156 && firstBuffer[fi + 22] == secondBuffer[si + 22]
157 && firstBuffer[fi + 23] == secondBuffer[si + 23]
158 && firstBuffer[fi + 24] == secondBuffer[si + 24]
159 && firstBuffer[fi + 25] == secondBuffer[si + 25]
160 && firstBuffer[fi + 26] == secondBuffer[si + 26]
161 && firstBuffer[fi + 27] == secondBuffer[si + 27]
162 && firstBuffer[fi + 28] == secondBuffer[si + 28]
163 && firstBuffer[fi + 29] == secondBuffer[si + 29]
164 && firstBuffer[fi + 30] == secondBuffer[si + 30]
165 && firstBuffer[fi + 31] == secondBuffer[si + 31];
166 }
167
168 /**
169 * Convert a LongObjectId from raw binary representation.
170 *
171 * @param bs
172 * the raw byte buffer to read from. At least 32 bytes must be
173 * available within this byte array.
174 * @return the converted object id.
175 */
176 public static final LongObjectId fromRaw(byte[] bs) {
177 return fromRaw(bs, 0);
178 }
179
180 /**
181 * Convert a LongObjectId from raw binary representation.
182 *
183 * @param bs
184 * the raw byte buffer to read from. At least 32 bytes after p
185 * must be available within this byte array.
186 * @param p
187 * position to read the first byte of data from.
188 * @return the converted object id.
189 */
190 public static final LongObjectId fromRaw(byte[] bs, int p) {
191 final long a = NB.decodeInt64(bs, p);
192 final long b = NB.decodeInt64(bs, p + 8);
193 final long c = NB.decodeInt64(bs, p + 16);
194 final long d = NB.decodeInt64(bs, p + 24);
195 return new LongObjectId(a, b, c, d);
196 }
197
198 /**
199 * Convert a LongObjectId from raw binary representation.
200 *
201 * @param is
202 * the raw long buffer to read from. At least 4 longs must be
203 * available within this long array.
204 * @return the converted object id.
205 */
206 public static final LongObjectId fromRaw(long[] is) {
207 return fromRaw(is, 0);
208 }
209
210 /**
211 * Convert a LongObjectId from raw binary representation.
212 *
213 * @param is
214 * the raw long buffer to read from. At least 4 longs after p
215 * must be available within this long array.
216 * @param p
217 * position to read the first long of data from.
218 * @return the converted object id.
219 */
220 public static final LongObjectId fromRaw(long[] is, int p) {
221 return new LongObjectId(is[p], is[p + 1], is[p + 2], is[p + 3]);
222 }
223
224 /**
225 * Convert a LongObjectId from hex characters (US-ASCII).
226 *
227 * @param buf
228 * the US-ASCII buffer to read from. At least 64 bytes after
229 * offset must be available within this byte array.
230 * @param offset
231 * position to read the first character from.
232 * @return the converted object id.
233 */
234 public static final LongObjectId fromString(byte[] buf, int offset) {
235 return fromHexString(buf, offset);
236 }
237
238 /**
239 * Convert a LongObjectId from hex characters.
240 *
241 * @param str
242 * the string to read from. Must be 64 characters long.
243 * @return the converted object id.
244 */
245 public static LongObjectId fromString(String str) {
246 if (str.length() != Constants.LONG_OBJECT_ID_STRING_LENGTH)
247 throw new InvalidLongObjectIdException(str);
248 return fromHexString(org.eclipse.jgit.lib.Constants.encodeASCII(str),
249 0);
250 }
251
252 private static final LongObjectId fromHexString(byte[] bs, int p) {
253 try {
254 final long a = RawParseUtils.parseHexInt64(bs, p);
255 final long b = RawParseUtils.parseHexInt64(bs, p + 16);
256 final long c = RawParseUtils.parseHexInt64(bs, p + 32);
257 final long d = RawParseUtils.parseHexInt64(bs, p + 48);
258 return new LongObjectId(a, b, c, d);
259 } catch (ArrayIndexOutOfBoundsException e1) {
260 throw new InvalidLongObjectIdException(bs, p,
261 Constants.LONG_OBJECT_ID_STRING_LENGTH);
262 }
263 }
264
265 LongObjectId(final long new_1, final long new_2, final long new_3,
266 final long new_4) {
267 w1 = new_1;
268 w2 = new_2;
269 w3 = new_3;
270 w4 = new_4;
271 }
272
273 /**
274 * Initialize this instance by copying another existing LongObjectId.
275 * <p>
276 * This constructor is mostly useful for subclasses which want to extend a
277 * LongObjectId with more properties, but initialize from an existing
278 * LongObjectId instance acquired by other means.
279 *
280 * @param src
281 * another already parsed LongObjectId to copy the value out of.
282 */
283 protected LongObjectId(AnyLongObjectId src) {
284 w1 = src.w1;
285 w2 = src.w2;
286 w3 = src.w3;
287 w4 = src.w4;
288 }
289
290 /** {@inheritDoc} */
291 @Override
292 public LongObjectId toObjectId() {
293 return this;
294 }
295
296 private void writeObject(ObjectOutputStream os) throws IOException {
297 os.writeLong(w1);
298 os.writeLong(w2);
299 os.writeLong(w3);
300 os.writeLong(w4);
301 }
302
303 private void readObject(ObjectInputStream ois) throws IOException {
304 w1 = ois.readLong();
305 w2 = ois.readLong();
306 w3 = ois.readLong();
307 w4 = ois.readLong();
308 }
309 }