Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #define PCRE2_CODE_UNIT_WIDTH 8
- #include "pcre2.h"
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- int main(int argc, char *argv[]) {
- if (argc < 2) {
- printf("usage: %s pattern\n", argv[0]);
- return 0;
- } // Print manual if no string is inserted
- pcre2_code *re;
- pcre2_match_data *match_data;
- int errorcode;
- int rc;
- int checksum = 0;
- int i, j = 0;
- int tmp;
- 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}$";
- PCRE2_SPTR input = (const unsigned char*)argv[1];
- re = pcre2_compile(pattern, -1, 0, &errorcode, &erroffset, NULL);
- if (re == NULL) {
- PCRE2_UCHAR8 buffer[120];
- (void)pcre2_get_error_message(errorcode, buffer, 120);
- /* Handle error */
- return 0;
- }
- match_data = pcre2_match_data_create(20, NULL);
- rc = pcre2_match(re, input, -1, 0, 0, match_data, NULL);
- if (rc <= 0)
- printf("INVALID(Incorrect format)\n")
- else {
- // Generate checksum
- for (i = 0; i < 13; i++) {
- if (argv[1][i] != '-') {
- tmp = argv[1][i] - '0'; // Convert ASCII to integer
- checksum += tmp * ((j++) % 8 + 2); // Mutiply specific numbers to each digit and sum up all the results
- }
- }
- checksum = (11 - (checksum % 11)) % 10; // If checksum is 10, check sum is 0
- // Compare checksum with last digit of input
- if (argv[1][13] - '0' != checksum) {
- printf("INVALID(Wrong Checksum)\n");
- }
- else {
- printf("VALID\n");
- }
- /* Do whatever you need to validate the match */
- }
- pcre2_match_data_free(match_data);
- pcre2_code_free(re);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement