package inline import ( "errors" ) const ( hextable = "0123456789abcdef" reverseHexTable = "" + "\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff" + "\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff" + "\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff" + "\x00\x01\x02\x03\x04\x05\x06\x07\x08\x09\xff\xff\xff\xff\xff\xff" + "\xff\x0a\x0b\x0c\x0d\x0e\x0f\xff\xff\xff\xff\xff\xff\xff\xff\xff" + "\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff" + "\xff\x0a\x0b\x0c\x0d\x0e\x0f\xff\xff\xff\xff\xff\xff\xff\xff\xff" + "\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff" + "\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff" + "\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff" + "\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff" + "\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff" + "\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff" + "\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff" + "\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff" + "\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff" ) // EncodedLen returns the length of an encoding of n source bytes. // Specifically, it returns n * 2. func EncodedLen(n int) int { return n * 2 } // Encode encodes src into [EncodedLen](len(src)) // bytes of dst. As a convenience, it returns the number // of bytes written to dst, but this value is always [EncodedLen](len(src)). // Encode implements hexadecimal encoding. func Encode(dst, src []byte) int { j := 0 for _, v := range src { dst[j] = hextable[v>>4] dst[j+1] = hextable[v&0x0f] j += 2 } return len(src) * 2 } // ErrLength reports an attempt to decode an odd-length input // using [Decode] or [DecodeString]. // The stream-based Decoder returns [io.ErrUnexpectedEOF] instead of ErrLength. var ErrLength = errors.New("encoding/hex: odd length hex string") // InvalidByteError values describe errors resulting from an invalid byte in a hex string. type InvalidByteError byte func (e InvalidByteError) Error() string { return "encoding/hex: invalid byte" } // DecodedLen returns the length of a decoding of x source bytes. // Specifically, it returns x / 2. func DecodedLen(x int) int { return x / 2 } // Decode decodes src into [DecodedLen](len(src)) bytes, // returning the actual number of bytes written to dst. // // Decode expects that src contains only hexadecimal // characters and that src has even length. // If the input is malformed, Decode returns the number // of bytes decoded before the error. func Decode(dst, src []byte) (int, error) { i, j := 0, 1 for ; j < len(src); j += 2 { p := src[j-1] q := src[j] a := reverseHexTable[p] b := reverseHexTable[q] if a > 0x0f { return i, InvalidByteError(p) } if b > 0x0f { return i, InvalidByteError(q) } dst[i] = (a << 4) | b i++ } if len(src)%2 == 1 { // Check for invalid char before reporting bad length, // since the invalid char (if present) is an earlier problem. if reverseHexTable[src[j-1]] > 0x0f { return i, InvalidByteError(src[j-1]) } return i, ErrLength } return i, nil }