root.c 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. #include <u.h>
  2. #include <libc.h>
  3. #include <venti.h>
  4. #include "cvt.h"
  5. static int
  6. checksize(int n)
  7. {
  8. if(n < 256 || n > VtMaxLumpSize) {
  9. werrstr("bad block size");
  10. return -1;
  11. }
  12. return 0;
  13. }
  14. void
  15. vtrootpack(VtRoot *r, uchar *p)
  16. {
  17. uchar *op = p;
  18. U16PUT(p, VtRootVersion);
  19. p += 2;
  20. memmove(p, r->name, sizeof(r->name));
  21. p += sizeof(r->name);
  22. memmove(p, r->type, sizeof(r->type));
  23. p += sizeof(r->type);
  24. memmove(p, r->score, VtScoreSize);
  25. p += VtScoreSize;
  26. U16PUT(p, r->blocksize);
  27. p += 2;
  28. memmove(p, r->prev, VtScoreSize);
  29. p += VtScoreSize;
  30. assert(p-op == VtRootSize);
  31. }
  32. int
  33. vtrootunpack(VtRoot *r, uchar *p)
  34. {
  35. uchar *op = p;
  36. uint vers;
  37. memset(r, 0, sizeof(*r));
  38. vers = U16GET(p);
  39. if(vers != VtRootVersion) {
  40. werrstr("unknown root version");
  41. return -1;
  42. }
  43. p += 2;
  44. memmove(r->name, p, sizeof(r->name));
  45. r->name[sizeof(r->name)-1] = 0;
  46. p += sizeof(r->name);
  47. memmove(r->type, p, sizeof(r->type));
  48. r->type[sizeof(r->type)-1] = 0;
  49. p += sizeof(r->type);
  50. memmove(r->score, p, VtScoreSize);
  51. p += VtScoreSize;
  52. r->blocksize = U16GET(p);
  53. if(checksize(r->blocksize) < 0)
  54. return -1;
  55. p += 2;
  56. memmove(r->prev, p, VtScoreSize);
  57. p += VtScoreSize;
  58. assert(p-op == VtRootSize);
  59. return 0;
  60. }