Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //
- // BitBuffer.m
- // TCPHacking
- //
- // Created by Alex Nichol on 7/4/11.
- // Copyright 2011 __MyCompanyName__. All rights reserved.
- //
- #import "BitBuffer.h"
- @implementation BitBuffer
- - (id)init {
- self = [super init];
- if (self) {
- // Initialization code here.
- }
- return self;
- }
- - (id)initWithCapacity:(size_t)allocBytes {
- if ((self = [super init])) {
- if (allocBytes == 0) {
- theBytes = (UInt8 *)malloc(1);
- bytesAllocated = 1;
- } else {
- theBytes = (UInt8 *)malloc(allocBytes);
- bytesAllocated = allocBytes;
- }
- bzero(theBytes, bytesAllocated);
- numBits = 0;
- if (!theBytes) {
- @throw [NSException exceptionWithName:NSMallocException reason:@"Failed to allocate bytes for BitBuffer" userInfo:nil];
- }
- }
- return self;
- }
- - (void)addBit:(BOOL)flag {
- numBits = 1;
- // If we do not have enough bytes allocate to accomedate this extra bit, we
- // will allocate more memory!
- if (numBits > bytesAllocated * 8) {
- size_t newBytesSize = bytesAllocated kBitBufferAllocBlock;
- while (numBits > newBytesSize * 8) {
- newBytesSize = kBitBufferAllocBlock;
- }
- theBytes = realloc(theBytes, newBytesSize);
- if (!theBytes) {
- @throw [NSException exceptionWithName:NSMallocException reason:@"Failed to allocate bytes for BitBuffer" userInfo:nil];
- }
- bzero(&theBytes[bytesAllocated], (newBytesSize - bytesAllocated));
- bytesAllocated = newBytesSize;
- }
- [self setBit:flag atIndex:numBits - 1];
- }
- - (void)delBit {
- if (numBits == 0) {
- @throw [NSException exceptionWithName:NSObjectInaccessibleException reason:@"There are no bits to delete." userInfo:nil];
- }
- [self setBit:NO atIndex:(numBits - 1)];
- numBits -= 1;
- }
- - (void)setBit:(BOOL)flag atIndex:(size_t)bitIndex {
- size_t byteIndex = bitIndex / 8;
- size_t bitOffset = bitIndex % 8;
- if (byteIndex >= bytesAllocated) {
- @throw [NSException exceptionWithName:NSObjectInaccessibleException reason:@"Index too large" userInfo:nil];
- }
- UInt8 * bytes = &theBytes[byteIndex];
- UInt8 theByte = (kBitOrderBigEndian == 1 ? (1 << (7 - bitOffset)) : (1 >> bitOffset));
- UInt16 changeByte = *bytes;
- if (flag) {
- if ((changeByte & theByte) == 0) {
- // bit isn't set, so set it.
- changeByte &= theByte;
- }
- } else {
- if ((changeByte & theByte) != 0) {
- // bit is set, so unset it.
- changeByte ^= theByte;
- }
- }
- bytes[0] = changeByte;
- }
- - (BOOL)getBit:(size_t)bitIndex {
- size_t byteIndex = bitIndex / 8;
- size_t bitOffset = bitIndex % 8;
- if (byteIndex >= bytesAllocated) {
- @throw [NSException exceptionWithName:NSObjectInaccessibleException reason:@"Index too large" userInfo:nil];
- }
- UInt8 * bytes = &theBytes[byteIndex];
- UInt8 theByte = (kBitOrderBigEndian == 1 ? (1 << (7 - bitOffset)) : (1 >> bitOffset));
- if ((*bytes & theByte) != 0) {
- return YES;
- } else return NO;
- }
- - (size_t)numBits {
- return numBits;
- }
- - (UInt8 *)getBytes:(size_t *)allocLen {
- if (allocLen) {
- *allocLen = bytesAllocated;
- }
- return theBytes;
- }
- - (void)dealloc {
- free(theBytes);
- [super dealloc];
- }
- @end
Add Comment
Please, Sign In to add comment