github.com/letsencrypt/trillian@v1.1.2-0.20180615153820-ae375a99d36a/storage/suffix.go (about) 1 // Copyright 2017 Google Inc. All Rights Reserved. 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 15 package storage 16 17 import ( 18 "encoding/base64" 19 ) 20 21 // Suffix represents the tail of a NodeID. It is the path within the subtree. 22 // The portion of the path that extends beyond the subtree is not part of this suffix. 23 type Suffix struct { 24 // bits is the number of bits in the node ID suffix. 25 // TODO(gdbelvin): make bits an integer. 26 Bits byte 27 // path is the suffix itself. 28 Path []byte 29 } 30 31 // String returns a string that represents Suffix. 32 // This is a base64 encoding of the following format: 33 // [ 1 byte for depth || path bytes ] 34 func (s Suffix) String() string { 35 r := make([]byte, 1, 1+(len(s.Path))) 36 r[0] = s.Bits 37 r = append(r, s.Path...) 38 return base64.StdEncoding.EncodeToString(r) 39 } 40 41 // ParseSuffix converts a suffix string back into a Suffix. 42 func ParseSuffix(s string) (Suffix, error) { 43 b, err := base64.StdEncoding.DecodeString(s) 44 if err != nil { 45 return Suffix{}, err 46 } 47 48 return Suffix{ 49 Bits: byte(b[0]), 50 Path: b[1:], 51 }, nil 52 }