/*
 * Copyright (C) 2004-2007  Andrea Arcangeli <andrea@cpushare.com>
 *                    2008  Sam Hocevar <sam@zoy.org>
 * 
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation;
 * only version 2.1 of the License.
 *
 * This library 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
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */

#include <signal.h>
#include <math.h>
#include <string.h>

#include <seccomp-loader.h>

char x[1024] = "this is just here because the CPUShare server cannot "
               "handle bytecode with no .data section";

volatile int stop;

void sighandler(int signal)
{
    if(sys_write(1, "done", 4) != 4)
        sys_exit(3);
    stop = 1;
}

void bytecode(unsigned char * mem, int heap_size, int stack_size)
{
    unsigned char n;
    char c;

    if(sys_read(0, (char *)&n, 1) != 1)
        sys_exit(-5);

    while(n-- && !stop)
    {
        if(sys_read(0, &c, 1) != 1)
            sys_exit(-5);

        if(c >= 'a' && c <= 'z')
            c = 'a' + ((c - 'a' + 13) % 26);
        else if(c >= 'A' && c <= 'Z')
            c = 'A' + ((c - 'A' + 13) % 26);

        if(sys_write(1, &c, 1) != 1)
            sys_exit(-6);
    }

    sys_exit(0);
}