webmcp
diff framework/env/encode/file_path_element.lua @ 0:9fdfb27f8e67
Version 1.0.0
author | jbe/bsw |
---|---|
date | Sun Oct 25 12:00:00 2009 +0100 (2009-10-25) |
parents | |
children |
line diff
1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/framework/env/encode/file_path_element.lua Sun Oct 25 12:00:00 2009 +0100 1.3 @@ -0,0 +1,22 @@ 1.4 +--[[-- 1.5 +encoded_path_element = -- string which can't contain evil stuff like "/" 1.6 +encode.file_path_element( 1.7 + path_element -- string to be encoded 1.8 +) 1.9 + 1.10 +This function is encoding a string in a way that it can be used as a file or directory name, without security risks. See the source for details. 1.11 + 1.12 +--]]-- 1.13 + 1.14 +function encode.file_path_element(path_element) 1.15 + return ( 1.16 + string.gsub( 1.17 + string.gsub( 1.18 + path_element, "[^0-9A-Za-z_%.-]", 1.19 + function(char) 1.20 + return string.format("%%%02x", string.byte(char)) 1.21 + end 1.22 + ), "^%.", string.format("%%%%%02x", string.byte(".")) 1.23 + ) 1.24 + ) 1.25 +end