示例#1
1
judge_result testcase_impl::run(env &env, compiler::result &cr)
{
	judge_result result = {0};
	shared_ptr<temp_dir> dir = _prepare_dir(env.pool(), cr);
	env.grant_access(dir->path());

	path_a executable_path;
	if (cr.compiler->target_executable_path().empty()) {
		executable_path = dir->path();
		executable_path.push(cr.compiler->target_filename().c_str());
	} else {
		executable_path = cr.compiler->target_executable_path();
	}

	shared_ptr<testcase_impl::context> context(new testcase_impl::context(*this));
	judge::bunny bunny(env, false, executable_path.c_str(), cr.compiler->target_command_line(), dir->path(),
		context->stdin_pipe.read_handle(), context->stdout_pipe.write_handle(), context->stderr_pipe.write_handle(), limit_);
	context->stdin_pipe.close_read();
	context->stdout_pipe.close_write();
	context->stderr_pipe.close_write();

	// stdin thread
	env.pool().thread_pool().queue([context]()->void {
		try {
			istream in(&context->stdin_buf);
			os_filebuf out_buf(context->stdin_pipe.write_handle(), false);
			ostream out(&out_buf);
			const size_t buffer_size = 4096;
			util::stream_copy<buffer_size>(in, out);
		} catch (...) {
		}
		context->stdin_pipe.close_write();
		context->stdin_event.set();
	});

	// stderr thread
	env.pool().thread_pool().queue([context]()->void {
		try {
			os_filebuf in_buf(context->stderr_pipe.read_handle(), false);
			istream in(&in_buf);
			const size_t buffer_size = 4096;
			util::stream_copy_n<buffer_size>(in, context->stderr_stream, context->stderr_output_limit);
		} catch (...) {
		}
		context->stderr_pipe.close_read();
		context->stderr_event.set();
	});

	bunny.start();

	// judge
	{
		istream model_in(&context->stdout_buf);
		os_filebuf user_buf(context->stdout_pipe.read_handle(), false);
		istream user_in(&user_buf);
		pair<bool, string> compare_result = compare_stream(model_in, user_in);
		if (compare_result.first) {
			result.flag = max(result.flag, JUDGE_ACCEPTED);
		} else {
			result.flag = max(result.flag, JUDGE_WRONG_ANSWER);
		}
		judge_output_ = move(compare_result.second);

		// read all user output
		const size_t buffer_size = 4096;
		util::stream_copy<buffer_size>(user_in, onullstream());
	}

	bunny::result bunny_result = bunny.wait();
	DWORD wait_result = winstl::wait_for_multiple_objects(context->stdin_event, context->stderr_event, true, INFINITE);
	if (wait_result == WAIT_FAILED) {
		throw win32_exception(::GetLastError());
	}

	result.flag = max(result.flag, bunny_result.flag);
	result.time_usage_ms = bunny_result.time_usage_ms;
	result.memory_usage_kb = bunny_result.memory_usage_kb;
	result.runtime_error = bunny_result.runtime_error;
	result.judge_output = judge_output_.c_str();
	user_output_ = context->stderr_stream.str();
	result.user_output = user_output_.c_str();
	return result;
}
示例#2
0
    bool ResourceInfoListFile::GetResourceInfo(FileResourceInfo &r_info, bool & bEof)
    {
        static boost::uint32_t item_max_len = 10 * 1024;
        boost::uint32_t item_len = 0;
        boost::uint32_t r_len = SecRead((boost::uint8_t*) &item_len, 4);
        bEof = false;
        if (r_len != 4)
        {
            // 已经读到文件尾部
            bEof = true;
            return false;
        }

        // herain:2010-12-31:如果item_len小于4,那么分配的缓冲区也将小于4字节
        // 这样在向缓冲区内写入4字节数据后将造成堆被破坏,产生不确定的行为
        if (item_len > item_max_len || item_len < 4)
        {
            return false;
        }

        base::AppBuffer in_buf(item_len);
        base::util::memcpy2((void*)in_buf.Data(), in_buf.Length(), (void*)(&item_len), sizeof(int));
        r_len = SecRead(in_buf.Data() + 4, item_len - 4);
        if (r_len != item_len - 4)
        {
            return false;
        }
        return r_info.Parse(in_buf, HeadVersion());
    }
示例#3
0
文件: tga.cpp 项目: adekto/nya-engine
bool tga_file::load(const char *file_name)
{
    release();

    nya_resources::resource_data *in_data=nya_resources::get_resources_provider().access(file_name);
    if(!in_data)
    {
        printf( "unable to open texture %s\n", file_name );
        return false;
    }

    nya_memory::tmp_buffer_scoped in_buf(in_data->get_size());
    in_data->read_all(in_buf.get_data());
    m_header.decode_header(in_buf.get_data(),in_data->get_size());
    in_data->release();

    if(m_header.rle)
    {
        m_data.resize(m_header.compressed_size);
        memcpy(&m_data[0],m_header.data,m_header.compressed_size);
    }
    else
    {
        m_data.resize(m_header.uncompressed_size);
        memcpy(&m_data[0],m_header.data,m_header.uncompressed_size);
    }

    return true;
}
示例#4
0
static int	add_buffer(t_main *c, char *buf, int len)
{
  int		i;
  int		start;
  int		end;

  i = 0;
  while (i != len)
    if (!in_buf(c, buf[i++]))
      break;
  start = c->start;
  end = c->end;
  while (start != end)
    {
      if (c->buf[start] == '\n')
	return (out_buf(c, 0, start));
      start = (start + 1) % BUF_SIZE;
    }
  c->start = start;
  return (0);
}
示例#5
0
bool CompressFileToBlob(const std::string& infile_path, const std::string& outfile_path,
                        u32 sub_type, int block_size, CompressCB callback, void* arg)
{
  bool scrubbing = false;

  File::IOFile infile(infile_path, "rb");
  if (IsGCZBlob(infile))
  {
    PanicAlertT("\"%s\" is already compressed! Cannot compress it further.", infile_path.c_str());
    return false;
  }

  if (!infile)
  {
    PanicAlertT("Failed to open the input file \"%s\".", infile_path.c_str());
    return false;
  }

  File::IOFile outfile(outfile_path, "wb");
  if (!outfile)
  {
    PanicAlertT("Failed to open the output file \"%s\".\n"
                "Check that you have permissions to write the target folder and that the media can "
                "be written.",
                outfile_path.c_str());
    return false;
  }

  DiscScrubber disc_scrubber;
  if (sub_type == 1)
  {
    if (!disc_scrubber.SetupScrub(infile_path, block_size))
    {
      PanicAlertT("\"%s\" failed to be scrubbed. Probably the image is corrupt.",
                  infile_path.c_str());
      return false;
    }

    scrubbing = true;
  }

  z_stream z = {};
  if (deflateInit(&z, 9) != Z_OK)
    return false;

  callback(GetStringT("Files opened, ready to compress."), 0, arg);

  CompressedBlobHeader header;
  header.magic_cookie = GCZ_MAGIC;
  header.sub_type = sub_type;
  header.block_size = block_size;
  header.data_size = infile.GetSize();

  // round upwards!
  header.num_blocks = (u32)((header.data_size + (block_size - 1)) / block_size);

  std::vector<u64> offsets(header.num_blocks);
  std::vector<u32> hashes(header.num_blocks);
  std::vector<u8> out_buf(block_size);
  std::vector<u8> in_buf(block_size);

  // seek past the header (we will write it at the end)
  outfile.Seek(sizeof(CompressedBlobHeader), SEEK_CUR);
  // seek past the offset and hash tables (we will write them at the end)
  outfile.Seek((sizeof(u64) + sizeof(u32)) * header.num_blocks, SEEK_CUR);
  // seek to the start of the input file to make sure we get everything
  infile.Seek(0, SEEK_SET);

  // Now we are ready to write compressed data!
  u64 position = 0;
  int num_compressed = 0;
  int num_stored = 0;
  int progress_monitor = std::max<int>(1, header.num_blocks / 1000);
  bool success = true;

  for (u32 i = 0; i < header.num_blocks; i++)
  {
    if (i % progress_monitor == 0)
    {
      const u64 inpos = infile.Tell();
      int ratio = 0;
      if (inpos != 0)
        ratio = (int)(100 * position / inpos);

      std::string temp =
          StringFromFormat(GetStringT("%i of %i blocks. Compression ratio %i%%").c_str(), i,
                           header.num_blocks, ratio);
      bool was_cancelled = !callback(temp, (float)i / (float)header.num_blocks, arg);
      if (was_cancelled)
      {
        success = false;
        break;
      }
    }

    offsets[i] = position;

    size_t read_bytes;
    if (scrubbing)
      read_bytes = disc_scrubber.GetNextBlock(infile, in_buf.data());
    else
      infile.ReadArray(in_buf.data(), header.block_size, &read_bytes);
    if (read_bytes < header.block_size)
      std::fill(in_buf.begin() + read_bytes, in_buf.begin() + header.block_size, 0);

    int retval = deflateReset(&z);
    z.next_in = in_buf.data();
    z.avail_in = header.block_size;
    z.next_out = out_buf.data();
    z.avail_out = block_size;

    if (retval != Z_OK)
    {
      ERROR_LOG(DISCIO, "Deflate failed");
      success = false;
      break;
    }

    int status = deflate(&z, Z_FINISH);
    int comp_size = block_size - z.avail_out;

    u8* write_buf;
    int write_size;
    if ((status != Z_STREAM_END) || (z.avail_out < 10))
    {
      // PanicAlert("%i %i Store %i", i*block_size, position, comp_size);
      // let's store uncompressed
      write_buf = in_buf.data();
      offsets[i] |= 0x8000000000000000ULL;
      write_size = block_size;
      num_stored++;
    }
    else
    {
      // let's store compressed
      // PanicAlert("Comp %i to %i", block_size, comp_size);
      write_buf = out_buf.data();
      write_size = comp_size;
      num_compressed++;
    }

    if (!outfile.WriteBytes(write_buf, write_size))
    {
      PanicAlertT("Failed to write the output file \"%s\".\n"
                  "Check that you have enough space available on the target drive.",
                  outfile_path.c_str());
      success = false;
      break;
    }

    position += write_size;

    hashes[i] = HashAdler32(write_buf, write_size);
  }

  header.compressed_data_size = position;

  if (!success)
  {
    // Remove the incomplete output file.
    outfile.Close();
    File::Delete(outfile_path);
  }
  else
  {
    // Okay, go back and fill in headers
    outfile.Seek(0, SEEK_SET);
    outfile.WriteArray(&header, 1);
    outfile.WriteArray(offsets.data(), header.num_blocks);
    outfile.WriteArray(hashes.data(), header.num_blocks);
  }

  // Cleanup
  deflateEnd(&z);

  if (success)
  {
    callback(GetStringT("Done compressing disc image."), 1.0f, arg);
  }
  return success;
}
示例#6
0
文件: decoder.c 项目: neutony/upb
static bool in_residual_buf(const upb_pbdecoder *d, const char *p) {
  return in_buf(p, d->residual, d->residual_end);
}