473,566 Members | 2,924 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

a self-checking program in standard C

Do you know how to write a self-checking program in standard C?
Do I can think that if I write in a file.c
static g[100]="1234567";
in the file.exe (or file) there is in some place
1234567'\0''\0' '\0''\0''\0''\0 ''\0'...'\0'

This is my first attempt:

/* file.c -> file.exe */
#include <stdio.h>
#include <string.h>

char* estr(char* nome)
{
char *a = nome, *b;
char c;
if(nome == 0)
return 0;
while( 1 )
{b = a;
while( (c = *a) && c != '\\' && c != '/' )
++a;
if(c == 0) break;
++a;
}
return b;
}

int main(int c, char** argv)
{
(void) c;
printf("I'm a self-checking program\n");
if(strncmp("dc. exe", estr(argv[0]), 6) != 0)
{printf("File corrupted\n"); return 0;}
printf("Continu e\n");
return 0;
}

Nov 14 '05 #1
6 2387
RoSsIaCrIiLoIA wrote:
Do you know how to write a self-checking program in standard C?
Do I can think that if I write in a file.c
static g[100]="1234567";
in the file.exe (or file) there is in some place
1234567'\0''\0' '\0''\0''\0''\0 ''\0'...'\0'

I think you can. But it is out of Standard C scope.

This is my first attempt:

/* file.c -> file.exe */
#include <stdio.h>
#include <string.h>

char* estr(char* nome)
{
char *a = nome, *b;
char c;
if(nome == 0)
return 0;
while( 1 )
{b = a;
while( (c = *a) && c != '\\' && c != '/' )
what about c != '\0'
++a;
if(c == 0) break;
++a;
}
return b;
}

int main(int c, char** argv)
{
(void) c;
printf("I'm a self-checking program\n");
if(strncmp("dc. exe", estr(argv[0]), 6) != 0)
{printf("File corrupted\n"); return 0;}
Why do you think this garanties that file is corrupted? Why user
shouldn't rename files? If he shouldn't then you must consider that file
may had more than one name in some environments (hard and symbolic links).
printf("Continu e\n");
return 0;
}


How does this code relate with your proposal made in the begining.

Anyway all of this is offtopic here.

vir

Nov 14 '05 #2
On Wed, 14 Apr 2004 15:26:47 +0400, Victor Nazarov <vv*****@mail.r u>
wrote:
RoSsIaCrIiLo IA wrote:
Do you know how to write a self-checking program in standard C?
Do I can think that if I write in a file.c
static g[100]="1234567";
in the file.exe (or file) there is in some place
1234567'\0''\0' '\0''\0''\0''\0 ''\0'...'\0'

I think you can. But it is out of Standard C scope.


But are there any system where this is not true?

This is my first attempt:

/* file.c -> file.exe */
#include <stdio.h>
#include <string.h>

char* estr(char* nome)
{
char *a = nome, *b;
char c;
if(nome == 0)
return 0;
while( 1 )
{b = a;
while( (c = *a) && c != '\\' && c != '/' )


what about c != '\0'


ok (c = *a)!=0
++a;
if(c == 0) break;
++a;
}
return b;
}

int main(int c, char** argv)
{
(void) c;
printf("I'm a self-checking program\n");
if(strncmp("dc. exe", estr(argv[0]), 6) != 0)
{printf("File corrupted\n"); return 0;}


Why do you think this garanties that file is corrupted? Why user
shouldn't rename files? If he shouldn't then you must consider that file
may had more than one name in some environments (hard and symbolic links).


I'm thinking some of this type:

/* my1.c */
#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)
{
if(c != 2)
{
if( c==0 || !argv[0] )
printf("Use:> this_prog prog\n");
else printf("Use:> %s prog[.exe]\n", estr(argv[0]));

return 0;
}
if( info_p( argv[1] )==0 )
{ printf("Not signed %s\n", estr(argv[1])); return 0;}

printf("Ok signed %s\n", estr(argv[1]));
return 0;
}

void printu(char* a)
{
int i;
for( i = 0; i < sizeof(UNS); ++i)
printf(" %d ", (int) a[i]);
}
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[i], fp);
return j;
}

int info_p(char* as)
{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( (fp = fopen( as, "r+b"))==NU LL )
{printf("Proble mi 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)
{
fseek(fp, ove, SEEK_SET);
clearerr(fp);
if(f(acount, fp)==EOF || f(arox, fp)==EOF || f(asum, fp)==EOF)
{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);p rintf("M");
printu(asum) ; printf("\n");
return r;
}
/*end my1.c*/
_______________ _____

/* ali.c */
#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(" %d ", (int) a[i]);
}

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[i]) return 0;
}
return 1;
}

/* ###### HERE WE HAVE TO WRITE ###### */
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((fp = fopen( as, "rb"))==NUL L)
{
printf("Problem i 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){
fseek(fp, ove, SEEK_SET);
clearerr(fp);
if( f(acount, fp)==0 || f(arox, fp)==0 || f(asum, fp)==0 )
r = 0;
}
fclose(fp);
return r;
}
/*end ali.c*/
_______________ ___
C:\b>ali
Start
File corrupted

C:\b>my1 ali.exe
Find
count=59909 rox=3806249208 sum=5559428
String=?Û°+ÌÔäÈ T
In_num= 5 -22 0 0 M -8 -64 -34 -30 M -124 -44 84 0
Ok signed ali.exe

C:\b>ali
Start
oK continue
_______________

Are there errors?
Are there system where this doesn't run with success?

Nov 14 '05 #3
In <hc************ *************** *****@4ax.com> RoSsIaCrIiLoIA <n@esiste.ee> writes:
Do you know how to write a self-checking program in standard C?
I don't know what a self-checking program is in the first place.
Do I can think that if I write in a file.c
static g[100]="1234567";
in the file.exe (or file) there is in some place
1234567'\0''\0 ''\0''\0''\0''\ 0''\0'...'\0'
Not necessarily. Long sequences of null bytes are so common that some
linkers compress them.
This is my first attempt:

/* file.c -> file.exe */
#include <stdio.h>
#include <string.h>

char* estr(char* nome)
{
char *a = nome, *b;
char c;
if(nome == 0)
return 0;
while( 1 )
{b = a;
while( (c = *a) && c != '\\' && c != '/' )
++a;
if(c == 0) break;
++a;
}
return b;
}

int main(int c, char** argv)
{
(void) c;
printf("I'm a self-checking program\n");
if(strncmp("dc. exe", estr(argv[0]), 6) != 0)
{printf("File corrupted\n"); return 0;}
printf("Continu e\n");
return 0;
}


Let's try it:

fangorn:~/tmp 371> gcc test.c
fangorn:~/tmp 372> ./a.out
I'm a self-checking program
File corrupted

Which file is corrupted and why? And how can you determine whether a
file is corrupted or not by examining argv[0]?

Dan
--
Dan Pop
DESY Zeuthen, RZ group
Email: Da*****@ifh.de
Nov 14 '05 #4
Da*****@cern.ch (Dan Pop) wrote in message news:<c5******* ***@sunnews.cer n.ch>...
In <hc************ *************** *****@4ax.com> RoSsIaCrIiLoIA <n@esiste.ee> writes:
Do you know how to write a self-checking program in standard C?


I don't know what a self-checking program is in the first place.
Do I can think that if I write in a file.c
static g[100]="1234567";
in the file.exe (or file) there is in some place
1234567'\0''\0 ''\0''\0''\0''\ 0''\0'...'\0'


Not necessarily. Long sequences of null bytes are so common that some
linkers compress them.
This is my first attempt:

/* file.c -> file.exe */
#include <stdio.h>
#include <string.h>

char* estr(char* nome)
{
char *a = nome, *b;
char c;
if(nome == 0)
return 0;
while( 1 )
{b = a;
while( (c = *a) && c != '\\' && c != '/' )
++a;
if(c == 0) break;
++a;
}

return b;
}

int main(int c, char** argv)
{
(void) c;
printf("I'm a self-checking program\n");
if(strncmp("dc. exe", estr(argv[0]), 6) != 0)
{printf("File corrupted\n"); return 0;}
printf("Continu e\n");
return 0;
}


Let's try it:

fangorn:~/tmp 371> gcc test.c
fangorn:~/tmp 372> ./a.out
I'm a self-checking program
File corrupted

Which file is corrupted and why? And how can you determine whether a
file is corrupted or not by examining argv[0]?

Dan

A lot of work along these lines has already been done. A company that
I used to work for had visual studio add-in tools. See the page:
http://www.arxan.com/home/index.php

Paul
Nov 14 '05 #5
RoSsIaCrIiLoIA wrote:
C:\b>ali
Start
File corrupted

C:\b>my1 ali.exe
Find
count=59909 rox=3806249208 sum=5559428
String=?Û°+ÌÔäÈ T
In_num= 5 -22 0 0 M -8 -64 -34 -30 M -124 -44 84 0
Ok signed ali.exe

C:\b>ali
Start
oK continue
_______________

Are there errors?
Are there system where this doesn't run with success?


This is not portable anyway. Really It depends on the executable format.
Executables often get comressed. Try another newsgroup...

vir

Nov 14 '05 #6
[comp.lang.c, sci.crypt]
On Wed, 14 Apr 2004 +0400, Victor Nazarov <vv*****@mail.r u> wrote:
This is not portable anyway. Really It depends on the executable format.
Executables often get comressed. Try another newsgroup...


yes compressed or *encrypted*
If the OS has a database of all the public key of software houses,
if M.exe is a encrypted program of Company XX
the loader of the OS could take the public key of Company XX
encrypt it [on the fly] and jump on it.

If a crakker want to crack M.exe he has to know the secret key of
Company XX or change the code in the memory space of that running
program.(in many OSes seems to me that the memory space of a program
is closed to other process)

Regarding Internet **why** OSes seems to have the default in allowing
*every* FTP-telnet-http-all *fu?king* protocol- IN EXIT for the
home-PC ( and change it in a world server of resources) ???
_______________ ___
Il popolo dell'iraaq non vuole eserciti oppressori sia onu o altro
(se non ci credete fate un referendum)
Come ragionate con i piedi?
Nov 14 '05 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
4697
by: Marc | last post by:
Hi all, I was using Tkinter.IntVar() to store values from a large list of parts that I pulled from a list. This is the code to initialize the instances: def initVariables(self): self.e = IntVar() for part, list in info.masterList.items():
15
2572
by: Ralf W. Grosse-Kunstleve | last post by:
****************************************************************************** This posting is also available in HTML format: http://cci.lbl.gov/~rwgk/python/adopt_init_args_2005_07_02.html ****************************************************************************** Hi fellow Python coders, I often find myself writing:: class...
18
2246
by: Ralf W. Grosse-Kunstleve | last post by:
My initial proposal (http://cci.lbl.gov/~rwgk/python/adopt_init_args_2005_07_02.html) didn't exactly get a warm welcome... And Now for Something Completely Different: class autoinit(object): def __init__(self, *args, **keyword_args): self.__dict__.update(
4
2758
by: David Coffin | last post by:
I'd like to subclass int to support list access, treating the integer as if it were a list of bits. Assigning bits to particular indices involves changing the value of the integer itself, but changing 'self' obviously just alters the value of that local variable. Is there some way for me to change the value of the BitSequence object itself?...
4
1799
by: marek.rocki | last post by:
First of all, please don't flame me immediately. I did browse archives and didn't see any solution to my problem. Assume I want to add a method to an object at runtime. Yes, to an object, not a class - because changing a class would have global effects and I want to alter a particular object only. The following approach fails: class kla:...
7
1896
by: Andrew Robert | last post by:
Hi Everyone, I am having a problem with a class and hope you can help. When I try to use the class listed below, I get the statement that self is not defined. test=TriggerMessage(data) var = test.decode(self.qname)
24
2276
by: Peter Maas | last post by:
The Python FAQ 1.4.5 gives 3 reasons for explicit self (condensed version): 1. Instance variables can be easily distinguished from local variables. 2. A method from a particular class can be called as baseclass.methodname(self, <argument list>). 3. No need for declarations to disambiguate assignments to local/instance variables.
84
7149
by: braver | last post by:
Is there any trick to get rid of having to type the annoying, character-eating "self." prefix everywhere in a class? Sometimes I avoid OO just not to deal with its verbosity. In fact, I try to use Ruby anywhere speed is not crucial especially for @ prefix is better- looking than self. But things grow -- is there any metaprogramming tricks...
13
11995
by: Kurda Yon | last post by:
Hi, I found one example which defines the addition of two vectors as a method of a class. It looks like that: class Vector: def __add__(self, other): data = for j in range(len(self.data)): data.append(self.data + other.data)
6
1799
by: Bart Kastermans | last post by:
I am playing with some trees. In one of the procedures I wrote for this I am trying to change self to a different tree. A tree here has four members (val/type/left/right). I found that self = SS does not work; I have to write self.val = SS.val and the same for the other members (as shown below). Is there a better way to do this? In the...
0
7673
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7893
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
8109
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
0
7953
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
1
5485
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
5213
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3626
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2085
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
0
926
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.