00001
package com.quadcap.util;
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041
import java.util.Comparator;
00042
00043
00044
00045
00046
00047
00048 public class OctetComparator implements Comparator {
00049 boolean reverse =
false;
00050 boolean casemap =
false;
00051
00052 static OctetComparator cmp =
new OctetComparator();
00053 static OctetComparator
casecmp =
new OctetComparator(
true,
false);
00054
00055
00056
00057 public OctetComparator() {}
00058
00059 public OctetComparator(
boolean casemap,
boolean reverse) {
00060
this.casemap = casemap;
00061
this.reverse = reverse;
00062 }
00063
00064
00065
00066
00067
00068
00069
00070
00071
00072
00073
00074
00075
00076
00077
00078
00079
00080
00081
00082
00083
00084 public int compare(Object a, Object b) {
00085
OctetString oa = (
OctetString)a;
00086
OctetString ob = (
OctetString)b;
00087
00088
int before =
reverse ? 1 : -1;
00089
int after = 0 - before;
00090
00091 byte[] va =
getBytes(oa);
00092 byte[] vb = getBytes(ob);
00093
int i = 0;
00094
while (i < va.length && i < vb.length) {
00095
if (va[i] < vb[i])
return before;
00096
if (va[i] > vb[i])
return after;
00097 i++;
00098 }
00099
if (i < vb.length)
return before;
00100
if (i < va.length)
return after;
00101
return 0;
00102 }
00103
00104
00105
00106
00107
00108
00109
00110
00111
00112
00113
00114
00115
00116 public boolean prefixMatch(
OctetString obj,
OctetString val) {
00117 byte[] va =
getBytes(val);
00118 byte[] vb = getBytes(obj);
00119
if (va.length > vb.length)
return false;
00120
for (
int i = 0; i < va.length; i++) {
00121
if (va[i] != vb[i])
return false;
00122 }
00123
return true;
00124 }
00125
00126
00127
00128
00129
00130
00131
00132
00133
00134
00135
00136
00137
00138 public boolean substringMatch(
OctetString obj,
OctetString val) {
00139 byte[] va =
getBytes(obj);
00140 byte[] vb = getBytes(val);
00141
int left = va.length - vb.length;
00142
for (
int i = 0; i <= left; i++) {
00143
boolean match =
true;
00144
for (
int j = 0; match && j < vb.length; j++) {
00145 match = va[i+j] == vb[j];
00146 }
00147
if (match)
return true;
00148 }
00149
return false;
00150 }
00151
00152
00153
00154
00155
00156
00157
00158
00159
00160
00161 public boolean patternMatch(
OctetString obj,
OctetString pattern) {
00162 byte[] va =
getBytes(obj);
00163 byte[] vb = getBytes(pattern);
00164
return pMatch(va, 0, vb, 0);
00165 }
00166
00167 boolean pMatch(byte[] va,
int a, byte[] vb,
int b) {
00168
while (b < vb.length) {
00169 byte p = vb[b++];
00170
switch (p) {
00171
case (byte)
'*':
00172
if (a >= va.length || b >= vb.length)
return true;
00173
while (a < va.length) {
00174
if (pMatch(va, a++, vb, b))
return true;
00175 }
00176
return false;
00177
case (byte)
'?':
00178
if (a++ >= va.length)
return false;
00179
break;
00180
case (byte)
'\\':
00181
if (b >= vb.length)
return false;
00182 p = vb[b++];
00183
00184
default:
00185
if (a >= va.length)
return false;
00186
if (va[a++] != p)
return false;
00187 }
00188 }
00189
return a >= va.length;
00190 }
00191
00192 int pMatchOrder(byte[] va,
int a, byte[] vb,
int b) {
00193
int ret = 0;
00194
while (b < vb.length) {
00195 byte p = vb[b++];
00196
switch (p) {
00197
case (byte)
'*':
00198
while (a < va.length) {
00199 ret = pMatchOrder(va, a++, vb, b);
00200
if (ret == 0)
return ret;
00201 }
00202
return ret;
00203
case (byte)
'?':
00204
if (a++ >= va.length)
return -1;
00205
break;
00206
case (byte)
'\\':
00207
if (b >= vb.length)
return 1;
00208 p = vb[b++];
00209
00210
default:
00211
if (a >= va.length)
return -1;
00212 byte q = va[a++];
00213
if (q < p)
return -1;
00214
if (q > p)
return 1;
00215 }
00216 }
00217
return a >= va.length ? 0 : 1;
00218 }
00219
00220
00221
00222
00223
00224
00225
00226 byte[]
getBytes(
OctetString obj) {
00227 byte[] b = obj.
getBytes();
00228
if (
casemap) {
00229 byte[] cb =
new byte[b.length];
00230
for (
int i = 0; i < b.length; i++) {
00231 byte c = b[i];
00232
if (c >=
'a' && c <=
'z')
00233 cb[i] = (byte)(c - 32);
00234
else
00235 cb[i] = c;
00236 }
00237 b = cb;
00238 }
00239
return b;
00240 }
00241 }