From b1698ee33926e44885f5d24b7aa53805b17e0e3f Mon Sep 17 00:00:00 2001 From: Maarten Billemont Date: Mon, 25 Jun 2018 12:25:17 -0400 Subject: [PATCH 1/3] C type fixes. --- platform-independent/c/core/src/mpw-util.c | 10 +++++----- platform-independent/c/core/src/mpw-util.h | 2 +- 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/platform-independent/c/core/src/mpw-util.c b/platform-independent/c/core/src/mpw-util.c index 96cf284e..e103e65d 100644 --- a/platform-independent/c/core/src/mpw-util.c +++ b/platform-independent/c/core/src/mpw-util.c @@ -66,9 +66,9 @@ const char **mpw_strings(size_t *count, const char *strings, ...) { va_list args; va_start( args, strings ); - char **array = NULL; + const char **array = NULL; size_t arraySize = 0; - for (const char *string; string = va_arg( args, const char * );) { + for (const char *string; (string = va_arg( args, const char * ));) { size_t cursor = arraySize; if (!mpw_realloc( &array, &arraySize, sizeof(string) )) { mpw_free( &array, arraySize ); @@ -311,7 +311,7 @@ static uint8_t const *mpw_aes(bool encrypt, const uint8_t *key, const size_t key AES_CBC_encrypt_buffer( resultBuf, aesBuf, aesSize, key, iv ); else AES_CBC_decrypt_buffer( resultBuf, aesBuf, aesSize, key, iv ); - mpw_free( aesBuf, aesSize ); + mpw_free( &aesBuf, aesSize ); // Truncate PKCS#7 padding if (encrypt) @@ -516,7 +516,7 @@ char *mpw_strndup(const char *src, size_t max) { return dst; } -int *mpw_strncasecmp(const char *s1, const char *s2, size_t max) { +int mpw_strncasecmp(const char *s1, const char *s2, size_t max) { if (s1 && s2 && max) for (; --max > 0; ++s1, ++s2) { @@ -526,4 +526,4 @@ int *mpw_strncasecmp(const char *s1, const char *s2, size_t max) { } return 0; -} \ No newline at end of file +} diff --git a/platform-independent/c/core/src/mpw-util.h b/platform-independent/c/core/src/mpw-util.h index a2d19baf..bf95b6cb 100644 --- a/platform-independent/c/core/src/mpw-util.h +++ b/platform-independent/c/core/src/mpw-util.h @@ -216,6 +216,6 @@ char *mpw_strdup(const char *src); /** Drop-in for POSIX strndup(3). */ char *mpw_strndup(const char *src, size_t max); /** Drop-in for POSIX strncasecmp(3). */ -int *mpw_strncasecmp(const char *s1, const char *s2, size_t max); +int mpw_strncasecmp(const char *s1, const char *s2, size_t max); #endif // _MPW_UTIL_H From b5040a7786456ee85e8322b7f81ca9325fa353f6 Mon Sep 17 00:00:00 2001 From: Maarten Billemont Date: Mon, 25 Jun 2018 12:25:38 -0400 Subject: [PATCH 2/3] Harmonize standardized platform naming between Native class and gradle. --- gradle/.idea/misc.xml | 24 +++++++++++++++ .../gradle/wrapper/gradle-wrapper.properties | 3 +- platform-independent/c/core/build.gradle | 24 ++++++++++----- .../lyndir/masterpassword/impl/Native.java | 29 ++++++++++++++++--- platform-independent/java/model/build.gradle | 4 +-- platform-independent/java/tests/build.gradle | 8 ++--- 6 files changed, 73 insertions(+), 19 deletions(-) diff --git a/gradle/.idea/misc.xml b/gradle/.idea/misc.xml index fdae1d0d..88b86d0a 100644 --- a/gradle/.idea/misc.xml +++ b/gradle/.idea/misc.xml @@ -1,5 +1,29 @@ + + + + diff --git a/gradle/gradle/wrapper/gradle-wrapper.properties b/gradle/gradle/wrapper/gradle-wrapper.properties index 49df2ab4..16d28051 100644 --- a/gradle/gradle/wrapper/gradle-wrapper.properties +++ b/gradle/gradle/wrapper/gradle-wrapper.properties @@ -1,6 +1,5 @@ -#Fri Jun 22 01:12:28 EDT 2018 distributionBase=GRADLE_USER_HOME distributionPath=wrapper/dists +distributionUrl=https\://services.gradle.org/distributions/gradle-4.7-bin.zip zipStoreBase=GRADLE_USER_HOME zipStorePath=wrapper/dists -distributionUrl=https\://services.gradle.org/distributions/gradle-4.7-all.zip diff --git a/platform-independent/c/core/build.gradle b/platform-independent/c/core/build.gradle index 4de67c83..5b97e0dd 100644 --- a/platform-independent/c/core/build.gradle +++ b/platform-independent/c/core/build.gradle @@ -79,18 +79,28 @@ library { static String standardOperatingSystem(NativePlatform platform) { OperatingSystem os = platform.getOperatingSystem() - if (os.isWindows()) { + if (os.isWindows()) return OperatingSystemFamily.WINDOWS - } else if (os.isLinux()) { - return OperatingSystemFamily.LINUX - } else if (os.isMacOsX()) { + else if (os.isMacOsX()) return OperatingSystemFamily.MAC_OS - } + else if (os.isLinux()) + return OperatingSystemFamily.LINUX - return os.name.toLowerCase() + // Other systems will need to use a Linux compatibility layer for now. + return OperatingSystemFamily.LINUX } static String standardArchitecture(NativePlatform platform) { Architecture arch = platform.getArchitecture() - return arch.name.toLowerCase().replaceAll( "-", "_" ) + if (arch.isArm()) + return "arm" + else if (arch.name.toLowerCase( Locale.ROOT ).startsWith( "arm" )) + return "arm64" + else if (arch.isAmd64()) + return "x86_64" + else if (arch.isI386()) + return "x86" + + // Other systems will need to be compatible with the x86 architecture. + return "x86" } diff --git a/platform-independent/java/algorithm/src/main/java/com/lyndir/masterpassword/impl/Native.java b/platform-independent/java/algorithm/src/main/java/com/lyndir/masterpassword/impl/Native.java index 1d69f4c8..63c036ea 100644 --- a/platform-independent/java/algorithm/src/main/java/com/lyndir/masterpassword/impl/Native.java +++ b/platform-independent/java/algorithm/src/main/java/com/lyndir/masterpassword/impl/Native.java @@ -18,12 +18,16 @@ package com.lyndir.masterpassword.impl; +import static com.lyndir.lhunath.opal.system.util.ObjectUtils.*; + import com.google.common.base.Joiner; +import com.google.common.collect.ImmutableList; import com.google.common.io.ByteStreams; import com.lyndir.lhunath.opal.system.logging.Logger; import java.io.*; import java.util.Locale; import javax.annotation.Nonnull; +import javax.annotation.Nullable; /** @@ -45,7 +49,8 @@ public final class Native { try { System.loadLibrary( name ); return; - } catch (@SuppressWarnings("ErrorNotRethrown") final UnsatisfiedLinkError ignored) { + } + catch (@SuppressWarnings("ErrorNotRethrown") final UnsatisfiedLinkError ignored) { } // Try to find and open a stream to the packaged library resource. @@ -82,10 +87,26 @@ public final class Native { @Nonnull private static String getLibraryResource(final String library) { - String system = System.getProperty( "os.name" ).toLowerCase( Locale.ROOT ); - String architecture = System.getProperty( "os.arch" ).toLowerCase( Locale.ROOT ); - if ("Mac OS X".equalsIgnoreCase( system )) + String system = ifNotNullElse( System.getProperty( "os.name" ), "linux" ).toLowerCase( Locale.ROOT ); + String architecture = ifNotNullElse( System.getProperty( "os.arch" ), "x86" ).toLowerCase( Locale.ROOT ); + + // Standardize system naming in accordance with masterpassword-core. + if (system.contains( "windows" )) + system = "windows"; + else if (system.contains( "mac os x" ) || system.contains( "darwin" ) || system.contains( "osx" )) system = "macos"; + else + system = "linux"; + + // Standardize architecture naming in accordance with masterpassword-core. + if (ImmutableList.of( "arm", "arm-v7", "armv7", "arm32" ).contains( architecture )) + architecture = "arm"; + else if (architecture.startsWith( "arm" )) + architecture = "arm64"; + else if (ImmutableList.of( "x86_64", "amd64", "x64", "x86-64" ).contains( architecture )) + architecture = "x86_64"; + else + architecture = "x86"; return Joiner.on( RESOURCE_SEPARATOR ).join( "", NATIVES_PATH, system, architecture, library ); } diff --git a/platform-independent/java/model/build.gradle b/platform-independent/java/model/build.gradle index 8e1584bc..a2b00d23 100644 --- a/platform-independent/java/model/build.gradle +++ b/platform-independent/java/model/build.gradle @@ -18,7 +18,7 @@ dependencies { compileOnly group: 'com.google.auto.value', name: 'auto-value', version: '1.2' apt group: 'com.google.auto.value', name: 'auto-value', version: '1.2' - testCompile group: 'org.testng', name: 'testng', version: '6.8.5' - testCompile group: 'ch.qos.logback', name: 'logback-classic', version: '1.1.2' + testImplementation group: 'org.testng', name: 'testng', version: '6.8.5' + testImplementation group: 'ch.qos.logback', name: 'logback-classic', version: '1.1.2' } test.useTestNG() diff --git a/platform-independent/java/tests/build.gradle b/platform-independent/java/tests/build.gradle index 9737d6a9..3f26e7b1 100644 --- a/platform-independent/java/tests/build.gradle +++ b/platform-independent/java/tests/build.gradle @@ -7,11 +7,11 @@ description = 'Master Password Test Suite' dependencies { implementation group: 'com.lyndir.lhunath.opal', name: 'opal-system', version: '1.7-p2' - compile project( ':masterpassword-algorithm' ) - compile project( ':masterpassword-model' ) + implementation project( ':masterpassword-algorithm' ) + implementation project( ':masterpassword-model' ) - testCompile group: 'org.testng', name: 'testng', version: '6.8.5' - testCompile group: 'ch.qos.logback', name: 'logback-classic', version: '1.1.2' + testImplementation group: 'org.testng', name: 'testng', version: '6.8.5' + testImplementation group: 'ch.qos.logback', name: 'logback-classic', version: '1.1.2' } //tasks.withType(Test) { // systemProperty "java.library.path", "/Users/lhunath/Documents/workspace/lyndir/MasterPassword/core/java/algorithm/build/libs/mpw/shared" From 42d78da74e3311fcf4e2239faafdaf8c7dfe71de Mon Sep 17 00:00:00 2001 From: Maarten Billemont Date: Mon, 25 Jun 2018 13:19:26 -0400 Subject: [PATCH 3/3] Fix some bugs in the new mpw_strings & mpw_strncasecmp. --- platform-independent/c/core/src/mpw-util.c | 23 +++++++++++----------- platform-independent/c/core/src/mpw-util.h | 4 ++-- 2 files changed, 13 insertions(+), 14 deletions(-) diff --git a/platform-independent/c/core/src/mpw-util.c b/platform-independent/c/core/src/mpw-util.c index e103e65d..55cbf05a 100644 --- a/platform-independent/c/core/src/mpw-util.c +++ b/platform-independent/c/core/src/mpw-util.c @@ -67,17 +67,19 @@ const char **mpw_strings(size_t *count, const char *strings, ...) { va_list args; va_start( args, strings ); const char **array = NULL; - size_t arraySize = 0; - for (const char *string; (string = va_arg( args, const char * ));) { - size_t cursor = arraySize; - if (!mpw_realloc( &array, &arraySize, sizeof(string) )) { - mpw_free( &array, arraySize ); + size_t size = 0; + for (const char *string = strings; string; (string = va_arg( args, const char * ))) { + size_t cursor = size / sizeof( *array ); + if (!mpw_realloc( &array, &size, sizeof( string ) )) { + mpw_free( &array, size ); + *count = 0; return NULL; } array[cursor] = string; } va_end( args ); + *count = size / sizeof( *array ); return array; } @@ -518,12 +520,9 @@ char *mpw_strndup(const char *src, size_t max) { int mpw_strncasecmp(const char *s1, const char *s2, size_t max) { - if (s1 && s2 && max) - for (; --max > 0; ++s1, ++s2) { - int cmp = tolower( *(unsigned char *)s1 ) - tolower( *(unsigned char *)s2 ); - if (!cmp || *s1 == '\0') - return cmp; - } + int cmp = 0; + for (; !cmp && max-- > 0 && s1 && s2; ++s1, ++s2) + cmp = tolower( *(unsigned char *)s1 ) - tolower( *(unsigned char *)s2 ); - return 0; + return cmp; } diff --git a/platform-independent/c/core/src/mpw-util.h b/platform-independent/c/core/src/mpw-util.h index bf95b6cb..eb8a3c8b 100644 --- a/platform-independent/c/core/src/mpw-util.h +++ b/platform-independent/c/core/src/mpw-util.h @@ -118,9 +118,9 @@ bool mpw_push_int( /** Reallocate the given buffer from the given size by adding the delta size. * On success, the buffer size pointer will be updated to the buffer's new size * and the buffer pointer may be updated to a new memory address. - * On failure, the buffer and pointers will remain unaffected. + * On failure, the pointers will remain unaffected. * @param buffer A pointer to the buffer to reallocate. - * @param bufferSize A pointer to the buffer's actual size. + * @param bufferSize A pointer to the buffer's current size. * @param deltaSize The amount to increase the buffer's size by. * @return true if successful, false if reallocation failed. */