WEBVTT
Kind: captions
Language: en

00:00:00.000 --> 00:00:02.450
Yes! At least in theory.

00:00:02.450 --> 00:00:07.220
And I'm not talking about just putting the URL to some online game in; you can obviously do that.

00:00:07.220 --> 00:00:11.060
I mean 100% of the game's code stored entirely

00:00:11.060 --> 00:00:12.590
on one of these.

00:00:12.590 --> 00:00:17.770
As surprising as it is to say, there theoretically is absolutely no reason you couldn't.

00:00:17.770 --> 00:00:21.810
A QR code is technically just a storage medium like any other.

00:00:21.810 --> 00:00:25.850
It's just as legitimate as a floppy disk, CD, or even hard drive.

00:00:25.850 --> 00:00:29.630
It doesn't store much data, but it is data nonetheless.

00:00:29.630 --> 00:00:33.090
Keep in mind the bar for data storage is actually pretty low.

00:00:33.090 --> 00:00:37.660
You could also manually write out binary zeros and ones or hex bytes by hand.

00:00:37.660 --> 00:00:41.540
It's not very efficient, but technically you've stored computer data.

00:00:41.540 --> 00:00:45.220
And while a QR code is most commonly used to store ASCII text,

00:00:45.220 --> 00:00:48.280
it can actually be made to store binary data as well.

00:00:48.280 --> 00:00:55.280
Which pretty much means anything you can store on a computer, you can store on a QR code - provided that it fits inside the size limitations.

00:00:55.280 --> 00:00:56.570
How limited are we?

00:00:56.570 --> 00:01:01.970
Well QR codes come in various standardized sizes, but the largest is version 40

00:01:01.970 --> 00:01:05.290
which gives us an ABSOLUTELY LEVIATHAN

00:01:05.290 --> 00:01:08.960
2,953...

00:01:08.960 --> 00:01:10.340
...bytes.

00:01:10.340 --> 00:01:15.330
Yeah, slightly less than 3 KB. It's not much.

00:01:15.330 --> 00:01:19.440
If you're curious just how not much, this sound from Windows 10...

00:01:19.440 --> 00:01:20.180
[short click]

00:01:20.180 --> 00:01:25.320
A sound that lasts for 1/15th of a second, this sound...

00:01:25.320 --> 00:01:26.010
[short click]

00:01:26.010 --> 00:01:29.530
...is 11 KB. Yeah.

00:01:29.530 --> 00:01:35.130
Or remember floppy disks? The ones I remember are these: the 3.5" high-density floppies.

00:01:35.130 --> 00:01:38.370
Remember needing like twenty of them to install Windows 95?

00:01:38.370 --> 00:01:45.540
Well, those things are 1.44 MB or 1,474,560 bytes.

00:01:45.540 --> 00:01:50.240
Which means just one of those floppy disks can store not one, not two,

00:01:50.240 --> 00:01:54.900
but almost 500 version 40 QR codes worth of data.

00:01:54.900 --> 00:01:59.950
So... can we really fit a whole game into just one?

00:01:59.950 --> 00:02:05.400
Well, while pictures, music, video, and... operating systems do take up a lot of storage space,

00:02:05.400 --> 00:02:08.590
simple code can actually be surprisingly economical.

00:02:08.590 --> 00:02:14.130
And while 3 KB is pretty small even then, people have made games with far less.

00:02:14.130 --> 00:02:19.390
Going back to floppy disks, did you know that people have managed to fit games into a floppy disk's boot sector?

00:02:19.390 --> 00:02:23.370
And if you don't know how small that is, it's only 512 bytes.

00:02:23.370 --> 00:02:26.990
Roughly a sixth of what we're dealing with with QR codes.

00:02:26.990 --> 00:02:31.560
If you can fit a game into 512 bytes, you can fit a game into 3,000.

00:02:31.560 --> 00:02:33.410
Or look at the Atari 2600.

00:02:33.410 --> 00:02:39.960
A huge amount of those cartridges could only store 4 KB, so games had to fit into that or less.

00:02:39.960 --> 00:02:44.660
So in theory, there's absolutely no reason we couldn't store a perfectly

00:02:44.660 --> 00:02:49.130
playable - if somewhat basic - game onto a QR code.

00:02:50.060 --> 00:02:51.260
So let's do it!

00:02:51.260 --> 00:02:57.580
Our first decision is what game to write, and since this is just for science, we may as well implement something that already exists.

00:02:57.580 --> 00:03:00.510
My go-to simple game is Tetris - I love Tetris.

00:03:00.510 --> 00:03:04.480
But I've already talked about how litigious the Tetris company can be.

00:03:04.480 --> 00:03:09.100
And since I'd like to make this QR code available to the public, I ain't goin' near that.

00:03:09.100 --> 00:03:13.950
I'm gonna go for the game that anyone who had one of these will undoubtedly remember fondly.

00:03:13.950 --> 00:03:15.840
That's right, the one and only...

00:03:15.840 --> 00:03:16.770
Snake.

00:03:16.770 --> 00:03:22.520
Extremely simple but still just fun enough to fend off boredom in the pre-smartphone era.

00:03:22.520 --> 00:03:26.470
Our next decision is platform, and while it seems obvious to write a smartphone app,

00:03:26.470 --> 00:03:29.520
since that's what QR codes are usually used for nowadays,

00:03:29.520 --> 00:03:33.690
I have some doubts that I'll be able to make something within the size limitations on mobile.

00:03:33.690 --> 00:03:40.600
Additionally, the more locked down nature of smartphones I think would make it a lot harder to get an executable to run from a QR code.

00:03:40.900 --> 00:03:42.740
Which is probably a good thing now that I think about it.

00:03:42.740 --> 00:03:45.940
So I'm just gonna make this a Windows app instead,

00:03:45.940 --> 00:03:49.500
with the idea being that you could read the QR code from the computer's webcam.

00:03:49.500 --> 00:03:54.650
Fitting an entire Windows executable into those 2,900-whatever bytes.

00:03:54.650 --> 00:03:58.670
Before we get to that though, I will mention a really easy way you could pull this off.

00:03:58.670 --> 00:04:02.400
You could just write it in HTML and JavaScript.

00:04:02.400 --> 00:04:05.300
In fact, just for demonstration, I did!

00:04:05.300 --> 00:04:10.030
This is Snake written in JavaScript, with some HTML and CSS to pull it all together of course.

00:04:10.030 --> 00:04:13.690
Once it's been minified, which basically means removing everything not strictly required,

00:04:13.690 --> 00:04:16.920
it fits snugly into our QR code size limit.

00:04:16.920 --> 00:04:20.000
You'll notice I was even able to fit some comfort features in, like

00:04:20.000 --> 00:04:25.610
writing extra code to draw the snake as long and thin rather than a big blob of white squares or dots,

00:04:25.610 --> 00:04:30.220
or queuing up button presses so that if you press buttons in quick succession, they'll still get recognized,

00:04:30.220 --> 00:04:35.150
and giving you one extra grace turn to move if you're just about to game over.

00:04:35.150 --> 00:04:37.090
Just like the old phones used to do.

00:04:37.090 --> 00:04:41.700
So technically we're already sort of done.

00:04:41.700 --> 00:04:43.720
But it kind of feels like cheating, right?

00:04:43.720 --> 00:04:47.450
While it is actual code and not just a website URL or something,

00:04:47.450 --> 00:04:50.140
you still need a modern browser installed which...

00:04:50.680 --> 00:04:52.810
Wait that's not a modern browser, get that out of here!

00:04:52.810 --> 00:04:58.170
So yeah, modern browsers, which - let's be honest - are kind of doing a lot of the work for us.

00:04:58.170 --> 00:05:00.480
Somehow it just didn't feel right.

00:05:00.480 --> 00:05:02.830
So we're gonna make this a native executable.

00:05:02.830 --> 00:05:05.230
At first glance, this would appear to be impossible.

00:05:05.230 --> 00:05:11.040
A super simple "Hello World" program written in C already consumes 100 KB.

00:05:11.040 --> 00:05:17.260
But most of this is actually not our code at all, it's all the runtime libraries that the compiler is linking by default.

00:05:17.260 --> 00:05:23.660
We can disable this with the /NODEFAULTLIB option, which does mean that we have to specify the libraries we do need manually,

00:05:23.660 --> 00:05:29.000
but doing this cuts the file size all the way down to 3 KB. A great start.

00:05:29.000 --> 00:05:36.510
Now we could probably get it down further with more compiler options, but what I'm actually gonna try is forgoing the C compiler entirely,

00:05:36.510 --> 00:05:38.290
and writing this thing...

00:05:38.290 --> 00:05:40.680
...in x86 assembly.

00:05:40.680 --> 00:05:44.370
[dramatic music]

00:05:44.370 --> 00:05:49.190
Yep, as close to the raw machine code as is generally possible.

00:05:49.190 --> 00:05:53.470
I touched on this in videos like my Super Mario 64 compiler optimizations video.

00:05:53.470 --> 00:05:56.650
C code, while it may look technical to the uninitiated,

00:05:56.650 --> 00:05:59.140
is very much designed to be human friendly.

00:05:59.140 --> 00:06:04.270
For your computer to run it, it must be translated into machine code by a compiler,

00:06:04.270 --> 00:06:08.640
and while compilers are generally very good at what they do, they're still not perfect,

00:06:08.640 --> 00:06:11.120
and most importantly, they're not human.

00:06:11.120 --> 00:06:15.070
They can't really think about the best ways to translate and optimize your app.

00:06:15.070 --> 00:06:18.540
They can only really do what their developers have told them to do.

00:06:18.540 --> 00:06:24.390
As such, generally a human will always be able to write lighter and faster machine code than a compiler.

00:06:24.390 --> 00:06:30.420
Which is why most video games, up until the mid-90s or so, were almost always written in assembly.

00:06:30.420 --> 00:06:37.170
The slight inefficiencies introduced by compilers was just too detrimental to performance on the hardware of that era.

00:06:37.170 --> 00:06:43.310
Now I'm planning to write an extremely simple game and run it on modern hardware, so clearly performance isn't really an issue.

00:06:43.310 --> 00:06:50.730
But writing in assembly also gives us a lot of control over the size of the binary since we're kind of writing all the bytes ourselves.

00:06:50.730 --> 00:06:55.410
So even though I am a lot more familiar with C, this really seems like the way to go.

00:06:55.410 --> 00:07:02.640
And with absolutely no outside help at all, I soon had an assembly program that uses the Win32 API to make a window.

00:07:02.640 --> 00:07:06.270
And all within 2.5 KB.

00:07:06.270 --> 00:07:13.070
Yeah we're actually already pretty dangerously close to our limit and we haven't even written any game code yet.

00:07:13.070 --> 00:07:18.520
I trimmed a few unnecessary lines out and found the file size had jumped all the way down to 2 KB.

00:07:18.520 --> 00:07:20.650
That's way more code than I actually removed.

00:07:20.650 --> 00:07:24.620
When I put the lines back, it jumped back up to 2.5. Weird.

00:07:24.620 --> 00:07:33.790
So it turns out the Win32 PE format, which is the format of pretty much all Windows executables these days, is by default aligned to 512 bytes.

00:07:33.790 --> 00:07:37.160
And it's also listed as the minimum alignment possible.

00:07:37.160 --> 00:07:43.080
This made me a little nervous since it means we don't just have to get our code below 2.88 KB,

00:07:43.080 --> 00:07:48.580
we actually have to get it below 2.5 since anything over will round up to 3.

00:07:48.580 --> 00:07:54.520
I went ahead anyway until I had a white dot that could be moved around the screen, but I was already at 3 KB.

00:07:54.520 --> 00:07:57.650
Already over our limit with still barely any game.

00:07:57.650 --> 00:07:59.550
So... is that it then?

00:07:59.550 --> 00:08:04.290
Well not quite, 512 maybe the default and even the documented minimum,

00:08:04.290 --> 00:08:06.830
but Windows will actually accept almost any.

00:08:06.830 --> 00:08:09.650
We just need to override the default in our linker.

00:08:09.650 --> 00:08:14.390
Unlucky for me, the linker I've been using doesn't seem to have byte alignment options.

00:08:14.390 --> 00:08:18.500
I was using this one since it made it super easy to link with existing Win32 DLLs,

00:08:18.500 --> 00:08:22.620
but seeing as size is a priority, it looks like we'll have to swap out with another,

00:08:22.620 --> 00:08:25.210
which meant having to make some changes to my code.

00:08:25.210 --> 00:08:28.830
Luckily they weren't too hard and soon we were linking with MSVC.

00:08:28.830 --> 00:08:35.590
I couldn't remove the byte alignment, I could only reduce it to 16 bytes since that was the alignment of the Win32 libraries I was using.

00:08:35.590 --> 00:08:40.200
But that still got us down to 2.66 KB which is definitely an improvement.

00:08:40.200 --> 00:08:44.130
Unfortunately, it still didn't take long to reach the limits once again.

00:08:44.130 --> 00:08:50.200
 2.9 is already over and we still only barely have a game here.

00:08:50.630 --> 00:08:55.160
[exhales] You know, there comes a time when one must acknowledge their limitations.

00:08:55.160 --> 00:09:00.990
Yes, technically I can write x86 assembly, but do I really know what I'm doing?

00:09:01.560 --> 00:09:03.490
[whispers] No...

00:09:03.490 --> 00:09:09.020
Do I know all of the tricks or best practices to make the smallest binaries possible? Not really.

00:09:09.020 --> 00:09:12.240
I probably know a lot less than the developers of a C compiler do.

00:09:12.240 --> 00:09:16.850
And I'm sure I could learn them, but then we'd be here for another few months I'm sure.

00:09:16.850 --> 00:09:23.050
I decided I'd bitten off more than I could chew here, and I was curious to see if a C compiler set up the right way,

00:09:23.050 --> 00:09:26.100
would be able to write more efficient assembly code than me.

00:09:26.100 --> 00:09:29.930
So I rewrote everything in C, which was actually easier than it sounds,

00:09:29.930 --> 00:09:32.780
and yep, that pretty much says it all.

00:09:32.780 --> 00:09:41.320
So while theoretically a human being can always write better assembly code than a compiler, that doesn't necessarily mean that they will.

00:09:41.320 --> 00:09:49.620
So I decided to carry on in C. I'm much more comfortable with it and I just have more faith that I could write cleaner C code than I could assembly code.

00:09:49.620 --> 00:09:54.930
The whole time I was keeping a close eye on the executable size, which in itself was pretty nervewracking since

00:09:54.930 --> 00:09:57.640
it was now all at the mercy of the compiler.

00:09:57.640 --> 00:10:02.820
A single new line of C code could add anywhere from a few bytes to a few hundred,

00:10:02.820 --> 00:10:06.510
and I was constantly trying to find ways to do more with less code.

00:10:06.510 --> 00:10:09.300
But finally, eventually...

00:10:09.860 --> 00:10:10.790
...I failed.

00:10:10.790 --> 00:10:19.420
I got Snake working just how I wanted it to but my executable was 3.1 KB - way more than 2.88.

00:10:19.420 --> 00:10:24.720
I did write in my comfort features - the drawing code, the button queuing, the brief bite grace period -

00:10:24.720 --> 00:10:29.620
and I did play around with compromising these things to try to get it below 3.1.

00:10:29.620 --> 00:10:32.690
But doing that always made the game feel a lot less playable.

00:10:32.690 --> 00:10:36.910
I know that is the kind of sacrifice you have to make with extreme size limitations like these,

00:10:36.910 --> 00:10:41.070
but I felt like there had to be more I could do elsewhere before I started cutting code out.

00:10:41.070 --> 00:10:47.530
Even though this whole thing is just a QR code experiment, I really hoped the game you would get would be at least kinda decent.

00:10:47.530 --> 00:10:50.830
So I started looking at other avenues of shrinking down the binary.

00:10:50.830 --> 00:10:54.000
We only need about 200 bytes or so.

00:10:54.000 --> 00:11:01.270
I looked into some pretty complicated unofficial modifications you could make to the PE header, but the all kinda went over my head.

00:11:01.270 --> 00:11:05.150
And they could also potentially break compatibility with some versions of Windows.

00:11:05.150 --> 00:11:07.790
It was at this point I started thinking about compression.

00:11:07.790 --> 00:11:13.850
It did feel a little like cheating, but seeing as I didn't want to cut functionality, it looked like it was going to be the only choice.

00:11:13.850 --> 00:11:19.330
Now I didn't want to put it in a zip, because I wanted the experience of just getting an executable and running it.

00:11:19.330 --> 00:11:22.140
No unzipping or anything else required.

00:11:22.140 --> 00:11:26.970
But luckily there are other ways to compress, particularly when it comes to executables.

00:11:26.970 --> 00:11:32.950
"Executable packing" is a means of compressing an executable and then attaching it to a small decompressor program

00:11:32.950 --> 00:11:36.300
that unpacks and runs the original program on startup.

00:11:36.300 --> 00:11:40.850
To the user, this is completely transparent and just looks like you're running an ordinary app.

00:11:40.850 --> 00:11:44.720
Compression is heavily used in the demoscene to make really tiny demos.

00:11:44.720 --> 00:11:50.260
64 KB, 4 KB, or even 1 KB demos which I'm showing here.

00:11:50.260 --> 00:11:54.120
I look at executable packing as kind of like the Auto-Tune of the demoscene.

00:11:54.120 --> 00:11:59.080
Yes it's technically kind of cheating, but it's also really clever technology, and

00:11:59.080 --> 00:12:02.550
everyone's using it so you're kind of crippling yourself if you don't use it.

00:12:02.550 --> 00:12:06.190
First, I tried UPX since that was the only one I was already familiar with.

00:12:06.190 --> 00:12:12.720
But it didn't like my custom alignment and disabling it blew our Snake program up to 4.5 KB,

00:12:12.720 --> 00:12:15.320
which UPX could only shrink to 4.

00:12:15.320 --> 00:12:19.520
Yeah ironically this means compression has made our file larger.

00:12:19.520 --> 00:12:24.570
But soon, I discovered the program I should have been using. The same one that the demoscene uses:

00:12:24.570 --> 00:12:25.630
Crinkler.

00:12:25.630 --> 00:12:30.010
This takes the place of the linker in our compile chain so once again we're swapping linkers.

00:12:30.010 --> 00:12:33.720
This was pretty much a drop-in replacement though so I had no issues this time.

00:12:33.720 --> 00:12:40.800
With Crinkler in the toolchain, our Snake program went from 3.1 KB to just under 1.4!

00:12:40.800 --> 00:12:43.370
All that Snake in less than half the size!

00:12:43.370 --> 00:12:45.650
The QR code dream lives on!

00:12:45.650 --> 00:12:49.150
I have a feeling if we took it back to assembly, someone who knew what they were

00:12:49.150 --> 00:12:53.210
doing would be able to shrink it down with no compromise and no compression.

00:12:53.210 --> 00:12:54.200
Uhh...

00:12:54.200 --> 00:12:56.390
But instead you're watching me.

00:12:56.390 --> 00:12:58.740
But I will say I think the smaller it is the better.

00:12:58.740 --> 00:13:02.620
It means the QR code can be smaller and probably easier for the software to read.

00:13:02.620 --> 00:13:05.360
Speaking of which, how do we create a QR code you may ask?

00:13:05.360 --> 00:13:10.470
Well with some of my favorite things in the world: Unix command line utilities.

00:13:10.470 --> 00:13:15.960
First, I'll use a tool called qrencode to convert our binary data into QR.

00:13:15.960 --> 00:13:20.220
We'll have to tell it our data is binary since it will otherwise assume it is text.

00:13:20.640 --> 00:13:22.860
And it's done - doesn't even take a second.

00:13:23.470 --> 00:13:28.550
There it is. An entire Snake game inside a QR code.

00:13:28.550 --> 00:13:33.180
This is what we've been waiting for this whole time and we're finally here.

00:13:33.180 --> 00:13:37.660
And now for the ultimate test, can we get our executable back?

00:13:37.660 --> 00:13:40.510
For this, I'll be using another tool called ZBar.

00:13:40.510 --> 00:13:44.530
It reads all sorts of barcodes, including QR codes, and comes in two forms:

00:13:44.530 --> 00:13:48.770
zbarimg for images and zbarcam for webcams.

00:13:48.770 --> 00:13:52.270
I'll try zbarimg first with our original QR code picture.

00:13:52.270 --> 00:13:56.390
Once again, we'll have to tell it that our code contains binary data rather than text.

00:13:56.940 --> 00:13:58.780
Okay, looks good...

00:14:01.000 --> 00:14:03.880
And yep, it decoded perfectly!

00:14:03.880 --> 00:14:09.000
This executable that we're playing right now was just decoded from a QR code.

00:14:09.000 --> 00:14:11.990
And now it's time for the ultimate, ultimate test.

00:14:11.990 --> 00:14:16.800
The whole point of a QR code is that it can be easily scanned in from the real world with just a camera.

00:14:16.800 --> 00:14:18.880
So here I go printing it out.

00:14:20.980 --> 00:14:23.340
Now let's fire up zbarcam...

00:14:23.340 --> 00:14:24.770
Eww gross, who's that?

00:14:24.770 --> 00:14:28.180
It took a moment to recognize, but it eventually made an executable.

00:14:28.180 --> 00:14:29.490
And...

00:14:30.860 --> 00:14:31.620
Oh...

00:14:32.180 --> 00:14:32.900
Huh...

00:14:33.200 --> 00:14:34.540
It doesn't work.

00:14:34.540 --> 00:14:36.800
Maybe it just didn't read properly?

00:14:36.800 --> 00:14:42.320
It did seem to have some trouble with the paper not being flat, so I tried to use cardboard to keep it straight.

00:14:43.000 --> 00:14:45.360
No it's... still broken.

00:14:45.360 --> 00:14:49.630
What happened? It read perfectly fine from the image but not from the webcam?

00:14:49.630 --> 00:14:56.500
Something's definitely wrong. When I run it from the command line, it just says segmentation fault, so it must be getting corrupted somewhere.

00:14:56.500 --> 00:14:58.670
We were so damn close!

00:14:58.670 --> 00:15:02.410
I decided to create a new QR code with the error correction turned up.

00:15:02.410 --> 00:15:07.180
Before it was set to low, I'm setting it to Q which is the highest that can still fit.

00:15:07.180 --> 00:15:11.620
Error correction inherently requires more data so there is a limit to how much we can have.

00:15:11.620 --> 00:15:18.370
I didn't expect this to do much since QR code error correction is intended more for if the code gets scratched or damaged somehow.

00:15:18.370 --> 00:15:19.980
But I had to try it.

00:15:19.980 --> 00:15:21.360
Ehh?

00:15:21.360 --> 00:15:23.240
Nope, still not working.

00:15:23.240 --> 00:15:28.040
This is so weird. How could it decode perfectly from the original image but not now?

00:15:28.040 --> 00:15:31.880
I decided to bring out the hex editor and compare the executables side by side.

00:15:31.880 --> 00:15:35.250
From there it was immediately clear what was wrong.

00:15:35.250 --> 00:15:38.190
You see this 0A byte here in the original?

00:15:38.190 --> 00:15:46.090
 In the executable retrieved from the QR code, that byte has magically become two: 0D and 0A.

00:15:46.090 --> 00:15:49.360
Now those of you who know ASCII already know what's happened here.

00:15:49.360 --> 00:15:54.910
But in case you don't, you know how we often tend to break up language into paragraphs or separate lines?

00:15:54.910 --> 00:15:59.850
Well, computers aren't made of lines, they don't really have a way of doing this internally.

00:15:59.850 --> 00:16:07.120
So usually text is stored like this. One long contiguous stream with a special character where the line breaks go so that

00:16:07.120 --> 00:16:12.860
when it draws that text on the screen for us, it knows how to split the text into the lines we made.

00:16:12.860 --> 00:16:17.630
This by the way doesn't include automatic word wrapping since the computer can calculate that on the fly.

00:16:17.630 --> 00:16:21.070
This only applies to line and paragraph breaks specifically.

00:16:21.070 --> 00:16:28.940
This special character is called a "Newline" or "Line Ending" and it's not really a dot like this, it's a control byte you never see.

00:16:28.940 --> 00:16:35.640
But what byte that is specifically is hard to pinpoint because not everyone could agree on that.

00:16:35.640 --> 00:16:39.030
Today there are only really two line endings you'll ever see.

00:16:39.030 --> 00:16:46.030
Unix-based systems, so Mac, Linux, iOS, and Android, all use a single character called a line feed,

00:16:46.030 --> 00:16:48.650
which is, in hex, byte 0A.

00:16:48.650 --> 00:16:51.920
Programmers will know this better as backslash-N.

00:16:51.920 --> 00:16:55.600
Windows, however, has historically used a two-byte sequence.

00:16:55.600 --> 00:16:59.730
They also use a line feed, but it's preceded by a carriage return.

00:16:59.730 --> 00:17:03.490
0D in hex, or backslash-R in code.

00:17:03.490 --> 00:17:08.760
So basically Unix line endings look like this and Windows line endings look like this.

00:17:08.760 --> 00:17:14.110
While most software these days can understand both line endings, a handful still can't.

00:17:14.110 --> 00:17:22.350
Up until 2018, Windows Notepad only acknowledged Windows line endings, which is why you'd sometimes open text documents with it that would look like this.

00:17:22.350 --> 00:17:28.360
As such, it's not uncommon for some programs to automatically convert line endings to whatever system you're using.

00:17:28.360 --> 00:17:30.140
Are you starting to see what's happened?

00:17:30.140 --> 00:17:35.020
Ordinarily you wouldn't notice this since software that handles both will recognize either type,

00:17:35.020 --> 00:17:39.690
and software that doesn't will magically work now that the line endings are "correct".

00:17:39.690 --> 00:17:42.790
But our Snake game isn't text.

00:17:42.790 --> 00:17:50.820
We know this, but something somewhere doesn't and is inserting carriage returns into what's actually precise machine code.

00:17:50.820 --> 00:17:52.980
It's no wonder it just crashes on startup.

00:17:52.980 --> 00:17:59.600
We already know zbarimg can decode our executable fine, so it must be something with zbarcam.

00:17:59.600 --> 00:18:01.290
Some sort of bug maybe?

00:18:01.290 --> 00:18:06.180
Well the joys of open source software allow us to dive into the code and see what's wrong for ourselves,

00:18:06.180 --> 00:18:10.310
and I pretty quickly found that the "stdout" wasn't being set to binary mode.

00:18:10.910 --> 00:18:17.820
Which basically means zbarcam gave no indication that its output wasn't text, so Windows assumed that it was.

00:18:17.820 --> 00:18:19.910
Let's fix that up and try it again.

00:18:19.910 --> 00:18:22.540
And I've gone back to my original QR code for this.

00:18:23.410 --> 00:18:26.360
Alright, we've got our executable. And...

00:18:26.360 --> 00:18:28.740
Yes! There it is!

00:18:28.740 --> 00:18:33.450
A game that streamed in from a piece of paper through the webcam.

00:18:33.450 --> 00:18:37.580
I can even write a little script to open Snake automatically after the read.

00:18:37.580 --> 00:18:41.730
Let me try it out on my laptop which has never seen this Snake executable before.

00:18:43.390 --> 00:18:45.970
Haha it's like a magic trick!

00:18:45.970 --> 00:18:48.750
So if you ask me this is unbelievably cool.

00:18:48.750 --> 00:18:53.180
Sure, it doesn't serve much practical purpose, but just the fact that QR codes which are

00:18:53.180 --> 00:18:56.720
usually relegated to some [censored] McDonalds website or something,

00:18:56.720 --> 00:19:01.600
could sort of function like a small game cartridge.

00:19:01.600 --> 00:19:03.020
I think that's really cool!

00:19:03.020 --> 00:19:05.660
The security implications do worry me though.

00:19:05.660 --> 00:19:09.940
Obviously QR codes could always link to malicious websites, but if you can store

00:19:09.940 --> 00:19:16.030
malware directly into the code, which clearly you can, that has the potential to be even more dangerous.

00:19:16.030 --> 00:19:21.840
I can already imagine a code exploiting an Android camera bug or whatever to trigger some arbitrary code execution

00:19:21.840 --> 00:19:24.380
and steal your passwords and banking information.

00:19:24.380 --> 00:19:26.380
Uhhhh hopefully not?

00:19:26.380 --> 00:19:28.130
Anyway I hope you enjoyed this video.

00:19:28.130 --> 00:19:31.340
If you'd like to play around with this QR code, check out the link in the description.

00:19:31.340 --> 00:19:38.540
I have the code, the reader program with the fix I wrote in, and a step-by-step tutorial if you'd wanna play Snake from a QR code.

00:19:38.540 --> 00:19:41.910
As always thank you so much for watching and I'll see you all next time.

00:19:41.910 --> 00:19:42.940
Ciao!

