Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cannot read nested array in bencode #13

Open
Kineolyan opened this issue Nov 7, 2021 · 2 comments
Open

Cannot read nested array in bencode #13

Kineolyan opened this issue Nov 7, 2021 · 2 comments

Comments

@Kineolyan
Copy link

From a bencode input "li1ei2el3:stree", I get [1 2 [#object["[B" 0x5351baec "[B@5351baec"]]]
Note: the input was created using write-bencode and seems correct with respect to the spec

Reproducing code:

(ns b
  (:require [bencode.core :as b]))

(defn bread
  [content]
  (-> (.getBytes content "UTF-8")
      java.io.ByteArrayInputStream.
      java.io.PushbackInputStream.
      b/read-bencode))

(bread "li1ei2el3:stree")

I am using version from babashka 0.6.4, so I guess this is the latest.

@Kineolyan
Copy link
Author

A quick check showed that this is not an issue for nested maps.

@Kineolyan
Copy link
Author

So, after investigating this issue, I found some magic performed in the test: the function <bytes. As I understand it, it transforms the raw bytes that were decoded into beautiful UTF-8 strings. As I am writing this, it seems completely legit, because the string can have any charset.

Though I just discovered this library, I would say that converting every byte-array to an original string form is quite common. I would gladly appreciate a nice utility function to convert all those bytes to string, using a custom function.
I see that it applies to both bencode and netstring.
What do you think?

(defmulti #^{:private true} <bytes class)
(defmethod <bytes :default
[input]
input)
(defmethod <bytes (RT/classForName "[B")
[#^"[B" input]
(String. input "UTF-8"))
(defmethod <bytes clojure.lang.IPersistentVector
[input]
(vec (map <bytes input)))
(defmethod <bytes clojure.lang.IPersistentMap
[input]
(->> input
(map (fn [[k v]] [k (<bytes v)]))
(into {})))

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant