buffer.c 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. #include <stdio.h>
  2. #include <unistd.h>
  3. #include <malloc.h>
  4. #include <string.h>
  5. #include "buffer.h"
  6. /**
  7. * Create a new byte buffer
  8. *
  9. * @return the created buffer
  10. */
  11. BYTE_BUFFER createByteBuffer()
  12. {
  13. BYTE_BUFFER buffer;
  14. buffer.size = 0;
  15. buffer.data = (char*) malloc(1);
  16. return buffer;
  17. }
  18. /**
  19. * Release the memory
  20. *
  21. * @param buffer the buffer to destroy
  22. */
  23. void destroyByteBuffer(BYTE_BUFFER buffer)
  24. {
  25. free(buffer.data);
  26. }
  27. /**
  28. * Print all the bits from a buffer
  29. *
  30. * @param buffer the buffer holding the data
  31. */
  32. void printfBitBuffer(BYTE_BUFFER buffer)
  33. {
  34. printf("bytes: %lu\nbits: %lu\n", buffer.size, buffer.size*8);
  35. unsigned int x;
  36. for(x=0; x<buffer.size ; x++) {
  37. printfBit(buffer.data[x]);
  38. printf("\n");
  39. }
  40. }
  41. /**
  42. * Print a byte in binary
  43. *
  44. * @param byte the byte to print
  45. */
  46. void printfBit(unsigned char byte)
  47. {
  48. int i;
  49. for(i=0x80; i>0; i>>=1) {
  50. fprintf(stdout, "%d", ((byte & i) == i));
  51. }
  52. }
  53. /**
  54. * Print all the bytes from a buffer
  55. *
  56. * @param buffer the buffer holding the data
  57. */
  58. void printfByteBuffer(BYTE_BUFFER buffer)
  59. {
  60. unsigned int i;
  61. for(i=0; i<buffer.size; i++) {
  62. fprintf(stdout, "%02X ", (unsigned char)buffer.data[i]);
  63. }
  64. fprintf(stdout, "\n");
  65. }
  66. /**
  67. * Push a byte in a buffer
  68. *
  69. * @param buffer the buffer holding the data
  70. * @param byte the byte to push
  71. */
  72. void pushByte(BYTE_BUFFER* buffer, unsigned char byte)
  73. {
  74. buffer->size++;
  75. buffer->data = (char*)realloc(buffer->data, buffer->size);
  76. buffer->data[buffer->size-1] = byte;
  77. }
  78. /**
  79. * Push a word in a buffer
  80. *
  81. * @param buffer the buffer holding the data
  82. * @param word the word to push
  83. */
  84. void pushWord(BYTE_BUFFER* buffer, unsigned short int word)
  85. {
  86. unsigned char* data = (unsigned char*)&word;
  87. pushByte(buffer, data[1]);
  88. pushByte(buffer, data[0]);
  89. }
  90. /**
  91. * Push some bytes in a buffer
  92. *
  93. * @param buffer the buffer holding the data
  94. * @param bytes the bytes to push
  95. * @param len the number of bytes to push
  96. */
  97. void pushBytes(BYTE_BUFFER* buffer, unsigned char *byte, unsigned int len)
  98. {
  99. buffer->data = (char*)realloc(buffer->data, buffer->size+len);
  100. memcpy(&buffer->data[buffer->size], byte, len);
  101. buffer->size += len;
  102. }
  103. /**
  104. * Gives the bit at a specific position
  105. *
  106. * @param buffer the buffer to evaluate
  107. * @param n the bit position
  108. *
  109. * @return the bit value
  110. */
  111. unsigned int bitAt(BYTE_BUFFER buffer, unsigned long int n)
  112. {
  113. unsigned char byte = buffer.data[n / 8];
  114. return (byte & (0x80 >> (n % 8)) != 0);
  115. }