OpenVAS Scanner 23.43.1
exec.h File Reference
#include "nasl_lex_ctxt.h"
Include dependency graph for exec.h:
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Functions

tree_cellnasl_exec (lex_ctxt *, tree_cell *)
 Execute a parse tree.
long int cell_cmp (lex_ctxt *, tree_cell *, tree_cell *)
tree_cellcell2atom (lex_ctxt *, tree_cell *)

Function Documentation

◆ cell2atom()

tree_cell * cell2atom ( lex_ctxt * lexic,
tree_cell * c1 )
Returns
A 'referenced' cell.

Definition at line 195 of file exec.c.

196{
197 tree_cell *c2 = NULL, *ret = NULL;
198 if (c1 == NULL || c1 == FAKE_CELL)
199 return c1;
200
201 switch (c1->type)
202 {
203 case CONST_INT:
204 case CONST_STR:
205 case CONST_DATA:
206 case REF_ARRAY:
207 case DYN_ARRAY:
208 ref_cell (c1);
209 return c1;
210 default:
211 c2 = nasl_exec (lexic, c1);
212 ret = cell2atom (lexic, c2);
213 deref_cell (c2);
214 return ret;
215 }
216}
tree_cell * cell2atom(lex_ctxt *lexic, tree_cell *c1)
Definition exec.c:195
tree_cell * nasl_exec(lex_ctxt *lexic, tree_cell *st)
Execute a parse tree.
Definition exec.c:771
void ref_cell(tree_cell *c)
Definition nasl_tree.c:164
void deref_cell(tree_cell *c)
Definition nasl_tree.c:178
@ CONST_DATA
Definition nasl_tree.h:82
@ DYN_ARRAY
Definition nasl_tree.h:90
@ CONST_STR
Definition nasl_tree.h:80
@ CONST_INT
Definition nasl_tree.h:79
@ REF_ARRAY
Definition nasl_tree.h:89
struct TC tree_cell
#define FAKE_CELL
Definition nasl_tree.h:110
short type
Definition nasl_tree.h:95

◆ cell_cmp()

long int cell_cmp ( lex_ctxt * lexic,
tree_cell * c1,
tree_cell * c2 )

Definition at line 219 of file exec.c.

220{
221 int flag, typ, typ1, typ2;
222 long int x1, x2;
223 char *s1, *s2;
224 int len_s1, len_s2, len_min;
225
226 if (c1 == NULL || c1 == FAKE_CELL)
227 nasl_perror (lexic, "cell_cmp: c1 == NULL !\n");
228 if (c2 == NULL || c2 == FAKE_CELL)
229 nasl_perror (lexic, "cell_cmp: c2 == NULL !\n");
230
231 /* We first convert the cell to atomic types. */
232 c1 = cell2atom (lexic, c1);
233 c2 = cell2atom (lexic, c2);
234
235 /*
236 * Comparing anything to something else which is entirely different
237 * may lead to unpredictable results.
238 * Here are the rules:
239 * 1. No problem with same types, although we do not compare arrays yet
240 * 2. No problem with CONST_DATA / CONST_STR
241 * 3. When an integer is compared to a string, the integer is converted
242 * 4. When NULL is compared to an integer, it is converted to 0
243 * 5. When NULL is compared to a string, it is converted to ""
244 * 6. NULL is "smaller" than anything else (i.e. an array)
245 * Anything else is an error
246 */
247 typ1 = cell_type (c1);
248 typ2 = cell_type (c2);
249
250 if (typ1 == 0 && typ2 == 0) /* Two NULL */
251 {
252 deref_cell (c1);
253 deref_cell (c2);
254 return 0;
255 }
256
257 if (typ1 == typ2) /* Same type, no problem */
258 typ = typ1;
259 else if ((typ1 == CONST_DATA || typ1 == CONST_STR)
260 && (typ2 == CONST_DATA || typ2 == CONST_STR))
261 typ = CONST_DATA; /* Same type in fact (string) */
262 /* We convert an integer into a string before compare */
263 else if ((typ1 == CONST_INT && (typ2 == CONST_DATA || typ2 == CONST_STR))
264 || (typ2 == CONST_INT && (typ1 == CONST_DATA || typ1 == CONST_STR)))
265 typ = CONST_DATA;
266 else if (typ1 == 0) /* 1st argument is null */
267 if (typ2 == CONST_INT || typ2 == CONST_DATA || typ2 == CONST_STR)
268 typ = typ2; /* We convert it to 0 or "" */
269 else
270 {
271 deref_cell (c1);
272 deref_cell (c2);
273 return -1; /* NULL is smaller than anything else */
274 }
275 else if (typ2 == 0) /* 2nd argument is null */
276 if (typ1 == CONST_INT || typ1 == CONST_DATA || typ1 == CONST_STR)
277 typ = typ1; /* We convert it to 0 or "" */
278 else
279 {
280 deref_cell (c1);
281 deref_cell (c2);
282 return 1; /* Anything else is greater than NULL */
283 }
284 else
285 {
286 gchar *n1, *n2;
287
288 n1 = cell2str (lexic, c1);
289 n2 = cell2str (lexic, c2);
290 nasl_perror (lexic,
291 "cell_cmp: comparing '%s' of type %s and '%s' of "
292 "type %s does not make sense\n",
293 n1, nasl_type_name (typ1), n2, nasl_type_name (typ2));
294 g_free (n1);
295 g_free (n2);
296 deref_cell (c1);
297 deref_cell (c2);
298 return 0;
299 }
300
301 switch (typ)
302 {
303 case CONST_INT:
304 x1 = cell2int (lexic, c1);
305 x2 = cell2int (lexic, c2);
306 deref_cell (c1);
307 deref_cell (c2);
308 return x1 - x2;
309
310 case CONST_STR:
311 case CONST_DATA:
312 s1 = cell2str (lexic, c1);
313 if (typ1 == CONST_STR || typ1 == CONST_DATA)
314 len_s1 = c1->size;
315 else if (s1 == NULL)
316 len_s1 = 0;
317 else
318 len_s1 = strlen (s1);
319
320 s2 = cell2str (lexic, c2);
321 if (typ2 == CONST_STR || typ2 == CONST_DATA)
322 len_s2 = c2->size;
323 else if (s2 == NULL)
324 len_s2 = 0;
325 else
326 len_s2 = strlen (s2);
327
328 len_min = len_s1 < len_s2 ? len_s1 : len_s2;
329 flag = 0;
330
331 if (len_min > 0)
332 flag = memcmp (s1, s2, len_min);
333 if (flag == 0)
334 flag = len_s1 - len_s2;
335
336 g_free (s1);
337 g_free (s2);
338 deref_cell (c1);
339 deref_cell (c2);
340 return flag;
341
342 case REF_ARRAY:
343 case DYN_ARRAY:
344 g_message ("cell_cmp: cannot compare arrays yet");
345 deref_cell (c1);
346 deref_cell (c2);
347 return 0;
348
349 default:
350 g_message ("cell_cmp: don't known how to compare %s and %s",
351 nasl_type_name (typ1), nasl_type_name (typ2));
352 deref_cell (c1);
353 deref_cell (c2);
354 return 0;
355 }
356}
static long int cell2int(lex_ctxt *lexic, tree_cell *c)
Definition exec.c:125
static char * cell2str(lex_ctxt *lexic, tree_cell *c)
Definition exec.c:151
void nasl_perror(lex_ctxt *lexic, char *msg,...)
Definition nasl_debug.c:105
int cell_type(const tree_cell *c)
Definition nasl_tree.c:404
const char * nasl_type_name(int t)
Definition nasl_tree.c:343
long int size
Definition nasl_tree.h:99

References cell2atom(), cell2int(), cell2str(), cell_type(), CONST_DATA, CONST_INT, CONST_STR, deref_cell(), DYN_ARRAY, FAKE_CELL, nasl_perror(), nasl_type_name(), REF_ARRAY, and TC::size.

Referenced by nasl_exec(), and var_cmp().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ nasl_exec()

tree_cell * nasl_exec ( lex_ctxt * lexic,
tree_cell * st )

Execute a parse tree.

Todo
There is a lot of duplicated code in following cases, could be refactored.

Definition at line 771 of file exec.c.

772{
773 tree_cell *ret = NULL, *ret2 = NULL, *tc1 = NULL, *tc2 = NULL, *tc3 = NULL,
774 *idx = NULL, *args;
775 int flag, z;
776 char *s1 = NULL, *s2 = NULL, *s3 = NULL, *p = NULL;
777 char *p1, *p2;
778 int len1, len2;
779 nasl_func *pf = NULL;
780 long int x, y, n;
781 int i, lint_mode = 0;
782
783 if (st)
784 if (st->line_nb != 0)
785 lexic->line_nb = st->line_nb;
786 /* return */
787 if (lexic->ret_val != NULL)
788 {
789 ref_cell (lexic->ret_val);
790 return lexic->ret_val;
791 }
792
793 /* break or continue */
794 if (lexic->break_flag || lexic->cont_flag)
795 return FAKE_CELL;
796
797 if (st == FAKE_CELL)
798 return FAKE_CELL;
799
800 if (st == NULL)
801 {
802 return NULL;
803 }
804
805 if (nasl_trace_fp != NULL)
807
808 switch (st->type)
809 {
810 case NODE_IF_ELSE:
811 ret = nasl_exec (lexic, st->link[0]);
812#ifdef STOP_AT_FIRST_ERROR
813 if (ret == NULL)
814 return NULL;
815#endif
816 if (cell2bool (lexic, ret))
817 ret2 = nasl_exec (lexic, st->link[1]);
818 else if (st->link[2] != NULL) /* else branch */
819 ret2 = nasl_exec (lexic, st->link[2]);
820 else /* No else */
821 ret2 = FAKE_CELL;
822 deref_cell (ret);
823 return ret2;
824
825 case NODE_INSTR_L: /* Block. [0] = first instr, [1] = tail */
826 ret = nasl_exec (lexic, st->link[0]);
827 if (st->link[1] == NULL || lexic->break_flag || lexic->cont_flag)
828 return ret;
829 deref_cell (ret);
830 ret = nasl_exec (lexic, st->link[1]);
831 return ret;
832
833 case NODE_FOR:
834 /* [0] = start expr, [1] = cond, [2] = end_expr, [3] = block */
835 ret2 = nasl_exec (lexic, st->link[0]);
836#ifdef STOP_AT_FIRST_ERROR
837 if (ret2 == NULL)
838 return NULL;
839#endif
840 deref_cell (ret2);
841 for (;;)
842 {
843 /* Break the loop if 'return' */
844 if (lexic->ret_val != NULL)
845 {
846 ref_cell (lexic->ret_val);
847 return lexic->ret_val;
848 }
849
850 /* condition */
851 if ((ret = nasl_exec (lexic, st->link[1])) == NULL)
852 return NULL; /* We can return here, as NULL is false */
853 flag = cell2bool (lexic, ret);
854 deref_cell (ret);
855 if (!flag)
856 break;
857 /* block */
858 ret = nasl_exec (lexic, st->link[3]);
859#ifdef STOP_AT_FIRST_ERROR
860 if (ret == NULL)
861 return NULL;
862#endif
863 deref_cell (ret);
864
865 /* break */
866 if (lexic->break_flag)
867 {
868 lexic->break_flag = 0;
869 return FAKE_CELL;
870 }
871
872 lexic->cont_flag = 0; /* No need to test if set */
873
874 /* end expression */
875 ret = nasl_exec (lexic, st->link[2]);
876#ifdef STOP_AT_FIRST_ERROR
877 if (ret == NULL)
878 return NULL;
879#endif
880 deref_cell (ret);
881 }
882 return FAKE_CELL;
883
884 case NODE_WHILE:
885 /* [0] = cond, [1] = block */
886 for (;;)
887 {
888 /* return? */
889 if (lexic->ret_val != NULL)
890 {
891 ref_cell (lexic->ret_val);
892 return lexic->ret_val;
893 }
894 /* Condition */
895 if ((ret = nasl_exec (lexic, st->link[0])) == NULL)
896 return NULL; /* NULL is false */
897 flag = cell2bool (lexic, ret);
898 deref_cell (ret);
899 if (!flag)
900 break;
901 /* Block */
902 ret = nasl_exec (lexic, st->link[1]);
903#ifdef STOP_AT_FIRST_ERROR
904 if (ret == NULL)
905 return NULL;
906#endif
907 deref_cell (ret);
908
909 /* break */
910 if (lexic->break_flag)
911 {
912 lexic->break_flag = 0;
913 return FAKE_CELL;
914 }
915 lexic->cont_flag = 0;
916 }
917 return FAKE_CELL;
918
920 /* [0] = block, [1] = cond */
921 for (;;)
922 {
923 /* return? */
924 if (lexic->ret_val != NULL)
925 {
926 ref_cell (lexic->ret_val);
927 return lexic->ret_val;
928 }
929 /* Block */
930 ret = nasl_exec (lexic, st->link[0]);
931#ifdef STOP_AT_FIRST_ERROR
932 if (ret == NULL)
933 return NULL;
934#endif
935 deref_cell (ret);
936
937 /* break */
938 if (lexic->break_flag)
939 {
940 lexic->break_flag = 0;
941 return FAKE_CELL;
942 }
943 lexic->cont_flag = 0;
944
945 /* Condition */
946 ret = nasl_exec (lexic, st->link[1]);
947#ifdef STOP_AT_FIRST_ERROR
948 if (ret == NULL)
949 return NULL;
950#endif
951 flag = cell2bool (lexic, ret);
952 deref_cell (ret);
953 if (flag)
954 break;
955 }
956 return FAKE_CELL;
957
958 case NODE_FOREACH:
959 /* str_val = index name, [0] = array, [1] = block */
960 {
961 nasl_iterator ai;
962 tree_cell *v, *a, *val;
963
964 v = get_variable_by_name (lexic, st->x.str_val);
965 if (v == NULL)
966 return NULL; /* We cannot go on if we have no variable to iterate */
967 a = nasl_exec (lexic, st->link[0]);
968 ai = nasl_array_iterator (lexic, a);
969 while ((val = nasl_iterate_array (&ai)) != NULL)
970 {
971 tc1 = nasl_affect (v, val);
972 ret = nasl_exec (lexic, st->link[1]);
973 deref_cell (val);
974 deref_cell (tc1);
975#ifdef STOP_AT_FIRST_ERROR
976 if (ret == NULL)
977 break;
978#endif
979 deref_cell (ret);
980
981 /* return */
982 if (lexic->ret_val != NULL)
983 break;
984 /* break */
985 if (lexic->break_flag)
986 {
987 lexic->break_flag = 0;
988 break;
989 }
990 lexic->cont_flag = 0;
991 }
992 free_array (ai.a);
993 g_free (ai.a);
994 deref_cell (a);
995 deref_cell (v);
996 }
997 return FAKE_CELL;
998
999 case NODE_FUN_DEF:
1000 /* x.str_val = function name, [0] = argdecl, [1] = block */
1001 /* 3rd arg is only for lint. Hier is always 0 */
1002 ret = decl_nasl_func (lexic, st, lint_mode);
1003 return ret;
1004
1005 case NODE_FUN_CALL:
1006 pf = get_func_ref_by_name (lexic, st->x.str_val);
1007 if (pf == NULL)
1008 {
1009 nasl_perror (lexic, "Undefined function '%s'\n", st->x.str_val);
1010 return NULL;
1011 }
1012 args = st->link[0];
1013 ret = nasl_func_call (lexic, pf, args);
1014 return ret;
1015
1016 case NODE_REPEATED:
1017 n = cell2intW (lexic, st->link[1]);
1018 if (n <= 0)
1019 return NULL;
1020
1021#ifdef STOP_AT_FIRST_ERROR
1022 for (tc1 = NULL, i = 1; i <= n; i++)
1023 {
1024 deref_cell (tc1);
1025 if ((tc1 = nasl_exec (lexic, st->link[0])) == NULL)
1026 return NULL;
1027 }
1028 return tc1;
1029#else
1030 for (i = 1; i <= n; i++)
1031 {
1032 tc1 = nasl_exec (lexic, st->link[0]);
1033 deref_cell (tc1);
1034 }
1035 return FAKE_CELL;
1036#endif
1037
1038 /*
1039 * I wonder...
1040 * Will nasl_exec be really called with NODE_EXEC or NODE_ARG?
1041 */
1042 case NODE_DECL: /* Used in function declarations */
1043 /* [0] = next arg in list */
1044 /* TBD? */
1045 return st; /* ? */
1046
1047 case NODE_ARG: /* Used function calls */
1048 /* val = name can be NULL, [0] = val, [1] = next arg */
1049 ret = nasl_exec (lexic, st->link[0]); /* Is this wise? */
1050 return ret;
1051
1052 case NODE_RETURN:
1053 /* [0] = ret val */
1054 ret = nasl_return (lexic, st->link[0]);
1055 return ret;
1056
1057 case NODE_BREAK:
1058 lexic->break_flag = 1;
1059 return FAKE_CELL;
1060
1061 case NODE_CONTINUE:
1062 lexic->cont_flag = 1;
1063 return FAKE_CELL;
1064
1065 case NODE_ARRAY_EL: /* val = array name, [0] = index */
1066 idx = cell2atom (lexic, st->link[0]);
1067 ret = get_array_elem (lexic, st->x.str_val, idx);
1068 deref_cell (idx);
1069 return ret;
1070
1073 case NODE_AFF:
1074 /* [0] = lvalue, [1] = rvalue */
1075 tc1 = nasl_exec (lexic, st->link[0]);
1076 tc2 = nasl_exec (lexic, st->link[1]);
1077 ret = nasl_affect (tc1, tc2);
1078 deref_cell (tc1); /* Must free VAR_REF */
1079 deref_cell (ret);
1080 return tc2; /* So that "a = b = e;" works */
1081
1082 case NODE_PLUS_EQ:
1083 tc1 = nasl_exec (lexic, st->link[0]);
1084 tc2 = nasl_exec (lexic, st->link[1]);
1085 tc3 = alloc_expr_cell (0, EXPR_PLUS, tc1, tc2);
1086 ret2 = nasl_exec (lexic, tc3);
1087 ret = nasl_affect (tc1, ret2);
1088 deref_cell (tc3); /* Frees tc1 and tc2 */
1089 deref_cell (ret);
1090 return ret2; /* So that "a = b += e;" works */
1091
1092 case NODE_MINUS_EQ:
1093 tc1 = nasl_exec (lexic, st->link[0]);
1094 tc2 = nasl_exec (lexic, st->link[1]);
1095 tc3 = alloc_expr_cell (0, EXPR_MINUS, tc1, tc2);
1096 ret2 = nasl_exec (lexic, tc3);
1097 ret = nasl_affect (tc1, ret2);
1098 deref_cell (tc3); /* Frees tc1 and tc2 */
1099 deref_cell (ret);
1100 return ret2; /* So that "a = b -= e;" works */
1101
1102 case NODE_MULT_EQ:
1103 tc1 = nasl_exec (lexic, st->link[0]);
1104 tc2 = nasl_exec (lexic, st->link[1]);
1105 tc3 = alloc_expr_cell (0, EXPR_MULT, tc1, tc2);
1106 ret2 = nasl_exec (lexic, tc3);
1107 ret = nasl_affect (tc1, ret2);
1108 deref_cell (tc3); /* Frees tc1 and tc2 */
1109 deref_cell (ret);
1110 return ret2;
1111
1112 case NODE_DIV_EQ:
1113 tc1 = nasl_exec (lexic, st->link[0]);
1114 tc2 = nasl_exec (lexic, st->link[1]);
1115 tc3 = alloc_expr_cell (0, EXPR_DIV, tc1, tc2);
1116 ret2 = nasl_exec (lexic, tc3);
1117 ret = nasl_affect (tc1, ret2);
1118 deref_cell (tc3); /* Frees tc1 and tc2 */
1119 deref_cell (ret);
1120 return ret2;
1121
1122 case NODE_MODULO_EQ:
1123 tc1 = nasl_exec (lexic, st->link[0]);
1124 tc2 = nasl_exec (lexic, st->link[1]);
1125 tc3 = alloc_expr_cell (0, EXPR_MODULO, tc1, tc2);
1126 ret2 = nasl_exec (lexic, tc3);
1127 ret = nasl_affect (tc1, ret2);
1128 deref_cell (tc3); /* Frees tc1 and tc2 */
1129 deref_cell (ret);
1130 return ret2;
1131
1132 case NODE_L_SHIFT_EQ:
1133 tc1 = nasl_exec (lexic, st->link[0]);
1134 tc2 = nasl_exec (lexic, st->link[1]);
1135 tc3 = alloc_expr_cell (0, EXPR_L_SHIFT, tc1, tc2);
1136 ret2 = nasl_exec (lexic, tc3);
1137 ret = nasl_affect (tc1, ret2);
1138 deref_cell (tc3); /* Frees tc1 and tc2 */
1139 deref_cell (ret);
1140 return ret2;
1141
1142 case NODE_R_SHIFT_EQ:
1143 tc1 = nasl_exec (lexic, st->link[0]);
1144 tc2 = nasl_exec (lexic, st->link[1]);
1145 tc3 = alloc_expr_cell (0, EXPR_R_SHIFT, tc1, tc2);
1146 ret2 = nasl_exec (lexic, tc3);
1147 ret = nasl_affect (tc1, ret2);
1148 deref_cell (tc3); /* Frees tc1 and tc2 */
1149 deref_cell (ret);
1150 return ret2;
1151
1152 case NODE_R_USHIFT_EQ:
1153 tc1 = nasl_exec (lexic, st->link[0]);
1154 tc2 = nasl_exec (lexic, st->link[1]);
1155 tc3 = alloc_expr_cell (0, EXPR_R_USHIFT, tc1, tc2);
1156 ret2 = nasl_exec (lexic, tc3);
1157 ret = nasl_affect (tc1, ret2);
1158 deref_cell (tc3); /* Frees tc1 and tc2 */
1159 deref_cell (ret);
1160 return ret2;
1161
1162 case NODE_VAR:
1163 /* val = variable name */
1164 ret = get_variable_by_name (lexic, st->x.str_val);
1165 return ret;
1166
1167 case NODE_LOCAL: /* [0] = argdecl */
1168 ret = decl_local_variables (lexic, st->link[0]);
1169 return ret;
1170
1171 case NODE_GLOBAL: /* [0] = argdecl */
1172 ret = decl_global_variables (lexic, st->link[0]);
1173 return ret;
1174
1175 case EXPR_AND:
1176 x = cell2bool (lexic, st->link[0]);
1177 if (!x)
1178 return bool2cell (0);
1179
1180 y = cell2bool (lexic, st->link[1]);
1181 return bool2cell (y);
1182
1183 case EXPR_OR:
1184 x = cell2bool (lexic, st->link[0]);
1185 if (x)
1186 return bool2cell (x);
1187 y = cell2bool (lexic, st->link[1]);
1188 return bool2cell (y);
1189
1190 case EXPR_NOT:
1191 x = cell2bool (lexic, st->link[0]);
1192 return bool2cell (!x);
1193
1194 case EXPR_INCR:
1195 case EXPR_DECR:
1196 x = (st->type == EXPR_INCR) ? 1 : -1;
1197 if (st->link[0] == NULL)
1198 {
1199 y = 1; /* pre */
1200 tc1 = st->link[1];
1201 }
1202 else
1203 {
1204 y = 0; /* post */
1205 tc1 = st->link[0];
1206 }
1207 tc2 = nasl_exec (lexic, tc1);
1208 if (tc2 == NULL)
1209 return NULL;
1210 ret = nasl_incr_variable (lexic, tc2, y, x);
1211 deref_cell (tc2);
1212 return ret;
1213
1214 case EXPR_PLUS:
1215 s1 = s2 = NULL;
1216 tc1 = cell2atom (lexic, st->link[0]);
1217#ifdef STOP_AT_FIRST_ERROR
1218 if (tc1 == NULL || tc1 == FAKE_CELL)
1219 return NULL;
1220#endif
1221 tc2 = cell2atom (lexic, st->link[1]);
1222 if (tc2 == NULL || tc2 == FAKE_CELL)
1223 {
1224#ifdef STOP_AT_FIRST_ERROR
1225 deref_cell (tc1);
1226 return NULL;
1227#else
1228 return tc1;
1229#endif
1230 }
1231
1232 if (tc1 == NULL || tc1 == FAKE_CELL)
1233 return tc2;
1234
1235 /*
1236 * Anything added to a string is converted to a string
1237 * Otherwise anything added to an integer is converted into an integer
1238 */
1239 if (tc1->type == CONST_DATA || tc2->type == CONST_DATA)
1240 flag = CONST_DATA;
1241 else if (tc1->type == CONST_STR || tc2->type == CONST_STR)
1242 flag = CONST_STR;
1243 else if (tc1->type == CONST_INT || tc2->type == CONST_INT)
1244 flag = CONST_INT;
1245 else
1246 flag = NODE_EMPTY;
1247 switch (flag)
1248 {
1249 long sz;
1250 case CONST_INT:
1251 x = tc1->x.i_val;
1252 y = cell2int (lexic, tc2);
1253 ret = int2cell (x + y);
1254 break;
1255
1256 case CONST_STR:
1257 case CONST_DATA:
1258 s1 = s2 = NULL;
1259 if (tc1->type == CONST_STR || tc1->type == CONST_DATA)
1260 len1 = tc1->size;
1261 else
1262 {
1263 s1 = cell2str (lexic, tc1);
1264 len1 = (s1 == NULL ? 0 : strlen (s1));
1265 }
1266
1267 if (tc2->type == CONST_STR || tc2->type == CONST_DATA)
1268 len2 = tc2->size;
1269 else
1270 {
1271 s2 = cell2str (lexic, tc2);
1272 len2 = (s2 == NULL ? 0 : strlen (s2));
1273 }
1274
1275 sz = len1 + len2;
1276 s3 = g_malloc0 (sz + 1);
1277 if (len1 > 0)
1278 memcpy (s3, s1 != NULL ? s1 : tc1->x.str_val, len1);
1279 if (len2 > 0)
1280 memcpy (s3 + len1, s2 != NULL ? s2 : tc2->x.str_val, len2);
1281 g_free (s1);
1282 g_free (s2);
1283 ret = alloc_typed_cell (flag);
1284 ret->x.str_val = s3;
1285 ret->size = sz;
1286 break;
1287
1288 default:
1289 ret = NULL;
1290 break;
1291 }
1292 deref_cell (tc1);
1293 deref_cell (tc2);
1294 return ret;
1295
1296 case EXPR_MINUS: /* Infamous duplicated code */
1297 s1 = s2 = NULL;
1298 tc1 = cell2atom (lexic, st->link[0]);
1299#ifdef STOP_AT_FIRST_ERROR
1300 if (tc1 == NULL || tc1 == FAKE_CELL)
1301 return NULL;
1302#endif
1303 tc2 = cell2atom (lexic, st->link[1]);
1304 if (tc2 == NULL || tc2 == FAKE_CELL)
1305 {
1306#ifdef STOP_AT_FIRST_ERROR
1307 deref_cell (tc1);
1308 return NULL;
1309#else
1310 return tc1;
1311#endif
1312 }
1313
1314 if (tc1 == NULL || tc1 == FAKE_CELL)
1315 {
1316 if (tc2->type == CONST_INT)
1317 {
1318 y = cell2int (lexic, tc2);
1319 ret = int2cell (-y);
1320 }
1321 else
1322 ret = NULL;
1323 deref_cell (tc2);
1324 return ret;
1325 }
1326
1327 /*
1328 * Anything subtracted from a string is converted to a string
1329 * Otherwise anything subtracted from integer is converted into an
1330 * integer
1331 */
1332 if (tc1->type == CONST_DATA || tc2->type == CONST_DATA)
1333 flag = CONST_DATA;
1334 else if (tc1->type == CONST_STR || tc2->type == CONST_STR)
1335 flag = CONST_STR;
1336 else if (tc1->type == CONST_INT || tc2->type == CONST_INT)
1337 flag = CONST_INT;
1338 else
1339 flag = NODE_EMPTY;
1340 switch (flag)
1341 {
1342 case CONST_INT:
1343 x = cell2int (lexic, tc1);
1344 y = cell2int (lexic, tc2);
1345 ret = int2cell (x - y);
1346 break;
1347
1348 case CONST_STR:
1349 case CONST_DATA:
1350 if (tc1->type == CONST_STR || tc1->type == CONST_DATA)
1351 {
1352 p1 = tc1->x.str_val;
1353 len1 = tc1->size;
1354 }
1355 else
1356 {
1357 p1 = s1 = cell2str (lexic, tc1);
1358 len1 = (s1 == NULL ? 0 : strlen (s1));
1359 }
1360
1361 if (tc2->type == CONST_STR || tc2->type == CONST_DATA)
1362 {
1363 p2 = tc2->x.str_val;
1364 len2 = tc2->size;
1365 }
1366 else
1367 {
1368 p2 = s2 = cell2str (lexic, tc2);
1369 len2 = (s2 == NULL ? 0 : strlen (s2));
1370 }
1371
1372 /* if p1 is null, last condition p=memem() will not be evaluated
1373 * and p remains NULL */
1374 if (len2 == 0 || len1 < len2
1375 || (p1 != NULL && (p = memmem (p1, len1, p2, len2)) == NULL))
1376 {
1377 s3 = g_malloc0 (len1 + 1);
1378 if (p1 != NULL)
1379 memcpy (s3, p1, len1);
1380 ret = alloc_typed_cell (flag);
1381 ret->x.str_val = s3;
1382 ret->size = len1;
1383 }
1384 else
1385 {
1386 long sz = len1 - len2;
1387 if (sz <= 0)
1388 {
1389 sz = 0;
1390 s3 = g_strdup ("");
1391 }
1392 else
1393 {
1394 s3 = g_malloc0 (sz + 1);
1395 if (p - p1 > 0)
1396 memcpy (s3, p1, p - p1);
1397 if (p != NULL && sz > p - p1)
1398 memcpy (s3 + (p - p1), p + len2, sz - (p - p1));
1399 }
1400 ret = alloc_typed_cell (flag);
1401 ret->x.str_val = s3;
1402 ret->size = sz;
1403 }
1404
1405 g_free (s1);
1406 g_free (s2);
1407 break;
1408
1409 default:
1410 ret = NULL;
1411 break;
1412 }
1413 deref_cell (tc1);
1414 deref_cell (tc2);
1415 return ret;
1416
1417 case EXPR_MULT:
1418 x = cell2intW (lexic, st->link[0]);
1419 y = cell2intW (lexic, st->link[1]);
1420 return int2cell (x * y);
1421
1422 case EXPR_DIV:
1423 x = cell2intW (lexic, st->link[0]);
1424 y = cell2intW (lexic, st->link[1]);
1425 if (y != 0)
1426 return int2cell (x / y);
1427 else
1428 return int2cell (0);
1429
1430 case EXPR_EXPO:
1431 x = cell2intW (lexic, st->link[0]);
1432 y = cell2intW (lexic, st->link[1]);
1433 return int2cell (expo (x, y));
1434
1435 case EXPR_MODULO:
1436 x = cell2intW (lexic, st->link[0]);
1437 y = cell2intW (lexic, st->link[1]);
1438 if (y != 0)
1439 return int2cell (x % y);
1440 else
1441 return int2cell (0);
1442
1443 case EXPR_BIT_AND:
1444 x = cell2intW (lexic, st->link[0]);
1445 y = cell2intW (lexic, st->link[1]);
1446 return int2cell (x & y);
1447
1448 case EXPR_BIT_OR:
1449 x = cell2intW (lexic, st->link[0]);
1450 y = cell2intW (lexic, st->link[1]);
1451 return int2cell (x | y);
1452
1453 case EXPR_BIT_XOR:
1454 x = cell2intW (lexic, st->link[0]);
1455 y = cell2intW (lexic, st->link[1]);
1456 return int2cell (x ^ y);
1457
1458 case EXPR_BIT_NOT:
1459 x = cell2intW (lexic, st->link[0]);
1460 return int2cell (~x);
1461
1462 case EXPR_U_MINUS:
1463 x = cell2intW (lexic, st->link[0]);
1464 return int2cell (-x);
1465
1466 /* TBD: Handle shift for strings and arrays */
1467 case EXPR_L_SHIFT:
1468 x = cell2intW (lexic, st->link[0]);
1469 y = cell2intW (lexic, st->link[1]);
1470 return int2cell (x << y);
1471
1472 case EXPR_R_SHIFT: /* arithmetic right shift */
1473 x = cell2intW (lexic, st->link[0]);
1474 y = cell2intW (lexic, st->link[1]);
1475 z = x >> y;
1476#ifndef __GNUC__
1477 if (x < 0 && z >= 0) /* Fix it */
1478 z |= (~0) << (sizeof (x) * 8 - y);
1479#endif
1480 return int2cell (z);
1481
1482 case EXPR_R_USHIFT:
1483 x = cell2intW (lexic, st->link[0]);
1484 y = cell2intW (lexic, st->link[1]);
1485 z = (unsigned) x >> (unsigned) y;
1486#ifndef __GNUC__
1487 if (x < 0 && z <= 0) /* Fix it! */
1488 z &= ~((~0) << (sizeof (x) * 8 - y));
1489#endif
1490 return int2cell (z);
1491
1492 case COMP_MATCH:
1493 case COMP_NOMATCH:
1494 tc1 = cell2atom (lexic, st->link[0]);
1495 tc2 = cell2atom (lexic, st->link[1]);
1496 s1 = s2 = NULL;
1497
1498 if (tc1 == NULL || tc1 == FAKE_CELL)
1499 {
1500 p1 = "";
1501 len1 = 0;
1502 }
1503 else if (tc1->type == CONST_STR || tc1->type == CONST_DATA)
1504 {
1505 p1 = tc1->x.str_val;
1506 len1 = tc1->size;
1507 }
1508 else
1509 {
1510 p1 = s1 = cell2str (lexic, tc1);
1511 len1 = strlen (s1);
1512 }
1513
1514 if (tc2 == NULL || tc2 == FAKE_CELL)
1515 {
1516 p2 = "";
1517 len2 = 0;
1518 }
1519 else if (tc2->type == CONST_STR || tc2->type == CONST_DATA)
1520 {
1521 p2 = tc2->x.str_val;
1522 len2 = tc2->size;
1523 }
1524 else
1525 {
1526 p2 = s2 = cell2str (lexic, tc2);
1527 len2 = strlen (s2);
1528 }
1529
1530 if (len1 <= len2)
1531 flag = (memmem (p2, len2, p1, len1) != NULL);
1532 else
1533 flag = 0;
1534
1535 g_free (s1);
1536 g_free (s2);
1537 deref_cell (tc1);
1538 deref_cell (tc2);
1539 if (st->type == COMP_MATCH)
1540 return bool2cell (flag);
1541 else
1542 return bool2cell (!flag);
1543
1544 case COMP_RE_MATCH:
1545 case COMP_RE_NOMATCH:
1546 if (st->x.ref_val == NULL)
1547 {
1548 nasl_perror (lexic, "nasl_exec: bad regex at or near line %d\n",
1549 st->line_nb);
1550 return NULL;
1551 }
1552 s1 = cell2str (lexic, st->link[0]);
1553 if (s1 == NULL)
1554 return 0;
1555 flag = regexec (st->x.ref_val, s1, 0, NULL, 0);
1556 g_free (s1);
1557 if (st->type == COMP_RE_MATCH)
1558 return bool2cell (flag != REG_NOMATCH);
1559 else
1560 return bool2cell (flag == REG_NOMATCH);
1561
1562 case COMP_LT:
1563 return bool2cell (cell_cmp (lexic, st->link[0], st->link[1]) < 0);
1564
1565 case COMP_LE:
1566 return bool2cell (cell_cmp (lexic, st->link[0], st->link[1]) <= 0);
1567
1568 case COMP_EQ:
1569 return bool2cell (cell_cmp (lexic, st->link[0], st->link[1]) == 0);
1570
1571 case COMP_NE:
1572 return bool2cell (cell_cmp (lexic, st->link[0], st->link[1]) != 0);
1573
1574 case COMP_GT:
1575 return bool2cell (cell_cmp (lexic, st->link[0], st->link[1]) > 0);
1576
1577 case COMP_GE:
1578 return bool2cell (cell_cmp (lexic, st->link[0], st->link[1]) >= 0);
1579
1580 case REF_ARRAY:
1581 case DYN_ARRAY:
1582 case CONST_INT:
1583 case CONST_STR:
1584 case CONST_DATA:
1585 ref_cell (st); /* nasl_exec returns a cell that should be deref-ed */
1586 return st;
1587
1588 case REF_VAR:
1589 ret = nasl_read_var_ref (lexic, st);
1590 return ret;
1591
1592 default:
1593 nasl_perror (lexic, "nasl_exec: unhandled node type %d\n", st->type);
1594 abort ();
1595 return NULL;
1596 }
1597}
FILE * nasl_trace_fp
Definition exec.c:358
long int cell_cmp(lex_ctxt *lexic, tree_cell *c1, tree_cell *c2)
Definition exec.c:219
static long int cell2intW(lex_ctxt *lexic, tree_cell *c)
Definition exec.c:131
static void nasl_short_dump(FILE *fp, const tree_cell *c)
Definition exec.c:652
static long int expo(long int x, long int y)
Definition exec.c:746
static tree_cell * bool2cell(long int x)
Definition exec.c:145
static int cell2bool(lex_ctxt *lexic, tree_cell *c)
Definition exec.c:45
static tree_cell * int2cell(long int x)
Definition exec.c:137
tree_cell * nasl_func_call(lex_ctxt *lexic, const nasl_func *f, tree_cell *arg_list)
Definition nasl_func.c:95
tree_cell * decl_nasl_func(lex_ctxt *lexic, tree_cell *decl_node, int lint_mode)
Definition nasl_func.c:66
nasl_func * get_func_ref_by_name(lex_ctxt *ctxt, const char *name)
Definition nasl_func.c:82
tree_cell * nasl_return(lex_ctxt *ctxt, tree_cell *retv)
Definition nasl_func.c:236
struct st_nasl_func nasl_func
const char * val
Definition nasl_init.c:441
tree_cell * nasl_incr_variable(lex_ctxt *, tree_cell *, int, int)
Definition nasl_var.c:919
tree_cell * get_array_elem(lex_ctxt *, const char *, tree_cell *)
Definition nasl_var.c:208
tree_cell * nasl_read_var_ref(lex_ctxt *, tree_cell *)
Definition nasl_var.c:832
tree_cell * decl_global_variables(lex_ctxt *, tree_cell *)
Definition nasl_var.c:771
tree_cell * decl_local_variables(lex_ctxt *, tree_cell *)
Definition nasl_var.c:758
tree_cell * get_variable_by_name(lex_ctxt *, const char *)
Definition nasl_var.c:176
tree_cell * alloc_expr_cell(int lnb, int t, tree_cell *l, tree_cell *r)
Definition nasl_tree.c:60
tree_cell * alloc_typed_cell(int typ)
Definition nasl_tree.c:25
@ NODE_FOR
Definition nasl_tree.h:16
@ NODE_ARRAY_EL
Definition nasl_tree.h:29
@ NODE_L_SHIFT_EQ
Definition nasl_tree.h:41
@ NODE_PLUS_EQ
Definition nasl_tree.h:35
@ NODE_FUN_CALL
Definition nasl_tree.h:22
@ COMP_LE
Definition nasl_tree.h:73
@ NODE_R_SHIFT_EQ
Definition nasl_tree.h:42
@ COMP_GT
Definition nasl_tree.h:76
@ NODE_LOCAL
Definition nasl_tree.h:32
@ NODE_AFF
Definition nasl_tree.h:30
@ EXPR_MODULO
Definition nasl_tree.h:54
@ COMP_RE_MATCH
Definition nasl_tree.h:69
@ NODE_MULT_EQ
Definition nasl_tree.h:37
@ COMP_NE
Definition nasl_tree.h:75
@ NODE_VAR
Definition nasl_tree.h:31
@ NODE_R_USHIFT_EQ
Definition nasl_tree.h:43
@ NODE_ARG
Definition nasl_tree.h:24
@ NODE_CONTINUE
Definition nasl_tree.h:27
@ COMP_EQ
Definition nasl_tree.h:74
@ EXPR_MULT
Definition nasl_tree.h:52
@ NODE_FUN_DEF
Definition nasl_tree.h:21
@ COMP_GE
Definition nasl_tree.h:77
@ NODE_REPEAT_UNTIL
Definition nasl_tree.h:19
@ NODE_REPEATED
Definition nasl_tree.h:20
@ REF_VAR
Definition nasl_tree.h:88
@ EXPR_BIT_NOT
Definition nasl_tree.h:60
@ EXPR_R_SHIFT
Definition nasl_tree.h:64
@ EXPR_DIV
Definition nasl_tree.h:53
@ COMP_RE_NOMATCH
Definition nasl_tree.h:70
@ NODE_RETURN
Definition nasl_tree.h:25
@ NODE_EMPTY
Definition nasl_tree.h:13
@ EXPR_BIT_XOR
Definition nasl_tree.h:59
@ NODE_FOREACH
Definition nasl_tree.h:18
@ NODE_IF_ELSE
Definition nasl_tree.h:14
@ NODE_DIV_EQ
Definition nasl_tree.h:38
@ NODE_MINUS_EQ
Definition nasl_tree.h:36
@ COMP_NOMATCH
Definition nasl_tree.h:68
@ COMP_MATCH
Definition nasl_tree.h:67
@ EXPR_NOT
Definition nasl_tree.h:47
@ EXPR_PLUS
Definition nasl_tree.h:49
@ EXPR_L_SHIFT
Definition nasl_tree.h:63
@ NODE_DECL
Definition nasl_tree.h:23
@ EXPR_BIT_AND
Definition nasl_tree.h:57
@ EXPR_EXPO
Definition nasl_tree.h:55
@ EXPR_INCR
Definition nasl_tree.h:61
@ NODE_INSTR_L
Definition nasl_tree.h:15
@ EXPR_BIT_OR
Definition nasl_tree.h:58
@ EXPR_U_MINUS
Definition nasl_tree.h:51
@ COMP_LT
Definition nasl_tree.h:72
@ EXPR_OR
Definition nasl_tree.h:46
@ NODE_BREAK
Definition nasl_tree.h:26
@ EXPR_DECR
Definition nasl_tree.h:62
@ EXPR_R_USHIFT
Definition nasl_tree.h:65
@ NODE_GLOBAL
Definition nasl_tree.h:33
@ NODE_WHILE
Definition nasl_tree.h:17
@ EXPR_MINUS
Definition nasl_tree.h:50
@ NODE_MODULO_EQ
Definition nasl_tree.h:39
@ EXPR_AND
Definition nasl_tree.h:45
nasl_iterator nasl_array_iterator(void *ctxt, tree_cell *c)
Definition nasl_var.c:1169
tree_cell * nasl_iterate_array(nasl_iterator *it)
Definition nasl_var.c:1205
tree_cell * nasl_affect(tree_cell *lval, tree_cell *rval)
Definition nasl_var.c:697
void free_array(nasl_array *a)
Definition nasl_var.c:339
struct TC * link[4]
Definition nasl_tree.h:107
union TC::@332262321161220155002104006201360276211317150140 x
short line_nb
Definition nasl_tree.h:96
char * str_val
Definition nasl_tree.h:103
void * ref_val
Definition nasl_tree.h:105
nasl_array * a
Definition nasl_var.h:67
tree_cell * ret_val
unsigned break_flag

References nasl_iterator::a, alloc_expr_cell(), alloc_typed_cell(), bool2cell(), struct_lex_ctxt::break_flag, cell2atom(), cell2bool(), cell2int(), cell2intW(), cell2str(), cell_cmp(), COMP_EQ, COMP_GE, COMP_GT, COMP_LE, COMP_LT, COMP_MATCH, COMP_NE, COMP_NOMATCH, COMP_RE_MATCH, COMP_RE_NOMATCH, CONST_DATA, CONST_INT, CONST_STR, struct_lex_ctxt::cont_flag, decl_global_variables(), decl_local_variables(), decl_nasl_func(), deref_cell(), DYN_ARRAY, expo(), EXPR_AND, EXPR_BIT_AND, EXPR_BIT_NOT, EXPR_BIT_OR, EXPR_BIT_XOR, EXPR_DECR, EXPR_DIV, EXPR_EXPO, EXPR_INCR, EXPR_L_SHIFT, EXPR_MINUS, EXPR_MODULO, EXPR_MULT, EXPR_NOT, EXPR_OR, EXPR_PLUS, EXPR_R_SHIFT, EXPR_R_USHIFT, EXPR_U_MINUS, FAKE_CELL, free_array(), get_array_elem(), get_func_ref_by_name(), get_variable_by_name(), int2cell(), struct_lex_ctxt::line_nb, TC::line_nb, TC::link, nasl_affect(), nasl_array_iterator(), nasl_exec(), nasl_func_call(), nasl_incr_variable(), nasl_iterate_array(), nasl_perror(), nasl_read_var_ref(), nasl_return(), nasl_short_dump(), nasl_trace_fp, NODE_AFF, NODE_ARG, NODE_ARRAY_EL, NODE_BREAK, NODE_CONTINUE, NODE_DECL, NODE_DIV_EQ, NODE_EMPTY, NODE_FOR, NODE_FOREACH, NODE_FUN_CALL, NODE_FUN_DEF, NODE_GLOBAL, NODE_IF_ELSE, NODE_INSTR_L, NODE_L_SHIFT_EQ, NODE_LOCAL, NODE_MINUS_EQ, NODE_MODULO_EQ, NODE_MULT_EQ, NODE_PLUS_EQ, NODE_R_SHIFT_EQ, NODE_R_USHIFT_EQ, NODE_REPEAT_UNTIL, NODE_REPEATED, NODE_RETURN, NODE_VAR, NODE_WHILE, REF_ARRAY, ref_cell(), TC::ref_val, REF_VAR, struct_lex_ctxt::ret_val, TC::size, TC::str_val, TC::type, val, and TC::x.

Referenced by cell2atom(), cell2bool(), cell2int3(), cell2str(), exec_nasl_script(), nasl_exec(), and nasl_func_call().

Here is the call graph for this function:
Here is the caller graph for this function: