Skip to content

Commit

Permalink
Swaps isBase64 with internal implementation for Android compatibility (
Browse files Browse the repository at this point in the history
…#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>
  • Loading branch information
3 people committed Jan 18, 2023
1 parent 2c60f5b commit f58b7ac
Show file tree
Hide file tree
Showing 8 changed files with 73 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ public String fhirType() {
* @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("");
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ public String fhirType() {
* @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("");
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@ public boolean isEmpty() {
* @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("");
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@ public boolean isEmpty() {
* @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("");
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,7 @@ public String primitiveValue() {
* @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("");
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,7 @@ public String primitiveValue() {
* @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("");
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
}
}
}
27 changes: 27 additions & 0 deletions org.hl7.fhir.utilities/src/test/java/Base64Tests.java
Original file line number Diff line number Diff line change
@@ -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[] { '$' }));
}
}

0 comments on commit f58b7ac

Please sign in to comment.