示例#1
0
string IdxSigEntryList::serialize()
{
    header_t bodysize, realbodysize = 0;
    string buf;
    vector<IdxSigEntry>::iterator iter;
    
    bodysize = bodySize();

    appendToBuffer(buf, &bodysize, sizeof(bodysize));
    
    //cout << "list body put in: " << bodysize << endl;

    for ( iter = list.begin() ;
          iter != list.end() ;
          iter++ )
    {
        string tmpbuf;
        tmpbuf = iter->serialize();
        if ( tmpbuf.size() > 0 ) {
            appendToBuffer(buf, &tmpbuf[0], tmpbuf.size());
        }
        realbodysize += tmpbuf.size();
    }
    assert(realbodysize == bodysize);
    //cout << realbodysize << "==" << bodysize << endl;

    return buf;
}
/**
 * Fast approach for continued division<br/>
 * <b>Note:</b> This method has a overhead that can make the naïve algorithm faster,
 * however I have decreased the overhead and it appears to be best always the use this method
 *
 * @param   remainder   The integer without any factor of the factor, the result
 * @param   integer     The integer to divide
 * @param   factor      The integer with which to divide
 * @param   output      The buffer to write the factor to once for every time the factor divides the integer
 * @param   outputPtr   The buffer's pointer
 * @param   rootOrder   The root order
 * @return              The number of this the factor is dividable
 */
int contDiv(Bignum remainder, Bignum integer, Bignum factor, Buffer output, long* outputPtr, int rootOrder)
{
    //Just like n↑m by squaring, excepted this is division

    String string = bignumToString(factor);

    long ropt, i;
    int times = 0;  //number of times the integer is dividable
    int ptimes = 1; //Partial number of times the integer is dividable

    Bignum n; mpz_init_set(n, integer); //Intger to divide
    Bignum q; mpz_init(q);              //Quotient
    Bignum r; mpz_init(r);              //Remainder
    Bignum x; mpz_init_set(x, factor);  //Factor

    Bignum* divStack = malloc(sizeof(Bignum) * 6); //lb log₃ 2¹⁰⁰ ≈ 6
    mpz_init_set(*divStack++, factor);


    for (;;)
    {
        mpz_mul(x, x, x);
	if (mpz_cmp(x, n) <= 0)
	{
	    mpz_init_set(*divStack++, x);
	    ptimes <<= 1;
	}
	else
        {
	    ropt = rootOrder * ptimes;
	    while (ptimes)
	    {
	        divMod(q, r, n, *--divStack);
		if (equals(r, 0))
	        {
		    for (i = 0; i < ropt; i++)
		    {
		        appendToBuffer(output, *outputPtr, string);
		        appendToBuffer(output, (*outputPtr) + 1, "\n");
			(*outputPtr) += 2;
		    }
		    times |= ptimes;
		    mpz_set(n, q);
		}
		ropt >>= 1;
		ptimes >>= 1;
	    }

	    mpz_set(remainder, n);
	    return times;
	}
    }
}
示例#3
0
void WebSocketChannel::didReceiveSocketStreamData(SocketStreamHandle& handle, const char* data, std::optional<size_t> len)
{
    if (len)
        LOG(Network, "WebSocketChannel %p didReceiveSocketStreamData() Received %zu bytes", this, len.value());
    else
        LOG(Network, "WebSocketChannel %p didReceiveSocketStreamData() Received no bytes", this);
    Ref<WebSocketChannel> protectedThis(*this); // The client can close the channel, potentially removing the last reference.
    ASSERT(&handle == m_handle);
    if (!m_document) {
        return;
    }
    if (!len || !len.value()) {
        handle.disconnect();
        return;
    }
    if (!m_client) {
        m_shouldDiscardReceivedData = true;
        handle.disconnect();
        return;
    }
    if (m_shouldDiscardReceivedData)
        return;
    if (!appendToBuffer(data, len.value())) {
        m_shouldDiscardReceivedData = true;
        fail("Ran out of memory while receiving WebSocket data.");
        return;
    }
    while (!m_suspended && m_client && !m_buffer.isEmpty()) {
        if (!processBuffer())
            break;
    }
}
示例#4
0
extern DLLSERVER_API void appendResource(MemoryBuffer & mb, size32_t len, const void *data, bool compress)
{
    mb.append((byte)0x80).append(resourceHeaderVersion);
    if (compress)
        compressToBuffer(mb, len, data);
    else
        appendToBuffer(mb, len, data);
}
示例#5
0
string 
PatternUnit::serialize()
{
    string buf; //let me put it in string and see if it works
    header_t seqbodysize;
    header_t totalsize;

    totalsize = bodySize(); 
    
    appendToBuffer(buf, &totalsize, sizeof(totalsize));
    appendToBuffer(buf, &init, sizeof(init));
    appendToBuffer(buf, &cnt, sizeof(cnt));
    seqbodysize = seq.size()*sizeof(off_t);
    appendToBuffer(buf, &(seqbodysize), sizeof(header_t));
    if (seqbodysize > 0 ) {
        appendToBuffer(buf, &seq[0], seqbodysize);
    }
    return buf;
}
示例#6
0
void MessageCipher::transformReadData(const QByteArray& message)
{
    if (m_asym) {
        try {
            appendToBuffer(m_asym->decrypt(message));
        } catch (const AsymetricAlgorithm::AsymmetricAlgorithmException &e) {
            LOG_ENTRY(MyLogger::ERROR,
                    "Unable to decrypt a message: "<<message<<" because: "<<e.what());
            emit SinkError(MessageCorrupted);
        }
    } else {
        try {
            appendToBuffer(m_sym->decrypt(message));
        } catch (const SymetricAlgorithm::SymmetricAlgorithmException &e) {
            LOG_ENTRY(MyLogger::ERROR,
                    "Unable to decrypt a message: "<<message<<" because: "<<e.what());
            emit SinkError(MessageCorrupted);
        }
    }
}
示例#7
0
/* Append the given string to the specified buffer, with printf style additional arguments
 * param    buffer               buffer to add string to
 * param    format               string to add to end of buffer
 * param    ...                  additional arguments
 */
void catToBuffer(Buffer *buffer, const char *format, ...)
{
    va_list args;
    int len;  /* Length printed */
    static char buf[CHAR_BUF_LEN + 1];

    assert(buffer != NULL);
    assert(format != NULL);

    va_start(args, format);
    len = vsnprintf(buf, CHAR_BUF_LEN, format, args);
    va_end(args);

    if(len < 0)
        appendToBuffer(buffer, "#PRINT ERROR#");
    else if(len > 0)
    {
        buf[len] = '\0';
        appendToBuffer(buffer, buf);
    }
}
示例#8
0
string IdxSigEntry::serialize()
{
    header_t totalsize = 0;
    string buf, tmpbuf;
    header_t datasize;
    
    totalsize = bodySize();
    //cout << "IdxSigEntry totalsize put in: " << totalsize << endl;
    appendToBuffer(buf, &totalsize, sizeof(totalsize));
    appendToBuffer(buf, &original_chunk, sizeof(original_chunk));
    appendToBuffer(buf, &new_chunk_id, sizeof(new_chunk_id));
    //cout << "IdxSigEntry original_chunk put in: " << original_chunk << endl; 
    
    //this tmpbuf includes [data size][data]
    tmpbuf = logical_offset.serialize(); 
    appendToBuffer(buf, &tmpbuf[0], tmpbuf.size());
    
    tmpbuf = length.serialize();
    appendToBuffer(buf, &tmpbuf[0], tmpbuf.size());

    tmpbuf = physical_offset.serialize();
    appendToBuffer(buf, &tmpbuf[0], tmpbuf.size());

    return buf;
}
示例#9
0
void WebSocketChannel::didReceiveData(SocketStreamHandle* handle, const char* data, int len)
{
    LOG(Network, "WebSocketChannel %p didReceiveData %d", this, len);
    RefPtr<WebSocketChannel> protect(this); // The client can close the channel, potentially removing the last reference.
    ASSERT(handle == m_handle);
    if (!m_context) {
        return;
    }
    if (!m_client) {
        m_shouldDiscardReceivedData = true;
        handle->close();
        return;
    }
    if (m_shouldDiscardReceivedData)
        return;
    if (!appendToBuffer(data, len)) {
        m_shouldDiscardReceivedData = true;
        handle->close();
        return;
    }
    while (!m_suspended && m_client && m_buffer)
        if (!processBuffer())
            break;
}
void MainThreadWebSocketChannel::didReceiveSocketStreamData(SocketStreamHandle* handle, const char* data, int len)
{
    WTF_LOG(Network, "MainThreadWebSocketChannel %p didReceiveSocketStreamData() Received %d bytes", this, len);
    ASSERT(handle == m_handle);
    if (!m_document)
        return;
    if (len <= 0) {
        disconnectHandle();
        return;
    }
    if (!m_client) {
        m_shouldDiscardReceivedData = true;
        disconnectHandle();
        return;
    }
    if (m_shouldDiscardReceivedData)
        return;
    if (!appendToBuffer(data, len)) {
        m_shouldDiscardReceivedData = true;
        failAsError("Ran out of memory while receiving WebSocket data.");
        return;
    }
    processBuffer();
}
/**
 * <p>Factorises a big integer using Pollard's rho method and some tweaks</a>
 * <p>
 *   Pollard's rho (ρ) method is a special-purpose integer factorisation algorithm used
 *   for factoring integers with small factors. As you know, they are not that unusual.
 * </p>
 *
 * @param   n                 The big integer
 * @param   c                 The seed index, 0 for autoselection of seed value
 * @param   output            The buffer to which to add [probable] prime factors
 * @param   outputPtr         The buffer's pointer
 * @param   initialRootOrder  The initial root order
 * @return                    Whether the factorisation was successful
 */
boolean factorisePollardsRho(Bignum n, llong c, Buffer output, long* outputPtr, int initialRootOrder)
{
    if (c > SEED_LIMIT)
        return false;

    llong cc = *(seeds + c);
    if (c == 0)
        { selectSeed(cc, n); }

    int i, m, cd;
    String prime;
    int itr;

    int rootOrder = initialRootOrder, pRootOrder = 1, r;

    #define f(X)  mpz_mul(X, X, X);  mpz_add_ui(X, X, cc)

    Bignum factor; mpz_init_set(factor, n);
    Bignum d; mpz_init(d);
    Bignum x; mpz_init_set_ui(x, cc);
    Bignum y; mpz_init_set_ui(y, cc);
    Bignum conjA; mpz_init(conjA);
    Bignum conjB; mpz_init(conjB);


    #define recursion(X, Y)  factorisePollardsRho(X, c + 1, output, outputPtr, Y)


    for (;;)
    {
        pRootOrder = maxRoot(factor);
	if (pRootOrder != 1)
	{
	    rootOrder *= pRootOrder;
	    mpz_root(factor, factor, pRootOrder);
	}
	else
	{
	    //There may exist a number b = (A = ⌊√n⌋ + 1)² − n such that B = √b is an integer
	    //in which case n = (A − B)(A + B)  [n is(!) odd composite]. If not, the only the
	    //trivial iteration of A (A += 1) seems to be the one not consuming your entire
	    //CPU and it is also guaranteed to find the factors, but it is just so slow.

	    mpz_sqrt(conjA, factor);
	    mpz_add_ui(conjA, conjA, 1);
	    mpz_mul(conjB, conjA, conjA);
	    mpz_sub(conjB, conjB, factor);

	    if (mpz_root(conjB, conjB, 2))
	    {
		mpz_sub(factor, conjA, conjB);
	        if (recursion(factor, rootOrder) == false)
		    return false;

		mpz_add(factor, conjA, conjB);
		return recursion(factor, rootOrder);
	    }
	}


        itr = 0;
	do // Pollard  //                  http://en.wikipedia.org/wiki/Pollard's_rho_algorithm#Variants
	{
	    if (c)
	    {
	        if (itr++ > MAX_ITERATIONS_C)
		    return false;
	    }
	    else if (cc >= 10000)
	        if (itr++ > MAX_ITERATIONS)
	            return recursion(factor, rootOrder);

	    //Floyd
	    f(x); f(y); f(y);
	    mpz_mod(x, x, factor);
	    mpz_mod(y, y, factor);

	    mpz_sub(d, x, y);
	    mpz_abs(d, d);
	    mpz_gcd(d, d, factor);
	}
	  while (equals(d, 1));


	if (mpz_cmp(factor, d) == 0)
	{
	    if (isPrime(factor))
	    {
	        String strFactor = bignumToString(factor);
	        for (r = 0; r < rootOrder; r++)
		{
		    appendToBuffer(output, *outputPtr, strFactor);
		    appendToBuffer(output, (*outputPtr) + 1, "\n");
		    (*outputPtr) += 2;
		}
	        return true;
	    }
	    return recursion(factor, rootOrder);
	}

	if (isNotPrime(d))
	{
	    cd = contDiv(factor, factor, d, null, null, 0);

            if (recursion(d, rootOrder * cd) == false)
	        return false;

	    if (equals(factor, 1))
	        return true;
	}
	else
	{
	    contDiv(factor, factor, d, output, outputPtr, rootOrder);

	    if (equals(factor, 1))
	        return true;
	}
        
	if (isPrime(factor))
	{
	    String strFactor = bignumToString(factor);
	    for (r = 0; r < rootOrder; r++)
	    {
	        appendToBuffer(output, *outputPtr, strFactor);
		appendToBuffer(output, (*outputPtr) + 1, "\n");
		(*outputPtr) += 2;
	    }
	    return true;
	}
    }

    return true;
}
bool StaticLogFilter::useFilter(Log *currentLog, Log *newLog, AbstractFilter *filter)
{
//    Log *newLog = new Log("temp.bin", currentLog->blockSize, currentLog->memorySize, currentLog->eventsInfo);
    newLog->load(false, true);

    qint64 currentLogPos = currentLog->pos();
    currentLog->seek(0);

    // index parameters
    newLog->index->logSize = 0;
    newLog->index->blockElementsCount = 0;
    newLog->index->filePos = 0;

    // TODO: allocate half of memory size here
    char *newLogBuffer = new char[newLog->memorySize];
    quint64 newLogBufferPos = 0;
    quint64 newLogBufferSize = 0;

    bool breakFirst = false;

    QProgressDialog dlg(QObject::tr("Filtration..."), QObject::tr("Cancel"), 0, 100);
    dlg.setWindowModality(Qt::WindowModal);

    while(currentLog->pos() < currentLog->size())
    {
        qDebug() << (currentLog->pos() * 100) / currentLog->size();

        if(dlg.wasCanceled())
            return false;

        dlg.setValue((currentLog->pos() * 100) / currentLog->size());
        QCoreApplication::processEvents();

        if(breakFirst)
            break;

        qint64 blockSize = 0;
        char *blockBuffer;

        if((currentLog->size() - currentLog->pos()) < currentLog->blockSize)
            blockBuffer = currentLog->read(currentLog->size() - currentLog->pos(), blockSize);

        else
            blockBuffer = currentLog->read(currentLog->blockSize, blockSize);

        qint64 pos = 0;
        while(pos < blockSize)
        {
            qint64 readedSize = 0;
            bool success = false;
            QVariant argValue;
            quint64 time = 0;
            if(!StaticRecordsReader::checkRecord(blockBuffer, blockSize, pos, readedSize, success, filter->argName(), argValue, time, currentLog->eventsInfo))
            {
                QErrorMessage errorMessager;
                errorMessager.showMessage(QObject::tr("Unexpected end of record"));
                return false;
            }

            if(success == true)
            {
                if(filter->check(argValue))
                {
                    appendToBuffer(blockBuffer, pos, newLogBuffer, newLogBufferPos, readedSize);
                    newLogBufferSize += readedSize;

                    if(newLog->index->indexSize == 1)
                    {
                        newLog->index->blockElementsCount ++;
                        newLog->index->logSize ++;

                        newLog->index->append(newLog->index->filePos, time);
                        newLog->index->filePos += readedSize;
                    }
                    else
                    {
                        newLog->index->blockElementsCount ++;
                        newLog->index->logSize ++;
                        newLog->index->filePos += readedSize;

                        if(newLog->index->blockElementsCount == newLog->index->blockSize)
                        {
                            newLog->index->append(newLog->index->filePos, time);

                            newLog->index->blockElementsCount = 0;

                            StaticLogWriter::writeLogFile(*newLog->file, newLogBuffer, newLogBufferSize);

                            newLogBufferPos = 0;
                            newLogBufferSize = 0;
                        }
                    }
                }
                else
                {
//                    if(filter->argName() == "time")
//                    {
//                        pos += readedSize;
//                        breakFirst = true;
//                        break;
//                    }
                }
            }

            pos += readedSize;
        }
    }

    if(newLog->index->blockElementsCount != 0)
    {
        newLog->index->blockElementsCount = 0;

        StaticLogWriter::writeLogFile(*newLog->file, newLogBuffer, newLogBufferSize);

        newLogBufferPos = 0;
        newLogBufferSize = 0;
    }

    currentLog->seek(currentLogPos);
//    currentLog->file->seek(currentLogPos);

    delete[] newLogBuffer;

    newLog->updateFileSize();

    // TODO: must return new Log

    return true;
}