OpenCores
URL https://opencores.org/ocsvn/scarts/scarts/trunk

Subversion Repositories scarts

[/] [scarts/] [trunk/] [toolchain/] [scarts-gcc/] [gcc-4.1.1/] [libjava/] [classpath/] [gnu/] [java/] [security/] [x509/] [Util.java] - Blame information for rev 14

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 14 jlechner
/* Util.java -- Miscellaneous utility methods.
2
   Copyright (C) 2004  Free Software Foundation, Inc.
3
 
4
This file is part of GNU Classpath.
5
 
6
GNU Classpath is free software; you can redistribute it and/or modify
7
it under the terms of the GNU General Public License as published by
8
the Free Software Foundation; either version 2, or (at your option)
9
any later version.
10
 
11
GNU Classpath is distributed in the hope that it will be useful, but
12
WITHOUT ANY WARRANTY; without even the implied warranty of
13
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14
General Public License for more details.
15
 
16
You should have received a copy of the GNU General Public License
17
along with GNU Classpath; see the file COPYING.  If not, write to the
18
Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
19
02110-1301 USA.
20
 
21
Linking this library statically or dynamically with other modules is
22
making a combined work based on this library.  Thus, the terms and
23
conditions of the GNU General Public License cover the whole
24
combination.
25
 
26
As a special exception, the copyright holders of this library give you
27
permission to link this library with independent modules to produce an
28
executable, regardless of the license terms of these independent
29
modules, and to copy and distribute the resulting executable under
30
terms of your choice, provided that you also meet, for each linked
31
independent module, the terms and conditions of the license of that
32
module.  An independent module is a module which is not derived from
33
or based on this library.  If you modify this library, you may extend
34
this exception to your version of the library, but you are not
35
obligated to do so.  If you do not wish to do so, delete this
36
exception statement from your version. */
37
 
38
 
39
package gnu.java.security.x509;
40
 
41
/**
42
 * A collection of useful class methods.
43
 *
44
 * @author Casey Marshall (rsdio@metastatic.org)
45
 */
46
public final class Util
47
{
48
 
49
  // Constants.
50
  // -------------------------------------------------------------------------
51
 
52
  public static final String HEX = "0123456789abcdef";
53
 
54
  // Class methods.
55
  // -------------------------------------------------------------------------
56
 
57
  /**
58
   * Convert a byte array to a hexadecimal string, as though it were a
59
   * big-endian arbitrarily-sized integer.
60
   *
61
   * @param buf The bytes to format.
62
   * @param off The offset to start at.
63
   * @param len The number of bytes to format.
64
   * @return A hexadecimal representation of the specified bytes.
65
   */
66
  public static String toHexString(byte[] buf, int off, int len)
67
  {
68
    StringBuffer str = new StringBuffer();
69
    for (int i = 0; i < len; i++)
70
      {
71
        str.append(HEX.charAt(buf[i+off] >>> 4 & 0x0F));
72
        str.append(HEX.charAt(buf[i+off] & 0x0F));
73
      }
74
    return str.toString();
75
  }
76
 
77
  /**
78
   * See {@link #toHexString(byte[],int,int)}.
79
   */
80
  public static String toHexString(byte[] buf)
81
  {
82
    return Util.toHexString(buf, 0, buf.length);
83
  }
84
 
85
  /**
86
   * Convert a byte array to a hexadecimal string, separating octets
87
   * with the given character.
88
   *
89
   * @param buf The bytes to format.
90
   * @param off The offset to start at.
91
   * @param len The number of bytes to format.
92
   * @param sep The character to insert between octets.
93
   * @return A hexadecimal representation of the specified bytes.
94
   */
95
  public static String toHexString(byte[] buf, int off, int len, char sep)
96
  {
97
    StringBuffer str = new StringBuffer();
98
    for (int i = 0; i < len; i++)
99
      {
100
        str.append(HEX.charAt(buf[i+off] >>> 4 & 0x0F));
101
        str.append(HEX.charAt(buf[i+off] & 0x0F));
102
        if (i < len - 1)
103
          str.append(sep);
104
      }
105
    return str.toString();
106
  }
107
 
108
  /**
109
   * See {@link #toHexString(byte[],int,int,char)}.
110
   */
111
  public static String toHexString(byte[] buf, char sep)
112
  {
113
    return Util.toHexString(buf, 0, buf.length, sep);
114
  }
115
 
116
  /**
117
   * Create a representation of the given byte array similar to the
118
   * output of `hexdump -C', which is
119
   *
120
   * <p><pre>OFFSET  SIXTEEN-BYTES-IN-HEX  PRINTABLE-BYTES</pre>
121
   *
122
   * <p>The printable bytes show up as-is if they are printable and
123
   * not a newline character, otherwise showing as '.'.
124
   *
125
   * @param buf The bytes to format.
126
   * @param off The offset to start at.
127
   * @param len The number of bytes to encode.
128
   * @return The formatted string.
129
   */
130
  public static String hexDump(byte[] buf, int off, int len, String prefix)
131
  {
132
    String nl = System.getProperty("line.separator");
133
    StringBuffer str = new StringBuffer();
134
    int i = 0;
135
    while (i < len)
136
      {
137
        str.append(prefix);
138
        str.append(Util.formatInt(i+off, 16, 8));
139
        str.append("  ");
140
        String s = Util.toHexString(buf, i+off, Math.min(16, len-i), ' ');
141
        str.append(s);
142
        for (int j = 56 - (56 - s.length()); j < 56; j++)
143
          str.append(" ");
144
        for (int j = 0; j < Math.min(16, len - i); j++)
145
          {
146
            if ((buf[i+off+j] & 0xFF) < 0x20 || (buf[i+off+j] & 0xFF) > 0x7E)
147
              str.append('.');
148
            else
149
              str.append((char) (buf[i+off+j] & 0xFF));
150
          }
151
        str.append(nl);
152
        i += 16;
153
      }
154
    return str.toString();
155
  }
156
 
157
  /**
158
   * See {@link #hexDump(byte[],int,int)}.
159
   */
160
  public static String hexDump(byte[] buf, String prefix)
161
  {
162
    return hexDump(buf, 0, buf.length, prefix);
163
  }
164
 
165
  /**
166
   * Format an integer into the specified radix, zero-filled.
167
   *
168
   * @param i The integer to format.
169
   * @param radix The radix to encode to.
170
   * @param len The target length of the string. The string is
171
   *   zero-padded to this length, but may be longer.
172
   * @return The formatted integer.
173
   */
174
  public static String formatInt(int i, int radix, int len)
175
  {
176
    String s = Integer.toString(i, radix);
177
    StringBuffer buf = new StringBuffer();
178
    for (int j = 0; j < len - s.length(); j++)
179
      buf.append("0");
180
    buf.append(s);
181
    return buf.toString();
182
  }
183
 
184
  /**
185
   * Convert a hexadecimal string into its byte representation.
186
   *
187
   * @param hex The hexadecimal string.
188
   * @return The converted bytes.
189
   */
190
  public static byte[] toByteArray(String hex)
191
  {
192
    hex = hex.toLowerCase();
193
    byte[] buf = new byte[hex.length() / 2];
194
    int j = 0;
195
    for (int i = 0; i < buf.length; i++)
196
      {
197
        buf[i] = (byte) ((Character.digit(hex.charAt(j++), 16) << 4) |
198
                          Character.digit(hex.charAt(j++), 16));
199
      }
200
    return buf;
201
  }
202
}

powered by: WebSVN 2.1.0

© copyright 1999-2025 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.