RANDOM BITS

A random site by a random clueless human
Random bits of programming, math, and thoughts By a clueless human          Random bits of programming, math, and thoughts By a clueless human

The Unintended Consequence of Replacing CRLF (\r\n) to LF (\n) on PNG Images

December 11, 2025

Today there was a PR at work that tried to address the issue of engineers submitting code with DOS (Window) end of line (EOL) to the codebase.


Background (Skip if you are a Programmer)

\r and \n are what we call control characters, non-printable characters that have effects to data and devices. For instance, \a will ring a bell, \n (line feed) will cause the cursor to go to the next line and \r (carriage return) causes the cursor to return to the first character in the same line.

DOS (Microsoft’s text-based OS) uses \r\n to indicate an end of line (which we commonly refer to as CRLF) meanwhile, UNIX and its variants and sucessors use \n to indicate the end of file (commonly refer to as LF or newline in layman terms). CRLF notation makes more sense from a historical point of view (i.e. teletype and typewriters) but it also takes unnecessarily an extra character.


Anyhow, long story short, I noticed that the PR and subsequent PRs relating to convert DOS to UNIX file format broke PNG since the first 8 Bytes of all PNG (the header, also called the magic number) contains 0D 0A (CR LF):

89 50 4E 47 0D 0A 1A 0A 

Use xxd or hexdump on any PNG file and you will discover this yourself..

$ convert -size 32x32 xc:white empty.png
$ xxd -l 8 empty.png  | grep 0d0a
00000000: 8950 4e47 0d0a 1a0a                      .PNG....

Converting to UNIX style causes the PNG file to no longer be an image:

$ file empty.png 
empty.png: PNG image data, 32 x 32, 1-bit grayscale, non-interlaced
$ dos2unix -f empty.png 
dos2unix: conversion du fichier empty.png au format Unix…
$ file empty.png 
empty.png: data