// remove redir args from cmdline, does this to the original buffer bool removeRedirArgs(LPTSTR lpCmdLine) { bool success = false; const int maxArgChars = 1024; TCHAR arg[maxArgChars]; ArgType argType; RedirArg redirArg; LPTSTR p; // pointer to position in lpCmdLine before previous getCmdLineArg call, ie. start of arg if (lpCmdLine == NULL) return false; p = lpCmdLine; while ((argType = getCmdLineArg(&lpCmdLine, arg, maxArgChars, &redirArg, NULL)) > 0) { if (argType == TOK_REDIR) { if (redirArg.redirType == RT_FILE) { TCHAR filename[1024]; if (getCmdLineArg(&lpCmdLine, filename, sizeof(filename), NULL, NULL) != TOK_STRING) goto cleanup; } // remove redir arg if (*lpCmdLine == 0) *p = 0; else memmove(p, lpCmdLine, ts_strsizez(lpCmdLine)); lpCmdLine = p; } p = lpCmdLine; } // success success = true; cleanup: return success; }
// if childData is NULL, the size will be returned in childDataLen bool ChildData::encode(void* childData, int* childDataSize) { int currentDirectorySize = (currentDirectory == NULL) ? sizeof(WCHAR) : ts_strsizez(currentDirectory); int redirSize = 0; for (int i=0; i<numRedirArgs; i++) { redirSize += sizeof(DWORD) + sizeof(DWORD) + sizeof(DWORD); if (redirArgs[i].redirType == RT_PIPE_UNSPEC || redirArgs[i].redirType == RT_PIPE_STDIN || redirArgs[i].redirType == RT_PIPE_STDOUT || redirArgs[i].redirType == RT_PIPE_STDERR || redirArgs[i].redirType == RT_FILE) { if (redirArgs[i].filename != NULL) redirSize += ts_strsizez(redirArgs[i].filename); } } DWORD envSize = 0; if (environment != NULL) { for (int i=0; i<environmentLength; i++) envSize += (_tcslen(environment[i].name) + 1 + _tcslen(environment[i].value) + 1) * sizeof(WCHAR); envSize += sizeof(WCHAR); // list termination } int totalSize = 0; totalSize += sizeof(DWORD); // length totalSize += sizeof(DWORD); // reserved totalSize += currentDirectorySize; // current directory totalSize += sizeof(DWORD); // number of redirs totalSize += redirSize; // redirs totalSize += sizeof(DWORD); // environment length totalSize += envSize; // environment variable list if (childDataSize != NULL) *childDataSize = totalSize; if (childData != NULL) { char* p = (char*)childData; // total size memcpy(p, &totalSize, sizeof(DWORD)); //*(DWORD*)p = totalSize; p += sizeof(DWORD); // reserved { DWORD reserved = 0; memcpy(p, &reserved, sizeof(DWORD)); //*(DWORD*)p = 0; p += sizeof(DWORD); } // current directory if (currentDirectory == NULL) { WCHAR zero = '\0'; *p = '\0'; memcpy(p, &zero, sizeof(WCHAR)); //*p = '\0'; p += sizeof(WCHAR); } else { ts_strcpy((WCHAR*)p, currentDirectory); p += (ts_strlen((WCHAR*)p) + 1) * 2; } // num redirs memcpy(p, &numRedirArgs, sizeof(DWORD)); //*(DWORD*)p = numRedirArgs; p += sizeof(DWORD); // redirs for (int i=0; i<numRedirArgs; i++) { // determine if filename is present bool filenamePresent = false; if (redirArgs[i].redirType == RT_PIPE_UNSPEC || redirArgs[i].redirType == RT_PIPE_STDIN || redirArgs[i].redirType == RT_PIPE_STDOUT || redirArgs[i].redirType == RT_PIPE_STDERR || redirArgs[i].redirType == RT_FILE) { if (redirArgs[i].filename != NULL) filenamePresent = true; } // flags DWORD flags = 0; if (redirArgs[i].redirType == RT_FILE) flags |= 0x00000001; else if (redirArgs[i].redirType == RT_HANDLE) flags |= 0x00000002; if (filenamePresent) flags |= 0x00000004; if (redirArgs[i].append) flags |= 0x00000008; if (redirArgs[i].openForRead) flags |= 0x00000010; if (redirArgs[i].openForWrite) flags |= 0x00000020; memcpy(p, &flags, sizeof(DWORD)); //*(DWORD*)p = flags; p += sizeof(DWORD); // fd memcpy(p, &redirArgs[i].fd, sizeof(DWORD)); //*(DWORD*)p = redirArgs[i].fd; p += sizeof(DWORD); // fd2 memcpy(p, &redirArgs[i].fd2, sizeof(DWORD)); //*(DWORD*)p = redirArgs[i].fd2; p += sizeof(DWORD); // pipe/file name if (filenamePresent) { ts_strcpy((WCHAR*)p, redirArgs[i].filename); p += (ts_strlen((WCHAR*)p) + 1) * 2; } } // environment size memcpy(p, &envSize, sizeof(DWORD)); //*(DWORD*)p = ENVIRONMENTLENGTH * 2; p += sizeof(DWORD); // environment if (envSize != 0) { for (int i=0; i<environmentLength; i++) { ts_strcpy((WCHAR*)p, environment[i].name); p += ts_strlen((WCHAR*)p) * sizeof(WCHAR); ts_strcpy((WCHAR*)p, L"="); p += sizeof(WCHAR); ts_strcpy((WCHAR*)p, environment[i].value); p += (ts_strlen((WCHAR*)p) + 1) * sizeof(WCHAR); } // terminate the environment list WCHAR zero = '\0'; memcpy(p, &zero, sizeof(WCHAR)); //*p = '\0'; p += sizeof(WCHAR); } } return true; }