1 /*
   2  * Copyright (c) 1999, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.  Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 /*
  26  * COMPONENT_NAME: idl.parser
  27  *
  28  * ORIGINS: 27
  29  *
  30  * Licensed Materials - Property of IBM
  31  * 5639-D57 (C) COPYRIGHT International Business Machines Corp. 1997, 1999
  32  * RMI-IIOP v1.0
  33  *
  34  */
  35 
  36 package com.sun.tools.corba.se.idl;
  37 
  38 // NOTES:
  39 
  40 import java.security.MessageDigest;
  41 import java.util.Hashtable;
  42 
  43 /**
  44  *
  45  **/
  46 public class ValueRepositoryId
  47 {
  48   private MessageDigest sha;       // Message digest used to compute SHA-1
  49   private int           index;     // Current index in the 'logical' sequence
  50   private Hashtable     types;     // Already processed types
  51   private String        hashcode;  // The computed hashcode
  52 
  53   public ValueRepositoryId ()
  54   {
  55     try
  56     {
  57       sha = MessageDigest.getInstance ("SHA-1");
  58     }
  59     catch (Exception exception)
  60     {}
  61     index    = 0;
  62     types    = new Hashtable ();
  63     hashcode = null;
  64   } // ctor
  65 
  66   /**Add a value to the hashcode being computed.
  67      @param value the value to be added to the value RepositoryID. */
  68   public void addValue (int value)
  69   {
  70     sha.update ((byte)((value >> 24) & 0x0F));
  71     sha.update ((byte)((value >> 16) & 0x0F));
  72     sha.update ((byte)((value >>  8) & 0x0F));
  73     sha.update ((byte)(value & 0x0F));
  74     index++;
  75   } // addValue
  76 
  77   /** Add a type to the list of types which have already been included.
  78       Note that the type should be added prior to its value.
  79       @param entry the type to be added to the value RepositoryID. */
  80   public void addType (SymtabEntry entry)
  81   {
  82     types.put (entry, new Integer (index));
  83   }
  84 
  85   /** Check to see if a specified type has already been processed. If so,
  86       add the appropriate 'previously processed' code (0xFFFFFFFF) and
  87       sequence offset, and return false; otherwise add the symbol table entry
  88       and current offset to the hashtable and return false.
  89       @param entry the type to be checked
  90       @return true if the symbol table entry has not been previously added;
  91        and false otherwise. */
  92   public boolean isNewType (SymtabEntry entry)
  93   {
  94     Object index = types.get (entry);
  95     if (index == null)
  96     {
  97       addType (entry);
  98       return true;
  99     }
 100     addValue (0xFFFFFFFF);
 101     addValue (((Integer)index).intValue ());
 102     return false;
 103   } // isNewType
 104 
 105   /** Get the hashcode computed for the value type. This method MUST not be
 106       called until all fields have been added, since it computes the hash
 107       code from the values entered for each field.
 108       @return the 64 bit hashcode for the value type represented as a
 109        16 character hexadecimal string. */
 110   public String getHashcode ()
 111   {
 112     if (hashcode == null)
 113     {
 114       byte [] digest = sha.digest ();
 115       hashcode = hexOf (digest[0]) + hexOf (digest[1]) +
 116                  hexOf (digest[2]) + hexOf (digest[3]) +
 117                  hexOf (digest[4]) + hexOf (digest[5]) +
 118                  hexOf (digest[6]) + hexOf (digest[7]);
 119     }
 120     return hashcode;
 121   } // getHashCode
 122 
 123   // Convert a byte to a two character hex string:
 124   private static String hexOf (byte value)
 125   {
 126     int d1 = (value >> 4) & 0x0F;
 127     int d2 = value & 0x0F;
 128     return "0123456789ABCDEF".substring (d1, d1 + 1) +
 129            "0123456789ABCDEF".substring (d2, d2 + 1);
 130   } // hexOf
 131 } // class ValueRepositoryId