Guest User

Untitled

a guest
Oct 20th, 2017
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.06 KB | None | 0 0
  1. //
  2. // BitBuffer.m
  3. // TCPHacking
  4. //
  5. // Created by Alex Nichol on 7/4/11.
  6. // Copyright 2011 __MyCompanyName__. All rights reserved.
  7. //
  8.  
  9. #import "BitBuffer.h"
  10.  
  11.  
  12. @implementation BitBuffer
  13.  
  14. - (id)init {
  15. self = [super init];
  16. if (self) {
  17. // Initialization code here.
  18. }
  19.  
  20. return self;
  21. }
  22.  
  23. - (id)initWithCapacity:(size_t)allocBytes {
  24. if ((self = [super init])) {
  25. if (allocBytes == 0) {
  26. theBytes = (UInt8 *)malloc(1);
  27. bytesAllocated = 1;
  28. } else {
  29. theBytes = (UInt8 *)malloc(allocBytes);
  30. bytesAllocated = allocBytes;
  31. }
  32. bzero(theBytes, bytesAllocated);
  33. numBits = 0;
  34. if (!theBytes) {
  35. @throw [NSException exceptionWithName:NSMallocException reason:@"Failed to allocate bytes for BitBuffer" userInfo:nil];
  36. }
  37. }
  38. return self;
  39. }
  40.  
  41. - (void)addBit:(BOOL)flag {
  42. numBits = 1;
  43. // If we do not have enough bytes allocate to accomedate this extra bit, we
  44. // will allocate more memory!
  45. if (numBits > bytesAllocated * 8) {
  46. size_t newBytesSize = bytesAllocated kBitBufferAllocBlock;
  47. while (numBits > newBytesSize * 8) {
  48. newBytesSize = kBitBufferAllocBlock;
  49. }
  50. theBytes = realloc(theBytes, newBytesSize);
  51. if (!theBytes) {
  52. @throw [NSException exceptionWithName:NSMallocException reason:@"Failed to allocate bytes for BitBuffer" userInfo:nil];
  53. }
  54. bzero(&theBytes[bytesAllocated], (newBytesSize - bytesAllocated));
  55. bytesAllocated = newBytesSize;
  56. }
  57. [self setBit:flag atIndex:numBits - 1];
  58. }
  59.  
  60. - (void)delBit {
  61. if (numBits == 0) {
  62. @throw [NSException exceptionWithName:NSObjectInaccessibleException reason:@"There are no bits to delete." userInfo:nil];
  63. }
  64. [self setBit:NO atIndex:(numBits - 1)];
  65. numBits -= 1;
  66. }
  67.  
  68. - (void)setBit:(BOOL)flag atIndex:(size_t)bitIndex {
  69. size_t byteIndex = bitIndex / 8;
  70. size_t bitOffset = bitIndex % 8;
  71. if (byteIndex >= bytesAllocated) {
  72. @throw [NSException exceptionWithName:NSObjectInaccessibleException reason:@"Index too large" userInfo:nil];
  73. }
  74. UInt8 * bytes = &theBytes[byteIndex];
  75. UInt8 theByte = (kBitOrderBigEndian == 1 ? (1 << (7 - bitOffset)) : (1 >> bitOffset));
  76. UInt16 changeByte = *bytes;
  77. if (flag) {
  78. if ((changeByte & theByte) == 0) {
  79. // bit isn't set, so set it.
  80. changeByte &= theByte;
  81. }
  82. } else {
  83. if ((changeByte & theByte) != 0) {
  84. // bit is set, so unset it.
  85. changeByte ^= theByte;
  86. }
  87. }
  88. bytes[0] = changeByte;
  89. }
  90.  
  91. - (BOOL)getBit:(size_t)bitIndex {
  92. size_t byteIndex = bitIndex / 8;
  93. size_t bitOffset = bitIndex % 8;
  94. if (byteIndex >= bytesAllocated) {
  95. @throw [NSException exceptionWithName:NSObjectInaccessibleException reason:@"Index too large" userInfo:nil];
  96. }
  97. UInt8 * bytes = &theBytes[byteIndex];
  98. UInt8 theByte = (kBitOrderBigEndian == 1 ? (1 << (7 - bitOffset)) : (1 >> bitOffset));
  99. if ((*bytes & theByte) != 0) {
  100. return YES;
  101. } else return NO;
  102. }
  103.  
  104. - (size_t)numBits {
  105. return numBits;
  106. }
  107.  
  108. - (UInt8 *)getBytes:(size_t *)allocLen {
  109. if (allocLen) {
  110. *allocLen = bytesAllocated;
  111. }
  112. return theBytes;
  113. }
  114.  
  115. - (void)dealloc {
  116. free(theBytes);
  117. [super dealloc];
  118. }
  119.  
  120. @end
Add Comment
Please, Sign In to add comment