spacepaste

  1.  
  2. $ cat ../../disassemblertmp/encoding.h
  3. int offset = 0;
  4. int bitcount = 0;
  5. unsigned char * readbin(unsigned char * encoding, int len) {
  6. unsigned char * bits = malloc(32);
  7. int ii = 7;
  8. int was_set = 0;
  9. for (int i = 0; i < len; i++)
  10. {
  11. bits[i]=!getBit(*encoding, ii)?'0':'1';
  12. ii--;
  13. bitcount++;
  14. if (bitcount>7) {
  15. encoding = (encoding)+0x1;
  16. bitcount = 0;
  17. was_set = 1;
  18. ii = 7;
  19. }
  20. }
  21. if (was_set == 0 && bitcount<=7) for(int i = 0; i < len; i++) *encoding = *encoding << 0x1;
  22. was_set = 0;
  23. return bits;
  24. }
  25. unsigned char * readbin_(unsigned char * encoding, int len) {
  26. unsigned char * bits = malloc(len);
  27. int ii = 7;
  28. int was_set = 0;
  29. for (int i = 0; i < len; i++)
  30. {
  31. bits[i]=!getBit(*encoding, ii)?'0':'1';
  32. ii--;
  33. bitcount++;
  34. if (bitcount>7) {
  35. encoding = (encoding)+0x1;
  36. bitcount = 0;
  37. was_set = 1;
  38. ii = 7;
  39. }
  40. }
  41. bits[len] = 0;
  42. if (was_set == 0 && bitcount<=7) for(int i = 0; i < len; i++) *encoding = *encoding << 0x1;
  43. was_set = 0;
  44. return bits;
  45. }
  46. unsigned char * readbin_l(unsigned char * encoding, int len) {
  47. unsigned char * bits = malloc(len);
  48. int ii = 7;
  49. int was_set = 0;
  50. for (int i = len-1; i >= 0 ; i--)
  51. {
  52. bits[i]=!getBit(*encoding, ii)?'0':'1';
  53. ii--;
  54. bitcount++;
  55. if (bitcount>7) {
  56. encoding = (encoding)+0x1;
  57. bitcount = 0;
  58. was_set = 1;
  59. ii = 7;
  60. }
  61. }
  62. bits[len] = 0;
  63. if (was_set == 0 && bitcount<=7) for(int i = 0; i < len; i++) *encoding = *encoding << 0x1;
  64. was_set = 0;
  65. return bits;
  66. }
  67. unsigned char * readbin_r(unsigned char * encoding, int len) {
  68. unsigned char * bits = malloc(len);
  69. int ii = 7;
  70. int was_set = 0;
  71. for (int i = 0; i < len; i++)
  72. {
  73. bits[i]=!getBit(*encoding, ii)?'0':'1';
  74. ii--;
  75. bitcount++;
  76. if (bitcount>7) {
  77. encoding = (encoding)+0x1;
  78. bitcount = 0;
  79. was_set = 1;
  80. ii = 7;
  81. }
  82. }
  83. bits[len] = 0;
  84. if (was_set == 0 && bitcount<=7) for(int i = 0; i < len; i++) *encoding = *encoding << 0x1;
  85. was_set = 0;
  86. return bits;
  87. }
  88. #define printBin32(num) { \
  89. for (int i = 31; i != -1; i--) { \
  90. ifc printf_r("%d", (num >> i) & 0x1); \
  91. else printf("%d", (num >> i) & 0x1); \
  92. } \
  93. printf(" = %s", #num); \
  94. }
  95. #define printBinLen(num, len) { \
  96. for (int i = len-1; i != -1; i--) { \
  97. ifc printf_r("%d", (num >> i) & 0x1); \
  98. else printf("%d", (num >> i) & 0x1); \
  99. } \
  100. printf(" = %s", #num); \
  101. }
  102. int BITC = 0;
  103. int BITL = 0;
  104. int BITI = 0;
  105. char * BinLen(unsigned char ** num, int len) {
  106. unsigned char * n = *num;
  107. unsigned char * bits = malloc(len+1);
  108. for (int i = len-1; i != -1; i--) {
  109. pi(getBit((*num), 7));
  110. pi(getBit((*num), 7));
  111. }
  112. return bits;
  113. }
  114. char * BinLen_(unsigned char ** encoding, int len) {
  115. unsigned char * bits = malloc(32);
  116. int ii = 7;
  117. int was_set = 0;
  118. for (int i = 0; i < len; i++)
  119. {
  120. bits[i]=(!getBit(*encoding, ii))?'0':'1';
  121. ii--;
  122. bitcount++;
  123. if (bitcount>7) {
  124. (*encoding)++;
  125. bitcount = 0;
  126. was_set = 1;
  127. ii = 7;
  128. }
  129. }
  130. // if (was_set == 0 && bitcount<=7) for(int i = 0; i < len; i++) *encoding = *encoding << 0x1;
  131. was_set = 0;
  132. return bits;
  133. }
  134. #define printarray32(num) { \
  135. for (int i = 0; i != 32; i++) { \
  136. ifc printf_r("%c", num[i]); \
  137. else printf("%c", num[i]); \
  138. } \
  139. printf(" = %s", #num); \
  140. }
  141. $ cat ../../disassemblertmp/binary.h
  142. #include <stdio.h>
  143. #include <inttypes.h>
  144. #include <string.h>
  145. #include <stdlib.h>
  146. #include <limits.h>
  147. #include <stdbool.h>
  148. #define BINMODE_WRITE 0
  149. #define BINMODE_APPEND 1
  150. // add two write modes:
  151. // truncate to 0 and append from index 0
  152. // append from current index without truncating
  153. #define ENDIAN_LITTLE 1
  154. #define ENDIAN_BIG 0
  155. #define pui64(x) printf("%s = %" PRIu64 "\n", #x, x);
  156. #define pi(x) printf("%s = %d\n", #x, x);
  157. #define ps(x) printf("%s = %s\n", #x, x);
  158. #define pc(x) printf("%s = %c\n", #x, x);
  159. #define psize_t(x) printf("%s = %zu\n", #x, x);
  160. #define pz(x) psize_t(x)
  161. int bitcount_ = 1;
  162. struct strStream {
  163. unsigned char *s;
  164. int bitpos;
  165. uint64_t *s64;
  166. int bitpos64;
  167. };
  168. struct binbuf {
  169. struct strStream * data;
  170. size_t length;
  171. size_t length64;
  172. int BINMODE;
  173. int bitcount;
  174. int bitcount64;
  175. };
  176. struct strStream * strStream_init(unsigned char * string) {
  177. struct strStream * str = malloc(sizeof(struct strStream));
  178. str->s = string;
  179. str->s64 = NULL;
  180. str->bitpos = 0;
  181. str->bitpos64 = 0;
  182. return str;
  183. }
  184. void strStream_deinit(struct strStream * * str) {
  185. (*str)->s = NULL;
  186. (*str)->s64 = NULL;
  187. (*str)->bitpos = 0;
  188. (*str)->bitpos64 = 0;
  189. free((*str));
  190. }
  191. struct binbuf binbuf_init(int mode) {
  192. struct binbuf bin = {0};
  193. bin.data = strStream_init(NULL);
  194. bin.BINMODE = mode;
  195. bin.bitcount = 0;
  196. bin.bitcount64 = 0;
  197. return bin;
  198. }
  199. int divtoshift(int x) {
  200. int n = 0;
  201. while(x /= 2) n++;
  202. return n;
  203. }
  204. // reads a binary string writing it as literal to a buffer
  205. int read__binary(struct binbuf * bin, char * s) {
  206. //printf( "INPUT:%s\n", s );
  207. int newlen = strlen(s);
  208. int totlen = bin->length + newlen; // adds number of bits in input string
  209. // realloc new total (rounded up) bytes for new bittotal
  210. bin->data->s = realloc(bin->data->s, ((bin->length = totlen) + (CHAR_BIT-1)) /CHAR_BIT );
  211. memset( bin->data->s + ( ( bin->bitcount + (CHAR_BIT-1)) /CHAR_BIT ), 0, ( ( bin->length - bin->bitcount + (CHAR_BIT-1)) /CHAR_BIT ) );
  212. int i;
  213. for( i = 0; i < newlen; i++) {
  214. bin->data->s[((bin->bitcount + i)>>divtoshift(CHAR_BIT))] |= ( s[i] == '1' ? (UINT8_MAX) : 0 ) >> ((CHAR_BIT-1)-(bin->bitcount + i)&(CHAR_BIT-1));
  215. }
  216. // bitcount is really the same as bin->length, but delayed ....
  217. bin->bitcount += i;
  218. return 0;
  219. }
  220. int read__binary64(struct binbuf * bin, char * s) {
  221. //printf( "INPUT:%s\n", s );
  222. int newlen = strlen(s);
  223. int totlen = bin->length64 + newlen; // adds number of bits in input string
  224. // realloc new total (rounded up) bytes for new bittotal
  225. bin->data->s64 = realloc(bin->data->s64, ((bin->length64 = totlen) + (64-1)) /64 );
  226. memset( bin->data->s64 + ( ( bin->bitcount64 + (64-1) ) / 64 ), 0, ( ( bin->length64 - bin->bitcount64 + (64-1) ) / 64 ) );
  227. int i;
  228. for( i = 0; i < newlen; i++) {
  229. bin->data->s64[((bin->bitcount64 + i)>>divtoshift(64))] |= ( s[i] == '1' ? (UINT64_MAX) : 0 ) >> ((64-1)-(bin->bitcount64 + i)&(64-1));
  230. }
  231. // bitcount is really the same as bin->length, but delayed ....
  232. bin->bitcount64 += i;
  233. return 0;
  234. }
  235. unsigned char * bits = NULL;
  236. int read__string_undo(struct strStream * strStream) {
  237. if (bits) strStream->bitpos -= strlen(bits);
  238. return 0;
  239. }
  240. // reads a string returning it as a binary string
  241. unsigned char * read__string(struct strStream * strStream, int len, bool undo, bool endian) {
  242. if (undo == true) read__string_undo(strStream);
  243. if (bits) {
  244. memset(bits,0,strlen(bits));
  245. free(bits);
  246. }
  247. bits = malloc(len+1);
  248. int i, idx = 0;
  249. if (endian == ENDIAN_BIG) {
  250. for ( i = 0; i < len; i++) {
  251. if( strStream->s[ (strStream->bitpos + i)>>divtoshift(CHAR_BIT) ]
  252. & ( 1 << ( ( strStream->bitpos + i ) & (CHAR_BIT-1) ) ) )
  253. bits[idx] = '1';
  254. else
  255. bits[idx] = '0';
  256. idx++;
  257. }
  258. } else if (endian == ENDIAN_LITTLE) { // reads incorrectly
  259. for ( i = len-1; i >= 0; i--) {
  260. if( strStream->s[ (strStream->bitpos + i)>>divtoshift(CHAR_BIT) ]
  261. & ( 1 << ( ( strStream->bitpos + i ) & (CHAR_BIT-1) ) ) )
  262. bits[idx] = '1';
  263. else
  264. bits[idx] = '0';
  265. idx++;
  266. }
  267. }
  268. strStream->bitpos += idx;
  269. bits[idx] = 0;
  270. return bits;
  271. }
  272. unsigned char * bits64 = NULL;
  273. int read__string_undo64(struct strStream * strStream) {
  274. if (bits64) strStream->bitpos64 -= strlen(bits64);
  275. return 0;
  276. }
  277. unsigned char * read__string64(struct strStream * strStream, int len, bool undo, bool endian) {
  278. if (undo == true) read__string_undo64(strStream);
  279. if (bits64) {
  280. memset(bits64,0,strlen(bits64));
  281. free(bits64);
  282. }
  283. bits64 = malloc(len+1);
  284. int i, idx = 0;
  285. if (endian == ENDIAN_BIG) {
  286. for ( i = 0; i < len; i++) {
  287. if( strStream->s64[ (strStream->bitpos64 + i)>>divtoshift(64) ]
  288. & ( (uint64_t)1 << ( ( strStream->bitpos64 + i ) & (64-1)) ) )
  289. bits64[idx] = '1';
  290. else
  291. bits64[idx] = '0';
  292. idx++;
  293. }
  294. } else if (endian == ENDIAN_LITTLE) {
  295. for ( i = len; i > 0; i--) {
  296. if( strStream->s64[ (strStream->bitpos64 + i)>>divtoshift(64) ]
  297. & ( (uint64_t)1 << ( ( strStream->bitpos64 + i ) & (64-1)) ) )
  298. bits64[idx] = '1';
  299. else
  300. bits64[idx] = '0';
  301. idx++;
  302. }
  303. }
  304. strStream->bitpos64 += len;
  305. bits64[idx] = 0;
  306. return bits64;
  307. }
  308.