Leaked source code of windows server 2003
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

129 lines
3.9 KiB

  1. /*++
  2. Copyright (c) 1997-1999 Microsoft Corporation
  3. --*/
  4. #include <windows.h>
  5. #define TIFF_MIN_RUN 4 /* Minimum repeats before use RLE */
  6. #define TIFF_MAX_RUN 128 /* Maximum repeats */
  7. #define TIFF_MAX_LITERAL 128 /* Maximum consecutive literal data */
  8. int
  9. iCompTIFF( pbOBuf, cbO, pbIBuf, cb )
  10. BYTE *pbOBuf; /* Output buffer, PRESUMED BIG ENOUGH: see above */
  11. int cbO; /* Output buffer size */
  12. BYTE *pbIBuf; /* Raster data to send */
  13. int cb; /* Number of bytes in the above */
  14. {
  15. BYTE *pbOut; /* Output byte location */
  16. BYTE *pbStart; /* Start of current input stream */
  17. BYTE *pb; /* Miscellaneous usage */
  18. BYTE *pbEnd; /* The last byte of input */
  19. BYTE jLast; /* Last byte, for match purposes */
  20. BYTE *pbOEnd; /* The last byte of Output buffer */
  21. int cSize; /* Bytes in the current length */
  22. int cSend; /* Number to send in this command */
  23. pbOut = pbOBuf;
  24. pbStart = pbIBuf;
  25. pbEnd = pbIBuf + cb; /* The last byte */
  26. pbOEnd = pbOBuf + cbO; /* The last byte of Output buffer */
  27. jLast = *pbIBuf++;
  28. while( pbIBuf < pbEnd )
  29. {
  30. if( jLast == *pbIBuf )
  31. {
  32. /* Find out how long this run is. Then decide on using it */
  33. for( pb = pbIBuf; pb < pbEnd && *pb == jLast; ++pb )
  34. ;
  35. /*
  36. * Note that pbIBuf points at the SECOND byte of the pattern!
  37. * AND also that pb points at the first byte AFTER the run.
  38. */
  39. if( (pb - pbIBuf) >= (TIFF_MIN_RUN - 1) )
  40. {
  41. /*
  42. * Worth recording as a run, so first set the literal
  43. * data which may have already been scanned before recording
  44. * this run.
  45. */
  46. if( (cSize = (int)(pbIBuf - pbStart - 1)) > 0 )
  47. {
  48. /* There is literal data, so record it now */
  49. while( (cSend = min( cSize, TIFF_MAX_LITERAL )) > 0 )
  50. {
  51. // Buffer over run check
  52. if ( (pbOut+cSend)<=pbOEnd ) {
  53. *pbOut++ = cSend - 1;
  54. CopyMemory( pbOut, pbStart, cSend );
  55. pbOut += cSend;
  56. pbStart += cSend;
  57. cSize -= cSend;
  58. } else {
  59. return 0;
  60. }
  61. }
  62. }
  63. /*
  64. * Now for the repeat pattern. Same logic, but only
  65. * one byte is needed per entry.
  66. */
  67. cSize = (int)(pb - pbIBuf + 1);
  68. while( (cSend = min( cSize, TIFF_MAX_RUN )) > 0 )
  69. {
  70. // Buffer over run check
  71. if ( (pbOut+2)<=pbOEnd ) {
  72. *pbOut++ = 1 - cSend; /* -ve indicates repeat */
  73. *pbOut++ = jLast;
  74. cSize -= cSend;
  75. } else {
  76. return 0;
  77. }
  78. }
  79. pbStart = pb; /* Ready for the next one! */
  80. }
  81. pbIBuf = pb; /* Start from this position! */
  82. }
  83. else
  84. jLast = *pbIBuf++; /* Onto the next byte */
  85. }
  86. if( pbStart < pbIBuf )
  87. {
  88. /* Left some dangling. This can only be literal data. */
  89. cSize = (int)(pbIBuf - pbStart);
  90. while( (cSend = min( cSize, TIFF_MAX_LITERAL )) > 0 )
  91. {
  92. // Buffer over run check
  93. if ( (pbOut+cSend)<=pbOEnd ) {
  94. *pbOut++ = cSend - 1;
  95. CopyMemory( pbOut, pbStart, cSend );
  96. pbOut += cSend;
  97. pbStart += cSend;
  98. cSize -= cSend;
  99. } else {
  100. return 0;
  101. }
  102. }
  103. }
  104. return (int)(pbOut - pbOBuf);
  105. }