Mercurial Hosting > luan
comparison src/org/eclipse/jetty/util/StringUtil.java @ 853:3242aff51053
remove RolloverFileOutputStream and cleanup NCSARequestLog
| author | Franklin Schmidt <fschmidt@gmail.com> |
|---|---|
| date | Tue, 20 Sep 2016 00:23:56 -0600 |
| parents | 8e9db0bbf4f9 |
| children | 8d0bdd357e6e |
comparison
equal
deleted
inserted
replaced
| 852:b462b4ff22d8 | 853:3242aff51053 |
|---|---|
| 33 * | 33 * |
| 34 * | 34 * |
| 35 */ | 35 */ |
| 36 public class StringUtil | 36 public class StringUtil |
| 37 { | 37 { |
| 38 private static final Logger LOG = LoggerFactory.getLogger(StringUtil.class); | 38 private static final Logger LOG = LoggerFactory.getLogger(StringUtil.class); |
| 39 | 39 |
| 40 public static final String ALL_INTERFACES="0.0.0.0"; | 40 public static final String ALL_INTERFACES="0.0.0.0"; |
| 41 public static final String CRLF="\015\012"; | 41 public static final String CRLF="\015\012"; |
| 42 public static final String __LINE_SEPARATOR= | 42 |
| 43 System.getProperty("line.separator","\n"); | 43 public static final String __ISO_8859_1="ISO-8859-1"; |
| 44 | 44 public final static String __UTF8="UTF-8"; |
| 45 public static final String __ISO_8859_1="ISO-8859-1"; | 45 public final static String __UTF8Alt="UTF8"; |
| 46 public final static String __UTF8="UTF-8"; | 46 public final static String __UTF16="UTF-16"; |
| 47 public final static String __UTF8Alt="UTF8"; | 47 |
| 48 public final static String __UTF16="UTF-16"; | 48 public final static Charset __UTF8_CHARSET; |
| 49 | 49 public final static Charset __ISO_8859_1_CHARSET; |
| 50 public final static Charset __UTF8_CHARSET; | 50 |
| 51 public final static Charset __ISO_8859_1_CHARSET; | 51 static |
| 52 | 52 { |
| 53 static | 53 __UTF8_CHARSET=Charset.forName(__UTF8); |
| 54 { | 54 __ISO_8859_1_CHARSET=Charset.forName(__ISO_8859_1); |
| 55 __UTF8_CHARSET=Charset.forName(__UTF8); | 55 } |
| 56 __ISO_8859_1_CHARSET=Charset.forName(__ISO_8859_1); | 56 |
| 57 } | 57 private static char[] lowercases = { |
| 58 | 58 '\000','\001','\002','\003','\004','\005','\006','\007', |
| 59 private static char[] lowercases = { | 59 '\010','\011','\012','\013','\014','\015','\016','\017', |
| 60 '\000','\001','\002','\003','\004','\005','\006','\007', | 60 '\020','\021','\022','\023','\024','\025','\026','\027', |
| 61 '\010','\011','\012','\013','\014','\015','\016','\017', | 61 '\030','\031','\032','\033','\034','\035','\036','\037', |
| 62 '\020','\021','\022','\023','\024','\025','\026','\027', | 62 '\040','\041','\042','\043','\044','\045','\046','\047', |
| 63 '\030','\031','\032','\033','\034','\035','\036','\037', | 63 '\050','\051','\052','\053','\054','\055','\056','\057', |
| 64 '\040','\041','\042','\043','\044','\045','\046','\047', | 64 '\060','\061','\062','\063','\064','\065','\066','\067', |
| 65 '\050','\051','\052','\053','\054','\055','\056','\057', | 65 '\070','\071','\072','\073','\074','\075','\076','\077', |
| 66 '\060','\061','\062','\063','\064','\065','\066','\067', | 66 '\100','\141','\142','\143','\144','\145','\146','\147', |
| 67 '\070','\071','\072','\073','\074','\075','\076','\077', | 67 '\150','\151','\152','\153','\154','\155','\156','\157', |
| 68 '\100','\141','\142','\143','\144','\145','\146','\147', | 68 '\160','\161','\162','\163','\164','\165','\166','\167', |
| 69 '\150','\151','\152','\153','\154','\155','\156','\157', | 69 '\170','\171','\172','\133','\134','\135','\136','\137', |
| 70 '\160','\161','\162','\163','\164','\165','\166','\167', | 70 '\140','\141','\142','\143','\144','\145','\146','\147', |
| 71 '\170','\171','\172','\133','\134','\135','\136','\137', | 71 '\150','\151','\152','\153','\154','\155','\156','\157', |
| 72 '\140','\141','\142','\143','\144','\145','\146','\147', | 72 '\160','\161','\162','\163','\164','\165','\166','\167', |
| 73 '\150','\151','\152','\153','\154','\155','\156','\157', | 73 '\170','\171','\172','\173','\174','\175','\176','\177' }; |
| 74 '\160','\161','\162','\163','\164','\165','\166','\167', | 74 |
| 75 '\170','\171','\172','\173','\174','\175','\176','\177' }; | 75 /* ------------------------------------------------------------ */ |
| 76 | 76 /** |
| 77 /* ------------------------------------------------------------ */ | 77 * fast lower case conversion. Only works on ascii (not unicode) |
| 78 /** | 78 * @param s the string to convert |
| 79 * fast lower case conversion. Only works on ascii (not unicode) | 79 * @return a lower case version of s |
| 80 * @param s the string to convert | 80 */ |
| 81 * @return a lower case version of s | 81 public static String asciiToLowerCase(String s) |
| 82 */ | 82 { |
| 83 public static String asciiToLowerCase(String s) | 83 char[] c = null; |
| 84 { | 84 int i=s.length(); |
| 85 char[] c = null; | 85 |
| 86 int i=s.length(); | 86 // look for first conversion |
| 87 | 87 while (i-->0) |
| 88 // look for first conversion | 88 { |
| 89 while (i-->0) | 89 char c1=s.charAt(i); |
| 90 { | 90 if (c1<=127) |
| 91 char c1=s.charAt(i); | 91 { |
| 92 if (c1<=127) | 92 char c2=lowercases[c1]; |
| 93 { | 93 if (c1!=c2) |
| 94 char c2=lowercases[c1]; | 94 { |
| 95 if (c1!=c2) | 95 c=s.toCharArray(); |
| 96 { | 96 c[i]=c2; |
| 97 c=s.toCharArray(); | 97 break; |
| 98 c[i]=c2; | 98 } |
| 99 break; | 99 } |
| 100 } | 100 } |
| 101 } | 101 |
| 102 } | 102 while (i-->0) |
| 103 | 103 { |
| 104 while (i-->0) | 104 if(c[i]<=127) |
| 105 { | 105 c[i] = lowercases[c[i]]; |
| 106 if(c[i]<=127) | 106 } |
| 107 c[i] = lowercases[c[i]]; | 107 |
| 108 } | 108 return c==null?s:new String(c); |
| 109 | 109 } |
| 110 return c==null?s:new String(c); | 110 |
| 111 } | 111 |
| 112 | 112 /* ------------------------------------------------------------ */ |
| 113 | 113 public static boolean startsWithIgnoreCase(String s,String w) |
| 114 /* ------------------------------------------------------------ */ | 114 { |
| 115 public static boolean startsWithIgnoreCase(String s,String w) | 115 if (w==null) |
| 116 { | 116 return true; |
| 117 if (w==null) | 117 |
| 118 return true; | 118 if (s==null || s.length()<w.length()) |
| 119 | 119 return false; |
| 120 if (s==null || s.length()<w.length()) | 120 |
| 121 return false; | 121 for (int i=0;i<w.length();i++) |
| 122 | 122 { |
| 123 for (int i=0;i<w.length();i++) | 123 char c1=s.charAt(i); |
| 124 { | 124 char c2=w.charAt(i); |
| 125 char c1=s.charAt(i); | 125 if (c1!=c2) |
| 126 char c2=w.charAt(i); | 126 { |
| 127 if (c1!=c2) | 127 if (c1<=127) |
| 128 { | 128 c1=lowercases[c1]; |
| 129 if (c1<=127) | 129 if (c2<=127) |
| 130 c1=lowercases[c1]; | 130 c2=lowercases[c2]; |
| 131 if (c2<=127) | 131 if (c1!=c2) |
| 132 c2=lowercases[c2]; | 132 return false; |
| 133 if (c1!=c2) | 133 } |
| 134 return false; | 134 } |
| 135 } | 135 return true; |
| 136 } | 136 } |
| 137 return true; | 137 |
| 138 } | 138 /* ------------------------------------------------------------ */ |
| 139 | 139 public static boolean endsWithIgnoreCase(String s,String w) |
| 140 /* ------------------------------------------------------------ */ | 140 { |
| 141 public static boolean endsWithIgnoreCase(String s,String w) | 141 if (w==null) |
| 142 { | 142 return true; |
| 143 if (w==null) | 143 |
| 144 return true; | 144 if (s==null) |
| 145 | 145 return false; |
| 146 if (s==null) | 146 |
| 147 return false; | 147 int sl=s.length(); |
| 148 | 148 int wl=w.length(); |
| 149 int sl=s.length(); | 149 |
| 150 int wl=w.length(); | 150 if (sl<wl) |
| 151 | 151 return false; |
| 152 if (sl<wl) | 152 |
| 153 return false; | 153 for (int i=wl;i-->0;) |
| 154 | 154 { |
| 155 for (int i=wl;i-->0;) | 155 char c1=s.charAt(--sl); |
| 156 { | 156 char c2=w.charAt(i); |
| 157 char c1=s.charAt(--sl); | 157 if (c1!=c2) |
| 158 char c2=w.charAt(i); | 158 { |
| 159 if (c1!=c2) | 159 if (c1<=127) |
| 160 { | 160 c1=lowercases[c1]; |
| 161 if (c1<=127) | 161 if (c2<=127) |
| 162 c1=lowercases[c1]; | 162 c2=lowercases[c2]; |
| 163 if (c2<=127) | 163 if (c1!=c2) |
| 164 c2=lowercases[c2]; | 164 return false; |
| 165 if (c1!=c2) | 165 } |
| 166 return false; | 166 } |
| 167 } | 167 return true; |
| 168 } | 168 } |
| 169 return true; | 169 |
| 170 } | 170 /* ------------------------------------------------------------ */ |
| 171 | 171 /** |
| 172 /* ------------------------------------------------------------ */ | 172 * returns the next index of a character from the chars string |
| 173 /** | 173 */ |
| 174 * returns the next index of a character from the chars string | 174 public static int indexFrom(String s,String chars) |
| 175 */ | 175 { |
| 176 public static int indexFrom(String s,String chars) | 176 for (int i=0;i<s.length();i++) |
| 177 { | 177 if (chars.indexOf(s.charAt(i))>=0) |
| 178 for (int i=0;i<s.length();i++) | 178 return i; |
| 179 if (chars.indexOf(s.charAt(i))>=0) | 179 return -1; |
| 180 return i; | 180 } |
| 181 return -1; | 181 |
| 182 } | 182 /* ------------------------------------------------------------ */ |
| 183 | 183 /** |
| 184 /* ------------------------------------------------------------ */ | 184 * replace substrings within string. |
| 185 /** | 185 */ |
| 186 * replace substrings within string. | 186 public static String replace(String s, String sub, String with) |
| 187 */ | 187 { |
| 188 public static String replace(String s, String sub, String with) | 188 int c=0; |
| 189 { | 189 int i=s.indexOf(sub,c); |
| 190 int c=0; | 190 if (i == -1) |
| 191 int i=s.indexOf(sub,c); | 191 return s; |
| 192 if (i == -1) | 192 |
| 193 return s; | 193 StringBuilder buf = new StringBuilder(s.length()+with.length()); |
| 194 | 194 |
| 195 StringBuilder buf = new StringBuilder(s.length()+with.length()); | 195 do |
| 196 | 196 { |
| 197 do | 197 buf.append(s.substring(c,i)); |
| 198 { | 198 buf.append(with); |
| 199 buf.append(s.substring(c,i)); | 199 c=i+sub.length(); |
| 200 buf.append(with); | 200 } while ((i=s.indexOf(sub,c))!=-1); |
| 201 c=i+sub.length(); | 201 |
| 202 } while ((i=s.indexOf(sub,c))!=-1); | 202 if (c<s.length()) |
| 203 | 203 buf.append(s.substring(c,s.length())); |
| 204 if (c<s.length()) | 204 |
| 205 buf.append(s.substring(c,s.length())); | 205 return buf.toString(); |
| 206 | 206 |
| 207 return buf.toString(); | 207 } |
| 208 | 208 |
| 209 } | 209 |
| 210 | 210 /* ------------------------------------------------------------ */ |
| 211 | 211 /** Remove single or double quotes. |
| 212 /* ------------------------------------------------------------ */ | 212 */ |
| 213 /** Remove single or double quotes. | 213 public static String unquote(String s) |
| 214 */ | 214 { |
| 215 public static String unquote(String s) | 215 return QuotedStringTokenizer.unquote(s); |
| 216 { | 216 } |
| 217 return QuotedStringTokenizer.unquote(s); | 217 |
| 218 } | 218 |
| 219 | 219 /* ------------------------------------------------------------ */ |
| 220 | 220 /** Append substring to StringBuilder |
| 221 /* ------------------------------------------------------------ */ | 221 * @param buf StringBuilder to append to |
| 222 /** Append substring to StringBuilder | 222 * @param s String to append from |
| 223 * @param buf StringBuilder to append to | 223 * @param offset The offset of the substring |
| 224 * @param s String to append from | 224 * @param length The length of the substring |
| 225 * @param offset The offset of the substring | 225 */ |
| 226 * @param length The length of the substring | 226 public static void append(StringBuilder buf, |
| 227 */ | 227 String s, |
| 228 public static void append(StringBuilder buf, | 228 int offset, |
| 229 String s, | 229 int length) |
| 230 int offset, | 230 { |
| 231 int length) | 231 synchronized(buf) |
| 232 { | 232 { |
| 233 synchronized(buf) | 233 int end=offset+length; |
| 234 { | 234 for (int i=offset; i<end;i++) |
| 235 int end=offset+length; | 235 { |
| 236 for (int i=offset; i<end;i++) | 236 if (i>=s.length()) |
| 237 { | 237 break; |
| 238 if (i>=s.length()) | 238 buf.append(s.charAt(i)); |
| 239 break; | 239 } |
| 240 buf.append(s.charAt(i)); | 240 } |
| 241 } | 241 } |
| 242 } | 242 |
| 243 } | 243 |
| 244 | 244 /* ------------------------------------------------------------ */ |
| 245 | 245 /** |
| 246 /* ------------------------------------------------------------ */ | 246 * append hex digit |
| 247 /** | 247 * |
| 248 * append hex digit | 248 */ |
| 249 * | 249 public static void append(StringBuilder buf,byte b,int base) |
| 250 */ | 250 { |
| 251 public static void append(StringBuilder buf,byte b,int base) | 251 int bi=0xff&b; |
| 252 { | 252 int c='0'+(bi/base)%base; |
| 253 int bi=0xff&b; | 253 if (c>'9') |
| 254 int c='0'+(bi/base)%base; | 254 c= 'a'+(c-'0'-10); |
| 255 if (c>'9') | 255 buf.append((char)c); |
| 256 c= 'a'+(c-'0'-10); | 256 c='0'+bi%base; |
| 257 buf.append((char)c); | 257 if (c>'9') |
| 258 c='0'+bi%base; | 258 c= 'a'+(c-'0'-10); |
| 259 if (c>'9') | 259 buf.append((char)c); |
| 260 c= 'a'+(c-'0'-10); | 260 } |
| 261 buf.append((char)c); | 261 |
| 262 } | 262 /* ------------------------------------------------------------ */ |
| 263 | 263 public static void append2digits(StringBuffer buf,int i) |
| 264 /* ------------------------------------------------------------ */ | 264 { |
| 265 public static void append2digits(StringBuffer buf,int i) | 265 if (i<100) |
| 266 { | 266 { |
| 267 if (i<100) | 267 buf.append((char)(i/10+'0')); |
| 268 { | 268 buf.append((char)(i%10+'0')); |
| 269 buf.append((char)(i/10+'0')); | 269 } |
| 270 buf.append((char)(i%10+'0')); | 270 } |
| 271 } | 271 |
| 272 } | 272 /* ------------------------------------------------------------ */ |
| 273 | 273 public static void append2digits(StringBuilder buf,int i) |
| 274 /* ------------------------------------------------------------ */ | 274 { |
| 275 public static void append2digits(StringBuilder buf,int i) | 275 if (i<100) |
| 276 { | 276 { |
| 277 if (i<100) | 277 buf.append((char)(i/10+'0')); |
| 278 { | 278 buf.append((char)(i%10+'0')); |
| 279 buf.append((char)(i/10+'0')); | 279 } |
| 280 buf.append((char)(i%10+'0')); | 280 } |
| 281 } | 281 |
| 282 } | 282 /* ------------------------------------------------------------ */ |
| 283 | 283 /** Return a non null string. |
| 284 /* ------------------------------------------------------------ */ | 284 * @param s String |
| 285 /** Return a non null string. | 285 * @return The string passed in or empty string if it is null. |
| 286 * @param s String | 286 */ |
| 287 * @return The string passed in or empty string if it is null. | 287 public static String nonNull(String s) |
| 288 */ | 288 { |
| 289 public static String nonNull(String s) | 289 if (s==null) |
| 290 { | 290 return ""; |
| 291 if (s==null) | 291 return s; |
| 292 return ""; | 292 } |
| 293 return s; | 293 |
| 294 } | 294 /* ------------------------------------------------------------ */ |
| 295 | 295 public static boolean equals(String s,char[] buf, int offset, int length) |
| 296 /* ------------------------------------------------------------ */ | 296 { |
| 297 public static boolean equals(String s,char[] buf, int offset, int length) | 297 if (s.length()!=length) |
| 298 { | 298 return false; |
| 299 if (s.length()!=length) | 299 for (int i=0;i<length;i++) |
| 300 return false; | 300 if (buf[offset+i]!=s.charAt(i)) |
| 301 for (int i=0;i<length;i++) | 301 return false; |
| 302 if (buf[offset+i]!=s.charAt(i)) | 302 return true; |
| 303 return false; | 303 } |
| 304 return true; | 304 |
| 305 } | 305 /* ------------------------------------------------------------ */ |
| 306 | 306 public static String toUTF8String(byte[] b,int offset,int length) |
| 307 /* ------------------------------------------------------------ */ | 307 { |
| 308 public static String toUTF8String(byte[] b,int offset,int length) | 308 try |
| 309 { | 309 { |
| 310 try | 310 return new String(b,offset,length,__UTF8); |
| 311 { | 311 } |
| 312 return new String(b,offset,length,__UTF8); | 312 catch (UnsupportedEncodingException e) |
| 313 } | 313 { |
| 314 catch (UnsupportedEncodingException e) | 314 throw new IllegalArgumentException(e); |
| 315 { | 315 } |
| 316 throw new IllegalArgumentException(e); | 316 } |
| 317 } | 317 |
| 318 } | 318 /* ------------------------------------------------------------ */ |
| 319 | 319 public static String toString(byte[] b,int offset,int length,String charset) |
| 320 /* ------------------------------------------------------------ */ | 320 { |
| 321 public static String toString(byte[] b,int offset,int length,String charset) | 321 try |
| 322 { | 322 { |
| 323 try | 323 return new String(b,offset,length,charset); |
| 324 { | 324 } |
| 325 return new String(b,offset,length,charset); | 325 catch (UnsupportedEncodingException e) |
| 326 } | 326 { |
| 327 catch (UnsupportedEncodingException e) | 327 throw new IllegalArgumentException(e); |
| 328 { | 328 } |
| 329 throw new IllegalArgumentException(e); | 329 } |
| 330 } | 330 |
| 331 } | 331 |
| 332 | 332 /* ------------------------------------------------------------ */ |
| 333 | 333 public static boolean isUTF8(String charset) |
| 334 /* ------------------------------------------------------------ */ | 334 { |
| 335 public static boolean isUTF8(String charset) | 335 return __UTF8.equalsIgnoreCase(charset)||__UTF8Alt.equalsIgnoreCase(charset); |
| 336 { | 336 } |
| 337 return __UTF8.equalsIgnoreCase(charset)||__UTF8Alt.equalsIgnoreCase(charset); | 337 |
| 338 } | 338 |
| 339 | 339 /* ------------------------------------------------------------ */ |
| 340 | 340 public static String printable(String name) |
| 341 /* ------------------------------------------------------------ */ | 341 { |
| 342 public static String printable(String name) | 342 if (name==null) |
| 343 { | 343 return null; |
| 344 if (name==null) | 344 StringBuilder buf = new StringBuilder(name.length()); |
| 345 return null; | 345 for (int i=0;i<name.length();i++) |
| 346 StringBuilder buf = new StringBuilder(name.length()); | 346 { |
| 347 for (int i=0;i<name.length();i++) | 347 char c=name.charAt(i); |
| 348 { | 348 if (!Character.isISOControl(c)) |
| 349 char c=name.charAt(i); | 349 buf.append(c); |
| 350 if (!Character.isISOControl(c)) | 350 } |
| 351 buf.append(c); | 351 return buf.toString(); |
| 352 } | 352 } |
| 353 return buf.toString(); | 353 |
| 354 } | 354 /* ------------------------------------------------------------ */ |
| 355 | 355 public static String printable(byte[] b) |
| 356 /* ------------------------------------------------------------ */ | 356 { |
| 357 public static String printable(byte[] b) | 357 StringBuilder buf = new StringBuilder(); |
| 358 { | 358 for (int i=0;i<b.length;i++) |
| 359 StringBuilder buf = new StringBuilder(); | 359 { |
| 360 for (int i=0;i<b.length;i++) | 360 char c=(char)b[i]; |
| 361 { | 361 if (Character.isWhitespace(c)|| c>' ' && c<0x7f) |
| 362 char c=(char)b[i]; | 362 buf.append(c); |
| 363 if (Character.isWhitespace(c)|| c>' ' && c<0x7f) | 363 else |
| 364 buf.append(c); | 364 { |
| 365 else | 365 buf.append("0x"); |
| 366 { | 366 TypeUtil.toHex(b[i],buf); |
| 367 buf.append("0x"); | 367 } |
| 368 TypeUtil.toHex(b[i],buf); | 368 } |
| 369 } | 369 return buf.toString(); |
| 370 } | 370 } |
| 371 return buf.toString(); | 371 |
| 372 } | 372 public static byte[] getBytes(String s) |
| 373 | 373 { |
| 374 public static byte[] getBytes(String s) | 374 try |
| 375 { | 375 { |
| 376 try | 376 return s.getBytes(__ISO_8859_1); |
| 377 { | 377 } |
| 378 return s.getBytes(__ISO_8859_1); | 378 catch(Exception e) |
| 379 } | 379 { |
| 380 catch(Exception e) | 380 LOG.warn("",e); |
| 381 { | 381 return s.getBytes(); |
| 382 LOG.warn("",e); | 382 } |
| 383 return s.getBytes(); | 383 } |
| 384 } | 384 |
| 385 } | 385 public static byte[] getBytes(String s,String charset) |
| 386 | 386 { |
| 387 public static byte[] getBytes(String s,String charset) | 387 try |
| 388 { | 388 { |
| 389 try | 389 return s.getBytes(charset); |
| 390 { | 390 } |
| 391 return s.getBytes(charset); | 391 catch(Exception e) |
| 392 } | 392 { |
| 393 catch(Exception e) | 393 LOG.warn("",e); |
| 394 { | 394 return s.getBytes(); |
| 395 LOG.warn("",e); | 395 } |
| 396 return s.getBytes(); | 396 } |
| 397 } | 397 |
| 398 } | 398 |
| 399 | 399 |
| 400 | 400 /** |
| 401 | 401 * Converts a binary SID to a string SID |
| 402 /** | 402 * |
| 403 * Converts a binary SID to a string SID | 403 * http://en.wikipedia.org/wiki/Security_Identifier |
| 404 * | 404 * |
| 405 * http://en.wikipedia.org/wiki/Security_Identifier | 405 * S-1-IdentifierAuthority-SubAuthority1-SubAuthority2-...-SubAuthorityn |
| 406 * | 406 */ |
| 407 * S-1-IdentifierAuthority-SubAuthority1-SubAuthority2-...-SubAuthorityn | 407 public static String sidBytesToString(byte[] sidBytes) |
| 408 */ | 408 { |
| 409 public static String sidBytesToString(byte[] sidBytes) | 409 StringBuilder sidString = new StringBuilder(); |
| 410 { | 410 |
| 411 StringBuilder sidString = new StringBuilder(); | 411 // Identify this as a SID |
| 412 | 412 sidString.append("S-"); |
| 413 // Identify this as a SID | 413 |
| 414 sidString.append("S-"); | 414 // Add SID revision level (expect 1 but may change someday) |
| 415 | 415 sidString.append(Byte.toString(sidBytes[0])).append('-'); |
| 416 // Add SID revision level (expect 1 but may change someday) | 416 |
| 417 sidString.append(Byte.toString(sidBytes[0])).append('-'); | 417 StringBuilder tmpBuilder = new StringBuilder(); |
| 418 | 418 |
| 419 StringBuilder tmpBuilder = new StringBuilder(); | 419 // crunch the six bytes of issuing authority value |
| 420 | 420 for (int i = 2; i <= 7; ++i) |
| 421 // crunch the six bytes of issuing authority value | 421 { |
| 422 for (int i = 2; i <= 7; ++i) | 422 tmpBuilder.append(Integer.toHexString(sidBytes[i] & 0xFF)); |
| 423 { | 423 } |
| 424 tmpBuilder.append(Integer.toHexString(sidBytes[i] & 0xFF)); | 424 |
| 425 } | 425 sidString.append(Long.parseLong(tmpBuilder.toString(), 16)); // '-' is in the subauth loop |
| 426 | |
| 427 sidString.append(Long.parseLong(tmpBuilder.toString(), 16)); // '-' is in the subauth loop | |
| 428 | 426 |
| 429 // the number of subAuthorities we need to attach | 427 // the number of subAuthorities we need to attach |
| 430 int subAuthorityCount = sidBytes[1]; | 428 int subAuthorityCount = sidBytes[1]; |
| 431 | 429 |
| 432 // attach each of the subAuthorities | 430 // attach each of the subAuthorities |
| 433 for (int i = 0; i < subAuthorityCount; ++i) | 431 for (int i = 0; i < subAuthorityCount; ++i) |
| 434 { | 432 { |
| 435 int offset = i * 4; | 433 int offset = i * 4; |
| 436 tmpBuilder.setLength(0); | 434 tmpBuilder.setLength(0); |
| 437 // these need to be zero padded hex and little endian | 435 // these need to be zero padded hex and little endian |
| 438 tmpBuilder.append(String.format("%02X%02X%02X%02X", | 436 tmpBuilder.append(String.format("%02X%02X%02X%02X", |
| 439 (sidBytes[11 + offset] & 0xFF), | 437 (sidBytes[11 + offset] & 0xFF), |
| 440 (sidBytes[10 + offset] & 0xFF), | 438 (sidBytes[10 + offset] & 0xFF), |
| 441 (sidBytes[9 + offset] & 0xFF), | 439 (sidBytes[9 + offset] & 0xFF), |
| 442 (sidBytes[8 + offset] & 0xFF))); | 440 (sidBytes[8 + offset] & 0xFF))); |
| 443 sidString.append('-').append(Long.parseLong(tmpBuilder.toString(), 16)); | 441 sidString.append('-').append(Long.parseLong(tmpBuilder.toString(), 16)); |
| 444 } | 442 } |
| 445 | 443 |
| 446 return sidString.toString(); | 444 return sidString.toString(); |
| 447 } | 445 } |
| 448 | 446 |
| 449 /** | 447 /** |
| 450 * Converts a string SID to a binary SID | 448 * Converts a string SID to a binary SID |
| 451 * | 449 * |
| 452 * http://en.wikipedia.org/wiki/Security_Identifier | 450 * http://en.wikipedia.org/wiki/Security_Identifier |
| 453 * | 451 * |
| 454 * S-1-IdentifierAuthority-SubAuthority1-SubAuthority2-...-SubAuthorityn | 452 * S-1-IdentifierAuthority-SubAuthority1-SubAuthority2-...-SubAuthorityn |
| 455 */ | 453 */ |
| 456 public static byte[] sidStringToBytes( String sidString ) | 454 public static byte[] sidStringToBytes( String sidString ) |
| 457 { | 455 { |
| 458 String[] sidTokens = sidString.split("-"); | 456 String[] sidTokens = sidString.split("-"); |
| 459 | 457 |
| 460 int subAuthorityCount = sidTokens.length - 3; // S-Rev-IdAuth- | 458 int subAuthorityCount = sidTokens.length - 3; // S-Rev-IdAuth- |
| 461 | 459 |
| 462 int byteCount = 0; | 460 int byteCount = 0; |
| 463 byte[] sidBytes = new byte[1 + 1 + 6 + (4 * subAuthorityCount)]; | 461 byte[] sidBytes = new byte[1 + 1 + 6 + (4 * subAuthorityCount)]; |
| 464 | 462 |
| 465 // the revision byte | 463 // the revision byte |
| 466 sidBytes[byteCount++] = (byte)Integer.parseInt(sidTokens[1]); | 464 sidBytes[byteCount++] = (byte)Integer.parseInt(sidTokens[1]); |
| 467 | 465 |
| 468 // the # of sub authorities byte | 466 // the # of sub authorities byte |
| 469 sidBytes[byteCount++] = (byte)subAuthorityCount; | 467 sidBytes[byteCount++] = (byte)subAuthorityCount; |
| 470 | 468 |
| 471 // the certAuthority | 469 // the certAuthority |
| 472 String hexStr = Long.toHexString(Long.parseLong(sidTokens[2])); | 470 String hexStr = Long.toHexString(Long.parseLong(sidTokens[2])); |
| 473 | 471 |
| 474 while( hexStr.length() < 12) // pad to 12 characters | 472 while( hexStr.length() < 12) // pad to 12 characters |
| 475 { | 473 { |
| 476 hexStr = "0" + hexStr; | 474 hexStr = "0" + hexStr; |
| 477 } | 475 } |
| 478 | 476 |
| 479 // place the certAuthority 6 bytes | 477 // place the certAuthority 6 bytes |
| 480 for ( int i = 0 ; i < hexStr.length(); i = i + 2) | 478 for ( int i = 0 ; i < hexStr.length(); i = i + 2) |
| 481 { | 479 { |
| 482 sidBytes[byteCount++] = (byte)Integer.parseInt(hexStr.substring(i, i + 2),16); | 480 sidBytes[byteCount++] = (byte)Integer.parseInt(hexStr.substring(i, i + 2),16); |
| 483 } | 481 } |
| 484 | 482 |
| 485 | 483 |
| 486 for ( int i = 3; i < sidTokens.length ; ++i) | 484 for ( int i = 3; i < sidTokens.length ; ++i) |
| 487 { | 485 { |
| 488 hexStr = Long.toHexString(Long.parseLong(sidTokens[i])); | 486 hexStr = Long.toHexString(Long.parseLong(sidTokens[i])); |
| 489 | 487 |
| 490 while( hexStr.length() < 8) // pad to 8 characters | 488 while( hexStr.length() < 8) // pad to 8 characters |
| 491 { | 489 { |
| 492 hexStr = "0" + hexStr; | 490 hexStr = "0" + hexStr; |
| 493 } | 491 } |
| 494 | 492 |
| 495 // place the inverted sub authorities, 4 bytes each | 493 // place the inverted sub authorities, 4 bytes each |
| 496 for ( int j = hexStr.length(); j > 0; j = j - 2) | 494 for ( int j = hexStr.length(); j > 0; j = j - 2) |
| 497 { | 495 { |
| 498 sidBytes[byteCount++] = (byte)Integer.parseInt(hexStr.substring(j-2, j),16); | 496 sidBytes[byteCount++] = (byte)Integer.parseInt(hexStr.substring(j-2, j),16); |
| 499 } | 497 } |
| 500 } | 498 } |
| 501 | 499 |
| 502 return sidBytes; | 500 return sidBytes; |
| 503 } | 501 } |
| 504 } | 502 } |
