From: Jonathan Vollebregt Subject: [PATCH v4 3/8] reg.exe: Add wchar/type conversion functions Message-Id: <1412844914-30876-3-git-send-email-jnvsor@gmail.com> Date: Thu, 9 Oct 2014 10:55:09 +0200 --- programs/reg/reg.c | 60 ++++++++++++++++++++++++++++++++++-------------------- 1 file changed, 38 insertions(+), 22 deletions(-) diff --git a/programs/reg/reg.c b/programs/reg/reg.c index 415ee32..1caa3dc 100644 --- a/programs/reg/reg.c +++ b/programs/reg/reg.c @@ -22,6 +22,8 @@ #define MAX_ROOT_KEY_NAME_LENGTH 20 #define NUM_ROOT_KEYS 5 +#define MAX_TYPE_LENGTH 21 +#define NUM_TYPES 8 static const WCHAR short_HKEY_name[NUM_ROOT_KEYS][5] = { {'H','K','L','M',0}, @@ -47,6 +49,17 @@ static const HKEY HKEYs[NUM_ROOT_KEYS] = { HKEY_CURRENT_CONFIG, }; +static const WCHAR type_names[NUM_TYPES][MAX_TYPE_LENGTH] = { + {'R','E','G','_','N','O','N','E',0}, + {'R','E','G','_','S','Z',0}, + {'R','E','G','_','E','X','P','A','N','D','_','S','Z',0}, + {'R','E','G','_','B','I','N','A','R','Y',0}, + {'R','E','G','_','D','W','O','R','D',0}, + {'R','E','G','_','D','W','O','R','D','_','B','I','G','_','E','N','D','I','A','N',0}, + {'R','E','G','_','L','I','N','K',0}, + {'R','E','G','_','M','U','L','T','I','_','S','Z',0}, +}; + static int reg_printfW(const WCHAR *msg, ...) { va_list va_args; @@ -140,28 +153,27 @@ static HKEY path_get_key(const WCHAR *path) return k; } -static DWORD get_regtype(LPWSTR type) +static const WCHAR *type_get_wchar(const DWORD type) { - static const WCHAR szREG_SZ[] = {'R','E','G','_','S','Z',0}; - static const WCHAR szREG_MULTI_SZ[] = {'R','E','G','_','M','U','L','T','I','_','S','Z',0}; - static const WCHAR szREG_DWORD_BIG_ENDIAN[] = {'R','E','G','_','D','W','O','R','D','_','B','I','G','_','E','N','D','I','A','N',0}; - static const WCHAR szREG_DWORD[] = {'R','E','G','_','D','W','O','R','D',0}; - static const WCHAR szREG_BINARY[] = {'R','E','G','_','B','I','N','A','R','Y',0}; - static const WCHAR szREG_DWORD_LITTLE_ENDIAN[] = {'R','E','G','_','D','W','O','R','D','_','L','I','T','T','L','E','_','E','N','D','I','A','N',0}; - static const WCHAR szREG_NONE[] = {'R','E','G','_','N','O','N','E',0}; - static const WCHAR szREG_EXPAND_SZ[] = {'R','E','G','_','E','X','P','A','N','D','_','S','Z',0}; - - if (!type) - return REG_SZ; - - if (lstrcmpiW(type,szREG_SZ)==0) return REG_SZ; - if (lstrcmpiW(type,szREG_DWORD)==0) return REG_DWORD; - if (lstrcmpiW(type,szREG_MULTI_SZ)==0) return REG_MULTI_SZ; - if (lstrcmpiW(type,szREG_EXPAND_SZ)==0) return REG_EXPAND_SZ; - if (lstrcmpiW(type,szREG_DWORD_BIG_ENDIAN)==0) return REG_DWORD_BIG_ENDIAN; - if (lstrcmpiW(type,szREG_DWORD_LITTLE_ENDIAN)==0) return REG_DWORD_LITTLE_ENDIAN; - if (lstrcmpiW(type,szREG_BINARY)==0) return REG_BINARY; - if (lstrcmpiW(type,szREG_NONE)==0) return REG_NONE; + if (type_names[type][0]) + return type_names[type]; + else + return type_names[REG_NONE]; +} + +static DWORD wchar_get_type(const WCHAR *type) +{ + static const WCHAR type_dword_le[] = {'R','E','G','_','D','W','O','R','D','_','L','I','T','T','L','E','_','E','N','D','I','A','N',0}; + DWORD i; + + if (lstrcmpiW(type, type_dword_le) == 0) + return REG_DWORD_LITTLE_ENDIAN; + + for (i = 0; i < NUM_TYPES; i++) + { + if (lstrcmpiW(type, type_names[i]) == 0) + return i; + } return -1; } @@ -247,7 +259,11 @@ static int reg_add(WCHAR *key_name, WCHAR *value_name, BOOL value_empty, } } - reg_type = get_regtype(type); + if (!type) + reg_type = REG_SZ; + else + reg_type = wchar_get_type(type); + if (reg_type == -1) { RegCloseKey(subkey); -- 2.1.1