You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

55 lines
942 B

6 years ago
6 years ago
11 years ago
11 years ago
4 years ago
  1. package needle
  2. import (
  3. "fmt"
  4. "io"
  5. "hash/crc32"
  6. "github.com/seaweedfs/seaweedfs/weed/util"
  7. )
  8. var table = crc32.MakeTable(crc32.Castagnoli)
  9. type CRC uint32
  10. func NewCRC(b []byte) CRC {
  11. return CRC(0).Update(b)
  12. }
  13. func (c CRC) Update(b []byte) CRC {
  14. return CRC(crc32.Update(uint32(c), table, b))
  15. }
  16. // Value Deprecated. Just use the raw uint32 value to compare.
  17. func (c CRC) Value() uint32 {
  18. return uint32(c>>15|c<<17) + 0xa282ead8
  19. }
  20. func (n *Needle) Etag() string {
  21. bits := make([]byte, 4)
  22. util.Uint32toBytes(bits, uint32(n.Checksum))
  23. return fmt.Sprintf("%x", bits)
  24. }
  25. func NewCRCwriter(w io.Writer) *CRCwriter {
  26. return &CRCwriter{
  27. crc: CRC(0),
  28. w: w,
  29. }
  30. }
  31. type CRCwriter struct {
  32. crc CRC
  33. w io.Writer
  34. }
  35. func (c *CRCwriter) Write(p []byte) (n int, err error) {
  36. n, err = c.w.Write(p) // with each write ...
  37. c.crc = c.crc.Update(p)
  38. return
  39. }
  40. func (c *CRCwriter) Sum() uint32 { return uint32(c.crc) } // final hash