offset_4bytes.go 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. // +build !5BytesOffset
  2. package types
  3. import (
  4. "fmt"
  5. )
  6. type OffsetHigher struct {
  7. // b4 byte
  8. }
  9. const (
  10. OffsetSize = 4
  11. MaxPossibleVolumeSize = 4 * 1024 * 1024 * 1024 * 8 // 32GB
  12. )
  13. func OffsetToBytes(bytes []byte, offset Offset) {
  14. bytes[3] = offset.b0
  15. bytes[2] = offset.b1
  16. bytes[1] = offset.b2
  17. bytes[0] = offset.b3
  18. }
  19. // only for testing, will be removed later.
  20. func Uint32ToOffset(offset uint32) Offset {
  21. return Offset{
  22. OffsetLower: OffsetLower{
  23. b0: byte(offset),
  24. b1: byte(offset >> 8),
  25. b2: byte(offset >> 16),
  26. b3: byte(offset >> 24),
  27. },
  28. }
  29. }
  30. func BytesToOffset(bytes []byte) Offset {
  31. return Offset{
  32. OffsetLower: OffsetLower{
  33. b0: bytes[3],
  34. b1: bytes[2],
  35. b2: bytes[1],
  36. b3: bytes[0],
  37. },
  38. }
  39. }
  40. func (offset Offset) IsZero() bool {
  41. return offset.b0 == 0 && offset.b1 == 0 && offset.b2 == 0 && offset.b3 == 0
  42. }
  43. func ToOffset(offset int64) Offset {
  44. smaller := uint32(offset / int64(NeedlePaddingSize))
  45. return Uint32ToOffset(smaller)
  46. }
  47. func (offset Offset) ToAcutalOffset() (actualOffset int64) {
  48. return (int64(offset.b0) + int64(offset.b1)<<8 + int64(offset.b2)<<16 + int64(offset.b3)<<24) * int64(NeedlePaddingSize)
  49. }
  50. func (offset Offset) String() string {
  51. return fmt.Sprintf("%d", int64(offset.b0)+int64(offset.b1)<<8+int64(offset.b2)<<16+int64(offset.b3)<<24)
  52. }