The Poor Man's (or Woman's) Intrusion Detection System
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 

137 lines
5.1 KiB

  1. //////////////////////////////////////////////////////////////////////
  2. // Mini INI File Crackers
  3. // Written by Jonathan A. Foster <jon@jfpossibilities.com>
  4. // Started June 1st, 2021
  5. // Copyright JF Possibilities, Inc. All rights reserved.
  6. // Used with permission in the "Poor Man's IDS" project
  7. //
  8. // This set of classes is to prvide easy access to reading config data
  9. // from INI files. I'm going to take some liberty with the origianl
  10. // M$ format and allow groups to parse specialized text formats, not
  11. // just name/value pairs.
  12. //////////////////////////////////////////////////////////////////////
  13. #ifndef __JFP_MINIINI_H__
  14. #define __JFP_MINIINI_H__
  15. #include <string>
  16. #include <ostream>
  17. #include "strutil.h"
  18. //////////////////////////////////////////////////////////////////////
  19. // The base abstract group class
  20. //
  21. // Subclass and override this class to create a parser for the kind of
  22. // data you want in your group.
  23. //////////////////////////////////////////////////////////////////////
  24. struct MiniINI;
  25. struct MiniINIgroup {
  26. // vals: By convention the content of the group
  27. // this is overriden to handle the parsing of lines in this group
  28. // it must ignore whitespace and remarks, if allowed in the group's
  29. // format.
  30. virtual void add(const std::string &in) = 0;
  31. // This is overridden to dump the group back out to a file
  32. virtual std::ostream &save(std::ostream &out) const = 0;
  33. // cause we need the destructor virtual too!
  34. virtual ~MiniINIgroup() {}
  35. };
  36. inline std::ostream &operator<<(std::ostream &out, const MiniINIgroup &ini) {
  37. return ini.save(out);
  38. }
  39. //////////////////////////////////////////////////////////////////////
  40. // The typical collection of name+value pairs
  41. //////////////////////////////////////////////////////////////////////
  42. struct MiniINIvars: public MiniINIgroup {
  43. NameVal vals; // name val pairs
  44. /// Get a value setting it to def if not found
  45. ///
  46. /// This looks for name in vals. If found that value is returned. Otherwise
  47. /// the def is set in vals for the name. This means it will be saved at
  48. /// next writing. Def defaults to empty string. If you don't want to auto-
  49. /// create the value in the config file then use the appropriate methods of
  50. /// vals.
  51. virtual std::string get(const std::string &name, const std::string &def);
  52. /// Get a value NOT adding it to vals if not found
  53. ///
  54. /// This looks for name in vals. If found that value is returned. Otherwise
  55. /// "" is returned but the name is not added to vals.
  56. virtual std::string get(const std::string &name) const;
  57. /// Get an int from vals
  58. ///
  59. /// This will convert the string value, if found in vals, to an int and make
  60. /// sure its within bounds. If its not in bounds or doesn't exist or is an
  61. /// an empty string them def is returned. The vals is not modified.
  62. ///
  63. /// For safety values are limited to 9 digits plus sign, since this is a safe
  64. /// range for converting to 32 bit ints.
  65. virtual int geti(const std::string &name, int def=0, int min=-999999999, int max=999999999) const;
  66. /// parse an INI line into a name value pair
  67. virtual void add(const std::string &in);
  68. /// write all name value pairs from vals to out
  69. virtual std::ostream &save(std::ostream &out) const;
  70. };
  71. //////////////////////////////////////////////////////////////////////
  72. // A raw collection of lines in a group
  73. //////////////////////////////////////////////////////////////////////
  74. struct MiniINIlines: public MiniINIgroup {
  75. StringList vals; // the raw lines of this group
  76. virtual void add(const std::string &in);// add an incoming line
  77. virtual std::ostream &save(std::ostream &out) const;
  78. };
  79. //////////////////////////////////////////////////////////////////////
  80. // The main INI container that contains all groups
  81. //
  82. // Fill groups with a list of group objects and their associated
  83. // names. Call load() to read the groups from an INI file. Only the
  84. // group with names specified in groups will be read. Their content
  85. // parsed according to the group's rules. save() will write it back
  86. // out again. No attempt to preserve whitespace and remarks is made.
  87. //////////////////////////////////////////////////////////////////////
  88. typedef std::map<std::string,MiniINIgroup*> INIgroupList;
  89. struct MiniINI {
  90. // This is a list of groups allowed in the INI and after load() those
  91. // groups will contain the data from the INI and they will be written
  92. // on save().
  93. //
  94. // This must be initialized with MiniINIgroup* objects so that it knows
  95. // how to process groups. No attempt is made to manage memory allocation.
  96. INIgroupList groups;
  97. // Load the file and parse into groups[]. If a group is encountered that
  98. // was not specified in groups[] it is ignored, and will be lost on
  99. // save(). Same for pre-group content.
  100. virtual void load(const std::string &fname);
  101. // Save the groups[] into the file. No attempt is made to preserve
  102. // whitespace or remarks. This is the "mini INI". Preserving thoee
  103. // requires a more complicated and thus less "mini" implementation.
  104. virtual void save(const std::string &fname);
  105. // A virtual NOP destructor... because its recommended.
  106. virtual ~MiniINI() {}
  107. };
  108. #endif