• 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
  • base32
bytearraybase32streamencoder.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 "bytearraybase32streamencoder.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 base32ClassicEncodeMap[32] =
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', '2', '3', '4', '5', '6', '7'
42 };
43 static const char base32HexEncodeMap[32] =
44 {
45  '0', '1', '2', '3', '4', '5', '6', '7',
46  '8', '9', 'A', 'B', 'C', 'D', 'E', 'F',
47  'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N',
48  'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V'
49 };
50 static const char base32ZHexEncodeMap[32] =
51 {
52  'y', 'b', 'n', 'd', 'r', 'f', 'g', '8',
53  'e', 'j', 'k', 'm', 'c', 'p', 'q', 'x',
54  'o', 't', '1', 'u', 'w', 'i', 's', 'z',
55  'a', '3', '4', '5', 'h', '7', '6', '9'
56 };
57 
58 static const char* const base32PaddingData[4] =
59 {
60  "======",
61  "====",
62  "===",
63  "="
64 };
65 
66 static inline const char* base32Padding( ByteArrayBase32StreamEncoder::InputByteIndex index )
67 {
68  return base32PaddingData[index - 1];
69 }
70 static inline const char* noPadding( ByteArrayBase32StreamEncoder::InputByteIndex index )
71 {
72  Q_UNUSED(index);
73 
74  return "";
75 }
76 
77 struct Base32EncodingData
78 {
79  const char* const encodeMap;
80  const char* (*padding)( ByteArrayBase32StreamEncoder::InputByteIndex );
81 };
82 
83 static const Base32EncodingData
84 base32EncodingData[3] =
85 {
86  {base32ClassicEncodeMap, &base32Padding},
87  {base32HexEncodeMap, &base32Padding},
88  {base32ZHexEncodeMap, &noPadding}
89 };
90 
91 
92 Base32StreamEncoderSettings::Base32StreamEncoderSettings()
93  : algorithmId( ClassicId )
94 {}
95 
96 ByteArrayBase32StreamEncoder::ByteArrayBase32StreamEncoder()
97  : AbstractByteArrayStreamEncoder( i18nc("name of the encoding target","Base32"), QString::fromLatin1("text/x-base32") )
98 {}
99 
100 
101 bool ByteArrayBase32StreamEncoder::encodeDataToStream( QIODevice* device,
102  const ByteArrayView* byteArrayView,
103  const Okteta::AbstractByteArrayModel* byteArrayModel,
104  const Okteta::AddressRange& range )
105 {
106  Q_UNUSED( byteArrayView );
107 
108  bool success = true;
109 
110  // encode
111  QTextStream textStream( device );
112 
113  // prepare
114  const char* const base32EncodeMap = base32EncodingData[mSettings.algorithmId].encodeMap;
115  const char* (*base32Padding)( InputByteIndex ) = base32EncodingData[mSettings.algorithmId].padding;
116 
117  InputByteIndex inputByteIndex = FirstByte;
118  int outputGroupsPerLine = 0;
119  unsigned char bitsFromLastByte;
120 
121  for( Okteta::Address i=range.start(); i<=range.end(); ++i )
122  {
123  const Okteta::Byte byte = byteArrayModel->byte( i );
124 
125  switch( inputByteIndex )
126  {
127  case FirstByte:
128  // bits 7..3
129  textStream << base32EncodeMap[( byte >> 3 )];
130  // bits 2..0 -> 4..2 for next
131  bitsFromLastByte = (byte & 0x7) << 2;
132  inputByteIndex = SecondByte;
133  break;
134  case SecondByte:
135  // from last and bits 7..6 as 1..0 from this
136  textStream << base32EncodeMap[( bitsFromLastByte | byte >> 6 )];
137  // bits 5..1 as 4..0
138  textStream << base32EncodeMap[( byte & 0x3E )>>1];
139  // bits 0 -> 4 for next
140  bitsFromLastByte = (byte & 0x1) << 4;
141  inputByteIndex = ThirdByte;
142  break;
143  case ThirdByte:
144  // from last and bits 7..4 as 3..0 from this
145  textStream << base32EncodeMap[( bitsFromLastByte | byte >> 4 )];
146  // bits 3..0 -> 4..1 for next
147  bitsFromLastByte = (byte & 0xF) << 1;
148  inputByteIndex = FourthByte;
149  break;
150  case FourthByte:
151  // from last and bit 7 as 0 from this
152  textStream << base32EncodeMap[( bitsFromLastByte | byte >> 7 )];
153  // bits 6..2 as 4..0
154  textStream << base32EncodeMap[( byte & 0x7C )>>2];
155  // bits 1..0 -> 4..3 for next
156  bitsFromLastByte = (byte & 0x3) << 3;
157  inputByteIndex = FifthByte;
158  break;
159  case FifthByte:
160  // from last and bits 7..5 as 2..0 from this
161  textStream << base32EncodeMap[( bitsFromLastByte | byte >> 5 )];
162  // bits 4..0
163  textStream << base32EncodeMap[( byte & 0x1F )];
164  inputByteIndex = FirstByte;
165  ++outputGroupsPerLine;
166  if( outputGroupsPerLine >= maxOutputGroupsPerLine && i<range.end() )
167  {
168  textStream << "\r\n";
169  outputGroupsPerLine = 0;
170  }
171  break;
172  }
173  }
174  const bool hasBitsLeft = ( inputByteIndex != FirstByte );
175  if( hasBitsLeft )
176  textStream << base32EncodeMap[bitsFromLastByte]
177  << base32Padding(inputByteIndex);
178 
179  return success;
180 }
181 
182 ByteArrayBase32StreamEncoder::~ByteArrayBase32StreamEncoder() {}
183 
184 }
Kasten2::ByteArrayBase32StreamEncoder::ByteArrayBase32StreamEncoder
ByteArrayBase32StreamEncoder()
Definition: bytearraybase32streamencoder.cpp:96
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::ByteArrayBase32StreamEncoder::maxOutputGroupsPerLine
static const int maxOutputGroupsPerLine
Definition: bytearraybase32streamencoder.h:58
Kasten2::ByteArrayBase32StreamEncoder::ThirdByte
Definition: bytearraybase32streamencoder.h:60
KDE::NumberRange< Address, Size >
KDE::Range::start
T start() const
Definition: range.h:86
bytearraybase32streamencoder.h
Kasten2::ByteArrayBase32StreamEncoder::~ByteArrayBase32StreamEncoder
virtual ~ByteArrayBase32StreamEncoder()
Definition: bytearraybase32streamencoder.cpp:182
Okteta::Byte
unsigned char Byte
Definition: byte.h:29
Kasten2::ByteArrayBase32StreamEncoder::InputByteIndex
InputByteIndex
Definition: bytearraybase32streamencoder.h:60
Kasten2::Base32StreamEncoderSettings::Base32StreamEncoderSettings
Base32StreamEncoderSettings()
Definition: bytearraybase32streamencoder.cpp:92
Kasten2::Base32StreamEncoderSettings::algorithmId
AlgorithmId algorithmId
Definition: bytearraybase32streamencoder.h:45
Kasten2::AbstractByteArrayStreamEncoder
Definition: abstractbytearraystreamencoder.h:45
Kasten2::base32HexEncodeMap
static const char base32HexEncodeMap[32]
Definition: bytearraybase32streamencoder.cpp:43
Kasten2::noPadding
static const char * noPadding(ByteArrayBase32StreamEncoder::InputByteIndex index)
Definition: bytearraybase32streamencoder.cpp:70
Kasten2::base32ZHexEncodeMap
static const char base32ZHexEncodeMap[32]
Definition: bytearraybase32streamencoder.cpp:50
Kasten2::base32EncodingData
static const Base32EncodingData base32EncodingData[3]
Definition: bytearraybase32streamencoder.cpp:84
Kasten2::ByteArrayBase32StreamEncoder::mSettings
Base32StreamEncoderSettings mSettings
Definition: bytearraybase32streamencoder.h:77
KDE::Range::end
T end() const
Definition: range.h:88
Kasten2::ByteArrayBase32StreamEncoder::encodeDataToStream
virtual bool encodeDataToStream(QIODevice *device, const ByteArrayView *byteArrayView, const Okteta::AbstractByteArrayModel *byteArrayModel, const Okteta::AddressRange &range)
Definition: bytearraybase32streamencoder.cpp:101
Kasten2::base32PaddingData
static const char *const base32PaddingData[4]
Definition: bytearraybase32streamencoder.cpp:58
Kasten2::ByteArrayBase32StreamEncoder::FifthByte
Definition: bytearraybase32streamencoder.h:60
Kasten2::base32ClassicEncodeMap
static const char base32ClassicEncodeMap[32]
Definition: bytearraybase32streamencoder.cpp:36
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::base32Padding
static const char * base32Padding(ByteArrayBase32StreamEncoder::InputByteIndex index)
Definition: bytearraybase32streamencoder.cpp:66
Kasten2::ByteArrayBase32StreamEncoder::FirstByte
Definition: bytearraybase32streamencoder.h:60
QIODevice
Kasten2::ByteArrayView
Definition: bytearrayview.h:51
Kasten2::ByteArrayBase32StreamEncoder::SecondByte
Definition: bytearraybase32streamencoder.h:60
Kasten2::ByteArrayBase32StreamEncoder::FourthByte
Definition: bytearraybase32streamencoder.h:60
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