1
0

plstring.cpp 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. #include "plstring.h"
  2. #include "api__playlist.h"
  3. #include <shlwapi.h>
  4. #include <stdint.h>
  5. static wchar_t *_plstring_wcsdup( const wchar_t *str )
  6. {
  7. if ( !str )
  8. return 0;
  9. size_t len = wcslen( str );
  10. size_t *self = (size_t *)calloc( ( len + 1 ) * sizeof( wchar_t ), sizeof( size_t ) );
  11. *self = 1;
  12. wchar_t *new_str = (wchar_t *)( ( (int8_t *)self ) + sizeof( size_t ) );
  13. memcpy( new_str, str, ( len + 1 ) * sizeof( wchar_t ) );
  14. return new_str;
  15. }
  16. static wchar_t *_plstring_malloc( size_t str_size )
  17. {
  18. size_t *self = (size_t *)calloc( ( str_size ), sizeof( size_t ) );
  19. *self = 1;
  20. wchar_t *new_str = (wchar_t *)( ( (int8_t *)self ) + sizeof( size_t ) );
  21. return new_str;
  22. }
  23. static void _plstring_release( wchar_t *str )
  24. {
  25. if ( str )
  26. {
  27. size_t *self = (size_t *)( ( (int8_t *)str ) - sizeof( size_t ) );
  28. ( *self )--;
  29. if ( *self == 0 )
  30. free( self );
  31. }
  32. }
  33. static void _plstring_retain( wchar_t *str )
  34. {
  35. if ( str )
  36. {
  37. size_t *self = (size_t *)( ( (int8_t *)str ) - sizeof( size_t ) );
  38. ( *self )++;
  39. }
  40. }
  41. wchar_t *( *plstring_wcsdup )( const wchar_t *str ) = _plstring_wcsdup;
  42. wchar_t *( *plstring_malloc )( size_t str_size ) = _plstring_malloc;
  43. void ( *plstring_release )( wchar_t *str ) = _plstring_release;
  44. void ( *plstring_retain )( wchar_t *str ) = _plstring_retain;
  45. static bool ndestring_tried_load = false;
  46. void plstring_init()
  47. {
  48. if ( !ndestring_tried_load )
  49. {
  50. wchar_t path[ MAX_PATH ] = { 0 };
  51. const wchar_t *PROGDIR = WASABI_API_APP->path_getAppPath();
  52. PathCombineW( path, PROGDIR, L"winamp.exe" );
  53. HMODULE ndelib = LoadLibraryW( path );
  54. if ( ndelib )
  55. {
  56. FARPROC ndestring_wcsdup = GetProcAddress( ndelib, "plstring_wcsdup" );
  57. FARPROC ndestring_malloc = GetProcAddress( ndelib, "plstring_malloc" );
  58. FARPROC ndestring_release = GetProcAddress( ndelib, "plstring_release" );
  59. FARPROC ndestring_retain = GetProcAddress( ndelib, "plstring_retain" );
  60. if ( ndestring_wcsdup && ndestring_malloc && ndestring_release && ndestring_retain )
  61. {
  62. *(FARPROC *)&plstring_wcsdup = *(FARPROC *)ndestring_wcsdup;
  63. *(FARPROC *)&plstring_malloc = *(FARPROC *)ndestring_malloc;
  64. *(FARPROC *)&plstring_release = *(FARPROC *)ndestring_release;
  65. *(FARPROC *)&plstring_retain = *(FARPROC *)ndestring_retain;
  66. }
  67. }
  68. ndestring_tried_load = true;
  69. }
  70. }