opc_zipextract.c 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. /**
  2. Copyright (c) 2010, Florian Reuter
  3. All rights reserved.
  4. Redistribution and use in source and binary forms, with or without
  5. modification, are permitted provided that the following conditions
  6. are met:
  7. * Redistributions of source code must retain the above copyright
  8. notice, this list of conditions and the following disclaimer.
  9. * Redistributions in binary form must reproduce the above copyright
  10. notice, this list of conditions and the following disclaimer in
  11. the documentation and/or other materials provided with the
  12. distribution.
  13. * Neither the name of Florian Reuter nor the names of its contributors
  14. may be used to endorse or promote products derived from this
  15. software without specific prior written permission.
  16. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  17. "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  18. LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
  19. FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
  20. COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
  21. INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
  22. BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  23. LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  24. CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
  25. STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  26. ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
  27. OF THE POSSIBILITY OF SUCH DAMAGE.
  28. */
  29. /*
  30. Tests the low-level ZIP functionality. Will extract all streams and verify the CRC checksum.
  31. Ussage:
  32. opc_zipextract FILENAME
  33. Sample:
  34. opc_zipextract OOXMLI1.docx
  35. */
  36. #include <opc/opc.h>
  37. #include <stdio.h>
  38. #include <libxml/xmlstring.h>
  39. #include <time.h>
  40. #include <zlib.h> // for crc32 function
  41. #ifdef WIN32
  42. #include <crtdbg.h>
  43. #endif
  44. static opc_error_t loadSegment(void *iocontext,
  45. void *userctx,
  46. opcZipSegmentInfo_t *info,
  47. opcZipLoaderOpenCallback *open,
  48. opcZipLoaderReadCallback *read,
  49. opcZipLoaderCloseCallback *close,
  50. opcZipLoaderSkipCallback *skip) {
  51. opcZip *zip=(opcZip*)userctx;
  52. // OPC_ENSURE(0==skip(iocontext));
  53. OPC_ENSURE(0==open(iocontext));
  54. opc_uint32_t crc=0;
  55. char buf[100];
  56. int ret=0;
  57. while((ret=read(iocontext, buf, sizeof(buf)))>0) {
  58. crc=crc32(crc, (const Bytef*)buf, ret);
  59. }
  60. OPC_ASSERT(info->data_crc==crc);
  61. OPC_ASSERT(ret>=0);
  62. OPC_ENSURE(0==close(iocontext));
  63. opcZipLoadSegment(zip, xmlStrdup(info->name), info->rels_segment, info);
  64. return OPC_ERROR_NONE;
  65. }
  66. static opc_error_t releaseSegment(opcZip *zip, opc_uint32_t segment_id) {
  67. const xmlChar *name=NULL;
  68. OPC_ENSURE(OPC_ERROR_NONE==opcZipGetSegmentInfo(zip, segment_id, &name, NULL, NULL));
  69. OPC_ASSERT(NULL!=name);
  70. xmlFree((void*)name);
  71. return OPC_ERROR_NONE;
  72. }
  73. int main( int argc, const char* argv[] )
  74. {
  75. #ifdef WIN32
  76. _CrtSetDbgFlag (_CRTDBG_ALLOC_MEM_DF | _CRTDBG_LEAK_CHECK_DF);
  77. #endif
  78. time_t start_time=time(NULL);
  79. opc_error_t err=OPC_ERROR_NONE;
  80. if (OPC_ERROR_NONE==(err=opcInitLibrary())) {
  81. if (argc>2) {
  82. opcIO_t io;
  83. if (OPC_ERROR_NONE==opcFileInitIOFile(&io, _X(argv[1]), OPC_FILE_READ)) {
  84. opcZip *zip=opcZipCreate(&io);
  85. if (NULL!=zip) {
  86. err=opcZipLoader(&io, zip, loadSegment);
  87. if (OPC_ERROR_NONE==err) {
  88. // successfully loaded; dump all segments
  89. for(opc_uint32_t segment_id=opcZipGetFirstSegmentId(zip);
  90. -1!=segment_id;
  91. segment_id=opcZipGetNextSegmentId(zip, segment_id)) {
  92. const xmlChar *name=NULL;
  93. opc_bool_t rels_segment=OPC_FALSE;
  94. opc_uint32_t data_crc=0;
  95. OPC_ENSURE(OPC_ERROR_NONE==opcZipGetSegmentInfo(zip, segment_id, &name, &rels_segment, &data_crc));
  96. OPC_ASSERT(NULL!=name);
  97. if (!rels_segment && 0==xmlStrcmp(name, _X(argv[2]))) {
  98. printf("extracting \"%s\"\n", name);
  99. opcZipInputStream *stream=opcZipOpenInputStream(zip, segment_id);
  100. if (NULL!=stream) {
  101. opc_uint32_t crc=0;
  102. opc_uint8_t buf[100];
  103. opc_uint32_t ret=0;
  104. while((ret=opcZipReadInputStream(zip, stream , buf, sizeof(buf)))>0) {
  105. // printf("%.*s", ret, buf);
  106. crc=crc32(crc, (const Bytef*)buf, ret);
  107. }
  108. OPC_ASSERT(crc==data_crc);
  109. opcZipCloseInputStream(zip, stream);
  110. }
  111. } else {
  112. printf("skip \"%s\" %s\n", name, (rels_segment?"(.rels)":""));
  113. }
  114. }
  115. }
  116. opcZipClose(zip, releaseSegment);
  117. } else {
  118. OPC_ENSURE(OPC_ERROR_NONE==opcFileCleanupIO(&io));
  119. }
  120. }
  121. }
  122. if (OPC_ERROR_NONE==err) err=opcFreeLibrary();
  123. }
  124. time_t end_time=time(NULL);
  125. fprintf(stderr, "time %.2lfsec\n", difftime(end_time, start_time));
  126. #ifdef WIN32
  127. OPC_ASSERT(!_CrtDumpMemoryLeaks());
  128. #endif
  129. return (OPC_ERROR_NONE==err?0:3);
  130. }