3 /// Data filter class, to morph PPP data into something that
4 /// the Blackberry / Rogers / ISP can handle.
5 /// This logic is based partly on XmBlackBerry's
6 /// gprs_protocol_fix.c program.
10 Copyright (C) 2008-2012, Net Direct Inc. (http://www.netdirect.ca/)
12 This program is free software; you can redistribute it and/or modify
13 it under the terms of the GNU General Public License as published by
14 the Free Software Foundation; either version 2 of the License, or
15 (at your option) any later version.
17 This program is distributed in the hope that it will be useful,
18 but WITHOUT ANY WARRANTY; without even the implied warranty of
19 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
21 See the GNU General Public License in the COPYING file at the
22 root directory of this project for more details.
25 #include "pppfilter.h"
30 //////////////////////////////////////////////////////////////////////////////
33 PppFilter::PppFilter()
42 /// Copy PPP data from src to dest, creating needed space in dest,
43 /// and inserting missing 0x7e characters wherever they are detected.
44 /// Starts writing at the start of the dest buffer + destoffset.
46 void PppFilter::Filter(Data
&dest
, const Data
&src
, unsigned int destoffset
)
48 const unsigned char *b
= src
.GetData(), *e
= src
.GetData() + src
.GetSize();
49 size_t needed
= src
.GetSize() / 2 * 3 + 4 + destoffset
; // worst case
50 unsigned char *buf
= dest
.GetBuffer(needed
) + destoffset
;
51 unsigned char *put
= buf
;
54 // if last character was 0x7e, then next one must be,
55 // or else we insert it ourselves
56 if( m_last
== 0x7e ) {
64 // copy all non-0x7e chars verbatim
65 while( b
!= e
&& *b
!= 0x7e ) {
69 if( b
!= e
) { // if b!=e then *b == 0x7e and must keep going
75 dest
.ReleaseBuffer(put
- buf
+ destoffset
);
81 /// If PPP mode has not been detected, just return the data buffer.
82 /// If in PPP mode, then filter data into internal write buffer,
83 /// inserting any missing 0x7e characters and return reference
84 /// to internal write buffer.
86 const Data
& PppFilter::Write(const Data
&data
)
88 if( data
.GetSize() == 0 )
89 return data
; // nothing to do
91 const unsigned char *b
= data
.GetData();
99 // not in ppp mode yet, so just pass the buffer
100 // straight back to the caller
105 Filter(m_writeBuf
, data
, 0);
110 // Write (with prepend)
112 /// Same as Write(data), but makes sure that prepend bytes are available
113 /// at the beginning of the returned buffer.
114 /// If not in PPP mode, the extra bytes are still provided.
116 Data
& PppFilter::Write(const Data
&data
, unsigned int prepend
)
118 const unsigned char *b
= data
.GetData(), *e
= data
.GetData() + data
.GetSize();
121 if( b
!= e
&& *b
== 0x7e ) {
126 // make space, copy, return
127 unsigned int size
= data
.GetSize() + prepend
;
128 unsigned char *buf
= m_writeBuf
.GetBuffer(size
);
129 memcpy(&buf
[prepend
], data
.GetData(), data
.GetSize());
130 m_writeBuf
.ReleaseBuffer(size
);
135 Filter(m_writeBuf
, data
, prepend
);