📄 stafutilcommon.cpp
字号:
/*****************************************************************************//* Software Testing Automation Framework (STAF) *//* (C) Copyright IBM Corp. 2001 *//* *//* This software is licensed under the Common Public License (CPL) V1.0. *//*****************************************************************************/#include "STAF.h"#include "STAFUtil.h"#include "STAFMutexSem.h"#include <stdarg.h>#include <stdlib.h>static STAFMutexSem sPIDSem;STAFProcessID_t STAFUtilGetPID(){ static STAFProcessID_t thePID = 0; if (thePID == 0) { STAFMutexSemLock lock(sPIDSem); if (thePID == 0) thePID = getpid(); } return thePID;}inline unsigned int internalSwapUInt(unsigned int theUInt){ unsigned int newInt = (theUInt & 0x000000ff) << 24; newInt |= (theUInt & 0x0000ff00) << 8; newInt |= (theUInt & 0x00ff0000) >> 8; newInt |= (theUInt & 0xff000000) >> 24; return newInt;}unsigned int STAFUtilSwapUInt(unsigned int theUInt){ return internalSwapUInt(theUInt);}unsigned int STAFUtilConvertNativeUIntToLE(unsigned int theUInt){ static unsigned short test = 0xdead; static unsigned char *lead = (unsigned char *)&test; // If big-endian platform swap, otherwise, just return return (*lead == 0xde) ? internalSwapUInt(theUInt) : theUInt;}unsigned int STAFUtilConvertLEUIntToNative(unsigned int theUInt){ static unsigned short test = 0xdead; static unsigned char *lead = (unsigned char *)&test; // If big-endian platform swap, otherwise, just return return (*lead == 0xde) ? internalSwapUInt(theUInt) : theUInt;}unsigned int STAFUtilFormatString(STAFStringConst_t formatString, STAFString_t *outputString, ...){ if (formatString == 0) return kSTAFInvalidParm; if (outputString == 0) return kSTAFInvalidParm; va_list theArgs; va_start(theArgs, outputString); return STAFUtilFormatString2(formatString, outputString, theArgs);}unsigned int STAFUtilFormatString2(STAFStringConst_t formatString, STAFString_t *outputString, va_list args){ static STAFString sPercent(kUTF8_PERCENT); static STAFString sUIntChar("d"); static STAFString sStringChar("s"); static STAFString sCLCStringChar("C"); if (formatString == 0) return kSTAFInvalidParm; if (outputString == 0) return kSTAFInvalidParm; STAFString format(formatString); STAFString output; unsigned int lastPos = 0; for (unsigned int currPos = format.find(sPercent); currPos != STAFString::kNPos; currPos = format.find(sPercent, lastPos)) { output += format.subString(lastPos, currPos - lastPos); STAFString formatChar = format.subString(currPos + 1, 1, STAFString::kChar); lastPos = currPos + 1 + formatChar.length(); if (formatChar == sUIntChar) output += STAFString(va_arg(args, unsigned int)); else if (formatChar == sStringChar) output += va_arg(args, STAFStringConst_t); else if (formatChar == sCLCStringChar) output += STAFHandle::wrapData(va_arg(args, STAFStringConst_t)); else if (formatChar == sPercent) output += sPercent; } output += format.subString(lastPos); va_end(args); *outputString = output.adoptImpl(); return kSTAFOk;}STAFRC_t STAFUtilStripPortFromEndpoint(STAFStringConst_t endpoint, STAFString_t *strippedEndpoint){ if (endpoint == 0) return kSTAFInvalidParm; if (strippedEndpoint == 0) return kSTAFInvalidParm; // Strip the port from the endpoint, if present. STAFString endpointStr = STAFString(endpoint); unsigned int portIndex = endpointStr.find(kUTF8_AT); if (portIndex != STAFString::kNPos) { // If the characters following the "@" are numeric, then assume // it's a valid port and strip the @ and the port number from // the endpoint. STAFString port = endpointStr.subString(portIndex + 1); if (port.isDigits()) { endpointStr = endpointStr.subString(0, portIndex); } } *strippedEndpoint = endpointStr.adoptImpl(); return kSTAFOk;}STAFRC_t STAFUtilValidateTrust(unsigned int actualTrustLevel, unsigned int requiredTrustLevel, STAFStringConst_t service, STAFStringConst_t request, STAFStringConst_t localMachine, STAFStringConst_t requestingEndpoint, STAFStringConst_t physicalInterfaceID, STAFStringConst_t requestingUser, STAFString_t *errorBuffer){ if (service == 0) return kSTAFInvalidParm; if (request == 0) return kSTAFInvalidParm; if (localMachine == 0) return kSTAFInvalidParm; if (requestingEndpoint == 0) return kSTAFInvalidParm; if (physicalInterfaceID == 0) return kSTAFInvalidParm; if (requestingUser == 0) return kSTAFInvalidParm; if (errorBuffer == 0) return kSTAFInvalidParm; if (actualTrustLevel < requiredTrustLevel) { // Strip the port from the machine's endpoint, if present. STAFString_t strippedEndpoint = 0; STAFUtilStripPortFromEndpoint(requestingEndpoint, &strippedEndpoint); *errorBuffer = STAFString( "Trust level " + STAFString(requiredTrustLevel) + " required for the " + service + " service's " + request + " request\nRequester has trust level " + STAFString(actualTrustLevel) + " on machine " + localMachine + "\nRequesting machine: " + STAFString(strippedEndpoint, STAFString::kShallow) + " (" + physicalInterfaceID + ")\nRequesting user : " + requestingUser).adoptImpl(); return kSTAFAccessDenied; } return kSTAFOk;}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -