From: "Shawn O. Pearce" <spearce@spearce.org>
To: Robin Rosenberg <robin.rosenberg@dewire.com>
Cc: git@vger.kernel.org
Subject: [JGIT PATCH 3/7] Move PacketLineIn hex parsing to RawParseUtils
Date: Thu, 4 Jun 2009 14:43:59 -0700 [thread overview]
Message-ID: <1244151843-26954-4-git-send-email-spearce@spearce.org> (raw)
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
next prev parent reply other threads:[~2009-06-04 21:45 UTC|newest]
Thread overview: 9+ messages / expand[flat|nested] mbox.gz Atom feed top
2009-06-04 21:43 [JGIT PATCH 0/7] Misc. transport cleanup Shawn O. Pearce
2009-06-04 21:43 ` [JGIT PATCH 1/7] Move hex parsing functions to RawParseUtil, accept upper case Shawn O. Pearce
2009-06-04 21:43 ` [JGIT PATCH 2/7] Disambiguate pkt-line "0000" from "0004" Shawn O. Pearce
2009-06-04 21:43 ` Shawn O. Pearce [this message]
2009-06-04 21:44 ` [JGIT PATCH 4/7] Add tests for RawParseUtil's hex string parsing Shawn O. Pearce
2009-06-04 21:44 ` [JGIT PATCH 5/7] Add tests for PacketLineIn Shawn O. Pearce
2009-06-04 21:44 ` [JGIT PATCH 6/7] Add tests for PacketLineOut Shawn O. Pearce
2009-06-04 21:44 ` [JGIT PATCH 7/7] Add tests for SideBandOutputStream Shawn O. Pearce
2009-06-15 14:36 ` [JGIT PATCH 1/7] Move hex parsing functions to RawParseUtil, accept upper case Shawn O. Pearce
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=1244151843-26954-4-git-send-email-spearce@spearce.org \
--to=spearce@spearce.org \
--cc=git@vger.kernel.org \
--cc=robin.rosenberg@dewire.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).