1 /*
2  * @(#) $Id: PatternUtil.java,v 1.3 2003/07/08 08:13:53 pankaj Exp $
3  *
4  * Copyright (c) 2002-03 by Pankaj Kumar (http://www.pankaj-k.net). 
5  * All rights reserved.
6  *
7  * The license governing the use of this file can be found in the 
8  * root directory of the containing software.
9  */
10package org.jstk.ssl;
11
12import org.jstk.JSTKOptions;
13import java.util.HashMap;
14
15public class PatternUtil {
16    private String pattern = null;
17    private byte[] patbuf = null;
18    private int patidx = 0;
19    private boolean matched = true;
20
21    public PatternUtil(String pattern){
22        this.pattern = pattern;
23        if (pattern != null)
24            patbuf = pattern.getBytes();
25    }
26
27    public boolean needToFill(){
28        return (pattern != null);
29    }
30
31    public boolean needToMatch(){
32        return (pattern != null);
33    }
34
35    public void fillPattern(JSTKBuffer jbuf){
36        if (pattern == null)
37            return;
38        // Same PatternUtil could be applied on multiple buffers. PatternUtil remembers
39        // state in patidx variable.
40        jbuf.clear();
41        int patlen = patbuf.length - patidx;
42        int bufoff = 0;
43        while (bufoff + patlen <= jbuf.length()){
44            jbuf.putBytes(patbuf, patidx, patlen);
45            bufoff += patlen;
46            patidx = 0;
47            patlen = patbuf.length - patidx;
48        }
49        if (bufoff < jbuf.length()){    // Some work is left.
50            patlen = jbuf.length() - bufoff;
51            jbuf.putBytes(patbuf, patidx, patlen);
52            patidx = patlen;
53        }
54    }
55
56    public boolean matchPattern(JSTKBuffer jbuf){
57        if (pattern == null)
58            return true;
59        byte[] buf = jbuf.getBytes();
60        int patlen = patbuf.length - patidx;
61        int bufoff = 0;
62        while (bufoff + patlen < buf.length){
63            if (!equals(patbuf, patidx, buf, bufoff, patlen)){
64                return false;
65            }
66            bufoff += patlen;
67            patidx = 0;
68            patlen = patbuf.length - patidx;
69        }
70        if (bufoff < buf.length){   // Some work is left.
71            patlen = buf.length - bufoff;
72            if (!equals(patbuf, patidx, buf, bufoff, patlen)){
73                return false;
74            }
75            patidx = patlen;
76        }
77        return true;
78    }
79
80    public boolean getMatched(){
81        return matched;
82    }
83
84    private boolean equals(byte[] srcbuf, int srcoff, byte[] dstbuf, int dstoff, int len){
85        for (int i = 0; i < len; i++){
86            if (srcbuf[srcoff + i] != dstbuf[dstoff + i]){
87                matched = false;
88                return matched;
89            }
90        }
91        return true;
92    }
93
94    public static void main(String[] args){
95        JSTKOptions opts = new JSTKOptions();
96        opts.parse(args, 0);
97        HashMap defaults = new HashMap();
98        defaults.put("pattern1", "Test Pattern");
99        defaults.put("pattern2", "Test Pattern");
00        defaults.put("size1", "1024");
01        defaults.put("size2", "512");
02        opts.setDefaults(defaults);
03
04        String pattern1 = opts.get("pattern1");
05        String pattern2 = opts.get("pattern2");
06        int size1 = Integer.parseInt(opts.get("size1"));
07        int size2 = Integer.parseInt(opts.get("size2"));
08
09        System.out.println("pattern1: " + pattern1 + ", pattern2: " + pattern2);
10        System.out.println("size1: " + size1 + ", size2: " + size2);
11
12        PatternUtil pu1 = new PatternUtil(pattern1);
13        JSTKBuffer buf1 = JSTKBuffer.getInstance(size1, opts);
14        JSTKBuffer buf2 = JSTKBuffer.getInstance(size2, opts);
15
16        pu1.fillPattern(buf1);
17        pu1.fillPattern(buf2);
18
19        PatternUtil pu2 = new PatternUtil(pattern2);
20        if (!pu2.matchPattern(buf1)){
21            System.out.println("buf1: Match Failed.");
22            System.exit(1);
23        }
24        if (!pu2.matchPattern(buf2)){
25            System.out.println("buf2: Match Failed.");
26            System.exit(1);
27        }
28        System.out.println("buf1, buf2: Match Succeeded.");
29        System.exit(0);
30    }
31}
32