2
0

schemetable.c 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  1. /***************************************************************************
  2. * _ _ ____ _
  3. * Project ___| | | | _ \| |
  4. * / __| | | | |_) | |
  5. * | (__| |_| | _ <| |___
  6. * \___|\___/|_| \_\_____|
  7. *
  8. * Copyright (C) Daniel Stenberg, <daniel@haxx.se>, et al.
  9. *
  10. * This software is licensed as described in the file COPYING, which
  11. * you should have received as part of this distribution. The terms
  12. * are also available at https://curl.se/docs/copyright.html.
  13. *
  14. * You may opt to use, copy, modify, merge, publish, distribute and/or sell
  15. * copies of the Software, and permit persons to whom the Software is
  16. * furnished to do so, under the terms of the COPYING file.
  17. *
  18. * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
  19. * KIND, either express or implied.
  20. *
  21. * SPDX-License-Identifier: curl
  22. *
  23. ***************************************************************************/
  24. #include <stdio.h>
  25. #include <curl/curl.h>
  26. /*
  27. * Use this tool to generate an updated table for the Curl_getn_scheme_handler
  28. * function in url.c.
  29. */
  30. struct detail {
  31. const char *n;
  32. const char *ifdef;
  33. };
  34. static const struct detail scheme[] = {
  35. {"dict", "#ifndef CURL_DISABLE_DICT" },
  36. {"file", "#ifndef CURL_DISABLE_FILE" },
  37. {"ftp", "#ifndef CURL_DISABLE_FTP" },
  38. {"ftps", "#if defined(USE_SSL) && !defined(CURL_DISABLE_FTP)" },
  39. {"gopher", "#ifndef CURL_DISABLE_GOPHER" },
  40. {"gophers", "#if defined(USE_SSL) && !defined(CURL_DISABLE_GOPHER)" },
  41. {"http", "#ifndef CURL_DISABLE_HTTP" },
  42. {"https", "#if defined(USE_SSL) && !defined(CURL_DISABLE_HTTP)" },
  43. {"imap", "#ifndef CURL_DISABLE_IMAP" },
  44. {"imaps", "#if defined(USE_SSL) && !defined(CURL_DISABLE_IMAP)" },
  45. {"ldap", "#ifndef CURL_DISABLE_LDAP" },
  46. {"ldaps", "#if !defined(CURL_DISABLE_LDAP) && \\\n"
  47. " !defined(CURL_DISABLE_LDAPS) && \\\n"
  48. " ((defined(USE_OPENLDAP) && defined(USE_SSL)) || \\\n"
  49. " (!defined(USE_OPENLDAP) && defined(HAVE_LDAP_SSL)))" },
  50. {"mqtt", "#ifndef CURL_DISABLE_MQTT" },
  51. {"pop3", "#ifndef CURL_DISABLE_POP3" },
  52. {"pop3s", "#if defined(USE_SSL) && !defined(CURL_DISABLE_POP3)" },
  53. {"rtmp", "#ifdef USE_LIBRTMP" },
  54. {"rtmpt", "#ifdef USE_LIBRTMP" },
  55. {"rtmpe", "#ifdef USE_LIBRTMP" },
  56. {"rtmpte", "#ifdef USE_LIBRTMP" },
  57. {"rtmps", "#ifdef USE_LIBRTMP" },
  58. {"rtmpts", "#ifdef USE_LIBRTMP" },
  59. {"rtsp", "#ifndef CURL_DISABLE_RTSP" },
  60. {"scp", "#if defined(USE_SSH) && !defined(USE_WOLFSSH)" },
  61. {"sftp", "#if defined(USE_SSH)" },
  62. {"smb", "#if !defined(CURL_DISABLE_SMB) && \\\n"
  63. " defined(USE_CURL_NTLM_CORE) && (SIZEOF_CURL_OFF_T > 4)" },
  64. {"smbs", "#if defined(USE_SSL) && !defined(CURL_DISABLE_SMB) && \\\n"
  65. " defined(USE_CURL_NTLM_CORE) && (SIZEOF_CURL_OFF_T > 4)" },
  66. {"smtp", "#ifndef CURL_DISABLE_SMTP" },
  67. {"smtps", "#if defined(USE_SSL) && !defined(CURL_DISABLE_SMTP)" },
  68. {"telnet", "#ifndef CURL_DISABLE_TELNET" },
  69. {"tftp", "#ifndef CURL_DISABLE_TFTP" },
  70. {"ws",
  71. "#if !defined(CURL_DISABLE_WEBSOCKETS) && !defined(CURL_DISABLE_HTTP)" },
  72. {"wss", "#if !defined(CURL_DISABLE_WEBSOCKETS) && \\\n"
  73. " defined(USE_SSL) && !defined(CURL_DISABLE_HTTP)" },
  74. { NULL, NULL }
  75. };
  76. unsigned int calc(const char *s, int add, int shift)
  77. {
  78. const char *so = s;
  79. unsigned int c = add;
  80. while(*s) {
  81. c <<= shift;
  82. c += *s;
  83. s++;
  84. }
  85. return c;
  86. }
  87. unsigned int num[100];
  88. unsigned int ix[100];
  89. static void showtable(int try, int init, int shift)
  90. {
  91. int nulls = 0;
  92. int i;
  93. for(i = 0; scheme[i].n; ++i)
  94. num[i] = calc(scheme[i].n, init, shift);
  95. for(i = 0; scheme[i].n; ++i)
  96. ix[i] = num[i] % try;
  97. printf("/*\n"
  98. " unsigned int c = %d\n"
  99. " while(l) {\n"
  100. " c <<= %d;\n"
  101. " c += Curl_raw_tolower(*s);\n"
  102. " s++;\n"
  103. " l--;\n"
  104. " }\n"
  105. "*/\n", init, shift);
  106. printf(" static const struct Curl_handler * const protocols[%d] = {", try);
  107. /* generate table */
  108. for(i = 0; i < try; i++) {
  109. int match = 0;
  110. int j;
  111. for(j = 0; scheme[j].n; j++) {
  112. if(ix[j] == i) {
  113. printf("\n");
  114. printf("%s\n", scheme[j].ifdef);
  115. printf(" &Curl_handler_%s,\n", scheme[j].n);
  116. printf("#else\n NULL,\n");
  117. printf("#endif");
  118. match = 1;
  119. nulls = 0;
  120. break;
  121. }
  122. }
  123. if(!match) {
  124. if(!nulls || (nulls > 10)) {
  125. printf("\n ");
  126. nulls = 0;
  127. }
  128. printf(" NULL,", nulls);
  129. nulls++;
  130. }
  131. }
  132. printf("\n };\n");
  133. }
  134. int main(void)
  135. {
  136. int i;
  137. int try;
  138. int besttry = 9999;
  139. int bestadd = 0;
  140. int bestshift = 0;
  141. int add;
  142. int shift;
  143. for(shift = 0; shift < 8; shift++) {
  144. for(add = 0; add < 999; add++) {
  145. for(i = 0; scheme[i].n; ++i) {
  146. unsigned int v = calc(scheme[i].n, add, shift);
  147. int j;
  148. int badcombo = 0;
  149. for(j = 0; j < i; j++) {
  150. if(num[j] == v) {
  151. /*
  152. printf("NOPE: %u is a dupe (%s and %s)\n",
  153. v, scheme[i], scheme[j]);
  154. */
  155. badcombo = 1;
  156. break;
  157. }
  158. }
  159. if(badcombo)
  160. break;
  161. num[i] = v;
  162. }
  163. #if 0
  164. for(i = 0; scheme[i].n; ++i) {
  165. printf("%u - %s\n", num[i], scheme[i].n);
  166. }
  167. #endif
  168. /* try different remainders to find smallest possible table */
  169. for(try = 28; try < 199; try++) {
  170. int good = 1;
  171. for(i = 0; scheme[i].n; ++i) {
  172. ix[i] = num[i] % try;
  173. }
  174. /* check for dupes */
  175. for(i = 0; scheme[i].n && good; ++i) {
  176. int j;
  177. for(j = 0; j < i; j++) {
  178. if(ix[j] == ix[i]) {
  179. good = 0;
  180. break;
  181. }
  182. }
  183. }
  184. if(good) {
  185. if(try < besttry) {
  186. besttry = try;
  187. bestadd = add;
  188. bestshift = shift;
  189. }
  190. break;
  191. }
  192. }
  193. }
  194. }
  195. showtable(besttry, bestadd, bestshift);
  196. }