You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

182 lines
4.7 KiB

​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
  1. /* source: utils.c */
  2. /* Copyright Gerhard Rieger and contributors (see file CHANGES) */
  3. /* Published under the GNU General Public License V.2, see file COPYING */
  4. /* useful additions to C library */
  5. #include "config.h"
  6. #include "sysincludes.h"
  7. #include "compat.h" /* socklen_t */
  8. #include "mytypes.h"
  9. #include "sycls.h"
  10. #include "utils.h"
  11. #if !HAVE_PROTOTYPE_LIB_memrchr
  12. /* GNU extension, available since glibc 2.1.91 */
  13. void *memrchr(const void *s, int c, size_t n) {
  14. const unsigned char *t = ((unsigned char *)s)+n;
  15. while (--t >= (unsigned char *)s) {
  16. if (*t == c) break;
  17. }
  18. if (t < (unsigned char *)s)
  19. return NULL;
  20. return (void *)t;
  21. }
  22. #endif /* !HAVE_PROTOTYPE_LIB_memrchr */
  23. void *memdup(const void *src, size_t n) {
  24. void *dest;
  25. if ((dest = Malloc(n)) == NULL) {
  26. return NULL;
  27. }
  28. memcpy(dest, src, n);
  29. return dest;
  30. }
  31. /* search the keyword-table for a match of the leading part of name. */
  32. /* returns the pointer to the matching field of the keyword or NULL if no
  33. keyword was found. */
  34. const struct wordent *keyw(const struct wordent *keywds, const char *name, unsigned int nkeys) {
  35. unsigned int lower, upper, mid;
  36. int r;
  37. lower = 0;
  38. upper = nkeys;
  39. while (upper - lower > 1)
  40. {
  41. mid = (upper + lower) >> 1;
  42. if (!(r = strcasecmp(keywds[mid].name, name)))
  43. {
  44. return &keywds[mid];
  45. }
  46. if (r < 0)
  47. lower = mid;
  48. else
  49. upper = mid;
  50. }
  51. if (nkeys > 0 && !(strcasecmp(keywds[lower].name, name)))
  52. {
  53. return &keywds[lower];
  54. }
  55. return NULL;
  56. }
  57. /* Linux: setenv(), AIX (4.3?): putenv() */
  58. #if !HAVE_SETENV
  59. int setenv(const char *name, const char *value, int overwrite) {
  60. int result;
  61. char *env;
  62. if (!overwrite) {
  63. if (getenv(name)) return 0; /* already exists */
  64. }
  65. if ((env = Malloc(strlen(name)+strlen(value)+2)) == NULL) {
  66. return -1;
  67. }
  68. sprintf(env, "%s=%s", name, value);
  69. if ((result = putenv(env)) != 0) { /* AIX docu says "... nonzero ..." */
  70. free(env);
  71. result = -1;
  72. }
  73. /* linux "man putenv" says: ...this string becomes part of the environment*/
  74. return result;
  75. }
  76. #endif /* !HAVE_SETENV */
  77. /* sanitizes an "untrusted" character. output buffer must provide at least 4
  78. characters space.
  79. Does not append \0. returns length of output (currently: max 4) */
  80. static size_t sanitize_char(char c, char *o, int style) {
  81. int hn; /* high nibble */
  82. int ln; /* low nibble */
  83. int n; /* written chars */
  84. if (isprint(c)) {
  85. *o = c;
  86. return 1;
  87. }
  88. *o++ = '\\';
  89. n = 2;
  90. switch (c) {
  91. case '\0': *o++ = '0'; break;
  92. case '\a': *o++ = 'a'; break;
  93. case '\b': *o++ = 'b'; break;
  94. case '\t': *o++ = 't'; break;
  95. case '\n': *o++ = 'n'; break;
  96. case '\v': *o++ = 'v'; break;
  97. case '\f': *o++ = 'f'; break;
  98. case '\r': *o++ = 'r'; break;
  99. case '\'': *o++ = '\''; break;
  100. case '\"': *o++ = '"'; break;
  101. case '\\': *o++ = '\\'; break;
  102. default:
  103. *o++ = 'x';
  104. hn = (c>>4)&0x0f;
  105. ln = c&0x0f;
  106. *o++ = (hn>=10 ? (('A'-1)+(hn-10)) : ('0'+hn));
  107. *o++ = (ln>=10 ? (('A'-1)+(ln-10)) : ('0'+ln));
  108. n = 4;
  109. }
  110. return n;
  111. }
  112. /* sanitizes "untrusted" text, replacing special control characters with the C
  113. string version (eg."\n"), and replacing unprintable chars with hex
  114. representation ("\xAB").
  115. text can grow to four times of input, so keep output buffer long enough!
  116. returns a pointer to the first untouched byte of the output buffer.
  117. Output is not \0 terminated.
  118. */
  119. char *sanitize_string(const char *data, /* input data */
  120. size_t bytes, /* length of input data, >=0 */
  121. char *coded, /* output buffer, must be long enough */
  122. int style
  123. ) {
  124. int c;
  125. while (bytes > 0) {
  126. c = *(unsigned char *)data++;
  127. coded += sanitize_char(c, coded, style);
  128. --bytes;
  129. }
  130. return coded;
  131. }
  132. /* copies a substring out of a given buff
  133. returns scratch, \0 terminated; scratch must provide len+1 bytes
  134. */
  135. char *xiosubstr(char *scratch, const char *str, size_t from, size_t len) {
  136. char *scratch0 = scratch;
  137. str += from;
  138. while (len--) {
  139. *scratch++ = *str++;
  140. }
  141. *scratch = '\0';
  142. return scratch0;
  143. }
  144. /* since version 1.7.2.4 socat supports C-99 behaviour of snprintf but still
  145. can handle the old glibc case with -1 return on truncation.
  146. Do not rely on exact return value in case of truncation
  147. */
  148. int xio_snprintf(char *str, size_t size, const char *format, ...) {
  149. va_list ap;
  150. int result;
  151. va_start(ap, format);
  152. result = vsnprintf(str, size, format, ap);
  153. #if ! HAVE_C99_SNPRINTF
  154. if (result < 0) {
  155. result = size+63; /* indicate truncation with just some guess */
  156. }
  157. #endif /* !HAVE_C99_SNPRINTF */
  158. va_end(ap);
  159. return result;
  160. }