Source code of Windows XP (NT5)
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.

149 lines
3.3 KiB

  1. #include <mytypes.h>
  2. #include <misclib.h>
  3. #include <dos.h>
  4. #include <string.h>
  5. #define BIT16
  6. #include "mrcicode.h"
  7. unsigned
  8. CompressData(
  9. IN CompressionType Type,
  10. IN FPBYTE Data,
  11. IN unsigned DataSize,
  12. OUT FPBYTE CompressedData,
  13. IN unsigned BufferSize
  14. )
  15. {
  16. unsigned u;
  17. switch(Type) {
  18. case CompressNone:
  19. default:
  20. //
  21. // Force caller to do something intelligent, such as
  22. // writing directly out of the uncompressed buffer.
  23. // This avoids an extra memory move.
  24. //
  25. u = (unsigned)(-1);
  26. break;
  27. case CompressMrci1:
  28. u = Mrci1MaxCompress(Data,DataSize,CompressedData,BufferSize);
  29. break;
  30. case CompressMrci2:
  31. u = Mrci2MaxCompress(Data,DataSize,CompressedData,BufferSize);
  32. break;
  33. }
  34. return(u);
  35. }
  36. unsigned
  37. DecompressData(
  38. IN CompressionType Type,
  39. IN FPBYTE CompressedData,
  40. IN unsigned CompressedDataSize,
  41. OUT FPBYTE DecompressedData,
  42. IN unsigned BufferSize
  43. )
  44. {
  45. unsigned u;
  46. switch(Type) {
  47. case CompressNone:
  48. if(BufferSize >= CompressedDataSize) {
  49. memmove(DecompressedData,CompressedData,CompressedDataSize);
  50. u = CompressedDataSize;
  51. } else {
  52. u = (unsigned)(-1);
  53. }
  54. break;
  55. case CompressMrci1:
  56. u = Mrci1Decompress(CompressedData,CompressedDataSize,DecompressedData,BufferSize);
  57. break;
  58. case CompressMrci2:
  59. u = Mrci2Decompress(CompressedData,CompressedDataSize,DecompressedData,BufferSize);
  60. break;
  61. default:
  62. u = (unsigned)(-1);
  63. break;
  64. }
  65. return(u);
  66. }
  67. BOOL
  68. CompressAndSave(
  69. IN CompressionType Type,
  70. IN FPBYTE Data,
  71. IN unsigned DataSize,
  72. OUT FPBYTE CompressScratchBuffer,
  73. IN unsigned BufferSize,
  74. IN UINT FileHandle
  75. )
  76. {
  77. unsigned u;
  78. unsigned Size;
  79. unsigned Written;
  80. FPBYTE p;
  81. //
  82. // Need high bit for private use so max data size is 32K.
  83. // Also disallow 0.
  84. //
  85. if(!DataSize || (DataSize > 32768)) {
  86. return(FALSE);
  87. }
  88. //
  89. // Compress the data.
  90. //
  91. u = CompressData(Type,Data,DataSize,CompressScratchBuffer,BufferSize);
  92. if((u == (unsigned)(-1)) || ((u + sizeof(unsigned)) >= DataSize)) {
  93. //
  94. // Data expanded or didn't compress enough to make it
  95. // worthwhile to store it in compressed form.
  96. //
  97. // Store the data size minus 1 with the high bit set to indicate
  98. // that the data is not compressed.
  99. //
  100. Size = DataSize;
  101. u = (DataSize - 1) | 0x8000;
  102. p = Data;
  103. } else {
  104. //
  105. // Data has compressed nicely. Store the data size minus 1.
  106. //
  107. Size = u;
  108. u = u-1;
  109. p = CompressScratchBuffer;
  110. }
  111. //
  112. // Write the header to the file, unless compression type is none.
  113. //
  114. if(Type != CompressNone) {
  115. if(_dos_write(FileHandle,&u,sizeof(unsigned),&Written) || (Written != sizeof(unsigned))) {
  116. return(FALSE);
  117. }
  118. }
  119. //
  120. // Write the data to the file.
  121. //
  122. if(_dos_write(FileHandle,p,Size,&Written) || (Written != Size)) {
  123. return(FALSE);
  124. }
  125. return(TRUE);
  126. }