vtread.c 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. #include "stdinc.h"
  2. #include <bio.h>
  3. typedef struct Source Source;
  4. struct Source
  5. {
  6. ulong gen;
  7. int psize;
  8. int dsize;
  9. int dir;
  10. int active;
  11. int depth;
  12. uvlong size;
  13. uchar score[VtScoreSize];
  14. int reserved;
  15. };
  16. int bsize;
  17. Biobuf *bout;
  18. VtRootLump root;
  19. int ver;
  20. int cmp;
  21. int all;
  22. int find;
  23. uchar fscore[VtScoreSize];
  24. int dirSize;
  25. void (*parse)(Source*, uchar*);
  26. VtSession *z;
  27. int vtGetUint16(uchar *p);
  28. ulong vtGetUint32(uchar *p);
  29. uvlong vtGetUint48(uchar *p);
  30. void usage(void);
  31. int parseScore(uchar *score, char *buf, int n);
  32. void readRoot(VtRootLump*, uchar *score, char *file);
  33. void parse1(Source*, uchar*);
  34. void parse2(Source*, uchar*);
  35. int dumpDir(Source*, int indent);
  36. void
  37. main(int argc, char *argv[])
  38. {
  39. char *host = nil;
  40. uchar score[VtScoreSize];
  41. uchar buf[VtMaxLumpSize];
  42. int type;
  43. int n;
  44. type = VtDataType;
  45. ARGBEGIN{
  46. case 't':
  47. type = atoi(ARGF());
  48. break;
  49. }ARGEND
  50. vtAttach();
  51. bout = vtMemAllocZ(sizeof(Biobuf));
  52. Binit(bout, 1, OWRITE);
  53. if(argc != 1)
  54. usage();
  55. vtAttach();
  56. fmtinstall('V', vtScoreFmt);
  57. fmtinstall('R', vtErrFmt);
  58. z = vtDial(host);
  59. if(z == nil)
  60. vtFatal("could not connect to server: %s", vtGetError());
  61. if(!vtConnect(z, 0))
  62. sysfatal("vtConnect: %r");
  63. if(!parseScore(score, argv[0], strlen(argv[0])))
  64. vtFatal("could not parse score: %s", vtGetError());
  65. n = vtRead(z, score, type, buf, VtMaxLumpSize);
  66. if(n < 0)
  67. vtFatal("could not read block: %s", vtGetError());
  68. Bwrite(bout, buf, n);
  69. Bterm(bout);
  70. vtClose(z);
  71. vtDetach();
  72. exits(0);
  73. }
  74. void
  75. usage(void)
  76. {
  77. fprint(2, "%s: -t type score\n", argv0);
  78. exits("usage");
  79. }
  80. int
  81. parseScore(uchar *score, char *buf, int n)
  82. {
  83. int i, c;
  84. memset(score, 0, VtScoreSize);
  85. if(n < VtScoreSize*2)
  86. return 0;
  87. for(i=0; i<VtScoreSize*2; i++) {
  88. if(buf[i] >= '0' && buf[i] <= '9')
  89. c = buf[i] - '0';
  90. else if(buf[i] >= 'a' && buf[i] <= 'f')
  91. c = buf[i] - 'a' + 10;
  92. else if(buf[i] >= 'A' && buf[i] <= 'F')
  93. c = buf[i] - 'A' + 10;
  94. else {
  95. return 0;
  96. }
  97. if((i & 1) == 0)
  98. c <<= 4;
  99. score[i>>1] |= c;
  100. }
  101. return 1;
  102. }