/*
 * Decode URLs without doing any charset conversion.
 *
 * Most simple approach to do it without any lookahead.
 */
static QByteArray decodePercentEncoding(const QByteArray& input)
{
    int actualLength = 0;
    QByteArray tmpVal;
    QByteArray output;
    ParserState state = State_Begin;

    output.resize(input.length());
    tmpVal.resize(2);

    for (int i = 0; i < input.length(); ++i)
        if (state == State_Begin) {
            if (input.at(i) == '%') {
                state = State_FirstChar;
            } else {
                output[actualLength++] = input[i];
            }
        } else if (state == State_FirstChar) {
            state = State_SecondChar;
            tmpVal[0] = input[i];
        } else if (state == State_SecondChar) {
            state = State_Begin;
            tmpVal[1] = input[i];
            output[actualLength++] = tmpVal.toShort(0, 16);
        }

    output.resize(actualLength);
    return output;
}
Пример #2
0
void QCmdParser::GetThreshold( QByteArray &data, QString &strInfo )
{
    QByteArray byHex = data.toHex(  );
    if ( 2 > byHex.count( ) ) {
        return;
    }

    qint8 cHigh = byHex.at( 0 );
    qint8 nLow = byHex.at( 1 );

    data.clear( );
    data.append( ( qint8 ) byHex.toShort( ) );

    strInfo.sprintf( "%c.%c", cHigh, nLow );
}
Пример #3
0
/**@brief convert QString to short int (2 bytes, 16 bits);
 *
 *@param str [IN]: the string to convert;
 *@param data [OUT]: the result is stored in this parm;
 *@return bool: if convert succeed, return true; else, return false.
 *@note
 */
bool Lms511Thread::Str2Int_16(QByteArray str, unsigned short int * data)
{
    bool ok;
    *data = str.toShort(&ok, 16);
    return ok;
}
Пример #4
0
void BytesInteger::transform(const QByteArray &input, QByteArray &output)
{
    output.clear();
    if (input.isEmpty())
        return;

    bool reverseByteOrdering = false;

#ifdef Q_LITTLE_ENDIAN
    bool currentSystemLittleEndian = true;
#else
    bool currentSystemLittleEndian = false;
#endif

    if (currentSystemLittleEndian != littleendian) {
        reverseByteOrdering = true;
    }

    QByteArray temp = input;
    if (wayValue == INBOUND) {

        if (temp.size() > integerSize) {
            temp = temp.mid(0,integerSize);
            emit warning(tr("Input size too big for the selected Integer size. the last bytes have been ignored"),id);
        }

        if (temp.size() < integerSize) {
            if (reverseByteOrdering)
                temp.prepend(QByteArray(integerSize - temp.size(),'\x00'));
            else
                temp.append(QByteArray(integerSize - temp.size(),'\x00'));
            emit error(tr("Input size too small for the selected Integer size. Padded with zeros"),id);
        }

        if (reverseByteOrdering) {
            QByteArray temp2;
            for (int i = temp.size() - 1; i >= 0; i--) {
                temp2.append(temp.at(i));
            }
            temp = temp2;
        }



        if (signedInteger) {
            switch (integerSize) {
            case I8bits: {
                    qint8 val = 0;
                    memcpy(&val, temp.data(), integerSize);
                    output.append(QByteArray::number(val));
                    break;
            }
            case I16bits: {
                    qint16 val = 0;
                    memcpy(&val, temp.data(), integerSize);
                    output.append(QByteArray::number(val));
                    break;
            }
            case I32bits: {
                    qint32 val = 0;
                    memcpy(&val, temp.data(), integerSize);
                    output.append(QByteArray::number(val));
                    break;
            }
            case I64bits: {
                    qint64 val = 0;
                    memcpy(&val, temp.data(), integerSize);
                    output.append(QByteArray::number(val));
                    break;
            }
            }

        } else {
            switch (integerSize) {
            case I8bits: {
                    quint8 val = 0;
                    memcpy(&val, temp.data(), integerSize);
                    output.append(QByteArray::number(val));
                    break;
            }
            case I16bits: {
                    quint16 val = 0;
                    memcpy(&val, temp.data(), integerSize);
                    output.append(QByteArray::number(val));
                    break;
            }
            case I32bits: {
                    quint32 val = 0;
                    memcpy(&val, temp.data(), integerSize);
                    output.append(QByteArray::number(val));
                    break;
            }
            case I64bits: {
                    quint64 val = 0;
                    memcpy(&val, temp.data(), integerSize);
                    output.append(QByteArray::number(val));
                    break;
            }
            }
        }
    } else {
        bool ok;
        char data[8] = { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };

        if (signedInteger) {
            switch (integerSize) {
            case I8bits: {
                    qint16 val16 = input.toShort(&ok);
                    if (ok && val16 > -129 && val16 < 128) {
                        qint8 val8 = (qint8)val16;
                        memcpy(data, &val8, I8bits);
                    } else {
                        emit error(tr("Invalid 8bits signed Integer"),id);
                    }

                    break;
            }
            case I16bits: {
                    qint16 val16 = input.toShort(&ok);
                    if (ok) {
                        memcpy(data, &val16, I16bits);
                    } else {
                        emit error(tr("Invalid 16bits signed Integer"),id);
                    }
                    break;
            }
            case I32bits: {
                    qint32 val32 = input.toInt(&ok);
                    if (ok) {
                        memcpy(data, &val32, I32bits);
                    } else {
                        emit error(tr("Invalid 32bits signed Integer"),id);
                    }
                    break;
            }
            case I64bits: {
                    qint64 val64 = input.toInt(&ok);
                    if (ok) {
                        memcpy(data, &val64, I64bits);
                    } else {
                        emit error(tr("Invalid 64bits signed Integer"),id);
                    }
                    break;
            }
            }
        } else {
                switch (integerSize) {
                case I8bits: {
                        quint16 val16 = input.toShort(&ok);
                        if (ok && val16 < 256) {
                            quint8 val8 = (qint8)val16;
                            memcpy(data, &val8, I8bits);
                        } else {
                            emit error(tr("Invalid 8bits unsigned Integer"),id);
                        }
                }
                case I16bits: {
                        quint16 val16 = input.toShort(&ok);
                        if (ok) {
                            memcpy(data, &val16, I16bits);
                        } else {
                            emit error(tr("Invalid 16bits unsigned Integer"),id);
                        }
                        break;
                }
                case I32bits: {
                        quint32 val32 = input.toInt(&ok);
                        if (ok) {
                            memcpy(data, &val32, I32bits);
                        } else {
                            emit error(tr("Invalid 32bits unsigned Integer"),id);
                        }
                        break;
                }
                case I64bits: {
                        quint64 val64 = input.toInt(&ok);
                        if (ok) {
                            memcpy(data, &val64, I64bits);
                        } else {
                            emit error(tr("Invalid 64bits unsigned Integer"),id);
                        }
                        break;
                }
                }
            }
        if (reverseByteOrdering) {
            for (int i = integerSize - 1; i >= 0; i--) {
                output.append(data[i]);
            }
        } else {
            output = QByteArray(data, integerSize);
        }

    }
}