PageRenderTime 87ms CodeModel.GetById 30ms RepoModel.GetById 1ms app.codeStats 0ms

/sgp/SGPStrings.cc

https://bitbucket.org/gennady/ja2-stracciatella
C++ | 152 lines | 69 code | 24 blank | 59 comment | 27 complexity | 15d1cae12d91881d79db238d6d187c31 MD5 | raw file
Possible License(s): BSD-3-Clause, LGPL-2.1
  1. #include "SGPStrings.h"
  2. #if defined(__linux__) || defined(_WIN32)
  3. #include <sys/types.h>
  4. /* $OpenBSD: wcslcpy.c,v 1.4 2006/05/05 15:27:38 millert Exp $ */
  5. /* $NetBSD: wcslcpy.c,v 1.2 2001/01/03 14:33:02 lukem Exp $ */
  6. /* from OpenBSD: strlcpy.c,v 1.4 1999/05/01 18:56:41 millert Exp */
  7. /*
  8. * Copyright (c) 1998 Todd C. Miller <Todd.Miller@courtesan.com>
  9. * All rights reserved.
  10. *
  11. * Redistribution and use in source and binary forms, with or without
  12. * modification, are permitted provided that the following conditions
  13. * are met:
  14. * 1. Redistributions of source code must retain the above copyright
  15. * notice, this list of conditions and the following disclaimer.
  16. * 2. Redistributions in binary form must reproduce the above copyright
  17. * notice, this list of conditions and the following disclaimer in the
  18. * documentation and/or other materials provided with the distribution.
  19. * 3. The name of the author may not be used to endorse or promote products
  20. * derived from this software without specific prior written permission.
  21. *
  22. * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
  23. * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
  24. * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
  25. * THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  26. * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  27. * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
  28. * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
  29. * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
  30. * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
  31. * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  32. */
  33. /*
  34. * Copy src to string dst of size siz. At most siz-1 characters
  35. * will be copied. Always NUL terminates (unless siz == 0).
  36. * Returns wcslen(src); if retval >= siz, truncation occurred.
  37. */
  38. size_t
  39. wcslcpy(wchar_t *dst, const wchar_t *src, size_t siz)
  40. {
  41. wchar_t *d = dst;
  42. const wchar_t *s = src;
  43. size_t n = siz;
  44. /* Copy as many bytes as will fit */
  45. if (n != 0) {
  46. while (--n != 0) {
  47. if ((*d++ = *s++) == '\0')
  48. break;
  49. }
  50. }
  51. /* Not enough room in dst, add NUL and traverse rest of src */
  52. if (n == 0) {
  53. if (siz != 0)
  54. *d = '\0'; /* NUL-terminate dst */
  55. while (*s++)
  56. ;
  57. }
  58. return(s - src - 1); /* count does not include NUL */
  59. }
  60. /* $OpenBSD: strlcpy.c,v 1.11 2006/05/05 15:27:38 millert Exp $ */
  61. /*
  62. * Copyright (c) 1998 Todd C. Miller <Todd.Miller@courtesan.com>
  63. *
  64. * Permission to use, copy, modify, and distribute this software for any
  65. * purpose with or without fee is hereby granted, provided that the above
  66. * copyright notice and this permission notice appear in all copies.
  67. *
  68. * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
  69. * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
  70. * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
  71. * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
  72. * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
  73. * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
  74. * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  75. */
  76. /*
  77. * Copy src to string dst of size siz. At most siz-1 characters
  78. * will be copied. Always NUL terminates (unless siz == 0).
  79. * Returns strlen(src); if retval >= siz, truncation occurred.
  80. */
  81. size_t
  82. strlcpy(char *dst, const char *src, size_t siz)
  83. {
  84. char *d = dst;
  85. const char *s = src;
  86. size_t n = siz;
  87. /* Copy as many bytes as will fit */
  88. if (n != 0) {
  89. while (--n != 0) {
  90. if ((*d++ = *s++) == '\0')
  91. break;
  92. }
  93. }
  94. /* Not enough room in dst, add NUL and traverse rest of src */
  95. if (n == 0) {
  96. if (siz != 0)
  97. *d = '\0'; /* NUL-terminate dst */
  98. while (*s++)
  99. ;
  100. }
  101. return(s - src - 1); /* count does not include NUL */
  102. }
  103. #endif
  104. #ifdef _WIN32
  105. int WINsnprintf(char* const s, size_t const n, const char* const fmt, ...)
  106. {
  107. va_list arg;
  108. va_start(arg, fmt);
  109. int const ret = _vsnprintf(s, n, fmt, arg);
  110. va_end(arg);
  111. if (n != 0) s[n - 1] = '\0'; // _vsnprintf() does not guarantee NUL termination
  112. return ret;
  113. }
  114. int WINswprintf(wchar_t* const s, size_t const n, const wchar_t* const fmt, ...)
  115. {
  116. va_list arg;
  117. va_start(arg, fmt);
  118. int const ret = WINvswprintf(s, n, fmt, arg);
  119. va_end(arg);
  120. return ret;
  121. }
  122. int WINvswprintf(wchar_t* const s, size_t const n, const wchar_t* const fmt, va_list const arg)
  123. {
  124. int const ret = _vsnwprintf(s, n, fmt, arg);
  125. if (n != 0) s[n - 1] = L'\0'; // _vsnwprintf() does not guarantee NUL termination
  126. return ret;
  127. }
  128. #endif