| 19 | } |
| 20 | |
| 21 | func decodeNullPaddedASCIIString(bs []byte) string { |
| 22 | // First, null-terminate if necessary. |
| 23 | bs = nullTerminate(bs) |
| 24 | |
| 25 | var replaceBuf [4]byte |
| 26 | n := utf8.EncodeRune(replaceBuf[:], unicode.ReplacementChar) |
| 27 | |
| 28 | // Replace all non-ASCII characters with the replacement character. |
| 29 | var outBytes []byte |
| 30 | for _, b := range bs { |
| 31 | if b <= unicode.MaxASCII { |
| 32 | outBytes = append(outBytes, b) |
| 33 | } else { |
| 34 | outBytes = append(outBytes, replaceBuf[:n]...) |
| 35 | } |
| 36 | } |
| 37 | |
| 38 | return string(outBytes) |
| 39 | } |
| 40 | |
| 41 | func encodeASCIIString(s string) ([]byte, error) { |
| 42 | var bs []byte |