Leaked source code of windows server 2003
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.

254 lines
5.0 KiB

  1. //----------------------------------------------------------------------------
  2. //
  3. // Simple parameter string parsing.
  4. //
  5. // Copyright (C) Microsoft Corporation, 2000-2002.
  6. //
  7. //----------------------------------------------------------------------------
  8. #include "pch.hpp"
  9. #include "cmnutil.hpp"
  10. #include "pparse.hpp"
  11. //----------------------------------------------------------------------------
  12. //
  13. // ParameterStringParser.
  14. //
  15. //----------------------------------------------------------------------------
  16. ParameterStringParser::ParameterStringParser(void)
  17. {
  18. m_Name = NULL;
  19. }
  20. BOOL
  21. ParameterStringParser::ParseParameters(PCSTR ParamString)
  22. {
  23. if (ParamString == NULL)
  24. {
  25. // Nothing to parse.
  26. return TRUE;
  27. }
  28. PCSTR Scan = ParamString;
  29. // Skip <name>: if present.
  30. while (*Scan && *Scan != ':')
  31. {
  32. Scan++;
  33. }
  34. if (*Scan == ':')
  35. {
  36. Scan++;
  37. }
  38. else
  39. {
  40. Scan = ParamString;
  41. }
  42. //
  43. // Scan options string for comma-delimited parameters
  44. // and pass them into the parameter handling method.
  45. //
  46. char Param[MAX_PARAM_NAME];
  47. char Value[MAX_PARAM_VALUE];
  48. PSTR ValStr;
  49. PSTR Str;
  50. for (;;)
  51. {
  52. while (*Scan && isspace(*Scan))
  53. {
  54. Scan++;
  55. }
  56. if (!*Scan)
  57. {
  58. break;
  59. }
  60. Str = Param;
  61. while (*Scan && *Scan != ',' && *Scan != '=' &&
  62. (Str - Param) < MAX_PARAM_NAME)
  63. {
  64. *Str++ = *Scan++;
  65. }
  66. if (Str >= Param + MAX_PARAM_NAME)
  67. {
  68. return FALSE;
  69. }
  70. // Terminate option name and default value to nothing.
  71. *Str++ = 0;
  72. ValStr = NULL;
  73. if (*Scan == '=')
  74. {
  75. // Parameter has a value, scan it.
  76. Scan++;
  77. while (*Scan && isspace(*Scan))
  78. {
  79. Scan++;
  80. }
  81. Str = Value;
  82. while (*Scan && *Scan != ',' &&
  83. (Str - Value) < MAX_PARAM_VALUE)
  84. {
  85. *Str++ = *Scan++;
  86. }
  87. if (Str >= Value + MAX_PARAM_VALUE)
  88. {
  89. return FALSE;
  90. }
  91. *Str++ = 0;
  92. ValStr = Value;
  93. }
  94. if (*Scan)
  95. {
  96. // Skip comma for next iteration.
  97. Scan++;
  98. }
  99. // Set the value in the parser.
  100. if (!SetParameter(Param, ValStr))
  101. {
  102. return FALSE;
  103. }
  104. }
  105. return TRUE;
  106. }
  107. BOOL
  108. ParameterStringParser::GetParameters(PSTR Buffer, ULONG BufferSize)
  109. {
  110. ULONG Len;
  111. BOOL Ret = FALSE;
  112. // Reserve space for the terminator.
  113. if (BufferSize < 1)
  114. {
  115. return FALSE;
  116. }
  117. BufferSize--;
  118. if (m_Name != NULL)
  119. {
  120. Len = strlen(m_Name);
  121. if (BufferSize < Len + 1)
  122. {
  123. goto EH_Exit;
  124. }
  125. memcpy(Buffer, m_Name, Len);
  126. Buffer += Len;
  127. *Buffer++ = ':';
  128. BufferSize -= Len + 1;
  129. }
  130. ULONG Params;
  131. ULONG i;
  132. char Name[MAX_PARAM_NAME];
  133. char Value[MAX_PARAM_VALUE];
  134. BOOL NeedComma;
  135. Params = GetNumberParameters();
  136. NeedComma = FALSE;
  137. for (i = 0; i < Params; i++)
  138. {
  139. Name[0] = 0;
  140. Value[0] = 0;
  141. GetParameter(i, Name, DIMA(Name), Value, DIMA(Value));
  142. Len = strlen(Name);
  143. if (Len == 0)
  144. {
  145. continue;
  146. }
  147. if (BufferSize < Len)
  148. {
  149. goto EH_Exit;
  150. }
  151. if (NeedComma)
  152. {
  153. if (BufferSize < 1)
  154. {
  155. goto EH_Exit;
  156. }
  157. *Buffer++ = ',';
  158. BufferSize--;
  159. }
  160. memcpy(Buffer, Name, Len);
  161. Buffer += Len;
  162. BufferSize -= Len;
  163. NeedComma = TRUE;
  164. Len = strlen(Value);
  165. if (Len == 0)
  166. {
  167. continue;
  168. }
  169. if (BufferSize < Len + 1)
  170. {
  171. goto EH_Exit;
  172. }
  173. *Buffer++ = '=';
  174. memcpy(Buffer, Value, Len);
  175. Buffer += Len;
  176. BufferSize -= Len + 1;
  177. }
  178. Ret = TRUE;
  179. EH_Exit:
  180. *Buffer++ = 0;
  181. return Ret;
  182. }
  183. ULONG
  184. ParameterStringParser::GetParser(PCSTR ParamString,
  185. ULONG NumNames, PCSTR* Names)
  186. {
  187. if (ParamString == NULL)
  188. {
  189. return PARSER_INVALID;
  190. }
  191. //
  192. // Parse out <name>: and look up the name.
  193. //
  194. PCSTR Scan = ParamString;
  195. while (*Scan && *Scan != ':')
  196. {
  197. Scan++;
  198. }
  199. ULONG Len = (ULONG)(Scan - ParamString);
  200. if (*Scan != ':' || Len < 1)
  201. {
  202. return PARSER_INVALID;
  203. }
  204. ULONG i;
  205. for (i = 0; i < NumNames; i++)
  206. {
  207. if (strlen(Names[i]) == Len &&
  208. !_memicmp(Names[i], ParamString, Len))
  209. {
  210. return i;
  211. }
  212. }
  213. return PARSER_INVALID;
  214. }