Skip to content

Commit

Permalink
Refactor packSHA1 routine to use a lookup table instead of strchr()
Browse files Browse the repository at this point in the history
Also, since this method now operates directly on bytes, create a new
method 'packSHA1FromBytes' and call it from 'packSHA1FromString' passing
a pointer to the bytes in the string.

Signed-off-by: Geoff Garside <geoff@geoffgarside.co.uk>
  • Loading branch information
chapados authored and geoffgarside committed Jun 17, 2009
1 parent 5f139e0 commit 7fb78de
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 14 deletions.
1 change: 1 addition & 0 deletions Source/GITUtilityBelt.h
Expand Up @@ -8,6 +8,7 @@

#import <Foundation/Foundation.h>

NSData * packSHA1FromBytes(const char *hexBytes);
NSData * packSHA1(NSString * unpackedSHA1);
NSString * unpackSHA1FromString(NSString * packedSHA1);
NSString * unpackSHA1FromData(NSData * packedSHA1);
Expand Down
50 changes: 36 additions & 14 deletions Source/GITUtilityBelt.m
Expand Up @@ -14,25 +14,47 @@

static const char hexchars[] = "0123456789abcdef";

// pre-calculated hex value (v) -> strchr(hexchars, v) - hexchars
// to save searching the string on each iteration
static signed char from_hex[256] = {
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, /* 00 */
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, /* 10 */
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, /* 20 */
0, 1, 2, 3, 4, 5, 6, 7, 8, 9, -1, -1, -1, -1, -1, -1, /* 30 */
-1, 10, 11, 12, 13, 14, 15, -1, -1, -1, -1, -1, -1, -1, -1, -1, /* 40 */
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, /* 50 */
-1, 10, 11, 12, 13, 14, 15, -1, -1, -1, -1, -1, -1, -1, -1, -1, /* 60 */
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, /* 70 */
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, /* 80 */
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, /* 90 */
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, /* a0 */
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, /* b0 */
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, /* c0 */
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, /* d0 */
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, /* e0 */
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, /* f0 */
};

NSData *
packSHA1(NSString * unpackedSHA1)
packSHA1FromBytes(const char *hexBytes)
{
NSData *unpackedSHA1Data = [unpackedSHA1 dataUsingEncoding:NSASCIIStringEncoding];
const uint8_t *unpackedSHA1CString = (const uint8_t *)[unpackedSHA1Data bytes];
uint8_t highBits, lowBits, bits;
NSMutableData *packedSHA1 = [NSMutableData dataWithCapacity:kGITPackedSha1Length];
NSMutableData *packedSHA1 = [NSMutableData dataWithLength:kGITPackedSha1Length];
uint8_t *packedBytes = [packedSHA1 mutableBytes];
int i;
for (i = 0; i < [unpackedSHA1 length]; i++)
{
if (i % 2 == 0) {
highBits = (strchr(hexchars, unpackedSHA1CString[i]) - hexchars) << 4;
} else {
lowBits = strchr(hexchars, unpackedSHA1CString[i]) - hexchars;
bits = (highBits | lowBits);
[packedSHA1 appendBytes:&bits length:1];
for ( i = 0; i < kGITPackedSha1Length; i++, hexBytes += 2) {
NSUInteger bits = (from_hex[hexBytes[0]] << 4) | (from_hex[hexBytes[1]]);
if ( bits < 0 ) {
return nil;
}
packedBytes[i] = (unsigned char)bits;
}
return [NSData dataWithData:packedSHA1];
return packedSHA1;
}

NSData *
packSHA1(NSString * unpackedSHA1)
{
return packSHA1FromBytes([unpackedSHA1 cStringUsingEncoding:NSASCIIStringEncoding]);
}

NSString *
Expand Down

0 comments on commit 7fb78de

Please sign in to comment.