Merge branch 'master' of github.com:r0ket/r0ket

This commit is contained in:
Stefan `Sec` Zehl 2011-08-02 20:46:29 +02:00
commit f7fcaf0ab8
5 changed files with 356 additions and 231 deletions

View File

@ -37,11 +37,12 @@ struct gamestate {
char shots_x[ENEMY_COLUMNS]; char shots_x[ENEMY_COLUMNS];
char shots_y[ENEMY_COLUMNS]; char shots_y[ENEMY_COLUMNS];
char alive; char alive;
char move, direction, lastcol; int16_t move;
char direction, lastcol;
bool killed; bool killed;
bool step; bool step;
uint32_t score; uint32_t score;
char level; uint16_t level;
int8_t rokets; int8_t rokets;
char enemy_x[ENEMY_ROWS][ENEMY_COLUMNS]; char enemy_x[ENEMY_ROWS][ENEMY_COLUMNS];
char enemy_row_y[ENEMY_ROWS]; char enemy_row_y[ENEMY_ROWS];
@ -268,7 +269,7 @@ void move_shots() {
if (game.shots_x[col] == DISABLED) { if (game.shots_x[col] == DISABLED) {
for (char row = 0; row<ENEMY_ROWS; row++) { for (char row = 0; row<ENEMY_ROWS; row++) {
if (game.enemy_x[row][col] != DISABLED) { if (game.enemy_x[row][col] != DISABLED) {
if(getRandom()%(game.alive*5)==0) { if(getRandom()%(game.alive*20/((game.level/3)+1))==0) {
game.shots_x[col] = game.enemy_x[row][col]+5; game.shots_x[col] = game.enemy_x[row][col]+5;
game.shots_y[col] = game.enemy_row_y[row]+0; game.shots_y[col] = game.enemy_row_y[row]+0;
} }
@ -343,7 +344,7 @@ void move_player() {
void move_enemy() { void move_enemy() {
if(game.move > 0){ if(game.move > 0){
game.move--; game.move-=game.level/5+1;
return; return;
} }
@ -380,7 +381,7 @@ void move_enemy() {
(pos >=RESX-11-1 && game.direction == 1)){ (pos >=RESX-11-1 && game.direction == 1)){
game.direction = (game.direction==1)?-1:1; game.direction = (game.direction==1)?-1:1;
for (char r = 0; r<ENEMY_ROWS; r++) { for (char r = 0; r<ENEMY_ROWS; r++) {
game.enemy_row_y[r]+=2; game.enemy_row_y[r]+=game.level>=23?4:2;
} }
return; return;
} }
@ -389,7 +390,7 @@ void move_enemy() {
} }
} }
game.move = game.alive-1; game.move = game.alive*2-1;
} }
void draw_player() { void draw_player() {
@ -476,7 +477,7 @@ void check_end() {
if (game.killed) { if (game.killed) {
game.rokets--; game.rokets--;
delayms(500); delayms(500);
game.player = RESX/2+4; game.player = RESX/2-4;
for(int col=0; col<ENEMY_COLUMNS; col++) { for(int col=0; col<ENEMY_COLUMNS; col++) {
game.shots_x[col] = DISABLED; game.shots_x[col] = DISABLED;

View File

@ -1,18 +1,13 @@
CC = gcc CC = gcc
LD = gcc CFLAGS = -Wall -O2 -std=c99 -DSAFE
LDFLAGS = -Wall -O2 -std=c99 EXE = xxtea
EXES = xxtea FILES = main.c xxtea.c
OBJS = main.o xxtea.o
TESTFILE= test.out TESTFILE= test.out
all: $(EXES) all: $(FILES)
$(CC) $(CFLAGS) $(FILES) -o $(EXE)
% : %.c
$(LD) $(LDFLAGS) -o $@ $<
clean: clean:
rm -f $(EXES) $(TESTFILE) rm -f $(EXE) $(OBJS)
tests: all
./xxtea -o $(TESTFILE) test/in.1
cmp $(TESTFILE) test/out.1
$(RM) $(TESTFILE)

255
tools/crypto/main.c Normal file
View File

@ -0,0 +1,255 @@
/* simple XXTEA en/decrypt utility
*
* BSD Licence
*
* btea function is from
* <https://secure.wikimedia.org/wikipedia/en/wiki/XXTEA#Reference_code>
*
* (c) by Sec <sec@42.org> 6/2011
*/
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdint.h>
#include <getopt.h>
#include "xxtea.h"
// default block size
void hexkey(char *string, uint32_t k[4]);
int main(int argc, char *argv[]) {
FILE *fp;
FILE *ofp;
char *prog;
char c; /* for getopt */
uint32_t k[4]; /* key */
uint8_t *buf;
/* Default values */
char verbose=0; // be silent
k[0]=0; k[1]=0; k[2]=0; k[3]=0;
char *outfile=NULL; // outfile == infile
int decrypt=0;
int encrypt=0;
int sign=0;
/* init section */
prog=argv[0];
if(!prog)prog="xxtea";
if(strrchr(prog,'/')){
prog=strrchr(argv[0],'/');
prog++;
}
/* The big getopt loop */
while ((c = getopt(argc, argv, "vhesdk:o:")) != EOF)
switch (c) {
case 'v':
verbose++;
break;
case 'd':
decrypt=1;
break;
case 'e':
encrypt=1;
break;
case 's':
sign=1;
break;
case 'k':
hexkey(optarg, k);
break;
case 'o':
outfile=optarg;
break;
case 'h':
default:
fprintf(stderr, "Usage: %s [options] filename\n\n"
"This program en/decrypts a file with the XXTEA algorithm\n"
"\n\n"
"-v Be verbose (-v -v = even more)\n"
"-d Decrypt file\n"
"-e Encrypt file\n"
"-s Sign file\n\n"
"-o file Output to <file>. (Default: overwrite input file)\n"
"-k key 128bit hex key.\n"
"-h This help\n\n"
"\n",prog);
exit(255);
}
argc -= optind; argv += optind;
if (argc !=1){
fprintf(stderr,"Error: No filename given!\n");
exit(254);
};
if( !(decrypt||encrypt||sign) ){
fprintf(stderr, "No operation specified!\n");
exit(254);
}
if( decrypt && (encrypt || sign) ){
fprintf(stderr, "Can not decrypt and sign or decrypt and encrypt!\n");
exit(254);
}
if(outfile){
if ((fp = fopen(argv[0],"rb")) == NULL){
fprintf(stderr,"Error: Can't open file %s\n",argv[0]);
exit(253);
}
if ((ofp = fopen(outfile,"wb")) == NULL){
fprintf(stderr,"Error: Can't open file %s\n",argv[0]);
exit(253);
}
}else{
if ((fp = fopen(argv[0],"r+b")) == NULL){
fprintf(stderr,"Error: Can't open file %s\n",argv[0]);
exit(253);
}
ofp=fp;
};
fseek(fp, 0L, SEEK_END);
int filesize = ftell(fp);
fseek(fp, 0L, SEEK_SET);
if (verbose)
fprintf(stderr,"file size=%d\n", filesize);
int words = (filesize+3)/sizeof(uint32_t);
int bytes = sizeof(uint32_t)*words;
if (verbose)
fprintf(stderr,"byte count=%d word count=%d\n", bytes, words);
buf=malloc(bytes);
if(!buf){
fprintf(stderr,"Error: malloc() failed.\n");
exit(253);
};
if (verbose)
fprintf(stderr,"Key: %08x %08x %08x %08x\n",k[0],k[1],k[2],k[3]);
if( sign ){
if (verbose)
fprintf(stderr,"Signing: ");
memset(buf, 0, bytes);
int cnt = fread(buf,sizeof(*buf),filesize,fp);
uint32_t mac[4];
xxtea_cbcmac(mac, (uint32_t*)buf, words, k);
if (verbose)
fprintf(stderr,"MAC: %08x %08x %08x %08x\n",mac[0],mac[1],mac[2],mac[3]);
if(!outfile) // in-place crypting...
if( fseek(fp, 0L, SEEK_SET) != 0){
fprintf(stderr, "Error: Seek failed\n");
exit(253);
}
if (fwrite(buf,sizeof(*buf),bytes,ofp) != bytes){
fprintf(stderr, "Error: write failed\n");
exit(253);
}
if (fwrite(mac,sizeof(*mac),4,ofp) != 4*sizeof(*mac)){
fprintf(stderr, "Error: write failed\n");
exit(253);
}
if(verbose) fprintf(stderr,".\n");
}
if( encrypt ){
int cnt, block;
if (verbose)
fprintf(stderr,"Encrypting: ");
do{
cnt=fread(buf,sizeof(*buf),block,fp); // XXX: deal with non-block-sized?
if(cnt<0){
fprintf(stderr, "Error: read failed\n");
exit(253);
};
if(verbose)
fprintf(stderr,"cnt=%d:",cnt);
/* if(cnt%sizeof(*buf)!=0){
fprintf(stderr,"Whoops. needs padding: cnt=%d, mod=%d\n",cnt,cnt%sizeof(*buf));
}; */
//btea(buf, decrypt?-cnt:cnt, k);
if(!outfile) // in-place crypting...
if (fseek(fp,-cnt*sizeof(*buf),SEEK_CUR) != 0){
fprintf(stderr, "Error: Seek failed\n");
exit(253);
}
if (fwrite(buf,sizeof(*buf),cnt,ofp) != cnt){
fprintf(stderr, "Error: CRC write failed\n");
exit(253);
}
if(verbose) fprintf(stderr,".\n");
}while(cnt==block);
}
if(verbose)
fprintf(stderr,"done\n");
fclose(fp);
if(outfile)
fclose(ofp);
return 0;
}
void hexkey(char *string, uint32_t k[4]){
int idx=0;
int kidx=0;
int kctr=0;
int value;
char ch;
while ((ch=string[idx++])!=0){
if(ch == ' ')
continue;
if(ch == '\t')
continue;
if (ch >= '0' && ch <= '9')
value = (ch - '0');
else if (ch >= 'A' && ch <= 'F')
value = (ch - 'A' + 10);
else if (ch >= 'a' && ch <= 'f')
value = (ch - 'a' + 10);
else
continue;
k[kidx]=(k[kidx]<<4)+value;
kctr++;
if(kctr>=8){
kctr=0;
kidx++;
if(kidx>4)
return;
};
};
}

View File

@ -8,231 +8,95 @@
* (c) by Sec <sec@42.org> 6/2011 * (c) by Sec <sec@42.org> 6/2011
*/ */
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdint.h> #include <stdint.h>
#include <getopt.h> #include "xxtea.h"
// default block size #ifdef SAFE
uint32_t htonl(uint32_t v)
{
uint32_t r=0;
r |= (v>> 0)&0xFF; r<<=8;
r |= (v>> 8)&0xFF; r<<=8;
r |= (v>>16)&0xFF; r<<=8;
r |= (v>>24)&0xFF;
return r;
}
#else
uint32_t htonl(uint32_t v){
__asm("rev %[value], %[value];" \
: [value] "+r" (v) : );
return v;
};
#endif
void btea(uint32_t *v, int n, uint32_t const k[4]);
void hexkey(char *string, uint32_t k[4]);
int main(int argc, char *argv[]) { void htonlp(uint32_t *v, uint8_t n)
FILE *fp; {
FILE *ofp; while(n--){
v[n] = htonl(v[n]);
}
}
char *prog; void xxtea_cbcmac(uint32_t mac[4], uint32_t *data,
char c; /* for getopt */ uint32_t len, uint32_t const key[4])
uint32_t k[4]; /* key */ {
uint32_t *buf; if( len & 0x03 )
return;
/* Default values */ mac[0]=0;mac[1]=0;mac[2]=0;mac[3]=0;
char verbose=0; // be silent for(int i=0; i<len;){
k[0]=0; k[1]=0; k[2]=0; k[3]=0; mac[0] ^= data[i++];
char block=16; mac[1] ^= data[i++];
char *outfile=NULL; // outfile == infile mac[2] ^= data[i++];
int decrypt=0; mac[3] ^= data[i++];
xxtea_encode_words(mac, 4, key);
/* init section */ }
prog=argv[0];
if(!prog)prog="xxtea";
if(strrchr(prog,'/')){
prog=strrchr(argv[0],'/');
prog++;
}
/* The big getopt loop */
while ((c = getopt(argc, argv, "vhdk:b:o:")) != EOF)
switch (c) {
case 'v':
verbose++;
break;
case 'd':
decrypt=1;
break;
case 'k':
hexkey(optarg, k);
break;
case 'b':
block=atoi(optarg);
break;
case 'o':
outfile=optarg;
break;
case 'h':
default:
fprintf(stderr, "Usage: %s [options] filename\n\n"
"This program en/decrypts a file with the XXTEA algorithm\n"
"\n\n"
"-v Be verbose (-v -v = even more)\n"
"-d Decrypt (instead of encrypt)\n"
"-o file Output to <file>. (Default: overwrite input file)\n"
"-k key 128bit hex key.\n"
"-b block Set blocksize. (Default: file size)\n"
"-h This help\n\n"
"\n",prog);
exit(255);
}
argc -= optind; argv += optind;
if (argc !=1){
fprintf(stderr,"Error: No filename given!\n");
exit(254);
};
if(outfile){
if ((fp = fopen(argv[0],"rb")) == NULL){
fprintf(stderr,"Error: Can't open file %s\n",argv[0]);
exit(253);
}
if ((ofp = fopen(outfile,"wb")) == NULL){
fprintf(stderr,"Error: Can't open file %s\n",argv[0]);
exit(253);
}
}else{
if ((fp = fopen(argv[0],"r+b")) == NULL){
fprintf(stderr,"Error: Can't open file %s\n",argv[0]);
exit(253);
}
ofp=fp;
};
if(block==0){
fseek(fp, 0L, SEEK_END);
block = ftell(fp)/sizeof(*buf); // XXX: padding!
fseek(fp, 0L, SEEK_SET);
};
buf=malloc(sizeof(*buf)*block);
if(!buf){
fprintf(stderr,"Error: malloc() failed.\n");
};
if (verbose)
fprintf(stderr,"Key: %08x %08x %08x %08x\n",k[0],k[1],k[2],k[3]);
int cnt;
if (verbose)
fprintf(stderr,"Encrypting: ");
do{
cnt=fread(buf,sizeof(*buf),block,fp); // XXX: deal with non-block-sized?
if(cnt<0){
fprintf(stderr, "Error: read failed\n");
exit(253);
};
if(verbose)
fprintf(stderr,"cnt=%d:",cnt);
/* if(cnt%sizeof(*buf)!=0){
fprintf(stderr,"Whoops. needs padding: cnt=%d, mod=%d\n",cnt,cnt%sizeof(*buf));
}; */
btea(buf, decrypt?-cnt:cnt, k);
if(!outfile) // in-place crypting...
if (fseek(fp,-cnt*sizeof(*buf),SEEK_CUR) != 0){
fprintf(stderr, "Error: Seek failed\n");
exit(253);
}
if (fwrite(buf,sizeof(*buf),cnt,ofp) != cnt){
fprintf(stderr, "Error: CRC write failed\n");
exit(253);
}
if(verbose) fprintf(stderr,".\n");
}while(cnt==block);
if(verbose)
fprintf(stderr,"done\n");
fclose(fp);
if(outfile)
fclose(ofp);
return 0;
} }
#define DELTA 0x9e3779b9 #define DELTA 0x9e3779b9
#define MX (((z>>5^y<<2) + (y>>3^z<<4)) ^ ((sum^y) + (k[(p&3)^e] ^ z))) #define MX (((z>>5^y<<2) + (y>>3^z<<4)) ^ ((sum^y) + (k[(p&3)^e] ^ z)))
void btea(uint32_t *v, int n, uint32_t const k[4]) { void xxtea_encode_words(uint32_t *v, int n, uint32_t const k[4])
{
if(k[0] == 0 && k[1] == 0 && k[2] == 0 && k[3] == 0) return;
uint32_t y, z, sum; uint32_t y, z, sum;
unsigned p, rounds, e; unsigned p, rounds, e;
if (n > 1) { /* Coding Part */
rounds = 6 + 52/n; htonlp(v ,n);
sum = 0; rounds = 6 + 52/n;
z = v[n-1]; sum = 0;
do { z = v[n-1];
sum += DELTA; do {
e = (sum >> 2) & 3; sum += DELTA;
for (p=0; p<n-1; p++) { e = (sum >> 2) & 3;
y = v[p+1]; for (p=0; p<n-1; p++) {
z = v[p] += MX; y = v[p+1];
} z = v[p] += MX;
y = v[0]; }
z = v[n-1] += MX;
} while (--rounds);
} else if (n < -1) { /* Decoding Part */
n = -n;
rounds = 6 + 52/n;
sum = rounds*DELTA;
y = v[0]; y = v[0];
do { z = v[n-1] += MX;
e = (sum >> 2) & 3; } while (--rounds);
for (p=n-1; p>0; p--) { htonlp(v ,n);
z = v[p-1];
y = v[p] -= MX;
}
z = v[n-1];
y = v[0] -= MX;
} while ((sum -= DELTA) != 0);
}
} }
void hexkey(char *string, uint32_t k[4]){ void xxtea_decode_words(uint32_t *v, int n, uint32_t const k[4])
int idx=0; {
int kidx=0; if(k[0] == 0 && k[1] == 0 && k[2] == 0 && k[3] == 0) return;
int kctr=0; uint32_t y, z, sum;
int value; unsigned p, rounds, e;
char ch; htonlp(v ,n);
while ((ch=string[idx++])!=0){ rounds = 6 + 52/n;
if(ch == ' ') sum = rounds*DELTA;
continue; y = v[0];
if(ch == '\t') do {
continue; e = (sum >> 2) & 3;
for (p=n-1; p>0; p--) {
if (ch >= '0' && ch <= '9') z = v[p-1];
value = (ch - '0'); y = v[p] -= MX;
else if (ch >= 'A' && ch <= 'F') }
value = (ch - 'A' + 10); z = v[n-1];
else if (ch >= 'a' && ch <= 'f') y = v[0] -= MX;
value = (ch - 'a' + 10); } while ((sum -= DELTA) != 0);
else htonlp(v ,n);
continue;
k[kidx]=(k[kidx]<<4)+value;
kctr++;
if(kctr>=8){
kctr=0;
kidx++;
if(kidx>4)
return;
};
};
} }

10
tools/crypto/xxtea.h Normal file
View File

@ -0,0 +1,10 @@
#ifndef _XXTEA_H_
#define _XXTEA_H_
void xxtea_cbcmac(uint32_t mac[4], uint32_t *data,
uint32_t len, uint32_t const key[4]);
void xxtea_encode_words(uint32_t *v, int n, uint32_t const k[4]);
void xxtea_decode_words(uint32_t *v, int n, uint32_t const k[4]);
#endif