gwenhywfar 4.0.3
|
00001 /*************************************************************************** 00002 begin : Tue Apr 27 2010 00003 copyright : (C) 2010 by Martin Preuss 00004 email : martin@libchipcard.de 00005 00006 *************************************************************************** 00007 * * 00008 * This library is free software; you can redistribute it and/or * 00009 * modify it under the terms of the GNU Lesser General Public * 00010 * License as published by the Free Software Foundation; either * 00011 * version 2.1 of the License, or (at your option) any later version. * 00012 * * 00013 * This library is distributed in the hope that it will be useful, * 00014 * but WITHOUT ANY WARRANTY; without even the implied warranty of * 00015 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * 00016 * Lesser General Public License for more details. * 00017 * * 00018 * You should have received a copy of the GNU Lesser General Public * 00019 * License along with this library; if not, write to the Free Software * 00020 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, * 00021 * MA 02111-1307 USA * 00022 * * 00023 ***************************************************************************/ 00024 00025 #ifdef HAVE_CONFIG_H 00026 # include <config.h> 00027 #endif 00028 00029 #define DISABLE_DEBUGLOG 00030 00031 00032 00033 #include "syncio_memory_p.h" 00034 #include "i18n_l.h" 00035 00036 #include <gwenhywfar/misc.h> 00037 #include <gwenhywfar/debug.h> 00038 #include <gwenhywfar/gui.h> 00039 00040 #include <assert.h> 00041 #include <errno.h> 00042 #include <string.h> 00043 00044 00045 00046 GWEN_INHERIT(GWEN_SYNCIO, GWEN_SYNCIO_MEMORY) 00047 00048 00049 00050 GWEN_SYNCIO *GWEN_SyncIo_Memory_new(GWEN_BUFFER *buffer, int take) { 00051 GWEN_SYNCIO *sio; 00052 GWEN_SYNCIO_MEMORY *xio; 00053 00054 sio=GWEN_SyncIo_new(GWEN_SYNCIO_MEMORY_TYPE, NULL); 00055 GWEN_NEW_OBJECT(GWEN_SYNCIO_MEMORY, xio); 00056 GWEN_INHERIT_SETDATA(GWEN_SYNCIO, GWEN_SYNCIO_MEMORY, sio, xio, GWEN_SyncIo_Memory_FreeData); 00057 00058 GWEN_SyncIo_SetReadFn(sio, GWEN_SyncIo_Memory_Read); 00059 GWEN_SyncIo_SetWriteFn(sio, GWEN_SyncIo_Memory_Write); 00060 00061 if (buffer) { 00062 xio->buffer=buffer; 00063 xio->own=take?1:0; 00064 } 00065 else { 00066 xio->buffer=GWEN_Buffer_new(0, 256, 0, 1); 00067 xio->own=1; 00068 } 00069 00070 return sio; 00071 } 00072 00073 00074 00075 GWEN_SYNCIO *GWEN_SyncIo_Memory_fromBuffer(const uint8_t *buffer, int size) { 00076 GWEN_SYNCIO *sio; 00077 GWEN_SYNCIO_MEMORY *xio; 00078 00079 sio=GWEN_SyncIo_new(GWEN_SYNCIO_MEMORY_TYPE, NULL); 00080 GWEN_NEW_OBJECT(GWEN_SYNCIO_MEMORY, xio); 00081 GWEN_INHERIT_SETDATA(GWEN_SYNCIO, GWEN_SYNCIO_MEMORY, sio, xio, GWEN_SyncIo_Memory_FreeData); 00082 00083 GWEN_SyncIo_SetReadFn(sio, GWEN_SyncIo_Memory_Read); 00084 GWEN_SyncIo_SetWriteFn(sio, GWEN_SyncIo_Memory_Write); 00085 00086 /* adapt size, if necessary */ 00087 if (size==-1) { 00088 if (buffer) 00089 size=strlen((const char*) buffer)+1; 00090 else 00091 size=0; 00092 } 00093 00094 xio->buffer=GWEN_Buffer_new(0, size, 0, 1); 00095 xio->own=1; 00096 if (buffer && size>0) 00097 GWEN_Buffer_AppendBytes(xio->buffer, (const char*) buffer, size); 00098 00099 return sio; 00100 } 00101 00102 00103 00104 void GWENHYWFAR_CB GWEN_SyncIo_Memory_FreeData(void *bp, void *p) { 00105 GWEN_SYNCIO_MEMORY *xio; 00106 00107 xio=(GWEN_SYNCIO_MEMORY*) p; 00108 if (xio->buffer && xio->own) 00109 GWEN_Buffer_free(xio->buffer); 00110 GWEN_FREE_OBJECT(xio); 00111 } 00112 00113 00114 00115 GWEN_BUFFER *GWEN_SyncIo_Memory_GetBuffer(const GWEN_SYNCIO *sio) { 00116 GWEN_SYNCIO_MEMORY *xio; 00117 00118 assert(sio); 00119 xio=GWEN_INHERIT_GETDATA(GWEN_SYNCIO, GWEN_SYNCIO_MEMORY, sio); 00120 assert(xio); 00121 00122 return xio->buffer; 00123 } 00124 00125 00126 00127 GWEN_BUFFER *GWEN_SyncIo_Memory_TakeBuffer(const GWEN_SYNCIO *sio) { 00128 GWEN_SYNCIO_MEMORY *xio; 00129 GWEN_BUFFER *buf; 00130 00131 assert(sio); 00132 xio=GWEN_INHERIT_GETDATA(GWEN_SYNCIO, GWEN_SYNCIO_MEMORY, sio); 00133 assert(xio); 00134 00135 if (xio->own==0 || xio->buffer==NULL) { 00136 DBG_ERROR(GWEN_LOGDOMAIN, "Can't give away buffer, object does not own it"); 00137 return NULL; 00138 } 00139 buf=xio->buffer; 00140 xio->buffer=NULL; 00141 xio->own=0; 00142 return buf; 00143 } 00144 00145 00146 00147 int GWENHYWFAR_CB GWEN_SyncIo_Memory_Read(GWEN_SYNCIO *sio, 00148 uint8_t *buffer, 00149 uint32_t size) { 00150 GWEN_SYNCIO_MEMORY *xio; 00151 uint32_t bytesLeft; 00152 00153 assert(sio); 00154 xio=GWEN_INHERIT_GETDATA(GWEN_SYNCIO, GWEN_SYNCIO_MEMORY, sio); 00155 assert(xio); 00156 00157 if (xio->buffer==NULL) { 00158 DBG_ERROR(GWEN_LOGDOMAIN, "No buffer"); 00159 return GWEN_ERROR_INTERNAL; 00160 } 00161 00162 bytesLeft=GWEN_Buffer_GetBytesLeft(xio->buffer); 00163 if (bytesLeft==0) { 00164 DBG_VERBOUS(GWEN_LOGDOMAIN, "EOF met"); 00165 return 0; 00166 } 00167 00168 if (size>bytesLeft) 00169 size=bytesLeft; 00170 memmove(buffer, GWEN_Buffer_GetPosPointer(xio->buffer), size); 00171 GWEN_Buffer_IncrementPos(xio->buffer, size); 00172 00173 return size; 00174 } 00175 00176 00177 00178 int GWENHYWFAR_CB GWEN_SyncIo_Memory_Write(GWEN_SYNCIO *sio, 00179 const uint8_t *buffer, 00180 uint32_t size) { 00181 GWEN_SYNCIO_MEMORY *xio; 00182 00183 assert(sio); 00184 xio=GWEN_INHERIT_GETDATA(GWEN_SYNCIO, GWEN_SYNCIO_MEMORY, sio); 00185 assert(xio); 00186 00187 if (xio->buffer==NULL) { 00188 DBG_ERROR(GWEN_LOGDOMAIN, "No socket"); 00189 return GWEN_ERROR_INTERNAL; 00190 } 00191 00192 if (size) { 00193 int rv; 00194 00195 rv=GWEN_Buffer_AppendBytes(xio->buffer, (const char*) buffer, size); 00196 if (rv<0) { 00197 DBG_INFO(GWEN_LOGDOMAIN, "here (%d)", rv); 00198 return rv; 00199 } 00200 } 00201 00202 return size; 00203 } 00204 00205 00206 00207 00208