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
|
/***************************************************************************
* _ _ ____ _
* Project ___| | | | _ \| |
* / __| | | | |_) | |
* | (__| |_| | _ <| |___
* \___|\___/|_| \_\_____|
*
* Copyright (C) Daniel Stenberg, <daniel@haxx.se>, et al.
*
* This software is licensed as described in the file COPYING, which
* you should have received as part of this distribution. The terms
* are also available at https://curl.se/docs/copyright.html.
*
* You may opt to use, copy, modify, merge, publish, distribute and/or sell
* copies of the Software, and permit persons to whom the Software is
* furnished to do so, under the terms of the COPYING file.
*
* This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
* KIND, either express or implied.
*
* SPDX-License-Identifier: curl
*
***************************************************************************/
#include <stdio.h>
#include <curl/curl.h>
/*
* Use this tool to generate an updated table for the Curl_getn_scheme_handler
* function in url.c.
*/
struct detail {
const char *n;
const char *ifdef;
};
static const struct detail scheme[] = {
{"dict", "#ifndef CURL_DISABLE_DICT" },
{"file", "#ifndef CURL_DISABLE_FILE" },
{"ftp", "#ifndef CURL_DISABLE_FTP" },
{"ftps", "#if defined(USE_SSL) && !defined(CURL_DISABLE_FTP)" },
{"gopher", "#ifndef CURL_DISABLE_GOPHER" },
{"gophers", "#if defined(USE_SSL) && !defined(CURL_DISABLE_GOPHER)" },
{"http", "#ifndef CURL_DISABLE_HTTP" },
{"https", "#if defined(USE_SSL) && !defined(CURL_DISABLE_HTTP)" },
{"imap", "#ifndef CURL_DISABLE_IMAP" },
{"imaps", "#if defined(USE_SSL) && !defined(CURL_DISABLE_IMAP)" },
{"ldap", "#ifndef CURL_DISABLE_LDAP" },
{"ldaps", "#if !defined(CURL_DISABLE_LDAP) && \\\n"
" !defined(CURL_DISABLE_LDAPS) && \\\n"
" ((defined(USE_OPENLDAP) && defined(USE_SSL)) || \\\n"
" (!defined(USE_OPENLDAP) && defined(HAVE_LDAP_SSL)))" },
{"mqtt", "#ifndef CURL_DISABLE_MQTT" },
{"pop3", "#ifndef CURL_DISABLE_POP3" },
{"pop3s", "#if defined(USE_SSL) && !defined(CURL_DISABLE_POP3)" },
{"rtmp", "#ifdef USE_LIBRTMP" },
{"rtmpt", "#ifdef USE_LIBRTMP" },
{"rtmpe", "#ifdef USE_LIBRTMP" },
{"rtmpte", "#ifdef USE_LIBRTMP" },
{"rtmps", "#ifdef USE_LIBRTMP" },
{"rtmpts", "#ifdef USE_LIBRTMP" },
{"rtsp", "#ifndef CURL_DISABLE_RTSP" },
{"scp", "#if defined(USE_SSH) && !defined(USE_WOLFSSH)" },
{"sftp", "#if defined(USE_SSH)" },
{"smb", "#if !defined(CURL_DISABLE_SMB) && \\\n"
" defined(USE_CURL_NTLM_CORE) && (SIZEOF_CURL_OFF_T > 4)" },
{"smbs", "#if defined(USE_SSL) && !defined(CURL_DISABLE_SMB) && \\\n"
" defined(USE_CURL_NTLM_CORE) && (SIZEOF_CURL_OFF_T > 4)" },
{"smtp", "#ifndef CURL_DISABLE_SMTP" },
{"smtps", "#if defined(USE_SSL) && !defined(CURL_DISABLE_SMTP)" },
{"telnet", "#ifndef CURL_DISABLE_TELNET" },
{"tftp", "#ifndef CURL_DISABLE_TFTP" },
{"ws",
"#if !defined(CURL_DISABLE_WEBSOCKETS) && !defined(CURL_DISABLE_HTTP)" },
{"wss", "#if !defined(CURL_DISABLE_WEBSOCKETS) && \\\n"
" defined(USE_SSL) && !defined(CURL_DISABLE_HTTP)" },
{ NULL, NULL }
};
unsigned int calc(const char *s, int add, int shift)
{
const char *so = s;
unsigned int c = add;
while(*s) {
c <<= shift;
c += *s;
s++;
}
return c;
}
unsigned int num[100];
unsigned int ix[100];
static void showtable(int try, int init, int shift)
{
int nulls = 0;
int i;
for(i = 0; scheme[i].n; ++i)
num[i] = calc(scheme[i].n, init, shift);
for(i = 0; scheme[i].n; ++i)
ix[i] = num[i] % try;
printf("/*\n"
" unsigned int c = %d\n"
" while(l) {\n"
" c <<= %d;\n"
" c += Curl_raw_tolower(*s);\n"
" s++;\n"
" l--;\n"
" }\n"
"*/\n", init, shift);
printf(" static const struct Curl_handler * const protocols[%d] = {", try);
/* generate table */
for(i = 0; i < try; i++) {
int match = 0;
int j;
for(j = 0; scheme[j].n; j++) {
if(ix[j] == i) {
printf("\n");
printf("%s\n", scheme[j].ifdef);
printf(" &Curl_handler_%s,\n", scheme[j].n);
printf("#else\n NULL,\n");
printf("#endif");
match = 1;
nulls = 0;
break;
}
}
if(!match) {
if(!nulls || (nulls > 10)) {
printf("\n ");
nulls = 0;
}
printf(" NULL,", nulls);
nulls++;
}
}
printf("\n };\n");
}
int main(void)
{
int i;
int try;
int besttry = 9999;
int bestadd = 0;
int bestshift = 0;
int add;
int shift;
for(shift = 0; shift < 8; shift++) {
for(add = 0; add < 999; add++) {
for(i = 0; scheme[i].n; ++i) {
unsigned int v = calc(scheme[i].n, add, shift);
int j;
int badcombo = 0;
for(j = 0; j < i; j++) {
if(num[j] == v) {
/*
printf("NOPE: %u is a dupe (%s and %s)\n",
v, scheme[i], scheme[j]);
*/
badcombo = 1;
break;
}
}
if(badcombo)
break;
num[i] = v;
}
#if 0
for(i = 0; scheme[i].n; ++i) {
printf("%u - %s\n", num[i], scheme[i].n);
}
#endif
/* try different remainders to find smallest possible table */
for(try = 28; try < 199; try++) {
int good = 1;
for(i = 0; scheme[i].n; ++i) {
ix[i] = num[i] % try;
}
/* check for dupes */
for(i = 0; scheme[i].n && good; ++i) {
int j;
for(j = 0; j < i; j++) {
if(ix[j] == ix[i]) {
good = 0;
break;
}
}
}
if(good) {
if(try < besttry) {
besttry = try;
bestadd = add;
bestshift = shift;
}
break;
}
}
}
}
showtable(besttry, bestadd, bestshift);
}
|