前些时间做 xauth 认证程序的编写,网上找到RFC3986编码不支持中文的编码,所以便查找了一些资料.自己写了一个,代码如下.
#include <stdlib.h> #include <string.h> #include <stdio.h> #include <android/log.h> #include "RFC3986Encoder.h" bool isReverseChar(char c); char* charToHexString(char c); bool isChinese(char c); //#define snprintf _snprintf /** * Escape 'string' according to RFC3986 and * http://oauth.net/core/1.0/#encoding_parameters. * * @param string The data to be encoded * @return encoded string otherwise NULL * The caller must free the returned string. */ static void *xmalloc_fatal(size_t size) { if (size==0) return NULL; fprintf(stderr, "Out of memory."); exit(1); } void *xrealloc (void *ptr, size_t size) { void *p = realloc (ptr, size); if (p == NULL) return xmalloc_fatal(size); return p; } void *xmalloc (size_t size) { void *ptr = malloc (size); if (ptr == NULL) return xmalloc_fatal(size); return ptr; } char *xstrdup (const char *s) { void *ptr = xmalloc(strlen(s)+1); strcpy((char *)ptr, s); return (char*)ptr; } char *oauth_url_escape(const char *string) { size_t alloc, newlen; char *ns = NULL, *testing_ptr = NULL; unsigned char in; size_t strindex=0; size_t length; if (!string) return xstrdup(""); alloc = strlen(string)+1; newlen = alloc; ns = (char*) xmalloc(alloc); length = alloc-1; while(length--) { in = *string; switch(in){ case '0': case '1': case '2': case '3': case '4': case '5': case '6': case '7': case '8': case '9': case 'a': case 'b': case 'c': case 'd': case 'e': case 'f': case 'g': case 'h': case 'i': case 'j': case 'k': case 'l': case 'm': case 'n': case 'o': case 'p': case 'q': case 'r': case 's': case 't': case 'u': case 'v': case 'w': case 'x': case 'y': case 'z': case 'A': case 'B': case 'C': case 'D': case 'E': case 'F': case 'G': case 'H': case 'I': case 'J': case 'K': case 'L': case 'M': case 'N': case 'O': case 'P': case 'Q': case 'R': case 'S': case 'T': case 'U': case 'V': case 'W': case 'X': case 'Y': case 'Z': case '_': case '~': case '.': case '-': ns[strindex++]=in; break; default: newlen += 2; /* this'll become a %XX */ if(newlen > alloc) { alloc *= 2; testing_ptr = (char*) xrealloc(ns, alloc); ns = testing_ptr; } //转换成16进制. 58-->3A //%3A 这是3个.这里好像是自动 加 '/0' 如果生成的的字符串,大于4则不会,自动加/0 //在linux 下,这个方法,的count 包括 '/0'所以最后的结果也是正确的. int result = snprintf(&ns[strindex], 4, "%%%02X", in); /* printf("%d ",result); printf("%d ",in); printf("strlen(buf) = %d ",strlen(ns)); printf("%s ",ns); */ strindex+=3; break; } string++; } ns[strindex]=0; return ns; } #ifndef ISXDIGIT # define ISXDIGIT(x) (isxdigit((int) ((unsigned char)x))) #endif //自己写的代码 3 char* rfc3986Encoder(const char* input) { //__android_log_print(ANDROID_LOG_INFO, "Test_jni","encoder start %s ", input); if (!input) return xstrdup(""); //char* sb = (char*)malloc(1024); char* sb = new char[1024]; sb[0]= '