Back to index

tetex-bin  3.0
Public Member Functions | Protected Attributes | Private Member Functions | Private Attributes | Static Private Attributes
FlateStream Class Reference

#include <Stream.h>

Inheritance diagram for FlateStream:
Inheritance graph
[legend]
Collaboration diagram for FlateStream:
Collaboration graph
[legend]

List of all members.

Public Member Functions

 FlateStream (Stream *strA, int predictor, int columns, int colors, int bits)
virtual ~FlateStream ()
virtual StreamKind getKind ()
virtual void reset ()
virtual int getChar ()
virtual int lookChar ()
virtual int getRawChar ()
virtual GStringgetPSFilter (int psLevel, char *indent)
virtual GBool isBinary (GBool last=gTrue)
virtual void close ()
virtual int getPos ()
virtual void setPos (Guint pos, int dir=0)
virtual BaseStreamgetBaseStream ()
virtual DictgetDict ()
virtual void ignoreLength ()
int incRef ()
int decRef ()
virtual char * getLine (char *buf, int size)
virtual GBool isEncoder ()
StreamaddFilters (Object *dict)

Protected Attributes

Streamstr

Private Member Functions

void readSome ()
GBool startBlock ()
void loadFixedCodes ()
GBool readDynamicCodes ()
void compHuffmanCodes (int *lengths, int n, FlateHuffmanTab *tab)
int getHuffmanCodeWord (FlateHuffmanTab *tab)
int getCodeWord (int bits)

Private Attributes

StreamPredictorpred
Guchar buf [flateWindow]
int index
int remain
int codeBuf
int codeSize
int codeLengths [flateMaxLitCodes+flateMaxDistCodes]
FlateHuffmanTab litCodeTab
FlateHuffmanTab distCodeTab
GBool compressedBlock
int blockLen
GBool endOfBlock
GBool eof

Static Private Attributes

static int codeLenCodeMap [flateMaxCodeLenCodes]
static FlateDecode lengthDecode [flateMaxLitCodes-257]
static FlateDecode distDecode [flateMaxDistCodes]

Detailed Description

Definition at line 666 of file Stream.h.


Constructor & Destructor Documentation

FlateStream::FlateStream ( Stream strA,
int  predictor,
int  columns,
int  colors,
int  bits 
)

Definition at line 3253 of file Stream.cc.

                                           :
    FilterStream(strA) {
  if (predictor != 1) {
    pred = new StreamPredictor(this, predictor, columns, colors, bits);
  } else {
    pred = NULL;
  }
  litCodeTab.codes = NULL;
  distCodeTab.codes = NULL;
}

Definition at line 3265 of file Stream.cc.

                          {
  gfree(litCodeTab.codes);
  gfree(distCodeTab.codes);
  if (pred) {
    delete pred;
  }
  delete str;
}

Here is the call graph for this function:


Member Function Documentation

Stream * Stream::addFilters ( Object dict) [inherited]

Definition at line 92 of file Stream.cc.

                                       {
  Object obj, obj2;
  Object params, params2;
  Stream *str;
  int i;

  str = this;
  dict->dictLookup("Filter", &obj);
  if (obj.isNull()) {
    obj.free();
    dict->dictLookup("F", &obj);
  }
  dict->dictLookup("DecodeParms", &params);
  if (params.isNull()) {
    params.free();
    dict->dictLookup("DP", &params);
  }
  if (obj.isName()) {
    str = makeFilter(obj.getName(), str, &params);
  } else if (obj.isArray()) {
    for (i = 0; i < obj.arrayGetLength(); ++i) {
      obj.arrayGet(i, &obj2);
      if (params.isArray())
       params.arrayGet(i, &params2);
      else
       params2.initNull();
      if (obj2.isName()) {
       str = makeFilter(obj2.getName(), str, &params2);
      } else {
       error(getPos(), "Bad filter name");
       str = new EOFStream(str);
      }
      obj2.free();
      params2.free();
    }
  } else if (!obj.isNull()) {
    error(getPos(), "Bad 'Filter' attribute in stream");
  }
  obj.free();
  params.free();

  return str;
}

Here is the call graph for this function:

Here is the caller graph for this function:

void FilterStream::close ( ) [virtual, inherited]

Reimplemented from Stream.

Definition at line 306 of file Stream.cc.

                         {
  str->close();
}

Here is the call graph for this function:

void FlateStream::compHuffmanCodes ( int lengths,
int  n,
FlateHuffmanTab tab 
) [private]

Definition at line 3628 of file Stream.cc.

                                                                            {
  int tabSize, len, code, code2, skip, val, i, t;

  // find max code length
  tab->maxLen = 0;
  for (val = 0; val < n; ++val) {
    if (lengths[val] > tab->maxLen) {
      tab->maxLen = lengths[val];
    }
  }

  // allocate the table
  tabSize = 1 << tab->maxLen;
  tab->codes = (FlateCode *)gmalloc(tabSize * sizeof(FlateCode));

  // clear the table
  for (i = 0; i < tabSize; ++i) {
    tab->codes[i].len = 0;
    tab->codes[i].val = 0;
  }

  // build the table
  for (len = 1, code = 0, skip = 2;
       len <= tab->maxLen;
       ++len, code <<= 1, skip <<= 1) {
    for (val = 0; val < n; ++val) {
      if (lengths[val] == len) {

       // bit-reverse the code
       code2 = 0;
       t = code;
       for (i = 0; i < len; ++i) {
         code2 = (code2 << 1) | (t & 1);
         t >>= 1;
       }

       // fill in the table entries
       for (i = code2; i < tabSize; i += skip) {
         tab->codes[i].len = (Gushort)len;
         tab->codes[i].val = (Gushort)val;
       }

       ++code;
      }
    }
  }
}

Here is the call graph for this function:

Here is the caller graph for this function:

int Stream::decRef ( ) [inline, inherited]

Definition at line 58 of file Stream.h.

{ return --ref; }
virtual BaseStream* FilterStream::getBaseStream ( ) [inline, virtual, inherited]

Implements Stream.

Definition at line 174 of file Stream.h.

{ return str->getBaseStream(); }

Here is the call graph for this function:

int FlateStream::getChar ( ) [virtual]

Implements Stream.

Definition at line 3310 of file Stream.cc.

                         {
  int c;

  if (pred) {
    return pred->getChar();
  }
  while (remain == 0) {
    if (endOfBlock && eof)
      return EOF;
    readSome();
  }
  c = buf[index];
  index = (index + 1) & flateMask;
  --remain;
  return c;
}

Here is the call graph for this function:

int FlateStream::getCodeWord ( int  bits) [private]

Definition at line 3696 of file Stream.cc.

                                     {
  int c;

  while (codeSize < bits) {
    if ((c = str->getChar()) == EOF)
      return EOF;
    codeBuf |= (c & 0xff) << codeSize;
    codeSize += 8;
  }
  c = codeBuf & ((1 << bits) - 1);
  codeBuf >>= bits;
  codeSize -= bits;
  return c;
}

Here is the call graph for this function:

Here is the caller graph for this function:

virtual Dict* FilterStream::getDict ( ) [inline, virtual, inherited]

Implements Stream.

Definition at line 175 of file Stream.h.

{ return str->getDict(); }

Here is the call graph for this function:

Definition at line 3676 of file Stream.cc.

                                                        {
  FlateCode *code;
  int c;

  while (codeSize < tab->maxLen) {
    if ((c = str->getChar()) == EOF) {
      break;
    }
    codeBuf |= (c & 0xff) << codeSize;
    codeSize += 8;
  }
  code = &tab->codes[codeBuf & ((1 << tab->maxLen) - 1)];
  if (codeSize == 0 || codeSize < code->len || code->len == 0) {
    return EOF;
  }
  codeBuf >>= code->len;
  codeSize -= code->len;
  return (int)code->val;
}

Here is the call graph for this function:

Here is the caller graph for this function:

virtual StreamKind FlateStream::getKind ( ) [inline, virtual]

Implements Stream.

Definition at line 672 of file Stream.h.

{ return strFlate; }
char * Stream::getLine ( char *  buf,
int  size 
) [virtual, inherited]

Definition at line 67 of file Stream.cc.

                                         {
  int i;
  int c;

  if (lookChar() == EOF)
    return NULL;
  for (i = 0; i < size - 1; ++i) {
    c = getChar();
    if (c == EOF || c == '\n')
      break;
    if (c == '\r') {
      if ((c = lookChar()) == '\n')
       getChar();
      break;
    }
    buf[i] = c;
  }
  buf[i] = '\0';
  return buf;
}

Here is the call graph for this function:

Here is the caller graph for this function:

virtual int FilterStream::getPos ( ) [inline, virtual, inherited]

Implements Stream.

Definition at line 172 of file Stream.h.

{ return str->getPos(); }

Here is the call graph for this function:

GString * FlateStream::getPSFilter ( int  psLevel,
char *  indent 
) [virtual]

Reimplemented from Stream.

Definition at line 3356 of file Stream.cc.

                                                           {
  GString *s;

  if (psLevel < 3 || pred) {
    return NULL;
  }
  if (!(s = str->getPSFilter(psLevel, indent))) {
    return NULL;
  }
  s->append(indent)->append("<< >> /FlateDecode filter\n");
  return s;
}

Here is the call graph for this function:

Reimplemented from Stream.

Definition at line 3342 of file Stream.cc.

                            {
  int c;

  while (remain == 0) {
    if (endOfBlock && eof)
      return EOF;
    readSome();
  }
  c = buf[index];
  index = (index + 1) & flateMask;
  --remain;
  return c;
}

Here is the call graph for this function:

virtual void FilterStream::ignoreLength ( ) [inline, virtual, inherited]

Reimplemented from Stream.

Definition at line 176 of file Stream.h.

{ str->ignoreLength(); }

Here is the call graph for this function:

int Stream::incRef ( ) [inline, inherited]

Definition at line 57 of file Stream.h.

{ return ++ref; }
GBool FlateStream::isBinary ( GBool  last = gTrue) [virtual]

Implements Stream.

Definition at line 3369 of file Stream.cc.

                                      {
  return str->isBinary(gTrue);
}

Here is the call graph for this function:

virtual GBool Stream::isEncoder ( ) [inline, virtual, inherited]

Reimplemented in RunLengthEncoder, ASCII85Encoder, ASCIIHexEncoder, and FixedLengthEncoder.

Definition at line 103 of file Stream.h.

{ return gFalse; }

Here is the caller graph for this function:

Definition at line 3501 of file Stream.cc.

                                 {
  int i;

  // build the literal code table
  for (i = 0; i <= 143; ++i) {
    codeLengths[i] = 8;
  }
  for (i = 144; i <= 255; ++i) {
    codeLengths[i] = 9;
  }
  for (i = 256; i <= 279; ++i) {
    codeLengths[i] = 7;
  }
  for (i = 280; i <= 287; ++i) {
    codeLengths[i] = 8;
  }
  compHuffmanCodes(codeLengths, flateMaxLitCodes, &litCodeTab);

  // build the distance code table
  for (i = 0; i < flateMaxDistCodes; ++i) {
    codeLengths[i] = 5;
  }
  compHuffmanCodes(codeLengths, flateMaxDistCodes, &distCodeTab);
}

Here is the call graph for this function:

Here is the caller graph for this function:

int FlateStream::lookChar ( ) [virtual]

Implements Stream.

Definition at line 3327 of file Stream.cc.

                          {
  int c;

  if (pred) {
    return pred->lookChar();
  }
  while (remain == 0) {
    if (endOfBlock && eof)
      return EOF;
    readSome();
  }
  c = buf[index];
  return c;
}

Here is the call graph for this function:

Definition at line 3526 of file Stream.cc.

                                    {
  int numCodeLenCodes;
  int numLitCodes;
  int numDistCodes;
  int codeLenCodeLengths[flateMaxCodeLenCodes];
  FlateHuffmanTab codeLenCodeTab;
  int len, repeat, code;
  int i;

  codeLenCodeTab.codes = NULL;

  // read lengths
  if ((numLitCodes = getCodeWord(5)) == EOF) {
    goto err;
  }
  numLitCodes += 257;
  if ((numDistCodes = getCodeWord(5)) == EOF) {
    goto err;
  }
  numDistCodes += 1;
  if ((numCodeLenCodes = getCodeWord(4)) == EOF) {
    goto err;
  }
  numCodeLenCodes += 4;
  if (numLitCodes > flateMaxLitCodes ||
      numDistCodes > flateMaxDistCodes ||
      numCodeLenCodes > flateMaxCodeLenCodes) {
    goto err;
  }

  // build the code length code table
  for (i = 0; i < flateMaxCodeLenCodes; ++i) {
    codeLenCodeLengths[i] = 0;
  }
  for (i = 0; i < numCodeLenCodes; ++i) {
    if ((codeLenCodeLengths[codeLenCodeMap[i]] = getCodeWord(3)) == -1) {
      goto err;
    }
  }
  compHuffmanCodes(codeLenCodeLengths, flateMaxCodeLenCodes, &codeLenCodeTab);

  // build the literal and distance code tables
  len = 0;
  repeat = 0;
  i = 0;
  while (i < numLitCodes + numDistCodes) {
    if ((code = getHuffmanCodeWord(&codeLenCodeTab)) == EOF) {
      goto err;
    }
    if (code == 16) {
      if ((repeat = getCodeWord(2)) == EOF) {
       goto err;
      }
      repeat += 3;
      if (i + repeat > numLitCodes + numDistCodes) {
       goto err;
      }
      for (; repeat > 0; --repeat) {
       codeLengths[i++] = len;
      }
    } else if (code == 17) {
      if ((repeat = getCodeWord(3)) == EOF) {
       goto err;
      }
      repeat += 3;
      if (i + repeat > numLitCodes + numDistCodes) {
       goto err;
      }
      len = 0;
      for (; repeat > 0; --repeat) {
       codeLengths[i++] = 0;
      }
    } else if (code == 18) {
      if ((repeat = getCodeWord(7)) == EOF) {
       goto err;
      }
      repeat += 11;
      if (i + repeat > numLitCodes + numDistCodes) {
       goto err;
      }
      len = 0;
      for (; repeat > 0; --repeat) {
       codeLengths[i++] = 0;
      }
    } else {
      codeLengths[i++] = len = code;
    }
  }
  compHuffmanCodes(codeLengths, numLitCodes, &litCodeTab);
  compHuffmanCodes(codeLengths + numLitCodes, numDistCodes, &distCodeTab);

  gfree(codeLenCodeTab.codes);
  return gTrue;

err:
  error(getPos(), "Bad dynamic code table in flate stream");
  gfree(codeLenCodeTab.codes);
  return gFalse;
}

Here is the call graph for this function:

Here is the caller graph for this function:

void FlateStream::readSome ( ) [private]

Definition at line 3373 of file Stream.cc.

                           {
  int code1, code2;
  int len, dist;
  int i, j, k;
  int c;

  if (endOfBlock) {
    if (!startBlock())
      return;
  }

  if (compressedBlock) {
    if ((code1 = getHuffmanCodeWord(&litCodeTab)) == EOF)
      goto err;
    if (code1 < 256) {
      buf[index] = code1;
      remain = 1;
    } else if (code1 == 256) {
      endOfBlock = gTrue;
      remain = 0;
    } else {
      code1 -= 257;
      code2 = lengthDecode[code1].bits;
      if (code2 > 0 && (code2 = getCodeWord(code2)) == EOF)
       goto err;
      len = lengthDecode[code1].first + code2;
      if ((code1 = getHuffmanCodeWord(&distCodeTab)) == EOF)
       goto err;
      code2 = distDecode[code1].bits;
      if (code2 > 0 && (code2 = getCodeWord(code2)) == EOF)
       goto err;
      dist = distDecode[code1].first + code2;
      i = index;
      j = (index - dist) & flateMask;
      for (k = 0; k < len; ++k) {
       buf[i] = buf[j];
       i = (i + 1) & flateMask;
       j = (j + 1) & flateMask;
      }
      remain = len;
    }

  } else {
    len = (blockLen < flateWindow) ? blockLen : flateWindow;
    for (i = 0, j = index; i < len; ++i, j = (j + 1) & flateMask) {
      if ((c = str->getChar()) == EOF) {
       endOfBlock = eof = gTrue;
       break;
      }
      buf[j] = c & 0xff;
    }
    remain = i;
    blockLen -= len;
    if (blockLen == 0)
      endOfBlock = gTrue;
  }

  return;

err:
  error(getPos(), "Unexpected end of file in flate stream");
  endOfBlock = eof = gTrue;
  remain = 0;
}

Here is the call graph for this function:

Here is the caller graph for this function:

void FlateStream::reset ( ) [virtual]

Implements Stream.

Definition at line 3274 of file Stream.cc.

                        {
  int cmf, flg;

  index = 0;
  remain = 0;
  codeBuf = 0;
  codeSize = 0;
  compressedBlock = gFalse;
  endOfBlock = gTrue;
  eof = gTrue;

  str->reset();

  // read header
  //~ need to look at window size?
  endOfBlock = eof = gTrue;
  cmf = str->getChar();
  flg = str->getChar();
  if (cmf == EOF || flg == EOF)
    return;
  if ((cmf & 0x0f) != 0x08) {
    error(getPos(), "Unknown compression method in flate stream");
    return;
  }
  if ((((cmf << 8) + flg) % 31) != 0) {
    error(getPos(), "Bad FCHECK in flate stream");
    return;
  }
  if (flg & 0x20) {
    error(getPos(), "FDICT bit set in flate stream");
    return;
  }

  eof = gFalse;
}

Here is the call graph for this function:

void FilterStream::setPos ( Guint  pos,
int  dir = 0 
) [virtual, inherited]

Implements Stream.

Definition at line 310 of file Stream.cc.

                                            {
  error(-1, "Internal: called setPos() on FilterStream");
}

Here is the call graph for this function:

Definition at line 3438 of file Stream.cc.

                              {
  int blockHdr;
  int c;
  int check;

  // free the code tables from the previous block
  gfree(litCodeTab.codes);
  litCodeTab.codes = NULL;
  gfree(distCodeTab.codes);
  distCodeTab.codes = NULL;

  // read block header
  blockHdr = getCodeWord(3);
  if (blockHdr & 1)
    eof = gTrue;
  blockHdr >>= 1;

  // uncompressed block
  if (blockHdr == 0) {
    compressedBlock = gFalse;
    if ((c = str->getChar()) == EOF)
      goto err;
    blockLen = c & 0xff;
    if ((c = str->getChar()) == EOF)
      goto err;
    blockLen |= (c & 0xff) << 8;
    if ((c = str->getChar()) == EOF)
      goto err;
    check = c & 0xff;
    if ((c = str->getChar()) == EOF)
      goto err;
    check |= (c & 0xff) << 8;
    if (check != (~blockLen & 0xffff))
      error(getPos(), "Bad uncompressed block length in flate stream");
    codeBuf = 0;
    codeSize = 0;

  // compressed block with fixed codes
  } else if (blockHdr == 1) {
    compressedBlock = gTrue;
    loadFixedCodes();

  // compressed block with dynamic codes
  } else if (blockHdr == 2) {
    compressedBlock = gTrue;
    if (!readDynamicCodes()) {
      goto err;
    }

  // unknown block type
  } else {
    goto err;
  }

  endOfBlock = gFalse;
  return gTrue;

err:
  error(getPos(), "Bad block header in flate stream");
  endOfBlock = eof = gTrue;
  return gFalse;
}

Here is the call graph for this function:

Here is the caller graph for this function:


Member Data Documentation

Definition at line 693 of file Stream.h.

Definition at line 683 of file Stream.h.

Definition at line 686 of file Stream.h.

int FlateStream::codeLenCodeMap [static, private]
Initial value:
 {
  16, 17, 18, 0, 8, 7, 9, 6, 10, 5, 11, 4, 12, 3, 13, 2, 14, 1, 15
}

Definition at line 698 of file Stream.h.

Definition at line 689 of file Stream.h.

Definition at line 687 of file Stream.h.

Definition at line 692 of file Stream.h.

Definition at line 691 of file Stream.h.

Definition at line 702 of file Stream.h.

Definition at line 694 of file Stream.h.

Definition at line 695 of file Stream.h.

Definition at line 684 of file Stream.h.

Definition at line 700 of file Stream.h.

Definition at line 690 of file Stream.h.

Definition at line 682 of file Stream.h.

Definition at line 685 of file Stream.h.

Stream* FilterStream::str [protected, inherited]

Definition at line 180 of file Stream.h.


The documentation for this class was generated from the following files: