Back to index

tetex-bin  3.0
ascii85.c
Go to the documentation of this file.
00001 /*  $Header$
00002 
00003     This is dvipdfm, a DVI to PDF translator.
00004     Copyright (C) 1998, 1999 by Mark A. Wicks
00005 
00006     This program is free software; you can redistribute it and/or modify
00007     it under the terms of the GNU General Public License as published by
00008     the Free Software Foundation; either version 2 of the License, or
00009     (at your option) any later version.
00010 
00011     This program is distributed in the hope that it will be useful,
00012     but WITHOUT ANY WARRANTY; without even the implied warranty of
00013     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00014     GNU General Public License for more details.
00015 
00016     You should have received a copy of the GNU General Public License
00017     along with this program; if not, write to the Free Software
00018     Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00019     
00020     The author may be contacted via the e-mail address
00021 
00022        mwicks@kettering.edu
00023 */
00024 
00025        
00026 #include <stdio.h>
00027 #include <ctype.h>
00028 
00029 static int nextch(void)
00030 {
00031   int ch;
00032   while ((ch = getchar()) >= 0) {
00033     if (ch >= '!' && ch <= 'u' || ch == 'z')
00034       return ch;
00035     if (ch == '~' && getchar() == '>') {
00036       return -1;
00037     }
00038   }
00039   if (ch < 0) {
00040     fprintf (stderr, "Premature end of file.\n");
00041     exit(1);
00042   }
00043 }
00044 
00045 unsigned long int ascii85;
00046 unsigned binary[4];
00047 
00048 static decode_block(void)
00049 {
00050   int i;
00051   for (i=3; i>=0; i--){
00052     binary[i] = ascii85 % 256u;
00053     ascii85 /= 256u;
00054   }
00055 }
00056 
00057 static output_block (int n)
00058 {
00059   int i;
00060   for (i=0; i<n; i++) {
00061     putchar (binary[i]);
00062   }
00063 }
00064 
00065 main (int argc, char *argv[])
00066 {
00067   int i, ch, eof = 0, nread;
00068   while (!eof) {
00069     ascii85 = 0;
00070     nread = 0;
00071     /* Look ahead for special zero key */
00072     if ((ch = nextch()) == 'z') {
00073       nread = 4;  /* Lie to it */
00074     } else{
00075       ungetc (ch, stdin);
00076       for (i=0; i<5; i++) {
00077        if ((ch=nextch()) < 0) {
00078          eof = 1;
00079          break;
00080        }
00081        ascii85 = ascii85 * 85 + (ch-'!');
00082        nread += 1;
00083       }
00084     }
00085     if (nread > 1) {
00086       decode_block();
00087       output_block(nread-1);
00088     }
00089   }
00090 }