1
2
3
4
5
6
7
8
9
10
11
|
#! /usr/bin/env tclsh
set modulus 16777259
proc filed_hash {str mod} {
set retval [expr {$mod - 1}]
set prev [expr {$mod % 255}]
for {set idx 0} {$idx < [string length $str]} {incr idx} {
set curr [string index $str $idx]
binary scan $curr H* curr
set curr [format %u 0x$curr]
|
>
>
>
>
>
>
>
>
>
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
|
#! /usr/bin/env tclsh
if {[llength $argv] != 1} {
puts stderr "Usage: generate-mime-type <file>"
exit 1
}
set mimeinfofile [lindex $argv 0]
set modulus 16777259
# Must match what is in filed.c
proc filed_hash {str mod} {
set retval [expr {$mod - 1}]
set prev [expr {$mod % 255}]
for {set idx 0} {$idx < [string length $str]} {incr idx} {
set curr [string index $str $idx]
binary scan $curr H* curr
set curr [format %u 0x$curr]
|
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
|
set retval [expr {$retval % $mod}]
return $retval
}
set mimeinfofile "/etc/httpd/mime.types"
set fd [open $mimeinfofile]
set mimeinfo [read $fd]
close $fd
foreach line [split $mimeinfo "\n"] {
regsub {#.*} $line {} line
set line [string trim $line]
if {$line == ""} {
continue
}
|
<
|
>
|
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
|
set retval [expr {$retval % $mod}]
return $retval
}
# Read contents of mime types file
set fd [open $mimeinfofile]
set mimeinfo [read $fd]
close $fd
# Parse into type and extensions pairs
foreach line [split $mimeinfo "\n"] {
regsub {#.*} $line {} line
set line [string trim $line]
if {$line == ""} {
continue
}
|
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
|
continue
}
set extensioninfo($extension) $mime
}
}
foreach extension [array names extensioninfo] {
set hash_id [filed_hash $extension $modulus]
lappend hashinfo($hash_id) $extension
}
puts "\tswitch (filed_hash((const unsigned char *) p, $modulus)) \{"
foreach hash [lsort -integer -increasing [array names hashinfo]] {
puts "\t\tcase $hash:"
foreach extension $hashinfo($hash) {
puts "\t\t\tif (strcmp(p, \"$extension\") == 0) \{"
puts "\t\t\t\treturn(\"$extensioninfo($extension)\");"
puts "\t\t\t\}"
}
puts "\t\t\treturn(FILED_DEFAULT_TYPE);"
}
puts "\t\}"
|
>
>
>
>
>
>
>
|
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
|
continue
}
set extensioninfo($extension) $mime
}
}
# For every extension, generate a hash
# For every hash, note the extension
foreach extension [array names extensioninfo] {
set hash_id [filed_hash $extension $modulus]
lappend hashinfo($hash_id) $extension
}
# Emit a C fragment to take a pointer (p) to an extension determine the mime type
puts "\tswitch (filed_hash((const unsigned char *) p, $modulus)) \{"
foreach hash [lsort -integer -increasing [array names hashinfo]] {
puts "\t\tcase $hash:"
foreach extension $hashinfo($hash) {
puts "\t\t\tif (strcmp(p, \"$extension\") == 0) \{"
puts "\t\t\t\treturn(\"$extensioninfo($extension)\");"
puts "\t\t\t\}"
}
puts "\t\t\treturn(FILED_DEFAULT_TYPE);"
}
puts "\t\}"
# Declare victory
exit 0
|