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.

146 lines
2.6 KiB

  1. #include <windows.h>
  2. #define ASSERT(x) if (!(x)) DebugBreak();
  3. LPBYTE
  4. MemFind (LPBYTE lpB, DWORD cbB, LPBYTE lpP, DWORD cbP)
  5. {
  6. DWORD i, j;
  7. LPBYTE lpF = NULL;
  8. // LPBYTE lpB = (LPBYTE) lp1, lpP = (LPBYTE) lp2;
  9. if ( (!lpB) || (!cbB) || (!lpP) || (!cbP) )
  10. return NULL;
  11. for (i = 0; i < cbB ; i++)
  12. {
  13. for (j = 0; i < cbB, j < cbP ; i++, j++)
  14. {
  15. if (lpB[i] != lpP[j])
  16. {
  17. lpF = NULL;
  18. break;
  19. }
  20. if (!j) //the first letter
  21. lpF = &(lpB[i]);
  22. }
  23. if (lpF)
  24. return lpF;
  25. }
  26. return NULL;
  27. }
  28. BOOL
  29. ParseArgsDyn(
  30. LPTSTR InBuffer,
  31. LPTSTR **pArgv,
  32. LPDWORD pArgc
  33. )
  34. #define DEFAULT_ARGV_SIZE 16
  35. {
  36. LPTSTR CurrentPtr = InBuffer;
  37. DWORD cArgv = DEFAULT_ARGV_SIZE;
  38. LPTSTR *temp = NULL;
  39. *pArgv = (LPTSTR *) LocalAlloc (LPTR, cArgv * sizeof(LPTSTR));
  40. if (!*pArgv)
  41. return FALSE;
  42. for ( ;; ) {
  43. //
  44. // skip blanks.
  45. //
  46. while( *CurrentPtr == ' ' ) {
  47. CurrentPtr++;
  48. }
  49. if( *CurrentPtr == '\0' ) {
  50. break;
  51. }
  52. (*pArgv)[*pArgc] = CurrentPtr;
  53. (*pArgc)++;
  54. //
  55. // go to next space.
  56. //
  57. while( (*CurrentPtr != ' ') &&
  58. (*CurrentPtr != '\0') &&
  59. (*CurrentPtr != '\n') ) {
  60. CurrentPtr++;
  61. }
  62. if( *CurrentPtr == '\0' ) {
  63. break;
  64. }
  65. *CurrentPtr++ = '\0';
  66. ASSERT(*pArgc <= cArgv);
  67. if (*pArgc == cArgv) //grow the array
  68. {
  69. temp = *pArgv;
  70. *pArgv = (LPTSTR *) LocalAlloc (LPTR, (cArgv + DEFAULT_ARGV_SIZE) * sizeof (LPTSTR));
  71. if (!*pArgv)
  72. {
  73. LocalFree (temp);
  74. return FALSE;
  75. }
  76. memcpy (*pArgv, temp, cArgv * sizeof(LPTSTR));
  77. LocalFree (temp);
  78. cArgv += DEFAULT_ARGV_SIZE;
  79. }
  80. }
  81. return TRUE;
  82. }
  83. DWORD
  84. AddArgvDyn (LPTSTR **pArgv, DWORD *pArgc, LPTSTR szNew)
  85. {
  86. DWORD cArgv = (*pArgc / DEFAULT_ARGV_SIZE + ( *pArgc % DEFAULT_ARGV_SIZE ? 1 : 0 )) * DEFAULT_ARGV_SIZE;
  87. LPTSTR *temp = NULL;
  88. if (cArgv <= *pArgc)
  89. {
  90. temp = *pArgv;
  91. *pArgv = (LPTSTR *) LocalAlloc (LPTR, (cArgv + DEFAULT_ARGV_SIZE) * sizeof (LPTSTR));
  92. if (!*pArgv)
  93. {
  94. *pArgv = temp;
  95. return ERROR_NOT_ENOUGH_MEMORY;
  96. }
  97. memcpy (*pArgv, temp, cArgv * sizeof(LPTSTR));
  98. if (temp)
  99. LocalFree (temp);
  100. cArgv += DEFAULT_ARGV_SIZE;
  101. ASSERT (*pArgc < cArgv);
  102. }
  103. //this means there is room for another LPTSTR
  104. (*pArgv)[*pArgc] = szNew; //NOTE this is volatile memory not alloc by us
  105. (*pArgc)++;
  106. return ERROR_SUCCESS;
  107. }