StdLib.h 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. /*
  2. * Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
  3. * All rights reserved.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions are met:
  7. *
  8. * 1. Redistributions of source code must retain the above copyright notice, this
  9. * list of conditions and the following disclaimer.
  10. *
  11. * 2. Redistributions in binary form must reproduce the above copyright notice,
  12. * this list of conditions and the following disclaimer in the documentation
  13. * and/or other materials provided with the distribution.
  14. *
  15. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  16. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  17. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  18. * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
  19. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  20. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  21. * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  22. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  23. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  24. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  25. */
  26. #pragma once
  27. #include <AK/Forward.h>
  28. #include <AK/Userspace.h>
  29. namespace Syscall {
  30. struct StringArgument;
  31. }
  32. String copy_string_from_user(const char*, size_t);
  33. extern "C" {
  34. void copy_to_user(void*, const void*, size_t);
  35. void copy_from_user(void*, const void*, size_t);
  36. void memset_user(void*, int, size_t);
  37. void* memcpy(void*, const void*, size_t);
  38. int strncmp(const char* s1, const char* s2, size_t n);
  39. char* strstr(const char* haystack, const char* needle);
  40. int strcmp(char const*, const char*);
  41. size_t strlen(const char*);
  42. size_t strnlen(const char*, size_t);
  43. void* memset(void*, int, size_t);
  44. int memcmp(const void*, const void*, size_t);
  45. void* memmove(void* dest, const void* src, size_t n);
  46. const void* memmem(const void* haystack, size_t, const void* needle, size_t);
  47. inline u16 ntohs(u16 w) { return (w & 0xff) << 8 | ((w >> 8) & 0xff); }
  48. inline u16 htons(u16 w) { return (w & 0xff) << 8 | ((w >> 8) & 0xff); }
  49. }
  50. template<typename T>
  51. inline void copy_from_user(T* dest, const T* src)
  52. {
  53. copy_from_user(dest, src, sizeof(T));
  54. }
  55. template<typename T>
  56. inline void copy_to_user(T* dest, const T* src)
  57. {
  58. copy_to_user(dest, src, sizeof(T));
  59. }
  60. template<typename T>
  61. inline void copy_from_user(T* dest, Userspace<const T*> src)
  62. {
  63. copy_from_user(dest, src.unsafe_userspace_ptr(), sizeof(T));
  64. }
  65. template<typename T>
  66. inline void copy_to_user(Userspace<T*> dest, const T* src)
  67. {
  68. copy_to_user(dest.unsafe_userspace_ptr(), src, sizeof(T));
  69. }
  70. template<typename T>
  71. inline void copy_to_user(Userspace<T*> dest, const void* src, size_t size)
  72. {
  73. copy_to_user(dest.unsafe_userspace_ptr(), src, size);
  74. }
  75. template<typename T>
  76. inline void copy_from_user(void* dest, Userspace<const T*> src, size_t size)
  77. {
  78. copy_from_user(dest, src.unsafe_userspace_ptr(), size);
  79. }