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.

250 lines
6.9 KiB

  1. #include "pre.h"
  2. #include "tchar.h"
  3. #define LCID_JPN 1041 //JAPANESE
  4. // BUGBUG - This function is not very effecient since it requires a alloc/free for each validation
  5. // plus strtok will tokenize the fill string requring a full search of the string.
  6. BOOL IsValid(LPCTSTR pszText, HWND hWndParent, WORD wNameID)
  7. {
  8. ASSERT(pszText);
  9. TCHAR* pszTemp = NULL;
  10. BOOL bRetVal = FALSE;
  11. pszTemp = _tcsdup (pszText);
  12. if (pszTemp)
  13. {
  14. if (_tcslen(pszTemp))
  15. {
  16. TCHAR seps[] = TEXT(" ");
  17. TCHAR* token = NULL;
  18. token = _tcstok( pszTemp, seps );
  19. if (token)
  20. {
  21. bRetVal = TRUE;
  22. }
  23. }
  24. free(pszTemp);
  25. }
  26. // If not valid, give the user the error message
  27. if (!bRetVal)
  28. DoValidErrMsg(hWndParent, wNameID);
  29. return bRetVal;
  30. }
  31. // ============================================================================
  32. // Credit card number validation
  33. // ============================================================================
  34. // Takes a credit card number in the form of 1111-1111-1111-11
  35. // and pads converts it to:
  36. // 11111111111111
  37. // **IMPORTANT** :: This code is multibyte safe but ONLY because we care ONLY about
  38. // ANSI #'s
  39. BOOL PadCardNum
  40. (
  41. LPCTSTR lpszRawCardNum,
  42. LPTSTR szPaddedCardNum,
  43. UINT uLenOfRaw
  44. )
  45. {
  46. LPTSTR lpszTmp = CharPrev(lpszRawCardNum, lpszRawCardNum);
  47. UINT uIndex = 0;
  48. for (UINT i = 0; i < uLenOfRaw; i++)
  49. {
  50. if( *lpszTmp == '\0' )
  51. break;
  52. if((*lpszTmp != '-') && (*lpszTmp != ' '))
  53. {
  54. //make sure it's not some other than ansi char.
  55. if ((*lpszTmp < '0') || (*lpszTmp > '9'))
  56. return(FALSE);
  57. szPaddedCardNum[uIndex] = *lpszTmp;
  58. uIndex++;
  59. }
  60. // Get the prev char
  61. lpszTmp = CharNext(lpszRawCardNum + i);
  62. }
  63. szPaddedCardNum[uIndex] = '\0';
  64. return(TRUE);
  65. }
  66. /*
  67. mod_chk()
  68. performs "Double-Add-Double MOD 10 Check Digit Routine"
  69. on card number
  70. */
  71. BOOL mod_chk
  72. (
  73. LPTSTR credit_card,
  74. UINT uCardNumLen
  75. )
  76. {
  77. TCHAR *cp;
  78. int dbl = 0;
  79. int check_sum = 0;
  80. /*
  81. * This checksum algorithm has a name,
  82. * but I can't think of it.
  83. */
  84. cp = credit_card + lstrlen(credit_card) - 1;
  85. while (cp >= credit_card)
  86. {
  87. int c;
  88. c = *cp-- - '0';
  89. if (dbl)
  90. {
  91. c *= 2;
  92. if (c >= 10)
  93. c -= 9;
  94. }
  95. check_sum += c;
  96. dbl = !dbl;
  97. }
  98. return (BOOL)((check_sum % 10) == 0);
  99. }
  100. BOOL validate_cardnum(HWND hWndParent, LPCTSTR lpszRawCardNum)
  101. // performs:
  102. // a) card type prefix check
  103. // b) Double-Add-Double MOD 10 check digit routine via mod_chk()
  104. // on the card number.
  105. // The card_num parameter is assumed to have been pre-checked for
  106. // numeric characters and right-justified with '0' padding on the
  107. // left.
  108. {
  109. BOOL bRet = FALSE;
  110. UINT uRawLen = lstrlen(lpszRawCardNum);
  111. TCHAR* pszPaddedCardNum = (TCHAR*)malloc((uRawLen + 1)*sizeof(TCHAR));
  112. if (!pszPaddedCardNum)
  113. return FALSE;
  114. ZeroMemory(pszPaddedCardNum ,(uRawLen + 1)*sizeof(TCHAR));
  115. if (PadCardNum(lpszRawCardNum, pszPaddedCardNum, uRawLen))
  116. {
  117. UINT i = 0;
  118. LPTSTR tmp_pt = pszPaddedCardNum;
  119. UINT uPadLen = lstrlen(pszPaddedCardNum);
  120. /* find the first non-zero number in card_num */
  121. while (*tmp_pt == '0' && ++i < uPadLen)
  122. ++tmp_pt;
  123. /* all valid card types are at least 13 characters in length */
  124. if (uPadLen < 13)
  125. bRet = FALSE;
  126. /* check for OK VISA prefix - 4 */
  127. if ((uPadLen == 16 || uPadLen == 13) && *tmp_pt == '4')
  128. bRet = mod_chk(pszPaddedCardNum, uPadLen);
  129. /* check for OK MasterCard prefix - 51 to 55 */
  130. if (uPadLen == 16) {
  131. if (*tmp_pt == '5' &&
  132. *(tmp_pt + 1) >= '1' && *(tmp_pt + 1) <= '5')
  133. bRet = mod_chk(pszPaddedCardNum, uPadLen);
  134. }
  135. /* check for OK American Express prefix - 37 and 34 */
  136. if (uPadLen == 15 && *tmp_pt == '3' &&
  137. (*(tmp_pt + 1) == '7' || *(tmp_pt + 1) == '4'))
  138. bRet = mod_chk(pszPaddedCardNum, uPadLen);
  139. /* check for OK Discovery prefix - 6011 */
  140. if (uPadLen == 16 &&
  141. *tmp_pt == '6' && *(tmp_pt + 1) == '0' &&
  142. *(tmp_pt + 2) == '1' && *(tmp_pt + 3) == '1')
  143. bRet = mod_chk(pszPaddedCardNum, uPadLen);
  144. }
  145. if (!bRet)
  146. {
  147. DoSpecificErrMsg(hWndParent, IDS_PAYMENT_CC_LUHNCHK);
  148. }
  149. free(pszPaddedCardNum);
  150. return bRet;
  151. }
  152. BOOL validate_cardexpdate(HWND hWndParent, int month, int year)
  153. {
  154. BOOL bRet = FALSE;
  155. SYSTEMTIME SystemTime;
  156. GetLocalTime(&SystemTime);
  157. if (year > SystemTime.wYear)
  158. {
  159. bRet = TRUE;
  160. }
  161. else if (year == SystemTime.wYear)
  162. {
  163. if (month >= SystemTime.wMonth)
  164. {
  165. bRet = TRUE;
  166. }
  167. }
  168. if (!bRet)
  169. {
  170. DoSpecificErrMsg(hWndParent, IDS_PAYMENT_CCEXPDATE);
  171. }
  172. return bRet;
  173. }
  174. // ============================================================================
  175. // Error message handlers
  176. // ============================================================================
  177. void DoValidErrMsg(HWND hWndParent, int iNameId)
  178. {
  179. TCHAR szCaption [MAX_RES_LEN+1] = TEXT("\0");
  180. TCHAR szErrMsgFmt [MAX_RES_LEN+1] = TEXT("\0");
  181. TCHAR szErrMsgName [MAX_RES_LEN+1] = TEXT("\0");
  182. TCHAR szErrMsg [2*MAX_RES_LEN];
  183. if (!LoadString(ghInstance, IDS_APPNAME, szCaption, ARRAYSIZE(szCaption)))
  184. return;
  185. if ((IDS_USERINFO_ADDRESS2 == iNameId) && (LCID_JPN == GetUserDefaultLCID()))
  186. iNameId = IDS_USERINFO_FURIGANA;
  187. if (!LoadString(ghInstance, iNameId, szErrMsgName, ARRAYSIZE(szErrMsgName)))
  188. return;
  189. if (!LoadString(ghInstance, IDS_ERR_INVALID_MSG, szErrMsgFmt, ARRAYSIZE(szErrMsgFmt)))
  190. return;
  191. wsprintf(szErrMsg, szErrMsgFmt, szErrMsgName);
  192. MessageBox(hWndParent, szErrMsg, szCaption, MB_OK | MB_ICONEXCLAMATION | MB_APPLMODAL);
  193. }
  194. void DoSpecificErrMsg(HWND hWndParent, int iErrId)
  195. {
  196. TCHAR szCaption [MAX_RES_LEN+1] = TEXT("\0");
  197. TCHAR szErrMsg [MAX_RES_LEN+1] = TEXT("\0");
  198. if (!LoadString(ghInstance, IDS_APPNAME, szCaption, ARRAYSIZE(szCaption) ))
  199. return;
  200. if (!LoadString(ghInstance, iErrId, szErrMsg, ARRAYSIZE(szErrMsg) ))
  201. return;
  202. MessageBox(hWndParent, szErrMsg, szCaption, MB_OK | MB_ICONEXCLAMATION | MB_APPLMODAL);
  203. }