[go: up one dir, main page]

File: frame_parse.cpp

package info (click to toggle)
id3lib3.8.3 3.8.3-6
  • links: PTS
  • area: main
  • in suites: etch-m68k
  • size: 4,468 kB
  • ctags: 2,608
  • sloc: cpp: 12,324; sh: 10,351; ansic: 7,236; makefile: 387; php: 325
file content (179 lines) | stat: -rw-r--r-- 6,078 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
// $Id: frame_parse.cpp,v 1.34 2002/07/06 13:53:18 t1mpy Exp $

// id3lib: a C++ library for creating and manipulating id3v1/v2 tags
// Copyright 1999, 2000  Scott Thomas Haug

// 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/

#if defined HAVE_CONFIG_H
#include <config.h>
#endif

#include "frame_impl.h"
#include "id3/io_decorators.h" //has "readers.h" "io_helpers.h" "utils.h"

using namespace dami;

namespace
{
  bool parseFields(ID3_Reader& rdr, ID3_FrameImpl& frame)
  {
    int iLoop;
    int iFields;
    io::ExitTrigger et(rdr);
    ID3_TextEnc enc = ID3TE_ASCII;  // set the default encoding 
    ID3_V2Spec spec = frame.GetSpec(); 
    // parse the frame's fields  
    iFields = frame.NumFields();
    ID3D_NOTICE( "ID3_FrameImpl::Parse(): num_fields = " << iFields );
    iLoop = 0;
    for (ID3_FrameImpl::iterator fi = frame.begin(); fi != frame.end(); ++fi)
    {
      ID3_Field* fp = *fi;
      ++iLoop;

      if (rdr.atEnd())
      { 
        // there's no remaining data to parse! 
        ID3D_WARNING( "ID3_FrameImpl::Parse(): out of data at postion " <<
                      rdr.getCur() );
        if (iLoop == iFields)
        {
          //if we are at the last field, (the 'data' field) it's apparently
          //an empty tag used for filling up padding, it's no problem
          //break will set the current 'cursor' to the right spot outside the for loop
          break;
        }
        return false;
      } 
      
      if (NULL == fp)
      {
        // Ack!  Why is the field NULL?  Log this...
        ID3D_WARNING( "ID3_FrameImpl::Parse(): field is null" );
        continue;
      }
      
      if (!fp->InScope(spec)) 
      {
        ID3D_NOTICE( "ID3_FrameImpl::Parse(): field is not in scope" );
        // continue with the rest of the fields
        continue; 
      }
      
      ID3D_NOTICE( "ID3_FrameImpl::Parse(): setting enc to " << enc );
      fp->SetEncoding(enc);
      ID3_Reader::pos_type beg = rdr.getCur();
      et.setExitPos(beg);
      ID3D_NOTICE( "ID3_FrameImpl::Parse(): parsing field, cur = " << beg );
      ID3D_NOTICE( "ID3_FrameImpl::Parse(): parsing field, end = " << 
                   rdr.getEnd() );
      if (!fp->Parse(rdr) || rdr.getCur() == beg) 
      { 
        // nothing to parse!  ack!  parse error... 
        ID3D_WARNING( "ID3_FrameImpl::Parse(): no data parsed, bad parse" );
        return false;
      }
      
      if (fp->GetID() == ID3FN_TEXTENC)  
      {
        enc = static_cast<ID3_TextEnc>(fp->Get());  
        ID3D_NOTICE( "ID3_FrameImpl::Parse(): found encoding = " << enc );
      }
    }
    et.setExitPos(rdr.getCur());
    
    return true;
  }
};

bool ID3_FrameImpl::Parse(ID3_Reader& reader) 
{ 
  io::ExitTrigger et(reader);
  ID3D_NOTICE( "ID3_FrameImpl::Parse(): reader.getBeg() = " << reader.getBeg() );
  ID3D_NOTICE( "ID3_FrameImpl::Parse(): reader.getCur() = " << reader.getCur() );
  ID3D_NOTICE( "ID3_FrameImpl::Parse(): reader.getEnd() = " << reader.getEnd() );
  ID3_Reader::pos_type beg = reader.getCur();

  if (!_hdr.Parse(reader) || reader.getCur() == beg)  
  { 
    ID3D_WARNING( "ID3_FrameImpl::Parse(): no header to parse" );
    return false; 
  }
  ID3D_NOTICE( "ID3_FrameImpl::Parse(): after hdr, getCur() = " << reader.getCur() );
  ID3D_NOTICE( "ID3_FrameImpl::Parse(): found frame! id = " << _hdr.GetTextID() );

  // data is the part of the frame buffer that appears after the header  
  const size_t dataSize = _hdr.GetDataSize();
  ID3D_NOTICE( "ID3_FrameImpl::Parse(): dataSize = " << dataSize );
  if (reader.getEnd() < beg + dataSize)
  {
    ID3D_WARNING( "ID3_FrameImpl::Parse(): not enough data to parse frame" );
    return false;
  }
  io::WindowedReader wr(reader, dataSize);
  ID3D_NOTICE( "ID3_FrameImpl::Parse(): window getBeg() = " << wr.getBeg() );
  ID3D_NOTICE( "ID3_FrameImpl::Parse(): window getCur() = " << wr.getCur() );
  ID3D_NOTICE( "ID3_FrameImpl::Parse(): window getEnd() = " << wr.getEnd() );
  
  unsigned long origSize = 0;
  if (_hdr.GetCompression())
  {
    origSize = io::readBENumber(reader, sizeof(uint32));
    ID3D_NOTICE( "ID3_FrameImpl::Parse(): frame is compressed, origSize = " << origSize );
  }

  if (_hdr.GetEncryption())
  {
    char ch = wr.readChar();
    this->SetEncryptionID(ch);
    ID3D_NOTICE( "ID3_FrameImpl::Parse(): frame is encrypted, encryption_id = " << (int) ch );
  }

  if (_hdr.GetGrouping())
  {
    char ch = wr.readChar();
    this->SetGroupingID(ch);
    ID3D_NOTICE( "ID3_FrameImpl::Parse(): frame is encrypted, grouping_id = " << (int) ch );
  }

  // set the type of frame based on the parsed header  
  this->_ClearFields(); 
  this->_InitFields(); 

  bool success = false;
  // expand out the data if it's compressed 
  if (!_hdr.GetCompression())
  {
    success = parseFields(wr, *this);
  }
  else
  {
    io::CompressedReader csr(wr, origSize);
    success = parseFields(csr, *this);
  }
  et.setExitPos(wr.getCur());

  _changed = false;
  return true;
}