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.Serializable;
47 import java.text.MessageFormat;
48
49 import org.eclipse.jgit.lfs.errors.InvalidLongObjectIdException;
50 import org.eclipse.jgit.lfs.internal.LfsText;
51 import org.eclipse.jgit.util.NB;
52 import org.eclipse.jgit.util.RawParseUtils;
53
54 /**
55 * A prefix abbreviation of an {@link org.eclipse.jgit.lfs.lib.LongObjectId}.
56 * <p>
57 * Enable abbreviating SHA-256 strings used by Git LFS, using sufficient leading
58 * digits from the LongObjectId name to still be unique within the repository
59 * the string was generated from. These ids are likely to be unique for a useful
60 * period of time, especially if they contain at least 6-10 hex digits.
61 * <p>
62 * This class converts the hex string into a binary form, to make it more
63 * efficient for matching against an object.
64 *
65 * Ported to SHA-256 from {@link org.eclipse.jgit.lib.AbbreviatedObjectId}
66 *
67 * @since 4.3
68 */
69 public final class AbbreviatedLongObjectId implements Serializable {
70 private static final long serialVersionUID = 1L;
71
72 /**
73 * Test a string of characters to verify it is a hex format.
74 * <p>
75 * If true the string can be parsed with {@link #fromString(String)}.
76 *
77 * @param id
78 * the string to test.
79 * @return true if the string can converted into an AbbreviatedObjectId.
80 */
81 public static final boolean isId(String id) {
82 if (id.length() < 2
83 || Constants.LONG_OBJECT_ID_STRING_LENGTH < id.length())
84 return false;
85 try {
86 for (int i = 0; i < id.length(); i++)
87 RawParseUtils.parseHexInt4((byte) id.charAt(i));
88 return true;
89 } catch (ArrayIndexOutOfBoundsException e) {
90 return false;
91 }
92 }
93
94 /**
95 * Convert an AbbreviatedObjectId from hex characters (US-ASCII).
96 *
97 * @param buf
98 * the US-ASCII buffer to read from.
99 * @param offset
100 * position to read the first character from.
101 * @param end
102 * one past the last position to read (<code>end-offset</code> is
103 * the length of the string).
104 * @return the converted object id.
105 */
106 public static final AbbreviatedLongObjectId fromString(final byte[] buf,
107 final int offset, final int end) {
108 if (end - offset > Constants.LONG_OBJECT_ID_STRING_LENGTH)
109 throw new IllegalArgumentException(MessageFormat.format(
110 LfsText.get().invalidLongIdLength,
111 Integer.valueOf(end - offset),
112 Integer.valueOf(Constants.LONG_OBJECT_ID_STRING_LENGTH)));
113 return fromHexString(buf, offset, end);
114 }
115
116 /**
117 * Convert an AbbreviatedObjectId from an
118 * {@link org.eclipse.jgit.lib.AnyObjectId}.
119 * <p>
120 * This method copies over all bits of the Id, and is therefore complete
121 * (see {@link #isComplete()}).
122 *
123 * @param id
124 * the {@link org.eclipse.jgit.lib.ObjectId} to convert from.
125 * @return the converted object id.
126 */
127 public static final AbbreviatedLongObjectId fromLongObjectId(
128 AnyLongObjectId id) {
129 return new AbbreviatedLongObjectId(
130 Constants.LONG_OBJECT_ID_STRING_LENGTH, id.w1, id.w2, id.w3,
131 id.w4);
132 }
133
134 /**
135 * Convert an AbbreviatedLongObjectId from hex characters.
136 *
137 * @param str
138 * the string to read from. Must be <= 64 characters.
139 * @return the converted object id.
140 */
141 public static final AbbreviatedLongObjectId fromString(String str) {
142 if (str.length() > Constants.LONG_OBJECT_ID_STRING_LENGTH)
143 throw new IllegalArgumentException(
144 MessageFormat.format(LfsText.get().invalidLongId, str));
145 final byte[] b = org.eclipse.jgit.lib.Constants.encodeASCII(str);
146 return fromHexString(b, 0, b.length);
147 }
148
149 private static final AbbreviatedLongObjectId fromHexString(final byte[] bs,
150 int ptr, final int end) {
151 try {
152 final long a = hexUInt64(bs, ptr, end);
153 final long b = hexUInt64(bs, ptr + 16, end);
154 final long c = hexUInt64(bs, ptr + 32, end);
155 final long d = hexUInt64(bs, ptr + 48, end);
156 return new AbbreviatedLongObjectId(end - ptr, a, b, c, d);
157 } catch (ArrayIndexOutOfBoundsException e1) {
158 throw new InvalidLongObjectIdException(bs, ptr, end - ptr);
159 }
160 }
161
162 private static final long hexUInt64(final byte[] bs, int p, final int end) {
163 if (16 <= end - p)
164 return RawParseUtils.parseHexInt64(bs, p);
165
166 long r = 0;
167 int n = 0;
168 while (n < 16 && p < end) {
169 r <<= 4;
170 r |= RawParseUtils.parseHexInt4(bs[p++]);
171 n++;
172 }
173 return r << ((16 - n) * 4);
174 }
175
176 static long mask(int nibbles, long word, long v) {
177 final long b = (word - 1) * 16;
178 if (b + 16 <= nibbles) {
179 // We have all of the bits required for this word.
180 //
181 return v;
182 }
183
184 if (nibbles <= b) {
185 // We have none of the bits required for this word.
186 //
187 return 0;
188 }
189
190 final long s = 64 - (nibbles - b) * 4;
191 return (v >>> s) << s;
192 }
193
194 /** Number of half-bytes used by this id. */
195 final int nibbles;
196
197 final long w1;
198
199 final long w2;
200
201 final long w3;
202
203 final long w4;
204
205 AbbreviatedLongObjectId(final int n, final long new_1, final long new_2,
206 final long new_3, final long new_4) {
207 nibbles = n;
208 w1 = new_1;
209 w2 = new_2;
210 w3 = new_3;
211 w4 = new_4;
212 }
213
214 /**
215 * Get length
216 *
217 * @return number of hex digits appearing in this id.
218 */
219 public int length() {
220 return nibbles;
221 }
222
223 /**
224 * Check if this id is complete
225 *
226 * @return true if this ObjectId is actually a complete id.
227 */
228 public boolean isComplete() {
229 return length() == Constants.LONG_OBJECT_ID_STRING_LENGTH;
230 }
231
232 /**
233 * Convert to LongObjectId
234 *
235 * @return a complete ObjectId; null if {@link #isComplete()} is false.
236 */
237 public LongObjectId toLongObjectId() {
238 return isComplete() ? new LongObjectId(w1, w2, w3, w4) : null;
239 }
240
241 /**
242 * Compares this abbreviation to a full object id.
243 *
244 * @param other
245 * the other object id.
246 * @return <0 if this abbreviation names an object that is less than
247 * <code>other</code>; 0 if this abbreviation exactly matches the
248 * first {@link #length()} digits of <code>other.name()</code>;
249 * >0 if this abbreviation names an object that is after
250 * <code>other</code>.
251 */
252 public final int prefixCompare(AnyLongObjectId other) {
253 int cmp;
254
255 cmp = NB.compareUInt64(w1, mask(1, other.w1));
256 if (cmp != 0)
257 return cmp;
258
259 cmp = NB.compareUInt64(w2, mask(2, other.w2));
260 if (cmp != 0)
261 return cmp;
262
263 cmp = NB.compareUInt64(w3, mask(3, other.w3));
264 if (cmp != 0)
265 return cmp;
266
267 return NB.compareUInt64(w4, mask(4, other.w4));
268 }
269
270 /**
271 * Compare this abbreviation to a network-byte-order LongObjectId.
272 *
273 * @param bs
274 * array containing the other LongObjectId in network byte order.
275 * @param p
276 * position within {@code bs} to start the compare at. At least
277 * 32 bytes, starting at this position are required.
278 * @return <0 if this abbreviation names an object that is less than
279 * <code>other</code>; 0 if this abbreviation exactly matches the
280 * first {@link #length()} digits of <code>other.name()</code>;
281 * >0 if this abbreviation names an object that is after
282 * <code>other</code>.
283 */
284 public final int prefixCompare(byte[] bs, int p) {
285 int cmp;
286
287 cmp = NB.compareUInt64(w1, mask(1, NB.decodeInt64(bs, p)));
288 if (cmp != 0)
289 return cmp;
290
291 cmp = NB.compareUInt64(w2, mask(2, NB.decodeInt64(bs, p + 8)));
292 if (cmp != 0)
293 return cmp;
294
295 cmp = NB.compareUInt64(w3, mask(3, NB.decodeInt64(bs, p + 16)));
296 if (cmp != 0)
297 return cmp;
298
299 return NB.compareUInt64(w4, mask(4, NB.decodeInt64(bs, p + 24)));
300 }
301
302 /**
303 * Compare this abbreviation to a network-byte-order LongObjectId.
304 *
305 * @param bs
306 * array containing the other LongObjectId in network byte order.
307 * @param p
308 * position within {@code bs} to start the compare at. At least 4
309 * longs, starting at this position are required.
310 * @return <0 if this abbreviation names an object that is less than
311 * <code>other</code>; 0 if this abbreviation exactly matches the
312 * first {@link #length()} digits of <code>other.name()</code>;
313 * >0 if this abbreviation names an object that is after
314 * <code>other</code>.
315 */
316 public final int prefixCompare(long[] bs, int p) {
317 int cmp;
318
319 cmp = NB.compareUInt64(w1, mask(1, bs[p]));
320 if (cmp != 0)
321 return cmp;
322
323 cmp = NB.compareUInt64(w2, mask(2, bs[p + 1]));
324 if (cmp != 0)
325 return cmp;
326
327 cmp = NB.compareUInt64(w3, mask(3, bs[p + 2]));
328 if (cmp != 0)
329 return cmp;
330
331 return NB.compareUInt64(w4, mask(4, bs[p + 3]));
332 }
333
334 /**
335 * Get the first byte of this id
336 *
337 * @return value for a fan-out style map, only valid of length >= 2.
338 */
339 public final int getFirstByte() {
340 return (int) (w1 >>> 56);
341 }
342
343 private long mask(long word, long v) {
344 return mask(nibbles, word, v);
345 }
346
347 /** {@inheritDoc} */
348 @Override
349 public int hashCode() {
350 return (int) (w1 >> 32);
351 }
352
353 /** {@inheritDoc} */
354 @Override
355 public boolean equals(Object o) {
356 if (o instanceof AbbreviatedLongObjectId) {
357 final AbbreviatedLongObjectId../org/eclipse/jgit/lfs/lib/AbbreviatedLongObjectId.html#AbbreviatedLongObjectId">AbbreviatedLongObjectId b = (AbbreviatedLongObjectId) o;
358 return nibbles == b.nibbles && w1 == b.w1 && w2 == b.w2
359 && w3 == b.w3 && w4 == b.w4;
360 }
361 return false;
362 }
363
364 /**
365 * <p>name.</p>
366 *
367 * @return string form of the abbreviation, in lower case hexadecimal.
368 */
369 public final String name() {
370 final char[] b = new char[Constants.LONG_OBJECT_ID_STRING_LENGTH];
371
372 AnyLongObjectId.formatHexChar(b, 0, w1);
373 if (nibbles <= 16)
374 return new String(b, 0, nibbles);
375
376 AnyLongObjectId.formatHexChar(b, 16, w2);
377 if (nibbles <= 32)
378 return new String(b, 0, nibbles);
379
380 AnyLongObjectId.formatHexChar(b, 32, w3);
381 if (nibbles <= 48)
382 return new String(b, 0, nibbles);
383
384 AnyLongObjectId.formatHexChar(b, 48, w4);
385 return new String(b, 0, nibbles);
386 }
387
388 /** {@inheritDoc} */
389 @SuppressWarnings("nls")
390 @Override
391 public String toString() {
392 return "AbbreviatedLongObjectId[" + name() + "]"; //$NON-NLS-1$
393 }
394 }