Advertisement
Guest User

Dave

a guest
Sep 17th, 2013
23
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.41 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4.  
  5. static void php_ldap_do_escape(const unsigned char *value, const int valuelen, const unsigned char *map, unsigned char **result, int *resultlen)
  6. {
  7. char hex[] = "0123456789abcdef";
  8. int i, p;
  9. size_t len;
  10.  
  11. for (i = len = 0; i < valuelen; i++) {
  12. len += map[value[i]] ? 3 : 1;
  13. }
  14.  
  15. (*result) = (unsigned char *) malloc(len + 1);
  16. (*resultlen) = (int) len;
  17.  
  18. for (i = p = 0; i < valuelen; i++) {
  19. if (map[value[i]]) {
  20. (*result)[p++] = 92;
  21. (*result)[p++] = hex[value[i] >> 4];
  22. (*result)[p++] = hex[value[i] & 0x0f];
  23. } else {
  24. (*result)[p++] = value[i];
  25. }
  26. }
  27.  
  28. (*result)[p] = '\0';
  29. }
  30.  
  31. static void php_ldap_make_escape_map(unsigned char **map, const unsigned char *ignores, const int ignoreslen, const unsigned char *chars, const int charslen)
  32. {
  33. int i;
  34.  
  35. if (!*map) {
  36. (*map) = (unsigned char *)calloc(256, sizeof(char));
  37.  
  38. if (!chars) {
  39. for (i = 0; i < 256; i++) {
  40. (*map)[i] = 1;
  41. }
  42. return;
  43. }
  44. }
  45.  
  46. if (chars) {
  47. for (i = 0; i < charslen; i++) {
  48. (*map)[chars[i]] = 1;
  49. }
  50. }
  51.  
  52. if (ignores) {
  53. for (i = 0; i < ignoreslen; i++) {
  54. (*map)[ignores[i]] = 0;
  55. }
  56. }
  57. }
  58.  
  59. int main(char **argv) {
  60. unsigned char *value, *ignores, *map, *result;
  61. int valuelen, ignoreslen, resultlen;
  62. long flags;
  63.  
  64. value = "#()*+=<foo>=+*()#";
  65. valuelen = strlen(value)+1;
  66.  
  67. ignores = "()<>=";
  68. ignoreslen = strlen(ignores)+1;
  69.  
  70. map = NULL;
  71.  
  72. php_ldap_make_escape_map(&map, ignores, ignoreslen - 1, "\\*()\0", sizeof("\\*()\0") - 1);
  73. php_ldap_make_escape_map(&map, ignores, ignoreslen - 1, "\\,=+<>;\"#", sizeof("\\,=+<>;\"#") - 1);
  74. php_ldap_make_escape_map(&map, ignores, ignoreslen - 1, NULL, 0);
  75. php_ldap_do_escape(
  76. value, valuelen - 1, map, &result, &resultlen);
  77.  
  78. printf("result: %s\n", result);
  79.  
  80. free(map);
  81. free(result);
  82. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement