static Size percentEncode( char* encoded, Size encodedLength, const char* source, Size sourceLength) { if (!encoded || !source) return 0; const char* encodedStart = encoded; const char* sourceEnd = source + sourceLength; char temp; encodedLength--; while (source < sourceEnd && encodedLength) { temp = *source; if ((temp >= 'A' && temp <= 'Z') || (temp >= 'a' && temp <= 'z') || (temp >= '0' && temp <= '9') || temp == '-' || temp == '.' || temp == '_' || temp == '~') { *(encoded++) = temp; } else { *(encoded++) = '%'; if (!(--encodedLength)) break; *(encoded++) = hexCharFromValue((unsigned char)temp >> 4); if (!(--encodedLength)) break; *(encoded++) = hexCharFromValue(temp & 0x0F); encodedLength -= 2; } source++; encodedLength--; } *encoded = '\0'; return encoded - encodedStart; }
//URLエンコードにエンコードする void urlEncode(char* encoded ,unsigned int encoded_length ,const char* source) { char temp; encoded_length--; while(*source && encoded_length){ temp =*source; if( (ASCII_ALPHA1_START <= temp && temp<= ASCII_ALPHA1_END) ||(ASCII_ALPHA2_START <= temp && temp<= ASCII_ALPHA2_END) ||(ASCII_DIGIT_START <= temp && temp<= ASCII_DIGIT_END) || temp == ASCII_HYPHEN || temp == ASCII_PERIOD || temp == ASCII_UNDERSCORE || temp == ASCII_TILDA) { //Unreserved Characters *(encoded++) = temp; }else{ //Reserved Characters *(encoded++) = '%'; if(!(--encoded_length)) break; *(encoded++) = hexCharFromValue((unsigned char)temp >> 4); if(!(--encoded_length)) break; *(encoded++) = hexCharFromValue(temp & 0x0F); encoded_length-=2; } source++; encoded_length--; } *encoded = '\0'; }