473,382 Members | 1,302 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,382 software developers and data experts.

function to url decode a string

Hello,

Can anyone tell me where Can I find a function that can decode a url
encoded string

like
ram%40domain.tld ==> ra*@domain.tld

Thanks
Ram
ram 'at' netcore.co.in
Nov 13 '05 #1
11 21325
"Ramprasad A Padmanabhan" <my****@netcore.co.in> wrote in message
news:3F***************@netcore.co.in...
Hello,

Can anyone tell me where Can I find a function that can decode a url
encoded string

like
ram%40domain.tld ==> ra*@domain.tld


Write one, isn't that what a programmer is being?
strip off the "%", then take the next 2 characters and convert them from
ascii-hex to ascii.
Nov 13 '05 #2


Ramprasad A Padmanabhan wrote:

Hello,

Can anyone tell me where Can I find a function that can decode a url
encoded string

This is a very common thing. Do a google search for cgi utilities
written in C.

Brian Rodenborn
Nov 13 '05 #3

Ramprasad A Padmanabhan <my****@netcore.co.in> wrote in message
news:3F***************@netcore.co.in...
Hello,

Can anyone tell me where Can I find a function that can decode a url
encoded string

like
ram%40domain.tld ==> ra*@domain.tld

/* (no error checking included) */

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

void cvt(char *dest, const char *src)
{
const char *p = src;
char code[3] = {0};
unsigned long ascii = 0;
char *end = NULL;

while(*p)
{
if(*p == '%')
{
memcpy(code, ++p, 2);
ascii = strtoul(code, &end, 16);
*dest++ = (char)ascii;
p += 2;
}
else
*dest++ = *p++;
}
}

int main()
{
char in[] = "ram%40domain.tld";
char out[sizeof in] = {0};
cvt(out, in);
printf("in == %s\nout == %s\n", in, out);
return 0;
}

-Mike


Nov 13 '05 #4
On Wed, 23 Jul 2003 18:48:10 -0400, Mike Wahler wrote:

/* (no error checking included) */
<snip> while(*p)
{


This function should be called 'bugtraq'.

Mike
Nov 13 '05 #5
Michael B Allen wrote:

On Wed, 23 Jul 2003 18:48:10 -0400, Mike Wahler wrote:

/* (no error checking included) */

<snip>
while(*p)
{


This function should be called 'bugtraq'.

Mike


Thanks , But I am not worried about any security risk in the function.
The function caller will do all the checking.

I am not a great programming expert and just a beginner with c But IMHO
it is not worth trying to trap all overflows in all functions and in the
end making the code very Heavy

If the this function is called *only* from my own script and I know
exactly what I am doing then i think it still will do

Thanks
Ram
Nov 13 '05 #6

Michael B Allen <mb*****@ioplex.com> wrote in message
news:pa**********************************@ioplex.c om...
On Wed, 23 Jul 2003 18:48:10 -0400, Mike Wahler wrote:

/* (no error checking included) */

<snip>
while(*p)
{


This function should be called 'bugtraq'.


It's a non-production *example*. I included a caveat
about no error checking. Call it what you will.

-Mike

Nov 13 '05 #7
Ramprasad A Padmanabhan wrote:
Can anyone tell me where Can I find a function that can decode
a url encoded string like ram%40domain.tld ==> ra*@domain.tld


Ram...

The code at http://www.iedu.com/mrd/c/kvp.c contains a function
to do the decoding you want. I'm sure that you can find much more
(and possibly much better code) with a Google search.
--
Morris Dovey
West Des Moines, Iowa USA
C links at http://www.iedu.com/c

Nov 13 '05 #8
On Thu, 24 Jul 2003 18:29:29 +0530, Ramprasad A Padmanabhan wrote:
Michael B Allen wrote:

On Wed, 23 Jul 2003 18:48:10 -0400, Mike Wahler wrote:
>
> /* (no error checking included) */
> <snip>
> while(*p)
> {


This function should be called 'bugtraq'.

Mike


Thanks , But I am not worried about any security risk in the function.
The function caller will do all the checking.


This isn't likely IMO. Given an interface like the above, it's much hard
to check that the arguments are good to use.
Personally I'd recommend looking at a real string API
http://www.and.org/vstr/comparison.html ... the first on the list has
uri encode/decode functions.

If you want to pretend you don't need one then the libclc function
discussed a couple of days ago, in this very group, would be a better
starting point.
I am not a great programming expert and just a beginner with c But IMHO
it is not worth trying to trap all overflows in all functions and in the
end making the code very Heavy


This is a misconception due to your inexperience, stopping errors
_always_ needs to be done and if done properly doesn't make the code any
heavier.

--
James Antill -- ja***@and.org
Need an efficent and powerful string library for C?
http://www.and.org/vstr/

Nov 13 '05 #9
On Wed, 23 Jul 2003 18:48:10 -0400, Mike Wahler wrote:
If the this function is called *only* from my own script and I know
exactly what I am doing then i think it still will do


A URL is something that is almost invariably supplied by a user or suppied
by a program. In both cases unless *you* are always the one typing in the
URL your code must consider errnoeous input. Poor URL processing is a
favorite target of crackers.

The below code should be correct and safe although I have only tested
it with the one input.

Mike

--8<--

#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <ctype.h>
#include <stdio.h>

int
url_decode(const char *src, const char *slim, char *dst, char *dlim)
{
int state = 0, code;
char *start = dst;

if (dst >= dlim) {
return 0;
}
dlim--; /* ensure spot for '\0' */

while (src < slim && dst < dlim) {
switch (state) {
case 0:
if (*src == '%') {
state = 1;
} else {
*dst++ = *src;
}
break;
case 1:
code = *src - 48;
case 2:
if (isdigit(*src) == 0) {
errno = EILSEQ;
return -1;
}
if (state == 2) {
*dst++ = (code * 16) + *src - 48;
state = 0;
} else {
state = 2;
}
break;
}
src++;
}
*dst = '\0'; /* I'll be back */

return dst - start;
}

int main()
{
const char *src = "ram%40domain.tld/a/b/c%40d/%24%40%24abc";
char dst[1024];

if (url_decode(src, src + strlen(src), dst, dst + 1024) == -1) {
perror("url_decode");
return EXIT_FAILURE;
}
puts(src);
puts(dst);

return EXIT_SUCCESS;
}
Nov 13 '05 #10
James Antill wrote:

On Thu, 24 Jul 2003 18:29:29 +0530, Ramprasad A Padmanabhan wrote:
Michael B Allen wrote:

On Wed, 23 Jul 2003 18:48:10 -0400, Mike Wahler wrote:
>
> /* (no error checking included) */
>
<snip>
> while(*p)
> {

This function should be called 'bugtraq'.

Mike


Thanks , But I am not worried about any security risk in the function.
The function caller will do all the checking.


This isn't likely IMO. Given an interface like the above, it's much hard
to check that the arguments are good to use.
Personally I'd recommend looking at a real string API
http://www.and.org/vstr/comparison.html ... the first on the list has
uri encode/decode functions.

If you want to pretend you don't need one then the libclc function
discussed a couple of days ago, in this very group, would be a better
starting point.
I am not a great programming expert and just a beginner with c But IMHO
it is not worth trying to trap all overflows in all functions and in the
end making the code very Heavy


This is a misconception due to your inexperience, stopping errors
_always_ needs to be done and if done properly doesn't make the code any
heavier.


Why not ? I just want to get my fundamentals clear and not argue that I
am right

If a string is used in function A() and within A() in B() and within B()
in C()
Then If I check the string( for some error condition ) I will do it only
only A() because B() and C() are not exposed at all directly

If I include the check in B() and in C() then there are more if's and
else's in my function then how can that be a better code

Ram
Nov 13 '05 #11
On Thu, 24 Jul 2003 16:19:49 -0400, Michael B Allen wrote:
On Wed, 23 Jul 2003 18:48:10 -0400, Mike Wahler wrote:
If the this function is called *only* from my own script and I know
exactly what I am doing then i think it still will do


A URL is something that is almost invariably supplied by a user or
suppied by a program. In both cases unless *you* are always the one
typing in the URL your code must consider errnoeous input. Poor URL
processing is a favorite target of crackers.

The below code should be correct and safe although I have only tested it
with the one input.


And thus it did not convert all hex digits correctly. See the state
machine example on the homepage for an updated version.

http://www.ioplex.com/~miallen/

Mike
Nov 13 '05 #12

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

9
by: Derek Hart | last post by:
I wish to execute code from a string. The string will have a function name, which will return a string: Dim a as string a = "MyFunctionName(param1, param2)" I have seen a ton of people...
4
by: Ralph Noble | last post by:
Does anyone know of a string function in Access that will allow me to count the number of instances one string occurs within another? Or if there is some sort of word count function? If there is,...
0
by: steve mettraux | last post by:
I have this kind of string, dim myString as string = " =E0 ton essai d'=E9tablir une" i know the kind of encoding, here I have iso-8859-1 dim charset as string ="iso-8859-1" how to decode...
5
by: krbyxtrm | last post by:
Any knows of a Function Signature string Parser? typically one that can parse string: "unsigned int const * __cdecl MyFunction(int,void const *)" into: 0 : 'unsigned int const *' 1 :...
12
by: Ima Loozer | last post by:
OK folks here is what I need help with. Lets assume I have a text field that will contain AlphaNumeric data. There is no set pattern to the field such that any given character can be either alpha...
44
by: gokkog | last post by:
Hi there, There's a classic hash function to hash strings, where MULT is defined as "31": //from programming pearls unsigned int hash(char *ptr) { unsigned int h = 0; unsigned char *p =...
17
by: streamkid | last post by:
Why can't a function not return string? (error: new types may not be defined in a return type)... How can i solve it? I thought of passing the string into the function, but that will make me...
4
by: ali | last post by:
Hi, I'm have a base class with a virual function toString. All the derived classes will have to implement this function. Here's the code I have used: //in base.h virtual string* toString();
3
by: =?Utf-8?B?SmFuIEhlcHBlbg==?= | last post by:
Hi, I've a question. I'm developing a windows application and for the application i need to run functions and procedures that are stored in a database. Here is an example that i tried to get...
10
by: james_027 | last post by:
hi, i have a function that I could like to call, but to make it more dynamic I am constructing a string first that could equivalent to the name of the function I wish to call. how could I do...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.