loader.c 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. //
  2. // Dynamically loads the vorbis shared library / dll
  3. // Currently only get the pointer to the function to get the library version
  4. //
  5. #include "loader.h"
  6. typedef void (*alProc)(void);
  7. //
  8. // Windows --------------------------------------------------------------------
  9. //
  10. #ifdef _WIN32
  11. #define WIN32_LEAN_AND_MEAN 1
  12. #include <windows.h>
  13. static HMODULE libvb;
  14. static int open_libvb(void) {
  15. libvb = LoadLibraryA("libvorbis.dll");
  16. if (libvb == NULL) {
  17. return -1;
  18. }
  19. return 0;
  20. }
  21. static void close_libvb(void) {
  22. FreeLibrary(libvb);
  23. }
  24. static alProc get_proc(const char *proc) {
  25. return (alProc) GetProcAddress(libvb, proc);
  26. }
  27. //
  28. // Mac --------------------------------------------------------------------
  29. //
  30. #elif defined(__APPLE__) || defined(__APPLE_CC__)
  31. //
  32. // Linux --------------------------------------------------------------------
  33. //
  34. #else
  35. #include <dlfcn.h>
  36. static void *libvb;
  37. static char* lib_names[] = {
  38. "libvorbis.so",
  39. "libvorbis.so.0",
  40. NULL
  41. };
  42. static int open_libvb(void) {
  43. int i = 0;
  44. while (lib_names[i] != NULL) {
  45. libvb = dlopen(lib_names[i], RTLD_LAZY | RTLD_GLOBAL);
  46. if (libvb != NULL) {
  47. dlerror(); // clear errors
  48. return 0;
  49. }
  50. i++;
  51. }
  52. return -1;
  53. }
  54. static void close_libvb(void) {
  55. dlclose(libvb);
  56. }
  57. static alProc get_proc(const char *proc) {
  58. return dlsym(libvb, proc);
  59. }
  60. #endif
  61. // Prototypes of local functions
  62. static void load_procs(void);
  63. // Pointers to functions loaded from shared library
  64. LPVORBISVERSIONSTRING p_vorbis_version_string;
  65. // Load functions from shared library
  66. int vorbis_load() {
  67. int res = open_libvb();
  68. if (res) {
  69. return res;
  70. }
  71. load_procs();
  72. return 0;
  73. }
  74. static void load_procs(void) {
  75. p_vorbis_version_string = (LPVORBISVERSIONSTRING)get_proc("vorbis_version_string");
  76. }
  77. //
  78. // Go code cannot directly call the vorbis file function pointers loaded dynamically
  79. // The following C functions call the corresponding function pointers and can be
  80. // called by Go code.
  81. //
  82. const char *vorbis_version_string(void) {
  83. return p_vorbis_version_string();
  84. }