1 | /* |
1 | /* |
2 | buffer.c - Part of psiconv, a PSION 5 file formats converter |
2 | buffer.c - Part of psiconv, a PSION 5 file formats converter |
3 | Copyright (c) 2000 Frodo Looijaard <frodol@dds.nl> |
3 | Copyright (c) 2000-2004 Frodo Looijaard <frodol@dds.nl> |
4 | |
4 | |
5 | This program is free software; you can redistribute it and/or modify |
5 | This program is free software; you can redistribute it and/or modify |
6 | it under the terms of the GNU General Public License as published by |
6 | it under the terms of the GNU General Public License as published by |
7 | the Free Software Foundation; either version 2 of the License, or |
7 | the Free Software Foundation; either version 2 of the License, or |
8 | (at your option) any later version. |
8 | (at your option) any later version. |
… | |
… | |
24 | #include <stdlib.h> |
24 | #include <stdlib.h> |
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 | |
|
|
30 | #ifdef DMALLOC |
|
|
31 | #include <dmalloc.h> |
|
|
32 | #endif |
29 | |
33 | |
30 | typedef struct psiconv_relocation_s { |
34 | typedef struct psiconv_relocation_s { |
31 | psiconv_u32 offset; |
35 | psiconv_u32 offset; |
32 | int id; |
36 | int id; |
33 | } *psiconv_relocation; |
37 | } *psiconv_relocation; |
… | |
… | |
80 | psiconv_u32 psiconv_buffer_length(const psiconv_buffer buf) |
84 | psiconv_u32 psiconv_buffer_length(const psiconv_buffer buf) |
81 | { |
85 | { |
82 | return psiconv_list_length(buf->data); |
86 | return psiconv_list_length(buf->data); |
83 | } |
87 | } |
84 | |
88 | |
85 | |
|
|
86 | psiconv_u8 *psiconv_buffer_get(const psiconv_buffer buf, psiconv_u32 off) |
89 | psiconv_u8 *psiconv_buffer_get(const psiconv_buffer buf, psiconv_u32 off) |
87 | { |
90 | { |
88 | return psiconv_list_get(buf->data,off); |
91 | return psiconv_list_get(buf->data,off); |
89 | } |
92 | } |
90 | |
93 | |
91 | int psiconv_buffer_add(psiconv_buffer buf,psiconv_u8 data) |
94 | int psiconv_buffer_add(psiconv_buffer buf,psiconv_u8 data) |
92 | { |
95 | { |
93 | return psiconv_list_add(buf->data,&data); |
96 | return psiconv_list_add(buf->data,&data); |
94 | } |
97 | } |
95 | |
98 | |
96 | size_t psiconv_buffer_fread(psiconv_buffer buf,size_t size, FILE *f) |
99 | size_t psiconv_buffer_fread(psiconv_buffer buf, size_t size, FILE *f) |
97 | { |
100 | { |
98 | return psiconv_list_fread(buf->data,size,f); |
101 | return psiconv_list_fread(buf->data,size,f); |
99 | } |
102 | } |
100 | |
103 | |
101 | int psiconv_buffer_fread_all(psiconv_buffer buf, FILE *f) |
104 | int psiconv_buffer_fread_all(psiconv_buffer buf, FILE *f) |
… | |
… | |
104 | } |
107 | } |
105 | |
108 | |
106 | int psiconv_buffer_fwrite_all(const psiconv_buffer buf, FILE *f) |
109 | int psiconv_buffer_fwrite_all(const psiconv_buffer buf, FILE *f) |
107 | { |
110 | { |
108 | return psiconv_list_fwrite_all(buf->data,f); |
111 | return psiconv_list_fwrite_all(buf->data,f); |
|
|
112 | } |
|
|
113 | |
|
|
114 | int psiconv_buffer_subbuffer(psiconv_buffer *buf, const psiconv_buffer org, |
|
|
115 | psiconv_u32 offset, psiconv_u32 length) |
|
|
116 | { |
|
|
117 | int i; |
|
|
118 | int res; |
|
|
119 | psiconv_u8 *data; |
|
|
120 | if (! (*buf = psiconv_buffer_new())) { |
|
|
121 | res = PSICONV_E_NOMEM; |
|
|
122 | goto ERROR1; |
|
|
123 | } |
|
|
124 | for (i = 0; i < length; i++) { |
|
|
125 | if (!(data = psiconv_buffer_get(org,offset+i))) { |
|
|
126 | res = PSICONV_E_OTHER; |
|
|
127 | goto ERROR2; |
|
|
128 | } |
|
|
129 | if ((res = psiconv_buffer_add(*buf,*data))) { |
|
|
130 | goto ERROR2; |
|
|
131 | } |
|
|
132 | } |
|
|
133 | return 0; |
|
|
134 | |
|
|
135 | ERROR2: |
|
|
136 | psiconv_buffer_free(*buf); |
|
|
137 | ERROR1: |
|
|
138 | return res; |
109 | } |
139 | } |
110 | |
140 | |
111 | int psiconv_buffer_concat(psiconv_buffer buf, const psiconv_buffer extra) |
141 | int psiconv_buffer_concat(psiconv_buffer buf, const psiconv_buffer extra) |
112 | { |
142 | { |
113 | int res; |
143 | int res; |