• Skip to content
  • Skip to link menu
KDE API Reference
  • KDE API Reference
  • kdesdk API Reference
  • KDE Home
  • Contact Us
 

okteta

  • sources
  • kde-4.12
  • kdesdk
  • okteta
  • kasten
  • gui
  • io
  • streamencoder
  • ihex
bytearrayihexstreamencoder.cpp
Go to the documentation of this file.
1 /*
2  This file is part of the Okteta Kasten module, made within the KDE community.
3 
4  Copyright 2010 Friedrich W. H. Kossebau <kossebau@kde.org>
5 
6  This library is free software; you can redistribute it and/or
7  modify it under the terms of the GNU Lesser General Public
8  License as published by the Free Software Foundation; either
9  version 2.1 of the License, or (at your option) version 3, or any
10  later version accepted by the membership of KDE e.V. (or its
11  successor approved by the membership of KDE e.V.), which shall
12  act as a proxy defined in Section 6 of version 3 of the license.
13 
14  This library is distributed in the hope that it will be useful,
15  but WITHOUT ANY WARRANTY; without even the implied warranty of
16  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17  Lesser General Public License for more details.
18 
19  You should have received a copy of the GNU Lesser General Public
20  License along with this library. If not, see <http://www.gnu.org/licenses/>.
21 */
22 
23 #include "bytearrayihexstreamencoder.h"
24 
25 // lib
26 #include <bytearrayview.h>
27 // Okteta gui
28 #include <bytearraytablelayout.h>
29 // Okteta core
30 #include <abstractbytearraymodel.h>
31 // KDE
32 #include <KLocale>
33 // Qt
34 #include <QtCore/QTextStream>
35 
36 
37 namespace Kasten2
38 {
39 
40 IHexStreamEncoderSettings::IHexStreamEncoderSettings()
41  : addressSizeId( Bits32Id )
42 {}
43 
44 
45 const char ByteArrayIHexStreamEncoder::hexDigits[16] =
46  { '0','1','2','3','4','5','6','7','8','9','A','B','C','D','E','F' };
47 
48 
49 void ByteArrayIHexStreamEncoder::streamLine( QTextStream& textStream,
50  const unsigned char* line )
51 {
52  // checksum is two's complement of sum of the values in the line
53  unsigned char checksum = 0;
54 
55  textStream << startCode;
56 
57  const uint length = byteCountLineSize + addressLineSize + recordTypeLineSize + line[0];
58  for( uint i = 0; i < length; ++i )
59  {
60  const unsigned char byte = line[i];
61  textStream << hexValueOfNibble( byte >> 4 )
62  << hexValueOfNibble( byte );
63  checksum += byte;
64  }
65 
66  checksum = (checksum ^ 0xFF) + 1;
67  textStream << hexValueOfNibble( checksum >> 4 )
68  << hexValueOfNibble( checksum ) << '\n';
69 }
70 
71 void ByteArrayIHexStreamEncoder::streamExtendedSegmentAddress( QTextStream& textStream,
72  unsigned char* line,
73  quint16 upperSegmentBaseAddress )
74 {
75  static const int nullAddress = 0;
76  static const int upperSegmentBaseAddressSize = 2;
77 
78  line[byteCountLineOffset] = upperSegmentBaseAddressSize;
79  writeBigEndian( &line[addressLineOffset], nullAddress, addressLineSize );
80  line[recordTypeLineOffset] = ExtendedSegmentAddressRecord;
81  line[dataLineOffset] = upperSegmentBaseAddress >> 8;
82  line[dataLineOffset+1] = upperSegmentBaseAddress;
83 
84  streamLine( textStream, line );
85 }
86 
87 void ByteArrayIHexStreamEncoder::streamExtendedLinearAddress( QTextStream& textStream,
88  unsigned char* line,
89  quint16 upperLinearBaseAddress )
90 {
91  static const int nullAddress = 0;
92  static const int upperLinearBaseAddressSize = 2;
93 
94  line[byteCountLineOffset] = upperLinearBaseAddressSize;
95  writeBigEndian( &line[addressLineOffset], nullAddress, addressLineSize );
96  line[recordTypeLineOffset] = ExtendedLinearAddressRecord;
97  line[dataLineOffset] = upperLinearBaseAddress >> 8;
98  line[dataLineOffset+1] = upperLinearBaseAddress;
99 
100  streamLine( textStream, line );
101 }
102 
103 void ByteArrayIHexStreamEncoder::streamEndOfFile( QTextStream& textStream,
104  unsigned char* line,
105  quint16 startAddress )
106 {
107  static const int endOfFileByteCount = 0;
108 
109  line[byteCountLineOffset] = endOfFileByteCount;
110  writeBigEndian( &line[addressLineOffset], startAddress, addressLineSize );
111  line[recordTypeLineOffset] = EndOfFileRecord;
112 
113  streamLine( textStream, line );
114 }
115 
116 
117 ByteArrayIHexStreamEncoder::ByteArrayIHexStreamEncoder()
118  : AbstractByteArrayStreamEncoder( i18nc("name of the encoding target","Intel Hex"), QString::fromLatin1("text/x-ihex") )
119 {}
120 
121 
122 bool ByteArrayIHexStreamEncoder::encodeDataToStream( QIODevice* device,
123  const ByteArrayView* byteArrayView,
124  const Okteta::AbstractByteArrayModel* byteArrayModel,
125  const Okteta::AddressRange& range )
126 {
127  Q_UNUSED( byteArrayView );
128 
129  bool success = true;
130 
131  // encode
132  QTextStream textStream( device );
133 
134  // prepare
135  static const int maxDataPerLineCount = 255;
136  static const int maxLineLength =
137  maxDataPerLineCount + byteCountLineSize + addressLineSize + recordTypeLineSize;
138 
139  const Okteta::ByteArrayTableLayout layout( byteArrayView->noOfBytesPerLine(),
140  byteArrayView->firstLineOffset(),
141  byteArrayView->startOffset(), 0, byteArrayModel->size() );
142 
143  const Okteta::Coord startCoord = layout.coordOfIndex( range.start() );
144  const int lastLinePosition = layout.lastLinePosition( startCoord.line() );
145  const int dataPerLineCount = qMin( byteArrayView->noOfBytesPerLine(), maxDataPerLineCount );
146  unsigned char line[maxLineLength];
147  unsigned char* lineData = &line[dataLineOffset];
148  Okteta::Address lineOffset = range.start();
149  const int firstDataEnd = lastLinePosition - startCoord.pos() + 1;
150  int nextUpperAddressChangeDataEnd = 0x10000 - (range.start() & 0xFFFF);
151  int d = 0;
152  int nextDataEnd = qMin( dataPerLineCount,
153  qMin(firstDataEnd,nextUpperAddressChangeDataEnd) );
154 
155  // data
156  if( mSettings.addressSizeId == IHexStreamEncoderSettings::Bits32Id )
157  {
158  const quint16 upperLinearBaseAddress = (range.start() >> 16);
159  streamExtendedLinearAddress( textStream, line, upperLinearBaseAddress );
160  }
161  else if( mSettings.addressSizeId == IHexStreamEncoderSettings::Bits16Id )
162  {
163  const quint16 upperSegmentBaseAddress = (range.start() >> 4) & 0xF000;
164  streamExtendedSegmentAddress( textStream, line, upperSegmentBaseAddress );
165  }
166  Okteta::Address i = range.start();
167  while( i <= range.end() )
168  {
169  const Okteta::Byte byte = byteArrayModel->byte( i );
170  lineData[d] = byte;
171 
172  ++d;
173  ++i;
174  if( d == nextDataEnd )
175  {
176  line[byteCountLineOffset] = d;
177  writeBigEndian( &line[addressLineOffset], lineOffset, addressLineSize );
178  line[recordTypeLineOffset] = DataRecord;
179 
180  streamLine( textStream, line );
181 
182  lineOffset = i;
183 
184  if( d == nextUpperAddressChangeDataEnd )
185  {
186  if( mSettings.addressSizeId == IHexStreamEncoderSettings::Bits32Id )
187  {
188  const quint16 upperLinearBaseAddress = (i >> 16);
189  streamExtendedLinearAddress( textStream, line, upperLinearBaseAddress );
190  }
191  else if( mSettings.addressSizeId == IHexStreamEncoderSettings::Bits16Id )
192  {
193  const quint16 upperSegmentBaseAddress = (i >> 4) & 0xF000;
194  streamExtendedSegmentAddress( textStream, line, upperSegmentBaseAddress );
195  }
196  }
197  nextUpperAddressChangeDataEnd = 0x10000 - (i & 0xFFFF);
198  nextDataEnd = qMin( dataPerLineCount,
199  qMin(range.end()-i+1, nextUpperAddressChangeDataEnd) );
200  d = 0;
201  }
202  }
203 
204  // footer
205  streamEndOfFile( textStream, line );
206 
207  return success;
208 }
209 
210 ByteArrayIHexStreamEncoder::~ByteArrayIHexStreamEncoder() {}
211 
212 }
Kasten2::IHexStreamEncoderSettings::Bits32Id
Definition: bytearrayihexstreamencoder.h:42
Kasten2::ByteArrayIHexStreamEncoder::~ByteArrayIHexStreamEncoder
virtual ~ByteArrayIHexStreamEncoder()
Definition: bytearrayihexstreamencoder.cpp:210
Okteta::Address
qint32 Address
Definition: address.h:34
Okteta::AbstractByteArrayModel
could it be useful to hide the data access behind an iterator? * class KDataBufferIterator { public: ...
Definition: abstractbytearraymodel.h:79
bytearrayihexstreamencoder.h
abstractbytearraymodel.h
Kasten2::ByteArrayView::noOfBytesPerLine
int noOfBytesPerLine() const
Definition: bytearrayview.cpp:244
KDE::NumberRange< Address, Size >
KDE::Range::start
T start() const
Definition: range.h:86
Okteta::Byte
unsigned char Byte
Definition: byte.h:29
Okteta::Coord
a class which represents a coord in a 2-dim.
Definition: coord.h:47
Kasten2::ByteArrayView::firstLineOffset
Okteta::Address firstLineOffset() const
Definition: bytearrayview.cpp:240
Kasten2::ByteArrayIHexStreamEncoder::mSettings
IHexStreamEncoderSettings mSettings
Definition: bytearrayihexstreamencoder.h:103
Kasten2::AbstractByteArrayStreamEncoder
Definition: abstractbytearraystreamencoder.h:45
Kasten2::IHexStreamEncoderSettings::Bits16Id
Definition: bytearrayihexstreamencoder.h:42
Kasten2::IHexStreamEncoderSettings::addressSizeId
AddressSizeId addressSizeId
Definition: bytearrayihexstreamencoder.h:47
bytearraytablelayout.h
Okteta::ByteArrayTableLayout
the logical layout of a byte array table for a view
Definition: bytearraytablelayout.h:61
Kasten2::ByteArrayIHexStreamEncoder::ByteArrayIHexStreamEncoder
ByteArrayIHexStreamEncoder()
Definition: bytearrayihexstreamencoder.cpp:117
Okteta::AbstractByteArrayModel::size
virtual Size size() const =0
KDE::Range::end
T end() const
Definition: range.h:88
Kasten2::IHexStreamEncoderSettings::IHexStreamEncoderSettings
IHexStreamEncoderSettings()
Definition: bytearrayihexstreamencoder.cpp:40
Okteta::AbstractByteArrayModel::byte
virtual Byte byte(Address offset) const =0
locates working range The idea behind is to tell buffer which range will be requested in the followin...
Kasten2::ByteArrayIHexStreamEncoder::encodeDataToStream
virtual bool encodeDataToStream(QIODevice *device, const ByteArrayView *byteArrayView, const Okteta::AbstractByteArrayModel *byteArrayModel, const Okteta::AddressRange &range)
Definition: bytearrayihexstreamencoder.cpp:122
Kasten2::ByteArrayView::startOffset
Okteta::Address startOffset() const
Definition: bytearrayview.cpp:236
QIODevice
Kasten2::ByteArrayView
Definition: bytearrayview.h:51
bytearrayview.h
This file is part of the KDE documentation.
Documentation copyright © 1996-2014 The KDE developers.
Generated on Tue Oct 14 2014 23:04:07 by doxygen 1.8.7 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.

okteta

Skip menu "okteta"
  • Main Page
  • Namespace List
  • Namespace Members
  • Alphabetical List
  • Class List
  • Class Hierarchy
  • Class Members
  • File List
  • File Members
  • Related Pages

kdesdk API Reference

Skip menu "kdesdk API Reference"
  • kapptemplate
  • kcachegrind
  • kompare
  • lokalize
  • okteta
  • umbrello
  •   umbrello

Search



Report problems with this website to our bug tracking system.
Contact the specific authors with questions and comments about the page contents.

KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. | Legal