offset_4bytes.go 1.3 KB

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