summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorepoch <epoch@hack.thebackupbox.net>2020-01-25 05:43:20 +0000
committerepoch <epoch@hack.thebackupbox.net>2020-01-25 05:43:20 +0000
commitfabcd1a464b125c8be8229a4ef5eb3dfca55bf9d (patch)
treec51eac1c278b308ce5005ef3df1f62ab2efbd3a4
parentd4b183d46ac08139107b8c5f04ba9dc714b8c4f0 (diff)
downloaduritools-fabcd1a464b125c8be8229a4ef5eb3dfca55bf9d.tar.gz
uritools-fabcd1a464b125c8be8229a4ef5eb3dfca55bf9d.zip
it actually works now and is no longer hideous
-rw-r--r--uriunescape.c59
1 files changed, 29 insertions, 30 deletions
diff --git a/uriunescape.c b/uriunescape.c
index 9543c4c..c99cdfc 100644
--- a/uriunescape.c
+++ b/uriunescape.c
@@ -2,43 +2,42 @@
#include <unistd.h>
#include <stdio.h>
+#define BUFSIZE 4096
+
int main(int argc,char *argv[]) {
- int i,j;
+ int i=0,j=0;
int len;
-
- char doh[2];
- int hack=0;
-
- char buf[16];
- char buf2[16];
+ char buf[BUFSIZE];
+ char buf2[BUFSIZE];
+ int blen;
+ int hack;
if(argc > 1) {
- for(argv++,argc--;argc;argc--,argv++) {
- len=uriunescape(*argv,*argv);
- write(1,*argv,len);
- if(argc-1) write(1," ",1);
- }
+ for(argv++,argc--;argc;argc--,argv++) {
+ len=uriunescape(*argv,*argv);
+ write(1,*argv,len);
+ if(argc-1) write(1," ",1);
+ }
} else {
- while(fgets(buf+hack,sizeof(buf)-1-hack,stdin)) {//this is a bad idea. first chunk could end inside of a sequence we could decode. [first %] [09 second]
- hack=0;
- for(i=0;i<2;i++) {//2 being max length of a %XX thing.
- if(buf[strlen(buf)-1-i] == '%') {
- for(j=0;j<i;j++) {
- doh[j] = buf[strlen(buf)-1-j];//save it for when we need to fix it back.
+ while((blen=read(0,buf+hack,BUFSIZE-hack-1)) > 0) {
+ buf[blen+hack]=0;
+ blen=strlen(buf);
+ hack=0;
+ for(i=0;i<2;i++) {//2 being max length of a %XX thing. and we're starting at the last.
+ if(buf[blen-i-1] == '%') {//we're looping from the end first...
+ buf[blen-i-1] = 0;//zero out the %
+ hack=i+1;
+ break;
+ }
}
- buf[strlen(buf)-1-i] = 0;//zero out the %
- hack=2;
- len=uriunescape(buf,buf2);
- write(1,buf2,len);
- //we end with a %[single char]
- buf[0]='%';
- buf[1]=doh[0];
- }
- }
- if(!hack) {//we're good.
- len=uriunescape(buf,buf2);
+ len=uriunescape(buf,buf2);//uriunescape wants null terminated strings
write(1,buf2,len);
+ if(hack) {
+ buf[0]='%';
+ for(j=0;j<hack;j++) {//move the entity to the start of the string
+ buf[j+1]=buf[blen-(hack-1-j)];
+ }
+ }
}
- }
}
return 0;
}