STDMETHODIMP CMemoryStream::Write(const void *pv, ULONG cb, ULONG *pcbWritten) { TraceEnter(); if (NULL == pv) { return STG_E_INVALIDPOINTER; } TraceOut("pv=%p, cb=%u, pcbWritten=%p", pv, cb, pcbWritten); BYTE *pbEnd = m_pbBuffer + m_nSize; BYTE *pbReqStart = m_pbBuffer + m_nPos; BYTE *pbReqEnd = pbReqStart + cb; ULONG nBytesToSet = (ULONG)(min(pbEnd, pbReqEnd) - pbReqStart); memcpy(pbReqStart, pv, nBytesToSet); if (NULL != pcbWritten) { *pcbWritten = nBytesToSet; } m_nPos += nBytesToSet; TraceOut("m_nPos=%d", m_nPos); SYSTEMTIME st; GetSystemTime(&st); SystemTimeToFileTime(&st, &m_ftModified); if (nBytesToSet < cb) { TraceLeaveHr(STG_E_MEDIUMFULL); return STG_E_MEDIUMFULL; } else { TraceLeaveHr(S_OK); return S_OK; } }
/////////////////////////////////////////////////////////////////////////// // CDebugLoggerBase::BuildTraceBuffer // // This builds a trace buffer and outputs it through the service provider. // void CDebugLoggerBase::BuildTraceBuffer(DWORD dwTraceLevel, LPCTSTR pszFormat, va_list args) { const UINT MAX_BUFFER_SIZE = 4096; // If we aren't in this trace level, exit. if ((dwTraceLevel & GetTraceLevel()) == 0) return; // Put the contents into a buffer tsplib::inauto_ptr<TCHAR> pszBuff(new TCHAR[MAX_BUFFER_SIZE]); _TSP_VERIFY(wvsprintf(pszBuff.get(), pszFormat, args) < MAX_BUFFER_SIZE); TraceOut(pszBuff.get()); }// CDebugLoggerbase::BuildTraceBuffer
STDMETHODIMP CMemoryStream::Read(void *pv, ULONG cb, ULONG *pcbRead) { TraceEnter(); if (NULL == pv) { TraceLeaveHr(STG_E_INVALIDPOINTER); return STG_E_INVALIDPOINTER; } TraceOut("pv=%p, cb=%u, pcbRead=%p, m_nPos=%d", pv, cb, pcbRead, m_nPos); BYTE *pbEnd = m_pbBuffer + m_nSize; BYTE *pbReqStart = m_pbBuffer + m_nPos; BYTE *pbReqEnd = pbReqStart + cb; ULONG nBytesToGet = (ULONG)(min(pbEnd - pbReqStart, pbReqEnd - pbReqStart)); memcpy(pv, pbReqStart, nBytesToGet); if (NULL != pcbRead) { *pcbRead = nBytesToGet; } m_nPos += nBytesToGet; TraceOut("m_nPos=%d", m_nPos); SYSTEMTIME st; GetSystemTime(&st); SystemTimeToFileTime(&st, &m_ftAccessed); TraceLeaveHr(S_OK); return S_OK; }
CMemoryStream::CMemoryStream(BYTE *pbBuffer, int nSize) : m_pbBuffer(pbBuffer), m_nSize(nSize), m_nPos(0) { TraceEnter(); TraceOut("m_nSize=%d", m_nSize); m_lRefCount = 1; SYSTEMTIME st; GetSystemTime(&st); SystemTimeToFileTime(&st, &m_ftCreation); SystemTimeToFileTime(&st, &m_ftModified); SystemTimeToFileTime(&st, &m_ftAccessed); TraceLeave(); }
// IUnknown methods STDMETHODIMP CMemoryStream::QueryInterface(REFIID iid, void **ppvObject) { TraceEnter(); TraceOut("%S", GuidToString(iid)); if (NULL == ppvObject) { return E_INVALIDARG; } if (IsEqualCLSID(iid, IID_IStream)) { AddRef(); *ppvObject = this; return S_OK; } else { return E_NOINTERFACE; } }
STDMETHODIMP CMemoryStream::Stat(STATSTG *pstatstg, DWORD grfStatFlag) { TraceEnter(); TraceOut("grfStatFlag=%u", grfStatFlag); if (NULL == pstatstg) { return STG_E_INVALIDPOINTER; } ZeroMemory(pstatstg, sizeof(*pstatstg)); if (grfStatFlag != STATFLAG_NONAME) { WCHAR wszName[64]; wsprintfW(wszName, L"%p", m_pbBuffer); size_t nSize = 1 + wcslen(wszName); pstatstg->pwcsName = (LPOLESTR)CoTaskMemAlloc(nSize); wcscpy_s(pstatstg->pwcsName, nSize, wszName); } pstatstg->cbSize.QuadPart = (ULONGLONG)m_nSize; pstatstg->type = STGTY_STREAM; pstatstg->ctime = m_ftCreation; pstatstg->mtime = m_ftModified; pstatstg->atime = m_ftAccessed; pstatstg->grfMode = STGM_READ; pstatstg->grfLocksSupported = 0; pstatstg->clsid = CLSID_NULL; pstatstg->grfStateBits = 0; pstatstg->reserved = 0; return S_OK; }
SQLRETURN CDiagRec::GetDiagRec(SQLSMALLINT RecNumber, DWORD ErrorMsgLang, SQLWCHAR *SqlState, SQLINTEGER *NativeErrPtr, SQLWCHAR *MessageText, SQLSMALLINT BufferLength, SQLSMALLINT *TextLengthPtr) { CDiagStatusPtr diagStatusPtr; short strLen; short tmpStrLen; ODBCMXMSG_Def MsgStruct; SQLRETURN rc = SQL_SUCCESS; SQLINTEGER translateLength; SQLINTEGER translateLengthMax; UCHAR errorMsg[MAX_TRANSLATE_ERROR_MSG_LEN]; char cTmpBuf[132]; if (RecNumber <= (SQLSMALLINT)m_DiagStatusCollect.size()) { GETDIAGRECPTR(diagStatusPtr, m_DiagStatusCollect, RecNumber-1); // Get the Formatted Message Text from mc file if (MAX_DIAG_PARAMS >= 4) // if MAX_DIAG_PARAMS is changed you may need to change here { gDrvrGlobal.gOdbcMsg.GetOdbcMessage(ErrorMsgLang, diagStatusPtr->m_DiagErrorCode, &MsgStruct, diagStatusPtr->m_DiagParams[0].c_str(), diagStatusPtr->m_DiagParams[1].c_str(), diagStatusPtr->m_DiagParams[2].c_str(), diagStatusPtr->m_DiagParams[3].c_str()); } // Append the Message Text MsgStruct.lpsMsgText.append(diagStatusPtr->m_DiagMessageText); // Insert the Message Header MsgStruct.lpsMsgText.insert(0, gErrorMsgHeader[diagStatusPtr->m_DiagComponentCode]); // Append row id if(diagStatusPtr->m_DiagRowNumber>SQL_NO_ROW_NUMBER) { MsgStruct.lpsMsgText.append(" Row: "); MsgStruct.lpsMsgText.append(itoa(diagStatusPtr->m_DiagRowNumber,cTmpBuf,10)); // Append column id if(diagStatusPtr->m_DiagColumnNumber>SQL_COLUMN_NUMBER_UNKNOWN) { MsgStruct.lpsMsgText.append(" Column: "); MsgStruct.lpsMsgText.append(itoa(diagStatusPtr->m_DiagColumnNumber,cTmpBuf,10)); } } int transLen = 0; if (SqlState != NULL) { char tmpSqlState[6]; char error[50]; if (diagStatusPtr->m_DiagSqlState[0] != '\0') strncpy((char *)tmpSqlState, (const char *)diagStatusPtr->m_DiagSqlState, 5); else strncpy((char *)tmpSqlState, MsgStruct.lpsSQLState, 5); tmpSqlState[5] = 0; SqlState[0] = 0; //convert to UTF-16 rc = UTF8ToWChar(tmpSqlState, 5, SqlState, 6, &transLen, error); if(pdwGlobalTraceVariable && *pdwGlobalTraceVariable && rc == SQL_ERROR) TraceOut(TR_ODBC_API, "CDiagRec::GetDiagRec: SqlState Error: tmpSqlState \"%s\"", tmpSqlState); } if (NativeErrPtr != NULL) *NativeErrPtr = diagStatusPtr->m_DiagNative; tmpStrLen = MsgStruct.lpsMsgText.size(); //Double strLen to circumvent the bug in driver manager, that requires us to give //the NO. OF BYTES instead of no. of characters strLen = tmpStrLen * 2; translateLengthMax = (BufferLength == SQL_NTS) ? strLen : BufferLength; if (MessageText != NULL) { // translate from UTF8 to WChar if ( (rc = UTF8ToWChar((char *)MsgStruct.lpsMsgText.c_str(), tmpStrLen, MessageText, translateLengthMax/2, (int *)&translateLength, (char *)errorMsg)) != SQL_SUCCESS ) rc = SQL_SUCCESS_WITH_INFO; //ERROR; strLen = translateLength; ((wchar_t *)MessageText)[strLen] = L'\0' ; } if (TextLengthPtr != NULL) *TextLengthPtr = strLen; } else { if (SqlState != NULL) wcsncpy(SqlState, L"00000", 5); if (MessageText != NULL) MessageText[0] =L'\0'; if (TextLengthPtr != NULL) *TextLengthPtr = 0; rc = SQL_NO_DATA; } return rc; }
STDMETHODIMP CMemoryStream::Seek(LARGE_INTEGER dlibMove, DWORD dwOrigin, ULARGE_INTEGER *plibNewPosition) { HRESULT hr = S_OK; TraceEnter(); TraceOut("dlibMove=%Ld, dwOrigin=%d", dlibMove.QuadPart, dwOrigin); if (dlibMove.QuadPart > INT_MAX || dlibMove.QuadPart < INT_MIN) { hr = STG_E_INVALIDFUNCTION; } else { int nMove = (int)dlibMove.QuadPart; TraceOut("nMove = %d", nMove); int nNewPos = 0; switch (dwOrigin) { case STREAM_SEEK_SET: if (nMove >= 0 && nMove < m_nSize) { m_nPos = nMove; if (NULL != plibNewPosition) { plibNewPosition->QuadPart = (__int64)m_nPos; } } else { hr = STG_E_INVALIDFUNCTION; } break; case STREAM_SEEK_CUR: nNewPos = m_nPos + nMove; if (nNewPos >= 0 && nNewPos < m_nSize) { m_nPos = nNewPos; if (NULL != plibNewPosition) { plibNewPosition->QuadPart = (__int64)m_nPos; } } else { hr = STG_E_INVALIDFUNCTION; } break; case STREAM_SEEK_END: nNewPos = m_nSize - 1 + nMove; if (nNewPos >= 0 && nNewPos < m_nSize) { m_nPos = nNewPos; if (NULL != plibNewPosition) { plibNewPosition->QuadPart = (__int64)m_nPos; } } else { hr = STG_E_INVALIDFUNCTION; } break; default: hr = STG_E_INVALIDFUNCTION; break; } } TraceLeaveHr(hr); return hr; }
SQLRETURN returnAttrValue(BOOL reportError, CHandle *pHandle, RETURN_VALUE_STRUCT *retValues, SQLPOINTER ValuePtr, SQLINTEGER BufferLength, SQLINTEGER *StringLengthPtr) { SQLINTEGER strLen = DRVR_PENDING; SQLRETURN rc = SQL_SUCCESS; SQLINTEGER translateLength; SQLINTEGER translateLengthMax; UCHAR errorMsg[MAX_TRANSLATE_ERROR_MSG_LEN]; ICUConverter* iconv; if (pHandle == NULL ) // and check whether a connection has happened. iconv = &gDrvrGlobal.ICUConv; else iconv = pHandle->m_ICUConv; errorMsg[0] = '\0'; if (ValuePtr != NULL) { switch (retValues->dataType) { case SQL_C_SBIGINT: *(IDL_long_long*)ValuePtr = retValues->u.s64Value; strLen = sizeof(IDL_long_long); break; case SQL_C_UBIGINT: *(IDL_unsigned_long_long*)ValuePtr = retValues->u.u64Value; strLen = sizeof(IDL_unsigned_long_long); break; case SQL_IS_POINTER: *(SQLPOINTER *)ValuePtr = retValues->u.pValue; strLen = sizeof(SQLPOINTER); break; case SQL_IS_INTEGER: *(SQLINTEGER *)ValuePtr = retValues->u.s32Value; strLen = sizeof(SQLINTEGER); break; case SQL_IS_UINTEGER: *(SQLUINTEGER *)ValuePtr = retValues->u.u32Value; strLen = sizeof(SQLUINTEGER); break; case SQL_IS_SMALLINT: *(SQLSMALLINT *)ValuePtr = retValues->u.s16Value; strLen = sizeof(SQLSMALLINT); break; case SQL_IS_USMALLINT: *(SQLUSMALLINT *)ValuePtr = retValues->u.u16Value; strLen = sizeof(SQLUSMALLINT); break; default: if (retValues->u.strPtr != NULL) { strLen = strlen(retValues->u.strPtr); translateLengthMax = (BufferLength == SQL_NTS) ? strLen : BufferLength; if(pdwGlobalTraceVariable && *pdwGlobalTraceVariable) TraceOut(TR_ODBC_ERROR, "UTF8ToWChar retValues->u.strPtr \"%s\", strLen %d ", retValues->u.strPtr, strLen); rc = iconv->OutArgTranslationHelper((SQLCHAR*)retValues->u.strPtr, strLen, (char*)ValuePtr, translateLengthMax, (int *)&translateLength, NULL, (char*)errorMsg); // For ALM CR 5228. The returned length of the attribute value should be counted in bytes for buffer size as ODBC // standard specification explains. Thus, translateLength should be used to guarantee the length is correctly // returned not matter the application type is ANSI or UNICODE. strLen = translateLength; if(rc != SQL_SUCCESS) { if (errorMsg && errorMsg[0] != '\0') { if (reportError) pHandle->setDiagRec(DRIVER_ERROR, IDS_186_DSTODRV_TRUNC, 0, (char *)errorMsg); } else { if (reportError) pHandle->setDiagRec(DRIVER_ERROR, IDS_186_DSTODRV_TRUNC); } } } else iconv->NullTerminate((char*) ValuePtr); break; } } else { switch (retValues->dataType) { case SQL_IS_POINTER: strLen = sizeof(SQLPOINTER); break; case SQL_IS_INTEGER: strLen = sizeof(SQLINTEGER); break; case SQL_IS_UINTEGER: strLen = sizeof(SQLUINTEGER); break; case SQL_IS_SMALLINT: strLen = sizeof(SQLSMALLINT); break; case SQL_IS_USMALLINT: strLen = sizeof(SQLUSMALLINT); break; default: if (retValues->u.strPtr != NULL) strLen = gDrvrGlobal.ICUConv.FindStrLength((const char*)retValues->u.strPtr, BufferLength); break; } } if (strLen != DRVR_PENDING) if (StringLengthPtr != NULL) *StringLengthPtr = strLen; return rc; }