All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.github.jlangch.venice.hexdump.venice Maven / Gradle / Ivy

;;;;   __    __         _
;;;;   \ \  / /__ _ __ (_) ___ ___
;;;;    \ \/ / _ \ '_ \| |/ __/ _ \
;;;;     \  /  __/ | | | | (_|  __/
;;;;      \/ \___|_| |_|_|\___\___|
;;;;
;;;;
;;;; Copyright 2017-2024 Venice
;;;;
;;;; Licensed under the Apache License, Version 2.0 (the "License");
;;;; you may not use this file except in compliance with the License.
;;;; You may obtain a copy of the License at
;;;;
;;;;     http://www.apache.org/licenses/LICENSE-2.0
;;;;
;;;; Unless required by applicable law or agreed to in writing, software
;;;; distributed under the License is distributed on an "AS IS" BASIS,
;;;; WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
;;;; See the License for the specific language governing permissions and
;;;; limitations under the License.


(ns hexdump)


(defn byte [val]
  (mod (long val) 256))

(defn ascii [val]
  (let [v (byte val)]
    (if (< 0x1F v 0x7F) (char v) (char "."))))

(defn byte-offsets []
  (map #(str/format "%08x" %) (map #(* 16 %) (lazy-seq 0 inc))))

(defn hex-ascii-lines [s]
  (->> (map byte s)
       (map #(str/format "%02x" %))
       (partition 2 2 (repeat " "))
       (interpose " ")
       flatten
       (apply str)
       (seq)
       (partition 40 40 (repeat " "))
       (map #(apply str %))))

(defn ascii-lines [s]
  (->> (map ascii s)
       (partition 16 16 (repeat " "))
       (map #(apply str %))))

(defn hex-lines [s opts]
  (cond
    (string? s)     (hex-lines (io/slurp s :binary true) opts)
    (io/file? s)    (hex-lines (io/slurp s :binary true) opts)
    (bytebuf? s)    (hex-lines (bytebuf-to-list s) opts)
    (sequential? s) (let [{:keys [offset size] :or {offset 0 size :all}} opts
                          vals (if (= size :all) s (take size (drop offset s)))
                          parts-seq (map list
                                         (byte-offsets)
                                         (repeat ": ")
                                         (hex-ascii-lines vals)
                                         (repeat " ")
                                         (ascii-lines vals)
                                         (repeat "\n"))]
                      (map #(apply str %) parts-seq))
    :else           (throw (ex :VncException
                               """
                               Can only hexdump a sequential collection, an \
                               io/file or a string representing a path to a file.
                               """))))

(defn
  ^{ :arglists '("(dump s & opts)")
     :doc """
          Prints a hexdump of the given argument to `*out*`. Optionally supply
          byte offset (:offset, default: 0) and size (:size, default: :all)
          arguments. Can create hexdump from a collection of values, a
          bytebuf, a java.io.File, or a string representing a path to a file.

          Example: `(hexdump/dump (range 100))`

          ```
          00000000: 0001 0203 0405 0607 0809 0a0b 0c0d 0e0f  ................
          00000010: 1011 1213 1415 1617 1819 1a1b 1c1d 1e1f  ................
          00000020: 2021 2223 2425 2627 2829 2a2b 2c2d 2e2f   !\"#$%&'()*+,-./
          00000030: 3031 3233 3435 3637 3839 3a3b 3c3d 3e3f  0123456789:;<=>?
          00000040: 4041 4243 4445 4647 4849 4a4b 4c4d 4e4f  @ABCDEFGHIJKLMNO
          00000050: 5051 5253 5455 5657 5859 5a5b 5c5d 5e5f  PQRSTUVWXYZ[\\]^_
          00000060: 6061 6263                                `abc
          ```
          """
     :examples (list
          "(hexdump/dump [0 1 2 3])"
          "(hexdump/dump (range 1000))"
          "(hexdump/dump (range 10000) :offset 9000 :size 256)"
          """(hexdump/dump "./img.png")""",
          """(hexdump/dump "./img.png" :offset 0 :size 64)"""
          """
          (try-with [ps (io/capturing-print-stream)]
            (binding [*out* ps]
              (hexdump/dump [0 1 2 3])
              (str ps)))
          """ ) }

  dump [s & opts]

  (let [options (apply hash-map opts)]
    (->> (hex-lines s options)
         (apply str)
         (println))))




© 2015 - 2025 Weber Informatics LLC | Privacy Policy