Got an iPhone or iPad? We have a brand new Pastebin App for both devices, and it's totally free! Click here to download the new Pastebin App for iOS.
Guest

Untitled

By: a guest on Feb 12th, 2012  |  syntax: None  |  size: 1.76 KB  |  hits: 26  |  expires: Never
download  |  raw  |  embed  |  report abuse
Copied
  1. /*
  2.         CGI Helper functions
  3. */
  4.  
  5. /*
  6.         todo:   - escape mysql string
  7.                 - unescape http shit
  8. */
  9.  
  10.  
  11.  
  12. #include <stdio.h>
  13. #include <stdlib.h>
  14. #include <string.h>
  15.  
  16. //Function declarations
  17. size_t get_var(char *haystack, char *name, char **result);
  18. void* zalloc(size_t num, size_t type);
  19.  
  20. int main()
  21. {
  22.         char *buffer;
  23.         int num;
  24.         num = get_var("pretty=nice&parsing=function&findme=hik0pp&hello=bye", "findme", &buffer);
  25.         printf("%d\n", num);
  26.         printf("%s\n", buffer);
  27.         return 0;
  28. }
  29.  
  30. size_t get_var(char *haystack, char *needle, char **result)
  31. {
  32.         char *buffer;
  33.         char *search;
  34.         char *res;
  35.         char *found;
  36.        
  37.         //create copies to work with
  38.         buffer = zalloc(strlen(haystack) + 1, sizeof(char));    //Copy of haystack to rape
  39.         search = zalloc(strlen(needle) + 2, sizeof(char));              //'needle' + '='
  40.         strcpy(buffer, haystack);
  41.         sprintf(search, "%s=", needle);
  42.        
  43.         //Find the needle
  44.         res = strstr(buffer, search);
  45.         if(res == NULL)
  46.         {
  47.                 *result = NULL;
  48.                 return 0;
  49.         }
  50.         //Go past the =
  51.         res += strlen(search);
  52.         found = res;
  53.        
  54.         //Look for the end of the value
  55.         while(res[0] != 0 && res[0]     != '&')
  56.         {
  57.                 res++;
  58.         }
  59.        
  60.         //Make res[0] into a null byte, in case its &
  61.         res[0] = 0;
  62.        
  63.         //Create the result buffer
  64.         *result = zalloc(strlen(found) + 1, sizeof(char));
  65.         strcpy(*result, found);
  66.         printf("%s\n", *result);
  67.        
  68.                
  69.         //Free the buffers so there is no leak
  70.         free(buffer);
  71.         free(search);
  72.        
  73.         //Return length
  74.         return strlen(*result);
  75. }
  76.  
  77.  
  78. //Zero alloc, allocates, zeros and error checks <3
  79. void* zalloc(size_t num, size_t type)
  80. {
  81.         void *return_buffer;
  82.         return_buffer = calloc(num, type);                      //allocate it
  83.         if(return_buffer == NULL)
  84.         {
  85.                 //Out of memory, hard quit
  86.                 exit(-1);
  87.         }
  88.         memset(return_buffer, 0, num * type);           //ZERO the memory
  89.        
  90.         return return_buffer;
  91. }