/[public]/psiconv/trunk/lib/psiconv/buffer.c
ViewVC logotype

Diff of /psiconv/trunk/lib/psiconv/buffer.c

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

Revision 79 Revision 80
25 25
26#include "list.h" 26#include "list.h"
27#include "error.h" 27#include "error.h"
28#include "buffer.h" 28#include "buffer.h"
29 29
30typedef struct psiconv_relocation_s {
31 psiconv_u32 offset;
32 int id;
33} *psiconv_relocation;
34
30struct psiconv_buffer_s { 35struct psiconv_buffer_s {
31 psiconv_u32 base_offset; 36 psiconv_list reloc_target; /* of struct relocation_s */
32 psiconv_list data; 37 psiconv_list reloc_ref; /* of struct relocation_s */
38 psiconv_list data; /* of psiconv_u8 */
33}; 39};
34 40
35psiconv_buffer psiconv_buffer_new(psiconv_u32 base_offset) 41static psiconv_u32 unique_id = 1;
42
43psiconv_u32 psiconv_buffer_unique_id(void)
44{
45 return unique_id ++;
46}
47
48psiconv_buffer psiconv_buffer_new(void)
36{ 49{
37 psiconv_buffer buf; 50 psiconv_buffer buf;
38 if (!(buf = malloc(sizeof(*buf)))) 51 if (!(buf = malloc(sizeof(*buf))))
39 return NULL; 52 goto ERROR1;
40 if (!(buf->data = psiconv_list_new(sizeof(psiconv_u8)))) { 53 if (!(buf->data = psiconv_list_new(sizeof(psiconv_u8))))
41 free(buf); 54 goto ERROR2;
42 return NULL; 55 if (!(buf->reloc_target = psiconv_list_new(
43 } 56 sizeof(struct psiconv_relocation_s))))
44 buf->base_offset = base_offset; 57 goto ERROR3;
58 if (!(buf->reloc_ref = psiconv_list_new(
59 sizeof(struct psiconv_relocation_s))))
60 goto ERROR4;
45 return buf; 61 return buf;
62ERROR4:
63 psiconv_list_free(buf->reloc_target);
64ERROR3:
65 psiconv_list_free(buf->data);
66ERROR2:
67 free(buf);
68ERROR1:
69 return NULL;
46} 70}
47 71
48void psiconv_buffer_free(psiconv_buffer buf) 72void psiconv_buffer_free(psiconv_buffer buf)
49{ 73{
74 psiconv_list_free(buf->reloc_ref);
75 psiconv_list_free(buf->reloc_target);
50 psiconv_list_free(buf->data); 76 psiconv_list_free(buf->data);
51 free(buf); 77 free(buf);
52}
53
54psiconv_u32 psiconv_buffer_base_offset(const psiconv_buffer buf)
55{
56 return buf->base_offset;
57} 78}
58 79
59psiconv_u32 psiconv_buffer_length(const psiconv_buffer buf) 80psiconv_u32 psiconv_buffer_length(const psiconv_buffer buf)
60{ 81{
61 return psiconv_list_length(buf->data); 82 return psiconv_list_length(buf->data);
87 return psiconv_list_fwrite_all(buf->data,f); 108 return psiconv_list_fwrite_all(buf->data,f);
88} 109}
89 110
90int psiconv_buffer_concat(psiconv_buffer buf, const psiconv_buffer extra) 111int psiconv_buffer_concat(psiconv_buffer buf, const psiconv_buffer extra)
91{ 112{
113 int res;
114 psiconv_u32 i;
115 psiconv_relocation reloc;
116
117
118 for (i = 0; i < psiconv_list_length(extra->reloc_target); i++) {
119 if (!(reloc = psiconv_list_get(extra->reloc_target,i)))
120 return -PSICONV_E_OTHER;
121 reloc->offset += psiconv_list_length(buf->data);
122 if ((res=psiconv_list_add(buf->reloc_target,reloc)))
123 return res;
124 }
125 for (i = 0; i < psiconv_list_length(extra->reloc_ref); i++) {
126 if (!(reloc = psiconv_list_get(extra->reloc_ref,i)))
127 return -PSICONV_E_OTHER;
128 reloc->offset += psiconv_list_length(buf->data);
129 if ((res = psiconv_list_add(buf->reloc_ref,reloc)))
130 return res;
131 }
92 return psiconv_list_concat(buf->data,extra->data); 132 return psiconv_list_concat(buf->data,extra->data);
93} 133}
134
135int psiconv_buffer_resolve(psiconv_buffer buf)
136{
137 int res;
138 psiconv_u32 i,j,temp;
139 psiconv_relocation target,ref;
140
141 for (i = 0; i < psiconv_list_length(buf->reloc_ref);i++) {
142 if (!(ref = psiconv_list_get(buf->reloc_ref,i)))
143 return -PSICONV_E_OTHER;
144 for (j = 0; j < psiconv_list_length(buf->reloc_target);j++) {
145 if (!(target = psiconv_list_get(buf->reloc_target,j)))
146 return -PSICONV_E_OTHER;
147 if (ref->id == target->id) {
148 temp = target->offset & 0xff;
149 if ((res = psiconv_list_replace(buf->data,ref->offset,&temp)))
150 return -PSICONV_E_OTHER;
151 temp = (target->offset >> 8) & 0xff;
152 if ((res = psiconv_list_replace(buf->data,ref->offset + 1,&temp)))
153 return -PSICONV_E_OTHER;
154 temp = (target->offset >> 16) & 0xff;
155 if ((res = psiconv_list_replace(buf->data,ref->offset + 2,&temp)))
156 return -PSICONV_E_OTHER;
157 temp = (target->offset >> 24) & 0xff;
158 if ((res = psiconv_list_replace(buf->data,ref->offset + 3,&temp)))
159 return -PSICONV_E_OTHER;
160 break;
161 }
162 }
163 if (j == psiconv_list_length(buf->reloc_target))
164 return -PSICONV_E_OTHER;
165 }
166 psiconv_list_empty(buf->reloc_target);
167 psiconv_list_empty(buf->reloc_ref);
168 return -PSICONV_E_OK;
169}
170
171int psiconv_buffer_add_reference(psiconv_buffer buf,int id)
172{
173 struct psiconv_relocation_s reloc;
174 int res,i;
175 psiconv_u8 data;
176
177 reloc.offset = psiconv_list_length(buf->data);
178 reloc.id = id;
179 if ((res = psiconv_list_add(buf->reloc_ref,&reloc)))
180 return res;
181 data = 0x00;
182 for (i = 0; i < 4; i++)
183 if ((res = psiconv_list_add(buf->data,&data)))
184 return res;
185 return -PSICONV_E_OK;
186}
187
188int psiconv_buffer_add_target(psiconv_buffer buf, int id)
189{
190 struct psiconv_relocation_s reloc;
191
192 reloc.offset = psiconv_list_length(buf->data);
193 reloc.id = id;
194 return psiconv_list_add(buf->reloc_target,&reloc);
195}

Legend:
Removed from v.79  
changed lines
  Added in v.80

frodo@frodo.looijaard.name
ViewVC Help
Powered by ViewVC 1.1.26