Advertisement
Guest User

Untitled

a guest
May 30th, 2016
62
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.61 KB | None | 0 0
  1. #define PCRE2_CODE_UNIT_WIDTH 8
  2.  
  3. #include "pcre2.h"
  4. #include <stdio.h>
  5. #include <stdlib.h>
  6. #include <string.h>
  7.  
  8. int main(int argc, char *argv[]) {
  9.     if (argc < 2) {
  10.         printf("usage: %s pattern\n", argv[0]);
  11.         return 0;
  12.     } // Print manual if no string is inserted
  13.  
  14.     pcre2_code *re;
  15.     pcre2_match_data *match_data;
  16.     int errorcode;
  17.     int rc;
  18.     int checksum = 0;
  19.     int i, j = 0;
  20.     int tmp;
  21.  
  22.     PCRE2_SPTR pattern = (const unsigned char*)"^((?:[0-9]{2})(?:0[1-9]|1[0-2])(?:0[1-9]|[1,2][0-9]|3[0,1]))-[1-2][0-9]{6}$";
  23.     PCRE2_SPTR input = (const unsigned char*)argv[1];
  24.  
  25.     re = pcre2_compile(pattern, -1, 0, &errorcode, &erroffset, NULL);
  26.  
  27.     if (re == NULL) {
  28.         PCRE2_UCHAR8 buffer[120];
  29.         (void)pcre2_get_error_message(errorcode, buffer, 120);
  30.         /* Handle error */
  31.         return 0;
  32.     }
  33.  
  34.     match_data = pcre2_match_data_create(20, NULL);
  35.     rc = pcre2_match(re, input, -1, 0, 0, match_data, NULL);
  36.  
  37.     if (rc <= 0)
  38.         printf("INVALID(Incorrect format)\n")
  39.     else {
  40.         // Generate checksum
  41.         for (i = 0; i < 13; i++) {
  42.             if (argv[1][i] != '-') {
  43.                 tmp = argv[1][i] - '0';             // Convert ASCII to integer
  44.                 checksum += tmp * ((j++) % 8 + 2);  // Mutiply specific numbers to each digit and sum up all the results
  45.             }
  46.         }
  47.         checksum = (11 - (checksum % 11)) % 10;     // If checksum is 10, check sum is 0
  48.  
  49.         // Compare checksum with last digit of input
  50.         if (argv[1][13] - '0' != checksum) {
  51.             printf("INVALID(Wrong Checksum)\n");
  52.         }
  53.         else {
  54.             printf("VALID\n");
  55.         }
  56.         /* Do whatever you need to validate the match */
  57.     }
  58.  
  59.     pcre2_match_data_free(match_data);
  60.     pcre2_code_free(re);
  61. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement