tizen 2.4 release
[framework/web/wrt-commons.git] / modules / core / src / zip_input.cpp
old mode 100644 (file)
new mode 100755 (executable)
similarity index 83%
rename from modules_mobile/core/src/zip_input.cpp
rename to modules/core/src/zip_input.cpp
index fadc60f..ce2abd4
 #include <dpl/zip_input.h>
 #include <dpl/scoped_close.h>
 #include <dpl/binary_queue.h>
-#include <dpl/scoped_free.h>
+#include <dpl/free_deleter.h>
 #include <memory>
-#include <dpl/scoped_array.h>
 #include <dpl/foreach.h>
-#include <dpl/log/log.h>
+#include <dpl/log/wrt_log.h>
 #include <new>
 #include <minizip/unzip.h>
 
@@ -58,7 +57,7 @@ class ScopedUnzClose
         }
 
         if (unzClose(m_file) != UNZ_OK) {
-            LogPedantic("Failed to close zip input file");
+            WrtLogD("Failed to close zip input file");
         }
     }
 
@@ -110,9 +109,16 @@ class Device
     };
 
   public:
-    Device(const std::string &fileName)
+
+    Device (const Device &) = delete;
+    Device & operator= (const Device &) = delete;
+
+    Device(const std::string &fileName) :
+        m_handle(-1),
+        m_size(0),
+        m_address(NULL)
     {
-        LogPedantic("Creating file mapping");
+        WrtLogD("Creating file mapping");
         // Open device and map it to user space
         int file = TEMP_FAILURE_RETRY(open(fileName.c_str(), O_RDONLY));
 
@@ -151,10 +157,8 @@ class Device
         m_size = size;
         m_address = static_cast<unsigned char *>(address);
 
-        LogPedantic("Created file mapping: " << fileName <<
-                    " of size: " << m_size <<
-                    " at address: " << std::hex <<
-                    static_cast<void *>(m_address));
+        WrtLogD("Created file mapping: %s of size: %lli at address: %p",
+                fileName.c_str(), m_size, m_address);
     }
 
     ~Device()
@@ -162,14 +166,15 @@ class Device
         // Close mapping
         if (munmap(m_address, static_cast<size_t>(m_size)) == -1) {
             int error = errno;
-            LogPedantic("Failed to munmap file. errno = " << error);
+            WrtLogD("Failed to munmap file. errno = %i", error);
         }
-
+        m_address= NULL;
         // Close file descriptor
         if (close(m_handle) == -1) {
             int error = errno;
-            LogPedantic("Failed to close file. errno = " << error);
+            WrtLogD("Failed to close file. errno = %i", error);
         }
+        m_handle = -1;
     }
 
     // zlib_filefunc64_def interface: files
@@ -188,12 +193,22 @@ class Device
                                      void* buf,
                                      uLong size)
     {
+        if(!buf){
+            WrtLogE("target buffer is null");
+            return -1;
+        }
+
         Device *device = static_cast<Device *>(opaque);
         File *deviceFile = static_cast<File *>(pstream);
 
+        if(device->m_address == NULL) {
+            WrtLogE("Device: already closed");
+            return -1;
+        }
+
         // Check if offset is out of bounds
         if (deviceFile->offset >= device->m_size) {
-            LogPedantic("Device: read offset out of bounds");
+            WrtLogD("Device: read offset out of bounds");
             return -1;
         }
 
@@ -209,6 +224,10 @@ class Device
             bytesToRead = static_cast<off64_t>(size);
         }
 
+        if(device->m_size < deviceFile->offset + bytesToRead){
+            WrtLogE("Device : end offset out of bounds");
+            return -1;
+        }
         // Do copy
         memcpy(buf,
                device->m_address + deviceFile->offset,
@@ -227,7 +246,7 @@ class Device
                                       uLong /*size*/)
     {
         // Not supported by device
-        LogPedantic("Unsupported function called!");
+        WrtLogD("Unsupported function called!");
         return -1;
     }
 
@@ -296,10 +315,10 @@ ZipInput::ZipInput(const std::string &fileName) :
     m_totalUncompressedSize(0),
     m_fileInfos()
 {
-    LogPedantic("Zip input file: " << fileName);
+    WrtLogD("Zip input file: %s", fileName.c_str());
 
     // Create master device
-    LogPedantic("Creating master device");
+    WrtLogD("Creating master device");
     std::unique_ptr<Device> device(new Device(fileName));
 
     // Open master file
@@ -313,11 +332,11 @@ ZipInput::ZipInput(const std::string &fileName) :
     interface.zerror_file = &Device::testerror_file;
     interface.opaque = device.get();
 
-    LogPedantic("Opening zip file");
+    WrtLogD("Opening zip file");
     unzFile file = unzOpen2_64(NULL, &interface);
 
     if (file == NULL) {
-        LogPedantic("Failed to open zip file");
+        WrtLogD("Failed to open zip file");
 
         // Some errror occured
         ThrowMsg(Exception::OpenFailed,
@@ -336,14 +355,14 @@ ZipInput::ZipInput(const std::string &fileName) :
     m_masterFile = scopedUnzClose.Release();
     m_device = device.release();
 
-    LogPedantic("Zip file opened");
+    WrtLogD("Zip file opened");
 }
 
 ZipInput::~ZipInput()
 {
     // Close zip
     if (unzClose(static_cast<unzFile>(m_masterFile)) != UNZ_OK) {
-        LogPedantic("Failed to close zip input file");
+        WrtLogD("Failed to close zip input file");
     }
 
     // Close device
@@ -358,7 +377,7 @@ void ZipInput::ReadGlobalInfo(void *masterFile)
     if (unzGetGlobalInfo(static_cast<unzFile>(masterFile),
                          &globalInfo) != UNZ_OK)
     {
-        LogPedantic("Failed to read zip global info");
+        WrtLogD("Failed to read zip global info");
 
         ThrowMsg(Exception::ReadGlobalInfoFailed,
                  "Failed to read global info");
@@ -367,26 +386,26 @@ void ZipInput::ReadGlobalInfo(void *masterFile)
     m_numberOfFiles = static_cast<size_t>(globalInfo.number_entry);
     m_globalCommentSize = static_cast<size_t>(globalInfo.size_comment);
 
-    LogPedantic("Number of files: " << m_numberOfFiles);
-    LogPedantic("Global comment size: " << m_globalCommentSize);
+    WrtLogD("Number of files: %u", m_numberOfFiles);
+    WrtLogD("Global comment size: %u", m_globalCommentSize);
 }
 
 void ZipInput::ReadGlobalComment(void *masterFile)
 {
-    ScopedArray<char> comment(new char[m_globalCommentSize + 1]);
+    std::unique_ptr<char[]> comment(new char[m_globalCommentSize + 1]);
 
     if (unzGetGlobalComment(static_cast<unzFile>(masterFile),
-                            comment.Get(),
+                            comment.get(),
                             m_globalCommentSize + 1) != UNZ_OK)
     {
-        LogPedantic("Failed to read zip global comment");
+        WrtLogD("Failed to read zip global comment");
 
         ThrowMsg(Exception::ReadGlobalCommentFailed,
                  "Failed to read global comment");
     }
 
-    m_globalComment = comment.Get();
-    LogPedantic("Global comment: " << m_globalComment);
+    m_globalComment = comment.get();
+    WrtLogD("Global comment: %s", m_globalComment.c_str());
 }
 
 void ZipInput::ReadInfos(void *masterFile)
@@ -395,7 +414,7 @@ void ZipInput::ReadInfos(void *masterFile)
     m_fileInfos.reserve(m_numberOfFiles);
 
     if (unzGoToFirstFile(static_cast<unzFile>(masterFile)) != UNZ_OK) {
-        LogPedantic("Failed to go to first file");
+        WrtLogD("Failed to go to first file");
         ThrowMsg(Exception::SeekFileFailed, "Failed to seek first file");
     }
 
@@ -405,7 +424,7 @@ void ZipInput::ReadInfos(void *masterFile)
         if (unzGetFilePos(static_cast<unzFile>(masterFile),
                           &filePos) != UNZ_OK)
         {
-            LogPedantic("Failed to get file pos");
+            WrtLogD("Failed to get file pos");
             ThrowMsg(Exception::FileInfoFailed, "Failed to get zip file info");
         }
 
@@ -420,23 +439,23 @@ void ZipInput::ReadInfos(void *masterFile)
                                     NULL,
                                     0) != UNZ_OK)
         {
-            LogPedantic("Failed to get file pos");
+            WrtLogD("Failed to get file pos");
             ThrowMsg(Exception::FileInfoFailed, "Failed to get zip file info");
         }
 
-        ScopedArray<char> fileName(new char[fileInfo.size_filename + 1]);
-        ScopedArray<char> fileComment(new char[fileInfo.size_file_comment + 1]);
+        std::unique_ptr<char[]> fileName(new char[fileInfo.size_filename + 1]);
+        std::unique_ptr<char[]> fileComment(new char[fileInfo.size_file_comment + 1]);
 
         if (unzGetCurrentFileInfo64(static_cast<unzFile>(masterFile),
                                     &fileInfo,
-                                    fileName.Get(),
+                                    fileName.get(),
                                     fileInfo.size_filename + 1,
                                     NULL,
                                     0,
-                                    fileComment.Get(),
+                                    fileComment.get(),
                                     fileInfo.size_file_comment + 1) != UNZ_OK)
         {
-            LogPedantic("Failed to get file pos");
+            WrtLogD("Failed to get file pos");
             ThrowMsg(Exception::FileInfoFailed, "Failed to get zip file info");
         }
 
@@ -446,8 +465,8 @@ void ZipInput::ReadInfos(void *masterFile)
                     static_cast<size_t>(filePos.pos_in_zip_directory),
                     static_cast<size_t>(filePos.num_of_file)
                     ),
-                std::string(fileName.Get()),
-                std::string(fileComment.Get()),
+                std::string(fileName.get()),
+                std::string(fileComment.get()),
                 static_cast<off64_t>(fileInfo.compressed_size),
                 static_cast<off64_t>(fileInfo.uncompressed_size)
                 )
@@ -459,7 +478,7 @@ void ZipInput::ReadInfos(void *masterFile)
             if (unzGoToNextFile(
                     static_cast<unzFile>(masterFile)) != UNZ_OK)
             {
-                LogPedantic("Failed to go to next file");
+                WrtLogD("Failed to go to next file");
 
                 ThrowMsg(Exception::FileInfoFailed,
                          "Failed to go to next file");
@@ -519,11 +538,11 @@ ZipInput::File::File(class Device *device, FileHandle handle)
     interface.zerror_file = &Device::testerror_file;
     interface.opaque = device;
 
-    LogPedantic("Opening zip file");
+    WrtLogD("Opening zip file");
     unzFile file = unzOpen2_64(NULL, &interface);
 
     if (file == NULL) {
-        LogPedantic("Failed to open zip file");
+        WrtLogD("Failed to open zip file");
 
         // Some errror occured
         ThrowMsg(ZipInput::Exception::OpenFileFailed,
@@ -540,7 +559,7 @@ ZipInput::File::File(class Device *device, FileHandle handle)
     };
 
     if (unzGoToFilePos64(file, &filePos) != UNZ_OK) {
-        LogPedantic("Failed to seek to zip file");
+        WrtLogD("Failed to seek to zip file");
 
         // Some errror occured
         ThrowMsg(ZipInput::Exception::OpenFileFailed,
@@ -549,7 +568,7 @@ ZipInput::File::File(class Device *device, FileHandle handle)
 
     // Open current file for reading
     if (unzOpenCurrentFile(file) != UNZ_OK) {
-        LogPedantic("Failed to open current zip file");
+        WrtLogD("Failed to open current zip file");
 
         // Some errror occured
         ThrowMsg(ZipInput::Exception::OpenFileFailed,
@@ -559,19 +578,19 @@ ZipInput::File::File(class Device *device, FileHandle handle)
     // Release scoped unz close
     m_file = scopedUnzClose.Release();
 
-    LogPedantic("Zip file opened");
+    WrtLogD("Zip file opened");
 }
 
 ZipInput::File::~File()
 {
     // Close current file for reading
     if (unzCloseCurrentFile(static_cast<unzFile>(m_file)) != UNZ_OK) {
-        LogPedantic("Failed to close current zip input file");
+        WrtLogD("Failed to close current zip input file");
     }
 
     // Close zip file
     if (unzClose(static_cast<unzFile>(m_file)) != UNZ_OK) {
-        LogPedantic("Failed to close zip input file");
+        WrtLogD("Failed to close zip input file");
     }
 }
 
@@ -588,7 +607,7 @@ DPL::BinaryQueueAutoPtr ZipInput::File::Read(size_t size)
         size;
 
     // Extract zip file data (one-copy)
-    ScopedFree<void> rawBuffer(malloc(sizeToRead));
+    std::unique_ptr<void,free_deleter> rawBuffer(malloc(sizeToRead));
 
     if (!rawBuffer) {
         throw std::bad_alloc();
@@ -596,12 +615,12 @@ DPL::BinaryQueueAutoPtr ZipInput::File::Read(size_t size)
 
     // Do unpack
     int bytes = unzReadCurrentFile(static_cast<unzFile>(m_file),
-                                   rawBuffer.Get(),
+                                   rawBuffer.get(),
                                    sizeToRead);
 
     // Internal unzipper error
     if (bytes < 0) {
-        LogPedantic("Extract failed. Error: " << bytes);
+        WrtLogD("Extract failed. Error: %i", bytes);
 
         ThrowMsg(ZipInput::Exception::ReadFileFailed,
                  "Failed to extract file with error: " << bytes);
@@ -610,12 +629,12 @@ DPL::BinaryQueueAutoPtr ZipInput::File::Read(size_t size)
     // Data was read (may be zero bytes)
     DPL::BinaryQueueAutoPtr buffer(new DPL::BinaryQueue());
 
-    buffer->AppendUnmanaged(rawBuffer.Get(),
+    buffer->AppendUnmanaged(rawBuffer.get(),
                             static_cast<size_t>(bytes),
                             &DPL::BinaryQueue::BufferDeleterFree,
                             NULL);
 
-    rawBuffer.Release();
+    rawBuffer.release();
 
     return buffer;
 }