github.com/safing/portbase@v0.19.5/database/record/meta-gencode.go (about) 1 package record 2 3 import ( 4 "fmt" 5 ) 6 7 // GenCodeSize returns the size of the gencode marshalled byte slice. 8 func (m *Meta) GenCodeSize() (s int) { 9 s += 34 10 return 11 } 12 13 // GenCodeMarshal gencode marshalls Meta into the given byte array, or a new one if its too small. 14 func (m *Meta) GenCodeMarshal(buf []byte) ([]byte, error) { 15 size := m.GenCodeSize() 16 { 17 if cap(buf) >= size { 18 buf = buf[:size] 19 } else { 20 buf = make([]byte, size) 21 } 22 } 23 i := uint64(0) 24 25 { 26 27 buf[0+0] = byte(m.Created >> 0) 28 29 buf[1+0] = byte(m.Created >> 8) 30 31 buf[2+0] = byte(m.Created >> 16) 32 33 buf[3+0] = byte(m.Created >> 24) 34 35 buf[4+0] = byte(m.Created >> 32) 36 37 buf[5+0] = byte(m.Created >> 40) 38 39 buf[6+0] = byte(m.Created >> 48) 40 41 buf[7+0] = byte(m.Created >> 56) 42 43 } 44 { 45 46 buf[0+8] = byte(m.Modified >> 0) 47 48 buf[1+8] = byte(m.Modified >> 8) 49 50 buf[2+8] = byte(m.Modified >> 16) 51 52 buf[3+8] = byte(m.Modified >> 24) 53 54 buf[4+8] = byte(m.Modified >> 32) 55 56 buf[5+8] = byte(m.Modified >> 40) 57 58 buf[6+8] = byte(m.Modified >> 48) 59 60 buf[7+8] = byte(m.Modified >> 56) 61 62 } 63 { 64 65 buf[0+16] = byte(m.Expires >> 0) 66 67 buf[1+16] = byte(m.Expires >> 8) 68 69 buf[2+16] = byte(m.Expires >> 16) 70 71 buf[3+16] = byte(m.Expires >> 24) 72 73 buf[4+16] = byte(m.Expires >> 32) 74 75 buf[5+16] = byte(m.Expires >> 40) 76 77 buf[6+16] = byte(m.Expires >> 48) 78 79 buf[7+16] = byte(m.Expires >> 56) 80 81 } 82 { 83 84 buf[0+24] = byte(m.Deleted >> 0) 85 86 buf[1+24] = byte(m.Deleted >> 8) 87 88 buf[2+24] = byte(m.Deleted >> 16) 89 90 buf[3+24] = byte(m.Deleted >> 24) 91 92 buf[4+24] = byte(m.Deleted >> 32) 93 94 buf[5+24] = byte(m.Deleted >> 40) 95 96 buf[6+24] = byte(m.Deleted >> 48) 97 98 buf[7+24] = byte(m.Deleted >> 56) 99 100 } 101 { 102 if m.secret { 103 buf[32] = 1 104 } else { 105 buf[32] = 0 106 } 107 } 108 { 109 if m.cronjewel { 110 buf[33] = 1 111 } else { 112 buf[33] = 0 113 } 114 } 115 return buf[:i+34], nil 116 } 117 118 // GenCodeUnmarshal gencode unmarshalls Meta and returns the bytes read. 119 func (m *Meta) GenCodeUnmarshal(buf []byte) (uint64, error) { 120 if len(buf) < m.GenCodeSize() { 121 return 0, fmt.Errorf("insufficient data: got %d out of %d bytes", len(buf), m.GenCodeSize()) 122 } 123 124 i := uint64(0) 125 126 { 127 m.Created = 0 | (int64(buf[0+0]) << 0) | (int64(buf[1+0]) << 8) | (int64(buf[2+0]) << 16) | (int64(buf[3+0]) << 24) | (int64(buf[4+0]) << 32) | (int64(buf[5+0]) << 40) | (int64(buf[6+0]) << 48) | (int64(buf[7+0]) << 56) 128 } 129 { 130 m.Modified = 0 | (int64(buf[0+8]) << 0) | (int64(buf[1+8]) << 8) | (int64(buf[2+8]) << 16) | (int64(buf[3+8]) << 24) | (int64(buf[4+8]) << 32) | (int64(buf[5+8]) << 40) | (int64(buf[6+8]) << 48) | (int64(buf[7+8]) << 56) 131 } 132 { 133 m.Expires = 0 | (int64(buf[0+16]) << 0) | (int64(buf[1+16]) << 8) | (int64(buf[2+16]) << 16) | (int64(buf[3+16]) << 24) | (int64(buf[4+16]) << 32) | (int64(buf[5+16]) << 40) | (int64(buf[6+16]) << 48) | (int64(buf[7+16]) << 56) 134 } 135 { 136 m.Deleted = 0 | (int64(buf[0+24]) << 0) | (int64(buf[1+24]) << 8) | (int64(buf[2+24]) << 16) | (int64(buf[3+24]) << 24) | (int64(buf[4+24]) << 32) | (int64(buf[5+24]) << 40) | (int64(buf[6+24]) << 48) | (int64(buf[7+24]) << 56) 137 } 138 { 139 m.secret = buf[32] == 1 140 } 141 { 142 m.cronjewel = buf[33] == 1 143 } 144 return i + 34, nil 145 }