stream.cc
来自「source code: Covert TXT to PDF」· CC 代码 · 共 2,936 行 · 第 1/5 页
CC
2,936 行
#else savePos = (Guint)ftell(f); fseek(f, start, SEEK_SET);#endif saved = gTrue; bufPtr = bufEnd = buf; bufPos = start;#ifndef NO_DECRYPTION if (decrypt) decrypt->reset();#endif}void FileStream::close() { if (saved) {#if HAVE_FSEEKO fseeko(f, savePos, SEEK_SET);#elif HAVE_FSEEK64 fseek64(f, savePos, SEEK_SET);#else fseek(f, savePos, SEEK_SET);#endif saved = gFalse; }}GBool FileStream::fillBuf() { int n;#ifndef NO_DECRYPTION char *p;#endif bufPos += bufEnd - buf; bufPtr = bufEnd = buf; if (limited && bufPos >= start + length) { return gFalse; } if (limited && bufPos + fileStreamBufSize > start + length) { n = start + length - bufPos; } else { n = fileStreamBufSize; } n = fread(buf, 1, n, f); bufEnd = buf + n; if (bufPtr >= bufEnd) { return gFalse; }#ifndef NO_DECRYPTION if (decrypt) { for (p = buf; p < bufEnd; ++p) { *p = (char)decrypt->decryptByte((Guchar)*p); } }#endif return gTrue;}void FileStream::setPos(Guint pos, int dir) { Guint size; if (dir >= 0) {#if HAVE_FSEEKO fseeko(f, pos, SEEK_SET);#elif HAVE_FSEEK64 fseek64(f, pos, SEEK_SET);#else fseek(f, pos, SEEK_SET);#endif bufPos = pos; } else {#if HAVE_FSEEKO fseeko(f, 0, SEEK_END); size = (Guint)ftello(f);#elif HAVE_FSEEK64 fseek64(f, 0, SEEK_END); size = (Guint)ftell64(f);#else fseek(f, 0, SEEK_END); size = (Guint)ftell(f);#endif if (pos > size) pos = (Guint)size;#ifdef __CYGWIN32__ //~ work around a bug in cygwin's implementation of fseek rewind(f);#endif#if HAVE_FSEEKO fseeko(f, -(int)pos, SEEK_END); bufPos = (Guint)ftello(f);#elif HAVE_FSEEK64 fseek64(f, -(int)pos, SEEK_END); bufPos = (Guint)ftell64(f);#else fseek(f, -(int)pos, SEEK_END); bufPos = (Guint)ftell(f);#endif } bufPtr = bufEnd = buf;}void FileStream::moveStart(int delta) { start += delta; bufPtr = bufEnd = buf; bufPos = start;}//------------------------------------------------------------------------// MemStream//------------------------------------------------------------------------MemStream::MemStream(char *bufA, Guint lengthA, Object *dictA): BaseStream(dictA) { buf = bufA; needFree = gFalse; length = lengthA; bufEnd = buf + length; bufPtr = buf;}MemStream::~MemStream() { if (needFree) { gfree(buf); }}Stream *MemStream::makeSubStream(Guint start, GBool limited, Guint lengthA, Object *dictA) { Guint newLength; if (!limited || start + lengthA > length) { newLength = length - start; } else { newLength = lengthA; } return new MemStream(buf + start, newLength, dictA);}void MemStream::reset() { bufPtr = buf;#ifndef NO_DECRYPTION if (decrypt) { decrypt->reset(); }#endif}void MemStream::close() {}void MemStream::setPos(Guint pos, int dir) { if (dir >= 0) { if (pos > length) { bufPtr = bufEnd; } else { bufPtr = buf + pos; } } else { if (pos > length) { bufPtr = buf; } else { bufPtr = bufEnd - pos; } }}void MemStream::moveStart(int delta) { buf += delta; bufPtr = buf;}#ifndef NO_DECRYPTIONvoid MemStream::doDecryption(Guchar *fileKey, int keyLength, int objNum, int objGen) { char *newBuf; char *p, *q; this->BaseStream::doDecryption(fileKey, keyLength, objNum, objGen); if (decrypt) { newBuf = (char *)gmalloc(bufEnd - buf); for (p = buf, q = newBuf; p < bufEnd; ++p, ++q) { *q = (char)decrypt->decryptByte((Guchar)*p); } bufEnd = newBuf + (bufEnd - buf); bufPtr = newBuf + (bufPtr - buf); buf = newBuf; needFree = gTrue; }}#endif//------------------------------------------------------------------------// EmbedStream//------------------------------------------------------------------------EmbedStream::EmbedStream(Stream *strA, Object *dictA): BaseStream(dictA) { str = strA;}EmbedStream::~EmbedStream() {}Stream *EmbedStream::makeSubStream(Guint start, GBool limited, Guint length, Object *dictA) { error(-1, "Internal: called makeSubStream() on EmbedStream"); return NULL;}void EmbedStream::setPos(Guint pos, int dir) { error(-1, "Internal: called setPos() on EmbedStream");}Guint EmbedStream::getStart() { error(-1, "Internal: called getStart() on EmbedStream"); return 0;}void EmbedStream::moveStart(int delta) { error(-1, "Internal: called moveStart() on EmbedStream");}//------------------------------------------------------------------------// ASCIIHexStream//------------------------------------------------------------------------ASCIIHexStream::ASCIIHexStream(Stream *strA): FilterStream(strA) { buf = EOF; eof = gFalse;}ASCIIHexStream::~ASCIIHexStream() { delete str;}void ASCIIHexStream::reset() { str->reset(); buf = EOF; eof = gFalse;}int ASCIIHexStream::lookChar() { int c1, c2, x; if (buf != EOF) return buf; if (eof) { buf = EOF; return EOF; } do { c1 = str->getChar(); } while (isspace(c1)); if (c1 == '>') { eof = gTrue; buf = EOF; return buf; } do { c2 = str->getChar(); } while (isspace(c2)); if (c2 == '>') { eof = gTrue; c2 = '0'; } if (c1 >= '0' && c1 <= '9') { x = (c1 - '0') << 4; } else if (c1 >= 'A' && c1 <= 'F') { x = (c1 - 'A' + 10) << 4; } else if (c1 >= 'a' && c1 <= 'f') { x = (c1 - 'a' + 10) << 4; } else if (c1 == EOF) { eof = gTrue; x = 0; } else { error(getPos(), "Illegal character <%02x> in ASCIIHex stream", c1); x = 0; } if (c2 >= '0' && c2 <= '9') { x += c2 - '0'; } else if (c2 >= 'A' && c2 <= 'F') { x += c2 - 'A' + 10; } else if (c2 >= 'a' && c2 <= 'f') { x += c2 - 'a' + 10; } else if (c2 == EOF) { eof = gTrue; x = 0; } else { error(getPos(), "Illegal character <%02x> in ASCIIHex stream", c2); } buf = x & 0xff; return buf;}GString *ASCIIHexStream::getPSFilter(char *indent) { GString *s; if (!(s = str->getPSFilter(indent))) { return NULL; } s->append(indent)->append("/ASCIIHexDecode filter\n"); return s;}GBool ASCIIHexStream::isBinary(GBool last) { return str->isBinary(gFalse);}//------------------------------------------------------------------------// ASCII85Stream//------------------------------------------------------------------------ASCII85Stream::ASCII85Stream(Stream *strA): FilterStream(strA) { index = n = 0; eof = gFalse;}ASCII85Stream::~ASCII85Stream() { delete str;}void ASCII85Stream::reset() { str->reset(); index = n = 0; eof = gFalse;}int ASCII85Stream::lookChar() { int k; Gulong t; if (index >= n) { if (eof) return EOF; index = 0; do { c[0] = str->getChar(); } while (c[0] == '\n' || c[0] == '\r'); if (c[0] == '~' || c[0] == EOF) { eof = gTrue; n = 0; return EOF; } else if (c[0] == 'z') { b[0] = b[1] = b[2] = b[3] = 0; n = 4; } else { for (k = 1; k < 5; ++k) { do { c[k] = str->getChar(); } while (c[k] == '\n' || c[k] == '\r'); if (c[k] == '~' || c[k] == EOF) break; } n = k - 1; if (k < 5 && (c[k] == '~' || c[k] == EOF)) { for (++k; k < 5; ++k) c[k] = 0x21 + 84; eof = gTrue; } t = 0; for (k = 0; k < 5; ++k) t = t * 85 + (c[k] - 0x21); for (k = 3; k >= 0; --k) { b[k] = (int)(t & 0xff); t >>= 8; } } } return b[index];}GString *ASCII85Stream::getPSFilter(char *indent) { GString *s; if (!(s = str->getPSFilter(indent))) { return NULL; } s->append(indent)->append("/ASCII85Decode filter\n"); return s;}GBool ASCII85Stream::isBinary(GBool last) { return str->isBinary(gFalse);}//------------------------------------------------------------------------// LZWStream//------------------------------------------------------------------------LZWStream::LZWStream(Stream *strA, int predictor, int columns, int colors, int bits, int earlyA): FilterStream(strA) { if (predictor != 1) { pred = new StreamPredictor(this, predictor, columns, colors, bits); } else { pred = NULL; } early = earlyA; eof = gFalse; inputBits = 0; clearTable();}LZWStream::~LZWStream() { if (pred) { delete pred; } delete str;}int LZWStream::getChar() { if (pred) { return pred->getChar(); } if (eof) { return EOF; } if (seqIndex >= seqLength) { if (!processNextCode()) { return EOF; } } return seqBuf[seqIndex++];}int LZWStream::lookChar() { if (pred) { return pred->lookChar(); } if (eof) { return EOF; } if (seqIndex >= seqLength) { if (!processNextCode()) { return EOF; } } return seqBuf[seqIndex];}int LZWStream::getRawChar() { if (eof) { return EOF; } if (seqIndex >= seqLength) { if (!processNextCode()) { return EOF; } } return seqBuf[seqIndex++];}void LZWStream::reset() { str->reset(); eof = gFalse; inputBits = 0; clearTable();}GBool LZWStream::processNextCode() { int code; int nextLength; int i, j; // check for EOF if (eof) { return gFalse; } // check for eod and clear-table codes start: code = getCode(); if (code == EOF || code == 257) { eof = gTrue; return gFalse; } if (code == 256) { clearTable(); goto start; } if (nextCode >= 4097) { error(getPos(), "Bad LZW stream - expected clear-table code"); clearTable(); } // process the next code nextLength = seqLength + 1; if (code < 256) { seqBuf[0] = code; seqLength = 1; } else if (code < nextCode) { seqLength = table[code].length; for (i = seqLength - 1, j = code; i > 0; --i) { seqBuf[i] = table[j].tail; j = table[j].head; } seqBuf[0] = j; } else if (code == nextCode) { seqBuf[seqLength] = newChar; ++seqLength; } else { error(getPos(), "Bad LZW stream - unexpected code"); eof = gTrue; return gFalse; } newChar = seqBuf[0]; if (first) { first = gFalse; } else { table[nextCode].length = nextLength; table[nextCode].head = prevCode; table[nextCode].tail = newChar; ++nextCode; if (nextCode + early == 512) nextBits = 10; else if (nextCode + early == 1024) nextBits = 11; else if (nextCode + early == 2048) nextBits = 12; } prevCode = code; // reset buffer seqIndex = 0; return gTrue;}void LZWStream::clearTable() { nextCode = 258; nextBits = 9; seqIndex = seqLength = 0; first = gTrue;}int LZWStream::getCode() { int c; int code; while (inputBits < nextBits) { if ((c = str->getChar()) == EOF) return EOF; inputBuf = (inputBuf << 8) | (c & 0xff); inputBits += 8; } code = (inputBuf >> (inputBits - nextBits)) & ((1 << nextBits) - 1); inputBits -= nextBits; return code;}GString *LZWStream::getPSFilter(char *indent) { GString *s; if (pred) { return NULL; } if (!(s = str->getPSFilter(indent))) { return NULL; } s->append(indent)->append("/LZWDecode filter\n"); return s;}GBool LZWStream::isBinary(GBool last) { return str->isBinary(gTrue);}//------------------------------------------------------------------------// RunLengthStream//------------------------------------------------------------------------RunLengthStream::RunLengthStream(Stream *strA): FilterStream(strA) { bufPtr = bufEnd = buf; eof = gFalse;}RunLengthStream::~RunLengthStream() { delete str;}void RunLengthStream::reset() { str->reset(); bufPtr = bufEnd = buf; eof = gFalse;}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?