1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* vim: set ts=8 sts=2 et sw=2 tw=80: */
3 /* This Source Code Form is subject to the terms of the Mozilla Public
4  * License, v. 2.0. If a copy of the MPL was not distributed with this
5  * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
6 
7 // Moz headers (alphabetical)
8 #include "nsCRTGlue.h"
9 #include "nsError.h"
10 #include "nsIFile.h"
11 #include "nsINIParser.h"
12 #include "mozilla/FileUtils.h" // AutoFILE
13 
14 // System headers (alphabetical)
15 #include <stdio.h>
16 #include <stdlib.h>
17 #ifdef XP_WIN
18 #include <windows.h>
19 #endif
20 
21 #if defined(XP_WIN)
22 #define READ_BINARYMODE L"rb"
23 #else
24 #define READ_BINARYMODE "r"
25 #endif
26 
27 using namespace mozilla;
28 
29 #ifdef XP_WIN
30 inline FILE*
TS_tfopen(const char * aPath,const wchar_t * aMode)31 TS_tfopen(const char* aPath, const wchar_t* aMode)
32 {
33   wchar_t wPath[MAX_PATH];
34   MultiByteToWideChar(CP_UTF8, 0, aPath, -1, wPath, MAX_PATH);
35   return _wfopen(wPath, aMode);
36 }
37 #else
38 inline FILE*
TS_tfopen(const char * aPath,const char * aMode)39 TS_tfopen(const char* aPath, const char* aMode)
40 {
41   return fopen(aPath, aMode);
42 }
43 #endif
44 
45 // Stack based FILE wrapper to ensure that fclose is called, copied from
46 // toolkit/mozapps/update/updater/readstrings.cpp
47 
48 class AutoFILE
49 {
50 public:
AutoFILE(FILE * aFp=nullptr)51   explicit AutoFILE(FILE* aFp = nullptr) : fp_(aFp) {}
~AutoFILE()52   ~AutoFILE()
53   {
54     if (fp_) {
55       fclose(fp_);
56     }
57   }
operator FILE*()58   operator FILE*() { return fp_; }
operator &()59   FILE** operator&() { return &fp_; }
operator =(FILE * aFp)60   void operator=(FILE* aFp) { fp_ = aFp; }
61 private:
62   FILE* fp_;
63 };
64 
65 nsresult
Init(nsIFile * aFile)66 nsINIParser::Init(nsIFile* aFile)
67 {
68   /* open the file. Don't use OpenANSIFileDesc, because you mustn't
69      pass FILE* across shared library boundaries, which may be using
70      different CRTs */
71 
72   AutoFILE fd;
73 
74 #ifdef XP_WIN
75   nsAutoString path;
76   nsresult rv = aFile->GetPath(path);
77   if (NS_WARN_IF(NS_FAILED(rv))) {
78     return rv;
79   }
80 
81   fd = _wfopen(path.get(), READ_BINARYMODE);
82 #else
83   nsAutoCString path;
84   aFile->GetNativePath(path);
85 
86   fd = fopen(path.get(), READ_BINARYMODE);
87 #endif
88 
89   if (!fd) {
90     return NS_ERROR_FAILURE;
91   }
92 
93   return InitFromFILE(fd);
94 }
95 
96 nsresult
Init(const char * aPath)97 nsINIParser::Init(const char* aPath)
98 {
99   /* open the file */
100   AutoFILE fd(TS_tfopen(aPath, READ_BINARYMODE));
101   if (!fd) {
102     return NS_ERROR_FAILURE;
103   }
104 
105   return InitFromFILE(fd);
106 }
107 
108 static const char kNL[] = "\r\n";
109 static const char kEquals[] = "=";
110 static const char kWhitespace[] = " \t";
111 static const char kRBracket[] = "]";
112 
113 nsresult
InitFromFILE(FILE * aFd)114 nsINIParser::InitFromFILE(FILE* aFd)
115 {
116   /* get file size */
117   if (fseek(aFd, 0, SEEK_END) != 0) {
118     return NS_ERROR_FAILURE;
119   }
120 
121   long flen = ftell(aFd);
122   /* zero-sized file, or an error */
123   if (flen <= 0) {
124     return NS_ERROR_FAILURE;
125   }
126 
127   /* malloc an internal buf the size of the file */
128   mFileContents = MakeUnique<char[]>(flen + 2);
129   if (!mFileContents) {
130     return NS_ERROR_OUT_OF_MEMORY;
131   }
132 
133   /* read the file in one swoop */
134   if (fseek(aFd, 0, SEEK_SET) != 0) {
135     return NS_BASE_STREAM_OSERROR;
136   }
137 
138   int rd = fread(mFileContents.get(), sizeof(char), flen, aFd);
139   if (rd != flen) {
140     return NS_BASE_STREAM_OSERROR;
141   }
142 
143   // We write a UTF16 null so that the file is easier to convert to UTF8
144   mFileContents[flen] = mFileContents[flen + 1] = '\0';
145 
146   char* buffer = &mFileContents[0];
147 
148   if (flen >= 3 &&
149       mFileContents[0] == '\xEF' &&
150       mFileContents[1] == '\xBB' &&
151       mFileContents[2] == '\xBF') {
152     // Someone set us up the Utf-8 BOM
153     // This case is easy, since we assume that BOM-less
154     // files are Utf-8 anyway.  Just skip the BOM and process as usual.
155     buffer = &mFileContents[3];
156   }
157 
158 #ifdef XP_WIN
159   if (flen >= 2 &&
160       mFileContents[0] == '\xFF' &&
161       mFileContents[1] == '\xFE') {
162     // Someone set us up the Utf-16LE BOM
163     buffer = &mFileContents[2];
164     // Get the size required for our Utf8 buffer
165     flen = WideCharToMultiByte(CP_UTF8,
166                                0,
167                                reinterpret_cast<LPWSTR>(buffer),
168                                -1,
169                                nullptr,
170                                0,
171                                nullptr,
172                                nullptr);
173     if (flen == 0) {
174       return NS_ERROR_FAILURE;
175     }
176 
177     UniquePtr<char[]> utf8Buffer(new char[flen]);
178     if (WideCharToMultiByte(CP_UTF8, 0, reinterpret_cast<LPWSTR>(buffer), -1,
179                             utf8Buffer.get(), flen, nullptr, nullptr) == 0) {
180       return NS_ERROR_FAILURE;
181     }
182     mFileContents = Move(utf8Buffer);
183     buffer = mFileContents.get();
184   }
185 #endif
186 
187   char* currSection = nullptr;
188 
189   // outer loop tokenizes into lines
190   while (char* token = NS_strtok(kNL, &buffer)) {
191     if (token[0] == '#' || token[0] == ';') { // it's a comment
192       continue;
193     }
194 
195     token = (char*)NS_strspnp(kWhitespace, token);
196     if (!*token) { // empty line
197       continue;
198     }
199 
200     if (token[0] == '[') { // section header!
201       ++token;
202       currSection = token;
203 
204       char* rb = NS_strtok(kRBracket, &token);
205       if (!rb || NS_strtok(kWhitespace, &token)) {
206         // there's either an unclosed [Section or a [Section]Moretext!
207         // we could frankly decide that this INI file is malformed right
208         // here and stop, but we won't... keep going, looking for
209         // a well-formed [section] to continue working with
210         currSection = nullptr;
211       }
212 
213       continue;
214     }
215 
216     if (!currSection) {
217       // If we haven't found a section header (or we found a malformed
218       // section header), don't bother parsing this line.
219       continue;
220     }
221 
222     char* key = token;
223     char* e = NS_strtok(kEquals, &token);
224     if (!e || !token) {
225       continue;
226     }
227 
228     INIValue* v;
229     if (!mSections.Get(currSection, &v)) {
230       v = new INIValue(key, token);
231       if (!v) {
232         return NS_ERROR_OUT_OF_MEMORY;
233       }
234 
235       mSections.Put(currSection, v);
236       continue;
237     }
238 
239     // Check whether this key has already been specified; overwrite
240     // if so, or append if not.
241     while (v) {
242       if (!strcmp(key, v->key)) {
243         v->value = token;
244         break;
245       }
246       if (!v->next) {
247         v->next = MakeUnique<INIValue>(key, token);
248         if (!v->next) {
249           return NS_ERROR_OUT_OF_MEMORY;
250         }
251         break;
252       }
253       v = v->next.get();
254     }
255     NS_ASSERTION(v, "v should never be null coming out of this loop");
256   }
257 
258   return NS_OK;
259 }
260 
261 nsresult
GetString(const char * aSection,const char * aKey,nsACString & aResult)262 nsINIParser::GetString(const char* aSection, const char* aKey,
263                        nsACString& aResult)
264 {
265   INIValue* val;
266   mSections.Get(aSection, &val);
267 
268   while (val) {
269     if (strcmp(val->key, aKey) == 0) {
270       aResult.Assign(val->value);
271       return NS_OK;
272     }
273 
274     val = val->next.get();
275   }
276 
277   return NS_ERROR_FAILURE;
278 }
279 
280 nsresult
GetString(const char * aSection,const char * aKey,char * aResult,uint32_t aResultLen)281 nsINIParser::GetString(const char* aSection, const char* aKey,
282                        char* aResult, uint32_t aResultLen)
283 {
284   INIValue* val;
285   mSections.Get(aSection, &val);
286 
287   while (val) {
288     if (strcmp(val->key, aKey) == 0) {
289       strncpy(aResult, val->value, aResultLen);
290       aResult[aResultLen - 1] = '\0';
291       if (strlen(val->value) >= aResultLen) {
292         return NS_ERROR_LOSS_OF_SIGNIFICANT_DATA;
293       }
294 
295       return NS_OK;
296     }
297 
298     val = val->next.get();
299   }
300 
301   return NS_ERROR_FAILURE;
302 }
303 
304 nsresult
GetSections(INISectionCallback aCB,void * aClosure)305 nsINIParser::GetSections(INISectionCallback aCB, void* aClosure)
306 {
307   for (auto iter = mSections.Iter(); !iter.Done(); iter.Next()) {
308     if (!aCB(iter.Key(), aClosure)) {
309       break;
310     }
311   }
312   return NS_OK;
313 }
314 
315 nsresult
GetStrings(const char * aSection,INIStringCallback aCB,void * aClosure)316 nsINIParser::GetStrings(const char* aSection,
317                         INIStringCallback aCB, void* aClosure)
318 {
319   INIValue* val;
320 
321   for (mSections.Get(aSection, &val);
322        val;
323        val = val->next.get()) {
324 
325     if (!aCB(val->key, val->value, aClosure)) {
326       return NS_OK;
327     }
328   }
329 
330   return NS_OK;
331 }
332