summaryrefslogtreecommitdiff
path: root/src/packet_storage.c
blob: 922cc4505c080b2790997562d31875efea1f85b1 (plain) (blame)
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
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
#include "common.c"
#include <dirent.h>
#include <pthread.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/stat.h>
#include <zlib.h>

const uint64_t max_storage    = 1 << 16;
const uint64_t max_authors    = 1 << 14;
const uint64_t max_recipients = 1 << 14;

#define hash_fmt                                                                                   \
    "%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x"                             \
    "%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x"

#define hash_ref(_hash)                                                                            \
    _hash[ 0 ], _hash[ 1 ], _hash[ 2 ], _hash[ 3 ], _hash[ 4 ], _hash[ 5 ], _hash[ 6 ],            \
        _hash[ 7 ], _hash[ 8 ], _hash[ 9 ], _hash[ 10 ], _hash[ 11 ], _hash[ 12 ], _hash[ 13 ],    \
        _hash[ 14 ], _hash[ 15 ], _hash[ 16 ], _hash[ 17 ], _hash[ 18 ], _hash[ 19 ], _hash[ 20 ], \
        _hash[ 21 ], _hash[ 22 ], _hash[ 23 ], _hash[ 24 ], _hash[ 25 ], _hash[ 26 ], _hash[ 27 ], \
        _hash[ 28 ], _hash[ 29 ], _hash[ 30 ], _hash[ 31 ]

uint64_t current_packet_ptr = 0;
uint64_t packet_count       = 0;
struct data_packet_entry {
    byte    *compressed_payload;
    uint32_t compressed_size;
    uint64_t next;
    uint64_t prev;
    uint64_t author_next;
    uint64_t author_prev;
    uint64_t recipient_next;
    uint64_t recipient_prev;
    bool     is_full;
}       *packet_index;
byte    *packet_arena;
uint64_t packet_arena_size;

struct author_entry {
    public_key author;
    uint64_t   packet_head;
    uint64_t   next;
    uint64_t   prev;
    bool       is_full;
} *author_storage;

struct recipient_entry {
    recipient recipient;
    uint64_t  packet_head;
    uint64_t  next;
    uint64_t  prev;
    bool      is_full;
} *recipient_storage;

pthread_mutex_t author_storage_mutex    = PTHREAD_MUTEX_INITIALIZER;
pthread_mutex_t recipient_storage_mutex = PTHREAD_MUTEX_INITIALIZER;

#define invalid_index ((uint64_t) 0)

#define BLOCK_HEADER_SIZE (sizeof(uint8_t) + sizeof(uint32_t))

static void set_block_header(byte *block, uint8_t full, uint32_t size) {
    block[ 0 ]                = full;
    *(uint32_t *) (block + 1) = size;
}

static uint8_t get_block_full(byte *block) { return block[ 0 ]; }

static uint32_t get_block_size(byte *block) { return *(uint32_t *) (block + 1); }

byte *arena_alloc(uint32_t size) {
    uint32_t payload_size = (size + 7) & ~7;
    uint32_t total_size   = BLOCK_HEADER_SIZE + payload_size;

    byte *curr = packet_arena;
    while (curr < packet_arena + current_packet_ptr) {
        if (!get_block_full(curr) && get_block_size(curr) >= payload_size) {
            uint32_t curr_size = get_block_size(curr);
            if (curr_size >= payload_size + BLOCK_HEADER_SIZE + 8) {
                byte *new_block = curr + total_size;
                set_block_header(new_block, 0, curr_size - total_size);
                set_block_header(curr, 1, payload_size);
            } else {
                set_block_header(curr, 1, payload_size);
            }
            return curr + BLOCK_HEADER_SIZE;
        }
        curr += BLOCK_HEADER_SIZE + get_block_size(curr);
    }

    if (current_packet_ptr + total_size > packet_arena_size) {
        printf("arena_alloc: out of space for %u bytes\n", total_size);
        return NULL;
    }
    byte *block = packet_arena + current_packet_ptr;
    set_block_header(block, 1, payload_size);
    current_packet_ptr += total_size;
    return block + BLOCK_HEADER_SIZE;
}

void arena_free(byte *ptr) {
    if (!ptr || ptr < packet_arena || ptr >= packet_arena + packet_arena_size) {
        printf("arena_free: invalid pointer\n");
        return;
    }

    byte *block = ptr - BLOCK_HEADER_SIZE;
    if (!get_block_full(block)) {
        printf("arena_free: double free\n");
        return;
    }

    set_block_header(block, 0, get_block_size(block));

    byte *next = block + BLOCK_HEADER_SIZE + get_block_size(block);
    if (next < packet_arena + current_packet_ptr && !get_block_full(next)) {
        uint32_t new_size = get_block_size(block) + BLOCK_HEADER_SIZE + get_block_size(next);
        set_block_header(block, 0, new_size);
    }

    byte *curr = packet_arena;
    byte *prev = NULL;
    while (curr < block) {
        prev = curr;
        curr += BLOCK_HEADER_SIZE + get_block_size(curr);
    }
    if (prev && !get_block_full(prev)) {
        uint32_t new_size = get_block_size(prev) + BLOCK_HEADER_SIZE + get_block_size(block);
        set_block_header(prev, 0, new_size);
    }

    if (block + BLOCK_HEADER_SIZE + get_block_size(block) == packet_arena + current_packet_ptr) {
        current_packet_ptr = block - packet_arena;
    }
}

void save_author_packet_hash(const public_key author, const hash packet_hash) {
    const char *dir = "packets_by_author";
    mkdir(dir, 0755);

    char author_hex[65];
    sprintf(author_hex, hash_fmt, hash_ref(author));

    char filename[256];
    snprintf(filename, sizeof(filename), "%s/%s.list", dir, author_hex);

    FILE *f = fopen(filename, "rb");
    if (f) {
        unsigned char buf[64];
        while (fread(buf, 1, 64, f) >= 32) {
            int c = fgetc(f);
            (void)c;
            if (memcmp(buf, packet_hash, 32) == 0) {
                fclose(f);
                return;
            }
        }
        fclose(f);
    }

    FILE *fa = fopen(filename, "ab");
    if (!fa) return;
    fwrite(packet_hash, 32, 1, fa);
    fputc('\n', fa);
    fclose(fa);
}

void save_recipient_packet_hash(const recipient recipient, const hash packet_hash) {
    const char *dir = "packets_by_recipient";
    mkdir(dir, 0755);

    char recipient_hex[ 129 ];
    sprintf(recipient_hex, hash_fmt hash_fmt, hash_ref(recipient), hash_ref((recipient + 32)));

    char filename[ 256 ];
    snprintf(filename, sizeof(filename), "%s/%s.list", dir, recipient_hex);

    FILE *f = fopen(filename, "ab");
    if (!f) return;
    fwrite(packet_hash, 32, 1, f);
    fputc('\n', f);
    fclose(f);
}

uint64_t get_author_entry(const public_key auth_key, bool create) {
    uint64_t hash_val;
    memcpy(&hash_val, auth_key, sizeof(uint64_t));
    uint64_t start_index = hash_val % max_authors;
    uint64_t cur_index   = start_index;

    pthread_mutex_lock(&author_storage_mutex);

    struct author_entry *current = &author_storage[ cur_index ];
    while (true) {
        if (current->is_full && memcmp(current->author, auth_key, 32) == 0) {
            pthread_mutex_unlock(&author_storage_mutex);
            return cur_index;
        }
        if (current->next == invalid_index) break;
        cur_index = current->next;
        current   = &author_storage[ cur_index ];
    }

    if (!create) {
        pthread_mutex_unlock(&author_storage_mutex);
        return invalid_index;
    }

    if (!current->is_full) {
        memcpy(current->author, auth_key, 32);
        current->packet_head = invalid_index;
        current->is_full     = true;
        current->next        = invalid_index;
        current->prev        = invalid_index;
        pthread_mutex_unlock(&author_storage_mutex);
        return cur_index;
    }

    uint64_t probes = 0;
    uint64_t lp_idx = (cur_index + 1) % max_authors;
    while (probes < max_authors) {
        struct author_entry *empty = &author_storage[ lp_idx ];
        if (!empty->is_full) {
            memcpy(empty->author, auth_key, 32);
            empty->packet_head = invalid_index;
            empty->is_full     = true;
            empty->next        = invalid_index;
            empty->prev        = cur_index;
            current->next      = lp_idx;
            pthread_mutex_unlock(&author_storage_mutex);
            return lp_idx;
        }
        lp_idx = (lp_idx + 1) % max_authors;
        probes++;
    }

    pthread_mutex_unlock(&author_storage_mutex);
    return invalid_index;
}

uint64_t get_recipient_entry(const recipient recip_key, bool create) {
    uint64_t hash_val;
    memcpy(&hash_val, recip_key, sizeof(uint64_t));
    uint64_t start_index = hash_val % max_recipients;
    uint64_t cur_index   = start_index;

    pthread_mutex_lock(&recipient_storage_mutex);

    struct recipient_entry *current = &recipient_storage[ cur_index ];
    while (true) {
        if (current->is_full && memcmp(current->recipient, recip_key, 64) == 0) {
            pthread_mutex_unlock(&recipient_storage_mutex);
            return cur_index;
        }
        if (current->next == invalid_index) break;
        cur_index = current->next;
        current   = &recipient_storage[ cur_index ];
    }

    if (!create) {
        pthread_mutex_unlock(&recipient_storage_mutex);
        return invalid_index;
    }

    if (!current->is_full) {
        memcpy(current->recipient, recip_key, 64);
        current->packet_head = invalid_index;
        current->is_full     = true;
        current->next        = invalid_index;
        current->prev        = invalid_index;
        pthread_mutex_unlock(&recipient_storage_mutex);
        return cur_index;
    }

    uint64_t probes = 0;
    uint64_t lp_idx = (cur_index + 1) % max_recipients;
    while (probes < max_recipients) {
        struct recipient_entry *empty = &recipient_storage[ lp_idx ];
        if (!empty->is_full) {
            memcpy(empty->recipient, recip_key, 64);
            empty->packet_head = invalid_index;
            empty->is_full     = true;
            empty->next        = invalid_index;
            empty->prev        = cur_index;
            current->next      = lp_idx;
            pthread_mutex_unlock(&recipient_storage_mutex);
            return lp_idx;
        }
        lp_idx = (lp_idx + 1) % max_recipients;
        probes++;
    }

    pthread_mutex_unlock(&recipient_storage_mutex);
    return invalid_index;
}

struct data_packet *load_packet(FILE *file) {
    if (!file) return NULL;

    fseek(file, 0, SEEK_END);
    size_t file_size = ftell(file);
    fseek(file, 0, SEEK_SET);

    if (file_size == 0) {
        fclose(file);
        return NULL;
    }

    byte comp_buf[ file_size ];

    size_t r = fread(comp_buf, 1, file_size, file);
    fclose(file);
    if (r != file_size) return NULL;

    byte buffer[ PACKET_SIZE ];

    uLongf dest_len = (uLongf) PACKET_SIZE;
    int    unres    = uncompress(buffer, &dest_len, comp_buf, (uLongf) file_size);

    if (unres != Z_OK || dest_len != (uLongf) PACKET_SIZE) return NULL;

    bool                success;
    struct data_packet *packet = malloc(sizeof(struct data_packet));
    if (!packet) return NULL;
    *packet = s2data_packet(PACKET_SIZE, buffer, &success);

    if (!success) {
        free(packet);
        return NULL;
    }

    return packet;
}

uint64_t set_data_packet(struct data_packet *new_packet) {
    byte temp_buffer[ sizeof(struct data_packet) ];
    memcpy(temp_buffer, new_packet, sizeof(struct data_packet));
    uLongf compressed_len = compressBound(sizeof(struct data_packet));
    byte   compressed[ sizeof(struct data_packet) ];
    if (compress(compressed, &compressed_len, temp_buffer, sizeof(struct data_packet)) != Z_OK) {
        fprintf(stderr, "set_data_packet: compression failed\n");
        return invalid_index;
    }

    byte *compressed_payload = arena_alloc(compressed_len);
    if (!compressed_payload) { return invalid_index; }
    memcpy(compressed_payload, compressed, compressed_len);

    pthread_mutex_lock(&packet_storage_mutex);
    uint64_t hash_val;
    memcpy(&hash_val, new_packet->hash, sizeof(uint64_t));
    uint64_t                  start_index   = hash_val % max_storage;
    uint64_t                  cur_index     = start_index;
    struct data_packet_entry *current_entry = &packet_index[ cur_index ];

    while (true) {
        if (current_entry->is_full && current_entry->compressed_size == compressed_len
            && memcmp(current_entry->compressed_payload, compressed_payload, compressed_len) == 0) {
            arena_free(current_entry->compressed_payload);
            current_entry->compressed_payload = compressed_payload;
            current_entry->compressed_size    = compressed_len;
            pthread_mutex_unlock(&packet_storage_mutex);
            save_author_packet_hash(new_packet->contents.author, new_packet->hash);
            for (uint8_t i = 0; i < new_packet->contents.recipient_count; i++) {
                save_recipient_packet_hash(new_packet->contents.recipients[ i ], new_packet->hash);
            }
            return cur_index;
        }

        if (current_entry->next == invalid_index) break;
        cur_index     = current_entry->next;
        current_entry = &packet_index[ cur_index ];
    }

    if (!current_entry->is_full) {
        current_entry->compressed_payload = compressed_payload;
        current_entry->compressed_size    = compressed_len;
        current_entry->is_full            = true;
        current_entry->next               = invalid_index;
        current_entry->prev               = invalid_index;
        current_entry->author_next        = invalid_index;
        current_entry->author_prev        = invalid_index;
        current_entry->recipient_next     = invalid_index;
        current_entry->recipient_prev     = invalid_index;
        packet_count++;
    } else {
        uint64_t probes             = 0;
        uint64_t linear_probe_index = (cur_index + 1) % max_storage;
        while (probes < max_storage) {
            struct data_packet_entry *empty_slot = &packet_index[ linear_probe_index ];
            if (!empty_slot->is_full) {
                empty_slot->compressed_payload = compressed_payload;
                empty_slot->compressed_size    = compressed_len;
                empty_slot->is_full            = true;
                empty_slot->next               = invalid_index;
                empty_slot->prev               = cur_index;
                empty_slot->author_next        = invalid_index;
                empty_slot->author_prev        = invalid_index;
                empty_slot->recipient_next     = invalid_index;
                empty_slot->recipient_prev     = invalid_index;
                current_entry->next            = linear_probe_index;
                packet_count++;
                cur_index = linear_probe_index;
                break;
            }
            linear_probe_index = (linear_probe_index + 1) % max_storage;
            probes++;
        }
        if (probes >= max_storage) {
            pthread_mutex_unlock(&packet_storage_mutex);
            arena_free(compressed_payload);
            return invalid_index;
        }
    }

    uint64_t auth_idx = get_author_entry(new_packet->contents.author, true);
    if (auth_idx != invalid_index) {
        current_entry->author_next = author_storage[ auth_idx ].packet_head;
        if (current_entry->author_next != invalid_index) {
            packet_index[ current_entry->author_next ].author_prev = cur_index;
        }
        author_storage[ auth_idx ].packet_head = cur_index;
    }

    for (uint8_t i = 0; i < new_packet->contents.recipient_count; i++) {
        uint64_t recip_idx = get_recipient_entry(new_packet->contents.recipients[ i ], true);
        if (recip_idx != invalid_index) {
            current_entry->recipient_next = recipient_storage[ recip_idx ].packet_head;
            if (current_entry->recipient_next != invalid_index) {
                packet_index[ current_entry->recipient_next ].recipient_prev = cur_index;
            }
            recipient_storage[ recip_idx ].packet_head = cur_index;
            save_recipient_packet_hash(new_packet->contents.recipients[ i ], new_packet->hash);
        }
    }

    pthread_mutex_unlock(&packet_storage_mutex);
    save_author_packet_hash(new_packet->contents.author, new_packet->hash);
    return cur_index;
}

struct data_packet_entry *get_data_packet(const hash _hash) {
    uint64_t hash_val;
    memcpy(&hash_val, _hash, sizeof(uint64_t));
    uint64_t cur_index = hash_val % max_storage;

    pthread_mutex_lock(&packet_storage_mutex);
    struct data_packet_entry *current_entry = &packet_index[ cur_index ];
    while (cur_index != invalid_index) {
        if (current_entry->is_full
            && memcmp(
                   ((struct data_packet *) current_entry->compressed_payload)->hash,
                   _hash,
                   sizeof(hash))
                   == 0) {
            byte   decompressed[ sizeof(struct data_packet) ];
            uLongf decompressed_len = sizeof(struct data_packet);
            if (uncompress(
                    decompressed,
                    &decompressed_len,
                    current_entry->compressed_payload,
                    current_entry->compressed_size)
                != Z_OK) {
                printf("get_data_packet: decompression failed\n");
                pthread_mutex_unlock(&packet_storage_mutex);
                return NULL;
            }
            static __thread struct data_packet_entry temp_entry;
            temp_entry                    = *current_entry;
            temp_entry.compressed_payload = arena_alloc(decompressed_len);
            if (!temp_entry.compressed_payload) {
                pthread_mutex_unlock(&packet_storage_mutex);
                return NULL;
            }
            memcpy(temp_entry.compressed_payload, decompressed, decompressed_len);
            temp_entry.compressed_size = decompressed_len;
            pthread_mutex_unlock(&packet_storage_mutex);
            return &temp_entry;
        }
        cur_index = current_entry->next;
        if (cur_index != invalid_index) current_entry = &packet_index[ cur_index ];
    }
    pthread_mutex_unlock(&packet_storage_mutex);

    char filename[ 128 ];
    sprintf(filename, "packets/" hash_fmt ".bin", hash_ref(_hash));
    FILE               *file   = fopen(filename, "rb");
    struct data_packet *packet = load_packet(file);
    if (packet) {
        uint64_t add_idx = set_data_packet(packet);
        if (add_idx != invalid_index) {
            free(packet);
            return &packet_index[ add_idx ];
        } else {
            static __thread struct data_packet_entry temp_entry;
            byte                                     compressed[ sizeof(struct data_packet) ];
            uLongf                                   compressed_len = sizeof(struct data_packet);
            if (compress(compressed, &compressed_len, (byte *) packet, sizeof(struct data_packet))
                != Z_OK) {
                free(packet);
                return NULL;
            }
            temp_entry.compressed_payload = arena_alloc(compressed_len);
            if (!temp_entry.compressed_payload) {
                free(packet);
                return NULL;
            }
            memcpy(temp_entry.compressed_payload, compressed, compressed_len);
            temp_entry.compressed_size = compressed_len;
            temp_entry.is_full         = true;
            temp_entry.next            = invalid_index;
            temp_entry.prev            = invalid_index;
            temp_entry.author_next     = invalid_index;
            temp_entry.author_prev     = invalid_index;
            temp_entry.recipient_next  = invalid_index;
            temp_entry.recipient_prev  = invalid_index;
            free(packet);
            return &temp_entry;
        }
    }
    return NULL;
}

#define overload __attribute__((overloadable))

bool _delete_at_index(uint64_t index);

overload void delete_data_packet(hash _hash) {
    pthread_mutex_lock(&packet_storage_mutex);

    uint64_t hash_val;
    memcpy(&hash_val, _hash, sizeof(uint64_t));
    uint64_t start_index = hash_val % max_storage;
    uint64_t cur_index   = start_index;

    while (cur_index != invalid_index) {
        struct data_packet_entry *current_entry = &packet_index[ cur_index ];
        if (current_entry->is_full
            && memcmp(
                   ((struct data_packet *) current_entry->compressed_payload)->hash,
                   _hash,
                   sizeof(hash))
                   == 0) {
            _delete_at_index(cur_index);
            pthread_mutex_unlock(&packet_storage_mutex);
            return;
        }
        if (current_entry->next == invalid_index) {
            pthread_mutex_unlock(&packet_storage_mutex);
            return;
        }
        cur_index = current_entry->next;
    }

    pthread_mutex_unlock(&packet_storage_mutex);
}

overload void delete_data_packet(uint64_t index) {
    pthread_mutex_lock(&packet_storage_mutex);
    _delete_at_index(index);
    pthread_mutex_unlock(&packet_storage_mutex);
}

bool _delete_at_index(uint64_t index) {
    if (index >= max_storage) return false;
    struct data_packet_entry *current_entry = &packet_index[ index ];
    if (!current_entry->is_full) return false;

    public_key auth_key;
    memcpy(
        auth_key, ((struct data_packet *) current_entry->compressed_payload)->contents.author, 32);
    uint64_t auth_idx = get_author_entry(auth_key, false);
    if (auth_idx != invalid_index) {
        if (author_storage[ auth_idx ].packet_head == index) {
            author_storage[ auth_idx ].packet_head = current_entry->author_next;
        }
    }
    uint64_t a_prev = current_entry->author_prev;
    uint64_t a_next = current_entry->author_next;
    if (a_prev != invalid_index) packet_index[ a_prev ].author_next = a_next;
    if (a_next != invalid_index) packet_index[ a_next ].author_prev = a_prev;

    for (uint8_t i = 0;
         i < ((struct data_packet *) current_entry->compressed_payload)->contents.recipient_count;
         i++) {
        recipient recip_key;
        memcpy(
            recip_key,
            ((struct data_packet *) current_entry->compressed_payload)->contents.recipients[ i ],
            64);
        uint64_t recip_idx = get_recipient_entry(recip_key, false);
        if (recip_idx != invalid_index) {
            if (recipient_storage[ recip_idx ].packet_head == index) {
                recipient_storage[ recip_idx ].packet_head = current_entry->recipient_next;
            }
        }
        uint64_t r_prev = current_entry->recipient_prev;
        uint64_t r_next = current_entry->recipient_next;
        if (r_prev != invalid_index) packet_index[ r_prev ].recipient_next = r_next;
        if (r_next != invalid_index) packet_index[ r_next ].recipient_prev = r_prev;
    }

    if (current_entry->prev != invalid_index) {
        packet_index[ current_entry->prev ].next = current_entry->next;
    }
    if (current_entry->next != invalid_index) {
        packet_index[ current_entry->next ].prev = current_entry->prev;
    }

    arena_free(current_entry->compressed_payload);
    current_entry->is_full            = false;
    current_entry->next               = invalid_index;
    current_entry->prev               = invalid_index;
    current_entry->compressed_payload = NULL;
    current_entry->compressed_size    = 0;
    current_entry->author_next        = invalid_index;
    current_entry->author_prev        = invalid_index;
    current_entry->recipient_next     = invalid_index;
    current_entry->recipient_prev     = invalid_index;
    return true;
}

int get_packets_for_author__cmp(const void *a, const void *b) {
    uint64_t ta = ((struct data_packet *) a)->contents.created_time_ms;
    uint64_t tb = ((struct data_packet *) b)->contents.created_time_ms;
    return (ta > tb) ? -1 : (ta < tb) ? 1 : 0;
}

uint16_t get_packets_for_author(
    const public_key    author,
    uint64_t            offset,
    uint16_t            max_count,
    struct data_packet *out_packets,
    uint64_t           *out_new_offset) {
    uint64_t auth_idx = get_author_entry(author, false);
    if (auth_idx == invalid_index) return 0;

    uint64_t fetch_factor = ((offset + (uint64_t) max_count) * 5LLU) / 2;

    pthread_mutex_lock(&packet_storage_mutex);
    uint64_t curr      = author_storage[ auth_idx ].packet_head;
    uint16_t mem_count = 0;
    while (curr != invalid_index) {
        mem_count++;
        curr = packet_index[ curr ].author_next;
    }

    struct data_packet *all_packets
        = malloc((mem_count + fetch_factor) * sizeof(struct data_packet));
    if (!all_packets) {
        pthread_mutex_unlock(&packet_storage_mutex);
        return 0;
    }
    size_t all_count = 0;

    curr = author_storage[ auth_idx ].packet_head;
    while (curr != invalid_index) {
        byte   decompressed[ sizeof(struct data_packet) ];
        uLongf decompressed_len = sizeof(struct data_packet);
        if (uncompress(
                decompressed,
                &decompressed_len,
                packet_index[ curr ].compressed_payload,
                packet_index[ curr ].compressed_size)
            != Z_OK) {
            printf("get_packets_for_author: decompression failed\n");
            continue;
        }
        memcpy(&all_packets[ all_count++ ], decompressed, sizeof(struct data_packet));
        curr = packet_index[ curr ].author_next;
    }
    pthread_mutex_unlock(&packet_storage_mutex);

    char author_hex[ 65 ];
    sprintf(author_hex, hash_fmt, hash_ref(author));

    char filename[ 256 ];
    snprintf(filename, sizeof(filename), "packets_by_author/%s.list", author_hex);

    FILE  *f          = fopen(filename, "rb");
    size_t disk_added = 0;
    if (f) {
        fseek(f, 0, SEEK_END);
        long fsize = ftell(f);
        fseek(f, 0, SEEK_SET);
        uint64_t total = fsize / 33;

        uint64_t fetch_count = fetch_factor;
        if (fetch_count > total) fetch_count = total;

        for (uint64_t i = 0; i < fetch_count; i++) {
            long pos = (total - 1 - i) * 33;
            fseek(f, pos, SEEK_SET);

            hash phash;
            if (fread(phash, 32, 1, f) != 1) break;
            if (fgetc(f) != '\n') break;

            bool dupe = false;
            for (size_t j = 0; j < all_count; j++) {
                if (memcmp(all_packets[ j ].hash, phash, 32) == 0) {
                    dupe = true;
                    break;
                }
            }
            if (dupe) continue;

            struct data_packet_entry *ent = get_data_packet(phash);
            if (!ent) continue;

            byte   decompressed[ sizeof(struct data_packet) ];
            uLongf decompressed_len = sizeof(struct data_packet);
            if (uncompress(
                    decompressed, &decompressed_len, ent->compressed_payload, ent->compressed_size)
                != Z_OK) {
                printf("get_packets_for_author: decompression failed for disk packet\n");
                continue;
            }
            memcpy(&all_packets[ all_count++ ], decompressed, sizeof(struct data_packet));
            disk_added++;
        }
        fclose(f);
    }

    qsort(all_packets, all_count, sizeof(struct data_packet), get_packets_for_author__cmp);

    if (offset >= all_count) {
        free(all_packets);
        *out_new_offset = offset;
        return 0;
    }

    size_t   avail     = all_count - offset;
    uint16_t collected = (avail > max_count) ? max_count : avail;
    memcpy(out_packets, all_packets + offset, collected * sizeof(struct data_packet));
    *out_new_offset = offset + collected;

    free(all_packets);
    return collected;
}

uint16_t get_packets_for_recipient(
    const recipient     recipient,
    uint64_t            offset,
    uint16_t            max_count,
    struct data_packet *out_packets,
    uint64_t           *out_new_offset) {
    uint64_t recip_idx = get_recipient_entry(recipient, false);
    if (recip_idx == invalid_index) return 0;

    uint64_t fetch_factor = ((offset + (uint64_t) max_count) * 5LLU) / 2;

    pthread_mutex_lock(&packet_storage_mutex);
    uint64_t curr      = recipient_storage[ recip_idx ].packet_head;
    uint16_t mem_count = 0;
    while (curr != invalid_index) {
        mem_count++;
        curr = packet_index[ curr ].recipient_next;
    }

    struct data_packet *all_packets
        = malloc((mem_count + fetch_factor) * sizeof(struct data_packet));
    if (!all_packets) {
        pthread_mutex_unlock(&packet_storage_mutex);
        return 0;
    }
    size_t all_count = 0;

    curr = recipient_storage[ recip_idx ].packet_head;
    while (curr != invalid_index) {
        byte   decompressed[ sizeof(struct data_packet) ];
        uLongf decompressed_len = sizeof(struct data_packet);
        if (uncompress(
                decompressed,
                &decompressed_len,
                packet_index[ curr ].compressed_payload,
                packet_index[ curr ].compressed_size)
            != Z_OK) {
            printf("get_packets_for_recipient: decompression failed\n");
            continue;
        }
        memcpy(&all_packets[ all_count++ ], decompressed, sizeof(struct data_packet));
        curr = packet_index[ curr ].recipient_next;
    }
    pthread_mutex_unlock(&packet_storage_mutex);

    char recipient_hex[ 129 ];
    sprintf(recipient_hex, hash_fmt hash_fmt, hash_ref(recipient), hash_ref((recipient + 32)));

    char filename[ 256 ];
    snprintf(filename, sizeof(filename), "packets_by_recipient/%s.list", recipient_hex);

    FILE  *f          = fopen(filename, "rb");
    size_t disk_added = 0;
    if (f) {
        fseek(f, 0, SEEK_END);
        long fsize = ftell(f);
        fseek(f, 0, SEEK_SET);
        uint64_t total = fsize / 33;

        uint64_t fetch_count = fetch_factor;
        if (fetch_count > total) fetch_count = total;

        for (uint64_t i = 0; i < fetch_count; i++) {
            long pos = (total - 1 - i) * 33;
            fseek(f, pos, SEEK_SET);

            hash phash;
            if (fread(phash, 32, 1, f) != 1) break;
            if (fgetc(f) != '\n') break;

            bool dupe = false;
            for (size_t j = 0; j < all_count; j++) {
                if (memcmp(all_packets[ j ].hash, phash, 32) == 0) {
                    dupe = true;
                    break;
                }
            }
            if (dupe) continue;

            struct data_packet_entry *ent = get_data_packet(phash);
            if (!ent) continue;

            byte   decompressed[ sizeof(struct data_packet) ];
            uLongf decompressed_len = sizeof(struct data_packet);
            if (uncompress(
                    decompressed, &decompressed_len, ent->compressed_payload, ent->compressed_size)
                != Z_OK) {
                printf("get_packets_for_recipient: decompression failed for disk packet\n");
                continue;
            }
            memcpy(&all_packets[ all_count++ ], decompressed, sizeof(struct data_packet));
            disk_added++;
        }
        fclose(f);
    }

    qsort(all_packets, all_count, sizeof(struct data_packet), get_packets_for_author__cmp);

    if (offset >= all_count) {
        free(all_packets);
        *out_new_offset = offset;
        return 0;
    }

    size_t   avail     = all_count - offset;
    uint16_t collected = (avail > max_count) ? max_count : avail;
    memcpy(out_packets, all_packets + offset, collected * sizeof(struct data_packet));
    *out_new_offset = offset + collected;

    free(all_packets);
    return collected;
}