Ejemplo n.º 1
0
void correctBit(binary& bin)
{
    if(bin.size() == 2)
    {
        bin.resize(1);
    }
}
Ejemplo n.º 2
0
void printBinary(const binary& x)
{
    for(int i=x.size()-1; i>=0; --i)
    {
        std::cout << x.getNumberAt(i);
    }
    std::cout << std::endl;
}
Ejemplo n.º 3
0
 bool operator< (const binary<Alloc>& rhs) {
     if (size() < rhs.size()) return true;
     if (size() > rhs.size()) return false;
     if (size() == 0)         return false;
     int res = memcmp(data(), rhs.data(), size());
     if (res < 0) return true;
     if (res > 0) return false;
     return false;
 }
Ejemplo n.º 4
0
long long int convertBinToDec(const binary& x) // binary -> decimal converter
{
    long long int result = 0;

    for(int i=x.size()-1; i>=0; --i)
    {
        if(x.getNumberAt(i))
        {
            result += pow(2,i);
        }
    }

    return result;
}
Ejemplo n.º 5
0
binary generateRandom(int size, const binary& n = binary()) // generates random binary number
{
    int nInt;
    if(n.size() == 0)
    {
        nInt = 1;
    }
    else
    {
        nInt = convertBinToDec(n);
    }

    binary result = initBinaryResult(size);
    int resultInt;

    do
    {
        for(int i=0; i<size; ++i)
        {
            result.setNumberAt(i, generateSecret());
			std::this_thread::sleep_for(std::chrono::milliseconds(1));
        }
        resultInt = convertBinToDec(result);
    } while(resultInt == 0 || gcd(resultInt, nInt) != 1);

    return result;
}
Ejemplo n.º 6
0
object flusspferd::xml::html_parse_binary(binary &b, object options) {
  opt x(options);

  htmlDocPtr doc =
    htmlReadMemory(
      reinterpret_cast<char*>(&b.get_data()[0]),
      b.get_length(),
      x.url,
      x.encoding,
      x.flags);

  if (!doc)
    throw exception("Could not parse HTML document");

  return node::create(xmlNodePtr(doc));
}
Ejemplo n.º 7
0
// Optimize reads by short-circuiting what is unnecessary.
// Invalid returns are conflated with skipped, but are store only.
bool stealth_record::from_data(reader& source, size_t start_height,
    const binary& filter)
{
    height_ = source.read_4_bytes_little_endian();

    if (height_ < start_height)
    {
        reset();
        source.skip(serialized_size(false) - sizeof(uint32_t));
        return false;
    }

    prefix_ = source.read_4_bytes_little_endian();

    if (!filter.is_prefix_of(prefix_))
    {
        reset();
        source.skip(serialized_size(false) - 2 * sizeof(uint32_t));
        return false;
    }

    unsigned_ephemeral_ = source.read_hash();
    public_key_hash_ = source.read_short_hash();
    transaction_hash_ = source.read_hash();

    if (!source)
        reset();

    return source;
}
Ejemplo n.º 8
0
binary operator+(const binary& left, const binary& right)
{
    int n = left.size();
    binary result = initBinaryResult(n);

    binary rightFilled = right;
    if(right.size() < n)
    {
        rightFilled.fill(n);
    }

    binary leftFilled = left;
    if(left.size() < n)
    {
        leftFilled.fill(n);
    }

    bool overflow = false;
    for(int i=0; i<n; ++i)
    {
        bool leftI = leftFilled.getNumberAt(i);
        bool rightI = rightFilled.getNumberAt(i);

        result.setNumberAt(i, leftI ^ rightI ^ overflow);

        if((leftI & rightI)
           || (overflow & (leftI | rightI))
          )
        {
            overflow = true;
        }
        else
        {
            overflow = false;
        }
    }
    if (overflow == true)
    {
        result.resize(n+1);
        result.setNumberAt(n, true);
    }

    return result;
}
Ejemplo n.º 9
0
// Mine a filter into the leftmost bytes of sha256(sha256(output-script)).
bool create_stealth_data(data_chunk& out_stealth_data, ec_secret& out_secret,
    const binary& filter, const data_chunk& seed)
{
    // Create a valid ephemeral key pair.
    ec_secret secret;
    ec_compressed point;
    if (!create_ephemeral_keys(secret, point, seed))
        return false;

    // [ephemeral-public-key-hash:32][pad:0-44][nonce:4]
    static const size_t max_pad_size = operation::max_null_data_size -
        hash_size - sizeof(uint32_t);

    // Derive our initial nonce data from the provided seed.
    const auto bytes = sha512_hash(seed);

    // Create a pad size of 0-44 using the last of bytes (avoiding pad/nonce).
    const auto pad_size = bytes.back() % max_pad_size;

    // Allocate data of target size (36-80 bytes)
    data_chunk data(hash_size + pad_size + sizeof(uint32_t));

    // Copy the unsigned portion of the ephemeral public key into data.
    std::copy(point.begin() + 1, point.end(), data.begin());

    // Copy arbitrary pad bytes into data.
    const auto pad_begin = data.begin() + hash_size;
    std::copy(bytes.begin(), bytes.begin() + pad_size, pad_begin);

    // Create an initial 32 bit nonce value from last byte (avoiding pad).
    const auto start = from_little_endian_unsafe<uint32_t>(bytes.begin() +
        max_pad_size);

    // Mine a prefix into the double sha256 hash of the stealth script.
    // This will iterate up to 2^32 times before giving up.
    for (uint32_t nonce = start + 1; nonce != start; ++nonce)
    {
        // Create the stealth script with the current data.
        const auto ops = operation::to_null_data_pattern(data);
        const auto stealth_script = script{ ops };

        // Test for match of filter to stealth script hash prefix.
        uint32_t field;
        if (to_stealth_prefix(field, stealth_script) &&
            filter.is_prefix_of(field))
        {
            out_stealth_data = data;
            out_secret = secret;
            return true;
        }
    }

    return false;
}
Ejemplo n.º 10
0
binary operator-(const binary& left, const binary& right)
{
    int n = left.size();
    binary result = initBinaryResult(n);

    binary rightTemp = right; // 2-complement
    rightTemp.fill(n);

    for(int i=0; i<n; ++i)
    {
        rightTemp.setNumberAt(i, !rightTemp.getNumberAt(i));
    }
    rightTemp = rightTemp + 1;

    bool overflow = false;
    for(int i=0; i<n; ++i)
    {
        bool leftI = left.getNumberAt(i);
        bool rightI = rightTemp.getNumberAt(i);

        result.setNumberAt(i, leftI ^ rightI ^ overflow);

        if((leftI & rightI)
           || (overflow & (leftI | rightI))
          )
        {
            overflow = true;
        }
        else
        {
            overflow = false;
        }
    }

    return result;
}
Ejemplo n.º 11
0
binary operator*(const binary& left, const binary& right)
{
    binary result = initBinaryResult(left.size());
    int howMuch = left.size();

    if(right.powerOf2())
    {
        long long int temp = convertBinToDec(left);
        temp = temp << (howMuch - 1);
        return convertDecToBin(temp);
    }

    if(left.powerOf2())
    {
        long long int temp = convertBinToDec(right);
        temp = temp << (howMuch - 1);
        return convertDecToBin(temp);
    }

    binary tempR = right;
    binary tempL = left;
    if(left.size() > right.size())
    {
        tempR.fill(left.size());
    }
    else
    {
        tempL.fill(right.size());
    }

// Egyptian method
    for(int i=0; i<howMuch; ++i)
    {
        if(tempL.getNumberAt(i))
        {
            result = tempR + result;
        }
        tempR = tempR + tempR;
    }

    return result;
}
Ejemplo n.º 12
0
std::wstring load_string<wchar_t>(const binary& data) {
	//std::wstring result;
	bool bom;
	const char* charset = test_charset(data, bom);
	unsigned int cp = charset_to_codepage(charset);
	if (cp == 65001 && bom) { // utf-8
		return utf8_to_wstring(std::string(data.begin() + 3, data.end()));
	} else if ((cp == 1200 || cp == 1201) && bom) { // utf-16
		return charset_to_wstring(std::string(data.begin() + 2, data.end()),
				charset);
	} else
		return charset_to_wstring(std::string(data.begin(), data.end()),
				charset);

}
Ejemplo n.º 13
0
bool lsb(const binary& x) // least significant bit
{
	return x.getNumberAt(0);
}
Ejemplo n.º 14
0
void UnixDomainConnector::do_receive()
{
    // If a header was read from the socket, but the payload did not
    // yet arrive completely, the read header is stored here until
    // more data arrived:
    static binary last_header;

    // Read all PDUs into distinct buffers
    std::list<binary> queue;
    do
    {
        binary buf;

        // Read header
        if(last_header.size() != 0)
        {
            // Last time, we read a header, but no payload yet.
            // Therefore we don't read the header here, but continue
            // with reading the payload.
            buf = last_header;
            last_header.clear();
        }
        else
        {
            // No header read last time.
            char header[20];
            if(m_socket.read(header, 20) != 20)
            {
                disconnect(); // error!
                break; // Stop reading PDUs
            }
            buf.append(reinterpret_cast<quint8*>(header), 20);
        }

        // Extract endianness flag
        bool big_endian = ( buf[2] & (1<<4) ) ? true : false;

        // Extract payload length
        quint32 payload_length;
        binary::const_iterator pos = buf.begin() + 16;
        payload_length = read32(pos, big_endian);
        if( payload_length % 4 != 0 )
        {
            // payload length must be a multiple of 4!
            // See RFC 2741, 6.1. "AgentX PDU Header"
            // We don't know where next PDU starts within the byte stream,
            // therefore we disconnect.
            disconnect(); // error!
            // stop reading PDU's (but process the ones we got so far)
            break;
        }

        // Read payload
        if(m_socket.bytesAvailable() < payload_length)
        {
            // Payload did not completely arrive. We store the header until
            // more data arrived:
            last_header = buf;
            break;
        }
        QScopedArrayPointer<char> payload(new char[payload_length]);
        qint64 bytes_read = m_socket.read(payload.data(), payload_length);
        if(bytes_read != payload_length)
        {
            disconnect();
            return;
        }
        buf.append(reinterpret_cast<quint8*>(payload.data()), payload_length);

        queue.push_back(buf);
    }
    while(m_socket.bytesAvailable() >= 20); // still enough data for next header

    // Process all received PDU's
    for(list<binary>::const_iterator i = queue.begin(); i != queue.end(); i++)
    {
        // Parse PDU
        QSharedPointer<PDU> pdu;
        try
        {
            pdu = PDU::parse_pdu(*i);
        }
        catch(version_error)
        {
            return;
        }
        catch(parse_error)
        {
            return;
        }

        // Special case: ResponsePDU's
        QSharedPointer<ResponsePDU> response;
        response = qSharedPointerDynamicCast<ResponsePDU>(pdu);
        if(response)
        {
            m_response_mutex.lock();
            // Was a response
            std::map< quint32, QSharedPointer<ResponsePDU> >::iterator i;
            i = this->m_responses.find( response->get_packetID() );
            if(i != this->m_responses.end())
            {
                // Someone is waiting for this response
                i->second = response;
                m_response_mutex.unlock();
                m_response_arrived.wakeAll();
            }
            else
            {
                // Nobody was waiting for the response
                // -> ignore it
                m_response_mutex.unlock();
            }
        }
        else
        {
            // Was not a Response
            // -> emit signal
            emit pduArrived(pdu);
        }
    }
}
Ejemplo n.º 15
0
 bool operator== (const binary<Alloc>& rhs) const {
     return size() == rhs.size() 
         && (size() == 0 || memcmp(data(), rhs.data(), size()) == 0);
 }
Ejemplo n.º 16
0
inline pn_bytes_t pn_bytes(const binary& s) {
    pn_bytes_t b = { s.size(), s.empty() ? 0 : reinterpret_cast<const char*>(&s[0]) };
    return b;
}