summaryrefslogtreecommitdiff
path: root/noncore/unsupported/qpdf/xpdf/Stream.cc
Unidiff
Diffstat (limited to 'noncore/unsupported/qpdf/xpdf/Stream.cc') (more/less context) (ignore whitespace changes)
-rw-r--r--noncore/unsupported/qpdf/xpdf/Stream.cc215
1 files changed, 187 insertions, 28 deletions
diff --git a/noncore/unsupported/qpdf/xpdf/Stream.cc b/noncore/unsupported/qpdf/xpdf/Stream.cc
index 18490d4..c558478 100644
--- a/noncore/unsupported/qpdf/xpdf/Stream.cc
+++ b/noncore/unsupported/qpdf/xpdf/Stream.cc
@@ -1,11 +1,11 @@
1//======================================================================== 1//========================================================================
2// 2//
3// Stream.cc 3// Stream.cc
4// 4//
5// Copyright 1996 Derek B. Noonburg 5// Copyright 1996-2002 Glyph & Cog, LLC
6// 6//
7//======================================================================== 7//========================================================================
8 8
9#ifdef __GNUC__ 9#ifdef __GNUC__
10#pragma implementation 10#pragma implementation
11#endif 11#endif
@@ -300,13 +300,13 @@ FilterStream::~FilterStream() {
300} 300}
301 301
302void FilterStream::close() { 302void FilterStream::close() {
303 str->close(); 303 str->close();
304} 304}
305 305
306void FilterStream::setPos(int pos) { 306void FilterStream::setPos(Guint pos, int dir) {
307 error(-1, "Internal: called setPos() on FilterStream"); 307 error(-1, "Internal: called setPos() on FilterStream");
308} 308}
309 309
310//------------------------------------------------------------------------ 310//------------------------------------------------------------------------
311// ImageStream 311// ImageStream
312//------------------------------------------------------------------------ 312//------------------------------------------------------------------------
@@ -551,60 +551,74 @@ GBool StreamPredictor::getNextLine() {
551} 551}
552 552
553//------------------------------------------------------------------------ 553//------------------------------------------------------------------------
554// FileStream 554// FileStream
555//------------------------------------------------------------------------ 555//------------------------------------------------------------------------
556 556
557FileStream::FileStream(FILE *fA, int startA, int lengthA, Object *dictA): 557FileStream::FileStream(FILE *fA, Guint startA, GBool limitedA,
558 Guint lengthA, Object *dictA):
558 BaseStream(dictA) { 559 BaseStream(dictA) {
559 f = fA; 560 f = fA;
560 start = startA; 561 start = startA;
562 limited = limitedA;
561 length = lengthA; 563 length = lengthA;
562 bufPtr = bufEnd = buf; 564 bufPtr = bufEnd = buf;
563 bufPos = start; 565 bufPos = start;
564 savePos = -1; 566 savePos = 0;
567 saved = gFalse;
565} 568}
566 569
567FileStream::~FileStream() { 570FileStream::~FileStream() {
568 close(); 571 close();
569} 572}
570 573
571Stream *FileStream::makeSubStream(int startA, int lengthA, Object *dictA) { 574Stream *FileStream::makeSubStream(Guint startA, GBool limitedA,
572 return new FileStream(f, startA, lengthA, dictA); 575 Guint lengthA, Object *dictA) {
576 return new FileStream(f, startA, limitedA, lengthA, dictA);
573} 577}
574 578
575void FileStream::reset() { 579void FileStream::reset() {
576 savePos = (int)ftell(f); 580#if HAVE_FSEEK64
581 savePos = (Guint)ftell64(f);
582 fseek64(f, start, SEEK_SET);
583#else
584 savePos = (Guint)ftell(f);
577 fseek(f, start, SEEK_SET); 585 fseek(f, start, SEEK_SET);
586#endif
587 saved = gTrue;
578 bufPtr = bufEnd = buf; 588 bufPtr = bufEnd = buf;
579 bufPos = start; 589 bufPos = start;
580#ifndef NO_DECRYPTION 590#ifndef NO_DECRYPTION
581 if (decrypt) 591 if (decrypt)
582 decrypt->reset(); 592 decrypt->reset();
583#endif 593#endif
584} 594}
585 595
586void FileStream::close() { 596void FileStream::close() {
587 if (savePos >= 0) { 597 if (saved) {
598#if HAVE_FSEEK64
599 fseek64(f, savePos, SEEK_SET);
600#else
588 fseek(f, savePos, SEEK_SET); 601 fseek(f, savePos, SEEK_SET);
589 savePos = -1; 602#endif
603 saved = gFalse;
590 } 604 }
591} 605}
592 606
593GBool FileStream::fillBuf() { 607GBool FileStream::fillBuf() {
594 int n; 608 int n;
595#ifndef NO_DECRYPTION 609#ifndef NO_DECRYPTION
596 char *p; 610 char *p;
597#endif 611#endif
598 612
599 bufPos += bufEnd - buf; 613 bufPos += bufEnd - buf;
600 bufPtr = bufEnd = buf; 614 bufPtr = bufEnd = buf;
601 if (length >= 0 && bufPos >= start + length) { 615 if (limited && bufPos >= start + length) {
602 return gFalse; 616 return gFalse;
603 } 617 }
604 if (length >= 0 && bufPos + fileStreamBufSize > start + length) { 618 if (limited && bufPos + fileStreamBufSize > start + length) {
605 n = start + length - bufPos; 619 n = start + length - bufPos;
606 } else { 620 } else {
607 n = fileStreamBufSize; 621 n = fileStreamBufSize;
608 } 622 }
609 n = fread(buf, 1, n, f); 623 n = fread(buf, 1, n, f);
610 bufEnd = buf + n; 624 bufEnd = buf + n;
@@ -618,66 +632,165 @@ GBool FileStream::fillBuf() {
618 } 632 }
619 } 633 }
620#endif 634#endif
621 return gTrue; 635 return gTrue;
622} 636}
623 637
624void FileStream::setPos(int pos) { 638void FileStream::setPos(Guint pos, int dir) {
625 long size; 639 Guint size;
626 640
627 if (pos >= 0) { 641 if (dir >= 0) {
642#if HAVE_FSEEK64
643 fseek64(f, pos, SEEK_SET);
644#else
628 fseek(f, pos, SEEK_SET); 645 fseek(f, pos, SEEK_SET);
646#endif
629 bufPos = pos; 647 bufPos = pos;
630 } else { 648 } else {
649#if HAVE_FSEEK64
650 fseek64(f, 0, SEEK_END);
651 size = (Guint)ftell64(f);
652#else
631 fseek(f, 0, SEEK_END); 653 fseek(f, 0, SEEK_END);
632 size = ftell(f); 654 size = (Guint)ftell(f);
633 if (pos < -size) 655#endif
634 pos = (int)(-size); 656 if (pos > size)
657 pos = (Guint)size;
635#ifdef __CYGWIN32__ 658#ifdef __CYGWIN32__
636 //~ work around a bug in cygwin's implementation of fseek 659 //~ work around a bug in cygwin's implementation of fseek
637 rewind(f); 660 rewind(f);
638#endif 661#endif
639 fseek(f, pos, SEEK_END); 662#if HAVE_FSEEK64
640 bufPos = (int)ftell(f); 663 fseek64(f, -(int)pos, SEEK_END);
664 bufPos = (Guint)ftell64(f);
665#else
666 fseek(f, -(int)pos, SEEK_END);
667 bufPos = (Guint)ftell(f);
668#endif
641 } 669 }
642 bufPtr = bufEnd = buf; 670 bufPtr = bufEnd = buf;
643} 671}
644 672
645void FileStream::moveStart(int delta) { 673void FileStream::moveStart(int delta) {
646 start += delta; 674 start += delta;
647 bufPtr = bufEnd = buf; 675 bufPtr = bufEnd = buf;
648 bufPos = start; 676 bufPos = start;
649} 677}
650 678
651//------------------------------------------------------------------------ 679//------------------------------------------------------------------------
680// MemStream
681//------------------------------------------------------------------------
682
683MemStream::MemStream(char *bufA, Guint lengthA, Object *dictA):
684 BaseStream(dictA) {
685 buf = bufA;
686 needFree = gFalse;
687 length = lengthA;
688 bufEnd = buf + length;
689 bufPtr = buf;
690}
691
692MemStream::~MemStream() {
693 if (needFree) {
694 gfree(buf);
695 }
696}
697
698Stream *MemStream::makeSubStream(Guint start, GBool limited,
699 Guint lengthA, Object *dictA) {
700 Guint newLength;
701
702 if (!limited || start + lengthA > length) {
703 newLength = length - start;
704 } else {
705 newLength = lengthA;
706 }
707 return new MemStream(buf + start, newLength, dictA);
708}
709
710void MemStream::reset() {
711 bufPtr = buf;
712#ifndef NO_DECRYPTION
713 if (decrypt) {
714 decrypt->reset();
715 }
716#endif
717}
718
719void MemStream::close() {
720}
721
722void MemStream::setPos(Guint pos, int dir) {
723 if (dir >= 0) {
724 if (pos > length) {
725 bufPtr = bufEnd;
726 } else {
727 bufPtr = buf + pos;
728 }
729 } else {
730 if (pos > length) {
731 bufPtr = buf;
732 } else {
733 bufPtr = bufEnd - pos;
734 }
735 }
736}
737
738void MemStream::moveStart(int delta) {
739 buf += delta;
740 bufPtr = buf;
741}
742
743#ifndef NO_DECRYPTION
744void MemStream::doDecryption(Guchar *fileKey, int keyLength,
745 int objNum, int objGen) {
746 char *newBuf;
747 char *p, *q;
748
749 this->BaseStream::doDecryption(fileKey, keyLength, objNum, objGen);
750 if (decrypt) {
751 newBuf = (char *)gmalloc(bufEnd - buf);
752 for (p = buf, q = newBuf; p < bufEnd; ++p, ++q) {
753 *q = (char)decrypt->decryptByte((Guchar)*p);
754 }
755 bufEnd = newBuf + (bufEnd - buf);
756 bufPtr = newBuf + (bufPtr - buf);
757 buf = newBuf;
758 needFree = gTrue;
759 }
760}
761#endif
762
763//------------------------------------------------------------------------
652// EmbedStream 764// EmbedStream
653//------------------------------------------------------------------------ 765//------------------------------------------------------------------------
654 766
655EmbedStream::EmbedStream(Stream *strA, Object *dictA): 767EmbedStream::EmbedStream(Stream *strA, Object *dictA):
656 BaseStream(dictA) { 768 BaseStream(dictA) {
657 str = strA; 769 str = strA;
658} 770}
659 771
660EmbedStream::~EmbedStream() { 772EmbedStream::~EmbedStream() {
661} 773}
662 774
663Stream *EmbedStream::makeSubStream(int start, int length, Object *dictA) { 775Stream *EmbedStream::makeSubStream(Guint start, GBool limited,
776 Guint length, Object *dictA) {
664 error(-1, "Internal: called makeSubStream() on EmbedStream"); 777 error(-1, "Internal: called makeSubStream() on EmbedStream");
665 return NULL; 778 return NULL;
666} 779}
667 780
668void EmbedStream::setPos(int pos) { 781void EmbedStream::setPos(Guint pos, int dir) {
669 error(-1, "Internal: called setPos() on EmbedStream"); 782 error(-1, "Internal: called setPos() on EmbedStream");
670} 783}
671 784
672int EmbedStream::getStart() { 785Guint EmbedStream::getStart() {
673 error(-1, "Internal: called getStart() on EmbedStream"); 786 error(-1, "Internal: called getStart() on EmbedStream");
674 return 0; 787 return 0;
675} 788}
676 789
677void EmbedStream::moveStart(int start) { 790void EmbedStream::moveStart(int delta) {
678 error(-1, "Internal: called moveStart() on EmbedStream"); 791 error(-1, "Internal: called moveStart() on EmbedStream");
679} 792}
680 793
681//------------------------------------------------------------------------ 794//------------------------------------------------------------------------
682// ASCIIHexStream 795// ASCIIHexStream
683//------------------------------------------------------------------------ 796//------------------------------------------------------------------------
@@ -956,17 +1069,13 @@ void LZWStream::reset() {
956 error(getPos(), "Couldn't popen '%s'", zCmd->getCString()); 1069 error(getPos(), "Couldn't popen '%s'", zCmd->getCString());
957 unlink(zName->getCString()); 1070 unlink(zName->getCString());
958 delete zName; 1071 delete zName;
959 return; 1072 return;
960 } 1073 }
961#else // HAVE_POPEN 1074#else // HAVE_POPEN
962#ifdef VMS 1075 if (!executeCommand(zCmd->getCString())) {
963 if (!system(zCmd->getCString())) {
964#else
965 if (system(zCmd->getCString())) {
966#endif
967 error(getPos(), "Couldn't execute '%s'", zCmd->getCString()); 1076 error(getPos(), "Couldn't execute '%s'", zCmd->getCString());
968 unlink(zName->getCString()); 1077 unlink(zName->getCString());
969 delete zName; 1078 delete zName;
970 return; 1079 return;
971 } 1080 }
972 zName->del(zName->getLength() - 2, 2); 1081 zName->del(zName->getLength() - 2, 2);
@@ -3284,12 +3393,62 @@ int FixedLengthEncoder::lookChar() {
3284 if (length >= 0 && count >= length) 3393 if (length >= 0 && count >= length)
3285 return EOF; 3394 return EOF;
3286 return str->getChar(); 3395 return str->getChar();
3287} 3396}
3288 3397
3289//------------------------------------------------------------------------ 3398//------------------------------------------------------------------------
3399// ASCIIHexEncoder
3400//------------------------------------------------------------------------
3401
3402ASCIIHexEncoder::ASCIIHexEncoder(Stream *strA):
3403 FilterStream(strA) {
3404 bufPtr = bufEnd = buf;
3405 lineLen = 0;
3406 eof = gFalse;
3407}
3408
3409ASCIIHexEncoder::~ASCIIHexEncoder() {
3410 if (str->isEncoder()) {
3411 delete str;
3412 }
3413}
3414
3415void ASCIIHexEncoder::reset() {
3416 str->reset();
3417 bufPtr = bufEnd = buf;
3418 lineLen = 0;
3419 eof = gFalse;
3420}
3421
3422void ASCIIHexEncoder::close() {
3423}
3424
3425GBool ASCIIHexEncoder::fillBuf() {
3426 static char *hex = "0123456789abcdef";
3427 int c;
3428
3429 if (eof) {
3430 return gFalse;
3431 }
3432 bufPtr = bufEnd = buf;
3433 if ((c = str->getChar()) == EOF) {
3434 *bufEnd++ = '>';
3435 eof = gTrue;
3436 } else {
3437 if (lineLen >= 64) {
3438 *bufEnd++ = '\n';
3439 lineLen = 0;
3440 }
3441 *bufEnd++ = hex[(c >> 4) & 0x0f];
3442 *bufEnd++ = hex[c & 0x0f];
3443 lineLen += 2;
3444 }
3445 return gTrue;
3446}
3447
3448//------------------------------------------------------------------------
3290// ASCII85Encoder 3449// ASCII85Encoder
3291//------------------------------------------------------------------------ 3450//------------------------------------------------------------------------
3292 3451
3293ASCII85Encoder::ASCII85Encoder(Stream *strA): 3452ASCII85Encoder::ASCII85Encoder(Stream *strA):
3294 FilterStream(strA) { 3453 FilterStream(strA) {
3295 bufPtr = bufEnd = buf; 3454 bufPtr = bufEnd = buf;