I need to read file content as string (binary file) to variable, then iterate over each character in that string, convert each character to its hexadecimal representation, use comma (or any other distinguishable character) as character separator and embed the result in my actual LaTeX file where I'm doing all that stuff.
In python I have something like this (4 lines!):
fileContent = ""
with open("binary.so", "rb") as f:
fileContent = f.read()
print( ",".join( map(lambda x: str(ord(x)), fileContent) ) )
Now the hard part:
- no extra packages = only tex primitives
- defined as tex macro:
\readfile{ input_file_here } - must work with pdflatex
- file might be empty
- newlines and other special characters must be preserved (it is binary!)
- I need a check if it is really a file and not a directory and if I'm allowed to read such files
- should work on Linux/Unix and Windows (actually I need only for Linux, but more general solution would be nice)
- no extra definitions in preamble
- no
luatex, noimmediate, so no shell and no extra tools
Because this is a binary file (I need to treat all files like this) we have character values between 0 (0x00) and 255 (0xFF). Numbers might be HEX or just decimal. base64 encoding would be OK too, but without external tools and extra flags like --shell-escape.
\readfile{binary.so} % will return something like "D,E,A,D,B,E,E,F,1,3,3,7" or "0x0D,0x0E,..." or "13,14,..."
I tried something like this and it didn't work: (example taken from here)
\newread\makerfile
\openin\makerfile=binary.so
\ifeof\makerfile
\else
% it looks promising, but it shouldn't read by newlines
\read\makerfile to\makerline
\closein\makerfile
% iterate over each character
% ...
\fi
E.g. this works, but I cannot iterate over characters, because I don't know how
\makeatletter
\newcommand\saferead[1] {
\bgroup
\let\do\@makeother
\dospecials
\catcode`\ =10 % spaces
\catcode`\^^M=\active % newlines
\input{#1}
\egroup
}
\makeatother
I googled like crazy and found only some useful bits but I'm still far away from what I want.




\immediate? Do you mean the commands to be executed only when TeX ships a page out? – Phelype Oleinik Aug 26 '19 at 22:19$PATH,$$etc. (invoked without--shell-escapeflag for security reasons). I'm able to read arbitrary ascii files, and also write arbitrary tex in temp. There are multiple injection points in the middle of the document. – Awaaaaarghhh Aug 26 '19 at 22:24\begin{document}. So only between\begin{document}and\end{document}. – Awaaaaarghhh Aug 26 '19 at 22:27phar://binary-generated-with-latexand abuseuse-after-free/buffer overflowvulnerabilities in php. latex is used of course server-side to generate PDFs. – Awaaaaarghhh Aug 26 '19 at 22:29\loop...\repeat). I made a faster (comparing to my previous answer) version which sees all characters. But it's as far from "primitives only" as it can get :-) – Phelype Oleinik Aug 29 '19 at 13:47\begin{document} ... \end{document}. My job is to show that server side LaTeX injection is a real threat. I was able to read some simple ASCII files however I was not able to read arbitrary binary files because I would have to use some extra packages (in preamble). That's why the restriction not to use any packages and only tex primitives. – Awaaaaarghhh Aug 30 '19 at 17:45