📄 t1io.c
字号:
unsigned long off_save; unsigned long filesize; off_save=lseek( f->fd, 0, SEEK_CUR); filesize=lseek( f->fd, 0, SEEK_END); lseek( f->fd, off_save, SEEK_SET); return( filesize);}/* -------------------------------------------------------------- */int T1Ungetc(c, f) /* Put back one character */ int c; F_FILE *f; /* Stream descriptor */{ if (c != EOF) { f->ungotc = c; f->flags |= UNGOTTENC; /* set flag */ f->flags &= ~FIOEOF; /* reset EOF */ } return c;} /* 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 bytelen, cnt, i; F_char *p = (F_char *)buffP; int icnt; /* Number of characters to read */ if (f->b_base == NULL) return 0; /* closed */ icnt = (size!=1)?n*size:n; /* Number of bytes we want */ if (f->flags & UNGOTTENC) { /* there is an ungotten c */ f->flags &= ~UNGOTTENC; *(p++) = f->ungotc; icnt--; bytelen = 1; } else bytelen = 0; while (icnt > 0) { /* First use any bytes we have buffered in the stream buffer */ if ((cnt=f->b_cnt) > 0) { if (cnt > icnt) cnt = icnt; for (i=0; i<cnt; i++) *(p++) = *(f->b_ptr++); f->b_cnt -= cnt; icnt -= cnt; bytelen += cnt; } if ((icnt == 0) || (f->flags & FIOEOF)) break; f->b_cnt = T1Fill(f); } return ((size!=1)?bytelen/size:bytelen);} /* end Read */ /* -------------------------------------------------------------- */int T1Close(f) /* Close the file */ 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 */ /* -------------------------------------------------------------- */F_FILE *T1eexec(f) /* Initialization */ F_FILE *f; /* Stream descriptor */{ int i; int H; unsigned char *p; unsigned char randomP[8]; r = 55665; /* initial key */ asc = 1; /* indicate ASCII form */#ifdef DEBUG_DECRYPTION printf("T1eexec(1): first 20 bytes=%.20s, b_cnt=%d\n", f->b_ptr, f->b_cnt);#endif /* 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.) */ /* Skipping over initial white space chars has been removed since it could lead to unprocessable pfb-fonts if accindentally the first cipher text byte was of the class HWHITE_SPACE. Instead, we just read ahead, this should suffice for any Type 1 font program. (RMz, 08/02/1998) */ /* If ASCII, the next 7 chars are guaranteed consecutive */ randomP[0] = getc(f); /* store first non white space char */ fread(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 */ fread(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; #ifdef DEBUG_DECRYPTION printf("T1eexec(2): first 120 bytes=%.120s, b_cnt=%d\n", f->b_ptr, f->b_cnt);#endif return (feof(f))?NULL:f;} /* end eexec */ /* -------------------------------------------------------------- */STATIC int T1Decrypt(p, len) unsigned char *p; int len;{ int n; int H=0, L=0; unsigned char *inp = p; unsigned char *tblP; #ifdef DEBUG_DECRYPTION printf("T1_Decrypt(): called with len=%d\n",len);#endif if (asc) { if (haveextrach) { H = extrach; tblP = LowHexP; } else tblP = HighHexP; for (n=0; len>0; len--) { L = tblP[*inp++];#ifdef DEBUG_DECRYPTION printf("L=0x%X, %d, inp=%c (%d)\n", L,L, *(inp-1), *(inp-1));#endif if (L == HWHITE_SPACE) {#ifdef DEBUG_DECRYPTION printf("continue\n");#endif continue; } if (L > LAST_HDIGIT) {#ifdef DEBUG_DECRYPTION printf("L=0x%X, --> break\n", L);#endif break; } if (tblP == HighHexP) { /* Got first hexit value */ H = L; tblP = LowHexP; } else { /* Got second hexit value; compute value and store it */ n++; tblP = HighHexP; H |= L; /* H is an int, 0 <= H <= 255, so all of this will work */ *p++ = H ^ (r >> 8); r = (H + r) * c1 + c2; } } if (tblP != HighHexP) { /* We had an odd number of hexits */ extrach = H; haveextrach = 1; } else haveextrach = 0;#ifdef DEBUG_DECRYPTION printf("T1_Decrypt(): Decrypted %d bytes\n",n);#endif return n; } else { for (n = len; n>0; n--) { H = *inp++; *p++ = H ^ (r >> 8); r = (H + r) * c1 + c2; } return len; }} /* end Decrypt */ /* -------------------------------------------------------------- *//* This function has been adapted to support pfb-files with multiple data segments */STATIC int T1Fill(f) /* Refill stream buffer */ F_FILE *f; /* Stream descriptor */{ int rc,i; static unsigned char hdr_buf[6]; if (starthex80){ /* we have a pfb-file -> be aware of pfb-blocks */ if ( pfbblocklen-accu >= F_BUFSIZ){ /* fill the buffer */ rc = read(f->fd, f->b_base, F_BUFSIZ); bytecnt+=rc; accu +=rc; } else{ if (pfbblocklen-accu>0){ /* read the remaining of the pfb-block ... */ rc = read(f->fd, f->b_base, pfbblocklen-accu); bytecnt +=rc; accu +=rc; /* ... and examine the next header */ i=read(f->fd, hdr_buf, 6); bytecnt +=i; pfbblocklen=0; pfbblocklen += hdr_buf[2]&0xFF ; pfbblocklen += (hdr_buf[3] & 0xFF) <<8; pfbblocklen += (hdr_buf[4] & 0xFF) <<16; pfbblocklen += (hdr_buf[5] & 0xFF) <<24;#ifdef DEBUG_PFB_BLOCKS printf("t1io: New segment, length=%d, type=%d\n", pfbblocklen, hdr_buf[1]);#endif accu=0; } else{ /* We are at the beginning of a new block -> examine header */ i=read(f->fd, hdr_buf, 6); pfbblocklen=0; pfbblocklen += hdr_buf[2]&0xFF ; pfbblocklen += (hdr_buf[3] & 0xFF) <<8; pfbblocklen += (hdr_buf[4] & 0xFF) <<16; pfbblocklen += (hdr_buf[5] & 0xFF) <<24;#ifdef DEBUG_PFB_BLOCKS printf("t1io: New segment, length=%d, type=%d\n", pfbblocklen, hdr_buf[1]);#endif accu=0; /* header read, now fill the buffer */ if (pfbblocklen-accu >= F_BUFSIZ){ rc = read(f->fd, f->b_base, F_BUFSIZ); accu +=rc; } else{ /* we have the unusual case that the pfb-block size is shorter than F_BUFSIZ -> Read this block only */ rc = read(f->fd, f->b_base, pfbblocklen); accu +=rc; } } } } else{ /* We have a pfa-file -> read straight ahead and fill buffer */ rc = read(f->fd, f->b_base, F_BUFSIZ); } /* propagate any error or eof to current file */ if (rc <= 0) { if (rc == 0) /* means EOF */ f->flags |= FIOEOF; else { f->error = (short)-rc; f->flags |= FIOERROR; rc = 0; } } f->b_ptr = f->b_base;#ifdef DEBUG_DECRYPTION printf("T1_Fill(): read %d bytes\n", rc);#endif if (Decrypt){ rc = T1Decrypt(f->b_base, rc);#ifdef DEBUG_DECRYPTION printf("T1_Fill(): decrypted %d bytes\n", rc);#endif } return rc;} /* end Fill */void T1io_reset(void){ pfbblocklen=0; accu=0; starthex80=0; eexec_startOK=0; eexec_endOK=0; in_eexec=0;}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -