1

I am trying to replace CRLF with LF (see reason @ Read binary stdout data from adb shell?)

A brief summary of the above referenced thread is that basically when piping a screenshot from an Android device via the Android debug bridge, it looks like the line feeds line endings in the data stream are being replaced with carriage return line feeds, therefore I am receiving a corrupted file at the other end of the pipe. What has worked for others is undoing the replacement via code, as below, but it doesn't seem to be working from me.

My code is still spitting a corrupted file... any ideas why?

++ I know the code isn't as clean and efficient as it can be, will fix up after so please hold the comments related to my coding skill, or lack thereof.

Thanks

        static void Main(string[] args)
        {
            // adb shell screencap -p > <path>
            string path = @"<filepath>\screen.png";

            var fs = new FileStream(path, FileMode.Open);
            var fsw = new FileStream(path.Replace(".png", "_fixed.png"), FileMode.Create);

            var buffer = new byte[fs.Length];
            fs.Read(buffer, 0, buffer.Length);
            fs.Flush();
            fs.Close();

            var switched = Repair(buffer);
            fsw.Write(switched, 0, switched.Length);
            fsw.Flush();
            fsw.Close();

            Console.WriteLine(buffer.Length);
            Console.WriteLine(switched.Length);

            Console.Read();
        }

        static byte[] Repair(byte[] enc)
        {
            var bstr = new MemoryStream();
            for (int i = 0; i < enc.Length; i++)
            {
                if (enc.Length > i + 1 && enc[i] == 0x0d && enc[i + 1] == 0x0a)
                {
                    bstr.WriteByte(0x0a);
                    i++;
                }
                else bstr.WriteByte(enc[i]);
            }

            bstr.Flush();
            bstr.Close();

            return bstr.ToArray();
        }
Community
  • 1
  • 1
DawnFreeze
  • 164
  • 1
  • 3
  • 13
  • 1
    Do I understand correctly that you're trying to change line endings in binary data? If so, what guarantee do you have that the particular byte sequence you're changing is embedded text and not binary values? – adv12 Feb 12 '15 at 20:16
  • @adv12 Yes. File format is PNG. For more info see linked thread. I ported this over from working Java which is essentially doing the same thing successfully... was wondering if there are some C# guru's who might be able to spot any mistakes or further insight. Cheers - DF. – DawnFreeze Feb 12 '15 at 20:20
  • What do you mean by corrupted exactly? – Blorgbeard Feb 13 '15 at 03:14
  • Make sure you do check the return value from `fs.Read(buffer, 0, buffer.Length);`. It can sometimes be less than what has been requested (`buffer.Length`). Can you post the two files (correct png and corrupt png)? – tofi9 Feb 13 '15 at 03:15
  • BTW, I suggest you put a short explanation of why you're doing this at the top of your question, since a lot of people are not reading the linked thread very closely :) – Blorgbeard Feb 13 '15 at 03:16
  • Good point, thanks - DF – DawnFreeze Feb 13 '15 at 08:37
  • @tofi9 That's the annoying part, I haven't rooted my device yet, so I cant save the valid png and compare the binary... so I am unable to 'reverse engineer' the problem. – DawnFreeze Feb 13 '15 at 08:45
  • @DawnFreeze I'm not sure how the problem is related to rooting. Why don't you take a picture on your android using whatever (download it, screenshot, camera picture) and then download it off your device in two different ways: 1) `adb pull /path/to/file.png` and 2) `adb shell cat /path/to/file.png > corrupt.png`. You could even try it with a file that has a sequence of `0x00,0x01....0xff` and see what's happening there – tofi9 Feb 13 '15 at 14:13
  • @tofi9 Yea ok good point. It just wasnt letting me write to the device via adb but I suppose I could take a screenshot and pull it... I was just trying to keep things consistent, i.e. adb screenshot > save local > pull / adb screenshot > push to pc, as any single difference when comparing the binaries would throw off the compare. Will give it a shot. Thank you - DF – DawnFreeze Feb 13 '15 at 20:42
  • @DawnFreeze try writing to `/sdcard/...`, or if you have a physical sdcard, well... write to it via the pc. Also, this exercise can be done on the android simulator too. – tofi9 Feb 13 '15 at 21:25
  • @tofi9 Those are good ideas. Will give it a shot and update the question. Tyvm - DF – DawnFreeze Feb 13 '15 at 22:48

0 Answers0