void correctBit(binary& bin) { if(bin.size() == 2) { bin.resize(1); } }
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; }
void printBinary(const binary& x) { for(int i=x.size()-1; i>=0; --i) { std::cout << x.getNumberAt(i); } std::cout << std::endl; }
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; }
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; }
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; }
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; }
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; }
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); } } }
bool operator== (const binary<Alloc>& rhs) const { return size() == rhs.size() && (size() == 0 || memcmp(data(), rhs.data(), size()) == 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; }