memory.c 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. /*
  2. * Copyright (C) 2007 The Android Open Source Project
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * 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. #include <cutils/memory.h>
  17. #if !HAVE_MEMSET16
  18. void android_memset16(uint16_t* dst, uint16_t value, size_t size)
  19. {
  20. size >>= 1;
  21. while (size--) {
  22. *dst++ = value;
  23. }
  24. }
  25. #endif
  26. #if !HAVE_MEMSET32
  27. void android_memset32(uint32_t* dst, uint32_t value, size_t size)
  28. {
  29. size >>= 2;
  30. while (size--) {
  31. *dst++ = value;
  32. }
  33. }
  34. #endif
  35. #if !HAVE_STRLCPY
  36. /*
  37. * Copyright (c) 1998 Todd C. Miller <Todd.Miller@courtesan.com>
  38. *
  39. * Permission to use, copy, modify, and distribute this software for any
  40. * purpose with or without fee is hereby granted, provided that the above
  41. * copyright notice and this permission notice appear in all copies.
  42. *
  43. * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
  44. * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
  45. * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
  46. * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
  47. * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
  48. * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
  49. * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  50. */
  51. #include <sys/types.h>
  52. #include <string.h>
  53. /* Implementation of strlcpy() for platforms that don't already have it. */
  54. /*
  55. * Copy src to string dst of size siz. At most siz-1 characters
  56. * will be copied. Always NUL terminates (unless siz == 0).
  57. * Returns strlen(src); if retval >= siz, truncation occurred.
  58. */
  59. size_t
  60. strlcpy(char *dst, const char *src, size_t siz)
  61. {
  62. char *d = dst;
  63. const char *s = src;
  64. size_t n = siz;
  65. /* Copy as many bytes as will fit */
  66. if (n != 0) {
  67. while (--n != 0) {
  68. if ((*d++ = *s++) == '\0')
  69. break;
  70. }
  71. }
  72. /* Not enough room in dst, add NUL and traverse rest of src */
  73. if (n == 0) {
  74. if (siz != 0)
  75. *d = '\0'; /* NUL-terminate dst */
  76. while (*s++)
  77. ;
  78. }
  79. return(s - src - 1); /* count does not include NUL */
  80. }
  81. #endif