Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@padde
Created July 27, 2012 11:34
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save padde/3187503 to your computer and use it in GitHub Desktop.
Save padde/3187503 to your computer and use it in GitHub Desktop.
NSData CRC32 Category
@interface NSData (CRC32)
-(NSUInteger) crc32;
@end
#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
#import <SenTestingKit/SenTestingKit.h>
#import "NSData+CRC32.h"
@interface NSDataCRC32Tests : SenTestCase
@end
#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
@evgeniyd
Copy link

Migrated to XCTest Framework

#import "NSData+CRC32.h"
#import <XCTest/XCTest.h>

@interface NSData_CRC32Tests : XCTestCase

@end

@implementation NSData_CRC32Tests

- (void)setUp {
    [super setUp];
    // Put setup code here. This method is called before the invocation of each test method in the class.
}

- (void)tearDown {
    // Put teardown code here. This method is called after the invocation of each test method in the class.
    [super tearDown];
}

- (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;
    XCTAssertEqual(checksum, expected, @"did not compute correct checksum for %@", str);

    str = @"abc";
    data = [str dataUsingEncoding:NSUTF8StringEncoding];
    checksum = [data crc32];
    expected = 891568578;
    XCTAssertEqual(checksum, expected, @"did not compute correct checksum for %@", str);

    str = @"Hello, World!";
    data = [str dataUsingEncoding:NSUTF8StringEncoding];
    checksum = [data crc32];
    expected = 3964322768;
    XCTAssertEqual(checksum, expected, @"did not compute correct checksum for %@", str);
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment