summaryrefslogtreecommitdiffstats
path: root/kfile-plugins/torrent/bint.cpp
blob: 0e23731ccaa9fe70ec4ac2a27c4ee51bc240bc0e (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
/*
 * Copyright (c) 2003, 2004 Michael Pyne <michael.pyne@kdemail.net>
 *
 * This software is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License as
 * published by the Free Software Foundation; either version 2 of
 * the License, or (at your option) any later version.
 *
 * This software is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public
 * License along with this library; see the file COPYING.
 * If not, write to the Free Software Foundation, Inc.,
 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
 */
#include <tqstring.h>
#include <tqcstring.h>
#include <tqiodevice.h>

#include "bytetape.h"
#include "bint.h"

// A bencoded int is (approximately) as follows:
// i(\d)+e
BInt::BInt (TQByteArray &dict, int start)
    : m_value (0), m_valid(false)
{
    ByteTape tape (dict, start);
    init (tape);
}

BInt::BInt (ByteTape &tape)
    : m_value(0), m_valid(false)
{
    init(tape);
}

void BInt::init (ByteTape &tape)
{
    if (*tape != 'i')
        return;

    tape ++; // Move to start of digits

    TQByteArray &dict (tape.data());
    if (dict.tqfind('e', tape.pos()) == -1)
        return;

    // Copy the part from the start to the e.  The values in-between
    // should be digits, perhaps preceded by a negative sign.
    int length = dict.tqfind('e', tape.pos()) - tape.pos();
    char *ptr = dict.data(); // Get start of buffer
    ptr += tape.pos(); // Advance to current position in tape

    // Allocate temporary data buffer
    TQByteArray buffer(length + 1);

    tqmemmove (buffer.data(), ptr, length);
    buffer[length] = 0; // Null-terminate

    TQString numberString (buffer);
    bool a_isValid; // We want to make sure the string is a valid number

    m_value = numberString.toLongLong(&a_isValid);

    tape += length; // Move to 'e'
    tape ++;        // Move to next char

    m_valid = a_isValid; // Now we're good, if it was a number
}

BInt::~BInt()
{
    /* Nothing yet */
}

bool BInt::writeToDevice (TQIODevice &device)
{
    if (!m_valid)
        return false;

    /* Write out i234e, and such */
    TQString str = TQString("i%1e").
        arg (m_value);

    TQ_LONG written = 0, result = 0;
    written = device.writeBlock (str.latin1(), str.length());
    while ((uint) written < str.length())
    {
        if (written < 0 || result < 0)
            return false;

        result = device.writeBlock(str.latin1() + written,
                str.length() - written);
        written += result;
    }

    return true;
}

// vim: set et ts=4 sw=4: