📄 stream.cc
字号:
limited = limitedA; length = lengthA; bufPtr = bufEnd = buf; bufPos = start; savePos = 0; saved = gFalse;}FileStream::~FileStream() { close();}Stream *FileStream::makeSubStream(Guint startA, GBool limitedA, Guint lengthA, Object *dictA) { return new FileStream(f, startA, limitedA, lengthA, dictA);}void FileStream::reset() {#if HAVE_FSEEKO savePos = (Guint)ftello(f); fseeko(f, start, SEEK_SET);#elif HAVE_FSEEK64 savePos = (Guint)ftell64(f); fseek64(f, start, SEEK_SET);#else savePos = (Guint)ftell(f); fseek(f, start, SEEK_SET);#endif saved = gTrue; bufPtr = bufEnd = buf; bufPos = start;}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; 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; } 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 startA, Guint lengthA, Object *dictA): BaseStream(dictA) { buf = bufA; start = startA; length = lengthA; bufEnd = buf + start + length; bufPtr = buf + start; needFree = gFalse;}MemStream::~MemStream() { if (needFree) { gfree(buf); }}Stream *MemStream::makeSubStream(Guint startA, GBool limited, Guint lengthA, Object *dictA) { MemStream *subStr; Guint newLength; if (!limited || startA + lengthA > start + length) { newLength = start + length - startA; } else { newLength = lengthA; } subStr = new MemStream(buf, startA, newLength, dictA); return subStr;}void MemStream::reset() { bufPtr = buf + start;}void MemStream::close() {}void MemStream::setPos(Guint pos, int dir) { Guint i; if (dir >= 0) { i = pos; } else { i = start + length - pos; } if (i < start) { i = start; } else if (i > start + length) { i = start + length; } bufPtr = buf + i;}void MemStream::moveStart(int delta) { start += delta; length -= delta; bufPtr = buf + start;}//------------------------------------------------------------------------// EmbedStream//------------------------------------------------------------------------EmbedStream::EmbedStream(Stream *strA, Object *dictA, GBool limitedA, Guint lengthA): BaseStream(dictA) { str = strA; limited = limitedA; length = lengthA;}EmbedStream::~EmbedStream() {}Stream *EmbedStream::makeSubStream(Guint start, GBool limitedA, Guint lengthA, Object *dictA) { error(-1, "Internal: called makeSubStream() on EmbedStream"); return NULL;}int EmbedStream::getChar() { if (limited && !length) { return EOF; } --length; return str->getChar();}int EmbedStream::lookChar() { if (limited && !length) { return EOF; } return str->lookChar();}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(int psLevel, char *indent) { GString *s; if (psLevel < 2) { return NULL; } if (!(s = str->getPSFilter(psLevel, 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 (Lexer::isSpace(c[0])); 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 (Lexer::isSpace(c[k])); 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(int psLevel, char *indent) { GString *s; if (psLevel < 2) { return NULL; } if (!(s = str->getPSFilter(psLevel, 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); if (!pred->isOk()) { delete pred; pred = NULL; } } 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(int psLevel, char *indent) { GString *s; if (psLevel < 2 || pred) { return NULL; } if (!(s = str->getPSFilter(psLevel, indent))) { return NULL; } s->append(indent)->append("<< "); if (!early) { s->append("/EarlyChange 0 "); } s->append(">> /LZWDecode filter\n"); return s;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -