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)
163 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';
185 IniFile_Load_NextLine(ini, ini->buffer);
189 IniFile_Close_File(fp);
193static BOOL IniFile_Load_HasNextLine(wIniFile* ini)
197 return (ini->nextLine) ? TRUE : FALSE;
200static char* IniFile_Load_GetNextLine(wIniFile* ini)
204 ini->line = ini->nextLine;
205 ini->lineLength = strlen(ini->line);
206 IniFile_Load_NextLine(ini, NULL);
210static void IniFile_Key_Free(wIniFileKey* key)
220static wIniFileKey* IniFile_Key_New(
const char* name,
const char* value)
225 wIniFileKey* key = calloc(1,
sizeof(wIniFileKey));
229 key->name = _strdup(name);
230 key->value = _strdup(value);
232 if (!key->name || !key->value)
234 IniFile_Key_Free(key);
242static void IniFile_Section_Free(wIniFileSection* section)
249 for (
size_t index = 0; index < section->nKeys; index++)
251 IniFile_Key_Free(section->keys[index]);
254 free((
void*)section->keys);
258static BOOL IniFile_SectionKeysResize(wIniFileSection* section,
size_t count)
260 WINPR_ASSERT(section);
262 if (section->nKeys + count >= section->cKeys)
264 const size_t new_size = section->cKeys + count + 1024;
265 const size_t diff = new_size - section->cKeys;
266 wIniFileKey** new_keys =
267 (wIniFileKey**)realloc((
void*)section->keys,
sizeof(wIniFileKey*) * new_size);
272 memset((
void*)&new_keys[section->cKeys], 0, diff *
sizeof(wIniFileKey*));
273 section->cKeys = new_size;
274 section->keys = new_keys;
279static wIniFileSection* IniFile_Section_New(
const char* name)
284 wIniFileSection* section = calloc(1,
sizeof(wIniFileSection));
289 section->name = _strdup(name);
294 if (!IniFile_SectionKeysResize(section, 64))
300 IniFile_Section_Free(section);
304static wIniFileSection* IniFile_GetSection(wIniFile* ini,
const char* name)
306 wIniFileSection* section = NULL;
313 for (
size_t index = 0; index < ini->nSections; index++)
315 if (_stricmp(name, ini->sections[index]->name) == 0)
317 section = ini->sections[index];
325static BOOL IniFile_SectionResize(wIniFile* ini,
size_t count)
329 if (ini->nSections + count >= ini->cSections)
331 const size_t new_size = ini->cSections + count + 1024;
332 const size_t diff = new_size - ini->cSections;
333 wIniFileSection** new_sect =
334 (wIniFileSection**)realloc((
void*)ini->sections,
sizeof(wIniFileSection*) * new_size);
339 memset((
void*)&new_sect[ini->cSections], 0, diff *
sizeof(wIniFileSection*));
340 ini->cSections = new_size;
341 ini->sections = new_sect;
346static wIniFileSection* IniFile_AddToSection(wIniFile* ini,
const char* name)
353 wIniFileSection* section = IniFile_GetSection(ini, name);
357 if (!IniFile_SectionResize(ini, 1))
360 section = IniFile_Section_New(name);
363 ini->sections[ini->nSections++] = section;
369static wIniFileKey* IniFile_GetKey(wIniFileSection* section,
const char* name)
371 wIniFileKey* key = NULL;
373 WINPR_ASSERT(section);
378 for (
size_t index = 0; index < section->nKeys; index++)
380 if (_stricmp(name, section->keys[index]->name) == 0)
382 key = section->keys[index];
390static wIniFileKey* IniFile_AddKey(wIniFileSection* section,
const char* name,
const char* value)
392 WINPR_ASSERT(section);
397 wIniFileKey* key = IniFile_GetKey(section, name);
401 if (!IniFile_SectionKeysResize(section, 1))
404 key = IniFile_Key_New(name, value);
409 section->keys[section->nKeys++] = key;
414 key->value = _strdup(value);
423static int IniFile_Load(wIniFile* ini)
427 char* separator = NULL;
430 wIniFileSection* section = NULL;
434 while (IniFile_Load_HasNextLine(ini))
436 char* line = IniFile_Load_GetNextLine(ini);
444 end = strchr(line,
']');
450 IniFile_AddToSection(ini, beg);
451 section = ini->sections[ini->nSections - 1];
455 separator = strchr(line,
'=');
457 if (separator == NULL)
462 while ((&end[-1] > line) && ((end[-1] ==
' ') || (end[-1] ==
'\t')))
469 while (*beg && ((*beg ==
' ') || (*beg ==
'\t')))
475 end = &line[ini->lineLength];
477 while ((end > beg) && ((end[-1] ==
' ') || (end[-1] ==
'\t')))
485 if (!IniFile_AddKey(section, name, value))
493static BOOL IniFile_SetFilename(wIniFile* ini,
const char* name)
497 ini->filename = NULL;
501 ini->filename = _strdup(name);
502 return ini->filename != NULL;
505int IniFile_ReadBuffer(wIniFile* ini,
const char* buffer)
514 ini->readOnly = TRUE;
515 status = IniFile_Load_String(ini, buffer);
520 return IniFile_Load(ini);
523int IniFile_ReadFile(wIniFile* ini,
const char* filename)
527 ini->readOnly = TRUE;
528 if (!IniFile_SetFilename(ini, filename))
533 if (!IniFile_Load_File(ini, filename))
536 return IniFile_Load(ini);
539char** IniFile_GetSectionNames(wIniFile* ini,
size_t* count)
546 if (ini->nSections > INT_MAX)
549 size_t length = (
sizeof(
char*) * ini->nSections) +
sizeof(char);
551 for (
size_t index = 0; index < ini->nSections; index++)
553 wIniFileSection* section = ini->sections[index];
554 const size_t nameLength = strlen(section->name);
555 length += (nameLength + 1);
558 char** sectionNames = (
char**)calloc(length,
sizeof(
char*));
563 char* p = (
char*)&((BYTE*)sectionNames)[
sizeof(
char*) * ini->nSections];
565 for (
size_t index = 0; index < ini->nSections; index++)
567 sectionNames[index] = p;
568 wIniFileSection* section = ini->sections[index];
569 const size_t nameLength = strlen(section->name);
570 CopyMemory(p, section->name, nameLength + 1);
571 p += (nameLength + 1);
575 *count = ini->nSections;
579char** IniFile_GetSectionKeyNames(wIniFile* ini,
const char* section,
size_t* count)
583 if (!section || !count)
586 wIniFileSection* pSection = IniFile_GetSection(ini, section);
591 if (pSection->nKeys > INT_MAX)
594 size_t length = (
sizeof(
char*) * pSection->nKeys) +
sizeof(char);
596 for (
size_t index = 0; index < pSection->nKeys; index++)
598 wIniFileKey* pKey = pSection->keys[index];
599 const size_t nameLength = strlen(pKey->name);
600 length += (nameLength + 1);
603 char** keyNames = (
char**)calloc(length,
sizeof(
char*));
608 char* p = (
char*)&((BYTE*)keyNames)[
sizeof(
char*) * pSection->nKeys];
610 for (
size_t index = 0; index < pSection->nKeys; index++)
613 wIniFileKey* pKey = pSection->keys[index];
614 const size_t nameLength = strlen(pKey->name);
615 CopyMemory(p, pKey->name, nameLength + 1);
616 p += (nameLength + 1);
620 *count = pSection->nKeys;
624const char* IniFile_GetKeyValueString(wIniFile* ini,
const char* section,
const char* key)
626 const char* value = NULL;
627 wIniFileKey* pKey = NULL;
628 wIniFileSection* pSection = NULL;
632 pSection = IniFile_GetSection(ini, section);
637 pKey = IniFile_GetKey(pSection, key);
642 value = (
const char*)pKey->value;
646int IniFile_GetKeyValueInt(wIniFile* ini,
const char* section,
const char* key)
650 wIniFileKey* pKey = NULL;
651 wIniFileSection* pSection = NULL;
655 pSection = IniFile_GetSection(ini, section);
660 pKey = IniFile_GetKey(pSection, key);
667 value = strtol(pKey->value, NULL, 0);
668 if ((value < INT_MIN) || (value > INT_MAX) || (errno != 0))
676int IniFile_SetKeyValueString(wIniFile* ini,
const char* section,
const char* key,
679 wIniFileKey* pKey = NULL;
682 wIniFileSection* pSection = IniFile_GetSection(ini, section);
685 pSection = IniFile_AddToSection(ini, section);
690 pKey = IniFile_AddKey(pSection, key, value);
698int IniFile_SetKeyValueInt(wIniFile* ini,
const char* section,
const char* key,
int value)
700 char strVal[128] = { 0 };
701 wIniFileKey* pKey = NULL;
702 wIniFileSection* pSection = NULL;
706 (void)sprintf_s(strVal,
sizeof(strVal),
"%d", value);
707 pSection = IniFile_GetSection(ini, section);
710 pSection = IniFile_AddToSection(ini, section);
715 pKey = IniFile_AddKey(pSection, key, strVal);
723char* IniFile_WriteBuffer(wIniFile* ini)
731 for (
size_t i = 0; i < ini->nSections; i++)
733 wIniFileSection* section = ini->sections[i];
734 size += (strlen(section->name) + 3);
736 for (
size_t j = 0; j < section->nKeys; j++)
738 wIniFileKey* key = section->keys[j];
739 size += (strlen(key->name) + strlen(key->value) + 2);
746 buffer = calloc(size + 1,
sizeof(
char));
753 for (
size_t i = 0; i < ini->nSections; i++)
755 wIniFileSection* section = ini->sections[i];
756 (void)sprintf_s(&buffer[offset], size - offset,
"[%s]\n", section->name);
757 offset += (strlen(section->name) + 3);
759 for (
size_t j = 0; j < section->nKeys; j++)
761 wIniFileKey* key = section->keys[j];
762 (void)sprintf_s(&buffer[offset], size - offset,
"%s=%s\n", key->name, key->value);
763 offset += (strlen(key->name) + strlen(key->value) + 2);
766 (void)sprintf_s(&buffer[offset], size - offset,
"\n");
773int IniFile_WriteFile(wIniFile* ini,
const char* filename)
779 char* buffer = IniFile_WriteBuffer(ini);
784 const size_t length = strlen(buffer);
785 ini->readOnly = FALSE;
788 filename = ini->filename;
790 FILE* fp = IniFile_Open_File(ini, filename);
794 if (fwrite((
void*)buffer, length, 1, fp) != 1)
800 IniFile_Close_File(fp);
805void IniFile_Free(wIniFile* ini)
810 IniFile_SetFilename(ini, NULL);
812 for (
size_t index = 0; index < ini->nSections; index++)
813 IniFile_Section_Free(ini->sections[index]);
815 free((
void*)ini->sections);
820wIniFile* IniFile_New(
void)
822 wIniFile* ini = (wIniFile*)calloc(1,
sizeof(wIniFile));
827 if (!IniFile_SectionResize(ini, 64))
833 WINPR_PRAGMA_DIAG_PUSH
834 WINPR_PRAGMA_DIAG_IGNORED_MISMATCHED_DEALLOC
836 WINPR_PRAGMA_DIAG_POP
840wIniFile* IniFile_Clone(
const wIniFile* ini)
845 wIniFile* copy = IniFile_New();
849 copy->lineLength = ini->lineLength;
850 if (!IniFile_SetFilename(copy, ini->filename))
853 if (ini->buffersize > 0)
855 if (!IniFile_BufferResize(copy, ini->buffersize))
857 memcpy(copy->buffer, ini->buffer, copy->buffersize);
860 copy->readOnly = ini->readOnly;
862 for (
size_t x = 0; x < ini->nSections; x++)
864 const wIniFileSection* cur = ini->sections[x];
868 wIniFileSection* scopy = IniFile_AddToSection(copy, cur->name);
872 for (
size_t y = 0; y < cur->nKeys; y++)
874 const wIniFileKey* key = cur->keys[y];
878 IniFile_AddKey(scopy, key->name, key->value);