Exemplo n.º 1
0
Encrypt::Encrypt(CryptoConfig config, Slice key, Slice iv, Slice entity):
    WithState(State::INITIAL),
    config_(config),
    buffer_(VERSION_SIZE + config.ivLength + config.keyLength),
    version_(buffer_(0, VERSION_SIZE)),
    iv_(buffer_(VERSION_SIZE, VERSION_SIZE + config.ivLength)),
    key_(buffer_(VERSION_SIZE + config.ivLength, VERSION_SIZE + config.ivLength + config.keyLength)),
    entity_(entity.length()),
    tag_(config_.tagLength) {

  checkArgument(key.length() == config_.keyLength, "Invalid key");
  checkArgument(iv.length() == config_.ivLength, "Invalid IV");

  version_[FORMAT_BYTE] = FORMAT_VALUE;
  version_[CONFIG_BYTE] = config_.id;

  // copy the data to memory I own
  iv.copyTo(iv_);
  key.copyTo(key_);
  entity.copyTo(entity_);

  const EVP_CIPHER* cipher = config_.cipher;
  ctx_ = EVP_CIPHER_CTX_new();
  check(ctx_, "Encryption context creation failed");
  int code = EVP_EncryptInit_ex(ctx_, cipher, NULL, NULL, NULL);
  check(code == EVP_SUCCESS, "Encryption context creation failed (cipher)");
  code = EVP_CIPHER_CTX_ctrl(ctx_, EVP_CTRL_GCM_SET_IVLEN, config_.ivLength, NULL);
  check(code == EVP_SUCCESS, "Encryption context creation failed (IV length)");
  code = EVP_EncryptInit_ex(ctx_, NULL, NULL, key_.offset(0), iv_.offset(0));
  check(code == EVP_SUCCESS, "Encryption initialization failed");
}
Exemplo n.º 2
0
T DelayLine<T>::tic(T sample)
{
  // check for no delay
  if( delay_ == 0 )
    return sample;

  T outsample = buffer_( readpt_ );
  buffer_( readpt_ ) = sample;
  readpt_ = (readpt_ % delay_) + 1;

  return outsample;
}
Exemplo n.º 3
0
Slice Encrypt::start() {
  checkState(State::INITIAL, State::PROGRESS, "Start already called");

  updateAad(version_);
  updateAad(entity_);
  return buffer_(0, VERSION_SIZE + config_.ivLength);
}
void MemoryInstrumenter::finish() {
    if(output_ == NULL)
        return;
    int n = cumulatives_.size();
    std::vector<mem_t> cumulator(n);
    std::vector<mem_t> buffer_(n);
    rewind(output_);
    fpos_t pos;
    if(std::find(cumulatives_.begin(), cumulatives_.end(), true) != cumulatives_.end()) {
        while(true) {
            fgetpos(output_, &pos);
            int r = fread(buffer_.data(), sizeof(size_t) * buffer_.size(), 1, output_);
            if(!r)
                break;
            assert(r == 1);
            assert(buffer_[0] > 0);
            for(unsigned int i = 0; i < buffer_.size(); i++) {
                if(cumulatives_[i])
                    cumulator[i] += buffer_[i];
                else
                    cumulator[i] = buffer_[i];
            }
            assert(cumulator[0] > 0);
            fsetpos(output_, &pos);
            r = fwrite(cumulator.data(), sizeof(size_t) * cumulator.size(), 1, output_);
            assert(r == 1);
        }
    }
    fclose(output_);
    output_ = NULL;
    enabled_ = false;
}
Exemplo n.º 5
0
void EncryptBuffer::encryptAndFlush() {
  std::ptrdiff_t n = pptr() - pbase();
  if (n > 0) {
    Slice slice = buffer_(0, n);
    encrypt_.write(slice, slice);
    pbump(-n);
    sink_.sputn(pbase(), n);
  }
}
Exemplo n.º 6
0
bool gpci(int playerid, char *buffer, std::size_t size) {
	static AMX_NATIVE native = Wrapper::GetInstance().GetNative("gpci");
	FakeAmxHeapObject buffer_(size);
	cell params[] = {
		3 * 4,
		playerid,
		buffer_.address(),
		size
	};
	bool ret = FakeAmx::GetInstance().CallBooleanNative(native, params);
	buffer_.GetAsString(buffer, size);
	return ret;
}
Exemplo n.º 7
0
  void verify(mu::MuTick buffer_start, double freq, double ampl, double phas) {
    double omega = (freq * 2.0 * M_PI);
    int n_channels = buffer_.channels();

    for (mu::MuTick tick=buffer_.frames()-1; tick >= 0; tick--) {
      double tau = (tick + buffer_start) / buffer_.dataRate();
      mu::MuFloat expected = ampl * sin(tau * omega + phas);
      for (int channel = n_channels-1; channel >= 0; channel--) {
        mu::MuFloat actual = buffer_(tick, channel);
        ASSERT_DOUBLE_EQ(expected, actual);
      }
    }
  }
Exemplo n.º 8
0
  void Verify(mu::MuTick buffer_start) {
    int n_frames = buffer_.frames();
    int n_channels = buffer_.channels();

    for (int frame=n_frames-1; frame >= 0; frame--) {
      mu::MuFloat expected = frame + buffer_start;
      for (int channel=n_channels-1; channel >= 0; channel--) {
        mu::MuFloat value = buffer_(frame, channel);
        ASSERT_EQ(expected, value) << 
          "at (frame, channel)[tick]=(" << frame << 
          ", " << channel << 
          ")[" << frame + buffer_start << "]";
      }
    }
  }
Exemplo n.º 9
0
int DabOutputRaw::Write(void* buffer, int size)
{
    std::vector<unsigned char> buffer_(6144);
    if ((size_t)size > buffer_.size()) {
        throw std::logic_error("DabOutputRaw::Write size exceeded");
    }

    // Encode data, extend our frame with 0x55 padding
    int i = 0;
    for (; i < size; i++) {
        buffer_[i] = revTable[reinterpret_cast<uint8_t*>(buffer)[i]];
    }
    for (; i < 6144; i++) {
        buffer_[i] = revTable[0x55];
    }

    // Write data
#ifdef _WIN32
    DWORD result;
    if(!DeviceIoControl(socket_, IoctlCodeTxFrame, buffer_.data(), 6144,
        NULL, 0, &result, NULL)) {
            goto RAW_WRITE_ERROR;
    }
#else
    if (isCyclades_) {
        if (write(socket_, buffer_.data() + 1, 6143) != 6143) {
            goto RAW_WRITE_ERROR;
        }
    }
    else {
        int ret = send(socket_, buffer_.data(), 6144, 0);
        if (ret != 6144) {
            etiLog.log(error, "%i/6144 bytes written", ret);
            return -1;
        }
    }
#endif

    return size;

RAW_WRITE_ERROR:
#ifdef _WIN32
    DWORD err = GetLastError();
    LPSTR errMsg;
    if(FormatMessage(
        FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM,
        NULL,
        err,
        0,
        (LPTSTR)&errMsg,
        0,
        NULL) == 0) {
            fprintf(stderr, "Error while writing to raw socket: %i", err);
    } else {
        fprintf(stderr, "Error while writing to raw socket: %s", errMsg);
        LocalFree(errMsg);
    }
#else
    etiLog.level(error) << "Error while writing to raw socket: " <<
        strerror(errno);
#endif

    return -1;
}
Exemplo n.º 10
0
template <typename T> static
void im_load_gray(boost::shared_ptr<TIFF> in_file, bob::io::base::array::interface& b)
{
  const bob::io::base::array::typeinfo& info = b.type();
  const size_t height = info.shape[0];
  const size_t width = info.shape[1];

  // Read in the possibly multiple strips
  tsize_t strip_size = TIFFStripSize(in_file.get());
  tstrip_t n_strips = TIFFNumberOfStrips(in_file.get());

  unsigned long buffer_size = n_strips * strip_size;
  boost::shared_array<unsigned char> buffer_(new unsigned char[buffer_size]);
  unsigned char* buffer = buffer_.get();
  if(buffer == 0) throw std::runtime_error("TIFF: error while getting the color buffer");

  tsize_t result;
  tsize_t image_offset = 0;
  for(tstrip_t strip_count=0; strip_count<n_strips; ++strip_count)
  {
    if((result = TIFFReadEncodedStrip(in_file.get(), strip_count, buffer+image_offset, strip_size)) == -1)
      throw std::runtime_error("TIFF: error in function TIFFReadEncodedStrip()");
    image_offset += result;
  }

  //Comment just to document
  //PHOTOMETRIC_PALETTE: In this model, a color is described with a single component. The value of the component is used as an index into the red, green and blue curves in the ColorMap field to retrieve an RGB triplet that defines the color. When PhotometricInterpretation=3

  // Deal with photometric interpretations
  uint16 photo = PHOTOMETRIC_MINISBLACK;
  if(TIFFGetField(in_file.get(), TIFFTAG_PHOTOMETRIC, &photo) == 0 || (photo != PHOTOMETRIC_MINISBLACK && photo != PHOTOMETRIC_MINISWHITE && photo != PHOTOMETRIC_PALETTE)){
    throw std::runtime_error("TIFF: error in function TIFFGetField()");
  }

  if(photo == PHOTOMETRIC_MINISWHITE)
  {
    // Flip bits
    for(unsigned long count=0; count<buffer_size; ++count)
      buffer[count] = ~buffer[count];
  }

  // Deal with fillorder
  uint16 fillorder = FILLORDER_MSB2LSB;
  TIFFGetField(in_file.get(), TIFFTAG_FILLORDER, &fillorder);

  if(fillorder != FILLORDER_MSB2LSB) {
    // We need to swap bits -- ABCDEFGH becomes HGFEDCBA
    for(unsigned long count=0; count<buffer_size; ++count)
    {
      unsigned char tempbyte = 0;
      if(buffer[count] & 128) tempbyte += 1;
      if(buffer[count] & 64) tempbyte += 2;
      if(buffer[count] & 32) tempbyte += 4;
      if(buffer[count] & 16) tempbyte += 8;
      if(buffer[count] & 8) tempbyte += 16;
      if(buffer[count] & 4) tempbyte += 32;
      if(buffer[count] & 2) tempbyte += 64;
      if(buffer[count] & 1) tempbyte += 128;
      buffer[count] = tempbyte;
    }
  }

  // Copy to output array
  T *element = reinterpret_cast<T*>(b.ptr());
  T *b_in = reinterpret_cast<T*>(buffer);
  memcpy(element, b_in, height*width*sizeof(T));
}
Exemplo n.º 11
0
template <typename T> static
void im_load_color(boost::shared_ptr<TIFF> in_file, bob::io::base::array::interface& b)
{
  const bob::io::base::array::typeinfo& info = b.type();
  const size_t height = info.shape[1];
  const size_t width = info.shape[2];
  const size_t frame_size = height*width;
  const size_t row_stride = width;
  const size_t row_color_stride = 3*width;

  // Read in the possibly multiple strips
  tsize_t strip_size = TIFFStripSize(in_file.get());
  tstrip_t n_strips = TIFFNumberOfStrips(in_file.get());

  unsigned long buffer_size = n_strips * strip_size;
  boost::shared_array<unsigned char> buffer_(new unsigned char[buffer_size]);
  unsigned char* buffer = buffer_.get();
  if(buffer == 0) throw std::runtime_error("TIFF: error while getting the color buffer");

  tsize_t result;
  tsize_t image_offset = 0;
  for(tstrip_t strip_count=0; strip_count<n_strips; ++strip_count)
  {
    if((result = TIFFReadEncodedStrip(in_file.get(), strip_count, buffer+image_offset, strip_size)) == -1)
      throw std::runtime_error("TIFF: error in function TIFFReadEncodedStrip()");

    image_offset += result;
  }

  // Deal with photometric interpretations
  uint16 photo = PHOTOMETRIC_RGB;
  if(TIFFGetField(in_file.get(), TIFFTAG_PHOTOMETRIC, &photo) == 0 || photo != PHOTOMETRIC_RGB)
    throw std::runtime_error("TIFF: error in function TIFFGetField()");

  // Deal with fillorder
  uint16 fillorder = FILLORDER_MSB2LSB;
  TIFFGetField(in_file.get(), TIFFTAG_FILLORDER, &fillorder);

  if(fillorder != FILLORDER_MSB2LSB) {
    // We need to swap bits -- ABCDEFGH becomes HGFEDCBA
    for(unsigned long count=0; count<(unsigned long)image_offset; ++count)
    {
      unsigned char tempbyte = 0;
      if(buffer[count] & 128) tempbyte += 1;
      if(buffer[count] & 64) tempbyte += 2;
      if(buffer[count] & 32) tempbyte += 4;
      if(buffer[count] & 16) tempbyte += 8;
      if(buffer[count] & 8) tempbyte += 16;
      if(buffer[count] & 4) tempbyte += 32;
      if(buffer[count] & 2) tempbyte += 64;
      if(buffer[count] & 1) tempbyte += 128;
      buffer[count] = tempbyte;
    }
  }

  // Read the image (one row at a time)
  // This can deal with interlacing
  T *element_r = reinterpret_cast<T*>(b.ptr());
  T *element_g = element_r + frame_size;
  T *element_b = element_g + frame_size;
  unsigned char *row_pointer = buffer;
  // Loop over the rows
  for(size_t y=0; y<height; ++y)
  {
    imbuffer_to_rgb(row_stride, reinterpret_cast<T*>(row_pointer), element_r, element_g, element_b);
    element_r += row_stride;
    element_g += row_stride;
    element_b += row_stride;
    row_pointer += row_color_stride * sizeof(T);
  }
}