Advertisement
Guest User

Untitled

a guest
Jul 17th, 2019
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.98 KB | None | 0 0
  1. /*
  2. * MIT License
  3. *
  4. * Copyright (c) 2019 James Fenn <me@jfenn.me>
  5. *
  6. * Permission is hereby granted, free of charge, to any person obtaining a copy
  7. * of this software and associated documentation files (the "Software"), to deal
  8. * in the Software without restriction, including without limitation the rights
  9. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  10. * copies of the Software, and to permit persons to whom the Software is
  11. * furnished to do so, subject to the following conditions:
  12. *
  13. * The above copyright notice and this permission notice shall be included in all
  14. * copies or substantial portions of the Software.
  15. *
  16. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  17. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  18. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  19. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  20. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  21. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  22. * SOFTWARE.
  23. */
  24.  
  25. #include <string.h>
  26. #include <dlfcn.h>
  27. #include <stdarg.h>
  28.  
  29. /**
  30. * Link a method pointer to a specified function in a dynamic
  31. * library.
  32. *
  33. * @param dlib The dynamic library to be linked, created
  34. * with dlopen().
  35. * @param method_ptr A function pointer to load the method into.
  36. * @param method_name The (string) name of the method to link.
  37. * @return 1 if linking was successful; 0 if not.
  38. */
  39. char dlib_link(void* dlib, void* method_ptr, char* method_name) {
  40. void (*method)() = dlsym(dlib, method_name); // load the method
  41. if (method == NULL) { // fail if null
  42. // Error: couldn't load method '%s' from shared library
  43. return 0;
  44. }
  45.  
  46. // copy method pointer to the location of the pointer passed in the array
  47. memcpy((void*) method_ptr, &method, sizeof(void*));
  48. return 1;
  49. }
  50.  
  51. /**
  52. * Load the specified dynamic library and assign its
  53. * function pointers to the ones provided.
  54. *
  55. * @param name The name of the shared library.
  56. * @param count The number of methods to load.
  57. * @param ... Variadic arguments alternating between
  58. * function pointers to write to and the
  59. * method names to load into them.
  60. * @return 1 if linking was successful; 0 if not.
  61. */
  62. char dlib_import(char* name, unsigned int count, ...) {
  63. va_list methods;
  64. va_start(methods, count);
  65.  
  66. void* lib = dlopen(name, RTLD_NOW); // open the library
  67. if (lib == NULL) { // fail if null
  68. // "Error: couldn't open shared library
  69. return 0;
  70. }
  71.  
  72. for (unsigned int i = 0; i < count; i++) {
  73. char status = dlib_link(
  74. lib,
  75. (void*) va_arg(methods, char*),
  76. va_arg(methods, char*)
  77. );
  78.  
  79. if (status == 0) {
  80. // Error: couldn't load specific method.
  81. return 0;
  82. }
  83. }
  84.  
  85. va_end(methods);
  86.  
  87. return 1;
  88. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement