• 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
  • base85
bytearraybase85streamencoder.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 "bytearraybase85streamencoder.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 inline void streamEncoded( QTextStream& textStream, int& outputBytesPerLine,
37  quint32 tuple, int inputByteCount )
38 {
39  // radix85 values, most significant first
40  char data[5];
41 
42  for( int i=4; i>=0; --i )
43  {
44  // TODO: find an efficient bit manipulating algorithm
45  data[i] = tuple % 85;
46  tuple /= 85;
47  }
48 
49  // output inputByteCount+1 from radix85 values
50  for( int i = 0; i<=inputByteCount; ++i )
51  {
52  textStream << (char)(data[i] + 33);
53  ++outputBytesPerLine;
54  if( outputBytesPerLine >= ByteArrayBase85StreamEncoder::maxOutputBytesPerLine )
55  {
56  textStream << '\n';
57  outputBytesPerLine = 0;
58  }
59  }
60 }
61 
62 
63 // TODO: for now this is just the Adobe/Ascii85 implementation, so present as that
64 // later also add btoa with different version, e.g. 4.2 added a "y" for 4 spaces
65 ByteArrayBase85StreamEncoder::ByteArrayBase85StreamEncoder()
66  : AbstractByteArrayStreamEncoder( i18nc("name of the encoding target","Ascii85"), QString::fromLatin1("text/x-ascii85") )
67 {}
68 
69 
70 bool ByteArrayBase85StreamEncoder::encodeDataToStream( QIODevice* device,
71  const ByteArrayView* byteArrayView,
72  const Okteta::AbstractByteArrayModel* byteArrayModel,
73  const Okteta::AddressRange& range )
74 {
75  Q_UNUSED( byteArrayView );
76 
77  bool success = true;
78 
79  // encode
80  QTextStream textStream( device );
81 
82  // prepare
83  InputByteIndex inputByteIndex = FirstByte;
84  quint32 tuple = 0;
85 
86  // header
87  int outputBytesPerLine = 2;
88  textStream << "<~";
89 
90  for( Okteta::Address i=range.start(); i<=range.end(); ++i )
91  {
92  const Okteta::Byte byte = byteArrayModel->byte( i );
93 
94  switch( inputByteIndex )
95  {
96  case FirstByte:
97  tuple |= (byte << 24);
98  inputByteIndex = SecondByte;
99  break;
100  case SecondByte:
101  tuple |= (byte << 16);
102  inputByteIndex = ThirdByte;
103  break;
104  case ThirdByte:
105  tuple |= (byte << 8);
106  inputByteIndex = FourthByte;
107  break;
108  case FourthByte:
109  tuple |= byte;
110  if( tuple == 0 )
111  {
112  textStream << 'z';
113  ++outputBytesPerLine;
114  if( outputBytesPerLine >= maxOutputBytesPerLine )
115  {
116  textStream << '\n';
117  outputBytesPerLine = 0;
118  }
119  }
120  else
121  streamEncoded( textStream, outputBytesPerLine, tuple, inputByteIndex+1 );
122  tuple = 0;
123  inputByteIndex = FirstByte;
124  break;
125  }
126  }
127  const bool hasBitsLeft = ( inputByteIndex != FirstByte );
128  if( hasBitsLeft )
129  streamEncoded( textStream, outputBytesPerLine, tuple, inputByteIndex );
130 
131  // footer
132  if( outputBytesPerLine + 2 > maxOutputBytesPerLine )
133  textStream << '\n';
134  textStream << "~>\n";
135 
136  return success;
137 }
138 
139 ByteArrayBase85StreamEncoder::~ByteArrayBase85StreamEncoder() {}
140 
141 }
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
KDE::NumberRange< Address, Size >
KDE::Range::start
T start() const
Definition: range.h:86
Kasten2::ByteArrayBase85StreamEncoder::FirstByte
Definition: bytearraybase85streamencoder.h:47
Okteta::Byte
unsigned char Byte
Definition: byte.h:29
Kasten2::ByteArrayBase85StreamEncoder::~ByteArrayBase85StreamEncoder
virtual ~ByteArrayBase85StreamEncoder()
Definition: bytearraybase85streamencoder.cpp:139
Kasten2::AbstractByteArrayStreamEncoder
Definition: abstractbytearraystreamencoder.h:45
Kasten2::ByteArrayBase85StreamEncoder::SecondByte
Definition: bytearraybase85streamencoder.h:47
Kasten2::ByteArrayBase85StreamEncoder::encodeDataToStream
virtual bool encodeDataToStream(QIODevice *device, const ByteArrayView *byteArrayView, const Okteta::AbstractByteArrayModel *byteArrayModel, const Okteta::AddressRange &range)
Definition: bytearraybase85streamencoder.cpp:70
KDE::Range::end
T end() const
Definition: range.h:88
Kasten2::ByteArrayBase85StreamEncoder::maxOutputBytesPerLine
static const int maxOutputBytesPerLine
Definition: bytearraybase85streamencoder.h:45
Kasten2::ByteArrayBase85StreamEncoder::FourthByte
Definition: bytearraybase85streamencoder.h:47
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::ByteArrayBase85StreamEncoder::ThirdByte
Definition: bytearraybase85streamencoder.h:47
bytearraybase85streamencoder.h
Kasten2::ByteArrayBase85StreamEncoder::ByteArrayBase85StreamEncoder
ByteArrayBase85StreamEncoder()
Definition: bytearraybase85streamencoder.cpp:65
Kasten2::ByteArrayBase85StreamEncoder::InputByteIndex
InputByteIndex
Definition: bytearraybase85streamencoder.h:47
char
QIODevice
Kasten2::ByteArrayView
Definition: bytearrayview.h:51
Kasten2::streamEncoded
static void streamEncoded(QTextStream &textStream, int &outputBytesPerLine, quint32 tuple, int inputByteCount)
Definition: bytearraybase85streamencoder.cpp:36
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