[go: up one dir, main page]

File: tag_render.cpp

package info (click to toggle)
id3lib3.8.3 3.8.3-18
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, trixie
  • size: 5,636 kB
  • sloc: cpp: 12,364; sh: 9,186; ansic: 7,240; makefile: 355; php: 325
file content (228 lines) | stat: -rw-r--r-- 6,523 bytes parent folder | download | duplicates (10)
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
// $Id: tag_render.cpp,v 1.44 2002/07/31 13:20:49 t1mpy Exp $

// id3lib: a C++ library for creating and manipulating id3v1/v2 tags
// Copyright 1999, 2000  Scott Thomas Haug
// Copyright 2002  Thijmen Klok (thijmen@id3lib.org)

// This library is free software; you can redistribute it and/or modify it
// under the terms of the GNU Library General Public License as published by
// the Free Software Foundation; either version 2 of the License, or (at your
// option) any later version.
//
// This library is distributed in the hope that it will be useful, but WITHOUT
// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
// FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Library General Public
// License for more details.
//
// You should have received a copy of the GNU Library General Public License
// along with this library; if not, write to the Free Software Foundation,
// Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.

// The id3lib authors encourage improvements and optimisations to be sent to
// the id3lib coordinator.  Please see the README file for details on where to
// send such submissions.  See the AUTHORS file for a list of people who have
// contributed to id3lib.  See the ChangeLog file for a list of changes to
// id3lib.  These files are distributed with id3lib at
// http://download.sourceforge.net/id3lib/

#include <memory.h>
#include "tag_impl.h" //has <stdio.h> "tag.h" "header_tag.h" "frame.h" "field.h" "spec.h" "id3lib_strings.h" "utils.h"
#include "helpers.h"
#include "writers.h"
#include "id3/io_decorators.h" //has "readers.h" "io_helpers.h" "utils.h"
#include "io_helpers.h"
#include "io_strings.h"

#if defined HAVE_SYS_PARAM_H
#include <sys/param.h>
#endif

using namespace dami;

void id3::v1::render(ID3_Writer& writer, const ID3_TagImpl& tag)
{
  writer.writeChars("TAG", 3);
  
  io::writeTrailingSpaces(writer, id3::v2::getTitle(tag),  ID3_V1_LEN_TITLE);
  io::writeTrailingSpaces(writer, id3::v2::getArtist(tag), ID3_V1_LEN_ARTIST);
  io::writeTrailingSpaces(writer, id3::v2::getAlbum(tag),  ID3_V1_LEN_ALBUM);
  io::writeTrailingSpaces(writer, id3::v2::getYear(tag),   ID3_V1_LEN_YEAR);
  
  size_t track = id3::v2::getTrackNum(tag);
  String comment = id3::v2::getV1Comment(tag);
  if (track > 0)
  {
    io::writeTrailingSpaces(writer, comment, ID3_V1_LEN_COMMENT - 2);
    writer.writeChar('\0');
    writer.writeChar((char) track);
  }
  else
  {
    io::writeTrailingSpaces(writer, comment, ID3_V1_LEN_COMMENT);
  }
  writer.writeChar((char) id3::v2::getGenreNum(tag));
}

namespace
{
  void renderFrames(ID3_Writer& writer, const ID3_TagImpl& tag)
  {
    for (ID3_TagImpl::const_iterator iter = tag.begin(); iter != tag.end(); ++iter)
    {
      const ID3_Frame* frame = *iter;
      if (frame) frame->Render(writer);
    }
  }
}

void id3::v2::render(ID3_Writer& writer, const ID3_TagImpl& tag)
{
  // There has to be at least one frame for there to be a tag...
  if (tag.NumFrames() == 0)
  {
    ID3D_WARNING( "id3::v2::render(): no frames to render" );
    return;
  }
  
  ID3D_NOTICE( "id3::v2::render(): rendering" );
  ID3_TagHeader hdr;
  hdr.SetSpec(tag.GetSpec());
  hdr.SetExtended(tag.GetExtended());
  hdr.SetExperimental(tag.GetExperimental());
  hdr.SetFooter(tag.GetFooter());
    
  // set up the encryption and grouping IDs

  // ...
  String frms;
  io::StringWriter frmWriter(frms);
  if (!tag.GetUnsync())
  {
    ID3D_NOTICE( "id3::v2::render(): rendering frames" );
    renderFrames(frmWriter, tag);
    hdr.SetUnsync(false);
  }
  else
  {
    ID3D_NOTICE( "id3::v2::render(): rendering unsynced frames" );
    io::UnsyncedWriter uw(frmWriter);
    renderFrames(uw, tag);
    uw.flush();
    ID3D_NOTICE( "id3::v2::render(): numsyncs = " << uw.getNumSyncs() );
    hdr.SetUnsync(uw.getNumSyncs() > 0);
  }
  size_t frmSize = frms.size();
  if (frmSize == 0)
  {
    ID3D_WARNING( "id3::v2::render(): rendered frame size is 0 bytes" );
    return;
  }
  
  // zero the remainder of the buffer so that our padding bytes are zero
  luint nPadding = tag.PaddingSize(frmSize);
  ID3D_NOTICE( "id3::v2::render(): padding size = " << nPadding );
  
  hdr.SetDataSize(frmSize + tag.GetExtendedBytes() + nPadding);
  
  hdr.Render(writer);

  writer.writeChars(frms.data(), frms.size());

  for (size_t i = 0; i < nPadding; ++i)
  {
    if (writer.writeChar('\0') == ID3_Writer::END_OF_WRITER)
    {
      break;
    }
  }
}

size_t ID3_TagImpl::Size() const
{
  if (this->NumFrames() == 0)
  {
    return 0;
  }
  ID3_TagHeader hdr;

  hdr.SetSpec(this->GetSpec());
  size_t bytesUsed = hdr.Size();
  
  size_t frameBytes = 0;
  for (const_iterator cur = _frames.begin(); cur != _frames.end(); ++cur)
  {
    if (*cur)
    {
      (*cur)->SetSpec(this->GetSpec());
      frameBytes += (*cur)->Size();
    }
  }
  
  if (!frameBytes)
  {
    return 0;
  }
  
  bytesUsed += frameBytes;
  // add 30% for sync
  if (this->GetUnsync())
  {
    bytesUsed += bytesUsed / 3;
  }
    
  bytesUsed += this->PaddingSize(bytesUsed);
  return bytesUsed;
}


void ID3_TagImpl::RenderExtHeader(uchar *buffer)
{
  if (this->GetSpec() == ID3V2_3_0)
  {
  }
  
  return ;
}


#define ID3_PADMULTIPLE (2048)
#define ID3_PADMAX  (4096)


size_t ID3_TagImpl::PaddingSize(size_t curSize) const
{
  luint newSize = 0;
  
  // if padding is switched off
  if (! _is_padded)
  {
    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 ((this->GetPrependedBytes()-ID3_TagHeader::SIZE > 0) &&
      (this->GetPrependedBytes()-ID3_TagHeader::SIZE >= curSize) && 
      (this->GetPrependedBytes()-ID3_TagHeader::SIZE - curSize) < ID3_PADMAX)
  {
    newSize = this->GetPrependedBytes()-ID3_TagHeader::SIZE;
  }
  else
  {
    luint tempSize = curSize + ID3_GetDataSize(*this) +
                     this->GetAppendedBytes() + ID3_TagHeader::SIZE;
    
    // 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 audio data
    newSize = tempSize - ID3_GetDataSize(*this) - this->GetAppendedBytes () -
              ID3_TagHeader::SIZE;
  }
  
  return newSize - curSize;
}