ap.inc 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. { Licensed to the Apache Software Foundation (ASF) under one or more
  2. * contributor license agreements. See the NOTICE file distributed with
  3. * this work for additional information regarding copyright ownership.
  4. * The ASF licenses this file to You under the Apache License, Version 2.0
  5. * (the "License"); you may not use this file except in compliance with
  6. * the License. You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. }
  16. {
  17. * The ap_vsnprintf/ap_snprintf functions are based on, and used with the
  18. * permission of, the SIO stdio-replacement strx_* functions by Panos
  19. * Tsirigotis <[email protected]> for xinetd.
  20. }
  21. function ap_cpystrn(param1: PChar; const param2: PChar; param3: size_t): PChar;
  22. {$IFDEF WINDOWS} stdcall; {$ELSE} cdecl; {$ENDIF} external LibHTTPD;
  23. {int ap_slack(int, int);
  24. int ap_execle(const char *, const char *, ...);
  25. int ap_execve(const char *, char * const argv[], char * const envp[]); }
  26. //function ap_getpass(const prompt: PChar; pwbuf: PChar; bufsiz: size_t): cint;
  27. // {$IFDEF WINDOWS} stdcall; {$ELSE} cdecl; {$ENDIF} external LibHTTPD;
  28. //{$ifndef ap_strtol}
  29. //function ap_strtol(const nptr: PChar; endptr: PPChar; base: cint): clong;
  30. // {$IFDEF WINDOWS} stdcall; {$ELSE} cdecl; {$ENDIF} external LibHTTPD;
  31. //{$endif}
  32. { small utility macros to make things easier to read }
  33. {.$ifdef WIN32
  34. #define ap_killpg(x, y)
  35. #else
  36. #ifdef NO_KILLPG
  37. #define ap_killpg(x, y) (kill (-(x), (y)))
  38. #else
  39. #define ap_killpg(x, y) (killpg ((x), (y)))
  40. #endif
  41. #endif} { WIN32 }
  42. { ap_vformatter() is a generic printf-style formatting routine
  43. * with some extensions. The extensions are:
  44. *
  45. * %pA takes a struct in_addr *, and prints it as a.b.c.d
  46. * %pI takes a struct sockaddr_in * and prints it as a.b.c.d:port
  47. * %pp takes a void * and outputs it in hex
  48. *
  49. * The %p hacks are to force gcc's printf warning code to skip
  50. * over a pointer argument without complaining. This does
  51. * mean that the ANSI-style %p (output a void * in hex format) won't
  52. * work as expected at all, but that seems to be a fair trade-off
  53. * for the increased robustness of having printf-warnings work.
  54. *
  55. * Additionally, ap_vformatter allows for arbitrary output methods
  56. * using the ap_vformatter_buff and flush_func.
  57. *
  58. * The ap_vformatter_buff has two elements curpos and endpos.
  59. * curpos is where ap_vformatter will write the next byte of output.
  60. * It proceeds writing output to curpos, and updating curpos, until
  61. * either the end of output is reached, or curpos == endpos (i.e. the
  62. * buffer is full).
  63. *
  64. * If the end of output is reached, ap_vformatter returns the
  65. * number of bytes written.
  66. *
  67. * When the buffer is full, the flush_func is called. The flush_func
  68. * can return -1 to indicate that no further output should be attempted,
  69. * and ap_vformatter will return immediately with -1. Otherwise
  70. * the flush_func should flush the buffer in whatever manner is
  71. * appropriate, re-initialize curpos and endpos, and return 0.
  72. *
  73. * Note that flush_func is only invoked as a result of attempting to
  74. * write another byte at curpos when curpos >= endpos. So for
  75. * example, it's possible when the output exactly matches the buffer
  76. * space available that curpos == endpos will be true when
  77. * ap_vformatter returns.
  78. *
  79. * ap_vformatter does not call out to any other code, it is entirely
  80. * self-contained. This allows the callers to do things which are
  81. * otherwise "unsafe". For example, ap_psprintf uses the "scratch"
  82. * space at the unallocated end of a block, and doesn't actually
  83. * complete the allocation until ap_vformatter returns. ap_psprintf
  84. * would be completely broken if ap_vformatter were to call anything
  85. * that used a pool. Similarly http_bprintf() uses the "scratch"
  86. * space at the end of its output buffer, and doesn't actually note
  87. * that the space is in use until it either has to flush the buffer
  88. * or until ap_vformatter returns.
  89. }
  90. type
  91. ap_vformatter_buff = record
  92. curpos: PChar;
  93. endpos: PChar;
  94. end;
  95. Pap_vformatter_buff = ^ap_vformatter_buff;
  96. flush_func_t = function (param: Pap_vformatter_buff): cint;
  97. function ap_vformatter(flush_func: flush_func_t;
  98. param2: Pap_vformatter_buff; const fmt: PChar; ap: va_list): cint;
  99. {$IFDEF WINDOWS} stdcall; {$ELSE} cdecl; {$ENDIF} external LibHTTPD;
  100. { These are snprintf implementations based on ap_vformatter().
  101. *
  102. * Note that various standards and implementations disagree on the return
  103. * value of snprintf, and side-effects due to %n in the formatting string.
  104. * ap_snprintf behaves as follows:
  105. *
  106. * Process the format string until the entire string is exhausted, or
  107. * the buffer fills. If the buffer fills then stop processing immediately
  108. * (so no further %n arguments are processed), and return the buffer
  109. * length. In all cases the buffer is NUL terminated. The return value
  110. * is the number of characters placed in the buffer, excluding the
  111. * terminating NUL. All this implies that, at most, (len-1) characters
  112. * will be copied over; if the return value is >= len, then truncation
  113. * occurred.
  114. *
  115. * In no event does ap_snprintf return a negative number.
  116. }
  117. function ap_snprintf(buf: PChar; len: size_t; const format: PChar;
  118. others: array of const): cint; cdecl; external LibHTTPD;
  119. // __attribute__((format(printf,3,4)));
  120. function ap_vsnprintf(buf: PChar; len: size_t; const format: PChar; ap: va_list): cint;
  121. {$IFDEF WINDOWS} stdcall; {$ELSE} cdecl; {$ENDIF} external LibHTTPD;
  122. { Simple BASE64 encode/decode functions.
  123. *
  124. * As we might encode binary strings, hence we require the length of
  125. * the incoming plain source. And return the length of what we decoded.
  126. *
  127. * The decoding function takes any non valid char (i.e. whitespace, \0
  128. * or anything non A-Z,0-9 etc as terminal.
  129. *
  130. * plain strings/binary sequences are not assumed '\0' terminated. Encoded
  131. * strings are neither. But propably should.
  132. *
  133. }
  134. function ap_base64encode_len(len: cint): cint;
  135. {$IFDEF WINDOWS} stdcall; {$ELSE} cdecl; {$ENDIF} external LibHTTPD;
  136. function ap_base64encode(coded_dst: PChar; const plain_src: PChar; len_plain_src: cint): cint;
  137. {$IFDEF WINDOWS} stdcall; {$ELSE} cdecl; {$ENDIF} external LibHTTPD;
  138. function ap_base64encode_binary(coded_dst: PChar; const plain_src: PChar; len_plain_src: cint): cint;
  139. {$IFDEF WINDOWS} stdcall; {$ELSE} cdecl; {$ENDIF} external LibHTTPD;
  140. function ap_base64decode_len(const coded_src: PChar): cint;
  141. {$IFDEF WINDOWS} stdcall; {$ELSE} cdecl; {$ENDIF} external LibHTTPD;
  142. function ap_base64decode(plain_dst: PChar; const coded_src: PChar): cint;
  143. {$IFDEF WINDOWS} stdcall; {$ELSE} cdecl; {$ENDIF} external LibHTTPD;
  144. function ap_base64decode_binary(plain_dst: PChar; const coded_src: PChar): cint;
  145. {$IFDEF WINDOWS} stdcall; {$ELSE} cdecl; {$ENDIF} external LibHTTPD;
  146. { Password validation, as used in AuthType Basic which is able to cope
  147. * (based on the prefix) with the SHA1, Apache's internal MD5 and (depending
  148. * on your platform either plain or crypt(3) passwords.
  149. }
  150. function ap_validate_password(const passwd, hash: PChar): PChar;
  151. {$IFDEF WINDOWS} stdcall; {$ELSE} cdecl; {$ENDIF} external LibHTTPD;