removed the local variables for emacs and vim, use the new sample.emacs
[platform/upstream/curl.git] / lib / escape.c
1 /***************************************************************************
2  *                                  _   _ ____  _     
3  *  Project                     ___| | | |  _ \| |    
4  *                             / __| | | | |_) | |    
5  *                            | (__| |_| |  _ <| |___ 
6  *                             \___|\___/|_| \_\_____|
7  *
8  * Copyright (C) 1998 - 2003, 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$
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 MALLOCDEBUG
37 #include "memdebug.h"
38 #endif
39
40 char *curl_escape(const char *string, int length)
41 {
42   int alloc = (length?length:(int)strlen(string))+1;  
43   char *ns = malloc(alloc);
44   char *testing_ptr = NULL;
45   unsigned char in;
46   int newlen = alloc;
47   int index=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[index], "%%%02X", in);
69
70       index+=3;
71     }
72     else {
73       /* just copy this */
74       ns[index++]=in;
75     }
76     string++;
77   }
78   ns[index]=0; /* terminate it */
79   return ns;
80 }
81
82 char *curl_unescape(const char *string, int length)
83 {
84   int alloc = (length?length:(int)strlen(string))+1;
85   char *ns = malloc(alloc);
86   unsigned char in;
87   int index=0;
88   unsigned int hex;
89  
90   if( !ns ) {
91     return NULL;
92   }  
93   
94   while(--alloc > 0) {
95     in = *string;
96     if('%' == in) {
97       /* encoded part */
98       if(sscanf(string+1, "%02X", &hex)) {
99         in = hex;
100         string+=2;
101         alloc-=2;
102       }
103     }
104     
105     ns[index++] = in;
106     string++;
107   }
108   ns[index]=0; /* terminate it */
109   return ns;
110 }
111
112 void curl_free(void *p)
113 {
114   free(p);
115 }