github.com/kubiko/snapd@v0.0.0-20201013125620-d4f3094d9ddf/bootloader/bootloadertest/utf16.go (about)

     1  // -*- Mode: Go; indent-tabs-mode: t -*-
     2  
     3  /*
     4   * Copyright (C) 2020 Canonical Ltd
     5   *
     6   * This program is free software: you can redistribute it and/or modify
     7   * it under the terms of the GNU General Public License version 3 as
     8   * published by the Free Software Foundation.
     9   *
    10   * This program is distributed in the hope that it will be useful,
    11   * but WITHOUT ANY WARRANTY; without even the implied warranty of
    12   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    13   * GNU General Public License for more details.
    14   *
    15   * You should have received a copy of the GNU General Public License
    16   * along with this program.  If not, see <http://www.gnu.org/licenses/>.
    17   *
    18   */
    19  
    20  package bootloadertest
    21  
    22  import (
    23  	"bytes"
    24  	"encoding/binary"
    25  	"unicode/utf16"
    26  )
    27  
    28  // UTF16Bytes converts the given string into its UTF16
    29  // encoding. Convenient for use together with efi.MockVars.
    30  func UTF16Bytes(s string) []byte {
    31  	r16 := utf16.Encode(bytes.Runes([]byte(s)))
    32  	b := bytes.NewBuffer(make([]byte, 0, (len(r16)+1)*2))
    33  	binary.Write(b, binary.LittleEndian, r16)
    34  	// zero termination
    35  	binary.Write(b, binary.LittleEndian, uint16(0))
    36  	return b.Bytes()
    37  }