2 * Empire - A multi-player, client/server Internet based war game.
3 * Copyright (C) 1986-2007, Dave Pare, Jeff Bailey, Thomas Ruschak,
4 * Ken Stevens, Steve McClure
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22 * See files README, COPYING and CREDITS in the root of the source
23 * tree for related information and legal notices. It is expected
24 * that future projects/authors will amend these files as needed.
28 * secure.c: Check redir etc. to protect against tampering deity
30 * Known contributors to this file:
31 * Markus Armbruster, 2007
41 struct ring recent_input;
42 static size_t saved_bytes;
45 * Remember line of input INP for a while.
46 * It must end with a newline.
47 * Return value is suitable for forget_input(): it makes it forget all
48 * input up to and including this line.
53 size_t len = strlen(inp);
56 assert(len && inp[len - 1] == '\n');
58 left = ring_putm(&recent_input, inp, len);
60 ring_discard(&recent_input, ring_search(&recent_input, "\n"));
61 ring_putm(&recent_input, inp, len);
68 * Can you still remember a line of input that ends with TAIL?
69 * It must end with a newline.
70 * Return non-zero iff TAIL can be remembered.
71 * Passing that value to forget_input() will forget all input up to
72 * and including this line.
75 seen_input(char *tail)
77 size_t len = strlen(tail);
78 size_t remembered = ring_len(&recent_input);
81 assert(len && tail[len - 1] == '\n');
83 dist = ring_search(&recent_input, tail);
87 assert(dist + len <= remembered && remembered <= saved_bytes);
88 return saved_bytes - remembered + dist + len;
92 * Forget remembered input up to SEEN.
93 * SEEN should be obtained from save_input() or seen_input().
96 forget_input(size_t seen)
98 size_t forgotten = saved_bytes - ring_len(&recent_input);
102 if (seen > forgotten) {
103 assert(ring_peek(&recent_input, seen - forgotten - 1) == '\n');
104 ring_discard(&recent_input, seen - forgotten);