| 1 | // Copyright ©2017 The bíogo Authors. All rights reserved. |
| 2 | // Use of this source code is governed by a BSD-style |
| 3 | // license that can be found in the LICENSE file. |
| 4 | |
| 5 | // Package ltf8 provides LTF-8 integer encoding. |
| 6 | // |
| 7 | // LTF-8 integer encoding is specified in the CRAM format specification |
| 8 | // section 2.3. |
| 9 | package ltf8 |
| 10 | |
| 11 | import "math/bits" |
| 12 | |
| 13 | // Len returns the number of bytes required to encode v. |
| 14 | func Len(v int64) int { |
| 15 | u := uint64(v) |
| 16 | switch { |
| 17 | case u < 0x80: |
| 18 | return 1 |
| 19 | case u < 0x4000: |
| 20 | return 2 |
| 21 | case u < 0x200000: |
| 22 | return 3 |
| 23 | case u < 0x10000000: |
| 24 | return 4 |
| 25 | case u < 0x800000000: |
| 26 | return 5 |
| 27 | case u < 0x40000000000: |
| 28 | return 6 |
| 29 | case u < 0x2000000000000: |
| 30 | return 7 |
| 31 | case u < 0x100000000000000: |
| 32 | return 8 |
| 33 | default: |
| 34 | return 9 |
| 35 | } |
| 36 | } |
| 37 | |
| 38 | // Decode decodes the LTF-8 encoding in b and returns the int64 value, its |
| 39 | // width in bytes and whether the decoding was successful. If the encoding |
| 40 | // is invalid, the expected length of b and false are returned. If b has zero |
| 41 | // length, zero, zero and false are returned. |
| 42 | func Decode(b []byte) (v int64, n int, ok bool) { |
| 43 | if len(b) == 0 { |
| 44 | return 0, 0, false |
| 45 | } |
| 46 | n = bits.LeadingZeros8(^(b[0])) + 1 |
| 47 | if len(b) < n { |
| 48 | return 0, n, false |
| 49 | } |
| 50 | switch n { |
| 51 | case 1: |
| 52 | v = int64(b[0]) |
| 53 | case 2: |
| 54 | v = int64(b[1]) | int64(b[0]&0x3f)<<8 |
| 55 | case 3: |
| 56 | v = int64(b[2]) | int64(b[1])<<8 | int64(b[0]&0x1f)<<16 |
| 57 | case 4: |
| 58 | v = int64(b[3]) | int64(b[2])<<8 | int64(b[1])<<16 | int64(b[0]&0x0f)<<24 |
| 59 | case 5: |
| 60 | v = int64(b[4]) | int64(b[3])<<8 | int64(b[2])<<16 | int64(b[1])<<24 | int64(b[0]&0x07)<<32 |
| 61 | case 6: |
| 62 | v = int64(b[5]) | int64(b[4])<<8 | int64(b[3])<<16 | int64(b[2])<<24 | int64(b[1])<<32 | int64(b[0]&0x03)<<40 |
| 63 | case 7: |
| 64 | v = int64(b[6]) | int64(b[5])<<8 | int64(b[4])<<16 | int64(b[3])<<24 | int64(b[2])<<32 | int64(b[1])<<40 | int64(b[0]&0x01)<<48 |
| 65 | case 8: |
| 66 | v = int64(b[7]) | int64(b[6])<<8 | int64(b[5])<<16 | int64(b[4])<<24 | int64(b[3])<<32 | int64(b[2])<<40 | int64(b[1])<<48 |
| 67 | case 9: |
| 68 | v = int64(b[8]) | int64(b[7])<<8 | int64(b[6])<<16 | int64(b[5])<<24 | int64(b[4])<<32 | int64(b[3])<<40 | int64(b[2])<<48 | int64(b[1])<<56 |
| 69 | } |
| 70 | return v, n, true |
| 71 | } |
| 72 | |
| 73 | // Encode encodes v as an LTF-8 into b, which must be large enough, and |
| 74 | // and returns the number of bytes written. |
| 75 | func Encode(b []byte, v int64) int { |
| 76 | u := uint64(v) |
| 77 | switch { |
| 78 | case u < 0x80: |
| 79 | b[0] = byte(u) |
| 80 | return 1 |
| 81 | case u < 0x4000: |
| 82 | _ = b[1] |
| 83 | b[0] = byte(u>>8)&0x3f | 0x80 |
| 84 | b[1] = byte(u) |
| 85 | return 2 |
| 86 | case u < 0x200000: |
| 87 | _ = b[2] |
| 88 | b[0] = byte(u>>16)&0x1f | 0xc0 |
| 89 | b[1] = byte(u >> 8) |
| 90 | b[2] = byte(u) |
| 91 | return 3 |
| 92 | case u < 0x10000000: |
| 93 | _ = b[3] |
| 94 | b[0] = byte(u>>24)&0x0f | 0xe0 |
| 95 | b[1] = byte(u >> 16) |
| 96 | b[2] = byte(u >> 8) |
| 97 | b[3] = byte(u) |
| 98 | return 4 |
| 99 | case u < 0x800000000: |
| 100 | _ = b[4] |
| 101 | b[0] = byte(u>>32)&0x07 | 0xf0 |
| 102 | b[1] = byte(u >> 24) |
| 103 | b[2] = byte(u >> 16) |
| 104 | b[3] = byte(u >> 8) |
| 105 | b[4] = byte(u) |
| 106 | return 5 |
| 107 | case u < 0x40000000000: |
| 108 | _ = b[5] |
| 109 | b[0] = byte(u>>40)&0x03 | 0xf8 |
| 110 | b[1] = byte(u >> 32) |
| 111 | b[2] = byte(u >> 24) |
| 112 | b[3] = byte(u >> 16) |
| 113 | b[4] = byte(u >> 8) |
| 114 | b[5] = byte(u) |
| 115 | return 6 |
| 116 | case u < 0x2000000000000: |
| 117 | _ = b[6] |
| 118 | b[0] = byte(u>>48)&0x01 | 0xfc |
| 119 | b[1] = byte(u >> 40) |
| 120 | b[2] = byte(u >> 32) |
| 121 | b[3] = byte(u >> 24) |
| 122 | b[4] = byte(u >> 16) |
| 123 | b[5] = byte(u >> 8) |
| 124 | b[6] = byte(u) |
| 125 | return 7 |
| 126 | case u < 0x100000000000000: |
| 127 | _ = b[7] |
| 128 | b[0] = 0xfe |
| 129 | b[1] = byte(u >> 48) |
| 130 | b[2] = byte(u >> 40) |
| 131 | b[3] = byte(u >> 32) |
| 132 | b[4] = byte(u >> 24) |
| 133 | b[5] = byte(u >> 16) |
| 134 | b[6] = byte(u >> 8) |
| 135 | b[7] = byte(u) |
| 136 | return 8 |
| 137 | default: |
| 138 | _ = b[8] |
| 139 | b[0] = 0xff |
| 140 | b[1] = byte(u >> 56) |
| 141 | b[2] = byte(u >> 48) |
| 142 | b[3] = byte(u >> 40) |
| 143 | b[4] = byte(u >> 32) |
| 144 | b[5] = byte(u >> 24) |
| 145 | b[6] = byte(u >> 16) |
| 146 | b[7] = byte(u >> 8) |
| 147 | b[8] = byte(u) |
| 148 | return 9 |
| 149 | } |
| 150 | } |