\documentclass[a4paper, 11pt]{article}
\renewcommand{\familydefault}{\sfdefault}
\usepackage{a4wide}
\usepackage[utf8]{inputenc}
\usepackage[ngerman]{babel}
\usepackage{color}
\usepackage{graphicx}
\usepackage{setspace}
\usepackage{ulem}
\usepackage{listings}
\usepackage{float}
\usepackage[rflt]{floatflt}
\usepackage{tabularx}
\setlength{\parindent}{0pt}
\title{GZIP / deflate Streams mit Free Pascal}
\usepackage[
pdftitle={GZIP / deflate Streams mit Free Pascal}
pdfsubject={GZIP / deflate Stream Funktionen programmiert in Free Pascal zum Einatz im ISAPI-CMS}
pdfauthor={Udo Schmal}
pdfkeywords={GZIP, deflate, inflate, Stream, Komprimierung, Free Pascal, Lazarus, Delphi, CMS, ISAPI, ISAPI-CMS}
]{hyperref}
\definecolor{darkblue}{rgb}{0,0,.5}
\hypersetup{pdftex=true, colorlinks=true, breaklinks=true, linkcolor=darkblue, menucolor=darkblue, pagecolor=darkblue, urlcolor=darkblue}
\begin{document}GZIP / deflate Streams mit Free Pascal
\singlespacing
{\LARGE \textbf{\label{sec:title}GZIP bzw. deflate Streams mit Free Pascal}}

\singlespacing
{\large \textbf{\label{sec:subtitle}Komprimierte Datenübertragung}}

\begin{floatingfigure}[r]{0.33\textwidth}
\centering
\includegraphics[width=0.25\textwidth]{/home/udo/webserver/www.gocher.me/www-root/media/gzip.png}

Unterschiede Deflate / GZip
\end{floatingfigure}

GZip und deflate(ZLib) unterscheidet sich lediglich durch den Header und die Checksumme, denn beide benutzt im Eigentlichen die selbe Methode zum Komprimieren (deflate = Luft heraus lassen :) )

Unter Free Pascal steht das Package paszlib zur Verfügung, welches die benötigten Funktionen zum Behandeln von deflated Blöcken zur Verfügung stellt. Mit Hilfe dieser Funktionen lassen sich alle benötigten Funktionen\&nbsp; deflate (raw), GZip, ZLib, inflate (raw) und\&nbsp;ZUncompressStream erzeugen. ZUncompessStream übernimmt die Identifizierung des Typs und nutzt die Funktion inflate zum entpacken des deflated Teils.

Die Routinen sollten dazu in der Lage sein sowohl deflated (ZLib) und gziped Streams zum Versand zu erzeugen, als auch Empfangene Streams dieser Formate wieder zu entpacken!

\singlespacing
{\large \textbf{Aufbau}}

Deflate (ZLib): \href{https://www.rfc-editor.org/rfc/rfc1950}{ZLIB Compressed Data Format Specification}

\begin{itemize}
\item 2 bytes Defines the compression mode          
\begin{itemize}            
\item 1 byte \$78 CMF Compression Method and flags\newline
 "deflate" compression method with a window size up to 32K 
\item 1 byte FLG              
\begin{itemize}                
\item bit 0-4 FCHECK (check bits for CMF and FLG)
\item bit 5 FDICT (preset dictionary)
\item bit 6-7 FLEVEL (compression level)
\end{itemize}
  Examples: (\href{https://en.wikipedia.org/wiki/List_of_file_signatures}{List of file signatures})              
\begin{itemize}
\item \$01 No Compression (no preset dictionary)
\item \$5E Best speed (no preset dictionary)
\item \$9C Default Compression (no preset dictionary)
\item \$DA Best Compression (no preset dictionary)
\item \$20 No Compression (with preset dictionary)
\item \$7D Best speed (with preset dictionary)
\item \$BB Default Compression (with preset dictionary)
\item \$F9 Best Compression (with preset dictionary)
\end{itemize}

\end{itemize}

\item 4 bytes DICTID Present only when FLG.FDICT is set.
\item deflated stream
\item 4 bytes adler32 checksum
\end{itemize}

GZip: \href{https://www.ietf.org/rfc/rfc1952.txt}{GZIP file format specification}

\begin{itemize}
\item 2 bytes \$1f \$8b\&nbsp; (IDentification)
\item 1 byte \$08 Compression Method = deflate
\item 1 byte \$00 FLaGs          
\begin{itemize}            
\item bit 0\&nbsp;\&nbsp; FTEXT - indicates file is ASCII text (can be safely ignored)
\item bit 1\&nbsp;\&nbsp; FHCRC - there is a CRC16 for the header immediately following the header
\item bit 2\&nbsp;\&nbsp; FEXTRA - extra fields are present
\item bit 3\&nbsp;\&nbsp; FNAME - the zero-terminated filename is present. encoding; ISO-8859-1.
\item bit 4\&nbsp;\&nbsp; FCOMMENT - a zero-terminated file comment is present. encoding: ISO-8859-1
\item bit 5-7 \&nbsp; reserved
\end{itemize}

\item 4 bytes \$00000000 Modification TIME = no time stamp is available
\item 1 byte \$00 eXtra FLags          
\begin{itemize}            
\item 00 - default compression
\item 02 - compressor used maximum compression, slowest algorithm
\item 04 - compressor used fastest algorithm
\end{itemize}

\item 1 byte \$0b Operating System = NTFS filesystem (NT)\&nbsp; for textfiles (line ending)          
\begin{itemize}            
\item 00 - FAT filesystem (MS-DOS, OS/2, NT/Win32)
\item 01 - Amiga 02 - VMS (or OpenVMS)
\item 03 - Unix
\item 04 - VM/CMS
\item 05 - Atari TOS
\item 06 - HPFS filesystem (OS/2, NT)
\item 07 - Macintosh
\item 08 - Z-System
\item 09 - CP/M
\item 0A - TOPS-20
\item 0B - NTFS filesystem (NT)
\item 0C - QDOS
\item 0D - Acorn RISCOS
\item FF - unknown
\end{itemize}

\item deflated stream
\item crc32 checksum
\item input size
\end{itemize}

\href{http://www.gocher.me/code/gziputils.pas}{GZIPUtils.pas}

\textbf{Beispiel:}

\href{http://www.gocher.me/code/TestGzip.pas}{TestGzip.pas}
\end{document}