Bool DynArray_Trim(DynArray *a) // IN/OUT { ASSERT(a); return DynBuf_Trim(&a->buf); }
void * Escape_Sh(void const *bufIn, // IN size_t sizeIn, // IN size_t *sizeOut) // OUT { static const char be[] = { '\'', }; static const char escSeq[] = { '\'', '"', '\'', '"', }; char const *buf; DynBuf b; size_t startUnescaped; size_t index; buf = (char const *)bufIn; ASSERT(buf); DynBuf_Init(&b); if (DynBuf_Append(&b, be, sizeof(be)) == FALSE) { goto nem; } startUnescaped = 0; for (index = 0; index < sizeIn; index++) { if (buf[index] == '\'') { /* We must escape that byte --hpreg */ if ( DynBuf_Append(&b, &buf[startUnescaped], index - startUnescaped) == FALSE || DynBuf_Append(&b, escSeq, sizeof(escSeq)) == FALSE) { goto nem; } startUnescaped = index; } } if ( /* Last unescaped chunk (if any) --hpreg */ DynBuf_Append(&b, &buf[startUnescaped], index - startUnescaped) == FALSE || DynBuf_Append(&b, be, sizeof(be)) == FALSE /* NUL terminator --hpreg */ || DynBuf_Append(&b, "", 1) == FALSE || DynBuf_Trim(&b) == FALSE) { goto nem; } if (sizeOut) { *sizeOut = DynBuf_GetSize(&b) - 1; } return DynBuf_Get(&b); nem: DynBuf_Destroy(&b); return NULL; }
static char * ExtractCommandLineFromAddressSpaceFile(psinfo_t *procInfo) //IN: psinfo struct { int argc; int i; char tempPath[MAXPATHLEN]; char *buf; FileIODescriptor asFd; FileIOResult res; DynBuf cmdLine; DynBufArray args; pid_t pid; FileIO_Invalidate(&asFd); pid = procInfo->pr_pid; if (Str_Snprintf(tempPath, sizeof tempPath, "/proc/%"FMT64"d/as", (int64_t)pid) == -1) { /* This should not happen. MAXPATHLEN should be large enough. */ ASSERT(FALSE); } res = FileIO_Open(&asFd, tempPath, FILEIO_OPEN_ACCESS_READ, FILEIO_OPEN); if (res != FILEIO_SUCCESS) { Warning("Could not open address space file for pid %"FMT64"d, %s\n", (int64_t)pid, FileIO_MsgError(res)); return NULL; } buf = NULL; if (ReadArgsFromAddressSpaceFile(asFd, procInfo, &args)) { /* Concatenate the strings in args into cmdLine. */ DynBuf_Init(&cmdLine); argc = DynBufArray_Count(&args); for (i = 0; i < argc; i++) { buf = DynBuf_Get(DynBufArray_AddressOf(&args, i)); DynBuf_Append(&cmdLine, buf, strlen(buf)); if (i + 1 < argc) { DynBuf_Append(&cmdLine, " ", 1); } DynBuf_Destroy(DynBufArray_AddressOf(&args, i)); } DynBuf_AppendString(&cmdLine,""); DynBufArray_Destroy(&args); DynBuf_Trim(&cmdLine); buf = DynBuf_Detach(&cmdLine); DynBuf_Destroy(&cmdLine); } FileIO_Close(&asFd); return buf; }
static Bool CodeSetDynBufFinalize(Bool ok, // IN: Earlier steps succeeded DynBuf *db, // IN: Buffer with converted string char **bufOut, // OUT: Converted string size_t *sizeOut) // OUT: Length of string in bytes { /* * NUL can be as long as 4 bytes if UTF-32, make no assumptions. */ if (!ok || !DynBuf_Append(db, "\0\0\0\0", 4) || !DynBuf_Trim(db)) { DynBuf_Destroy(db); return FALSE; } *bufOut = DynBuf_Get(db); if (sizeOut) { *sizeOut = DynBuf_GetSize(db) - 4; } return TRUE; }
char * Hostinfo_GetCpuidStr(void) { static const uint32 basic_args[] = {0x0, 0x1, 0xa}; static const uint32 extended_args[] = {0x80000000, 0x80000001, 0x80000008}; DynBuf buf; char *result; DynBuf_Init(&buf); HostInfoGetCpuidStrSection(basic_args, ARRAYSIZE(basic_args), &buf); HostInfoGetCpuidStrSection(extended_args, ARRAYSIZE(extended_args), &buf); // Trim buffer and set NULL character to replace last '-'. DynBuf_Trim(&buf); result = (char*)DynBuf_Get(&buf); ASSERT(result && result[0]); // We should at least get result from eax = 0x0. result[DynBuf_GetSize(&buf) - 1] = '\0'; return DynBuf_Detach(&buf); }
void * Escape_DoString(const char *escStr, // IN int const *bytesToEsc, // IN void const *bufIn, // IN size_t sizeIn, // IN size_t *sizeOut) // OUT { char const *buf; DynBuf b; size_t startUnescaped; size_t index; size_t escStrLen; ASSERT(escStr); escStrLen = strlen(escStr); ASSERT(escStrLen > 0); ASSERT(bytesToEsc); /* Unsigned does matter --hpreg */ ASSERT(bytesToEsc[(unsigned char)escStr[0]]); buf = (char const *)bufIn; ASSERT(buf); DynBuf_Init(&b); startUnescaped = 0; for (index = 0; index < sizeIn; index++) { /* Unsigned does matter --hpreg */ unsigned char ubyte; char escSeq[2]; ubyte = buf[index]; if (bytesToEsc[ubyte]) { /* We must escape that byte --hpreg */ escSeq[0] = Dec2Hex[ubyte >> 4]; escSeq[1] = Dec2Hex[ubyte & 0xF]; if ( DynBuf_Append(&b, &buf[startUnescaped], index - startUnescaped) == FALSE || DynBuf_Append(&b, escStr, escStrLen) == FALSE || DynBuf_Append(&b, escSeq, sizeof escSeq) == FALSE) { goto nem; } startUnescaped = index + 1; } } if ( /* Last unescaped chunk (if any) --hpreg */ DynBuf_Append(&b, &buf[startUnescaped], index - startUnescaped) == FALSE /* NUL terminator --hpreg */ || DynBuf_Append(&b, "", 1) == FALSE || DynBuf_Trim(&b) == FALSE) { goto nem; } if (sizeOut) { *sizeOut = DynBuf_GetSize(&b) - 1; } return DynBuf_Get(&b); nem: DynBuf_Destroy(&b); return NULL; }
void * Escape_AnsiToUnix(void const *bufIn, // IN size_t sizeIn, // IN size_t *sizeOut) // OUT { char const *buf; DynBuf b; unsigned int state; size_t startUnescaped; size_t index; buf = (char const *)bufIn; ASSERT(buf); DynBuf_Init(&b); startUnescaped = 0; state = 0; /* * Identify all chunks in buf (\r\n being the chunk separator), and copy * them into b --hpreg */ for (index = 0; index < sizeIn; index++) { char byte; byte = buf[index]; switch (state) { case 1: /* Found \r<byte> --hpreg */ state = 0; if (byte == '\n') { if (DynBuf_Append(&b, &buf[startUnescaped], index - 1 - startUnescaped) == FALSE) { goto nem; } startUnescaped = index; break; } /* Fall through --hpreg */ case 0: /* Found <byte> --hpreg */ if (byte == '\r') { state = 1; } break; default: NOT_IMPLEMENTED(); break; } } if ( /* Last unescaped chunk (if any) --hpreg */ DynBuf_Append(&b, &buf[startUnescaped], index - startUnescaped) == FALSE /* NUL terminator --hpreg */ || DynBuf_Append(&b, "", 1) == FALSE || DynBuf_Trim(&b) == FALSE) { goto nem; } if (sizeOut) { *sizeOut = DynBuf_GetSize(&b) - 1; } return DynBuf_Get(&b); nem: DynBuf_Destroy(&b); return NULL; }
void * Escape_Undo(char escByte, // IN void const *bufIn, // IN size_t sizeIn, // IN size_t *sizeOut) // OUT { char const *buf; DynBuf b; unsigned int state; size_t startUnescaped; size_t index; int h = 0; /* Compiler warning --hpreg */ int l; buf = (char const *)bufIn; ASSERT(buf); DynBuf_Init(&b); startUnescaped = 0; state = 0; for (index = 0; index < sizeIn; index++) { /* Unsigned does matter --hpreg */ unsigned char ubyte; ubyte = buf[index]; switch (state) { case 0: /* Found <byte> --hpreg */ if (ubyte == escByte) { state = 1; } break; case 1: /* Found <escByte><byte> --hpreg */ h = Hex2Dec[ubyte]; state = h >= 0 ? 2 : 0; break; case 2: /* Found <escByte><hexa digit><byte> --hpreg */ l = Hex2Dec[ubyte]; if (l >= 0) { char escaped; escaped = h << 4 | l; if ( DynBuf_Append(&b, &buf[startUnescaped], index - 2 - startUnescaped) == FALSE || DynBuf_Append(&b, &escaped, 1) == FALSE) { goto nem; } startUnescaped = index + 1; } state = 0; break; default: NOT_IMPLEMENTED(); break; } } if ( /* Last unescaped chunk (if any) --hpreg */ DynBuf_Append(&b, &buf[startUnescaped], index - startUnescaped) == FALSE /* NUL terminator --hpreg */ || DynBuf_Append(&b, "", 1) == FALSE || DynBuf_Trim(&b) == FALSE) { goto nem; } if (sizeOut) { *sizeOut = DynBuf_GetSize(&b) - 1; } return DynBuf_Get(&b); nem: DynBuf_Destroy(&b); return NULL; }
static char * ExtractCommandLineFromAddressSpaceFile(psinfo_t *procInfo, //IN: psinfo struct char **procCmdName) //OUT: command name { int argc; int i; char tempPath[MAXPATHLEN]; char *buf; FileIODescriptor asFd; FileIOResult res; DynBuf cmdLine; DynBufArray args; pid_t pid; char *cmdNameBegin; if (NULL != procCmdName) { *procCmdName = NULL; } FileIO_Invalidate(&asFd); pid = procInfo->pr_pid; if (Str_Snprintf(tempPath, sizeof tempPath, "/proc/%"FMT64"d/as", (int64_t)pid) == -1) { /* This should not happen. MAXPATHLEN should be large enough. */ ASSERT(FALSE); } res = FileIO_Open(&asFd, tempPath, FILEIO_OPEN_ACCESS_READ, FILEIO_OPEN); if (res != FILEIO_SUCCESS) { Warning("Could not open address space file for pid %"FMT64"d, %s\n", (int64_t)pid, FileIO_MsgError(res)); return NULL; } buf = NULL; if (ReadArgsFromAddressSpaceFile(asFd, procInfo, &args)) { /* Concatenate the strings in args into cmdLine. */ DynBuf_Init(&cmdLine); argc = DynBufArray_Count(&args); for (i = 0; i < argc; i++) { buf = DynBuf_Get(DynBufArray_AddressOf(&args, i)); DynBuf_Append(&cmdLine, buf, strlen(buf)); if (i + 1 < argc) { DynBuf_Append(&cmdLine, " ", 1); } if (NULL != procCmdName && 0 == i) { /* * Store the command name of the process. * Find the last path separator, to get the cmd name. * If no separator is found, then use the whole name. */ cmdNameBegin = strrchr(buf, '/'); if (NULL == cmdNameBegin) { cmdNameBegin = buf; } else { /* * Skip over the last separator. */ cmdNameBegin++; } *procCmdName = Unicode_Alloc(cmdNameBegin, STRING_ENCODING_DEFAULT); } DynBuf_Destroy(DynBufArray_AddressOf(&args, i)); } DynBuf_AppendString(&cmdLine,""); DynBufArray_Destroy(&args); DynBuf_Trim(&cmdLine); buf = DynBuf_Detach(&cmdLine); DynBuf_Destroy(&cmdLine); } FileIO_Close(&asFd); return buf; }