Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 9 additions & 2 deletions api/src/main/java/jakarta/xml/bind/DatatypeConverterImpl.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
/*
* Copyright (c) 2007, 2024 Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2025 Contributors to the Eclipse Foundation. All rights reserved.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Distribution License v. 1.0, which is available at
Expand Down Expand Up @@ -711,8 +712,8 @@ private static int guessLength(String text) {
* because JIT can inline a lot of string access (with data of 1K chars, it was twice as fast)
*/
public static byte[] _parseBase64Binary(String text) {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there a reason to not use the JDK-built in Base64 class? (avail since: 1.8)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I should take a look but maybe base64 in jdk is more strict than here

if (null == text || text.length() % 4 != 0) {
throw new IllegalArgumentException("base64 text invalid.");
if (null == text) {
throw new IllegalArgumentException("base64 \"null\" text invalid.");
}
final int buflen = guessLength(text);
final byte[] out = new byte[buflen];
Expand All @@ -738,9 +739,15 @@ public static byte[] _parseBase64Binary(String text) {
// quadruplet is now filled.
out[o++] = (byte) ((quadruplet[0] << 2) | (quadruplet[1] >> 4));
if (quadruplet[2] != PADDING) {
if (buflen == o) {
throw new IllegalArgumentException("base64 text invalid.");
}
out[o++] = (byte) ((quadruplet[1] << 4) | (quadruplet[2] >> 2));
}
if (quadruplet[3] != PADDING) {
if (buflen == o) {
throw new IllegalArgumentException("base64 text invalid.");
}
out[o++] = (byte) ((quadruplet[2] << 6) | (quadruplet[3]));
}
q = 0;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
/*
* Copyright (c) 2023, 2024 Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2025 Contributors to the Eclipse Foundation. All rights reserved.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Distribution License v. 1.0, which is available at
Expand Down Expand Up @@ -84,6 +85,8 @@ public void testPrint() {
@Test
public void testBase64() {
Assert.assertThrows(IllegalArgumentException.class, () -> DatatypeConverter.parseBase64Binary("Qxx=="));
Assert.assertThrows(IllegalArgumentException.class, () -> DatatypeConverter.parseBase64Binary("SGVsbG8sIJdvcmxkIQQxx=="));
Assert.assertThrows(IllegalArgumentException.class, () -> DatatypeConverter.parseBase64Binary("dGhpcyBpcyB\nhbiBleGFtcGxl=="));

Assert.assertEquals("", new String(DatatypeConverter.parseBase64Binary("")));
Assert.assertEquals("f", new String(DatatypeConverter.parseBase64Binary("Zg==")));
Expand All @@ -92,6 +95,9 @@ public void testBase64() {
Assert.assertEquals("foob", new String(DatatypeConverter.parseBase64Binary("Zm9vYg==")));
Assert.assertEquals("fooba", new String(DatatypeConverter.parseBase64Binary("Zm9vYmE=")));
Assert.assertEquals("foobar", new String(DatatypeConverter.parseBase64Binary("Zm9vYmFy")));
Assert.assertEquals("this is an example", new String(DatatypeConverter.parseBase64Binary("dGhpcyBpcyB hbiBleGFtcGxl")));
Assert.assertEquals("this is an example", new String(DatatypeConverter.parseBase64Binary("dGhpcyBpcyB\nhbiBleGFtcGxl")));
Assert.assertEquals("this is an example", new String(DatatypeConverter.parseBase64Binary("dGhpcyBpcyB\thbiBleGFtcGxl")));

Assert.assertNotEquals("Hello, world!", new String(DatatypeConverter.parseBase64Binary("SGVsbG8sIJdvcmxkIQ==")));

Expand Down