Skip to content

Commit

Permalink
fix (#2105)
Browse files Browse the repository at this point in the history
  • Loading branch information
lilgreenbird committed Apr 5, 2023
1 parent 17625f2 commit 180c97d
Showing 1 changed file with 29 additions and 13 deletions.
42 changes: 29 additions & 13 deletions src/main/java/com/microsoft/sqlserver/jdbc/AuthenticationJNI.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.nio.file.Files;
import java.nio.file.Paths;
Expand Down Expand Up @@ -51,15 +50,17 @@ static int getMaxSSPIBlobSize() {

static {
UnsatisfiedLinkError temp = null;
// Load the DLL

// Load the DLL in java.library.path
try {
System.loadLibrary(SQLServerDriver.AUTH_DLL_NAME);
int[] pkg = new int[1];

if (0 == SNISecInitPackage(pkg, authLogger)) {
sspiBlobMaxlen = pkg[0];
} else {
MessageFormat form = new MessageFormat(SQLServerException.getErrString("R_UnableToLoadAuthDllManually"));
MessageFormat form = new MessageFormat(
SQLServerException.getErrString("R_UnableToLoadAuthDllManually"));
throw new UnsatisfiedLinkError(form.format(new Object[] {SQLServerDriver.AUTH_DLL_NAME}));
}
isDllLoaded = true;
Expand All @@ -71,36 +72,51 @@ static int getMaxSSPIBlobSize() {

try {
Files.createDirectories(Paths.get(tempDirectory));
if (authLogger.isLoggable(Level.FINEST)) {
authLogger.finest("DLL extracted to: " + outputDLL.getAbsolutePath());
}

try (InputStream is = AuthenticationJNI.class.getResourceAsStream(
"/" + SQLServerDriver.DLL_NAME + "." + Util.getJVMArchOnWindows() + ".dll")) {

try (FileOutputStream fos = new FileOutputStream(outputDLL)) {
if (is != null) {
int length = is.available();
byte[] buffer = new byte[length];
if ((length = is.read(buffer)) != -1) {
fos.write(buffer, 0, length);
}

// length may not be returned all at once so a loop is required
int pos = 0;
do {
int b = is.read(buffer, pos, length - pos);
if (b == -1) {
break;
} else {
fos.write(buffer, pos, b);
pos += b;
}
} while (pos < length);
} else {
throw new UnsatisfiedLinkError(
SQLServerException.getErrString("R_UnableToLoadPackagedAuthDll"));
}
}
}

System.load(outputDLL.getAbsolutePath());

int[] pkg = new int[1];

if (0 == SNISecInitPackage(pkg, authLogger)) {
sspiBlobMaxlen = pkg[0];
} else {
throw new UnsatisfiedLinkError(SQLServerException.getErrString("R_UnableToLoadPackagedAuthDll"));
throw new UnsatisfiedLinkError(
SQLServerException.getErrString("R_UnableToLoadPackagedAuthDll"));
}

isDllLoaded = true;

} catch (UnsatisfiedLinkError e) {
temp = e; // R_UnableToLoadPackagedAuthDll
} catch (IOException ioe) {
temp = new UnsatisfiedLinkError(ioe.getMessage());
} catch (UnsatisfiedLinkError ule) {
temp = ule; // R_UnableToLoadPackagedAuthDll
} catch (Exception e) {
temp = new UnsatisfiedLinkError(e.getMessage());
} finally {
Runtime.getRuntime().addShutdownHook(cleanup(tempDirectory));
}
Expand Down

0 comments on commit 180c97d

Please sign in to comment.