summaryrefslogtreecommitdiffstats
path: root/src/coff/base/coff.cpp
blob: e85b53d4c79d522dea5435d25c05a83a2d118e2e (plain)
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
/***************************************************************************
 *   Copyright (C) 2006-2007 Nicolas Hadacek <hadacek@kde.org>             *
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU General Public License as published by  *
 *   the Free Software Foundation; either version 2 of the License, or     *
 *   (at your option) any later version.                                   *
 ***************************************************************************/
#include "coff.h"

#include <time.h>

#include "common/common/misc.h"
#include "devices/list/device_list.h"
#include "devices/base/device_group.h"
#include "devices/pic/base/pic_register.h"
#include "coff_data.h"
#include "common/global/pfile.h"

//----------------------------------------------------------------------------
const CoffType::Data CoffType::DATA[Nb_Types] = {
  { "archive", I18N_NOOP("Archive") },
  { "object",  I18N_NOOP("Object")  }
};

const Coff::Format::Data Coff::Format::DATA[Nb_Types] = {
  { 0, I18N_NOOP("Old Microchip"), 0x1234, { 20, 16, 40, 18, 16, 12 } },
  { 0, I18N_NOOP("PIC30"),         0x1236, { 20, 28, 40, 18,  8, 10 } }, // from PIC30 binutils "coff.h" file
  { 0, I18N_NOOP("New Microchip"), 0x1240, { 20, 18, 40, 20, 16, 12 } }  // (C18 >= 3.0)
};

CoffType Coff::identify(const PURL::Url &url, Log::Base &log)
{
  PURL::File file(url, log);
  if ( !file.openForRead() ) return CoffType::Nb_Types;
  TQByteArray data = file.readAll();
  if ( log.hasError() ) return CoffType::Nb_Types;
  uint offset = 0;
  Format format;
  TQ_UINT32 magic;
  return identify(data, offset, log, format, magic);
}

CoffType Coff::identify(const TQByteArray &data, uint &offset, Log::Base &log, Format &format, TQ_UINT32 &magic)
{
  TQString id = "!<arch>\012";
  if ( data.count()>=id.length() ) {
    TQString s = TQString::fromAscii(data.data(), id.length());
    if ( s==id ) {
      offset += id.length();
      return CoffType::Archive;
    }
  }
  if ( !getULong(data, offset, 2, log, magic) ) return CoffType::Nb_Types;
  log.log(Log::DebugLevel::Extra, TQString("COFF format: %1").arg(toHexLabel(magic, 4)));
  format = Format::Nb_Types;
  FOR_EACH(Format, f) if ( magic==f.data().magic ) format = f;
  return CoffType::Object;
}

//----------------------------------------------------------------------------
bool Coff::getULong(const TQByteArray &data, uint &offset, uint nbBytes, Log::Base &log, TQ_UINT32 &v)
{
  bool ok;
  v = ::getULong(data, offset, nbBytes, &ok);
  if ( !ok ) {
    log.log(Log::LineType::Error, i18n("COFF file is truncated (offset: %1  nbBytes: %2  size:%3).").arg(offset).arg(nbBytes).arg(data.count()));
    return false;
  }
  offset += nbBytes;
  return true;
}

bool Coff::getString(const TQByteArray &data, uint &offset, uint nbChars, Log::Base &log, TQString &name)
{
  if ( !checkAvailable(data, offset, nbChars) ) {
    log.log(Log::LineType::Error, i18n("COFF file is truncated (offset: %1  nbBytes: %2  size:%3).").arg(offset).arg(nbChars).arg(data.count()));
    return false;
  }
  name = TQString::fromAscii(data.data()+offset, nbChars);
  offset += nbChars;
  return true;
}

//----------------------------------------------------------------------------
bool Coff::Base::initParse(CoffType type, TQByteArray &data, uint &offset, Log::Base &log)
{
  PURL::File file(_url, log);
  if ( !file.openForRead() ) return false;
  data = file.readAll();
  if ( log.hasError() ) return false;
  if ( identify(data, offset, log, _format, _magic)!=type ) {
    log.log(Log::LineType::Error, i18n("Could not recognize file (magic number is %1).").arg(toHexLabel(_magic, 4)));
    return false;
  }
  return true;
}