bincookie
Loading...
Searching...
No Matches
bincookie.h
Go to the documentation of this file.
1
8#ifndef _BINCOOKIE_H
9#define _BINCOOKIE_H
10
11#ifdef __cplusplus
12extern "C" {
13#endif
14
16// https://github.com/nodejs/http-parser/blob/master/http_parser.h#L32
17#include <sys/types.h>
18#if defined(_WIN32) && !defined(__MINGW32__) && (!defined(_MSC_VER) || _MSC_VER < 1600) && \
19 !defined(__WINE__)
20#include <BaseTsd.h>
21#include <stddef.h>
22typedef unsigned __int32 uint32_t;
23typedef unsigned __int8 bool;
24#else
25#include <stdbool.h>
26#include <stdint.h>
27#endif
28#include <errno.h>
29#include <stdio.h>
30#include <stdlib.h>
31#include <string.h>
32#include <time.h>
34
37
40#define bincookie_is_secure(cookie_ptr) (cookie_ptr->flags & secure)
42
45#define bincookie_domain_access_full(cookie_ptr) \
46 (((char *)cookie_ptr + cookie_ptr->domain_offset)[0] == '.')
48
51#define bincookie_iter_state_init(s) \
52 s.page_offset = 0; \
53 s.page_index = 0;
55
58#define bincookie_domain(c) ((char *)c + c->domain_offset)
60
63#define bincookie_path(c) \
64 ((char *)c + \
65 c->path_offset)
68
71#define bincookie_name(c) \
72 ((char *)c + \
73 c->name_offset)
76
79#define bincookie_value(c) ((char *)c + c->value_offset)
81#define APPLE_EPOCH_OFFSET 978307200
83
86#define bincookie_expiration_time(c) \
87 (c->expiry_date_epoch + APPLE_EPOCH_OFFSET)
89
92#define bincookie_creation_time(c) \
93 (c->create_date_epoch + APPLE_EPOCH_OFFSET)
96
98typedef enum {
99 secure = 1,
100 http_only = 1 << 2,
104typedef struct {
105 uint32_t page_offset;
106 uint32_t page_index;
110typedef struct {
111 uint32_t size;
112 unsigned char unk1[4];
114 unsigned char unk2[4];
116 uint32_t domain_offset;
117 uint32_t name_offset;
118 uint32_t path_offset;
119 uint32_t value_offset;
121 unsigned char unk[8];
123 double expiry_date_epoch;
124 double create_date_epoch;
128typedef struct {
129 unsigned char unk1[4];
131 uint32_t num_cookies;
132 uint32_t cookie_offsets[];
134
136typedef struct {
137 unsigned char magic[4];
138 uint32_t num_pages;
139 uint32_t page_sizes[];
143
147static inline bincookie_t *const bincookie_init_file(FILE *fin) {
148 const long last_pos = ftell(fin);
149 rewind(fin);
150 char magic[4];
151 bincookie_t *cook = NULL;
152 size_t read = fread(magic, sizeof(magic), 1, fin);
153 bool is_cook = magic[0] == 'c' && magic[1] == 'o' && magic[2] == 'o' && magic[3] == 'k';
154 if (read != 1 || !is_cook) {
155 errno = EIO;
156 goto done;
157 }
158 // Read entire file
159 fseek(fin, 0, SEEK_END);
160 size_t num_bytes = (size_t)ftell(fin);
161 cook = (bincookie_t *)malloc(num_bytes);
162 // LCOV_EXCL_START
163 if (cook == NULL) {
164 goto done;
165 } // LCOV_EXCL_STOP
166 memset(cook, 0, num_bytes);
167 rewind(fin);
168 // LCOV_EXCL_START
169 if (fread(cook, num_bytes, 1, fin) != 1) {
170 free(cook);
171 cook = NULL;
172 goto done;
173 } // LCOV_EXCL_STOP
174 cook->num_pages = __builtin_bswap32(cook->num_pages);
175 // Fix page size numbers
176 for (uint32_t i = 0; i < cook->num_pages; i++) {
177 *(cook->page_sizes + i) = __builtin_bswap32(*(cook->page_sizes + i));
178 }
179done:
180 fseek(fin, last_pos, SEEK_SET);
181 return cook;
182}
184
188static inline bincookie_t *const bincookie_init_path(const char *file_path) {
189 FILE *binary_file = fopen(file_path, "rb");
190 if (!binary_file) {
191 return NULL;
192 }
193 bincookie_t *ret = bincookie_init_file(binary_file);
194 fclose(binary_file);
195 return ret;
196}
198
203static inline bincookie_page_t *const bincookie_iter_pages(const bincookie_t *bc,
204 bincookie_iter_state_t *const state) {
205 if (state->page_offset == 0) {
206 state->page_offset = (uint32_t)(4 + sizeof(uint32_t) + (sizeof(uint32_t) * bc->num_pages));
207 }
208 if (state->page_index < bc->num_pages) {
209 bincookie_page_t *page = (bincookie_page_t *)((char *)bc + state->page_offset);
210 state->page_offset += bc->page_sizes[state->page_index];
211 state->page_index++;
212 return page;
213 }
214 return NULL;
215}
217
222static inline bincookie_cookie_t *const bincookie_iter_cookies(const bincookie_page_t *page,
223 unsigned int *i) {
224 if (*i < page->num_cookies) {
225 bincookie_cookie_t *ptr = (bincookie_cookie_t *)((char *)page + page->cookie_offsets[*i]);
226 (*i)++;
227 return ptr;
228 }
229 return NULL;
230}
231
232#ifdef __cplusplus
233}
234#endif
235
236#endif // _BINCOOKIE_H
bincookie_flag
Security enabled for a cookie.
Definition bincookie.h:107
@ http_only
Definition bincookie.h:109
@ secure
Definition bincookie.h:108
Keeps track of iteration state when iterating cookie file pages.
Definition bincookie.h:113
uint32_t page_offset
Definition bincookie.h:114
uint32_t page_index
Definition bincookie.h:115
Cookie page structure. A page can consist of 1 or more cookies.
Definition bincookie.h:137
uint32_t cookie_offsets[]
Definition bincookie.h:141
bincookie file structure.
Definition bincookie.h:145
uint32_t num_pages
Definition bincookie.h:147
uint32_t page_sizes[]
Definition bincookie.h:148