Coverity: Core module errors fixed
[scilab.git] / scilab / modules / core / src / c / loadversion.c
1 /*
2  * Scilab ( http://www.scilab.org/ ) - This file is part of Scilab
3  * Copyright (C) 2006 - INRIA - Allan CORNET
4  * Copyright (C) 2011 - DIGITEO - Allan CORNET
5  *
6  * Copyright (C) 2012 - 2016 - Scilab Enterprises
7  *
8  * This file is hereby licensed under the terms of the GNU GPL v2.0,
9  * pursuant to article 5.3.4 of the CeCILL v.2.1.
10  * This file was originally licensed under the terms of the CeCILL v2.1,
11  * and continues to be available under such terms.
12  * For more information, see the COPYING file which you should have received
13  * along with this program.
14  *
15  */
16 #include <stdlib.h>
17 #include <stdio.h>
18 #include <string.h>
19 #include <libxml/xpath.h>
20 #include <libxml/xmlreader.h>
21 #include "loadversion.h"
22 #include "with_module.h"
23 #include "sci_path.h"
24 #include "sci_malloc.h"
25 #include "GetXmlFileEncoding.h"
26 #include "scilabDefaults.h"
27 #include "localization.h"
28 #include "FileExist.h"
29 #include "os_string.h"
30 #include "getshortpathname.h"
31 #include "charEncoding.h"
32 #include "version.h"
33 /*--------------------------------------------------------------------------*/
34 BOOL getversionmodule(wchar_t* _pwstModule,
35                       int *sci_version_major,
36                       int *sci_version_minor,
37                       int *sci_version_maintenance,
38                       wchar_t* _pwstSciVersionString,
39                       int *sci_version_revision)
40 {
41     BOOL bOK = FALSE;
42
43     if (with_module(_pwstModule))
44     {
45         char* filename_VERSION_module = NULL;
46         char* pstModule = wide_string_to_UTF8(_pwstModule);
47         char* SciPath = NULL;
48         int len = 0;
49
50         SciPath = getSCI();
51         len = (int)strlen(FORMATVERSIONFILENAME) + (int)strlen(SciPath) + (int)strlen(pstModule) + 1;
52         filename_VERSION_module = (char*)MALLOC(sizeof(char) * len);
53         sprintf(filename_VERSION_module, FORMATVERSIONFILENAME, SciPath, pstModule);
54         FREE(pstModule);
55         FREE(SciPath);
56         SciPath = NULL;
57
58         if (FileExist(filename_VERSION_module))
59         {
60             char *encoding = GetXmlFileEncoding(filename_VERSION_module);
61
62             /* Don't care about line return / empty line */
63             xmlKeepBlanksDefault(0);
64
65             /* check if the XML file has been encoded with utf8 (unicode) or not */
66             if ( stricmp("utf-8", encoding) == 0)
67             {
68                 xmlDocPtr doc = NULL;
69                 xmlXPathContextPtr xpathCtxt = NULL;
70                 xmlXPathObjectPtr xpathObj = NULL;
71
72                 int version_major = 0;
73                 int version_minor = 0;
74                 int version_maintenance = 0;
75                 int version_revision = 0;
76                 wchar_t *pwstSciVersionString = 0;
77
78                 {
79                     BOOL bConvert = FALSE;
80                     char *shortfilename_VERSION_module = getshortpathname(filename_VERSION_module, &bConvert);
81                     if (shortfilename_VERSION_module)
82                     {
83                         doc = xmlParseFile (shortfilename_VERSION_module);
84                         FREE(shortfilename_VERSION_module);
85                         shortfilename_VERSION_module = NULL;
86                     }
87                 }
88
89                 if (doc == NULL)
90                 {
91                     fprintf(stderr, _("Error: Could not parse file %s\n"), filename_VERSION_module);
92                     FREE(encoding);
93                     encoding = NULL;
94                     return FALSE;
95                 }
96
97                 xpathCtxt = xmlXPathNewContext(doc);
98                 xpathObj = xmlXPathEval((const xmlChar*)"//MODULE_VERSION/VERSION", xpathCtxt);
99
100                 if (xpathObj && xpathObj->nodesetval->nodeMax)
101                 {
102
103                     xmlAttrPtr attrib = xpathObj->nodesetval->nodeTab[0]->properties;
104                     while (attrib != NULL)
105                     {
106                         if (xmlStrEqual (attrib->name, (const xmlChar*) "major"))
107                         {
108                             /* we found <major> */
109                             const char *str = (const char*)attrib->children->content;
110                             version_major = atoi(str);
111                         }
112                         else if (xmlStrEqual (attrib->name, (const xmlChar*)"minor"))
113                         {
114                             /* we found <minor> */
115                             const char *str = (const char*)attrib->children->content;
116                             version_minor = atoi(str);
117                         }
118                         else if (xmlStrEqual (attrib->name, (const xmlChar*)"maintenance"))
119                         {
120                             /* we found <maintenance> */
121                             const char *str = (const char*)attrib->children->content;
122                             version_maintenance = atoi(str);
123                         }
124                         else if (xmlStrEqual (attrib->name, (const xmlChar*)"revision"))
125                         {
126                             /* we found <revision> */
127                             const char *str = (const char*)attrib->children->content;
128                             version_revision = atoi(str);
129                         }
130                         else if (xmlStrEqual (attrib->name, (const xmlChar*)"string"))
131                         {
132                             /* we found <string> */
133                             const char *str = (const char*)attrib->children->content;
134                             if (pwstSciVersionString)
135                             {
136                                 FREE(pwstSciVersionString);
137                             }
138                             pwstSciVersionString = to_wide_string(str);
139                         }
140
141                         attrib = attrib->next;
142                     }
143
144                     *sci_version_major = version_major;
145                     *sci_version_minor = version_minor;
146                     *sci_version_maintenance = version_maintenance;
147                     *sci_version_revision = version_revision;
148                     if (pwstSciVersionString)
149                     {
150                         wcscpy(_pwstSciVersionString, pwstSciVersionString);
151                         FREE(pwstSciVersionString);
152                         pwstSciVersionString = NULL;
153                     }
154                     else
155                     {
156                         _pwstSciVersionString = NULL;
157                     }
158                 }
159                 else
160                 {
161                     fprintf(stderr, _("Error: Not a valid version file %s (should start with <MODULE_VERSION> and contain <VERSION major='' minor='' maintenance='' revision='' string=''>)\n"), filename_VERSION_module);
162                     FREE(encoding);
163                     encoding = NULL;
164                     return FALSE;
165                 }
166                 if (xpathObj)
167                 {
168                     xmlXPathFreeObject(xpathObj);
169                 }
170                 if (xpathCtxt)
171                 {
172                     xmlXPathFreeContext(xpathCtxt);
173                 }
174                 xmlFreeDoc (doc);
175             }
176             else
177             {
178                 fprintf(stderr, _("Error: Not a valid version file %s (encoding not 'utf-8') Encoding '%s' found\n"), filename_VERSION_module, encoding);
179             }
180
181             FREE(encoding);
182             encoding = NULL;
183             bOK = TRUE;
184         }
185         else
186         {
187             // version.xml does not exist but module exists then we returns scilab version
188             *sci_version_major =  SCI_VERSION_MAJOR;
189             *sci_version_minor = SCI_VERSION_MINOR;
190             *sci_version_maintenance = SCI_VERSION_MAINTENANCE;
191             *sci_version_revision = SCI_VERSION_TIMESTAMP;
192             wcscpy(_pwstSciVersionString, L"");
193             bOK = TRUE;
194         }
195         FREE(filename_VERSION_module);
196         filename_VERSION_module = NULL;
197     }
198     return bOK;
199 }
200 /*--------------------------------------------------------------------------*/