uri解碼簡析與C語言實現

2021-10-10 21:40:20 字數 2849 閱讀 7370

1、遇到'%'才開始解析,%後的兩個字元最終解析為乙個ascii字元.如%e4 -> 0xe4 -> 『e』*16+4 -> 244,即 %e4 解析為ascii碼為244的字元

2、%xx才進行解析,其餘字元保留原樣

總結:abc -> abc 、%61%62%63 -> 0x610x620x63 -> abc

注:本**是對glib2庫的g_filename_from_uri()的精簡,注釋是自己的理解,有錯請指正。

1、uri-decode.h

#ifndef url_decode_h

#define url_decode_h

char

*uridecode

(const

char

* uristring)

;#endif

2、uri-decode.c

#include

#include

#include

#include

#include

const

unsigned

int asciitabledata[

256]=;

char

*unescapeuristring

(const

char

* uristring,bool asciiescape)

;int

unescapecharacter

(const

char

*scanner)

;int

asciixdigitvalue

(char c)

;int

asciidigitvalue

(char c)

; bool asciiisdigit

(char c)

;/*uri解碼*/

char

*uridecode

(const

char

* uristring)

/* 解析uri字串

@ asciiescape 布林值,ascii碼是否必須解析

*/char

*unescapeuristring

(const

char

* uristring,bool asciiescape)

*out++

= c;

//儲存轉義結果

}*out =

'\0';if

(in != end)

return result;

}/*假設 @scanner is "%e4",則返回"%e4"的解碼值

*實際上是計算0xe4對應的十進位制。

*/int

unescapecharacter

(const

char

*scanner)

/*求任一字元的數字值*/

intasciixdigitvalue

(char c)

/*ascii碼是數字則返回數字值,否則返回-1*/

intasciidigitvalue

(char c)

/*判斷ascii碼是否是數字0-9*/

bool asciiisdigit

(char c)

3、main.c 對**功能的檢驗驗證

#include

#include

#include

#include

#include

"uri-decode.h"

intmain

(int argc,

char

* ar**)

memset

(uristring,0,

sizeof

(uristring));

strncpy

(uristring,ar**[1]

,sizeof

(uristring));

retstring =

uridecode

(uristring)

;printf

("%s\n"

,retstring);if

(retstring)

free

(retstring)

;return0;

}

1、編譯:gcc uri-decode.c uri-decode.h main.c -o main

2、執行:(讀者可參照輸出對第1步深入理解)

輸入輸出

./main %61%62%63

abc./main abc

abc./main 「

語言1、解碼的過程其實就是16進製制轉10進製,求字元的10進製ascii碼值的過程

2、求出的10進製ascii碼值存放在字元陣列的1個位元組的空間裡(僅1位元組)

3、單獨對這乙個位元組來講,它對應的字元仍然是不可見字元,但是多個不可見字元在連續的陣列記憶體空間中可以拼接成乙個可見的字串

4、如漢字就是這個邏輯,linux下strlen("中")==3,即漢字由3個char字元拼接而成。

return

(first <<4)

| second;

//== (first*16 | second) == (first*16 + second)

C語言實現析構器

按照iso c的規定,乙個程序可以註冊多大32個函式,這些函式將由exit自動呼叫。這些函式被稱為 終止處理程式 exit handler 並呼叫atexit函式來註冊這些函式。include 成功返回0,失敗返回非0 int atexit void func void 從原型我們可以看到,被註冊的...

哈夫曼編碼與解碼 C語言實現

include include include include define maxnum 60 typedef struct huffnode typedef struct huffcode huffnode ht maxnum 2 存放哈夫曼樹 huffcode hcd maxnum 存放ht陣...

C語言實現huffman編譯碼與壓縮文字

哈夫曼編碼 huffman coding 又稱霍夫曼編碼,是一種編碼方式,哈夫曼編碼是可變字長編碼 vlc 的一種。huffman於1952年提出一種編碼方法,該方法完全依據字元出現概率來構造異字頭的平均長度最短的碼字,有時稱之為最佳編碼,一般就叫做huffman編碼 有時也稱為霍夫曼編碼 在計算機...