github.com/yoctocloud/packer@v0.6.2-0.20160520224004-e11a0a18423f/builder/azure/pkcs12/bmp-string.go (about) 1 package pkcs12 2 3 import ( 4 "errors" 5 "unicode/utf16" 6 ) 7 8 func bmpString(s string) ([]byte, error) { 9 // References: 10 // https://tools.ietf.org/html/rfc7292#appendix-B.1 11 // http://en.wikipedia.org/wiki/Plane_(Unicode)#Basic_Multilingual_Plane 12 // - non-BMP characters are encoded in UTF 16 by using a surrogate pair of 16-bit codes 13 // EncodeRune returns 0xfffd if the rune does not need special encoding 14 // - the above RFC provides the info that BMPStrings are NULL terminated. 15 16 rv := make([]byte, 0, 2*len(s)+2) 17 18 for _, r := range s { 19 if t, _ := utf16.EncodeRune(r); t != 0xfffd { 20 return nil, errors.New("string contains characters that cannot be encoded in UCS-2") 21 } 22 rv = append(rv, byte(r/256), byte(r%256)) 23 } 24 rv = append(rv, 0, 0) 25 return rv, nil 26 }