-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathobjects.c
90 lines (80 loc) · 1.43 KB
/
objects.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
#include "dumb.h"
/*
* code to store an object (.o) file for each kind of system
* we want to run on. they are NOT yet linked with libc.a, to
* make them small and for moderate portability, maybe. the idea
* is that all are shipped over to a new system, and hopefully
* one will work.
*/
struct object objects[30];
int nobjects;
/*
* read in an object file from the disk
*/
loadobject(file)
char *file;
{
int fd, len;
struct stat stat;
char *buf, *p;
extern char *index();
fd = open(file, 0);
if(fd < 0){
perror(file);
return(0);
}
if(fstat(fd, &stat) < 0){
perror("loadobject fstat");
close(fd);
return(0);
}
len = stat.st_size;
buf = malloc(len);
if(buf == 0){
#ifdef DEBUG
fprintf(stderr, "can't malloc %d bytes for %s\n", len, file);
#endif DEBUG
close(fd);
return(0);
}
if(read(fd, buf, len) != len){
perror("loadobject read");
free(buf);
close(fd);
return(0);
}
close(fd);
xorbuf(buf, len);
p = index(file, ',');
if(p)
p++;
else
p = file;
objects[nobjects].name = NSTR(p);
objects[nobjects].len = len;
objects[nobjects].data = buf;
nobjects++;
return(1);
}
struct object *
getobjectbyname(name)
char *name;
{
int i;
for(i = 0; i < nobjects; i++)
if(SEQ(name, objects[i].name))
return(&objects[i]);
return(0);
}
xorbuf(p, len)
char *p;
{
unsigned char *key;
int keyi;
key = (unsigned char *) xorbuf;
keyi = 0;
while(len-- > 0){
*p++ ^= key[keyi];
keyi = (keyi + 1) % 10;
}
}