• 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
  • uuencoding
bytearrayuuencodingstreamencoder.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 "bytearrayuuencodingstreamencoder.h"
24 
25 // lib
26 #include "../base64/bytearraybase64streamencoder.h"
27 // Okteta core
28 #include <abstractbytearraymodel.h>
29 // KDE
30 #include <KLocale>
31 // Qt
32 #include <QtCore/QTextStream>
33 
34 
35 namespace Kasten2
36 {
37 
38 static const int defaultUuInputLineLength = 45;
39 static const int uuInputLineLength = defaultUuInputLineLength;
40 static const int uuInputGroupLength = 3;
41 static const int maxInputGroupsPerLine = uuInputLineLength/uuInputGroupLength;
42 
43 
44 static inline char uumapByteHistorical( char byte ) { return (byte > 0) ? (byte + 32) : '`'; }
45 static inline char uumapByteBase64( char byte ) { return base64EncodeMap[(int)byte]; }
46 
47 struct UumapEncodeData
48 {
49  char (*mapByte)( char );
50  const char* header;
51  const char* footer;
52  const char* paddingData[2];
53  bool hasLength;
54 
55  inline const char* padding( ByteArrayUuencodingStreamEncoder::InputByteIndex index ) const
56  {
57  return paddingData[(int)(index) - 1];
58  }
59 };
60 
61 static const UumapEncodeData historicalUumapEncodeData =
62 {
63  &uumapByteHistorical,
64  "begin",
65  "\n`\nend\n",
66  {"``","`"},
67  true
68 };
69 
70 static const UumapEncodeData base64UumapEncodeData =
71 {
72  &uumapByteBase64,
73  "begin-base64",
74  "\n====\n",
75  {"==","="},
76  false
77 };
78 
79 
80 UuencodingStreamEncoderSettings::UuencodingStreamEncoderSettings()
81  : fileName( QString::fromLatin1("okteta-export")), algorithmId( Base64Id )
82 {}
83 
84 ByteArrayUuencodingStreamEncoder::ByteArrayUuencodingStreamEncoder()
85  : AbstractByteArrayStreamEncoder( i18nc("name of the encoding target","Uuencoding"), QString::fromLatin1("text/x-uuencode") )
86 {}
87 
88 
89 bool ByteArrayUuencodingStreamEncoder::encodeDataToStream( QIODevice* device,
90  const ByteArrayView* byteArrayView,
91  const Okteta::AbstractByteArrayModel* byteArrayModel,
92  const Okteta::AddressRange& range )
93 {
94  Q_UNUSED( byteArrayView );
95 
96  bool success = true;
97 
98  // encode
99  QTextStream textStream( device );
100 
101  // prepare
102  InputByteIndex inputByteIndex = FirstByte;
103  int inputGroupsPerLine = 0;
104  unsigned char bitsFromLastByte;
105 
106  const UumapEncodeData* encodeData =
107  (mSettings.algorithmId == UuencodingStreamEncoderSettings::HistoricalId ) ?
108  &historicalUumapEncodeData :
109  /* else */
110  &base64UumapEncodeData;
111 
112  // header
113  textStream << encodeData->header << " 644 " << mSettings.fileName.toLatin1();
114 
115  const int firstLineLength = qMin( range.width(), uuInputLineLength );
116  if( firstLineLength > 0 )
117  {
118  textStream << '\n';
119  if( encodeData->hasLength )
120  textStream << encodeData->mapByte( firstLineLength );
121  }
122 
123  for( Okteta::Address i=range.start(); i<=range.end(); ++i )
124  {
125  const Okteta::Byte byte = byteArrayModel->byte( i );
126 
127  switch( inputByteIndex )
128  {
129  case FirstByte:
130  // bits 7..2
131  textStream << encodeData->mapByte( byte >> 2 );
132  // bits 1..0 -> 5..4 for next
133  bitsFromLastByte = (byte & 0x3) << 4;
134  inputByteIndex = SecondByte;
135  break;
136  case SecondByte:
137  // from last and bits 7..4 as 3..0 from this
138  textStream << encodeData->mapByte( bitsFromLastByte | byte >> 4 );
139  // bits 3..0 -> 5..2 for next
140  bitsFromLastByte = (byte & 0xf) << 2;
141  inputByteIndex = ThirdByte;
142  break;
143  case ThirdByte:
144  // from last and bits 7..6 as 1..0 from this
145  textStream << encodeData->mapByte( bitsFromLastByte | byte >> 6 );
146  // bits 5..0
147  textStream << encodeData->mapByte( byte & 0x3F );
148  inputByteIndex = FirstByte;
149  ++inputGroupsPerLine;
150  if( inputGroupsPerLine >= maxInputGroupsPerLine && i<range.end() )
151  {
152  const int remainsCount = range.end() - i;
153  const int nextLineLength = qMin( remainsCount, uuInputLineLength );
154  textStream << '\n';
155  if( encodeData->hasLength )
156  textStream << encodeData->mapByte( nextLineLength );
157  inputGroupsPerLine = 0;
158  }
159  break;
160  }
161  }
162  const bool hasBitsLeft = ( inputByteIndex != FirstByte );
163  if( hasBitsLeft )
164  textStream << encodeData->mapByte(bitsFromLastByte)
165  << encodeData->padding(inputByteIndex);
166  // footer
167  textStream << encodeData->footer;
168 
169  return success;
170 }
171 
172 ByteArrayUuencodingStreamEncoder::~ByteArrayUuencodingStreamEncoder() {}
173 
174 }
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::ByteArrayUuencodingStreamEncoder::SecondByte
Definition: bytearrayuuencodingstreamencoder.h:55
Kasten2::UuencodingStreamEncoderSettings::UuencodingStreamEncoderSettings
UuencodingStreamEncoderSettings()
Definition: bytearrayuuencodingstreamencoder.cpp:80
Kasten2::ByteArrayUuencodingStreamEncoder::FirstByte
Definition: bytearrayuuencodingstreamencoder.h:55
KDE::NumberRange< Address, Size >
KDE::Range::start
T start() const
Definition: range.h:86
Kasten2::ByteArrayUuencodingStreamEncoder::mSettings
UuencodingStreamEncoderSettings mSettings
Definition: bytearrayuuencodingstreamencoder.h:72
Okteta::Byte
unsigned char Byte
Definition: byte.h:29
Kasten2::AbstractByteArrayStreamEncoder
Definition: abstractbytearraystreamencoder.h:45
Kasten2::uumapByteBase64
static char uumapByteBase64(char byte)
Definition: bytearrayuuencodingstreamencoder.cpp:45
KDE::NumberRange::width
S width() const
Definition: numberrange.h:141
Kasten2::uumapByteHistorical
static char uumapByteHistorical(char byte)
Definition: bytearrayuuencodingstreamencoder.cpp:44
Kasten2::historicalUumapEncodeData
static const UumapEncodeData historicalUumapEncodeData
Definition: bytearrayuuencodingstreamencoder.cpp:61
Kasten2::ByteArrayUuencodingStreamEncoder::InputByteIndex
InputByteIndex
Definition: bytearrayuuencodingstreamencoder.h:55
bytearrayuuencodingstreamencoder.h
KDE::Range::end
T end() const
Definition: range.h:88
Kasten2::ByteArrayUuencodingStreamEncoder::~ByteArrayUuencodingStreamEncoder
virtual ~ByteArrayUuencodingStreamEncoder()
Definition: bytearrayuuencodingstreamencoder.cpp:172
Kasten2::defaultUuInputLineLength
static const int defaultUuInputLineLength
Definition: bytearrayuuencodingstreamencoder.cpp:38
Kasten2::UuencodingStreamEncoderSettings::HistoricalId
Definition: bytearrayuuencodingstreamencoder.h:40
Kasten2::ByteArrayUuencodingStreamEncoder::encodeDataToStream
virtual bool encodeDataToStream(QIODevice *device, const ByteArrayView *byteArrayView, const Okteta::AbstractByteArrayModel *byteArrayModel, const Okteta::AddressRange &range)
Definition: bytearrayuuencodingstreamencoder.cpp:89
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::base64UumapEncodeData
static const UumapEncodeData base64UumapEncodeData
Definition: bytearrayuuencodingstreamencoder.cpp:70
Kasten2::UuencodingStreamEncoderSettings::fileName
QString fileName
Definition: bytearrayuuencodingstreamencoder.h:45
Kasten2::base64EncodeMap
const char base64EncodeMap[64]
Definition: bytearraybase64streamencoder.cpp:36
Kasten2::maxInputGroupsPerLine
static const int maxInputGroupsPerLine
Definition: bytearrayuuencodingstreamencoder.cpp:41
Kasten2::ByteArrayUuencodingStreamEncoder::ThirdByte
Definition: bytearrayuuencodingstreamencoder.h:55
Kasten2::uuInputGroupLength
static const int uuInputGroupLength
Definition: bytearrayuuencodingstreamencoder.cpp:40
Kasten2::uuInputLineLength
static const int uuInputLineLength
Definition: bytearrayuuencodingstreamencoder.cpp:39
char
QIODevice
Kasten2::ByteArrayView
Definition: bytearrayview.h:51
Kasten2::UuencodingStreamEncoderSettings::algorithmId
AlgorithmId algorithmId
Definition: bytearrayuuencodingstreamencoder.h:46
Kasten2::ByteArrayUuencodingStreamEncoder::ByteArrayUuencodingStreamEncoder
ByteArrayUuencodingStreamEncoder()
Definition: bytearrayuuencodingstreamencoder.cpp:84
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