bitstream.c 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  1. /*
  2. * qrencode - QR Code encoder
  3. *
  4. * Binary sequence class.
  5. * Copyright (C) 2006-2014 Kentaro Fukuchi <[email protected]>
  6. *
  7. * This library is free software; you can redistribute it and/or
  8. * modify it under the terms of the GNU Lesser General Public
  9. * License as published by the Free Software Foundation; either
  10. * version 2.1 of the License, or any later version.
  11. *
  12. * This library is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  15. * Lesser General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU Lesser General Public
  18. * License along with this library; if not, write to the Free Software
  19. * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
  20. */
  21. #if HAVE_CONFIG_H
  22. # include "config.h"
  23. #endif
  24. #include <stdio.h>
  25. #include <stdlib.h>
  26. #include <string.h>
  27. #include "bitstream.h"
  28. #define DEFAULT_BUFSIZE (128)
  29. BitStream *BitStream_new(void)
  30. {
  31. BitStream *bstream;
  32. bstream = (BitStream *)malloc(sizeof(BitStream));
  33. if(bstream == NULL) return NULL;
  34. bstream->length = 0;
  35. bstream->data = (unsigned char *)malloc(DEFAULT_BUFSIZE);
  36. if(bstream->data == NULL) {
  37. free(bstream);
  38. return NULL;
  39. }
  40. bstream->datasize = DEFAULT_BUFSIZE;
  41. return bstream;
  42. }
  43. static int BitStream_expand(BitStream *bstream)
  44. {
  45. unsigned char *data;
  46. data = (unsigned char *)realloc(bstream->data, bstream->datasize * 2);
  47. if(data == NULL) {
  48. return -1;
  49. }
  50. bstream->data = data;
  51. bstream->datasize *= 2;
  52. return 0;
  53. }
  54. static void BitStream_writeNum(unsigned char *dest, int bits, unsigned int num)
  55. {
  56. unsigned int mask;
  57. int i;
  58. unsigned char *p;
  59. p = dest;
  60. mask = 1 << (bits - 1);
  61. for(i=0; i<bits; i++) {
  62. if(num & mask) {
  63. *p = 1;
  64. } else {
  65. *p = 0;
  66. }
  67. p++;
  68. mask = mask >> 1;
  69. }
  70. }
  71. static void BitStream_writeBytes(unsigned char *dest, int size, unsigned char *data)
  72. {
  73. unsigned char mask;
  74. int i, j;
  75. unsigned char *p;
  76. p = dest;
  77. for(i=0; i<size; i++) {
  78. mask = 0x80;
  79. for(j=0; j<8; j++) {
  80. if(data[i] & mask) {
  81. *p = 1;
  82. } else {
  83. *p = 0;
  84. }
  85. p++;
  86. mask = mask >> 1;
  87. }
  88. }
  89. }
  90. int BitStream_append(BitStream *bstream, BitStream *arg)
  91. {
  92. int ret;
  93. if(arg == NULL) {
  94. return -1;
  95. }
  96. if(arg->length == 0) {
  97. return 0;
  98. }
  99. while(bstream->length + arg->length > bstream->datasize) {
  100. ret = BitStream_expand(bstream);
  101. if(ret < 0) return ret;
  102. }
  103. memcpy(bstream->data + bstream->length, arg->data, arg->length);
  104. bstream->length += arg->length;
  105. return 0;
  106. }
  107. int BitStream_appendNum(BitStream *bstream, int bits, unsigned int num)
  108. {
  109. int ret;
  110. if(bits == 0) return 0;
  111. while(bstream->datasize - bstream->length < bits) {
  112. ret = BitStream_expand(bstream);
  113. if(ret < 0) return ret;
  114. }
  115. BitStream_writeNum(bstream->data + bstream->length, bits, num);
  116. bstream->length += bits;
  117. return 0;
  118. }
  119. int BitStream_appendBytes(BitStream *bstream, int size, unsigned char *data)
  120. {
  121. int ret;
  122. if(size == 0) return 0;
  123. while(bstream->datasize - bstream->length < size * 8) {
  124. ret = BitStream_expand(bstream);
  125. if(ret < 0) return ret;
  126. }
  127. BitStream_writeBytes(bstream->data + bstream->length, size, data);
  128. bstream->length += size * 8;
  129. return 0;
  130. }
  131. unsigned char *BitStream_toByte(BitStream *bstream)
  132. {
  133. int i, j, size, bytes;
  134. unsigned char *data, v;
  135. unsigned char *p;
  136. size = BitStream_size(bstream);
  137. if(size == 0) {
  138. return NULL;
  139. }
  140. data = (unsigned char *)malloc((size + 7) / 8);
  141. if(data == NULL) {
  142. return NULL;
  143. }
  144. bytes = size / 8;
  145. p = bstream->data;
  146. for(i=0; i<bytes; i++) {
  147. v = 0;
  148. for(j=0; j<8; j++) {
  149. v = v << 1;
  150. v |= *p;
  151. p++;
  152. }
  153. data[i] = v;
  154. }
  155. if(size & 7) {
  156. v = 0;
  157. for(j=0; j<(size & 7); j++) {
  158. v = v << 1;
  159. v |= *p;
  160. p++;
  161. }
  162. data[bytes] = v;
  163. }
  164. return data;
  165. }
  166. void BitStream_free(BitStream *bstream)
  167. {
  168. if(bstream != NULL) {
  169. free(bstream->data);
  170. free(bstream);
  171. }
  172. }