doom3-gpl
Doom 3 GPL source release
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
escape.c
Go to the documentation of this file.
1 /***************************************************************************
2  * _ _ ____ _
3  * Project ___| | | | _ \| |
4  * / __| | | | |_) | |
5  * | (__| |_| | _ <| |___
6  * \___|\___/|_| \_\_____|
7  *
8  * Copyright (C) 1998 - 2004, Daniel Stenberg, <daniel@haxx.se>, et al.
9  *
10  * This software is licensed as described in the file COPYING, which
11  * you should have received as part of this distribution. The terms
12  * are also available at http://curl.haxx.se/docs/copyright.html.
13  *
14  * You may opt to use, copy, modify, merge, publish, distribute and/or sell
15  * copies of the Software, and permit persons to whom the Software is
16  * furnished to do so, under the terms of the COPYING file.
17  *
18  * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
19  * KIND, either express or implied.
20  *
21  * $Id: escape.c,v 1.30 2004/03/08 08:38:29 bagder Exp $
22  ***************************************************************************/
23 
24 /* Escape and unescape URL encoding in strings. The functions return a new
25  * allocated string or NULL if an error occurred. */
26 
27 #include "setup.h"
28 #include <ctype.h>
29 #include <curl/curl.h>
30 
31 #include <stdio.h>
32 #include <stdlib.h>
33 #include <string.h>
34 
35 /* The last #include file should be: */
36 #ifdef CURLDEBUG
37 #include "memdebug.h"
38 #endif
39 
40 char *curl_escape(const char *string, int length)
41 {
42  size_t alloc = (length?(size_t)length:strlen(string))+1;
43  char *ns = malloc(alloc);
44  char *testing_ptr = NULL;
45  unsigned char in;
46  size_t newlen = alloc;
47  int strindex=0;
48 
49  length = alloc-1;
50  while(length--) {
51  in = *string;
52  if(!(in >= 'a' && in <= 'z') &&
53  !(in >= 'A' && in <= 'Z') &&
54  !(in >= '0' && in <= '9')) {
55  /* encode it */
56  newlen += 2; /* the size grows with two, since this'll become a %XX */
57  if(newlen > alloc) {
58  alloc *= 2;
59  testing_ptr = realloc(ns, alloc);
60  if(!testing_ptr) {
61  free( ns );
62  return NULL;
63  }
64  else {
65  ns = testing_ptr;
66  }
67  }
68  sprintf(&ns[strindex], "%%%02X", in);
69 
70  strindex+=3;
71  }
72  else {
73  /* just copy this */
74  ns[strindex++]=in;
75  }
76  string++;
77  }
78  ns[strindex]=0; /* terminate it */
79  return ns;
80 }
81 
82 #define ishex(in) ((in >= 'a' && in <= 'f') || \
83  (in >= 'A' && in <= 'F') || \
84  (in >= '0' && in <= '9'))
85 
86 char *curl_unescape(const char *string, int length)
87 {
88  int alloc = (length?length:(int)strlen(string))+1;
89  char *ns = malloc(alloc);
90  unsigned char in;
91  int strindex=0;
92  long hex;
93 
94  if( !ns ) {
95  return NULL;
96  }
97 
98  while(--alloc > 0) {
99  in = *string;
100  if(('%' == in) && ishex(string[1]) && ishex(string[2])) {
101  /* this is two hexadecimal digits following a '%' */
102  char hexstr[3];
103  char *ptr;
104  hexstr[0] = string[1];
105  hexstr[1] = string[2];
106  hexstr[2] = 0;
107 
108  hex = strtol(hexstr, &ptr, 16);
109 
110  in = (unsigned char)hex; /* this long is never bigger than 255 anyway */
111  string+=2;
112  alloc-=2;
113  }
114 
115  ns[strindex++] = in;
116  string++;
117  }
118  ns[strindex]=0; /* terminate it */
119  return ns;
120 }
121 
122 /* For operating systems/environments that use different malloc/free
123  ssystems for the app and for this library, we provide a free that uses
124  the library's memory system */
125 void curl_free(void *p)
126 {
127  free(p);
128 }
case const int
Definition: Callbacks.cpp:52
GLenum GLsizei const GLvoid * string
Definition: glext.h:3472
#define NULL
Definition: Lib.h:88
#define ishex(in)
Definition: escape.c:82
char * curl_unescape(const char *string, int length)
Definition: escape.c:86
char * curl_escape(const char *string, int length)
Definition: escape.c:40
void curl_free(void *p)
Definition: escape.c:125
GLuint in
Definition: glext.h:5388
GLsizei const GLcharARB const GLint * length
Definition: glext.h:3599
GLfloat GLfloat p
Definition: glext.h:4674
int sprintf(idStr &string, const char *fmt,...)
Definition: Str.cpp:1528