1 /* Copyright (C) 2009 Trend Micro Inc.
4 * This program is a free software; you can redistribute it
5 * and/or modify it under the terms of the GNU General Public
6 * License (version 2) as published by the FSF - Free Software
15 #include "os_xml_internal.h"
18 int OS_ApplyVariables(OS_XML *_lxml)
20 unsigned int i, j = 0, s = 0;
26 char *var_placeh = NULL;
28 /* Get all variables */
29 for (i = 0; i < _lxml->cur; i++) {
30 if (_lxml->tp[i] == XML_VARIABLE_BEGIN) {
33 for (j = i + 1; j < _lxml->cur; j++) {
34 if (_lxml->rl[j] < _lxml->rl[i]) {
38 else if (_lxml->tp[j] == XML_ATTR) {
39 if ((_lxml->el[j]) && (strcasecmp(_lxml->el[j], XML_VAR_ATTRIBUTE) == 0)) {
41 snprintf(_lxml->err, XML_ERR_LENGTH, "XMLERR: Invalid variable content.");
42 _lxml->err_line = _lxml->ln[j];
44 } else if (strlen(_lxml->ct[j]) >= XML_VARIABLE_MAXSIZE) {
45 snprintf(_lxml->err, XML_ERR_LENGTH, "XMLERR: Invalid variable name size.");
46 _lxml->err_line = _lxml->ln[j];
50 /* If not used, it will be cleaned later */
51 snprintf(_lxml->err, XML_ERR_LENGTH, "XMLERR: Memory error.");
53 tmp = (char **)realloc(var, (s + 1) * sizeof(char *));
59 var[s] = _lxml->ct[j];
61 /* Clean the lxml->err */
62 strncpy(_lxml->err, " ", 3);
67 snprintf(_lxml->err, XML_ERR_LENGTH,
68 "XMLERR: Only \""XML_VAR_ATTRIBUTE"\" is allowed"
69 " as an attribute for a variable.");
70 _lxml->err_line = _lxml->ln[j];
77 if ((_found_var == 0) || (!_lxml->ct[i])) {
78 snprintf(_lxml->err, XML_ERR_LENGTH,
79 "XMLERR: No value set for variable.");
80 _lxml->err_line = _lxml->ln[i];
84 snprintf(_lxml->err, XML_ERR_LENGTH, "XMLERR: Memory error.");
86 tmp = (char **)realloc(value, (s + 1) * sizeof(char *));
92 value[s] = _lxml->ct[i];
94 strncpy(_lxml->err, " ", 3);
96 } else if (((_lxml->tp[i] == XML_ELEM) || (_lxml->tp[i] == XML_ATTR)) &&
101 char lvar[XML_VARIABLE_MAXSIZE]; /* MAX Var size */
104 if (strlen(_lxml->ct[i]) <= 2) {
108 /* Check if any variable is defined */
113 /* Duplicate string */
114 p = strdup(_lxml->ct[i]);
118 snprintf(_lxml->err, XML_ERR_LENGTH, "XMLERR: Memory error.");
122 /* Read the whole string */
124 if (*p == XML_VARIABLE_BEGIN) {
127 memset(lvar, '\0', XML_VARIABLE_MAXSIZE);
130 if ((*p == XML_VARIABLE_BEGIN)
139 for (j = 0; j < s; j++) {
140 if (var[j] == NULL) {
144 if (strcasecmp(var[j], lvar) != 0) {
148 size_t tsize = strlen(_lxml->ct[i]) +
149 strlen(value[j]) - tp + 1;
150 var_placeh = strdup(_lxml->ct[i]);
152 _lxml->ct[i] = (char *)calloc(tsize + 2,
155 if (_lxml->ct[i] == NULL || var_placeh == NULL) {
156 snprintf(_lxml->err, XML_ERR_LENGTH, "XMLERR: Memory "
161 strncpy(_lxml->ct[i], var_placeh, tsize);
163 _lxml->ct[i][init] = '\0';
164 strncat(_lxml->ct[i], value[j], tsize - init);
166 init = strlen(_lxml->ct[i]);
167 strncat(_lxml->ct[i], p,
168 tsize - strlen(_lxml->ct[i]));
176 /* Variable not found */
177 if ((j == s) && (strlen(lvar) >= 1)) {
178 snprintf(_lxml->err, XML_ERR_LENGTH,
179 "XMLERR: Unknown variable"
181 _lxml->err_line = _lxml->ln[i];
190 /* Maximum size for a variable */
191 if (tp >= XML_VARIABLE_MAXSIZE - 1) {
192 snprintf(_lxml->err, XML_ERR_LENGTH, "XMLERR: Invalid "
193 "variable name size: '%u'.", tp);
194 _lxml->err_line = _lxml->ln[i];
203 } /* IF XML_VAR_BEGIN */
227 /* Clean up the variables */