summaryrefslogtreecommitdiffstats
path: root/src/jsonbuffer.cpp
blob: fbabb85b7b405de4a4716b86a922101e855faddd (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
/****************************************************************************
**
** Copyright (C) 2011 Nokia Corporation and/or its subsidiary(-ies).
** All rights reserved.
** Contact: Nokia Corporation (qt-info@nokia.com)
**
** This file is part of the QtAddOn.JsonStream module of the Qt Toolkit.
**
** $QT_BEGIN_LICENSE:LGPL$
** GNU Lesser General Public License Usage
** This file may be used under the terms of the GNU Lesser General Public
** License version 2.1 as published by the Free Software Foundation and
** appearing in the file LICENSE.LGPL included in the packaging of this
** file. Please review the following information to ensure the GNU Lesser
** General Public License version 2.1 requirements will be met:
** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
**
** In addition, as a special exception, Nokia gives you certain additional
** rights. These rights are described in the Nokia Qt LGPL Exception
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
**
** GNU General Public License Usage
** Alternatively, this file may be used under the terms of the GNU General
** Public License version 3.0 as published by the Free Software Foundation
** and appearing in the file LICENSE.GPL included in the packaging of this
** file. Please review the following information to ensure the GNU General
** Public License version 3.0 requirements will be met:
** http://www.gnu.org/copyleft/gpl.html.
**
** Other Usage
** Alternatively, this file may be used in accordance with the terms and
** conditions contained in a signed written agreement between you and Nokia.
**
**
**
**
**
** $QT_END_LICENSE$
**
****************************************************************************/

#include <QDebug>
#include <QtEndian>
#include <QJsonDocument>
#include <QTextCodec>
#include <QMutexLocker>

#include "jsonbuffer_p.h"
#include "qjsondocument.h"
#include "qjsonobject.h"
#include "bson/qt-bson_p.h"

QT_BEGIN_NAMESPACE_JSONSTREAM

template <typename T>
inline bool isjsonws(T c)
{
    return c == '\n' || c == ' ' || c == '\t' || c == '\r';
}

/*!
  \class JsonBuffer
  \brief The JsonBuffer class parses data received into appropriate Json messages

  The JsonBuffer class wraps an internal buffer.  As you append
  data, the JsonBuffer object parses for received Json objects and
  raises an appropriate signal for each received object.

  \warning This class does not support JSON arrays.  Arrays that are received will be ignored.
*/

/*!
  Construct a JsonBuffer object with the given \a parent
*/

JsonBuffer::JsonBuffer(QObject *parent)
    : QObject(parent)
    , mFormat(FormatUndefined)
    , mParserState(ParseNormal)
    , mParserDepth(0)
    , mParserOffset(0)
    , mParserStartOffset(-1)
    , mEmittedReadyRead(false)
    , mMessageAvailable(false)
    , mMessageSize(0)
    , mEnabled(true)
    , mThreadProtection(false)
{
}

/*!
    \fn bool JsonBuffer::isEnabled() const

    Returns true if \l{readyReadMessage()} signal notifier is enabled; otherwise returns false.

    \sa setEnabled(), readyReadMessage()
*/

/*!
    \fn void JsonBuffer::setEnabled(bool enable)

    If \a enable is true, \l{readyReadMessage()} signal notifier is enabled;
    otherwise the notifier is disabled.

    The notifier is enabled by default, i.e. it emits the \l{readyReadMessage()}
    signal whenever a new message is ready.

    The notifier should normally be disabled while user is reading existing messages.

    \sa isEnabled(), readyReadMessage()
*/

/*!
    \fn int JsonBuffer::size() const

    Returns the size of the buffer in bytes.
*/

/*!
  Append the contents of a byte array \a data onto the buffer.
  During the execution of this function, the \l{readyReadMessage()}
  signal may be raised.
*/

void JsonBuffer::append(const QByteArray& data)
{
    {
        QScopedPointer<QMutexLocker> locker(createLocker());
        mBuffer.append(data.data(), data.size());
    }
    if (0 < size())
        processMessages();
}

/*!
  Append the \a data pointer with length \a len onto the JsonBuffer.
  During the execution of this function, the \l{readyReadMessage()}
  signal may be raised.
*/

void JsonBuffer::append(const char *data, int len)
{
    {
        QScopedPointer<QMutexLocker> locker(createLocker());
        mBuffer.append(data, len);
    }
    if (0 < size())
        processMessages();
}

/*!
  Copy data from a file descriptor \a fd into the buffer.
  This function tries to eliminate extra data copy operations.
  It assumes that the file descriptor is ready to read and
  it does not try to read all of the data.

  Returns the number of bytes read or -1 for an error condition.
 */

int JsonBuffer::copyFromFd(int fd)
{
    const int maxcopy = 1024;
    QScopedPointer<QMutexLocker> locker(createLocker());
    uint oldSize = mBuffer.size();
    mBuffer.resize(oldSize + maxcopy);
    int n = ::read(fd, mBuffer.data()+oldSize, maxcopy);
    if (n > 0) {
        mBuffer.resize(oldSize+n);
        if (!locker.isNull())
            locker->unlock();
        processMessages();
    }
    else
        mBuffer.resize(oldSize);
    return n;
}

/*!
    Clear the contents of the buffer.
 */

void JsonBuffer::clear()
{
    QScopedPointer<QMutexLocker> locker(createLocker());
    mBuffer.clear();
    resetParser();
}

/*!
  \internal
*/

bool JsonBuffer::scanUtf( int c )
{
    switch (mParserState) {
    case ParseNormal:
        if ( c == '{' ) {
            if ( mParserDepth == 0 )
                mParserStartOffset = mParserOffset;
            mParserDepth += 1;
        }
        else if ( c == '}' && mParserDepth > 0 ) {
            mParserDepth -= 1;
            if ( mParserDepth == 0 ) {
                return true;
            }
        }
        else if ( c == '"' ) {
            mParserState = ParseInString;
        }
        break;
    case ParseInString:
        if ( c == '"' ) {
            mParserState = ParseNormal;
        } else if ( c == '\\' ) {
            mParserState = ParseInBackslash;
        }
        break;
    case ParseInBackslash:
        mParserState = ParseInString;
        break;
    }
    return false;
}

void JsonBuffer::resetParser()
{
    mParserState  = ParseNormal;
    mParserDepth  = 0;
    mParserOffset = 0;
    mParserStartOffset = -1;
    mMessageAvailable = false;
    mMessageSize = 0;
}

/*!
  \internal
*/

void JsonBuffer::processMessages()
{
    // do not process anything if disabled or if control is still inside readyReadMessage() slot
    if (mEnabled && !mEmittedReadyRead) {
        mEmittedReadyRead = true;
        if (messageAvailable()) {
            emit readyReadMessage();
        }
        mEmittedReadyRead = false;
    }
}

/*!
  \internal
*/
bool JsonBuffer::messageAvailable()
{
    QScopedPointer<QMutexLocker> locker(createLocker());
    if (mMessageAvailable) {
        // already found - no need to check again
        return true;
    }

    if (mBuffer.size() < 4) {
        // buffer too small for a json message
        return false;
    }

    if (mFormat == FormatUndefined && mBuffer.size() >= 4) {
        if (strncmp("bson", mBuffer.constData(), 4) == 0)
            mFormat = FormatBSON;
        else if (QJsonDocument::BinaryFormatTag == *((uint *) mBuffer.constData()))
            mFormat = FormatQBJS;
        else {
            uchar u0 = mBuffer.at(0), u1 = mBuffer.at(1), u2 = mBuffer.at(2), u3 = mBuffer.at(3);
            // has a BOM?
            if (u0 == 0xFF && u1 == 0xFE) { // utf-32 le or utf-16 le + BOM
                mFormat = (u2 == 0 && u3 == 0 ) ? FormatUTF32LE : FormatUTF16LE;
                mParserOffset++;
            }
            else if (u0 == 0xFE && u1 == 0xFF) { // utf16 be + BOM
                mFormat = FormatUTF16BE;
                mParserOffset++;
            }
            else if (u0 == 0x00 && u1 == 0x00 && u2 == 0xFE && u3 == 0xFF ) { // utf-32 be + BOM
                mFormat = FormatUTF32BE;
                mParserOffset++;
            }
            else if (u0 == 0xEF && u1 == 0xBB && u2 == 0xBF) { // utf8 + BOM
                mFormat = FormatUTF8;
                mParserOffset+=3;
            }
            // no BOM
            else if (u0 == 0 && u1 != 0 && u2 == 0 && u3 != 0 ) {
                mFormat = FormatUTF16BE;
            }
            else if (u0 != 0 && u1 == 0 && u2 != 0 && u3 == 0 ) {
                mFormat = FormatUTF16LE;
            }
            else if (u0 == 0 && u1 == 0 && u2 == 0 && u3 != 0 ) {
                mFormat = FormatUTF32BE;
            }
            else if (u0 != 0 && u1 == 0 && u2 == 0 && u3 == 0 ) {
                mFormat = FormatUTF32LE;
            }
            else {
                mFormat = FormatUTF8;
            }
        }
    }

    switch (mFormat) {
    case FormatUndefined:
        break;
    case FormatUTF8:
        for (  ; mParserOffset < mBuffer.size() ; mParserOffset++ ) {
            char c = mBuffer.at(mParserOffset);
            if (mMessageAvailable) {
                if (!isjsonws(c))
                    break;
            }
            else if (scanUtf(c)) {
                mMessageAvailable = true;
            }
        }
        break;
    case FormatUTF16BE:
        for (  ; 2 * mParserOffset < mBuffer.size() ; mParserOffset++ ) {
            int16_t c = qFromBigEndian(reinterpret_cast<const int16_t *>(mBuffer.constData())[mParserOffset]);
            if (mMessageAvailable) {
                if (!isjsonws(c))
                    break;
            }
            else if (scanUtf(c)) {
                mMessageAvailable = true;
            }
        }
        break;
    case FormatUTF16LE:
        for (  ; 2 * mParserOffset < mBuffer.size() ; mParserOffset++ ) {
            int16_t c = qFromLittleEndian(reinterpret_cast<const int16_t *>(mBuffer.constData())[mParserOffset]);
            if (mMessageAvailable) {
                if (!isjsonws(c))
                    break;
            }
            else if (scanUtf(c)) {
                mMessageAvailable = true;
            }
        }
        break;
    case FormatUTF32BE:
        for (  ; 4 * mParserOffset < mBuffer.size() ; mParserOffset++ ) {
            int32_t c = qFromBigEndian(reinterpret_cast<const int32_t *>(mBuffer.constData())[mParserOffset]);
            if (mMessageAvailable) {
                if (!isjsonws(c))
                    break;
            }
            else if (scanUtf(c)) {
                mMessageAvailable = true;
            }
        }
        break;
    case FormatUTF32LE:
        for (  ; 4 * mParserOffset < mBuffer.size() ; mParserOffset++ ) {
            int32_t c = qFromLittleEndian(reinterpret_cast<const int32_t *>(mBuffer.constData())[mParserOffset]);
            if (mMessageAvailable) {
                if (!isjsonws(c))
                    break;
            }
            else if (scanUtf(c)) {
                mMessageAvailable = true;
            }
        }
        break;
    case FormatBSON:
        if (mBuffer.size() >= 8) {
            qint32 message_size = qFromLittleEndian(((qint32 *)mBuffer.constData())[1]);
            if (mBuffer.size() >= message_size + 4) {
                mMessageSize = message_size;
                mMessageAvailable = true;
            }
        }
        break;
    case FormatQBJS:
        if (mBuffer.size() >= 12) {
            // ### TODO: Should use 'sizeof(Header)'
            qint32 message_size = qFromLittleEndian(((qint32 *)mBuffer.constData())[2]) + 8;
            if (mBuffer.size() >= message_size) {
                mMessageSize = message_size;
                mMessageAvailable = true;
            }
        }
        break;
    }
    return mMessageAvailable;
}

/*!
  \internal
*/
QJsonObject JsonBuffer::readMessage()
{
    QJsonObject obj;
    if (messageAvailable()) {
        QScopedPointer<QMutexLocker> locker(createLocker());
        switch (mFormat) {
        case FormatUndefined:
            break;
        case FormatUTF8:
            if (mParserStartOffset >= 0) {
                QByteArray msg = rawData(mParserStartOffset, mParserOffset - mParserStartOffset);
                obj = QJsonDocument::fromJson(msg).object();
                // prepare for the next
                mBuffer.remove(0, mParserOffset);
                resetParser();
            }
            break;
        case FormatUTF16BE:
            if (mParserStartOffset >= 0) {
                QByteArray msg = rawData(mParserStartOffset * 2, 2*(mParserOffset - mParserStartOffset));
                QString s = QTextCodec::codecForName("UTF-16BE")->toUnicode(msg);
                obj = QJsonDocument::fromJson(s.toUtf8()).object();
                // prepare for the next
                mBuffer.remove(0, mParserOffset*2);
                resetParser();
            }
            break;
        case FormatUTF16LE:
            if (mParserStartOffset >= 0) {
                QByteArray msg = rawData(mParserStartOffset * 2, 2*(mParserOffset - mParserStartOffset));
                QString s = QTextCodec::codecForName("UTF-16LE")->toUnicode(msg);
                obj = QJsonDocument::fromJson(s.toUtf8()).object();
                // prepare for the next
                mBuffer.remove(0, mParserOffset*2);
                resetParser();
            }
            break;
        case FormatUTF32BE:
            if (mParserStartOffset >= 0) {
                QByteArray msg = rawData(mParserStartOffset * 4, 4*(mParserOffset - mParserStartOffset));
                QString s = QTextCodec::codecForName("UTF-32BE")->toUnicode(msg);
                obj = QJsonDocument::fromJson(s.toUtf8()).object();
                // prepare for the next
                mBuffer.remove(0, mParserOffset*4);
                resetParser();
            }
            break;
        case FormatUTF32LE:
            if (mParserStartOffset >= 0) {
                QByteArray msg = rawData(mParserStartOffset * 4, 4*(mParserOffset - mParserStartOffset));
                QString s = QTextCodec::codecForName("UTF-32LE")->toUnicode(msg);
                obj = QJsonDocument::fromJson(s.toUtf8()).object();
                // prepare for the next
                mBuffer.remove(0, mParserOffset*4);
                resetParser();
            }
            break;
        case FormatBSON:
            if (mMessageSize > 0) {
                QByteArray msg = rawData(4, mMessageSize);
                obj = QJsonDocument::fromVariant(BsonObject(msg).toMap()).object();
                mBuffer.remove(0, mMessageSize+4);
                mMessageSize = 0;
            }
            break;
        case FormatQBJS:
            if (mMessageSize > 0) {
                QByteArray msg = rawData(0, mMessageSize);
                obj = QJsonDocument::fromBinaryData(msg).object();
                mBuffer.remove(0, mMessageSize);
                mMessageSize = 0;
            }
            break;
        }
        mMessageAvailable = false;
    }
    return obj;
}

/*!
  Return the current encoding format used by the receive buffer
*/

EncodingFormat JsonBuffer::format() const
{
    return mFormat;
}

QMutexLocker *JsonBuffer::createLocker()
{
    return mThreadProtection ? new QMutexLocker(&mMutex) : 0;
}

/*!
    \fn void JsonBuffer::readyReadMessage()

    This signal is emitted once every time new data is appended to the buffer
    and a message is ready. \b readMessage() should be used to retrieve a message
    and \b messageAvailable() to check for next available messages.
    The client code may look like this:

    \code
    ...
    connect(jsonbuffer, SIGNAL(readyReadMessage()), this, SLOT(processMessages()));
    ...

    void Foo::processMessages()
    {
        while (jsonbuffer->messageAvailable()) {
            QJsonObject obj = jsonbuffer->readMessage();
            <process message>
        }
    }
    \endcode

    \b readyReadMessage() is not emitted recursively; if you reenter the event loop
    inside a slot connected to the \b readyReadMessage() signal, the signal will not
    be reemitted.
*/

#include "moc_jsonbuffer_p.cpp"

QT_END_NAMESPACE_JSONSTREAM