Line data Source code
1 0 : /*
2 :
3 : OOLegacyScriptWhitelist.h
4 :
5 : Functions to apply method whitelist and basic tokenization to legacy scripts.
6 :
7 :
8 : A sanitized script is an array of zero or more sanitized statements.
9 :
10 : A sanitized statement is an array whose first element is a boolean indicating
11 : whether it's a conditional statement (true) or an action statement (false).
12 :
13 : A conditional statement has three additional elements. The first is an array
14 : of sanitized conditions (see below). The second is a sanitized script to
15 : execute if the condition evaluates to true. The third is a sanitized script to
16 : execute if the condition evaluates to false.
17 :
18 : An action statement has one or two additional elements, both strings. The
19 : first is a selector. If the selector ends with a colon (i.e., takes an
20 : argument), the second is the argument.
21 :
22 :
23 : A sanitized condition is an array of the form:
24 : (opType, rawString, selector, comparisonType, operandArray).
25 :
26 : opType and comparisonType are NSNumbers containing OOOperationType and
27 : OOComparisonType enumerators, respectively.
28 :
29 : rawString is the original textual representation of the condition for
30 : display purposes.
31 :
32 : selector is a string, either a method selector or a mission/local
33 : variable name.
34 :
35 : operandArray is an array of operands. Each operand is itself an array
36 : of two items: a boolean indicating whether it's a method selector
37 : (true) or a string (false), and a string.
38 :
39 : The special opType OP_FALSE doesn't require any other elements in the
40 : array. All other valid opTypes require the array to have five elements.
41 :
42 :
43 : A complete example: given the following script (the Cloaking Device mission
44 : script from Oolite 1.65):
45 : (
46 : {
47 : conditions = (
48 : "galaxy_number equal 4",
49 : "status_string equal STATUS_EXITING_WITCHSPACE",
50 : "mission_cloak undefined"
51 : );
52 : do = (
53 : {
54 : conditions = ("mission_cloakcounter undefined");
55 : do = ("set: mission_cloakcounter 0");
56 : },
57 : "increment: mission_cloakcounter",
58 : "checkForShips: asp-cloaked",
59 : {
60 : conditions = ("shipsFound_number equal 0", "mission_cloakcounter greaterthan 6");
61 : do = ("addShips: asp-cloaked 1", "addShips: asp-pirate 2");
62 : }
63 : );
64 : }
65 : )
66 : the sanitized form (with rawString values replaced with "..." for simplicity) is:
67 : (
68 : (
69 : true, // This is a conditonal statement
70 : ( // conditions
71 : (OP_NUMBER, "...", "galaxy_number", COMPARISON_EQUAL, ((false, "4"))),
72 : (OP_STRING, "...", "status_string", COMPARISON_EQUAL, ((false, "STATUS_EXITING_WITCHSPACE"))),
73 : (OP_MISSION_VAR, "...", "mission_cloak", COMPARISON_UNDEFINED, ())
74 : ),
75 : ( // do
76 : (
77 : true,
78 : ( (OP_MISSION_VAR, "...", "mission_cloakcounter", COMPARISON_UNDEFINED, ()) ),
79 : ( (false, "set:", "mission_cloakcounter 0") ),
80 : ()
81 : ),
82 : (false, "increment:", "mission_cloakcounter"),
83 : (false, "checkForShips:", "asp-cloaked"),
84 : (true,
85 : (
86 : (OP_NUMBER, "...", "shipsFound_number", COMPARISON_EQUAL, ((false, "0"))),
87 : (OP_MISSION_VAR, "...", "mission_cloakcounter, COMPARISON_GREATERTHAN, ((false, "6"))),
88 : ),
89 : (
90 : (false, "addShips:", "asp-cloaked 1"),
91 : (false, "addShips:", "asp-pirate 2"),
92 : ),
93 : ()
94 : )
95 : ),
96 : () // else
97 : )
98 : )
99 :
100 :
101 : Oolite
102 : Copyright (C) 2004-2013 Giles C Williams and contributors
103 :
104 : This program is free software; you can redistribute it and/or
105 : modify it under the terms of the GNU General Public License
106 : as published by the Free Software Foundation; either version 2
107 : of the License, or (at your option) any later version.
108 :
109 : This program is distributed in the hope that it will be useful,
110 : but WITHOUT ANY WARRANTY; without even the implied warranty of
111 : MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
112 : GNU General Public License for more details.
113 :
114 : You should have received a copy of the GNU General Public License
115 : along with this program; if not, write to the Free Software
116 : Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
117 : MA 02110-1301, USA.
118 :
119 : */
120 :
121 : #import "OOCocoa.h"
122 :
123 :
124 : // context is used for error messages.
125 0 : NSArray *OOSanitizeLegacyScript(NSArray *script, NSString *context, BOOL allowAIMethods);
126 0 : NSArray *OOSanitizeLegacyScriptConditions(NSArray *conditions, NSString *context);
127 :
128 :
129 : /* Quick test of whether a conditions array is sanitized. It is assumed that
130 : this will only be passed fully-sanitized or fully-unsanitized conditions
131 : arrays, so the test doesn't need to be exhaustive.
132 :
133 : Note that OOLegacyConditionsAreSanitized() is *not* called by
134 : OOSanitizeLegacyScript(), so that it is not possible to sneak an
135 : unwhitelisted "pre-compiled" condition past it.
136 : */
137 0 : BOOL OOLegacyConditionsAreSanitized(NSArray *conditions);
|