20#include <winpr/config.h>
21#include <winpr/assert.h>
28#include <winpr/wtypes.h>
30#include <winpr/file.h>
60 wIniFileSection** sections;
63static BOOL IniFile_Load_NextLine(wIniFile* ini,
char* str)
69 ini->nextLine = strtok_s(str,
"\n", &ini->tokctx);
72 length = strlen(ini->nextLine);
76 if (ini->nextLine[length - 1] ==
'\r')
78 ini->nextLine[length - 1] =
'\0';
86 return (ini->nextLine) ? TRUE : FALSE;
89static BOOL IniFile_BufferResize(wIniFile* ini,
size_t size)
92 if (size > ini->buffersize)
94 const size_t diff = size - ini->buffersize;
95 char* tmp = realloc(ini->buffer, size);
99 memset(&tmp[ini->buffersize], 0, diff *
sizeof(
char));
101 ini->buffersize = size;
106static BOOL IniFile_Load_String(wIniFile* ini,
const char* iniString)
116 ini->nextLine = NULL;
117 fileSize = strlen(iniString);
122 if (!IniFile_BufferResize(ini, fileSize + 2))
125 CopyMemory(ini->buffer, iniString, fileSize);
126 ini->buffer[fileSize] =
'\n';
127 IniFile_Load_NextLine(ini, ini->buffer);
131static void IniFile_Close_File(FILE* fp)
137static FILE* IniFile_Open_File(wIniFile* ini,
const char* filename)
145 return winpr_fopen(filename,
"rb");
147 return winpr_fopen(filename,
"w+b");
150static BOOL IniFile_Load_File(wIniFile* ini,
const char* filename)
156 FILE* fp = IniFile_Open_File(ini, filename);
160 if (_fseeki64(fp, 0, SEEK_END) < 0)
164 const INT64 fileSize = _ftelli64(fp);
168 if (_fseeki64(fp, 0, SEEK_SET) < 0)
172 ini->nextLine = NULL;
177 if (!IniFile_BufferResize(ini, (
size_t)fileSize + 2))
180 if (fread(ini->buffer, (
size_t)fileSize, 1ul, fp) != 1)
183 ini->buffer[fileSize] =
'\n';
184 ini->buffer[fileSize + 1] =
'\0';
186 IniFile_Load_NextLine(ini, ini->buffer);
190 IniFile_Close_File(fp);
194static BOOL IniFile_Load_HasNextLine(wIniFile* ini)
198 return (ini->nextLine) ? TRUE : FALSE;
201static char* IniFile_Load_GetNextLine(wIniFile* ini)
205 ini->line = ini->nextLine;
206 ini->lineLength = strlen(ini->line);
207 IniFile_Load_NextLine(ini, NULL);
211static void IniFile_Key_Free(wIniFileKey* key)
221static wIniFileKey* IniFile_Key_New(
const char* name,
const char* value)
226 wIniFileKey* key = calloc(1,
sizeof(wIniFileKey));
230 key->name = _strdup(name);
231 key->value = _strdup(value);
233 if (!key->name || !key->value)
235 IniFile_Key_Free(key);
243static void IniFile_Section_Free(wIniFileSection* section)
250 for (
size_t index = 0; index < section->nKeys; index++)
252 IniFile_Key_Free(section->keys[index]);
255 free((
void*)section->keys);
259static BOOL IniFile_SectionKeysResize(wIniFileSection* section,
size_t count)
261 WINPR_ASSERT(section);
263 if (section->nKeys + count >= section->cKeys)
265 const size_t new_size = section->cKeys + count + 1024;
266 const size_t diff = new_size - section->cKeys;
267 wIniFileKey** new_keys =
268 (wIniFileKey**)realloc((
void*)section->keys,
sizeof(wIniFileKey*) * new_size);
273 memset((
void*)&new_keys[section->cKeys], 0, diff *
sizeof(wIniFileKey*));
274 section->cKeys = new_size;
275 section->keys = new_keys;
280static wIniFileSection* IniFile_Section_New(
const char* name)
285 wIniFileSection* section = calloc(1,
sizeof(wIniFileSection));
290 section->name = _strdup(name);
295 if (!IniFile_SectionKeysResize(section, 64))
301 IniFile_Section_Free(section);
305static wIniFileSection* IniFile_GetSection(wIniFile* ini,
const char* name)
307 wIniFileSection* section = NULL;
314 for (
size_t index = 0; index < ini->nSections; index++)
316 if (_stricmp(name, ini->sections[index]->name) == 0)
318 section = ini->sections[index];
326static BOOL IniFile_SectionResize(wIniFile* ini,
size_t count)
330 if (ini->nSections + count >= ini->cSections)
332 const size_t new_size = ini->cSections + count + 1024;
333 const size_t diff = new_size - ini->cSections;
334 wIniFileSection** new_sect =
335 (wIniFileSection**)realloc((
void*)ini->sections,
sizeof(wIniFileSection*) * new_size);
340 memset((
void*)&new_sect[ini->cSections], 0, diff *
sizeof(wIniFileSection*));
341 ini->cSections = new_size;
342 ini->sections = new_sect;
347static wIniFileSection* IniFile_AddToSection(wIniFile* ini,
const char* name)
354 wIniFileSection* section = IniFile_GetSection(ini, name);
358 if (!IniFile_SectionResize(ini, 1))
361 section = IniFile_Section_New(name);
364 ini->sections[ini->nSections++] = section;
370static wIniFileKey* IniFile_GetKey(wIniFileSection* section,
const char* name)
372 wIniFileKey* key = NULL;
374 WINPR_ASSERT(section);
379 for (
size_t index = 0; index < section->nKeys; index++)
381 if (_stricmp(name, section->keys[index]->name) == 0)
383 key = section->keys[index];
391static wIniFileKey* IniFile_AddKey(wIniFileSection* section,
const char* name,
const char* value)
393 WINPR_ASSERT(section);
398 wIniFileKey* key = IniFile_GetKey(section, name);
402 if (!IniFile_SectionKeysResize(section, 1))
405 key = IniFile_Key_New(name, value);
410 section->keys[section->nKeys++] = key;
415 key->value = _strdup(value);
424static int IniFile_Load(wIniFile* ini)
428 char* separator = NULL;
431 wIniFileSection* section = NULL;
435 while (IniFile_Load_HasNextLine(ini))
437 char* line = IniFile_Load_GetNextLine(ini);
445 end = strchr(line,
']');
451 IniFile_AddToSection(ini, beg);
452 section = ini->sections[ini->nSections - 1];
456 separator = strchr(line,
'=');
458 if (separator == NULL)
463 while ((&end[-1] > line) && ((end[-1] ==
' ') || (end[-1] ==
'\t')))
470 while (*beg && ((*beg ==
' ') || (*beg ==
'\t')))
476 end = &line[ini->lineLength];
478 while ((end > beg) && ((end[-1] ==
' ') || (end[-1] ==
'\t')))
486 if (!IniFile_AddKey(section, name, value))
494static BOOL IniFile_SetFilename(wIniFile* ini,
const char* name)
498 ini->filename = NULL;
502 ini->filename = _strdup(name);
503 return ini->filename != NULL;
506int IniFile_ReadBuffer(wIniFile* ini,
const char* buffer)
515 ini->readOnly = TRUE;
516 status = IniFile_Load_String(ini, buffer);
521 return IniFile_Load(ini);
524int IniFile_ReadFile(wIniFile* ini,
const char* filename)
528 ini->readOnly = TRUE;
529 if (!IniFile_SetFilename(ini, filename))
534 if (!IniFile_Load_File(ini, filename))
537 return IniFile_Load(ini);
540char** IniFile_GetSectionNames(wIniFile* ini,
size_t* count)
547 if (ini->nSections > INT_MAX)
550 size_t length = (
sizeof(
char*) * ini->nSections) +
sizeof(char);
552 for (
size_t index = 0; index < ini->nSections; index++)
554 wIniFileSection* section = ini->sections[index];
555 const size_t nameLength = strlen(section->name);
556 length += (nameLength + 1);
559 char** sectionNames = (
char**)calloc(length,
sizeof(
char*));
564 char* p = (
char*)&((BYTE*)sectionNames)[
sizeof(
char*) * ini->nSections];
566 for (
size_t index = 0; index < ini->nSections; index++)
568 sectionNames[index] = p;
569 wIniFileSection* section = ini->sections[index];
570 const size_t nameLength = strlen(section->name);
571 CopyMemory(p, section->name, nameLength + 1);
572 p += (nameLength + 1);
576 *count = ini->nSections;
580char** IniFile_GetSectionKeyNames(wIniFile* ini,
const char* section,
size_t* count)
584 if (!section || !count)
587 wIniFileSection* pSection = IniFile_GetSection(ini, section);
592 if (pSection->nKeys > INT_MAX)
595 size_t length = (
sizeof(
char*) * pSection->nKeys) +
sizeof(char);
597 for (
size_t index = 0; index < pSection->nKeys; index++)
599 wIniFileKey* pKey = pSection->keys[index];
600 const size_t nameLength = strlen(pKey->name);
601 length += (nameLength + 1);
604 char** keyNames = (
char**)calloc(length,
sizeof(
char*));
609 char* p = (
char*)&((BYTE*)keyNames)[
sizeof(
char*) * pSection->nKeys];
611 for (
size_t index = 0; index < pSection->nKeys; index++)
614 wIniFileKey* pKey = pSection->keys[index];
615 const size_t nameLength = strlen(pKey->name);
616 CopyMemory(p, pKey->name, nameLength + 1);
617 p += (nameLength + 1);
621 *count = pSection->nKeys;
625const char* IniFile_GetKeyValueString(wIniFile* ini,
const char* section,
const char* key)
627 const char* value = NULL;
628 wIniFileKey* pKey = NULL;
629 wIniFileSection* pSection = NULL;
633 pSection = IniFile_GetSection(ini, section);
638 pKey = IniFile_GetKey(pSection, key);
643 value = (
const char*)pKey->value;
647int IniFile_GetKeyValueInt(wIniFile* ini,
const char* section,
const char* key)
651 wIniFileKey* pKey = NULL;
652 wIniFileSection* pSection = NULL;
656 pSection = IniFile_GetSection(ini, section);
661 pKey = IniFile_GetKey(pSection, key);
668 value = strtol(pKey->value, NULL, 0);
669 if ((value < INT_MIN) || (value > INT_MAX) || (errno != 0))
677int IniFile_SetKeyValueString(wIniFile* ini,
const char* section,
const char* key,
680 wIniFileKey* pKey = NULL;
683 wIniFileSection* pSection = IniFile_GetSection(ini, section);
686 pSection = IniFile_AddToSection(ini, section);
691 pKey = IniFile_AddKey(pSection, key, value);
699int IniFile_SetKeyValueInt(wIniFile* ini,
const char* section,
const char* key,
int value)
701 char strVal[128] = { 0 };
702 wIniFileKey* pKey = NULL;
703 wIniFileSection* pSection = NULL;
707 (void)sprintf_s(strVal,
sizeof(strVal),
"%d", value);
708 pSection = IniFile_GetSection(ini, section);
711 pSection = IniFile_AddToSection(ini, section);
716 pKey = IniFile_AddKey(pSection, key, strVal);
724char* IniFile_WriteBuffer(wIniFile* ini)
732 for (
size_t i = 0; i < ini->nSections; i++)
734 wIniFileSection* section = ini->sections[i];
735 size += (strlen(section->name) + 3);
737 for (
size_t j = 0; j < section->nKeys; j++)
739 wIniFileKey* key = section->keys[j];
740 size += (strlen(key->name) + strlen(key->value) + 2);
747 buffer = calloc(size + 1,
sizeof(
char));
754 for (
size_t i = 0; i < ini->nSections; i++)
756 wIniFileSection* section = ini->sections[i];
757 (void)sprintf_s(&buffer[offset], size - offset,
"[%s]\n", section->name);
758 offset += (strlen(section->name) + 3);
760 for (
size_t j = 0; j < section->nKeys; j++)
762 wIniFileKey* key = section->keys[j];
763 (void)sprintf_s(&buffer[offset], size - offset,
"%s=%s\n", key->name, key->value);
764 offset += (strlen(key->name) + strlen(key->value) + 2);
767 (void)sprintf_s(&buffer[offset], size - offset,
"\n");
774int IniFile_WriteFile(wIniFile* ini,
const char* filename)
780 char* buffer = IniFile_WriteBuffer(ini);
785 const size_t length = strlen(buffer);
786 ini->readOnly = FALSE;
789 filename = ini->filename;
791 FILE* fp = IniFile_Open_File(ini, filename);
795 if (fwrite((
void*)buffer, length, 1, fp) != 1)
801 IniFile_Close_File(fp);
806void IniFile_Free(wIniFile* ini)
811 IniFile_SetFilename(ini, NULL);
813 for (
size_t index = 0; index < ini->nSections; index++)
814 IniFile_Section_Free(ini->sections[index]);
816 free((
void*)ini->sections);
821wIniFile* IniFile_New(
void)
823 wIniFile* ini = (wIniFile*)calloc(1,
sizeof(wIniFile));
828 if (!IniFile_SectionResize(ini, 64))
834 WINPR_PRAGMA_DIAG_PUSH
835 WINPR_PRAGMA_DIAG_IGNORED_MISMATCHED_DEALLOC
837 WINPR_PRAGMA_DIAG_POP
841wIniFile* IniFile_Clone(
const wIniFile* ini)
846 wIniFile* copy = IniFile_New();
850 copy->lineLength = ini->lineLength;
851 if (!IniFile_SetFilename(copy, ini->filename))
854 if (ini->buffersize > 0)
856 if (!IniFile_BufferResize(copy, ini->buffersize))
858 memcpy(copy->buffer, ini->buffer, copy->buffersize);
861 copy->readOnly = ini->readOnly;
863 for (
size_t x = 0; x < ini->nSections; x++)
865 const wIniFileSection* cur = ini->sections[x];
869 wIniFileSection* scopy = IniFile_AddToSection(copy, cur->name);
873 for (
size_t y = 0; y < cur->nKeys; y++)
875 const wIniFileKey* key = cur->keys[y];
879 IniFile_AddKey(scopy, key->name, key->value);