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

Commit 0c3f696

Browse files
adeas31OpenModelica-Hudson
authored andcommitted
Load encrypted package
Checkout the SEMLA and OMEncryption repositories inside the OM super project i.e., at the same level where OMCompiler is. Run the makefile with OMENCRYPTION=yes i.e., `make -f Makefile.omdev.mingw OMENCRYPTION=yes` on Windows. Configure on Linux `./configure --with-ENCRYPTION`. loadEncryptedPackage API takes a .mol file and decrypts the package and load it into the AST. Belonging to [master]: - #2212
1 parent 526b822 commit 0c3f696

File tree

15 files changed

+227
-42
lines changed

15 files changed

+227
-42
lines changed

Compiler/FrontEnd/ClassLoader.mo

Lines changed: 41 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,7 @@ public function loadClass
9191
input String modelicaPath;
9292
input Option<String> encoding;
9393
input Boolean requireExactVersion = false;
94+
input Boolean encrypted = false;
9495
output Absyn.Program outProgram;
9596
algorithm
9697
outProgram := matchcontinue (inPath,priorityList,modelicaPath,encoding)
@@ -104,7 +105,7 @@ algorithm
104105
equation
105106
gd = System.groupDelimiter();
106107
mps = System.strtok(mp, gd);
107-
p = loadClassFromMps(classname, priorityList, mps, encoding, requireExactVersion);
108+
p = loadClassFromMps(classname, priorityList, mps, encoding, requireExactVersion, encrypted);
108109
checkOnLoadMessage(p);
109110
then
110111
p;
@@ -113,7 +114,7 @@ algorithm
113114
equation
114115
gd = System.groupDelimiter();
115116
mps = System.strtok(mp, gd);
116-
p = loadClassFromMps(pack, priorityList, mps, encoding, requireExactVersion);
117+
p = loadClassFromMps(pack, priorityList, mps, encoding, requireExactVersion, encrypted);
117118
checkOnLoadMessage(p);
118119
then
119120
p;
@@ -134,6 +135,7 @@ protected function loadClassFromMps
134135
input list<String> mps;
135136
input Option<String> encoding;
136137
input Boolean requireExactVersion = false;
138+
input Boolean encrypted = false;
137139
output Absyn.Program outProgram;
138140
protected
139141
String mp, name, pwd, cmd, version, userLibraries;
@@ -164,7 +166,7 @@ algorithm
164166
end try;
165167
// print("System.getLoadModelPath: " + id + " {" + stringDelimitList(prios,",") + "} " + stringDelimitList(mps,",") + " => " + mp + " " + name + " " + boolString(isDir));
166168
Config.setLanguageStandardFromMSL(name);
167-
cl := loadClassFromMp(id, mp, name, isDir, encoding);
169+
cl := loadClassFromMp(id, mp, name, isDir, encoding, encrypted);
168170
outProgram := Absyn.PROGRAM({cl},Absyn.TOP());
169171
end loadClassFromMps;
170172

@@ -174,6 +176,7 @@ protected function loadClassFromMp
174176
input String name;
175177
input Boolean isDir;
176178
input Option<String> optEncoding;
179+
input Boolean encrypted = false;
177180
output Absyn.Class outClass;
178181
algorithm
179182
outClass := match (id,path,name,isDir,optEncoding)
@@ -204,27 +207,32 @@ algorithm
204207
if Config.getRunningTestsuite() or Config.noProc()==1 then
205208
strategy = STRATEGY_ON_DEMAND(encoding);
206209
else
207-
filenames = getAllFilesFromDirectory(path + pd + name);
210+
filenames = getAllFilesFromDirectory(path + pd + name, encrypted);
208211
// print("Files load in parallel:\n" + stringDelimitList(filenames, "\n") + "\n");
209-
strategy = STRATEGY_HASHTABLE(Parser.parallelParseFiles(filenames, encoding));
212+
strategy = STRATEGY_HASHTABLE(Parser.parallelParseFiles(filenames, encoding, Config.noProc(), encrypted));
210213
end if;
211-
cl = loadCompletePackageFromMp(id, name, path, strategy, Absyn.TOP(), Error.getNumErrorMessages());
214+
cl = loadCompletePackageFromMp(id, name, path, strategy, Absyn.TOP(), Error.getNumErrorMessages(), encrypted);
212215
then
213216
cl;
214217
end match;
215218
end loadClassFromMp;
216219

217220
protected function getAllFilesFromDirectory
218221
input String dir;
222+
input Boolean encrypted;
219223
input list<String> acc = {};
220224
output list<String> files;
221225
protected
222226
list<String> subdirs;
223227
String pd = System.pathDelimiter();
224228
algorithm
225-
files := (dir + pd + "package.mo") :: listAppend(list(dir + pd + f for f in System.moFiles(dir)), acc);
226-
subdirs := list(dir + pd + d for d in List.filter1OnTrue(System.subDirectories(dir), existPackage, dir));
227-
files := List.fold(subdirs, getAllFilesFromDirectory, files);
229+
if encrypted then
230+
files := (dir + pd + "package.moc") :: listAppend(list(dir + pd + f for f in System.mocFiles(dir)), acc);
231+
else
232+
files := (dir + pd + "package.mo") :: listAppend(list(dir + pd + f for f in System.moFiles(dir)), acc);
233+
end if;
234+
subdirs := list(dir + pd + d for d in List.filter2OnTrue(System.subDirectories(dir), existPackage, dir, encrypted));
235+
files := List.fold1(subdirs, getAllFilesFromDirectory, encrypted, files);
228236
end getAllFilesFromDirectory;
229237

230238
protected function loadCompletePackageFromMp
@@ -235,6 +243,7 @@ protected function loadCompletePackageFromMp
235243
input LoadFileStrategy strategy;
236244
input Absyn.Within inWithin;
237245
input Integer numError;
246+
input Boolean encrypted = false;
238247
output Absyn.Class cl;
239248
algorithm
240249
cl := matchcontinue (id,inIdent,inString,inWithin)
@@ -256,7 +265,7 @@ algorithm
256265
equation
257266
pd = System.pathDelimiter();
258267
mp_1 = stringAppendList({mp,pd,pack});
259-
packagefile = stringAppendList({mp_1,pd,"package.mo"});
268+
packagefile = stringAppendList({mp_1,pd,if encrypted then "package.moc" else "package.mo"});
260269
orderfile = stringAppendList({mp_1,pd,"package.order"});
261270
if not System.regularFileExists(packagefile) then
262271
Error.addInternalError("Expected file " + packagefile + " to exist", sourceInfo());
@@ -265,10 +274,10 @@ algorithm
265274
// print("Look for " + packagefile + "\n");
266275
(cl as Absyn.CLASS(name,pp,fp,ep,r,Absyn.PARTS(tv,ca,cp,ann,cmt),info)) = parsePackageFile(packagefile, strategy, true, within_, id);
267276
// print("Got " + packagefile + "\n");
268-
reverseOrder = getPackageContentNames(cl, orderfile, mp_1, Error.getNumErrorMessages());
277+
reverseOrder = getPackageContentNames(cl, orderfile, mp_1, Error.getNumErrorMessages(), encrypted);
269278
path = Absyn.joinWithinPath(within_,Absyn.IDENT(id));
270279
w2 = Absyn.WITHIN(path);
271-
cp = List.fold3(reverseOrder, loadCompletePackageFromMp2, mp_1, strategy, w2, {});
280+
cp = List.fold4(reverseOrder, loadCompletePackageFromMp2, mp_1, strategy, w2, encrypted, {});
272281
then Absyn.CLASS(name,pp,fp,ep,r,Absyn.PARTS(tv,ca,cp,ann,cmt),info);
273282
case (_,pack,mp,_)
274283
equation
@@ -305,6 +314,7 @@ protected function loadCompletePackageFromMp2
305314
input String mp;
306315
input LoadFileStrategy strategy;
307316
input Absyn.Within w1 "With the parent added";
317+
input Boolean encrypted = false;
308318
input list<Absyn.ClassPart> acc;
309319
output list<Absyn.ClassPart> cps;
310320
algorithm
@@ -334,14 +344,14 @@ algorithm
334344
case CLASSLOAD(id)
335345
equation
336346
pd = System.pathDelimiter();
337-
file = mp + pd + id + "/package.mo";
347+
file = mp + pd + id + (if encrypted then "/package.moc" else "/package.mo");
338348
bDirectoryAndFileExists = System.directoryExists(mp + pd + id) and System.regularFileExists(file);
339349
if bDirectoryAndFileExists then
340-
cl = loadCompletePackageFromMp(id,id,mp,strategy,w1,Error.getNumErrorMessages());
350+
cl = loadCompletePackageFromMp(id,id,mp,strategy,w1,Error.getNumErrorMessages(),encrypted);
341351
ei = Absyn.makeClassElement(cl);
342352
cps = mergeBefore(Absyn.PUBLIC({ei}),acc);
343353
else
344-
file = mp + pd + id + ".mo";
354+
file = mp + pd + id + (if encrypted then ".moc" else ".mo");
345355
if not System.regularFileExists(file) then
346356
Error.addInternalError("Expected file " + file + " to exist", sourceInfo());
347357
fail();
@@ -408,6 +418,7 @@ protected function getPackageContentNames
408418
input String filename;
409419
input String mp;
410420
input Integer numError;
421+
input Boolean encrypted = false;
411422
output list<PackageOrder> po "reverse";
412423
algorithm
413424
(po) := matchcontinue (cl,filename,mp,numError)
@@ -428,11 +439,16 @@ algorithm
428439
duplicatesStr = stringDelimitList(duplicates, ", ");
429440
Error.assertionOrAddSourceMessage(listEmpty(duplicates),Error.PACKAGE_ORDER_DUPLICATES,{duplicatesStr},SOURCEINFO(filename,true,0,0,0,0,0.0));
430441

431-
// get all the .mo files in the directory!
432-
mofiles = List.map(System.moFiles(mp), Util.removeLast3Char);
433-
// get all the subdirs containing package.mo
442+
if encrypted then
443+
// get all the .moc files in the directory!
444+
mofiles = List.map(System.mocFiles(mp), Util.removeLast4Char);
445+
else
446+
// get all the .mo files in the directory!
447+
mofiles = List.map(System.moFiles(mp), Util.removeLast3Char);
448+
end if;
449+
// get all the subdirs
434450
subdirs = System.subDirectories(mp);
435-
subdirs = List.filter1OnTrue(subdirs, existPackage, mp);
451+
subdirs = List.filter2OnTrue(subdirs, existPackage, mp, encrypted);
436452
// build a list
437453
intersection = List.intersectionOnTrue(subdirs,mofiles,stringEq);
438454
differencesStr = stringDelimitList(List.map1(intersection, getBothPackageAndFilename, mp), ", ");
@@ -444,15 +460,15 @@ algorithm
444460
differencesStr = stringDelimitList(differences, "\n\t");
445461
Error.assertionOrAddSourceMessage(listEmpty(differences),Error.PACKAGE_ORDER_FILE_NOT_COMPLETE,{differencesStr},SOURCEINFO(filename,true,0,0,0,0,0.0));
446462
po1 = getPackageContentNamesinParts(namesToFind,cp,{});
447-
List.map2_0(po1,checkPackageOrderFilesExist,mp,info);
463+
List.map3_0(po1,checkPackageOrderFilesExist,mp,info,encrypted);
448464

449465
po2 = List.map(differences, makeClassLoad);
450466

451467
po = listAppend(po2, po1);
452468
else // file not found
453469
mofiles = List.map(System.moFiles(mp), Util.removeLast3Char) "Here .mo files in same directory as package.mo should be loaded as sub-packages";
454470
subdirs = System.subDirectories(mp);
455-
subdirs = List.filter1OnTrue(subdirs, existPackage, mp);
471+
subdirs = List.filter2OnTrue(subdirs, existPackage, mp, encrypted);
456472
mofiles = List.sort(listAppend(subdirs,mofiles), Util.strcmpBool);
457473
// Look for duplicates
458474
intersection = List.sortedDuplicates(mofiles,stringEq);
@@ -499,14 +515,15 @@ protected function checkPackageOrderFilesExist
499515
input PackageOrder po;
500516
input String mp;
501517
input SourceInfo info;
518+
input Boolean encrypted = false;
502519
algorithm
503520
_ := match (po,mp,info)
504521
local
505522
String pd,str;
506523
case (CLASSLOAD(str),_,_)
507524
equation
508525
pd = System.pathDelimiter();
509-
Error.assertionOrAddSourceMessage(System.directoryExists(mp + pd + str) or System.regularFileExists(mp + pd + str + ".mo"),Error.PACKAGE_ORDER_FILE_NOT_FOUND,{str},info);
526+
Error.assertionOrAddSourceMessage(System.directoryExists(mp + pd + str) or System.regularFileExists(mp + pd + str + (if encrypted then ".moc" else ".mo")),Error.PACKAGE_ORDER_FILE_NOT_FOUND,{str},info);
510527
then ();
511528
else ();
512529
end match;
@@ -515,12 +532,13 @@ end checkPackageOrderFilesExist;
515532
protected function existPackage
516533
input String name;
517534
input String mp;
535+
input Boolean encrypted = false;
518536
output Boolean b;
519537
protected
520538
String pd;
521539
algorithm
522540
pd := System.pathDelimiter();
523-
b := System.regularFileExists(mp + pd + name + pd + "package.mo");
541+
b := System.regularFileExists(mp + pd + name + pd + (if encrypted then "package.moc" else "package.mo"));
524542
end existPackage;
525543
526544
protected function getPackageContentNamesinParts

Compiler/FrontEnd/ModelicaBuiltin.mo

Lines changed: 10 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 loadEncryptedPackage
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 Boolean success;
1163+
external "builtin";
1164+
annotation(Documentation(info="<html>
1165+
<p>Loads the given encrypted package.</p>
1166+
</html>"), preferredView="text");
1167+
end loadEncryptedPackage;
1168+
11591169
function reloadClass "reloads the file associated with the given (loaded class)"
11601170
input TypeName name;
11611171
input String encoding = "UTF-8";

Compiler/FrontEnd/Parser.mo

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -122,11 +122,12 @@ function parallelParseFiles
122122
input list<String> filenames;
123123
input String encoding;
124124
input Integer numThreads = Config.noProc();
125+
input Boolean encrypted = false;
125126
output HashTableStringToProgram.HashTable ht;
126127
protected
127128
list<ParserResult> partialResults;
128129
algorithm
129-
partialResults := parallelParseFilesWork(filenames, encoding, numThreads);
130+
partialResults := parallelParseFilesWork(filenames, encoding, numThreads, encrypted);
130131
ht := HashTableStringToProgram.emptyHashTableSized(Util.nextPrime(listLength(partialResults)));
131132
for res in partialResults loop
132133
ht := match res
@@ -167,11 +168,12 @@ function parallelParseFilesWork
167168
input list<String> filenames;
168169
input String encoding;
169170
input Integer numThreads;
171+
input Boolean encrypted = false;
170172
output list<ParserResult> partialResults;
171173
protected
172174
list<tuple<String,String>> workList = list((file,encoding) for file in filenames);
173175
algorithm
174-
if Config.getRunningTestsuite() or Config.noProc()==1 or numThreads == 1 or listLength(filenames)<2 then
176+
if Config.getRunningTestsuite() or Config.noProc()==1 or numThreads == 1 or listLength(filenames)<2 or encrypted then
175177
partialResults := list(loadFileThread(t) for t in workList);
176178
else
177179
// GC.disable(); // Seems to sometimes break building nightly omc

0 commit comments

Comments
 (0)