Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Extend JKBigInteger to initialize using binary data (e.g. from NSData). #8

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions JKBigInteger/JKBigInteger.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@
- (id)initWithString:(NSString *)string andRadix:(int)radix;
- (id)initWithCString:(char *)cString;
- (id)initWithCString:(char *)cString andRadix:(int)radix;
- (id)initWithUnsignedByteArray:(const unsigned char*)byteArray andSize:(int)size;
- (id)initWithSignedByteArray:(const unsigned char*)byteArray andSize:(int)size;

- (id)add:(JKBigInteger *)bigInteger;
- (id)subtract:(JKBigInteger *)bigInteger;
Expand Down Expand Up @@ -50,6 +52,7 @@
- (NSString *)description;

- (unsigned int)countBytes;
- (unsigned int)countSignedBytes;
- (void)toByteArraySigned: (unsigned char*) byteArray;
- (void)toByteArrayUnsigned: (unsigned char*) byteArray;

Expand Down
44 changes: 44 additions & 0 deletions JKBigInteger/JKBigInteger.m
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,45 @@ - (id)initWithCoder:(NSCoder *)decoder {

return self;
}

- (id)initWithUnsignedByteArray:(const unsigned char *)byteArray andSize:(int)size {

self = [super init];

if (self) {
//allocate a buffer large enough to read this value
if (MP_OKAY != mp_init_size(&m_value, size))
return nil;

//read the value
if (MP_OKAY != mp_read_unsigned_bin(&m_value, byteArray, size)) {
mp_clear(&m_value);
return nil;
}
}

return self;
}

- (id)initWithSignedByteArray:(const unsigned char *)byteArray andSize:(int)size {

self = [super init];

if (self) {
//allocate a buffer large enough to read this value
if (MP_OKAY != mp_init_size(&m_value, size))
return nil;

//read the value
if (MP_OKAY != mp_read_signed_bin(&m_value, byteArray, size)) {
mp_clear(&m_value);
return nil;
}
}

return self;
}

- (void)encodeWithCoder:(NSCoder *)encoder {

mp_clamp(&m_value);
Expand Down Expand Up @@ -399,6 +438,11 @@ - (unsigned int)countBytes {
return (unsigned int) mp_unsigned_bin_size(&m_value);
}

/* Returns the number of bytes required to store this JKBigInteger as signed binary */
- (unsigned int)countSignedBytes {
return (unsigned int) mp_signed_bin_size(&m_value);
}

/* Retrieves the signed [big endian] format of this JKBigInteger */
- (void)toByteArraySigned: (unsigned char*) byteArray {
mp_to_signed_bin(&m_value, byteArray);
Expand Down
33 changes: 33 additions & 0 deletions Unit Tests/Unit_Tests.m
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,39 @@ - (void)testEqual2 {
XCTAssertEqual(result, NSOrderedSame, @"Test equal 2 failed!");
}

- (void)testInitWithUnsignedByteArray {
JKBigInteger *int1 = [[JKBigInteger alloc] initWithString:@"939202309485773939498585839393498477393984"];
NSMutableData* data = [[NSMutableData alloc] initWithCapacity:[int1 countBytes]];
data.length = [int1 countBytes];

[int1 toByteArrayUnsigned:[data mutableBytes]];
JKBigInteger *result = [[JKBigInteger alloc] initWithUnsignedByteArray:[data bytes] andSize:(int)[data length]];

XCTAssertEqualObjects([result stringValueWithRadix:10], [int1 stringValueWithRadix:10]);
}

- (void)testInitWithSignedByteArray0 {
JKBigInteger *int1 = [[JKBigInteger alloc] initWithString:@"939202309485773939498585839393498477393984"];
NSMutableData* data = [[NSMutableData alloc] initWithCapacity:[int1 countBytes]];
data.length = [int1 countSignedBytes];

[int1 toByteArraySigned:[data mutableBytes]];
JKBigInteger *result = [[JKBigInteger alloc] initWithSignedByteArray:[data bytes] andSize:(int)[data length]];

XCTAssertEqualObjects([result stringValueWithRadix:10], [int1 stringValueWithRadix:10]);
}

- (void)testInitWithSignedByteArray1 {
JKBigInteger *int1 = [[JKBigInteger alloc] initWithString:@"-939202309485773939498585839393498477393984"];
NSMutableData* data = [[NSMutableData alloc] initWithCapacity:[int1 countBytes]];
data.length = [int1 countSignedBytes];

[int1 toByteArraySigned:[data mutableBytes]];
JKBigInteger *result = [[JKBigInteger alloc] initWithSignedByteArray:[data bytes] andSize:(int)[data length]];

XCTAssertEqualObjects([result stringValueWithRadix:10], [int1 stringValueWithRadix:10]);
}

// Auto generated tests below

- (void)testAdd0 {
Expand Down