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.

256 lines
8.2 KiB

  1. static PWSTR cszFirst = L"First";
  2. static PWSTR cszLast = L"Last";
  3. static PWSTR cszUserName = L"Username";
  4. static PWSTR cszMAC = L"MAC";
  5. static const int iFirst = (sizeof(L"First") / sizeof(WCHAR)) - 1;
  6. static const int iLast = (sizeof(L"Last" ) / sizeof(WCHAR)) - 1;
  7. static const int iUserName = (sizeof(L"Username") / sizeof(WCHAR)) - 1;
  8. static const int iMAC = (sizeof(L"MAC" ) / sizeof(WCHAR)) - 1;
  9. #define STRING_MISSING(_x) (((_x) == NULL) || (*(_x) == 0))
  10. DWORD
  11. GenerateNameFromTemplate (
  12. IN PWSTR Template,
  13. IN PGENNAME_VARIABLES Variables,
  14. IN PWSTR Name,
  15. IN DWORD NameLength,
  16. OUT PWSTR *MissingVariable OPTIONAL,
  17. OUT BOOL *UsedCounter OPTIONAL,
  18. OUT DWORD *MaximumGeneratedNameLength OPTIONAL
  19. )
  20. {
  21. DWORD error;
  22. DWORD maxLength;
  23. DWORD fieldLength;
  24. WCHAR localString[10];
  25. WCHAR localFormat[10];
  26. BOOL padding;
  27. PWSTR pTemplate;
  28. PWSTR pOutput;
  29. PWSTR pOutputEnd;
  30. PWSTR stringToAdd;
  31. PWSTR pString;
  32. BOOL usedUserName;
  33. pTemplate = Template;
  34. pOutput = Name;
  35. pOutputEnd = pOutput + NameLength - 1;
  36. error = GENNAME_NO_ERROR;
  37. maxLength = 0;
  38. usedUserName = FALSE;
  39. if ( UsedCounter != NULL ) {
  40. *UsedCounter = FALSE;
  41. }
  42. while ( *pTemplate != 0 ) {
  43. if ( *pTemplate == L'%' ) {
  44. pTemplate++;
  45. fieldLength = 0;
  46. padding = FALSE;
  47. if ( *pTemplate >= L'0' && *pTemplate <= L'9' ) {
  48. if (*pTemplate == L'0') {
  49. padding = TRUE;
  50. //
  51. // see if this request to do padding is from the "sample"
  52. // entrypoint. If we're doing padding, we want to make
  53. // the sample output show that we will actually do some
  54. // padding, so we make our counter small enough to show
  55. // the padding.
  56. //
  57. if (Variables->Counter == 123456789 &&
  58. Variables->AllowCounterTruncation &&
  59. (0 == wcscmp(Variables->UserName, L"JOHNSMI")) &&
  60. (0 == wcscmp(Variables->MacAddress, L"123456789012"))) {
  61. Variables->Counter = 1;
  62. }
  63. }
  64. do {
  65. fieldLength = (fieldLength * 10) + (*pTemplate - L'0');
  66. pTemplate++;
  67. } while ( *pTemplate >= L'0' && *pTemplate <= L'9' );
  68. }
  69. if ( *pTemplate == L'#' ) {
  70. DWORD maxCounter;
  71. DWORD counter;
  72. DWORD i;
  73. if (fieldLength > 9) {
  74. fieldLength = 9;
  75. }
  76. if (fieldLength == 0) {
  77. fieldLength = 2;
  78. }
  79. if(padding){
  80. wsprintf(localFormat,L"%s%d%s", L"%0",fieldLength,L"d");
  81. } else {
  82. wcscpy(localFormat,L"%d");
  83. }
  84. maxCounter = 10;
  85. for ( i = 1; i < fieldLength; i++ ) {
  86. maxCounter *= 10;
  87. }
  88. counter = Variables->Counter;
  89. if ( counter >= maxCounter ) {
  90. if ( !Variables->AllowCounterTruncation ) {
  91. return GENNAME_COUNTER_TOO_HIGH;
  92. }
  93. //
  94. // Truncate the counter on the right.
  95. //
  96. while ( counter > maxCounter ) {
  97. counter /= 10;
  98. }
  99. }
  100. if ( UsedCounter != NULL ) {
  101. *UsedCounter = TRUE;
  102. }
  103. wsprintf( localString, localFormat, counter );
  104. stringToAdd = localString;
  105. pTemplate++;
  106. } else if ( StrCmpNI( pTemplate, cszFirst, iFirst ) == 0 ) {
  107. if (fieldLength > DNS_MAX_LABEL_LENGTH) {
  108. fieldLength = DNS_MAX_LABEL_LENGTH;
  109. }
  110. if (fieldLength == 0) {
  111. fieldLength = DNS_MAX_LABEL_LENGTH;
  112. }
  113. stringToAdd = Variables->FirstName;
  114. if ( STRING_MISSING(stringToAdd) ) {
  115. if ( !usedUserName ) {
  116. stringToAdd = Variables->UserName;
  117. if ( STRING_MISSING(stringToAdd) ) {
  118. if ( MissingVariable != NULL ) {
  119. *MissingVariable = GENNAME_VARIABLE_FIRSTNAME;
  120. }
  121. return GENNAME_VARIABLE_MISSING;
  122. }
  123. usedUserName = TRUE;
  124. }
  125. }
  126. pTemplate += iFirst;
  127. } else if ( StrCmpNI( pTemplate, cszLast, iLast ) == 0 ) {
  128. if (fieldLength > DNS_MAX_LABEL_LENGTH) {
  129. fieldLength = DNS_MAX_LABEL_LENGTH;
  130. }
  131. if (fieldLength == 0) {
  132. fieldLength = DNS_MAX_LABEL_LENGTH;
  133. }
  134. stringToAdd = Variables->LastName;
  135. if ( STRING_MISSING(stringToAdd) ) {
  136. if ( !usedUserName ) {
  137. stringToAdd = Variables->UserName;
  138. if ( STRING_MISSING(stringToAdd) ) {
  139. if ( MissingVariable != NULL ) {
  140. *MissingVariable = GENNAME_VARIABLE_LASTNAME;
  141. }
  142. return GENNAME_VARIABLE_MISSING;
  143. }
  144. usedUserName = TRUE;
  145. }
  146. }
  147. pTemplate += iLast;
  148. } else if ( StrCmpNI( pTemplate, cszUserName, iUserName ) == 0 ) {
  149. if (fieldLength > DNS_MAX_LABEL_LENGTH) {
  150. fieldLength = DNS_MAX_LABEL_LENGTH;
  151. }
  152. if (fieldLength == 0) {
  153. fieldLength = DNS_MAX_LABEL_LENGTH;
  154. }
  155. if ( !usedUserName ) {
  156. stringToAdd = Variables->UserName;
  157. if ( STRING_MISSING(stringToAdd) ) {
  158. if ( MissingVariable != NULL ) {
  159. *MissingVariable = GENNAME_VARIABLE_USERNAME;
  160. }
  161. return GENNAME_VARIABLE_MISSING;
  162. }
  163. usedUserName = TRUE;
  164. }
  165. pTemplate += iUserName;
  166. } else if ( StrCmpNI( pTemplate, cszMAC, iMAC ) == 0 ) {
  167. if (fieldLength > 12) {
  168. fieldLength = 12;
  169. }
  170. if (fieldLength == 0) {
  171. fieldLength = 12;
  172. }
  173. stringToAdd = Variables->MacAddress;
  174. if ( STRING_MISSING(stringToAdd) ) {
  175. if ( MissingVariable != NULL ) {
  176. *MissingVariable = GENNAME_VARIABLE_MAC;
  177. }
  178. return GENNAME_VARIABLE_MISSING;
  179. }
  180. pTemplate += iMAC;
  181. } else {
  182. return GENNAME_TEMPLATE_INVALID;
  183. }
  184. } else {
  185. fieldLength = 1;
  186. localString[0] = *pTemplate;
  187. localString[1] = 0;
  188. stringToAdd = localString;
  189. pTemplate++;
  190. }
  191. maxLength += fieldLength;
  192. pString = stringToAdd;
  193. for ( pString = stringToAdd;
  194. (fieldLength > 0) && (*pString != 0);
  195. fieldLength--, pString++ ) {
  196. if ( pOutput < pOutputEnd ) {
  197. *pOutput++ = *pString;
  198. } else {
  199. error = GENNAME_NAME_TOO_LONG;
  200. break;
  201. }
  202. }
  203. }
  204. if ( MaximumGeneratedNameLength != NULL ) {
  205. *MaximumGeneratedNameLength = maxLength;
  206. }
  207. *pOutput++ = 0;
  208. return error;
  209. } // GenerateNameFromTemplate