1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260
|
// The authors have released ID3Lib as Public Domain (PD) and claim no copyright,
// patent or other intellectual property protection in this work. This means that
// it may be modified, redistributed and used in commercial and non-commercial
// software and hardware without restrictions. ID3Lib is distributed on an "AS IS"
// basis, WITHOUT WARRANTY OF ANY KIND, either express or implied.
//
// The ID3Lib authors encourage improvements and optimisations to be sent to the
// ID3Lib coordinator, currently Dirk Mahoney (dirk@id3.org). Approved
// submissions may be altered, and will be included and released under these terms.
//
// Mon Nov 23 18:34:01 1998
#include <string.h>
#include <memory.h>
#include "id3_tag.h"
#include "id3_misc_support.h"
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 ( version, revision );
bytesUsed += header.Size();
// set up the encryption and grouping IDs
// ...
while ( cur )
{
if ( cur->frame )
{
cur->frame->compression = compression;
cur->frame->SetVersion ( version, revision );
bytesUsed += cur->frame->Render ( &buffer[ bytesUsed ] );
}
cur = cur->next;
}
if ( syncOn )
{
uchar *tempz;
luint newTagSize;
newTagSize = GetUnSyncSize ( &buffer[ header.Size() ], bytesUsed - header.Size() );
if ( newTagSize > 0 && ( newTagSize + header.Size() ) > bytesUsed )
{
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
hasChanged = false;
return bytesUsed;
}
luint ID3_Tag::Size ( void )
{
luint bytesUsed = 0;
ID3_Elem *cur = frameList;
ID3_TagHeader header;
header.SetVersion ( version, revision );
bytesUsed += header.Size();
while ( cur )
{
if ( cur->frame )
{
cur->frame->SetVersion ( version, revision );
bytesUsed += cur->frame->Size();
}
cur = cur->next;
}
// add 30% for sync
if ( syncOn )
bytesUsed += bytesUsed / 3;
bytesUsed += PaddingSize ( bytesUsed );
return bytesUsed;
}
void ID3_Tag::RenderExtHeader ( uchar *buffer )
{
luint bytesUsed = 0;
if ( version == 3 && revision == 0 )
{
}
return;
}
void ID3_Tag::GenerateTempName ( void )
{
tmpnam ( tempName );
return;
}
void ID3_Tag::RenderToHandle ( void )
{
uchar *buffer;
luint size;
if ( fileHandle )
{
if ( size = Size() )
{
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 ( ( oldTagSize == 0 && fileSize == 0 ) || ( size == oldTagSize && size != 0 ) )
{
fseek ( fileHandle, 0, SEEK_SET );
fwrite ( buffer, 1, size, fileHandle );
oldTagSize = 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 ( tempName ) > 0 )
{
if ( tempOut = fopen ( tempName, "wb" ) )
{
uchar *buffer2;
fwrite ( buffer, 1, size, tempOut );
fseek ( fileHandle, oldTagSize, 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 );
remove ( fileName );
rename ( tempName, fileName );
OpenLinkedFile();
oldTagSize = size;
}
}
}
}
delete[] buffer;
}
else
ID3_THROW ( ID3E_NoMemory );
}
}
else
ID3_THROW ( ID3E_NoData );
return;
}
#define ID3_PADMULTIPLE ( 2048 )
#define ID3_PADMAX ( 4096 )
luint ID3_Tag::PaddingSize ( luint curSize )
{
luint newSize = 0;
// if padding is switched off or there is no attached file
if ( ! padding || fileSize == 0 )
return 0;
// if the old tag was large enough to hold the new tag, then
// we will simply pad out the difference - that way the new
// tag can be written without shuffling the rest of the song
// file around
if ( oldTagSize && ( oldTagSize > curSize ) && ( curSize - oldTagSize ) < ID3_PADMAX )
newSize = oldTagSize;
else
{
luint tempSize = curSize + fileSize;
// this method of automatic padding
// rounds the COMPLETE FILE up to the
// nearest 2K. If the file will already be an
// even multiple of 2K (with the tag included)
// then we just add another 2K of padding
tempSize = ( ( tempSize / ID3_PADMULTIPLE ) + 1 ) * ID3_PADMULTIPLE;
// the size of the new tag is the new filesize
// minus the song size
newSize = tempSize - fileSize;
}
return newSize - curSize;
}
|