当前位置: 动力学知识库 > 问答 > 编程问答 >

Clojure: Find Byte Sequence in Java Byte Array

问题描述:

Can someone give me a hint how to find a sequence of two bytes inside of an java byte array. The two bytes I'm looking for are: FF DA (in hex)

It looks like java maps the range of a byte to -127 to 128. I would have expected having a 0 - 255 range.

Here is a propose:

(->> (partition 2 byte-array)

(keep-indexed (fn [i ab]

(when (= ab [(byte 0xFF) (byte 0xDA)]))))

first)

But this does not work due to the minus ranges. Plus using those high-level functions and the overhead of lazy sequences will most likely be not right here.

My actual usecase is to find in a byte array, of a JPG image the position where the image data starts (and the meta data section stops).

网友答案:

i would go with something like this to convert number to byte:

user> 
(defn as-byte [^long n]
  {:pre [(<= 0 n 255)]}
  ^byte (unchecked-subtract n 256))
#'user/as-byte

user> (as-byte 0xff)
;;=> -1

user> (as-byte 0xda)
;;=> -38

user> (as-byte 0xfff)
AssertionError Assert failed: (<= 0 n 255)  user/as-byte (form-init2939145917481178115.clj:259)

then i would rewrite the search function, to operate on arrays, not sequences, since it should be much faster:

(defn find-bytes-idx [^bytes look-for ^bytes data]
  (let [search-len (alength look-for)
        diff (- (alength data) search-len)]
    (if (neg? diff)
      -1
      (loop [i 0]
        (cond (> i diff) -1
              (Arrays/equals look-for (Arrays/copyOfRange data i (+ i search-len))) i
              :else (recur (inc i)))))))

in repl:

user> (find-bytes-idx (byte-array (map as-byte [0xcd 0xef]))
                      (byte-array (map as-byte [0xaa 0xab 0xcd 0xcd 0xef 0xdf])))

;=> 3
user> (find-bytes-idx (byte-array (map as-byte [0xcd 0xef]))
                      (byte-array (map as-byte [0xaa 0xab 0xcd 0xcd 0xee])))

;=> -1
网友答案:

unchecked-byte will coerce to byte. Alternatively you could just convert your Hex to signed bytes (-128 to 127)

(unchecked-byte 0xff)
;;=> -1
(unchecked-byte 0xda)
;;=> -38

(->>
 (byte-array (map unchecked-byte
                  [0x07 0x00 0x05 0x06
                   0x07 0xE9 0xFF 0xDA
                   0x01 0x02 0x03 0x2A
                   0x33 0x99 0xFF 0xDA]))
             (partition 2)
             (keep-indexed (fn [i ab]
                             (when
                                 (= ab [(unchecked-byte 0xFF)
                                        (unchecked-byte 0xDA)])
                                 [i ab]))))

;;=> ([3 (-1 -38)] [7 (-1 -38)])
网友答案:

Updated Solution

Here is a better way of solving the problem. While I am usually not a big fan of using nil when I mean false, the build-in function keep-indexed does a good job of combining the decision of keep=true/false and the reducing step of discarding all the values where keep=false.

(s/defn find-patterns :- [s/Int]
  [pattern-vec :- tsk/List
   data-vec :- tsk/List]
  (let [parts         (partition (count pattern-vec) 1 data-vec)
        idxs          (keep-indexed
                        (fn [idx candidate]
                          (when (= candidate pattern-vec)
                            idx))
                        parts)]
    idxs))

(s/defn find-pattern :- s/Int
  [pattern  :- tsk/List
   data     :- tsk/List ]
  (first (find-patterns pattern data)))

(deftest t-find-pattern
;              0 1 2  3    4    5    6    7   8 9]
  (let [data [ 0 1 2 0xAA 0xFA 0xFF 0xDA 0xDD 8 9] ]
    (is= 5 (find-pattern  [0xFF 0xDA] data))))

Original solution

Here is a way of doing it. Using spy-let from the Tupelo library you can visualize the steps as it goes:

(ns tst.clj.core
  (:require [tupelo.core :as t]))
(t/refer-tupelo)

;           0 1 2  3    4    5    6    7   8 9]
(def data [ 0 1 2 0xAA 0xFA 0xFF 0xDA 0xDD 8 9] )

(defn find-pattern
  [pattern data]
  (spy-let [
    patt-matches?   (fn [idx tst-pat] [idx (= tst-pat pattern) ] )
    parts           (partition (count pattern) 1 data)
    idx-labelled    (map-indexed patt-matches? parts)
    idx-matches?    (fn [[idx matches]] (= true matches))
    idx-that-match  (filter idx-matches? idx-labelled)
    result          (first (first idx-that-match))
  ]
    result
  )
)
(spyx (find-pattern [0xFF 0xDA] data))

with result:

parts => ((0 1) (1 2) (2 170) (170 250) (250 255) (255 218) (218 221) (221 8) (8 9))

idx-labelled => ([0 false] [1 false] [2 false] [3 false] [4 false] [5 true] [6 false] [7 false] [8 false])

idx-that-match => ([5 true])

result => 5

(find-pattern [255 218] data) => 5

Not knowing your data format, just drop it into a Clojure vector and then don't worry about signed vs unsigned bytes.

分享给朋友:
您可能感兴趣的文章:
随机阅读: