001: /*
002: *
003: * Jsmtpd, Java SMTP daemon
004: * Copyright (C) 2005 Jean-Francois POUX, jf.poux@laposte.net
005: *
006: * This program is free software; you can redistribute it and/or
007: * modify it under the terms of the GNU General Public License
008: * as published by the Free Software Foundation; either version 2
009: * of the License, or (at your option) any later version.
010: *
011: * This program is distributed in the hope that it will be useful,
012: * but WITHOUT ANY WARRANTY; without even the implied warranty of
013: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
014: * GNU General Public License for more details.
015: *
016: * You should have received a copy of the GNU General Public License
017: * along with this program; if not, write to the Free Software
018: * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
019: *
020: */
021: package org.jsmtpd.tools;
022:
023: import java.io.ByteArrayOutputStream;
024: import java.util.ArrayList;
025: import java.util.Arrays;
026: import java.util.List;
027:
028: /**
029: * Tool to manipulate byte arrays
030: * Implementations is quite slow at the momment...
031: * @author Jean-Francois POUX
032: */
033: public class ByteArrayTool {
034:
035: // this is slow ...
036: public static byte[] replaceBytes(byte[] src, byte[] pattern,
037: byte[] replace) {
038: ByteArrayOutputStream bos = new ByteArrayOutputStream();
039: int start = 0;
040: int i;
041: for (i = 0; i < src.length; i++) {
042: if (src[i] == pattern[0]) {
043: boolean found = true;
044: for (int j = 0; j < pattern.length; j++) { //starts at j=1!
045: if (i + pattern.length > src.length) { //don't loop anymore
046: found = false;
047: break;
048: }
049: if (src[i + j] != pattern[j]) {
050: found = false;
051: break;
052: }
053: }
054: bos.write(src, start, i - start); //copy to begin of pattern
055: if (found) { // add pattern
056: bos.write(replace, 0, replace.length);
057: i += pattern.length - 1; // move src forward
058: start = i + 1;
059: } else
060: start = i;
061: }
062: }
063: bos.write(src, start, i - start);
064: return bos.toByteArray();
065: }
066:
067: // check for any LF, if it is no preceeded with CR, add CR
068: // slow impl ...
069: public static byte[] crlfFix(byte[] src) {
070: ByteArrayOutputStream bos = new ByteArrayOutputStream();
071: for (int i = 0; i < src.length; i++) {
072: if ((src[i] == LF[0]) && (i > 1)) {
073: if (src[i - 1] != CR[0])
074: bos.write(CR, 0, 1);
075: }
076: bos.write(src[i]);
077: }
078: return bos.toByteArray();
079: }
080:
081: public static boolean compare(byte[] a, byte[] b) {
082: return Arrays.equals(a, b);
083: }
084:
085: public static List<byte[]> split(byte[] input, byte separator) {
086: if (input == null)
087: return null;
088:
089: List<byte[]> lst = new ArrayList<byte[]>();
090: int startPos = 0;
091: for (int i = 0; i < input.length; i++) {
092: if (input[i] == separator) {
093: byte[] tmp = new byte[i - startPos];
094: System.arraycopy(input, startPos, tmp, 0, i - startPos);
095: lst.add(tmp);
096: startPos = i + 1;
097: }
098: }
099: if ((startPos != 0) && (startPos < input.length)) {
100: byte[] tmp = new byte[input.length - startPos];
101: System.arraycopy(input, startPos, tmp, 0, input.length
102: - startPos);
103: lst.add(tmp);
104: }
105: return lst;
106: }
107:
108: public static int patternAt(byte[] toCheck, byte[] pattern) {
109: /*
110: for (int i = toCheck.length-1; i ==0; i--) {
111: if (toCheck[i]==pattern[0]) { // look for first byte of pattern
112: int offset = toCheck.length-i;
113: if (offset>=pattern.length) {
114: for (int j = 0; j < pattern.length; j++) {
115: byte b = pattern[j];
116:
117: }
118: }
119: }
120:
121: }
122: */
123: return -1;
124: }
125:
126: public static final byte[] CRLF = { 13, 10 };
127: public static final byte[] LF = { 10 };
128: public static final byte[] CR = { 13 };
129: public static final byte[] EOM = { 13, 10, 46, 13, 10 };
130: public static final byte NULL = 0;
131: }
|