From f7dc6beda191dcf7d919cb218e40dca3ccde46cb Mon Sep 17 00:00:00 2001 From: Christopher Snowhill Date: Tue, 24 May 2022 01:05:43 -0700 Subject: [PATCH] Plugin utilities: Moved encoding guesser to header Moved the string encoding guesser/converter to the Plugin.h header, so it may be accessible from any plugin. I may make it a global member of something eventually, but a static inline for such a simple function should be fine for now. This function facilitates converting arbitrary 8 bit encoded strings to Unicode NSString objects. It should be used anywhere that UTF-8 is expected, but not necessarily guaranteed, and where other 8-bit encodings may also be supplied by a user's files. Not using this setup for string inputs has already led to failed UTF-8 decoding resulting in nil NSStrings being passed to the inline array or dictionary initializers, which results in crashes due to uncaught exceptions. Signed-off-by: Christopher Snowhill --- Audio/Plugin.h | 7 +++++++ Plugins/HTTPSource/HTTPSource.m | 7 ------- 2 files changed, 7 insertions(+), 7 deletions(-) diff --git a/Audio/Plugin.h b/Audio/Plugin.h index 0db7ec560..c29ac3a90 100644 --- a/Audio/Plugin.h +++ b/Audio/Plugin.h @@ -98,3 +98,10 @@ - (int)putMetadataInURL:(NSURL *)url; @end + +static NSString *guess_encoding_of_string(const char *input) { + NSString *ret = @""; + NSData *stringData = [NSData dataWithBytes:input length:strlen(input)]; + [NSString stringEncodingForData:stringData encodingOptions:nil convertedString:&ret usedLossyConversion:nil]; + return ret; +} diff --git a/Plugins/HTTPSource/HTTPSource.m b/Plugins/HTTPSource/HTTPSource.m index 87b56384a..850ea9b54 100644 --- a/Plugins/HTTPSource/HTTPSource.m +++ b/Plugins/HTTPSource/HTTPSource.m @@ -59,13 +59,6 @@ static size_t http_curl_write_wrapper(HTTPSource *fp, void *ptr, size_t size) { return size - avail; } -static NSString *guess_encoding_of_string(const char *input) { - NSString *ret = @""; - NSData *stringData = [NSData dataWithBytes:input length:strlen(input)]; - [NSString stringEncodingForData:stringData encodingOptions:nil convertedString:&ret usedLossyConversion:nil]; - return ret; -} - static int http_parse_shoutcast_meta(HTTPSource *fp, const char *meta, size_t size) { // DLog (@"reading %d bytes of metadata\n", size); DLog(@"%s", meta);