ADCBase.cs 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  1. //
  2. // ADC.cs
  3. //
  4. // Author:
  5. // Natalia Portillo <claunia@claunia.com>
  6. //
  7. // Copyright (c) 2016 © Claunia.com
  8. //
  9. // Permission is hereby granted, free of charge, to any person obtaining a copy
  10. // of this software and associated documentation files (the "Software"), to deal
  11. // in the Software without restriction, including without limitation the rights
  12. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  13. // copies of the Software, and to permit persons to whom the Software is
  14. // furnished to do so, subject to the following conditions:
  15. //
  16. // The above copyright notice and this permission notice shall be included in
  17. // all copies or substantial portions of the Software.
  18. //
  19. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  20. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  21. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  22. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  23. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  24. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  25. // THE SOFTWARE.
  26. using System;
  27. using System.IO;
  28. namespace SharpCompress.Compressors.ADC
  29. {
  30. /// <summary>
  31. /// Provides static methods for decompressing Apple Data Compression data
  32. /// </summary>
  33. public static class ADCBase
  34. {
  35. private const int PLAIN = 1;
  36. private const int TWO_BYTE = 2;
  37. private const int THREE_BYTE = 3;
  38. private static int GetChunkType(byte byt)
  39. {
  40. if ((byt & 0x80) == 0x80)
  41. {
  42. return PLAIN;
  43. }
  44. if ((byt & 0x40) == 0x40)
  45. {
  46. return THREE_BYTE;
  47. }
  48. return TWO_BYTE;
  49. }
  50. private static int GetChunkSize(byte byt)
  51. {
  52. switch (GetChunkType(byt))
  53. {
  54. case PLAIN:
  55. return (byt & 0x7F) + 1;
  56. case TWO_BYTE:
  57. return ((byt & 0x3F) >> 2) + 3;
  58. case THREE_BYTE:
  59. return (byt & 0x3F) + 4;
  60. default:
  61. return -1;
  62. }
  63. }
  64. private static int GetOffset(byte[] chunk, int position)
  65. {
  66. switch (GetChunkType(chunk[position]))
  67. {
  68. case PLAIN:
  69. return 0;
  70. case TWO_BYTE:
  71. return ((chunk[position] & 0x03) << 8) + chunk[position + 1];
  72. case THREE_BYTE:
  73. return (chunk[position + 1] << 8) + chunk[position + 2];
  74. default:
  75. return -1;
  76. }
  77. }
  78. /// <summary>
  79. /// Decompresses a byte buffer that's compressed with ADC
  80. /// </summary>
  81. /// <param name="input">Compressed buffer</param>
  82. /// <param name="output">Buffer to hold decompressed data</param>
  83. /// <param name="bufferSize">Max size for decompressed data</param>
  84. /// <returns>How many bytes are stored on <paramref name="output"/></returns>
  85. public static int Decompress(byte[] input, out byte[] output, int bufferSize = 262144)
  86. {
  87. return Decompress(new MemoryStream(input), out output, bufferSize);
  88. }
  89. /// <summary>
  90. /// Decompresses a stream that's compressed with ADC
  91. /// </summary>
  92. /// <param name="input">Stream containing compressed data</param>
  93. /// <param name="output">Buffer to hold decompressed data</param>
  94. /// <param name="bufferSize">Max size for decompressed data</param>
  95. /// <returns>How many bytes are stored on <paramref name="output"/></returns>
  96. public static int Decompress(Stream input, out byte[] output, int bufferSize = 262144)
  97. {
  98. output = null;
  99. if (input == null || input.Length == 0)
  100. {
  101. return 0;
  102. }
  103. int start = (int)input.Position;
  104. int position = (int)input.Position;
  105. int chunkSize;
  106. int offset;
  107. int chunkType;
  108. byte[] buffer = new byte[bufferSize];
  109. int outPosition = 0;
  110. bool full = false;
  111. MemoryStream tempMs;
  112. while (position < input.Length)
  113. {
  114. int readByte = input.ReadByte();
  115. if (readByte == -1)
  116. {
  117. break;
  118. }
  119. chunkType = GetChunkType((byte)readByte);
  120. switch (chunkType)
  121. {
  122. case PLAIN:
  123. chunkSize = GetChunkSize((byte)readByte);
  124. if (outPosition + chunkSize > bufferSize)
  125. {
  126. full = true;
  127. break;
  128. }
  129. input.Read(buffer, outPosition, chunkSize);
  130. outPosition += chunkSize;
  131. position += chunkSize + 1;
  132. break;
  133. case TWO_BYTE:
  134. tempMs = new MemoryStream();
  135. chunkSize = GetChunkSize((byte)readByte);
  136. tempMs.WriteByte((byte)readByte);
  137. tempMs.WriteByte((byte)input.ReadByte());
  138. offset = GetOffset(tempMs.ToArray(), 0);
  139. if (outPosition + chunkSize > bufferSize)
  140. {
  141. full = true;
  142. break;
  143. }
  144. if (offset == 0)
  145. {
  146. byte lastByte = buffer[outPosition - 1];
  147. for (int i = 0; i < chunkSize; i++)
  148. {
  149. buffer[outPosition] = lastByte;
  150. outPosition++;
  151. }
  152. position += 2;
  153. }
  154. else
  155. {
  156. for (int i = 0; i < chunkSize; i++)
  157. {
  158. buffer[outPosition] = buffer[outPosition - offset - 1];
  159. outPosition++;
  160. }
  161. position += 2;
  162. }
  163. break;
  164. case THREE_BYTE:
  165. tempMs = new MemoryStream();
  166. chunkSize = GetChunkSize((byte)readByte);
  167. tempMs.WriteByte((byte)readByte);
  168. tempMs.WriteByte((byte)input.ReadByte());
  169. tempMs.WriteByte((byte)input.ReadByte());
  170. offset = GetOffset(tempMs.ToArray(), 0);
  171. if (outPosition + chunkSize > bufferSize)
  172. {
  173. full = true;
  174. break;
  175. }
  176. if (offset == 0)
  177. {
  178. byte lastByte = buffer[outPosition - 1];
  179. for (int i = 0; i < chunkSize; i++)
  180. {
  181. buffer[outPosition] = lastByte;
  182. outPosition++;
  183. }
  184. position += 3;
  185. }
  186. else
  187. {
  188. for (int i = 0; i < chunkSize; i++)
  189. {
  190. buffer[outPosition] = buffer[outPosition - offset - 1];
  191. outPosition++;
  192. }
  193. position += 3;
  194. }
  195. break;
  196. }
  197. if (full)
  198. {
  199. break;
  200. }
  201. }
  202. output = new byte[outPosition];
  203. Array.Copy(buffer, 0, output, 0, outPosition);
  204. return position - start;
  205. }
  206. }
  207. }