ucl_str.c 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. /* ucl_str.c -- string functions for the the UCL library
  2. This file is part of the UCL data compression library.
  3. Copyright (C) 1996-2002 Markus Franz Xaver Johannes Oberhumer
  4. All Rights Reserved.
  5. The UCL library is free software; you can redistribute it and/or
  6. modify it under the terms of the GNU General Public License as
  7. published by the Free Software Foundation; either version 2 of
  8. the License, or (at your option) any later version.
  9. The UCL library is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. GNU General Public License for more details.
  13. You should have received a copy of the GNU General Public License
  14. along with the UCL library; see the file COPYING.
  15. If not, write to the Free Software Foundation, Inc.,
  16. 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  17. Markus F.X.J. Oberhumer
  18. <markus@oberhumer.com>
  19. http://www.oberhumer.com/opensource/ucl/
  20. */
  21. #include "ucl_conf.h"
  22. #undef ucl_memcmp
  23. #undef ucl_memcpy
  24. #undef ucl_memmove
  25. #undef ucl_memset
  26. /***********************************************************************
  27. // slow but portable <string.h> stuff, only used in assertions
  28. ************************************************************************/
  29. UCL_PUBLIC(int)
  30. ucl_memcmp(const ucl_voidp s1, const ucl_voidp s2, ucl_uint len)
  31. {
  32. #if (UCL_UINT_MAX <= SIZE_T_MAX) && defined(HAVE_MEMCMP)
  33. return memcmp(s1,s2,len);
  34. #else
  35. const ucl_byte *p1 = (const ucl_byte *) s1;
  36. const ucl_byte *p2 = (const ucl_byte *) s2;
  37. int d;
  38. if (len > 0) do
  39. {
  40. d = *p1 - *p2;
  41. if (d != 0)
  42. return d;
  43. p1++;
  44. p2++;
  45. }
  46. while (--len > 0);
  47. return 0;
  48. #endif
  49. }
  50. UCL_PUBLIC(ucl_voidp)
  51. ucl_memcpy(ucl_voidp dest, const ucl_voidp src, ucl_uint len)
  52. {
  53. #if (UCL_UINT_MAX <= SIZE_T_MAX) && defined(HAVE_MEMCPY)
  54. return memcpy(dest,src,len);
  55. #else
  56. ucl_byte *p1 = (ucl_byte *) dest;
  57. const ucl_byte *p2 = (const ucl_byte *) src;
  58. if (len <= 0 || p1 == p2)
  59. return dest;
  60. do
  61. *p1++ = *p2++;
  62. while (--len > 0);
  63. return dest;
  64. #endif
  65. }
  66. UCL_PUBLIC(ucl_voidp)
  67. ucl_memmove(ucl_voidp dest, const ucl_voidp src, ucl_uint len)
  68. {
  69. #if (UCL_UINT_MAX <= SIZE_T_MAX) && defined(HAVE_MEMMOVE)
  70. return memmove(dest,src,len);
  71. #else
  72. ucl_byte *p1 = (ucl_byte *) dest;
  73. const ucl_byte *p2 = (const ucl_byte *) src;
  74. if (len <= 0 || p1 == p2)
  75. return dest;
  76. if (p1 < p2)
  77. {
  78. do
  79. *p1++ = *p2++;
  80. while (--len > 0);
  81. }
  82. else
  83. {
  84. p1 += len;
  85. p2 += len;
  86. do
  87. *--p1 = *--p2;
  88. while (--len > 0);
  89. }
  90. return dest;
  91. #endif
  92. }
  93. UCL_PUBLIC(ucl_voidp)
  94. ucl_memset(ucl_voidp s, int c, ucl_uint len)
  95. {
  96. #if (UCL_UINT_MAX <= SIZE_T_MAX) && defined(HAVE_MEMSET)
  97. return memset(s,c,len);
  98. #else
  99. ucl_byte *p = (ucl_byte *) s;
  100. if (len > 0) do
  101. *p++ = UCL_BYTE(c);
  102. while (--len > 0);
  103. return s;
  104. #endif
  105. }
  106. /*
  107. vi:ts=4:et
  108. */