% \iffalse meta-comment
%
% Copyright (C) 2007 by Heiko Oberdiek <oberdiek@uni-freiburg.de>
%
% This work may be distributed and/or modified under the
% conditions of the LaTeX Project Public License, either
% version 1.3 of this license or (at your option) any later
% version. The latest version of this license is in
%    http://www.latex-project.org/lppl.txt
% and version 1.3 or later is part of all distributions of
% LaTeX version 2005/12/01 or later.
%
% This work has the LPPL maintenance status "maintained".
%
% This Current Maintainer of this work is Heiko Oberdiek.
%
% This work consists of the main source file pdfescape.dtx
% and the derived files
%    pdfescape.sty, pdfescape.pdf, pdfescape.ins, pdfescape.drv,
%    pdfescape-test1.tex, pdfescape-test2.tex,
%    pdfescape-test3.tex.
%
% Distribution:
%    CTAN:macros/latex/contrib/oberdiek/pdfescape.dtx
%    CTAN:macros/latex/contrib/oberdiek/pdfescape.pdf
%
% Unpacking:
%    (a) If pdfescape.ins is present:
%           tex pdfescape.ins
%    (b) Without pdfescape.ins:
%           tex pdfescape.dtx
%    (c) If you insist on using LaTeX
%           latex \let\install=y\input{pdfescape.dtx}
%        (quote the arguments according to the demands of your shell)
%
% Documentation:
%    (a) If pdfescape.drv is present:
%           latex pdfescape.drv
%    (b) Without pdfescape.drv:
%           latex pdfescape.dtx; ...
%    The class ltxdoc loads the configuration file ltxdoc.cfg
%    if available. Here you can specify further options, e.g.
%    use A4 as paper format:
%       \PassOptionsToClass{a4paper}{article}
%
%    Programm calls to get the documentation (example):
%       pdflatex pdfescape.dtx
%       makeindex -s gind.ist pdfescape.idx
%       pdflatex pdfescape.dtx
%       makeindex -s gind.ist pdfescape.idx
%       pdflatex pdfescape.dtx
%
% Installation:
%    TDS:tex/generic/oberdiek/pdfescape.sty
%    TDS:doc/latex/oberdiek/pdfescape.pdf
%    TDS:doc/latex/oberdiek/pdfescape-test1.tex
%    TDS:doc/latex/oberdiek/pdfescape-test2.tex
%    TDS:doc/latex/oberdiek/pdfescape-test3.tex
%    TDS:source/latex/oberdiek/pdfescape.dtx
%
%<*ignore>
\begingroup
  \def\x{LaTeX2e}%
\expandafter\endgroup
\ifcase 0\ifx\install y1\fi\expandafter
         \ifx\csname processbatchFile\endcsname\relax\else1\fi
         \ifx\fmtname\x\else 1\fi\relax
\else\csname fi\endcsname
%</ignore>
%<*install>
\input docstrip.tex
\Msg{************************************************************************}
\Msg{* Installation}
\Msg{* Package: pdfescape 2007/04/21 v1.4 Provides hex, PDF name and string conversions (HO)}
\Msg{************************************************************************}

\keepsilent
\askforoverwritefalse

\preamble

This is a generated file.

Copyright (C) 2007 by Heiko Oberdiek <oberdiek@uni-freiburg.de>

This work may be distributed and/or modified under the
conditions of the LaTeX Project Public License, either
version 1.3 of this license or (at your option) any later
version. The latest version of this license is in
   http://www.latex-project.org/lppl.txt
and version 1.3 or later is part of all distributions of
LaTeX version 2005/12/01 or later.

This work has the LPPL maintenance status "maintained".

This Current Maintainer of this work is Heiko Oberdiek.

This work consists of the main source file pdfescape.dtx
and the derived files
   pdfescape.sty, pdfescape.pdf, pdfescape.ins, pdfescape.drv,
   pdfescape-test1.tex, pdfescape-test2.tex,
   pdfescape-test3.tex.

\endpreamble

\generate{%
  \file{pdfescape.ins}{\from{pdfescape.dtx}{install}}%
  \file{pdfescape.drv}{\from{pdfescape.dtx}{driver}}%
  \usedir{tex/generic/oberdiek}%
  \file{pdfescape.sty}{\from{pdfescape.dtx}{package}}%
  \usedir{doc/latex/oberdiek}%
  \file{pdfescape-test1.tex}{\from{pdfescape.dtx}{test1}}%
  \file{pdfescape-test2.tex}{\from{pdfescape.dtx}{test2}}%
  \file{pdfescape-test3.tex}{\from{pdfescape.dtx}{test3}}%
}

\obeyspaces
\Msg{************************************************************************}
\Msg{*}
\Msg{* To finish the installation you have to move the following}
\Msg{* file into a directory searched by TeX:}
\Msg{*}
\Msg{*     pdfescape.sty}
\Msg{*}
\Msg{* To produce the documentation run the file `pdfescape.drv'}
\Msg{* through LaTeX.}
\Msg{*}
\Msg{* Happy TeXing!}
\Msg{*}
\Msg{************************************************************************}

\endbatchfile
%</install>
%<*ignore>
\fi
%</ignore>
%<*driver>
\NeedsTeXFormat{LaTeX2e}
\ProvidesFile{pdfescape.drv}%
  [2007/04/21 v1.4 Provides hex, PDF name and string conversions (HO)]%
\documentclass{ltxdoc}
\usepackage{holtxdoc}
\begin{document}
  \DocInput{pdfescape.dtx}%
\end{document}
%</driver>
% \fi
%
% \CheckSum{1815}
%
% \CharacterTable
%  {Upper-case    \A\B\C\D\E\F\G\H\I\J\K\L\M\N\O\P\Q\R\S\T\U\V\W\X\Y\Z
%   Lower-case    \a\b\c\d\e\f\g\h\i\j\k\l\m\n\o\p\q\r\s\t\u\v\w\x\y\z
%   Digits        \0\1\2\3\4\5\6\7\8\9
%   Exclamation   \!     Double quote  \"     Hash (number) \#
%   Dollar        \$     Percent       \%     Ampersand     \&
%   Acute accent  \'     Left paren    \(     Right paren   \)
%   Asterisk      \*     Plus          \+     Comma         \,
%   Minus         \-     Point         \.     Solidus       \/
%   Colon         \:     Semicolon     \;     Less than     \<
%   Equals        \=     Greater than  \>     Question mark \?
%   Commercial at \@     Left bracket  \[     Backslash     \\
%   Right bracket \]     Circumflex    \^     Underscore    \_
%   Grave accent  \`     Left brace    \{     Vertical bar  \|
%   Right brace   \}     Tilde         \~}
%
% \GetFileInfo{pdfescape.drv}
%
% \title{The \xpackage{pdfescape} package}
% \date{2007/04/21 v1.4}
% \author{Heiko Oberdiek\\\xemail{oberdiek@uni-freiburg.de}}
%
% \maketitle
%
% \begin{abstract}
% This package implements \pdfTeX's escape features
% (\cs{pdfescapehex}, \cs{pdfunescapehex}, \cs{pdfescapename},
% \cs{pdfescapestring}) using \TeX\ or \eTeX.
% \end{abstract}
%
% \tableofcontents
%
% \section{Documentation}
%
% \begin{declcs}{EdefEscapeHex} \M{cmd} \M{string}\\
%  \cs{EdefUnescapeHex} \M{cmd} \M{string}\\
%  \cs{EdefEscapeName} \M{cmd} \M{string}\\
%  \cs{EdefEscapeString} \M{cmd} \M{string}
% \end{declcs}
% These commands converts \meta{string} and stores the result
% in macro \meta{cmd}. The conversion result is the same
% as the conversion of the corresponding \pdfTeX's primitives.
% Note that the argument \meta{string} is expanded before
% the conversion.
%
% For example, if \pdfTeX\ \textgreater= 1.30 is present,
% then \cs{EdefEscapeHex}
% becomes to:
% \begin{quote}
%\begin{verbatim}
%\def\EdefEscapeHex#1#2{%
%  \edef#1{\pdfescapehex{#2}}%
%}
%\end{verbatim}
% \end{quote}
% The package provides implementations for the case that \pdfTeX\ is
% not present (or too old). Even \eTeX\ can be missing, however it is
% used if it is detected.
%
% \paragraph{Babel.}
% The input strings may contain shorthand characters of package
% \xpackage{babel}.
%
% \subsection{Additional unescape macros}
%
% \begin{declcs}{EdefUnescapeName} \M{cmd} \M{string}\\
% \end{declcs}
% Sequences of a hash sign with two hexadecimal digits are
% converted to the corresponding character (PDF-1.2).
% A hash sign that is not followed by two hexadecimal digits
% is left unchanged. The catcodes in the result string follow
% TeX's conventions. The space has catcode 10 (space) and
% the other characters have catcode 12 (other).
%
% \begin{declcs}{EdefUnescapeString} \M{cmd} \M{string}\\
% \end{declcs}
% Macro \meta{cmd} stores the unescaped string in \meta{string}.
% All the rules for literal strings are implemented, see
% PDF specification.
% The catcodes in the result string follow TeX's conventions.
%
% \StopEventually{
% }
%
% \section{Implementation}
%
%    \begin{macrocode}
%<*package>
%    \end{macrocode}
%
% \subsection{Catcodes}
%
%    \begin{macrocode}
\expandafter\edef\csname PE@AtEnd\endcsname{%
  \catcode64 \the\catcode64\relax
}
\catcode64 11 % @
\def\PE@EnsureCode#1#2#3{%
  \edef\PE@AtEnd{%
    \PE@AtEnd
    #1#2 \the#1#2\relax
  }%
  #1#2 #3\relax
}
\PE@EnsureCode\catcode{0}{12}% ^^@
\PE@EnsureCode\catcode{34}{12}% "
\PE@EnsureCode\catcode{42}{12}% *
\PE@EnsureCode\catcode{45}{12}% -
\PE@EnsureCode\catcode{46}{12}% .
\PE@EnsureCode\catcode{60}{12}% <
\PE@EnsureCode\catcode{61}{12}% =
\PE@EnsureCode\catcode{62}{12}% >
\PE@EnsureCode\catcode{94}{7}% ^
\PE@EnsureCode\catcode{96}{12}% `
\PE@EnsureCode\uccode{34}{0}% "
\PE@EnsureCode\uccode{48}{0}% 0
\PE@EnsureCode\uccode{61}{0}% =
%    \end{macrocode}
%
% \subsection{Reload check and package identification}
%    Reload check, especially if the package is not used with \LaTeX.
%    \begin{macrocode}
\begingroup
  \catcode44 12 % ,
  \catcode45 12 % -
  \catcode46 12 % .
  \catcode58 12 % :
  \catcode64 11 % @
  \expandafter\let\expandafter\x\csname ver@pdfescape.sty\endcsname
  \ifcase 0%
    \ifx\x\relax % plain
    \else
      \ifx\x\empty % LaTeX
      \else
        1%
      \fi
    \fi
  \else
    \expandafter\ifx\csname PackageInfo\endcsname\relax
      \def\x#1#2{%
        \immediate\write-1{Package #1 Info: #2.}%
      }%
    \else
      \def\x#1#2{\PackageInfo{#1}{#2, stopped}}%
    \fi
    \x{pdfescape}{The package is already loaded}%
    \endgroup
    \expandafter\endinput
  \fi
\endgroup
%    \end{macrocode}
%    Package identification:
%    \begin{macrocode}
\begingroup
  \catcode40 12 % (
  \catcode41 12 % )
  \catcode44 12 % ,
  \catcode45 12 % -
  \catcode46 12 % .
  \catcode47 12 % /
  \catcode58 12 % :
  \catcode64 11 % @
  \expandafter\ifx\csname ProvidesPackage\endcsname\relax
    \def\x#1#2#3[#4]{\endgroup
      \immediate\write-1{Package: #3 #4}%
      \xdef#1{#4}%
    }%
  \else
    \def\x#1#2[#3]{\endgroup
      #2[{#3}]%
      \ifx#1\relax
        \xdef#1{#3}%
      \fi
    }%
  \fi
\expandafter\x\csname ver@pdfescape.sty\endcsname
\ProvidesPackage{pdfescape}%
  [2007/04/21 v1.4 Provides hex, PDF name and string conversions (HO)]
%    \end{macrocode}
%
% \subsection{Sanitizing}
%
%    \begin{macro}{\PE@sanitize}
%    Macro \cs{PE@sanitize} takes |#2|, entirely converts
%    it to token with catcode 12 (other) and stores the
%    result in macro |#1|.
%    \begin{macrocode}
\begingroup\expandafter\expandafter\expandafter\endgroup
\expandafter\ifx\csname detokenize\endcsname\relax
  \long\def\PE@sanitize#1#2{%
    \begingroup
      \csname @safe@activestrue\endcsname
      \edef#1{#2}%
      \PE@onelevel@sanitize#1%
    \expandafter\endgroup
    \expandafter\def\expandafter#1\expandafter{#1}%
  }%
  \begingroup\expandafter\expandafter\expandafter\endgroup
  \expandafter\ifx\csname @onelevel@sanitize\endcsname\relax
    \def\PE@onelevel@sanitize#1{%
      \edef#1{\expandafter\PE@strip@prefix\meaning#1}%
    }%
    \def\PE@strip@prefix#1>{}%
  \else
    \let\PE@onelevel@sanitize\@onelevel@sanitize
  \fi
\else
  \long\def\PE@sanitize#1#2{%
    \begingroup
      \csname @safe@activestrue\endcsname
      \edef#1{#2}%
      \PE@onelevel@sanitize#1%
    \expandafter\endgroup
    \expandafter\def\expandafter#1\expandafter{#1}%
  }%
  \def\PE@onelevel@sanitize#1{%
    \edef#1{\detokenize\expandafter{#1}}%
  }%
\fi
%    \end{macrocode}
%    \end{macro}
%
% \subsubsection{Space characters}
%
%    \begin{macro}{\PE@space@other}
%    \begin{macrocode}
\begingroup
  \catcode`\ =12\relax%
\def\x{\endgroup\def\PE@space@other{ }}\x\relax
%    \end{macrocode}
%    \end{macro}
%    \begin{macro}{\PE@space@space}
%    \begin{macrocode}
\def\PE@space@space{ }
%    \end{macrocode}
%    \end{macro}
%
% \subsubsection{Space normalization}
%
%    \begin{macro}{\PE@SanitizeSpaceOther}
%    \begin{macrocode}
\def\PE@SanitizeSpaceOther#1{%
  \edef#1{\expandafter\PE@SpaceToOther#1 \relax}%
}
%    \end{macrocode}
%    \end{macro}
%
%    \begin{macro}{\PE@SpaceToOther}
%    \begin{macrocode}
\def\PE@SpaceToOther#1 #2\relax{%
  #1%
  \ifx\\#2\\%
  \else
    \PE@space@other
    \@ReturnAfterFi{%
      \PE@SpaceToOther#2\relax
    }%
  \fi
}
%    \end{macrocode}
%    \end{macro}
%
%    \begin{macro}{\@ReturnAfterFi}
%    \begin{macrocode}
\long\def\@ReturnAfterFi#1\fi{\fi#1}
%    \end{macrocode}
%    \end{macro}
%
% \subsection{\cs{EdefUnescapeName}}
%
%    \begin{macro}{\EdefUnescapeName}
%    \begin{macrocode}
\def\EdefUnescapeName#1#2{%
  \PE@sanitize#1{#2}%
  \PE@SanitizeSpaceOther#1%
  \PE@UnescapeName#1%
  \PE@onelevel@sanitize#1%
}
%    \end{macrocode}
%    \end{macro}
%
%    \begin{macro}{\PE@UnescapeName}
%    \begin{macrocode}
\begingroup
  \catcode`\$=6  % hash
  \catcode`\#=12 % other
  \gdef\PE@UnescapeName$1{%
    \begingroup
      \PE@InitUccodeHexDigit
      \def\PE@result{}%
      \expandafter\PE@DeName$1#\relax\relax
    \expandafter\endgroup
    \expandafter\def\expandafter$1\expandafter{\PE@result}%
  }%
  \gdef\PE@DeName$1#$2$3{%
    \ifx\relax$2%
      \edef\PE@result{\PE@result$1}%
      \let\PE@next\relax
    \else
      \ifx\relax$3%
        % wrong escape sequence in input
        \edef\PE@result{\PE@result$1#}%
        \let\PE@next\relax
      \else
        \uppercase{%
          \def\PE@testA{$2}%
          \def\PE@testB{$3}%
        }%
        \ifcase\ifcase\expandafter\PE@TestUcHexDigit\PE@testA
                 \ifcase\expandafter\PE@TestUcHexDigit\PE@testB
                   \z@
                 \else
                   \@ne
                 \fi
               \else
                 \@ne
               \fi
          \uccode\z@="\PE@testA\PE@testB\relax
          \uppercase{%
            \def\PE@temp{^^@}%
          }%
          \uccode\z@=\z@
          \edef\PE@result{\PE@result$1\PE@temp}%
          \let\PE@next\PE@DeName
        \else
          % wrong escape sequence in input
          \edef\PE@result{\PE@result$1#}%
          \def\PE@next{\PE@DeName$2$3}%
        \fi
      \fi
    \fi
    \PE@next
  }%
\endgroup
%    \end{macrocode}
%    \end{macro}
%
%    \begin{macro}{\PE@InitUccodeHexDigit}
%    \begin{macrocode}
\def\PE@InitUccodeHexDigit{%
  \uccode`a=`A\relax
  \uccode`b=`B\relax
  \uccode`c=`C\relax
  \uccode`d=`D\relax
  \uccode`e=`E\relax
  \uccode`f=`F\relax
  \uccode`A=\z@
  \uccode`B=\z@
  \uccode`C=\z@
  \uccode`D=\z@
  \uccode`E=\z@
  \uccode`F=\z@
  \uccode`0=\z@
  \uccode`1=\z@
  \uccode`2=\z@
  \uccode`3=\z@
  \uccode`4=\z@
  \uccode`5=\z@
  \uccode`6=\z@
  \uccode`7=\z@
  \uccode`8=\z@
  \uccode`9=\z@
}
%    \end{macrocode}
%    \end{macro}
%
%    \begin{macro}{\PE@TestUcHexDigit}
%    \begin{macrocode}
\def\PE@TestUcHexDigit#1{%
  \ifnum`#1<48 % 0
    \@ne
  \else
    \ifnum`#1>70 % F
      \@ne
    \else
      \ifnum`#1>57 % 9
        \ifnum`#1<65 % A
          \@ne
        \else
          \z@
        \fi
      \else
        \z@
      \fi
    \fi
  \fi
}
%    \end{macrocode}
%    \end{macro}
%
% \subsection{\cs{EdefUnescapeString}}
%
%    \begin{macro}{\EdefUnescapeString}
%    \begin{macrocode}
\def\EdefUnescapeString#1#2{%
  \PE@sanitize#1{#2}%
  \PE@SanitizeSpaceOther#1%
  \PE@NormalizeLineEnd#1%
  \PE@UnescapeString#1%
  \PE@onelevel@sanitize#1%
}
%    \end{macrocode}
%    \end{macro}
%
%    \begin{macrocode}
\begingroup
  \uccode`\8=10 % lf
  \uccode`\9=13 % cr
\def\x#1#2{\endgroup
%    \end{macrocode}
%    \begin{macro}{\PE@NormalizeLineEnd}
%    \begin{macrocode}
  \def\PE@NormalizeLineEnd##1{%
    \def\PE@result{}%
    \expandafter\PE@@NormalizeLineEnd##1#2\relax
    \let##1\PE@result
  }%
%    \end{macrocode}
%    \end{macro}
%    \begin{macro}{\PE@@NormalizeLineEnd}
%    \begin{macrocode}
  \def\PE@@NormalizeLineEnd##1#2##2{%
    \ifx\relax##2%
      \edef\PE@result{\PE@result##1}%
      \let\PE@next\relax
    \else
      \edef\PE@result{\PE@result##1#1}%
      \ifx#1##2% lf
        \let\PE@next\PE@@NormalizeLineEnd
      \else
        \def\PE@next{\PE@@NormalizeLineEnd##2}%
      \fi
    \fi
    \PE@next
  }%
}%
\uppercase{%
  \x 89%
}
%    \end{macrocode}
%    \end{macro}
%
%    \begin{macrocode}
\begingroup
  \catcode`\|=0 %
  \catcode`\\=12 %
%    \end{macrocode}
%    \begin{macro}{\PE@UnescapeString}
%    \begin{macrocode}
  |gdef|PE@UnescapeString#1{%
    |begingroup
      |def|PE@result{}%
      |expandafter|PE@DeString#1\|relax
    |expandafter|endgroup
    |expandafter|def|expandafter#1|expandafter{|PE@result}%
  }%
%    \end{macrocode}
%    \end{macro}
%    \begin{macro}{\PE@DeString}
%    \begin{macrocode}
  |gdef|PE@DeString#1\#2{%
    |ifx|relax#2%
      |edef|PE@result{|PE@result#1}%
      |let|PE@next|relax
    |else
      |if n#2%
        |uccode|z@=10 %
      |else|if r#2%
        |uccode|z@=13 %
      |else|if t#2%
        |uccode|z@=9 %
      |else|if b#2%
        |uccode|z@=8 %
      |else|if f#2%
        |uccode|z@=12 %
      |else
        |uccode|z@=|z@
      |fi|fi|fi|fi|fi
      |ifnum|uccode|z@>|z@
        |uppercase{%
          |edef|PE@temp{^^@}%
        }%
        |edef|PE@result{|PE@result#1|PE@temp}%
        |let|PE@next|PE@DeString
      |else
        |if\#2% backslash
          |edef|PE@result{|PE@result#1}%
          |let|PE@next|PE@CheckEndBackslash
        |else
          |ifnum`#2=10 % linefeed
            |edef|PE@result{|PE@result#1}%
            |let|PE@next|PE@DeString
          |else
            |ifcase|PE@TestOctDigit#2%
              |edef|PE@result{|PE@result#1}%
              |def|PE@next{|PE@OctI#2}%
            |else
              |edef|PE@result{|PE@result#1#2}%
              |let|PE@next|PE@DeString
            |fi
          |fi
        |fi
      |fi
    |fi
    |PE@next
  }%
%    \end{macrocode}
%    \end{macro}
%    \begin{macro}{\PE@CheckEndBackslash}
%    \begin{macrocode}
  |gdef|PE@CheckEndBackslash#1{%
    |ifx|relax#1%
    |else
      |edef|PE@result{|PE@result\}%
      |expandafter|PE@DeString|expandafter#1%
    |fi
  }%
%    \end{macrocode}
%    \end{macro}
%    \begin{macrocode}
|endgroup
%    \end{macrocode}
%    \begin{macro}{\PE@TestOctDigit}
%    \begin{macrocode}
\def\PE@TestOctDigit#1{%
  \ifnum`#1<48 % 0
    \@ne
  \else
    \ifnum`#1>55 % 7
      \@ne
    \else
      \z@
    \fi
  \fi
}
%    \end{macrocode}
%    \end{macro}
%    \begin{macro}{\PE@OctI}
%    \begin{macrocode}
\def\PE@OctI#1#2{%
  \ifcase\PE@TestOctDigit#2%
    \def\PE@next{\PE@OctII{#1#2}}%
  \else
    \def\PE@next{\PE@OctAll#1#2}%
  \fi
  \PE@next
}
%    \end{macrocode}
%    \end{macro}
%    \begin{macro}{\PE@OctII}
%    \begin{macrocode}
\def\PE@OctII#1#2{%
  \ifcase\PE@TestOctDigit#2%
    \def\PE@next{\PE@OctAll{#1#2}}%
  \else
    \def\PE@next{\PE@OctAll{#1}#2}%
  \fi
  \PE@next
}
%    \end{macrocode}
%    \end{macro}
%    \begin{macro}{\PE@OctAll}
%    \begin{macrocode}
\def\PE@OctAll#1{%
  \uccode\z@='#1\relax
  \uppercase{%
    \edef\PE@result{\PE@result^^@}%
  }%
  \PE@DeString
}
%    \end{macrocode}
%    \end{macro}
%
% \subsection{User macros (\pdfTeX\space analogues)}
%
%    \begin{macrocode}
\begingroup\expandafter\expandafter\expandafter\endgroup
\expandafter\ifx\csname pdfescapehex\endcsname\relax
%    \end{macrocode}
%
%    \begin{macro}{\EdefEscapeHex}
%    \begin{macrocode}
  \long\def\EdefEscapeHex#1#2{%
    \PE@sanitize#1{#2}%
    \PE@SanitizeSpaceOther#1%
    \PE@EscapeHex#1%
  }%
%    \end{macrocode}
%    \end{macro}
%    \begin{macro}{\EdefUnescapeHex}
%    \begin{macrocode}
  \def\EdefUnescapeHex#1#2{%
    \PE@sanitize#1{#2}%
    \PE@UnescapeHex#1%
  }%
%    \end{macrocode}
%    \end{macro}
%    \begin{macro}{\EdefEscapeName}
%    \begin{macrocode}
  \long\def\EdefEscapeName#1#2{%
    \PE@sanitize#1{#2}%
    \PE@SanitizeSpaceOther#1%
    \PE@EscapeName#1%
  }%
%    \end{macrocode}
%    \end{macro}
%    \begin{macro}{\EdefEscapeString}
%    \begin{macrocode}
  \long\def\EdefEscapeString#1#2{%
    \PE@sanitize#1{#2}%
    \PE@SanitizeSpaceOther#1%
    \PE@EscapeString#1%
  }%
%    \end{macrocode}
%    \end{macro}
%
%    \begin{macrocode}
\else
%    \end{macrocode}
%
%    \begin{macro}{\PE@edefbabel}
%    Help macro that adds support for \xpackage{babel}'s shorthand
%    characters.
%    \begin{macrocode}
  \long\def\PE@edefbabel#1#2#3{%
    \begingroup
      \csname @save@activestrue\endcsname
      \edef#1{#2{#3}}%
    \expandafter\endgroup
    \expandafter\def\expandafter#1\expandafter{#1}%
  }%
%    \end{macrocode}
%    \end{macro}
%
%    \begin{macro}{\EdefEscapeHex}
%    \begin{macrocode}
  \long\def\EdefEscapeHex#1#2{%
    \PE@edefbabel#1\pdfescapehex{#2}%
  }%
%    \end{macrocode}
%    \end{macro}
%    \begin{macro}{\EdefUnescapeHex}
%    \begin{macrocode}
  \def\EdefUnescapeHex#1#2{%
    \PE@edefbabel#1\pdfunescapehex{#2}%
  }%
%    \end{macrocode}
%    \end{macro}
%    \begin{macro}{\EdefEscapeName}
%    \begin{macrocode}
  \long\def\EdefEscapeName#1#2{%
    \PE@edefbabel#1\pdfescapename{#2}%
  }%
%    \end{macrocode}
%    \end{macro}
%    \begin{macro}{\EdefEscapeString}
%    \begin{macrocode}
  \long\def\EdefEscapeString#1#2{%
    \PE@edefbabel#1\pdfescapestring{#2}%
  }%
%    \end{macrocode}
%    \end{macro}
%
%    \begin{macrocode}
  \PE@AtEnd
  \expandafter\endinput
\fi
%    \end{macrocode}
%
% \subsection{Help macros}
%
% \subsubsection{Characters}
%
%    Special characters with catcode 12 (other) are created and
%    stored in macros.
%    \begin{macro}{\PE@hash}
%    \begin{macrocode}
\edef\PE@hash{\string#}
%    \end{macrocode}
%    \end{macro}
%    \begin{macro}{\PE@backslash}
%    \begin{macrocode}
\begingroup
  \escapechar=-1 %
\edef\x{\endgroup
  \def\noexpand\PE@backslash{\string\\}%
}
\x
%    \end{macrocode}
%    \end{macro}
%
% \subsubsection{Switch for \eTeX}
%
%    \begin{macrocode}
\newif\ifPE@etex
\begingroup\expandafter\expandafter\expandafter\endgroup
\expandafter\ifx\csname numexpr\endcsname\relax
\else
  \PE@etextrue
\fi
%    \end{macrocode}
%
% \subsection{Conversions}
%
% \subsubsection{Conversion to hex string}
%
%    \begin{macro}{\PE@EscapeHex}
%    \begin{macrocode}
\ifPE@etex
  \def\PE@EscapeHex#1{%
    \edef#1{\expandafter\PE@ToHex#1\relax}%
  }%
\else
  \def\PE@EscapeHex#1{%
    \def\PE@result{}%
    \expandafter\PE@ToHex#1\relax
    \let#1\PE@result
  }%
\fi
%    \end{macrocode}
%    \end{macro}
%    \begin{macro}{\PE@ToHex}
%    \begin{macrocode}
\def\PE@ToHex#1{%
  \ifx\relax#1%
  \else
    \PE@HexChar{#1}%
    \expandafter\PE@ToHex
  \fi
}%
%    \end{macrocode}
%    \end{macro}
%    \begin{macro}{\PE@HexChar}
%    \begin{macrocode}
\ifPE@etex
  \def\PE@HexChar#1{%
    \PE@HexDigit{\numexpr\dimexpr.0625\dimexpr`#1sp\relax\relax\relax}%
    \PE@HexDigit{%
      \numexpr`#1-16*\dimexpr.0625\dimexpr`#1sp\relax\relax\relax
    }%
  }%
\else
  \def\PE@HexChar#1{%
    \dimen0=`#1sp%
    \dimen2=.0625\dimen0 %
    \advance\dimen0-16\dimen2 %
    \edef\PE@result{%
      \PE@result
      \PE@HexDigit{\dimen2 }%
      \PE@HexDigit{\dimen0 }%
    }%
  }%
\fi
%    \end{macrocode}
%    \end{macro}
%    \begin{macro}{\PE@HexDigit}
%    \begin{macrocode}
\def\PE@HexDigit#1{%
  \expandafter\string
  \ifcase#1%
    0\or 1\or 2\or 3\or 4\or 5\or 6\or 7\or 8\or 9\or
    A\or B\or C\or D\or E\or F%
  \fi
}
%    \end{macrocode}
%    \end{macro}
%
% \subsubsection{Character code to octal number}
%
%    \begin{macro}{\PE@OctChar}
%    \begin{macrocode}
\ifPE@etex
  \def\PE@OctChar#1{%
    \expandafter\PE@@OctChar
        \the\numexpr\dimexpr.015625\dimexpr`#1sp\relax\relax
            \expandafter\relax
        \expandafter\relax
        \the\numexpr\dimexpr.125\dimexpr`#1sp\relax\relax\relax
        \relax
        #1%
  }%
  \def\PE@@OctChar#1\relax#2\relax#3{%
    \PE@backslash
    #1%
    \the\numexpr#2-8*#1\relax
    \the\numexpr\dimexpr`#3sp\relax-8*#2\relax
  }%
\else
  \def\PE@OctChar#1{%
    \dimen0=`#1sp%
    \dimen2=.125\dimen0 %
    \dimen4=.125\dimen2 %
    \advance\dimen0-8\dimen2 %
    \advance\dimen2-8\dimen4 %
    \edef\PE@result{%
      \PE@result
      \PE@backslash
      \number\dimen4 %
      \number\dimen2 %
      \number\dimen0 %
    }%
  }%
\fi
%    \end{macrocode}
%    \end{macro}
%
% \subsubsection{Unpack hex string}
%
%    \begin{macro}{\PE@UnescapeHex}
%    \begin{macrocode}
\def\PE@UnescapeHex#1{%
  \begingroup
    \PE@InitUccodeHexDigit
    \def\PE@result{}%
    \expandafter\PE@DeHex#1\relax\relax
  \expandafter\endgroup
  \expandafter\def\expandafter#1\expandafter{\PE@result}%
}
%    \end{macrocode}
%    \end{macro}
%    \begin{macro}{\PE@DeHex}
%    \begin{macrocode}
\def\PE@DeHex#1#2{%
  \ifx#2\relax
    \ifx#1\relax
      \let\PE@next\relax
    \else
      \uppercase{%
        \def\PE@testA{#1}%
      }%
      \ifcase\expandafter\PE@TestUcHexDigit\PE@testA
        \def\PE@next{%
          \PE@DeHex#10\relax\relax
        }%
      \else
        \let\PE@next\relax
      \fi
    \fi
  \else
    \uppercase{%
      \def\PE@testA{#1}%
      \def\PE@testB{#2}%
    }%
    \ifcase\expandafter\PE@TestUcHexDigit\PE@testA
      \ifcase\expandafter\PE@TestUcHexDigit\PE@testB
        \uccode\z@="\PE@testA\PE@testB\relax
        \ifnum\uccode\z@=32 %
          \let\PE@temp\PE@space@space
        \else
          \uppercase{%
            \def\PE@temp{^^@}%
          }%
        \fi
        \edef\PE@result{\PE@result\PE@temp}%
        \let\PE@next\PE@DeHex
      \else
        % invalid input sequence
        \def\PE@next{%
          \PE@DeHex#1%
        }%
      \fi
    \else
      % invalid input sequence
      \def\PE@next{\PE@DeHex#2}%
    \fi
  \fi
  \PE@next
}
%    \end{macrocode}
%    \end{macro}
%
% \subsubsection{Conversion to PDF name}
%
%    \begin{macro}{\PE@EscapeName}
%    \begin{macrocode}
\ifPE@etex
  \def\PE@EscapeName#1{%
    \edef#1{\expandafter\PE@EscapeNameTokens#1\relax}%
  }%
\else
  \def\PE@EscapeName#1{%
    \def\PE@result{}%
    \expandafter\PE@EscapeNameTokens#1\relax
    \let#1\PE@result
  }%
\fi
%    \end{macrocode}
%    \end{macro}
%    \begin{macro}{\PE@EscapeNameTokens}
%    \begin{macrocode}
\def\PE@EscapeNameTokens#1{%
  \ifx\relax#1%
  \else
    \ifnum`#1<33 %
      \ifcase`#1 %
        % drop illegal zero
      \else
        \PE@EscapeNameAdd\PE@hash
        \PE@HexChar#1%
      \fi
    \else
      \ifnum`#1>126 %
        \PE@EscapeNameAdd\PE@hash
        \PE@HexChar#1%
      \else  \ifnum`#1=35  \PE@EscapeNameHashChar 23% #
        \else\ifnum`#1=37  \PE@EscapeNameHashChar 25% %
        \else\ifnum`#1=40  \PE@EscapeNameHashChar 28% (
        \else\ifnum`#1=41  \PE@EscapeNameHashChar 29% )
        \else\ifnum`#1=47  \PE@EscapeNameHashChar 2F% /
        \else\ifnum`#1=60  \PE@EscapeNameHashChar 3C% <
        \else\ifnum`#1=62  \PE@EscapeNameHashChar 3E% >
        \else\ifnum`#1=91  \PE@EscapeNameHashChar 5B% [
        \else\ifnum`#1=93  \PE@EscapeNameHashChar 5D% ]
        \else\ifnum`#1=123 \PE@EscapeNameHashChar 7B% {
        \else\ifnum`#1=125 \PE@EscapeNameHashChar 7D% }
        \else
          \PE@EscapeNameAdd{#1}%
        \fi\fi\fi\fi\fi\fi\fi\fi\fi\fi\fi
      \fi
    \fi
    \expandafter\PE@EscapeNameTokens
  \fi
}%
\def\PE@EscapeNameHashChar#1#2{%
  \PE@EscapeNameAdd{\PE@hash\string#1\string#2}%
}%
%    \end{macrocode}
%    \end{macro}
%    \begin{macro}{\PE@EscapeNameAdd}
%    \begin{macrocode}
\ifPE@etex
  \def\PE@EscapeNameAdd#1{#1}%
\else
  \def\PE@EscapeNameAdd#1{%
    \edef\PE@result{%
      \PE@result
      #1%
    }%
  }%
\fi
%    \end{macrocode}
%    \end{macro}
%
% \subsubsection{Conversion to PDF string}
%
%    \begin{macro}{\PE@EscapeString}
%    \begin{macrocode}
\ifPE@etex
  \def\PE@EscapeString#1{%
    \edef#1{\expandafter\PE@EscapeStringTokens#1\relax}%
  }%
\else
  \def\PE@EscapeString#1{%
    \begingroup
      \def\PE@result{}%
      \expandafter\PE@EscapeStringTokens#1\relax
    \expandafter\endgroup
    \expandafter\def\expandafter#1\expandafter{\PE@result}%
  }%
\fi
%    \end{macrocode}
%    \end{macro}
%    \begin{macro}{\PE@EscapeStringTokens}
%    \begin{macrocode}
\def\PE@EscapeStringTokens#1{%
  \ifx\relax#1%
  \else
    \ifnum`#1<33 %
      \PE@OctChar#1%
    \else
      \ifnum`#1>126 %
        \PE@OctChar#1%
      \else  \ifnum`#1=40 \PE@EscapeStringAdd{\string\(}% (
        \else\ifnum`#1=41 \PE@EscapeStringAdd{\string\)}% )
        \else\ifnum`#1=92 \PE@EscapeStringAdd{\string\\}% \
        \else
          \PE@EscapeStringAdd{#1}%
        \fi\fi\fi
      \fi
    \fi
    \expandafter\PE@EscapeStringTokens
  \fi
}%
%    \end{macrocode}
%    \end{macro}
%    \begin{macro}{\PE@EscapeStringAdd}
%    \begin{macrocode}
\ifPE@etex
  \def\PE@EscapeStringAdd#1{#1}%
\else
  \def\PE@EscapeStringAdd#1{%
    \edef\PE@result{%
      \PE@result
      #1%
    }%
  }%
\fi
%    \end{macrocode}
%    \end{macro}
%
%    \begin{macrocode}
\PE@AtEnd
%    \end{macrocode}
%
%    \begin{macrocode}
%</package>
%    \end{macrocode}
%
% \section{Test}
%
%    \begin{macrocode}
%<*test1|test2|test3>
\NeedsTeXFormat{LaTeX2e}
\makeatletter
%    \end{macrocode}
%
% \subsection{Test with \cs{pdfescape...} commands}
%
%    \begin{macrocode}
%<*test1>
\ProvidesFile{pdfescape-test1.tex}%
    [2007/04/21 v1.4 Test with \string\pdfescape... commands]%
%</test1>
%    \end{macrocode}
%
% \subsection{Test without \cs{pdfescape...}, with \eTeX}
%
%    \begin{macrocode}
%<*test2>
\ProvidesFile{pdfescape-test2.tex}%
    [2007/04/21 v1.4 Test without \string\pdfescape..., with e-TeX]%
%</test2>
%    \end{macrocode}
%
% \subsection{Test without \cs{pdfescape...} and \eTeX}
%
%    \begin{macrocode}
%<*test3>
\ProvidesFile{pdfescape-test3.tex}%
    [2007/04/21 v1.4 Test without \string\pdfescape... and e-TeX]%
%</test3>
%    \end{macrocode}
%
% \subsection{Check/ensure test preconditions}
%
% \subsubsection{Check \cs{pdfescape...}}
%
%    \begin{macrocode}
%<*test1>
\@ifundefined{pdfescapehex}{%
  \PackageError{pdfescape-test1}{%
    Missing \string\pdfescape... commands%
  }{Test aborted.}%
  \stop
}{}
%</test1>
%    \end{macrocode}
%    \begin{macrocode}
%<*test2|test3>
\let\pdfescapehex\@undefined
\let\pdfunescapehex\@undefined
\let\pdfescapename\@undefined
\let\pdfescapestring\@undefined
%</test2|test3>
%    \end{macrocode}
%
% \subsubsection{Check \eTeX}
%
%    \begin{macrocode}
%<*test2>
\@ifundefined{numexpr}{%
  \PackageError{pdfescape-test2}{%
    Missing \eTeX
  }{Test aborted.}%
  \stop
}{}
%</test2>
%    \end{macrocode}
% Package \xpackage{qstest} uses \eTeX, thus \eTeX's features
% can only be disabled later during loading of package \xpackage{pdfescape}.
%
% \subsection{Common part}
%
%    The files for testing uses the framework, provided by the
%    new package \xpackage{qstest} of David Kastrup.
%    \begin{macrocode}
\RequirePackage{qstest}
\IncludeTests{*}
\LogTests{log}{*}{*}

\newcommand*{\ExpectVar}[2]{%
  \ifx#1#2%
  \else
    \begingroup
      \@onelevel@sanitize#1%
      \@onelevel@sanitize#2%
      \typeout{[#1] <> [#2]}% hash-ok
    \endgroup
  \fi
  \Expect*{\ifx#1#2true\else false\fi}{true}%
}

\makeatletter
\begingroup
  \gdef\AllBytes{}%
  \count@=0 %
  \catcode0=12 %
  \@whilenum\count@<256 \do{%
    \lccode0=\count@
    \ifnum\count@=32 %
      \xdef\AllBytes{\AllBytes\space}%
    \else
      \lowercase{%
        \xdef\AllBytes{\AllBytes^^@}%
      }%
    \fi
    \advance\count@ by 1 %
  }%
\endgroup
\newcommand*{\AllBytesHex}{%
  000102030405060708090A0B0C0D0E0F%
  101112131415161718191A1B1C1D1E1F%
  202122232425262728292A2B2C2D2E2F%
  303132333435363738393A3B3C3D3E3F%
  404142434445464748494A4B4C4D4E4F%
  505152535455565758595A5B5C5D5E5F%
  606162636465666768696A6B6C6D6E6F%
  707172737475767778797A7B7C7D7E7F%
  808182838485868788898A8B8C8D8E8F%
  909192939495969798999A9B9C9D9E9F%
  A0A1A2A3A4A5A6A7A8A9AAABACADAEAF%
  B0B1B2B3B4B5B6B7B8B9BABBBCBDBEBF%
  C0C1C2C3C4C5C6C7C8C9CACBCCCDCECF%
  D0D1D2D3D4D5D6D7D8D9DADBDCDDDEDF%
  E0E1E2E3E4E5E6E7E8E9EAEBECEDEEEF%
  F0F1F2F3F4F5F6F7F8F9FAFBFCFDFEFF%
}
\@onelevel@sanitize\AllBytesHex
\expandafter\lowercase\expandafter{%
  \expandafter\newcommand\expandafter*\expandafter\AllBytesHexLC
      \expandafter{\AllBytesHex}%
}
\newcommand*{\AllBytesName}{}
\begingroup
  \catcode`\#=12 %
  \xdef\AllBytesName{%
    #01#02#03#04#05#06#07#08#09#0A#0B#0C#0D#0E#0F%
    #10#11#12#13#14#15#16#17#18#19#1A#1B#1C#1D#1E#1F%
    #20!"#23$#25&'#28#29*+,-.#2F%
    0123456789:;#3C=#3E?%
    @ABCDEFGHIJKLMNO%
    PQRSTUVWXYZ#5B\@backslashchar#5D^_%
    `abcdefghijklmno%
    pqrstuvwxyz#7B|#7D\string~#7F%
    #80#81#82#83#84#85#86#87#88#89#8A#8B#8C#8D#8E#8F%
    #90#91#92#93#94#95#96#97#98#99#9A#9B#9C#9D#9E#9F%
    #A0#A1#A2#A3#A4#A5#A6#A7#A8#A9#AA#AB#AC#AD#AE#AF%
    #B0#B1#B2#B3#B4#B5#B6#B7#B8#B9#BA#BB#BC#BD#BE#BF%
    #C0#C1#C2#C3#C4#C5#C6#C7#C8#C9#CA#CB#CC#CD#CE#CF%
    #D0#D1#D2#D3#D4#D5#D6#D7#D8#D9#DA#DB#DC#DD#DE#DF%
    #E0#E1#E2#E3#E4#E5#E6#E7#E8#E9#EA#EB#EC#ED#EE#EF%
    #F0#F1#F2#F3#F4#F5#F6#F7#F8#F9#FA#FB#FC#FD#FE#FF%
  }%
\endgroup
\@onelevel@sanitize\AllBytesName

\newcommand*{\AllBytesString}{}
\begingroup
  \def\|{|}%
  \edef\%{\@percentchar}%
  \catcode`\|=0 %
  \catcode`\#=12 %
  \catcode`\~=12 %
  \catcode`\\=12 %
  |xdef|AllBytesString{%
    \000\001\002\003\004\005\006\007\010\011\012\013\014\015\016\017%
    \020\021\022\023\024\025\026\027\030\031\032\033\034\035\036\037%
    \040!"#$|%&'\(\)*+,-./%
    0123456789:;<=>?%
    @ABCDEFGHIJKLMNO%
    PQRSTUVWXYZ[\\]^_%
    `abcdefghijklmno%
    pqrstuvwxyz{||}~\177%
    \200\201\202\203\204\205\206\207\210\211\212\213\214\215\216\217%
    \220\221\222\223\224\225\226\227\230\231\232\233\234\235\236\237%
    \240\241\242\243\244\245\246\247\250\251\252\253\254\255\256\257%
    \260\261\262\263\264\265\266\267\270\271\272\273\274\275\276\277%
    \300\301\302\303\304\305\306\307\310\311\312\313\314\315\316\317%
    \320\321\322\323\324\325\326\327\330\331\332\333\334\335\336\337%
    \340\341\342\343\344\345\346\347\350\351\352\353\354\355\356\357%
    \360\361\362\363\364\365\366\367\370\371\372\373\374\375\376\377%
  }%
|endgroup
\@onelevel@sanitize\AllBytesString

%<*test3>
\let\org@detokenize\detokenize
\let\detokenize\@undefined
\let\org@numexpr\numexpr
\let\numexpr\@undefined
%</test3>
\RequirePackage{pdfescape}
%<*test3>
\let\detokenize\org@detokenize
\let\numexpr\org@numexpr
%</test3>

\begin{qstest}{all-hex}{\AllBytes, escapehex}
  \EdefEscapeHex\x{\AllBytes}%
  \Expect*{\x}*{\AllBytesHex}%
  \ExpectVar\x\AllBytesHex
\end{qstest}

\begin{qstest}{all-unhex}{\AllBytesHex, unescapehex}
  \EdefUnescapeHex\x{\AllBytesHex}%
  \Expect*{\x}*{\AllBytes}%
  \ExpectVar\x\AllBytes
\end{qstest}

\begin{qstest}{all-unhex-lc}{\AllBytesHexLC, unescapehex, lowercase}
  \EdefUnescapeHex\x{\AllBytesHexLC}%
  \Expect*{\x}*{\AllBytes}%
  \ExpectVar\x\AllBytes
\end{qstest}

\begin{qstest}{unhex-incomplete}{unescapehex, incomplete}
  \EdefUnescapeHex\x{4}%
  \Expect*{\x}{@}%
\end{qstest}

\begin{qstest}{unhex-space}{unescapehex, space}
  \EdefUnescapeHex\x{20}%
  \Expect*{\x}{ }%
  \ExpectVar\x\space
\end{qstest}

\begin{qstest}{unhex-spaces}{unescapehex, spaces}
  \EdefUnescapeHex\x{204020204120}%
  \def\y#1{%
    \edef\z{#1\string @#1#1\string A#1}%
  }\y{ }%
  \Expect*{\x}*{\z}%
  \ExpectVar\x\z
\end{qstest}

\begin{qstest}{unhex-hash}{unescapehex, hash}
  \catcode`\#=12 %
  \EdefUnescapeHex\x{#20}%
  \ExpectVar\x\space
\end{qstest}

\begin{qstest}{unhex-invalid}{unescapehex, invalid}
  \def\test#1#2{%
    \EdefUnescapeHex\x{#1}%
    \edef\y{#2}%
    \@onelevel@sanitize\y
    \ExpectVar\x\y
  }%
%<*test1>
  \edef\x{\pdfunescapehex{4X}}%
  \edef\y{\string @}%
  \ifx\x\y
  \else
    \def~{\space}%
    \typeout{************************************}%
    \typeout{* Your pdfTeX contains bug 777.~~~~*}%
    \typeout{* This test is redefined as dummy, *}%
    \typeout{* because it triggers the bug.~~~~~*}%
    \typeout{************************************}%
    \def\test#1#2{}%
  \fi
%</test1>
  \test{X}{}%
  \test{XY}{}%
  \test{XYZ}{}%
  \test{A}{^^a0}%
  \test{AX}{^^a0}%
  \test{XA}{^^a0}%
  \test{XXAXX}{^^a0}%
\end{qstest}

\begin{qstest}{all-name}{\AllBytes, escapename}
  \EdefEscapeName\x{\AllBytes}%
  \Expect*{\x}*{\AllBytesName}%
  \ExpectVar\x\AllBytesName
\end{qstest}

\begin{qstest}{all-string}{\AllBytes, escapestring}
  \EdefEscapeString\x{\AllBytes}%
  \Expect*{\x}*{\AllBytesString}%
  \ExpectVar\x\AllBytesString
\end{qstest}

\begin{qstest}{uchexdigit}{unescape, uppercase hex digit}
  \catcode`\@=11 %
  \catcode0=12 %
  \def\test#1#2{%
    \uccode0=#1\relax
    \uppercase{%
      \def\x{^^@}%
    }%
    \Expect*{%
       \ifcase\expandafter\PE@TestUcHexDigit\x
         true%
       \else
         false%
       \fi
    }{#2}%
  }%
  \def\range#1#2#3{%
    \count0=#1\relax
    \loop
    \ifnum\count0<#2\relax
      \test{\count0}{#3}%
      \advance\count0 by 1 %
    \repeat
  }%
  \range{0}{47}{false}%
  \range{48}{57}{true}%
  \range{58}{64}{false}%
  \range{65}{70}{true}%
  \range{71}{255}{false}%
\end{qstest}

\begin{qstest}{unescapename}{unescapename}
  \def\test#1#2{%
    \EdefUnescapeName\x{#1}%
    \edef\y{#2}%
    \@onelevel@sanitize\y
    \ExpectVar\x\y
  }%
  \catcode`\#=12 %
  \catcode0=12 %
  \test{}{}%
  \test{x}{x}%
  \test{xy}{xy}%
  \test{#}{#}%
  \test{##}{##}%
  \test{###}{###}%
  \test{####}{####}%
  \test{#x}{#x}%
  \test{#xy}{#xy}%
  \test{#1}{#1}%
  \test{#40}{@}%
  \test{#400}{@0}%
  \test{#4x0}{#4x0}%
  \test{#ab}{^^ab}%
  \test{#00}{^^@}%
  \test{x#40y#40z}{x@y@z}%
  \test{#40#40#40#40}{@@@@}%
  \test{a#x}{a#x}%
  \test{a#xy}{a#xy}%
  \test{a#1}{a#1}%
  \test{a#40}{a@}%
  \test{a#400}{a@0}%
  \test{#20}{ }%
  \test{a#20}{a }%
  \test{a#20b}{a b}%
  \test{a#20#20#20b}{a \space\space b}%
\end{qstest}

\begin{qstest}{unescapestring}{unescapestring}
  \def\test#1#2{%
    \EdefUnescapeString\x{#1}%
    \edef\y{#2}%
    \@onelevel@sanitize\y
    \ExpectVar\x\y
  }%
  \catcode0=12 %
  \def\DefChar#1#2{%
    \begingroup
      \uccode0=#2\relax
    \uppercase{\endgroup
      \def#1{^^@}%
    }%
  }%
  \DefChar\nul{0}%
  \DefChar\one{1}%
  \DefChar\bel{8}%
  \DefChar\tab{9}%
  \DefChar\lf{10}%
  \DefChar\ff{12}%
  \DefChar\cr{13}%
  \DefChar\\{92}%
  \test{}{}%
  \test{a}{a}%
  \test{\\}{}%
  \test{\\\\}{\\}%
  \test{\\\\y}{\\y}%
  \test{\\000}{\nul}%
  \test{\\b}{\bel}%
  \test{\\t}{\tab}%
  \test{\\n}{\lf}%
  \test{\\f}{\ff}%
  \test{\\r}{\cr}%
  \test{\\(}{(}%
  \test{\\)}{)}%
  \test{\\040}{ }%
  \test{\\100}{@}%
  \test{\\40}{ }%
  \test{\\1}{\one}%
  \test{\\01}{\one}%
  \test{\\001}{\one}%
  \test{\\18}{\one8}%
  \test{\\018}{\one8}%
  \test{\\0018}{\one8}%
  \test{x\\}{x}%
  \test{x\\\\}{x\\}%
  \test{x\\\\y}{x\\y}%
  \test{x\\000}{x\nul}%
  \test{x\\b}{x\bel}%
  \test{x\\t}{x\tab}%
  \test{x\\n}{x\lf}%
  \test{x\\f}{x\ff}%
  \test{x\\r}{x\cr}%
  \test{x\\(}{x(}%
  \test{x\\)}{x)}%
  \test{x\\040}{x }%
  \test{x\\100}{x@}%
  \test{x\\40}{x }%
  \test{x\\1}{x\one}%
  \test{x\\01}{x\one}%
  \test{x\\001}{x\one}%
  \test{x\\18}{x\one8}%
  \test{x\\018}{x\one8}%
  \test{x\\0018}{x\one8}%
  \test{\\b\\t\\n\\f\\r\\(\\)\\\\\\000\\040}{%
    \bel\tab\lf\ff\cr()\\\nul\space
  }%
  \test{\\\lf}{}%
  \test{x\\\lf}{x}%
  \test{\cr}{\lf}%
  \test{\cr\lf}{\lf}%
  \test{\lf}{\lf}%
  \test{\lf\cr}{\lf\lf}%
  \test{x\cr}{x\lf}%
  \test{x\cr\lf}{x\lf}%
  \test{x\lf}{x\lf}%
  \test{x\lf\cr}{x\lf\lf}%
  \test{x\\\cr\lf y\cr}{xy\lf}%
\end{qstest}
\stop
%    \end{macrocode}
%
%    \begin{macrocode}
%</test1|test2|test3>
%    \end{macrocode}
% \section{Installation}
%
% \subsection{Download}
%
% \paragraph{Package.} This package is available on
% CTAN\footnote{\url{ftp://ftp.ctan.org/tex-archive/}}:
% \begin{description}
% \item[\CTAN{macros/latex/contrib/oberdiek/pdfescape.dtx}] The source file.
% \item[\CTAN{macros/latex/contrib/oberdiek/pdfescape.pdf}] Documentation.
% \end{description}
%
%
% \paragraph{Bundle.} All the packages of the bundle `oberdiek'
% are also available in a TDS compliant ZIP archive. There
% the packages are already unpacked and the documentation files
% are generated. The files and directories obey the TDS standard.
% \begin{description}
% \item[\CTAN{macros/latex/contrib/oberdiek/oberdiek-tds.zip}]
% \end{description}
%
% \subsection{Bundle installation}
%
% \paragraph{Unpacking.} Unpack the \xfile{oberdiek-tds.zip} in the
% TDS tree (also known as \xfile{texmf} tree) of your choice.
% Example (linux):
% \begin{quote}
%   |unzip oberdiek-tds.zip -d ~/texmf|
% \end{quote}
%
% \paragraph{Script installation.}
% Check the directory \xfile{TDS:scripts/oberdiek/} for
% scripts that need further installation steps.
% Package \xpackage{attachfile2} comes with the Perl script
% \xfile{pdfatfi.pl} that should be installed in such a way
% that it can be called as \texttt{pdfatfi}.
% Example (linux):
% \begin{quote}
%   |chmod +x scripts/oberdiek/pdfatfi.pl|\\
%   |cp scripts/oberdiek/pdfatfi.pl /usr/local/bin/|
% \end{quote}
%
% \subsection{Package installation}
%
% \paragraph{Unpacking.} The \xfile{.dtx} file is a self-extracting
% \docstrip\ archive. The files are extracted by running the
% \xfile{.dtx} through \plainTeX:
% \begin{quote}
%   \verb|tex pdfescape.dtx|
% \end{quote}
%
% \paragraph{TDS.} Now the different files must be moved into
% the different directories in your installation TDS tree
% (also known as \xfile{texmf} tree):
% \begin{quote}
% \def\t{^^A
% \begin{tabular}{@{}>{\ttfamily}l@{ $\rightarrow$ }>{\ttfamily}l@{}}
%   pdfescape.sty & tex/generic/oberdiek/pdfescape.sty\\
%   pdfescape.pdf & doc/latex/oberdiek/pdfescape.pdf\\
%   pdfescape-test1.tex & doc/latex/oberdiek/pdfescape-test1.tex\\
%   pdfescape-test2.tex & doc/latex/oberdiek/pdfescape-test2.tex\\
%   pdfescape-test3.tex & doc/latex/oberdiek/pdfescape-test3.tex\\
%   pdfescape.dtx & source/latex/oberdiek/pdfescape.dtx\\
% \end{tabular}^^A
% }^^A
% \sbox0{\t}^^A
% \ifdim\wd0>\linewidth
%   \begingroup
%     \advance\linewidth by\leftmargin
%     \advance\linewidth by\rightmargin
%   \edef\x{\endgroup
%     \def\noexpand\lw{\the\linewidth}^^A
%   }\x
%   \def\lwbox{^^A
%     \leavevmode
%     \hbox to \linewidth{^^A
%       \kern-\leftmargin\relax
%       \hss
%       \usebox0
%       \hss
%       \kern-\rightmargin\relax
%     }^^A
%   }^^A
%   \ifdim\wd0>\lw
%     \sbox0{\small\t}^^A
%     \ifdim\wd0>\linewidth
%       \ifdim\wd0>\lw
%         \sbox0{\footnotesize\t}^^A
%         \ifdim\wd0>\linewidth
%           \ifdim\wd0>\lw
%             \sbox0{\scriptsize\t}^^A
%             \ifdim\wd0>\linewidth
%               \ifdim\wd0>\lw
%                 \sbox0{\tiny\t}^^A
%                 \ifdim\wd0>\linewidth
%                   \lwbox
%                 \else
%                   \usebox0
%                 \fi
%               \else
%                 \lwbox
%               \fi
%             \else
%               \usebox0
%             \fi
%           \else
%             \lwbox
%           \fi
%         \else
%           \usebox0
%         \fi
%       \else
%         \lwbox
%       \fi  
%     \else
%       \usebox0
%     \fi
%   \else
%     \lwbox
%   \fi
% \else
%   \usebox0
% \fi
% \end{quote}
% If you have a \xfile{docstrip.cfg} that configures and enables \docstrip's
% TDS installing feature, then some files can already be in the right
% place, see the documentation of \docstrip.
%
% \subsection{Refresh file name databases}
%
% If your \TeX~distribution
% (\teTeX, \mikTeX, \dots) relies on file name databases, you must refresh
% these. For example, \teTeX\ users run \verb|texhash| or
% \verb|mktexlsr|.
%
% \subsection{Some details for the interested}
%
% \paragraph{Attached source.}
%
% The PDF documentation on CTAN also includes the
% \xfile{.dtx} source file. It can be extracted by
% AcrobatReader 6 or higher. Another option is \textsf{pdftk},
% e.g. unpack the file into the current directory:
% \begin{quote}
%   \verb|pdftk pdfescape.pdf unpack_files output .|
% \end{quote}
%
% \paragraph{Unpacking with \LaTeX.}
% The \xfile{.dtx} chooses its action depending on the format:
% \begin{description}
% \item[\plainTeX:] Run \docstrip\ and extract the files.
% \item[\LaTeX:] Generate the documentation.
% \end{description}
% If you insist on using \LaTeX\ for \docstrip\ (really,
% \docstrip\ does not need \LaTeX), then inform the autodetect routine
% about your intention:
% \begin{quote}
%   \verb|latex \let\install=y\input{pdfescape.dtx}|
% \end{quote}
% Do not forget to quote the argument according to the demands
% of your shell.
%
% \paragraph{Generating the documentation.}
% You can use both the \xfile{.dtx} or the \xfile{.drv} to generate
% the documentation. The process can be configured by the
% configuration file \xfile{ltxdoc.cfg}. For instance, put this
% line into this file, if you want to have A4 as paper format:
% \begin{quote}
%   \verb|\PassOptionsToClass{a4paper}{article}|
% \end{quote}
% An example follows how to generate the
% documentation with pdf\LaTeX:
% \begin{quote}
%\begin{verbatim}
%pdflatex pdfescape.dtx
%makeindex -s gind.ist pdfescape.idx
%pdflatex pdfescape.dtx
%makeindex -s gind.ist pdfescape.idx
%pdflatex pdfescape.dtx
%\end{verbatim}
% \end{quote}
%
% \begin{History}
%   \begin{Version}{2007/02/21 v1.0}
%   \item
%     First version.
%   \end{Version}
%   \begin{Version}{2007/02/25 v1.1}
%   \item
%     Test files added.
%   \item
%     \cs{EdefUnescapeHex} supports lowercase letters.
%   \item
%     Fix: \verb|\EdefEscapeName{^^@}|
%   \item
%     Fix: \verb|\EdefEscapeName{\string#}|
%   \item
%     Fix for \cs{EdefUnescapeHex} in case of incomplete hex string.
%   \item
%     Fix: \cs{EdefUnescapeHex} generates space tokens with catcode
%     10 (space) in all cases.
%   \item
%     Fix: \cs{EdefEscapeHex} and \cs{EdefEscapeName} now generate
%     tokens with catcode 12 (other) only.
%   \end{Version}
%   \begin{Version}{2007/03/20 v1.2}
%   \item
%     Fix: Wrong year in \cs{ProvidesPackage}.
%   \end{Version}
%   \begin{Version}{2007/04/11 v1.3}
%   \item
%     Line ends sanitized.
%   \end{Version}
%   \begin{Version}{2007/04/21 v1.4}
%   \item
%     \cs{EdefUnescapeName} and \cs{EdefUnescapeString} added.
%   \end{Version}
% \end{History}
%
% \PrintIndex
%
% \Finale
\endinput
