Ejemplo n.º 1
0
 GroupElement * CryptoPpDlogZpSafePrime::encodeByteArrayToGroupElement(const vector<unsigned char> & binaryString) {
	//Any string of length up to k has numeric value that is less than (p-1)/2 - 1.
	//If longer than k then throw exception.
	 int bs_size = binaryString.size();
	if (bs_size > k) {
		throw length_error("The binary array to encode is too long.");
	}

	//Pad the binaryString with a x01 byte in the most significant byte to ensure that the 
	//encoding and decoding always work with positive numbers.
	list<unsigned char> newString(binaryString.begin(), binaryString.end());
	newString.push_front(1);

	byte *bstr = new byte[bs_size + 1];
	for (auto it = newString.begin(); it != newString.end(); ++it) {
		int index = std::distance(newString.begin(), it);
		bstr[index] = *it;
	}
	biginteger s = cryptoppint_to_biginteger(CryptoPP::Integer(bstr, bs_size + 1));

	//Denote the string of length k by s.
	//Set the group element to be y=(s+1)^2 (this ensures that the result is not 0 and is a square)
	biginteger y = boost::multiprecision::powm((s + 1), 2, ((ZpGroupParams *)groupParams)->getP());

	//There is no need to check membership since the "element" was generated so that it is always an element.
	ZpSafePrimeElementCryptoPp * element = new ZpSafePrimeElementCryptoPp(y, ((ZpGroupParams * )groupParams)->getP(), false);
	delete(bstr);
	return element;
}
Ejemplo n.º 2
0
    string toUtf8String(const wstring& wide) {
        if (wide.size() > boost::integer_traits<int>::const_max)
            throw length_error(
                "Wide string cannot be more than INT_MAX characters long.");
        if (wide.size() == 0)
            return "";

        // Calculate necessary buffer size
        int len = ::WideCharToMultiByte(
                      CP_UTF8, 0, wide.c_str(), static_cast<int>(wide.size()),
                      NULL, 0, NULL, NULL);

        // Perform actual conversion
        if (len > 0) {
            vector<char> buffer(len);
            len = ::WideCharToMultiByte(
                      CP_UTF8, 0, wide.c_str(), static_cast<int>(wide.size()),
                      &buffer[0], static_cast<int>(buffer.size()), NULL, NULL);
            if (len > 0) {
                assert(len == static_cast<int>(buffer.size()));
                return string(&buffer[0], buffer.size());
            }
        }

        throw boost::system::system_error(
            ::GetLastError(), boost::system::system_category());
    }
Ejemplo n.º 3
0
string Base64::Encode(const string &bindata)
{
  if (bindata.size() > (numeric_limits<string::size_type>::max() / 4u) * 3u) {
    throw length_error("Converting too large a string to base64.");
  }

  const size_t binlen = bindata.size();
  // Use = signs so the end is properly padded.
  string retval((((binlen + 2) / 3) * 4), '=');
  //string retval(ceil(((binlen) / 3.0) * 4.0), '='); //this doesn't seem
  //to be complian with the standard and it fails to decode with
  //python decode function
  size_t outpos = 0;
  int bits_collected = 0;
  unsigned int accumulator = 0;
  const string::const_iterator binend = bindata.end();

  for (string::const_iterator i = bindata.begin(); i != binend; ++i) {
    accumulator = (accumulator << 8) | (*i & 0xffu);
    bits_collected += 8;
    while (bits_collected >= 6) {
      bits_collected -= 6;
      retval[outpos++] = b64_table[(accumulator >> bits_collected) & 0x3fu];
    }
  }
   if (bits_collected > 0) { // Any trailing bits that are missing.
    assert(bits_collected < 6);
    accumulator <<= 6 - bits_collected;
    retval[outpos++] = b64_table[accumulator & 0x3fu];
   }
   //assert(outpos >= (retval.size() - 2));
   assert(outpos <= retval.size());
   return retval;
}
Ejemplo n.º 4
0
void wstring::resize(size_t n, wchar_t c) {
    if (n > max_size())
        throw length_error("n > max_size()");
    if (n > length())
        append(n - length(), c);
    else
        erase(n);
}
Ejemplo n.º 5
0
        int pop() {
            if (maxHeap_.empty())
                throw length_error("stack empty");

            TimestampValue tmp = maxHeap_.top();
            maxHeap_.pop();
            return tmp.value;
        }
Ejemplo n.º 6
0
/**
** WORKS
**/
set<string> WordSearch::files_with_most_frequent_words()  throw(length_error) {
	if (leastFrequent.rbegin() == leastFrequent.rend()) // Err if empty
		throw length_error ("length error");

	set<string> files;
	for (auto iter = leastFrequent.rbegin()->second.begin(); iter!=leastFrequent.rbegin()->second.end(); iter++){
			files.insert(wordFileMap[*iter].begin(),wordFileMap[*iter].end());
	}
    return files;
}
Ejemplo n.º 7
0
T dequeue()
{
	if(_count)
	{
		T ret = _data[head];
		_count--;
		_head = (_head+1)%_data.size();
		return ret;
  }
  else throw length_error(“Queue is Empty”);
}
Ejemplo n.º 8
0
RealVector RealVector::operator /(const RealVector& rhs) const {
	RealVector ret(*this);
	int i;

	if (rhs.n != n)
		throw length_error("Lengths of vectors are unequal");

	for (i = 0; i < n; i++) {
		ret.entries[i] /= rhs.entries[i];
	}

	return ret;
}
Ejemplo n.º 9
0
void lowerMatrix<T>::setsize(const int size)
{
  if (size < 0)
  {
    throw length_error("setsize param");
  }
  m_dimentions = size;
  m_data.setsize(size);

  for (int i=0; i < size; i++)
  {
    m_data[i].setsize(i+1);
  }
  return;
}
Ejemplo n.º 10
0
string str::wordWrap(string in, unsigned cols) {
    unsigned i = cols;
    while (i < in.size()) {
        for (; i > 0; i--) {
            if (isspace(int((in.at(i))))) {
                in.replace(i, 1, "\n");
                break;
            }
        }
        if (i == 0) {
            throw length_error("length of word exceeds cols");
        }
        i += cols;
    }
    return in;
}
Ejemplo n.º 11
0
VideoFrame::VideoFrame(const uint8_t *data, size_t size, PixelFormat pixelFormat, int width, int height, int align)
    : VideoFrame(pixelFormat, width, height, align)
{
    size_t calcSize = av_image_get_buffer_size(pixelFormat, width, height, align);
    if (calcSize != size)
        throw length_error("Data size and required buffer for this format/width/height/align not equal");

    uint8_t *buf[4];
    int      linesize[4];
    av_image_fill_arrays(buf, linesize, data, pixelFormat, width, height, align);

    // copy data
    for (size_t i = 0; i < 4 && buf[i]; ++i) {
        std::copy(buf[i], buf[i]+linesize[i], m_raw->data[i]);
    }
}
Ejemplo n.º 12
0
AudioSamples::AudioSamples(const uint8_t *data, size_t size, SampleFormat sampleFormat, int samplesCount, uint64_t channelLayout, int sampleRate, int align)
    : AudioSamples(sampleFormat, samplesCount, channelLayout, sampleRate, align)
{
    const auto channels = av_get_channel_layout_nb_channels(channelLayout);
    auto calcSize = sampleFormat.requiredBufferSize(channels, samplesCount, align);

    if (calcSize > size)
        throw length_error("Data size and required buffer for this format/nb_samples/nb_channels/align not equal");

    uint8_t *buf[AV_NUM_DATA_POINTERS];
    int      linesize[AV_NUM_DATA_POINTERS];
    SampleFormat::fillArrays(buf, linesize, data, channels, samplesCount, sampleFormat, align);

    // copy data
    for (size_t i = 0; i < size_t(channels) && i < size_t(AV_NUM_DATA_POINTERS); ++i) {
        std::copy(buf[i], buf[i]+linesize[i], m_raw->data[i]);
    }
}
Ejemplo n.º 13
0
wstring& wstring::replace(size_t pos, size_t n1, const wchar_t* s, size_t n2) {
    const size_t len = length();
    if (pos > len)
        throw out_of_range("pos > len");
    if (n1 > len - pos)
        n1 = len - pos;
    if (len - n1 > max_size() - n2)
        throw length_error("len - n1 > max_size() - n2");
    size_t newlen = len - n1 + n2;
    if (check_realloc(newlen)) {
        Rep *p = Rep::create(newlen);
        p->copy(0, data(), pos);
        p->copy(pos + n2, data() + pos + n1, len -(pos + n1));
        p->copy(pos, s, n2);
        repup(p);
    }
    else {
        rep()->move(pos + n2, data() + pos + n1, len -(pos + n1));
        rep()->copy(pos, s, n2);
    }
    rep()->len = newlen;
    return *this;
}
Ejemplo n.º 14
0
 T max(){
   if(empty() == false)
     return _s.top().second;
   else
     throw length_error("Empty Stack");
 }
Ejemplo n.º 15
0
_UCXXEXPORT void __throw_length_error(const char * message){
	if(message == 0){
		throw length_error();
	}
	throw length_error(message);
}
Ejemplo n.º 16
0
void __throw_length_error(const char* s) { throw length_error(s); }
Ejemplo n.º 17
0
 T pop(){
   if(empty() == true)
     throw length_error("Empty Stack");
   else
     return _s.pop();
 }
Ejemplo n.º 18
0
/**
** WORKS
**/
pair<unsigned int,set<string>> WordSearch::most_frequent_words() const throw (length_error) {
    if (leastFrequent.rbegin() == leastFrequent.rend()) // Err if empty
    	throw length_error ("length error");
    return make_pair(leastFrequent.rbegin()->first,leastFrequent.rbegin()->second); 
// Otherwise return the end of the map (map is already sorted least to greatest)
}
Ejemplo n.º 19
0
        int top() const {
            if (maxHeap_.empty())
                throw length_error("stack empty");

            return maxHeap_.top().value;
        }