0

I am using this bencode https://github.com/dampcake/bencode to decode a torrent file. I am having an issue : the encoded torrent file looks something like this :

d8:announce21:http://127.0.0.1:  ....etc.....  piece lengthi65536e6:pieces28300:a�ډ|E���� ���#-14   .....etc........

The thing is that when I enter this string in the 'decoder', I get an error because of the � symbols. Here is my question: should I stop decoding just before those symbols ? Or is the whole string necessary to properly decode the .torrent file ?

From what I've read, I need to stop the decoding at the end of the dictionary, ie. when I encounter the final 'e', but I don't know how to properly identify it..

Thanks

UPDATE:

Here is my code :

byte[] to_decode = null;

            try {
                 Path path = Paths.get("/user/.../file.torrent");
                 to_decode = Files.readAllBytes(path);                    

            } catch (IOException e) {
                System.out.println(e.toString());
            }

            //System.out.println(to_decode.toString());

            Bencode bencode = new Bencode();
            Map<String, Object> dict = bencode.decode(to_decode, Type.DICTIONARY);

            System.out.println(dict);

When I run it, I have no errors but this kind of output:

f<�>�0�1FT���n" ......etc......  4'}$�Q�3�� Җk�, private=0}}

So, considering the brackets, I think the output is a dictionary but not in a usable format, and I can't seem to make it work

Any advice ?

stevemju
  • 1
  • 1
  • It has nothing to do with java, does it? – zubergu Oct 18 '17 at 09:20
  • It's because I am developing a torrent client in java so I entered java without thinking about it, but yes, I forgot to add the proper tags... – stevemju Oct 18 '17 at 09:37
  • @stevemju welcome to stack overflow. In future questions, please provide details of error - java stack trace and java code you are using. This will help identifying cause. You are also free to edit this questions providing more details. – Piro Oct 18 '17 at 10:56

1 Answers1

0

Following specification https://en.wikipedia.org/wiki/Bencode 6:pieces28300:a means there is a 28300 bytes long string. So it should be parsed too. You should stop at the end of dictionary but it is not in 6:pieces28300:a (it is at the end).
Both length and � indicate that you are dealing with binary data. You do not specify error, neither source code you are using, but you are using wrong character encoding. So check character encoding of encoded torrent file data and make sure to use same encoding in your Bencode constructor.

Piro
  • 1,367
  • 2
  • 19
  • 40
  • Thanks for your answer. Now I seem to have an issue with reading the file : i use `Path path = Paths.get("path/file.torrent");` `to_decode = Files.readAllBytes(path);` and when I print to_decode I have the following value : `[B@42a57993` Is it the adress of the variable ? If so, how do I use its value ? – stevemju Oct 18 '17 at 10:39
  • [B is a class name for array of bytes, part after @ is hashing result of that array. This is default result of Object.toString() method. You either want to use bytes as is or create a String from bytes. – Piro Oct 18 '17 at 10:51
  • Exactly as Piro says - simply create a String from the Byte Array via new String(to_decode) and print it. – Schmitzi Nov 18 '19 at 15:20