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.

174 lines
3.8 KiB

  1. ///////////////////////////////////////////////////////////////////////////////
  2. //
  3. // Copyright (c) Microsoft Corp. All rights reserved.
  4. //
  5. // FILE
  6. //
  7. // textmap.cpp
  8. //
  9. // SYNOPSIS
  10. //
  11. // This file defines functions for converting Time of Day restriction
  12. // hour maps to and from a textual representation.
  13. //
  14. // MODIFICATION HISTORY
  15. //
  16. // 02/05/1998 Original version.
  17. // 04/06/1998 Sunday is day zero again.
  18. //
  19. ///////////////////////////////////////////////////////////////////////////////
  20. #include <ias.h>
  21. #include <Parser.h>
  22. #include <textmap.h>
  23. //////////
  24. // Definition of whitespace.
  25. //////////
  26. #define WSP L" "
  27. //////////
  28. // Valid delimiters for days.
  29. //////////
  30. #define DELIM L",;|"
  31. ///////////////////////////////////////////////////////////////////////////////
  32. //
  33. // CLASS
  34. //
  35. // TimeOfDayParser
  36. //
  37. // DESCRIPTION
  38. //
  39. // This class extends Parser to extract hour maps from a string.
  40. //
  41. ///////////////////////////////////////////////////////////////////////////////
  42. class TimeOfDayParser : public Parser
  43. {
  44. public:
  45. TimeOfDayParser(PWSTR source) throw ()
  46. : Parser(source) { }
  47. // Extract time of day in the format hh:mm.
  48. void extractTime(ULONG* hour, ULONG* minute) throw (ParseError)
  49. {
  50. *hour = extractUnsignedLong();
  51. skip(WSP);
  52. // Minutes are optional.
  53. if (*current == L':')
  54. {
  55. ++current;
  56. *minute = extractUnsignedLong();
  57. }
  58. else
  59. {
  60. *minute = 0;
  61. }
  62. if (*hour > 24 || *minute > 59 || (*hour == 24 && *minute != 0))
  63. {
  64. throw ParseError();
  65. }
  66. }
  67. // Extracts a single day's hour map.
  68. void extractDay(PBYTE hourMap) throw (ParseError)
  69. {
  70. // Get the day of week (an integer from 0-6).
  71. ULONG dayOfWeek = extractUnsignedLong();
  72. skip(WSP);
  73. if (dayOfWeek > 6) { throw ParseError(); }
  74. do
  75. {
  76. // Get the start time of the range.
  77. ULONG startHour, startMinute;
  78. extractTime(&startHour, &startMinute);
  79. skip(WSP);
  80. ignore(L'-');
  81. // Get the end time of the range.
  82. ULONG endHour, endMinute;
  83. extractTime(&endHour, &endMinute);
  84. skip(WSP);
  85. // Make sure the values are legit.
  86. if (startHour * 60 + startMinute > endHour * 60 + endMinute)
  87. {
  88. throw ParseError();
  89. }
  90. // Set all bits in the range.
  91. for (size_t i=startHour; i<endHour; ++i)
  92. {
  93. hourMap[dayOfWeek * 3 + i / 8] |= 0x80 >> (i % 8);
  94. }
  95. } while (more());
  96. }
  97. };
  98. ///////////////////////////////////////////////////////////////////////////////
  99. //
  100. // FUNCTION
  101. //
  102. // IASTextToHourMap
  103. //
  104. ///////////////////////////////////////////////////////////////////////////////
  105. DWORD
  106. WINAPI
  107. IASHourMapFromText(
  108. IN PCWSTR szText,
  109. IN BOOL keepMap,
  110. IN OUT PBYTE pHourMap
  111. )
  112. {
  113. if (szText == NULL || pHourMap == NULL)
  114. {
  115. return ERROR_INVALID_PARAMETER;
  116. }
  117. if (keepMap == FALSE)
  118. {
  119. memset(pHourMap, 0, IAS_HOUR_MAP_LENGTH);
  120. }
  121. //////////
  122. // Make a local copy so we can modify the text.
  123. //////////
  124. PWSTR copy = (PWSTR)_alloca((wcslen(szText) + 1) * sizeof(WCHAR));
  125. wcscpy(copy, szText);
  126. //////////
  127. // Parse the text.
  128. //////////
  129. try
  130. {
  131. // Each day should be separated by a comma or semicolon.
  132. PWSTR token = wcstok(copy, DELIM);
  133. while (token)
  134. {
  135. TimeOfDayParser parser(token);
  136. parser.extractDay(pHourMap);
  137. token = wcstok(NULL, DELIM);
  138. }
  139. }
  140. catch (Parser::ParseError)
  141. {
  142. return ERROR_INVALID_DATA;
  143. }
  144. return NO_ERROR;
  145. }