1 /*
2  * filedb3.h -- part of filesys.mod
3  *   filedb header file
4  *
5  * Written by Fabian Knittel <fknittel@gmx.de>
6  */
7 /*
8  * Copyright (C) 1999 - 2021 Eggheads Development Team
9  *
10  * This program is free software; you can redistribute it and/or
11  * modify it under the terms of the GNU General Public License
12  * as published by the Free Software Foundation; either version 2
13  * of the License, or (at your option) any later version.
14  *
15  * This program is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU General Public License for more details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with this program; if not, write to the Free Software
22  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
23  */
24 
25 #ifndef _EGG_MOD_FILESYS_FILEDB3_H
26 #define _EGG_MOD_FILESYS_FILEDB3_H
27 
28 /* Top of each DB */
29 typedef struct {
30   char version;                 /* DB version                   */
31   time_t timestamp;             /* Last updated                 */
32 } filedb_top;
33 
34 /* Header of each entry */
35 typedef struct {
36   time_t uploaded;              /* Upload time                  */
37   unsigned int size;            /* File size                    */
38   unsigned short int stat;      /* Misc information             */
39   unsigned short int gots;      /* Number of gets               */
40   unsigned short int filename_len;      /* Length of filename buf       */
41   unsigned short int desc_len;  /* Length of description buf    */
42   unsigned short int sharelink_len;     /* Length of sharelink buf      */
43   unsigned short int chan_len;  /* Length of channel name buf   */
44   unsigned short int uploader_len;      /* Length of uploader buf       */
45   unsigned short int flags_req_len;     /* Length of flags buf          */
46   unsigned short int buffer_len;        /* Length of additional buffer  */
47 } filedb_header;
48 
49 /* Structure used to pass data between lower level
50  * and higher level functions.
51  */
52 typedef struct {
53   time_t uploaded;              /* Upload time                  */
54   unsigned int size;            /* File size                    */
55   unsigned short int stat;      /* Misc information             */
56   unsigned short int gots;      /* Number of gets               */
57   unsigned short int _type;     /* Type of entry (private)      */
58 
59   /* NOTE: These three are only valid during one db open/close. Entry
60    *       movements often invalidate them too, so make sure you know
61    *       what you're doing before using/relying on them.
62    */
63   unsigned long pos;            /* Last position in the filedb  */
64   unsigned short int dyn_len;   /* Length of dynamic data in DB */
65   unsigned short int buf_len;   /* Length of additional buffer  */
66 
67   char *filename;               /* Filename                     */
68   char *desc;                   /* Description                  */
69   char *sharelink;              /* Share link. Points to remote
70                                  * file on linked bot.          */
71   char *chan;                   /* Channel name                 */
72   char *uploader;               /* Uploader                     */
73   char *flags_req;              /* Required flags               */
74 } filedb_entry;
75 
76 
77 /*
78  *   Macros
79  */
80 
81 #define my_free(ptr)                                                    \
82   if (ptr) {                                                            \
83     nfree(ptr);                                                         \
84     ptr = NULL;                                                         \
85   }
86 
87 /* Copy entry to target -- Uses dynamic memory allocation, which
88  * means you'll eventually have to free the memory again. 'target'
89  * will be overwritten.
90  */
91 #define malloc_strcpy(target, entry)                                    \
92 do {                                                                    \
93   if (entry) {                                                          \
94     (target) = nrealloc((target), strlen(entry) + 1);                   \
95     strcpy((target), (entry));                                          \
96   } else                                                                \
97     my_free(target);                                                    \
98 } while (0)
99 
100 #define malloc_strcpy_nocheck(target, entry)                            \
101 do {                                                                    \
102   (target) = nrealloc((target), strlen(entry) + 1);                     \
103   strcpy((target), (entry));                                            \
104 } while (0)
105 
106 /* Macro to calculate the total length of dynamic data. */
107 #define filedb_tot_dynspace(fdh) ((fdh).filename_len + (fdh).desc_len + \
108         (fdh).chan_len + (fdh).uploader_len + (fdh).flags_req_len + \
109         (fdh).sharelink_len)
110 
111 #define filedb_zero_dynspace(fdh) {                                     \
112         (fdh).filename_len      = 0;                                    \
113         (fdh).desc_len          = 0;                                    \
114         (fdh).chan_len          = 0;                                    \
115         (fdh).uploader_len      = 0;                                    \
116         (fdh).flags_req_len     = 0;                                    \
117         (fdh).sharelink_len     = 0;                                    \
118 }
119 
120 /* Memory debugging macros */
121 #define malloc_fdbe() _malloc_fdbe(__FILE__, __LINE__)
122 #define filedb_getfile(fdb, pos, get) _filedb_getfile(fdb, pos, get, __FILE__, __LINE__)
123 #define filedb_matchfile(fdb, pos, match) _filedb_matchfile(fdb, pos, match, __FILE__, __LINE__)
124 #define filedb_updatefile(fdb, pos, fdbe, update) _filedb_updatefile(fdb, pos, fdbe, update, __FILE__, __LINE__)
125 #define filedb_addfile(fdb, fdbe) _filedb_addfile(fdb, fdbe, __FILE__, __LINE__)
126 #define filedb_movefile(fdb, pos, fdbe) _filedb_movefile(fdb, pos, fdbe, __FILE__, __LINE__)
127 
128 
129 /*
130  *  Constants
131  */
132 
133 #define FILEDB_VERSION1 0x0001
134 #define FILEDB_VERSION2 0x0002  /* DB version used for 1.3, 1.4         */
135 #define FILEDB_VERSION3 0x0003
136 #define FILEDB_NEWEST_VER FILEDB_VERSION3       /* Newest DB version    */
137 
138 #define POS_NEW         0       /* Position which indicates that the
139                                  * entry wants to be repositioned.      */
140 
141 #define FILE_UNUSED     0x0001  /* Deleted entry.                       */
142 #define FILE_DIR        0x0002  /* It's actually a directory.           */
143 #define FILE_SHARE      0x0004  /* Can be shared on the botnet.         */
144 #define FILE_HIDDEN     0x0008  /* Hidden file.                         */
145 #define FILE_ISLINK     0x0010  /* The file is a link to another bot.   */
146 
147 #define FILEDB_ESTDYN   50      /* Estimated dynamic length of an entry */
148 
149 enum {
150   GET_HEADER,                   /* Only save minimal data               */
151   GET_FILENAME,                 /* Additionally save filename           */
152   GET_FULL,                     /* Save all data                        */
153 
154   UPDATE_HEADER,                /* Only update header                   */
155   UPDATE_SIZE,                  /* Update header, enforce new buf sizes */
156   UPDATE_ALL,                   /* Update additional data too           */
157 
158   TYPE_NEW,                     /* New entry                            */
159   TYPE_EXIST                    /* Existing entry                       */
160 };
161 
162 
163 /*
164  *  filedb3.c prototypes
165  */
166 
167 static void free_fdbe(filedb_entry **);
168 static filedb_entry *_malloc_fdbe(char *, int);
169 static int filedb_readtop(FILE *, filedb_top *);
170 static int filedb_writetop(FILE *, filedb_top *);
171 static int filedb_delfile(FILE *, long);
172 static filedb_entry *filedb_findempty(FILE *, int);
173 static int _filedb_updatefile(FILE *, long, filedb_entry *, int, char *, int);
174 static int _filedb_movefile(FILE *, long, filedb_entry *, char *, int);
175 static int _filedb_addfile(FILE *, filedb_entry *, char *, int);
176 static filedb_entry *_filedb_getfile(FILE *, long, int, char *, int);
177 static filedb_entry *_filedb_matchfile(FILE *, long, char *, char *, int);
178 static filedb_entry *filedb_getentry(char *, char *);
179 
180 #endif /* _EGG_MOD_FILESYS_FILEDB3_H */
181