Annotation of truecrypt/common/registry.c, revision 1.1.1.16

1.1.1.7   root        1: /*
1.1.1.16! root        2:  Copyright (c) 2004-2010 TrueCrypt Developers Association. All rights reserved.
1.1.1.4   root        3: 
1.1.1.16! root        4:  Governed by the TrueCrypt License 3.0 the full text of which is contained in
1.1.1.15  root        5:  the file License.txt included in TrueCrypt binary and source code distribution
                      6:  packages.
1.1.1.4   root        7: */
1.1       root        8: 
1.1.1.4   root        9: #include "Tcdefs.h"
                     10: #include "Registry.h"
1.1       root       11: 
1.1.1.9   root       12: BOOL ReadLocalMachineRegistryDword (char *subKey, char *name, DWORD *value)
                     13: {
                     14:        HKEY hkey = 0;
                     15:        DWORD size = sizeof (*value);
1.1.1.10  root       16:        DWORD type;
1.1.1.9   root       17: 
                     18:        if (RegOpenKeyEx (HKEY_LOCAL_MACHINE, subKey, 0, KEY_READ, &hkey) != ERROR_SUCCESS)
                     19:                return FALSE;
                     20: 
1.1.1.10  root       21:        if (RegQueryValueEx (hkey, name, NULL, &type, (BYTE *) value, &size) != ERROR_SUCCESS)
1.1.1.9   root       22:        {
                     23:                RegCloseKey (hkey);
                     24:                return FALSE;
                     25:        }
                     26: 
                     27:        RegCloseKey (hkey);
1.1.1.10  root       28:        return type == REG_DWORD;
                     29: }
                     30: 
                     31: BOOL ReadLocalMachineRegistryMultiString (char *subKey, char *name, char *value, DWORD *size)
                     32: {
                     33:        HKEY hkey = 0;
                     34:        DWORD type;
                     35: 
                     36:        if (RegOpenKeyEx (HKEY_LOCAL_MACHINE, subKey, 0, KEY_READ, &hkey) != ERROR_SUCCESS)
                     37:                return FALSE;
                     38: 
                     39:        if (RegQueryValueEx (hkey, name, NULL, &type, (BYTE *) value, size) != ERROR_SUCCESS)
                     40:        {
                     41:                RegCloseKey (hkey);
                     42:                return FALSE;
                     43:        }
                     44: 
                     45:        RegCloseKey (hkey);
                     46:        return type == REG_MULTI_SZ;
1.1.1.9   root       47: }
                     48: 
1.1.1.16! root       49: BOOL ReadLocalMachineRegistryString (const char *subKey, char *name, char *str, DWORD *size)
1.1.1.12  root       50: {
                     51:        HKEY hkey = 0;
                     52:        DWORD type;
                     53: 
                     54:        if (RegOpenKeyEx (HKEY_LOCAL_MACHINE, subKey, 0, KEY_READ, &hkey) != ERROR_SUCCESS)
                     55:                return FALSE;
                     56: 
                     57:        if (RegQueryValueEx (hkey, name, NULL, &type, (BYTE *) str, size) != ERROR_SUCCESS)
                     58:        {
                     59:                RegCloseKey (hkey);
                     60:                return FALSE;
                     61:        }
                     62: 
                     63:        RegCloseKey (hkey);
                     64:        return type == REG_SZ;
                     65: }
                     66: 
1.1.1.16! root       67: BOOL ReadLocalMachineRegistryStringNonReflected (const char *subKey, char *name, char *str, DWORD *size)
        !            68: {
        !            69:        HKEY hkey = 0;
        !            70:        DWORD type;
        !            71: 
        !            72:        if (RegOpenKeyEx (HKEY_LOCAL_MACHINE, subKey, 0, KEY_READ | KEY_WOW64_64KEY, &hkey) != ERROR_SUCCESS)
        !            73:                return FALSE;
        !            74: 
        !            75:        if (RegQueryValueEx (hkey, name, NULL, &type, (BYTE *) str, size) != ERROR_SUCCESS)
        !            76:        {
        !            77:                RegCloseKey (hkey);
        !            78:                return FALSE;
        !            79:        }
        !            80: 
        !            81:        RegCloseKey (hkey);
        !            82:        return type == REG_SZ;
        !            83: }
        !            84: 
1.1.1.4   root       85: int ReadRegistryInt (char *subKey, char *name, int defaultValue)
1.1       root       86: {
                     87:        HKEY hkey = 0;
                     88:        DWORD value, size = sizeof (DWORD);
                     89: 
1.1.1.4   root       90:        if (RegOpenKeyEx (HKEY_CURRENT_USER, subKey,
1.1       root       91:                0, KEY_READ, &hkey) != ERROR_SUCCESS)
                     92:                return defaultValue;
                     93: 
                     94:        if (RegQueryValueEx (hkey, name, 0,     0, (LPBYTE) &value, &size) != ERROR_SUCCESS)
                     95:                value = defaultValue;
                     96: 
                     97:        RegCloseKey (hkey);
                     98:        return value;
                     99: }
                    100: 
1.1.1.4   root      101: char *ReadRegistryString (char *subKey, char *name, char *defaultValue, char *str, int maxLen)
1.1       root      102: {
                    103:        HKEY hkey = 0;
1.1.1.4   root      104:        char value[MAX_PATH*4];
1.1       root      105:        DWORD size = sizeof (value);
                    106: 
                    107:        strncpy (str, defaultValue, maxLen-1);
                    108: 
                    109:        ZeroMemory (value, sizeof value);
1.1.1.4   root      110:        if (RegOpenKeyEx (HKEY_CURRENT_USER, subKey,
1.1       root      111:                0, KEY_READ, &hkey) == ERROR_SUCCESS)
                    112:                if (RegQueryValueEx (hkey, name, 0,     0, (LPBYTE) &value,     &size) == ERROR_SUCCESS)
                    113:                        strncpy (str, value, maxLen-1);
                    114: 
                    115:        RegCloseKey (hkey);
                    116:        return str;
                    117: }
                    118: 
1.1.1.4   root      119: DWORD ReadRegistryBytes (char *path, char *name, char *value, int maxLen)
                    120: {
                    121:        HKEY hkey = 0;
                    122:        DWORD size = maxLen;
                    123:        BOOL success = FALSE;
                    124: 
                    125:        if (RegOpenKeyEx (HKEY_CURRENT_USER, path, 0, KEY_READ, &hkey) != ERROR_SUCCESS)
                    126:                return 0;
                    127: 
                    128:        success = (RegQueryValueEx (hkey, name, 0,      0, (LPBYTE) value,      &size) == ERROR_SUCCESS);
                    129:        RegCloseKey (hkey);
                    130: 
                    131:        return success ? size : 0;
                    132: }
                    133: 
                    134: void WriteRegistryInt (char *subKey, char *name, int value)
1.1       root      135: {
                    136:        HKEY hkey = 0;
                    137:        DWORD disp;
                    138: 
1.1.1.4   root      139:        if (RegCreateKeyEx (HKEY_CURRENT_USER, subKey,
1.1       root      140:                0, NULL, REG_OPTION_NON_VOLATILE, KEY_WRITE, NULL, &hkey, &disp) != ERROR_SUCCESS)
                    141:                return;
                    142: 
                    143:        RegSetValueEx (hkey, name, 0, REG_DWORD, (BYTE *) &value, sizeof value);
                    144:        RegCloseKey (hkey);
                    145: }
                    146: 
1.1.1.9   root      147: BOOL WriteLocalMachineRegistryDword (char *subKey, char *name, DWORD value)
                    148: {
                    149:        HKEY hkey = 0;
                    150:        DWORD disp;
1.1.1.10  root      151:        LONG status;
1.1.1.9   root      152: 
1.1.1.10  root      153:        if ((status = RegCreateKeyEx (HKEY_LOCAL_MACHINE, subKey,
                    154:                0, NULL, REG_OPTION_NON_VOLATILE, KEY_WRITE, NULL, &hkey, &disp)) != ERROR_SUCCESS)
                    155:        {
                    156:                SetLastError (status);
1.1.1.9   root      157:                return FALSE;
1.1.1.10  root      158:        }
                    159: 
                    160:        if ((status = RegSetValueEx (hkey, name, 0, REG_DWORD, (BYTE *) &value, sizeof value)) != ERROR_SUCCESS)
                    161:        {
                    162:                RegCloseKey (hkey);
                    163:                SetLastError (status);
                    164:                return FALSE;
                    165:        }
                    166: 
                    167:        RegCloseKey (hkey);
                    168:        return TRUE;
                    169: }
                    170: 
                    171: BOOL WriteLocalMachineRegistryMultiString (char *subKey, char *name, char *multiString, DWORD size)
                    172: {
                    173:        HKEY hkey = 0;
                    174:        DWORD disp;
                    175:        LONG status;
                    176: 
                    177:        if ((status = RegCreateKeyEx (HKEY_LOCAL_MACHINE, subKey,
                    178:                0, NULL, REG_OPTION_NON_VOLATILE, KEY_WRITE, NULL, &hkey, &disp)) != ERROR_SUCCESS)
                    179:        {
                    180:                SetLastError (status);
                    181:                return FALSE;
                    182:        }
1.1.1.9   root      183: 
1.1.1.10  root      184:        if ((status = RegSetValueEx (hkey, name, 0, REG_MULTI_SZ, (BYTE *) multiString, size)) != ERROR_SUCCESS)
1.1.1.9   root      185:        {
                    186:                RegCloseKey (hkey);
1.1.1.10  root      187:                SetLastError (status);
1.1.1.9   root      188:                return FALSE;
                    189:        }
                    190: 
                    191:        RegCloseKey (hkey);
                    192:        return TRUE;
                    193: }
                    194: 
1.1.1.16! root      195: BOOL WriteLocalMachineRegistryString (char *subKey, char *name, char *str, BOOL expandable)
1.1.1.12  root      196: {
                    197:        HKEY hkey = 0;
                    198:        DWORD disp;
                    199:        LONG status;
                    200: 
                    201:        if ((status = RegCreateKeyEx (HKEY_LOCAL_MACHINE, subKey,
                    202:                0, NULL, REG_OPTION_NON_VOLATILE, KEY_WRITE, NULL, &hkey, &disp)) != ERROR_SUCCESS)
                    203:        {
                    204:                SetLastError (status);
                    205:                return FALSE;
                    206:        }
                    207: 
1.1.1.16! root      208:        if ((status = RegSetValueEx (hkey, name, 0, expandable ? REG_EXPAND_SZ : REG_SZ, (BYTE *) str, strlen (str) + 1)) != ERROR_SUCCESS)
1.1.1.12  root      209:        {
                    210:                RegCloseKey (hkey);
                    211:                SetLastError (status);
                    212:                return FALSE;
                    213:        }
                    214: 
                    215:        RegCloseKey (hkey);
                    216:        return TRUE;
                    217: }
                    218: 
1.1.1.4   root      219: void WriteRegistryString (char *subKey, char *name, char *str)
1.1       root      220: {
                    221:        HKEY hkey = 0;
                    222:        DWORD disp;
                    223: 
1.1.1.4   root      224:        if (RegCreateKeyEx (HKEY_CURRENT_USER, subKey,
1.1       root      225:                0, NULL, REG_OPTION_NON_VOLATILE, KEY_WRITE, NULL, &hkey, &disp) != ERROR_SUCCESS)
                    226:                return;
                    227: 
                    228:        RegSetValueEx (hkey, name, 0, REG_SZ, (BYTE *) str, strlen (str) + 1);
                    229:        RegCloseKey (hkey);
                    230: }
1.1.1.4   root      231: 
                    232: BOOL WriteRegistryBytes (char *path, char *name, char *str, DWORD size)
                    233: {
                    234:        HKEY hkey = 0;
                    235:        DWORD disp;
                    236:        BOOL res;
                    237: 
                    238:        if (RegCreateKeyEx (HKEY_CURRENT_USER, path,
                    239:                0, NULL, REG_OPTION_NON_VOLATILE, KEY_WRITE, NULL, &hkey, &disp) != ERROR_SUCCESS)
                    240:                return FALSE;
                    241: 
                    242:        res = RegSetValueEx (hkey, name, 0, REG_BINARY, (BYTE *) str, size);
                    243:        RegCloseKey (hkey);
                    244:        return res == ERROR_SUCCESS;
                    245: }
                    246: 
1.1.1.16! root      247: BOOL DeleteLocalMachineRegistryKey (char *parentKey, char *subKeyToDelete)
        !           248: {
        !           249:        LONG status;
        !           250:        HKEY hkey = 0;
        !           251: 
        !           252:        if ((status = RegOpenKeyEx (HKEY_LOCAL_MACHINE, parentKey, 0, KEY_WRITE, &hkey)) != ERROR_SUCCESS)
        !           253:        {
        !           254:                SetLastError (status);
        !           255:                return FALSE;
        !           256:        }
        !           257: 
        !           258:        if ((status = RegDeleteKey (hkey, subKeyToDelete)) != ERROR_SUCCESS)
        !           259:        {
        !           260:                RegCloseKey (hkey);
        !           261:                SetLastError (status);
        !           262:                return FALSE;
        !           263:        }
        !           264: 
        !           265:        RegCloseKey (hkey);
        !           266:        return TRUE;
        !           267: }
        !           268: 
1.1.1.4   root      269: void DeleteRegistryValue (char *subKey, char *name)
                    270: {
                    271:        HKEY hkey = 0;
                    272: 
                    273:        if (RegOpenKeyEx (HKEY_CURRENT_USER, subKey, 0, KEY_WRITE, &hkey) != ERROR_SUCCESS)
                    274:                return;
                    275: 
                    276:        RegDeleteValue (hkey, name);
                    277:        RegCloseKey (hkey);
1.1.1.16! root      278: }
        !           279: 
        !           280: 
        !           281: void GetStartupRegKeyName (char *regk)
        !           282: {
        !           283:        // The string is split in order to prevent some antivirus packages from falsely reporting  
        !           284:        // TrueCrypt.exe to contain a possible Trojan horse because of this string (heuristic scan).
        !           285:        sprintf (regk, "%s%s", "Software\\Microsoft\\Windows\\Curren", "tVersion\\Run");
        !           286: }

unix.superglobalmegacorp.com

This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.