Skip to content

Instantly share code, notes, and snippets.

@padde

padde/NSData+CRC32.h

Created Jul 27, 2012
Embed
What would you like to do?
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

This comment has been minimized.

Copy link

@evgeniyd evgeniyd commented Dec 23, 2014

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