Created
October 22, 2019 06:26
-
-
Save lysu/57847a91ae3651db6e3f0eaf7f878f87 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const ( | |
ENC_GROUP_SIZE = 8 | |
ENC_MARKER = byte(0xFF) | |
) | |
func DecodeKVStorageKeyToKey(data []byte) []byte { | |
key := make([]byte, 0, len(data)/(ENC_GROUP_SIZE+1)*ENC_GROUP_SIZE) | |
offset := 0 | |
chunkLen := ENC_GROUP_SIZE + 1 | |
for { | |
nextOffset := offset + chunkLen | |
if nextOffset > len(data) { | |
return nil | |
} | |
chunk := data[offset:nextOffset] | |
offset = nextOffset | |
marker, bytes := chunk[len(chunk)-1], chunk[:len(chunk)-1] | |
padSize := int(ENC_MARKER - marker) | |
if padSize == 0 { | |
key = append(key, bytes...) | |
continue | |
} | |
if padSize > ENC_GROUP_SIZE { | |
return nil | |
} | |
bytes, padding := bytes[:ENC_GROUP_SIZE-padSize], bytes[ENC_GROUP_SIZE-padSize:] | |
key = append(key, bytes...) | |
for _, x := range padding { | |
if x != 0 { | |
return nil | |
} | |
} | |
data = data[offset:] | |
return key | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment