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

Incorrect parsing of file content in latin1 encoding #60

Open
stanojevicboban opened this issue Dec 6, 2021 · 1 comment
Open

Incorrect parsing of file content in latin1 encoding #60

stanojevicboban opened this issue Dec 6, 2021 · 1 comment

Comments

@stanojevicboban
Copy link

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.

@stuartmorgan
Copy link

stuartmorgan commented Dec 7, 2021

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

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

2 participants