01: /* ===========================================================================
02: * $RCSfile: Tools.java,v $
03: * ===========================================================================
04: *
05: * RetroGuard -- an obfuscation package for Java classfiles.
06: *
07: * Copyright (c) 1999 Mark Welsh (markw@retrologic.com)
08: *
09: * This library is free software; you can redistribute it and/or
10: * modify it under the terms of the GNU Lesser General Public
11: * License as published by the Free Software Foundation; either
12: * version 2 of the License, or (at your option) any later version.
13: *
14: * This library is distributed in the hope that it will be useful,
15: * but WITHOUT ANY WARRANTY; without even the implied warranty of
16: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17: * Lesser General Public License for more details.
18: *
19: * You should have received a copy of the GNU Lesser General Public
20: * License along with this library; if not, write to the Free Software
21: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22: *
23: * The author may be contacted at markw@retrologic.com
24: *
25: *
26: * $Date: 2002/10/16 08:14:35 $
27: * $Revision: 1.1.1.1 $
28: */
29: package com.yworks.yguard.obf;
30:
31: import java.io.*;
32: import java.util.*;
33:
34: /**
35: * A Tools class containing generally useful, miscellaneous static methods.
36: *
37: * @author Mark Welsh
38: */
39: public class Tools {
40: // Constants -------------------------------------------------------------
41:
42: // Fields ----------------------------------------------------------------
43:
44: // Class Methods ---------------------------------------------------------
45: /**
46: * Is the string one of the ones in the array?
47: */
48: public static boolean isInArray(String s, String[] list) {
49: for (int i = 0; i < list.length; i++)
50: if (s.equals(list[i]))
51: return true;
52: return false;
53: }
54:
55: /** Encode a byte[] as a Base64 (see RFC1521, Section 5.2) String. */
56: private static final char[] base64 = { 'A', 'B', 'C', 'D', 'E',
57: 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q',
58: 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', 'a', 'b', 'c',
59: 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o',
60: 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', '0',
61: '1', '2', '3', '4', '5', '6', '7', '8', '9', '+', '/' };
62: private static final char pad = '=';
63:
64: public static String toBase64(byte[] b) {
65: StringBuffer sb = new StringBuffer();
66: for (int ptr = 0; ptr < b.length; ptr += 3) {
67: sb.append(base64[(b[ptr] >> 2) & 0x3F]);
68: if (ptr + 1 < b.length) {
69: sb.append(base64[((b[ptr] << 4) & 0x30)
70: | ((b[ptr + 1] >> 4) & 0x0F)]);
71: if (ptr + 2 < b.length) {
72: sb.append(base64[((b[ptr + 1] << 2) & 0x3C)
73: | ((b[ptr + 2] >> 6) & 0x03)]);
74: sb.append(base64[b[ptr + 2] & 0x3F]);
75: } else {
76: sb.append(base64[(b[ptr + 1] << 2) & 0x3C]);
77: sb.append(pad);
78: }
79: } else {
80: sb.append(base64[((b[ptr] << 4) & 0x30)]);
81: sb.append(pad);
82: sb.append(pad);
83: }
84: }
85: return sb.toString();
86: }
87: }
|