3

I am writing a game for iOS that uses .tmx map files. I am creating the maps in the application 'Tiled' and then at some point before they get to iOS, I'm parsing them with Perl.

When I save the files as straight XML, it's a cinch for perl to parse them. However, cocos2d insists that the files be base64-encoded. The 'Tiled' map editor has no problem saving files with this encoding scheme, and iOS reads them just fine, but it's presenting problems for my perl code.

For some reason, the standard MIME::Base64 decode_base64() method in perl is not cutting the mustard here- when I decode the strings, I get one or two binary characters-- question marks in diamond boxes and such.

And the vague documentation for the TMX file format makes it unclear if there is some other encoding going on before or after the base64 encoding which might be causing this problems. I looked at the cpp source for the encoder, and I saw lots of references to Latin1, but I couldn't decipher what's going on in detail.

I noticed that when I tried doing my own tests with MIME::Base64, encoding and then decoding a test string, the encoded text looks dramatically different than that which I see coming out of the TMX files-- for instance, my base64-encoded text for a short string looks like this:

aGVyZSBpcyBhIHNlbnRlbmNl

But the base64-encoded text coming from the TMX files looks like this:

9QAAAAABAAANAQAAGAEAAA==

Any suggestions on what else I might try in attempts to decode a string that looks like that?

todd412
  • 1,308
  • 2
  • 17
  • 24

1 Answers1

3

I think this page might be what you're looking for. It suggests that first you decode_base64, then (if the compression="gzip" attribute is present) use gunzip to uncompress it, and finally use unpack('V*', $data) to extract the list of 4-byte little-endian integers.

cjm
  • 61,471
  • 9
  • 126
  • 175
  • Thanks, that's very helpful. However, it seems I'm still missing something- after base64-decoding the (uncompressed) data, according to their instructions: "you can read 4 bytes at a time for each GID from the beginning of the data stream until the end." - but all I see is a mangled little special character that my text editor can't even display properly- which makes me think there's still something I'm not getting with regards to post-processing the base64-decoded data into a series of numbers. – todd412 Jul 10 '11 at 07:22
  • I'm not a perl guy so I can't tell you exactly what to do, but after base-64 decoding you should have an array or stream with a length that's divisible by 4. You need to read this stream 4 bytes at a time to create 32-bit integers (4 bytes * 8 bits/byte = 32). Its important to understand endian-ness when reading bytes as @cjm pointed out. – Chris Haas Jul 10 '11 at 09:11
  • Thanks to both of you, I think the remaining issues are just things I need to learn about the details of base64 encoding. – todd412 Jul 15 '11 at 04:49