📄 csp.h
字号:
/////////////////////////////////////////////////////////////////////////////
// FILE : csp.h //
// DESCRIPTION : Header file for testcsp and cspinstl //
// USAGE : //
// AUTHOR : //
// HISTORY : // //
// Nov 18 1998 davidteo Ported to Windows CE //
// //
// Copyright (C) 1993 Microsoft Corporation All Rights Reserved //
/////////////////////////////////////////////////////////////////////////////
// Windows CE does not need the 3 includes below
// and only supports UNICODE
/*
#undef UNICODE
#include <stdlib.h>
#include <stdio.h>
#include <ctype.h>
*/
#include <windows.h>
#include <wincrypt.h>
// Name of CSP
#define CSP_PROV L"CSP Provider"
// Provider Type
#define CSP_PROV_TYPE 900
// Filename of CSP DLL
#define CSP_FILE L"CSP.DLL"
// Filename of CSP sign file, generated by sign.exe
#define CSP_SIG_FILE L"CSP.SIG"
//*****************************************************************************
void TRACE(LPCTSTR szFormat, ...)
{
#define MSG_HDR APPNAME L": "
TCHAR szBuffer[128];
// Build the message.
_tcscpy(szBuffer, MSG_HDR);
va_list pArgs;
va_start(pArgs, szFormat);
wvsprintf(szBuffer + 9, szFormat, pArgs);
va_end(pArgs);
// Send the message string to the debugger as well.
_tcscat(szBuffer, TEXT("\r\n"));
OutputDebugString(szBuffer); // Send to Debugger
wprintf (szBuffer); // and also send to console
}
//*******************************************************************************
#ifndef _O_RDONLY
#define _O_RDONLY 0x0000
#endif
#ifndef _O_WRONLY
#define _O_WRONLY 0x0001
#endif
#ifndef _O_SEQUENTIAL
#define _O_SEQUENTIAL 0x0020
#endif
#ifndef _O_CREAT
#define _O_CREAT 0x0100
#endif
#ifndef _O_EXCL
#define _O_EXCL 0x0400
#endif
#ifndef SEEK_SET
#define SEEK_SET 0
#endif
#ifndef SEEK_END
#define SEEK_END 2
#endif
#define countof(a) (sizeof(a)/sizeof(*(a)))
#define BUFFER_SIZE 32768
//******************************************************************************
extern "C" int U_ropen(const WCHAR *, UINT);
extern "C" int U_rread(int, BYTE *, int);
extern "C" int U_rwrite(int, BYTE *, int);
extern "C" int U_rlseek(int, int, int);
extern "C" int U_rclose(int);
//******************************************************************************
BOOL CopyCESH (LPCTSTR szSrc, LPCTSTR szDst)
{
BYTE *pbBuffer = NULL;
int fileSrc = -1, fileDst = -1, length;
HANDLE hFileSrc = INVALID_HANDLE_VALUE, hFileDst = INVALID_HANDLE_VALUE;
DWORD dwBytesLeft = 0, dwBytesBlock = 0, dwBytesTransferred = 0;
BOOL fSuccess = FALSE;
TCHAR szFullDst[MAX_PATH + 6], *szTemp;
// Make sure we were passed valid args.
if (!szSrc || !*szSrc)
{
SetLastError(ERROR_INVALID_PARAMETER);
return FALSE;
}
// Check to see if the destination is actually a path (ends in '\').
length = _tcslen(szDst);
if ((length > 0) && (*(szDst + length - 1) == TEXT('\\')))
{
// Build a new destination path - start by copying the path to our buffer.
_tcscpy(szFullDst, szDst);
// Locate the just the file portion of the source file path.
szDst = szSrc;
if (szTemp = _tcsrchr(szDst, TEXT('\\'))) {
szDst = szTemp + 1;
}
if (szTemp = _tcsrchr(szDst, TEXT(':'))) {
szDst = szTemp + 1;
}
// Append the source file name to the destination file path.
_tcscat(szFullDst, szDst);
// Point our destination file name to our new full path buffer.
szDst = szFullDst;
}
// Tell the user what we are about to do.
TRACE(TEXT("Copying \"%s\" to \"%s\"..."), szSrc, szDst);
__try
{
if (!(pbBuffer = new BYTE[BUFFER_SIZE]))
{
TRACE(TEXT("Not enough memory to allocate a %u byte copy buffer."), BUFFER_SIZE);
__leave;
}
// Open the source file for read across PPSH.
if ((fileSrc = U_ropen(szSrc, _O_RDONLY | _O_SEQUENTIAL)) == -1)
{
TRACE(TEXT("Error %u trying to open \"%s\"."), GetLastError(), szSrc);
__leave;
}
// Get the size of the file
dwBytesLeft = U_rlseek(fileSrc, 0, SEEK_END);
U_rlseek(fileSrc, 0, SEEK_SET);
// Make sure we have a valid file size.
if (dwBytesLeft == 0xFFFFFFFF)
{
TRACE(TEXT("Error %u trying to get size of \"%s\"."), GetLastError(), szSrc);
__leave;
}
// Try CREATE_NEW first and display overwrite warning?
// Open the destination file for write on the device.
hFileDst = CreateFile(szDst, GENERIC_WRITE, FILE_SHARE_READ, NULL, \
CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
if (hFileDst == INVALID_HANDLE_VALUE)
{
TRACE(TEXT("Error %u trying to create \"%s\"."), GetLastError(), szDst);
__leave;
}
// Loop through the file do read block / write block
while (dwBytesLeft > 0)
{
// Compute the size of this block of data we are about to transfer.
dwBytesBlock = (dwBytesLeft < BUFFER_SIZE) ? dwBytesLeft : BUFFER_SIZE;
// Read a block of data from the source PPSH file.
if (U_rread(fileSrc, pbBuffer, dwBytesBlock) != (int)dwBytesBlock)
{
TRACE(TEXT("Error %u reading from \"%s\"."), GetLastError(), szSrc);
__leave;
}
// Write a block of data to the destination device file.
if (!WriteFile(hFileDst, pbBuffer, dwBytesBlock, &(dwBytesTransferred = 0), NULL) ||
(dwBytesBlock != dwBytesTransferred))
{
TRACE(TEXT("Error %u writing to \"%s\"."), GetLastError(), szDst);
__leave;
}
// Reduce our "bytes left" counter by the number of bytes we just transferred.
dwBytesLeft -= dwBytesBlock;
} // end of while
// If we made it this far, things look good.
fSuccess = TRUE;
TRACE(TEXT("Successfully copied \"%s\" to \"%s\"."), szSrc, szDst);
// Global cleanup code
}
__finally
{
// Free our copy buffer.
if (pbBuffer)
{
delete[] pbBuffer;
}
// Close any source PPSH file we may have opened.
if (fileSrc != -1)
{
U_rclose(fileSrc);
}
// Close any destination PPSH file we may have opened.
if (fileDst != -1)
{
U_rclose(fileDst);
}
// Close any source device file we may have opened.
if (hFileSrc != INVALID_HANDLE_VALUE)
{
CloseHandle(hFileSrc);
}
// Close any destination device file we may have opened.
if (hFileDst != INVALID_HANDLE_VALUE)
{
CloseHandle(hFileDst);
}
}
return fSuccess;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -