1 /* 2 * Copyright (C) 2015-2020 Alibaba Group Holding Limited 3 */ 4 #include <stdio.h> 5 #include <stdlib.h> 6 #include <stdint.h> 7 #include <string.h> 8 #include <signal.h> 9 dec2hex(char c)10static char dec2hex(char c) 11 { 12 if (0 <= c && c <= 9) { 13 return c + '0'; 14 } else { 15 if( 10 <= c && c <= 15 ) { 16 return c + 'A' - 10; 17 } else { 18 return -1; 19 } 20 } 21 } 22 uvoice_urlencode(char * url)23void uvoice_urlencode(char *url) 24 { 25 int i = 0; 26 int len = strlen( url ); 27 int res_len = 0; 28 char *res = malloc(len * 3 + 1); 29 memset(res, 0, len * 3 + 1); 30 for (i = 0; i < len; ++i) { 31 char c = url[i]; 32 if (('0' <= c && c <= '9') || 33 ('a' <= c && c <= 'z') || 34 ('A' <= c && c <= 'Z') || 35 c == '/' || c == '.') { 36 res[res_len++] = c; 37 } else { 38 int j = c; 39 if (j < 0) 40 j += 256; 41 int i1, i0; 42 i1 = j / 16; 43 i0 = j - i1 * 16; 44 res[res_len++] = '%'; 45 res[res_len++] = dec2hex((char)i1); 46 res[res_len++] = dec2hex((char)i0); 47 } 48 } 49 res[res_len] = '\0'; 50 memcpy(url, res, res_len + 1); 51 free(res); 52 }