Fixed buffer overflow in mserver; fixed type of checkinfo () 2nd parameter; memory
[ZeXOS.git] / apps / zasm / buffer.c
blobe52d97298945361ba33b0abd050a2e1e64616845
1 /*
2 * ZeX/OS
3 * Copyright (C) 2008 Tomas 'ZeXx86' Jedrzejek (zexx86@zexos.org)
4 * Copyright (C) 2009 Tomas 'ZeXx86' Jedrzejek (zexx86@zexos.org)
6 * This program is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation, either version 3 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program. If not, see <http://www.gnu.org/licenses/>.
20 #include <stdio.h>
21 #include <stdlib.h>
22 #include <string.h>
23 #include <fcntl.h>
24 #include "buffer.h"
26 static char *buffer;
28 int buffer_copy (unsigned offset, char *data, unsigned size)
30 memcpy (buffer+offset, data, size);
32 return 1;
35 char *buffer_get ()
37 return buffer;
40 int buffer_write (char *file)
42 int fd = open (file, O_CREAT | O_WRONLY);
44 if (fd < 1) {
45 printf ("error -> file '%s' not found !\n", file);
46 return 0;
49 if (!write (fd, buffer, 10240-1)) {
50 printf ("error -> something is wrong !\n");
51 return 0;
54 close (fd);
56 return 1;
59 int buffer_init ()
61 buffer = (char *) malloc (sizeof (char) * 10240);
63 if (!buffer)
64 return 0;
66 elf_file_t elf;
68 memset (&elf, 0, sizeof (elf_file_t));
70 elf.magic = 0x464C457FL;
71 elf.bitness = 1;
72 elf.endian = 1;
73 elf.elf_ver_1 = 1;
74 elf.file_type = 2;
75 elf.machine = 3;
76 elf.elf_ver_2 = 1;
77 elf.entry_pt = 0x0;
78 elf.phtab_offset = 0x34;
79 elf.shtab_offset = 0x204c;
80 elf.flags = 0x0;
81 elf.file_hdr_size = 0x34;
82 elf.phtab_ent_size = 0x20;
83 elf.num_phtab_ents = 0x2;
84 elf.shtab_ent_size = 0x28;
85 elf.num_sects = 0x7;
86 elf.shstrtab_index = 0x6;
88 elf_sect_t sect;
90 memset (&sect, 0, sizeof (elf_sect_t));
92 sect.sect_name = 0x1;
93 sect.type = 0x1000;
94 sect.flags = 0;
95 sect.virt_adr = 0x0;
96 sect.offset = 0x1000;
97 sect.size = 0x1000;
98 sect.link = 0x5;
99 sect.info = 0x1000;
100 sect.align = 0x65041580;
101 sect.ent_size = 0x0;
103 buffer_copy (0, (void *) &elf, sizeof (elf_file_t));
104 buffer_copy (sizeof (elf_file_t), (void *) &sect, sizeof (elf_sect_t));
107 /* sections */
108 unsigned s = elf.shtab_ent_size; // section size
110 /* s = 0 */
111 memset (&sect, 0, sizeof (elf_sect_t));
112 buffer_copy (elf.shtab_offset, (void *) &sect, sizeof (elf_sect_t));
114 /* s = 1 - .text */
115 sect.sect_name = 0x0b;
116 sect.type = 0x1;
117 sect.flags = 0x6;
118 sect.virt_adr = 0x801000;
119 sect.offset = 0x1000;
120 sect.size = 0x1000;
121 sect.link = 0x0;
122 sect.info = 0x0;
123 sect.align = 0x10;
124 sect.ent_size = 0x0;
125 buffer_copy (elf.shtab_offset+s, (void *) &sect, sizeof (elf_sect_t));
127 /* s = 2 - .data */
128 sect.sect_name = 0x11;
129 sect.type = 0x1;
130 sect.flags = 0x1;
131 sect.virt_adr = 0x802000;
132 sect.offset = 0x1000;
133 sect.size = 0x0;
134 sect.link = 0x0;
135 sect.info = 0x0;
136 sect.align = 0x1;
137 sect.ent_size = 0x0;
138 buffer_copy (elf.shtab_offset+s*2, (void *) &sect, sizeof (elf_sect_t));
140 /* s = 3 - .rodata */
141 sect.sect_name = 0x17;
142 sect.type = 0x1;
143 sect.flags = 0x1;
144 sect.virt_adr = 0x802000;
145 sect.offset = 0x2000;
146 sect.size = 0x0;
147 sect.link = 0x0;
148 sect.info = 0x0;
149 sect.align = 0x1;
150 sect.ent_size = 0x0;
151 buffer_copy (elf.shtab_offset+s*3, (void *) &sect, sizeof (elf_sect_t));
153 /* s = 4 - .bss */
154 sect.sect_name = 0x1f;
155 sect.type = 0x1;
156 sect.flags = 0x32;
157 sect.virt_adr = 0x802c00;
158 sect.offset = 0x500;
159 sect.size = 0x0;
160 sect.link = 0x0;
161 sect.info = 0x0;
162 sect.align = 0x1;
163 sect.ent_size = 0x1;
164 buffer_copy (elf.shtab_offset+s*4, (void *) &sect, sizeof (elf_sect_t));
166 /* s = 5 - .comment */
167 sect.sect_name = 0x24;
168 sect.type = 0x1;
169 sect.flags = 0x0;
170 sect.virt_adr = 0x803000;
171 sect.offset = 0x2000;
172 sect.size = 0x1c;
173 sect.link = 0x0;
174 sect.info = 0x0;
175 sect.align = 0x1;
176 sect.ent_size = 0x0;
177 buffer_copy (elf.shtab_offset+s*5, (void *) &sect, sizeof (elf_sect_t));
179 /* s = 6 - .shstrtab */
180 sect.sect_name = 0x1;
181 sect.type = 0x3;
182 sect.flags = 0x0;
183 sect.virt_adr = 0x0;
184 sect.offset = 0x201c0000;
185 sect.size = 0x2d;
186 sect.link = 0x0;
187 sect.info = 0x0;
188 sect.align = 0x1;
189 sect.ent_size = 0x0;
190 buffer_copy (elf.shtab_offset+s*6, (void *) &sect, sizeof (elf_sect_t));
192 return 1;