return 2;\r
}\r
\r
- /* Probably not possible */\r
- if (! envsize) return 0;\r
+ /* Minimum usable environment would be A= NULL NULL. */\r
+ if (envsize < 4 * sizeof(TCHAR)) {\r
+ *env = 0;\r
+ return 3;\r
+ }\r
\r
/* Previously initialised? */\r
if (*env) HeapFree(GetProcessHeap(), 0, *env);\r
*env = (TCHAR *) HeapAlloc(GetProcessHeap(), 0, envsize);\r
if (! *env) {\r
log_event(EVENTLOG_ERROR_TYPE, NSSM_EVENT_OUT_OF_MEMORY, value, _T("get_environment()"), 0);\r
- return 3;\r
+ return 4;\r
}\r
\r
/* Actually get the strings. */\r
log_event(EVENTLOG_ERROR_TYPE, NSSM_EVENT_QUERYVALUE_FAILED, value, error_string(ret), 0);\r
HeapFree(GetProcessHeap(), 0, *env);\r
*env = 0;\r
- return 4;\r
+ return 5;\r
}\r
\r
/* Value retrieved by RegQueryValueEx() is SIZE not COUNT. */\r
- *envlen = (unsigned long) environment_length(env);\r
+ *envlen = (unsigned long) environment_length(*env);\r
\r
return 0;\r
}\r
return 0;\r
}\r
\r
+/* Copy a block. */\r
+int copy_double_null(TCHAR *dn, unsigned long dnlen, TCHAR **newdn) {\r
+ if (! newdn) return 1;\r
+\r
+ *newdn = 0;\r
+ if (! dn) return 0;\r
+\r
+ *newdn = (TCHAR *) HeapAlloc(GetProcessHeap(), 0, dnlen * sizeof(TCHAR));\r
+ if (! *newdn) {\r
+ log_event(EVENTLOG_ERROR_TYPE, NSSM_EVENT_OUT_OF_MEMORY, _T("dn"), _T("copy_double_null()"), 0);\r
+ return 2;\r
+ }\r
+\r
+ memmove(*newdn, dn, dnlen * sizeof(TCHAR));\r
+ return 0;\r
+}\r
+\r
+/*\r
+ Create a new block with all the strings of the first block plus a new string.\r
+ The new string may be specified as <key> <delimiter> <value> and the keylen\r
+ gives the offset into the string to compare against existing entries.\r
+ If the key is already present its value will be overwritten in place.\r
+ If the key is blank or empty the new block will still be allocated and have\r
+ non-zero length.\r
+*/\r
+int append_to_double_null(TCHAR *dn, unsigned long dnlen, TCHAR **newdn, unsigned long *newlen, TCHAR *append, size_t keylen, bool case_sensitive) {\r
+ if (! append || ! append[0]) return copy_double_null(dn, dnlen, newdn);\r
+ size_t appendlen = _tcslen(append);\r
+ int (*fn)(const TCHAR *, const TCHAR *, size_t) = (case_sensitive) ? _tcsncmp : _tcsnicmp;\r
+\r
+ /* Identify the key, if any, or treat the whole string as the key. */\r
+ TCHAR *key = 0;\r
+ if (! keylen || keylen > appendlen) keylen = appendlen;\r
+ key = (TCHAR *) HeapAlloc(GetProcessHeap(), 0, (keylen + 1) * sizeof(TCHAR));\r
+ if (! key) {\r
+ log_event(EVENTLOG_ERROR_TYPE, NSSM_EVENT_OUT_OF_MEMORY, _T("key"), _T("append_to_double_null()"), 0);\r
+ return 1;\r
+ }\r
+ memmove(key, append, keylen * sizeof(TCHAR));\r
+ key[keylen] = _T('\0');\r
+\r
+ /* Find the length of the block not including any existing key. */\r
+ size_t len = 0;\r
+ TCHAR *s;\r
+ for (s = dn; *s; s++) {\r
+ if (fn(s, key, keylen)) len += _tcslen(s) + 1;\r
+ for ( ; *s; s++);\r
+ }\r
+\r
+ /* Account for new entry. */\r
+ len += _tcslen(append) + 1;\r
+\r
+ /* Account for trailing NULL. */\r
+ len++;\r
+\r
+ /* Allocate a new block. */\r
+ *newdn = (TCHAR *) HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, len * sizeof(TCHAR));\r
+ if (! *newdn) {\r
+ log_event(EVENTLOG_ERROR_TYPE, NSSM_EVENT_OUT_OF_MEMORY, _T("newdn"), _T("append_to_double_null()"), 0);\r
+ HeapFree(GetProcessHeap(), 0, key);\r
+ return 2;\r
+ }\r
+\r
+ /* Copy existing entries.*/\r
+ *newlen = (unsigned long) len;\r
+ TCHAR *t = *newdn;\r
+ TCHAR *u;\r
+ bool replaced = false;\r
+ for (s = dn; *s; s++) {\r
+ if (fn(s, key, keylen)) u = s;\r
+ else {\r
+ u = append;\r
+ replaced = true;\r
+ }\r
+ len = _tcslen(u) + 1;\r
+ memmove(t, u, len * sizeof(TCHAR));\r
+ t += len;\r
+ for ( ; *s; s++);\r
+ }\r
+\r
+ /* Add the entry if it wasn't already replaced. The buffer was zeroed. */\r
+ if (! replaced) memmove(t, append, _tcslen(append) * sizeof(TCHAR));\r
+\r
+ HeapFree(GetProcessHeap(), 0, key);\r
+ return 0;\r
+}\r
+\r
+/*\r
+ Create a new block with all the string of the first block minus the given\r
+ string.\r
+ The keylen parameter gives the offset into the string to compare against\r
+ existing entries. If a substring of existing value matches the string to\r
+ the given length it will be removed.\r
+ If the last entry is removed the new block will still be allocated and\r
+ have non-zero length.\r
+*/\r
+int remove_from_double_null(TCHAR *dn, unsigned long dnlen, TCHAR **newdn, unsigned long *newlen, TCHAR *remove, size_t keylen, bool case_sensitive) {\r
+ if (! remove || !remove[0]) return copy_double_null(dn, dnlen, newdn);\r
+ size_t removelen = _tcslen(remove);\r
+ int (*fn)(const TCHAR *, const TCHAR *, size_t) = (case_sensitive) ? _tcsncmp : _tcsnicmp;\r
+\r
+ /* Identify the key, if any, or treat the whole string as the key. */\r
+ TCHAR *key = 0;\r
+ if (! keylen || keylen > removelen) keylen = removelen;\r
+ key = (TCHAR *) HeapAlloc(GetProcessHeap(), 0, (keylen + 1) * sizeof(TCHAR));\r
+ if (! key) {\r
+ log_event(EVENTLOG_ERROR_TYPE, NSSM_EVENT_OUT_OF_MEMORY, _T("key"), _T("remove_from_double_null()"), 0);\r
+ return 1;\r
+ }\r
+ memmove(key, remove, keylen * sizeof(TCHAR));\r
+ key[keylen] = _T('\0');\r
+\r
+ /* Find the length of the block not including any existing key. */\r
+ size_t len = 0;\r
+ TCHAR *s;\r
+ for (s = dn; *s; s++) {\r
+ if (fn(s, key, keylen)) len += _tcslen(s) + 1;\r
+ for ( ; *s; s++);\r
+ }\r
+\r
+ /* Account for trailing NULL. */\r
+ if (++len < 2) len = 2;\r
+\r
+ /* Allocate a new block. */\r
+ *newdn = (TCHAR *) HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, len * sizeof(TCHAR));\r
+ if (! *newdn) {\r
+ log_event(EVENTLOG_ERROR_TYPE, NSSM_EVENT_OUT_OF_MEMORY, _T("newdn"), _T("remove_from_double_null()"), 0);\r
+ HeapFree(GetProcessHeap(), 0, key);\r
+ return 2;\r
+ }\r
+\r
+ /* Copy existing entries.*/\r
+ *newlen = (unsigned long) len;\r
+ TCHAR *t = *newdn;\r
+ for (s = dn; *s; s++) {\r
+ if (fn(s, key, keylen)) {\r
+ len = _tcslen(s) + 1;\r
+ memmove(t, s, len * sizeof(TCHAR));\r
+ t += len;\r
+ }\r
+ for ( ; *s; s++);\r
+ }\r
+\r
+ HeapFree(GetProcessHeap(), 0, key);\r
+ return 0;\r
+}\r
+\r
void override_milliseconds(TCHAR *service_name, HKEY key, TCHAR *value, unsigned long *buffer, unsigned long default_value, unsigned long event) {\r
unsigned long type = REG_DWORD;\r
unsigned long buflen = sizeof(unsigned long);\r