Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>I found this in other question and put together what solved the issue for me. Hope you find this helpful.</p> <p>The PNG format has several built in checks. Each "chunk" has a CRC32 check, but to check that you'd need to read the full file.</p> <p>A more basic check (not foolproof, of course) would be to read the start and ending of the file.</p> <p>The first 8 bytes should always be the following (decimal) values { 137, 80, 78, 71, 13, 10, 26, 10 } (ref). In particular, the bytes second-to-fourth correspond to the ASCII string "PNG".</p> <p>In hexadecimal:</p> <pre><code>89 50 4e 47 0d 0a 1a 0a .. P N G ........... </code></pre> <p>You can also check the last 12 bytes of the file (IEND chunk). The middle 4 bytes should correspond to the ASCII string "IEND". More specifically the last 12 bytes should be (in hexa):</p> <pre><code>00 00 00 00 49 45 4e 44 ae 42 60 82 ........... I E N D ........... </code></pre> <p>(Strictly speaking, it's not really obligatory for a PNG file to end with those 12 bytes, the IEND chunk itself signals the end of the PNG stream and so a file could in principle have extra trailing bytes which would be ignored by the PNG reader. In practice, this is extremely improbable).</p> <p>Here is an implementation:</p> <pre><code>- (BOOL)dataIsValidPNG:(NSData *)data { if (!data || data.length &lt; 12) { return NO; } NSInteger totalBytes = data.length; const char *bytes = (const char *)[data bytes]; return (bytes[0] == (char)0x89 &amp;&amp; // PNG bytes[1] == (char)0x50 &amp;&amp; bytes[2] == (char)0x4e &amp;&amp; bytes[3] == (char)0x47 &amp;&amp; bytes[4] == (char)0x0d &amp;&amp; bytes[5] == (char)0x0a &amp;&amp; bytes[6] == (char)0x1a &amp;&amp; bytes[7] == (char)0x0a &amp;&amp; bytes[totalBytes - 12] == (char)0x00 &amp;&amp; // IEND bytes[totalBytes - 11] == (char)0x00 &amp;&amp; bytes[totalBytes - 10] == (char)0x00 &amp;&amp; bytes[totalBytes - 9] == (char)0x00 &amp;&amp; bytes[totalBytes - 8] == (char)0x49 &amp;&amp; bytes[totalBytes - 7] == (char)0x45 &amp;&amp; bytes[totalBytes - 6] == (char)0x4e &amp;&amp; bytes[totalBytes - 5] == (char)0x44 &amp;&amp; bytes[totalBytes - 4] == (char)0xae &amp;&amp; bytes[totalBytes - 3] == (char)0x42 &amp;&amp; bytes[totalBytes - 2] == (char)0x60 &amp;&amp; bytes[totalBytes - 1] == (char)0x82); } </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload