gwenhywfar
4.3.1
|
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 GWEN_SyncIo_SetStatus(sio, GWEN_SyncIo_Status_Connected); 00071 00072 return sio; 00073 } 00074 00075 00076 00077 GWEN_SYNCIO *GWEN_SyncIo_Memory_fromBuffer(const uint8_t *buffer, int size) { 00078 GWEN_SYNCIO *sio; 00079 GWEN_SYNCIO_MEMORY *xio; 00080 00081 sio=GWEN_SyncIo_new(GWEN_SYNCIO_MEMORY_TYPE, NULL); 00082 GWEN_NEW_OBJECT(GWEN_SYNCIO_MEMORY, xio); 00083 GWEN_INHERIT_SETDATA(GWEN_SYNCIO, GWEN_SYNCIO_MEMORY, sio, xio, GWEN_SyncIo_Memory_FreeData); 00084 00085 GWEN_SyncIo_SetReadFn(sio, GWEN_SyncIo_Memory_Read); 00086 GWEN_SyncIo_SetWriteFn(sio, GWEN_SyncIo_Memory_Write); 00087 00088 /* adapt size, if necessary */ 00089 if (size==-1) { 00090 if (buffer) 00091 size=strlen((const char*) buffer)+1; 00092 else 00093 size=0; 00094 } 00095 00096 xio->buffer=GWEN_Buffer_new(0, size, 0, 1); 00097 xio->own=1; 00098 if (buffer && size>0) { 00099 GWEN_Buffer_AppendBytes(xio->buffer, (const char*) buffer, size); 00100 GWEN_Buffer_Rewind(xio->buffer); 00101 } 00102 GWEN_SyncIo_SetStatus(sio, GWEN_SyncIo_Status_Connected); 00103 return sio; 00104 } 00105 00106 00107 00108 void GWENHYWFAR_CB GWEN_SyncIo_Memory_FreeData(void *bp, void *p) { 00109 GWEN_SYNCIO_MEMORY *xio; 00110 00111 xio=(GWEN_SYNCIO_MEMORY*) p; 00112 if (xio->buffer && xio->own) 00113 GWEN_Buffer_free(xio->buffer); 00114 GWEN_FREE_OBJECT(xio); 00115 } 00116 00117 00118 00119 GWEN_BUFFER *GWEN_SyncIo_Memory_GetBuffer(const GWEN_SYNCIO *sio) { 00120 GWEN_SYNCIO_MEMORY *xio; 00121 00122 assert(sio); 00123 xio=GWEN_INHERIT_GETDATA(GWEN_SYNCIO, GWEN_SYNCIO_MEMORY, sio); 00124 assert(xio); 00125 00126 return xio->buffer; 00127 } 00128 00129 00130 00131 GWEN_BUFFER *GWEN_SyncIo_Memory_TakeBuffer(const GWEN_SYNCIO *sio) { 00132 GWEN_SYNCIO_MEMORY *xio; 00133 GWEN_BUFFER *buf; 00134 00135 assert(sio); 00136 xio=GWEN_INHERIT_GETDATA(GWEN_SYNCIO, GWEN_SYNCIO_MEMORY, sio); 00137 assert(xio); 00138 00139 if (xio->own==0 || xio->buffer==NULL) { 00140 DBG_ERROR(GWEN_LOGDOMAIN, "Can't give away buffer, object does not own it"); 00141 return NULL; 00142 } 00143 buf=xio->buffer; 00144 xio->buffer=NULL; 00145 xio->own=0; 00146 return buf; 00147 } 00148 00149 00150 00151 int GWENHYWFAR_CB GWEN_SyncIo_Memory_Read(GWEN_SYNCIO *sio, 00152 uint8_t *buffer, 00153 uint32_t size) { 00154 GWEN_SYNCIO_MEMORY *xio; 00155 uint32_t bytesLeft; 00156 00157 assert(sio); 00158 xio=GWEN_INHERIT_GETDATA(GWEN_SYNCIO, GWEN_SYNCIO_MEMORY, sio); 00159 assert(xio); 00160 00161 if (xio->buffer==NULL) { 00162 DBG_ERROR(GWEN_LOGDOMAIN, "No buffer"); 00163 return GWEN_ERROR_INTERNAL; 00164 } 00165 00166 bytesLeft=GWEN_Buffer_GetBytesLeft(xio->buffer); 00167 if (bytesLeft==0) { 00168 DBG_VERBOUS(GWEN_LOGDOMAIN, "EOF met"); 00169 return 0; 00170 } 00171 00172 if (size>bytesLeft) 00173 size=bytesLeft; 00174 memmove(buffer, GWEN_Buffer_GetPosPointer(xio->buffer), size); 00175 GWEN_Buffer_IncrementPos(xio->buffer, size); 00176 00177 return size; 00178 } 00179 00180 00181 00182 int GWENHYWFAR_CB GWEN_SyncIo_Memory_Write(GWEN_SYNCIO *sio, 00183 const uint8_t *buffer, 00184 uint32_t size) { 00185 GWEN_SYNCIO_MEMORY *xio; 00186 00187 assert(sio); 00188 xio=GWEN_INHERIT_GETDATA(GWEN_SYNCIO, GWEN_SYNCIO_MEMORY, sio); 00189 assert(xio); 00190 00191 if (xio->buffer==NULL) { 00192 DBG_ERROR(GWEN_LOGDOMAIN, "No socket"); 00193 return GWEN_ERROR_INTERNAL; 00194 } 00195 00196 if (size) { 00197 int rv; 00198 00199 rv=GWEN_Buffer_AppendBytes(xio->buffer, (const char*) buffer, size); 00200 if (rv<0) { 00201 DBG_INFO(GWEN_LOGDOMAIN, "here (%d)", rv); 00202 return rv; 00203 } 00204 } 00205 00206 return size; 00207 } 00208 00209 00210 00211 00212