/libnpk/tests/libnpk_read_entity_partial.cpp

http://npk.googlecode.com/ · C++ · 44 lines · 32 code · 10 blank · 2 comment · 4 complexity · b38233ce71d2ca6a464d00ab5940cf64 MD5 · raw file

  1. #include "testutil.h"
  2. #include <string>
  3. #include <npk.h>
  4. #include <npk_dev.h>
  5. int libnpk_read_entity_partial( int argc, char * argv [] )
  6. {
  7. int teakey[4] = {98521,16322,7163,992};
  8. NPK_PACKAGE pack = npk_package_open( "sample.npk", teakey );
  9. CHECK( pack != NULL );
  10. std::string entityNames[2] = { "sample.txt", "tea.txt" };
  11. for( int i = 0; i < 2; ++i )
  12. {
  13. NPK_ENTITY entity = npk_package_get_entity( pack, entityNames[i].c_str() );
  14. CHECK( entity != NULL );
  15. NPK_SIZE size = npk_entity_get_size( entity );
  16. void* buf = malloc( size );
  17. NPK_FLAG flag;
  18. npk_entity_get_current_flag( entity, &flag );
  19. if( flag & ( NPK_ENTITY_ENCRYPT_TEA | NPK_ENTITY_ENCRYPT_XXTEA ) )
  20. {
  21. // offset must be aligned by 8 bytes
  22. CHECK( (!npk_entity_read_partial( entity, buf, 9, 32 )) );
  23. // Size can be not aligned by 8 bytes when read end of file
  24. NPK_SIZE offset = size - (size % 8) - 32;
  25. CHECK( npk_entity_read_partial( entity, buf, offset, size - offset ) );
  26. CHECK_EQUAL_STR_WITH_FILE_PARTIAL( (const char*)buf, "sample.txt", offset, size - offset );
  27. }
  28. CHECK( npk_entity_read_partial( entity, buf, 8, 32 ) );
  29. CHECK_EQUAL_STR_WITH_FILE_PARTIAL( (const char*)buf, "sample.txt", 8, 32 );
  30. free( buf );
  31. }
  32. npk_package_close( pack );
  33. return 0;
  34. }