Configuration INI added. Memory leak in VESA shutdown fixed.
This commit is contained in:
parent
aa09a6c31c
commit
bd2bc76c8b
22 changed files with 3661 additions and 153 deletions
1
.gitignore
vendored
1
.gitignore
vendored
|
@ -1,6 +1,7 @@
|
|||
build-*
|
||||
*~
|
||||
*.user
|
||||
*.o
|
||||
|
||||
client/bin/
|
||||
client/obj/
|
||||
|
|
15
LICENSE
15
LICENSE
|
@ -22,6 +22,11 @@ Licenses Used By:
|
|||
Client
|
||||
======
|
||||
|
||||
blowfish-api
|
||||
------------
|
||||
https://github.com/tombonner/blowfish-api
|
||||
Attribution
|
||||
|
||||
DOS Serial Library
|
||||
------------------
|
||||
https://github.com/kstenerud/DOS-Serial-Library
|
||||
|
@ -32,6 +37,11 @@ ENet
|
|||
https://github.com/zpl-c/enet
|
||||
MIT
|
||||
|
||||
ini
|
||||
---
|
||||
https://github.com/rxi/ini
|
||||
MIT
|
||||
|
||||
MemWatch
|
||||
--------
|
||||
http://www.linkdata.se/sourcecode/memwatch/
|
||||
|
@ -80,6 +90,11 @@ ENet
|
|||
https://github.com/zpl-c/enet
|
||||
MIT
|
||||
|
||||
ini
|
||||
---
|
||||
https://github.com/rxi/ini
|
||||
MIT
|
||||
|
||||
MemWatch
|
||||
--------
|
||||
http://www.linkdata.se/sourcecode/memwatch/
|
||||
|
|
|
@ -33,7 +33,9 @@ BINDIR = bin
|
|||
## CHANGE THIS ##
|
||||
|
||||
# CFLAGS, LDFLAGS, CPPFLAGS, PREFIX can be overriden on CLI
|
||||
CFLAGS := $(DEBUG) -I$(SRCDIR) -I$(SRCDIR)/system -I$(SRCDIR)/dos -I$(SRCDIR)/gui -I$(SRCDIR)/thirdparty -I$(SRCDIR)/thirdparty/serial -I$(SHARED) -I$(SHARED)/thirdparty
|
||||
CFLAGS := $(DEBUG)
|
||||
CFLAGS += -I$(SRCDIR) -I$(SRCDIR)/system -I$(SRCDIR)/dos -I$(SRCDIR)/gui -I$(SRCDIR)/thirdparty
|
||||
CFLAGS += -I$(SHARED) -I$(SHARED)/thirdparty
|
||||
CPPFLAGS :=
|
||||
LDFLAGS :=
|
||||
PREFIX := /usr/local
|
||||
|
|
|
@ -16,6 +16,8 @@
|
|||
# along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
#
|
||||
|
||||
#CONFIG *= testing
|
||||
|
||||
# NOTE: You'll occasionally find a file with a capital "C" extension.
|
||||
# These are C files that we want ignored by the DOS compiler.
|
||||
|
||||
|
@ -25,8 +27,7 @@ CONFIG -= qt
|
|||
DESTDIR = $$OUT_PWD/bin
|
||||
SHARED = $$PWD/../shared
|
||||
|
||||
DOS_HEADERS = \
|
||||
src/thirdparty/serial/serial.h
|
||||
DOS_HEADERS =
|
||||
|
||||
DOS_SOURCES = \
|
||||
src/thirdparty/serial/serial.c \
|
||||
|
@ -59,14 +60,17 @@ HEADERS = \
|
|||
$$SHARED/thirdparty/stb_ds.h \
|
||||
$$SHARED/thirdparty/stb_image.h \
|
||||
$$SHARED/thirdparty/memwatch/memwatch.h \
|
||||
src/system/comport.h \
|
||||
$$SHARED/thirdparty/blowfish-api/blowfish.h \
|
||||
$$SHARED/thirdparty/ini/src/ini.h \
|
||||
src/config.h \
|
||||
src/system/util.h \
|
||||
src/thirdparty/minicoro/minicoro.h \
|
||||
src/system/comport.h \
|
||||
src/settings.h \
|
||||
src/system/color.h \
|
||||
src/system/memory.h \
|
||||
src/system/surface.h \
|
||||
src/system/taglist.h \
|
||||
src/test.h \
|
||||
src/system/keyboard.h \
|
||||
src/system/task.h \
|
||||
src/system/timer.h \
|
||||
|
@ -97,10 +101,14 @@ HEADERS = \
|
|||
SOURCES = \
|
||||
$$LINUX_SOURCES \
|
||||
$$SHARED/thirdparty/memwatch/memwatch.c \
|
||||
$$SHARED/thirdparty/blowfish-api/blowfish.c \
|
||||
$$SHARED/thirdparty/ini/src/ini.c \
|
||||
src/config.c \
|
||||
src/system/memory.c \
|
||||
src/settings.c \
|
||||
src/system/surface.c \
|
||||
src/system/taglist.c \
|
||||
src/system/util.c \
|
||||
src/test.c \
|
||||
src/system/array.c \
|
||||
src/system/log.c \
|
||||
|
@ -135,3 +143,9 @@ OTHER_FILES = \
|
|||
build.sh \
|
||||
test.sh \
|
||||
test.conf
|
||||
|
||||
testing {
|
||||
DEFINES *= TESTING
|
||||
HEADERS += src/test.h
|
||||
SOURCES += src/test.c
|
||||
}
|
||||
|
|
97
client/src/config.c
Normal file
97
client/src/config.c
Normal file
|
@ -0,0 +1,97 @@
|
|||
/*
|
||||
* Kangaroo Punch MultiPlayer Game Server Mark II
|
||||
* Copyright (C) 2020-2021 Scott Duensing
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
*
|
||||
*/
|
||||
|
||||
|
||||
#include "thirdparty/ini/src/ini.h"
|
||||
|
||||
#include "config.h"
|
||||
#include "util.h"
|
||||
|
||||
|
||||
ConfigT _configData;
|
||||
|
||||
static char *_configName = NULL;
|
||||
|
||||
|
||||
void configShutdown(void) {
|
||||
FILE *out = NULL;
|
||||
|
||||
out = fopen(_configName, "w");
|
||||
if (out) {
|
||||
fprintf(out,
|
||||
"[VIDEO]\n"
|
||||
"WIDTH=%d\n"
|
||||
"HEIGHT=%d\n"
|
||||
"DEPTH=%d\n\n"
|
||||
"[SERIAL]\n"
|
||||
"COM=%d\n\n"
|
||||
"[SERVER]\n"
|
||||
"HOST=%s\n"
|
||||
"PORT=%d\n\n"
|
||||
"[USER]\n"
|
||||
"NAME=%s\n",
|
||||
_configData.videoWidth, _configData.videoHeight, _configData.videoDepth,
|
||||
_configData.serialCom,
|
||||
_configData.serverHost, _configData.serverPort,
|
||||
_configData.userName
|
||||
);
|
||||
fclose(out);
|
||||
}
|
||||
|
||||
free(_configData.serverHost);
|
||||
_configData.serverHost = NULL;
|
||||
|
||||
free(_configData.userName);
|
||||
_configData.userName = NULL;
|
||||
|
||||
free(_configName);
|
||||
_configName = NULL;
|
||||
}
|
||||
|
||||
|
||||
void configStartup(char *file) {
|
||||
ini_t *ini = NULL;
|
||||
|
||||
// We need this later.
|
||||
_configName = utilAppNameWithNewExtensionGet(file, "ini");
|
||||
|
||||
// Numeric Defaults.
|
||||
_configData.videoWidth = 800;
|
||||
_configData.videoHeight = 600;
|
||||
_configData.videoDepth = 16;
|
||||
_configData.serialCom = 0;
|
||||
_configData.serverPort = 16550;
|
||||
|
||||
// Load from disk if available.
|
||||
ini = ini_load(_configName);
|
||||
if (ini) {
|
||||
ini_sget(ini, "VIDEO", "WIDTH", "%d", &_configData.videoWidth);
|
||||
ini_sget(ini, "VIDEO", "HEIGHT", "%d", &_configData.videoHeight);
|
||||
ini_sget(ini, "VIDEO", "DEPTH", "%d", &_configData.videoDepth);
|
||||
ini_sget(ini, "SERIAL", "COM", "%d", &_configData.serialCom);
|
||||
_configData.serverHost = strdup(ini_get(ini, "SERVER", "HOST"));
|
||||
ini_sget(ini, "SERVER", "PORT", "%d", &_configData.serverPort);
|
||||
_configData.userName = strdup(ini_get(ini, "USER", "NAME"));
|
||||
ini_free(ini);
|
||||
}
|
||||
|
||||
// String defaults.
|
||||
if (!_configData.serverHost) _configData.serverHost = strdup("\"kanga.world\"");
|
||||
if (!_configData.userName) _configData.userName = strdup("\"New User\"");
|
||||
}
|
46
client/src/config.h
Normal file
46
client/src/config.h
Normal file
|
@ -0,0 +1,46 @@
|
|||
/*
|
||||
* Kangaroo Punch MultiPlayer Game Server Mark II
|
||||
* Copyright (C) 2020-2021 Scott Duensing
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
*
|
||||
*/
|
||||
|
||||
|
||||
#ifndef CONFIG_H
|
||||
#define CONFIG_H
|
||||
|
||||
|
||||
#include "os.h"
|
||||
|
||||
|
||||
typedef struct ConfigS {
|
||||
uint16_t videoWidth;
|
||||
uint16_t videoHeight;
|
||||
uint8_t videoDepth;
|
||||
uint8_t serialCom;
|
||||
char *serverHost;
|
||||
uint16_t serverPort;
|
||||
char *userName;
|
||||
} ConfigT;
|
||||
|
||||
|
||||
extern ConfigT _configData;
|
||||
|
||||
|
||||
void configShutdown(void);
|
||||
void configStartup(char *file);
|
||||
|
||||
|
||||
#endif // CONFIG_H
|
|
@ -22,6 +22,7 @@
|
|||
|
||||
#include "vesa.h"
|
||||
#include "surface.h"
|
||||
#include "../system/log.h"
|
||||
|
||||
|
||||
// http://www.delorie.com/djgpp/doc/brennan/brennan_att_inline_djgpp.html
|
||||
|
@ -135,13 +136,9 @@ typedef struct VBESurfaceS {
|
|||
static void vbeCreatePalette(void);
|
||||
static VBEInfoT *vbeGetInfo(void);
|
||||
static VBEModeInfoT *vbeGetModeInfo(uint16_t vbeModeNumber);
|
||||
//static VBEModeInfoT *vbeGetModeInfoPtr(void);
|
||||
static void *vbeGetPmodeInterface(void);
|
||||
//static VBESurfaceT *vbeGetVBESurfacePtr(void);
|
||||
static uint8_t vbeIsDesiredMode(void);
|
||||
//static VBESurfaceT *vbeModeInit(uint16_t xRes, uint16_t yRes, uint8_t bpp);
|
||||
static uint16_t vbeSelectModeNumber(uint16_t xRes, uint16_t yRes, uint8_t bpp);
|
||||
//static void vbeSetDisplayStart(uint32_t pixel, uint32_t scanline);
|
||||
static VBESurfaceT *vbeSetMode(uint16_t vbeModeNumber);
|
||||
static uint16_t vbeSetScanlineLength(uint16_t pixelLength);
|
||||
|
||||
|
@ -305,13 +302,6 @@ static VBEModeInfoT *vbeGetModeInfo(uint16_t vbeModeNumber) {
|
|||
}
|
||||
|
||||
|
||||
/*
|
||||
static VBEModeInfoT *vbeGetModeInfoPtr(void) {
|
||||
return(&_vbeModeInfo);
|
||||
}
|
||||
*/
|
||||
|
||||
|
||||
static void *vbeGetPmodeInterface(void) {
|
||||
__dpmi_regs r;
|
||||
__dpmi_meminfo m;
|
||||
|
@ -332,7 +322,6 @@ static void *vbeGetPmodeInterface(void) {
|
|||
|
||||
// need memory-mapped IO?
|
||||
if (_pmodeInterfacePtr->ioInfo) {
|
||||
printf("\nGot IOInfo\n");
|
||||
ptr = (uint16_t *)((char *)_pmodeInterfacePtr + _pmodeInterfacePtr->ioInfo);
|
||||
// skip the port table...
|
||||
while (*ptr != 0xFFFF)
|
||||
|
@ -362,13 +351,6 @@ static void *vbeGetPmodeInterface(void) {
|
|||
}
|
||||
|
||||
|
||||
/*
|
||||
static VBESurfaceT *vbeGetVBESurfacePtr(void) {
|
||||
return(&_vbeSurface);
|
||||
}
|
||||
*/
|
||||
|
||||
|
||||
static uint8_t vbeIsDesiredMode(void) {
|
||||
// This is a custom filter to remove modes not supported by the calling application.
|
||||
|
||||
|
@ -400,32 +382,6 @@ ColorT vbeColorMake(uint8_t red, uint8_t green, uint8_t blue) {
|
|||
}
|
||||
|
||||
|
||||
/*
|
||||
static VBESurfaceT *vbeModeInit(uint16_t xRes, uint16_t yRes, uint8_t bpp) {
|
||||
uint16_t vbeModeNumber;
|
||||
__dpmi_meminfo m;
|
||||
|
||||
if (_vbeSurface.vbeBoolean == 0) return NULL;
|
||||
if (_vbeSurface.vbeInitBoolean == 0) return NULL;
|
||||
|
||||
m.size = (_vbeInfo.totalMemory * 64 * 1024);
|
||||
m.address = _vbeSurface.lfbLinearAddress;
|
||||
__dpmi_unlock_linear_region(&m);
|
||||
__dpmi_free_physical_address_mapping(&m);
|
||||
__dpmi_free_ldt_descriptor(_vbeSurface.lfbSelector);
|
||||
|
||||
_vbeSurface.vbeInitBoolean = 0;
|
||||
|
||||
if ((vbeModeNumber = vbeSelectModeNumber(xRes, yRes, bpp)) == 0) return NULL;
|
||||
|
||||
vbeModeNumber |= 0x8000;
|
||||
if (vbeSetMode(vbeModeNumber) == NULL) return NULL;
|
||||
|
||||
return(&_vbeSurface);
|
||||
}
|
||||
*/
|
||||
|
||||
|
||||
void vbePresent(void) {
|
||||
SurfaceT *o = surfaceOffScreenGet();
|
||||
_movedatal(_my_ds(), (int32_t)o->buffer.bits32, _vbeSurface.lfbSelector, 0x0, _vbeSurface.screenDWords);
|
||||
|
@ -450,43 +406,6 @@ static uint16_t vbeSelectModeNumber(uint16_t xRes, uint16_t yRes, uint8_t bpp) {
|
|||
}
|
||||
|
||||
|
||||
/*
|
||||
static void vbeSetDisplayStart(uint32_t pixel, uint32_t scanline) {
|
||||
__dpmi_regs r;
|
||||
int32_t address;
|
||||
int32_t selector;
|
||||
|
||||
if (pmVBESetDisplayStart) {
|
||||
address = (_yTable[scanline] + (pixel * _vbeSurface.bytesPerPixel)) >> 2;
|
||||
selector = (_vbeSurface.ioSegment) ? _vbeSurface.ioSegment : _my_ds();
|
||||
asm(
|
||||
" pushw %%es ; "
|
||||
" movw %w1, %%es ; " // set the IO segment
|
||||
" call *%0 ; " // call the VESA function
|
||||
" popw %%es "
|
||||
: // no outputs
|
||||
|
||||
: "S"(pmVBESetDisplayStart), // function pointer in esi
|
||||
"a"(selector), // IO segment in eax
|
||||
"b"(0x0080), // mode in ebx
|
||||
"c"(address & 0xFFFF), // low word of address in ecx
|
||||
"d"((address >> 16)) // high word of address in edx
|
||||
|
||||
: "memory", "%edi", "%cc" // touches edi and flags
|
||||
);
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
r.x.ax = 0x4F07;
|
||||
r.x.bx = 0x0080;
|
||||
r.x.cx = pixel;
|
||||
r.x.dx = scanline;
|
||||
__dpmi_int(0x10, &r);
|
||||
}
|
||||
*/
|
||||
|
||||
|
||||
static VBESurfaceT *vbeSetMode(uint16_t vbeModeNumber) {
|
||||
__dpmi_regs r;
|
||||
__dpmi_meminfo m;
|
||||
|
@ -597,10 +516,11 @@ int16_t vbeInfoShow(void) {
|
|||
//printf("Based on: VBE 2.0 driver v1.0 (c) 1999, Tobias Koch <tobias.koch@gmail.com>\n\n");
|
||||
|
||||
if (vbeGetInfo() == NULL) {
|
||||
printf("No VESA BIOS Extensions found.\n");
|
||||
logWrite("No VESA BIOS Extensions found.\n");
|
||||
vbeShutdown();
|
||||
return(1);
|
||||
}
|
||||
printf(
|
||||
logWrite(
|
||||
"Video Memory - %d KB\n"
|
||||
"VBE Version - %d.%d detected\n"
|
||||
"OEM Specification - %s\n",
|
||||
|
@ -609,7 +529,7 @@ int16_t vbeInfoShow(void) {
|
|||
_vbeInfo.oemStringPtr);
|
||||
|
||||
if (_vbeInfo.vbeVersion >= 0x0200) {
|
||||
printf(
|
||||
logWrite(
|
||||
"OEM Software Revision - %d.%d\n"
|
||||
"OEM Vendor Name - %s\n"
|
||||
"OEM Product Name - %s\n"
|
||||
|
@ -621,7 +541,8 @@ int16_t vbeInfoShow(void) {
|
|||
_vbeInfo.oemProductRevPtr,
|
||||
vbeGetPmodeInterface() ? "Found" : "Missing");
|
||||
} else {
|
||||
printf("VESA BIOS Extension 2.0 or better required!\n");
|
||||
logWrite("VESA BIOS Extension 2.0 or better required!\n");
|
||||
vbeShutdown();
|
||||
return(1);
|
||||
}
|
||||
|
||||
|
@ -629,7 +550,7 @@ int16_t vbeInfoShow(void) {
|
|||
if (_vbeInfo.videoModePtr[counter] == 0xFFFF) break;
|
||||
vbeGetModeInfo(_vbeInfo.videoModePtr[counter]);
|
||||
if (vbeIsDesiredMode()) {
|
||||
printf("Mode %Xh - %4d x %4d x %2d - RGB %d:%d:%d - %s\n",
|
||||
logWrite("Mode %Xh - %4d x %4d x %2d - RGB %d:%d:%d - %s\n",
|
||||
_vbeInfo.videoModePtr[counter],
|
||||
_vbeModeInfo.xResolution,
|
||||
_vbeModeInfo.yResolution,
|
||||
|
@ -641,6 +562,7 @@ int16_t vbeInfoShow(void) {
|
|||
}
|
||||
}
|
||||
|
||||
vbeShutdown();
|
||||
return(0);
|
||||
}
|
||||
|
||||
|
@ -652,7 +574,7 @@ int16_t vbeShutdown(void) {
|
|||
if (_vbeSurface.vbeInitBoolean == 0) {
|
||||
r.x.ax = 0x03; // make sure we're in 3h
|
||||
__dpmi_int(0x10, &r); // for buggy vesa implementations
|
||||
return(1);
|
||||
//return(1);
|
||||
}
|
||||
|
||||
if (_yTable) free(_yTable);
|
||||
|
@ -690,23 +612,27 @@ uint8_t vbeStartup(uint16_t xRes, uint16_t yRes, uint8_t bpp) {
|
|||
uint16_t vbeModeNumber;
|
||||
|
||||
if (vbeGetInfo() == NULL) {
|
||||
printf("No VESA BIOS Extensions found.\n");
|
||||
logWrite("No VESA BIOS Extensions found.\n");
|
||||
vbeShutdown();
|
||||
return 1;
|
||||
}
|
||||
|
||||
if (_vbeInfo.vbeVersion < 0x0200) {
|
||||
printf("VBE Version 2.0 or better required.\n");
|
||||
logWrite("VBE Version 2.0 or better required.\n");
|
||||
vbeShutdown();
|
||||
return 2;
|
||||
}
|
||||
|
||||
vbeGetPmodeInterface();
|
||||
|
||||
if ((vbeModeNumber = vbeSelectModeNumber(xRes, yRes, bpp)) == 0) {
|
||||
printf("No appropriate video mode available.\n");
|
||||
logWrite("No appropriate video mode available.\n");
|
||||
vbeShutdown();
|
||||
return 3;
|
||||
}
|
||||
|
||||
if (vbeSetMode(vbeModeNumber) == NULL) {
|
||||
vbeShutdown();
|
||||
return 4;
|
||||
}
|
||||
|
||||
|
|
|
@ -18,22 +18,19 @@
|
|||
*/
|
||||
|
||||
|
||||
//#define TESTING
|
||||
|
||||
|
||||
/*
|
||||
* To Do:
|
||||
*
|
||||
* - Replace any direct data manipulation from outside a class with methods to handle it
|
||||
* - More widget states: Ghosted, hidden
|
||||
* - More widget states: Ghosted (underway), hidden
|
||||
* - Methods that can change the width of a widget (such as setTitle) need to repaint the parent window as well
|
||||
* - Metrics, colors, etc. should be defined in each widget and not in GUI
|
||||
* - Widgets should support a "changed" callback that can cancel the change
|
||||
* - Use LabelT in all widgets that have a label
|
||||
* - Find a light grey to replace white widget data areas
|
||||
* - No thumb in listbox scrollbar
|
||||
*
|
||||
* - Random crash on exit - looks memwatch / task related - hasn't happened on DOS yet
|
||||
* - Layout container widgets!
|
||||
* - DOS makefile is placing thirdparty object files in the wrong place
|
||||
*/
|
||||
|
||||
|
||||
|
@ -48,6 +45,7 @@
|
|||
#include "font.h"
|
||||
#include "timer.h"
|
||||
#include "gui.h"
|
||||
#include "config.h"
|
||||
|
||||
#include "welcome.h"
|
||||
|
||||
|
@ -113,33 +111,29 @@ static void taskGuiEventLoop(void *data) {
|
|||
|
||||
int main(int argc, char *argv[]) {
|
||||
|
||||
uint16_t xResolution = 0;
|
||||
uint16_t yResolution = 0;
|
||||
uint16_t colorDepth = 0;
|
||||
|
||||
memoryStartup(argv[0]);
|
||||
logOpenByHandle(memoryLogHandleGet());
|
||||
configStartup(argv[0]);
|
||||
|
||||
// 0 1 2 3 4 5 6 7 8
|
||||
// 12345678901234567890123456789012345678901234567890123456789012345678901234567890
|
||||
printf("Kangaroo Punch MultiPlayer DOS Game Client Mark II\n");
|
||||
printf("Copyright (C) 2020-2021 Scott Duensing scott@kangaroopunch.com\n\n");
|
||||
fflush(stdout);
|
||||
logWrite("%s", "Kangaroo Punch MultiPlayer DOS Game Client Mark II\n");
|
||||
logWrite("%s", "Copyright (C) 2020-2021 Scott Duensing scott@kangaroopunch.com\n\n");
|
||||
|
||||
// Command line needs to have the desired resolution and color depth on it.
|
||||
if (argc != 4) {
|
||||
vbeInfoShow();
|
||||
fflush(stdout);
|
||||
memoryShutdown();
|
||||
return 0;
|
||||
if (argc > 1) {
|
||||
if (strcmp(argv[1], "/?") == 0) {
|
||||
vbeInfoShow();
|
||||
configShutdown();
|
||||
logClose();
|
||||
memoryShutdown();
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
xResolution = atoi(argv[1]);
|
||||
yResolution = atoi(argv[2]);
|
||||
colorDepth = atoi(argv[3]);
|
||||
|
||||
// Do we have the video mode they asked for?
|
||||
if (vbeStartup(xResolution, yResolution, colorDepth)) {
|
||||
fflush(stdout);
|
||||
if (vbeStartup(_configData.videoWidth, _configData.videoHeight, _configData.videoDepth)) {
|
||||
configShutdown();
|
||||
logClose();
|
||||
memoryShutdown();
|
||||
return 1;
|
||||
}
|
||||
|
@ -165,7 +159,7 @@ int main(int argc, char *argv[]) {
|
|||
mouseShutdown();
|
||||
surfaceShutdown();
|
||||
vbeShutdown();
|
||||
|
||||
configShutdown();
|
||||
logClose();
|
||||
memoryShutdown();
|
||||
|
||||
|
|
|
@ -25,6 +25,7 @@
|
|||
#include "task.h"
|
||||
#include "comport.h"
|
||||
#include "timer.h"
|
||||
#include "config.h"
|
||||
|
||||
#include "window.h"
|
||||
#include "button.h"
|
||||
|
@ -37,9 +38,10 @@
|
|||
|
||||
#define GROUP_COM 1
|
||||
|
||||
#define PORT_NONE 0
|
||||
#define PORT_BAD 1
|
||||
#define PORT_GOOD 2
|
||||
#define PORT_NONE 0
|
||||
#define PORT_NO_MODEM 1
|
||||
#define PORT_BAD_MODEM 2
|
||||
#define PORT_GOOD_MODEM 3
|
||||
|
||||
#define TITLE_LEN 80
|
||||
|
||||
|
@ -49,6 +51,7 @@ typedef struct PortS {
|
|||
char title[TITLE_LEN];
|
||||
uint8_t selected;
|
||||
uint8_t enabled;
|
||||
RadioT *rdoCOM;
|
||||
} PortT;
|
||||
|
||||
|
||||
|
@ -59,16 +62,35 @@ static WindowT *winSettings;
|
|||
static FrameT *frmComPorts;
|
||||
static FrameT *frmServer;
|
||||
static ButtonT *btnOkay;
|
||||
static RadioT *rdoCOM[4];
|
||||
static TextboxT *txtServer;
|
||||
static UpdownT *updPort;
|
||||
|
||||
static PortT port[4];
|
||||
|
||||
|
||||
static void btnOkayClick(WidgetT *widget);
|
||||
|
||||
|
||||
static void btnOkayClick(WidgetT *widget) {
|
||||
uint8_t x;
|
||||
RadioT *selected = radioSelectedGet(port[0].rdoCOM);
|
||||
|
||||
(void)widget;
|
||||
|
||||
// Save selected COM port.
|
||||
for (x=0; x<4; x++) {
|
||||
if (selected == port[x].rdoCOM) {
|
||||
_configData.serialCom = x + 1;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
// Save server info.
|
||||
free(_configData.serverHost);
|
||||
_configData.serverHost = strdup(textboxValueGet(txtServer));
|
||||
_configData.serverPort = updownValueGet(updPort);
|
||||
|
||||
// Return to welcome dialog.
|
||||
taskCreate(taskWelcome, NULL);
|
||||
guiDelete(D(winSettings));
|
||||
}
|
||||
|
@ -80,7 +102,6 @@ void taskSettings(void *data) {
|
|||
uint32_t len;
|
||||
char buffer[1024];
|
||||
uint8_t quarterSeconds = 0;
|
||||
PortT port[4];
|
||||
uint8_t selected = 1;
|
||||
|
||||
(void)data;
|
||||
|
@ -101,6 +122,7 @@ void taskSettings(void *data) {
|
|||
tagListRun(uiDetecting);
|
||||
taskYield(); // Cause dialog to paint.
|
||||
|
||||
// Scan the COM ports for a compatable modem.
|
||||
for (int x=0; x<4; x++) {
|
||||
rc = comOpen(x, 57600L, 8, 'n', 1, SER_HANDSHAKING_RTSCTS);
|
||||
if (rc == SER_SUCCESS) {
|
||||
|
@ -116,15 +138,22 @@ void taskSettings(void *data) {
|
|||
buffer[len] = 0;
|
||||
if (strstr(buffer, "OK")) {
|
||||
snprintf(port[x].title, TITLE_LEN - 1, "COM%d - SoftModem Found!", x + 1);
|
||||
port[x].status = PORT_GOOD;
|
||||
port[x].status = PORT_GOOD_MODEM;
|
||||
port[x].selected = selected;
|
||||
port[x].enabled = 1;
|
||||
selected = 0;
|
||||
} else {
|
||||
snprintf(port[x].title, TITLE_LEN - 1, "COM%d - Incompatable Modem", x + 1);
|
||||
port[x].status = PORT_BAD;
|
||||
port[x].selected = 0;
|
||||
port[x].enabled = 0;
|
||||
if (strstr(buffer, "ERROR")) {
|
||||
snprintf(port[x].title, TITLE_LEN - 1, "COM%d - Incompatable Modem", x + 1);
|
||||
port[x].status = PORT_BAD_MODEM;
|
||||
port[x].selected = 0;
|
||||
port[x].enabled = 0;
|
||||
} else {
|
||||
snprintf(port[x].title, TITLE_LEN - 1, "COM%d - No Modem", x + 1);
|
||||
port[x].status = PORT_NO_MODEM;
|
||||
port[x].selected = 0;
|
||||
port[x].enabled = 0;
|
||||
}
|
||||
}
|
||||
comClose(x);
|
||||
} else {
|
||||
|
@ -154,12 +183,12 @@ void taskSettings(void *data) {
|
|||
T_TEXTBOX, O(txtServer),
|
||||
T_X, 5, T_WIDTH, 250,
|
||||
T_TITLE, P("Address:"),
|
||||
T_VALUE, P("kangaworld.kangaroopunch.com"),
|
||||
T_VALUE, P(_configData.serverHost),
|
||||
T_TEXTBOX, T_DONE,
|
||||
T_UPDOWN, O(updPort),
|
||||
T_X, 5, T_Y, 30, T_WIDTH, 250,
|
||||
T_TITLE, P(" Port:"),
|
||||
T_VALUE, 16550,
|
||||
T_VALUE, _configData.serverPort,
|
||||
T_MINIMUM, 1,
|
||||
T_MAXIMUM, 65535,
|
||||
T_UPDOWN, T_DONE,
|
||||
|
@ -177,12 +206,23 @@ void taskSettings(void *data) {
|
|||
|
||||
tagListRun(uiSettings);
|
||||
|
||||
// If we found more than one, use the last selected.
|
||||
if (_configData.serialCom > 0 && _configData.serialCom < 4) {
|
||||
if (port[_configData.serialCom - 1].enabled) {
|
||||
for (len=0; len<4; len++) {
|
||||
port[len].selected = 0;
|
||||
}
|
||||
port[_configData.serialCom - 1].selected = 1;
|
||||
}
|
||||
}
|
||||
|
||||
// Add COM discovery to GUI.
|
||||
rc = 0;
|
||||
for (len=0; len<4; len++) {
|
||||
rdoCOM[len] = radioNew(5, rc, port[len].title, GROUP_COM);
|
||||
if (port[len].selected) radioSelectedSet(rdoCOM[len]);
|
||||
widgetEnableSet(W(rdoCOM[len]), port[len].enabled);
|
||||
guiAttach(W(frmComPorts), W(rdoCOM[len]));
|
||||
port[len].rdoCOM = radioNew(5, rc, port[len].title, GROUP_COM);
|
||||
if (port[len].selected) radioSelectedSet(port[len].rdoCOM);
|
||||
widgetEnableSet(W(port[len].rdoCOM), port[len].enabled);
|
||||
guiAttach(W(frmComPorts), W(port[len].rdoCOM));
|
||||
rc += 20;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -19,6 +19,7 @@
|
|||
|
||||
|
||||
#include "memory.h"
|
||||
#include "util.h"
|
||||
|
||||
|
||||
FILE *_memoryLog = NULL;
|
||||
|
@ -47,28 +48,14 @@ void memoryOutput(int c) {
|
|||
|
||||
void memoryShutdown(void) {
|
||||
#ifdef MEMORY_CHECK_ENABLED
|
||||
unlink("memwatch.log"); // It just insists on creating this!
|
||||
mwTerm();
|
||||
#endif
|
||||
}
|
||||
|
||||
|
||||
void memoryStartup(char *appName) {
|
||||
char logName[32] = { "log.log" };
|
||||
char *c = NULL;
|
||||
int16_t x = strlen(appName);
|
||||
|
||||
// Find last portion of filename.
|
||||
while (x > 0) {
|
||||
if (appName[x] == '/' || appName[x] == '\\') break;
|
||||
x--;
|
||||
}
|
||||
if (strlen(appName) - x < 32) {
|
||||
// Replace any extension with ".log"
|
||||
strncpy(logName, &appName[x + 1], 31);
|
||||
c = strstr(logName, ".");
|
||||
if (c) *c = 0;
|
||||
strncat(logName, ".log", 31);
|
||||
}
|
||||
char *logName = utilAppNameWithNewExtensionGet(appName, "log");
|
||||
|
||||
_memoryLog = fopen(logName, "w");
|
||||
|
||||
|
@ -77,4 +64,6 @@ void memoryStartup(char *appName) {
|
|||
mwSetOutFunc(memoryOutput);
|
||||
mwInit();
|
||||
#endif
|
||||
|
||||
free(logName);
|
||||
}
|
||||
|
|
|
@ -30,6 +30,7 @@
|
|||
#include <string.h>
|
||||
#include <stdint.h>
|
||||
#include <stdarg.h>
|
||||
#include <unistd.h>
|
||||
|
||||
|
||||
#ifdef __linux__
|
||||
|
|
51
client/src/system/util.c
Normal file
51
client/src/system/util.c
Normal file
|
@ -0,0 +1,51 @@
|
|||
/*
|
||||
* Kangaroo Punch MultiPlayer Game Server Mark II
|
||||
* Copyright (C) 2020-2021 Scott Duensing
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
*
|
||||
*/
|
||||
|
||||
|
||||
#include "util.h"
|
||||
|
||||
|
||||
char *utilAppNameWithNewExtensionGet(char *appName, char *extension) {
|
||||
char *c = NULL;
|
||||
char *newName = NULL;
|
||||
int16_t x = strlen(appName);
|
||||
uint16_t len = 2 + strlen(extension); // 2 = dot in extension and 0 terminator.
|
||||
|
||||
// Find last portion of filename.
|
||||
while (x > 0) {
|
||||
if (appName[x] == '/' || appName[x] == '\\') break;
|
||||
x--;
|
||||
len++;
|
||||
}
|
||||
|
||||
// We use this + length of new extension for new string length.
|
||||
newName = (char *)malloc(len);
|
||||
if (newName) {
|
||||
if (strlen(appName) - x < len) {
|
||||
// Replace extension
|
||||
strncpy(newName, &appName[x + 1], len - 1);
|
||||
c = strstr(newName, ".");
|
||||
if (c) *c = 0;
|
||||
strncat(newName, ".", len - 1);
|
||||
strncat(newName, extension, len - 1);
|
||||
}
|
||||
}
|
||||
|
||||
return newName;
|
||||
}
|
31
client/src/system/util.h
Normal file
31
client/src/system/util.h
Normal file
|
@ -0,0 +1,31 @@
|
|||
/*
|
||||
* Kangaroo Punch MultiPlayer Game Server Mark II
|
||||
* Copyright (C) 2020-2021 Scott Duensing
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
*
|
||||
*/
|
||||
|
||||
|
||||
#ifndef UTIL_H
|
||||
#define UTIL_H
|
||||
|
||||
|
||||
#include "os.h"
|
||||
|
||||
|
||||
char *utilAppNameWithNewExtensionGet(char *appName, char *extension);
|
||||
|
||||
|
||||
#endif // UTIL_H
|
25
shared/thirdparty/blowfish-api/Makefile
vendored
Normal file
25
shared/thirdparty/blowfish-api/Makefile
vendored
Normal file
|
@ -0,0 +1,25 @@
|
|||
TARGET = blowfish_test
|
||||
LIBS = -lgomp
|
||||
CC = gcc
|
||||
CFLAGS = -std=c99 -Wall -Wextra -Werror -pedantic -O3 -I ./ -fopenmp
|
||||
LDFLAGS =
|
||||
|
||||
.PHONY: default all clean
|
||||
|
||||
default: $(TARGET)
|
||||
all: default
|
||||
|
||||
OBJECTS = $(patsubst %.c, %.o, $(wildcard *.c))
|
||||
HEADERS = $(wildcard *.h)
|
||||
|
||||
%.o: %.c $(HEADERS)
|
||||
$(CC) $(CFLAGS) -c $< -o $@
|
||||
|
||||
.PRECIOUS: $(TARGET) $(OBJECTS)
|
||||
|
||||
$(TARGET): $(OBJECTS)
|
||||
$(CC) $(OBJECTS) $(LIBS) -o $@
|
||||
|
||||
clean:
|
||||
-rm -f *.o
|
||||
-rm -f $(TARGET)
|
4
shared/thirdparty/blowfish-api/README.md
vendored
Normal file
4
shared/thirdparty/blowfish-api/README.md
vendored
Normal file
|
@ -0,0 +1,4 @@
|
|||
blowfish-api
|
||||
============
|
||||
|
||||
Portable, optimised implementation of Bruce Schneier's 64-bit symmetric block cipher, Blowfish. Includes support for multiple block cipher modes, including electronic codebook (ECB), cipher block chaining (CBC), cipher feedback (CFB), output feedback (OFB) and counter (CTR), as well as support for weak key detection and parallelisation using OpenMP.
|
1596
shared/thirdparty/blowfish-api/blowfish.c
vendored
Normal file
1596
shared/thirdparty/blowfish-api/blowfish.c
vendored
Normal file
File diff suppressed because it is too large
Load diff
408
shared/thirdparty/blowfish-api/blowfish.h
vendored
Normal file
408
shared/thirdparty/blowfish-api/blowfish.h
vendored
Normal file
|
@ -0,0 +1,408 @@
|
|||
/**
|
||||
|
||||
@file blowfish.h
|
||||
|
||||
@brief Public interface for Bruce Schneier's 64-bit symmetric block
|
||||
cipher, Blowfish.
|
||||
|
||||
@author Tom Bonner (tom.bonner@gmail.com)
|
||||
|
||||
@date 15-June-2008
|
||||
|
||||
Copyright (c) 2008, Tom Bonner.
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a
|
||||
copy of this software and associated documentation files (the "Software"),
|
||||
to deal in the Software without restriction, including without limitation
|
||||
the rights to use, copy, modify, merge, publish, distribute, sublicense,
|
||||
and/or sell copies of the Software, and to permit persons to whom the
|
||||
Software is furnished to do so, subject to the following conditions:
|
||||
|
||||
The above copyright notice and this permission notice shall be included in
|
||||
all copies or substantial portions of the Software.
|
||||
|
||||
Except as contained in this notice, the name(s) of the above copyright
|
||||
holders shall not be used in advertising or otherwise to promote the sale,
|
||||
use or other dealings in this Software without prior written authorisation.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
|
||||
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
|
||||
DEALINGS IN THE SOFTWARE.
|
||||
|
||||
*/
|
||||
|
||||
#ifndef __BLOWFISH_H__
|
||||
#define __BLOWFISH_H__
|
||||
|
||||
/**
|
||||
|
||||
@ingroup blowfish
|
||||
@defgroup blowfish_api Blowfish API
|
||||
@{
|
||||
|
||||
*/
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C"
|
||||
{
|
||||
#endif
|
||||
|
||||
/* Definitions for fixed sized types required by Blowfish, to be redefined where necessary. */
|
||||
|
||||
typedef unsigned char BLOWFISH_UCHAR; /*!< Must be an 8-bit unsigned type. */
|
||||
typedef BLOWFISH_UCHAR * BLOWFISH_PUCHAR; /*!< Must be a pointer to an 8-bit unsigned type. */
|
||||
typedef const BLOWFISH_UCHAR * BLOWFISH_PCUCHAR; /*!< Must be a pointer to a constant 8-bit unsigned type */
|
||||
|
||||
typedef unsigned int BLOWFISH_ULONG; /*!< Must be a 32-bit unsigned type. */
|
||||
typedef BLOWFISH_ULONG * BLOWFISH_PULONG; /*!< Must be a pointer to a 32-bit unsigned type. */
|
||||
typedef const BLOWFISH_ULONG * BLOWFISH_PCULONG; /*!< Must be a pointer to a constant 32-bit unsigned type. */
|
||||
|
||||
/* Note! Altering the size and/or signedness of BLOWFISH_SIZE_T will affect the amount of data that can be enciphed/deciphered! */
|
||||
|
||||
#ifdef _OPENMP
|
||||
|
||||
typedef signed long BLOWFISH_SIZE_T; /*!< Must be a signed 32 or 64-bit type for use with OpenMP. */
|
||||
|
||||
#else
|
||||
|
||||
typedef unsigned long BLOWFISH_SIZE_T; /*!< Must be an unsigned 32 or 64-bit type. */
|
||||
|
||||
#endif
|
||||
|
||||
/** Blowfish block cipher modes. */
|
||||
|
||||
typedef enum _BLOWFISH_MODE
|
||||
{
|
||||
BLOWFISH_MODE_CURRENT = 0, /*!< For use only with #BLOWFISH_Reset to re-use the mode that the context record was initialised with. */
|
||||
BLOWFISH_MODE_ECB, /*!< Electronic codebook mode. Encipher/Decipher data in 8-byte blocks. This mode is weak at masking repeating patterns, and therefore insecure, but can be parallelised. */
|
||||
BLOWFISH_MODE_CBC, /*!< Cipher block chaining mode (recommended). XOR plaintext block with previous cipher text block before encrypting. This mode cannot be parallelised for encryption. */
|
||||
BLOWFISH_MODE_CFB, /*!< Cipher feedback mode. Plaintext is XOR encrypted with previous block of ciphertext. This mode cannot be parallelised for encryption. */
|
||||
BLOWFISH_MODE_OFB, /*!< Ouput feedback mode. Plaintext is XOR encrypted with enciphered initialisation vector. This mode cannot be parallelised. */
|
||||
BLOWFISH_MODE_CTR /*!< Counter mode. Plaintext is XOR encrypted with enciphered initialisation vector added with a counter. This mode can be parallelised for encryption/decryption. */
|
||||
|
||||
} BLOWFISH_MODE;
|
||||
|
||||
/** Blowfish return codes. */
|
||||
|
||||
typedef enum _BLOWFISH_RC
|
||||
{
|
||||
BLOWFISH_RC_SUCCESS = 0, /*!< Function completed successfully. */
|
||||
BLOWFISH_RC_INVALID_PARAMETER, /*!< One of the parameters suppied to the function is invalid (null pointer). */
|
||||
BLOWFISH_RC_INVALID_KEY, /*!< The length of the key supplied to the #BLOWFISH_Init/#BLOWFISH_Reset function is either greater than #BLOWFISH_MAX_KEY_LENGTH or less than #BLOWFISH_MIN_KEY_LENGTH. */
|
||||
BLOWFISH_RC_WEAK_KEY, /*!< The key supplied to the #BLOWFISH_Init/#BLOWFISH_Reset function has been deemed to be weak, and should not be used. */
|
||||
BLOWFISH_RC_BAD_BUFFER_LENGTH, /*!< The size of the buffer supplied to one of the encipher/decipher buffer/stream functions is not a multiple of 8. */
|
||||
BLOWFISH_RC_INVALID_MODE, /*!< The mode specified to the #BLOWFISH_Init/#BLOWFISH_Reset function is not supported. */
|
||||
BLOWFISH_RC_TEST_FAILED, /*!< Self test failed. For more information see stdout (only used by test applications). */
|
||||
BLOWFISH_RC_ERROR, /*!< Generic error (only used by test applications). */
|
||||
|
||||
} BLOWFISH_RC;
|
||||
|
||||
/* Various static array/buffer lengths (do not modify!). */
|
||||
|
||||
#define BLOWFISH_SUBKEYS 18 /*!< Number of subkeys in the P-Array. */
|
||||
#define BLOWFISH_SBOXES 4 /*!< Number of S-Boxes. */
|
||||
#define BLOWFISH_SBOX_ENTRIES 256 /*!< Number of entries in each S-Box. */
|
||||
|
||||
#define BLOWFISH_MIN_KEY_LENGTH 4 /*!< Maximum length of a key (4-bytes, or 32-bits). */
|
||||
#define BLOWFISH_MAX_KEY_LENGTH 56 /*!< Maximum length of a key (56-bytes, or 448-bits). */
|
||||
|
||||
/** Blowfish context record. */
|
||||
|
||||
typedef struct _BLOWFISH_CONTEXT
|
||||
{
|
||||
BLOWFISH_ULONG PArray [ BLOWFISH_SUBKEYS ]; /*!< Original P-Array which has been XOR'd with the key, and overwritten with output from #BLOWFISH_Encipher. */
|
||||
BLOWFISH_ULONG SBox [ BLOWFISH_SBOXES ] [ BLOWFISH_SBOX_ENTRIES ]; /*!< Original S-Boxes which have been overwritten with output from #BLOWFISH_Encipher. */
|
||||
BLOWFISH_ULONG OriginalIvHigh32; /*!< Original high 32-bytes of the initialisation vector. */
|
||||
BLOWFISH_ULONG OriginalIvLow32; /*!< Original low 32-bytes of the initialisation vector. */
|
||||
BLOWFISH_ULONG IvHigh32; /*!< Current high 32-bytes of the initialisation vector (used for stream operations). */
|
||||
BLOWFISH_ULONG IvLow32; /*!< Current low 32-bytes of the initialisation vector (used for stream operations). */
|
||||
void ( *EncipherStream ) ( ); /*!< Pointer to a callback function to perform the encipher based on the block cipher mode */
|
||||
void ( *DecipherStream ) ( ); /*!< Pointer to a callback function to perform the decipher based on the block cipher mode */
|
||||
|
||||
} BLOWFISH_CONTEXT, *BLOWFISH_PCONTEXT;
|
||||
|
||||
/* Function prototypes. */
|
||||
|
||||
/**
|
||||
|
||||
Initialise a Blowfish context record.
|
||||
|
||||
@param Context Pointer to a Blowfish context record to initialise.
|
||||
|
||||
@param Key Pointer to a key to use for enciphering/deciphering data.
|
||||
|
||||
@param KeyLength Length of the key, which cannot exceed #BLOWFISH_MAX_KEY_LENGTH bytes (sizeof(#BLOWFISH_CONTEXT::PArray)), or be less than #BLOWFISH_MIN_KEY_LENGTH bytes.
|
||||
|
||||
@param Mode Mode to use when enciphering/decipering blocks. For supported modes see #BLOWFISH_MODE
|
||||
|
||||
@param IvHigh32 High 32-bits of the initialisation vector. Required if the Mode parameter is not #BLOWFISH_MODE_ECB.
|
||||
|
||||
@param IvLow32 Low 32-bits of the initialisation vector. Required if the Mode parameter is not #BLOWFISH_MODE_ECB.
|
||||
|
||||
@remarks For stream based enciphering/deciphering, call #BLOWFISH_BeginStream/#BLOWFISH_EndStream before/afer processing the stream.
|
||||
|
||||
@remarks Operations performed on a blowfish context record are not thread safe. Use #BLOWFISH_CloneContext to create a copy of a context record that can be safely used by another thread.
|
||||
|
||||
@return #BLOWFISH_RC_SUCCESS Initialised context record successfully.
|
||||
|
||||
@return #BLOWFISH_RC_INVALID_PARAMETER Either the context record or key pointer is null.
|
||||
|
||||
@return #BLOWFISH_RC_INVALID_KEY The key is either too short or too long.
|
||||
|
||||
@return #BLOWFISH_RC_WEAK_KEY The key has been deemed to be weak.
|
||||
|
||||
@return #BLOWFISH_RC_INVALID_MODE The specified mode is not supported.
|
||||
|
||||
*/
|
||||
|
||||
BLOWFISH_RC BLOWFISH_Init ( BLOWFISH_PCONTEXT Context, BLOWFISH_PCUCHAR Key, BLOWFISH_SIZE_T KeyLength, BLOWFISH_MODE Mode, BLOWFISH_ULONG IvHigh32, BLOWFISH_ULONG IvLow32 );
|
||||
|
||||
/**
|
||||
|
||||
Reinitialise either the key and/or mode and initialisation vector in a Blowfish context record.
|
||||
|
||||
@param Context Pointer to an initialised Blowfish context record to reinitialise.
|
||||
|
||||
@param Key Pointer to a new key to use for enciphering/deciphering data. (May be null to re-use the current key).
|
||||
|
||||
@param KeyLength Length of the new key, which cannot exceed #BLOWFISH_MAX_KEY_LENGTH bytes (sizeof(#BLOWFISH_CONTEXT::PArray)), or be less than #BLOWFISH_MIN_KEY_LENGTH bytes. (May be 0 if Key is null).
|
||||
|
||||
@param Mode New mode to use when enciphering/decipering blocks. Use #BLOWFISH_MODE_CURRENT to re-use the current mode and initialisation vector. For supported modes see #BLOWFISH_MODE.
|
||||
|
||||
@param IvHigh32 High 32-bits of the new initialisation vector. Required if the Mode parameter is not #BLOWFISH_MODE_ECB or #BLOWFISH_MODE_CURRENT.
|
||||
|
||||
@param IvLow32 Low 32-bits of the new initialisation vector. Required if the Mode parameter is not #BLOWFISH_MODE_ECB or #BLOWFISH_MODE_CURRENT.
|
||||
|
||||
@remarks See #BLOWFISH_Init remarks.
|
||||
|
||||
@return #BLOWFISH_RC_SUCCESS Reinitialised the context record successfully.
|
||||
|
||||
@return #BLOWFISH_RC_INVALID_PARAMETER The context record pointer is null.
|
||||
|
||||
@return #BLOWFISH_RC_INVALID_KEY The key is either too short or too long.
|
||||
|
||||
@return #BLOWFISH_RC_WEAK_KEY The key has been deemed to be weak.
|
||||
|
||||
@return #BLOWFISH_RC_INVALID_MODE The specified mode is not supported.
|
||||
|
||||
*/
|
||||
|
||||
BLOWFISH_RC BLOWFISH_Reset ( BLOWFISH_PCONTEXT Context, BLOWFISH_PCUCHAR Key, BLOWFISH_SIZE_T KeyLength, BLOWFISH_MODE Mode, BLOWFISH_ULONG IvHigh32, BLOWFISH_ULONG IvLow32 );
|
||||
|
||||
/**
|
||||
|
||||
Copy an initialised context record into an uninitialised context record for use in another thread.
|
||||
|
||||
@param InContext Pointer to an initialised context record.
|
||||
|
||||
@param OutContext Pointer to an uninitialised context record.
|
||||
|
||||
@remarks Destroy the cloned context record using #BLOWFISH_Exit.
|
||||
|
||||
@return BLOWFISH_RC_SUCCESS The context record was cloned successfully.
|
||||
|
||||
@return BLOWFISH_RC_INVALID_PARAMETER One of the context record pointers is null.
|
||||
|
||||
*/
|
||||
|
||||
BLOWFISH_RC BLOWFISH_CloneContext ( BLOWFISH_PCONTEXT InContext, BLOWFISH_PCONTEXT OutContext );
|
||||
|
||||
/**
|
||||
|
||||
Clear a blowfish context record.
|
||||
|
||||
@param Context Pointer to an initialised context record to overwrite.
|
||||
|
||||
@remarks Call this function regardless of whether #BLOWFISH_Init succeeds.
|
||||
|
||||
@remarks It is a security risk to not call this function once you have finished enciphering/deciphering data!
|
||||
|
||||
@remarks The context record may not be used again after this call without first calling #BLOWFISH_Init.
|
||||
|
||||
@return #BLOWFISH_RC_SUCCESS The context record was overwritten successfully.
|
||||
|
||||
@return #BLOWFISH_RC_INVALID_PARAMETER The supplied context record pointer is null.
|
||||
|
||||
*/
|
||||
|
||||
BLOWFISH_RC BLOWFISH_Exit ( BLOWFISH_PCONTEXT Context );
|
||||
|
||||
/**
|
||||
|
||||
Initialise a context record for stream based enciphering/deciphering.
|
||||
|
||||
@param Context Pointer to an initialised context record.
|
||||
|
||||
@remarks Before re-using the context record to process another stream, be sure to end and begin a new stream using #BLOWFISH_EndStream and BLOWFISH_BeginStream.
|
||||
|
||||
@remarks After calling BLOWFISH_BeginStream, the context will become corrupt if it is passed to any function other than #BLOWFISH_EncipherStream/#BLOWFISH_DecipherStream before calling #BLOWFISH_EndStream.
|
||||
|
||||
@return #BLOWFISH_RC_SUCCESS The context record was initialised for stream ciphering successfully.
|
||||
|
||||
@return #BLOWFISH_RC_INVALID_PARAMETER The supplied context record pointer is null.
|
||||
|
||||
*/
|
||||
|
||||
BLOWFISH_RC BLOWFISH_BeginStream ( BLOWFISH_PCONTEXT Context );
|
||||
|
||||
/**
|
||||
|
||||
Clear sensitive data from a context record after performing stream based enciphering/deciphering.
|
||||
|
||||
@param Context Pointer to an initialised context record.
|
||||
|
||||
@remarks The context record may be re-used after this call without needing to call #BLOWFISH_Init again.
|
||||
|
||||
@remarks After calling BLOWFISH_EndStream, the context record may be used in non-stream based functions without risking corruption. (See #BLOWFISH_BeginStream remarks)
|
||||
|
||||
@return #BLOWFISH_RC_SUCCESS Sensitive data was cleared from the context record successfully.
|
||||
|
||||
@return #BLOWFISH_RC_INVALID_PARAMETER The supplied context record pointer is null.
|
||||
|
||||
*/
|
||||
|
||||
BLOWFISH_RC BLOWFISH_EndStream ( BLOWFISH_PCONTEXT Context );
|
||||
|
||||
/**
|
||||
|
||||
Encipher an 8-byte block of data.
|
||||
|
||||
@param Context Pointer to an initialised context record.
|
||||
|
||||
@param High32 Pointer to the high 32 bits of data to encipher.
|
||||
|
||||
@param Low32 Pointer to the low 32 bits of data to encipher.
|
||||
|
||||
@remarks It is an unchecked runtime error to supply a null parameter to this function.
|
||||
|
||||
*/
|
||||
|
||||
void BLOWFISH_Encipher ( BLOWFISH_PCONTEXT Context, BLOWFISH_PULONG High32, BLOWFISH_PULONG Low32 );
|
||||
|
||||
/**
|
||||
|
||||
Encipher a buffer of data as part of a stream.
|
||||
|
||||
@param Context Pointer to an initialised context record.
|
||||
|
||||
@param PlainTextStream Pointer to a buffer of data to encipher within the stream.
|
||||
|
||||
@param CipherTextStream Pointer to a buffer within the stream to receive the enciphered data.
|
||||
|
||||
@param StreamLength Length of the plaintext and ciphertext stream buffers. Must be a multiple of 8.
|
||||
|
||||
@remarks The PlainTextStream and CipherTextStream pointers may overlap if the mode used to initialise the context was either #BLOWFISH_MODE_ECB or #BLOWFISH_MODE_CTR.
|
||||
|
||||
@remarks The PlainTextStream and CipherTextStream pointers must point to an offset within the stream that is a multiple of 8.
|
||||
|
||||
@return #BLOWFISH_RC_SUCCESS Successfully enciphered data.
|
||||
|
||||
@return #BLOWFISH_RC_INVALID_PARAMETER Either the context record or one of the stream buffer pointer is null.
|
||||
|
||||
@return #BLOWFISH_RC_BAD_BUFFER_LENGTH The size of the stream buffer is not a multiple of 8.
|
||||
|
||||
*/
|
||||
|
||||
BLOWFISH_RC BLOWFISH_EncipherStream ( BLOWFISH_PCONTEXT Context, BLOWFISH_PCUCHAR PlainTextStream, BLOWFISH_PUCHAR CipherTextStream, BLOWFISH_SIZE_T StreamLength );
|
||||
|
||||
/**
|
||||
|
||||
Encipher a buffer of data.
|
||||
|
||||
@param Context Pointer to an initialised context record.
|
||||
|
||||
@param PlainTextBuffer Pointer to a buffer of data to encipher.
|
||||
|
||||
@param CipherTextBuffer Pointer to a buffer to receive the enciphered data.
|
||||
|
||||
@param BufferLength Length of the plaintext and ciphertext buffers. Must be a multiple of 8.
|
||||
|
||||
@remarks The PlainTextBuffer and CipherTextBuffer pointers may overlap if the mode used to initialise the context was either #BLOWFISH_MODE_ECB or #BLOWFISH_MODE_CTR.
|
||||
|
||||
@return #BLOWFISH_RC_SUCCESS Successfully enciphered data.
|
||||
|
||||
@return #BLOWFISH_RC_INVALID_PARAMETER Either the context record or one of the buffer pointer is null.
|
||||
|
||||
@return #BLOWFISH_RC_BAD_BUFFER_LENGTH The size of the buffer is not a multiple of 8.
|
||||
|
||||
*/
|
||||
|
||||
BLOWFISH_RC BLOWFISH_EncipherBuffer ( BLOWFISH_PCONTEXT Context, BLOWFISH_PCUCHAR PlainTextBuffer, BLOWFISH_PUCHAR CipherTextBuffer, BLOWFISH_SIZE_T BufferLength );
|
||||
|
||||
/**
|
||||
|
||||
Decipher an 8-byte block of data.
|
||||
|
||||
@param Context Pointer to an initialised context record.
|
||||
|
||||
@param High32 Pointer to the high 32 bits of data to decipher.
|
||||
|
||||
@param Low32 Pointer to the low 32 bits of data to decipher.
|
||||
|
||||
@remarks It is an unchecked runtime error to supply a null parameter to this function.
|
||||
|
||||
*/
|
||||
|
||||
void BLOWFISH_Decipher ( BLOWFISH_PCONTEXT Context, BLOWFISH_PULONG High32, BLOWFISH_PULONG Low32 );
|
||||
|
||||
/**
|
||||
|
||||
Decipher a buffer of data as part of a stream.
|
||||
|
||||
@param Context Pointer to an initialised context record.
|
||||
|
||||
@param CipherTextStream Pointer to a buffer of data to decipher within the stream.
|
||||
|
||||
@param PlainTextStream Pointer to a buffer within the stream to receive the deciphered data.
|
||||
|
||||
@param StreamLength Length of the plaintext and ciphertext stream buffers. Must be a multiple of 8.
|
||||
|
||||
@remarks The PlainTextStream and CipherTextStream pointers may overlap if the mode used to initialise the context was either #BLOWFISH_MODE_ECB or #BLOWFISH_MODE_CTR.
|
||||
|
||||
@remarks The PlainTextStream and CipherTextStream pointers must point to an offset within the stream that is a multiple of 8.
|
||||
|
||||
@return #BLOWFISH_RC_SUCCESS Successfully enciphered data.
|
||||
|
||||
@return #BLOWFISH_RC_INVALID_PARAMETER Either the context record or one of the stream buffer pointer is null.
|
||||
|
||||
@return #BLOWFISH_RC_BAD_BUFFER_LENGTH The size of the stream buffer is not a multiple of 8.
|
||||
|
||||
*/
|
||||
|
||||
BLOWFISH_RC BLOWFISH_DecipherStream ( BLOWFISH_PCONTEXT Context, BLOWFISH_PCUCHAR CipherTextStream, BLOWFISH_PUCHAR PlainTextStream, BLOWFISH_SIZE_T StreamLength );
|
||||
|
||||
/**
|
||||
|
||||
Decipher a buffer of data.
|
||||
|
||||
@param Context Pointer to an initialised context record.
|
||||
|
||||
@param CipherTextBuffer Pointer to a buffer of data to decipher.
|
||||
|
||||
@param PlainTextBuffer Pointer to a buffer to receive the deciphered data.
|
||||
|
||||
@param BufferLength Length of the ciphertext and plaintext buffers. Must be a multiple of 8.
|
||||
|
||||
@remarks The PlainTextBuffer and CipherTextBuffer pointers may overlap if the mode used to initialise the context was either #BLOWFISH_MODE_ECB or #BLOWFISH_MODE_CTR.
|
||||
|
||||
@return #BLOWFISH_RC_SUCCESS Successfully enciphered data.
|
||||
|
||||
@return #BLOWFISH_RC_INVALID_PARAMETER Either the context record or one of the buffer pointer is null.
|
||||
|
||||
@return #BLOWFISH_RC_BAD_BUFFER_LENGTH The size of the buffer is not a multiple of 8.
|
||||
|
||||
*/
|
||||
|
||||
BLOWFISH_RC BLOWFISH_DecipherBuffer ( BLOWFISH_PCONTEXT Context, BLOWFISH_PCUCHAR CipherTextBuffer, BLOWFISH_PUCHAR PlainTextBuffer, BLOWFISH_SIZE_T BufferLength );
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
/** @} */
|
||||
|
||||
#endif /* __BLOWFISH_H__ */
|
891
shared/thirdparty/blowfish-api/blowfish_test.C
vendored
Normal file
891
shared/thirdparty/blowfish-api/blowfish_test.C
vendored
Normal file
|
@ -0,0 +1,891 @@
|
|||
/**
|
||||
|
||||
@file blowfish_test.c
|
||||
|
||||
@brief Blowfish self-test application. Includes standard ECB mode
|
||||
tests and throughput tests (parallel and serial) for all
|
||||
supported modes (ECB, CBC, CFB, OFB and CTR).
|
||||
|
||||
@author Tom Bonner (tom.bonner@gmail.com)
|
||||
|
||||
@date 22-June-2008
|
||||
|
||||
Copyright (c) 2008, Tom Bonner.
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a
|
||||
copy of this software and associated documentation files (the "Software"),
|
||||
to deal in the Software without restriction, including without limitation
|
||||
the rights to use, copy, modify, merge, publish, distribute, sublicense,
|
||||
and/or sell copies of the Software, and to permit persons to whom the
|
||||
Software is furnished to do so, subject to the following conditions:
|
||||
|
||||
The above copyright notice and this permission notice shall be included in
|
||||
all copies or substantial portions of the Software.
|
||||
|
||||
Except as contained in this notice, the name(s) of the above copyright
|
||||
holders shall not be used in advertising or otherwise to promote the sale,
|
||||
use or other dealings in this Software without prior written authorisation.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
|
||||
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
|
||||
DEALINGS IN THE SOFTWARE.
|
||||
|
||||
*/
|
||||
|
||||
#include <stdio.h>
|
||||
#include <time.h>
|
||||
#include <malloc.h>
|
||||
#include <memory.h>
|
||||
|
||||
#ifdef _OPENMP
|
||||
|
||||
#include <omp.h>
|
||||
|
||||
#endif
|
||||
|
||||
#include <blowfish.h>
|
||||
|
||||
/**
|
||||
|
||||
@ingroup blowfish
|
||||
@defgroup blowfish_selftest Blowfish Self-Test
|
||||
@{
|
||||
|
||||
*/
|
||||
|
||||
/** @internal Test vector. */
|
||||
|
||||
typedef struct __BLOWFISH_TEST_VECTOR
|
||||
{
|
||||
BLOWFISH_UCHAR Key [ 8 ]; /*!< 8-Byte key to use in the test. */
|
||||
BLOWFISH_ULONG PlainText [ 2 ]; /*!< 8-Byte block of plaintext to encipher. */
|
||||
BLOWFISH_ULONG CipherText [ 2 ]; /*!< 8-Byte block of expected ciphertext. */
|
||||
|
||||
} _BLOWFISH_TEST_VECTOR;
|
||||
|
||||
/** @internal ECB Test vector data from http://www.mirrors.wiretapped.net/security/cryptography/algorithms/blowfish/blowfish-TESTVECTORS.txt */
|
||||
|
||||
static const _BLOWFISH_TEST_VECTOR _BLOWFISH_EcbTv1 [ ] =
|
||||
{
|
||||
{ { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 }, { 0x00000000, 0x00000000 }, { 0x4ef99745, 0x6198dd78 } },
|
||||
{ { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff }, { 0xffffffff, 0xffffffff }, { 0x51866fd5, 0xb85ecb8a } },
|
||||
{ { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff }, { 0x00000000, 0x00000000 }, { 0xf21e9a77, 0xb71c49bc } },
|
||||
{ { 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11 }, { 0x01234567, 0x89abcdef }, { 0x7d0cc630, 0xafda1ec7 } },
|
||||
{ { 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11 }, { 0x11111111, 0x11111111 }, { 0x2466dd87, 0x8b963c9d } },
|
||||
{ { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 }, { 0xffffffff, 0xffffffff }, { 0x014933e0, 0xcdaff6e4 } },
|
||||
{ { 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01 }, { 0x01234567, 0x89abcdef }, { 0xfa34ec48, 0x47b268b2 } },
|
||||
{ { 0x1f, 0x1f, 0x1f, 0x1f, 0x0e, 0x0e, 0x0e, 0x0e }, { 0x01234567, 0x89abcdef }, { 0xa7907951, 0x08ea3cae } },
|
||||
{ { 0x30, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 }, { 0x10000000, 0x00000001 }, { 0x7d856f9a, 0x613063f2 } },
|
||||
{ { 0x01, 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef }, { 0x11111111, 0x11111111 }, { 0x61f9c380, 0x2281b096 } },
|
||||
{ { 0xfe, 0xdc, 0xba, 0x98, 0x76, 0x54, 0x32, 0x10 }, { 0x01234567, 0x89abcdef }, { 0x0aceab0f, 0xc6a0a28d } },
|
||||
{ { 0x7c, 0xa1, 0x10, 0x45, 0x4a, 0x1a, 0x6e, 0x57 }, { 0x01a1d6d0, 0x39776742 }, { 0x59c68245, 0xeb05282b } },
|
||||
{ { 0x01, 0x31, 0xd9, 0x61, 0x9d, 0xc1, 0x37, 0x6e }, { 0x5cd54ca8, 0x3def57da }, { 0xb1b8cc0b, 0x250f09a0 } },
|
||||
{ { 0x07, 0xa1, 0x13, 0x3e, 0x4a, 0x0b, 0x26, 0x86 }, { 0x0248d438, 0x06f67172 }, { 0x1730e577, 0x8bea1da4 } },
|
||||
{ { 0x38, 0x49, 0x67, 0x4c, 0x26, 0x02, 0x31, 0x9e }, { 0x51454b58, 0x2ddf440a }, { 0xa25e7856, 0xcf2651eb } },
|
||||
{ { 0x04, 0xb9, 0x15, 0xba, 0x43, 0xfe, 0xb5, 0xb6 }, { 0x42fd4430, 0x59577fa2 }, { 0x353882b1, 0x09ce8f1a } },
|
||||
{ { 0x01, 0x13, 0xb9, 0x70, 0xfd, 0x34, 0xf2, 0xce }, { 0x059b5e08, 0x51cf143a }, { 0x48f4d088, 0x4c379918 } },
|
||||
{ { 0x01, 0x70, 0xf1, 0x75, 0x46, 0x8f, 0xb5, 0xe6 }, { 0x0756d8e0, 0x774761d2 }, { 0x432193b7, 0x8951fc98 } },
|
||||
{ { 0x43, 0x29, 0x7f, 0xad, 0x38, 0xe3, 0x73, 0xfe }, { 0x762514b8, 0x29bf486a }, { 0x13f04154, 0xd69d1ae5 } },
|
||||
{ { 0x07, 0xa7, 0x13, 0x70, 0x45, 0xda, 0x2a, 0x16 }, { 0x3bdd1190, 0x49372802 }, { 0x2eedda93, 0xffd39c79 } },
|
||||
{ { 0x04, 0x68, 0x91, 0x04, 0xc2, 0xfd, 0x3b, 0x2f }, { 0x26955f68, 0x35af609a }, { 0xd887e039, 0x3c2da6e3 } },
|
||||
{ { 0x37, 0xd0, 0x6b, 0xb5, 0x16, 0xcb, 0x75, 0x46 }, { 0x164d5e40, 0x4f275232 }, { 0x5f99d04f, 0x5b163969 } },
|
||||
{ { 0x1f, 0x08, 0x26, 0x0d, 0x1a, 0xc2, 0x46, 0x5e }, { 0x6b056e18, 0x759f5cca }, { 0x4a057a3b, 0x24d3977b } },
|
||||
{ { 0x58, 0x40, 0x23, 0x64, 0x1a, 0xba, 0x61, 0x76 }, { 0x004bd6ef, 0x09176062 }, { 0x452031c1, 0xe4fada8e } },
|
||||
{ { 0x02, 0x58, 0x16, 0x16, 0x46, 0x29, 0xb0, 0x07 }, { 0x480d3900, 0x6ee762f2 }, { 0x7555ae39, 0xf59b87bd } },
|
||||
{ { 0x49, 0x79, 0x3e, 0xbc, 0x79, 0xb3, 0x25, 0x8f }, { 0x437540c8, 0x698f3cfa }, { 0x53c55f9c, 0xb49fc019 } },
|
||||
{ { 0x4f, 0xb0, 0x5e, 0x15, 0x15, 0xab, 0x73, 0xa7 }, { 0x072d43a0, 0x77075292 }, { 0x7a8e7bfa, 0x937e89a3 } },
|
||||
{ { 0x49, 0xe9, 0x5d, 0x6d, 0x4c, 0xa2, 0x29, 0xbf }, { 0x02fe5577, 0x8117f12a }, { 0xcf9c5d7a, 0x4986adb5 } },
|
||||
{ { 0x01, 0x83, 0x10, 0xdc, 0x40, 0x9b, 0x26, 0xd6 }, { 0x1d9d5c50, 0x18f728c2 }, { 0xd1abb290, 0x658bc778 } },
|
||||
{ { 0x1c, 0x58, 0x7f, 0x1c, 0x13, 0x92, 0x4f, 0xef }, { 0x30553228, 0x6d6f295a }, { 0x55cb3774, 0xd13ef201 } },
|
||||
{ { 0xe0, 0xfe, 0xe0, 0xfe, 0xf1, 0xfe, 0xf1, 0xfe }, { 0x01234567, 0x89abcdef }, { 0xc39e072d, 0x9fac631d } },
|
||||
{ { 0x01, 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef }, { 0x00000000, 0x00000000 }, { 0x24594688, 0x5754369a } },
|
||||
{ { 0xfe, 0xdc, 0xba, 0x98, 0x76, 0x54, 0x32, 0x10 }, { 0xffffffff, 0xffffffff }, { 0x6b5c5a9c, 0x5d9e0a5a } }
|
||||
};
|
||||
|
||||
/** @internal Part of ECB Test vector. See #_BLOWFISH_EcbTv1. */
|
||||
|
||||
static const BLOWFISH_UCHAR _BLOWFISH_EcbTv2Key [ ] = { 0xf0, 0xe1, 0xd2, 0xc3, 0xb4, 0xa5, 0x96, 0x87, 0x78, 0x69, 0x5a, 0x4b, 0x3c, 0x2d, 0x1e, 0x0f, 0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77 };
|
||||
|
||||
/** @internal Part of ECB Test vector. See #_BLOWFISH_EcbTv1. */
|
||||
|
||||
static const BLOWFISH_ULONG _BLOWFISH_EcbTv2PlainText [ 2 ] = { 0xfedcba98, 0x76543210 };
|
||||
|
||||
/** @internal Part of ECB Test vector. See #_BLOWFISH_EcbTv1. */
|
||||
|
||||
static const BLOWFISH_ULONG _BLOWFISH_EcbTv2CipherText [ ] [ 2 ] =
|
||||
{
|
||||
{ 0xbe1e6394, 0x08640f05 }, { 0xb39e4448, 0x1bdb1e6e }, { 0x9457aa83, 0xb1928c0d },
|
||||
{ 0x8bb77032, 0xf960629d }, { 0xe87a244e, 0x2cc85e82 }, { 0x15750e7a, 0x4f4ec577 },
|
||||
{ 0x122ba70b, 0x3ab64ae0 }, { 0x3a833c9a, 0xffc537f6 }, { 0x9409da87, 0xa90f6bf2 },
|
||||
{ 0x884f8062, 0x5060b8b4 }, { 0x1f85031c, 0x19e11968 }, { 0x79d9373a, 0x714ca34f },
|
||||
{ 0x93142887, 0xee3be15c }, { 0x03429e83, 0x8ce2d14b }, { 0xa4299e27, 0x469ff67b },
|
||||
{ 0xafd5aed1, 0xc1bc96a8 }, { 0x10851c0e, 0x3858da9f }, { 0xe6f51ed7, 0x9b9db21f },
|
||||
{ 0x64a6e14a, 0xfd36b46f }, { 0x80c7d7d4, 0x5a5479ad }, { 0x05044b62, 0xfa52d080 }
|
||||
};
|
||||
|
||||
/** @internal Part of CBC/CFB/OFB Test vector. See #_BLOWFISH_EcbTv1. */
|
||||
|
||||
static const BLOWFISH_UCHAR _BLOWFISH_Tv3Key [ ] = { 0x01, 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef, 0xf0, 0xe1, 0xd2, 0xc3, 0xb4, 0xa5, 0x96, 0x87 };
|
||||
|
||||
/** @internal Part of CBC/CFB/OFB Test vector. See #_BLOWFISH_EcbTv1. */
|
||||
|
||||
static const BLOWFISH_ULONG _BLOWFISH_Tv3Iv [ 2 ] = { 0xfedcba98, 0x76543210 };
|
||||
|
||||
/** @internal Part of CBC/CFB/OFB Test vector. See #_BLOWFISH_EcbTv1. */
|
||||
|
||||
static const BLOWFISH_ULONG _BLOWFISH_Tv3PlainText [ 6 ] = { 0x37363534, 0x33323120, 0x4e6f7720, 0x69732074, 0x68652074, 0x696d6520 };
|
||||
|
||||
/** @internal Part of CBC/CFB/OFB Test vector. See #_BLOWFISH_EcbTv1. */
|
||||
|
||||
static const BLOWFISH_ULONG _BLOWFISH_Tv3CipherText [ ] [ 6 ] =
|
||||
{
|
||||
{ 0x6b77b4d6, 0x3006dee6, 0x05b156e2, 0x74039793, 0x58deb9e7, 0x154616d9 }, /*!< CBC mode ciphertext. */
|
||||
{ 0xe73214a2, 0x822139ca, 0xf26ecf6d, 0x2eb9e76e, 0x3da3de04, 0xd1517200 }, /*!< CFB mode ciphertext. */
|
||||
{ 0xe73214a2, 0x822139ca, 0x62b343cc, 0x5b655873, 0x10dd908d, 0x0c241b22 } /*!< OFB mode ciphertext. */
|
||||
};
|
||||
|
||||
/** @internal CBC/CFB/OFB Test modes. */
|
||||
|
||||
static const BLOWFISH_MODE _BLOWFISH_Tv3Mode [ ] = { BLOWFISH_MODE_CBC, BLOWFISH_MODE_CFB, BLOWFISH_MODE_OFB };
|
||||
|
||||
/** @internal Throughput test vector */
|
||||
|
||||
typedef struct __BLOWFISH_THROUGHPUT_TEST
|
||||
{
|
||||
BLOWFISH_MODE Mode; /*!< Mode to use in the test. */
|
||||
BLOWFISH_UCHAR Parallel; /*!< Specifies whether the mode can be parallelised. */
|
||||
|
||||
} _BLOWFISH_THROUGHPUT_TEST;
|
||||
|
||||
/** @internal Throughput test modes */
|
||||
|
||||
static const _BLOWFISH_THROUGHPUT_TEST _BLOWFISH_ThroughputTv [ ] = { { BLOWFISH_MODE_ECB, 0x01 }, { BLOWFISH_MODE_CBC, 0x01 }, { BLOWFISH_MODE_CFB, 0x01 }, { BLOWFISH_MODE_OFB, 0x00 }, { BLOWFISH_MODE_CTR, 0x01 } };
|
||||
|
||||
/** @internal Specifies the duration (in seconds) for how long the throughput tests should run (must be greater than 1, and preferably a multiple of 2) */
|
||||
|
||||
#define _BLOWFISH_THROUGHPUT_DURATION 10
|
||||
|
||||
#define _BLOWFISH_THROUGHPUT_STREAM_LENGTH ( 128 * 1024 )
|
||||
|
||||
/**
|
||||
|
||||
@internal
|
||||
|
||||
Display function name and readable return code to stdout.
|
||||
|
||||
@param FunctionName Name of the function called.
|
||||
|
||||
@param ReturnCode Return code from the function.
|
||||
|
||||
@return Return code from printf().
|
||||
|
||||
*/
|
||||
|
||||
static int _BLOWFISH_PrintReturnCode ( char * FunctionName, BLOWFISH_RC ReturnCode )
|
||||
{
|
||||
switch ( ReturnCode )
|
||||
{
|
||||
case BLOWFISH_RC_SUCCESS:
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
case BLOWFISH_RC_INVALID_PARAMETER:
|
||||
{
|
||||
return printf ( "%s()=Invalid parameter!\n", FunctionName );
|
||||
}
|
||||
case BLOWFISH_RC_INVALID_KEY:
|
||||
{
|
||||
return printf ( "%s()=Invalid key!\n", FunctionName );
|
||||
}
|
||||
case BLOWFISH_RC_WEAK_KEY:
|
||||
{
|
||||
return printf ( "%s()=Weak key!\n", FunctionName );
|
||||
}
|
||||
case BLOWFISH_RC_BAD_BUFFER_LENGTH:
|
||||
{
|
||||
return printf ( "%s()=Invalid buffer length!\n", FunctionName );
|
||||
}
|
||||
case BLOWFISH_RC_INVALID_MODE:
|
||||
{
|
||||
return printf ( "%s()=Invalid mode!\n", FunctionName );
|
||||
}
|
||||
case BLOWFISH_RC_TEST_FAILED:
|
||||
{
|
||||
return printf ( "%s()=Self-test failed!\n", FunctionName );
|
||||
}
|
||||
default:
|
||||
{
|
||||
return printf ( "%s()=Unknown error!\n", FunctionName );
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@internal
|
||||
|
||||
Display the name of the specified mode to stdout.
|
||||
|
||||
@param Mode Mode to display.
|
||||
|
||||
@return Return code from printf().
|
||||
|
||||
*/
|
||||
|
||||
static int _BLOWFISH_PrintMode ( BLOWFISH_MODE Mode )
|
||||
{
|
||||
switch ( Mode )
|
||||
{
|
||||
case BLOWFISH_MODE_ECB:
|
||||
{
|
||||
return printf ( "Mode=Electronic codebook (ECB)\n" );
|
||||
}
|
||||
case BLOWFISH_MODE_CBC:
|
||||
{
|
||||
return printf ( "Mode=Cipher block chaining (CBC)\n" );
|
||||
}
|
||||
case BLOWFISH_MODE_CFB:
|
||||
{
|
||||
return printf ( "Mode=Cipher feedback (CFB)\n" );
|
||||
}
|
||||
case BLOWFISH_MODE_OFB:
|
||||
{
|
||||
return printf ( "Mode=Output feedback (OFB)\n" );
|
||||
}
|
||||
case BLOWFISH_MODE_CTR:
|
||||
{
|
||||
return printf ( "Mode=Counter (CTR)\n" );
|
||||
}
|
||||
default:
|
||||
{
|
||||
return printf ( "Mode=Invalid!\n" );
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@internal
|
||||
|
||||
Display a buffer as hex to stdout.
|
||||
|
||||
@param Name Name of the buffer.
|
||||
|
||||
@param Buffer Pointer to a buffer of data to display.
|
||||
|
||||
@param Buffer Length of the buffer.
|
||||
|
||||
*/
|
||||
|
||||
static void _BLOWFISH_PrintBuffer ( char * Name, BLOWFISH_PUCHAR Buffer, BLOWFISH_ULONG BufferLength )
|
||||
{
|
||||
BLOWFISH_ULONG i;
|
||||
|
||||
printf ( "%s=0x", Name );
|
||||
|
||||
for ( i = 0; i < BufferLength; i++ )
|
||||
{
|
||||
printf ( "%02x", Buffer [ i ] );
|
||||
}
|
||||
|
||||
printf ( " (%d bytes)\n", (int)BufferLength );
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@internal
|
||||
|
||||
Perform a raw ECB mode encipher/decipher on an 8-byte block of plaintext, and verify results.
|
||||
|
||||
@param Key Pointer to a buffer containing the key to use for cipher operations.
|
||||
|
||||
@param KeyLength Length of the key buffer.
|
||||
|
||||
@param PlainTextHigh32 High 32-bits of plaintext to encipher.
|
||||
|
||||
@param PlainTextLow32 Low 32-bits of plaintext to encipher.
|
||||
|
||||
@param CipherTextHigh32 High 32 bits of expected ciphertext.
|
||||
|
||||
@param CipherTextLow32 Low 32 bits of expected ciphertext.
|
||||
|
||||
@remarks For use with test vectors 1 and 2.
|
||||
|
||||
@return #BLOWFISH_RC_SUCCESS Test passed successfully.
|
||||
|
||||
@return Specific return code, see #BLOWFISH_RC.
|
||||
|
||||
*/
|
||||
|
||||
static BLOWFISH_RC _BLOWFISH_Test_ECB ( BLOWFISH_PUCHAR Key, BLOWFISH_ULONG KeyLength, BLOWFISH_ULONG PlainTextHigh32, BLOWFISH_ULONG PlainTextLow32, BLOWFISH_ULONG CipherTextHigh32, BLOWFISH_ULONG CipherTextLow32 )
|
||||
{
|
||||
BLOWFISH_RC ReturnCode = BLOWFISH_RC_SUCCESS;
|
||||
BLOWFISH_CONTEXT Context;
|
||||
BLOWFISH_ULONG XLeft = PlainTextHigh32;
|
||||
BLOWFISH_ULONG XRight = PlainTextLow32;
|
||||
|
||||
/* Initialise blowfish */
|
||||
|
||||
ReturnCode = BLOWFISH_Init ( &Context, Key, KeyLength, BLOWFISH_MODE_ECB, 0, 0 );
|
||||
|
||||
_BLOWFISH_PrintReturnCode ( "BLOWFISH_Init", ReturnCode );
|
||||
|
||||
/* Print key information */
|
||||
|
||||
_BLOWFISH_PrintMode ( BLOWFISH_MODE_ECB );
|
||||
|
||||
_BLOWFISH_PrintBuffer ( "Key", Key, KeyLength );
|
||||
|
||||
if ( ReturnCode == BLOWFISH_RC_SUCCESS )
|
||||
{
|
||||
/* Encipher the 8-byte block of plaintext */
|
||||
|
||||
BLOWFISH_Encipher ( &Context, &XLeft, &XRight );
|
||||
|
||||
printf ( "Plaintext=0x%08x%08x (8 bytes)\n", (unsigned int)PlainTextHigh32, (unsigned int)PlainTextLow32 );
|
||||
|
||||
/* Is the ciphertext as expected? */
|
||||
|
||||
if ( XLeft == CipherTextHigh32 && XRight == CipherTextLow32 )
|
||||
{
|
||||
/* Decipher the ciphertext */
|
||||
|
||||
BLOWFISH_Decipher ( &Context, &XLeft, &XRight );
|
||||
|
||||
printf ( "Ciphertext=0x%08x%08x (8 bytes)\n", (unsigned int)CipherTextHigh32, (unsigned int)CipherTextLow32 );
|
||||
|
||||
/* Is the plaintext as expected? */
|
||||
|
||||
if ( XLeft != PlainTextHigh32 && XRight != PlainTextLow32 )
|
||||
{
|
||||
/* Failed to decipher properly */
|
||||
|
||||
_BLOWFISH_PrintReturnCode ( "BLOWFISH_Decipher", BLOWFISH_RC_TEST_FAILED );
|
||||
|
||||
printf ( "Invalid plaintext=0x%08x%08x (8 bytes)\n", (unsigned int)XLeft, (unsigned int)XRight );
|
||||
|
||||
ReturnCode = BLOWFISH_RC_TEST_FAILED;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
/* Failed to encipher properly */
|
||||
|
||||
_BLOWFISH_PrintReturnCode ( "BLOWFISH_Encipher", BLOWFISH_RC_TEST_FAILED );
|
||||
|
||||
printf ( "Ciperhtext=0x%08x%08x (8 bytes)\nInvalid ciphertext=0x%08x%08x (8 bytes)\n", (unsigned int)CipherTextHigh32, (unsigned int)CipherTextLow32, (unsigned int)XLeft, (unsigned int)XRight );
|
||||
|
||||
ReturnCode = BLOWFISH_RC_TEST_FAILED;
|
||||
}
|
||||
}
|
||||
|
||||
printf ( "\n" );
|
||||
|
||||
/* Overwrite the blowfish context record */
|
||||
|
||||
BLOWFISH_Exit ( &Context );
|
||||
|
||||
return ReturnCode;
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@internal
|
||||
|
||||
Perform a CBC/CFB/OFB mode test on a buffer of data, and verify results.
|
||||
|
||||
@param Key Pointer to a buffer containing the key to use for cipher operations.
|
||||
|
||||
@param KeyLength Length of the key buffer.
|
||||
|
||||
@param Mode Mode with which to run the test. Must be either #BLOWFISH_MODE_CBC, #BLOWFISH_MODE_CFB or #BLOWFISH_MODE_OFB.
|
||||
|
||||
@param PlainTextBuffer Pointer to a buffer of plaintext to encipher.
|
||||
|
||||
@param CipherTextBuffer Pointer to a buffer of expected ciphertext.
|
||||
|
||||
@param BufferLength Length of the buffers.
|
||||
|
||||
@remarks For use with test vector 3.
|
||||
|
||||
@return #BLOWFISH_RC_SUCCESS Test passed successfully.
|
||||
|
||||
@return Specific return code, see #BLOWFISH_RC.
|
||||
|
||||
*/
|
||||
|
||||
static BLOWFISH_RC _BLOWFISH_Test_CBC_CFB_OFB ( BLOWFISH_PUCHAR Key, BLOWFISH_ULONG KeyLength, BLOWFISH_MODE Mode, BLOWFISH_PUCHAR PlainTextBuffer, BLOWFISH_PUCHAR CipherTextBuffer, BLOWFISH_ULONG BufferLength )
|
||||
{
|
||||
BLOWFISH_RC ReturnCode = BLOWFISH_RC_SUCCESS;
|
||||
BLOWFISH_CONTEXT Context;
|
||||
BLOWFISH_PUCHAR Buffer = 0;
|
||||
|
||||
/* Initialise blowfish */
|
||||
|
||||
ReturnCode = BLOWFISH_Init ( &Context, Key, KeyLength, Mode, _BLOWFISH_Tv3Iv [ 0 ], _BLOWFISH_Tv3Iv [ 1 ] );
|
||||
|
||||
_BLOWFISH_PrintReturnCode ( "BLOWFISH_Init", ReturnCode );
|
||||
|
||||
/* Print key information */
|
||||
|
||||
_BLOWFISH_PrintMode ( Mode );
|
||||
|
||||
_BLOWFISH_PrintBuffer ( "Key", Key, KeyLength );
|
||||
|
||||
_BLOWFISH_PrintBuffer ( "Initialisation vector", (BLOWFISH_PUCHAR)_BLOWFISH_Tv3Iv, 8 );
|
||||
|
||||
if ( ReturnCode == BLOWFISH_RC_SUCCESS )
|
||||
{
|
||||
Buffer = (BLOWFISH_PUCHAR)malloc ( BufferLength );
|
||||
|
||||
if ( Buffer != 0 )
|
||||
{
|
||||
/* Encipher the plaintext buffer */
|
||||
|
||||
ReturnCode = BLOWFISH_EncipherBuffer ( &Context, PlainTextBuffer, Buffer, BufferLength );
|
||||
|
||||
_BLOWFISH_PrintReturnCode ( "BLOWFISH_EncipherBuffer", ReturnCode );
|
||||
|
||||
_BLOWFISH_PrintBuffer ( "Plaintext", PlainTextBuffer, BufferLength );
|
||||
|
||||
if ( ReturnCode == BLOWFISH_RC_SUCCESS )
|
||||
{
|
||||
/* Is the ciphertext as expected? */
|
||||
|
||||
if ( memcmp ( CipherTextBuffer, Buffer, BufferLength ) == 0 )
|
||||
{
|
||||
/* Decipher the ciphertext buffer */
|
||||
|
||||
ReturnCode = BLOWFISH_DecipherBuffer ( &Context, CipherTextBuffer, Buffer, BufferLength );
|
||||
|
||||
_BLOWFISH_PrintReturnCode ( "BLOWFISH_DecipherBuffer", ReturnCode );
|
||||
|
||||
_BLOWFISH_PrintBuffer ( "Ciphertext", CipherTextBuffer, BufferLength );
|
||||
|
||||
if ( ReturnCode == BLOWFISH_RC_SUCCESS )
|
||||
{
|
||||
/* Is the plaintext as expected? */
|
||||
|
||||
if ( memcmp ( PlainTextBuffer, Buffer, BufferLength ) != 0 )
|
||||
{
|
||||
/* Failed to decipher properly */
|
||||
|
||||
_BLOWFISH_PrintBuffer ( "Invalid plaintext", Buffer, BufferLength );
|
||||
|
||||
ReturnCode = BLOWFISH_RC_TEST_FAILED;
|
||||
}
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
/* Failed to encipher properly */
|
||||
|
||||
_BLOWFISH_PrintBuffer ( "Ciphertext", CipherTextBuffer, BufferLength );
|
||||
|
||||
_BLOWFISH_PrintBuffer ( "Invalid ciphertext", Buffer, BufferLength );
|
||||
|
||||
ReturnCode = BLOWFISH_RC_TEST_FAILED;
|
||||
}
|
||||
}
|
||||
|
||||
free ( Buffer );
|
||||
}
|
||||
}
|
||||
|
||||
printf ( "\n" );
|
||||
|
||||
/* Overwrite the blowfish context record */
|
||||
|
||||
BLOWFISH_Exit ( &Context );
|
||||
|
||||
return ReturnCode;
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@internal
|
||||
|
||||
Perform a stream based throughput test for the selected mode.
|
||||
|
||||
@param Mode Mode to use for the test.
|
||||
|
||||
@param StreamBlockSize Size of each chunk of the stream.
|
||||
|
||||
@remarks Enciphers/deciphers data as a stream for #_BLOWFISH_THROUGHPUT_DURATION seconds, and then calculates the throughput.
|
||||
|
||||
@return #BLOWFISH_RC_SUCCESS Test passed successfully.
|
||||
|
||||
@return Specific return code, see #BLOWFISH_RC.
|
||||
|
||||
*/
|
||||
|
||||
static BLOWFISH_RC _BLOWFISH_Test_Throughput ( BLOWFISH_MODE Mode, BLOWFISH_ULONG StreamBlockSize )
|
||||
{
|
||||
BLOWFISH_RC ReturnCode = BLOWFISH_RC_SUCCESS;
|
||||
BLOWFISH_CONTEXT EncipherContext;
|
||||
BLOWFISH_CONTEXT DecipherContext;
|
||||
BLOWFISH_PUCHAR PlainTextBuffer = 0;
|
||||
BLOWFISH_PUCHAR CipherTextBuffer = 0;
|
||||
BLOWFISH_SIZE_T i = 0;
|
||||
BLOWFISH_SIZE_T j = 0;
|
||||
BLOWFISH_ULONG Sum = 0;
|
||||
clock_t StartTime = 0;
|
||||
clock_t EndTime = 0;
|
||||
clock_t ElapsedEncipherTime = 0;
|
||||
clock_t ElapsedDecipherTime = 0;
|
||||
float BlocksProcessed = 0;
|
||||
#ifdef _OPENMP
|
||||
BLOWFISH_SIZE_T Threads = omp_get_max_threads ( );
|
||||
#else
|
||||
BLOWFISH_SIZE_T Threads = 1;
|
||||
#endif
|
||||
|
||||
/* Initialise blowfish for the encipher stream */
|
||||
|
||||
ReturnCode = BLOWFISH_Init ( &EncipherContext, (BLOWFISH_PUCHAR)"0123456789abcdef", 16, Mode, _BLOWFISH_Tv3Iv [ 0 ], _BLOWFISH_Tv3Iv [ 1 ] );
|
||||
|
||||
_BLOWFISH_PrintReturnCode ( "BLOWFISH_Init", ReturnCode );
|
||||
|
||||
_BLOWFISH_PrintMode ( Mode );
|
||||
|
||||
printf ( "Key=\"0123456789abcdef\"\n" );
|
||||
|
||||
if ( Mode != BLOWFISH_MODE_ECB )
|
||||
{
|
||||
_BLOWFISH_PrintBuffer ( "Initialisation vector", (BLOWFISH_PUCHAR)_BLOWFISH_Tv3Iv, 8 );
|
||||
}
|
||||
|
||||
if ( ReturnCode == BLOWFISH_RC_SUCCESS )
|
||||
{
|
||||
/* Clone the context for the decipher stream */
|
||||
|
||||
ReturnCode = BLOWFISH_CloneContext ( &EncipherContext, &DecipherContext );
|
||||
|
||||
_BLOWFISH_PrintReturnCode ( "BLOWFISH_CloneContext", ReturnCode );
|
||||
|
||||
if ( ReturnCode == BLOWFISH_RC_SUCCESS )
|
||||
{
|
||||
/* Allocate the plaintext buffer */
|
||||
|
||||
PlainTextBuffer = (BLOWFISH_PUCHAR)malloc ( StreamBlockSize );
|
||||
|
||||
if ( PlainTextBuffer != 0 )
|
||||
{
|
||||
/* Allocate the ciphertext buffer */
|
||||
|
||||
CipherTextBuffer = (BLOWFISH_PUCHAR)malloc ( StreamBlockSize );
|
||||
|
||||
if ( CipherTextBuffer != 0 )
|
||||
{
|
||||
/* Create original plaintext buffer (use 0x01 for ease of vectorised verification) */
|
||||
|
||||
memset ( PlainTextBuffer, 0x01, StreamBlockSize );
|
||||
|
||||
/* Begin the stream for enciphering */
|
||||
|
||||
ReturnCode = BLOWFISH_BeginStream ( &EncipherContext );
|
||||
|
||||
_BLOWFISH_PrintReturnCode ( "BLOWFISH_BeginStream", ReturnCode );
|
||||
|
||||
if ( ReturnCode == BLOWFISH_RC_SUCCESS )
|
||||
{
|
||||
/* Begin the stream for deciphering */
|
||||
|
||||
ReturnCode = BLOWFISH_BeginStream ( &DecipherContext );
|
||||
|
||||
_BLOWFISH_PrintReturnCode ( "BLOWFISH_BeginStream", ReturnCode );
|
||||
|
||||
if ( ReturnCode == BLOWFISH_RC_SUCCESS )
|
||||
{
|
||||
/* While the total elapsed time (in seconds) has not passed the duration threshold, keep enciphering/deciphering the stream */
|
||||
|
||||
for ( i = 0; ( ( ElapsedEncipherTime + ElapsedDecipherTime ) / Threads ) / CLOCKS_PER_SEC <= _BLOWFISH_THROUGHPUT_DURATION; i++ )
|
||||
{
|
||||
/* Clear the ciphertext buffer */
|
||||
|
||||
memset ( CipherTextBuffer, 0x00, StreamBlockSize );
|
||||
|
||||
/* Encipher the plaintext */
|
||||
|
||||
StartTime = clock ( );
|
||||
|
||||
ReturnCode = BLOWFISH_EncipherStream ( &EncipherContext, PlainTextBuffer, CipherTextBuffer, StreamBlockSize );
|
||||
|
||||
EndTime = clock ( );
|
||||
|
||||
/* Compute the time elapsed enciphering (in milliseconds) */
|
||||
|
||||
ElapsedEncipherTime += ( EndTime - StartTime );
|
||||
|
||||
if ( ReturnCode != BLOWFISH_RC_SUCCESS )
|
||||
{
|
||||
_BLOWFISH_PrintReturnCode ( "BLOWFISH_EncipherStream", ReturnCode );
|
||||
|
||||
break;
|
||||
}
|
||||
|
||||
/* Clear the plaintext buffer */
|
||||
|
||||
memset ( PlainTextBuffer, 0x00, StreamBlockSize );
|
||||
|
||||
/* Decipher the ciphertext */
|
||||
|
||||
StartTime = clock ( );
|
||||
|
||||
ReturnCode = BLOWFISH_DecipherStream ( &DecipherContext, CipherTextBuffer, PlainTextBuffer, StreamBlockSize );
|
||||
|
||||
EndTime = clock ( );
|
||||
|
||||
/* Compute the time elapsed deciphering (in milliseconds) */
|
||||
|
||||
ElapsedDecipherTime += ( EndTime - StartTime );
|
||||
|
||||
if ( ReturnCode != BLOWFISH_RC_SUCCESS )
|
||||
{
|
||||
_BLOWFISH_PrintReturnCode ( "BLOWFISH_DecipherStream", ReturnCode );
|
||||
|
||||
break;
|
||||
}
|
||||
|
||||
/* Verify the integrity of the deciphered plaintext (sum of all bytes should equal the buffer length) */
|
||||
|
||||
for ( j = 0, Sum = 0; j < (BLOWFISH_SIZE_T)StreamBlockSize; j++ )
|
||||
{
|
||||
Sum += PlainTextBuffer [ j ];
|
||||
}
|
||||
|
||||
if ( Sum != StreamBlockSize )
|
||||
{
|
||||
printf ( "BLOWFISH_EncipherStream()/BLOWFISH_DecipherStream()=Integrity check failed!\n" );
|
||||
|
||||
ReturnCode = BLOWFISH_RC_TEST_FAILED;
|
||||
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if ( ReturnCode == BLOWFISH_RC_SUCCESS )
|
||||
{
|
||||
/* Ensure we managed to encipher/decipher enough data to determine the throughput in MB/s */
|
||||
|
||||
if ( ( i * StreamBlockSize ) > ( 1024 * 1024 ) )
|
||||
{
|
||||
/* Adjust elapsed time based on thread count */
|
||||
|
||||
ElapsedEncipherTime = ElapsedEncipherTime / Threads;
|
||||
ElapsedDecipherTime = ElapsedDecipherTime / Threads;
|
||||
|
||||
/* Convert elapsed time from milliseconds to seconds (minimum 1 second) */
|
||||
|
||||
ElapsedEncipherTime = ElapsedEncipherTime / CLOCKS_PER_SEC > 1 ? ElapsedEncipherTime / CLOCKS_PER_SEC : 1;
|
||||
ElapsedDecipherTime = ElapsedDecipherTime / CLOCKS_PER_SEC > 1 ? ElapsedDecipherTime / CLOCKS_PER_SEC : 1;
|
||||
|
||||
/* Calculate and display the stream length */
|
||||
|
||||
BlocksProcessed = (float)( (float)( i * StreamBlockSize ) / ( 1024 * 1024 ) );
|
||||
|
||||
printf ( "Stream length=%0.2f MB (%d*%d byte blocks)\n", BlocksProcessed, (int)i, (int)StreamBlockSize );
|
||||
|
||||
/* Calculate and display throughputs */
|
||||
|
||||
printf ( "Encipher throughput=%0.2f MB/s\n", BlocksProcessed / ElapsedEncipherTime );
|
||||
printf ( "Decipher throughput=%0.2f MB/s\n", BlocksProcessed / ElapsedDecipherTime );
|
||||
printf ( "Average throughput=%0.2f MB/s\n", ( ( BlocksProcessed / ElapsedDecipherTime ) + ( BlocksProcessed / ElapsedEncipherTime ) ) / 2 );
|
||||
}
|
||||
else
|
||||
{
|
||||
printf ( "Failed to process enough data to determine throughput in MB/s!\n" );
|
||||
|
||||
ReturnCode = BLOWFISH_RC_TEST_FAILED;
|
||||
}
|
||||
}
|
||||
|
||||
/* Finished decipering */
|
||||
|
||||
BLOWFISH_EndStream ( &DecipherContext );
|
||||
}
|
||||
|
||||
/* Finished encipering */
|
||||
|
||||
BLOWFISH_EndStream ( &EncipherContext );
|
||||
}
|
||||
|
||||
/* Free the ciphertext */
|
||||
|
||||
free ( CipherTextBuffer );
|
||||
}
|
||||
|
||||
/* Free the plaintext */
|
||||
|
||||
free ( PlainTextBuffer );
|
||||
}
|
||||
}
|
||||
|
||||
/* Overwrite the deciper stream context record */
|
||||
|
||||
BLOWFISH_Exit ( &DecipherContext );
|
||||
}
|
||||
|
||||
printf ( "\n" );
|
||||
|
||||
/* Overwrite the encipher stream context record */
|
||||
|
||||
BLOWFISH_Exit ( &EncipherContext );
|
||||
|
||||
return ReturnCode;
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@internal
|
||||
|
||||
Perform all self-tests for all supported modes.
|
||||
|
||||
@return #BLOWFISH_RC_SUCCESS All tests passed successfully.
|
||||
|
||||
@return Specific return code, see #BLOWFISH_RC.
|
||||
|
||||
*/
|
||||
|
||||
static BLOWFISH_RC _BLOWFISH_SelfTest ( )
|
||||
{
|
||||
BLOWFISH_RC ReturnCode;
|
||||
BLOWFISH_ULONG i = 0;
|
||||
|
||||
printf ( "Standard test vectors...\n\n" );
|
||||
|
||||
/* Perform ECB mode tests on test vector 1 */
|
||||
|
||||
for ( i = 0; i < sizeof ( _BLOWFISH_EcbTv1 ) / sizeof ( _BLOWFISH_EcbTv1 [ 0 ] ); i++ )
|
||||
{
|
||||
ReturnCode = _BLOWFISH_Test_ECB ( (BLOWFISH_PUCHAR)&_BLOWFISH_EcbTv1 [ i ].Key, 8, _BLOWFISH_EcbTv1 [ i ].PlainText [ 0 ], _BLOWFISH_EcbTv1 [ i ].PlainText [ 1 ], _BLOWFISH_EcbTv1 [ i ].CipherText [ 0 ], _BLOWFISH_EcbTv1 [ i ].CipherText [ 1 ] );
|
||||
|
||||
if ( ReturnCode != BLOWFISH_RC_SUCCESS )
|
||||
{
|
||||
return ReturnCode;
|
||||
}
|
||||
}
|
||||
|
||||
/* Perform ECB mode tests on test vector 2 */
|
||||
|
||||
for ( i = 0; i < sizeof ( _BLOWFISH_EcbTv2CipherText ) / sizeof ( _BLOWFISH_EcbTv2CipherText [ 0 ] ); i++ )
|
||||
{
|
||||
ReturnCode = _BLOWFISH_Test_ECB ( (BLOWFISH_PUCHAR)&_BLOWFISH_EcbTv2Key, i + 4, _BLOWFISH_EcbTv2PlainText [ 0 ], _BLOWFISH_EcbTv2PlainText [ 1 ], _BLOWFISH_EcbTv2CipherText [ i ] [ 0 ], _BLOWFISH_EcbTv2CipherText [ i ] [ 1 ] );
|
||||
|
||||
if ( ReturnCode != BLOWFISH_RC_SUCCESS )
|
||||
{
|
||||
return ReturnCode;
|
||||
}
|
||||
}
|
||||
|
||||
/* Perform CBC, CFB and OFB tests on test vector 3 */
|
||||
|
||||
for ( i = 0; i < sizeof ( _BLOWFISH_Tv3Mode ) / sizeof ( _BLOWFISH_Tv3Mode [ 0 ] ); i++ )
|
||||
{
|
||||
ReturnCode = _BLOWFISH_Test_CBC_CFB_OFB ( (BLOWFISH_PUCHAR)&_BLOWFISH_Tv3Key, sizeof ( _BLOWFISH_Tv3Key ), _BLOWFISH_Tv3Mode [ i ], (BLOWFISH_PUCHAR)&_BLOWFISH_Tv3PlainText, (BLOWFISH_PUCHAR)&_BLOWFISH_Tv3CipherText [ i ], sizeof ( _BLOWFISH_Tv3PlainText ) );
|
||||
|
||||
if ( ReturnCode != BLOWFISH_RC_SUCCESS )
|
||||
{
|
||||
return ReturnCode;
|
||||
}
|
||||
}
|
||||
|
||||
#ifdef _OPENMP
|
||||
|
||||
/* Perform parallelised throughput tests if there is more than 1 available thread */
|
||||
|
||||
if ( omp_get_max_threads ( ) > 1 )
|
||||
{
|
||||
printf ( "Parallelised throughput tests (using %d threads for ~%d seconds per mode)...\n\n", omp_get_max_threads ( ), _BLOWFISH_THROUGHPUT_DURATION );
|
||||
|
||||
for ( i = 0; i < sizeof ( _BLOWFISH_ThroughputTv ) / sizeof ( _BLOWFISH_ThroughputTv [ 0 ] ); i++ )
|
||||
{
|
||||
if ( _BLOWFISH_ThroughputTv [ i ].Parallel != 0x00 )
|
||||
{
|
||||
ReturnCode = _BLOWFISH_Test_Throughput ( _BLOWFISH_ThroughputTv [ i ].Mode, _BLOWFISH_THROUGHPUT_STREAM_LENGTH );
|
||||
|
||||
if ( ReturnCode != BLOWFISH_RC_SUCCESS )
|
||||
{
|
||||
return ReturnCode;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/* Use a single thread for the serialised tests */
|
||||
|
||||
omp_set_num_threads ( 1 );
|
||||
|
||||
#endif
|
||||
|
||||
/* Perform serialised throughput tests */
|
||||
|
||||
printf ( "Serialised throughput tests (using 1 thread for ~%d seconds per mode)...\n\n", _BLOWFISH_THROUGHPUT_DURATION );
|
||||
|
||||
for ( i = 0; i < sizeof ( _BLOWFISH_ThroughputTv ) / sizeof ( _BLOWFISH_ThroughputTv [ 0 ] ); i++ )
|
||||
{
|
||||
ReturnCode = _BLOWFISH_Test_Throughput ( _BLOWFISH_ThroughputTv [ i ].Mode, _BLOWFISH_THROUGHPUT_STREAM_LENGTH );
|
||||
|
||||
if ( ReturnCode != BLOWFISH_RC_SUCCESS )
|
||||
{
|
||||
return ReturnCode;
|
||||
}
|
||||
}
|
||||
|
||||
return BLOWFISH_RC_SUCCESS;
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@internal
|
||||
|
||||
Main entry point for the blowfish self-test application.
|
||||
|
||||
@param ArgumentCount Number of command line arguments passed to the application.
|
||||
|
||||
@param ArgumentVector Array of command line arguments passed to the application.
|
||||
|
||||
@return #BLOWFISH_RC_SUCCESS All tests passed successfully.
|
||||
|
||||
@return Specific return code, see #BLOWFISH_RC.
|
||||
|
||||
*/
|
||||
|
||||
int main ( int ArgumentCount, char * ArgumentVector [ ] )
|
||||
{
|
||||
BLOWFISH_RC ReturnCode;
|
||||
|
||||
/* Unreferenced parameters */
|
||||
|
||||
ArgumentCount = ArgumentCount;
|
||||
ArgumentVector = ArgumentVector;
|
||||
|
||||
/* Perform all self tests */
|
||||
|
||||
printf ( "Blowfish selft-test application.\nCopyright (c) 2008, Tom Bonner (tom.bonner@gmail.com)\n\n(For best results close all running applications)\n\n" );
|
||||
|
||||
ReturnCode = _BLOWFISH_SelfTest ( );
|
||||
|
||||
if ( ReturnCode == BLOWFISH_RC_SUCCESS )
|
||||
{
|
||||
/* All self test passed successfully */
|
||||
|
||||
printf ( "All Blowfish self-tests passed successfully!\n" );
|
||||
}
|
||||
else
|
||||
{
|
||||
/* One of the self tests failed */
|
||||
|
||||
printf ( "Blowfish self-test failed!\n" );
|
||||
}
|
||||
|
||||
return (int)ReturnCode;
|
||||
}
|
||||
|
||||
/** @} */
|
20
shared/thirdparty/ini/LICENSE
vendored
Normal file
20
shared/thirdparty/ini/LICENSE
vendored
Normal file
|
@ -0,0 +1,20 @@
|
|||
Copyright (c) 2016 rxi
|
||||
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy of
|
||||
this software and associated documentation files (the "Software"), to deal in
|
||||
the Software without restriction, including without limitation the rights to
|
||||
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
|
||||
of the Software, and to permit persons to whom the Software is furnished to do
|
||||
so, subject to the following conditions:
|
||||
|
||||
The above copyright notice and this permission notice shall be included in all
|
||||
copies or substantial portions of the Software.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
SOFTWARE.
|
63
shared/thirdparty/ini/README.md
vendored
Normal file
63
shared/thirdparty/ini/README.md
vendored
Normal file
|
@ -0,0 +1,63 @@
|
|||
|
||||
# ini
|
||||
A *tiny* ANSI C library for loading .ini config files
|
||||
|
||||
## Usage
|
||||
The files **[ini.c](src/ini.c?raw=1)** and **[ini.h](src/ini.h?raw=1)** should
|
||||
be dropped into an existing project.
|
||||
|
||||
The library has support for sections, comment lines and quoted string values
|
||||
(with escapes). Unquoted values and keys are trimmed of whitespace when loaded.
|
||||
|
||||
```ini
|
||||
; last modified 1 April 2001 by John Doe
|
||||
[owner]
|
||||
name = John Doe
|
||||
organization = Acme Widgets Inc.
|
||||
|
||||
[database]
|
||||
; use IP address in case network name resolution is not working
|
||||
server = 192.0.2.62
|
||||
port = 143
|
||||
file = "payroll.dat"
|
||||
```
|
||||
|
||||
An ini file can be loaded into memory by using the `ini_load()` function.
|
||||
`NULL` is returned if the file cannot be loaded.
|
||||
```c
|
||||
ini_t *config = ini_load("config.ini");
|
||||
```
|
||||
|
||||
The library provides two functions for retrieving values: the first is
|
||||
`ini_get()`. Given a section and a key the corresponding value is returned if
|
||||
it exists. If the `section` argument is `NULL` then all sections are searched.
|
||||
```c
|
||||
const char *name = ini_get(config, "owner", "name");
|
||||
if (name) {
|
||||
printf("name: %s\n", name);
|
||||
}
|
||||
```
|
||||
|
||||
The second, `ini_sget()`, takes the same arguments as `ini_get()` with the
|
||||
addition of a scanf format string and a pointer for where to store the value.
|
||||
```c
|
||||
const char *server = "default";
|
||||
int port = 80;
|
||||
|
||||
ini_sget(config, "database", "server", NULL, &server);
|
||||
ini_sget(config, "database", "port", "%d", &port);
|
||||
|
||||
printf("server: %s:%d\n", server, port);
|
||||
```
|
||||
|
||||
The `ini_free()` function is used to free the memory used by the `ini_t*`
|
||||
object when we are done with it. Calling this function invalidates all string
|
||||
pointers returned by the library.
|
||||
```c
|
||||
ini_free(config);
|
||||
```
|
||||
|
||||
|
||||
## License
|
||||
This library is free software; you can redistribute it and/or modify it under
|
||||
the terms of the MIT license. See [LICENSE](LICENSE) for details.
|
274
shared/thirdparty/ini/src/ini.c
vendored
Normal file
274
shared/thirdparty/ini/src/ini.c
vendored
Normal file
|
@ -0,0 +1,274 @@
|
|||
/**
|
||||
* Copyright (c) 2016 rxi
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
* of this software and associated documentation files (the "Software"), to deal
|
||||
* in the Software without restriction, including without limitation the rights
|
||||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
* copies of the Software, and to permit persons to whom the Software is
|
||||
* furnished to do so, subject to the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be included in
|
||||
* all copies or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
* SOFTWARE.
|
||||
*/
|
||||
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <ctype.h>
|
||||
|
||||
#include "ini.h"
|
||||
|
||||
struct ini_t {
|
||||
char *data;
|
||||
char *end;
|
||||
};
|
||||
|
||||
|
||||
/* Case insensitive string compare */
|
||||
static int strcmpci(const char *a, const char *b) {
|
||||
for (;;) {
|
||||
int d = tolower(*a) - tolower(*b);
|
||||
if (d != 0 || !*a) {
|
||||
return d;
|
||||
}
|
||||
a++, b++;
|
||||
}
|
||||
}
|
||||
|
||||
/* Returns the next string in the split data */
|
||||
static char* next(ini_t *ini, char *p) {
|
||||
p += strlen(p);
|
||||
while (p < ini->end && *p == '\0') {
|
||||
p++;
|
||||
}
|
||||
return p;
|
||||
}
|
||||
|
||||
static void trim_back(ini_t *ini, char *p) {
|
||||
while (p >= ini->data && (*p == ' ' || *p == '\t' || *p == '\r')) {
|
||||
*p-- = '\0';
|
||||
}
|
||||
}
|
||||
|
||||
static char* discard_line(ini_t *ini, char *p) {
|
||||
while (p < ini->end && *p != '\n') {
|
||||
*p++ = '\0';
|
||||
}
|
||||
return p;
|
||||
}
|
||||
|
||||
|
||||
static char *unescape_quoted_value(ini_t *ini, char *p) {
|
||||
/* Use `q` as write-head and `p` as read-head, `p` is always ahead of `q`
|
||||
* as escape sequences are always larger than their resultant data */
|
||||
char *q = p;
|
||||
p++;
|
||||
while (p < ini->end && *p != '"' && *p != '\r' && *p != '\n') {
|
||||
if (*p == '\\') {
|
||||
/* Handle escaped char */
|
||||
p++;
|
||||
switch (*p) {
|
||||
default : *q = *p; break;
|
||||
case 'r' : *q = '\r'; break;
|
||||
case 'n' : *q = '\n'; break;
|
||||
case 't' : *q = '\t'; break;
|
||||
case '\r' :
|
||||
case '\n' :
|
||||
case '\0' : goto end;
|
||||
}
|
||||
|
||||
} else {
|
||||
/* Handle normal char */
|
||||
*q = *p;
|
||||
}
|
||||
q++, p++;
|
||||
}
|
||||
end:
|
||||
return q;
|
||||
}
|
||||
|
||||
|
||||
/* Splits data in place into strings containing section-headers, keys and
|
||||
* values using one or more '\0' as a delimiter. Unescapes quoted values */
|
||||
static void split_data(ini_t *ini) {
|
||||
char *value_start, *line_start;
|
||||
char *p = ini->data;
|
||||
|
||||
while (p < ini->end) {
|
||||
switch (*p) {
|
||||
case '\r':
|
||||
case '\n':
|
||||
case '\t':
|
||||
case ' ':
|
||||
*p = '\0';
|
||||
/* Fall through */
|
||||
|
||||
case '\0':
|
||||
p++;
|
||||
break;
|
||||
|
||||
case '[':
|
||||
p += strcspn(p, "]\n");
|
||||
*p = '\0';
|
||||
break;
|
||||
|
||||
case ';':
|
||||
p = discard_line(ini, p);
|
||||
break;
|
||||
|
||||
default:
|
||||
line_start = p;
|
||||
p += strcspn(p, "=\n");
|
||||
|
||||
/* Is line missing a '='? */
|
||||
if (*p != '=') {
|
||||
p = discard_line(ini, line_start);
|
||||
break;
|
||||
}
|
||||
trim_back(ini, p - 1);
|
||||
|
||||
/* Replace '=' and whitespace after it with '\0' */
|
||||
do {
|
||||
*p++ = '\0';
|
||||
} while (*p == ' ' || *p == '\r' || *p == '\t');
|
||||
|
||||
/* Is a value after '=' missing? */
|
||||
if (*p == '\n' || *p == '\0') {
|
||||
p = discard_line(ini, line_start);
|
||||
break;
|
||||
}
|
||||
|
||||
if (*p == '"') {
|
||||
/* Handle quoted string value */
|
||||
value_start = p;
|
||||
p = unescape_quoted_value(ini, p);
|
||||
|
||||
/* Was the string empty? */
|
||||
if (p == value_start) {
|
||||
p = discard_line(ini, line_start);
|
||||
break;
|
||||
}
|
||||
|
||||
/* Discard the rest of the line after the string value */
|
||||
p = discard_line(ini, p);
|
||||
|
||||
} else {
|
||||
/* Handle normal value */
|
||||
p += strcspn(p, "\n");
|
||||
trim_back(ini, p - 1);
|
||||
}
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
ini_t* ini_load(const char *filename) {
|
||||
ini_t *ini = NULL;
|
||||
FILE *fp = NULL;
|
||||
int n, sz;
|
||||
|
||||
/* Init ini struct */
|
||||
ini = malloc(sizeof(*ini));
|
||||
if (!ini) {
|
||||
goto fail;
|
||||
}
|
||||
memset(ini, 0, sizeof(*ini));
|
||||
|
||||
/* Open file */
|
||||
fp = fopen(filename, "rb");
|
||||
if (!fp) {
|
||||
goto fail;
|
||||
}
|
||||
|
||||
/* Get file size */
|
||||
fseek(fp, 0, SEEK_END);
|
||||
sz = ftell(fp);
|
||||
rewind(fp);
|
||||
|
||||
/* Load file content into memory, null terminate, init end var */
|
||||
ini->data = malloc(sz + 1);
|
||||
ini->data[sz] = '\0';
|
||||
ini->end = ini->data + sz;
|
||||
n = fread(ini->data, 1, sz, fp);
|
||||
if (n != sz) {
|
||||
goto fail;
|
||||
}
|
||||
|
||||
/* Prepare data */
|
||||
split_data(ini);
|
||||
|
||||
/* Clean up and return */
|
||||
fclose(fp);
|
||||
return ini;
|
||||
|
||||
fail:
|
||||
if (fp) fclose(fp);
|
||||
if (ini) ini_free(ini);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
|
||||
void ini_free(ini_t *ini) {
|
||||
free(ini->data);
|
||||
free(ini);
|
||||
}
|
||||
|
||||
|
||||
const char* ini_get(ini_t *ini, const char *section, const char *key) {
|
||||
char *current_section = "";
|
||||
char *val;
|
||||
char *p = ini->data;
|
||||
|
||||
if (*p == '\0') {
|
||||
p = next(ini, p);
|
||||
}
|
||||
|
||||
while (p < ini->end) {
|
||||
if (*p == '[') {
|
||||
/* Handle section */
|
||||
current_section = p + 1;
|
||||
|
||||
} else {
|
||||
/* Handle key */
|
||||
val = next(ini, p);
|
||||
if (!section || !strcmpci(section, current_section)) {
|
||||
if (!strcmpci(p, key)) {
|
||||
return val;
|
||||
}
|
||||
}
|
||||
p = val;
|
||||
}
|
||||
|
||||
p = next(ini, p);
|
||||
}
|
||||
|
||||
return NULL;
|
||||
}
|
||||
|
||||
|
||||
int ini_sget(
|
||||
ini_t *ini, const char *section, const char *key,
|
||||
const char *scanfmt, void *dst
|
||||
) {
|
||||
const char *val = ini_get(ini, section, key);
|
||||
if (!val) {
|
||||
return 0;
|
||||
}
|
||||
if (scanfmt) {
|
||||
sscanf(val, scanfmt, dst);
|
||||
} else {
|
||||
*((const char**) dst) = val;
|
||||
}
|
||||
return 1;
|
||||
}
|
20
shared/thirdparty/ini/src/ini.h
vendored
Normal file
20
shared/thirdparty/ini/src/ini.h
vendored
Normal file
|
@ -0,0 +1,20 @@
|
|||
/**
|
||||
* Copyright (c) 2016 rxi
|
||||
*
|
||||
* This library is free software; you can redistribute it and/or modify it
|
||||
* under the terms of the MIT license. See `ini.c` for details.
|
||||
*/
|
||||
|
||||
#ifndef INI_H
|
||||
#define INI_H
|
||||
|
||||
#define INI_VERSION "0.1.1"
|
||||
|
||||
typedef struct ini_t ini_t;
|
||||
|
||||
ini_t* ini_load(const char *filename);
|
||||
void ini_free(ini_t *ini);
|
||||
const char* ini_get(ini_t *ini, const char *section, const char *key);
|
||||
int ini_sget(ini_t *ini, const char *section, const char *key, const char *scanfmt, void *dst);
|
||||
|
||||
#endif
|
Loading…
Add table
Reference in a new issue