#include <string.h>
#include <stdio.h>
#include <ctype.h>
#include <stdlib.h>
#include <assert.h>
#include <limits.h>
#define UNS unsigned
char* estr(char* nome)
{
char *a = nome, *b;
char c;
if(nome == 0)
return 0;
while( 1 )
{b = a;
while( (c = *a)!=0 && c!='\\' && c!='/' )
++a;
if(c == 0) break;
++a;
}
return b;
}
int info_p(char* , int );
int main(int c, char** ar)
{
char *pc;
int sign;
if(c == 2)
{
pc = ar[1];
sign = 0;
}
else if( c==3 && ar!=0 && ar[1]!=0 )
{
if( ar[1][0]!='-' || ar[1][1]!='s' || ar[1][2]!=0 )
goto label;
pc = ar[2];
sign = 1;
}
else
{
label:
printf("This program sign [-s] if possible an .exe\n");
printf("or calc and print its value on the screen\n");
if( c==0 || !ar[0] )
printf("Usage:> this_prog [-s] prog\n");
else printf("Usage:> %s [-s] prog[.exe]\n", estr(ar[0]));
return 0;
}
if( info_p( pc, sign )==0 )
{
if(sign)
printf("Not signed %s\n", estr(ar[1]));
return 0;
}
if(sign)
printf("Ok signed %s\n", estr(ar[2]));
return 0;
}
void printu(char* a)
{
int i;
for( i = 0; i < sizeof(UNS); ++i)
printf(" %u ", (UNS)(unsigned char) a);
}
int f(const char* a, FILE* fp)
{
int i, j;
for( i= 0, j= EOF + 1 ; i < sizeof(UNS) && j!=EOF; ++i)
j= fputc(a, fp);
return j;
}
int info_p(char* as, int sign)
{
int c, r, k, cp;
FILE *fp;
unsigned count= 0, rox= 0, sum= 0, *p1, buffer= 0;
char acount[ sizeof(UNS) + 8 ] = {0},
arox [ sizeof(UNS) + 8 ] = {0},
asum [ sizeof(UNS) + 8 ] = {0};
char *pc1 = acount, *pc2 = arox, *pc3 = asum;
long ove;
if(3*sizeof(UNS)>=512) return 0;
if( (fp = fopen( as, "r+b"))==NULL )
{
printf("Problemi di apertura\n");
return 0;
}
r = 0; cp = 0;
label:
while( (c = fgetc(fp))!=EOF && !(cp=='1' && c=='2') )
{
sum += c; ++count;
cp = c;
buffer = (buffer << CHAR_BIT) | ((UNS)(unsigned char) c);
if(count % sizeof(UNS) ==0)
rox = rox ^ buffer;
}
if( c!=EOF && r==0 )
{k = 2;
while( (c = fgetc(fp))!=EOF && c-'0'== ++k )
{
sum += c; ++count;
cp = c;
buffer = (buffer << CHAR_BIT) | (UNS)((unsigned char) c);
if(count % sizeof(UNS) == 0)
rox = rox ^ buffer;
} /*34567*/
if( k==8 && c!=EOF )
{
if((ove = ftell( fp ))== -1)
{ printf("Error\n"); exit(0); }
printf("Find\n");
r=1;
}
else if(c != EOF) goto label;
else goto fine;
while( (c = fgetc(fp))!=EOF && k!=512 ) ++k;
if( c!=EOF ) goto label;
}
if( c==EOF ) goto fine;
goto label;
fine:
*(UNS*) pc1 = count; *(UNS*) pc2 = rox; *(UNS*) pc3 = sum;
if( r==1 && sign )
{
if( fseek(fp, ove, SEEK_SET) )
goto erro;
clearerr(fp);
if(f(acount, fp)==EOF || f(arox, fp)==EOF || f(asum, fp)==EOF)
{
erro:
r=0;
printf("Error in writing\n");
}
}
fclose(fp);
printf("count=%u rox=%u sum=%u\n", count, rox, sum );
printf("String=\"%s%s%s\"\nIn_num=", acount, arox, asum);
printu(acount); printf("M") ;printu(arox);printf("M");
printu(asum) ; printf("\n");
return r;
}
___________________________________
#include <string.h>
#include <stdio.h>
#include <ctype.h>
#include <stdlib.h>
#include <assert.h>
#include <limits.h>
#define UNS unsigned
char* estr(char* nome)
{
char *a = nome, *b;
char c;
if(nome == 0)
return 0;
while( 1 )
{
b = a;
while( (c = *a)!=0 && c!='\\' && c!='/' ) ++a;
if(c == 0) break;
++a;
}
return b;
}
int info_p(char* );
int main(int c, char** argv)
{
printf("Start \n");
if( c==0 || !argv[0] )
return 0;
if( info_p(argv[0]) == 0 )
{ printf("File corrupted\n"); return 0; }
printf("oK continue\n");
return 0;
}
void printu(char* a)
{
int i;
for( i = 0 ; i < sizeof(UNS) ; ++i)
printf(" %u ", (UNS) (unsigned char) a);
}
int f(const char* a, FILE* fp)
{
int i, j;
for( i = 0 ; i < sizeof(UNS) ; ++i)
{
if( (j = fgetc( fp ))==EOF ) return 0;
if( j != (unsigned char) a) return 0;
}
return 1;
}
char al[512] = "1234567 ";
int info_p(char* as)
{
int l, c, r, k, cp;
FILE *fp;
unsigned count = 0, rox = 0, sum = 0, *p1, buffer = 0;
char acount[ sizeof(UNS) + 8 ] = {0},
arox [ sizeof(UNS) + 8 ] = {0},
asum [ sizeof(UNS) + 8 ] = {0};
char *pc1 = acount, *pc2 = arox, *pc3 = asum;
long ove;
if(3*sizeof(UNS)>=512) return 0;
if((fp = fopen( as, "rb"))==NULL)
{
printf("Problemi di apertura\n");
return 0;
}
r = 0; cp = 0;
label:
while((c = fgetc(fp))!=EOF && !(cp=='1' && c=='2'))
{
sum += c; ++count;
cp = c;
buffer = (buffer << CHAR_BIT) | ((UNS)(unsigned char) c);
if(count % sizeof(UNS) ==0)
rox = rox ^ buffer;
}
if(c!=EOF && r==0)
{
k = 2;
while((c = fgetc(fp))!=EOF && c== ++k + '0' )
{
sum += c; ++count;
cp = c;
buffer = (buffer << CHAR_BIT) | (UNS)((unsigned char) c);
if(count % sizeof(UNS) == 0)
rox = rox ^ buffer;
} /*34567*/
if(k==8 && c!=EOF)
{
if( (ove = ftell(fp)) == -1)
{ printf( "Error\n" ); exit( 0 ); }
r = 1;
}
else if(c != EOF) goto label;
else goto fine;
while((c = fgetc(fp))!=EOF && k!=512) ++k;
if(c !=EOF ) goto label;
}
if(c == EOF) goto fine;
goto label;
fine:
*(UNS*) pc1 = count; *(UNS*) pc2 = rox; *(UNS*) pc3 = sum;
if(r == 1){
if(fseek(fp, ove, SEEK_SET) != 0)
goto labbel;
clearerr(fp);
if( f(acount, fp)==0 || f(arox, fp)==0 || f(asum, fp)==0 )
{labbel:
r = 0;
}
}
fclose(fp);
return r;
}