zero.c 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  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 <oventi.h>
  12. /* score of a zero length block */
  13. uint8_t vtZeroScore[VtScoreSize] = {
  14. 0xda, 0x39, 0xa3, 0xee, 0x5e, 0x6b, 0x4b, 0x0d, 0x32, 0x55,
  15. 0xbf, 0xef, 0x95, 0x60, 0x18, 0x90, 0xaf, 0xd8, 0x07, 0x09
  16. };
  17. int
  18. vtZeroExtend(int type, uint8_t *buf, int n, int nn)
  19. {
  20. uint8_t *p, *ep;
  21. switch(type) {
  22. default:
  23. memset(buf+n, 0, nn-n);
  24. break;
  25. case VtPointerType0:
  26. case VtPointerType1:
  27. case VtPointerType2:
  28. case VtPointerType3:
  29. case VtPointerType4:
  30. case VtPointerType5:
  31. case VtPointerType6:
  32. case VtPointerType7:
  33. case VtPointerType8:
  34. case VtPointerType9:
  35. p = buf + (n/VtScoreSize)*VtScoreSize;
  36. ep = buf + (nn/VtScoreSize)*VtScoreSize;
  37. while(p < ep) {
  38. memmove(p, vtZeroScore, VtScoreSize);
  39. p += VtScoreSize;
  40. }
  41. memset(p, 0, buf+nn-p);
  42. break;
  43. }
  44. return 1;
  45. }
  46. int
  47. vtZeroTruncate(int type, uint8_t *buf, int n)
  48. {
  49. uint8_t *p;
  50. switch(type) {
  51. default:
  52. for(p = buf + n; p > buf; p--) {
  53. if(p[-1] != 0)
  54. break;
  55. }
  56. return p - buf;
  57. case VtRootType:
  58. if(n < VtRootSize)
  59. return n;
  60. return VtRootSize;
  61. case VtPointerType0:
  62. case VtPointerType1:
  63. case VtPointerType2:
  64. case VtPointerType3:
  65. case VtPointerType4:
  66. case VtPointerType5:
  67. case VtPointerType6:
  68. case VtPointerType7:
  69. case VtPointerType8:
  70. case VtPointerType9:
  71. /* ignore slop at end of block */
  72. p = buf + (n/VtScoreSize)*VtScoreSize;
  73. while(p > buf) {
  74. if(memcmp(p - VtScoreSize, vtZeroScore, VtScoreSize) != 0)
  75. break;
  76. p -= VtScoreSize;
  77. }
  78. return p - buf;
  79. }
  80. }