ldap_escape/src/ldap_escape.cr

47 lines
1.0 KiB
Crystal

module LdapEscape
extend self
FILTER = {
"\u0000" => "\\00", # null
"\u0028" => "\\28", # (
"\u0029" => "\\29", # )
"\u002a" => "\\2a", # *
"\u005c" => "\\5c", # \
}
FILTER_REGEX = /(\0|\x28|\x29|\x2a|\x5c)/
DN_BEGIN = {
"\u0020" => "\\ ", # space
}
DN_BEGIN_REGEX = /^(\x20)/
DN = {
"\u0022" => "\\\"", # "
"\u0023" => "\\#", # #
"\u002b" => "\\+", # +
"\u002c" => "\\,", # ,
"\u003b" => "\\;", # ;
"\u003c" => "\\<", # <
"\u003d" => "\\=", # =
"\u003e" => "\\>", # >
"\u005c" => "\\\\", # \
}
DN_REGEX = /(\x22|\x23|\x2b|\x2c|\x3b|\x3c|\x3d|\x3e|\x5c)/
DN_END = {
'\u0020' => "\\ ", # space
}
DN_END_REGEX = /(\x20)$/
def filter(input : String) : String
input.gsub(FILTER_REGEX) { |match| FILTER[match] }
end
def dn(input : String) : String
input
.gsub(DN_BEGIN_REGEX) { |match| DN_BEGIN[match] }
.gsub(DN_REGEX) { |match| DN[match] }
.gsub(DN_END_REGEX) { |match| DN_END[match] }
end
end