size_t ID3_Field::Get(char* buffer, size_t maxLength, index_t itemNum) const { unicode_t* unicode_buffer = new unicode_t[maxLength]; if (NULL == unicode_buffer) { ID3_THROW(ID3E_NoMemory); } size_t len = Get(unicode_buffer, maxLength, itemNum); char* ascii_buffer = new char[len + 1]; if (NULL == ascii_buffer) { ID3_THROW(ID3E_NoMemory); } ucstombs(ascii_buffer, unicode_buffer, len + 1); size_t ascii_len = strlen(ascii_buffer); size_t length = MIN(ascii_len, maxLength); strncpy(buffer, ascii_buffer, length); buffer[length] = '\0'; delete [] ascii_buffer; delete [] unicode_buffer; return length; }
void ID3_Field::Set(const uchar *newData, size_t newSize) { Clear(); if (newSize > 0) { __data = new uchar[newSize]; if (NULL == __data) { ID3_THROW(ID3E_NoMemory); } if (newData != NULL) { memcpy(__data, newData, newSize); } else { memset(__data, 0, newSize); } __size = newSize; __type = ID3FTY_BINARY; __changed = true; } return ; }
void ID3_Field::Get(uchar *buffer, size_t buffLength) const { if (NULL == buffer) { ID3_THROW(ID3E_NoBuffer); } if (__data != NULL && __size > 0) { memcpy(buffer, __data, MIN(buffLength, __size)); } }
ID3_Frame* ID3_AddSyncLyrics(ID3_Tag *tag, const char *lang, const char *desc, const uchar *text, size_t textsize, bool bReplace) { ID3_Frame* pFrame = NULL; // language and descriptor should be mandatory if ((NULL == lang) || (NULL == desc)) { return NULL; } // check if a SYLT frame of this language or descriptor already exists ID3_Frame* pFrameExist = tag->Find(ID3FID_SYNCEDLYRICS, ID3FN_LANGUAGE, (char *) lang); if (!pFrameExist) { pFrameExist = tag->Find(ID3FID_SYNCEDLYRICS, ID3FN_DESCRIPTION, (char *) desc); } if (NULL != tag && NULL != text) { if (bReplace && pFrameExist) { tag->RemoveFrame (pFrameExist); pFrameExist = NULL; } // if the frame still exist, cannot continue if (pFrameExist) { return NULL; } ID3_Frame* pFrame = new ID3_Frame(ID3FID_SYNCEDLYRICS); if (NULL == pFrame) { ID3_THROW(ID3E_NoMemory); } pFrame->Field(ID3FN_LANGUAGE) = lang; pFrame->Field(ID3FN_DESCRIPTION) = desc; pFrame->Field(ID3FN_DATA).Set(text, textsize); tag->AttachFrame(pFrame); } return pFrame; }
void ID3_AddTitle ( ID3_Tag *tag, char *text ) { if ( tag->Find ( ID3FID_TITLE ) == NULL && strlen ( text ) > 0 ) { ID3_Frame *titleFrame; if ( titleFrame = new ID3_Frame ) { titleFrame->SetID ( ID3FID_TITLE ); titleFrame->Field ( ID3FN_TEXT ) = text; tag->AddFrame ( titleFrame, true ); } else ID3_THROW ( ID3E_NoMemory ); } return; }
void ID3_AddAlbum ( ID3_Tag *tag, char *text ) { if ( tag->Find ( ID3FID_ALBUM ) == NULL && strlen ( text ) > 0 ) { ID3_Frame *albumFrame; if ( albumFrame = new ID3_Frame ) { albumFrame->SetID ( ID3FID_ALBUM ); albumFrame->Field ( ID3FN_TEXT ) = text; tag->AddFrame ( albumFrame, true ); } else ID3_THROW ( ID3E_NoMemory ); } return; }
void ID3_Field::ToFile(const char *info) const { if (NULL == info) { ID3_THROW(ID3E_NoData); } if ((__data != NULL) && (__size > 0)) { FILE* temp_file = fopen(info, "wb"); if (temp_file != NULL) { fwrite(__data, 1, __size, temp_file); fclose(temp_file); } } return ; }
void ID3_AddLyrics( ID3_Tag *tag, char *text ) { if ( tag->Find ( ID3FID_UNSYNCEDLYRICS ) == NULL && strlen ( text ) > 0 ) { ID3_Frame *lyricsFrame; if ( lyricsFrame = new ID3_Frame ) { lyricsFrame->SetID ( ID3FID_UNSYNCEDLYRICS ); lyricsFrame->Field ( ID3FN_LANGUAGE ) = "eng"; lyricsFrame->Field ( ID3FN_TEXT ) = text; tag->AddFrame ( lyricsFrame, true ); } else ID3_THROW ( ID3E_NoMemory ); } return; }
void ID3_Field::Add(const char *sString) { if (sString) { unicode_t *unicode_buffer; unicode_buffer = new unicode_t[strlen(sString) + 1]; if (NULL == unicode_buffer) { ID3_THROW(ID3E_NoMemory); } mbstoucs(unicode_buffer, sString, strlen(sString) + 1); Add(unicode_buffer); delete [] unicode_buffer; this->SetEncoding(ID3TE_ASCII); __type = ID3FTY_TEXTSTRING; } }
void ID3_AddArtist ( ID3_Tag *tag, char *text ) { if ( tag->Find ( ID3FID_LEADARTIST ) == NULL && tag->Find ( ID3FID_BAND ) == NULL && tag->Find ( ID3FID_CONDUCTOR ) == NULL && tag->Find ( ID3FID_COMPOSER ) == NULL && strlen ( text ) > 0 ) { ID3_Frame *artistFrame; if ( artistFrame = new ID3_Frame ) { artistFrame->SetID ( ID3FID_LEADARTIST ); artistFrame->Field ( ID3FN_TEXT ) = text; tag->AddFrame ( artistFrame, true ); } else ID3_THROW ( ID3E_NoMemory ); } return; }
void ID3_Field::Set(const char *sString) { if (sString != NULL) { Clear(); size_t nStrLen = (0 == __length) ? strlen(sString) : __length; unicode_t *temp = new unicode_t[nStrLen + 1]; if (NULL == temp) { ID3_THROW(ID3E_NoMemory); } mbstoucs(temp, sString, nStrLen + 1); Set(temp); delete [] temp; this->SetEncoding(ID3TE_ASCII); __type = ID3FTY_TEXTSTRING; } return ; }
luint ID3_Tag::Render( uchar *buffer ) { luint bytesUsed = 0; if ( buffer ) { ID3_Elem *cur = frameList; ID3_TagHeader header; SetVersion ( ID3_TAGVERSION, ID3_TAGREVISION ); header.SetVersion ( m_nVersion, m_nRevision ); bytesUsed += header.Size(); // set up the encryption and grouping IDs // ... while( cur ) { // PL // Check that frame has a valid FrameID if( ( cur->frame->GetID() <= 0 ) || (cur->frame->GetID() > ID3FID_CRYPTOREG )) { // TRACE( "Invalide FrameID\n" ); cur = cur->next; continue; } // End PL if ( cur->frame ) { cur->frame->compression = m_bCompression; cur->frame->SetVersion ( m_nVersion, m_nRevision ); bytesUsed += cur->frame->Render ( &buffer[ bytesUsed ] ); } cur = cur->next; } if ( m_bSyncOn ) { uchar *tempz; luint newTagSize; newTagSize = GetUnSyncSize ( &buffer[ header.Size() ], bytesUsed - header.Size() ); if ( newTagSize > 0 && ( newTagSize + header.Size() ) > bytesUsed ) { #ifdef _DEBUG // ASSERT( newTagSize < MAX_ALLOC ); // PL #endif if ( tempz = new uchar[ newTagSize ] ) { UnSync ( tempz, newTagSize, &buffer[ header.Size() ], bytesUsed - header.Size() ); header.SetFlags ( ID3HF_UNSYNC ); memcpy ( &buffer[ header.Size() ], tempz, newTagSize ); bytesUsed = newTagSize + header.Size(); delete[] tempz; } else ID3_THROW ( ID3E_NoMemory ); } } // zero the remainder of the buffer so that our // padding bytes are zero for ( luint i = 0; i < PaddingSize ( bytesUsed ); i++ ) buffer[ bytesUsed + i ] = 0; bytesUsed += PaddingSize ( bytesUsed ); header.SetDataSize ( bytesUsed - header.Size() ); header.Render ( buffer ); } else ID3_THROW ( ID3E_NoBuffer ); // set the flag which says that the tag hasn't changed m_bHasChanged = false; return bytesUsed; }
void ID3_Tag::RenderToHandle( void ) { uchar *buffer; luint size; if ( fileHandle ) { if ( size = Size() ) { #ifdef _DEBUG // ASSERT( size < MAX_ALLOC ); // PL #endif if ( buffer = new uchar[ size ] ) { if ( size = Render ( buffer ) ) { // if the new tag fits perfectly within the old // and the old one actually existed (ie this isn't the first tag // this file has had) if ( ( m_nOldTagSize == 0 && m_nFileSize == 0 ) || ( size == m_nOldTagSize && size != 0 ) ) { fseek ( fileHandle, 0, SEEK_SET ); fwrite ( buffer, 1, size, fileHandle ); m_nOldTagSize = size; } else { // else we gotta make a temp file, // copy the tag into it, copy the // rest of the old file after the tag, // delete the old file, rename this // new file to the old file's name // and update the fileHandle FILE *tempOut; if ( strlen ( m_strTempName ) > 0 ) { if ( tempOut = fopen ( m_strTempName, "wb" ) ) { uchar *buffer2; fwrite ( buffer, 1, size, tempOut ); fseek ( fileHandle, m_nOldTagSize, SEEK_SET ); if ( buffer2 = new uchar [ BUFF_SIZE ] ) { int i; while ( ! feof ( fileHandle ) ) { i = fread ( buffer2, 1, BUFF_SIZE, fileHandle ); fwrite ( buffer2, 1, i, tempOut ); } delete[] buffer2; } fclose ( tempOut ); fclose ( fileHandle ); // DeleteFileA( m_strFileName ); // MoveFileA( m_strTempName, m_strFileName ); OpenLinkedFile( LINK_READONLY ); m_nOldTagSize = size; } } } } delete[] buffer; } else ID3_THROW ( ID3E_NoMemory ); } } else ID3_THROW ( ID3E_NoData ); return; }
size_t ID3_Field::ParseASCIIString(const uchar *buffer, size_t nSize) { size_t bytesUsed = 0; char *temp = NULL; if (__length > 0) { // The string is of fixed length bytesUsed = __length; } else if (!(__flags & ID3FF_CSTR) || (__flags & ID3FF_LIST)) { // If the string isn't null-terminated or if it is null divided, we're // assured this is the last field of of the frame, and we can claim the // remaining bytes for ourselves bytesUsed = nSize; } else { while (bytesUsed < nSize && buffer[bytesUsed] != '\0') { bytesUsed++; } } if (0 == bytesUsed) { Set(""); } // This check needs to come before the check for ID3FF_CSTR else if (__flags & ID3FF_LIST) { const char *sBuffer = (const char *) buffer; for (size_t i = 0; i < bytesUsed; i += strlen(&sBuffer[i]) + 1) { Add(&sBuffer[i]); } } // This check needs to come after the check for ID3FF_LIST else if (__flags & ID3FF_CSTR) { Set((const char *)buffer); } else { // Sanity check our indices and sizes before we start copying memory if (bytesUsed > nSize) { ID3_THROW_DESC(ID3E_BadData, "field information invalid"); } temp = new char[bytesUsed + 1]; if (NULL == temp) { ID3_THROW(ID3E_NoMemory); } memcpy(temp, buffer, bytesUsed); temp[bytesUsed] = '\0'; Set(temp); delete [] temp; } if (__flags & ID3FF_CSTR && !(__flags & ID3FF_LIST)) { bytesUsed++; } __changed = false; return bytesUsed; }