git/list[1] front-page[2] threads[3] people[4] search[5] about
 

[JGIT PATCH 3/7] Move PacketLineIn hex parsing to RawParseUtils

From
Shawn O. Pearce <spearce@spearce.org>
Date
Jun 4, 2009, 21:43 UTC
Message-ID
<1244151843-26954-4-git-send-email-spearce@spearce.org>
In-Reply-To
<1244151843-26954-3-git-send-email-spearce@spearce.org>

This way we can reuse the same declaration buffer, and accept uppercase digits as well as lowercase digits.

Signed-off-by: Shawn O. Pearce <spearce@spearce.org>
---
 .../org/spearce/jgit/transport/PacketLineIn.java   |   26 ++---------------
 .../src/org/spearce/jgit/util/RawParseUtils.java   |   30 ++++++++++++++++++++
 2 files changed, 33 insertions(+), 23 deletions(-)
diff --git a/org.spearce.jgit/src/org/spearce/jgit/transport/PacketLineIn.java b/org.spearce.jgit/src/org/spearce/jgit/transport/PacketLineIn.java
index 92c7009..8d2cd18 100644
--- a/org.spearce.jgit/src/org/spearce/jgit/transport/PacketLineIn.java
+++ b/org.spearce.jgit/src/org/spearce/jgit/transport/PacketLineIn.java
@@ -40,7 +40,6 @@
 
 import java.io.IOException;
 import java.io.InputStream;
-import java.util.Arrays;
 
 import org.spearce.jgit.errors.PackProtocolException;
 import org.spearce.jgit.lib.Constants;
@@ -51,16 +50,6 @@
 
 class PacketLineIn {
 	static final String END = new String("") /* must not string pool */;
-	private static final byte fromhex[];
-
-	static {
-		fromhex = new byte['f' + 1];
-		Arrays.fill(fromhex, (byte) -1);
-		for (char i = '0'; i <= '9'; i++)
-			fromhex[i] = (byte) (i - '0');
-		for (char i = 'a'; i <= 'f'; i++)
-			fromhex[i] = (byte) ((i - 'a') + 10);
-	}
 
 	static enum AckNackResult {
 		/** NAK */
@@ -127,22 +116,13 @@ String readStringRaw() throws IOException {
 		return RawParseUtils.decode(Constants.CHARSET, raw, 0, len);
 	}
 
-
 	int readLength() throws IOException {
 		NB.readFully(in, lenbuffer, 0, 4);
 		try {
-			int r = fromhex[lenbuffer[0]] << 4;
-
-			r |= fromhex[lenbuffer[1]];
-			r <<= 4;
-
-			r |= fromhex[lenbuffer[2]];
-			r <<= 4;
-
-			r |= fromhex[lenbuffer[3]];
-			if (r < 0)
+			final int len = RawParseUtils.parseHexInt16(lenbuffer, 0);
+			if (len != 0 && len < 4)
 				throw new ArrayIndexOutOfBoundsException();
-			return r;
+			return len;
 		} catch (ArrayIndexOutOfBoundsException err) {
 			throw new IOException("Invalid packet line header: "
 					+ (char) lenbuffer[0] + (char) lenbuffer[1]
diff --git a/org.spearce.jgit/src/org/spearce/jgit/util/RawParseUtils.java b/org.spearce.jgit/src/org/spearce/jgit/util/RawParseUtils.java
index 0554acb..5fb3d27 100644
--- a/org.spearce.jgit/src/org/spearce/jgit/util/RawParseUtils.java
+++ b/org.spearce.jgit/src/org/spearce/jgit/util/RawParseUtils.java
@@ -255,6 +255,36 @@ public static final long parseLongBase10(final byte[] b, int ptr,
 	}
 
 	/**
+	 * Parse 4 character base 16 (hex) formatted string to unsigned integer.
+	 * <p>
+	 * The number is read in network byte order, that is, most significant
+	 * nybble first.
+	 *
+	 * @param bs
+	 *            buffer to parse digits from; positions {@code [p, p+4)} will
+	 *            be parsed.
+	 * @param p
+	 *            first position within the buffer to parse.
+	 * @return the integer value.
+	 * @throws ArrayIndexOutOfBoundsException
+	 *             if the string is not hex formatted.
+	 */
+	public static final int parseHexInt16(final byte[] bs, final int p) {
+		int r = digits16[bs[p]] << 4;
+
+		r |= digits16[bs[p + 1]];
+		r <<= 4;
+
+		r |= digits16[bs[p + 2]];
+		r <<= 4;
+
+		r |= digits16[bs[p + 3]];
+		if (r < 0)
+			throw new ArrayIndexOutOfBoundsException();
+		return r;
+	}
+
+	/**
 	 * Parse 8 character base 16 (hex) formatted string to unsigned integer.
 	 * <p>
 	 * The number is read in network byte order, that is, most significant
-- 
1.6.3.1.333.g3ebba7
Previous: Shawn O. PearceNext: Shawn O. Pearce
Message 4 of 9 in “Misc. transport cleanup”
  1. 0/7 Misc. transport cleanupShawn O. Pearce, Jun 4, 2009
  2. 1/7 Move hex parsing functions to RawParseUtil, accept upper caseShawn O. Pearce, Jun 4, 2009
  3. 2/7 Disambiguate pkt-line "0000" from "0004"Shawn O. Pearce, Jun 4, 2009
  4. 3/7 Move PacketLineIn hex parsing to RawParseUtilsShawn O. Pearce, Jun 4, 2009
  5. 4/7 Add tests for RawParseUtil's hex string parsingShawn O. Pearce, Jun 4, 2009
  6. 5/7 Add tests for PacketLineInShawn O. Pearce, Jun 4, 2009
  7. 6/7 Add tests for PacketLineOutShawn O. Pearce, Jun 4, 2009
  8. 7/7 Add tests for SideBandOutputStreamShawn O. Pearce, Jun 4, 2009
  9. Shawn O. PearceJun 15, 2009

Read the whole thread, see it on lore, or plain text.

$ cat FOOTERMessages come from the public archive at lore.kernel.org/git, fetched every hour. The front page is chosen and written each morning by an AI editor and can be wrong; the threads themselves are the record. About and API. For agents: an MCP server at https://gitlist.dev/mcp, and any thread, story or person page as Markdown by adding .md to its URL (or sending Accept: text/markdown). Details in /llms.txt.