• 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
  • xxencoding
bytearrayxxencodingstreamencoder.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 "bytearrayxxencodingstreamencoder.h"
24 
25 // Okteta core
26 #include <abstractbytearraymodel.h>
27 // KDE
28 #include <KLocale>
29 // Qt
30 #include <QtCore/QTextStream>
31 
32 
33 namespace Kasten2
34 {
35 
36 static const char xxencodeMap[64] =
37 {
38  '+', '-', '0', '1', '2', '3', '4', '5',
39  '6', '7', '8', '9', 'A', 'B', 'C', 'D',
40  'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L',
41  'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T',
42  'U', 'V', 'W', 'X', 'Y', 'Z', 'a', 'b',
43  'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j',
44  'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r',
45  's', 't', 'u', 'v', 'w', 'x', 'y', 'z'
46 };
47 
48 static const int defaultxxInputLineLength = 45;
49 static const int xxInputLineLength = defaultxxInputLineLength;
50 static const int xxInputGroupLength = 3;
51 static const int maxXxInputGroupsPerLine = xxInputLineLength/xxInputGroupLength;
52 
53 static inline char xxmapByte( char byte ) { return xxencodeMap[(int)byte]; }
54 
55 static inline const char* xxpadding( ByteArrayXxencodingStreamEncoder::InputByteIndex index )
56 {
57  const char* const paddingData[2] = {"++","+"};
58 
59  return paddingData[index - 1];
60 }
61 
62 
63 XxencodingStreamEncoderSettings::XxencodingStreamEncoderSettings()
64  : fileName( QString::fromLatin1("okteta-export"))
65 {}
66 
67 ByteArrayXxencodingStreamEncoder::ByteArrayXxencodingStreamEncoder()
68  : AbstractByteArrayStreamEncoder( i18nc("name of the encoding target","Xxencoding"), QString::fromLatin1("text/x-xxencode") )
69 {}
70 
71 // TODO: make this algorithm shared with ByteArrayUuencodingStreamEncoder again
72 bool ByteArrayXxencodingStreamEncoder::encodeDataToStream( QIODevice* device,
73  const ByteArrayView* byteArrayView,
74  const Okteta::AbstractByteArrayModel* byteArrayModel,
75  const Okteta::AddressRange& range )
76 {
77  Q_UNUSED( byteArrayView );
78 
79  const char header[] = "begin";
80  const char footer[] = "\n+\nend\n";
81 
82  bool success = true;
83 
84  // encode
85  QTextStream textStream( device );
86 
87  // prepare
88  InputByteIndex inputByteIndex = FirstByte;
89  int inputGroupsPerLine = 0;
90  unsigned char bitsFromLastByte;
91 
92  // header
93  textStream << header << " 644 " << mSettings.fileName.toLatin1();
94 
95  const int firstLineLength = qMin( range.width(), xxInputLineLength );
96  if( firstLineLength > 0 )
97  {
98  textStream << '\n';
99  textStream << xxmapByte( firstLineLength );
100  }
101 
102  for( Okteta::Address i=range.start(); i<=range.end(); ++i )
103  {
104  const Okteta::Byte byte = byteArrayModel->byte( i );
105 
106  switch( inputByteIndex )
107  {
108  case FirstByte:
109  // bits 7..2
110  textStream << xxmapByte( byte >> 2 );
111  // bits 1..0 -> 5..4 for next
112  bitsFromLastByte = (byte & 0x3) << 4;
113  inputByteIndex = SecondByte;
114  break;
115  case SecondByte:
116  // from last and bits 7..4 as 3..0 from this
117  textStream << xxmapByte( bitsFromLastByte | byte >> 4 );
118  // bits 3..0 -> 5..2 for next
119  bitsFromLastByte = (byte & 0xf) << 2;
120  inputByteIndex = ThirdByte;
121  break;
122  case ThirdByte:
123  // from last and bits 7..6 as 1..0 from this
124  textStream << xxmapByte( bitsFromLastByte | byte >> 6 );
125  // bits 5..0
126  textStream << xxmapByte( byte & 0x3F );
127  inputByteIndex = FirstByte;
128  ++inputGroupsPerLine;
129  if( inputGroupsPerLine >= maxXxInputGroupsPerLine && i<range.end() )
130  {
131  const int remainsCount = range.end() - i;
132  const int nextLineLength = qMin( remainsCount, xxInputLineLength );
133  textStream << '\n';
134  textStream << xxmapByte( nextLineLength );
135  inputGroupsPerLine = 0;
136  }
137  break;
138  }
139  }
140  const bool hasBitsLeft = ( inputByteIndex != FirstByte );
141  if( hasBitsLeft )
142  textStream << xxmapByte(bitsFromLastByte)
143  << xxpadding(inputByteIndex);
144  // footer
145  textStream << footer;
146 
147  return success;
148 }
149 
150 ByteArrayXxencodingStreamEncoder::~ByteArrayXxencodingStreamEncoder() {}
151 
152 }
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
abstractbytearraymodel.h
Kasten2::defaultxxInputLineLength
static const int defaultxxInputLineLength
Definition: bytearrayxxencodingstreamencoder.cpp:48
Kasten2::ByteArrayXxencodingStreamEncoder::~ByteArrayXxencodingStreamEncoder
virtual ~ByteArrayXxencodingStreamEncoder()
Definition: bytearrayxxencodingstreamencoder.cpp:150
Kasten2::ByteArrayXxencodingStreamEncoder::encodeDataToStream
virtual bool encodeDataToStream(QIODevice *device, const ByteArrayView *byteArrayView, const Okteta::AbstractByteArrayModel *byteArrayModel, const Okteta::AddressRange &range)
Definition: bytearrayxxencodingstreamencoder.cpp:72
Kasten2::xxInputLineLength
static const int xxInputLineLength
Definition: bytearrayxxencodingstreamencoder.cpp:49
KDE::NumberRange< Address, Size >
Kasten2::maxXxInputGroupsPerLine
static const int maxXxInputGroupsPerLine
Definition: bytearrayxxencodingstreamencoder.cpp:51
KDE::Range::start
T start() const
Definition: range.h:86
Kasten2::ByteArrayXxencodingStreamEncoder::ByteArrayXxencodingStreamEncoder
ByteArrayXxencodingStreamEncoder()
Definition: bytearrayxxencodingstreamencoder.cpp:67
Kasten2::ByteArrayXxencodingStreamEncoder::mSettings
XxencodingStreamEncoderSettings mSettings
Definition: bytearrayxxencodingstreamencoder.h:68
Okteta::Byte
unsigned char Byte
Definition: byte.h:29
Kasten2::ByteArrayXxencodingStreamEncoder::FirstByte
Definition: bytearrayxxencodingstreamencoder.h:51
Kasten2::AbstractByteArrayStreamEncoder
Definition: abstractbytearraystreamencoder.h:45
KDE::NumberRange::width
S width() const
Definition: numberrange.h:141
Kasten2::XxencodingStreamEncoderSettings::XxencodingStreamEncoderSettings
XxencodingStreamEncoderSettings()
Definition: bytearrayxxencodingstreamencoder.cpp:63
Kasten2::ByteArrayXxencodingStreamEncoder::ThirdByte
Definition: bytearrayxxencodingstreamencoder.h:51
KDE::Range::end
T end() const
Definition: range.h:88
Kasten2::ByteArrayXxencodingStreamEncoder::InputByteIndex
InputByteIndex
Definition: bytearrayxxencodingstreamencoder.h:51
bytearrayxxencodingstreamencoder.h
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::XxencodingStreamEncoderSettings::fileName
QString fileName
Definition: bytearrayxxencodingstreamencoder.h:42
Kasten2::xxencodeMap
static const char xxencodeMap[64]
Definition: bytearrayxxencodingstreamencoder.cpp:36
Kasten2::xxInputGroupLength
static const int xxInputGroupLength
Definition: bytearrayxxencodingstreamencoder.cpp:50
Kasten2::xxmapByte
static char xxmapByte(char byte)
Definition: bytearrayxxencodingstreamencoder.cpp:53
QIODevice
Kasten2::ByteArrayXxencodingStreamEncoder::SecondByte
Definition: bytearrayxxencodingstreamencoder.h:51
Kasten2::ByteArrayView
Definition: bytearrayview.h:51
Kasten2::xxpadding
static const char * xxpadding(ByteArrayXxencodingStreamEncoder::InputByteIndex index)
Definition: bytearrayxxencodingstreamencoder.cpp:55
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