mime icon indicating copy to clipboard operation
mime copied to clipboard

Incorrect parsing of file content in latin1 encoding

Open stanojevicboban opened this issue 3 years ago • 1 comments

It seems that if I try to read an image file in order to retrieve magic numbers from it so I could better detect the mime type of the file, iOS doesn't appear to be friendly about it.

I am using the following code to extract the first 20 hex codes from the file:

List<int> fileBytes = File(path).readAsBytesSync().toList();
List<int> header = [];
int i = 0;
String output = '';
fileBytes.forEach((element){
  if(element == 0) return;
  if (i > 20) return;
  output += element.toRadixString(16) + " ";
  header.add(element);
  i++;
});

In addition to that, I am also trying to read the file content into a string, with the code below:

String output = File(path).readAsStringSync(encoding: latin1);

In order to replicate the issue, I am using this image as an example.

On Android the header variable gives me the following hex:

52 49 46 46 b0 5b 57 45 42 50 56 50 38 20 a4 5b 10 c6 1 9d 1

Looking at the list of magic numbers this hex codes is for the WebP image file, which is correct.

Even the file content (the content variable) starts with: android

However, running the same code on iOS, produces this for hex:

89 50 4e 47 d a 1a a d 49 48 44 52 2 bc 1 90 8 6 4c 3c

This corresponds to the PNG image format, which is wrong. Even the file content is incorrect, as it start as the code below: ios

I can say for certain that the problem is not with the iOS emulator as the same issue appears on a physical device. It is also appearing on iOS 14+ version (as I know iOS just recently introduced WebP support). Regardless, I don't believe the issue is with the image format support.

I am having a hard time understanding why the same file is being read so differently on iOS. On android, it is read exactly as it's expected, while on iOS it's like the OS is converting the file to a format it understand and gives that to the flutter app to use.

So, if there's anyone who can give me a better explanation on why this happens, I would be extremely grateful. I have also submitted this issue on the flutter bug tracker.

stanojevicboban avatar Dec 06 '21 09:12 stanojevicboban

This should be close as a duplicate; as explained in the other issue this is a behavior of a plugin, not of file parsing.

stuartmorgan-g avatar Dec 07 '21 03:12 stuartmorgan-g