gwenhywfar  4.7.0beta
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  GWEN_SYNCIO *sio;
52  GWEN_SYNCIO_MEMORY *xio;
53 
55  GWEN_NEW_OBJECT(GWEN_SYNCIO_MEMORY, xio);
56  GWEN_INHERIT_SETDATA(GWEN_SYNCIO, GWEN_SYNCIO_MEMORY, sio, xio, GWEN_SyncIo_Memory_FreeData);
57 
60 
61  if (buffer) {
62  xio->buffer=buffer;
63  xio->own=take?1:0;
64  }
65  else {
66  xio->buffer=GWEN_Buffer_new(0, 256, 0, 1);
67  xio->own=1;
68  }
69 
71 
72  return sio;
73 }
74 
75 
76 
77 GWEN_SYNCIO *GWEN_SyncIo_Memory_fromBuffer(const uint8_t *buffer, int size) {
78  GWEN_SYNCIO *sio;
79  GWEN_SYNCIO_MEMORY *xio;
80 
82  GWEN_NEW_OBJECT(GWEN_SYNCIO_MEMORY, xio);
83  GWEN_INHERIT_SETDATA(GWEN_SYNCIO, GWEN_SYNCIO_MEMORY, sio, xio, GWEN_SyncIo_Memory_FreeData);
84 
87 
88  /* adapt size, if necessary */
89  if (size==-1) {
90  if (buffer)
91  size=strlen((const char*) buffer)+1;
92  else
93  size=0;
94  }
95 
96  xio->buffer=GWEN_Buffer_new(0, size, 0, 1);
97  xio->own=1;
98  if (buffer && size>0) {
99  GWEN_Buffer_AppendBytes(xio->buffer, (const char*) buffer, size);
100  GWEN_Buffer_Rewind(xio->buffer);
101  }
103  return sio;
104 }
105 
106 
107 
109  GWEN_SYNCIO_MEMORY *xio;
110 
111  xio=(GWEN_SYNCIO_MEMORY*) p;
112  if (xio->buffer && xio->own)
113  GWEN_Buffer_free(xio->buffer);
114  GWEN_FREE_OBJECT(xio);
115 }
116 
117 
118 
120  GWEN_SYNCIO_MEMORY *xio;
121 
122  assert(sio);
123  xio=GWEN_INHERIT_GETDATA(GWEN_SYNCIO, GWEN_SYNCIO_MEMORY, sio);
124  assert(xio);
125 
126  return xio->buffer;
127 }
128 
129 
130 
132  GWEN_SYNCIO_MEMORY *xio;
133  GWEN_BUFFER *buf;
134 
135  assert(sio);
136  xio=GWEN_INHERIT_GETDATA(GWEN_SYNCIO, GWEN_SYNCIO_MEMORY, sio);
137  assert(xio);
138 
139  if (xio->own==0 || xio->buffer==NULL) {
140  DBG_ERROR(GWEN_LOGDOMAIN, "Can't give away buffer, object does not own it");
141  return NULL;
142  }
143  buf=xio->buffer;
144  xio->buffer=NULL;
145  xio->own=0;
146  return buf;
147 }
148 
149 
150 
152  uint8_t *buffer,
153  uint32_t size) {
154  GWEN_SYNCIO_MEMORY *xio;
155  uint32_t bytesLeft;
156 
157  assert(sio);
158  xio=GWEN_INHERIT_GETDATA(GWEN_SYNCIO, GWEN_SYNCIO_MEMORY, sio);
159  assert(xio);
160 
161  if (xio->buffer==NULL) {
162  DBG_ERROR(GWEN_LOGDOMAIN, "No buffer");
163  return GWEN_ERROR_INTERNAL;
164  }
165 
166  bytesLeft=GWEN_Buffer_GetBytesLeft(xio->buffer);
167  if (bytesLeft==0) {
168  DBG_VERBOUS(GWEN_LOGDOMAIN, "EOF met");
169  return 0;
170  }
171 
172  if (size>bytesLeft)
173  size=bytesLeft;
174  memmove(buffer, GWEN_Buffer_GetPosPointer(xio->buffer), size);
175  GWEN_Buffer_IncrementPos(xio->buffer, size);
176 
177  return size;
178 }
179 
180 
181 
183  const uint8_t *buffer,
184  uint32_t size) {
185  GWEN_SYNCIO_MEMORY *xio;
186 
187  assert(sio);
188  xio=GWEN_INHERIT_GETDATA(GWEN_SYNCIO, GWEN_SYNCIO_MEMORY, sio);
189  assert(xio);
190 
191  if (xio->buffer==NULL) {
192  DBG_ERROR(GWEN_LOGDOMAIN, "No socket");
193  return GWEN_ERROR_INTERNAL;
194  }
195 
196  if (size) {
197  int rv;
198 
199  rv=GWEN_Buffer_AppendBytes(xio->buffer, (const char*) buffer, size);
200  if (rv<0) {
201  DBG_INFO(GWEN_LOGDOMAIN, "here (%d)", rv);
202  return rv;
203  }
204  }
205 
206  return size;
207 }
208 
209 
210 
211 
212