/usr/include/gwenhywfar4/gwenhywfar/fastbuffer.h is in libgwenhywfar-core-dev 4.20.0-1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 | /***************************************************************************
begin : Tue Apr 27 2010
copyright : (C) 2010 by Martin Preuss
email : martin@libchipcard.de
***************************************************************************
* Please see toplevel file COPYING for license details *
***************************************************************************/
#ifndef GWEN_FASTBUFFER_H
#define GWEN_FASTBUFFER_H
#include <gwenhywfar/syncio.h>
#include <gwenhywfar/buffer.h>
#define GWEN_FAST_BUFFER_FLAGS_DOSMODE 0x00000001
/**
* Do not use the fields of this struct directly!! Only use it via the functions and macros
* in this module, because otherwise future versions of you application might not work.
* Do not allocate such an object yourself, always use @ref GWEN_FastBuffer_new() otherwise
* future versions of you application might not work!
* This struct is not part of the API.
*/
typedef struct {
GWEN_SYNCIO *io;
uint32_t bufferSize;
uint32_t bufferReadPos;
uint32_t bufferWritePos;
uint32_t flags;
uint32_t bytesWritten;
uint32_t bytesRead;
uint8_t buffer[1];
} GWEN_FAST_BUFFER;
#ifdef __cplusplus
extern "C" {
#endif
GWENHYWFAR_API GWEN_FAST_BUFFER *GWEN_FastBuffer_new(uint32_t bsize, GWEN_SYNCIO *io);
GWENHYWFAR_API void GWEN_FastBuffer_free(GWEN_FAST_BUFFER *fb);
GWENHYWFAR_API uint32_t GWEN_FastBuffer_GetFlags(const GWEN_FAST_BUFFER *fb);
GWENHYWFAR_API void GWEN_FastBuffer_SetFlags(GWEN_FAST_BUFFER *fb, uint32_t fl);
GWENHYWFAR_API void GWEN_FastBuffer_AddFlags(GWEN_FAST_BUFFER *fb, uint32_t fl);
GWENHYWFAR_API void GWEN_FastBuffer_SubFlags(GWEN_FAST_BUFFER *fb, uint32_t fl);
GWENHYWFAR_API uint32_t GWEN_FastBuffer_GetBytesWritten(const GWEN_FAST_BUFFER *fb);
GWENHYWFAR_API uint32_t GWEN_FastBuffer_GetBytesRead(const GWEN_FAST_BUFFER *fb);
GWENHYWFAR_API int GWEN_FastBuffer_ReadLine(GWEN_FAST_BUFFER *fb, uint8_t *p, int len);
GWENHYWFAR_API int GWEN_FastBuffer_ReadLineToBuffer(GWEN_FAST_BUFFER *fb, GWEN_BUFFER *buf);
#ifdef __cplusplus
}
#endif
/**
* This macro peeks at the read buffer and returns the next available byte (if any).
* Consecutive peeks will always return the same byte. Also, the next @ref GWEN_FASTBUFFER_READBYTE
* will return the same byte as well.
*/
#define GWEN_FASTBUFFER_PEEKBYTE(fb, var) {\
if (fb->bufferReadPos>=fb->bufferWritePos) { \
int fb_peekbyte_rv; \
\
fb_peekbyte_rv=GWEN_SyncIo_Read(fb->io, fb->buffer, fb->bufferSize); \
if (fb_peekbyte_rv<0) { \
DBG_DEBUG(GWEN_LOGDOMAIN, "here (%d)", fb_peekbyte_rv); \
var=fb_peekbyte_rv; \
} \
else if (fb_peekbyte_rv==0) { \
DBG_DEBUG(GWEN_LOGDOMAIN, "EOF met"); \
var=GWEN_ERROR_EOF; \
} \
else { \
fb->bufferWritePos=fb_peekbyte_rv; \
fb->bufferReadPos=0; \
var=((int)((fb->buffer[fb->bufferReadPos])) & 0xff); \
} \
} \
else { \
var=((int)((fb->buffer[fb->bufferReadPos])) & 0xff); \
} \
}
/**
* Returns the next byte from the buffer (if any). That byte will be placed into "var". In case of an error
* var will contain an error code instead.
*/
#define GWEN_FASTBUFFER_READBYTE(fb, var) {\
if (fb->bufferReadPos>=fb->bufferWritePos) { \
int fb_readbyte_rv; \
\
fb_readbyte_rv=GWEN_SyncIo_Read(fb->io, fb->buffer, fb->bufferSize); \
if (fb_readbyte_rv<0) { \
DBG_DEBUG(GWEN_LOGDOMAIN, "here (%d)", fb_readbyte_rv); \
var=fb_readbyte_rv; \
} \
else if (fb_readbyte_rv==0) { \
DBG_DEBUG(GWEN_LOGDOMAIN, "EOF met"); \
var=GWEN_ERROR_EOF; \
} \
else { \
fb->bufferWritePos=fb_readbyte_rv; \
fb->bufferReadPos=0; \
var=((int)((fb->buffer[fb->bufferReadPos++])) & 0xff); \
fb->bytesRead++; \
} \
} \
else { \
var=((int)((fb->buffer[fb->bufferReadPos++])) & 0xff); \
fb->bytesRead++; \
} \
}
/**
* Writes a byte into the buffer (flushing it if necessary) and returns the result of this operation
* in "var".
*/
#define GWEN_FASTBUFFER_WRITEBYTE(fb, var, chr) {\
if (fb->bufferWritePos>=fb->bufferSize) { \
int fb_writeByte_rv; \
\
fb_writeByte_rv=GWEN_SyncIo_WriteForced(fb->io, fb->buffer, fb->bufferWritePos); \
if (fb_writeByte_rv<(int)(fb->bufferWritePos)) { \
DBG_INFO(GWEN_LOGDOMAIN, "here (%d)", fb_writeByte_rv); \
var=fb_writeByte_rv; \
} \
else { \
var=0; \
fb->bufferWritePos=0; \
fb->buffer[fb->bufferWritePos++]=chr; \
fb->bytesWritten++; \
} \
} \
else { \
var=0; \
fb->buffer[fb->bufferWritePos++]=chr; \
fb->bytesWritten++; \
} \
}
/**
* Flushes the write buffer (i.e. write all remaining bytes from the buffer to the io layer with
* the flag @ref GWEN_IO_REQUEST_FLAGS_FLUSH set).
*/
#define GWEN_FASTBUFFER_FLUSH(fb, var) {\
int fb_flush_rv; \
\
fb_flush_rv=GWEN_SyncIo_WriteForced(fb->io, fb->buffer, fb->bufferWritePos); \
if (fb_flush_rv<(int)(fb->bufferWritePos)) { \
DBG_INFO(GWEN_LOGDOMAIN, "here (%d)", fb_flush_rv); \
var=fb_flush_rv; \
} \
else { \
var=0; \
fb->bufferWritePos=0; \
} \
}
/**
* Reads a number of bytes from the buffer and stores it at the given memory location.
* @param fb fast buffer
* @param var variable to receive the result (<0: error code, number of bytes read otherwise)
* @param p pointer to the location to read the bytes to
* @param len number of bytes to read
*/
#define GWEN_FASTBUFFER_READBYTES(fb, var, p, len) { \
int fb_readbyte_bytes;\
\
var=0; \
if (fb->bufferReadPos>=fb->bufferWritePos) { \
int fb_readbyte_rv; \
\
fb_readbyte_rv=GWEN_SyncIo_Read(fb->io, fb->buffer, fb->bufferSize); \
if (fb_readbyte_rv<0) { \
DBG_DEBUG(GWEN_LOGDOMAIN, "here (%d)", fb_readbyte_rv); \
var=fb_readbyte_rv; \
} \
else {\
fb->bufferWritePos=fb_readbyte_rv; \
fb->bufferReadPos=0; \
}\
}\
if (var==0) {\
fb_readbyte_bytes=fb->bufferWritePos-fb->bufferReadPos;\
if (fb_readbyte_bytes>len)\
fb_readbyte_bytes=len;\
if (fb_readbyte_bytes) {\
memmove(p, fb->buffer+fb->bufferReadPos, fb_readbyte_bytes);\
fb->bufferReadPos+=fb_readbyte_bytes;\
fb->bytesRead+=fb_readbyte_bytes; \
}\
var=fb_readbyte_bytes;\
}\
}
#define GWEN_FASTBUFFER_READLINE(fb, var, p, len) {\
int fb_readline_bytes;\
\
var=0;\
if (fb->bufferReadPos>=fb->bufferWritePos) {\
int fb_readline_rv;\
\
fb_readline_rv=GWEN_SyncIo_Read(fb->io, fb->buffer, fb->bufferSize);\
if (fb_readline_rv<0) {\
DBG_DEBUG(GWEN_LOGDOMAIN, "here (%d)", fb_readline_rv);\
var=fb_readline_rv;\
}\
else {\
fb->bufferWritePos=fb_rv; \
fb->bufferReadPos=0; \
}\
}\
if (var==0) {\
fb_readline_bytes=fb->bufferWritePos-fb->bufferReadPos;\
if (fb_readline_bytes>len)\
fb_readline_bytes=len;\
if (fb_readline_bytes) {\
uint8_t *fb_readline_p;\
\
fb_readline_p=(uint8_t*)p;\
\
while(fb_readline_bytes) {\
uint8_t c;\
\
c=fb->buffer[fb->bufferReadPos++];\
fb->bytesRead++; \
fb_readline_bytes--;\
if (c==10) {\
*(fb_readline_p++)=c;\
var++;\
break;\
}\
else if (c!=13) {\
*(fb_readline_p++)=c;\
var++;\
}\
}\
}\
}\
}
#define GWEN_FASTBUFFER_READLINEFORCED(fb, var, p, len) {\
int fb_readlineforced_len;\
uint8_t *fb_readlineforced_p;\
\
fb_readlineforced_len=len;\
fb_readlineforced_p=(uint8_t*)p;\
var=0;\
while(fb_readlineforced_len && var==0) {\
int fb_readlineforced_rv;\
\
GWEN_FASTBUFFER_READLINE(fb, fb_readlineforced_rv, fb_readlineforced_p, fb_readlineforced_len);\
if (fb_readlineforced_rv<0) {\
var=fb_readlineforced_rv;\
break;\
}\
else if (fb_readlineforced_rv==0) {\
var=GWEN_ERROR_EOF;\
break;\
}\
else {\
if (fb_readlineforced_p[fb_readlineforced_rv-1]==10) {\
fb_readlineforced_p[fb_readlineforced_rv-1]=0;\
var=fb_readlineforced_rv;\
break;\
}\
fb_readlineforced_len-=fb_readlineforced_rv;\
fb_readlineforced_p+=fb_readlineforced_rv;\
}\
}\
if (var==0) {\
DBG_INFO(GWEN_LOGDOMAIN, "No line within %d bytes", len);\
var=GWEN_ERROR_BAD_SIZE;\
}\
}
#define GWEN_FASTBUFFER_READFORCED(fb, var, p, len) {\
int fb_readforced_len;\
uint8_t *fb_readforced_p;\
\
fb_readforced_len=len;\
fb_readforced_p=(uint8_t*)p;\
var=0;\
while(fb_readforced_len && var==0) {\
int fb_readforced_rv;\
\
GWEN_FASTBUFFER_READBYTES(fb, fb_readforced_rv, fb_readforced_p, fb_readforced_len);\
if (fb_readforced_rv<0) {\
var=fb_readforced_rv;\
break;\
}\
else if (fb_readforced_rv==0) {\
var=GWEN_ERROR_EOF;\
break;\
}\
else {\
fb_readforced_len-=fb_readforced_rv;\
fb_readforced_p+=fb_readforced_rv;\
}\
}\
}
/**
* Write a number of bytes to the buffer and stores it at the given memory location.
* @param fb fast buffer
* @param var variable to receive the result (<0: error code, number of bytes read otherwise)
* @param p pointer to the location to write the bytes from
* @param len number of bytes to write
*/
#define GWEN_FASTBUFFER_WRITEBYTES(fb, var, p, len) {\
int fb_writebytes_bytes;\
int fb_writebytes_len;\
\
fb_writebytes_len=len;\
if (fb_writebytes_len==-1)\
fb_writebytes_len=strlen((const char*)p);\
var=0; \
if (fb->bufferWritePos>=fb->bufferSize) { \
int fb_writebytes_rv; \
\
fb_writebytes_rv=GWEN_SyncIo_WriteForced(fb->io, fb->buffer, fb->bufferWritePos); \
if (fb_writebytes_rv<(int)(fb->bufferWritePos)) { \
DBG_DEBUG(GWEN_LOGDOMAIN, "here (%d)", fb_writebytes_rv); \
var=fb_writebytes_rv; \
} \
else {\
fb->bufferWritePos=0; \
}\
}\
if (var==0) {\
fb_writebytes_bytes=fb->bufferSize-fb->bufferWritePos;\
if (fb_writebytes_bytes>fb_writebytes_len)\
fb_writebytes_bytes=fb_writebytes_len;\
if (fb_writebytes_bytes) {\
memmove(fb->buffer+fb->bufferWritePos, p, fb_writebytes_bytes);\
fb->bufferWritePos+=fb_writebytes_bytes;\
fb->bytesWritten+=fb_writebytes_bytes; \
}\
var=fb_writebytes_bytes;\
}\
}
/**
* Write a number of bytes to the buffer and make sure that @b all bytes are written.
* @param fb fast buffer
* @param var variable to receive the result (<0: error code, 0 on success)
* @param p pointer to the location to write the bytes from
* @param len number of bytes to write
*/
#define GWEN_FASTBUFFER_WRITEFORCED(fb, var, p, len) {\
int fb_writeforced_len;\
const uint8_t *fb_writeforced_p;\
\
fb_writeforced_len=len;\
if (fb_writeforced_len==-1) \
fb_writeforced_len=strlen((const char*)p);\
fb_writeforced_p=(const uint8_t*)p;\
var=0;\
while(fb_writeforced_len && var==0) {\
int fb_writeforced_rv;\
\
GWEN_FASTBUFFER_WRITEBYTES(fb, fb_writeforced_rv, fb_writeforced_p, fb_writeforced_len);\
if (fb_writeforced_rv<0) {\
var=fb_writeforced_rv;\
break;\
}\
else if (fb_writeforced_rv==0) {\
var=GWEN_ERROR_EOF;\
break;\
}\
else {\
fb_writeforced_len-=fb_writeforced_rv;\
fb_writeforced_p+=fb_writeforced_rv;\
}\
}\
}
#define GWEN_FASTBUFFER_WRITELINE(fb, var, p) {\
int fb_writeline_rv;\
int fb_writeline_len=strlen((const char*)p);\
\
GWEN_FASTBUFFER_WRITEFORCED(fb, fb_writeline_rv, p, fb_writeline_len);\
if (fb_writeline_rv<0)\
var=fb_writeline_rv;\
else {\
if (fb->flags & GWEN_FAST_BUFFER_FLAGS_DOSMODE) {\
GWEN_FASTBUFFER_WRITEFORCED(fb, fb_writeline_rv, "\r\n", 2);\
}\
else {\
GWEN_FASTBUFFER_WRITEFORCED(fb, fb_writeline_rv, "\n", 1);\
}\
if (fb_writeline_rv<0)\
var=fb_writeline_rv;\
else\
var=0;\
}\
}
/**
* Copy a number of bytes from one buffer to another one.
* @param fb1 source fast buffer
* @param fb2 destination fast buffer
* @param var variable to receive the result (<0: error code, number of bytes read otherwise)
* @param len number of bytes to copy
*/
#define GWEN_FASTBUFFER_COPYBYTES(fb1, fb2, var, len) { \
int fb_copybytes_bytes;\
\
var=0; \
if (fb1->bufferReadPos>=fb1->bufferWritePos) { \
int fb_copybytes_rv; \
\
fb_copybytes_rv=GWEN_SyncIo_Read(fb1->io, fb1->buffer, fb1->bufferSize); \
if (fb_copybytes_rv<0) { \
DBG_DEBUG(GWEN_LOGDOMAIN, "here (%d)", fb_copybytes_rv); \
var=fb_copybytes_rv; \
} \
else {\
fb1->bufferWritePos=fb_copybytes_rv; \
fb1->bufferReadPos=0; \
}\
}\
if (var==0) {\
fb_bytes=fb1->bufferWritePos-fb1->bufferReadPos;\
if (fb_copybytes_bytes>len)\
fb_copybytes_bytes=len;\
if (fb_copybytes_bytes) {\
int fb_copybytes_rv;\
\
GWEN_FASTBUFFER_WRITEBYTES(fb2, fb_copybytes_rv, (fb1->buffer+fb1->bufferReadPos), fb_bytes);\
var=fb_copybytes_rv;\
if (fb_copybytes_rv>0) {\
fb1->bufferReadPos+=fb_copybytes_rv;\
fb1->bytesRead+=fb_copybytes_rv; \
}\
}\
}\
}
/**
* Copy a number of bytes to the buffer and make sure that @b all bytes are copied.
* @param fb1 source fast buffer
* @param fb2 destination fast buffer
* @param var variable to receive the result (<0: error code, 0 on success)
* @param p pointer to the location to write the bytes from
* @param len number of bytes to copy
*/
#define GWEN_FASTBUFFER_COPYFORCED(fb1, fb2, var, p, len) {\
int fb_copyforced_len;\
uint8_t *fb_copyforced_p;\
\
fb_copyforced_len=len;\
fb_copyforced_p=(uint8_t*)p;\
var=0;\
while(fb_copyforced_len && var==0) {\
int fb_copyforced_rv;\
\
GWEN_FASTBUFFER_COPYBYTES(fb1, fb2, fb_copyforced_rv, fb_copyforced_p, fb_copyforced_len);\
if (fb_copyforced_rv<0) {\
var=fb_copyforced_rv;\
break;\
}\
else if (fb_copyforced_rv==0)\
var=GWEN_ERROR_EOF;\
else {\
fb_len-=fb_copyforced_rv;\
fb_p+=fb_copyforced_rv;\
}\
}\
}
#endif
|