github.com/iDigitalFlame/xmt@v0.5.4/device/regedit/set.go (about)

     1  //go:build windows
     2  // +build windows
     3  
     4  // Copyright (C) 2020 - 2023 iDigitalFlame
     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 as published by
     8  // the Free Software Foundation, either version 3 of the License, or
     9  // any later version.
    10  //
    11  // This program is distributed in the hope that it will be useful,
    12  // but WITHOUT ANY WARRANTY; without even the implied warranty of
    13  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    14  // GNU General Public License for more details.
    15  //
    16  // You should have received a copy of the GNU General Public License
    17  // along with this program.  If not, see <https://www.gnu.org/licenses/>.
    18  //
    19  
    20  package regedit
    21  
    22  import (
    23  	"syscall"
    24  	"unsafe"
    25  
    26  	"github.com/iDigitalFlame/xmt/device/winapi"
    27  	"github.com/iDigitalFlame/xmt/device/winapi/registry"
    28  	"github.com/iDigitalFlame/xmt/util"
    29  )
    30  
    31  // SetString will attempt to set the data of the value in the supplied key path
    32  // to the supplied string as a REG_SZ value type.
    33  //
    34  // This will create the key/value if it does not exist.
    35  //
    36  // The key path can either be a "reg" style path (ex: HKLM\System or
    37  // HKCU\Software) or PowerShell style (ex: HKLM:\System or HKCU:\Software).
    38  //
    39  // Any errors writing the data will be returned.
    40  //
    41  // Returns device.ErrNoWindows on non-Windows devices.
    42  func SetString(key, value, v string) error {
    43  	return setString(key, value, registry.TypeString, v)
    44  }
    45  
    46  // SetBytes will attempt to set the data of the value in the supplied key path
    47  // to the supplied byte array as a REG_BINARY value type.
    48  //
    49  // This will create the key/value if it does not exist.
    50  //
    51  // The key path can either be a "reg" style path (ex: HKLM\System or
    52  // HKCU\Software) or PowerShell style (ex: HKLM:\System or HKCU:\Software).
    53  //
    54  // Any errors writing the data will be returned.
    55  //
    56  // Returns device.ErrNoWindows on non-Windows devices.
    57  func SetBytes(key, value string, b []byte) error {
    58  	return Set(key, value, registry.TypeBinary, b)
    59  }
    60  
    61  // SetDword will attempt to set the data of the value in the supplied key path
    62  // to the supplied uint32 integer as a REG_DWORD value type.
    63  //
    64  // This will create the key/value if it does not exist.
    65  //
    66  // The key path can either be a "reg" style path (ex: HKLM\System or
    67  // HKCU\Software) or PowerShell style (ex: HKLM:\System or HKCU:\Software).
    68  //
    69  // Any errors writing the data will be returned.
    70  //
    71  // Returns device.ErrNoWindows on non-Windows devices.
    72  func SetDword(key, value string, v uint32) error {
    73  	return Set(key, value, registry.TypeDword, (*[4]byte)(unsafe.Pointer(&v))[:])
    74  }
    75  
    76  // SetQword will attempt to set the data of the value in the supplied key path
    77  // to the supplied uint64 integer as a REG_QWORD value type.
    78  //
    79  // This will create the key/value if it does not exist.
    80  //
    81  // The key path can either be a "reg" style path (ex: HKLM\System or
    82  // HKCU\Software) or PowerShell style (ex: HKLM:\System or HKCU:\Software).
    83  //
    84  // Any errors writing the data will be returned.
    85  //
    86  // Returns device.ErrNoWindows on non-Windows devices.
    87  func SetQword(key, value string, v uint64) error {
    88  	return Set(key, value, registry.TypeQword, (*[8]byte)(unsafe.Pointer(&v))[:])
    89  }
    90  
    91  // SetExpandString will attempt to set the data of the value in the supplied key
    92  // path to the supplied string as a REG_EXPAND_SZ value type.
    93  //
    94  // This will create the key/value if it does not exist.
    95  //
    96  // The key path can either be a "reg" style path (ex: HKLM\System or
    97  // HKCU\Software) or PowerShell style (ex: HKLM:\System or HKCU:\Software).
    98  //
    99  // Any errors writing the data will be returned.
   100  //
   101  // Returns device.ErrNoWindows on non-Windows devices.
   102  func SetExpandString(key, value, v string) error {
   103  	return setString(key, value, registry.TypeExpandString, v)
   104  }
   105  
   106  // SetStrings will attempt to set the data of the value in the supplied key path
   107  // to the supplied string list as a REG_MULTI_SZ value type.
   108  //
   109  // This will create the key/value if it does not exist.
   110  //
   111  // The key path can either be a "reg" style path (ex: HKLM\System or
   112  // HKCU\Software) or PowerShell style (ex: HKLM:\System or HKCU:\Software).
   113  //
   114  // Any errors writing the data will be returned.
   115  //
   116  // Returns device.ErrNoWindows on non-Windows devices.
   117  func SetStrings(key, value string, v []string) error {
   118  	k, err := read(key, true)
   119  	if err != nil {
   120  		return err
   121  	}
   122  	var b util.Builder
   123  	for i := range v {
   124  		for x := range v[i] {
   125  			if v[i][x] == 0 {
   126  				k.Close()
   127  				return syscall.EINVAL
   128  			}
   129  		}
   130  		b.WriteString(v[i] + "\x00")
   131  	}
   132  	var (
   133  		r = winapi.UTF16EncodeStd([]rune(b.Output() + "\x00"))
   134  		o = (*[1 << 29]byte)(unsafe.Pointer(&r[0]))[: len(r)*2 : len(r)*2]
   135  	)
   136  	err = winapi.RegSetValueEx(uintptr(k), value, registry.TypeStringList, &o[0], uint32(len(o)))
   137  	k.Close()
   138  	return err
   139  }
   140  
   141  // Set will attempt to set the data of the value in the supplied key path to the
   142  // supplied value type indicated with the type flag and will use the provided
   143  // raw bytes for the value data.
   144  //
   145  // This function is a "lower-level" function but allows more control of HOW the
   146  // data is used.
   147  //
   148  // This will create the key/value if it does not exist.
   149  //
   150  // The key path can either be a "reg" style path (ex: HKLM\System or
   151  // HKCU\Software) or PowerShell style (ex: HKLM:\System or HKCU:\Software).
   152  //
   153  // Any errors writing the data will be returned.
   154  //
   155  // Returns device.ErrNoWindows on non-Windows devices.
   156  func Set(key, value string, t uint32, b []byte) error {
   157  	k, err := read(key, true)
   158  	if err != nil {
   159  		return err
   160  	}
   161  	if len(b) == 0 {
   162  		err = winapi.RegSetValueEx(uintptr(k), value, t, nil, 0)
   163  	} else {
   164  		err = winapi.RegSetValueEx(uintptr(k), value, t, &b[0], uint32(len(b)))
   165  	}
   166  	k.Close()
   167  	return err
   168  }
   169  func setString(key, value string, t uint32, v string) error {
   170  	k, err := read(key, true)
   171  	if err != nil {
   172  		return err
   173  	}
   174  	p, err := winapi.UTF16FromString(v)
   175  	if err != nil {
   176  		k.Close()
   177  		return err
   178  	}
   179  	b := (*[1 << 29]byte)(unsafe.Pointer(&p[0]))[: len(p)*2 : len(p)*2]
   180  	err = winapi.RegSetValueEx(uintptr(k), value, t, &b[0], uint32(len(b)))
   181  	k.Close()
   182  	return err
   183  }