Swaps isBase64 with internal implementation for Android compatibility (#1079)

* Adding animal sniffer to display all issues related to android compatibility.

* Duplicate isBase64 internally

Co-authored-by: Vitor Pamplona <vitor@vitorpamplona.com>
Co-authored-by: dotasek <david.otasek@smilecdr.com>
This commit is contained in:
dotasek 2023-01-18 18:40:24 -05:00 committed by GitHub
parent 2c60f5b5e3
commit f58b7acfb5
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
8 changed files with 73 additions and 6 deletions

View File

@ -99,7 +99,7 @@ public class Base64BinaryType extends PrimitiveType<byte[]> {
* @throws DataFormatException
*/
public void checkValidBase64(String toCheck) throws DataFormatException {
if (!Base64.isBase64(toCheck.getBytes())) {
if (!org.hl7.fhir.utilities.Base64.isBase64(toCheck.getBytes())) {
throw new DataFormatException("");
}
}

View File

@ -100,7 +100,7 @@ public class Base64BinaryType extends PrimitiveType<byte[]> {
* @throws DataFormatException
*/
public void checkValidBase64(String toCheck) throws DataFormatException {
if (!Base64.isBase64(toCheck.getBytes())) {
if (!org.hl7.fhir.utilities.Base64.isBase64(toCheck.getBytes())) {
throw new DataFormatException("");
}
}

View File

@ -152,7 +152,7 @@ public class Base64BinaryType extends PrimitiveType<byte[]> implements IPrimitiv
* @throws DataFormatException
*/
public void checkValidBase64(String toCheck) throws DataFormatException {
if (!Base64.isBase64(toCheck.getBytes())) {
if (!org.hl7.fhir.utilities.Base64.isBase64(toCheck.getBytes())) {
throw new DataFormatException("");
}
}

View File

@ -152,7 +152,7 @@ public class Base64BinaryType extends PrimitiveType<byte[]> implements IPrimitiv
* @throws DataFormatException
*/
public void checkValidBase64(String toCheck) throws DataFormatException {
if (!Base64.isBase64(toCheck.getBytes())) {
if (!org.hl7.fhir.utilities.Base64.isBase64(toCheck.getBytes())) {
throw new DataFormatException("");
}
}

View File

@ -156,7 +156,7 @@ public class Base64BinaryType extends PrimitiveType<byte[]> implements IPrimitiv
* @throws DataFormatException
*/
public void checkValidBase64(String toCheck) throws DataFormatException {
if (!Base64.isBase64(toCheck.getBytes())) {
if (!org.hl7.fhir.utilities.Base64.isBase64(toCheck.getBytes())) {
throw new DataFormatException("");
}
}

View File

@ -156,7 +156,7 @@ public class Base64BinaryType extends PrimitiveType<byte[]> implements IPrimitiv
* @throws DataFormatException
*/
public void checkValidBase64(String toCheck) throws DataFormatException {
if (!Base64.isBase64(toCheck.getBytes())) {
if (!org.hl7.fhir.utilities.Base64.isBase64(toCheck.getBytes())) {
throw new DataFormatException("");
}
}

View File

@ -0,0 +1,40 @@
package org.hl7.fhir.utilities;
/**
* This is a partial duplication of org.apache.commons.codec.binary.Base64
*
* It exists because Android compatibility only supports version 1.2 of that
* library, which only has the deprecated isArrayByteBase64. The use of
* isBase64 from this class will allow us to avoid using a deprecated method
* or hacking a solution that involves catching exceptions on decoding.
*/
public class Base64 {
private static final byte[] DECODE_TABLE = new byte[]{-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 62, -1, 62, -1, 63, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, -1, -1, -1, -1, -1, -1, -1, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, -1, -1, -1, -1, 63, -1, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51};
public static boolean isBase64(byte octet) {
return octet == 61 || octet >= 0 && octet < DECODE_TABLE.length && DECODE_TABLE[octet] != -1;
}
public static boolean isBase64(byte[] arrayOctet) {
for(int i = 0; i < arrayOctet.length; ++i) {
if (!isBase64(arrayOctet[i]) && !isWhiteSpace(arrayOctet[i])) {
return false;
}
}
return true;
}
protected static boolean isWhiteSpace(byte byteToCheck) {
switch (byteToCheck) {
case 9:
case 10:
case 13:
case 32:
return true;
default:
return false;
}
}
}

View File

@ -0,0 +1,27 @@
import org.junit.jupiter.api.Test;
import org.hl7.fhir.utilities.Base64;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
public class Base64Tests {
@Test
public void testIsArrayByteBase64() {
assertFalse(Base64.isBase64(new byte[] { Byte.MIN_VALUE }));
assertFalse(Base64.isBase64(new byte[] { -125 }));
assertFalse(Base64.isBase64(new byte[] { -10 }));
assertFalse(Base64.isBase64(new byte[] { 0 }));
assertFalse(Base64.isBase64(new byte[] { 64, Byte.MAX_VALUE }));
assertFalse(Base64.isBase64(new byte[] { Byte.MAX_VALUE }));
assertTrue(Base64.isBase64(new byte[] { 'A' }));
assertFalse(Base64.isBase64(new byte[] { 'A', Byte.MIN_VALUE }));
assertTrue(Base64.isBase64(new byte[] { 'A', 'Z', 'a' }));
assertTrue(Base64.isBase64(new byte[] { '/', '=', '+' }));
assertFalse(Base64.isBase64(new byte[] { '$' }));
}
}