use of com.sun.jna.platform.win32.Kernel32 in project jna by java-native-access.
the class Advapi32Util method backupEncryptedFile.
/**
* Backup an encrypted file or folder without decrypting it. A file named
* "bar/sample.text" will be backed-up to "destDir/sample.text". A directory
* named "bar" will be backed-up to "destDir/bar". This method is NOT
* recursive. If you have an encrypted directory with encrypted files, this
* method must be called once for the directory, and once for each encrypted
* file to be backed-up.
*
* @param src
* The encrypted file or directory to backup.
* @param destDir
* The directory where the backup will be saved.
*/
public static void backupEncryptedFile(File src, File destDir) {
if (!destDir.isDirectory()) {
throw new IllegalArgumentException("destDir must be a directory.");
}
// Open the file for export (backup)
ULONG readFlag = new ULONG(0);
// Import (restore) file
ULONG writeFlag = new ULONG(CREATE_FOR_IMPORT);
if (src.isDirectory()) {
writeFlag.setValue(CREATE_FOR_IMPORT | CREATE_FOR_DIR);
}
// open encrypted file for export
String srcFileName = src.getAbsolutePath();
PointerByReference pvContext = new PointerByReference();
if (Advapi32.INSTANCE.OpenEncryptedFileRaw(srcFileName, readFlag, pvContext) != W32Errors.ERROR_SUCCESS) {
throw new Win32Exception(Kernel32.INSTANCE.GetLastError());
}
// read encrypted file
final ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
FE_EXPORT_FUNC pfExportCallback = new FE_EXPORT_FUNC() {
@Override
public DWORD callback(Pointer pbData, Pointer pvCallbackContext, ULONG ulLength) {
byte[] arr = pbData.getByteArray(0, ulLength.intValue());
try {
outputStream.write(arr);
} catch (IOException e) {
throw new RuntimeException(e);
}
return new DWORD(W32Errors.ERROR_SUCCESS);
}
};
if (Advapi32.INSTANCE.ReadEncryptedFileRaw(pfExportCallback, null, pvContext.getValue()) != W32Errors.ERROR_SUCCESS) {
throw new Win32Exception(Kernel32.INSTANCE.GetLastError());
}
// close
try {
outputStream.close();
} catch (IOException e) {
throw new RuntimeException(e);
}
Advapi32.INSTANCE.CloseEncryptedFileRaw(pvContext.getValue());
// open file for import
String destFileName = destDir.getAbsolutePath() + File.separator + src.getName();
pvContext = new PointerByReference();
if (Advapi32.INSTANCE.OpenEncryptedFileRaw(destFileName, writeFlag, pvContext) != W32Errors.ERROR_SUCCESS) {
throw new Win32Exception(Kernel32.INSTANCE.GetLastError());
}
// write encrypted file
final IntByReference elementsReadWrapper = new IntByReference(0);
FE_IMPORT_FUNC pfImportCallback = new FE_IMPORT_FUNC() {
@Override
public DWORD callback(Pointer pbData, Pointer pvCallbackContext, ULONGByReference ulLength) {
int elementsRead = elementsReadWrapper.getValue();
int remainingElements = outputStream.size() - elementsRead;
int length = Math.min(remainingElements, ulLength.getValue().intValue());
pbData.write(0, outputStream.toByteArray(), elementsRead, length);
elementsReadWrapper.setValue(elementsRead + length);
ulLength.setValue(new ULONG(length));
return new DWORD(W32Errors.ERROR_SUCCESS);
}
};
if (Advapi32.INSTANCE.WriteEncryptedFileRaw(pfImportCallback, null, pvContext.getValue()) != W32Errors.ERROR_SUCCESS) {
throw new Win32Exception(Kernel32.INSTANCE.GetLastError());
}
// close
Advapi32.INSTANCE.CloseEncryptedFileRaw(pvContext.getValue());
}
use of com.sun.jna.platform.win32.Kernel32 in project jna by java-native-access.
the class Advapi32Util method isWellKnownSid.
/**
* Compares a SID to a well known SID and returns TRUE if they match.
*
* @param sidString
* String representation of a SID.
* @param wellKnownSidType
* Member of the WELL_KNOWN_SID_TYPE enumeration to compare with
* the SID at pSid.
* @return True if the SID is of the well-known type, false otherwise.
*/
public static boolean isWellKnownSid(String sidString, int wellKnownSidType) {
PSIDByReference pSID = new PSIDByReference();
if (!Advapi32.INSTANCE.ConvertStringSidToSid(sidString, pSID)) {
throw new Win32Exception(Kernel32.INSTANCE.GetLastError());
}
PSID value = pSID.getValue();
try {
return Advapi32.INSTANCE.IsWellKnownSid(value, wellKnownSidType);
} finally {
Kernel32Util.freeLocalMemory(value.getPointer());
}
}
use of com.sun.jna.platform.win32.Kernel32 in project jna by java-native-access.
the class Kernel32NamedPipeTest method testNamedPipeServerAPI.
@Test
public void testNamedPipeServerAPI() {
String pipeName = "\\\\.\\pipe\\" + getCurrentTestName();
HANDLE hNamedPipe = Kernel32.INSTANCE.CreateNamedPipe(pipeName, // dwOpenMode
WinBase.PIPE_ACCESS_DUPLEX, // dwPipeMode
WinBase.PIPE_TYPE_BYTE | WinBase.PIPE_READMODE_BYTE | WinBase.PIPE_WAIT, // nMaxInstances,
1, // nOutBufferSize,
Byte.MAX_VALUE, // nInBufferSize,
Byte.MAX_VALUE, // nDefaultTimeOut,
1000, // lpSecurityAttributes
null);
assertCallSucceeded("CreateNamedPipe", !WinBase.INVALID_HANDLE_VALUE.equals(hNamedPipe));
// NOTE: we don't really care what the returned values are only that the call succeeds
try {
IntByReference lpFlags = new IntByReference(0);
IntByReference lpOutBuffferSize = new IntByReference(0);
IntByReference lpInBufferSize = new IntByReference(0);
IntByReference lpMaxInstances = new IntByReference(0);
assertCallSucceeded("GetNamedPipeInfo", Kernel32.INSTANCE.GetNamedPipeInfo(hNamedPipe, lpFlags, lpOutBuffferSize, lpInBufferSize, lpMaxInstances));
ULONGByReference ServerProcessId = new ULONGByReference();
assertCallSucceeded("GetNamedPipeServerProcessId", Kernel32.INSTANCE.GetNamedPipeServerProcessId(hNamedPipe, ServerProcessId));
ULONGByReference ServerSessionId = new ULONGByReference();
assertCallSucceeded("GetNamedPipeServerSessionId", Kernel32.INSTANCE.GetNamedPipeServerSessionId(hNamedPipe, ServerSessionId));
assertCallSucceeded("DisconnectNamedPipe", Kernel32.INSTANCE.DisconnectNamedPipe(hNamedPipe));
} finally {
// clean up
assertCallSucceeded("Named pipe handle close", Kernel32.INSTANCE.CloseHandle(hNamedPipe));
}
}
use of com.sun.jna.platform.win32.Kernel32 in project jna by java-native-access.
the class Kernel32NamedPipeTest method testMultiThreadedNamedPipe.
@Test
public void testMultiThreadedNamedPipe() {
final String pipeName = "\\\\.\\pipe\\" + getCurrentTestName();
final Logger logger = Logger.getLogger(getClass().getName());
final int MAX_BUFFER_SIZE = 1024;
ExecutorService executors = Executors.newFixedThreadPool(2);
try {
Future<?> server = executors.submit(new Runnable() {
@Override
public void run() {
// based on https://msdn.microsoft.com/en-us/library/windows/desktop/aa365588(v=vs.85).aspx
HANDLE hNamedPipe = assertValidHandle("CreateNamedPipe", Kernel32.INSTANCE.CreateNamedPipe(pipeName, // dwOpenMode
WinBase.PIPE_ACCESS_DUPLEX, // dwPipeMode
WinBase.PIPE_TYPE_MESSAGE | WinBase.PIPE_READMODE_MESSAGE | WinBase.PIPE_WAIT, // nMaxInstances,
1, // nOutBufferSize,
MAX_BUFFER_SIZE, // nInBufferSize,
MAX_BUFFER_SIZE, // nDefaultTimeOut,
(int) TimeUnit.SECONDS.toMillis(30L), // lpSecurityAttributes
null));
try {
logger.info("Await client connection");
assertCallSucceeded("ConnectNamedPipe", Kernel32.INSTANCE.ConnectNamedPipe(hNamedPipe, null));
logger.info("Client connected");
byte[] readBuffer = new byte[MAX_BUFFER_SIZE];
IntByReference lpNumberOfBytesRead = new IntByReference(0);
assertCallSucceeded("ReadFile", Kernel32.INSTANCE.ReadFile(hNamedPipe, readBuffer, readBuffer.length, lpNumberOfBytesRead, null));
int readSize = lpNumberOfBytesRead.getValue();
logger.info("Received client data - length=" + readSize);
assertTrue("No data receieved from client", readSize > 0);
IntByReference lpNumberOfBytesWritten = new IntByReference(0);
assertCallSucceeded("WriteFile", Kernel32.INSTANCE.WriteFile(hNamedPipe, readBuffer, readSize, lpNumberOfBytesWritten, null));
logger.info("Echoed client data - length=" + lpNumberOfBytesWritten.getValue());
assertEquals("Mismatched write buffer size", readSize, lpNumberOfBytesWritten.getValue());
// Flush the pipe to allow the client to read the pipe's contents before disconnecting
assertCallSucceeded("FlushFileBuffers", Kernel32.INSTANCE.FlushFileBuffers(hNamedPipe));
logger.info("Disconnecting");
assertCallSucceeded("DisconnectNamedPipe", Kernel32.INSTANCE.DisconnectNamedPipe(hNamedPipe));
logger.info("Disconnected");
} finally {
// clean up
assertCallSucceeded("Named pipe handle close", Kernel32.INSTANCE.CloseHandle(hNamedPipe));
}
}
});
logger.info("Started server - handle=" + server);
Future<?> client = executors.submit(new Runnable() {
@Override
public void run() {
// based on https://msdn.microsoft.com/en-us/library/windows/desktop/aa365592(v=vs.85).aspx
assertCallSucceeded("WaitNamedPipe", Kernel32.INSTANCE.WaitNamedPipe(pipeName, (int) TimeUnit.SECONDS.toMillis(15L)));
logger.info("Connected to server");
HANDLE hPipe = assertValidHandle("CreateNamedPipe", Kernel32.INSTANCE.CreateFile(pipeName, WinNT.GENERIC_READ | WinNT.GENERIC_WRITE, // no sharing
0, // default security attributes
null, // opens existing pipe
WinNT.OPEN_EXISTING, // default attributes
0, // no template file
null));
try {
IntByReference lpMode = new IntByReference(WinBase.PIPE_READMODE_MESSAGE);
assertCallSucceeded("SetNamedPipeHandleState", Kernel32.INSTANCE.SetNamedPipeHandleState(hPipe, lpMode, null, null));
String expMessage = Thread.currentThread().getName() + " says hello";
byte[] expData = expMessage.getBytes();
IntByReference lpNumberOfBytesWritten = new IntByReference(0);
assertCallSucceeded("WriteFile", Kernel32.INSTANCE.WriteFile(hPipe, expData, expData.length, lpNumberOfBytesWritten, null));
logger.info("Sent hello message");
assertEquals("Mismatched write buffer size", expData.length, lpNumberOfBytesWritten.getValue());
byte[] readBuffer = new byte[MAX_BUFFER_SIZE];
IntByReference lpNumberOfBytesRead = new IntByReference(0);
assertCallSucceeded("ReadFile", Kernel32.INSTANCE.ReadFile(hPipe, readBuffer, readBuffer.length, lpNumberOfBytesRead, null));
int readSize = lpNumberOfBytesRead.getValue();
logger.info("Received server data - length=" + readSize);
assertTrue("No data receieved from server", readSize > 0);
String actMessage = new String(readBuffer, 0, readSize);
assertEquals("Mismatched server data", expMessage, actMessage);
} finally {
// clean up
assertCallSucceeded("Named pipe handle close", Kernel32.INSTANCE.CloseHandle(hPipe));
}
}
});
logger.info("Started client - handle=" + client);
for (Future<?> f : Arrays.asList(client, server)) {
try {
f.get(30L, TimeUnit.SECONDS);
logger.info("Finished " + f);
} catch (Exception e) {
logger.warning(e.getClass().getSimpleName() + " while await completion of " + f + ": " + e.getMessage());
}
}
} finally {
executors.shutdownNow();
}
}
use of com.sun.jna.platform.win32.Kernel32 in project jna by java-native-access.
the class Kernel32Test method testGetPrivateProfileSection.
public final void testGetPrivateProfileSection() throws IOException {
final File tmp = File.createTempFile("testGetPrivateProfileSection", ".ini");
tmp.deleteOnExit();
final PrintWriter writer = new PrintWriter(new BufferedWriter(new FileWriter(tmp)));
try {
writer.println("[X]");
writer.println("A=1");
writer.println("B=X");
} finally {
writer.close();
}
final char[] buffer = new char[9];
final DWORD len = Kernel32.INSTANCE.GetPrivateProfileSection("X", buffer, new DWORD(buffer.length), tmp.getCanonicalPath());
assertEquals(len.intValue(), 7);
assertEquals(new String(buffer), "A=1\0B=X\0\0");
}
Aggregations