/usr/share/tcltk/tcllib1.14/stringprep/stringprep.tcl is in tcllib 1.14-dfsg-1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 | # stringprep.tcl -*- tcl -*-
#
# Implementation of RFC 3454 "Preparation of Internationalized Strings"
#
# Copyright (c) 2007 Sergei Golovan
#
# See the file "license.terms" for information on usage and redistribution
# of this file, and for a DISCLAIMER OF ALL WARRANTIES.
#
# RCS: @(#) $Id: stringprep.tcl,v 1.2 2009/11/02 00:26:44 patthoyts Exp $
package require stringprep::data 1.0
package require unicode 1.0
namespace eval ::stringprep {
variable profiles
array unset profiles
}
########################################################################
# Register new stringprep profile
proc ::stringprep::register {profile args} {
variable profiles
array set props [list -mapping "" \
-normalization "" \
-prohibited 0 \
-prohibitedList {} \
-prohibitedCommand "" \
-prohibitedBidi 0]
foreach {opt val} $args {
switch -- $opt {
-mapping {
foreach tab $val {
switch -- $tab {
B.1 - B.2 - B.3 {}
default {
return -code error \
"::stringprep::register -mapping: Only\
B.1, B.2, B.3 tables are allowed"
}
}
}
set props(-mapping) $val
}
-normalization {
switch -- $val {
D - C - KD - KC - "" {
set props(-normalization) $val
}
default {
return -code error \
"::stringprep::register -normalization: Only\
D, C, KD, KC or empty normalization is allowed"
}
}
}
-prohibited {
set mask 0
set c39count 0
foreach tab $val {
switch -- $tab {
A.1 { set mask [expr {$mask | $data::A1Mask}] }
C.1.1 { set mask [expr {$mask | $data::C11Mask}] }
C.1.2 { set mask [expr {$mask | $data::C12Mask}] }
C.2.1 { set mask [expr {$mask | $data::C21Mask}] }
C.2.2 { set mask [expr {$mask | $data::C22Mask}] }
C.3 - C.4 - C.5 - C.6 - C.7 - C.8 -
C.9 { incr c39count }
default {
return -code error \
"::stringprep::register -prohibited: Only\
tables A.1, C.* are allowed to prohibit"
}
}
}
if {$c39count > 0 && $c39count < 7} {
return -code error \
"::stringprep::register -prohibited: Must prohibit\
all C.3--C.9 tables or none of them"
}
if {$c39count > 0} {
set mask [expr {$mask | $data::C39Mask}]
}
set props(-prohibited) $mask
}
-prohibitedList {
if {[catch {
foreach uc $val {
if {![string is integer -strict $uc]} {
error not_integer
} else {
lappend props(-prohibitedList) [expr {$uc}]
}
}}]} {
return -code error \
"::stringprep::register -prohibitedList: List\
of integers expected"
}
}
-prohibitedCommand {
set props(-prohibitedCommand) $val
}
-prohibitedBidi {
if {[string is true -strict $val]} {
set props(-prohibitedBidi) 1
} elseif {[string is false -strict $val]} {
set props(-prohibitedBidi) 0
} else {
return -code error \
"::stringprep::register -prohibitedBidi: Boolean\
value expected"
}
}
}
}
set profiles($profile) [array get props]
}
########################################################################
# Register identity profile
::stringprep::register none \
-mapping {} \
-normalization {} \
-prohibited {} \
-prohibitedBidi 0
########################################################################
proc ::stringprep::stringprep {profile str} {
variable profiles
if {![info exists profiles($profile)]} {
return -code error invalid_profile
}
set uclist [::unicode::fromstring $str]
set uclist [map $profile $uclist]
if {[llength $uclist] == 0} {
return ""
}
set uclist [normalize $profile $uclist]
if {[prohibited $profile $uclist]} {
return -code error prohibited_character
}
if {[prohibited_bidi $profile $uclist]} {
return -code error prohibited_bidi
}
::unicode::tostring $uclist
}
########################################################################
proc ::stringprep::compare {profile str1 str2} {
string compare [stringprep $profile $str1] [stringprep $profile $str2]
}
########################################################################
# Mapping (section 3)
proc ::stringprep::map {profile uclist} {
variable profiles
array set props $profiles($profile)
set B1Mask 0
set B3Mask 0
set B2 0
foreach tab $props(-mapping) {
switch -- $tab {
B.1 { set B1Mask $data::B1Mask }
B.2 { set B2 1 }
B.3 { set B3Mask $data::B3Mask }
}
}
set res {}
foreach uc $uclist {
set info [data::GetUniCharInfo $uc]
if {$info & $B1Mask} {
# Map to nothing
continue
}
if {$B2 || ($info & $B3Mask)} {
if {$info & $data::MCMask} {
set res [concat $res [data::GetMC $info]]
} else {
lappend res [expr {$uc + [data::GetDelta $info]}]
}
} else {
lappend res $uc
}
}
return $res
}
########################################################################
# Normalization (section 4)
proc ::stringprep::normalize {profile uclist} {
variable profiles
array set props $profiles($profile)
switch -- $props(-normalization) {
D - C - KD - KC {
return [::unicode::normalize $props(-normalization) $uclist]
}
default { return $uclist }
}
}
########################################################################
# Prohibit (section 5)
proc ::stringprep::prohibited {profile uclist} {
variable profiles
array set props $profiles($profile)
foreach uc $uclist {
set info [data::GetUniCharInfo $uc]
if {($info & $props(-prohibited)) || \
[lsearch -exact $props(-prohibitedList) $uc] >= 0} {
return 1
} elseif {$props(-prohibitedCommand) != "" && \
[uplevel #0 $props(-prohibitedCommand) [list $uc]]} {
return 1
}
}
return 0
}
########################################################################
# Check bidi (section 6)
proc ::stringprep::prohibited_bidi {profile uclist} {
variable profiles
array set props $profiles($profile)
if {!$props(-prohibitedBidi)} {
return 0
}
set info [data::GetUniCharInfo [lindex $uclist 0]]
set first_ral [expr {$info & $data::D1Mask}]
set last_ral 0
set have_ral 0
set have_l 0
foreach uc $uclist {
set info [data::GetUniCharInfo $uc]
set last_ral [expr {$info & $data::D1Mask}]
set have_ral [expr {$have_ral || $last_ral}]
set have_l [expr {$have_l || ($info & $data::D2Mask)}]
}
if {$have_ral && (!$first_ral || !$last_ral || $have_l)} {
return 1
} else {
return 0
}
}
########################################################################
package provide stringprep 1.0.1
########################################################################
|