func openfile(flags uint32, b *FileBuilder) (d filedlg) { d.buf = make([]uint16, w32.MAX_PATH) d.opf = &w32.OPENFILENAME{ File: utf16ptr(d.buf), MaxFile: uint32(len(d.buf)), Flags: flags, } d.opf.StructSize = uint32(unsafe.Sizeof(*d.opf)) if b.StartDir != "" { d.opf.InitialDir, _ = syscall.UTF16PtrFromString(b.StartDir) } if b.Dlg.Title != "" { d.opf.Title, _ = syscall.UTF16PtrFromString(b.Dlg.Title) } for _, filt := range b.Filters { /* build utf16 string of form "Music File\0*.mp3;*.ogg;*.wav;\0" */ d.filters = append(d.filters, utf16.Encode([]rune(filt.Desc))...) d.filters = append(d.filters, 0) for _, ext := range filt.Extensions { s := fmt.Sprintf("*.%s;", ext) d.filters = append(d.filters, utf16.Encode([]rune(s))...) } d.filters = append(d.filters, 0) } if d.filters != nil { d.filters = append(d.filters, 0, 0) // two extra NUL chars to terminate the list d.opf.Filter = utf16ptr(d.filters) } return d }
func (s *cScreen) draw() { // allocate a scratch line bit enough for no combining chars. // if you have combining characters, you may pay for extra allocs. if s.clear { s.clearScreen(s.style) s.clear = false s.cells.Invalidate() } buf := make([]uint16, 0, s.w) wcs := buf[:] lstyle := Style(-1) // invalid attribute lx, ly := -1, -1 ra := make([]rune, 1) for y := 0; y < int(s.h); y++ { for x := 0; x < int(s.w); x++ { mainc, combc, style, width := s.cells.GetContent(x, y) dirty := s.cells.Dirty(x, y) if style == StyleDefault { style = s.style } if !dirty || style != lstyle { // write out any data queued thus far // because we are going to skip over some // cells, or because we need to change styles s.writeString(lx, ly, lstyle, wcs) wcs = buf[0:0] lstyle = Style(-1) if !dirty { continue } } if x > s.w-width { mainc = ' ' combc = nil width = 1 } if len(wcs) == 0 { lstyle = style lx = x ly = y } ra[0] = mainc wcs = append(wcs, utf16.Encode(ra)...) if len(combc) != 0 { wcs = append(wcs, utf16.Encode(combc)...) } s.cells.SetDirty(x, y, false) x += width - 1 } s.writeString(lx, ly, lstyle, wcs) wcs = buf[0:0] lstyle = Style(-1) } }
// EncodeReparsePoint encodes a Win32 REPARSE_DATA_BUFFER structure describing a symlink or // mount point. func EncodeReparsePoint(rp *ReparsePoint) []byte { // Generate an NT path and determine if this is a relative path. var ntTarget string relative := false if strings.HasPrefix(rp.Target, `\\?\`) { ntTarget = rp.Target } else if strings.HasPrefix(rp.Target, `\\`) { ntTarget = `\??\UNC\` + rp.Target[2:] } else if len(rp.Target) >= 2 && isDriveLetter(rp.Target[0]) && rp.Target[1] == ':' { ntTarget = `\??\` + rp.Target } else { ntTarget = rp.Target relative = true } // The paths must be NUL-terminated even though they are counted strings. target16 := utf16.Encode([]rune(rp.Target + "\x00")) ntTarget16 := utf16.Encode([]rune(ntTarget + "\x00")) size := int(unsafe.Sizeof(reparseDataBuffer{})) - 8 size += len(ntTarget16)*2 + len(target16)*2 tag := uint32(reparseTagMountPoint) if !rp.IsMountPoint { tag = reparseTagSymlink size += 4 // Add room for symlink flags } data := reparseDataBuffer{ ReparseTag: tag, ReparseDataLength: uint16(size), SubstituteNameOffset: 0, SubstituteNameLength: uint16((len(ntTarget16) - 1) * 2), PrintNameOffset: uint16(len(ntTarget16) * 2), PrintNameLength: uint16((len(target16) - 1) * 2), } var b bytes.Buffer binary.Write(&b, binary.LittleEndian, &data) if !rp.IsMountPoint { flags := uint32(0) if relative { flags |= 1 } binary.Write(&b, binary.LittleEndian, flags) } binary.Write(&b, binary.LittleEndian, ntTarget16) binary.Write(&b, binary.LittleEndian, target16) return b.Bytes() }
func (c *Host) updateWinHost() string { new := c.createNewHost() path := os.Getenv("SystemRoot") old := path + `\System32\drivers\etc\hosts` newFile := utf16.Encode([]rune(new + "\x00")) oldFile := utf16.Encode([]rune(old + "\x00")) syscall.Rename(old, old+time.Now().Format("-2006-01-02-15-04-05")+".bak") err := syscall.MoveFile(&newFile[0], &oldFile[0]) fmt.Println(err) time.Sleep(time.Second * 10) return "" }
func (p *binStringPool) get(str string) *bstring { if p.m == nil { p.m = make(map[string]*bstring) } res := p.m[str] if res != nil { return res } res = &bstring{ ind: uint32(len(p.s)), str: str, } p.s = append(p.s, res) p.m[str] = res if len(str)>>16 > 0 { panic(fmt.Sprintf("string lengths over 1<<15 not yet supported, got len %d for string that starts %q", len(str), str[:100])) } strUTF16 := utf16.Encode([]rune(str)) res.enc = appendU16(nil, uint16(len(strUTF16))) for _, w := range strUTF16 { res.enc = appendU16(res.enc, w) } res.enc = appendU16(res.enc, 0) return res }
// WriteHeader writes the next backup stream header and prepares for calls to Write(). func (w *BackupStreamWriter) WriteHeader(hdr *BackupHeader) error { if w.bytesLeft != 0 { return fmt.Errorf("missing %d bytes", w.bytesLeft) } name := utf16.Encode([]rune(hdr.Name)) wsi := win32StreamId{ StreamId: hdr.Id, Attributes: hdr.Attributes, Size: uint64(hdr.Size), NameSize: uint32(len(name) * 2), } if hdr.Id == BackupSparseBlock { // Include space for the int64 block offset wsi.Size += 8 } if err := binary.Write(w.w, binary.LittleEndian, &wsi); err != nil { return err } if len(name) != 0 { if err := binary.Write(w.w, binary.LittleEndian, name); err != nil { return err } } if hdr.Id == BackupSparseBlock { if err := binary.Write(w.w, binary.LittleEndian, hdr.Offset); err != nil { return err } } w.bytesLeft = hdr.Size return nil }
func builtin_escape(input string) string { output := make([]byte, 0, len(input)) length := len(input) for index := 0; index < length; { if builtin_shouldEscape(input[index]) { chr, width := utf8.DecodeRuneInString(input[index:]) chr16 := utf16.Encode([]rune{chr})[0] if 256 > chr16 { output = append(output, '%', escapeBase16[chr16>>4], escapeBase16[chr16&15], ) } else { output = append(output, '%', 'u', escapeBase16[chr16>>12], escapeBase16[(chr16>>8)&15], escapeBase16[(chr16>>4)&15], escapeBase16[chr16&15], ) } index += width } else { output = append(output, input[index]) index += 1 } } return string(output) }
func EncodeString(v string, buffer []uint16) { raw := utf16.Encode([]rune(v)) copy(buffer, raw) for i := len(raw); i < len(buffer); i++ { buffer[i] = 0 } }
func (fp fieldParts) WriteTo(w io.Writer) (n int64, err error) { length := len(fieldPartV) fpv1, fpv2 := fieldPartV[:length-2], fieldPartV[length-2:] cew := &countErrWriter{w: w} length = len(fp.Parts) - 1 for i, part := range fp.Parts { if i == length { break } cew.Write(part) val := fp.Values[fp.Fields[i]] if len(val) == 0 { cew.Write(fieldPartV) } else { cew.Write(fpv1) cew.Write([]byte{0xfe, 0xff}) for _, u := range utf16.Encode([]rune(val)) { // http://stackoverflow.com/questions/6047970/weird-characters-when-filling-pdf-with-pdftk/19170162#19170162 // UTF16-BE cew.Write([]byte{byte(u >> 8), byte(u & 0xff)}) } cew.Write(fpv2) } if cew.err != nil { break } } cew.Write(fp.Parts[length]) return cew.n, cew.err }
func writeBytes(b io.Writer, values ...interface{}) error { for _, v := range values { var err error s, ok := v.(string) if ok { err = binary.Write(b, binary.BigEndian, int16(len(s))) if err != nil { return err } raw := utf16.Encode([]rune(s)) for _, ch := range raw { err = binary.Write(b, binary.BigEndian, ch) if err != nil { return err } } } else { err = binary.Write(b, binary.BigEndian, v) } if err != nil { return err } } return nil }
func newStringStash(value string, stash _stash) *_stringStash { self := &_stringStash{ value: value, value16: utf16.Encode([]rune(value)), _stash: stash, } return self }
// Save file record to ggpk file func (r FileRecord) Save(f *os.File) (err error) { name := utf16.Encode([]rune(r.Name)) name = append(name, 0) err = w(f, r.NameLength, err) err = w(f, r.Digest, err) err = w(f, name, err) return }
func str2ucs2(s string) []byte { res := utf16.Encode([]rune(s)) buf := new(bytes.Buffer) for _, item := range res { binary.Write(buf, binary.LittleEndian, item) } return buf.Bytes() }
func Utf8ToUtf16(u8 []rune) []byte { u16 := utf16.Encode(u8) buf := new(bytes.Buffer) binary.Write(buf, binary.LittleEndian, u16) return buf.Bytes() }
// UTF16FromString returns the UTF-16 encoding of the UTF-8 string // s, with a terminating NUL added. If s contains a NUL byte at any // location, it returns (nil, EINVAL). func UTF16FromString(s string) ([]uint16, error) { for i := 0; i < len(s); i++ { if s[i] == 0 { return nil, EINVAL } } return utf16.Encode([]rune(s + "\x00")), nil }
func (msg *ScratchMessage) PackString(toAdd string) (err error) { msg.PackUint16(uint16(len(toAdd)*2) + 2) msg.PackByte(0xFF) msg.PackByte(0xFE) u16 := utf16.Encode([]rune(toAdd)) err = binary.Write(msg.data, binary.LittleEndian, &u16) return }
// EncodeUcs2 encodes the given UTF-8 text into UCS2 (UTF-16) encoding and returns the produced octets. func encodeUcs2(str string) []byte { buf := utf16.Encode([]rune(str)) octets := make([]byte, 0, len(buf)*2) for _, n := range buf { octets = append(octets, byte(n&0xFF00>>8), byte(n&0x00FF)) } return octets }
// newArchive15 creates a new fileBlockReader for a Version 1.5 archive func newArchive15(r io.Reader, password string) fileBlockReader { a := new(archive15) a.v = r a.pass = utf16.Encode([]rune(password)) // convert to UTF-16 a.checksum.Hash32 = crc32.NewIEEE() a.buf = readBuf(make([]byte, 100)) return a }
// SysAllocStringLen copies up to length of given string returning pointer. func SysAllocStringLen(v string) (ss *int16) { utf16 := utf16.Encode([]rune(v + "\x00")) ptr := &utf16[0] pss, _, _ := procSysAllocStringLen.Call(uintptr(unsafe.Pointer(ptr)), uintptr(len(utf16)-1)) ss = (*int16)(unsafe.Pointer(pss)) return }
func utf16LeEncode(table *codePageTableInfo, str String) (bytes []byte) { for _, r := range utf16.Encode([]rune(string(str))) { bytes = append(bytes, byte(r)) bytes = append(bytes, byte(r>>8)) } return }
// Convert pass to UCS-2 (UTF-16) func ntPassword(pass string) []byte { buf := utf16.Encode([]rune(pass)) enc := make([]byte, len(pass)*2) for i := 0; i < len(pass); i++ { pos := 2 * i binary.LittleEndian.PutUint16(enc[pos:pos+2], buf[i]) } return enc }
func utf8_to_utf16le(utf8str string) []byte { utf16str := utf16.Encode([]rune(utf8str)) utf16lestr := make([]byte, len(utf16str)*2) for i, j := 0, 0; i < len(utf16str); i, j = i+1, j+2 { utf16lestr[j] = uint8(utf16str[i]) utf16lestr[j+1] = uint8(utf16str[i] >> 8) } return utf16lestr }
func SysAllocString(v string) uintptr { utf16 := utf16.Encode([]rune(v + "\x00")) ptr := &utf16[0] w := WStringPtr(SysAllocStringLen.Call(Arg(ptr), Arg(len(utf16)-1))) if w == 0 { panic(GetLastErrorString()) } return Arg(w) }
func WriteIntermediate(w io.Writer, intermediate string) error { _, err := w.Write([]byte{0xFF, 0xFE}) // Write BOM (Little Endian) if err != nil { return err } err = binary.Write(w, binary.LittleEndian, utf16.Encode([]rune(intermediate))) return err }
// convert Go string to UTF-16 encoded []byte (littleEndian) // done manually rather than using bytes and binary packages // for performance reasons func str2ucs2(s string) []byte { res := utf16.Encode([]rune(s)) ucs2 := make([]byte, 2*len(res)) for i := 0; i < len(res); i++ { ucs2[2*i] = byte(res[i]) ucs2[2*i+1] = byte(res[i] >> 8) } return ucs2 }
// BytesToUTF16 returns data given in UTF8 to UTF16 func BytesToUTF16(data []byte) []byte { u16 := utf16.Encode([]rune(string(data))) b := make([]byte, len(u16)*2) for i, r := range u16 { b[i*2] = byte(r) b[i*2+1] = byte(r >> 8) } return b }
func EncodeVariableString(v string, xor, sub int) []uint8 { data := utf16.Encode([]rune(v)) var odata bytes.Buffer end := binary.LittleEndian binary.Write(&odata, end, uint32(xor)^(uint32(len(data)+1)+uint32(sub))) binary.Write(&odata, end, data) binary.Write(&odata, end, uint16(0)) return odata.Bytes() }
func builtinString_charCodeAt(call FunctionCall) Value { checkObjectCoercible(call.This) value := toString(call.This) value16 := utf16.Encode([]rune(value)) index := toInteger(call.Argument(0)).value if 0 > index || index >= int64(len(value16)) { return NaNValue() } return toValue_uint16(value16[index]) }
func utf16FromString(s string) []byte { encoded := utf16.Encode([]rune(s)) // TODO: I'm sure there is an easier way to do the conversion from utf16 to bytes result := zeroBytes(len(encoded) * 2) for i := 0; i < len(encoded); i++ { result[i*2] = byte(encoded[i]) result[i*2+1] = byte(encoded[i] << 8) } return result }
func (pkg *Package) MarshalBinary() ([]byte, error) { bin := make([]byte, 284) putu16(bin, uint16(ResTablePackage)) putu16(bin[2:], 284) putu32(bin[8:], pkg.id) p := utf16.Encode([]rune(pkg.name)) for i, x := range p { putu16(bin[12+i*2:], x) } if pkg.typePool != nil { if pkg.typePool.IsUTF8() { pkg.typePool.flags ^= UTF8Flag defer func() { pkg.typePool.flags |= UTF8Flag }() } b, err := pkg.typePool.MarshalBinary() if err != nil { return nil, err } putu32(bin[268:], uint32(len(bin))) putu32(bin[272:], pkg.lastPublicType) bin = append(bin, b...) } if pkg.keyPool != nil { if pkg.keyPool.IsUTF8() { pkg.keyPool.flags ^= UTF8Flag defer func() { pkg.keyPool.flags |= UTF8Flag }() } b, err := pkg.keyPool.MarshalBinary() if err != nil { return nil, err } putu32(bin[276:], uint32(len(bin))) putu32(bin[280:], pkg.lastPublicKey) bin = append(bin, b...) } for _, spec := range pkg.specs { b, err := spec.MarshalBinary() if err != nil { return nil, err } bin = append(bin, b...) } putu32(bin[4:], uint32(len(bin))) return bin, nil }