commons-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From ggreg...@apache.org
Subject cvs commit: jakarta-commons/codec/src/test/org/apache/commons/codec/binary HexTest.java
Date Sun, 18 Apr 2004 18:22:33 GMT
ggregory    2004/04/18 11:22:33

  Modified:    codec/src/java/org/apache/commons/codec/binary Hex.java
               codec/src/test/org/apache/commons/codec/binary HexTest.java
  Log:
  Bugzilla Bug 28455: Hex converts illegal characters to 255.
  
  Revision  Changes    Path
  1.13      +44 -25    jakarta-commons/codec/src/java/org/apache/commons/codec/binary/Hex.java
  
  Index: Hex.java
  ===================================================================
  RCS file: /home/cvs/jakarta-commons/codec/src/java/org/apache/commons/codec/binary/Hex.java,v
  retrieving revision 1.12
  retrieving revision 1.13
  diff -u -r1.12 -r1.13
  --- Hex.java	29 Feb 2004 04:08:31 -0000	1.12
  +++ Hex.java	18 Apr 2004 18:22:33 -0000	1.13
  @@ -24,6 +24,7 @@
   /**
    * Hex encoder and decoder.
    * 
  + * @since 1.1
    * @author Apache Software Foundation
    * @version $Id$
    */
  @@ -32,7 +33,7 @@
       /** 
        * Used building output as Hex 
        */
  -    private static char[] digits = {
  +    private static final char[] DIGITS = {
           '0', '1', '2', '3', '4', '5', '6', '7',
              '8', '9', 'a', 'b', 'c', 'd', 'e', 'f'
       };
  @@ -47,36 +48,54 @@
        * @param data An array of characters containing hexidecimal digits
        * @return A byte array containing binary data decoded from
        *         the supplied char array.
  -     * @throws DecoderException Thrown if an odd number of characters is supplied
  -     *                   to this function
  +     * @throws DecoderException Thrown if an odd number or illegal of characters 
  +     *         is supplied
        */
       public static byte[] decodeHex(char[] data) throws DecoderException {
   
  -        int l = data.length;
  -
  -           if ((l & 0x01) != 0) {
  -               throw new DecoderException("Odd number of characters.");
  -           }
  +        int len = data.length;
   
  -           byte[] out = new byte[l >> 1];
  +        if ((len & 0x01) != 0) {
  +            throw new DecoderException("Odd number of characters.");
  +        }
  +
  +        byte[] out = new byte[len >> 1];
  +
  +        // two characters form the hex value.
  +        for (int i = 0, j = 0; j < len; i++) {
  +            int f = toDigit(data[j], j) << 4;
  +            j++;
  +            f = f | toDigit(data[j], j);
  +            j++;
  +            out[i] = (byte) (f & 0xFF);
  +        }
   
  -           // two characters form the hex value.
  -           for (int i = 0, j = 0; j < l; i++) {
  -               int f = Character.digit(data[j++], 16) << 4;
  -               f = f | Character.digit(data[j++], 16);
  -               out[i] = (byte) (f & 0xFF);
  -           }
  +        return out;
  +    }
   
  -           return out;
  +    /**
  +     * Converts a hexadecimal character to an integer.
  +     *  
  +     * @param ch A character to convert to an integer digit
  +     * @param index The index of the character in the source
  +     * @return An integer
  +     * @throws DecoderException Thrown if ch is an illegal hex character
  +     */
  +    protected static int toDigit(char ch, int index) throws DecoderException {
  +        int digit = Character.digit(ch, 16);
  +        if (digit == -1) {
  +            throw new DecoderException("Illegal hexadecimal charcter " + ch + " at index
" + index);
  +        }
  +        return digit;
       }
   
       /**
  -     * Converts an array of bytes into an array of characters representing the
  -     * hexidecimal values of each byte in order. The returned array will be
  -     * double the length of the passed array, as it takes two characters to
  -     * represent any given byte.
  -     *
  -     * @param data a byte[] to convert to Hex characters
  +     * Converts an array of bytes into an array of characters representing the hexidecimal
values of each byte in order.
  +     * The returned array will be double the length of the passed array, as it takes two
characters to represent any
  +     * given byte.
  +     * 
  +     * @param data
  +     *                  a byte[] to convert to Hex characters
        * @return A char[] containing hexidecimal characters
        */
       public static char[] encodeHex(byte[] data) {
  @@ -87,8 +106,8 @@
   
              // two characters form the hex value.
              for (int i = 0, j = 0; i < l; i++) {
  -               out[j++] = digits[(0xF0 & data[i]) >>> 4 ];
  -               out[j++] = digits[ 0x0F & data[i] ];
  +               out[j++] = DIGITS[(0xF0 & data[i]) >>> 4 ];
  +               out[j++] = DIGITS[ 0x0F & data[i] ];
              }
   
              return out;
  
  
  
  1.10      +21 -1     jakarta-commons/codec/src/test/org/apache/commons/codec/binary/HexTest.java
  
  Index: HexTest.java
  ===================================================================
  RCS file: /home/cvs/jakarta-commons/codec/src/test/org/apache/commons/codec/binary/HexTest.java,v
  retrieving revision 1.9
  retrieving revision 1.10
  diff -u -r1.9 -r1.10
  --- HexTest.java	17 Mar 2004 19:28:37 -0000	1.9
  +++ HexTest.java	18 Apr 2004 18:22:33 -0000	1.10
  @@ -46,6 +46,26 @@
           }
       }
   
  +    public void testDecodeBadCharacterPos0() {
  +        try {
  +            new Hex().decode("q0");
  +            fail("An exception wasn't thrown when trying to decode an illegal character");
  +        }
  +        catch (DecoderException e) {
  +            // Expected exception
  +        }
  +    }
  +
  +    public void testDecodeBadCharacterPos1() {
  +        try {
  +            new Hex().decode("0q");
  +            fail("An exception wasn't thrown when trying to decode an illegal character");
  +        }
  +        catch (DecoderException e) {
  +            // Expected exception
  +        }
  +    }
  +
       public void testDecodeClassCastException() {
           try {
               new Hex().decode(new int[] { 65 });
  
  
  

---------------------------------------------------------------------
To unsubscribe, e-mail: commons-dev-unsubscribe@jakarta.apache.org
For additional commands, e-mail: commons-dev-help@jakarta.apache.org


Mime
View raw message