[Refactoring] Code cleanup and remove duplicate methods
[platform/core/dotnet/launcher.git] / NativeLauncher / tool / r2r_checker.cc
index c2847b0..06ef6e4 100644 (file)
@@ -110,7 +110,6 @@ static bool hasValidR2RHeader(void* pAddr)
 {
        IMAGE_NT_HEADERS* pNTHeaders = getNTHeaders(pAddr);
        if (!pNTHeaders) {
-               _SERR("Invalid NT Header");
                return false;
        }
 
@@ -141,22 +140,21 @@ static bool hasValidR2RHeader(void* pAddr)
        return false;
 }
 
-bool isR2RImage(std::string fileName)
+bool isR2RImage(const std::string& fileName)
 {
        int fd;
        struct stat sb;
        if ((fd = open(fileName.c_str(), O_RDONLY)) == -1) {
-               _SERR("File Not Found: %s", fileName.c_str());
                return false;
        }
 
        if (fstat(fd, &sb) == -1) {
+               close(fd);
                return false;
        }
 
        void* pAddr = mmap(NULL, sb.st_size, PROT_READ, MAP_PRIVATE, fd, 0);
        if (pAddr == MAP_FAILED) {
-               _SERR("Fail to Map File: %s", fileName.c_str());
                close(fd);
                return false;
        }
@@ -167,3 +165,37 @@ bool isR2RImage(std::string fileName)
        close(fd);
        return ret;
 }
+
+unsigned int getSizeOfImage(const std::string& fileName)
+{
+       int fd;
+       struct stat sb;
+       if ((fd = open(fileName.c_str(), O_RDONLY)) == -1) {
+               return 0;
+       }
+
+       if (fstat(fd, &sb) == -1) {
+               close(fd);
+               return 0;
+       }
+
+       void* pAddr = mmap(NULL, sb.st_size, PROT_READ, MAP_PRIVATE, fd, 0);
+       if (pAddr == MAP_FAILED) {
+               close(fd);
+               return 0;
+       }
+
+       IMAGE_NT_HEADERS* pNTHeaders = getNTHeaders(pAddr);
+       if (!pNTHeaders) {
+               munmap(pAddr, sb.st_size);
+               close(fd);
+               return 0;
+       }
+
+       // typedef unsigned int ULONG;
+       unsigned int ret = pNTHeaders->OptionalHeader.SizeOfImage;
+
+       munmap(pAddr, sb.st_size);
+       close(fd);
+       return ret;
+}