bool ReadUID(int Index) { boost::mutex::scoped_lock l(mutex); if(! m_usb.is_open() ) { return false; } if(m_info.board_type==L"SF600") { ReadOnBoardFlash(false,Index); m_info.dwUID=(DWORD)m_vUID[0]<<16 | (DWORD)m_vUID[1]<<8 | m_vUID[2]; m_dwUID=m_info.dwUID; m_bManuID=m_vUID[3]; return true; } CNTRPIPE_RQ rq ; vector<unsigned char> vBuffer(3) ; // read rq.Function = URB_FUNCTION_VENDOR_OTHER ; rq.Direction = VENDOR_DIRECTION_IN ; rq.Request = 0x7; rq.Value = 0 ; rq.Index = 0xEF00 ; rq.Length = 3; if(! m_usb.InCtrlRequest(rq, vBuffer,Index)) { return false; } m_dwUID=(DWORD)vBuffer[0]<<16 | (DWORD)vBuffer[1]<<8 | vBuffer[2]; return true; }
bool ReadManufacturerID(int Index) { if(! m_usb.is_open() ) { return false; } if(m_info.board_type==L"SF600") { ReadOnBoardFlash(false,Index); m_bManuID=m_vUID[3]; return true; } CNTRPIPE_RQ rq ; vector<unsigned char> vBuffer(1) ; m_bManuID=0xFF; // read rq.Function = URB_FUNCTION_VENDOR_OTHER ; rq.Direction = VENDOR_DIRECTION_IN ; rq.Request = 0x7; rq.Value = 0 ; rq.Index = 0xEF03; rq.Length = 1; if(! m_usb.InCtrlRequest(rq, vBuffer,Index)) { return false; } m_bManuID=vBuffer[0]; return true; }
//写严重错误信息 void CLogger::TraceFatal(const char *lpcszFormat, ...) { //判断当前的写日志级别 if (EnumLogLevel::LogLevel_Fatal > m_nLogLevel) return; string strResult; if (NULL != lpcszFormat) { va_list marker = NULL; va_start(marker, lpcszFormat); //初始化变量参数 size_t nLength = _vscprintf(lpcszFormat, marker) + 1; //获取格式化字符串长度 std::vector<char> vBuffer(nLength, '\0'); //创建用于存储格式化字符串的字符数组 int nWritten = _vsnprintf_s(&vBuffer[0], vBuffer.size(), nLength, lpcszFormat, marker); if (nWritten > 0) { strResult = &vBuffer[0]; } va_end(marker); //重置变量参数 } if (strResult.empty()) { return; } string strFileLine = FormatString("%s:%d\t", path_file(__FILE__,'\\'), __LINE__); string strLog = strFatalPrefix; strLog.append(GetTime()).append(strFileLine).append(strResult); //写日志文件 Trace(strLog); }
void DifShellBase::buffer( const Eref& e, double kf, double kb, double bFree, double bBound ) { vBuffer( e, kf, kb, bFree, bBound ); }
bool encryptMessage( const BYTE* apMessage, DWORD adwMessageLength, BYTE* apEncrypted, DWORD& adwEncryptedLength) { if(adwMessageLength > getStreamSizes().cbMaximumMessage) { std::cout << "Message is too big\n"; return false; } size_t szTotalSize = getStreamSizes().cbHeader + getStreamSizes().cbMaximumMessage + getStreamSizes().cbTrailer; std::vector<BYTE> vBuffer(szTotalSize, 0); SecBufferDesc buffDesc = {0}; SecBuffer secBuff[3] = {0}; buffDesc.cBuffers = 3; buffDesc.pBuffers = secBuff; secBuff[0].BufferType = SECBUFFER_STREAM_HEADER; secBuff[0].cbBuffer = getStreamSizes().cbHeader; secBuff[0].pvBuffer = &vBuffer[0]; secBuff[1].BufferType = SECBUFFER_DATA; secBuff[1].cbBuffer = adwMessageLength; secBuff[1].pvBuffer = &vBuffer[0] + getStreamSizes().cbHeader; memcpy(secBuff[1].pvBuffer, apMessage, adwMessageLength); secBuff[2].BufferType = SECBUFFER_STREAM_TRAILER; secBuff[2].cbBuffer = getStreamSizes().cbTrailer; secBuff[2].pvBuffer = (BYTE*)secBuff[1].pvBuffer + adwMessageLength; SECURITY_STATUS ssResult = ::EncryptMessage( &g_hContext, 0, &buffDesc, 0); if(ssResult != SEC_E_OK) { std::cout << std::hex << "Error in ::EncryptMessage = " << ssResult << '\n'; return false; } memcpy( apEncrypted, &vBuffer[0], secBuff[0].cbBuffer + secBuff[1].cbBuffer + secBuff[2].cbBuffer); adwEncryptedLength = secBuff[1].cbBuffer; return true; }
bool rs::jsapi::Object::CallFunction(JSContext* cx, unsigned argc, JS::Value* vp) { JSAutoRequest ar(cx); char nameBuffer[256]; const char* name = nameBuffer; auto args = JS::CallArgsFromVp(argc, vp); auto func = JS_ValueToFunction(cx, args.calleev()); if (func != nullptr) { auto funcName = JS_GetFunctionId(func); if (funcName != nullptr) { auto nameLength = JS_EncodeStringToBuffer(cx, funcName, nameBuffer, sizeof(nameBuffer)); if ((nameLength + 1) < sizeof(nameBuffer)) { nameBuffer[nameLength] = '\0'; } else { std::vector<char> vBuffer(nameLength + 1); JS_EncodeStringToBuffer(cx, funcName, &vBuffer[0], nameLength); vBuffer[nameLength] = '\0'; name = &vBuffer[0]; } } } if (name == nullptr) { // TODO: test this case JS_ReportError(cx, "Unable to find function in libjsapi object"); return false; } else { auto that = args.thisv(); auto state = that.isObjectOrNull() ? Object::GetState(cx, JS::RootedObject(cx, that.toObjectOrNull())) : nullptr; if (state == nullptr) { // TODO: test this case JS_ReportError(cx, "Unable to find function callback in libjsapi object"); return false; } else { try { static thread_local std::vector<Value> vArgs; VectorUtils::ScopedVectorCleaner<Value> clean(vArgs); for (int i = 0; i < argc; ++i) { vArgs.emplace_back(cx, args.get(i)); } Value result(cx); state->functions[name](vArgs, result); args.rval().set(result); return true; } catch (const std::exception& ex) { JS_ReportError(cx, ex.what()); return false; } } } }
S32 NetworkStream::Write(const Vector<Byte>& _vBuffer, S32 _iOffset, S32 _iSize) { if (_iOffset < 0) { throw std::out_of_range("_iOffset is less than 0"); } else if (_iOffset > (S32)_vBuffer.size()) { throw std::out_of_range("_iOffset is greater than the length of _vBuffer_"); } else if (_iSize < 0) { throw std::out_of_range("_iSize is less than 0"); } else if (_iSize > (S32)_vBuffer.size() - _iOffset) { throw std::out_of_range("_iSize is greater than the length of _vBuffer_ minus the value of the _iOffset parameter"); } Vector<Byte> vBuffer(_vBuffer.data() + _iOffset, _vBuffer.data() + _iOffset + _iSize); return m_pSocket->Send(vBuffer); }
S32 NetworkStream::Read(Vector<Byte>& _vBuffer_, S32 _iOffset, S32 _iSize) { if (_iOffset < 0) { throw std::out_of_range("_iOffset is less than 0"); } else if (_iOffset > (S32)_vBuffer_.size()) { throw std::out_of_range("_iOffset is greater than the length of _vBuffer_"); } else if (_iSize < 0) { throw std::out_of_range("_iSize is less than 0"); } else if (_iSize > (S32)_vBuffer_.size() - _iOffset) { throw std::out_of_range("_iSize is greater than the length of _vBuffer_ minus the value of the _iOffset parameter"); } Vector<Byte> vBuffer(_vBuffer_.data() + _iOffset, _vBuffer_.data() + _iOffset + _iSize); S32 iResult = m_pSocket->Receive(vBuffer); _vBuffer_.swap(vBuffer); return iResult; }
string CLogger::FormatString(const char *lpcszFormat, ...) { string strResult; if (NULL != lpcszFormat) { va_list marker = NULL; va_start(marker, lpcszFormat); //初始化变量参数 size_t nLength = _vscprintf(lpcszFormat, marker) + 1; //获取格式化字符串长度 std::vector<char> vBuffer(nLength, '\0'); //创建用于存储格式化字符串的字符数组 int nWritten = _vsnprintf_s(&vBuffer[0], vBuffer.size(), nLength, lpcszFormat, marker); if (nWritten > 0) { strResult = &vBuffer[0]; } va_end(marker); //重置变量参数 } return strResult; }
bool WriteSF600UID(int Index) { CNTRPIPE_RQ rq ; vector<unsigned char> vBuffer(16) ; // first control packet rq.Function = URB_FUNCTION_VENDOR_ENDPOINT ; rq.Direction = VENDOR_DIRECTION_OUT ; rq.Request = WRITE_EEPROM ; rq.Value = 0; rq.Index = RFU ; rq.Length = vBuffer.size(); vBuffer[0]=(BYTE)(m_dwUID>>16) ; vBuffer[1]=(BYTE)(m_dwUID>>8) ; vBuffer[2]=(BYTE)(m_dwUID) ; vBuffer[3]=m_bManuID ; if(! m_usb.OutCtrlRequest(rq, vBuffer,Index)) return false ; return true ; }
bool SF600HoldPinControlBySW(bool boEnable, int Index) { if(! m_usb.is_open() ) { return false; } // send request CNTRPIPE_RQ rq ; vector<unsigned char> vBuffer(0) ; rq.Function = URB_FUNCTION_VENDOR_ENDPOINT ; rq.Direction = VENDOR_DIRECTION_OUT ; rq.Request = SET_HOLD; rq.Value = (boEnable? 0x08:0x00); rq.Index = RFU ; rq.Length = 0 ; if(! m_usb.OutCtrlRequest(rq, vBuffer,Index)) { return false; } return true; }
int MaxValueSimple::maxValueCL(int* values, size_t len) { try { cl_int status = CL_SUCCESS; /*** Ausgabe von Informationen ueber gewaehltes OpenCL-Device ***/ /* TODO logging Logger::logDebug( METHOD, Logger::sStream << "max compute units: " << devices[0].getInfo< CL_DEVICE_MAX_COMPUTE_UNITS> ()); Logger::logDebug( METHOD, Logger::sStream << "max work item sizes: " << devices[0].getInfo<CL_DEVICE_MAX_WORK_ITEM_SIZES> ()[0]); Logger::logDebug( METHOD, Logger::sStream << "max work group sizes: " << devices[0].getInfo<CL_DEVICE_MAX_WORK_GROUP_SIZE> ()); Logger::logDebug( METHOD, Logger::sStream << "max global mem size (KB): " << devices[0].getInfo<CL_DEVICE_GLOBAL_MEM_SIZE> () / 1024); Logger::logDebug( METHOD, Logger::sStream << "max local mem size (KB): " << devices[0].getInfo<CL_DEVICE_LOCAL_MEM_SIZE> () / 1024); */ /*** Erstellen und Vorbereiten der Daten ***/ cl::Buffer vBuffer(context, CL_MEM_READ_WRITE | CL_MEM_COPY_HOST_PTR, sizeof(cl_int) * len, &values[0], &status); if (status != CL_SUCCESS) { throw cl::Error(status, "cl::Buffer values"); } cmdQ.finish(); /*** Arbeitsgroeszen berechnen ***/ // Anzahl der Work-Items = globalSize // Work-Items pro Work-Group = localSize const size_t MAX_GROUP_SIZE = devices[0].getInfo< CL_DEVICE_MAX_WORK_GROUP_SIZE> (); size_t globalSize; size_t localSize; do { globalSize = len; localSize = MaxValueSimple::calcWorkGroupSize(globalSize, MAX_GROUP_SIZE); if (localSize == 1) { globalSize = ceil((double) len / WG_FAC) * WG_FAC; localSize = MaxValueSimple::calcWorkGroupSize(globalSize, MAX_GROUP_SIZE); /* TODO logging Logger::logDebug( METHOD, Logger::sStream << "GlobalSize has been extended to " << globalSize); */ } /* TODO logging Logger::logDebug(METHOD, Logger::sStream << "globalSize: " << globalSize); Logger::logDebug(METHOD, Logger::sStream << "localSize: " << localSize); */ /*** Kernel-Argumente setzen ***/ status = kernel.setArg(0, vBuffer); if (status != CL_SUCCESS) { throw cl::Error(status, "Kernel.SetArg"); } status = kernel.setArg(1, sizeof(cl_int) * localSize, NULL); if (status != CL_SUCCESS) { throw cl::Error(status, "Kernel.SetArg"); } /*** Kernel ausfuehren und auf Abarbeitung warten ***/ cl::KernelFunctor func = kernel.bind(cmdQ, cl::NDRange(globalSize), cl::NDRange(localSize)); event = func(); event.wait(); cmdQ.finish(); /* runtimeKernel += event.getProfilingInfo<CL_PROFILING_COMMAND_END> (); runtimeKernel -= event.getProfilingInfo<CL_PROFILING_COMMAND_START> (); */ len = globalSize / localSize; } while (globalSize > localSize && localSize > 1); /*** Daten vom OpenCL-Device holen ***/ // TODO nur 1. element auslesen status = cmdQ.enqueueReadBuffer(vBuffer, true, 0, sizeof(cl_int) * 1, &values[0]); if (status != CL_SUCCESS) { throw cl::Error(status, "CommandQueue.enqueueReadBuffer"); } /* TODO logging Logger::log( METHOD, TIME, Logger::sStream << "timeKernel=" << 1.0e-9 * runtimeKernel << ";"); */ return values[0]; } catch (cl::Error& err) { // TODO Logger::logError(METHOD, Logger::sStream << err.what()); std::cerr << "[ERROR] MaxValueSimple::maxValueCL(int*, size_t): " << err.what() << " (" << err.err() << ")" << std::endl; return MaxValueSimple::MAX_FAILURE; } catch (std::exception& err) { // TODO Logger::logError(METHOD, Logger::sStream << err.what()); std::cerr << "[ERROR] MaxValueSimple::maxValueCL(int*, size_t): " << err.what() << std::endl; return MaxValueSimple::MAX_FAILURE; } }
void NetworkStream::WriteByte(Byte _Value) { Vector<Byte> vBuffer(1, _Value); m_pSocket->Send(vBuffer); }
bool decryptMessage( const BYTE* apEncrypted, DWORD adwEncryptedLength, BYTE* apMessage, DWORD& adwMessageLength) { if(adwMessageLength > getStreamSizes().cbMaximumMessage) { std::cout << "Message is too big\n"; return false; } size_t szTotalSize = getStreamSizes().cbHeader + getStreamSizes().cbMaximumMessage + getStreamSizes().cbTrailer; std::vector<BYTE> vBuffer(szTotalSize, 0); memcpy(&vBuffer[0], apEncrypted, adwEncryptedLength); SecBufferDesc bufferDesc = {0}; SecBuffer secBuff[4] = {0}; bufferDesc.cBuffers = 4; bufferDesc.pBuffers = secBuff; secBuff[0].BufferType = SECBUFFER_DATA; secBuff[0].cbBuffer = adwEncryptedLength; secBuff[0].pvBuffer = &vBuffer[0]; secBuff[1].BufferType = SECBUFFER_EMPTY; secBuff[1].pvBuffer = NULL; secBuff[1].cbBuffer = 0; secBuff[2].BufferType = SECBUFFER_EMPTY; secBuff[2].pvBuffer = NULL; secBuff[2].cbBuffer = 0; secBuff[3].BufferType = SECBUFFER_EMPTY; secBuff[3].pvBuffer = NULL; secBuff[3].cbBuffer = 0; ULONG ulQ = 0; SECURITY_STATUS ssResult = SEC_E_INCOMPLETE_MESSAGE; for(;ssResult == SEC_E_INCOMPLETE_MESSAGE;) { ssResult = ::DecryptMessage( &g_hContext, &bufferDesc, 0, &ulQ); } if(ssResult != SEC_E_OK) { std::cout << std::hex << "Error in ::DecryptMessage = " << ssResult << '\n'; return false; } memcpy(apMessage, secBuff[1].pvBuffer, secBuff[1].cbBuffer); adwMessageLength = secBuff[1].cbBuffer; return true; }
S32 NetworkStream::ReadByte() { Vector<Byte> vBuffer(1); m_pSocket->Receive(vBuffer); return vBuffer[0]; }
bool sendEncrypted( SOCKET aSocket, const void* apOutMessage, size_t aszMsgLength) { size_t szTotalSize = getStreamSizes().cbHeader + getStreamSizes().cbMaximumMessage + getStreamSizes().cbTrailer; std::vector<BYTE> vBuffer(szTotalSize, 0); SecBufferDesc buffDesc = {0}; SecBuffer secBuff[4] = {0}; buffDesc.cBuffers = 4; buffDesc.pBuffers = secBuff; secBuff[0].BufferType = SECBUFFER_STREAM_HEADER; secBuff[0].cbBuffer = getStreamSizes().cbHeader; secBuff[0].pvBuffer = &vBuffer[0]; secBuff[1].BufferType = SECBUFFER_DATA; secBuff[2].BufferType = SECBUFFER_STREAM_TRAILER; secBuff[3].BufferType = SECBUFFER_EMPTY; secBuff[3].pvBuffer = NULL; secBuff[3].cbBuffer = 0; while(aszMsgLength > 0) { size_t szEncryptSize = min( aszMsgLength, getStreamSizes().cbMaximumMessage); secBuff[1].cbBuffer = szEncryptSize; secBuff[1].pvBuffer = &vBuffer[0] + secBuff[0].cbBuffer; memcpy(secBuff[1].pvBuffer, apOutMessage, szEncryptSize); secBuff[2].cbBuffer = getStreamSizes().cbTrailer; secBuff[2].pvBuffer = static_cast<BYTE*>(secBuff[1].pvBuffer) + szEncryptSize; SECURITY_STATUS ssResult = ::EncryptMessage( &g_hContext, 0, &buffDesc, 0); if(ssResult != SEC_E_OK) { std::cout << std::hex << "Error in ::EncryptMessage = " << ssResult << '\n'; return false; } std::cout << "\nEncrypted:\n"; printHexDump( secBuff[0].cbBuffer + secBuff[1].cbBuffer + secBuff[2].cbBuffer, static_cast<BYTE*>(secBuff[0].pvBuffer)); bool fResult = SendMsg( aSocket, static_cast<BYTE*>(secBuff[0].pvBuffer), secBuff[0].cbBuffer + secBuff[1].cbBuffer + secBuff[2].cbBuffer); if(!fResult) { std::cout << "cannot send message\n"; return false; } apOutMessage = static_cast<const BYTE*>(apOutMessage) + szEncryptSize; aszMsgLength -= szEncryptSize; } return true; }
bool receiveEncrypted( SOCKET aSocket, void* apMessage, size_t aszBufferSize, size_t& aszRecieved, long* aplDecryptReturn = NULL) { size_t szTotalSize = getStreamSizes().cbHeader + getStreamSizes().cbMaximumMessage + getStreamSizes().cbTrailer; std::vector<BYTE> vBuffer(szTotalSize, 0); SecBufferDesc bufferDesc = {0}; SecBuffer secBuff[4] = {0}; bufferDesc.cBuffers = 4; bufferDesc.pBuffers = secBuff; size_t szRawRead = 0; SECURITY_STATUS ssResult = SEC_E_INCOMPLETE_MESSAGE; while(ssResult == SEC_E_INCOMPLETE_MESSAGE) { DWORD dwRead = 0; bool fResult = ReceiveMsg( aSocket, &vBuffer[0], szTotalSize - szRawRead, &dwRead); if(!fResult) { std::cout << "cannot receive message\n"; return false; } szRawRead += dwRead; secBuff[0].BufferType = SECBUFFER_DATA; secBuff[0].cbBuffer = szRawRead; secBuff[0].pvBuffer = &vBuffer[0]; secBuff[1].BufferType = SECBUFFER_EMPTY; secBuff[1].cbBuffer = 0; secBuff[1].pvBuffer = NULL; secBuff[2].BufferType = SECBUFFER_EMPTY; secBuff[2].cbBuffer = 0; secBuff[2].pvBuffer = NULL; secBuff[3].BufferType = SECBUFFER_EMPTY; secBuff[3].cbBuffer = 0; secBuff[3].pvBuffer = NULL; ULONG ulQop = 0; ssResult = ::DecryptMessage( &g_hContext, &bufferDesc, 0, &ulQop); } if(ssResult != SEC_E_OK) { // TODO: remake this // SEC_I_CONTEXT_EXPIRED - shutdown // SEC_I_RENEGOTIATE - need to renegotate if(aplDecryptReturn && (ssResult == SEC_I_CONTEXT_EXPIRED || ssResult == SEC_I_RENEGOTIATE)) { *aplDecryptReturn = ssResult; return true; } else { if(aplDecryptReturn) *aplDecryptReturn = ssResult; std::cout << std::hex << "Error in ::DecryptMessage = " << ssResult << '\n'; return false; } } aszRecieved = min(aszBufferSize, secBuff[1].cbBuffer); memcpy(apMessage, secBuff[1].pvBuffer, aszRecieved); return true; }
void LevelTextureElementAutotexCube::apply(int x1, int y1, int z1, int x2, int y2, int z2, int baseX, int baseY, int baseZ, char* voxelBuffer, unsigned char* buffer) { for (int x = x1; x <= x2; x++) { for (int y = y1; y <= y2; y++) { for (int z = z1; z <= z2; z++) { int n = 0, nrock = 0; for (int dx = -1; dx < 2; dx++) for (int dy = -1; dy < 2; dy++) for (int dz = -1; dz < 2; dz++) { if (vBuffer(x + dx, y + dy, z + dz) < 0) n++; else nrock++; } if (n > 0 && nrock > 0) { // surface voxel Vector3 normal = Vector3(vBuffer(x + 1, y , z ) - vBuffer(x - 1, y , z ), vBuffer(x , y - 1, z ) - vBuffer(x , y + 1, z ), vBuffer(x , y , z - 1) - vBuffer(x , y , z + 1)); normal.normalise(); if (normal.y > 0.7f) tBuffer(x, y, z) = top; else tBuffer(x, y, z) = side; } else { if (vBuffer(x, y, z) < 0) { // air voxel; no variation for the sake of speed tBuffer(x, y, z) = inner1; } else { // ground voxel if (inner1 == inner2) tBuffer(x, y, z) = inner1; else { float perlin = Noise::perlinNoise3D((baseX+x)*2, (baseY+y)*2, (baseZ+z)*2); if (perlin >= 0.0f) tBuffer(x, y, z) = inner1; else tBuffer(x, y, z) = inner2; } } } } } } }
void DifBufferBase::buffer(const Eref& e,double C) { vBuffer(e,C); }