summaryrefslogtreecommitdiff
path: root/src/Type1/t1io.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/Type1/t1io.c')
-rw-r--r--src/Type1/t1io.c165
1 files changed, 126 insertions, 39 deletions
diff --git a/src/Type1/t1io.c b/src/Type1/t1io.c
index cd5b77a..d5ce774 100644
--- a/src/Type1/t1io.c
+++ b/src/Type1/t1io.c
@@ -28,21 +28,47 @@
* SOFTWARE.
* Author: Carol H. Thompson IBM Almaden Research Center
*/
+/* Copyright (c) 1994-1999 Silicon Graphics, Inc. All Rights Reserved.
+ *
+ * The contents of this file are subject to the CID Font Code Public Licence
+ * Version 1.0 (the "License"). You may not use this file except in compliance
+ * with the Licence. You may obtain a copy of the License at Silicon Graphics,
+ * Inc., attn: Legal Services, 2011 N. Shoreline Blvd., Mountain View, CA
+ * 94043 or at http://www.sgi.com/software/opensource/cid/license.html.
+ *
+ * Software distributed under the License is distributed on an "AS IS" basis.
+ * ALL WARRANTIES ARE DISCLAIMED, INCLUDING, WITHOUT LIMITATION, ANY IMPLIED
+ * WARRANTIES OF MERCHANTABILITY, OF FITNESS FOR A PARTICULAR PURPOSE OR OF
+ * NON-INFRINGEMENT. See the License for the specific language governing
+ * rights and limitations under the License.
+ *
+ * The Original Software is CID font code that was developed by Silicon
+ * Graphics, Inc.
+ */
+/* $XFree86: xc/lib/font/Type1/t1io.c,v 3.9 2001/08/27 19:49:54 dawes Exp $ */
/*******************************************************************
* I/O package for Type 1 font reading
********************************************************************/
-
+
#ifndef STATIC
#define STATIC static
#endif
+#ifndef FONTMODULE
#include <fcntl.h>
+#include <unistd.h>
+#else
+#include "Xdefs.h" /* Bool declaration */
+#include "Xmd.h" /* INT32 declaration */
+#include "xf86_ansic.h"
+#endif
#include "t1stdio.h"
#include "t1hdigit.h"
#ifdef WIN32
#include <X11/Xw32defs.h>
#endif
-
+#include "Xdefs.h"
+
/* Constants and variables used in the decryption */
#define c1 ((unsigned short)52845)
#define c2 ((unsigned short)22719)
@@ -56,16 +82,22 @@ STATIC F_FILE TheFile;
STATIC unsigned char TheBuffer[F_BUFSIZ];
/* Our routines */
-F_FILE *T1Open(), *T1Eexec();
-int T1Close();
-int T1Read(), T1Getc(), T1Ungetc();
-STATIC int T1Decrypt(), T1Fill();
+static int T1Decrypt ( unsigned char *p, int len );
+static int T1Fill ( F_FILE *f );
+
+#ifdef BUILDCID
+void
+resetDecrypt(void)
+{
+ Decrypt = 0;
+}
+#endif
/* -------------------------------------------------------------- */
/*ARGSUSED*/
-F_FILE *T1Open(fn, mode)
- char *fn; /* Pointer to filename */
- char *mode; /* Pointer to open mode string */
+F_FILE *
+T1Open(char *fn, /* Pointer to filename */
+ char *mode) /* Pointer to open mode string */
{
F_FILE *of = &TheFile;
int oflags = O_RDONLY; /* We know we are only reading */
@@ -75,7 +107,7 @@ F_FILE *T1Open(fn, mode)
#ifdef O_BINARY /* VMS or DOS */
oflags |= O_BINARY;
#endif
- of->fd = open(fn, oflags);
+ of->fd = open(fn, oflags, 0);
if (of->fd < 0)
return NULL;
@@ -91,8 +123,8 @@ F_FILE *T1Open(fn, mode)
} /* end Open */
/* -------------------------------------------------------------- */
-int T1Getc(f) /* Read one character */
- F_FILE *f; /* Stream descriptor */
+int /* Read one character */
+T1Getc(F_FILE *f) /* Stream descriptor */
{
if (f->b_base == NULL) return EOF; /* already closed */
@@ -111,9 +143,9 @@ int T1Getc(f) /* Read one character */
} /* end Getc */
/* -------------------------------------------------------------- */
-int T1Ungetc(c, f) /* Put back one character */
- int c;
- F_FILE *f; /* Stream descriptor */
+int /* Put back one character */
+T1Ungetc(int c,
+ F_FILE *f) /* Stream descriptor */
{
if (c != EOF) {
f->ungotc = c;
@@ -124,11 +156,11 @@ int T1Ungetc(c, f) /* Put back one character */
} /* end Ungetc */
/* -------------------------------------------------------------- */
-int T1Read(buffP, size, n, f) /* Read n items into caller's buffer */
- char *buffP; /* Buffer to be filled */
- int size; /* Size of each item */
- int n; /* Number of items to read */
- F_FILE *f; /* Stream descriptor */
+int /* Read n items into caller's buffer */
+T1Read(char *buffP, /* Buffer to be filled */
+ int size, /* Size of each item */
+ int n, /* Number of items to read */
+ F_FILE *f) /* Stream descriptor */
{
int bytelen, cnt, i;
F_char *p = (F_char *)buffP;
@@ -162,23 +194,66 @@ int T1Read(buffP, size, n, f) /* Read n items into caller's buffer */
} /* end Read */
/* -------------------------------------------------------------- */
-int T1Close(f) /* Close the file */
- F_FILE *f; /* Stream descriptor */
+int /* Close the file */
+T1Close(F_FILE *f) /* Stream descriptor */
{
if (f->b_base == NULL) return 0; /* already closed */
f->b_base = NULL; /* no valid stream */
return close(f->fd);
} /* end Close */
-#ifdef __STDC__
-#define pointer void *
-#else
-#define pointer char *
-#endif
/* -------------------------------------------------------------- */
-F_FILE *T1eexec(f) /* Initialization */
- F_FILE *f; /* Stream descriptor */
+F_FILE * /* Initialization */
+T1eexec(F_FILE *f) /* Stream descriptor */
+{
+ int i, c;
+ int H;
+ unsigned char *p;
+ unsigned char randomP[8];
+
+ r = 55665; /* initial key */
+ asc = 1; /* indicate ASCII form */
+
+ /* Consume the 4 random bytes, determining if we are also to
+ ASCIIDecodeHex as we process our input. (See pages 63-64
+ of the Adobe Type 1 Font Format book.) */
+
+ /* Skip over any initial white space chars */
+ while (HighHexP[c=_XT1getc(f)] == HWHITE_SPACE) ;
+
+ /* If ASCII, the next 7 chars are guaranteed consecutive */
+ randomP[0] = c; /* store first non white space char */
+ T1Read((pointer)(randomP+1), 1, 3, f); /* read 3 more, for a total of 4 */
+ /* store first four chars */
+ for (i=0,p=randomP; i<4; i++) { /* Check 4 valid ASCIIEncode chars */
+ if (HighHexP[*p++] > LAST_HDIGIT) { /* non-ASCII byte */
+ asc = 0;
+ break;
+ }
+ }
+ if (asc) { /* ASCII form, convert first eight bytes to binary */
+ T1Read((pointer)(randomP+4), 1, 4, f); /* Need four more */
+ for (i=0,p=randomP; i<4; i++) { /* Convert */
+ H = HighHexP[*p++];
+ randomP[i] = H | LowHexP[*p++];
+ }
+ }
+
+ /* Adjust our key */
+ for (i=0,p=randomP; i<4; i++) {
+ r = (*p++ + r) * c1 + c2;
+ }
+
+ /* Decrypt the remaining buffered bytes */
+ f->b_cnt = T1Decrypt(f->b_ptr, f->b_cnt);
+ Decrypt = 1;
+ return (T1Feof(f))?NULL:f;
+} /* end eexec */
+
+#ifdef BUILDCID
+F_FILE * /* Initialization */
+CIDeexec(F_FILE *f) /* Stream descriptor */
{
int i, c;
int H;
@@ -218,19 +293,31 @@ F_FILE *T1eexec(f) /* Initialization */
r = (*p++ + r) * c1 + c2;
}
- /* Decrypt the remaining buffered bytes */
- f->b_cnt = T1Decrypt(f->b_ptr, f->b_cnt);
+ /* Decrypt up to, but not including, the first '%' sign */
+ if (f->b_cnt > 0) {
+ for (i = 0; i < f->b_cnt; i++)
+ if (*(f->b_ptr + i) == '%')
+ break;
+
+ if (i < f->b_cnt) {
+ if (i == 0)
+ f->b_cnt = 0;
+ else
+ f->b_cnt = T1Decrypt(f->b_ptr, i);
+ } else
+ f->b_cnt = T1Decrypt(f->b_ptr, f->b_cnt);
+ }
Decrypt = 1;
return (T1Feof(f))?NULL:f;
} /* end eexec */
-
+#endif
+
/* -------------------------------------------------------------- */
-STATIC int T1Decrypt(p, len)
- unsigned char *p;
- int len;
+STATIC int
+T1Decrypt(unsigned char *p, int len)
{
int n;
- int H, L;
+ int H = 0, L;
unsigned char *inp = p;
unsigned char *tblP;
@@ -272,11 +359,11 @@ STATIC int T1Decrypt(p, len)
} /* end Decrypt */
/* -------------------------------------------------------------- */
-STATIC int T1Fill(f) /* Refill stream buffer */
- F_FILE *f; /* Stream descriptor */
+STATIC int /* Refill stream buffer */
+T1Fill(F_FILE *f) /* Stream descriptor */
{
int rc;
-
+
rc = read(f->fd, f->b_base, F_BUFSIZ);
/* propagate any error or eof to current file */
if (rc <= 0) {