Salome HOME
Merge branch 'V9_9_BR'
[modules/smesh.git] / src / SMESHUtils / SMESH_MGLicenseKeyGen.cxx
index 99336c6a34a28e226cae3228ba8be9858bdfaec1..c86bad807ba73776683187f5556b06212642e422 100644 (file)
@@ -1,4 +1,4 @@
-// Copyright (C) 2007-2021  CEA/DEN, EDF R&D, OPEN CASCADE
+// Copyright (C) 2007-2022  CEA/DEN, EDF R&D, OPEN CASCADE
 //
 // This library is free software; you can redistribute it and/or
 // modify it under the terms of the GNU Lesser General Public
 #include <Basics_DirUtils.hxx>
 #include <Basics_Utils.hxx>
 
-#include <regex>
 #include <cstdlib> // getenv, system
 
 #include <boost/filesystem.hpp>
+#include <boost/regex.hpp>
 namespace boofs = boost::filesystem;
 
 #ifdef WIN32
@@ -60,6 +60,8 @@ namespace boofs = boost::filesystem;
 #undef SMESH_CAUGHT
 #define SMESH_CAUGHT error =
 
+constexpr char MESHGEMS_OLD_STYLE[] = "MESHGEMS_OLD_STYLE";
+
 
 namespace
 {
@@ -152,12 +154,16 @@ namespace
                                  0,
                                  NULL
                                  );
-    if ( msgLen > 0 )
-      error = (char*) cstr;
-
-    LocalFree(cstr);
+    if ( msgLen > 0 ) {
+#  if defined( UNICODE )
+      error = Kernel_Utils::encode_s((wchar_t*)cstr);
+#  else
+      error = (char*)cstr;
+#  endif
+      LocalFree(cstr);
+    }
 
-    return msgLen;
+    return (bool)msgLen;
 
 #endif
   }
@@ -199,12 +205,12 @@ namespace
   {
     {// round1
       enum { SCHEME = 2, AUTHORITY = 4, PATH = 5 }; // sub-strings
-      std::regex urlRegex ( R"(^(([^:\/?#]+):)?(//([^\/?#]*))?([^?#]*)(\?([^#]*))?(#(.*))?)",
-                            std::regex::extended );
-      std::smatch matchResult;
+      boost::regex urlRegex ( R"(^(([^:\/?#]+):)?(//([^\/?#]*))?([^?#]*)(\?([^#]*))?(#(.*))?)",
+                              boost::regex::extended );
+      boost::smatch matchResult;
 
       libraryFile._isURL = false;
-      if ( std::regex_match( libraryFile._name, matchResult, urlRegex ))
+      if ( boost::regex_match( libraryFile._name, matchResult, urlRegex ))
         libraryFile._isURL = ( !matchResult.str( SCHEME    ).empty() &&
                               !matchResult.str( AUTHORITY ).empty() &&
                               !matchResult.str( PATH      ).empty() );
@@ -213,12 +219,12 @@ namespace
       return true;
     {// round2
       enum { HOST = 2, PORT = 3, PATH = 4 }; // sub-strings
-      std::regex urlRegex ( R"(^(([^:\/?#]+):)?([^/]+)?(/[^#]*))",
-                            std::regex::extended );
-      std::smatch matchResult;
+      boost::regex urlRegex ( R"(^(([^:\/?#]+):)?([^/]+)?(/[^#]*))",
+                              boost::regex::extended );
+      boost::smatch matchResult;
 
       libraryFile._isURL = false;
-      if ( std::regex_match( libraryFile._name, matchResult, urlRegex ))
+      if ( boost::regex_match( libraryFile._name, matchResult, urlRegex ))
         libraryFile._isURL = ( !matchResult.str( HOST ).empty() &&
                               !matchResult.str( PORT ).empty() &&
                               !matchResult.str( PATH ).empty() );
@@ -259,11 +265,11 @@ namespace
 
 #ifdef WIN32
 
-    std::string outFile = tmpDir + "libMeshGemsKeyGenerator.dll";
+    std::string outFile = tmpDir + "MeshGemsKeyGenerator.dll";
 
     // use wget (== Invoke-WebRequest) PowerShell command available since Windows 7
     std::string psCmd = "wget -Uri " + url + " -OutFile " + outFile;
-    std::string   cmd = "start powershell.exe " + psCmd;
+    std::string   cmd = "powershell.exe " + psCmd;
 
 #else
 
@@ -290,6 +296,8 @@ namespace
 
     if ( ok )
       libraryFile._name = outFile;
+    else
+      error = "Can't download file " + url;
 
     return ok;
   }
@@ -366,35 +374,44 @@ namespace SMESHUtils_MGLicenseKeyGen // API implementation
    */
   //================================================================================
 
-  bool SignCAD( void* meshgems_cad, std::string& error )
+  bool SignCAD_After( void* meshgems_cad, std::string& error )
   {
     LibraryFile libraryFile;
     if ( !loadLibrary( error, libraryFile ))
       return false;
 
+    bool ok = false;
     typedef bool (*SignFun)(void* );
-    SignFun signFun = (SignFun) GetProc( theLibraryHandle, "SignCAD" ); 
+    SignFun signFun = (SignFun) GetProc( theLibraryHandle, "SignCAD" );
     if ( !signFun )
     {
       if ( ! getLastError( error ))
         error = SMESH_Comment( "Can't find symbol 'SignCAD' in '") << getenv( theEnvVar ) << "'";
     }
+    else
+    {
+      SMESH_TRY;
 
-    bool ok;
-
-    SMESH_TRY;
-
-    ok = signFun( meshgems_cad );
-
-    SMESH_CATCH( SMESH::returnError );
+      ok = signFun( meshgems_cad );
 
-    if ( !error.empty() )
-      ok = false;
-    else if ( !ok )
-      error = "SignCAD() failed (located in '" + libraryFile._name + "')";
+      SMESH_CATCH( SMESH::returnError );
 
+      if ( !error.empty() )
+        ok = false;
+      else if ( !ok )
+        error = "SignCAD() failed (located in '" + libraryFile._name + "')";
+    }
     return ok;
   }
+  
+  bool SignCAD( void* meshgems_cad, std::string& error )
+  {
+    const char *meshGemsOldStyleEnvVar( getenv( MESHGEMS_OLD_STYLE ) );
+    if ( !meshGemsOldStyleEnvVar || strlen(meshGemsOldStyleEnvVar) == 0 )
+      return SignCAD_After(meshgems_cad, error);
+    else
+      return true;
+  }
 
   //================================================================================
   /*!
@@ -405,12 +422,13 @@ namespace SMESHUtils_MGLicenseKeyGen // API implementation
    */
   //================================================================================
 
-  bool SignMesh( void* meshgems_mesh, std::string& error )
+  bool SignMesh_After( void* meshgems_mesh, std::string& error )
   {
     LibraryFile libraryFile;
     if ( !loadLibrary( error, libraryFile ))
       return false;
 
+    bool ok = false;
     typedef bool (*SignFun)(void* );
     SignFun signFun = (SignFun) GetProc( theLibraryHandle, "SignMesh" );
     if ( !signFun )
@@ -418,21 +436,30 @@ namespace SMESHUtils_MGLicenseKeyGen // API implementation
       if ( ! getLastError( error ))
         error = SMESH_Comment( "Can't find symbol 'SignMesh' in '") << getenv( theEnvVar ) << "'";
     }
-    bool ok;
-
-    SMESH_TRY;
-
-    ok = signFun( meshgems_mesh );
+    else
+    {
+      SMESH_TRY;
 
-    SMESH_CATCH( SMESH::returnError );
+      ok = signFun( meshgems_mesh );
 
-    if ( !error.empty() )
-      ok = false;
-    else if ( !ok )
-      error = "SignMesh() failed (located in '" + libraryFile._name + "')";
+      SMESH_CATCH( SMESH::returnError );
 
+      if ( !error.empty() )
+        ok = false;
+      else if ( !ok )
+        error = "SignMesh() failed (located in '" + libraryFile._name + "')";
+    }
     return ok;
   }
+  
+  bool SignMesh( void* meshgems_mesh, std::string& error )
+  {
+    const char *meshGemsOldStyleEnvVar( getenv( MESHGEMS_OLD_STYLE ) );
+    if ( !meshGemsOldStyleEnvVar || strlen(meshGemsOldStyleEnvVar) == 0 )
+      return SignMesh_After(meshgems_mesh, error);
+    else
+      return true;
+  }
 
   //================================================================================
   /*!
@@ -444,12 +471,12 @@ namespace SMESHUtils_MGLicenseKeyGen // API implementation
    */
   //================================================================================
 
-  std::string GetKey(const std::string& gmfFile,
-                     int                nbVertex,
-                     int                nbEdge,
-                     int                nbFace,
-                     int                nbVol,
-                     std::string&       error)
+  std::string GetKey_After(const std::string& gmfFile,
+                            int                nbVertex,
+                            int                nbEdge,
+                            int                nbFace,
+                            int                nbVol,
+                            std::string&       error)
   {
     std::string key;
     LibraryFile libraryFile;
@@ -463,14 +490,30 @@ namespace SMESHUtils_MGLicenseKeyGen // API implementation
       if ( ! getLastError( error ))
         error = SMESH_Comment( "Can't find symbol 'GetKey' in '") << getenv( theEnvVar ) << "'";
     }
-    key = keyFun( gmfFile, nbVertex, nbEdge, nbFace, nbVol );
-
+    else
+    {
+      key = keyFun( gmfFile, nbVertex, nbEdge, nbFace, nbVol );
+    }
     if ( key.empty() )
       error = "GetKey() failed (located in '" + libraryFile._name + "')";
 
     return key;
   }
 
+  std::string GetKey(const std::string& gmfFile,
+                    int                nbVertex,
+                    int                nbEdge,
+                    int                nbFace,
+                    int                nbVol,
+                    std::string&       error)
+  {
+    const char *meshGemsOldStyleEnvVar( getenv( MESHGEMS_OLD_STYLE ) );
+    if ( !meshGemsOldStyleEnvVar || strlen(meshGemsOldStyleEnvVar) == 0 )
+      return GetKey_After(gmfFile,nbVertex,nbEdge,nbFace,nbVol,error);
+    else
+      return std::string("0");
+  }
+
   //================================================================================
   /*!
    * \brief Return false if libMeshGemsKeyGenerator.so is not functional