0

Currently jpeg images are stored as VARBINARY with HEX values:

0xFFD8FFE.......

So I convert it back to binary and display the images. For the proper jpeg VARBINARY, the HEX ends with FFD9 and everything works. However, I noticed many rows have extra characters at the end. So far I've encountered with extra 0200 at the end, so the HEX is of the form:

0xFFD8FFE.......FFD90200

I want to know what is the extra 0200, and whether it's the only thing that can be added (from any reason), or I should expect some other unpredictable extra characters?

jarlh
  • 42,561
  • 8
  • 45
  • 63
pileup
  • 1
  • 2
  • 18
  • 45
  • 2
    Which dbms are you using? – jarlh Aug 16 '22 at 17:26
  • 3
    Show us your INSERT. – jarlh Aug 16 '22 at 17:26
  • @jarlh the problem is, it's separate team in charge of the DB and images inserts, I will have to ask them tomorrow. The DB is MS SQL – pileup Aug 16 '22 at 17:27
  • 1
    Are you on Windows OS? – jarlh Aug 16 '22 at 17:28
  • 3
    This sounds like it's most likely something going wrong with your application, not something that somebody with no access to your code could figure out. – IMSoP Aug 16 '22 at 17:29
  • Yes Windows OS and also Windows Server. @IMSoP thank you. Does the `0200` look like a file type, like jpeg? Or perhaps somewhere in the code it could be inserting, maybe the HTTP status code for example? Also, is there any way I can check for known file types in HEX so that I can look for similar patters? – pileup Aug 16 '22 at 17:29
  • 1
    https://stackoverflow.com/a/50968/11683 – GSerg Aug 16 '22 at 17:32
  • 1
    I'm not sure what you mean by file type - a file type is just a way of interpreting some data. There are sometimes patterns of data that you can see and go "hm, I bet that's intended to be read as a JPEG" or whatever, but this is just two bytes, a string of bits that looks like this: `0000001000000000`. That's really not a lot to go on. It certainly isn't likely to be a whole file in its own right. – IMSoP Aug 16 '22 at 17:33
  • @GSerg when I convert the hex to binary with php's `hex2bin`, it won't work when the extra `0200` is added. IMSoP, I'm sorry if I sound completely off and wrong, it's just that it's the first time I'm dealing with it. I thought that when JPEG files are converted to HEX, they should end with `FFD9`, but you say it could be interpreted to end with other characters, depending on who does that? But then in my case, why do all of the files end with `FFD9` (And some extra 0200?), because it's the same interpreter? – pileup Aug 16 '22 at 17:40
  • 2
    @BGWay Does it open fine if you remove the 0200 *in the file* using a hex editor? – GSerg Aug 16 '22 at 17:42
  • I don't have hex editor installed, will have to request it. But what I do is to convert it to binary with PHP then `echo` the result with headers `Content-Type: image/jpeg` and it shows the image only when removing the `0200` – pileup Aug 16 '22 at 17:45
  • 1
    Where does it say that JPEG ends always with `FFD9`? Have you actually tried opening it, is it corrupted? Any case, maybe it's not a JPEG? – Charlieface Aug 16 '22 at 17:48
  • 1
    I assume you are viewing with a browser. Does it open in another browser? In another stanadlone viewer? Are you sure the 0200 is there in the first place, maybe you are providing `Content-Lenght` that is 2 bytes short so in order to transmit the full picture you need the extra two bytes of payload? – GSerg Aug 16 '22 at 17:48
  • 1
    @Charlieface If you click through https://stackoverflow.com/questions/73377970/why-does-the-database-have-extra-characters-at-the-end-of-the-varbinary-hex#comment129585538_73377970 -> https://stackoverflow.com/a/50968/11683 -> https://en.wikipedia.org/wiki/JPEG#Syntax_and_structure... – GSerg Aug 16 '22 at 17:49
  • @GSerg, I do view it with a browser, however in order to view it, I use a PHP script to interpret it: I copy the HEX string from the DB, then I use `hex2bin`, then I `echo` it with the `jpeg` headers, like so: `$string = "....FFD9"; header("Content-Type: image/jpeg"); echo hex2bin($string);`. Then I open the PHP file in the browser and it shows the image. When I change the string to `....FFD90200`, the image doesn't show – pileup Aug 16 '22 at 18:00
  • The fact that all valid JPEGs end in FFD9 doesn't mean that all files ending in FFD9 are valid JPEGs, or that all files ending in 0020 (or FFD90020) can be recognised as some other file type. There might be hundreds of different file types that _could_ end that way. It's more common to look at the _start_ of a file to guess its intended file type, but even that is just a guess, not a guarantee - e.g. I can easily save a file that begins the same way as a JPEG, but isn't a valid image. All of which is _probably_ irrelevant in this case - the data has probably just got corrupted somehow. – IMSoP Aug 17 '22 at 15:54
  • @IMSoP forgot to mention that all the files also start with `FFD8FFE0`, sorry. But, I managed to get it working by directly converting the VARBINARY to Base64. Apparently I did not need the step `VARBINARY HEX->BINARY->Base64`. My mistake was that, in the DB I see the HEX, but when I fetch the data it's already BINARY as the type implies. So I directly convert `VARBINARY->Base64` and it works even when the HEX ends with `0200` – pileup Aug 17 '22 at 19:53

0 Answers0