Created
July 27, 2012 11:34
-
-
Save padde/3187503 to your computer and use it in GitHub Desktop.
NSData CRC32 Category
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@interface NSData (CRC32) | |
-(NSUInteger) crc32; | |
@end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#import "NSData+CRC32.h" | |
@implementation NSData (CRC32) | |
-(NSUInteger)crc32 | |
{ | |
uint32_t *table = malloc(sizeof(uint32_t) * 256); | |
uint32_t crc = 0xffffffff; | |
uint8_t *bytes = (uint8_t *)[self bytes]; | |
for (uint32_t i=0; i<256; i++) { | |
table[i] = i; | |
for (int j=0; j<8; j++) { | |
if (table[i] & 1) { | |
table[i] = (table[i] >>= 1) ^ 0xedb88320; | |
} else { | |
table[i] >>= 1; | |
} | |
} | |
} | |
for (int i=0; i<self.length; i++) { | |
crc = (crc >> 8) ^ table[crc & 0xff ^ bytes[i]]; | |
} | |
crc ^= 0xffffffff; | |
free(table); | |
return crc; | |
} | |
@end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#import <SenTestingKit/SenTestingKit.h> | |
#import "NSData+CRC32.h" | |
@interface NSDataCRC32Tests : SenTestCase | |
@end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#import "NSData+CRC32Tests.h" | |
@implementation NSDataCRC32Tests | |
-(void) testNSDataCRC32 | |
{ | |
// # Test data from Ruby/Zlib: | |
// | |
// require 'zlib' | |
// Zlib::crc32('') # => 0 | |
// Zlib::crc32('abc') # => 891568578 | |
// Zlib::crc32('Hello, World!') # => 3964322768 | |
NSString *str; | |
NSData *data; | |
NSUInteger checksum, expected; | |
str = @""; | |
data = [str dataUsingEncoding:NSUTF8StringEncoding]; | |
checksum = [data crc32]; | |
expected = 0; | |
STAssertEquals(checksum, expected, [NSString stringWithFormat:@"did not compute correct checksum for %@", str]); | |
str = @"abc"; | |
data = [str dataUsingEncoding:NSUTF8StringEncoding]; | |
checksum = [data crc32]; | |
expected = 891568578; | |
STAssertEquals(checksum, expected, [NSString stringWithFormat:@"did not compute correct checksum for %@", str]); | |
str = @"Hello, World!"; | |
data = [str dataUsingEncoding:NSUTF8StringEncoding]; | |
checksum = [data crc32]; | |
expected = 3964322768; | |
STAssertEquals(checksum, expected, [NSString stringWithFormat:@"did not compute correct checksum for %@", str]); | |
} | |
@end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Migrated to XCTest Framework