本文整理汇总了C++中stringT::length方法的典型用法代码示例。如果您正苦于以下问题:C++ stringT::length方法的具体用法?C++ stringT::length怎么用?C++ stringT::length使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类stringT
的用法示例。
在下文中一共展示了stringT::length方法的4个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。
示例1: IssueError
// This routine uses Windows functions
void pws_os::IssueError(const stringT &csFunction, bool bMsgBox)
{
LPVOID lpMsgBuf;
LPVOID lpDisplayBuf;
const DWORD dw = GetLastError();
FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM,
NULL,
dw,
MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
(LPTSTR) &lpMsgBuf,
0, NULL);
lpDisplayBuf = (LPVOID)LocalAlloc(LMEM_ZEROINIT,
(lstrlen((LPCTSTR)lpMsgBuf) + csFunction.length() + 40) * sizeof(TCHAR));
wsprintf((LPTSTR)lpDisplayBuf, TEXT("%s failed with error %d: %s"),
csFunction.c_str(), dw, lpMsgBuf);
if (bMsgBox)
MessageBox(NULL, (LPCTSTR)lpDisplayBuf, TEXT("Error"), MB_OK);
else
OutputDebugString((LPCTSTR)lpDisplayBuf);
LocalFree(lpMsgBuf);
LocalFree(lpDisplayBuf);
}
示例2: Decrypt
bool PWSfile::Decrypt(const stringT &fn, const StringX &passwd, stringT &errmess)
{
size_t len;
unsigned char* buf = NULL;
bool status = true;
unsigned char salt[SaltLength];
unsigned char ipthing[8];
unsigned char randstuff[StuffSize];
unsigned char randhash[SHA1::HASHLEN];
unsigned char temphash[SHA1::HASHLEN];
FILE *in = pws_os::FOpen(fn, _T("rb"));
if (in == NULL) {
status = false;
goto exit;
}
#ifdef KEEP_FILE_MODE_BWD_COMPAT
uint32 i32;
fread(&i32, 1, sizeof(uint32), in); // XXX portability issue
len = i32;
#else
fread(randstuff, 1, 8, in);
randstuff[8] = randstuff[9] = TCHAR('\0'); // ugly bug workaround
fread(randhash, 1, sizeof(randhash), in);
GenRandhash(passwd, randstuff, temphash);
if (memcmp(reinterpret_cast<char *>(randhash), reinterpret_cast<char *>(temphash), SHA1::HASHLEN) != 0) {
fclose(in);
LoadAString(errmess, IDSC_BADPASSWORD);
return false;
}
#endif // KEEP_FILE_MODE_BWD_COMPAT
{ // decryption in a block, since we use goto
fread(salt, 1, SaltLength, in);
fread(ipthing, 1, 8, in);
unsigned char dummyType;
unsigned char *pwd = NULL;
size_t passlen = 0;
long file_len = pws_os::fileLength(in);
ConvertString(passwd, pwd, passlen);
Fish *fish = BlowFish::MakeBlowFish(pwd, reinterpret_cast<int &>(passlen), salt, SaltLength);
trashMemory(pwd, passlen);
#ifdef UNICODE
delete[] pwd; // gross - ConvertString allocates only if UNICODE.
#endif
if (_readcbc(in, buf, len,dummyType, fish, ipthing, 0, file_len) == 0) {
delete fish;
delete[] buf; // if not yet allocated, delete[] NULL, which is OK
return false;
}
delete fish;
fclose(in);
} // decrypt
{ // write decrypted data
size_t suffix_len = CIPHERTEXT_SUFFIX.length();
size_t filepath_len = fn.length();
stringT out_fn = fn;
out_fn = out_fn.substr(0,filepath_len - suffix_len);
FILE *out = pws_os::FOpen(out_fn, _T("wb"));
if (out != NULL) {
size_t fret = fwrite(buf, 1, len, out);
if (fret != len) {
int save_errno = errno;
fclose(out);
errno = save_errno;
goto exit;
}
if (fclose(out) != 0) {
status = false;
goto exit;
}
} else { // open failed
status = false;
goto exit;
}
} // write decrypted
exit:
if (!status)
errmess = ErrorMessages();
delete[] buf; // allocated by _readcbc
return status;
}
示例3: Decrypt
bool PWSfile::Decrypt(const stringT &fn, const StringX &passwd, stringT &errmess)
{
ulong64 file_len;
size_t len;
unsigned char* buf = nullptr;
bool status = true;
unsigned char salt[SaltLength];
unsigned char ipthing[8];
unsigned char randstuff[StuffSize];
unsigned char randhash[SHA1::HASHLEN];
unsigned char temphash[SHA1::HASHLEN];
FILE *in = pws_os::FOpen(fn, _T("rb"));
if (in == nullptr) {
status = false;
goto exit;
}
file_len = pws_os::fileLength(in);
if (file_len < (8 + sizeof(randhash) + 8 + SaltLength)) {
fclose(in);
LoadAString(errmess, IDSC_FILE_TOO_SHORT);
return false;
}
fread(randstuff, 1, 8, in);
randstuff[8] = randstuff[9] = TCHAR('\0'); // ugly bug workaround
fread(randhash, 1, sizeof(randhash), in);
GenRandhash(passwd, randstuff, temphash);
if (memcmp(reinterpret_cast<char *>(randhash), reinterpret_cast<char *>(temphash), SHA1::HASHLEN) != 0) {
fclose(in);
LoadAString(errmess, IDSC_BADPASSWORD);
return false;
}
{ // decryption in a block, since we use goto
fread(salt, 1, SaltLength, in);
fread(ipthing, 1, 8, in);
unsigned char dummyType;
unsigned char *pwd = nullptr;
size_t passlen = 0;
ConvertPasskey(passwd, pwd, passlen);
Fish *fish = BlowFish::MakeBlowFish(pwd, reinterpret_cast<unsigned int &>(passlen), salt, SaltLength);
trashMemory(pwd, passlen);
delete[] pwd; // gross - ConvertPasskey allocates.
if (_readcbc(in, buf, len,dummyType, fish, ipthing, 0, file_len) == 0) {
delete fish;
delete[] buf; // if not yet allocated, delete[] nullptr, which is OK
return false;
}
delete fish;
fclose(in);
} // decrypt
{ // write decrypted data
size_t suffix_len = CIPHERTEXT_SUFFIX.length();
size_t filepath_len = fn.length();
stringT out_fn = fn;
out_fn = out_fn.substr(0,filepath_len - suffix_len);
FILE *out = pws_os::FOpen(out_fn, _T("wb"));
if (out != nullptr) {
size_t fret = fwrite(buf, 1, len, out);
if (fret != len) {
int save_errno = errno;
fclose(out);
errno = save_errno;
goto exit;
}
if (fclose(out) != 0) {
status = false;
goto exit;
}
} else { // open failed
status = false;
goto exit;
}
} // write decrypted
exit:
if (!status)
errmess = ErrorMessages();
delete[] buf; // allocated by _readcbc
return status;
}
示例4: LockFile
//.........这里部分代码省略.........
}
// Since ::CreateFile can't create directories, we need to check it exists
// first and, if not, try and create it.
// This is primarily for the config directory in the local APPDATA directory
// but will also be called for the database lock file - and since the database
// is already there, it is a bit of a redundant check but easier than coding
// for every different situation.
stringT sDrive, sDir, sName, sExt;
pws_os::splitpath(lock_filename, sDrive, sDir, sName, sExt);
stringT sNewDir = sDrive + sDir;
DWORD dwAttrib = GetFileAttributes(sNewDir.c_str());
DWORD dwerr(0);
if (dwAttrib == INVALID_FILE_ATTRIBUTES)
dwerr = GetLastError();
BOOL brc(TRUE);
if (dwerr == ERROR_FILE_NOT_FOUND ||
(dwAttrib != INVALID_FILE_ATTRIBUTES) &&
!(dwAttrib & FILE_ATTRIBUTE_DIRECTORY)) {
SECURITY_ATTRIBUTES secatt = {0};
secatt.nLength = sizeof(secatt);
brc = ::CreateDirectory(sNewDir.c_str(), &secatt);
}
// Obviously, if we can't create the directory - don't bother trying to
// create the lock file!
if (brc) {
lockFileHandle = ::CreateFile(lock_filename.c_str(),
GENERIC_WRITE,
FILE_SHARE_READ,
NULL,
CREATE_ALWAYS, // rely on share to fail if exists!
FILE_ATTRIBUTE_NORMAL | FILE_FLAG_WRITE_THROUGH |
// (Lockheed Martin) Secure Coding 11-14-2007
SECURITY_SQOS_PRESENT | SECURITY_IDENTIFICATION,
NULL);
// Make sure it's a file and not a pipe. (Lockheed Martin) Secure Coding 11-14-2007
if (lockFileHandle != INVALID_HANDLE_VALUE) {
if (::GetFileType( lockFileHandle ) != FILE_TYPE_DISK) {
::CloseHandle( lockFileHandle );
lockFileHandle = INVALID_HANDLE_VALUE;
}
}
// End of Change. (Lockheed Martin) Secure Coding 11-14-2007
}
if (lockFileHandle == INVALID_HANDLE_VALUE) {
DWORD error = GetLastError();
switch (error) {
case ERROR_SHARING_VIOLATION: // already open by a live process
GetLocker(lock_filename, s_locker);
locker = s_locker.c_str();
break;
default: {
// Give detailed error message, if possible
LPTSTR lpMsgBuf = NULL;
if (FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM,
NULL,
error,
MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
(LPTSTR)&lpMsgBuf,
0, NULL) != 0) {
locker = lpMsgBuf;
LocalFree(lpMsgBuf);
} else { // should never happen!
LoadAString(locker, IDSC_NOLOCKACCESS); // berrer than nothing
}
}
break;
} // switch (error)
return false;
} else { // valid filehandle, write our info
DWORD numWrit, sumWrit;
BOOL write_status;
write_status = ::WriteFile(lockFileHandle,
user.c_str(), (DWORD)(user.length() * sizeof(TCHAR)),
&sumWrit, NULL);
write_status &= ::WriteFile(lockFileHandle,
_T("@"), (DWORD)(sizeof(TCHAR)),
&numWrit, NULL);
sumWrit += numWrit;
write_status &= ::WriteFile(lockFileHandle,
host.c_str(), (DWORD)(host.length() * sizeof(TCHAR)),
&numWrit, NULL);
sumWrit += numWrit;
write_status &= ::WriteFile(lockFileHandle,
_T(":"), (DWORD)(sizeof(TCHAR)),
&numWrit, NULL);
sumWrit += numWrit;
write_status &= ::WriteFile(lockFileHandle,
pid.c_str(), (DWORD)(pid.length() * sizeof(TCHAR)),
&numWrit, NULL);
sumWrit += numWrit;
ASSERT(sumWrit > 0);
LockCount++;
return (write_status == TRUE);
}
}