You guess, following the industry's best practices http://xkcd.com/221/

You have no information on the encoding of any given file, unless you're willing to go look at XML headers or HTML meta tags and whatnot (and lol if you believe those are going to be correct). If you can't parse it as Unicode, then maybe you can try to use the current user's locale (assuming this is not Unicode because it's used by a group of people with another de facto encoding).

As a last resort, you can do something `libmagic`- or `libicu`-like by looking at the content of the pre- or post-image and guessing based on that.