gwenhywfar  5.10.1
syncio_memory.c
Go to the documentation of this file.
1 /***************************************************************************
2  begin : Tue Apr 27 2010
3  copyright : (C) 2010 by Martin Preuss
4  email : martin@libchipcard.de
5 
6  ***************************************************************************
7  * *
8  * This library is free software; you can redistribute it and/or *
9  * modify it under the terms of the GNU Lesser General Public *
10  * License as published by the Free Software Foundation; either *
11  * version 2.1 of the License, or (at your option) any later version. *
12  * *
13  * This library is distributed in the hope that it will be useful, *
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of *
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU *
16  * Lesser General Public License for more details. *
17  * *
18  * You should have received a copy of the GNU Lesser General Public *
19  * License along with this library; if not, write to the Free Software *
20  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, *
21  * MA 02111-1307 USA *
22  * *
23  ***************************************************************************/
24 
25 #ifdef HAVE_CONFIG_H
26 # include <config.h>
27 #endif
28 
29 #define DISABLE_DEBUGLOG
30 
31 
32 
33 #include "syncio_memory_p.h"
34 #include "i18n_l.h"
35 
36 #include <gwenhywfar/misc.h>
37 #include <gwenhywfar/debug.h>
38 #include <gwenhywfar/gui.h>
39 
40 #include <assert.h>
41 #include <errno.h>
42 #include <string.h>
43 
44 
45 
46 GWEN_INHERIT(GWEN_SYNCIO, GWEN_SYNCIO_MEMORY)
47 
48 
49 
51 {
52  GWEN_SYNCIO *sio;
53  GWEN_SYNCIO_MEMORY *xio;
54 
56  GWEN_NEW_OBJECT(GWEN_SYNCIO_MEMORY, xio);
57  GWEN_INHERIT_SETDATA(GWEN_SYNCIO, GWEN_SYNCIO_MEMORY, sio, xio, GWEN_SyncIo_Memory_FreeData);
58 
61 
62  if (buffer) {
63  xio->buffer=buffer;
64  xio->own=take?1:0;
65  }
66  else {
67  xio->buffer=GWEN_Buffer_new(0, 256, 0, 1);
68  xio->own=1;
69  }
70 
72 
73  return sio;
74 }
75 
76 
77 
78 GWEN_SYNCIO *GWEN_SyncIo_Memory_fromBuffer(const uint8_t *buffer, int size)
79 {
80  GWEN_SYNCIO *sio;
81  GWEN_SYNCIO_MEMORY *xio;
82 
84  GWEN_NEW_OBJECT(GWEN_SYNCIO_MEMORY, xio);
85  GWEN_INHERIT_SETDATA(GWEN_SYNCIO, GWEN_SYNCIO_MEMORY, sio, xio, GWEN_SyncIo_Memory_FreeData);
86 
89 
90  /* adapt size, if necessary */
91  if (size==-1) {
92  if (buffer)
93  size=strlen((const char *) buffer)+1;
94  else
95  size=0;
96  }
97 
98  xio->buffer=GWEN_Buffer_new(0, size, 0, 1);
99  xio->own=1;
100  if (buffer && size>0) {
101  GWEN_Buffer_AppendBytes(xio->buffer, (const char *) buffer, size);
102  GWEN_Buffer_Rewind(xio->buffer);
103  }
105  return sio;
106 }
107 
108 
109 
111 {
112  GWEN_SYNCIO_MEMORY *xio;
113 
114  xio=(GWEN_SYNCIO_MEMORY *) p;
115  if (xio->buffer && xio->own)
116  GWEN_Buffer_free(xio->buffer);
117  GWEN_FREE_OBJECT(xio);
118 }
119 
120 
121 
123 {
124  GWEN_SYNCIO_MEMORY *xio;
125 
126  assert(sio);
127  xio=GWEN_INHERIT_GETDATA(GWEN_SYNCIO, GWEN_SYNCIO_MEMORY, sio);
128  assert(xio);
129 
130  return xio->buffer;
131 }
132 
133 
134 
136 {
137  GWEN_SYNCIO_MEMORY *xio;
138  GWEN_BUFFER *buf;
139 
140  assert(sio);
141  xio=GWEN_INHERIT_GETDATA(GWEN_SYNCIO, GWEN_SYNCIO_MEMORY, sio);
142  assert(xio);
143 
144  if (xio->own==0 || xio->buffer==NULL) {
145  DBG_ERROR(GWEN_LOGDOMAIN, "Can't give away buffer, object does not own it");
146  return NULL;
147  }
148  buf=xio->buffer;
149  xio->buffer=NULL;
150  xio->own=0;
151  return buf;
152 }
153 
154 
155 
157  uint8_t *buffer,
158  uint32_t size)
159 {
160  GWEN_SYNCIO_MEMORY *xio;
161  uint32_t bytesLeft;
162 
163  assert(sio);
164  xio=GWEN_INHERIT_GETDATA(GWEN_SYNCIO, GWEN_SYNCIO_MEMORY, sio);
165  assert(xio);
166 
167  if (xio->buffer==NULL) {
168  DBG_ERROR(GWEN_LOGDOMAIN, "No buffer");
169  return GWEN_ERROR_INTERNAL;
170  }
171 
172  bytesLeft=GWEN_Buffer_GetBytesLeft(xio->buffer);
173  if (bytesLeft==0) {
174  DBG_VERBOUS(GWEN_LOGDOMAIN, "EOF met");
175  return 0;
176  }
177 
178  if (size>bytesLeft)
179  size=bytesLeft;
180  memmove(buffer, GWEN_Buffer_GetPosPointer(xio->buffer), size);
181  GWEN_Buffer_IncrementPos(xio->buffer, size);
182 
183  return size;
184 }
185 
186 
187 
189  const uint8_t *buffer,
190  uint32_t size)
191 {
192  GWEN_SYNCIO_MEMORY *xio;
193 
194  assert(sio);
195  xio=GWEN_INHERIT_GETDATA(GWEN_SYNCIO, GWEN_SYNCIO_MEMORY, sio);
196  assert(xio);
197 
198  if (xio->buffer==NULL) {
199  DBG_ERROR(GWEN_LOGDOMAIN, "No socket");
200  return GWEN_ERROR_INTERNAL;
201  }
202 
203  if (size) {
204  int rv;
205 
206  rv=GWEN_Buffer_AppendBytes(xio->buffer, (const char *) buffer, size);
207  if (rv<0) {
208  DBG_INFO(GWEN_LOGDOMAIN, "here (%d)", rv);
209  return rv;
210  }
211  }
212 
213  return size;
214 }
215 
216 
217 
218 
219 
int GWENHYWFAR_CB GWEN_SyncIo_Memory_Write(GWEN_SYNCIO *sio, const uint8_t *buffer, uint32_t size)
uint32_t GWEN_Buffer_GetBytesLeft(GWEN_BUFFER *bf)
Definition: buffer.c:537
#define GWEN_SYNCIO_MEMORY_TYPE
Definition: syncio_memory.h:33
GWEN_SYNCIO_WRITE_FN GWEN_SyncIo_SetWriteFn(GWEN_SYNCIO *sio, GWEN_SYNCIO_WRITE_FN fn)
Definition: syncio.c:304
#define GWEN_FREE_OBJECT(varname)
Definition: memory.h:61
#define NULL
Definition: binreloc.c:300
#define DBG_VERBOUS(dbg_logger, format, args...)
Definition: debug.h:224
int GWENHYWFAR_CB GWEN_SyncIo_Memory_Read(GWEN_SYNCIO *sio, uint8_t *buffer, uint32_t size)
#define GWEN_LOGDOMAIN
Definition: logger.h:35
void GWEN_SyncIo_SetStatus(GWEN_SYNCIO *sio, GWEN_SYNCIO_STATUS st)
Definition: syncio.c:206
GWEN_BUFFER * GWEN_Buffer_new(char *buffer, uint32_t size, uint32_t used, int take)
Definition: buffer.c:42
char * GWEN_Buffer_GetPosPointer(const GWEN_BUFFER *bf)
Definition: buffer.c:549
int GWEN_Buffer_IncrementPos(GWEN_BUFFER *bf, uint32_t i)
Definition: buffer.c:452
#define GWEN_NEW_OBJECT(typ, varname)
Definition: memory.h:55
struct GWEN_SYNCIO GWEN_SYNCIO
Definition: syncio.h:40
GWEN_SYNCIO * GWEN_SyncIo_Memory_new(GWEN_BUFFER *buffer, int take)
Definition: syncio_memory.c:50
#define GWENHYWFAR_CB
Definition: gwenhywfarapi.h:89
GWEN_BUFFER * GWEN_SyncIo_Memory_GetBuffer(const GWEN_SYNCIO *sio)
GWEN_SYNCIO * GWEN_SyncIo_Memory_fromBuffer(const uint8_t *buffer, int size)
Definition: syncio_memory.c:78
void GWEN_Buffer_free(GWEN_BUFFER *bf)
Definition: buffer.c:89
struct GWEN_BUFFER GWEN_BUFFER
A dynamically resizeable text buffer.
Definition: buffer.h:38
#define DBG_ERROR(dbg_logger, format, args...)
Definition: debug.h:97
GWEN_SYNCIO * GWEN_SyncIo_new(const char *typeName, GWEN_SYNCIO *baseIo)
Definition: syncio.c:51
#define DBG_INFO(dbg_logger, format, args...)
Definition: debug.h:181
int GWEN_Buffer_AppendBytes(GWEN_BUFFER *bf, const char *buffer, uint32_t size)
Definition: buffer.c:361
void GWEN_Buffer_Rewind(GWEN_BUFFER *bf)
Definition: buffer.c:660
GWEN_BUFFER * GWEN_SyncIo_Memory_TakeBuffer(const GWEN_SYNCIO *sio)
#define GWEN_INHERIT(bt, t)
Definition: inherit.h:264
GWEN_SYNCIO_READ_FN GWEN_SyncIo_SetReadFn(GWEN_SYNCIO *sio, GWEN_SYNCIO_READ_FN fn)
Definition: syncio.c:291
#define GWEN_ERROR_INTERNAL
Definition: error.h:125
#define GWEN_INHERIT_SETDATA(bt, t, element, data, fn)
Definition: inherit.h:292
void GWENHYWFAR_CB GWEN_SyncIo_Memory_FreeData(GWEN_UNUSED void *bp, void *p)
#define GWEN_UNUSED
#define GWEN_INHERIT_GETDATA(bt, t, element)
Definition: inherit.h:271