Skip to content
This repository was archived by the owner on May 18, 2019. It is now read-only.

Commit a385b60

Browse files
adeas31OpenModelica-Hudson
authored andcommitted
Added new API parseEncryptedPackage
Works similar to `parseFile`. Added support for creating .mol packages without encryption which means they only contains the .mo files and also updated `loadEncryptedPackage` to be able to load such packages. Belonging to [master]: - OpenModelica/OpenModelica#98 - #2940
1 parent 5c5d5ad commit a385b60

File tree

5 files changed

+72
-7
lines changed

5 files changed

+72
-7
lines changed

Compiler/FrontEnd/ModelicaBuiltin.mo

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1156,6 +1156,16 @@ external "builtin";
11561156
annotation(preferredView="text");
11571157
end loadFiles;
11581158

1159+
function parseEncryptedPackage
1160+
input String fileName;
1161+
input String workdir = "<default>" "The output directory for imported encrypted files. <default> will put the files to current working directory.";
1162+
output TypeName names[:];
1163+
external "builtin";
1164+
annotation(Documentation(info="<html>
1165+
<p>Parses the given encrypted package and returns the names of the parsed classes.</p>
1166+
</html>"), preferredView="text");
1167+
end parseEncryptedPackage;
1168+
11591169
function loadEncryptedPackage
11601170
input String fileName;
11611171
input String workdir = "<default>" "The output directory for imported encrypted files. <default> will put the files to current working directory.";
@@ -2473,6 +2483,7 @@ end buildModelFMU;
24732483

24742484
function buildEncryptedPackage
24752485
input TypeName className "the class that should encrypted";
2486+
input Boolean encrypt = true;
24762487
output Boolean success;
24772488
output String commandOutput "Output of the packagetool executable";
24782489
external "builtin";

Compiler/Script/CevalScript.mo

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1434,6 +1434,45 @@ algorithm
14341434
// System.GC_enable();
14351435
then (cache,Values.BOOL(false));
14361436

1437+
case (cache,_,"parseEncryptedPackage",Values.STRING(filename)::Values.STRING(workdir)::_,_)
1438+
equation
1439+
if (System.regularFileExists(filename)) then
1440+
if (Util.endsWith(filename, ".mol")) then
1441+
workdir = if System.directoryExists(workdir) then workdir else System.pwd();
1442+
if (0 == System.systemCall("unzip -q -o -d \"" + workdir + "\" \"" + filename + "\"")) then
1443+
s1 = System.basename(filename);
1444+
s2 = Util.removeLast4Char(s1);
1445+
// possible .moc files to look for
1446+
filename1 = workdir + "/" + s2 + "/" + s2 + ".moc";
1447+
filename2 = workdir + "/" + s2 + "/package.moc";
1448+
filename_1 = if System.regularFileExists(filename1) then filename1 else filename2;
1449+
// possible .mo files to look for
1450+
str1 = workdir + "/" + s2 + "/" + s2 + ".mo";
1451+
str2 = workdir + "/" + s2 + "/package.mo";
1452+
str = if System.regularFileExists(str1) then str1 else str2;
1453+
// check if .mol contains .moc or .mo files
1454+
filename_1 = if System.regularFileExists(filename_1) then filename_1 else str;
1455+
if (System.regularFileExists(filename_1)) then
1456+
// clear the errors before!
1457+
Error.clearMessages() "Clear messages";
1458+
Print.clearErrorBuf() "Clear error buffer";
1459+
filename_1 = Util.testsuiteFriendlyPath(filename_1);
1460+
(paths) = Interactive.parseFile(filename_1, "UTF-8");
1461+
vals = List.map(paths,ValuesUtil.makeCodeTypeName);
1462+
else
1463+
Error.addMessage(Error.ENCRYPTED_FILE_NOT_FOUND_ERROR, {filename1, filename2});
1464+
end if;
1465+
else
1466+
Error.addMessage(Error.UNABLE_TO_UNZIP_FILE, {filename});
1467+
end if;
1468+
else
1469+
Error.addMessage(Error.EXPECTED_ENCRYPTED_PACKAGE, {filename});
1470+
end if;
1471+
else
1472+
Error.addMessage(Error.FILE_NOT_FOUND_ERROR, {filename});
1473+
end if;
1474+
then (cache,ValuesUtil.makeArray(vals));
1475+
14371476
case (_,_,"loadEncryptedPackage",Values.STRING(filename)::Values.STRING(workdir)::_,_)
14381477
equation
14391478
b = false;

Compiler/Script/CevalScriptBackend.mo

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1317,11 +1317,11 @@ algorithm
13171317
case (cache,_,"buildModelFMU", _,_)
13181318
then (cache,Values.STRING(""));
13191319

1320-
case (cache,env,"buildEncryptedPackage", {Values.CODE(Absyn.C_TYPENAME(className))},_)
1320+
case (cache,env,"buildEncryptedPackage", {Values.CODE(Absyn.C_TYPENAME(className)),Values.BOOL(b)},_)
13211321
algorithm
13221322
p := SymbolTable.getAbsyn();
1323-
(b, str) := buildEncryptedPackage(className, p);
1324-
then (cache,Values.TUPLE({Values.BOOL(b),Values.STRING(str)}));
1323+
(b1, str) := buildEncryptedPackage(className, b, p);
1324+
then (cache,Values.TUPLE({Values.BOOL(b1),Values.STRING(str)}));
13251325

13261326
case (cache,_,"buildEncryptedPackage",_,_)
13271327
then (cache,Values.TUPLE({Values.BOOL(false),Values.STRING("")}));
@@ -3559,6 +3559,7 @@ end buildModelFMU;
35593559

35603560
protected function buildEncryptedPackage
35613561
input Absyn.Path className "path for the model";
3562+
input Boolean encrypt;
35623563
input Absyn.Program inProgram;
35633564
output Boolean success;
35643565
output String commandOutput;
@@ -3577,7 +3578,7 @@ algorithm
35773578
str2 := stringAppendList({omhome,pd,"lib",pd,"omc",pd,"SEMLA",pd,"packagetool",str1});
35783579
if System.regularFileExists(str2) then
35793580
// create the list of arguments for packagetool
3580-
str3 := "-librarypath \"" + System.dirname(fileName) + "\" -version \"1.0\" -language \"3.2\" -encrypt \"true\"";
3581+
str3 := "-librarypath \"" + System.dirname(fileName) + "\" -version \"1.0\" -language \"3.2\" -encrypt \"" + boolString(encrypt) + "\"";
35813582
call := stringAppendList({str2," ",str3});
35823583
logFile := "packagetool.log";
35833584
// remove the logFile if it already exists.

Compiler/Script/Interactive.mo

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17379,12 +17379,26 @@ public function parseFile
1737917379
output list<Absyn.Path> topClassNamesQualified "The names of the classes from file, qualified!";
1738017380
protected
1738117381
Absyn.Program parsed;
17382+
String dir,filename;
17383+
Boolean lveStarted;
17384+
Option<Integer> lveInstance = NONE();
1738217385
algorithm
1738317386
if not System.regularFileExists(fileName) then
1738417387
topClassNamesQualified := {};
1738517388
return;
1738617389
end if;
17387-
parsed := Parser.parse(fileName,encoding);
17390+
(dir,filename) := Util.getAbsoluteDirectoryAndFile(fileName);
17391+
if filename == "package.moc" then
17392+
(lveStarted, lveInstance) := Parser.startLibraryVendorExecutable(dir);
17393+
if not lveStarted then
17394+
topClassNamesQualified := {};
17395+
return;
17396+
end if;
17397+
end if;
17398+
parsed := Parser.parse(fileName,encoding,dir,lveInstance);
17399+
if (lveStarted) then
17400+
Parser.stopLibraryVendorExecutable(lveInstance);
17401+
end if;
1738817402
parsed := MetaUtil.createMetaClassesInProgram(parsed);
1738917403
topClassNamesQualified := getTopQualifiedClassnames(parsed);
1739017404
if updateProgram then

Parser/parse.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -383,7 +383,7 @@ static void* parseString(const char* data, const char* interactiveFilename, int
383383
#elif defined(ANTLR_C_VERSION_3_4)
384384
input = antlr3StringStreamNew((pANTLR3_UINT8)data, ANTLR3_ENC_8BIT, strlen(data), fName);
385385
#else
386-
#error "Neiter ANTLR_C_VERSION_3_2 or ANTLR_C_VERSION_3_4 is defined. Could not find the ANTLR 3.x C runtime!"
386+
#error "Neither ANTLR_C_VERSION_3_2 or ANTLR_C_VERSION_3_4 is defined. Could not find the ANTLR 3.x C runtime!"
387387
#endif
388388
if ( input == NULL ) {
389389
fprintf(stderr, "Unable to open file %s\n", members.filename_C); fflush(stderr);
@@ -446,7 +446,7 @@ static void* parseFile(const char* fileName, const char* infoName, int flags, co
446446
#elif defined(ANTLR_C_VERSION_3_4)
447447
input = antlr3FileStreamNew(fName, ANTLR3_ENC_8BIT);
448448
#else
449-
#error "Neiter ANTLR_C_VERSION_3_2 or ANTLR_C_VERSION_3_4 is defined. Could not find the ANTLR 3.x C runtime!"
449+
#error "Neither ANTLR_C_VERSION_3_2 or ANTLR_C_VERSION_3_4 is defined. Could not find the ANTLR 3.x C runtime!"
450450
#endif
451451
if ( input == NULL ) {
452452
return NULL;

0 commit comments

Comments
 (0)