Skip to content

Commit

Permalink
Updating passthrough judge and corresponding wrapper to handle both s…
Browse files Browse the repository at this point in the history
…tdout and stderr.
  • Loading branch information
Martin Krulis committed Dec 5, 2020
1 parent b023aae commit 45f6b83
Show file tree
Hide file tree
Showing 3 changed files with 135 additions and 11 deletions.
2 changes: 1 addition & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
cmake_minimum_required(VERSION 2.8.11)
project(recodex-worker)
set(RECODEX_VERSION 1.6.4)
set(RECODEX_VERSION 1.7.1)
enable_testing()

# Hack for old CentOS and new GCC with uncompatible ABI
Expand Down
131 changes: 123 additions & 8 deletions judges/judge_passthrough/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,105 @@
using namespace std;


/**
* Class taken from a snippet
* https://gist.github.com/tomykaira/f0fd86b6c73063283afe550bc5d77594
*/
class Base64 {
public:
static std::string Encode(const std::string data) {
static constexpr char sEncodingTable[] = {
'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H',
'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P',
'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X',
'Y', 'Z', 'a', 'b', 'c', 'd', 'e', 'f',
'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n',
'o', 'p', 'q', 'r', 's', 't', 'u', 'v',
'w', 'x', 'y', 'z', '0', '1', '2', '3',
'4', '5', '6', '7', '8', '9', '+', '/'
};

size_t in_len = data.size();
size_t out_len = 4 * ((in_len + 2) / 3);
std::string ret(out_len, '\0');
size_t i;
char *p = const_cast<char*>(ret.c_str());

for (i = 0; i < in_len - 2; i += 3) {
*p++ = sEncodingTable[(data[i] >> 2) & 0x3F];
*p++ = sEncodingTable[((data[i] & 0x3) << 4) | ((int) (data[i + 1] & 0xF0) >> 4)];
*p++ = sEncodingTable[((data[i + 1] & 0xF) << 2) | ((int) (data[i + 2] & 0xC0) >> 6)];
*p++ = sEncodingTable[data[i + 2] & 0x3F];
}
if (i < in_len) {
*p++ = sEncodingTable[(data[i] >> 2) & 0x3F];
if (i == (in_len - 1)) {
*p++ = sEncodingTable[((data[i] & 0x3) << 4)];
*p++ = '=';
}
else {
*p++ = sEncodingTable[((data[i] & 0x3) << 4) | ((int) (data[i + 1] & 0xF0) >> 4)];
*p++ = sEncodingTable[((data[i + 1] & 0xF) << 2)];
}
*p++ = '=';
}

return ret;
}

static std::string Decode(const std::string& input, std::string& out) {
static constexpr unsigned char kDecodingTable[] = {
64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64,
64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64,
64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 62, 64, 64, 64, 63,
52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 64, 64, 64, 64, 64, 64,
64, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14,
15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 64, 64, 64, 64, 64,
64, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40,
41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 64, 64, 64, 64, 64,
64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64,
64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64,
64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64,
64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64,
64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64,
64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64,
64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64,
64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64
};

size_t in_len = input.size();
if (in_len % 4 != 0) return "Input data size is not a multiple of 4";

size_t out_len = in_len / 4 * 3;
if (input[in_len - 1] == '=') out_len--;
if (input[in_len - 2] == '=') out_len--;

out.resize(out_len);

for (size_t i = 0, j = 0; i < in_len;) {
uint32_t a = input[i] == '=' ? 0 & i++ : kDecodingTable[static_cast<int>(input[i++])];
uint32_t b = input[i] == '=' ? 0 & i++ : kDecodingTable[static_cast<int>(input[i++])];
uint32_t c = input[i] == '=' ? 0 & i++ : kDecodingTable[static_cast<int>(input[i++])];
uint32_t d = input[i] == '=' ? 0 & i++ : kDecodingTable[static_cast<int>(input[i++])];

uint32_t triple = (a << 3 * 6) + (b << 2 * 6) + (c << 1 * 6) + (d << 0 * 6);

if (j < out_len) out[j++] = (triple >> 2 * 8) & 0xFF;
if (j < out_len) out[j++] = (triple >> 1 * 8) & 0xFF;
if (j < out_len) out[j++] = (triple >> 0 * 8) & 0xFF;
}

return "";
}

};


/*
* Application entry point.
*/
int main(int argc, char **argv) {

int main(int argc, char **argv)
{
// Check amount of program arguments.
if (argc != 3) {
cerr << "Wrong amount of arguments." << endl;
Expand All @@ -35,15 +129,36 @@ int main(int argc, char **argv) {
ifstream inputFile(argv[1]);

// Get the first line which holds the exit code
std::string firstLine;
getline(inputFile, firstLine);
int exitCode = stoi(firstLine);
if (exitCode < 0 || exitCode > 255) {
throw runtime_error("Invalid exit code value.");
std::string line, decoded, result;
int exitCode = 0;
if (getline(inputFile, line)) {
exitCode = stoi(line);
if (exitCode < 0 || exitCode > 255) {
throw runtime_error("Invalid exit code value.");
}
} else {
throw runtime_error("No exit code value specified.");
}

// Get the stdout and decode it
if (getline(inputFile, line)) {
result = Base64::Decode(line, decoded);
if (!result.empty()) {
throw runtime_error(result);
}
cout << decoded;
}

// Get the stderr and decode it
if (getline(inputFile, line)) {
result = Base64::Decode(line, decoded);
if (!result.empty()) {
throw runtime_error(result);
}
cerr << decoded;
}

// Copy the rest of the input file to the output...
cout << inputFile.rdbuf();
inputFile.close();

return exitCode;
Expand Down
13 changes: 11 additions & 2 deletions judges/judge_passthrough/recodex-data-only-wrapper.sh
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,15 @@ if [[ "$EXECUTABLE" == *.jar ]]; then
EXECUTABLE="java -jar $EXECUTABLE"
fi

OUTPUT=`"$EXECUTABLE" "$@"`
TMPFILE=`mktemp`
OUTPUT=`"$EXECUTABLE" "$@" 2>"$TMPFILE"`

echo $?
echo "$OUTPUT"

echo "$OUTPUT" | base64 -w 0
echo

cat "$TMPFILE" | base64 -w 0
echo

rm -rf "$TMPFILE" 1>/dev/null 2>/dev/null

0 comments on commit 45f6b83

Please sign in to comment.