schemetable.c 6.0 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) && defined(USE_CURL_NTLM_CORE) && \\\n"
  63. " (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", "#if defined(USE_WEBSOCKETS) && !defined(CURL_DISABLE_HTTP)" },
  71. {"wss", "#if defined(USE_WEBSOCKETS) && \\\n"
  72. " defined(USE_SSL) && !defined(CURL_DISABLE_HTTP)" },
  73. { NULL, NULL }
  74. };
  75. unsigned int calc(const char *s, int add, int shift)
  76. {
  77. const char *so = s;
  78. unsigned int c = add;
  79. while(*s) {
  80. c <<= shift;
  81. c += *s;
  82. s++;
  83. }
  84. return c;
  85. }
  86. unsigned int num[100];
  87. unsigned int ix[100];
  88. static void showtable(int try, int init, int shift)
  89. {
  90. int nulls = 0;
  91. int i;
  92. for(i = 0; scheme[i].n; ++i)
  93. num[i] = calc(scheme[i].n, init, shift);
  94. for(i = 0; scheme[i].n; ++i)
  95. ix[i] = num[i] % try;
  96. printf("/*\n"
  97. " unsigned int c = %d\n"
  98. " while(l) {\n"
  99. " c <<= %d;\n"
  100. " c += Curl_raw_tolower(*s);\n"
  101. " s++;\n"
  102. " l--;\n"
  103. " }\n"
  104. "*/\n", init, shift);
  105. printf(" static const struct Curl_handler * const protocols[%d] = {", try);
  106. /* generate table */
  107. for(i=0; i < try; i++) {
  108. int match = 0;
  109. int j;
  110. for(j=0; scheme[j].n; j++) {
  111. if(ix[j] == i) {
  112. printf("\n");
  113. printf("%s\n", scheme[j].ifdef);
  114. printf(" &Curl_handler_%s,\n", scheme[j].n);
  115. printf("#else\n NULL,\n");
  116. printf("#endif");
  117. match = 1;
  118. nulls = 0;
  119. break;
  120. }
  121. }
  122. if(!match) {
  123. if(!nulls || (nulls>10)) {
  124. printf("\n ");
  125. nulls = 0;
  126. }
  127. printf(" NULL,", nulls);
  128. nulls++;
  129. }
  130. }
  131. printf("\n };\n");
  132. }
  133. int main(void)
  134. {
  135. int i;
  136. int try;
  137. int besttry = 9999;
  138. int bestadd = 0;
  139. int bestshift = 0;
  140. int add;
  141. int shift;
  142. for(shift = 0; shift < 8; shift++) {
  143. for(add = 0; add < 999; add++) {
  144. for(i = 0; scheme[i].n; ++i) {
  145. unsigned int v = calc(scheme[i].n, add, shift);
  146. int j;
  147. int badcombo = 0;
  148. for(j=0; j < i; j++) {
  149. if(num[j] == v) {
  150. /*
  151. printf("NOPE: %u is a dupe (%s and %s)\n",
  152. v, scheme[i], scheme[j]);
  153. */
  154. badcombo = 1;
  155. break;
  156. }
  157. }
  158. if(badcombo)
  159. break;
  160. num[i] = v;
  161. }
  162. #if 0
  163. for(i = 0; scheme[i].n; ++i) {
  164. printf("%u - %s\n", num[i], scheme[i].n);
  165. }
  166. #endif
  167. /* try different remainders to find smallest possible table */
  168. for(try = 28; try < 199; try++) {
  169. int good = 1;
  170. for(i = 0; scheme[i].n; ++i) {
  171. ix[i] = num[i] % try;
  172. }
  173. /* check for dupes */
  174. for(i = 0; scheme[i].n && good; ++i) {
  175. int j;
  176. for(j=0; j < i; j++) {
  177. if(ix[j] == ix[i]) {
  178. /* printf("NOPE, try %u causes dupes (%d and %d)\n", try, j, 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. }