mirror of
https://github.com/WinampDesktop/winamp.git
synced 2025-06-17 16:45:47 -04:00
Initial community commit
This commit is contained in:
170
Src/replicant/nsid3v2/windows/frame_apic.cpp
Normal file
170
Src/replicant/nsid3v2/windows/frame_apic.cpp
Normal file
@ -0,0 +1,170 @@
|
||||
#include "nsid3v2.h"
|
||||
#include "nsid3v2/header.h"
|
||||
#include "nsid3v2/tag.h"
|
||||
#include "nsid3v2/frame_utils.h"
|
||||
#include <api/memmgr/api_memmgr.h>
|
||||
#include <strsafe.h>
|
||||
|
||||
struct ParsedPicture
|
||||
{
|
||||
uint8_t encoding; // 0 - iso-8859-1, 1 - UTF16LE, 2 - UTF16BE, 3 - UTF8
|
||||
const char *mime_type;
|
||||
size_t mime_cch;
|
||||
uint8_t picture_type;
|
||||
union
|
||||
{
|
||||
const char *as8;
|
||||
const wchar_t *as16;
|
||||
} description_data;
|
||||
size_t description_cch;
|
||||
const void *picture_data;
|
||||
size_t picture_bytes;
|
||||
};
|
||||
|
||||
static int ParsePicture(const void *data, size_t data_len, ParsedPicture &parsed)
|
||||
{
|
||||
const uint8_t *data8 = (const uint8_t *)data;
|
||||
parsed.encoding = data8[0];
|
||||
parsed.mime_type = (const char *)&data8[1];
|
||||
data_len--;
|
||||
ParseDescription(parsed.mime_type, data_len, parsed.mime_cch);
|
||||
parsed.picture_type = data8[2+parsed.mime_cch];
|
||||
data_len--;
|
||||
|
||||
switch(parsed.encoding)
|
||||
{
|
||||
case 0: // ISO-8859-1
|
||||
ParseDescription(parsed.description_data.as8, parsed.description_cch, data_len);
|
||||
parsed.picture_data = parsed.description_data.as8 + parsed.description_cch + 1;
|
||||
parsed.picture_bytes = data_len;
|
||||
return NErr_Success;
|
||||
case 1: // UTF-16
|
||||
ParseDescription(parsed.description_data.as16, parsed.description_cch, data_len, parsed.encoding);
|
||||
parsed.picture_data = parsed.description_data.as8 + parsed.description_cch + 1;
|
||||
parsed.picture_bytes = data_len;
|
||||
return NErr_Success;
|
||||
|
||||
case 2: // UTF-16 BE
|
||||
ParseDescription(parsed.description_data.as16, parsed.description_cch, data_len, parsed.encoding);
|
||||
parsed.picture_data = parsed.description_data.as8 + parsed.description_cch + 1;
|
||||
parsed.picture_bytes = data_len;
|
||||
return NErr_Success;
|
||||
case 3: // UTF-8
|
||||
ParseDescription(parsed.description_data.as8, parsed.description_cch, data_len);
|
||||
parsed.picture_data = parsed.description_data.as8 + parsed.description_cch + 1;
|
||||
parsed.picture_bytes = data_len;
|
||||
return NErr_Success;
|
||||
}
|
||||
return NErr_NotImplemented;
|
||||
}
|
||||
|
||||
int NSID3v2_Tag_APIC_GetPicture(const nsid3v2_tag_t t, uint8_t picture_type, void *_memmgr, wchar_t **mime_type, void **picture_data, size_t *picture_bytes)
|
||||
{
|
||||
api_memmgr *memmgr = (api_memmgr *)_memmgr;
|
||||
const ID3v2::Tag *tag = (const ID3v2::Tag *)t;
|
||||
const ID3v2::Frame *frame = tag->FindFirstFrame(NSID3V2_FRAME_PICTURE);
|
||||
while (frame)
|
||||
{
|
||||
const void *data;
|
||||
size_t data_len;
|
||||
ParsedPicture parsed;
|
||||
if (frame->GetData(&data, &data_len) == NErr_Success && data_len > 0 && ParsePicture(data, data_len, parsed) == NErr_Success && parsed.picture_type == picture_type)
|
||||
{
|
||||
const char *type = strchr(parsed.mime_type, '/');
|
||||
|
||||
if (type && *type)
|
||||
{
|
||||
type++;
|
||||
int typelen = MultiByteToWideChar(CP_ACP, 0, type, -1, 0, 0);
|
||||
*mime_type = (wchar_t *)memmgr->sysMalloc(typelen * sizeof(wchar_t));
|
||||
MultiByteToWideChar(CP_ACP, 0, type, -1, *mime_type, typelen);
|
||||
}
|
||||
else
|
||||
*mime_type = 0; // unknown!
|
||||
|
||||
*picture_bytes = parsed.picture_bytes;
|
||||
*picture_data = memmgr->sysMalloc(parsed.picture_bytes);
|
||||
memcpy(*picture_data, parsed.picture_data, parsed.picture_bytes);
|
||||
return NErr_Success;
|
||||
}
|
||||
frame = tag->FindNextFrame(frame);
|
||||
}
|
||||
|
||||
return NErr_Error;
|
||||
}
|
||||
|
||||
|
||||
int NSID3v2_Tag_APIC_GetFirstPicture(const nsid3v2_tag_t t, void *_memmgr, wchar_t **mime_type, void **picture_data, size_t *picture_bytes)
|
||||
{
|
||||
api_memmgr *memmgr = (api_memmgr *)_memmgr;
|
||||
const ID3v2::Tag *tag = (const ID3v2::Tag *)t;
|
||||
const ID3v2::Frame *frame = tag->FindFirstFrame(NSID3V2_FRAME_PICTURE);
|
||||
while (frame)
|
||||
{
|
||||
const void *data;
|
||||
size_t data_len;
|
||||
ParsedPicture parsed;
|
||||
if (frame->GetData(&data, &data_len) == NErr_Success && data_len > 0 && ParsePicture(data, data_len, parsed) == NErr_Success)
|
||||
{
|
||||
const char *type = strchr(parsed.mime_type, '/');
|
||||
|
||||
if (type && *type)
|
||||
{
|
||||
type++;
|
||||
int typelen = MultiByteToWideChar(CP_ACP, 0, type, -1, 0, 0);
|
||||
*mime_type = (wchar_t *)memmgr->sysMalloc(typelen * sizeof(wchar_t));
|
||||
MultiByteToWideChar(CP_ACP, 0, type, -1, *mime_type, typelen);
|
||||
}
|
||||
else
|
||||
*mime_type = 0; // unknown!
|
||||
|
||||
*picture_bytes = parsed.picture_bytes;
|
||||
*picture_data = memmgr->sysMalloc(parsed.picture_bytes);
|
||||
memcpy(*picture_data, parsed.picture_data, parsed.picture_bytes);
|
||||
return NErr_Success;
|
||||
}
|
||||
frame = tag->FindNextFrame(frame);
|
||||
}
|
||||
|
||||
return NErr_Error;
|
||||
}
|
||||
|
||||
int NSID3v2_Tag_APIC_GetFrame(const nsid3v2_tag_t t, uint8_t picture_type, nsid3v2_frame_t *f)
|
||||
{
|
||||
const ID3v2::Tag *tag = (const ID3v2::Tag *)t;
|
||||
const ID3v2::Frame *frame = tag->FindFirstFrame(NSID3V2_FRAME_PICTURE);
|
||||
while (frame)
|
||||
{
|
||||
const void *data;
|
||||
size_t data_len;
|
||||
ParsedPicture parsed;
|
||||
if (frame->GetData(&data, &data_len) == NErr_Success && data_len > 0 && ParsePicture(data, data_len, parsed) == NErr_Success && parsed.picture_type == picture_type)
|
||||
{
|
||||
*f = (nsid3v2_frame_t)frame;
|
||||
return NErr_Success;
|
||||
}
|
||||
frame = tag->FindNextFrame(frame);
|
||||
}
|
||||
|
||||
return NErr_Error;
|
||||
}
|
||||
|
||||
int NSID3v2_Tag_APIC_GetFirstFrame(const nsid3v2_tag_t t, nsid3v2_frame_t *f)
|
||||
{
|
||||
const ID3v2::Tag *tag = (const ID3v2::Tag *)t;
|
||||
const ID3v2::Frame *frame = tag->FindFirstFrame(NSID3V2_FRAME_PICTURE);
|
||||
while (frame)
|
||||
{
|
||||
const void *data;
|
||||
size_t data_len;
|
||||
ParsedPicture parsed;
|
||||
if (frame->GetData(&data, &data_len) == NErr_Success && data_len > 0 && ParsePicture(data, data_len, parsed) == NErr_Success)
|
||||
{
|
||||
*f = (nsid3v2_frame_t)frame;
|
||||
return NErr_Success;
|
||||
}
|
||||
frame = tag->FindNextFrame(frame);
|
||||
}
|
||||
|
||||
return NErr_Error;
|
||||
}
|
228
Src/replicant/nsid3v2/windows/frame_comments.cpp
Normal file
228
Src/replicant/nsid3v2/windows/frame_comments.cpp
Normal file
@ -0,0 +1,228 @@
|
||||
#include "nsid3v2/nsid3v2.h"
|
||||
#include "nsid3v2/header.h"
|
||||
#include "nsid3v2/tag.h"
|
||||
#include "nsid3v2/frame_utils.h"
|
||||
#include "nu/AutoWide.h"
|
||||
#include <strsafe.h>
|
||||
|
||||
struct ParsedComments
|
||||
{
|
||||
const char *language;
|
||||
uint8_t description_encoding; // 0 - iso-8859-1, 1 - UTF16LE, 2 - UTF16BE, 3 - UTF8
|
||||
union
|
||||
{
|
||||
const char *as8;
|
||||
const wchar_t *as16;
|
||||
} description_data;
|
||||
size_t description_cch;
|
||||
uint8_t value_encoding; // 0 - iso-8859-1, 1 - UTF16LE, 2 - UTF16BE, 3 - UTF8
|
||||
union
|
||||
{
|
||||
const char *as8;
|
||||
const wchar_t *as16;
|
||||
} value_data;
|
||||
size_t value_cch;
|
||||
};
|
||||
|
||||
static int ParseComments(const void *data, size_t data_len, ParsedComments &parsed)
|
||||
{
|
||||
int ret;
|
||||
if (data_len < 5)
|
||||
return NErr_Error;
|
||||
|
||||
const uint8_t *data8 = (const uint8_t *)data;
|
||||
switch(data8[0])
|
||||
{
|
||||
case 0: // ISO-8859-1
|
||||
parsed.description_encoding = 0;
|
||||
parsed.language = (const char *)&data8[1];
|
||||
|
||||
parsed.value_encoding = 0;
|
||||
parsed.description_data.as8 = (const char *)&data8[4];
|
||||
data_len-=4;
|
||||
|
||||
ret = ParseDescription(parsed.description_data.as8, data_len, parsed.description_cch);
|
||||
if (ret != NErr_Success)
|
||||
return ret;
|
||||
|
||||
parsed.value_data.as8 = parsed.description_data.as8 + 2 + parsed.description_cch;
|
||||
parsed.value_cch = data_len;
|
||||
|
||||
return NErr_Success;
|
||||
case 1: // UTF-16
|
||||
parsed.language = (const char *)&data8[1];
|
||||
parsed.description_encoding=1;
|
||||
parsed.description_data.as16 = (const wchar_t *)&data8[4];
|
||||
data_len-=4;
|
||||
|
||||
ret = ParseDescription(parsed.description_data.as16, data_len, parsed.description_cch, parsed.description_encoding);
|
||||
if (ret != NErr_Success)
|
||||
return ret;
|
||||
|
||||
parsed.value_data.as16 = parsed.description_data.as16 + 2 + parsed.description_cch;
|
||||
parsed.value_cch = data_len/2;
|
||||
|
||||
if (parsed.value_cch && parsed.value_data.as16[0] == 0xFFFE)
|
||||
{
|
||||
parsed.value_encoding=2;
|
||||
parsed.value_data.as16++;
|
||||
parsed.value_cch--;
|
||||
}
|
||||
else if (parsed.value_cch && parsed.value_data.as16[0] == 0xFEFF)
|
||||
{
|
||||
parsed.value_encoding=1;
|
||||
parsed.value_data.as16++;
|
||||
parsed.value_cch--;
|
||||
}
|
||||
else
|
||||
{
|
||||
parsed.value_encoding=1;
|
||||
}
|
||||
|
||||
return NErr_Success;
|
||||
|
||||
case 2: // UTF-16 BE
|
||||
parsed.language = (const char *)&data8[1];
|
||||
parsed.description_encoding=2;
|
||||
parsed.description_data.as16 = (const wchar_t *)&data8[4];
|
||||
data_len-=3;
|
||||
|
||||
ret = ParseDescription(parsed.description_data.as16, data_len, parsed.description_cch, parsed.description_encoding);
|
||||
if (ret != NErr_Success)
|
||||
return ret;
|
||||
|
||||
parsed.value_data.as16 = parsed.description_data.as16 + 2 + parsed.description_cch;
|
||||
parsed.value_cch = data_len/2;
|
||||
parsed.value_encoding=2;
|
||||
|
||||
return NErr_Success;
|
||||
case 3: // UTF-8
|
||||
parsed.description_encoding = 3;
|
||||
parsed.language = (const char *)&data8[1];
|
||||
parsed.value_encoding = 3;
|
||||
parsed.description_data.as8 = (const char *)&data8[4];
|
||||
data_len-=4;
|
||||
|
||||
ret = ParseDescription(parsed.description_data.as8, data_len, parsed.description_cch);
|
||||
if (ret != NErr_Success)
|
||||
return ret;
|
||||
|
||||
// check for UTF-8 BOM
|
||||
if (parsed.description_cch >= 3 && parsed.description_data.as8[0] == 0xEF && parsed.description_data.as8[1] == 0xBB && parsed.description_data.as8[2] == 0xBF)
|
||||
{
|
||||
parsed.description_data.as8+=3;
|
||||
parsed.description_cch-=3;
|
||||
}
|
||||
|
||||
if (!data_len)
|
||||
return NErr_Error;
|
||||
|
||||
parsed.value_data.as8 = parsed.description_data.as8 + 2 + parsed.description_cch;
|
||||
parsed.value_cch = data_len;
|
||||
|
||||
// check for UTF-8 BOM
|
||||
if (parsed.value_cch >= 3 && parsed.value_data.as8[0] == 0xEF && parsed.value_data.as8[1] == 0xBB && parsed.value_data.as8[2] == 0xBF)
|
||||
{
|
||||
parsed.value_data.as8+=3;
|
||||
parsed.value_cch-=3;
|
||||
}
|
||||
|
||||
return NErr_Success;
|
||||
}
|
||||
return NErr_NotImplemented;
|
||||
}
|
||||
|
||||
|
||||
int NSID3v2_Tag_Comments_GetUTF16(const nsid3v2_tag_t t, const wchar_t *description, wchar_t *buf, size_t buf_cch, int text_flags)
|
||||
{
|
||||
const ID3v2::Tag *tag = (const ID3v2::Tag *)t;
|
||||
const ID3v2::Frame *frame = tag->FindFirstFrame(NSID3V2_FRAME_COMMENTS);
|
||||
while (frame)
|
||||
{
|
||||
const void *data;
|
||||
size_t data_len;
|
||||
ParsedComments parsed;
|
||||
if (frame->GetData(&data, &data_len) == NErr_Success && data_len > 0 && ParseComments(data, data_len, parsed) == NErr_Success)
|
||||
{
|
||||
// see if our description matches
|
||||
switch(parsed.description_encoding)
|
||||
{
|
||||
case 0: // ISO-8859-1
|
||||
{
|
||||
UINT codepage = (text_flags & NSID3V2_TEXT_SYSTEM)?28591:CP_ACP;
|
||||
if (CompareStringW(MAKELCID(MAKELANGID(LANG_ENGLISH, SUBLANG_ENGLISH_US), SORT_DEFAULT), NORM_IGNORECASE, AutoWide(parsed.description_data.as8, codepage), -1, description, -1) != CSTR_EQUAL)
|
||||
goto next_frame;
|
||||
}
|
||||
break;
|
||||
case 1:
|
||||
if (CompareStringW(MAKELCID(MAKELANGID(LANG_ENGLISH, SUBLANG_ENGLISH_US), SORT_DEFAULT), NORM_IGNORECASE, parsed.description_data.as16, -1, description, -1) != CSTR_EQUAL)
|
||||
goto next_frame;
|
||||
break;
|
||||
case 2:
|
||||
// TODO!
|
||||
goto next_frame;
|
||||
break;
|
||||
case 3:
|
||||
if (CompareStringW(MAKELCID(MAKELANGID(LANG_ENGLISH, SUBLANG_ENGLISH_US), SORT_DEFAULT), NORM_IGNORECASE, AutoWide(parsed.description_data.as8, CP_UTF8), -1, description, -1) != CSTR_EQUAL)
|
||||
goto next_frame;
|
||||
break;
|
||||
}
|
||||
|
||||
switch(parsed.value_encoding)
|
||||
{
|
||||
case 0: // ISO-8859-1
|
||||
{
|
||||
UINT codepage = (text_flags & NSID3V2_TEXT_SYSTEM)?28591:CP_ACP;
|
||||
int utf16_len = MultiByteToWideChar(codepage, 0, parsed.value_data.as8, parsed.value_cch, 0, 0);
|
||||
|
||||
if (utf16_len)
|
||||
{
|
||||
utf16_len = MultiByteToWideChar(codepage, 0, parsed.value_data.as8, parsed.value_cch, buf, utf16_len-1);
|
||||
buf[utf16_len]=0;
|
||||
}
|
||||
else
|
||||
{
|
||||
buf[0]=0;
|
||||
}
|
||||
}
|
||||
return NErr_Success;
|
||||
case 1: // UTF-16
|
||||
StringCchCopyNW(buf, buf_cch, parsed.value_data.as16, parsed.value_cch);
|
||||
return NErr_Success;
|
||||
case 2: // UTF-16BE
|
||||
{
|
||||
size_t toCopy = buf_cch-1;
|
||||
if (parsed.value_cch < toCopy)
|
||||
toCopy = parsed.value_cch;
|
||||
for (size_t i=0;i<toCopy;i++)
|
||||
{
|
||||
buf[i] = ((parsed.value_data.as16[i] >> 8) & 0xFF) | (((parsed.value_data.as16[i]) & 0xFF) << 8);
|
||||
}
|
||||
buf[toCopy]=0;
|
||||
}
|
||||
return NErr_Success;
|
||||
case 3: // UTF-8
|
||||
{
|
||||
int utf16_len = MultiByteToWideChar(CP_UTF8, 0, parsed.value_data.as8, parsed.value_cch, 0, 0);
|
||||
|
||||
if (utf16_len)
|
||||
{
|
||||
utf16_len = MultiByteToWideChar(CP_UTF8, 0, parsed.value_data.as8, parsed.value_cch, buf, utf16_len-1);
|
||||
buf[utf16_len]=0;
|
||||
}
|
||||
else
|
||||
{
|
||||
buf[0]=0;
|
||||
}
|
||||
}
|
||||
return NErr_Success;
|
||||
}
|
||||
|
||||
next_frame:
|
||||
frame = tag->FindNextFrame(frame);
|
||||
}
|
||||
}
|
||||
|
||||
return NErr_Error;
|
||||
}
|
||||
|
73
Src/replicant/nsid3v2/windows/nsid3v2.cpp
Normal file
73
Src/replicant/nsid3v2/windows/nsid3v2.cpp
Normal file
@ -0,0 +1,73 @@
|
||||
#include "nsid3v2/nsid3v2.h"
|
||||
#include "nsid3v2/header.h"
|
||||
#include "nsid3v2/tag.h"
|
||||
#include <new>
|
||||
#include <strsafe.h>
|
||||
|
||||
|
||||
/*
|
||||
================== Tag ==================
|
||||
= =
|
||||
=========================================
|
||||
*/
|
||||
#if 0 // save for reference
|
||||
int NSID3v2_Frame_Text_SetUTF16(nsid3v2_frame_t f, const wchar_t *value)
|
||||
{
|
||||
ID3v2::Frame *frame = (ID3v2::Frame *)f;
|
||||
size_t len = wcslen(value);
|
||||
size_t bytes = len * 2 + 1; // leave 1 byte for encoding
|
||||
if (bytes < len) // woops, integer overflow
|
||||
return NErr_Error;
|
||||
|
||||
size_t datalen;
|
||||
void *data;
|
||||
int ret = frame->NewData(bytes, &data, &datalen);
|
||||
if (ret == NErr_Success)
|
||||
{
|
||||
uint8_t *data8 = (uint8_t *)data;
|
||||
data8[0]=1; // set encoding to UTF-16
|
||||
memcpy(data8+1, value, len*2);
|
||||
}
|
||||
return ret;
|
||||
}
|
||||
|
||||
int NSID3v2_Frame_UserText_SetUTF16(nsid3v2_frame_t f, const wchar_t *description, const wchar_t *value)
|
||||
{
|
||||
ID3v2::Frame *frame = (ID3v2::Frame *)f;
|
||||
size_t value_len = wcslen(value);
|
||||
size_t description_len = wcslen(description);
|
||||
size_t bytes = (value_len + description_len + 1) * 2 + 1; // leave 1 byte for encoding
|
||||
|
||||
size_t datalen;
|
||||
void *data;
|
||||
int ret = frame->NewData(bytes, &data, &datalen);
|
||||
if (ret == NErr_Success)
|
||||
{
|
||||
uint8_t *data8 = (uint8_t *)data;
|
||||
data8[0]=1; // set encoding to UTF-16
|
||||
wcscpy((wchar_t *)(data8+1), description); // guaranteed to be room
|
||||
memcpy(data8+1+1+description_len*2, value, value_len*2);
|
||||
}
|
||||
return ret;
|
||||
}
|
||||
|
||||
int NSID3v2_Frame_UserText_SetLatin(nsid3v2_frame_t f, const char *description, const char *value)
|
||||
{
|
||||
ID3v2::Frame *frame = (ID3v2::Frame *)f;
|
||||
size_t value_len = strlen(value);
|
||||
size_t description_len = strlen(description);
|
||||
size_t bytes = (value_len + description_len + 1) + 1; // leave 1 byte for encoding
|
||||
|
||||
size_t datalen;
|
||||
void *data;
|
||||
int ret = frame->NewData(bytes, &data, &datalen);
|
||||
if (ret == NErr_Success)
|
||||
{
|
||||
uint8_t *data8 = (uint8_t *)data;
|
||||
data8[0]=0; // set encoding to ISO-8859-1
|
||||
strcpy((char *)(data8+1), description); // guaranteed to be room
|
||||
memcpy(data8+1+1+description_len, value, value_len);
|
||||
}
|
||||
return ret;
|
||||
}
|
||||
#endif
|
20
Src/replicant/nsid3v2/windows/nsid3v2.h
Normal file
20
Src/replicant/nsid3v2/windows/nsid3v2.h
Normal file
@ -0,0 +1,20 @@
|
||||
#pragma once
|
||||
|
||||
#include "foundation/types.h"
|
||||
#include "foundation/export.h"
|
||||
#include "foundation/error.h"
|
||||
#include "nx/nxstring.h"
|
||||
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
#define NSID3V2_EXPORT
|
||||
typedef struct nsid3v2_header_struct_t { } *nsid3v2_header_t;
|
||||
typedef struct nsid3v2_tag_struct_t { } *nsid3v2_tag_t;
|
||||
typedef struct nsid3v2_frame_struct_t { } *nsid3v2_frame_t;
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
Reference in New Issue
Block a user