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

Subversion Repositories openrisc

[/] [openrisc/] [trunk/] [gnu-dev/] [or1k-gcc/] [libjava/] [classpath/] [gnu/] [java/] [security/] [x509/] [Util.java] - Blame information for rev 769

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 769 jeremybenn
/* Util.java -- Miscellaneous utility methods.
2
   Copyright (C) 2004, 2006  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
import gnu.java.lang.CPStringBuilder;
42
 
43
/**
44
 * A collection of useful class methods.
45
 *
46
 * @author Casey Marshall (rsdio@metastatic.org)
47
 */
48
public final class Util
49
{
50
 
51
  // Constants.
52
  // -------------------------------------------------------------------------
53
 
54
  public static final String HEX = "0123456789abcdef";
55
 
56
  // Class methods.
57
  // -------------------------------------------------------------------------
58
 
59
  /**
60
   * Convert a byte array to a hexadecimal string, as though it were a
61
   * big-endian arbitrarily-sized integer.
62
   *
63
   * @param buf The bytes to format.
64
   * @param off The offset to start at.
65
   * @param len The number of bytes to format.
66
   * @return A hexadecimal representation of the specified bytes.
67
   */
68
  public static String toHexString(byte[] buf, int off, int len)
69
  {
70
    CPStringBuilder str = new CPStringBuilder();
71
    for (int i = 0; i < len; i++)
72
      {
73
        str.append(HEX.charAt(buf[i+off] >>> 4 & 0x0F));
74
        str.append(HEX.charAt(buf[i+off] & 0x0F));
75
      }
76
    return str.toString();
77
  }
78
 
79
  /**
80
   * See {@link #toHexString(byte[],int,int)}.
81
   */
82
  public static String toHexString(byte[] buf)
83
  {
84
    return Util.toHexString(buf, 0, buf.length);
85
  }
86
 
87
  /**
88
   * Convert a byte array to a hexadecimal string, separating octets
89
   * with the given character.
90
   *
91
   * @param buf The bytes to format.
92
   * @param off The offset to start at.
93
   * @param len The number of bytes to format.
94
   * @param sep The character to insert between octets.
95
   * @return A hexadecimal representation of the specified bytes.
96
   */
97
  public static String toHexString(byte[] buf, int off, int len, char sep)
98
  {
99
    CPStringBuilder str = new CPStringBuilder();
100
    for (int i = 0; i < len; i++)
101
      {
102
        str.append(HEX.charAt(buf[i+off] >>> 4 & 0x0F));
103
        str.append(HEX.charAt(buf[i+off] & 0x0F));
104
        if (i < len - 1)
105
          str.append(sep);
106
      }
107
    return str.toString();
108
  }
109
 
110
  /**
111
   * See {@link #toHexString(byte[],int,int,char)}.
112
   */
113
  public static String toHexString(byte[] buf, char sep)
114
  {
115
    return Util.toHexString(buf, 0, buf.length, sep);
116
  }
117
 
118
  /**
119
   * Create a representation of the given byte array similar to the
120
   * output of `hexdump -C', which is
121
   *
122
   * <p><pre>OFFSET  SIXTEEN-BYTES-IN-HEX  PRINTABLE-BYTES</pre>
123
   *
124
   * <p>The printable bytes show up as-is if they are printable and
125
   * not a newline character, otherwise showing as '.'.
126
   *
127
   * @param buf The bytes to format.
128
   * @param off The offset to start at.
129
   * @param len The number of bytes to encode.
130
   * @return The formatted string.
131
   */
132
  public static String hexDump(byte[] buf, int off, int len, String prefix)
133
  {
134
    String nl = System.getProperty("line.separator");
135
    CPStringBuilder str = new CPStringBuilder();
136
    int i = 0;
137
    while (i < len)
138
      {
139
        str.append(prefix);
140
        str.append(Util.formatInt(i+off, 16, 8));
141
        str.append("  ");
142
        String s = Util.toHexString(buf, i+off, Math.min(16, len-i), ' ');
143
        str.append(s);
144
        for (int j = 56 - (56 - s.length()); j < 56; j++)
145
          str.append(" ");
146
        for (int j = 0; j < Math.min(16, len - i); j++)
147
          {
148
            if ((buf[i+off+j] & 0xFF) < 0x20 || (buf[i+off+j] & 0xFF) > 0x7E)
149
              str.append('.');
150
            else
151
              str.append((char) (buf[i+off+j] & 0xFF));
152
          }
153
        str.append(nl);
154
        i += 16;
155
      }
156
    return str.toString();
157
  }
158
 
159
  /**
160
   * See {@link #hexDump(byte[],int,int,String)}.
161
   */
162
  public static String hexDump(byte[] buf, String prefix)
163
  {
164
    return hexDump(buf, 0, buf.length, prefix);
165
  }
166
 
167
  /**
168
   * Format an integer into the specified radix, zero-filled.
169
   *
170
   * @param i The integer to format.
171
   * @param radix The radix to encode to.
172
   * @param len The target length of the string. The string is
173
   *   zero-padded to this length, but may be longer.
174
   * @return The formatted integer.
175
   */
176
  public static String formatInt(int i, int radix, int len)
177
  {
178
    String s = Integer.toString(i, radix);
179
    CPStringBuilder buf = new CPStringBuilder();
180
    for (int j = 0; j < len - s.length(); j++)
181
      buf.append("0");
182
    buf.append(s);
183
    return buf.toString();
184
  }
185
 
186
  /**
187
   * Convert a hexadecimal string into its byte representation.
188
   *
189
   * @param hex The hexadecimal string.
190
   * @return The converted bytes.
191
   */
192
  public static byte[] toByteArray(String hex)
193
  {
194
    hex = hex.toLowerCase();
195
    byte[] buf = new byte[hex.length() / 2];
196
    int j = 0;
197
    for (int i = 0; i < buf.length; i++)
198
      {
199
        buf[i] = (byte) ((Character.digit(hex.charAt(j++), 16) << 4) |
200
                          Character.digit(hex.charAt(j++), 16));
201
      }
202
    return buf;
203
  }
204
}

powered by: WebSVN 2.1.0

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