Searching for a sequence of Bytes in a Binary File with Java

前端 未结 4 2085
故里飘歌
故里飘歌 2020-12-01 05:08

I have a sequence of bytes that I have to search for in a set of Binary files using Java.

Example: I\'m searching for the byte sequence DEADBEEF (in hex

4条回答
  •  醉酒成梦
    2020-12-01 05:53

    No, there is no built-in method to do that. But, directly copied from HERE (with two fixes applied to the original code):

    /**
     * Knuth-Morris-Pratt Algorithm for Pattern Matching
     */
    class KMPMatch {
        /**
         * Finds the first occurrence of the pattern in the text.
         */
        public static int indexOf(byte[] data, byte[] pattern) {
            if (data.length == 0) return -1;
    
            int[] failure = computeFailure(pattern);    
            int j = 0;
    
            for (int i = 0; i < data.length; i++) {
                while (j > 0 && pattern[j] != data[i]) {
                    j = failure[j - 1];
                }
                if (pattern[j] == data[i]) { j++; }
                if (j == pattern.length) {
                    return i - pattern.length + 1;
                }
            }
            return -1;
        }
    
        /**
         * Computes the failure function using a boot-strapping process,
         * where the pattern is matched against itself.
         */
        private static int[] computeFailure(byte[] pattern) {
            int[] failure = new int[pattern.length];
    
            int j = 0;
            for (int i = 1; i < pattern.length; i++) {
                while (j > 0 && pattern[j] != pattern[i]) {
                    j = failure[j - 1];
                }
                if (pattern[j] == pattern[i]) {
                    j++;
                }
                failure[i] = j;
            }
    
            return failure;
        }
    }
    

提交回复
热议问题