root.c 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. /*
  2. * This file is part of the UCB release of Plan 9. It is subject to the license
  3. * terms in the LICENSE file found in the top-level directory of this
  4. * distribution and at http://akaros.cs.berkeley.edu/files/Plan9License. No
  5. * part of the UCB release of Plan 9, including this file, may be copied,
  6. * modified, propagated, or distributed except according to the terms contained
  7. * in the LICENSE file.
  8. */
  9. #include <u.h>
  10. #include <libc.h>
  11. #include <venti.h>
  12. #include "cvt.h"
  13. static int
  14. checksize(int n)
  15. {
  16. if(n < 256 || n > VtMaxLumpSize) {
  17. werrstr("bad block size");
  18. return -1;
  19. }
  20. return 0;
  21. }
  22. void
  23. vtrootpack(VtRoot *r, uint8_t *p)
  24. {
  25. uint8_t *op = p;
  26. U16PUT(p, VtRootVersion);
  27. p += 2;
  28. memmove(p, r->name, sizeof(r->name));
  29. p += sizeof(r->name);
  30. memmove(p, r->type, sizeof(r->type));
  31. p += sizeof(r->type);
  32. memmove(p, r->score, VtScoreSize);
  33. p += VtScoreSize;
  34. U16PUT(p, r->blocksize);
  35. p += 2;
  36. memmove(p, r->prev, VtScoreSize);
  37. p += VtScoreSize;
  38. assert(p-op == VtRootSize);
  39. }
  40. int
  41. vtrootunpack(VtRoot *r, uint8_t *p)
  42. {
  43. uint8_t *op = p;
  44. uint vers;
  45. memset(r, 0, sizeof(*r));
  46. vers = U16GET(p);
  47. if(vers != VtRootVersion) {
  48. werrstr("unknown root version");
  49. return -1;
  50. }
  51. p += 2;
  52. memmove(r->name, p, sizeof(r->name));
  53. r->name[sizeof(r->name)-1] = 0;
  54. p += sizeof(r->name);
  55. memmove(r->type, p, sizeof(r->type));
  56. r->type[sizeof(r->type)-1] = 0;
  57. p += sizeof(r->type);
  58. memmove(r->score, p, VtScoreSize);
  59. p += VtScoreSize;
  60. r->blocksize = U16GET(p);
  61. if(checksize(r->blocksize) < 0)
  62. return -1;
  63. p += 2;
  64. memmove(r->prev, p, VtScoreSize);
  65. p += VtScoreSize;
  66. assert(p-op == VtRootSize);
  67. return 0;
  68. }