001: /* -*-mode:java; c-basic-offset:2; indent-tabs-mode:nil -*- */
002: /*
003: Copyright (c) 2002-2008 ymnk, JCraft,Inc. All rights reserved.
004:
005: Redistribution and use in source and binary forms, with or without
006: modification, are permitted provided that the following conditions are met:
007:
008: 1. Redistributions of source code must retain the above copyright notice,
009: this list of conditions and the following disclaimer.
010:
011: 2. Redistributions in binary form must reproduce the above copyright
012: notice, this list of conditions and the following disclaimer in
013: the documentation and/or other materials provided with the distribution.
014:
015: 3. The names of the authors may not be used to endorse or promote products
016: derived from this software without specific prior written permission.
017:
018: THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES,
019: INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
020: FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL JCRAFT,
021: INC. OR ANY CONTRIBUTORS TO THIS SOFTWARE BE LIABLE FOR ANY DIRECT, INDIRECT,
022: INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
023: LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
024: OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
025: LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
026: NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
027: EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
028: */
029:
030: package com.jcraft.jsch;
031:
032: import java.net.Socket;
033:
034: class Util {
035:
036: private static final byte[] b64 = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/="
037: .getBytes();
038:
039: private static byte val(byte foo) {
040: if (foo == '=')
041: return 0;
042: for (int j = 0; j < b64.length; j++) {
043: if (foo == b64[j])
044: return (byte) j;
045: }
046: return 0;
047: }
048:
049: static byte[] fromBase64(byte[] buf, int start, int length) {
050: byte[] foo = new byte[length];
051: int j = 0;
052: for (int i = start; i < start + length; i += 4) {
053: foo[j] = (byte) ((val(buf[i]) << 2) | ((val(buf[i + 1]) & 0x30) >>> 4));
054: if (buf[i + 2] == (byte) '=') {
055: j++;
056: break;
057: }
058: foo[j + 1] = (byte) (((val(buf[i + 1]) & 0x0f) << 4) | ((val(buf[i + 2]) & 0x3c) >>> 2));
059: if (buf[i + 3] == (byte) '=') {
060: j += 2;
061: break;
062: }
063: foo[j + 2] = (byte) (((val(buf[i + 2]) & 0x03) << 6) | (val(buf[i + 3]) & 0x3f));
064: j += 3;
065: }
066: byte[] bar = new byte[j];
067: System.arraycopy(foo, 0, bar, 0, j);
068: return bar;
069: }
070:
071: static byte[] toBase64(byte[] buf, int start, int length) {
072:
073: byte[] tmp = new byte[length * 2];
074: int i, j, k;
075:
076: int foo = (length / 3) * 3 + start;
077: i = 0;
078: for (j = start; j < foo; j += 3) {
079: k = (buf[j] >>> 2) & 0x3f;
080: tmp[i++] = b64[k];
081: k = (buf[j] & 0x03) << 4 | (buf[j + 1] >>> 4) & 0x0f;
082: tmp[i++] = b64[k];
083: k = (buf[j + 1] & 0x0f) << 2 | (buf[j + 2] >>> 6) & 0x03;
084: tmp[i++] = b64[k];
085: k = buf[j + 2] & 0x3f;
086: tmp[i++] = b64[k];
087: }
088:
089: foo = (start + length) - foo;
090: if (foo == 1) {
091: k = (buf[j] >>> 2) & 0x3f;
092: tmp[i++] = b64[k];
093: k = ((buf[j] & 0x03) << 4) & 0x3f;
094: tmp[i++] = b64[k];
095: tmp[i++] = (byte) '=';
096: tmp[i++] = (byte) '=';
097: } else if (foo == 2) {
098: k = (buf[j] >>> 2) & 0x3f;
099: tmp[i++] = b64[k];
100: k = (buf[j] & 0x03) << 4 | (buf[j + 1] >>> 4) & 0x0f;
101: tmp[i++] = b64[k];
102: k = ((buf[j + 1] & 0x0f) << 2) & 0x3f;
103: tmp[i++] = b64[k];
104: tmp[i++] = (byte) '=';
105: }
106: byte[] bar = new byte[i];
107: System.arraycopy(tmp, 0, bar, 0, i);
108: return bar;
109:
110: // return sun.misc.BASE64Encoder().encode(buf);
111: }
112:
113: static String[] split(String foo, String split) {
114: if (foo == null)
115: return null;
116: byte[] buf = foo.getBytes();
117: java.util.Vector bar = new java.util.Vector();
118: int start = 0;
119: int index;
120: while (true) {
121: index = foo.indexOf(split, start);
122: if (index >= 0) {
123: bar.addElement(new String(buf, start, index - start));
124: start = index + 1;
125: continue;
126: }
127: bar.addElement(new String(buf, start, buf.length - start));
128: break;
129: }
130: String[] result = new String[bar.size()];
131: for (int i = 0; i < result.length; i++) {
132: result[i] = (String) (bar.elementAt(i));
133: }
134: return result;
135: }
136:
137: static boolean glob(byte[] pattern, byte[] name) {
138: return glob0(pattern, 0, name, 0);
139: }
140:
141: static private boolean glob0(byte[] pattern, int pattern_index,
142: byte[] name, int name_index) {
143: if (name.length > 0 && name[0] == '.') {
144: if (pattern.length > 0 && pattern[0] == '.') {
145: if (pattern.length == 2 && pattern[1] == '*')
146: return true;
147: return glob(pattern, pattern_index + 1, name,
148: name_index + 1);
149: }
150: return false;
151: }
152: return glob(pattern, pattern_index, name, name_index);
153: }
154:
155: static private boolean glob(byte[] pattern, int pattern_index,
156: byte[] name, int name_index) {
157: //System.err.println("glob: "+new String(pattern)+", "+pattern_index+" "+new String(name)+", "+name_index);
158:
159: int patternlen = pattern.length;
160: if (patternlen == 0)
161: return false;
162:
163: int namelen = name.length;
164: int i = pattern_index;
165: int j = name_index;
166:
167: while (i < patternlen && j < namelen) {
168: if (pattern[i] == '\\') {
169: if (i + 1 == patternlen)
170: return false;
171: i++;
172: if (pattern[i] != name[j])
173: return false;
174: i += skipUTF8Char(pattern[i]);
175: j += skipUTF8Char(name[j]);
176: continue;
177: }
178:
179: if (pattern[i] == '*') {
180: while (i < patternlen) {
181: if (pattern[i] == '*') {
182: i++;
183: continue;
184: }
185: break;
186: }
187: if (patternlen == i)
188: return true;
189:
190: byte foo = pattern[i];
191: if (foo == '?') {
192: while (j < namelen) {
193: if (glob(pattern, i, name, j)) {
194: return true;
195: }
196: j += skipUTF8Char(name[j]);
197: }
198: return false;
199: } else if (foo == '\\') {
200: if (i + 1 == patternlen)
201: return false;
202: i++;
203: foo = pattern[i];
204: while (j < namelen) {
205: if (foo == name[j]) {
206: if (glob(pattern, i + skipUTF8Char(foo),
207: name, j + skipUTF8Char(name[j]))) {
208: return true;
209: }
210: }
211: j += skipUTF8Char(name[j]);
212: }
213: return false;
214: }
215:
216: while (j < namelen) {
217: if (foo == name[j]) {
218: if (glob(pattern, i, name, j)) {
219: return true;
220: }
221: }
222: j += skipUTF8Char(name[j]);
223: }
224: return false;
225: }
226:
227: if (pattern[i] == '?') {
228: i++;
229: j += skipUTF8Char(name[j]);
230: continue;
231: }
232:
233: if (pattern[i] != name[j])
234: return false;
235:
236: i += skipUTF8Char(pattern[i]);
237: j += skipUTF8Char(name[j]);
238:
239: if (!(j < namelen)) { // name is end
240: if (!(i < patternlen)) { // pattern is end
241: return true;
242: }
243: if (pattern[i] == '*') {
244: break;
245: }
246: }
247: continue;
248: }
249:
250: if (i == patternlen && j == namelen)
251: return true;
252:
253: if (!(j < namelen) && // name is end
254: pattern[i] == '*') {
255: boolean ok = true;
256: while (i < patternlen) {
257: if (pattern[i++] != '*') {
258: ok = false;
259: break;
260: }
261: }
262: return ok;
263: }
264:
265: return false;
266: }
267:
268: static String quote(String path) {
269: byte[] _path = str2byte(path);
270: int count = 0;
271: for (int i = 0; i < _path.length; i++) {
272: byte b = _path[i];
273: if (b == '\\' || b == '?' || b == '*')
274: count++;
275: }
276: if (count == 0)
277: return path;
278: byte[] _path2 = new byte[_path.length + count];
279: for (int i = 0, j = 0; i < _path.length; i++) {
280: byte b = _path[i];
281: if (b == '\\' || b == '?' || b == '*') {
282: _path2[j++] = '\\';
283: }
284: _path2[j++] = b;
285: }
286: return byte2str(_path2);
287: }
288:
289: static String unquote(String path) {
290: byte[] foo = str2byte(path);
291: byte[] bar = unquote(foo);
292: if (foo.length == bar.length)
293: return path;
294: return byte2str(bar);
295: }
296:
297: static byte[] unquote(byte[] path) {
298: int pathlen = path.length;
299: int i = 0;
300: while (i < pathlen) {
301: if (path[i] == '\\') {
302: if (i + 1 == pathlen)
303: break;
304: System.arraycopy(path, i + 1, path, i, path.length
305: - (i + 1));
306: pathlen--;
307: i++;
308: continue;
309: }
310: i++;
311: }
312: if (pathlen == path.length)
313: return path;
314: byte[] foo = new byte[pathlen];
315: System.arraycopy(path, 0, foo, 0, pathlen);
316: return foo;
317: }
318:
319: private static String[] chars = { "0", "1", "2", "3", "4", "5",
320: "6", "7", "8", "9", "a", "b", "c", "d", "e", "f" };
321:
322: static String getFingerPrint(HASH hash, byte[] data) {
323: try {
324: hash.init();
325: hash.update(data, 0, data.length);
326: byte[] foo = hash.digest();
327: StringBuffer sb = new StringBuffer();
328: int bar;
329: for (int i = 0; i < foo.length; i++) {
330: bar = foo[i] & 0xff;
331: sb.append(chars[(bar >>> 4) & 0xf]);
332: sb.append(chars[(bar) & 0xf]);
333: if (i + 1 < foo.length)
334: sb.append(":");
335: }
336: return sb.toString();
337: } catch (Exception e) {
338: return "???";
339: }
340: }
341:
342: static boolean array_equals(byte[] foo, byte bar[]) {
343: int i = foo.length;
344: if (i != bar.length)
345: return false;
346: for (int j = 0; j < i; j++) {
347: if (foo[j] != bar[j])
348: return false;
349: }
350: //try{while(true){i--; if(foo[i]!=bar[i])return false;}}catch(Exception e){}
351: return true;
352: }
353:
354: static Socket createSocket(String host, int port, int timeout)
355: throws JSchException {
356: Socket socket = null;
357: if (timeout == 0) {
358: try {
359: socket = new Socket(host, port);
360: return socket;
361: } catch (Exception e) {
362: String message = e.toString();
363: if (e instanceof Throwable)
364: throw new JSchException(message, (Throwable) e);
365: throw new JSchException(message);
366: }
367: }
368: final String _host = host;
369: final int _port = port;
370: final Socket[] sockp = new Socket[1];
371: final Exception[] ee = new Exception[1];
372: String message = "";
373: Thread tmp = new Thread(new Runnable() {
374: public void run() {
375: sockp[0] = null;
376: try {
377: sockp[0] = new Socket(_host, _port);
378: } catch (Exception e) {
379: ee[0] = e;
380: if (sockp[0] != null && sockp[0].isConnected()) {
381: try {
382: sockp[0].close();
383: } catch (Exception eee) {
384: }
385: }
386: sockp[0] = null;
387: }
388: }
389: });
390: tmp.setName("Opening Socket " + host);
391: tmp.start();
392: try {
393: tmp.join(timeout);
394: message = "timeout: ";
395: } catch (java.lang.InterruptedException eee) {
396: }
397: if (sockp[0] != null && sockp[0].isConnected()) {
398: socket = sockp[0];
399: } else {
400: message += "socket is not established";
401: if (ee[0] != null) {
402: message = ee[0].toString();
403: }
404: tmp.interrupt();
405: tmp = null;
406: throw new JSchException(message);
407: }
408: return socket;
409: }
410:
411: static byte[] str2byte(String str, String encoding) {
412: if (str == null)
413: return null;
414: try {
415: return str.getBytes(encoding);
416: } catch (java.io.UnsupportedEncodingException e) {
417: return str.getBytes();
418: }
419: }
420:
421: static byte[] str2byte(String str) {
422: return str2byte(str, "UTF-8");
423: }
424:
425: static String byte2str(byte[] str, String encoding) {
426: try {
427: return new String(str, encoding);
428: } catch (java.io.UnsupportedEncodingException e) {
429: return new String(str);
430: }
431: }
432:
433: static String byte2str(byte[] str) {
434: return byte2str(str, "UTF-8");
435: }
436:
437: /*
438: static byte[] char2byte(char[] foo){
439: int len=0;
440: for(int i=0; i<foo.length; i++){
441: if((foo[i]&0xff00)==0) len++;
442: else len+=2;
443: }
444: byte[] bar=new byte[len];
445: for(int i=0, j=0; i<foo.length; i++){
446: if((foo[i]&0xff00)==0){
447: bar[j++]=(byte)foo[i];
448: }
449: else{
450: bar[j++]=(byte)(foo[i]>>>8);
451: bar[j++]=(byte)foo[i];
452: }
453: }
454: return bar;
455: }
456: */
457: static void bzero(byte[] foo) {
458: if (foo == null)
459: return;
460: for (int i = 0; i < foo.length; i++)
461: foo[i] = 0;
462: }
463:
464: static String diffString(String str, String[] not_available) {
465: String[] stra = Util.split(str, ",");
466: String result = null;
467: loop: for (int i = 0; i < stra.length; i++) {
468: for (int j = 0; j < not_available.length; j++) {
469: if (stra[i].equals(not_available[j])) {
470: continue loop;
471: }
472: }
473: if (result == null) {
474: result = stra[i];
475: } else {
476: result = result + "," + stra[i];
477: }
478: }
479: return result;
480: }
481:
482: private static int skipUTF8Char(byte b) {
483: if ((byte) (b & 0x80) == 0)
484: return 1;
485: if ((byte) (b & 0xe0) == (byte) 0xc0)
486: return 2;
487: if ((byte) (b & 0xf0) == (byte) 0xe0)
488: return 3;
489: return 1;
490: }
491: }
|