centralDir.cc 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320
  1. //-----------------------------------------------------------------------------
  2. // Copyright (c) 2013 GarageGames, LLC
  3. //
  4. // Permission is hereby granted, free of charge, to any person obtaining a copy
  5. // of this software and associated documentation files (the "Software"), to
  6. // deal in the Software without restriction, including without limitation the
  7. // rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
  8. // sell copies of the Software, and to permit persons to whom the Software is
  9. // furnished to do so, subject to the following conditions:
  10. //
  11. // The above copyright notice and this permission notice shall be included in
  12. // all copies or substantial portions of the Software.
  13. //
  14. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  19. // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
  20. // IN THE SOFTWARE.
  21. //-----------------------------------------------------------------------------
  22. #include "platform/platform.h"
  23. #include "io/stream.h"
  24. #include "io/zip/centralDir.h"
  25. #include "io/zip/compressor.h"
  26. #include "memory/safeDelete.h"
  27. namespace Zip
  28. {
  29. //////////////////////////////////////////////////////////////////////////
  30. // CentralDir Class
  31. //////////////////////////////////////////////////////////////////////////
  32. CentralDir::CentralDir()
  33. {
  34. mHeaderSig = mCentralDirSignature;
  35. mDiskNumStart = 0;
  36. mInternalFileAttr = 0;
  37. mExternalFileAttr = 0;
  38. mLocalHeadOffset = 0;
  39. mVersionMadeBy = 0;
  40. mFileComment = NULL;
  41. mInternalFlags = 0;
  42. }
  43. CentralDir::CentralDir(FileHeader &fh) : FileHeader(fh)
  44. {
  45. mHeaderSig = mCentralDirSignature;
  46. mDiskNumStart = 0;
  47. mInternalFileAttr = 0;
  48. mExternalFileAttr = 0;
  49. mLocalHeadOffset = 0;
  50. mVersionMadeBy = 0;
  51. mFileComment = NULL;
  52. }
  53. CentralDir::~CentralDir()
  54. {
  55. SAFE_DELETE_ARRAY(mFileComment);
  56. }
  57. //////////////////////////////////////////////////////////////////////////
  58. bool CentralDir::read(Stream *stream)
  59. {
  60. stream->read(&mHeaderSig);
  61. if(mHeaderSig != mCentralDirSignature)
  62. return false;
  63. stream->read(&mVersionMadeBy);
  64. stream->read(&mExtractVer);
  65. stream->read(&mFlags);
  66. stream->read(&mCompressMethod);
  67. stream->read(&mModTime);
  68. stream->read(&mModDate);
  69. stream->read(&mCRC32);
  70. stream->read(&mCompressedSize);
  71. stream->read(&mUncompressedSize);
  72. U16 fnLen, efLen, fcLen;
  73. stream->read(&fnLen);
  74. stream->read(&efLen);
  75. stream->read(&fcLen);
  76. stream->read(&mDiskNumStart);
  77. stream->read(&mInternalFileAttr);
  78. stream->read(&mExternalFileAttr);
  79. stream->read(&mLocalHeadOffset);
  80. char *fn = new char[fnLen + 1];
  81. stream->read(fnLen, fn);
  82. fn[fnLen] = 0;
  83. SAFE_DELETE_ARRAY(mFilename);
  84. mFilename = fn;
  85. // [tom, 10/28/2006] We currently only need the extra fields when we want to
  86. // open the file, so we won't bother reading them here. This avoids keeping
  87. // them in memory twice.
  88. //readExtraFields(stream, efLen);
  89. stream->setPosition(stream->getPosition() + efLen);
  90. fn = new char[fcLen + 1];
  91. stream->read(fcLen, fn);
  92. fn[fcLen] = 0;
  93. SAFE_DELETE_ARRAY(mFileComment);
  94. mFileComment = fn;
  95. // Sanity checks to make life easier elsewhere
  96. if(mCompressMethod != Stored && mUncompressedSize == 0 && mCompressedSize == 0)
  97. mCompressMethod = Stored;
  98. return true;
  99. }
  100. bool CentralDir::write(Stream *stream)
  101. {
  102. mHeaderSig = mCentralDirSignature;
  103. stream->write(mHeaderSig);
  104. stream->write(mVersionMadeBy);
  105. stream->write(mExtractVer);
  106. stream->write(mFlags);
  107. stream->write(mCompressMethod);
  108. stream->write(mModTime);
  109. stream->write(mModDate);
  110. stream->write(mCRC32);
  111. stream->write(mCompressedSize);
  112. stream->write(mUncompressedSize);
  113. U16 fnLen = mFilename ? (U16)dStrlen(mFilename) : 0,
  114. efLen = 0,
  115. fcLen = mFileComment ? (U16)dStrlen(mFileComment) : 0;
  116. stream->write(fnLen);
  117. stream->write(efLen);
  118. stream->write(fcLen);
  119. stream->write(mDiskNumStart);
  120. stream->write(mInternalFileAttr);
  121. stream->write(mExternalFileAttr);
  122. stream->write(mLocalHeadOffset);
  123. if(fnLen)
  124. stream->write(fnLen, mFilename);
  125. // FIXME [tom, 10/29/2006] Write extra fields here
  126. if(fcLen)
  127. stream->write(fcLen, mFileComment);
  128. return true;
  129. }
  130. //////////////////////////////////////////////////////////////////////////
  131. void CentralDir::setFileComment(const char *comment)
  132. {
  133. SAFE_DELETE_ARRAY(mFileComment);
  134. mFileComment = new char [dStrlen(comment)+1];
  135. dStrcpy((char *)mFileComment, comment);
  136. }
  137. //////////////////////////////////////////////////////////////////////////
  138. // EndOfCentralDir Class
  139. //////////////////////////////////////////////////////////////////////////
  140. EndOfCentralDir::EndOfCentralDir()
  141. {
  142. mHeaderSig = mEOCDSignature;
  143. mDiskNum = 0;
  144. mStartCDDiskNum = 0;
  145. mNumEntriesInThisCD = 0;
  146. mTotalEntriesInCD = 0;
  147. mCDSize = 0;
  148. mCDOffset = 0;
  149. mCommentSize = 0;
  150. mZipComment = NULL;
  151. }
  152. EndOfCentralDir::~EndOfCentralDir()
  153. {
  154. SAFE_DELETE_ARRAY(mZipComment);
  155. }
  156. //////////////////////////////////////////////////////////////////////////
  157. bool EndOfCentralDir::read(Stream *stream)
  158. {
  159. stream->read(&mHeaderSig);
  160. if(mHeaderSig != mEOCDSignature)
  161. return false;
  162. stream->read(&mDiskNum);
  163. stream->read(&mStartCDDiskNum);
  164. stream->read(&mNumEntriesInThisCD);
  165. stream->read(&mTotalEntriesInCD);
  166. stream->read(&mCDSize);
  167. stream->read(&mCDOffset);
  168. stream->read(&mCommentSize);
  169. char *comment = new char[mCommentSize + 1];
  170. stream->read(mCommentSize, comment);
  171. comment[mCommentSize] = 0;
  172. SAFE_DELETE_ARRAY(mZipComment);
  173. mZipComment = comment;
  174. return true;
  175. }
  176. bool EndOfCentralDir::write(Stream *stream)
  177. {
  178. stream->write(mHeaderSig);
  179. stream->write(mDiskNum);
  180. stream->write(mStartCDDiskNum);
  181. stream->write(mNumEntriesInThisCD);
  182. stream->write(mTotalEntriesInCD);
  183. stream->write(mCDSize);
  184. stream->write(mCDOffset);
  185. stream->write(mCommentSize);
  186. if(mZipComment && mCommentSize)
  187. stream->write(mCommentSize, mZipComment);
  188. return true;
  189. }
  190. //////////////////////////////////////////////////////////////////////////
  191. // [tom, 10/19/2006] I know, i know ... this'll get rewritten.
  192. // [tom, 1/23/2007] Maybe.
  193. bool EndOfCentralDir::findInStream(Stream *stream)
  194. {
  195. U32 initialPos = stream->getPosition();
  196. U32 size = stream->getStreamSize();
  197. U32 pos;
  198. if(size == 0)
  199. return false;
  200. if(! stream->setPosition(size - mRecordSize))
  201. goto hell;
  202. U32 sig;
  203. stream->read(&sig);
  204. if(sig == mEOCDSignature)
  205. {
  206. stream->setPosition(size - mRecordSize);
  207. return true;
  208. }
  209. // OK, so we couldn't find the EOCD where we expected it. The zip file
  210. // either has comments or isn't a zip file. We need to search the last
  211. // 64Kb of the file for the EOCD.
  212. pos = size > mEOCDSearchSize ? size - mEOCDSearchSize : 0;
  213. if(! stream->setPosition(pos))
  214. goto hell;
  215. while(pos < (size - 4))
  216. {
  217. stream->read(&sig);
  218. if(sig == mEOCDSignature)
  219. {
  220. stream->setPosition(pos);
  221. return true;
  222. }
  223. pos++;
  224. if(! stream->setPosition(pos))
  225. goto hell;
  226. }
  227. hell:
  228. stream->setPosition(initialPos);
  229. return false;
  230. }
  231. //////////////////////////////////////////////////////////////////////////
  232. void EndOfCentralDir::setZipComment(U16 commentSize, const char *zipComment)
  233. {
  234. SAFE_DELETE_ARRAY(mZipComment);
  235. mZipComment = new char [commentSize];
  236. dMemcpy((void *)mZipComment, zipComment, commentSize);
  237. mCommentSize = commentSize;
  238. }
  239. void EndOfCentralDir::setZipComment(const char *zipComment)
  240. {
  241. setZipComment(dStrlen(zipComment), zipComment);
  242. }
  243. } // end namespace Zip