hex_other.go (1016B)
1 // Copyright 2009 The Go 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 // +build !amd64 gccgo appengine 6 7 package hex 8 9 // RawEncode encodes src into EncodedLen(len(src)) 10 // bytes of dst. As a convenience, it returns the number 11 // of bytes written to dst, but this value is always EncodedLen(len(src)). 12 // RawEncode implements hexadecimal encoding for a given alphabet. 13 func RawEncode(dst, src, alpha []byte) int { 14 if len(alpha) != 16 { 15 panic("invalid alphabet") 16 } 17 18 encodeGeneric(dst, src, alpha) 19 return len(src) * 2 20 } 21 22 // Decode decodes src into DecodedLen(len(src)) bytes, returning the actual 23 // number of bytes written to dst. 24 // 25 // If Decode encounters invalid input, it returns an error describing the failure. 26 func Decode(dst, src []byte) (int, error) { 27 if len(src)%2 == 1 { 28 return 0, errLength 29 } 30 31 if n, ok := decodeGeneric(dst, src); !ok { 32 return 0, InvalidByteError(src[n]) 33 } 34 35 return len(src) / 2, nil 36 }