INI.CPP 71 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284
  1. /*
  2. ** Command & Conquer Red Alert(tm)
  3. ** Copyright 2025 Electronic Arts Inc.
  4. **
  5. ** This program is free software: you can redistribute it and/or modify
  6. ** it under the terms of the GNU General Public License as published by
  7. ** the Free Software Foundation, either version 3 of the License, or
  8. ** (at your option) any later version.
  9. **
  10. ** This program is distributed in the hope that it will be useful,
  11. ** but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. ** GNU General Public License for more details.
  14. **
  15. ** You should have received a copy of the GNU General Public License
  16. ** along with this program. If not, see <http://www.gnu.org/licenses/>.
  17. */
  18. /* $Header: /CounterStrike/INI.CPP 1 3/03/97 10:24a Joe_bostic $ */
  19. /***********************************************************************************************
  20. *** C O N F I D E N T I A L --- W E S T W O O D S T U D I O S ***
  21. ***********************************************************************************************
  22. * *
  23. * Project Name : Command & Conquer *
  24. * *
  25. * File Name : INI.CPP *
  26. * *
  27. * Programmer : Joe L. Bostic *
  28. * *
  29. * Start Date : September 10, 1993 *
  30. * *
  31. * Last Update : November 2, 1996 [JLB] *
  32. * *
  33. *---------------------------------------------------------------------------------------------*
  34. * Functions: *
  35. * INIClass::Clear -- Clears out a section (or all sections) of the INI data. *
  36. * INIClass::Entry_Count -- Fetches the number of entries in a specified section. *
  37. * INIClass::Find_Entry -- Find specified entry within section. *
  38. * INIClass::Find_Section -- Find the specified section within the INI data. *
  39. * INIClass::Get_Bool -- Fetch a boolean value for the section and entry specified. *
  40. * INIClass::Get_Entry -- Get the entry identifier name given ordinal number and section name*
  41. * INIClass::Get_Fixed -- Fetch a fixed point number from the section & entry. *
  42. * INIClass::Put_Fixed -- Store a fixed point number to the INI database. *
  43. * INIClass::Get_Hex -- Fetches integer [hex format] from the section and entry specified. *
  44. * INIClass::Get_Int -- Fetch an integer entry from the specified section. *
  45. * INIClass::Get_PKey -- Fetch a key from the ini database. *
  46. * INIClass::Get_String -- Fetch the value of a particular entry in a specified section. *
  47. * INIClass::Get_TextBlock -- Fetch a block of normal text. *
  48. * INIClass::Get_UUBlock -- Fetch an encoded block from the section specified. *
  49. * INIClass::INISection::Find_Entry -- Finds a specified entry and returns pointer to it. *
  50. * INIClass::Load -- Load INI data from the file specified. *
  51. * INIClass::Load -- Load the INI data from the data stream (straw). *
  52. * INIClass::Put_Bool -- Store a boolean value into the INI database. *
  53. * INIClass::Put_Hex -- Store an integer into the INI database, but use a hex format. *
  54. * INIClass::Put_Int -- Stores a signed integer into the INI data base. *
  55. * INIClass::Put_PKey -- Stores the key to the INI database. *
  56. * INIClass::Put_String -- Output a string to the section and entry specified. *
  57. * INIClass::Put_TextBlock -- Stores a block of text into an INI section. *
  58. * INIClass::Put_UUBlock -- Store a binary encoded data block into the INI database. *
  59. * INIClass::Save -- Save the ini data to the file specified. *
  60. * INIClass::Save -- Saves the INI data to a pipe stream. *
  61. * INIClass::Section_Count -- Counts the number of sections in the INI data. *
  62. * INIClass::Strip_Comments -- Strips comments of the specified text line. *
  63. * INIClass::~INIClass -- Destructor for INI handler. *
  64. * - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
  65. #include <string.h>
  66. #include <stdlib.h>
  67. #include <stddef.h>
  68. #include <stdio.h>
  69. #include <ctype.h>
  70. #include "ini.h"
  71. #include "readline.h"
  72. #include "xpipe.h"
  73. #include "b64pipe.h"
  74. #include "xstraw.h"
  75. #include "b64straw.h"
  76. #ifdef FIXIT_FAST_LOAD
  77. #include "cstraw.h"
  78. #endif
  79. // Disable the "temporary object used to initialize a non-constant reference" warning.
  80. #pragma warning 665 9
  81. /***********************************************************************************************
  82. * INIClass::~INIClass -- Destructor for INI handler. *
  83. * *
  84. * This is the destructor for the INI class. It handles deleting all of the allocations *
  85. * it might have done. *
  86. * *
  87. * INPUT: none *
  88. * *
  89. * OUTPUT: none *
  90. * *
  91. * WARNINGS: none *
  92. * *
  93. * HISTORY: *
  94. * 07/02/1996 JLB : Created. *
  95. *=============================================================================================*/
  96. INIClass::~INIClass(void)
  97. {
  98. Clear();
  99. }
  100. /***********************************************************************************************
  101. * INIClass::Clear -- Clears out a section (or all sections) of the INI data. *
  102. * *
  103. * This routine is used to clear out the section specified. If no section is specified, *
  104. * then the entire INI data is cleared out. Optionally, this routine can be used to clear *
  105. * out just an individual entry in the specified section. *
  106. * *
  107. * INPUT: section -- Pointer to the section to clear out [pass NULL to clear all]. *
  108. * *
  109. * entry -- Pointer to optional entry specifier. If this parameter is specified, *
  110. * then only this specific entry (if found) will be cleared. Otherwise, *
  111. * the entire section specified will be cleared. *
  112. * *
  113. * OUTPUT: none *
  114. * *
  115. * WARNINGS: none *
  116. * *
  117. * HISTORY: *
  118. * 07/02/1996 JLB : Created. *
  119. * 08/21/1996 JLB : Optionally clears section too. *
  120. * 11/02/1996 JLB : Updates the index list. *
  121. *=============================================================================================*/
  122. bool INIClass::Clear(char const * section, char const * entry)
  123. {
  124. if (section == NULL) {
  125. SectionList.Delete();
  126. SectionIndex.Clear();
  127. } else {
  128. INISection * secptr = Find_Section(section);
  129. if (secptr != NULL) {
  130. if (entry != NULL) {
  131. INIEntry * entptr = secptr->Find_Entry(entry);
  132. if (entptr != NULL) {
  133. /*
  134. ** Remove the entry from the entry index list.
  135. */
  136. secptr->EntryIndex.Remove_Index(entptr->Index_ID());
  137. delete entptr;
  138. }
  139. } else {
  140. /*
  141. ** Remove this section index from the section index list.
  142. */
  143. SectionIndex.Remove_Index(secptr->Index_ID());
  144. delete secptr;
  145. }
  146. }
  147. }
  148. return(true);
  149. }
  150. /***********************************************************************************************
  151. * INIClass::Load -- Load INI data from the file specified. *
  152. * *
  153. * Use this routine to load the INI class with the data from the specified file. *
  154. * *
  155. * INPUT: file -- Reference to the file that will be used to fill up this INI manager. *
  156. * *
  157. * OUTPUT: bool; Was the file loaded successfully? *
  158. * *
  159. * WARNINGS: This routine allocates memory. *
  160. * *
  161. * HISTORY: *
  162. * 07/02/1996 JLB : Created. *
  163. *=============================================================================================*/
  164. bool INIClass::Load(FileClass & file)
  165. {
  166. return(Load(FileStraw(file)));
  167. }
  168. /***********************************************************************************************
  169. * INIClass::Load -- Load the INI data from the data stream (straw). *
  170. * *
  171. * This will fetch data from the straw and build an INI database from it. *
  172. * *
  173. * INPUT: straw -- The straw that the data will be provided from. *
  174. * *
  175. * OUTPUT: bool; Was the database loaded ok? *
  176. * *
  177. * WARNINGS: none *
  178. * *
  179. * HISTORY: *
  180. * 07/10/1996 JLB : Created. *
  181. *=============================================================================================*/
  182. #ifdef FIXIT_FAST_LOAD
  183. bool INIClass::Load(Straw & ffile)
  184. #else
  185. bool INIClass::Load(Straw & file)
  186. #endif
  187. {
  188. bool end_of_file = false;
  189. char buffer[MAX_LINE_LENGTH];
  190. #ifdef FIXIT_FAST_LOAD
  191. CacheStraw file;
  192. file.Get_From(ffile);
  193. #endif
  194. /*
  195. ** Prescan until the first section is found.
  196. */
  197. while (!end_of_file) {
  198. Read_Line(file, buffer, sizeof(buffer), end_of_file);
  199. if (end_of_file) return(false);
  200. if (buffer[0] == '[' && strchr(buffer, ']') != NULL) break;
  201. }
  202. /*
  203. ** Process a section. The buffer is prefilled with the section name line.
  204. */
  205. while (!end_of_file) {
  206. buffer[0] = ' ';
  207. char * ptr = strchr(buffer, ']');
  208. if (ptr) *ptr = '\0';
  209. strtrim(buffer);
  210. INISection * secptr = new INISection(strdup(buffer));
  211. if (secptr == NULL) {
  212. Clear();
  213. return(false);
  214. }
  215. /*
  216. ** Read in the entries of this section.
  217. */
  218. while (!end_of_file) {
  219. /*
  220. ** If this line is the start of another section, then bail out
  221. ** of the entry loop and let the outer section loop take
  222. ** care of it.
  223. */
  224. int len = Read_Line(file, buffer, sizeof(buffer), end_of_file);
  225. if (buffer[0] == '[' && strchr(buffer, ']') != NULL) break;
  226. /*
  227. ** Determine if this line is a comment or blank line. Throw it out if it is.
  228. */
  229. Strip_Comments(buffer);
  230. if (len == 0 || buffer[0] == ';' || buffer[0] == '=') continue;
  231. /*
  232. ** The line isn't an obvious comment. Make sure that there is the "=" character
  233. ** at an appropriate spot.
  234. */
  235. char * divider = strchr(buffer, '=');
  236. if (!divider) continue;
  237. /*
  238. ** Split the line into entry and value sections. Be sure to catch the
  239. ** "=foobar" and "foobar=" cases. These lines are ignored.
  240. */
  241. *divider++ = '\0';
  242. strtrim(buffer);
  243. if (!strlen(buffer)) continue;
  244. strtrim(divider);
  245. if (!strlen(divider)) continue;
  246. INIEntry * entryptr = new INIEntry(strdup(buffer), strdup(divider));
  247. if (entryptr == NULL) {
  248. delete secptr;
  249. Clear();
  250. return(false);
  251. }
  252. secptr->EntryIndex.Add_Index(entryptr->Index_ID(), entryptr);
  253. secptr->EntryList.Add_Tail(entryptr);
  254. }
  255. /*
  256. ** All the entries for this section have been parsed. If this section is blank, then
  257. ** don't bother storing it.
  258. */
  259. if (secptr->EntryList.Is_Empty()) {
  260. delete secptr;
  261. } else {
  262. SectionIndex.Add_Index(secptr->Index_ID(), secptr);
  263. SectionList.Add_Tail(secptr);
  264. }
  265. }
  266. return(true);
  267. }
  268. /***********************************************************************************************
  269. * INIClass::Save -- Save the ini data to the file specified. *
  270. * *
  271. * Use this routine to save the ini data to the file specified. All existing data in the *
  272. * file, if it was present, is replaced. *
  273. * *
  274. * INPUT: file -- Reference to the file to write the INI data to. *
  275. * *
  276. * OUTPUT: bool; Was the data written to the file? *
  277. * *
  278. * WARNINGS: none *
  279. * *
  280. * HISTORY: *
  281. * 07/02/1996 JLB : Created. *
  282. *=============================================================================================*/
  283. int INIClass::Save(FileClass & file) const
  284. {
  285. return(Save(FilePipe(file)));
  286. }
  287. /***********************************************************************************************
  288. * INIClass::Save -- Saves the INI data to a pipe stream. *
  289. * *
  290. * This routine will output the data of the INI file to a pipe stream. *
  291. * *
  292. * INPUT: pipe -- Reference to the pipe stream to pump the INI image to. *
  293. * *
  294. * OUTPUT: Returns with the number of bytes output to the pipe. *
  295. * *
  296. * WARNINGS: none *
  297. * *
  298. * HISTORY: *
  299. * 07/02/1996 JLB : Created. *
  300. *=============================================================================================*/
  301. int INIClass::Save(Pipe & pipe) const
  302. {
  303. int total = 0;
  304. INISection * secptr = SectionList.First();
  305. while (secptr && secptr->Is_Valid()) {
  306. /*
  307. ** Output the section identifier.
  308. */
  309. total += pipe.Put("[", 1);
  310. total += pipe.Put(secptr->Section, strlen(secptr->Section));
  311. total += pipe.Put("]", 1);
  312. total += pipe.Put("\r\n", strlen("\r\n"));
  313. /*
  314. ** Output all the entries and values in this section.
  315. */
  316. INIEntry * entryptr = secptr->EntryList.First();
  317. while (entryptr && entryptr->Is_Valid()) {
  318. total += pipe.Put(entryptr->Entry, strlen(entryptr->Entry));
  319. total += pipe.Put("=", 1);
  320. total += pipe.Put(entryptr->Value, strlen(entryptr->Value));
  321. total += pipe.Put("\r\n", strlen("\r\n"));
  322. entryptr = entryptr->Next();
  323. }
  324. /*
  325. ** After the last entry in this section, output an extra
  326. ** blank line for readability purposes.
  327. */
  328. total += pipe.Put("\r\n", strlen("\r\n"));
  329. secptr = secptr->Next();
  330. }
  331. total += pipe.End();
  332. return(total);
  333. }
  334. /***********************************************************************************************
  335. * INIClass::Find_Section -- Find the specified section within the INI data. *
  336. * *
  337. * This routine will scan through the INI data looking for the section specified. If the *
  338. * section could be found, then a pointer to the section control data is returned. *
  339. * *
  340. * INPUT: section -- The name of the section to search for. Don't enclose the name in *
  341. * brackets. Case is NOT sensitive in the search. *
  342. * *
  343. * OUTPUT: Returns with a pointer to the INI section control structure if the section was *
  344. * found. Otherwise, NULL is returned. *
  345. * *
  346. * WARNINGS: none *
  347. * *
  348. * HISTORY: *
  349. * 07/02/1996 JLB : Created. *
  350. * 11/02/1996 JLB : Uses index manager. *
  351. *=============================================================================================*/
  352. INIClass::INISection * INIClass::Find_Section(char const * section) const
  353. {
  354. if (section != NULL) {
  355. long crc = CRCEngine()(section, strlen(section));
  356. if (SectionIndex.Is_Present(crc)) {
  357. return(SectionIndex.Fetch_Index(crc));
  358. }
  359. }
  360. return(NULL);
  361. }
  362. /***********************************************************************************************
  363. * INIClass::Section_Count -- Counts the number of sections in the INI data. *
  364. * *
  365. * This routine will scan through all the sections in the INI data and return a count *
  366. * of the number it found. *
  367. * *
  368. * INPUT: none *
  369. * *
  370. * OUTPUT: Returns with the number of sections recorded in the INI data. *
  371. * *
  372. * WARNINGS: none *
  373. * *
  374. * HISTORY: *
  375. * 07/02/1996 JLB : Created. *
  376. * 11/02/1996 JLB : Uses index manager. *
  377. *=============================================================================================*/
  378. int INIClass::Section_Count(void) const
  379. {
  380. return(SectionIndex.Count());
  381. }
  382. /***********************************************************************************************
  383. * INIClass::Entry_Count -- Fetches the number of entries in a specified section. *
  384. * *
  385. * This routine will examine the section specified and return with the number of entries *
  386. * associated with it. *
  387. * *
  388. * INPUT: section -- Pointer to the section that will be examined. *
  389. * *
  390. * OUTPUT: Returns with the number entries in the specified section. *
  391. * *
  392. * WARNINGS: none *
  393. * *
  394. * HISTORY: *
  395. * 07/02/1996 JLB : Created. *
  396. * 11/02/1996 JLB : Uses index manager. *
  397. *=============================================================================================*/
  398. int INIClass::Entry_Count(char const * section) const
  399. {
  400. INISection * secptr = Find_Section(section);
  401. if (secptr != NULL) {
  402. return(secptr->EntryIndex.Count());
  403. }
  404. return(0);
  405. }
  406. /***********************************************************************************************
  407. * INIClass::Find_Entry -- Find specified entry within section. *
  408. * *
  409. * This support routine will find the specified entry in the specified section. If found, *
  410. * a pointer to the entry control structure will be returned. *
  411. * *
  412. * INPUT: section -- Pointer to the section name to search under. *
  413. * *
  414. * entry -- Pointer to the entry name to search for. *
  415. * *
  416. * OUTPUT: If the entry was found, then a pointer to the entry control structure will be *
  417. * returned. Otherwise, NULL will be returned. *
  418. * *
  419. * WARNINGS: none *
  420. * *
  421. * HISTORY: *
  422. * 07/02/1996 JLB : Created. *
  423. *=============================================================================================*/
  424. INIClass::INIEntry * INIClass::Find_Entry(char const * section, char const * entry) const
  425. {
  426. INISection * secptr = Find_Section(section);
  427. if (secptr != NULL) {
  428. return(secptr->Find_Entry(entry));
  429. }
  430. return(NULL);
  431. }
  432. /***********************************************************************************************
  433. * INIClass::Get_Entry -- Get the entry identifier name given ordinal number and section name. *
  434. * *
  435. * This will return the identifier name for the entry under the section specified. The *
  436. * ordinal number specified is used to determine which entry to retrieve. The entry *
  437. * identifier is the text that appears to the left of the "=" character. *
  438. * *
  439. * INPUT: section -- The section to use. *
  440. * *
  441. * index -- The ordinal number to use when fetching an entry name. *
  442. * *
  443. * OUTPUT: Returns with a pointer to the entry name. *
  444. * *
  445. * WARNINGS: none *
  446. * *
  447. * HISTORY: *
  448. * 07/02/1996 JLB : Created. *
  449. *=============================================================================================*/
  450. char const * INIClass::Get_Entry(char const * section, int index) const
  451. {
  452. INISection * secptr = Find_Section(section);
  453. if (secptr != NULL && index < secptr->EntryIndex.Count()) {
  454. INIEntry * entryptr = secptr->EntryList.First();
  455. while (entryptr != NULL && entryptr->Is_Valid()) {
  456. if (index == 0) return(entryptr->Entry);
  457. index--;
  458. entryptr = entryptr->Next();
  459. }
  460. }
  461. return(NULL);
  462. }
  463. /***********************************************************************************************
  464. * INIClass::Put_UUBlock -- Store a binary encoded data block into the INI database. *
  465. * *
  466. * Use this routine to store an arbitrary length binary block of data into the INI database.*
  467. * This routine will covert the data into displayable form and then break it into lines *
  468. * that are stored in sequence to the section. A section used to store data in this *
  469. * fashion can not be used for any other entries. *
  470. * *
  471. * INPUT: section -- The section identifier to place the data into. *
  472. * *
  473. * block -- Pointer to the block of binary data to store. *
  474. * *
  475. * len -- The length of the binary data. *
  476. * *
  477. * OUTPUT: bool; Was the data stored to the database? *
  478. * *
  479. * WARNINGS: none *
  480. * *
  481. * HISTORY: *
  482. * 07/03/1996 JLB : Created. *
  483. *=============================================================================================*/
  484. bool INIClass::Put_UUBlock(char const * section, void const * block, int len)
  485. {
  486. if (section == NULL || block == NULL || len < 1) return(false);
  487. Clear(section);
  488. BufferStraw straw(block, len);
  489. Base64Straw bstraw(Base64Straw::ENCODE);
  490. bstraw.Get_From(straw);
  491. int counter = 1;
  492. for (;;) {
  493. char buffer[71];
  494. char sbuffer[32];
  495. int length = bstraw.Get(buffer, sizeof(buffer)-1);
  496. buffer[length] = '\0';
  497. if (length == 0) break;
  498. sprintf(sbuffer, "%d", counter);
  499. Put_String(section, sbuffer, buffer);
  500. counter++;
  501. }
  502. return(true);
  503. }
  504. /***********************************************************************************************
  505. * INIClass::Get_UUBlock -- Fetch an encoded block from the section specified. *
  506. * *
  507. * This routine will take all the entries in the specified section and decompose them into *
  508. * a binary block of data that will be stored into the buffer specified. By using this *
  509. * routine [and the Put_UUBLock counterpart], arbitrary blocks of binary data may be *
  510. * stored in the INI file. A section processed by this routine can contain no other *
  511. * entries than those put there by a previous call to Put_UUBlock. *
  512. * *
  513. * INPUT: section -- The section name to process. *
  514. * *
  515. * block -- Pointer to the buffer that will hold the retrieved data. *
  516. * *
  517. * len -- The length of the buffer. The retrieved data will not fill past this *
  518. * limit. *
  519. * *
  520. * OUTPUT: Returns with the number of bytes decoded into the buffer specified. *
  521. * *
  522. * WARNINGS: If the number of bytes retrieved exactly matches the length of the buffer *
  523. * specified, then you might have a condition of buffer "overflow". *
  524. * *
  525. * HISTORY: *
  526. * 07/02/1996 JLB : Created. *
  527. *=============================================================================================*/
  528. int INIClass::Get_UUBlock(char const * section, void * block, int len) const
  529. {
  530. if (section == NULL) return(0);
  531. Base64Pipe b64pipe(Base64Pipe::DECODE);
  532. BufferPipe bpipe(block, len);
  533. b64pipe.Put_To(&bpipe);
  534. int total = 0;
  535. int counter = Entry_Count(section);
  536. for (int index = 0; index < counter; index++) {
  537. char buffer[128];
  538. int length = Get_String(section, Get_Entry(section, index), "=", buffer, sizeof(buffer));
  539. int outcount = b64pipe.Put(buffer, length);
  540. total += outcount;
  541. }
  542. total += b64pipe.End();
  543. return(total);
  544. }
  545. /***********************************************************************************************
  546. * INIClass::Put_TextBlock -- Stores a block of text into an INI section. *
  547. * *
  548. * This routine will take an arbitrarily long block of text and store it into the INI *
  549. * database. The text is broken up into lines and each line is then stored as a numbered *
  550. * entry in the specified section. A section used to store text in this way can not be used *
  551. * to hold any other entries. The text is presumed to contain space characters scattered *
  552. * throughout it and that one space between words and sentences is natural. *
  553. * *
  554. * INPUT: section -- The section to place the text block into. *
  555. * *
  556. * text -- Pointer to a null terminated text string that holds the block of *
  557. * text. The length can be arbitrary. *
  558. * *
  559. * OUTPUT: bool; Was the text block placed into the database? *
  560. * *
  561. * WARNINGS: none *
  562. * *
  563. * HISTORY: *
  564. * 07/03/1996 JLB : Created. *
  565. *=============================================================================================*/
  566. bool INIClass::Put_TextBlock(char const * section, char const * text)
  567. {
  568. if (section == NULL) return(false);
  569. Clear(section);
  570. int index = 1;
  571. while (text != NULL && *text != NULL) {
  572. char buffer[128];
  573. strncpy(buffer, text, 75);
  574. buffer[75] = '\0';
  575. char b[32];
  576. sprintf(b, "%d", index);
  577. /*
  578. ** Scan backward looking for a good break position.
  579. */
  580. int count = strlen(buffer);
  581. if (count > 0) {
  582. if (count >= 75) {
  583. while (count) {
  584. char c = buffer[count];
  585. if (isspace(c)) break;
  586. count--;
  587. }
  588. if (count == 0) {
  589. break;
  590. } else {
  591. buffer[count] = '\0';
  592. }
  593. }
  594. strtrim(buffer);
  595. Put_String(section, b, buffer);
  596. index++;
  597. text = ((char *)text) + count;
  598. } else {
  599. break;
  600. }
  601. }
  602. return(true);
  603. }
  604. /***********************************************************************************************
  605. * INIClass::Get_TextBlock -- Fetch a block of normal text. *
  606. * *
  607. * This will take all entries in the specified section and format them into a block of *
  608. * normalized text. That is, text with single spaces between each concatenated line. All *
  609. * entries in the specified section are processed by this routine. Use Put_TextBlock to *
  610. * build the entries in the section. *
  611. * *
  612. * INPUT: section -- The section name to process. *
  613. * *
  614. * buffer -- Pointer to the buffer that will hold the complete text. *
  615. * *
  616. * len -- The length of the buffer specified. The text will, at most, fill this *
  617. * buffer with the last character being forced to null. *
  618. * *
  619. * OUTPUT: Returns with the number of characters placed into the buffer. The trailing null *
  620. * is not counted. *
  621. * *
  622. * WARNINGS: none *
  623. * *
  624. * HISTORY: *
  625. * 07/02/1996 JLB : Created. *
  626. *=============================================================================================*/
  627. int INIClass::Get_TextBlock(char const * section, char * buffer, int len) const
  628. {
  629. if (len <= 0) return(0);
  630. buffer[0] = '\0';
  631. if (len <= 1) return(0);
  632. int elen = Entry_Count(section);
  633. int total = 0;
  634. for (int index = 0; index < elen; index++) {
  635. /*
  636. ** Add spacers between lines of fetched text.
  637. */
  638. if (index > 0) {
  639. *buffer++ = ' ';
  640. len--;
  641. total++;
  642. }
  643. Get_String(section, Get_Entry(section, index), "", buffer, len);
  644. int partial = strlen(buffer);
  645. total += partial;
  646. buffer += partial;
  647. len -= partial;
  648. if (len <= 1) break;
  649. }
  650. return(total);
  651. }
  652. /***********************************************************************************************
  653. * INIClass::Put_Int -- Stores a signed integer into the INI data base. *
  654. * *
  655. * Use this routine to store an integer value into the section and entry specified. *
  656. * *
  657. * INPUT: section -- The identifier for the section that the entry will be placed in. *
  658. * *
  659. * entry -- The entry identifier used for the integer number. *
  660. * *
  661. * number -- The integer number to store in the database. *
  662. * *
  663. * format -- The format to store the integer. The format is generally only a *
  664. * cosmetic affect. The Get_Int operation will interpret the value the *
  665. * same regardless of what format was used to store the integer. *
  666. * *
  667. * 0 : plain decimal digit *
  668. * 1 : hexadecimal digit (trailing "h") *
  669. * 2 : hexadecimal digit (leading "$") *
  670. * *
  671. * OUTPUT: bool; Was the number stored? *
  672. * *
  673. * WARNINGS: none *
  674. * *
  675. * HISTORY: *
  676. * 07/03/1996 JLB : Created. *
  677. * 07/10/1996 JLB : Handles multiple integer formats. *
  678. *=============================================================================================*/
  679. bool INIClass::Put_Int(char const * section, char const * entry, int number, int format)
  680. {
  681. char buffer[MAX_LINE_LENGTH];
  682. switch (format) {
  683. default:
  684. case 0:
  685. sprintf(buffer, "%d", number);
  686. break;
  687. case 1:
  688. sprintf(buffer, "%Xh", number);
  689. break;
  690. case 2:
  691. sprintf(buffer, "$%X", number);
  692. break;
  693. }
  694. return(Put_String(section, entry, buffer));
  695. }
  696. /***********************************************************************************************
  697. * INIClass::Get_Int -- Fetch an integer entry from the specified section. *
  698. * *
  699. * This routine will fetch an integer value from the entry and section specified. If no *
  700. * entry could be found, then the default value will be returned instead. *
  701. * *
  702. * INPUT: section -- The section name to search under. *
  703. * *
  704. * entry -- The entry name to search for. *
  705. * *
  706. * defvalue -- The default value to use if the specified entry could not be found. *
  707. * *
  708. * OUTPUT: Returns with the integer value specified in the INI database or else returns the *
  709. * default value. *
  710. * *
  711. * WARNINGS: none *
  712. * *
  713. * HISTORY: *
  714. * 07/02/1996 JLB : Created. *
  715. * 07/10/1996 JLB : Handles multiple integer formats. *
  716. *=============================================================================================*/
  717. int INIClass::Get_Int(char const * section, char const * entry, int defvalue) const
  718. {
  719. /*
  720. ** Verify that the parameters are nominally correct.
  721. */
  722. if (section == NULL || entry == NULL) return(defvalue);
  723. INIEntry * entryptr = Find_Entry(section, entry);
  724. if (entryptr && entryptr->Value != NULL) {
  725. if (*entryptr->Value == '$') {
  726. sscanf(entryptr->Value, "$%x", &defvalue);
  727. } else {
  728. if (tolower(entryptr->Value[strlen(entryptr->Value)-1]) == 'h') {
  729. sscanf(entryptr->Value, "%xh", &defvalue);
  730. } else {
  731. defvalue = atoi(entryptr->Value);
  732. }
  733. }
  734. }
  735. return(defvalue);
  736. }
  737. /***********************************************************************************************
  738. * INIClass::Put_Hex -- Store an integer into the INI database, but use a hex format. *
  739. * *
  740. * This routine is similar to the Put_Int routine, but the number is stored as a hexadecimal*
  741. * number. *
  742. * *
  743. * INPUT: section -- The identifier for the section that the entry will be placed in. *
  744. * *
  745. * entry -- The entry identifier to tag to the integer number specified. *
  746. * *
  747. * number -- The number to assign the the specified entry and placed in the *
  748. * specified section. *
  749. * *
  750. * OUTPUT: bool; Was the number placed into the INI database? *
  751. * *
  752. * WARNINGS: none *
  753. * *
  754. * HISTORY: *
  755. * 07/03/1996 JLB : Created. *
  756. *=============================================================================================*/
  757. bool INIClass::Put_Hex(char const * section, char const * entry, int number)
  758. {
  759. char buffer[MAX_LINE_LENGTH];
  760. sprintf(buffer, "%X", number);
  761. return(Put_String(section, entry, buffer));
  762. }
  763. /***********************************************************************************************
  764. * INIClass::Get_Hex -- Fetches integer [hex format] from the section and entry specified. *
  765. * *
  766. * This routine will search under the section specified, looking for a matching entry. The *
  767. * value is interpreted as a hexadecimal number and then returned. If no entry could be *
  768. * found, then the default value is returned instead. *
  769. * *
  770. * INPUT: section -- The section identifier to search under. *
  771. * *
  772. * entry -- The entry identifier to search for. *
  773. * *
  774. * defvalue -- The default value to use if the entry could not be located. *
  775. * *
  776. * OUTPUT: Returns with the integer value from the specified section and entry. If no entry *
  777. * could be found, then the default value will be returned instead. *
  778. * *
  779. * WARNINGS: none *
  780. * *
  781. * HISTORY: *
  782. * 07/02/1996 JLB : Created. *
  783. *=============================================================================================*/
  784. int INIClass::Get_Hex(char const * section, char const * entry, int defvalue) const
  785. {
  786. /*
  787. ** Verify that the parameters are nominally correct.
  788. */
  789. if (section == NULL || entry == NULL) return(defvalue);
  790. INIEntry * entryptr = Find_Entry(section, entry);
  791. if (entryptr && entryptr->Value != NULL) {
  792. sscanf(entryptr->Value, "%x", &defvalue);
  793. }
  794. return(defvalue);
  795. }
  796. /***********************************************************************************************
  797. * INIClass::Put_String -- Output a string to the section and entry specified. *
  798. * *
  799. * This routine will put an arbitrary string to the section and entry specified. Any *
  800. * previous matching entry will be replaced. *
  801. * *
  802. * INPUT: section -- The section identifier to place the string under. *
  803. * *
  804. * entry -- The entry identifier to identify this string [placed under the section]*
  805. * *
  806. * string -- Pointer to the string to assign to this entry. *
  807. * *
  808. * OUTPUT: bool; Was the entry assigned without error? *
  809. * *
  810. * WARNINGS: none *
  811. * *
  812. * HISTORY: *
  813. * 07/02/1996 JLB : Created. *
  814. * 11/02/1996 JLB : Uses index handler. *
  815. *=============================================================================================*/
  816. bool INIClass::Put_String(char const * section, char const * entry, char const * string)
  817. {
  818. if (section == NULL || entry == NULL) return(false);
  819. INISection * secptr = Find_Section(section);
  820. if (secptr == NULL) {
  821. secptr = new INISection(strdup(section));
  822. if (secptr == NULL) return(false);
  823. SectionList.Add_Tail(secptr);
  824. SectionIndex.Add_Index(secptr->Index_ID(), secptr);
  825. }
  826. /*
  827. ** Remove the old entry if found.
  828. */
  829. INIEntry * entryptr = secptr->Find_Entry(entry);
  830. if (entryptr != NULL) {
  831. secptr->EntryIndex.Remove_Index(entryptr->Index_ID());
  832. delete entryptr;
  833. }
  834. /*
  835. ** Create and add the new entry.
  836. */
  837. if (string != NULL && strlen(string) > 0) {
  838. entryptr = new INIEntry(strdup(entry), strdup(string));
  839. if (entryptr == NULL) {
  840. return(false);
  841. }
  842. secptr->EntryList.Add_Tail(entryptr);
  843. secptr->EntryIndex.Add_Index(entryptr->Index_ID(), entryptr);
  844. }
  845. return(true);
  846. }
  847. /***********************************************************************************************
  848. * INIClass::Get_String -- Fetch the value of a particular entry in a specified section. *
  849. * *
  850. * This will retrieve the entire text to the right of the "=" character. The text is *
  851. * found by finding a matching entry in the section specified. If no matching entry could *
  852. * be found, then the default value will be stored in the output string buffer. *
  853. * *
  854. * INPUT: section -- Pointer to the section name to search under. *
  855. * *
  856. * entry -- The entry identifier to search for. *
  857. * *
  858. * defvalue -- If no entry could be found, then this text will be returned. *
  859. * *
  860. * buffer -- Output buffer to store the retrieved string into. *
  861. * *
  862. * size -- The size of the output buffer. The maximum string length that could *
  863. * be retrieved will be one less than this length. This is due to the *
  864. * forced trailing zero added to the end of the string. *
  865. * *
  866. * OUTPUT: Returns with the length of the string retrieved. *
  867. * *
  868. * WARNINGS: none *
  869. * *
  870. * HISTORY: *
  871. * 07/02/1996 JLB : Created. *
  872. *=============================================================================================*/
  873. int INIClass::Get_String(char const * section, char const * entry, char const * defvalue, char * buffer, int size) const
  874. {
  875. /*
  876. ** Verify that the parameters are nominally legal.
  877. */
  878. if (buffer != NULL && size > 0) {
  879. buffer[0] = '\0';
  880. }
  881. if (buffer == NULL || !size || section == NULL || entry == NULL) return(0);
  882. /*
  883. ** Fetch the entry string if it is present. If not, then the normal default
  884. ** value will be used as the entry value.
  885. */
  886. INIEntry * entryptr = Find_Entry(section, entry);
  887. if (entryptr) {
  888. if (entryptr->Value) {
  889. defvalue = entryptr->Value;
  890. }
  891. }
  892. /*
  893. ** Fill in the buffer with the entry value and return with the length of the string.
  894. */
  895. if (defvalue == NULL) {
  896. buffer[0] = '\0';
  897. return(0);
  898. } else {
  899. strncpy(buffer, defvalue, size);
  900. buffer[size-1] = '\0';
  901. strtrim(buffer);
  902. return(strlen(buffer));
  903. }
  904. }
  905. /***********************************************************************************************
  906. * INIClass::Put_Bool -- Store a boolean value into the INI database. *
  907. * *
  908. * Use this routine to place a boolean value into the INI database. The boolean value will *
  909. * be stored as "yes" or "no". *
  910. * *
  911. * INPUT: section -- The section to place the entry and boolean value into. *
  912. * *
  913. * entry -- The entry identifier to tag to the boolean value. *
  914. * *
  915. * value -- The boolean value to place into the database. *
  916. * *
  917. * OUTPUT: bool; Was the boolean value placed into the database? *
  918. * *
  919. * WARNINGS: none *
  920. * *
  921. * HISTORY: *
  922. * 07/03/1996 JLB : Created. *
  923. *=============================================================================================*/
  924. bool INIClass::Put_Bool(char const * section, char const * entry, bool value)
  925. {
  926. if (value) {
  927. return(Put_String(section, entry, "yes"));
  928. } else {
  929. return(Put_String(section, entry, "no"));
  930. }
  931. }
  932. /***********************************************************************************************
  933. * INIClass::Get_Bool -- Fetch a boolean value for the section and entry specified. *
  934. * *
  935. * This routine will search under the section specified, looking for a matching entry. If *
  936. * one is found, the value is interpreted as a boolean value and then returned. In the case *
  937. * of no matching entry, the default value will be returned instead. The boolean value *
  938. * is interpreted using the standard boolean conventions. e.g., "Yes", "Y", "1", "True", *
  939. * "T" are all consider to be a TRUE boolean value. *
  940. * *
  941. * INPUT: section -- The section to search under. *
  942. * *
  943. * entry -- The entry to search for. *
  944. * *
  945. * defvalue -- The default value to use if no matching entry could be located. *
  946. * *
  947. * OUTPUT: Returns with the boolean value of the specified section and entry. If no match *
  948. * then the default boolean value is returned. *
  949. * *
  950. * WARNINGS: none *
  951. * *
  952. * HISTORY: *
  953. * 07/02/1996 JLB : Created. *
  954. *=============================================================================================*/
  955. bool INIClass::Get_Bool(char const * section, char const * entry, bool defvalue) const
  956. {
  957. /*
  958. ** Verify that the parameters are nominally correct.
  959. */
  960. if (section == NULL || entry == NULL) return(defvalue);
  961. INIEntry * entryptr = Find_Entry(section, entry);
  962. if (entryptr && entryptr->Value != NULL) {
  963. switch (toupper(*entryptr->Value)) {
  964. case 'Y':
  965. case 'T':
  966. case '1':
  967. return(true);
  968. case 'N':
  969. case 'F':
  970. case '0':
  971. return(false);
  972. }
  973. }
  974. return(defvalue);
  975. }
  976. /***********************************************************************************************
  977. * INIClass::INISection::Find_Entry -- Finds a specified entry and returns pointer to it. *
  978. * *
  979. * This routine scans the supplied entry for the section specified. This is used for *
  980. * internal database maintenance. *
  981. * *
  982. * INPUT: entry -- The entry to scan for. *
  983. * *
  984. * OUTPUT: Returns with a pointer to the entry control structure if the entry was found. *
  985. * Otherwise it returns NULL. *
  986. * *
  987. * WARNINGS: none *
  988. * *
  989. * HISTORY: *
  990. * 07/03/1996 JLB : Created. *
  991. * 11/02/1996 JLB : Uses index handler. *
  992. *=============================================================================================*/
  993. INIClass::INIEntry * INIClass::INISection::Find_Entry(char const * entry) const
  994. {
  995. if (entry != NULL) {
  996. int crc = CRCEngine()(entry, strlen(entry));
  997. if (EntryIndex.Is_Present(crc)) {
  998. return(EntryIndex.Fetch_Index(crc));
  999. }
  1000. }
  1001. return(NULL);
  1002. }
  1003. /***********************************************************************************************
  1004. * INIClass::Put_PKey -- Stores the key to the INI database. *
  1005. * *
  1006. * The key stored to the database will have both the exponent and modulus portions saved. *
  1007. * Since the fast key only requires the modulus, it is only necessary to save the slow *
  1008. * key to the database. However, storing the slow key stores the information necessary to *
  1009. * generate the fast and slow keys. Because public key encryption requires one key to be *
  1010. * completely secure, only store the fast key in situations where the INI database will *
  1011. * be made public. *
  1012. * *
  1013. * INPUT: key -- The key to store the INI database. *
  1014. * *
  1015. * OUTPUT: bool; Was the key stored to the database? *
  1016. * *
  1017. * WARNINGS: Store the fast key for public INI database availability. Store the slow key if *
  1018. * the INI database is secure. *
  1019. * *
  1020. * HISTORY: *
  1021. * 07/08/1996 JLB : Created. *
  1022. *=============================================================================================*/
  1023. bool INIClass::Put_PKey(PKey const & key)
  1024. {
  1025. char buffer[512];
  1026. int len = key.Encode_Modulus(buffer);
  1027. Put_UUBlock("PublicKey", buffer, len);
  1028. len = key.Encode_Exponent(buffer);
  1029. Put_UUBlock("PrivateKey", buffer, len);
  1030. return(true);
  1031. }
  1032. /***********************************************************************************************
  1033. * INIClass::Get_PKey -- Fetch a key from the ini database. *
  1034. * *
  1035. * This routine will fetch the key from the INI database. The key fetched is controlled by *
  1036. * the parameter. There are two choices of key -- the fast or slow key. *
  1037. * *
  1038. * INPUT: fast -- Should the fast key be retrieved? The fast key has the advantage of *
  1039. * requiring only the modulus value. *
  1040. * *
  1041. * OUTPUT: Returns with the key retrieved. *
  1042. * *
  1043. * WARNINGS: none *
  1044. * *
  1045. * HISTORY: *
  1046. * 07/08/1996 JLB : Created. *
  1047. *=============================================================================================*/
  1048. PKey INIClass::Get_PKey(bool fast) const
  1049. {
  1050. PKey key;
  1051. char buffer[512];
  1052. /*
  1053. ** When retrieving the fast key, the exponent is a known constant. Don't parse the
  1054. ** exponent from the database.
  1055. */
  1056. if (fast) {
  1057. BigInt exp = PKey::Fast_Exponent();
  1058. exp.DEREncode((unsigned char *)buffer);
  1059. key.Decode_Exponent(buffer);
  1060. } else {
  1061. Get_UUBlock("PrivateKey", buffer, sizeof(buffer));
  1062. key.Decode_Exponent(buffer);
  1063. }
  1064. Get_UUBlock("PublicKey", buffer, sizeof(buffer));
  1065. key.Decode_Modulus(buffer);
  1066. return(key);
  1067. }
  1068. /***********************************************************************************************
  1069. * INIClass::Get_Fixed -- Fetch a fixed point number from the section & entry. *
  1070. * *
  1071. * This routine will examine the section and entry specified and interpret the value *
  1072. * as if it were a fixed point number. The format of the fixed point number can be *
  1073. * percentage (e.g. 100%) or a floating point number (e.g., 1.7). *
  1074. * *
  1075. * INPUT: section -- Pointer to the section identifier to look under. *
  1076. * *
  1077. * entry -- Pointer to the entry identifier to examine. *
  1078. * *
  1079. * defvalue -- If the section and entry could not be found, then this value will *
  1080. * be returned. *
  1081. * *
  1082. * OUTPUT: Returns with the fixed point number that occurs at the section and entry *
  1083. * specified. If it could not be found, then the default value is returned. *
  1084. * *
  1085. * WARNINGS: none *
  1086. * *
  1087. * HISTORY: *
  1088. * 07/03/1996 JLB : Created. *
  1089. *=============================================================================================*/
  1090. fixed INIClass::Get_Fixed(char const * section, char const * entry, fixed defvalue) const
  1091. {
  1092. char buffer[128];
  1093. fixed retval = defvalue;
  1094. if (Get_String(section, entry, "", buffer, sizeof(buffer))) {
  1095. retval = fixed(buffer);
  1096. }
  1097. return(retval);
  1098. }
  1099. /***********************************************************************************************
  1100. * INIClass::Put_Fixed -- Store a fixed point number to the INI database. *
  1101. * *
  1102. * Use this routine to output a fixed point number to the database. The entry will be *
  1103. * placed in the section and entry specified. If there was any existing entry, it will *
  1104. * be replaced. *
  1105. * *
  1106. * INPUT: section -- Pointer to the section identifier. *
  1107. * *
  1108. * entry -- Pointer to the entry identifier to use for this value. *
  1109. * *
  1110. * value -- The value to store in the database. *
  1111. * *
  1112. * OUTPUT: bool; Was the data stored? *
  1113. * *
  1114. * WARNINGS: none *
  1115. * *
  1116. * HISTORY: *
  1117. * 07/03/1996 JLB : Created. *
  1118. *=============================================================================================*/
  1119. bool INIClass::Put_Fixed(char const * section, char const * entry, fixed value)
  1120. {
  1121. return(Put_String(section, entry, value.As_ASCII()));
  1122. }
  1123. /***********************************************************************************************
  1124. * INIClass::Strip_Comments -- Strips comments of the specified text line. *
  1125. * *
  1126. * This routine will scan the string (text line) supplied and if any comment portions are *
  1127. * found, they will be trimmed off. Leading and trailing blanks are also removed. *
  1128. * *
  1129. * INPUT: buffer -- Pointer to the null terminate string to be processed. *
  1130. * *
  1131. * OUTPUT: none *
  1132. * *
  1133. * WARNINGS: none *
  1134. * *
  1135. * HISTORY: *
  1136. * 07/03/1996 JLB : Created. *
  1137. *=============================================================================================*/
  1138. void INIClass::Strip_Comments(char * buffer)
  1139. {
  1140. if (buffer != NULL) {
  1141. char * comment = strchr(buffer, ';');
  1142. if (comment) {
  1143. *comment = '\0';
  1144. strtrim(buffer);
  1145. }
  1146. }
  1147. }