EsifLogType EsifLogType_FromString(const char *name) { EsifLogType result = (EsifLogType)0; if (NULL == name) return result; else if (esif_ccb_strnicmp(name, "eventlog", 5)==0) result = ESIF_LOG_EVENTLOG; else if (esif_ccb_strnicmp(name, "debugger", 5)==0) result = ESIF_LOG_DEBUGGER; else if (esif_ccb_stricmp(name, "shell")==0) result = ESIF_LOG_SHELL; else if (esif_ccb_stricmp(name, "trace")==0) result = ESIF_LOG_TRACE; else if (esif_ccb_stricmp(name, "ui")==0) result = ESIF_LOG_UI; return result; }
// Read a Registry Key Value into a specified memory buffer static eEsifError DataVault_ReadRegistry ( DataVaultPtr self, esif_string keyname, void * *buffer, UInt32 *buf_size ) { eEsifError rc = ESIF_E_UNSPECIFIED; HKEY hKey = 0; DWORD dwFlags = RRF_RT_ANY; DWORD dwError = 0; DWORD dwSize = 0; char *regkey = 0; char *valuename = 0; u32 j; UNREFERENCED_PARAMETER(self); // Format: HKLM\Subkey\Path\ValueName for (j = 0; HKeyList[j].name; j++) { size_t namelen = esif_ccb_strlen(HKeyList[j].name, 30); if (esif_ccb_strnicmp(HKeyList[j].name, keyname, namelen) == 0 && keyname[namelen] == '\\') { hKey = HKeyList[j].hkey; keyname += esif_ccb_strlen(HKeyList[j].name, 30) + 1; break; } } if (!hKey) { return rc; } regkey = esif_ccb_strdup(keyname); if ((valuename = strrchr(regkey, '\\')) == 0) { esif_ccb_free(regkey); return rc; } *valuename++ = 0; // Get Data Size from Registry and copy into buffer if found if ((dwError = RegGetValueA(hKey, regkey, valuename, dwFlags, NULL, 0, &dwSize)) == 0) { void *reg_buf = esif_ccb_malloc(dwSize); dwError = RegGetValueA(hKey, regkey, valuename, dwFlags, NULL, reg_buf, &dwSize); if (dwError == 0) { *buffer = reg_buf; *buf_size = dwSize; rc = ESIF_OK; } else { esif_ccb_free(reg_buf); } } else { rc = ESIF_E_NOT_FOUND; } esif_ccb_free(regkey); return rc; }
int IString_Strnicmp ( IStringPtr self, ZString str2, u32 count ) { ESIF_ASSERT(self); if (self->buf_ptr && str2) { return esif_ccb_strnicmp((ZString)self->buf_ptr, str2, count); } return 0; }
// This extracts the Kernel version from the string returned by esif_cmd_info() static void extract_kernel_version(char *str, size_t buf_len) { char *prefix = "Kernel Version = "; size_t len = esif_ccb_strlen(prefix, buf_len); if (str != NULL && esif_ccb_strnicmp(str, "Kernel Version = ", len) == 0) { esif_ccb_strcpy(str, str+len, buf_len); len = esif_ccb_strlen(str, buf_len); if (len > 1 && str[len-1] == '\n') { str[len-1] = 0; } } }
// Replacement for strstr() that supports Case-Insensitive searches static const char*strfind ( const char *str, const char *strSearch, int IgnoreCase ) { if (IgnoreCase) { size_t len = esif_ccb_strlen(strSearch, 0x7FFFFFFF); while (*str != 0 && esif_ccb_strnicmp(str, strSearch, len) != 0) str++; return *str != 0 ? str : 0; } return strstr(str, strSearch); }
// Compare two IStrings, up to count characters int IString_CompareMax ( IStringPtr self, IStringPtr str2, u32 count, int IgnoreCase ) { if (self && str2 && self->buf_ptr && str2->buf_ptr) { if (IgnoreCase) { return esif_ccb_strnicmp((ZString)self->buf_ptr, (ZString)str2->buf_ptr, count); } else { return esif_ccb_strncmp((ZString)self->buf_ptr, (ZString)str2->buf_ptr, count); } } return 0; }
/* TRUE if item is an exact match or item is a member of "item1|item2|...|itemN" (case-insensitive) */ static Bool minterm_matches( esif_string list, esif_string item ) { size_t len = esif_ccb_strlen(item, MAX_MINTERM_LENGTH); while (list) { if ((esif_ccb_strnicmp(list, item, len) == 0) && (list[len] == 0 || list[len] == '|')) { return ESIF_TRUE; } if ((list = esif_ccb_strchr(list, '|')) != NULL) list++; } return ESIF_FALSE; }
void esif_ws_server_execute_rest_cmd( const char *dataPtr, const size_t dataSize ) { char *command_buf = NULL; if (atomic_read(&g_ws_quit)) return; command_buf = strchr(dataPtr, ':'); if (NULL != command_buf) { u32 msg_id = atoi(dataPtr); *command_buf = 0; command_buf++; // Ad-Hoc UI Shell commands begin with "0:", so verify ESIF shell is enabled and command is valid if (msg_id == 0 || g_ws_restricted) { char *response = NULL; if (msg_id == 0 && !g_shell_enabled) { response = "Shell Disabled"; } else { static char *whitelist[] = { "status", "participants", NULL }; static char *blacklist[] = { "shell", "web", "exit", "quit", NULL }; Bool blocked = ESIF_FALSE; int j = 0; if (g_ws_restricted) { for (j = 0; whitelist[j] != NULL; j++) { if (esif_ccb_strnicmp(command_buf, whitelist[j], esif_ccb_strlen(whitelist[j], MAX_PATH)) == 0) { break; } } if (whitelist[j] == NULL) { blocked = ESIF_TRUE; } } else { for (j = 0; blacklist[j] != NULL; j++) { if (esif_ccb_strnicmp(command_buf, blacklist[j], esif_ccb_strlen(blacklist[j], MAX_PATH)) == 0) { blocked = ESIF_TRUE; break; } } } if (blocked) { response = "Unsupported Command"; } } // Exit if shell or command unavailable if (response) { char buffer[MAX_PATH] = { 0 }; esif_ccb_sprintf(sizeof(buffer), buffer, "%d:%s", msg_id, response); esif_ccb_free(g_rest_out); g_rest_out = esif_ccb_strdup(buffer); goto exit; } } // Lock Shell so we can capture output before another thread executes another command #ifdef ESIF_ATTR_SHELL_LOCK esif_ccb_mutex_lock(&g_shellLock); #endif if (!atomic_read(&g_ws_quit)) { EsifString cmd_results = esif_shell_exec_command(command_buf, dataSize, ESIF_TRUE); if (NULL != cmd_results) { size_t out_len = esif_ccb_strlen(cmd_results, OUT_BUF_LEN) + 12; esif_ccb_free(g_rest_out); g_rest_out = (EsifString) esif_ccb_malloc(out_len); if (g_rest_out) { esif_ccb_sprintf(out_len, g_rest_out, "%u:%s", msg_id, cmd_results); } } } #ifdef ESIF_ATTR_SHELL_LOCK esif_ccb_mutex_unlock(&g_shellLock); #endif } exit: return; }
// Write a Registry Key Value from an EsifData value static eEsifError DataVault_WriteRegistry ( DataVaultPtr self, esif_string keyname, EsifDataPtr value ) { eEsifError rc = ESIF_E_UNSPECIFIED; HKEY hKey = 0; DWORD dwFlags = RRF_RT_ANY; DWORD dwError = 0; DWORD dwSize = 0; DWORD dwType = REG_NONE; char *regkey = 0; char *valuename = 0; u32 j; UNREFERENCED_PARAMETER(self); // Keyname Format: "HKLM\Subkey\Path\ValueName" for (j = 0; HKeyList[j].name; j++) { size_t namelen = esif_ccb_strlen(HKeyList[j].name, 30); if (esif_ccb_strnicmp(HKeyList[j].name, keyname, namelen) == 0 && keyname[namelen] == '\\') { hKey = HKeyList[j].hkey; keyname += esif_ccb_strlen(HKeyList[j].name, 30) + 1; break; } } if (!hKey) { return rc; } regkey = esif_ccb_strdup(keyname); if ((valuename = strrchr(regkey, '\\')) == 0) { esif_ccb_free(regkey); return rc; } *valuename++ = 0; // Get Data Type from Registry if value already exists, otherwise deduce it from ESIF Data Type if ((dwError = RegGetValueA(hKey, regkey, valuename, dwFlags, &dwType, 0, 0)) != 0) { switch (value->type) { case ESIF_DATA_INT32: case ESIF_DATA_UINT32: case ESIF_DATA_TEMPERATURE: dwType = REG_DWORD; dwSize = value->data_len; break; case ESIF_DATA_INT64: case ESIF_DATA_UINT64: dwType = REG_QWORD; dwSize = value->data_len; break; case ESIF_DATA_STRING: dwType = REG_SZ; dwSize = value->data_len; break; case ESIF_DATA_BINARY: case ESIF_DATA_BLOB: dwType = REG_BINARY; dwSize = value->data_len; break; default: rc = ESIF_E_NOT_SUPPORTED; break; } } // Write Registry Value if (dwType != REG_NONE) { dwError = RegSetKeyValueA(hKey, regkey, valuename, dwType, value->buf_ptr, value->data_len); if (dwError == 0) { rc = ESIF_OK; } } esif_ccb_free(regkey); return rc; }