Guest User

Bitcoin transaction file dumper

a guest
Oct 21st, 2011
222
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.79 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3.  
  4. static FILE *f;
  5.  
  6.  
  7. void doczytaj(unsigned char *p, int len) {
  8.     if (fread(p, 1, len, f)!=len) {
  9.         fprintf(stderr, "File too short\n");
  10.         fclose(f);
  11.         exit(1);
  12.     }
  13. }
  14.  
  15. unsigned long long getle(unsigned char *p, int bytes) {
  16.     unsigned long long res=0;
  17.     while (bytes--) {
  18.         res|= p[bytes]<<(8*bytes);
  19.     }
  20.     return res;
  21. }
  22.  
  23. unsigned long long getvl() {
  24.     unsigned char b[8];
  25.     doczytaj(b, 1);
  26.     switch (*b) {
  27.         case 0xfd:
  28.             doczytaj(b, 2);
  29.             return getle(b, 2);
  30.         case 0xfe:
  31.             doczytaj(b, 4);
  32.             return getle(b, 4);
  33.         case 0xff:
  34.             doczytaj(b, 8);
  35.             return getle(b, 8);
  36.     }
  37.     return *b;
  38. }
  39.  
  40.  
  41. void prhash(unsigned char *p, unsigned int l) {
  42.     while (l--) printf("%02x", p[l]);
  43. }
  44.  
  45.  
  46. int main(int argc, char * argv[]) {
  47.     static unsigned char buf[0x10000];
  48.     unsigned long long i, sl, txcnt, v;
  49.  
  50.     if (argc!=2) {
  51.         printf("Specify the filename of the transaction file (.btd)\n");
  52.         return 1;
  53.     }
  54.  
  55.     f = fopen(argv[1], "rb");
  56.     if (!f) {
  57.         fprintf(stderr, "File %s not found\n", argv[1]);
  58.         return 1;
  59.     }
  60.  
  61.     doczytaj(buf, 4);
  62.     printf("Version: %llu\n", getle(buf, 4));
  63.  
  64.     txcnt = getvl();
  65.     printf("TX IN cnt: %llu\n", txcnt);
  66.     for (i=0; i<txcnt; i++) {
  67.         doczytaj(buf, 36);
  68.         sl = getvl();
  69.  
  70.         printf("  %lld) : ", i);
  71.         prhash(buf, 32);
  72.         printf(" Idx=%2lld  sl=%lld", getle(buf+32, 4), sl);
  73.         doczytaj(buf, sl);
  74.         doczytaj(buf, 4);
  75.  
  76.         printf(" seq=%x\n", (unsigned)getle(buf, 4));
  77.     }
  78.  
  79.     txcnt = getvl();
  80.     printf("TX OUT cnt: %llu\n", txcnt);
  81.     for (i=0; i<txcnt; i++) {
  82.         doczytaj(buf, 8);
  83.         sl = getvl();
  84.         v = getle(buf, 8);
  85.         printf("  %lld) : %lld.%8lld BTC    sl=%lld\n", i, v/100000000, v%100000000, sl);
  86.         doczytaj(buf, sl);
  87.     }
  88.  
  89.     doczytaj(buf, 4);
  90.     printf("Lock Time: %llu\n", getle(buf, 4));
  91.  
  92.     fclose(f);
  93.     return 0;
  94. }
  95.  
  96.  
Add Comment
Please, Sign In to add comment