diff options
| author | Naram Qashat <cyberbotx@cyberbotx.com> | 2010-06-27 23:15:05 -0400 |
|---|---|---|
| committer | Naram Qashat <cyberbotx@cyberbotx.com> | 2010-06-27 23:15:05 -0400 |
| commit | 28e12bc24a9c85f4f0d1e37567618ec39cb501f6 (patch) | |
| tree | cc70ebeef95a9d95174afe3ef038b0d673346f58 /src/tools/db-convert.h | |
| parent | 051ebe3eea0f8529b64c0e443c61103ba2f7dee8 (diff) | |
The next of a few "CBX OCDing over code style" commits, maybe the last.
NOTES: I have been unable to compile the db_mysql_* functions on my system here, so those are untested. db-convert seems to be badly programmed and needs more work in my opinion.
Diffstat (limited to 'src/tools/db-convert.h')
| -rw-r--r-- | src/tools/db-convert.h | 871 |
1 files changed, 418 insertions, 453 deletions
diff --git a/src/tools/db-convert.h b/src/tools/db-convert.h index b3712eaf5..cf265c92f 100644 --- a/src/tools/db-convert.h +++ b/src/tools/db-convert.h @@ -14,12 +14,19 @@ * GNU General Public License for more details. */ -#include <stdlib.h> -#include <string.h> -#include <stdio.h> +#ifndef DB_CONVERT_H +#define DB_CONVERT_H + +#include <string> +#include <iostream> +#include <fstream> + +#include <cstdlib> +#include <cstring> +#include <cstdio> +#include <cctype> +#include <ctime> #include <fcntl.h> -#include <ctype.h> -#include <time.h> #ifndef _WIN32 #include <unistd.h> @@ -29,10 +36,6 @@ #endif #include "sysconf.h" -#include <string> -#include <iostream> -#include <fstream> - #ifndef _WIN32 #define C_LBLUE "\033[1;34m" #define C_NONE "\033[m" @@ -41,156 +44,166 @@ #define C_NONE "" #endif -#define getc_db(f) (fgetc((f)->fp)) -#define HASH(nick) ((tolower((nick)[0])&31)<<5 | (tolower((nick)[1])&31)) -#define HASH2(chan) ((chan)[1] ? ((chan)[1]&31)<<5 | ((chan)[2]&31) : 0) -#define read_buffer(buf,f) (read_db((f),(buf),sizeof(buf)) == sizeof(buf)) -#define write_buffer(buf,f) (write_db((f),(buf),sizeof(buf)) == sizeof(buf)) -#define read_db(f,buf,len) (fread((buf),1,(len),(f)->fp)) -#define write_db(f,buf,len) (fwrite((buf),1,(len),(f)->fp)) -#define read_int8(ret,f) ((*(ret)=fgetc((f)->fp))==EOF ? -1 : 0) -#define write_int8(val,f) (fputc((val),(f)->fp)==EOF ? -1 : 0) -#define SAFE(x) do { \ - if ((x) < 0) { \ - printf("Error, the database is broken, trying to continue... no guarantee.\n"); \ - } \ -} while (0) -#define READ(x) do { \ - if ((x) < 0) { \ +#define getc_db(f) (fgetc((f)->fp)) +#define HASH(nick) ((tolower((nick)[0]) & 31)<<5 | (tolower((nick)[1]) & 31)) +#define HASH2(chan) ((chan)[1] ? ((chan)[1] & 31)<<5 | ((chan)[2] & 31) : 0) +#define read_buffer(buf, f) (read_db((f), (buf), sizeof(buf)) == sizeof(buf)) +#define write_buffer(buf, f) (write_db((f), (buf), sizeof(buf)) == sizeof(buf)) +#define read_db(f, buf, len) (fread((buf), 1, (len), (f)->fp)) +#define write_db(f, buf, len) (fwrite((buf), 1, (len), (f)->fp)) +#define read_int8(ret, f) ((*(ret) = fgetc((f)->fp)) == EOF ? -1 : 0) +#define write_int8(val, f) (fputc((val), (f)->fp) == EOF ? -1 : 0) +#define SAFE(x) \ +if (true) \ +{ \ + if ((x) < 0) \ + printf("Error, the database is broken, trying to continue... no guarantee.\n"); \ +} \ +else \ + static_cast<void>(0) +#define READ(x) \ +if (true) \ +{ \ + if ((x) < 0) \ + { \ printf("Error, the database is broken, trying to continue... no guarantee.\n"); \ exit(0); \ } \ -} while (0) - -typedef struct memo_ Memo; -typedef struct dbFILE_ dbFILE; -typedef struct nickalias_ NickAlias; -typedef struct nickcore_ NickCore; -typedef struct chaninfo_ ChannelInfo; -typedef struct badword_ BadWord; -typedef struct hostcore_ HostCore; - -struct memo_ { - uint32 number; /* Index number -- not necessarily array position! */ - uint16 flags; /* Flags */ - time_t time; /* When was it sent? */ - char sender[32]; /* Name of the sender */ +} \ +else \ + static_cast<void>(0) + +struct Memo +{ + uint32 number; /* Index number -- not necessarily array position! */ + uint16 flags; /* Flags */ + time_t time; /* When was it sent? */ + char sender[32]; /* Name of the sender */ char *text; }; -struct dbFILE_ { - int mode; /* 'r' for reading, 'w' for writing */ - FILE *fp; /* The normal file descriptor */ - char filename[1024]; /* Name of the database file */ +struct dbFILE +{ + int mode; /* 'r' for reading, 'w' for writing */ + FILE *fp; /* The normal file descriptor */ + char filename[1024]; /* Name of the database file */ }; -typedef struct { - int16 memocount; /* Current # of memos */ - int16 memomax; /* Max # of memos one can hold*/ - Memo *memos; /* Pointer to original memos */ -} MemoInfo; +struct MemoInfo +{ + int16 memocount; /* Current # of memos */ + int16 memomax; /* Max # of memos one can hold*/ + Memo *memos; /* Pointer to original memos */ +}; -typedef struct { - uint16 in_use; /* 1 if this entry is in use, else 0 */ - int16 level; - NickCore *nc; /* Guaranteed to be non-NULL if in use, NULL if not */ - time_t last_seen; -} ChanAccess; +struct NickCore +{ + NickCore *next, *prev; -typedef struct { - int16 in_use; /* Always 0 if not in use */ - int16 is_nick; /* 1 if a regged nickname, 0 if a nick!user@host mask */ - uint16 flags; - union { - char *mask; /* Guaranteed to be non-NULL if in use, NULL if not */ - NickCore *nc; /* Same */ - } u; - char *reason; - char *creator; - time_t addtime; -} AutoKick; + char *display; /* How the nick is displayed */ + char pass[32]; /* Password of the nicks */ + char *email; /* E-mail associated to the nick */ + char *greet; /* Greet associated to the nick */ + uint32 icq; /* ICQ # associated to the nick */ + char *url; /* URL associated to the nick */ + uint32 flags; /* See NI_* below */ + uint16 language; /* Language selected by nickname owner (LANG_*) */ + uint16 accesscount; /* # of entries */ + char **access; /* Array of strings */ + MemoInfo memos; /* Memo information */ + uint16 channelcount; /* Number of channels currently registered */ + int unused; /* Used for nick collisions */ + int aliascount; /* How many aliases link to us? Remove the core if 0 */ +}; -struct nickalias_ { +struct NickAlias +{ NickAlias *next, *prev; - char *nick; /* Nickname */ - time_t time_registered; /* When the nick was registered */ - time_t last_seen; /* When it was seen online for the last time */ - uint16 status; /* See NS_* below */ - NickCore *nc; /* I'm an alias of this */ + char *nick; /* Nickname */ + time_t time_registered; /* When the nick was registered */ + time_t last_seen; /* When it was seen online for the last time */ + uint16 status; /* See NS_* below */ + NickCore *nc; /* I'm an alias of this */ char *last_usermask; char *last_realname; char *last_quit; }; -struct nickcore_ { - NickCore *next, *prev; +struct ChanAccess +{ + uint16 in_use; /* 1 if this entry is in use, else 0 */ + int16 level; + NickCore *nc; /* Guaranteed to be non-NULL if in use, NULL if not */ + time_t last_seen; +}; - char *display; /* How the nick is displayed */ - char pass[32]; /* Password of the nicks */ - char *email; /* E-mail associated to the nick */ - char *greet; /* Greet associated to the nick */ - uint32 icq; /* ICQ # associated to the nick */ - char *url; /* URL associated to the nick */ - uint32 flags; /* See NI_* below */ - uint16 language; /* Language selected by nickname owner (LANG_*) */ - uint16 accesscount; /* # of entries */ - char **access; /* Array of strings */ - MemoInfo memos; /* Memo information */ - uint16 channelcount; /* Number of channels currently registered */ - int unused; /* Used for nick collisions */ - int aliascount; /* How many aliases link to us? Remove the core if 0 */ +struct AutoKick +{ + int16 in_use; /* Always 0 if not in use */ + int16 is_nick; /* 1 if a regged nickname, 0 if a nick!user@host mask */ + uint16 flags; + union + { + char *mask; /* Guaranteed to be non-NULL if in use, NULL if not */ + NickCore *nc; /* Same */ + } u; + char *reason; + char *creator; + time_t addtime; +}; +struct BadWord +{ + uint16 in_use; + char *word; + uint16 type; }; -struct chaninfo_ { +struct ChannelInfo +{ ChannelInfo *next, *prev; - char name[64]; /* Channel name */ - char *founder; /* Who registered the channel */ - char *successor; /* Who gets the channel if the founder nick is dropped or expires */ + char name[64]; /* Channel name */ + char *founder; /* Who registered the channel */ + char *successor; /* Who gets the channel if the founder nick is dropped or expires */ char founderpass[32]; /* Channel password */ - char *desc; /* Description */ - char *url; /* URL */ - char *email; /* Email address */ - time_t time_registered; /* When was it registered */ - time_t last_used; /* When was it used hte last time */ - char *last_topic; /* Last topic on the channel */ - char last_topic_setter[32]; /* Who set the last topic */ - time_t last_topic_time; /* When the last topic was set */ - uint32 flags; /* Flags */ - char *forbidby; /* if forbidden: who did it */ + char *desc; /* Description */ + char *url; /* URL */ + char *email; /* Email address */ + time_t time_registered; /* When was it registered */ + time_t last_used; /* When was it used hte last time */ + char *last_topic; /* Last topic on the channel */ + char last_topic_setter[32]; /* Who set the last topic */ + time_t last_topic_time; /* When the last topic was set */ + uint32 flags; /* Flags */ + char *forbidby; /* if forbidden: who did it */ char *forbidreason; /* if forbidden: why */ - int16 bantype; /* Bantype */ - int16 *levels; /* Access levels for commands */ - uint16 accesscount; /* # of pple with access */ - ChanAccess *access; /* List of authorized users */ - uint16 akickcount; /* # of akicked pple */ - AutoKick *akick; /* List of users to kickban */ - uint32 mlock_on, mlock_off; /* See channel modes below */ + int16 bantype; /* Bantype */ + int16 *levels; /* Access levels for commands */ + uint16 accesscount; /* # of pple with access */ + ChanAccess *access; /* List of authorized users */ + uint16 akickcount; /* # of akicked pple */ + AutoKick *akick; /* List of users to kickban */ + uint32 mlock_on, mlock_off; /* See channel modes below */ uint32 mlock_limit; /* 0 if no limit */ - char *mlock_key; /* NULL if no key */ - char *mlock_flood; /* NULL if no +f */ + char *mlock_key; /* NULL if no key */ + char *mlock_flood; /* NULL if no +f */ char *mlock_redirect; /* NULL if no +L */ - char *entry_message; /* Notice sent on entering channel */ - MemoInfo memos; /* Memos */ - char *bi; /* Bot used on this channel */ - uint32 botflags; /* BS_* below */ - int16 *ttb; /* Times to ban for each kicker */ - uint16 bwcount; /* Badword count */ - BadWord *badwords; /* For BADWORDS kicker */ - int16 capsmin, capspercent; /* For CAPS kicker */ - int16 floodlines, floodsecs; /* For FLOOD kicker */ - int16 repeattimes; /* For REPEAT kicker */ -}; - -struct badword_ { - uint16 in_use; - char *word; - uint16 type; + char *entry_message; /* Notice sent on entering channel */ + MemoInfo memos; /* Memos */ + char *bi; /* Bot used on this channel */ + uint32 botflags; /* BS_* below */ + int16 *ttb; /* Times to ban for each kicker */ + uint16 bwcount; /* Badword count */ + BadWord *badwords; /* For BADWORDS kicker */ + int16 capsmin, capspercent; /* For CAPS kicker */ + int16 floodlines, floodsecs; /* For FLOOD kicker */ + int16 repeattimes; /* For REPEAT kicker */ }; -struct hostcore_ { +struct HostCore +{ HostCore *next; char *nick; char *vIdent; @@ -199,7 +212,6 @@ struct hostcore_ { int32 time; }; -dbFILE *open_db_write(const char *service, const char *filename, int version); dbFILE *open_db_read(const char *service, const char *filename, int version); NickCore *findcore(const char *nick, int version); NickAlias *findnick(const char *nick); @@ -230,94 +242,94 @@ HostCore *head = NULL; int b64_encode(char *src, size_t srclength, char *target, size_t targsize); /* Memo Flags */ -#define MF_UNREAD 0x0001 /* Memo has not yet been read */ -#define MF_RECEIPT 0x0002 /* Sender requested receipt */ -#define MF_NOTIFYS 0x0004 /* Memo is a notification of receitp */ +#define MF_UNREAD 0x0001 /* Memo has not yet been read */ +#define MF_RECEIPT 0x0002 /* Sender requested receipt */ +#define MF_NOTIFYS 0x0004 /* Memo is a notification of receitp */ /* Nickname status flags: */ -#define NS_FORBIDDEN 0x0002 /* Nick may not be registered or used */ -#define NS_NO_EXPIRE 0x0004 /* Nick never expires */ +#define NS_FORBIDDEN 0x0002 /* Nick may not be registered or used */ +#define NS_NO_EXPIRE 0x0004 /* Nick never expires */ /* Nickname setting flags: */ -#define NI_KILLPROTECT 0x00000001 /* Kill others who take this nick */ -#define NI_SECURE 0x00000002 /* Don't recognize unless IDENTIFY'd */ -#define NI_MSG 0x00000004 /* Use PRIVMSGs instead of NOTICEs */ -#define NI_MEMO_HARDMAX 0x00000008 /* Don't allow user to change memo limit */ -#define NI_MEMO_SIGNON 0x00000010 /* Notify of memos at signon and un-away */ -#define NI_MEMO_RECEIVE 0x00000020 /* Notify of new memos when sent */ -#define NI_PRIVATE 0x00000040 /* Don't show in LIST to non-servadmins */ -#define NI_HIDE_EMAIL 0x00000080 /* Don't show E-mail in INFO */ -#define NI_HIDE_MASK 0x00000100 /* Don't show last seen address in INFO */ -#define NI_HIDE_QUIT 0x00000200 /* Don't show last quit message in INFO */ -#define NI_KILL_QUICK 0x00000400 /* Kill in 20 seconds instead of 60 */ -#define NI_KILL_IMMED 0x00000800 /* Kill immediately instead of in 60 sec */ -#define NI_ENCRYPTEDPW 0x00004000 /* Nickname password is encrypted */ -#define NI_MEMO_MAIL 0x00010000 /* User gets email on memo */ -#define NI_HIDE_STATUS 0x00020000 /* Don't show services access status */ -#define NI_SUSPENDED 0x00040000 /* Nickname is suspended */ -#define NI_AUTOOP 0x00080000 /* Autoop nickname in channels */ +#define NI_KILLPROTECT 0x00000001 /* Kill others who take this nick */ +#define NI_SECURE 0x00000002 /* Don't recognize unless IDENTIFY'd */ +#define NI_MSG 0x00000004 /* Use PRIVMSGs instead of NOTICEs */ +#define NI_MEMO_HARDMAX 0x00000008 /* Don't allow user to change memo limit */ +#define NI_MEMO_SIGNON 0x00000010 /* Notify of memos at signon and un-away */ +#define NI_MEMO_RECEIVE 0x00000020 /* Notify of new memos when sent */ +#define NI_PRIVATE 0x00000040 /* Don't show in LIST to non-servadmins */ +#define NI_HIDE_EMAIL 0x00000080 /* Don't show E-mail in INFO */ +#define NI_HIDE_MASK 0x00000100 /* Don't show last seen address in INFO */ +#define NI_HIDE_QUIT 0x00000200 /* Don't show last quit message in INFO */ +#define NI_KILL_QUICK 0x00000400 /* Kill in 20 seconds instead of 60 */ +#define NI_KILL_IMMED 0x00000800 /* Kill immediately instead of in 60 sec */ +#define NI_ENCRYPTEDPW 0x00004000 /* Nickname password is encrypted */ +#define NI_MEMO_MAIL 0x00010000 /* User gets email on memo */ +#define NI_HIDE_STATUS 0x00020000 /* Don't show services access status */ +#define NI_SUSPENDED 0x00040000 /* Nickname is suspended */ +#define NI_AUTOOP 0x00080000 /* Autoop nickname in channels */ #define NI_NOEXPIRE 0x00100000 /* nicks in this group won't expire */ // Old NS_FORBIDDEN, very fucking temporary. #define NI_FORBIDDEN 0x80000000 /* Retain topic even after last person leaves channel */ -#define CI_KEEPTOPIC 0x00000001 +#define CI_KEEPTOPIC 0x00000001 /* Don't allow non-authorized users to be opped */ -#define CI_SECUREOPS 0x00000002 +#define CI_SECUREOPS 0x00000002 /* Hide channel from ChanServ LIST command */ -#define CI_PRIVATE 0x00000004 +#define CI_PRIVATE 0x00000004 /* Topic can only be changed by SET TOPIC */ -#define CI_TOPICLOCK 0x00000008 +#define CI_TOPICLOCK 0x00000008 /* Those not allowed ops are kickbanned */ -#define CI_RESTRICTED 0x00000010 +#define CI_RESTRICTED 0x00000010 /* Don't allow ChanServ and BotServ commands to do bad things to bigger levels */ -#define CI_PEACE 0x00000020 +#define CI_PEACE 0x00000020 /* Don't allow any privileges unless a user is IDENTIFY'd with NickServ */ -#define CI_SECURE 0x00000040 +#define CI_SECURE 0x00000040 /* Don't allow the channel to be registered or used */ -#define CI_FORBIDDEN 0x00000080 +#define CI_FORBIDDEN 0x00000080 /* Channel password is encrypted */ -#define CI_ENCRYPTEDPW 0x00000100 +#define CI_ENCRYPTEDPW 0x00000100 /* Channel does not expire */ -#define CI_NO_EXPIRE 0x00000200 +#define CI_NO_EXPIRE 0x00000200 /* Channel memo limit may not be changed */ -#define CI_MEMO_HARDMAX 0x00000400 +#define CI_MEMO_HARDMAX 0x00000400 /* Send notice to channel on use of OP/DEOP */ -#define CI_OPNOTICE 0x00000800 +#define CI_OPNOTICE 0x00000800 /* Stricter control of channel founder status */ -#define CI_SECUREFOUNDER 0x00001000 +#define CI_SECUREFOUNDER 0x00001000 /* Always sign kicks */ -#define CI_SIGNKICK 0x00002000 +#define CI_SIGNKICK 0x00002000 /* Sign kicks if level is < than the one defined by the SIGNKICK level */ -#define CI_SIGNKICK_LEVEL 0x00004000 +#define CI_SIGNKICK_LEVEL 0x00004000 /* Use the xOP lists */ -#define CI_XOP 0x00008000 +#define CI_XOP 0x00008000 /* Channel is suspended */ -#define CI_SUSPENDED 0x00010000 +#define CI_SUSPENDED 0x00010000 /* akick */ -#define AK_USED 0x0001 -#define AK_ISNICK 0x0002 -#define AK_STUCK 0x0004 +#define AK_USED 0x0001 +#define AK_ISNICK 0x0002 +#define AK_STUCK 0x0004 /* botflags */ -#define BI_PRIVATE 0x0001 -#define BI_CHANSERV 0x0002 -#define BI_BOTSERV 0x0004 -#define BI_HOSTSERV 0x0008 -#define BI_OPERSERV 0x0010 -#define BI_MEMOSERV 0x0020 -#define BI_NICKSERV 0x0040 -#define BI_GLOBAL 0x0080 +#define BI_PRIVATE 0x0001 +#define BI_CHANSERV 0x0002 +#define BI_BOTSERV 0x0004 +#define BI_HOSTSERV 0x0008 +#define BI_OPERSERV 0x0010 +#define BI_MEMOSERV 0x0020 +#define BI_NICKSERV 0x0040 +#define BI_GLOBAL 0x0080 /* BotServ SET flags */ -#define BS_DONTKICKOPS 0x00000001 -#define BS_DONTKICKVOICES 0x00000002 -#define BS_FANTASY 0x00000004 -#define BS_SYMBIOSIS 0x00000008 -#define BS_GREET 0x00000010 -#define BS_NOBOT 0x00000020 +#define BS_DONTKICKOPS 0x00000001 +#define BS_DONTKICKVOICES 0x00000002 +#define BS_FANTASY 0x00000004 +#define BS_SYMBIOSIS 0x00000008 +#define BS_GREET 0x00000010 +#define BS_NOBOT 0x00000020 /* BotServ Kickers flags */ #define BS_KICK_BOLDS 0x80000000 @@ -330,35 +342,38 @@ int b64_encode(char *src, size_t srclength, char *target, size_t targsize); #define BS_KICK_REPEAT 0x01000000 /* Indices for TTB (Times To Ban) */ -#define TTB_BOLDS 0 -#define TTB_COLORS 1 -#define TTB_REVERSES 2 -#define TTB_UNDERLINES 3 -#define TTB_BADWORDS 4 -#define TTB_CAPS 5 -#define TTB_FLOOD 6 -#define TTB_REPEAT 7 -#define TTB_SIZE 8 - - - -#define LANG_EN_US 0 /* United States English */ -#define LANG_JA_JIS 1 /* Japanese (JIS encoding) */ -#define LANG_JA_EUC 2 /* Japanese (EUC encoding) */ -#define LANG_JA_SJIS 3 /* Japanese (SJIS encoding) */ -#define LANG_ES 4 /* Spanish */ -#define LANG_PT 5 /* Portugese */ -#define LANG_FR 6 /* French */ -#define LANG_TR 7 /* Turkish */ -#define LANG_IT 8 /* Italian */ -#define LANG_DE 9 /* German */ -#define LANG_CAT 10 /* Catalan */ -#define LANG_GR 11 /* Greek */ -#define LANG_NL 12 /* Dutch */ -#define LANG_RU 13 /* Russian */ -#define LANG_HUN 14 /* Hungarian */ -#define LANG_PL 15 /* Polish */ +enum +{ + TTB_BOLDS, + TTB_COLORS, + TTB_REVERSES, + TTB_UNDERLINES, + TTB_BADWORDS, + TTB_CAPS, + TTB_FLOOD, + TTB_REPEAT, + TTB_SIZE +}; +enum +{ + LANG_EN_US, /* United States English */ + LANG_JA_JIS, /* Japanese (JIS encoding) */ + LANG_JA_EUC, /* Japanese (EUC encoding) */ + LANG_JA_SJIS, /* Japanese (SJIS encoding) */ + LANG_ES, /* Spanish */ + LANG_PT, /* Portugese */ + LANG_FR, /* French */ + LANG_TR, /* Turkish */ + LANG_IT, /* Italian */ + LANG_DE, /* German */ + LANG_CAT, /* Catalan */ + LANG_GR, /* Greek */ + LANG_NL, /* Dutch */ + LANG_RU, /* Russian */ + LANG_HUN, /* Hungarian */ + LANG_PL /* Polish */ +}; const std::string GetLanguageID(int id) { @@ -420,80 +435,45 @@ dbFILE *open_db_read(const char *service, const char *filename, int version) FILE *fp; int myversion; - f = (dbFILE *)calloc(sizeof(*f), 1); - if (!f) { + f = new dbFILE; + if (!f) + { printf("Can't allocate memory for %s database %s.\n", service, filename); exit(0); } strscpy(f->filename, filename, sizeof(f->filename)); f->mode = 'r'; fp = fopen(f->filename, "rb"); - if (!fp) { + if (!fp) + { printf("Can't read %s database %s.\n", service, f->filename); - free(f); + //free(f); + delete f; return NULL; } f->fp = fp; myversion = fgetc(fp) << 24 | fgetc(fp) << 16 | fgetc(fp) << 8 | fgetc(fp); - if (feof(fp)) { + if (feof(fp)) + { printf("Error reading version number on %s: End of file detected.\n", f->filename); exit(0); - } else if (myversion < version) { - printf("Unsuported database version (%d) on %s.\n", myversion, f->filename); - exit(0); } - return f; -} - -/* Open a database file for reading and check for the version */ -dbFILE *open_db_write(const char *service, const char *filename, int version) -{ - dbFILE *f; - int fd; - - f = (dbFILE *)calloc(sizeof(*f), 1); - if (!f) { - printf("Can't allocate memory for %s database %s.\n", service, filename); + else if (myversion < version) + { + printf("Unsuported database version (%d) on %s.\n", myversion, f->filename); exit(0); } - strscpy(f->filename, filename, sizeof(f->filename)); - filename = f->filename; -#ifndef _WIN32 - unlink(filename); -#else - DeleteFile(filename); -#endif - f->mode = 'w'; -#ifndef _WIN32 - fd = open(filename, O_WRONLY | O_CREAT | O_EXCL, 0666); -#else - fd = _open(filename, O_WRONLY | O_CREAT | O_EXCL | _O_BINARY, 0666); -#endif - f->fp = fdopen(fd, "wb"); /* will fail and return NULL if fd < 0 */ - if (!f->fp || !write_file_version(f, version)) { - printf("Can't write to %s database %s.\n", service, filename); - if (f->fp) { - fclose(f->fp); -#ifndef _WIN32 - unlink(filename); -#else - DeleteFile(filename); -#endif - } - free(f); - return NULL; - } return f; } /* Close it */ -void close_db(dbFILE * f) +void close_db(dbFILE *f) { fclose(f->fp); - free(f); + delete f; } -int read_int16(int16 * ret, dbFILE * f) +int read_int16(int16 *ret, dbFILE *f) { int c1, c2; @@ -505,7 +485,7 @@ int read_int16(int16 * ret, dbFILE * f) return 0; } -int read_uint16(uint16 * ret, dbFILE * f) +int read_uint16(uint16 *ret, dbFILE *f) { int c1, c2; @@ -517,16 +497,14 @@ int read_uint16(uint16 * ret, dbFILE * f) return 0; } - -int write_int16(uint16 val, dbFILE * f) +int write_int16(uint16 val, dbFILE *f) { - if (fputc((val >> 8) & 0xFF, f->fp) == EOF - || fputc(val & 0xFF, f->fp) == EOF) + if (fputc((val >> 8) & 0xFF, f->fp) == EOF || fputc(val & 0xFF, f->fp) == EOF) return -1; return 0; } -int read_int32(int32 * ret, dbFILE * f) +int read_int32(int32 *ret, dbFILE *f) { int c1, c2, c3, c4; @@ -540,7 +518,7 @@ int read_int32(int32 * ret, dbFILE * f) return 0; } -int read_uint32(uint32 * ret, dbFILE * f) +int read_uint32(uint32 *ret, dbFILE *f) { int c1, c2, c3, c4; @@ -554,7 +532,7 @@ int read_uint32(uint32 * ret, dbFILE * f) return 0; } -int write_int32(uint32 val, dbFILE * f) +int write_int32(uint32 val, dbFILE *f) { if (fputc((val >> 24) & 0xFF, f->fp) == EOF) return -1; @@ -567,15 +545,14 @@ int write_int32(uint32 val, dbFILE * f) return 0; } - -int read_ptr(void **ret, dbFILE * f) +int read_ptr(void **ret, dbFILE *f) { int c; c = fgetc(f->fp); if (c == EOF) return -1; - *ret = (c ? (void *) 1 : (void *) 0); + *ret = c ? reinterpret_cast<void *>(1) : reinterpret_cast<void *>(0); return 0; } @@ -586,28 +563,29 @@ int write_ptr(const void *ptr, dbFILE * f) return 0; } - -int read_string(char **ret, dbFILE * f) +int read_string(char **ret, dbFILE *f) { char *s; uint16 len; if (read_uint16(&len, f) < 0) return -1; - if (len == 0) { + if (len == 0) + { *ret = NULL; return 0; } - s = (char *)calloc(len, 1); - if (len != fread(s, 1, len, f->fp)) { - free(s); + s = new char[len]; + if (len != fread(s, 1, len, f->fp)) + { + delete [] s; return -1; } *ret = s; return 0; } -int write_string(const char *s, dbFILE * f) +int write_string(const char *s, dbFILE *f) { uint32 len; @@ -616,7 +594,7 @@ int write_string(const char *s, dbFILE * f) len = strlen(s); if (len > 65534) len = 65534; - if (write_int16((uint16) (len + 1), f) < 0) + if (write_int16(static_cast<uint16>(len + 1), f) < 0) return -1; if (len > 0 && fwrite(s, 1, len, f->fp) != len) return -1; @@ -629,11 +607,9 @@ NickCore *findcore(const char *nick, int unused) { NickCore *nc; - for (nc = nclists[HASH(nick)]; nc; nc = nc->next) { - if (!mystricmp(nc->display, nick)) - if ((nc->unused && unused) || (!nc->unused && !unused)) - return nc; - } + for (nc = nclists[HASH(nick)]; nc; nc = nc->next) + if (!mystricmp(nc->display, nick) && ((nc->unused && unused) || (!nc->unused && !unused))) + return nc; return NULL; } @@ -642,23 +618,20 @@ NickAlias *findnick(const char *nick) { NickAlias *na; - for (na = nalists[HASH(nick)]; na; na = na->next) { + for (na = nalists[HASH(nick)]; na; na = na->next) if (!mystricmp(na->nick, nick)) return na; - } return NULL; } -int write_file_version(dbFILE * f, uint32 version) +int write_file_version(dbFILE *f, uint32 version) { FILE *fp = f->fp; - if (fputc(version >> 24 & 0xFF, fp) < 0 || - fputc(version >> 16 & 0xFF, fp) < 0 || - fputc(version >> 8 & 0xFF, fp) < 0 || - fputc(version & 0xFF, fp) < 0) { - printf("Error writing version number on %s.\n", f->filename); - exit(0); + if (fputc(version >> 24 & 0xFF, fp) < 0 || fputc(version >> 16 & 0xFF, fp) < 0 || fputc(version >> 8 & 0xFF, fp) < 0 || fputc(version & 0xFF, fp) < 0) + { + printf("Error writing version number on %s.\n", f->filename); + exit(0); } return 1; } @@ -682,11 +655,12 @@ int mystricmp(const char *s1, const char *s2) { register int c; - while ((c = tolower(*s1)) == tolower(*s2)) { - if (c == 0) + while ((c = tolower(*s1)) == tolower(*s2)) + { + if (!c) return 0; - s1++; - s2++; + ++s1; + ++s2; } if (c < tolower(*s2)) return -1; @@ -695,7 +669,8 @@ int mystricmp(const char *s1, const char *s2) int delnick(NickAlias *na, int donttouchthelist) { - if (!donttouchthelist) { + if (!donttouchthelist) + { /* Remove us from the aliases list */ if (na->next) na->next->prev = na->prev; @@ -706,14 +681,14 @@ int delnick(NickAlias *na, int donttouchthelist) } if (na->last_usermask) - free(na->last_usermask); + delete [] na->last_usermask; if (na->last_realname) - free(na->last_realname); + delete [] na->last_realname; if (na->last_quit) - free(na->last_quit); + delete [] na->last_quit; /* free() us */ - free(na->nick); - free(na); + delete [] na->nick; + delete na; return 1; } @@ -728,52 +703,48 @@ int delcore(NickCore *nc) else nclists[HASH(nc->display)] = nc->next; - free(nc->display); + delete [] nc->display; if (nc->pass) - free(nc->pass); + delete [] nc->pass; if (nc->email) - free(nc->email); + delete [] nc->email; if (nc->greet) - free(nc->greet); + delete [] nc->greet; if (nc->url) - free(nc->url); - if (nc->access) { - for (i = 0; i < nc->accesscount; i++) { + delete [] nc->url; + if (nc->access) + { + for (i = 0; i < nc->accesscount; ++i) if (nc->access[i]) - free(nc->access[i]); - } - free(nc->access); + delete [] nc->access[i]; + delete [] nc->access; } - if (nc->memos.memos) { - for (i = 0; i < nc->memos.memocount; i++) { + if (nc->memos.memos) + { + for (i = 0; i < nc->memos.memocount; ++i) if (nc->memos.memos[i].text) - free(nc->memos.memos[i].text); - } - free(nc->memos.memos); + delete [] nc->memos.memos[i].text; + delete [] nc->memos.memos; } - free(nc); + delete nc; return 1; } - ChannelInfo *cs_findchan(const char *chan) { ChannelInfo *ci; - for (ci = chanlists[tolower(chan[1])]; ci; ci = ci->next) { + for (ci = chanlists[tolower(chan[1])]; ci; ci = ci->next) if (!mystricmp(ci->name, chan)) return ci; - } return NULL; } -void alpha_insert_chan(ChannelInfo * ci) +void alpha_insert_chan(ChannelInfo *ci) { ChannelInfo *ptr, *prev; char *chan = ci->name; - for (prev = NULL, ptr = chanlists[tolower(chan[1])]; - ptr != NULL && mystricmp(ptr->name, chan) < 0; - prev = ptr, ptr = ptr->next); + for (prev = NULL, ptr = chanlists[tolower(chan[1])]; ptr && mystricmp(ptr->name, chan) < 0; prev = ptr, ptr = ptr->next); ci->prev = prev; ci->next = ptr; if (!prev) @@ -787,24 +758,22 @@ void alpha_insert_chan(ChannelInfo * ci) HostCore *findHostCore(char *nick) { for (HostCore *hc = head; hc; hc = hc->next) - { if (nick && hc->nick && !mystricmp(hc->nick, nick)) return hc; - } return NULL; } static char *int_to_base64(long); static long base64_to_int(char *); -const char* base64enc(long i) +const char *base64enc(long i) { if (i < 0) - return ("0"); + return "0"; return int_to_base64(i); } -long base64dec(char* b64) +long base64dec(char *b64) { if (b64) return base64_to_int(b64); @@ -812,9 +781,7 @@ long base64dec(char* b64) return 0; } - -static const char Base64[] = - "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"; +static const char Base64[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"; static const char Pad64 = '='; /* (From RFC1521 and draft-ietf-dnssec-secext-03.txt) @@ -887,7 +854,8 @@ int b64_encode(char *src, size_t srclength, char *target, size_t targsize) unsigned char output[4]; size_t i; - while (2 < srclength) { + while (srclength > 2) + { input[0] = *src++; input[1] = *src++; input[2] = *src++; @@ -899,7 +867,7 @@ int b64_encode(char *src, size_t srclength, char *target, size_t targsize) output[3] = input[2] & 0x3f; if (datalength + 4 > targsize) - return (-1); + return -1; target[datalength++] = Base64[output[0]]; target[datalength++] = Base64[output[1]]; target[datalength++] = Base64[output[2]]; @@ -907,10 +875,11 @@ int b64_encode(char *src, size_t srclength, char *target, size_t targsize) } /* Now we worry about padding. */ - if (0 != srclength) { + if (srclength) + { /* Get what's left. */ input[0] = input[1] = input[2] = '\0'; - for (i = 0; i < srclength; i++) + for (i = 0; i < srclength; ++i) input[i] = *src++; output[0] = input[0] >> 2; @@ -918,7 +887,7 @@ int b64_encode(char *src, size_t srclength, char *target, size_t targsize) output[2] = ((input[1] & 0x0f) << 2) + (input[2] >> 6); if (datalength + 4 > targsize) - return (-1); + return -1; target[datalength++] = Base64[output[0]]; target[datalength++] = Base64[output[1]]; if (srclength == 1) @@ -928,9 +897,9 @@ int b64_encode(char *src, size_t srclength, char *target, size_t targsize) target[datalength++] = Pad64; } if (datalength >= targsize) - return (-1); + return -1; target[datalength] = '\0'; /* Returned value doesn't count \0. */ - return (datalength); + return datalength; } /* skips all whitespace anywhere. @@ -942,64 +911,68 @@ int b64_encode(char *src, size_t srclength, char *target, size_t targsize) int b64_decode(const char *src, char *target, size_t targsize) { int tarindex, state, ch; - char *pos; + const char *pos; state = 0; tarindex = 0; - while ((ch = *src++) != '\0') { - if (isspace(ch)) /* Skip whitespace anywhere. */ + while ((ch = *src++)) + { + if (isspace(ch)) /* Skip whitespace anywhere. */ continue; if (ch == Pad64) break; - pos = const_cast<char *>(strchr(Base64, ch)); - if (pos == 0) /* A non-base64 character. */ - return (-1); - - switch (state) { - case 0: - if (target) { - if ((size_t) tarindex >= targsize) - return (-1); - target[tarindex] = (pos - Base64) << 2; - } - state = 1; - break; - case 1: - if (target) { - if ((size_t) tarindex + 1 >= targsize) - return (-1); - target[tarindex] |= (pos - Base64) >> 4; - target[tarindex + 1] = ((pos - Base64) & 0x0f) - << 4; - } - tarindex++; - state = 2; - break; - case 2: - if (target) { - if ((size_t) tarindex + 1 >= targsize) - return (-1); - target[tarindex] |= (pos - Base64) >> 2; - target[tarindex + 1] = ((pos - Base64) & 0x03) - << 6; - } - tarindex++; - state = 3; - break; - case 3: - if (target) { - if ((size_t) tarindex >= targsize) - return (-1); - target[tarindex] |= (pos - Base64); - } - tarindex++; - state = 0; - break; - default: - abort(); + pos = strchr(Base64, ch); + if (!pos) /* A non-base64 character. */ + return -1; + + switch (state) + { + case 0: + if (target) + { + if (static_cast<size_t>(tarindex) >= targsize) + return -1; + target[tarindex] = (pos - Base64) << 2; + } + state = 1; |
