# TeX document allowing any byte sequence being inserted

Out of a hacker's interest, I looked for a TeX document into which an arbitrary byte sequence can be inserted without causing the TeX process to fail, but had a similar issue; I basically set all catcodes to 9 except on the letters and the equals sign; this "worked" as in TeX never failed with an error, but it also never terminated due to missing \end.

Putting an \end first, and only then breaking all the catcodes works.

So now I am asking for a solution to this question that where there still can be "printed" text behind the random insertable junk (this kinda requires the junk to be printed too, of course).

-
add comment

## 1 Answer

You need something to terminate the run. If you make every non letter catcode 9 (ignored) you can't use a control sequence but you can use end-of-file, as long as you use etex (pdftex xtetex) etc, this doesn't work with classic tex.

My test file had nulls and bells control-@ and control-g etc in the stream but the system doesn't seem to allow me to cut and paste them on to the website, so you'll have to add them back:

\def\a{%
\count0=0
\loop
\ifnum\catcode\count0=11
\else
\catcode\count0=9
\fi
\advance\count0 1
\ifnum\count0<256
\repeat
\everyeof{\end}}
\a

stuff here....

-
This is great, and somewhat equivalent to my solution of putting \end first, then disabling all catcodes (BTW, nice trick with the macro definition \a, removes the need for ordering the catcode assignments right)... but this indeed requires \everyeof to work right. I wonder if it can be proven to be entirely impossible without \everyeof... but not sure. –  Rudolf Polzer May 3 '13 at 14:31
@RudolfPolzer No: if you put \end first (in plain TeX) then it doesn't matter what comes after that as TeX will stop at that point. This way TeX reads to end of file. –  David Carlisle May 3 '13 at 14:55
Now I have a suspicion that it may NOT be possible without using something like eTeX's \everyeof (which is actually quite neat): assume you want to insert a breakage string at position $n$, then let TeX parse the document until character $n$. Then, inspect TeX's parser state, and identify the byte sequence leaving any level of nesting, until no more level is left. Then insert }. All this works only assuming one can at least still write \catcode commands, as it may be needed to close all levels of nesting. But if not, the outer document also has no more way to get back to a "sane" state... –  Rudolf Polzer May 3 '13 at 16:39
As for my above comment which I deleted already, I was wrong. I tested the document \end} and even this works properly. –  Rudolf Polzer May 3 '13 at 16:40
add comment