Back to index

tetex-bin  3.0
t1io.c
Go to the documentation of this file.
00001 /* $XConsortium: t1io.c,v 1.4 91/10/10 11:19:41 rws Exp $ */
00002 /* Copyright International Business Machines,Corp. 1991
00003  * All Rights Reserved
00004  *
00005  * License to use, copy, modify, and distribute this software
00006  * and its documentation for any purpose and without fee is
00007  * hereby granted, provided that the above copyright notice
00008  * appear in all copies and that both that copyright notice and
00009  * this permission notice appear in supporting documentation,
00010  * and that the name of IBM not be used in advertising or
00011  * publicity pertaining to distribution of the software without
00012  * specific, written prior permission.
00013  *
00014  * IBM PROVIDES THIS SOFTWARE "AS IS", WITHOUT ANY WARRANTIES
00015  * OF ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING, BUT NOT
00016  * LIMITED TO ANY IMPLIED WARRANTIES OF MERCHANTABILITY,
00017  * FITNESS FOR A PARTICULAR PURPOSE, AND NONINFRINGEMENT OF
00018  * THIRD PARTY RIGHTS.  THE ENTIRE RISK AS TO THE QUALITY AND
00019  * PERFORMANCE OF THE SOFTWARE, INCLUDING ANY DUTY TO SUPPORT
00020  * OR MAINTAIN, BELONGS TO THE LICENSEE.  SHOULD ANY PORTION OF
00021  * THE SOFTWARE PROVE DEFECTIVE, THE LICENSEE (NOT IBM) ASSUMES
00022  * THE ENTIRE COST OF ALL SERVICING, REPAIR AND CORRECTION.  IN
00023  * NO EVENT SHALL IBM BE LIABLE FOR ANY SPECIAL, INDIRECT OR
00024  * CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING
00025  * FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF
00026  * CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT
00027  * OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
00028  * SOFTWARE.
00029  *
00030  * Author: Carol H. Thompson  IBM Almaden Research Center
00031  */
00032 /*******************************************************************
00033 *  I/O package for Type 1 font reading
00034 ********************************************************************/
00035  
00036 #ifndef STATIC
00037 #define STATIC static
00038 #endif
00039  
00040 #include "c-auto.h"
00041 
00042 #ifdef HAVE_FCNTL_H
00043 #include <fcntl.h>
00044 #endif
00045 
00046 #include "t1stdio.h"
00047 #include "t1hdigit.h"
00048  
00049 /* Constants and variables used in the decryption */
00050 #define c1 ((unsigned short)52845)
00051 #define c2 ((unsigned short)22719)
00052 static unsigned short r;
00053 static int asc, Decrypt;
00054 static int extrach;
00055 static int haveextrach;
00056  
00057 /* Our single FILE structure and buffer for this package */
00058 STATIC F_FILE TheFile;
00059 STATIC unsigned char TheBuffer[F_BUFSIZ];
00060  
00061 /* Our routines */
00062 F_FILE *T1Open(), *T1Eexec();
00063 int T1Close();
00064 int T1Read(), T1Getc(), T1Ungetc();
00065 STATIC int T1Decrypt(), T1Fill();
00066  
00067 /* -------------------------------------------------------------- */
00068 /*ARGSUSED*/
00069 F_FILE *T1Open(fn, mode)
00070   char *fn;    /* Pointer to filename */
00071   char *mode;  /* Pointer to open mode string */
00072 {
00073   F_FILE *of = &TheFile;
00074  
00075   Decrypt = 0;
00076  
00077   /* We know we are only reading */
00078 #if defined(MSDOS) || defined(VMS) || defined(WIN32)
00079   if ((of->fd=open(fn, O_RDONLY | O_BINARY)) < 0) return NULL;
00080 #else
00081   if ((of->fd=open(fn, O_RDONLY)) < 0) return NULL;
00082 #endif
00083  
00084   /* Initialize the buffer information of our file descriptor */
00085   of->b_base = TheBuffer;
00086   of->b_size = F_BUFSIZ;
00087   of->b_ptr = NULL;
00088   of->b_cnt = 0;
00089   of->flags = 0;
00090   of->error = 0;
00091   haveextrach = 0;
00092   return &TheFile;
00093 } /* end Open */
00094  
00095 /* -------------------------------------------------------------- */
00096 int T1Getc(f)        /* Read one character */
00097   F_FILE *f;         /* Stream descriptor */
00098 {
00099   if (f->b_base == NULL) return EOF;  /* already closed */
00100  
00101   if (f->flags & UNGOTTENC) { /* there is an ungotten c */
00102     f->flags &= ~UNGOTTENC;
00103     return (int) f->ungotc;
00104   }
00105  
00106   if (f->b_cnt == 0)  /* Buffer needs to be (re)filled */
00107     f->b_cnt = T1Fill(f);
00108   if (f->b_cnt > 0) return (f->b_cnt--, (int) *(f->b_ptr++));
00109   else {
00110     f->flags |= FIOEOF;
00111     return EOF;
00112   }
00113 } /* end Getc */
00114  
00115 /* -------------------------------------------------------------- */
00116 int T1Ungetc(c, f)   /* Put back one character */
00117   int c;
00118   F_FILE *f;         /* Stream descriptor */
00119 {
00120   if (c != EOF) {
00121     f->ungotc = c;
00122     f->flags |= UNGOTTENC;  /* set flag */
00123     f->flags &= ~FIOEOF;    /* reset EOF */
00124   }
00125   return c;
00126 } /* end Ungetc */
00127  
00128 /* -------------------------------------------------------------- */
00129 int T1Read(buffP, size, n, f)  /* Read n items into caller's buffer */
00130   char *buffP;       /* Buffer to be filled */
00131   int   size;        /* Size of each item */
00132   int   n;           /* Number of items to read */
00133   F_FILE *f;         /* Stream descriptor */
00134 {
00135   int bytelen, cnt, i;
00136   F_char *p = (F_char *)buffP;
00137   int  icnt;         /* Number of characters to read */
00138  
00139   if (f->b_base == NULL) return 0;  /* closed */
00140   icnt = (size!=1)?n*size:n;  /* Number of bytes we want */
00141  
00142   if (f->flags & UNGOTTENC) { /* there is an ungotten c */
00143     f->flags &= ~UNGOTTENC;
00144     *(p++) = f->ungotc;
00145     icnt--; bytelen = 1;
00146   }
00147   else bytelen = 0;
00148  
00149   while (icnt > 0) {
00150     /* First use any bytes we have buffered in the stream buffer */
00151     if ((cnt=f->b_cnt) > 0) {
00152       if (cnt > icnt) cnt = icnt;
00153       for (i=0; i<cnt; i++) *(p++) = *(f->b_ptr++);
00154       f->b_cnt -= cnt;
00155       icnt -= cnt;
00156       bytelen += cnt;
00157     }
00158  
00159     if ((icnt == 0) || (f->flags & FIOEOF)) break;
00160  
00161     f->b_cnt = T1Fill(f);
00162   }
00163   return ((size!=1)?bytelen/size:bytelen);
00164 } /* end Read */
00165  
00166 /* -------------------------------------------------------------- */
00167 int T1Close(f)       /* Close the file */
00168   F_FILE *f;         /* Stream descriptor */
00169 {
00170   if (f->b_base == NULL) return 0;  /* already closed */
00171   f->b_base = NULL;  /* no valid stream */
00172   return close(f->fd);
00173 } /* end Close */
00174  
00175 /* -------------------------------------------------------------- */
00176 F_FILE *T1eexec(f)   /* Initialization */
00177   F_FILE *f;         /* Stream descriptor */
00178 {
00179   int i, c;
00180   int H;
00181   unsigned char *p;
00182   unsigned char randomP[8];
00183  
00184   r = 55665;  /* initial key */
00185   asc = 1;    /* indicate ASCII form */
00186  
00187   /* Consume the 4 random bytes, determining if we are also to
00188      ASCIIDecodeHex as we process our input.  (See pages 63-64
00189      of the Adobe Type 1 Font Format book.)  */
00190  
00191   /* Skip over any initial white space chars */
00192   while (HighHexP[c=getc(f)] == HWHITE_SPACE) ;
00193  
00194   /* If ASCII, the next 7 chars are guaranteed consecutive */
00195   randomP[0] = c;  /* store first non white space char */
00196   fread(randomP+1, 1, 3, f);  /* read 3 more, for a total of 4 */
00197   /* store first four chars */
00198   for (i=0,p=randomP; i<4; i++) {  /* Check 4 valid ASCIIEncode chars */
00199     if (HighHexP[*p++] > LAST_HDIGIT) {  /* non-ASCII byte */
00200       asc = 0;
00201       break;
00202     }
00203   }
00204   if (asc) {  /* ASCII form, convert first eight bytes to binary */
00205     fread(randomP+4, 1, 4, f);  /* Need four more */
00206     for (i=0,p=randomP; i<4; i++) {  /* Convert */
00207       H = HighHexP[*p++];
00208       randomP[i] = H | LowHexP[*p++];
00209     }
00210   }
00211  
00212   /* Adjust our key */
00213   for (i=0,p=randomP; i<4; i++) {
00214     r = (*p++ + r) * c1 + c2;
00215   }
00216  
00217   /* Decrypt the remaining buffered bytes */
00218   f->b_cnt = T1Decrypt(f->b_ptr, f->b_cnt);
00219   Decrypt = 1;
00220   return (feof(f))?NULL:f;
00221 } /* end eexec */
00222  
00223 /* -------------------------------------------------------------- */
00224 STATIC int T1Decrypt(p, len)
00225   unsigned char *p;
00226   int len;
00227 {
00228   int n;
00229   int H, L;
00230   unsigned char *inp = p;
00231   unsigned char *tblP;
00232  
00233   if (asc) {
00234     if (haveextrach) {
00235       H = extrach;
00236       tblP = LowHexP;
00237     }
00238     else tblP = HighHexP;
00239     for (n=0; len>0; len--) {
00240       L = tblP[*inp++];
00241       if (L == HWHITE_SPACE) continue;
00242       if (L > LAST_HDIGIT) break;
00243       if (tblP == HighHexP) { /* Got first hexit value */
00244         H = L;
00245         tblP = LowHexP;
00246       } else { /* Got second hexit value; compute value and store it */
00247         n++;
00248         tblP = HighHexP;
00249         H |= L;
00250         /* H is an int, 0 <= H <= 255, so all of this will work */
00251         *p++ = H ^ (r >> 8);
00252         r = (H + r) * c1 + c2;
00253       }
00254     }
00255     if (tblP != HighHexP) {  /* We had an odd number of hexits */
00256       extrach = H;
00257       haveextrach = 1;
00258     } else haveextrach = 0;
00259     return n;
00260   } else {
00261     for (n = len; n>0; n--) {
00262       H = *inp++;
00263       *p++ = H ^ (r >> 8);
00264       r = (H + r) * c1 + c2;
00265     }
00266     return len;
00267   }
00268 } /* end Decrypt */
00269  
00270 /* -------------------------------------------------------------- */
00271 STATIC int T1Fill(f) /* Refill stream buffer */
00272   F_FILE *f;         /* Stream descriptor */
00273 {
00274   int rc;
00275  
00276   rc = read(f->fd, f->b_base, F_BUFSIZ);
00277   /* propagate any error or eof to current file */
00278   if (rc <= 0) {
00279     if (rc == 0)    /* means EOF */
00280       f->flags |= FIOEOF;
00281     else {
00282       f->error = (short)-rc;
00283       f->flags |= FIOERROR;
00284       rc = 0;
00285     }
00286   }
00287   f->b_ptr = f->b_base;
00288   if (Decrypt) rc = T1Decrypt(f->b_base, rc);
00289   return rc;
00290 } /* end Fill */