forked from blackav/ejudge
-
Notifications
You must be signed in to change notification settings - Fork 0
/
compile_packet_4.c
124 lines (108 loc) · 4.36 KB
/
compile_packet_4.c
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
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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
/* -*- c -*- */
/* Copyright (C) 2005-2016 Alexander Chernov <[email protected]> */
/*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*/
#include "ejudge/ej_types.h"
#include "ejudge/ej_limits.h"
#include "ejudge/ej_byteorder.h"
#include "ejudge/compile_packet.h"
#include "ejudge/compile_packet_priv.h"
#include "ejudge/pathutl.h"
#include "ejudge/errlog.h"
#include "ejudge/prepare.h"
#include "ejudge/runlog.h"
#include "ejudge/xalloc.h"
#include "ejudge/logger.h"
#include "ejudge/integral.h"
#include <stdlib.h>
#include <string.h>
#define FAIL_IF(c) if (c)do { errcode = __LINE__; goto failed; } while (0)
int
compile_reply_packet_read(
size_t in_size,
const void *in_data,
struct compile_reply_packet **p_out_data)
{
struct compile_reply_packet *pout = 0;
const struct compile_reply_bin_packet *pin = in_data;
int errcode = 0, pkt_size, pkt_version;
const unsigned char *in_ptr, *end_ptr;
FAIL_IF(in_size < sizeof(*pin));
pkt_size = cvt_bin_to_host_32(pin->packet_len);
FAIL_IF(pkt_size != in_size);
FAIL_IF(pkt_size < 0 || pkt_size > EJ_MAX_COMPILE_PACKET_SIZE);
FAIL_IF((pkt_size & 0xf));
pkt_version = cvt_bin_to_host_32(pin->version);
FAIL_IF(pkt_version != 1);
XCALLOC(pout, 1);
pout->judge_id = cvt_bin_to_host_32(pin->judge_id);
FAIL_IF(pout->judge_id < 0 || pout->judge_id > EJ_MAX_JUDGE_ID);
pout->contest_id = cvt_bin_to_host_32(pin->contest_id);
FAIL_IF(pout->contest_id <= 0 || pout->contest_id > EJ_MAX_CONTEST_ID);
pout->run_id = cvt_bin_to_host_32(pin->run_id);
FAIL_IF(pout->run_id < 0 || pout->run_id > EJ_MAX_RUN_ID);
// OK, COMPILE_ERR, CHECK_FAILED are allowed
pout->status = cvt_bin_to_host_32(pin->status);
FAIL_IF(pout->status != RUN_OK && pout->status != RUN_COMPILE_ERR && pout->status != RUN_CHECK_FAILED && pout->status != RUN_STYLE_ERR);
pout->ts1 = cvt_bin_to_host_32(pin->ts1);
pout->ts1_us = cvt_bin_to_host_32(pin->ts1_us);
FAIL_IF(pout->ts1_us < 0 || pout->ts1_us > USEC_MAX);
pout->ts2 = cvt_bin_to_host_32(pin->ts2);
pout->ts2_us = cvt_bin_to_host_32(pin->ts2_us);
FAIL_IF(pout->ts2_us < 0 || pout->ts2_us > USEC_MAX);
pout->ts3 = cvt_bin_to_host_32(pin->ts3);
pout->ts3_us = cvt_bin_to_host_32(pin->ts3_us);
FAIL_IF(pout->ts3_us < 0 || pout->ts3_us > USEC_MAX);
pout->use_uuid = cvt_bin_to_host_32(pin->use_uuid);
pout->uuid.v[0] = cvt_bin_to_host_32(pin->uuid.v[0]);
pout->uuid.v[1] = cvt_bin_to_host_32(pin->uuid.v[1]);
pout->uuid.v[2] = cvt_bin_to_host_32(pin->uuid.v[2]);
pout->uuid.v[3] = cvt_bin_to_host_32(pin->uuid.v[3]);
pout->zip_mode = cvt_bin_to_host_32(pin->zip_mode);
in_ptr = (const unsigned char*) pin + sizeof(*pin);
end_ptr = (const unsigned char*) pin + pkt_size;
pout->run_block_len = cvt_bin_to_host_32(pin->run_block_len);
FAIL_IF(pout->run_block_len < 0 || pout->run_block_len > EJ_MAX_COMPILE_RUN_BLOCK_LEN);
if (pout->run_block_len > 0) {
FAIL_IF(in_ptr + pout->run_block_len > end_ptr);
pout->run_block = xmalloc(pout->run_block_len);
memcpy(pout->run_block, in_ptr, pout->run_block_len);
in_ptr += pout->run_block_len;
}
pkt_bin_align_addr(in_ptr, pin);
FAIL_IF(in_ptr != end_ptr);
#if 0
/* debug */
fprintf(stderr,
"compile reply packet:\n"
" judge_id: %d\n"
" contest_id: %d\n"
" run_id: %d\n"
" status: %d\n"
" ts1: %d\n"
" ts1_us: %d\n"
" ts2: %d\n"
" ts2_us: %d\n"
" ts3: %d\n"
" ts3_us: %d\n"
" run_block_len: %d\n",
pout->judge_id, pout->contest_id, pout->run_id, pout->status,
pout->ts1, pout->ts1_us, pout->ts2, pout->ts2_us, pout->ts3, pout->ts3_us,
pout->run_block_len);
#endif
*p_out_data = pout;
return 0;
failed:
err("compile_reply_packet_read: error %s, %d", "$Revision$", errcode);
compile_reply_packet_free(pout);
return -1;
}