zero.c 1.5 KB

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