mazepa.git


mazepa

~ linux userland keylogger
git clone https://github.com/djnnvx/mazepa.git
log | files | refs | archive | README

ringbuf.c (915B)

      1 #include "implant.h"
      2 
      3 #include <string.h>
      4 
      5 void ringbuf_init(ringbuf_t *rb) {
      6     if (!rb)
      7         return;
      8     memset(rb->data, 0, RINGBUF_SIZE);
      9     rb->head = 0;
     10     rb->tail = 0;
     11     rb->count = 0;
     12 }
     13 
     14 uint8_t
     15 ringbuf_push(ringbuf_t *rb, uint8_t byte) {
     16     if (!rb || rb->count >= RINGBUF_SIZE)
     17         return ERROR;
     18 
     19     rb->data[rb->head] = byte;
     20     rb->head = (rb->head + 1) % RINGBUF_SIZE;
     21     rb->count++;
     22     return SUCCESSFUL;
     23 }
     24 
     25 size_t
     26 ringbuf_read(ringbuf_t *rb, uint8_t *dst, size_t len) {
     27     size_t bytes_read = 0;
     28 
     29     if (!rb || !dst)
     30         return 0;
     31 
     32     while (bytes_read < len && rb->count > 0) {
     33         dst[bytes_read] = rb->data[rb->tail];
     34         rb->tail = (rb->tail + 1) % RINGBUF_SIZE;
     35         rb->count--;
     36         bytes_read++;
     37     }
     38     return bytes_read;
     39 }
     40 
     41 int ringbuf_should_flush(ringbuf_t *rb) {
     42     if (!rb)
     43         return 0;
     44     return rb->count >= RINGBUF_FLUSH_THRESHOLD;
     45 }