Advertisement
Phr0zen_Penguin

stringComp.c

Nov 23rd, 2013
160
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 9.50 KB | None | 0 0
  1. /*============================================================================
  2.   ----------------------------------------------------------------------------
  3.   stringComp.c - Another way to compare two strings (or even authenticate
  4.             a password).
  5.                  (c) Damion 'Phr0z3n.Dev' Tapper, 2013.
  6.                  Email: Phr0z3n.Dev@Gmail.com
  7.   ----------------------------------------------------------------------------
  8.   ============================================================================*/
  9. #define USE_SEC_API /* Comment this line if you do not have the secure libraries. */
  10.  
  11. #include <stdio.h>  /* For the printf function. */
  12. #include <stdlib.h> /* For the calloc function. */
  13. #include <conio.h>  /* For the getch function. */
  14. #include <ctype.h>  /* For the isprint function. */
  15.  
  16. /*=====================================--=====================================
  17.             VARIABLE NAME DEFINITIONS
  18.   ----------------------------------------------------------------------------*/
  19. #ifndef TRUE
  20.     #define TRUE    1
  21.     #define FALSE   0
  22. #endif
  23.  
  24. #define LOOP            TRUE
  25. #define BUFFER_LENGTH       25
  26.  
  27. #define BUFFER_1        pscs->buffer1
  28. #define BUFFER_2        pscs->buffer2
  29. #define INPUT_CHARACTER     pscs->inputChar
  30. #define CHARACTER_INPUT     INPUT_CHARACTER = getch()
  31. #define POSITION        pscs->position
  32. #define INCREMENT       pscs->increment
  33. #define ACTION_OFFSET       pscs->actionOffset
  34. #define NADA            (int)NULL
  35. /* For some compilers NULL is defined as 0 and ((void *)0) for others. */
  36.  
  37. #define BACKSPACE_IS_PRESSED    INPUT_CHARACTER == 8
  38. #define ENTER_IS_NOT_PRESSED    INPUT_CHARACTER != 13
  39. #define STRINGS_MATCH       INCREMENT == POSITION
  40.  
  41. #define RESET_POSITION      POSITION = 0
  42.  
  43. /*=====================================--=====================================
  44.                 TYPE DEFINITIONS
  45.   ----------------------------------------------------------------------------*/
  46. typedef struct  stringCompStruct    SCS;
  47. typedef struct  stringCompStruct*   PSCS; /* Hungarian notation. */
  48.  
  49. #ifndef BOOL
  50. typedef int             BOOL;
  51. #endif
  52.  
  53. struct stringCompStruct
  54. {
  55.     /*
  56.      * On some compilers, declaring these two variables at the top of the structure
  57.      * could cause severe complications such as pointer conflicts and, eventual,
  58.      * program crash (major traits of an overflow)...
  59.      *
  60.      * ...but being that they will be absolutely allocated (on the heap) (with calloc)... ;)
  61.      */
  62.     char    *buffer1;
  63.     char    *buffer2;
  64.     char    inputChar;
  65.     int position;
  66.     int increment;
  67.     BOOL    actionOffset;      
  68. };
  69.  
  70. /*=====================================--=====================================
  71.             FUNCTION PROTOTYPE DEFINITIONS
  72.   ----------------------------------------------------------------------------*/
  73. static void stringEntry(PSCS); /* ...with limited scope (static).*/
  74. static void stringCopy(PSCS);
  75. static void stringComp(PSCS);
  76. static void clearBuffer1(PSCS);
  77. static void clearBuffer2(PSCS);
  78. static void freeAllocs(PSCS);
  79.  
  80. /*=====================================--=====================================
  81.                 MAIN FUNCTION
  82.   ----------------------------------------------------------------------------*/
  83. int main(void)
  84. {
  85.     /*
  86.      * "One pointer to rule 'em all...
  87.      * ...one structure to bind 'em."
  88.      */
  89.     PSCS    pscs;
  90.  
  91.     /* "One function to allocate 'em all..." */
  92.     pscs = calloc(1, sizeof(SCS));
  93.  
  94.     /* "...and on the heap you'll find 'em." */
  95.     BUFFER_1 = calloc(BUFFER_LENGTH, sizeof(char)); /* To be treated as array. */
  96.     BUFFER_2 = calloc(BUFFER_LENGTH, sizeof(char));
  97.  
  98.     ACTION_OFFSET = FALSE; /* Just to make sure... though calloc has already done the job. */
  99.  
  100.     stringEntry(pscs);
  101.  
  102.     return 0;
  103. }
  104.  
  105. /*=====================================--=====================================
  106.                 STRING ENTRY FUNCTION
  107.   ----------------------------------------------------------------------------*/
  108. static void stringEntry(PSCS pscs)
  109. {
  110.     if(ACTION_OFFSET == FALSE)
  111.     {
  112.     #ifdef USE_SEC_API
  113.         printf_s("Enter first string:\n"); /* The secure printf function (good programming practice). */
  114.     #else
  115.         printf("Enter first string:\n"); /* If you are lacking the secure libraries. */
  116.     #endif
  117.     }
  118.     else
  119.     {
  120.         clearBuffer1(pscs);
  121.  
  122.     #ifdef USE_SEC_API
  123.         printf_s("\n\nEnter second string:\n");
  124.     #else
  125.         printf("\n\nEnter second string:\n");
  126.     #endif
  127.     }
  128.  
  129.         while(LOOP) /* <<< ...I always wanted to do this. */
  130.         {
  131.             RESET_POSITION;
  132.    
  133.                 do
  134.                 {
  135.                     /*
  136.                      * An elite system analyst once stated that the implementation
  137.                      * of the NULL-terminated string is the single greatest 'mistake'
  138.                      * in the history of the C programming language.
  139.                      *
  140.                      * We have learned that this single fundamental flaw has led
  141.                      * to the devastating number of buffer overflow attacks, on
  142.                      * mission-critical systems, plaguing the cyberworld to this day...
  143.                      *
  144.                      * ...hence, the implementation of the secure libraries:
  145.                      * strncpy_s, strncat_s, strtok_s, etc.
  146.                      *
  147.                      * Now we have learned, in the aftermath of this implementation,
  148.                      * that such buffer overflow attacks have been reduced by as
  149.                      * much as an extraordinary 60 percent annually.
  150.                      *
  151.                      * All that was required in the beginning was the statement,
  152.                      * implementation and restriction of the size of the buffer
  153.                      * (in memory) being operated on.
  154.                      */
  155.                     if(isprint(CHARACTER_INPUT) && POSITION < BUFFER_LENGTH)
  156.                     {
  157.                     #ifdef USE_SEC_API
  158.                         printf_s("%c", INPUT_CHARACTER);
  159.                     #else
  160.                         printf("%c", INPUT_CHARACTER);
  161.                     #endif
  162.                         BUFFER_1[POSITION] = INPUT_CHARACTER;
  163.                         POSITION++;
  164.                     }
  165.                     if(BACKSPACE_IS_PRESSED && POSITION)
  166.                     {
  167.                         POSITION--;
  168.                         BUFFER_1[POSITION] = NADA;
  169.                     #ifdef USE_SEC_API
  170.                         printf_s("\b \b");
  171.                     #else
  172.                         printf_s("\b \b");
  173.                     #endif
  174.                     }
  175.                 }
  176.                 while(ENTER_IS_NOT_PRESSED);
  177.    
  178.                 /*
  179.                  * If at least one character is entered.
  180.                  * A character is not a string in itself but...
  181.                  */
  182.                 if(POSITION != NADA)
  183.                 {
  184.                     if(ACTION_OFFSET == FALSE)
  185.                     {
  186.                         stringCopy(pscs);
  187.                     }
  188.                     else
  189.                     {
  190.                         stringComp(pscs);
  191.                     }
  192.                 }
  193.         }
  194. }
  195.  
  196. /*=====================================--=====================================
  197.                 STRING COPY FUNCTION
  198.   ----------------------------------------------------------------------------*/
  199. static void stringCopy(PSCS pscs)
  200. {
  201.         for(POSITION = 0; BUFFER_1[POSITION] != NADA; POSITION++)
  202.             BUFFER_2[POSITION] = BUFFER_1[POSITION];
  203.  
  204.     /*
  205.      * This triggers the 'stringComp' function call logic upon the next entry.
  206.      */
  207.     ACTION_OFFSET = TRUE;
  208.  
  209.     stringEntry(pscs);
  210. }
  211.  
  212. /*=====================================--=====================================
  213.                 STRING COMPARE FUNCTION
  214.   ----------------------------------------------------------------------------*/
  215. static void stringComp(PSCS pscs)
  216. {
  217.     /*
  218.      * This is the clever section.
  219.      * | ABSOLUTELY NO BUFFER OVERFLOW ALLOWED!!! |
  220.      */
  221.  
  222.         /* STEP 1: Get the length of the first string entered. */
  223.         for(INCREMENT = 0; BUFFER_2[INCREMENT] != NADA; INCREMENT++)
  224.                 ;
  225.    
  226.         /* STEP 2: Get the length of the second string entered. */
  227.         for(POSITION = 0; BUFFER_1[POSITION] != NADA; POSITION++)
  228.                 ;
  229.    
  230.         /* STEP 3a: If both strings are of the same length... DUH! */
  231.         if(INCREMENT == POSITION)
  232.             /*
  233.              * STEP 3b: Now that the aforementioned is a fact...
  234.              * ...run along the first string entered and increment each time.
  235.              */
  236.             for(INCREMENT = 0, POSITION = 0; BUFFER_2[INCREMENT] != NADA; INCREMENT++)
  237.                 /*
  238.                  * STEP 3c: For everytime a character in the frist string
  239.                  * entered matches a character in the second string entered...
  240.                  */
  241.                 if(BUFFER_1[POSITION] == BUFFER_2[POSITION])
  242.                     /* STEP 3d: ...'tally' the position. */
  243.                     POSITION++;
  244.    
  245.         /*
  246.          * STEP 4: If in the end all the characters in the frist string entered
  247.          * match all the characters in the second string entered...
  248.          *
  249.          * ...let's run a test...
  250.          */
  251.         if(STRINGS_MATCH) /* << I Always wanted to do this. */
  252.         {
  253.             /* The result is announced... */
  254.         #ifdef USE_SEC_API
  255.             printf_s("\n\nTHE STRINGS MATCH.\n");
  256.         #else
  257.             printf("\n\nTHE STRINGS MATCH.\n");
  258.         #endif
  259.         }
  260.         else
  261.         {
  262.         #ifdef USE_SEC_API
  263.             printf_s("\n\nTHE STRINGS DO NOT MATCH.\n");
  264.         #else
  265.             printf("\n\nTHE STRINGS DO NOT MATCH.\n");
  266.         #endif
  267.         }
  268.  
  269.     freeAllocs(pscs);
  270.     exit(0);
  271. }
  272.  
  273. /*=====================================--=====================================
  274.                 CLEAR BUFFER FUNCTIONS
  275.   ----------------------------------------------------------------------------*/
  276. static void clearBuffer1(PSCS pscs)
  277. {
  278.     for(POSITION = 0; POSITION <= BUFFER_LENGTH; POSITION++)
  279.         /* Thanks to calloc. */
  280.         BUFFER_1[POSITION] = '\0'; /* Instead of 0... just to make sure. */
  281. }
  282.  
  283. static void clearBuffer2(PSCS pscs)
  284. {
  285.     for(POSITION = 0; POSITION <= BUFFER_LENGTH; POSITION++)
  286.         BUFFER_2[POSITION] = '\0';
  287.  
  288. }
  289.  
  290. /*=====================================--=====================================
  291.         FREE ALLOCATIONS FUNCTION (Superior Programming Practice)
  292.   ----------------------------------------------------------------------------*/
  293. static void freeAllocs(PSCS pscs)
  294. {
  295.     /* This bit would not be possible with arrays per se. */
  296.     free(BUFFER_2);
  297.     free(BUFFER_1);
  298.     free(pscs);     /* Reverse order.  Similar to 'stack unwinding' (in C++). */
  299.     /*
  300.      * What if the strings you compared were a password used in a
  301.      * mission-critical authentication process?  It would be foolish to have any
  302.      * shard of it sitting in memory even after the program closes.
  303.      *
  304.      * Microsoft could take a leaf out of my book.
  305.      */
  306.    
  307. #ifdef USE_SEC_API
  308.     printf_s("\n\n<<- Goodbye free world! ->>\n");
  309. #else
  310.     printf("\n\n<<- Goodbye free world! ->>\n");
  311. #endif
  312. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement