• 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
  • base64
bytearraybase64streamencoder.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 2009-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 "bytearraybase64streamencoder.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 const char base64EncodeMap[64] =
37 {
38  'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H',
39  'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P',
40  'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X',
41  'Y', 'Z', 'a', 'b', 'c', 'd', 'e', 'f',
42  'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n',
43  'o', 'p', 'q', 'r', 's', 't', 'u', 'v',
44  'w', 'x', 'y', 'z', '0', '1', '2', '3',
45  '4', '5', '6', '7', '8', '9', '+', '/'
46 };
47 
48 static const char* const base64PaddingData[2] =
49 {
50  "==",
51  "="
52 };
53 static inline const char* base64Padding( ByteArrayBase64StreamEncoder::InputByteIndex index )
54 {
55  return base64PaddingData[(int)(index) - 1];
56 }
57 
58 
59 ByteArrayBase64StreamEncoder::ByteArrayBase64StreamEncoder()
60  : AbstractByteArrayStreamEncoder( i18nc("name of the encoding target","Base64"), QString::fromLatin1("application/base64") )
61 {}
62 
63 
64 bool ByteArrayBase64StreamEncoder::encodeDataToStream( QIODevice* device,
65  const ByteArrayView* byteArrayView,
66  const Okteta::AbstractByteArrayModel* byteArrayModel,
67  const Okteta::AddressRange& range )
68 {
69  Q_UNUSED( byteArrayView );
70 
71  bool success = true;
72 
73  // encode
74  QTextStream textStream( device );
75 
76  // prepare
77  InputByteIndex inputByteIndex = FirstByte;
78  int outputGroupsPerLine = 0;
79  unsigned char bitsFromLastByte;
80 
81  for( Okteta::Address i=range.start(); i<=range.end(); ++i )
82  {
83  const Okteta::Byte byte = byteArrayModel->byte( i );
84 
85  switch( inputByteIndex )
86  {
87  case FirstByte:
88  // bits 7..2
89  textStream << base64EncodeMap[( byte >> 2 )];
90  // bits 1..0 -> 5..4 for next
91  bitsFromLastByte = (byte & 0x3) << 4;
92  inputByteIndex = SecondByte;
93  break;
94  case SecondByte:
95  // from last and bits 7..4 as 3..0 from this
96  textStream << base64EncodeMap[( bitsFromLastByte | byte >> 4 )];
97  // bits 3..0 -> 5..2 for next
98  bitsFromLastByte = (byte & 0xf) << 2;
99  inputByteIndex = ThirdByte;
100  break;
101  case ThirdByte:
102  // from last and bits 7..6 as 1..0 from this
103  textStream << base64EncodeMap[( bitsFromLastByte | byte >> 6 )];
104  // bits 5..0
105  textStream << base64EncodeMap[( byte & 0x3F )];
106  inputByteIndex = FirstByte;
107  ++outputGroupsPerLine;
108  if( outputGroupsPerLine >= maxOutputGroupsPerLine && i<range.end() )
109  {
110  textStream << "\r\n";
111  outputGroupsPerLine = 0;
112  }
113  break;
114  }
115  }
116  const bool hasBitsLeft = ( inputByteIndex != FirstByte );
117  if( hasBitsLeft )
118  textStream << base64EncodeMap[bitsFromLastByte]
119  << base64Padding(inputByteIndex);
120 
121  return success;
122 }
123 
124 ByteArrayBase64StreamEncoder::~ByteArrayBase64StreamEncoder() {}
125 
126 }
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::ByteArrayBase64StreamEncoder::~ByteArrayBase64StreamEncoder
virtual ~ByteArrayBase64StreamEncoder()
Definition: bytearraybase64streamencoder.cpp:124
Kasten2::base64Padding
static const char * base64Padding(ByteArrayBase64StreamEncoder::InputByteIndex index)
Definition: bytearraybase64streamencoder.cpp:53
KDE::NumberRange< Address, Size >
KDE::Range::start
T start() const
Definition: range.h:86
Okteta::Byte
unsigned char Byte
Definition: byte.h:29
Kasten2::AbstractByteArrayStreamEncoder
Definition: abstractbytearraystreamencoder.h:45
Kasten2::ByteArrayBase64StreamEncoder::maxOutputGroupsPerLine
static const int maxOutputGroupsPerLine
Definition: bytearraybase64streamencoder.h:47
Kasten2::ByteArrayBase64StreamEncoder::encodeDataToStream
virtual bool encodeDataToStream(QIODevice *device, const ByteArrayView *byteArrayView, const Okteta::AbstractByteArrayModel *byteArrayModel, const Okteta::AddressRange &range)
Definition: bytearraybase64streamencoder.cpp:64
KDE::Range::end
T end() const
Definition: range.h:88
Kasten2::ByteArrayBase64StreamEncoder::SecondByte
Definition: bytearraybase64streamencoder.h:49
Kasten2::base64PaddingData
static const char *const base64PaddingData[2]
Definition: bytearraybase64streamencoder.cpp:48
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::base64EncodeMap
const char base64EncodeMap[64]
Definition: bytearraybase64streamencoder.cpp:36
bytearraybase64streamencoder.h
Kasten2::ByteArrayBase64StreamEncoder::FirstByte
Definition: bytearraybase64streamencoder.h:49
Kasten2::ByteArrayBase64StreamEncoder::ThirdByte
Definition: bytearraybase64streamencoder.h:49
Kasten2::ByteArrayBase64StreamEncoder::ByteArrayBase64StreamEncoder
ByteArrayBase64StreamEncoder()
Definition: bytearraybase64streamencoder.cpp:59
QIODevice
Kasten2::ByteArrayView
Definition: bytearrayview.h:51
Kasten2::ByteArrayBase64StreamEncoder::InputByteIndex
InputByteIndex
Definition: bytearraybase64streamencoder.h:49
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