From commits-return-14100-archive-asf-public=cust-asf.ponee.io@pdfbox.apache.org Thu Jan 24 17:43:58 2019 Return-Path: X-Original-To: archive-asf-public@cust-asf.ponee.io Delivered-To: archive-asf-public@cust-asf.ponee.io Received: from mail.apache.org (hermes.apache.org [140.211.11.3]) by mx-eu-01.ponee.io (Postfix) with SMTP id 5255D18062C for ; Thu, 24 Jan 2019 17:43:58 +0100 (CET) Received: (qmail 41008 invoked by uid 500); 24 Jan 2019 16:43:52 -0000 Mailing-List: contact commits-help@pdfbox.apache.org; run by ezmlm Precedence: bulk List-Help: List-Unsubscribe: List-Post: List-Id: Reply-To: dev@pdfbox.apache.org Delivered-To: mailing list commits@pdfbox.apache.org Received: (qmail 40999 invoked by uid 99); 24 Jan 2019 16:43:52 -0000 Received: from Unknown (HELO svn01-us-west.apache.org) (209.188.14.144) by apache.org (qpsmtpd/0.29) with ESMTP; Thu, 24 Jan 2019 16:43:52 +0000 Received: from svn01-us-west.apache.org (localhost [127.0.0.1]) by svn01-us-west.apache.org (ASF Mail Server at svn01-us-west.apache.org) with ESMTP id CD4EF3A0563 for ; Thu, 24 Jan 2019 16:43:51 +0000 (UTC) Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit Subject: svn commit: r1852045 - /pdfbox/branches/2.0/pdfbox/src/main/java/org/apache/pdfbox/util/Hex.java Date: Thu, 24 Jan 2019 16:43:51 -0000 To: commits@pdfbox.apache.org From: tilman@apache.org X-Mailer: svnmailer-1.0.9 Message-Id: <20190124164351.CD4EF3A0563@svn01-us-west.apache.org> Author: tilman Date: Thu Jan 24 16:43:51 2019 New Revision: 1852045 URL: http://svn.apache.org/viewvc?rev=1852045&view=rev Log: PDFBOX-4437: decode ASCII Hex and base64 Modified: pdfbox/branches/2.0/pdfbox/src/main/java/org/apache/pdfbox/util/Hex.java Modified: pdfbox/branches/2.0/pdfbox/src/main/java/org/apache/pdfbox/util/Hex.java URL: http://svn.apache.org/viewvc/pdfbox/branches/2.0/pdfbox/src/main/java/org/apache/pdfbox/util/Hex.java?rev=1852045&r1=1852044&r2=1852045&view=diff ============================================================================== --- pdfbox/branches/2.0/pdfbox/src/main/java/org/apache/pdfbox/util/Hex.java (original) +++ pdfbox/branches/2.0/pdfbox/src/main/java/org/apache/pdfbox/util/Hex.java Thu Jan 24 16:43:51 2019 @@ -17,8 +17,12 @@ package org.apache.pdfbox.util; +import java.io.ByteArrayOutputStream; import java.io.IOException; import java.io.OutputStream; +import java.lang.reflect.Method; +import org.apache.commons.logging.Log; +import org.apache.commons.logging.LogFactory; /** * Utility functions for hex encoding. @@ -27,6 +31,8 @@ import java.io.OutputStream; */ public final class Hex { + private static final Log LOG = LogFactory.getLog(Hex.class); + /** * for hex conversion. * @@ -173,4 +179,78 @@ public final class Hex { return b & 0x0F; } + + /** + * Decode a base64 String. + * + * @param base64Value a base64 encoded String. + * + * @return the decoded String as a byte array. + * + * @throws IllegalArgumentException if this isn't a base64 encoded string. + */ + public static byte[] decodeBase64(String base64Value) throws IllegalArgumentException + { + // https://stackoverflow.com/questions/469695/decode-base64-data-in-java + try + { + // jdk8 and higher? java.util.Base64.getDecoder().decode() + Class b64Class = Class.forName("java.util.Base64"); + Method getDecoderMethod = b64Class.getMethod("getDecoder"); + Object base64Decoder = getDecoderMethod.invoke(b64Class); + Method decodeMethod = base64Decoder.getClass().getMethod("decode", String.class); + return (byte[]) decodeMethod.invoke(base64Decoder, base64Value.replaceAll("\\s", "")); + } + catch (ReflectiveOperationException ex) + { + LOG.debug(ex); + } + catch (SecurityException ex) + { + LOG.debug(ex); + } + try + { + // up to java7? javax.xml.bind.DatatypeConverter.parseBase64Binary() + Class datatypeConverterClass = Class.forName("javax.xml.bind.DatatypeConverter"); + Method parseBase64BinaryMethod = datatypeConverterClass.getMethod("parseBase64Binary", String.class); + return (byte[]) parseBase64BinaryMethod.invoke(null, base64Value); + } + catch (ReflectiveOperationException ex) + { + LOG.debug(ex); + } + catch (SecurityException ex) + { + LOG.debug(ex); + } + LOG.error("Can't decode base64 value, try adding javax.xml.bind:jaxb-api to your build"); + return new byte[0]; + } + + /** + * Decodes a hex String into a byte array. + * + * @param s A String with ASCII hex. + * @return decoded byte array. + * @throws IOException + */ + public static byte[] decodeHex(String s) throws IOException + { + ByteArrayOutputStream baos = new ByteArrayOutputStream(); + for (int i = 0; i < s.length() - 1; i += 2) + { + String hexByte = s.substring(i, i + 2); + try + { + baos.write(Integer.parseInt(hexByte, 16)); // Byte.parseByte won't work with "9C" + } + catch (NumberFormatException ex) + { + LOG.error("Can't parse " + hexByte + ", aborting decode", ex); + break; + } + } + return baos.toByteArray(); + } }