opc_zipread.c 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  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. #include <opc/opc.h>
  30. #include <stdio.h>
  31. #include <string.h>
  32. #include <time.h>
  33. #include <opc/zip.h>
  34. #include <zlib.h> // for crc32 function
  35. #ifdef WIN32
  36. #include <crtdbg.h>
  37. #endif
  38. /*
  39. This module demonstrates the low-level opcZipLoader functionality. You can use this method to get very information about a
  40. ZIP file. This can also be used to implement streaming-based access to ZIP files.
  41. Ussage:
  42. opc_zipread [--verify] [--skip] FILENAME
  43. * --verify will verify the checksums.
  44. * --skip will quickly skip the streams.
  45. Sample:
  46. opc_zipread --verify OOXMLI1.docx
  47. opc_zipread --skip OOXMLI1.docx
  48. */
  49. static opc_bool_t verify_crc = OPC_FALSE;
  50. opc_error_t loadSegment(void *iocontext,
  51. void *userctx,
  52. opcZipSegmentInfo_t *info,
  53. opcZipLoaderOpenCallback *open,
  54. opcZipLoaderReadCallback *read,
  55. opcZipLoaderCloseCallback *close,
  56. opcZipLoaderSkipCallback *skip) {
  57. printf("%i: %s%s(%i%s) %i/%i %i/%i...", (int)info->stream_ofs,
  58. info->name,
  59. (info->rels_segment?"(.rels)":""),
  60. info->segment_number,
  61. (info->last_segment?".last":""),
  62. info->compressed_size, info->uncompressed_size,
  63. info->min_header_size, info->header_size);
  64. if (!verify_crc) {
  65. // enable this to SKIP throught the files FAST.
  66. OPC_ENSURE(0==skip(iocontext));
  67. printf("skipped\n");
  68. } else {
  69. // enable this to very the CRC checksums
  70. opc_bool_t ok=OPC_FALSE;
  71. if (0==open(iocontext)) {
  72. opc_uint32_t crc=0;
  73. char buf[OPC_DEFLATE_BUFFER_SIZE];
  74. int ret=0;
  75. while((ret=read(iocontext, buf, sizeof(buf)))>0) {
  76. crc=crc32(crc, (const Bytef*)buf, ret);
  77. }
  78. OPC_ENSURE(0==close(iocontext));
  79. ok=(info->data_crc==crc);
  80. }
  81. if (ok) {
  82. printf("ok\n");
  83. } else {
  84. printf("failure\n");
  85. }
  86. }
  87. return OPC_ERROR_NONE;
  88. }
  89. int main( int argc, const char* argv[] )
  90. {
  91. #ifdef WIN32
  92. _CrtSetDbgFlag (_CRTDBG_ALLOC_MEM_DF | _CRTDBG_LEAK_CHECK_DF);
  93. #endif
  94. time_t start_time=time(NULL);
  95. opc_error_t err=OPC_ERROR_NONE;
  96. if (OPC_ERROR_NONE==(err=opcInitLibrary())) {
  97. for(int i=1;OPC_ERROR_NONE==err && i<argc;i++) {
  98. if (xmlStrcasecmp(_X(argv[i]), _X("--verify"))==0) {
  99. verify_crc=OPC_TRUE;
  100. } else if (xmlStrcasecmp(_X(argv[i]), _X("--skip"))==0) {
  101. verify_crc=OPC_FALSE;
  102. } else {
  103. opcIO_t io;
  104. if (OPC_ERROR_NONE==opcFileInitIOFile(&io, _X(argv[i]), OPC_FILE_READ)) {
  105. err=opcZipLoader(&io, NULL, loadSegment);
  106. }
  107. OPC_ENSURE(OPC_ERROR_NONE==opcFileCleanupIO(&io));
  108. }
  109. }
  110. if (OPC_ERROR_NONE==err) err=opcFreeLibrary();
  111. }
  112. time_t end_time=time(NULL);
  113. fprintf(stderr, "time %.2lfsec\n", difftime(end_time, start_time));
  114. #ifdef WIN32
  115. OPC_ASSERT(!_CrtDumpMemoryLeaks());
  116. #endif
  117. return (OPC_ERROR_NONE==err?0:3);
  118. }