1 /*
   2  * reserved comment block
   3  * DO NOT REMOVE OR ALTER!
   4  */
   5 /*
   6  * Licensed to the Apache Software Foundation (ASF) under one or more
   7  * contributor license agreements.  See the NOTICE file distributed with
   8  * this work for additional information regarding copyright ownership.
   9  * The ASF licenses this file to You under the Apache License, Version 2.0
  10  * (the "License"); you may not use this file except in compliance with
  11  * the License.  You may obtain a copy of the License at
  12  *
  13  *      http://www.apache.org/licenses/LICENSE-2.0
  14  *
  15  * Unless required by applicable law or agreed to in writing, software
  16  * distributed under the License is distributed on an "AS IS" BASIS,
  17  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  18  * See the License for the specific language governing permissions and
  19  * limitations under the License.
  20  */
  21 
  22 package com.sun.org.apache.xerces.internal.impl.dv.util;
  23 
  24 /**
  25  * format validation
  26  *
  27  * This class encodes/decodes hexadecimal data
  28  *
  29  * @xerces.internal
  30  *
  31  * @author Jeffrey Rodriguez
  32  */
  33 public final class  HexBin {
  34     static private final int  BASELENGTH   = 128;
  35     static private final int  LOOKUPLENGTH = 16;
  36     static final private byte [] hexNumberTable    = new byte[BASELENGTH];
  37     static final private char [] lookUpHexAlphabet = new char[LOOKUPLENGTH];
  38 
  39 
  40     static {
  41         for (int i = 0; i < BASELENGTH; i++ ) {
  42             hexNumberTable[i] = -1;
  43         }
  44         for ( int i = '9'; i >= '0'; i--) {
  45             hexNumberTable[i] = (byte) (i-'0');
  46         }
  47         for ( int i = 'F'; i>= 'A'; i--) {
  48             hexNumberTable[i] = (byte) ( i-'A' + 10 );
  49         }
  50         for ( int i = 'f'; i>= 'a'; i--) {
  51            hexNumberTable[i] = (byte) ( i-'a' + 10 );
  52         }
  53 
  54         for(int i = 0; i<10; i++ ) {
  55             lookUpHexAlphabet[i] = (char)('0'+i);
  56         }
  57         for(int i = 10; i<=15; i++ ) {
  58             lookUpHexAlphabet[i] = (char)('A'+i -10);
  59         }
  60     }
  61 
  62     /**
  63      * Encode a byte array to hex string
  64      *
  65      * @param binaryData array of byte to encode
  66      * @return return encoded string
  67      */
  68     static public String encode(byte[] binaryData) {
  69         if (binaryData == null)
  70             return null;
  71         int lengthData   = binaryData.length;
  72         int lengthEncode = lengthData * 2;
  73         char[] encodedData = new char[lengthEncode];
  74         int temp;
  75         for (int i = 0; i < lengthData; i++) {
  76             temp = binaryData[i];
  77             if (temp < 0)
  78                 temp += 256;
  79             encodedData[i*2] = lookUpHexAlphabet[temp >> 4];
  80             encodedData[i*2+1] = lookUpHexAlphabet[temp & 0xf];
  81         }
  82         return new String(encodedData);
  83     }
  84 
  85     /**
  86      * Decode hex string to a byte array
  87      *
  88      * @param encoded encoded string
  89      * @return return array of byte to encode
  90      */
  91     static public byte[] decode(String encoded) {
  92         if (encoded == null)
  93             return null;
  94         int lengthData = encoded.length();
  95         if (lengthData % 2 != 0)
  96             return null;
  97 
  98         char[] binaryData = encoded.toCharArray();
  99         int lengthDecode = lengthData / 2;
 100         byte[] decodedData = new byte[lengthDecode];
 101         byte temp1, temp2;
 102         char tempChar;
 103         for( int i = 0; i<lengthDecode; i++ ){
 104             tempChar = binaryData[i*2];
 105             temp1 = (tempChar < BASELENGTH) ? hexNumberTable[tempChar] : -1;
 106             if (temp1 == -1)
 107                 return null;
 108             tempChar = binaryData[i*2+1];
 109             temp2 = (tempChar < BASELENGTH) ? hexNumberTable[tempChar] : -1;
 110             if (temp2 == -1)
 111                 return null;
 112             decodedData[i] = (byte)((temp1 << 4) | temp2);
 113         }
 114         return decodedData;
 115     }
 116 }