58 Commits

Author SHA1 Message Date
86d87430da Fix socket build on Windows 2016-12-25 12:54:04 +01:00
0156c5a883 Don't allocate watcher queue on the heap 2016-12-25 00:54:05 +01:00
c966b42ac3 Fix FreeBSD build 2016-12-24 22:25:34 +01:00
200fff3714 Fix #1 2016-12-22 22:05:48 +01:00
28755b4d01 Rename module traits into enums 2016-12-22 22:05:06 +01:00
8bd6a14988 Fix issue going out of the range with back() 2016-12-22 22:01:45 +01:00
b41dcc9f37 Fix compatibility issue with dmd 2.071 2016-12-22 22:01:11 +01:00
38addb7a5b Add support for pow for big integers 2016-12-22 21:51:16 +01:00
f7fb89fed0 Move random.d into math submodule 2016-12-22 21:50:33 +01:00
e32af2d09e Add scalar type template parameter for buffers 2016-12-19 21:24:28 +01:00
f1bc4dc2e2 Add length and opCmp to the Queue 2016-12-19 16:33:16 +01:00
40857e69b7 Add capacity capabilities to the vector 2016-12-18 18:48:25 +01:00
c1fb89af99 Implement insertion into the vector 2016-12-15 15:00:06 +01:00
061cd6264b Use auto ref for templated overloaded functions 2016-12-13 10:59:05 +01:00
f437dafa6b Fix dispose for structs 2016-12-13 10:58:11 +01:00
54d0597657 Use resizeArray instead of expand/shrinkArray 2016-12-13 10:57:12 +01:00
ab9f96e0c7 Replace class Queue with the struct Queue 2016-12-13 10:56:29 +01:00
711855474c Remove unused buffer interface 2016-12-13 10:54:27 +01:00
b20f367aa8 Array support for refCounted factory function 2016-12-11 11:42:09 +01:00
a2dadda511 Fix subtraction of numbers with different signs 2016-12-08 18:30:22 +01:00
77dca31261 Add license info 2016-12-08 15:07:58 +01:00
b87aed4395 Add travis to README 2016-12-08 15:00:09 +01:00
42bbb3b023 Add travis 2016-12-08 14:58:59 +01:00
4309a30dfe Add opBinary for the other math operations on Integer 2016-12-08 14:51:49 +01:00
9362287938 Fix error with assignin long numbers to Integer 2016-12-08 14:43:50 +01:00
78bd901339 Add short description of the packages to the README 2016-12-07 23:16:49 +01:00
c8e6d44f7b Implement own dispose 2016-12-07 11:01:51 +01:00
f75433e0e6 Implement operations on negative numbers 2016-12-06 23:22:12 +01:00
fa607141e4 Make allocator shared and fix some RefCounted bugs 2016-12-06 21:29:08 +01:00
b3fdd6fd4a Implement unary operation for multiple precision integers 2016-12-05 22:06:06 +01:00
86c08e7af6 Use RefCounted as math.mp.Integer internal storage 2016-12-04 22:51:21 +01:00
1c5796eb96 Add RefCounted 2016-12-04 14:05:53 +01:00
f7f92e7906 Switch to container.queue. Remove PendingQueue 2016-12-02 19:18:37 +01:00
1123d01e6c Implement opApply for the Queue 2016-12-02 17:31:57 +01:00
c53d319337 Implement foreach_reverse for the vector 2016-12-02 14:12:12 +01:00
7c36dbb8f0 Rename SList front property into insertFront 2016-12-02 10:50:54 +01:00
dd3becf6b7 Implement slicing for the vector 2016-12-02 10:29:30 +01:00
b78ecdf4c5 Make tanya.memory.allocator compatible with dmd < 2.072 2016-12-01 20:04:04 +01:00
a4aa5bcb2e Make opApply delegate scoped for SList 2016-12-01 20:03:21 +01:00
edd3ec4b32 Add URL parser 2016-12-01 20:02:49 +01:00
9fdcef86e7 Replace defaultAllocator with theAllocator 2016-11-30 21:54:31 +01:00
ed0eb4ac74 Fix epoll connection bugs 2016-11-30 21:53:30 +01:00
192ee20bf7 Remove shared from the allocators 2016-11-30 21:20:18 +01:00
965ca0088e Add multiple precision unsigned integer module 2016-11-30 20:24:51 +01:00
b752acdff7 Fix tanya.math module name 2016-11-30 16:48:46 +01:00
cbeb0395f9 Remove @safe from potentially unsafe code 2016-11-30 16:21:20 +01:00
5e6f8446d8 Add an abandoned notice 2016-10-08 20:45:03 +02:00
79a7b840f7 Fix missing semicolons in echo server example 2016-10-08 19:43:40 +02:00
6b093cd5fa Add Windows IOCP and Kqueue implementations for the event loop 2016-10-08 19:33:06 +02:00
154e2f2ff7 Add socket module with overlapped socket support 2016-10-08 19:29:07 +02:00
e9a0a93d3c Add DES block cipher 2016-10-06 06:32:01 +02:00
da5dc276d5 Add BitVector container 2016-10-06 06:31:42 +02:00
721bb110e5 Use dispose from std.experimental 2016-10-05 13:12:50 +02:00
9241ec503c Add Mallocator (malloc allocator) 2016-10-05 13:04:25 +02:00
c2afb07ff6 Rename Ullocator to MmapPool and make it Windows compatible 2016-10-05 13:01:37 +02:00
698660c4c8 Make allocators shared 2016-10-04 18:19:48 +02:00
be9181698a Add remove padding 2016-08-27 07:49:43 +02:00
25c292662a Ignore static libraries and object files 2016-08-25 12:21:39 +02:00
40 changed files with 10292 additions and 3047 deletions

5
.gitignore vendored
View File

@ -1,2 +1,7 @@
# Binary
*.[oa]
# D
.dub
__test__*__
__test__*__.core

16
.travis.yml Normal file
View File

@ -0,0 +1,16 @@
sudo: false
os:
- linux
language: d
d:
- dmd-2.072.1
env:
matrix:
- ARCH=x86_64
script:
- dub test --arch=$ARCH

68
README.md Normal file
View File

@ -0,0 +1,68 @@
# Tanya
[![Build Status](https://travis-ci.org/caraus-ecms/tanya.svg?branch=master)](https://travis-ci.org/caraus-ecms/tanya)
[![Dub Version](https://img.shields.io/dub/v/tanya.svg)](https://code.dlang.org/packages/tanya)
[![License](https://img.shields.io/badge/license-MPL_2.0-blue.svg)](https://raw.githubusercontent.com/caraus-ecms/tanya/master/LICENSE)
Tanya is a general purpose library for D programming language that doesn't
rely on the Garbage Collector.
Its aim is to simplify the manual memory management in D and to provide a
guarantee with @nogc attribute that there are no hidden allocations on the
Garbage Collector heap. Everything in the library is usable in @nogc code.
Tanya extends Phobos functionality and provides alternative implementations for
data structures and utilities that depend on the Garbage Collector in Phobos.
## Overview
Tanya consists of the following packages:
* `async`: Event loop.
* `container`: Queue, Vector, Singly linked list.
* `crypto`: Work in progress TLS implementation.
* `math`: Multiple precision integer and a set of functions.
* `memory`: Tools for manual memory management (allocator, reference counting, helper functions).
* `network`: URL-Parsing, sockets.
### Current status
The library is currently under development, but some parts of it can already be
used.
Containers were newly reworked and the API won't change significantly, but will
be only extended. The same is true for the `memory` package.
`network` and `async` packages should be reviewed in the future and the API may
change.
`math` package contains an arbitrary precision integer implementation that has
a stable API (that mostly consists of operator overloads), but still needs
testing and work on its performance.
I'm currently mostly working on `crypto` that is not a complete cryptographic
suite, but contains (will contain) algorithm implementations required by TLS.
### Other properties
* Tanya is a native D library (only D and Assembler are tolerated).
* It is important for me to document the code and attach at least a few unit
tests where possible. So the documentation and usage examples can be found in
the source code.
* Tanya is mostly tested on a 64-bit Linux and some features are
platform-dependant, but not because it is a Linux-only library. Therefore any
help to bring better support for Windows and BSD systems would be accepted.
* The library isn't thread-safe. Thread-safity should be added later.
* I'm working with the latest dmd version, but will be looking to support other
D compilers and keep compatibility with the elder dmd versions in the future.
## Contributing
Since I'm mostly busy writing new code and implementing new features I would
appreciate, if anyone uses the library. It would help me to improve the
codebase and fix issues.
Feel free to contact me if you have any questions.

View File

@ -0,0 +1,187 @@
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
/**
* Copyright: Eugene Wissner 2016.
* License: $(LINK2 https://www.mozilla.org/en-US/MPL/2.0/,
* Mozilla Public License, v. 2.0).
* Authors: $(LINK2 mailto:belka@caraus.de, Eugene Wissner)
*/
module tanya.async.event.epoll;
version (linux):
public import core.sys.linux.epoll;
import tanya.async.protocol;
import tanya.async.event.selector;
import tanya.async.loop;
import tanya.async.transport;
import tanya.async.watcher;
import tanya.memory;
import tanya.memory.mmappool;
import tanya.network.socket;
import core.stdc.errno;
import core.sys.posix.unistd;
import core.time;
import std.algorithm.comparison;
extern (C) nothrow @nogc
{
int epoll_create1(int flags);
int epoll_ctl (int epfd, int op, int fd, epoll_event *event);
int epoll_wait (int epfd, epoll_event *events, int maxevents, int timeout);
}
class EpollLoop : SelectorLoop
{
protected int fd;
private epoll_event[] events;
/**
* Initializes the loop.
*/
this() @nogc
{
if ((fd = epoll_create1(EPOLL_CLOEXEC)) < 0)
{
throw MmapPool.instance.make!BadLoopException("epoll initialization failed");
}
super();
MmapPool.instance.resizeArray(events, maxEvents);
}
/**
* Free loop internals.
*/
~this() @nogc
{
MmapPool.instance.dispose(events);
close(fd);
}
/**
* Should be called if the backend configuration changes.
*
* Params:
* watcher = Watcher.
* oldEvents = The events were already set.
* events = The events should be set.
*
* Returns: $(D_KEYWORD true) if the operation was successful.
*/
protected override bool reify(ConnectionWatcher watcher,
EventMask oldEvents,
EventMask events) @nogc
in
{
assert(watcher !is null);
}
body
{
int op = EPOLL_CTL_DEL;
epoll_event ev;
if (events == oldEvents)
{
return true;
}
if (events && oldEvents)
{
op = EPOLL_CTL_MOD;
}
else if (events && !oldEvents)
{
op = EPOLL_CTL_ADD;
}
ev.data.fd = watcher.socket.handle;
ev.events = (events & (Event.read | Event.accept) ? EPOLLIN | EPOLLPRI : 0)
| (events & Event.write ? EPOLLOUT : 0)
| EPOLLET;
return epoll_ctl(fd, op, watcher.socket.handle, &ev) == 0;
}
/**
* Does the actual polling.
*/
protected override void poll() @nogc
{
// Don't block
immutable timeout = cast(immutable int) blockTime.total!"msecs";
auto eventCount = epoll_wait(fd, events.ptr, maxEvents, timeout);
if (eventCount < 0)
{
if (errno != EINTR)
{
throw defaultAllocator.make!BadLoopException();
}
return;
}
for (auto i = 0; i < eventCount; ++i)
{
auto io = cast(IOWatcher) connections[events[i].data.fd];
if (io is null)
{
acceptConnections(connections[events[i].data.fd]);
}
else if (events[i].events & EPOLLERR)
{
kill(io, null);
}
else if (events[i].events & (EPOLLIN | EPOLLPRI | EPOLLHUP))
{
auto transport = cast(SelectorStreamTransport) io.transport;
assert(transport !is null);
SocketException exception;
try
{
ptrdiff_t received;
do
{
received = transport.socket.receive(io.output[]);
io.output += received;
}
while (received);
}
catch (SocketException e)
{
exception = e;
}
if (transport.socket.disconnected)
{
kill(io, exception);
}
else if (io.output.length)
{
swapPendings.insertBack(io);
}
}
else if (events[i].events & EPOLLOUT)
{
auto transport = cast(SelectorStreamTransport) io.transport;
assert(transport !is null);
transport.writeReady = true;
if (transport.input.length)
{
feed(transport);
}
}
}
}
/**
* Returns: The blocking time.
*/
override protected @property inout(Duration) blockTime()
inout @safe pure nothrow
{
return min(super.blockTime, 1.dur!"seconds");
}
}

View File

@ -0,0 +1,291 @@
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
/**
* Copyright: Eugene Wissner 2016.
* License: $(LINK2 https://www.mozilla.org/en-US/MPL/2.0/,
* Mozilla Public License, v. 2.0).
* Authors: $(LINK2 mailto:belka@caraus.de, Eugene Wissner)
*/
module tanya.async.event.iocp;
version (Windows):
import tanya.container.buffer;
import tanya.async.loop;
import tanya.async.protocol;
import tanya.async.transport;
import tanya.async.watcher;
import tanya.memory;
import tanya.memory.mmappool;
import tanya.network.socket;
import core.sys.windows.basetyps;
import core.sys.windows.mswsock;
import core.sys.windows.winbase;
import core.sys.windows.windef;
import core.sys.windows.winsock2;
class IOCPStreamTransport : StreamTransport
{
private OverlappedConnectedSocket socket_;
private WriteBuffer!ubyte input;
/**
* Creates new completion port transport.
* Params:
* socket = Socket.
*/
this(OverlappedConnectedSocket socket) @nogc
in
{
assert(socket !is null);
}
body
{
socket_ = socket;
input = WriteBuffer!ubyte(8192, MmapPool.instance);
}
@property inout(OverlappedConnectedSocket) socket()
inout pure nothrow @safe @nogc
{
return socket_;
}
/**
* Write some data to the transport.
*
* Params:
* data = Data to send.
*/
void write(ubyte[] data) @nogc
{
immutable empty = input.length == 0;
input ~= data;
if (empty)
{
SocketState overlapped;
try
{
overlapped = MmapPool.instance.make!SocketState;
socket.beginSend(input[], overlapped);
}
catch (SocketException e)
{
MmapPool.instance.dispose(overlapped);
MmapPool.instance.dispose(e);
}
}
}
}
class IOCPLoop : Loop
{
protected HANDLE completionPort;
protected OVERLAPPED overlap;
/**
* Initializes the loop.
*/
this() @nogc
{
super();
completionPort = CreateIoCompletionPort(INVALID_HANDLE_VALUE, NULL, 0, 0);
if (!completionPort)
{
throw make!BadLoopException(defaultAllocator,
"Creating completion port failed");
}
}
/**
* Should be called if the backend configuration changes.
*
* Params:
* watcher = Watcher.
* oldEvents = The events were already set.
* events = The events should be set.
*
* Returns: $(D_KEYWORD true) if the operation was successful.
*/
override protected bool reify(ConnectionWatcher watcher,
EventMask oldEvents,
EventMask events) @nogc
{
SocketState overlapped;
if (!(oldEvents & Event.accept) && (events & Event.accept))
{
auto socket = cast(OverlappedStreamSocket) watcher.socket;
assert(socket !is null);
if (CreateIoCompletionPort(cast(HANDLE) socket.handle,
completionPort,
cast(ULONG_PTR) (cast(void*) watcher),
0) !is completionPort)
{
return false;
}
try
{
overlapped = MmapPool.instance.make!SocketState;
socket.beginAccept(overlapped);
}
catch (SocketException e)
{
MmapPool.instance.dispose(overlapped);
defaultAllocator.dispose(e);
return false;
}
}
if (!(oldEvents & Event.read) && (events & Event.read)
|| !(oldEvents & Event.write) && (events & Event.write))
{
auto io = cast(IOWatcher) watcher;
assert(io !is null);
auto transport = cast(IOCPStreamTransport) io.transport;
assert(transport !is null);
if (CreateIoCompletionPort(cast(HANDLE) transport.socket.handle,
completionPort,
cast(ULONG_PTR) (cast(void*) watcher),
0) !is completionPort)
{
return false;
}
// Begin to read
if (!(oldEvents & Event.read) && (events & Event.read))
{
try
{
overlapped = MmapPool.instance.make!SocketState;
transport.socket.beginReceive(io.output[], overlapped);
}
catch (SocketException e)
{
MmapPool.instance.dispose(overlapped);
defaultAllocator.dispose(e);
return false;
}
}
}
return true;
}
/**
* Does the actual polling.
*/
override protected void poll() @nogc
{
DWORD lpNumberOfBytes;
ULONG_PTR key;
LPOVERLAPPED overlap;
immutable timeout = cast(immutable int) blockTime.total!"msecs";
auto result = GetQueuedCompletionStatus(completionPort,
&lpNumberOfBytes,
&key,
&overlap,
timeout);
if (result == FALSE && overlap == NULL)
{
return; // Timeout
}
auto overlapped = (cast(SocketState) ((cast(void*) overlap) - 8));
assert(overlapped !is null);
scope (failure)
{
MmapPool.instance.dispose(overlapped);
}
switch (overlapped.event)
{
case OverlappedSocketEvent.accept:
auto connection = cast(ConnectionWatcher) (cast(void*) key);
assert(connection !is null);
auto listener = cast(OverlappedStreamSocket) connection.socket;
assert(listener !is null);
auto socket = listener.endAccept(overlapped);
auto transport = MmapPool.instance.make!IOCPStreamTransport(socket);
auto io = MmapPool.instance.make!IOWatcher(transport, connection.protocol);
connection.incoming.insertBack(io);
reify(io, EventMask(Event.none), EventMask(Event.read, Event.write));
swapPendings.insertBack(connection);
listener.beginAccept(overlapped);
break;
case OverlappedSocketEvent.read:
auto io = cast(IOWatcher) (cast(void*) key);
assert(io !is null);
if (!io.active)
{
MmapPool.instance.dispose(io);
MmapPool.instance.dispose(overlapped);
return;
}
auto transport = cast(IOCPStreamTransport) io.transport;
assert(transport !is null);
int received;
SocketException exception;
try
{
received = transport.socket.endReceive(overlapped);
}
catch (SocketException e)
{
exception = e;
}
if (transport.socket.disconnected)
{
// We want to get one last notification to destroy the watcher
transport.socket.beginReceive(io.output[], overlapped);
kill(io, exception);
}
else if (received > 0)
{
immutable full = io.output.free == received;
io.output += received;
// Receive was interrupted because the buffer is full. We have to continue
if (full)
{
transport.socket.beginReceive(io.output[], overlapped);
}
swapPendings.insertBack(io);
}
break;
case OverlappedSocketEvent.write:
auto io = cast(IOWatcher) (cast(void*) key);
assert(io !is null);
auto transport = cast(IOCPStreamTransport) io.transport;
assert(transport !is null);
transport.input += transport.socket.endSend(overlapped);
if (transport.input.length)
{
transport.socket.beginSend(transport.input[], overlapped);
}
else
{
transport.socket.beginReceive(io.output[], overlapped);
}
break;
default:
assert(false, "Unknown event");
}
}
}

View File

@ -0,0 +1,326 @@
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
/**
* Copyright: Eugene Wissner 2016.
* License: $(LINK2 https://www.mozilla.org/en-US/MPL/2.0/,
* Mozilla Public License, v. 2.0).
* Authors: $(LINK2 mailto:info@caraus.de, Eugene Wissner)
*/
module tanya.async.event.kqueue;
version (OSX)
{
version = MacBSD;
}
else version (iOS)
{
version = MacBSD;
}
else version (TVOS)
{
version = MacBSD;
}
else version (WatchOS)
{
version = MacBSD;
}
else version (FreeBSD)
{
version = MacBSD;
}
else version (OpenBSD)
{
version = MacBSD;
}
else version (DragonFlyBSD)
{
version = MacBSD;
}
version (MacBSD):
import core.stdc.stdint; // intptr_t, uintptr_t
import core.sys.posix.time; // timespec
void EV_SET(kevent_t* kevp, typeof(kevent_t.tupleof) args) pure nothrow @nogc
{
*kevp = kevent_t(args);
}
enum : short
{
EVFILT_READ = -1,
EVFILT_WRITE = -2,
EVFILT_AIO = -3, /* attached to aio requests */
EVFILT_VNODE = -4, /* attached to vnodes */
EVFILT_PROC = -5, /* attached to struct proc */
EVFILT_SIGNAL = -6, /* attached to struct proc */
EVFILT_TIMER = -7, /* timers */
EVFILT_MACHPORT = -8, /* Mach portsets */
EVFILT_FS = -9, /* filesystem events */
EVFILT_USER = -10, /* User events */
EVFILT_VM = -12, /* virtual memory events */
EVFILT_SYSCOUNT = 11
}
struct kevent_t
{
uintptr_t ident; /* identifier for this event */
short filter; /* filter for event */
ushort flags;
uint fflags;
intptr_t data;
void *udata; /* opaque user data identifier */
}
enum
{
/* actions */
EV_ADD = 0x0001, /* add event to kq (implies enable) */
EV_DELETE = 0x0002, /* delete event from kq */
EV_ENABLE = 0x0004, /* enable event */
EV_DISABLE = 0x0008, /* disable event (not reported) */
/* flags */
EV_ONESHOT = 0x0010, /* only report one occurrence */
EV_CLEAR = 0x0020, /* clear event state after reporting */
EV_RECEIPT = 0x0040, /* force EV_ERROR on success, data=0 */
EV_DISPATCH = 0x0080, /* disable event after reporting */
EV_SYSFLAGS = 0xF000, /* reserved by system */
EV_FLAG1 = 0x2000, /* filter-specific flag */
/* returned values */
EV_EOF = 0x8000, /* EOF detected */
EV_ERROR = 0x4000, /* error, data contains errno */
}
extern(C) int kqueue() nothrow @nogc;
extern(C) int kevent(int kq, const kevent_t *changelist, int nchanges,
kevent_t *eventlist, int nevents, const timespec *timeout)
nothrow @nogc;
import tanya.async.event.selector;
import tanya.async.loop;
import tanya.async.transport;
import tanya.async.watcher;
import tanya.memory;
import tanya.memory.mmappool;
import tanya.network.socket;
import core.stdc.errno;
import core.sys.posix.unistd;
import core.sys.posix.sys.time;
import core.time;
import std.algorithm.comparison;
class KqueueLoop : SelectorLoop
{
protected int fd;
private kevent_t[] events;
private kevent_t[] changes;
private size_t changeCount;
/**
* Returns: Maximal event count can be got at a time
* (should be supported by the backend).
*/
override protected @property inout(uint) maxEvents()
inout const pure nothrow @safe @nogc
{
return cast(uint) events.length;
}
this() @nogc
{
super();
if ((fd = kqueue()) == -1)
{
throw MmapPool.instance.make!BadLoopException("epoll initialization failed");
}
MmapPool.instance.resizeArray(events, 64);
MmapPool.instance.resizeArray(changes, 64);
}
/**
* Free loop internals.
*/
~this() @nogc
{
MmapPool.instance.dispose(events);
MmapPool.instance.dispose(changes);
close(fd);
}
private void set(socket_t socket, short filter, ushort flags) @nogc
{
if (changes.length <= changeCount)
{
MmapPool.instance.resizeArray(changes, changeCount + maxEvents);
}
EV_SET(&changes[changeCount],
cast(ulong) socket,
filter,
flags,
0U,
0L,
null);
++changeCount;
}
/**
* Should be called if the backend configuration changes.
*
* Params:
* watcher = Watcher.
* oldEvents = The events were already set.
* events = The events should be set.
*
* Returns: $(D_KEYWORD true) if the operation was successful.
*/
override protected bool reify(ConnectionWatcher watcher,
EventMask oldEvents,
EventMask events) @nogc
{
if (events != oldEvents)
{
if (oldEvents & Event.read || oldEvents & Event.accept)
{
set(watcher.socket.handle, EVFILT_READ, EV_DELETE);
}
if (oldEvents & Event.write)
{
set(watcher.socket.handle, EVFILT_WRITE, EV_DELETE);
}
}
if (events & (Event.read | events & Event.accept))
{
set(watcher.socket.handle, EVFILT_READ, EV_ADD | EV_ENABLE);
}
if (events & Event.write)
{
set(watcher.socket.handle, EVFILT_WRITE, EV_ADD | EV_DISPATCH);
}
return true;
}
/**
* Does the actual polling.
*/
protected override void poll() @nogc
{
timespec ts;
blockTime.split!("seconds", "nsecs")(ts.tv_sec, ts.tv_nsec);
if (changeCount > maxEvents)
{
MmapPool.instance.resizeArray(events, changes.length);
}
auto eventCount = kevent(fd, changes.ptr, cast(int) changeCount, events.ptr, maxEvents, &ts);
changeCount = 0;
if (eventCount < 0)
{
if (errno != EINTR)
{
throw defaultAllocator.make!BadLoopException();
}
return;
}
for (int i; i < eventCount; ++i)
{
assert(connections.length > events[i].ident);
IOWatcher io = cast(IOWatcher) connections[events[i].ident];
// If it is a ConnectionWatcher. Accept connections.
if (io is null)
{
acceptConnections(connections[events[i].ident]);
}
else if (events[i].flags & EV_ERROR)
{
kill(io, null);
}
else if (events[i].filter == EVFILT_READ)
{
auto transport = cast(SelectorStreamTransport) io.transport;
assert(transport !is null);
SocketException exception;
try
{
ptrdiff_t received;
do
{
received = transport.socket.receive(io.output[]);
io.output += received;
}
while (received);
}
catch (SocketException e)
{
exception = e;
}
if (transport.socket.disconnected)
{
kill(io, exception);
}
else if (io.output.length)
{
swapPendings.insertBack(io);
}
}
else if (events[i].filter == EVFILT_WRITE)
{
auto transport = cast(SelectorStreamTransport) io.transport;
assert(transport !is null);
transport.writeReady = true;
if (transport.input.length)
{
feed(transport);
}
}
}
}
/**
* Returns: The blocking time.
*/
override protected @property inout(Duration) blockTime()
inout @nogc @safe pure nothrow
{
return min(super.blockTime, 1.dur!"seconds");
}
/**
* If the transport couldn't send the data, the further sending should
* be handled by the event loop.
*
* Params:
* transport = Transport.
* exception = Exception thrown on sending.
*
* Returns: $(D_KEYWORD true) if the operation could be successfully
* completed or scheduled, $(D_KEYWORD false) otherwise (the
* transport is be destroyed then).
*/
protected override bool feed(SelectorStreamTransport transport,
SocketException exception = null) @nogc
{
if (!super.feed(transport, exception))
{
return false;
}
if (!transport.writeReady)
{
set(transport.socket.handle, EVFILT_WRITE, EV_DISPATCH);
return true;
}
return false;
}
}

View File

@ -0,0 +1,259 @@
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
/**
* Copyright: Eugene Wissner 2016.
* License: $(LINK2 https://www.mozilla.org/en-US/MPL/2.0/,
* Mozilla Public License, v. 2.0).
* Authors: $(LINK2 mailto:belka@caraus.de, Eugene Wissner)
*/
module tanya.async.event.selector;
version (Posix):
import tanya.async.loop;
import tanya.async.transport;
import tanya.async.watcher;
import tanya.container.buffer;
import tanya.memory;
import tanya.memory.mmappool;
import tanya.network.socket;
import core.sys.posix.netinet.in_;
import core.stdc.errno;
/**
* Transport for stream sockets.
*/
class SelectorStreamTransport : StreamTransport
{
private ConnectedSocket socket_;
/// Input buffer.
package WriteBuffer!ubyte input;
private SelectorLoop loop;
/// Received notification that the underlying socket is write-ready.
package bool writeReady;
/**
* Params:
* loop = Event loop.
* socket = Socket.
*/
this(SelectorLoop loop, ConnectedSocket socket) @nogc
{
socket_ = socket;
this.loop = loop;
input = WriteBuffer!ubyte(8192, MmapPool.instance);
}
/**
* Returns: Transport socket.
*/
inout(ConnectedSocket) socket() inout pure nothrow @safe @nogc
{
return socket_;
}
/**
* Write some data to the transport.
*
* Params:
* data = Data to send.
*/
void write(ubyte[] data) @nogc
{
if (!data.length)
{
return;
}
// Try to write if the socket is write ready.
if (writeReady)
{
ptrdiff_t sent;
SocketException exception;
try
{
sent = socket.send(data);
if (sent == 0)
{
writeReady = false;
}
}
catch (SocketException e)
{
writeReady = false;
exception = e;
}
if (sent < data.length)
{
input ~= data[sent..$];
loop.feed(this, exception);
}
}
else
{
input ~= data;
}
}
}
abstract class SelectorLoop : Loop
{
/// Pending connections.
protected ConnectionWatcher[] connections;
this() @nogc
{
super();
MmapPool.instance.resizeArray(connections, maxEvents);
}
~this() @nogc
{
foreach (ref connection; connections)
{
// We want to free only IOWatchers. ConnectionWatcher are created by the
// user and should be freed by himself.
auto io = cast(IOWatcher) connection;
if (io !is null)
{
MmapPool.instance.dispose(io);
connection = null;
}
}
MmapPool.instance.dispose(connections);
}
/**
* If the transport couldn't send the data, the further sending should
* be handled by the event loop.
*
* Params:
* transport = Transport.
* exception = Exception thrown on sending.
*
* Returns: $(D_KEYWORD true) if the operation could be successfully
* completed or scheduled, $(D_KEYWORD false) otherwise (the
* transport will be destroyed then).
*/
protected bool feed(SelectorStreamTransport transport,
SocketException exception = null) @nogc
{
while (transport.input.length && transport.writeReady)
{
try
{
ptrdiff_t sent = transport.socket.send(transport.input[]);
if (sent == 0)
{
transport.writeReady = false;
}
else
{
transport.input += sent;
}
}
catch (SocketException e)
{
exception = e;
transport.writeReady = false;
}
}
if (exception !is null)
{
auto watcher = cast(IOWatcher) connections[transport.socket.handle];
assert(watcher !is null);
kill(watcher, exception);
return false;
}
return true;
}
/**
* Start watching.
*
* Params:
* watcher = Watcher.
*/
override void start(ConnectionWatcher watcher) @nogc
{
if (watcher.active)
{
return;
}
if (connections.length <= watcher.socket)
{
MmapPool.instance.resizeArray(connections, watcher.socket.handle + maxEvents / 2);
}
connections[watcher.socket.handle] = watcher;
super.start(watcher);
}
/**
* Accept incoming connections.
*
* Params:
* connection = Connection watcher ready to accept.
*/
package void acceptConnections(ConnectionWatcher connection) @nogc
in
{
assert(connection !is null);
}
body
{
while (true)
{
ConnectedSocket client;
try
{
client = (cast(StreamSocket) connection.socket).accept();
}
catch (SocketException e)
{
defaultAllocator.dispose(e);
break;
}
if (client is null)
{
break;
}
IOWatcher io;
auto transport = MmapPool.instance.make!SelectorStreamTransport(this, client);
if (connections.length > client.handle)
{
io = cast(IOWatcher) connections[client.handle];
}
else
{
MmapPool.instance.resizeArray(connections, client.handle + maxEvents / 2);
}
if (io is null)
{
io = MmapPool.instance.make!IOWatcher(transport,
connection.protocol);
connections[client.handle] = io;
}
else
{
io(transport, connection.protocol);
}
reify(io, EventMask(Event.none), EventMask(Event.read, Event.write));
connection.incoming.insertBack(io);
}
if (!connection.incoming.empty)
{
swapPendings.insertBack(connection);
}
}
}

32
source/tanya/async/iocp.d Normal file
View File

@ -0,0 +1,32 @@
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
/**
* Copyright: Eugene Wissner 2016.
* License: $(LINK2 https://www.mozilla.org/en-US/MPL/2.0/,
* Mozilla Public License, v. 2.0).
* Authors: $(LINK2 mailto:belka@caraus.de, Eugene Wissner)
*/
module tanya.async.iocp;
version (Windows):
import core.sys.windows.winbase;
import core.sys.windows.windef;
/**
* Provides an extendable representation of a Win32 $(D_PSYMBOL OVERLAPPED)
* structure.
*/
class State
{
/// For internal use by Windows API.
align(1) OVERLAPPED overlapped;
/// File/socket handle.
HANDLE handle;
/// For keeping events or event masks.
int event;
}

369
source/tanya/async/loop.d Normal file
View File

@ -0,0 +1,369 @@
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
/**
* Copyright: Eugene Wissner 2016.
* License: $(LINK2 https://www.mozilla.org/en-US/MPL/2.0/,
* Mozilla Public License, v. 2.0).
* Authors: $(LINK2 mailto:belka@caraus.de, Eugene Wissner)
*
* ---
* import tanya.async;
* import tanya.memory;
* import tanya.network.socket;
*
* class EchoProtocol : TransmissionControlProtocol
* {
* private DuplexTransport transport;
*
* void received(ubyte[] data) @nogc
* {
* transport.write(data);
* }
*
* void connected(DuplexTransport transport) @nogc
* {
* this.transport = transport;
* }
*
* void disconnected(SocketException e = null) @nogc
* {
* }
* }
*
* void main()
* {
* auto address = defaultAllocator.make!InternetAddress("127.0.0.1", cast(ushort) 8192);
*
* version (Windows)
* {
* auto sock = defaultAllocator.make!OverlappedStreamSocket(AddressFamily.INET);
* }
* else
* {
* auto sock = defaultAllocator.make!StreamSocket(AddressFamily.INET);
* sock.blocking = false;
* }
*
* sock.bind(address);
* sock.listen(5);
*
* auto io = defaultAllocator.make!ConnectionWatcher(sock);
* io.setProtocol!EchoProtocol;
*
* defaultLoop.start(io);
* defaultLoop.run();
*
* sock.shutdown();
* defaultAllocator.dispose(io);
* defaultAllocator.dispose(sock);
* defaultAllocator.dispose(address);
* }
* ---
*/
module tanya.async.loop;
import core.time;
import std.algorithm.iteration;
import std.algorithm.mutation;
import std.typecons;
import tanya.async.protocol;
import tanya.async.transport;
import tanya.async.watcher;
import tanya.container.buffer;
import tanya.container.queue;
import tanya.memory;
import tanya.memory.mmappool;
import tanya.network.socket;
version (DisableBackends)
{
}
else version (linux)
{
import tanya.async.event.epoll;
version = Epoll;
}
else version (Windows)
{
import tanya.async.event.iocp;
version = IOCP;
}
else version (OSX)
{
version = Kqueue;
}
else version (iOS)
{
version = Kqueue;
}
else version (FreeBSD)
{
version = Kqueue;
}
else version (OpenBSD)
{
version = Kqueue;
}
else version (DragonFlyBSD)
{
version = Kqueue;
}
/**
* Events.
*/
enum Event : uint
{
none = 0x00, /// No events.
read = 0x01, /// Non-blocking read call.
write = 0x02, /// Non-blocking write call.
accept = 0x04, /// Connection made.
error = 0x80000000, /// Sent when an error occurs.
}
alias EventMask = BitFlags!Event;
/**
* Event loop.
*/
abstract class Loop
{
/// Pending watchers.
protected Queue!Watcher* pendings;
/// Ditto.
protected Queue!Watcher* swapPendings;
/**
* Returns: Maximal event count can be got at a time
* (should be supported by the backend).
*/
protected @property inout(uint) maxEvents()
inout const pure nothrow @safe @nogc
{
return 128U;
}
/**
* Initializes the loop.
*/
this() @nogc
{
pendings = MmapPool.instance.make!(Queue!Watcher)(MmapPool.instance);
swapPendings = MmapPool.instance.make!(Queue!Watcher)(MmapPool.instance);
}
/**
* Frees loop internals.
*/
~this() @nogc
{
foreach (w; *pendings)
{
MmapPool.instance.dispose(w);
}
MmapPool.instance.dispose(pendings);
foreach (w; *swapPendings)
{
MmapPool.instance.dispose(w);
}
MmapPool.instance.dispose(swapPendings);
}
/**
* Starts the loop.
*/
void run() @nogc
{
done_ = false;
do
{
poll();
// Invoke pendings
swapPendings.each!((ref p) @nogc => p.invoke());
swap(pendings, swapPendings);
}
while (!done_);
}
/**
* Break out of the loop.
*/
void unloop() @safe pure nothrow @nogc
{
done_ = true;
}
/**
* Start watching.
*
* Params:
* watcher = Watcher.
*/
void start(ConnectionWatcher watcher) @nogc
{
if (watcher.active)
{
return;
}
watcher.active = true;
reify(watcher, EventMask(Event.none), EventMask(Event.accept));
}
/**
* Stop watching.
*
* Params:
* watcher = Watcher.
*/
void stop(ConnectionWatcher watcher) @nogc
{
if (!watcher.active)
{
return;
}
watcher.active = false;
reify(watcher, EventMask(Event.accept), EventMask(Event.none));
}
/**
* Should be called if the backend configuration changes.
*
* Params:
* watcher = Watcher.
* oldEvents = The events were already set.
* events = The events should be set.
*
* Returns: $(D_KEYWORD true) if the operation was successful.
*/
abstract protected bool reify(ConnectionWatcher watcher,
EventMask oldEvents,
EventMask events) @nogc;
/**
* Returns: The blocking time.
*/
protected @property inout(Duration) blockTime()
inout @safe pure nothrow @nogc
{
// Don't block if we have to do.
return swapPendings.empty ? blockTime_ : Duration.zero;
}
/**
* Sets the blocking time for IO watchers.
*
* Params:
* blockTime = The blocking time. Cannot be larger than
* $(D_PSYMBOL maxBlockTime).
*/
protected @property void blockTime(in Duration blockTime) @safe pure nothrow @nogc
in
{
assert(blockTime <= 1.dur!"hours", "Too long to wait.");
assert(!blockTime.isNegative);
}
body
{
blockTime_ = blockTime;
}
/**
* Kills the watcher and closes the connection.
*/
protected void kill(IOWatcher watcher, SocketException exception) @nogc
{
watcher.socket.shutdown();
defaultAllocator.dispose(watcher.socket);
MmapPool.instance.dispose(watcher.transport);
watcher.exception = exception;
swapPendings.insertBack(watcher);
}
/**
* Does the actual polling.
*/
abstract protected void poll() @nogc;
/// Whether the event loop should be stopped.
private bool done_;
/// Maximal block time.
protected Duration blockTime_ = 1.dur!"minutes";
}
/**
* Exception thrown on errors in the event loop.
*/
class BadLoopException : Exception
{
/**
* Params:
* file = The file where the exception occurred.
* line = The line number where the exception occurred.
* next = The previous exception in the chain of exceptions, if any.
*/
this(string file = __FILE__, size_t line = __LINE__, Throwable next = null)
pure nothrow const @safe @nogc
{
super("Event loop cannot be initialized.", file, line, next);
}
}
/**
* Returns the event loop used by default. If an event loop wasn't set with
* $(D_PSYMBOL defaultLoop) before, $(D_PSYMBOL defaultLoop) will try to
* choose an event loop supported on the system.
*
* Returns: The default event loop.
*/
@property Loop defaultLoop() @nogc
{
if (defaultLoop_ !is null)
{
return defaultLoop_;
}
version (Epoll)
{
defaultLoop_ = MmapPool.instance.make!EpollLoop;
}
else version (IOCP)
{
defaultLoop_ = MmapPool.instance.make!IOCPLoop;
}
else version (Kqueue)
{
import tanya.async.event.kqueue;
defaultLoop_ = MmapPool.instance.make!KqueueLoop;
}
return defaultLoop_;
}
/**
* Sets the default event loop.
*
* This property makes it possible to implement your own backends or event
* loops, for example, if the system is not supported or if you want to
* extend the supported implementation. Just extend $(D_PSYMBOL Loop) and pass
* your implementation to this property.
*
* Params:
* loop = The event loop.
*/
@property void defaultLoop(Loop loop) @nogc
in
{
assert(loop !is null);
}
body
{
defaultLoop_ = loop;
}
private Loop defaultLoop_;

View File

@ -8,9 +8,9 @@
* Mozilla Public License, v. 2.0).
* Authors: $(LINK2 mailto:belka@caraus.de, Eugene Wissner)
*/
module tanya.event;
module tanya.async;
public import tanya.event.loop;
public import tanya.event.protocol;
public import tanya.event.transport;
public import tanya.event.watcher;
public import tanya.async.loop;
public import tanya.async.protocol;
public import tanya.async.transport;
public import tanya.async.watcher;

View File

@ -8,21 +8,21 @@
* Mozilla Public License, v. 2.0).
* Authors: $(LINK2 mailto:belka@caraus.de, Eugene Wissner)
*/
module tanya.event.protocol;
module tanya.async.protocol;
import tanya.event.transport;
import tanya.network.socket;
import tanya.async.transport;
/**
* Common protocol interface.
*/
interface Protocol
{
@nogc:
/**
* Params:
* data = Read data.
*/
void received(ubyte[] data);
void received(ubyte[] data) @nogc;
/**
* Called when a connection is made.
@ -30,12 +30,16 @@ interface Protocol
* Params:
* transport = Protocol transport.
*/
void connected(DuplexTransport transport);
void connected(DuplexTransport transport) @nogc;
/**
* Called when a connection is lost.
*
* Params:
* exception = $(D_PSYMBOL Exception) if an error caused
* the disconnect, $(D_KEYWORD null) otherwise.
*/
void disconnected();
void disconnected(SocketException exception = null) @nogc;
}
/**

View File

@ -0,0 +1,63 @@
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
/**
* Copyright: Eugene Wissner 2016.
* License: $(LINK2 https://www.mozilla.org/en-US/MPL/2.0/,
* Mozilla Public License, v. 2.0).
* Authors: $(LINK2 mailto:belka@caraus.de, Eugene Wissner)
*/
module tanya.async.transport;
import tanya.network.socket;
/**
* Base transport interface.
*/
interface Transport
{
}
/**
* Interface for read-only transports.
*/
interface ReadTransport : Transport
{
}
/**
* Interface for write-only transports.
*/
interface WriteTransport : Transport
{
/**
* Write some data to the transport.
*
* Params:
* data = Data to send.
*/
void write(ubyte[] data) @nogc;
}
/**
* Represents a bidirectional transport.
*/
interface DuplexTransport : ReadTransport, WriteTransport
{
}
/**
* Represents a socket transport.
*/
interface SocketTransport : Transport
{
@property inout(Socket) socket() inout pure nothrow @safe @nogc;
}
/**
* Represents a connection-oriented socket transport.
*/
package interface StreamTransport : DuplexTransport, SocketTransport
{
}

View File

@ -0,0 +1,245 @@
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
/**
* Copyright: Eugene Wissner 2016.
* License: $(LINK2 https://www.mozilla.org/en-US/MPL/2.0/,
* Mozilla Public License, v. 2.0).
* Authors: $(LINK2 mailto:belka@caraus.de, Eugene Wissner)
*/
module tanya.async.watcher;
import std.functional;
import std.exception;
import tanya.async.loop;
import tanya.async.protocol;
import tanya.async.transport;
import tanya.container.buffer;
import tanya.container.queue;
import tanya.memory;
import tanya.memory.mmappool;
import tanya.network.socket;
version (Windows)
{
import core.sys.windows.basetyps;
import core.sys.windows.mswsock;
import core.sys.windows.winbase;
import core.sys.windows.windef;
import core.sys.windows.winsock2;
}
/**
* A watcher is an opaque structure that you allocate and register to record
* your interest in some event.
*/
abstract class Watcher
{
/// Whether the watcher is active.
bool active;
/**
* Invoke some action on event.
*/
void invoke() @nogc;
}
class ConnectionWatcher : Watcher
{
/// Watched socket.
private Socket socket_;
/// Protocol factory.
protected Protocol delegate() @nogc protocolFactory;
package Queue!IOWatcher incoming;
/**
* Params:
* socket = Socket.
*/
this(Socket socket) @nogc
{
socket_ = socket;
}
/// Ditto.
protected this() pure nothrow @safe @nogc
{
}
~this() @nogc
{
foreach (w; incoming)
{
MmapPool.instance.dispose(w);
}
}
/**
* Params:
* P = Protocol should be used.
*/
void setProtocol(P : Protocol)() @nogc
{
this.protocolFactory = () @nogc => cast(Protocol) MmapPool.instance.make!P;
}
/**
* Returns: Socket.
*/
@property inout(Socket) socket() inout pure nothrow @nogc
{
return socket_;
}
/**
* Returns: New protocol instance.
*/
@property Protocol protocol() @nogc
in
{
assert(protocolFactory !is null, "Protocol isn't set.");
}
body
{
return protocolFactory();
}
/**
* Invokes new connection callback.
*/
override void invoke() @nogc
{
foreach (io; incoming)
{
io.protocol.connected(cast(DuplexTransport) io.transport);
}
}
}
/**
* Contains a pending watcher with the invoked events or a transport can be
* read from.
*/
class IOWatcher : ConnectionWatcher
{
/// If an exception was thrown the transport should be already invalid.
private union
{
StreamTransport transport_;
SocketException exception_;
}
private Protocol protocol_;
/**
* Returns: Underlying output buffer.
*/
package ReadBuffer!ubyte output;
/**
* Params:
* transport = Transport.
* protocol = New instance of the application protocol.
*/
this(StreamTransport transport, Protocol protocol) @nogc
in
{
assert(transport !is null);
assert(protocol !is null);
}
body
{
super();
transport_ = transport;
protocol_ = protocol;
output = ReadBuffer!ubyte(8192, 1024, MmapPool.instance);
active = true;
}
/**
* Destroys the watcher.
*/
~this() @nogc
{
MmapPool.instance.dispose(protocol_);
}
/**
* Assigns a transport.
*
* Params:
* transport = Transport.
* protocol = Application protocol.
*
* Returns: $(D_KEYWORD this).
*/
IOWatcher opCall(StreamTransport transport, Protocol protocol)
pure nothrow @nogc
in
{
assert(transport !is null);
assert(protocol !is null);
}
body
{
transport_ = transport;
protocol_ = protocol;
active = true;
return this;
}
/**
* Returns: Transport used by this watcher.
*/
@property inout(StreamTransport) transport() inout pure nothrow @nogc
{
return transport_;
}
/**
* Sets an exception occurred during a read/write operation.
*
* Params:
* exception = Thrown exception.
*/
@property void exception(SocketException exception) pure nothrow @nogc
{
exception_ = exception;
}
/**
* Returns: Application protocol.
*/
override @property Protocol protocol() pure nothrow @safe @nogc
{
return protocol_;
}
/**
* Returns: Socket.
*/
override @property inout(Socket) socket() inout pure nothrow @nogc
{
return transport.socket;
}
/**
* Invokes the watcher callback.
*/
override void invoke() @nogc
{
if (output.length)
{
protocol.received(output[0..$]);
output.clear();
}
else
{
protocol.disconnected(exception_);
active = false;
}
}
}

View File

@ -6,331 +6,362 @@
* Copyright: Eugene Wissner 2016.
* License: $(LINK2 https://www.mozilla.org/en-US/MPL/2.0/,
* Mozilla Public License, v. 2.0).
* Authors: $(LINK2 mailto:belka@caraus.de, Eugene Wissner)
* Authors: $(LINK2 mailto:info@caraus.de, Eugene Wissner)
*/
module tanya.container.buffer;
import std.traits;
import tanya.memory;
@nogc:
version (unittest)
{
private int fillBuffer(void* buffer,
private int fillBuffer(ubyte[] buffer,
in size_t size,
int start = 0,
int end = 10)
int end = 10) @nogc pure nothrow
in
{
assert(start < end);
}
body
{
ubyte[] buf = cast(ubyte[]) buffer[0..size];
auto numberRead = end - start;
for (ubyte i; i < numberRead; ++i)
{
buf[i] = cast(ubyte) (start + i);
buffer[i] = cast(ubyte) (start + i);
}
return numberRead;
}
}
/**
* Interface for implemeting input/output buffers.
*/
interface Buffer
{
@nogc:
/**
* Returns: The size of the internal buffer.
*/
@property size_t capacity() const @safe pure nothrow;
/**
* Returns: Data size.
*/
@property size_t length() const @safe pure nothrow;
/**
* Returns: Available space.
*/
@property size_t free() const @safe pure nothrow;
/**
* Appends some data to the buffer.
* Self-expanding buffer, that can be used with functions returning the number
* of the read bytes.
*
* This buffer supports asynchronous reading. It means you can pass a new chunk
* to an asynchronous read function during you are working with already
* available data. But only one asynchronous call at a time is supported. Be
* sure to call $(D_PSYMBOL ReadBuffer.clear()) before you append the result
* of the pended asynchronous call.
*
* Params:
* buffer = Buffer chunk got with $(D_PSYMBOL buffer).
* T = Buffer type.
*/
Buffer opOpAssign(string op)(void[] buffer)
if (op == "~");
}
/**
* Buffer that can be used with C functions accepting void pointer and
* returning the number of the read bytes.
*/
class ReadBuffer : Buffer
struct ReadBuffer(T = ubyte)
if (isScalarType!T)
{
@nogc:
/// Internal buffer.
protected ubyte[] _buffer;
private T[] buffer_;
/// Filled buffer length.
protected size_t _length;
private size_t length_;
/// Start of available data.
private size_t start;
/// Last position returned with $(D_KEYWORD []).
private size_t ring;
/// Available space.
protected immutable size_t minAvailable;
private immutable size_t minAvailable = 1024;
/// Size by which the buffer will grow.
protected immutable size_t blockSize;
private Allocator allocator;
private immutable size_t blockSize = 8192;
invariant
{
assert(_length <= _buffer.length);
assert(length_ <= buffer_.length);
assert(blockSize > 0);
assert(minAvailable > 0);
}
/**
* Creates a new read buffer.
*
* Params:
* size = Initial buffer size and the size by which the buffer
* will grow.
* minAvailable = minimal size should be always available to fill.
* So it will reallocate if $(D_INLINECODE
* $(D_PSYMBOL free) < $(D_PARAM minAvailable)
* ).
* $(D_PSYMBOL free) < $(D_PARAM minAvailable)).
* allocator = Allocator.
*/
this(size_t size = 8192,
size_t minAvailable = 1024,
Allocator allocator = defaultAllocator)
this(in size_t size,
in size_t minAvailable = 1024,
shared Allocator allocator = defaultAllocator) @trusted
{
this.allocator = allocator;
this(allocator);
this.minAvailable = minAvailable;
this.blockSize = size;
resizeArray!ubyte(this.allocator, _buffer, size);
buffer_ = cast(T[]) allocator_.allocate(size * T.sizeof);
}
/// Ditto.
this(shared Allocator allocator)
in
{
assert(allocator_ is null);
}
body
{
allocator_ = allocator;
}
/**
* Deallocates the internal buffer.
*/
~this()
~this() @trusted
{
finalize(allocator, _buffer);
allocator.deallocate(buffer_);
}
///
unittest
{
auto b = make!ReadBuffer(defaultAllocator);
assert(b.capacity == 8192);
ReadBuffer!ubyte b;
assert(b.capacity == 0);
assert(b.length == 0);
finalize(defaultAllocator, b);
}
/**
* Returns: The size of the internal buffer.
*/
@property size_t capacity() const @safe pure nothrow
@property size_t capacity() const
{
return _buffer.length;
return buffer_.length;
}
/**
* Returns: Data size.
*/
@property size_t length() const @safe pure nothrow
@property size_t length() const
{
return _length;
return length_ - start;
}
/// Ditto.
alias opDollar = length;
/**
* Clears the buffer.
*
* Returns: $(D_KEYWORD this).
*/
void clear()
{
start = length_ = ring;
}
/**
* Returns: Available space.
*/
@property size_t free() const @safe pure nothrow
@property size_t free() const
{
return capacity - length;
return length > ring ? capacity - length : capacity - ring;
}
///
unittest
{
auto b = make!ReadBuffer(defaultAllocator);
ReadBuffer!ubyte b;
size_t numberRead;
void* buf;
assert(b.free == 0);
// Fills the buffer with values 0..10
assert(b.free == b.blockSize);
buf = b.buffer;
numberRead = fillBuffer(buf, b.free, 0, 10);
b ~= buf[0..numberRead];
numberRead = fillBuffer(b[], b.free, 0, 10);
b += numberRead;
assert(b.free == b.blockSize - numberRead);
b[];
b.clear();
assert(b.free == b.blockSize);
finalize(defaultAllocator, b);
}
/**
* Returns a pointer to a chunk of the internal buffer. You can pass it to
* a function that requires such a buffer.
*
* Set the buffer again after reading something into it. Append
* $(D_KEYWORD ~=) a slice from the beginning of the buffer you got and
* till the number of the read bytes. The data will be appended to the
* existing buffer.
*
* Returns: A chunk of available buffer.
*/
@property void* buffer()
{
if (capacity - length < minAvailable)
{
resizeArray!ubyte(this.allocator, _buffer, capacity + blockSize);
}
return _buffer[_length..$].ptr;
}
/**
* Appends some data to the buffer. Use only the buffer you got
* with $(D_PSYMBOL buffer)!
* Appends some data to the buffer.
*
* Params:
* buffer = Buffer chunk got with $(D_PSYMBOL buffer).
* length = Number of the bytes read.
*
* Returns: $(D_KEYWORD this).
*/
ReadBuffer opOpAssign(string op)(void[] buffer)
if (op == "~")
ref ReadBuffer opOpAssign(string op)(in size_t length)
if (op == "+")
{
_length += buffer.length;
length_ += length;
ring = start;
return this;
}
///
unittest
{
auto b = make!ReadBuffer(defaultAllocator);
ReadBuffer!ubyte b;
size_t numberRead;
void* buf;
ubyte[] result;
// Fills the buffer with values 0..10
buf = b.buffer;
numberRead = fillBuffer(buf, b.free, 0, 10);
b ~= buf[0..numberRead];
numberRead = fillBuffer(b[], b.free, 0, 10);
b += numberRead;
result = b[];
result = b[0..$];
assert(result[0] == 0);
assert(result[1] == 1);
assert(result[9] == 9);
b.clear();
// It shouldn't overwrite, but append another 5 bytes to the buffer
buf = b.buffer;
numberRead = fillBuffer(buf, b.free, 0, 10);
b ~= buf[0..numberRead];
numberRead = fillBuffer(b[], b.free, 0, 10);
b += numberRead;
buf = b.buffer;
numberRead = fillBuffer(buf, b.free, 20, 25);
b ~= buf[0..numberRead];
numberRead = fillBuffer(b[], b.free, 20, 25);
b += numberRead;
result = b[];
result = b[0..$];
assert(result[0] == 0);
assert(result[1] == 1);
assert(result[9] == 9);
assert(result[10] == 20);
assert(result[14] == 24);
finalize(defaultAllocator, b);
}
/**
* Returns the buffer. The buffer is cleared after that. So you can get it
* only one time.
* Params:
* start = Start position.
* end = End position.
*
* Returns: The buffer as array.
* Returns: Array between $(D_PARAM start) and $(D_PARAM end).
*/
@property ubyte[] opIndex()
T[] opSlice(in size_t start, in size_t end)
{
auto ret = _buffer[0.._length];
_length = 0;
return buffer_[this.start + start .. this.start + end];
}
/**
* Returns a free chunk of the buffer.
*
* Add ($(D_KEYWORD +=)) the number of the read bytes after using it.
*
* Returns: A free chunk of the buffer.
*/
T[] opIndex()
{
if (start > 0)
{
auto ret = buffer_[0 .. start];
ring = 0;
return ret;
}
else
{
if (capacity - length < minAvailable)
{
void[] buf = buffer_;
immutable cap = capacity;
() @trusted {
allocator.reallocate(buf, (cap + blockSize) * T.sizeof);
buffer_ = cast(T[]) buf;
}();
buffer_[cap .. $] = T.init;
}
ring = length_;
return buffer_[length_ .. $];
}
}
///
unittest
{
auto b = make!ReadBuffer(defaultAllocator);
ReadBuffer!ubyte b;
size_t numberRead;
void* buf;
ubyte[] result;
// Fills the buffer with values 0..10
buf = b.buffer;
numberRead = fillBuffer(buf, b.free, 0, 10);
b ~= buf[0..numberRead];
numberRead = fillBuffer(b[], b.free, 0, 10);
b += numberRead;
assert(b.length == 10);
result = b[];
result = b[0..$];
assert(result[0] == 0);
assert(result[9] == 9);
b.clear();
assert(b.length == 0);
finalize(defaultAllocator, b);
}
mixin DefaultAllocator;
}
private unittest
{
static assert(is(ReadBuffer!int));
}
/**
* Circular, self-expanding buffer that can be used with C functions accepting
* void pointer and returning the number of the read bytes.
* Circular, self-expanding buffer with overflow support. Can be used with
* functions returning the number of the transferred bytes.
*
* The buffer is optimized for situations where you read all the data from it
* at once (without writing to it occasionally). It can become ineffective if
* you permanently keep some data in the buffer and alternate writing and
* reading, because it may allocate and move elements.
*
* Params:
* T = Buffer type.
*/
class WriteBuffer : Buffer
struct WriteBuffer(T = ubyte)
if (isScalarType!T)
{
@nogc:
/// Internal buffer.
protected ubyte[] _buffer;
private T[] buffer_;
/// Buffer start position.
protected size_t start;
private size_t start;
/// Buffer ring area size. After this position begins buffer overflow area.
protected size_t ring;
private size_t ring;
/// Size by which the buffer will grow.
protected immutable size_t blockSize;
private immutable size_t blockSize;
/// The position of the free area in the buffer.
protected size_t position;
private Allocator allocator;
private size_t position;
invariant
{
assert(blockSize > 0);
// position can refer to an element outside the buffer if the buffer is full.
assert(position <= _buffer.length);
// Position can refer to an element outside the buffer if the buffer is full.
assert(position <= buffer_.length);
}
/**
* Params:
* size = Initial buffer size and the size by which the buffer
* will grow.
* size = Initial buffer size and the size by which the buffer will
* grow.
* allocator = Allocator.
*/
this(size_t size = 8192,
Allocator allocator = defaultAllocator)
this(in size_t size, shared Allocator allocator = defaultAllocator) @trusted
in
{
assert(size > 0);
}
body
{
this.allocator = allocator;
blockSize = size;
ring = size - 1;
resizeArray!ubyte(this.allocator, _buffer, size);
this(allocator);
buffer_ = cast(T[]) allocator_.allocate(size * T.sizeof);
}
@disable this();
/// Ditto.
this(shared Allocator allocator)
in
{
assert(allocator !is null);
}
body
{
allocator_ = allocator;
}
/**
@ -338,26 +369,26 @@ class WriteBuffer : Buffer
*/
~this()
{
finalize(allocator, _buffer);
allocator.deallocate(buffer_);
}
/**
* Returns: The size of the internal buffer.
*/
@property size_t capacity() const @safe pure nothrow
@property size_t capacity() const
{
return _buffer.length;
return buffer_.length;
}
/**
* Note that $(D_PSYMBOL length) doesn't return the real length of the data,
* but only the array length that will be returned with $(D_PSYMBOL buffer)
* next time. Be sure to call $(D_PSYMBOL buffer) and set $(D_PSYMBOL written)
* but only the array length that will be returned with $(D_PSYMBOL opIndex)
* next time. Be sure to call $(D_PSYMBOL opIndex) and set $(D_KEYWORD +=)
* until $(D_PSYMBOL length) returns 0.
*
* Returns: Data size.
*/
@property size_t length() const @safe pure nothrow
@property size_t length() const
{
if (position > ring || position < start) // Buffer overflowed
{
@ -369,34 +400,35 @@ class WriteBuffer : Buffer
}
}
/// Ditto.
alias opDollar = length;
///
unittest
{
auto b = make!WriteBuffer(defaultAllocator, 4);
auto b = WriteBuffer!ubyte(4);
ubyte[3] buf = [48, 23, 255];
b ~= buf;
assert(b.length == 3);
b.written = 2;
b += 2;
assert(b.length == 1);
b ~= buf;
assert(b.length == 2);
b.written = 2;
b += 2;
assert(b.length == 2);
b ~= buf;
assert(b.length == 5);
b.written = b.length;
b += b.length;
assert(b.length == 0);
finalize(defaultAllocator, b);
}
/**
* Returns: Available space.
*/
@property size_t free() const @safe pure nothrow
@property size_t free() const
{
return capacity - length;
}
@ -405,9 +437,9 @@ class WriteBuffer : Buffer
* Appends data to the buffer.
*
* Params:
* buffer = Buffer chunk got with $(D_PSYMBOL buffer).
* buffer = Buffer chunk got with $(D_PSYMBOL opIndex).
*/
WriteBuffer opOpAssign(string op)(ubyte[] buffer)
ref WriteBuffer opOpAssign(string op)(in T[] buffer)
if (op == "~")
{
size_t end, start;
@ -422,7 +454,7 @@ class WriteBuffer : Buffer
end = afterRing;
}
start = end - position;
_buffer[position..end] = buffer[0..start];
buffer_[position .. end] = buffer[0 .. start];
if (end == afterRing)
{
position = this.start == 0 ? afterRing : 0;
@ -442,7 +474,7 @@ class WriteBuffer : Buffer
end = this.start;
}
auto areaEnd = end - position + start;
_buffer[position..end] = buffer[start..areaEnd];
buffer_[position .. end] = buffer[start .. areaEnd];
position = end == this.start ? ring + 1 : end - position;
start = areaEnd;
}
@ -453,11 +485,14 @@ class WriteBuffer : Buffer
end = position + buffer.length - start;
if (end > capacity)
{
auto newSize = end / blockSize * blockSize + blockSize;
resizeArray!ubyte(this.allocator, _buffer, newSize);
auto newSize = (end / blockSize * blockSize + blockSize) * T.sizeof;
() @trusted {
void[] buf = buffer_;
allocator.reallocate(buf, newSize);
buffer_ = cast(T[]) buf;
}();
}
_buffer[position..end] = buffer[start..$];
buffer_[position .. end] = buffer[start .. $];
position = end;
if (this.start == 0)
{
@ -471,47 +506,50 @@ class WriteBuffer : Buffer
///
unittest
{
auto b = make!WriteBuffer(defaultAllocator, 4);
auto b = WriteBuffer!ubyte(4);
ubyte[3] buf = [48, 23, 255];
b ~= buf;
assert(b.capacity == 4);
assert(b._buffer[0] == 48 && b._buffer[1] == 23 && b._buffer[2] == 255);
assert(b.buffer_[0] == 48 && b.buffer_[1] == 23 && b.buffer_[2] == 255);
b.written = 2;
b += 2;
b ~= buf;
assert(b.capacity == 4);
assert(b._buffer[0] == 23 && b._buffer[1] == 255
&& b._buffer[2] == 255 && b._buffer[3] == 48);
assert(b.buffer_[0] == 23 && b.buffer_[1] == 255
&& b.buffer_[2] == 255 && b.buffer_[3] == 48);
b.written = 2;
b += 2;
b ~= buf;
assert(b.capacity == 8);
assert(b._buffer[0] == 23 && b._buffer[1] == 255
&& b._buffer[2] == 48 && b._buffer[3] == 23 && b._buffer[4] == 255);
assert(b.buffer_[0] == 23 && b.buffer_[1] == 255
&& b.buffer_[2] == 48 && b.buffer_[3] == 23 && b.buffer_[4] == 255);
}
finalize(defaultAllocator, b);
b = make!WriteBuffer(defaultAllocator, 2);
///
unittest
{
auto b = WriteBuffer!ubyte(2);
ubyte[3] buf = [48, 23, 255];
b ~= buf;
assert(b.start == 0);
assert(b.capacity == 4);
assert(b.ring == 3);
assert(b.position == 3);
finalize(defaultAllocator, b);
}
/**
* Sets how many bytes were written. It will shrink the buffer
* appropriately. Always set this property after calling
* $(D_PSYMBOL buffer).
* appropriately. Always call it after $(D_PSYMBOL opIndex).
*
* Params:
* length = Length of the written data.
*
* Returns: $(D_KEYWORD this).
*/
@property void written(size_t length) @safe pure nothrow
ref WriteBuffer opOpAssign(string op)(in size_t length)
if (op == "+")
in
{
assert(length <= this.length);
@ -523,7 +561,7 @@ class WriteBuffer : Buffer
if (length <= 0)
{
return;
return this;
}
else if (position <= afterRing)
{
@ -537,19 +575,21 @@ class WriteBuffer : Buffer
{
auto overflow = position - afterRing;
if (overflow > length) {
_buffer[start.. start + length] = _buffer[afterRing.. afterRing + length];
_buffer[afterRing.. afterRing + length] = _buffer[afterRing + length ..position];
if (overflow > length)
{
immutable afterLength = afterRing + length;
buffer_[start .. start + length] = buffer_[afterRing .. afterLength];
buffer_[afterRing .. afterLength] = buffer_[afterLength .. position];
position -= length;
}
else if (overflow == length)
{
_buffer[start.. start + overflow] = _buffer[afterRing..position];
buffer_[start .. start + overflow] = buffer_[afterRing .. position];
position -= overflow;
}
else
{
_buffer[start.. start + overflow] = _buffer[afterRing..position];
buffer_[start .. start + overflow] = buffer_[afterRing .. position];
position = overflow;
}
start += length;
@ -568,74 +608,90 @@ class WriteBuffer : Buffer
{
start = 0;
}
return this;
}
///
unittest
{
auto b = make!WriteBuffer(defaultAllocator);
auto b = WriteBuffer!ubyte(6);
ubyte[6] buf = [23, 23, 255, 128, 127, 9];
b ~= buf;
assert(b.length == 6);
b.written = 2;
b += 2;
assert(b.length == 4);
b.written = 4;
b += 4;
assert(b.length == 0);
finalize(defaultAllocator, b);
}
/**
* Returns a pointer to a buffer chunk with data. You can pass it to
* a function that requires such a buffer.
* Returns a chunk with data.
*
* After calling it, set $(D_PSYMBOL written) to the length could be
* After calling it, set $(D_KEYWORD +=) to the length could be
* written.
*
* $(D_PSYMBOL buffer) may return only part of the data. You may need
* to call it (and set $(D_PSYMBOL written) several times until
* $(D_PSYMBOL opIndex) may return only part of the data. You may need
* to call it (and set $(D_KEYWORD +=) several times until
* $(D_PSYMBOL length) is 0. If all the data can be written,
* maximally 3 calls are required.
*
* Returns: A chunk of data buffer.
*/
@property void* buffer() @safe pure nothrow
T[] opSlice(in size_t start, in size_t end)
{
immutable internStart = this.start + start;
if (position > ring || position < start) // Buffer overflowed
{
return _buffer[start.. ring + 1].ptr;
return buffer_[this.start .. ring + 1 - length + end];
}
else
{
return _buffer[start..position].ptr;
return buffer_[this.start .. this.start + end];
}
}
///
unittest
{
auto b = make!WriteBuffer(defaultAllocator, 6);
auto b = WriteBuffer!ubyte(6);
ubyte[6] buf = [23, 23, 255, 128, 127, 9];
void* returnedBuf;
b ~= buf;
returnedBuf = b.buffer;
assert(returnedBuf[0..b.length] == buf[0..6]);
b.written = 2;
assert(b[0 .. $] == buf[0 .. 6]);
b += 2;
returnedBuf = b.buffer;
assert(returnedBuf[0..b.length] == buf[2..6]);
assert(b[0 .. $] == buf[2 .. 6]);
b ~= buf;
returnedBuf = b.buffer;
assert(returnedBuf[0..b.length] == buf[2..6]);
b.written = b.length;
assert(b[0 .. $] == buf[2 .. 6]);
b += b.length;
returnedBuf = b.buffer;
assert(returnedBuf[0..b.length] == buf[0..6]);
b.written = b.length;
assert(b[0 .. $] == buf[0 .. 6]);
b += b.length;
}
finalize(defaultAllocator, b);
/**
* After calling it, set $(D_KEYWORD +=) to the length could be
* written.
*
* $(D_PSYMBOL opIndex) may return only part of the data. You may need
* to call it (and set $(D_KEYWORD +=) several times until
* $(D_PSYMBOL length) is 0. If all the data can be written,
* maximally 3 calls are required.
*
* Returns: A chunk of data buffer.
*/
T[] opIndex()
{
return opSlice(0, length);
}
mixin DefaultAllocator;
}
private unittest
{
static assert(is(typeof(WriteBuffer!int(5))));
}

View File

@ -0,0 +1,45 @@
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
/*
* Internal package used by containers that rely on entries/nodes.
*
* Copyright: Eugene Wissner 2016.
* License: $(LINK2 https://www.mozilla.org/en-US/MPL/2.0/,
* Mozilla Public License, v. 2.0).
* Authors: $(LINK2 mailto:info@caraus.de, Eugene Wissner)
*/
module tanya.container.entry;
version (unittest)
{
package struct ConstEqualsStruct
{
int opEquals(typeof(this) that) const
{
return true;
}
}
package struct MutableEqualsStruct
{
int opEquals(typeof(this) that)
{
return true;
}
}
package struct NoEqualsStruct
{
}
}
package struct Entry(T)
{
/// Item content.
T content;
/// Next item.
Entry* next;
}

View File

@ -10,49 +10,51 @@
*/
module tanya.container.list;
import tanya.container.entry;
import tanya.memory;
/**
* Singly linked list.
* Singly-linked list.
*
* Params:
* T = Content type.
*/
class SList(T)
struct SList(T)
{
@nogc:
/**
* Creates a new $(D_PSYMBOL SList).
*
* Params:
* allocator = The allocator should be used for the element
* allocations.
*/
this(Allocator allocator = defaultAllocator)
{
this.allocator = allocator;
reset();
}
/**
* Removes all elements from the list.
*/
~this()
{
clear();
}
/**
* Removes all contents from the list.
*/
void clear()
{
while (!empty)
{
static if (isFinalizable!T)
{
finalize(allocator, front);
}
popFront();
}
}
///
unittest
{
SList!int l;
l.insertFront(8);
l.insertFront(5);
l.clear();
assert(l.empty);
}
/**
* Returns: First element.
*/
@property ref T front()
@property ref inout(T) front() inout
in
{
assert(!empty);
@ -68,64 +70,39 @@ class SList(T)
* Params:
* x = New element.
*/
@property void front(T x)
void insertFront(ref T x)
{
Entry* temp = make!Entry(allocator);
auto temp = allocator.make!(Entry!T);
temp.content = x;
temp.next = first.next;
first.next = temp;
}
///
unittest
/// Ditto.
void insertFront(T x)
{
auto l = make!(SList!int)(defaultAllocator);
int[2] values = [8, 9];
l.front = values[0];
assert(l.front == values[0]);
l.front = values[1];
assert(l.front == values[1]);
finalize(defaultAllocator, l);
insertFront(x);
}
/**
* Inserts a new element at the beginning.
*
* Params:
* x = New element.
*
* Returns: $(D_KEYWORD this).
*/
typeof(this) opOpAssign(string Op)(ref T x)
if (Op == "~")
{
front = x;
return this;
}
/// Ditto.
alias insert = insertFront;
///
unittest
{
auto l = make!(SList!int)(defaultAllocator);
int value = 5;
SList!int l;
assert(l.empty);
l ~= value;
assert(l.front == value);
assert(!l.empty);
finalize(defaultAllocator, l);
l.insertFront(8);
assert(l.front == 8);
l.insertFront(9);
assert(l.front == 9);
}
/**
* Returns: $(D_KEYWORD true) if the list is empty.
*/
@property bool empty() const @safe pure nothrow
@property bool empty() const
{
return first.next is null;
}
@ -135,7 +112,7 @@ class SList(T)
*
* Returns: The first element.
*/
T popFront()
void popFront()
in
{
assert(!empty);
@ -143,96 +120,60 @@ class SList(T)
body
{
auto n = first.next.next;
auto content = first.next.content;
finalize(allocator, first.next);
allocator.dispose(first.next);
first.next = n;
return content;
}
///
unittest
{
auto l = make!(SList!int)(defaultAllocator);
int[2] values = [8, 9];
SList!int l;
l.front = values[0];
l.front = values[1];
assert(l.front == values[1]);
l.insertFront(8);
l.insertFront(9);
assert(l.front == 9);
l.popFront();
assert(l.front == values[0]);
finalize(defaultAllocator, l);
assert(l.front == 8);
}
/**
* Returns the current item from the list and removes from the list.
* Removes $(D_PARAM howMany) elements from the list.
*
* Unlike $(D_PSYMBOL popFront()), this method doesn't fail, if it could not
* remove $(D_PARAM howMany) elements. Instead, if $(D_PARAM howMany) is
* greater than the list length, all elements are removed.
*
* Params:
* x = The item should be removed.
* howMany = How many elements should be removed.
*
* Returns: Removed item.
* Returns: The number of elements removed.
*/
T remove()
in
size_t removeFront(in size_t howMany = 1)
{
assert(!empty);
}
body
size_t i;
for (; i < howMany && !empty; ++i)
{
auto temp = position.next.next;
auto content = position.next.content;
finalize(allocator, position.next);
position.next = temp;
return content;
popFront();
}
return i;
}
/// Ditto.
alias remove = removeFront;
///
unittest
{
auto l = make!(SList!int)(defaultAllocator);
int[3] values = [8, 5, 4];
SList!int l;
l.front = values[0];
l.front = values[1];
assert(l.remove() == 5);
l.front = values[2];
assert(l.remove() == 4);
assert(l.remove() == 8);
assert(l.empty);
finalize(defaultAllocator, l);
}
/**
* Resets the current position.
*
* Returns: $(D_KEYWORD this).
*/
typeof(this) reset()
{
position = &first;
return this;
}
///
unittest
{
auto l = make!(SList!int)(defaultAllocator);
int[2] values = [8, 5];
l.current = values[0];
l.current = values[1];
assert(l.current == 5);
l.advance();
assert(l.current == 8);
l.reset();
assert(l.current == 5);
finalize(defaultAllocator, l);
l.insertFront(8);
l.insertFront(5);
l.insertFront(4);
assert(l.removeFront(0) == 0);
assert(l.removeFront(2) == 2);
assert(l.removeFront(3) == 1);
assert(l.removeFront(3) == 0);
}
/**
@ -241,165 +182,85 @@ class SList(T)
* Params:
* dg = $(D_KEYWORD foreach) body.
*/
int opApply(int delegate(ref size_t i, ref T) @nogc dg)
int opApply(scope int delegate(ref size_t i, ref T) dg)
{
int result;
size_t i;
for (position = first.next; position; position = position.next, ++i)
for (auto pos = first.next; pos; pos = pos.next, ++i)
{
result = dg(i, position.content);
result = dg(i, pos.content);
if (result != 0)
{
return result;
}
}
reset();
return result;
}
///
unittest
{
auto l = make!(SList!int)(defaultAllocator);
int[3] values = [5, 4, 9];
l.front = values[0];
l.front = values[1];
l.front = values[2];
foreach (i, e; l)
{
assert(i != 0 || e == values[2]);
assert(i != 1 || e == values[1]);
assert(i != 2 || e == values[0]);
}
finalize(defaultAllocator, l);
}
/// Ditto.
int opApply(int delegate(ref T) @nogc dg)
int opApply(scope int delegate(ref T) dg)
{
int result;
for (position = first.next; position; position = position.next)
for (auto pos = first.next; pos; pos = pos.next)
{
result = dg(position.content);
result = dg(pos.content);
if (result != 0)
{
return result;
}
}
reset();
return result;
}
///
unittest
{
auto l = make!(SList!int)(defaultAllocator);
int[3] values = [5, 4, 9];
size_t i;
SList!int l;
l.front = values[0];
l.front = values[1];
l.front = values[2];
foreach (e; l)
l.insertFront(5);
l.insertFront(4);
l.insertFront(9);
foreach (i, e; l)
{
assert(i != 0 || e == values[2]);
assert(i != 1 || e == values[1]);
assert(i != 2 || e == values[0]);
++i;
assert(i != 0 || e == 9);
assert(i != 1 || e == 4);
assert(i != 2 || e == 5);
}
finalize(defaultAllocator, l);
}
/**
* Returns: $(D_KEYWORD true) if the current position is the end position.
*/
@property bool end() const
{
return empty || position.next.next is null;
}
/**
* Moves to the next element and returns it.
*
* Returns: The element on the next position.
*/
T advance()
in
{
assert(!end);
}
body
{
position = position.next;
return position.content;
}
/**
* Returns: Element on the current position.
*/
@property ref T current()
in
{
assert(!empty);
}
body
{
return position.next.content;
}
/**
* Inserts a new element at the current position.
*
* Params:
* x = New element.
*/
@property void current(T x)
{
Entry* temp = make!Entry(allocator);
temp.content = x;
temp.next = position.next;
position.next = temp;
}
/**
* List entry.
*/
protected struct Entry
{
/// List item content.
T content;
/// Next list item.
Entry* next;
}
/// 0th element of the list.
protected Entry first;
private Entry!T first;
/// Current position in the list.
protected Entry* position;
private Allocator allocator;
}
interface Stuff
{
mixin DefaultAllocator;
}
///
unittest
{
auto l = make!(SList!Stuff)(defaultAllocator);
SList!int l;
size_t i;
finalize(defaultAllocator, l);
l.insertFront(5);
l.insertFront(4);
l.insertFront(9);
foreach (e; l)
{
assert(i != 0 || e == 9);
assert(i != 1 || e == 4);
assert(i != 2 || e == 5);
++i;
}
assert(i == 3);
}
private unittest
{
interface Stuff
{
}
static assert(is(SList!Stuff));
}

View File

@ -10,47 +10,187 @@
*/
module tanya.container.queue;
import tanya.container.entry;
import std.traits;
import tanya.memory;
/**
* Queue.
* FIFO queue.
*
* Params:
* T = Content type.
*/
class Queue(T)
struct Queue(T)
{
@nogc:
/**
* Creates a new $(D_PSYMBOL Queue).
*
* Params:
* allocator = The allocator should be used for the element
* allocations.
*/
this(Allocator allocator = defaultAllocator)
{
this.allocator = allocator;
}
/**
* Removes all elements from the queue.
*/
~this()
{
foreach (e; this)
clear();
}
/**
* Removes all elements from the queue.
*/
void clear()
{
static if (isFinalizable!T)
while (!empty)
{
finalize(allocator, e);
popFront();
}
}
///
unittest
{
Queue!int q;
assert(q.empty);
q.insertBack(8);
q.insertBack(9);
q.clear();
assert(q.empty);
}
/**
* Returns how many elements are in the queue. It iterates through the queue
* to count the elements.
*
* Returns: How many elements are in the queue.
*/
size_t length() const
{
size_t len;
for (const(Entry!T)* i = first.next; i !is null; i = i.next)
{
++len;
}
return len;
}
///
unittest
{
Queue!int q;
assert(q.length == 0);
q.insertBack(5);
assert(q.length == 1);
q.insertBack(4);
assert(q.length == 2);
q.insertBack(9);
assert(q.length == 3);
q.popFront();
assert(q.length == 2);
q.popFront();
assert(q.length == 1);
q.popFront();
assert(q.length == 0);
}
version (D_Ddoc)
{
/**
* Compares two queues. Checks if all elements of the both queues are equal.
*
* Returns: Whether $(D_KEYWORD this) and $(D_PARAM that) are equal.
*/
int opEquals(ref typeof(this) that);
/// Ditto.
int opEquals(typeof(this) that);
}
else static if (!hasMember!(T, "opEquals")
|| (functionAttributes!(T.opEquals) & FunctionAttribute.const_))
{
bool opEquals(in ref typeof(this) that) const
{
const(Entry!T)* i = first.next;
const(Entry!T)* j = that.first.next;
while (i !is null && j !is null)
{
if (i.content != j.content)
{
return false;
}
i = i.next;
j = j.next;
}
return i is null && j is null;
}
/// Ditto.
bool opEquals(in typeof(this) that) const
{
return opEquals(that);
}
}
else
{
/**
* Compares two queues. Checks if all elements of the both queues are equal.
*
* Returns: How many elements are in the queue.
*/
bool opEquals(ref typeof(this) that)
{
Entry!T* i = first.next;
Entry!T* j = that.first.next;
while (i !is null && j !is null)
{
if (i.content != j.content)
{
return false;
}
i = i.next;
j = j.next;
}
return i is null && j is null;
}
/// Ditto.
bool opEquals(typeof(this) that)
{
return opEquals(that);
}
}
///
unittest
{
Queue!int q1, q2;
q1.insertBack(5);
q1.insertBack(4);
q2.insertBack(5);
assert(q1 != q2);
q2.insertBack(4);
assert(q1 == q2);
q2.popFront();
assert(q1 != q2);
q1.popFront();
assert(q1 == q2);
q1.popFront();
q2.popFront();
assert(q1 == q2);
}
private unittest
{
static assert(is(Queue!ConstEqualsStruct));
static assert(is(Queue!MutableEqualsStruct));
static assert(is(Queue!NoEqualsStruct));
}
/**
* Returns: First element.
*/
@property ref T front()
@property ref inout(T) front() inout
in
{
assert(!empty);
@ -65,12 +205,10 @@ class Queue(T)
*
* Params:
* x = New element.
*
* Returns: $(D_KEYWORD this).
*/
typeof(this) insertBack(T x)
void insertBack(ref T x)
{
Entry* temp = make!Entry(allocator);
auto temp = allocator.make!(Entry!T);
temp.content = x;
@ -83,60 +221,33 @@ class Queue(T)
rear.next = temp;
rear = rear.next;
}
return this;
}
/// Ditto.
void insertBack(T x)
{
insertBack(x);
}
/// Ditto.
alias insert = insertBack;
///
unittest
{
auto q = make!(Queue!int)(defaultAllocator);
int[2] values = [8, 9];
q.insertBack(values[0]);
assert(q.front is values[0]);
q.insertBack(values[1]);
assert(q.front is values[0]);
finalize(defaultAllocator, q);
}
/**
* Inserts a new element.
*
* Params:
* x = New element.
*
* Returns: $(D_KEYWORD this).
*/
typeof(this) opOpAssign(string Op)(ref T x)
if (Op == "~")
{
return insertBack(x);
}
///
unittest
{
auto q = make!(Queue!int)(defaultAllocator);
int value = 5;
Queue!int q;
assert(q.empty);
q ~= value;
assert(q.front == value);
assert(!q.empty);
finalize(defaultAllocator, q);
q.insertBack(8);
assert(q.front == 8);
q.insertBack(9);
assert(q.front == 8);
}
/**
* Returns: $(D_KEYWORD true) if the queue is empty.
*/
@property bool empty() const @safe pure nothrow
@property bool empty() const
{
return first.next is null;
}
@ -144,76 +255,145 @@ class Queue(T)
///
unittest
{
auto q = make!(Queue!int)(defaultAllocator);
Queue!int q;
int value = 7;
assert(q.empty);
q.insertBack(value);
assert(!q.empty);
finalize(defaultAllocator, q);
}
/**
* Move position to the next element.
*
* Returns: $(D_KEYWORD this).
* Move the position to the next element.
*/
typeof(this) popFront()
void popFront()
in
{
assert(!empty);
assert(allocator !is null);
}
body
{
auto n = first.next.next;
finalize(allocator, first.next);
dispose(allocator, first.next);
first.next = n;
return this;
}
///
unittest
{
auto q = make!(Queue!int)(defaultAllocator);
int[2] values = [8, 9];
Queue!int q;
q.insertBack(values[0]);
q.insertBack(values[1]);
assert(q.front is values[0]);
q.insertBack(8);
q.insertBack(9);
assert(q.front == 8);
q.popFront();
assert(q.front is values[1]);
finalize(defaultAllocator, q);
assert(q.front == 9);
}
/**
* Queue entry.
* $(D_KEYWORD foreach) iteration. The elements will be automatically
* dequeued.
*
* Params:
* dg = $(D_KEYWORD foreach) body.
*/
protected struct Entry
int opApply(scope int delegate(ref size_t i, ref T) @nogc dg)
{
/// Queue item content.
T content;
int result;
/// Next list item.
Entry* next;
for (size_t i = 0; !empty; ++i)
{
if ((result = dg(i, front)) != 0)
{
return result;
}
popFront();
}
return result;
}
/// The first element of the list.
protected Entry first;
/// Ditto.
int opApply(scope int delegate(ref T) @nogc dg)
{
int result;
/// The last element of the list.
protected Entry* rear;
private Allocator allocator;
while (!empty)
{
if ((result = dg(front)) != 0)
{
return result;
}
popFront();
}
return result;
}
///
unittest
{
auto q = make!(Queue!int)(defaultAllocator);
Queue!int q;
finalize(defaultAllocator, q);
size_t j;
q.insertBack(5);
q.insertBack(4);
q.insertBack(9);
foreach (i, e; q)
{
assert(i != 2 || e == 9);
assert(i != 1 || e == 4);
assert(i != 0 || e == 5);
++j;
}
assert(j == 3);
assert(q.empty);
j = 0;
q.insertBack(5);
q.insertBack(4);
q.insertBack(9);
foreach (e; q)
{
assert(j != 2 || e == 9);
assert(j != 1 || e == 4);
assert(j != 0 || e == 5);
++j;
}
assert(j == 3);
assert(q.empty);
}
/// The first element of the list.
private Entry!T first;
/// The last element of the list.
private Entry!T* rear;
mixin DefaultAllocator;
}
///
unittest
{
Queue!int q;
q.insertBack(5);
assert(!q.empty);
q.insertBack(4);
assert(q.front == 5);
q.insertBack(9);
assert(q.front == 5);
q.popFront();
assert(q.front == 4);
foreach (i, ref e; q)
{
assert(i != 0 || e == 4);
assert(i != 1 || e == 9);
}
assert(q.empty);
}

File diff suppressed because it is too large Load Diff

510
source/tanya/crypto/bit.d Normal file
View File

@ -0,0 +1,510 @@
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
/**
* Copyright: Eugene Wissner 2016.
* License: $(LINK2 https://www.mozilla.org/en-US/MPL/2.0/,
* Mozilla Public License, v. 2.0).
* Authors: $(LINK2 mailto:belka@caraus.de, Eugene Wissner)
*/
module tanya.crypto.bit;
/**
* Wrapper that allows bit manipulation on $(D_KEYWORD ubyte[]) array.
*/
struct BitVector
{
protected ubyte[] vector;
/**
* Params:
* array = Array should be manipulated on.
*/
this(inout(ubyte[]) array) inout pure nothrow @safe @nogc
in
{
assert(array.length <= size_t.max / 8);
assert(array !is null);
}
body
{
vector = array;
}
///
unittest
{
ubyte[5] array1 = [234, 3, 252, 10, 18];
ubyte[3] array2 = [65, 13, 173];
auto bits = BitVector(array1);
assert(bits[] is array1);
assert(bits[] !is array2);
bits = BitVector(array2);
assert(bits[] is array2);
}
/**
* Returns: Number of bits in the vector.
*/
@property inout(size_t) length() inout const pure nothrow @safe @nogc
{
return vector.length * 8;
}
/// Ditto.
inout(size_t) opDollar() inout const pure nothrow @safe @nogc
{
return vector.length * 8;
}
///
unittest
{
// [01000001, 00001101, 10101101]
ubyte[3] arr = [65, 13, 173];
auto bits = BitVector(arr);
assert(bits.length == 24);
}
/**
* Params:
* bit = Bit position.
*
* Returns: $(D_KEYWORD true) if the bit on position $(D_PARAM bit) is set,
* $(D_KEYWORD false) if not set.
*/
inout(bool) opIndex(size_t bit) inout const pure nothrow @safe @nogc
in
{
assert(bit / 8 <= vector.length);
}
body
{
return (vector[bit / 8] & (0x80 >> (bit % 8))) != 0;
}
///
unittest
{
// [01000001, 00001101, 10101101]
ubyte[3] arr = [65, 13, 173];
auto bits = BitVector(arr);
assert(!bits[0]);
assert(bits[1]);
assert(bits[7]);
assert(!bits[8]);
assert(!bits[11]);
assert(bits[12]);
assert(bits[20]);
assert(bits[23]);
}
/**
* Returns: Underlying array.
*/
inout(ubyte[]) opIndex() inout pure nothrow @safe @nogc
{
return vector;
}
///
unittest
{
// [01000001, 00001101, 10101101]
ubyte[3] arr = [65, 13, 173];
auto bits = BitVector(arr);
assert(bits[] is arr);
}
/**
* Params:
* value = $(D_KEYWORD true) if the bit should be set,
* $(D_KEYWORD false) if cleared.
* bit = Bit position.
*
* Returns: $(D_PSYMBOL this).
*/
bool opIndexAssign(bool value, size_t bit) pure nothrow @safe @nogc
in
{
assert(bit / 8 <= vector.length);
}
body
{
if (value)
{
vector[bit / 8] |= (0x80 >> (bit % 8));
}
else
{
vector[bit / 8] &= ~(0x80 >> (bit % 8));
}
return value;
}
///
unittest
{
// [01000001, 00001101, 10101101]
ubyte[3] arr = [65, 13, 173];
auto bits = BitVector(arr);
bits[5] = bits[6] = true;
assert(bits[][0] == 71);
bits[14] = true;
bits[15] = false;
assert(bits[][1] == 14);
bits[16] = bits[23] = false;
assert(bits[][2] == 44);
}
/**
* Copies bits from $(D_PARAM vector) into this $(D_PSYMBOL BitVector).
*
* The array that should be assigned, can be smaller (but not larger) than
* the underlying array of this $(D_PSYMBOL BitVector), leading zeros will
* be added in this case to the left.
*
* Params:
* vector = $(D_KEYWORD ubyte[]) array not larger than
* `$(D_PSYMBOL length) / 8`.
*
* Returns: $(D_KEYWORD this).
*/
BitVector opAssign(ubyte[] vector) pure nothrow @safe @nogc
in
{
assert(vector.length <= this.vector.length);
}
body
{
immutable delta = this.vector.length - vector.length;
if (delta > 0)
{
this.vector[0..delta] = 0;
}
this.vector[delta..$] = vector[0..$];
return this;
}
///
unittest
{
ubyte[5] array1 = [234, 3, 252, 10, 18];
ubyte[3] array2 = [65, 13, 173];
auto bits = BitVector(array1);
bits = array2;
assert(bits[][0] == 0);
assert(bits[][1] == 0);
assert(bits[][2] == 65);
assert(bits[][3] == 13);
assert(bits[][4] == 173);
bits = array2[0..2];
assert(bits[][0] == 0);
assert(bits[][1] == 0);
assert(bits[][2] == 0);
assert(bits[][3] == 65);
assert(bits[][4] == 13);
}
/**
* Support for bitwise operations.
*
* Params:
* that = Another bit vector.
*
* Returns: $(D_KEYWORD this).
*/
BitVector opOpAssign(string op)(BitVector that) pure nothrow @safe @nogc
if ((op == "^") || (op == "|") || (op == "&"))
{
return opOpAssign(op)(that.vector);
}
/// Ditto.
BitVector opOpAssign(string op)(ubyte[] that) pure nothrow @safe @nogc
if ((op == "^") || (op == "|") || (op == "&"))
in
{
assert(that.length <= vector.length);
}
body
{
for (int i = cast(int) vector.length - 1; i >= 0; --i)
{
mixin("vector[i] " ~ op ~ "= " ~ "that[i];");
}
immutable delta = vector.length - that.length;
if (delta)
{
static if (op == "&")
{
vector[0..delta] = 0;
}
}
return this;
}
///
unittest
{
// [01000001, 00001101, 10101101]
ubyte[3] array1 = [65, 13, 173];
ubyte[3] array2 = [0b01010010, 0b10111110, 0b10111110];
auto bits = BitVector(array1);
bits |= array2;
assert(bits[][0] == 0b01010011);
assert(bits[][1] == 0b10111111);
assert(bits[][2] == 0b10111111);
bits &= array2;
assert(bits[][0] == array2[0]);
assert(bits[][1] == array2[1]);
assert(bits[][2] == array2[2]);
bits ^= array2;
assert(bits[][0] == 0);
assert(bits[][1] == 0);
assert(bits[][2] == 0);
}
/**
* Support for shift operations.
*
* Params:
* n = Number of bits.
*
* Returns: $(D_KEYWORD this).
*/
BitVector opOpAssign(string op)(in size_t n) pure nothrow @safe @nogc
if ((op == "<<") || (op == ">>"))
{
if (n >= length)
{
vector[0..$] = 0;
}
else if (n != 0)
{
immutable bit = n % 8, step = n / 8;
immutable delta = 8 - bit;
size_t i, j;
static if (op == "<<")
{
for (j = step; j < vector.length - 1; ++i)
{
vector[i] = cast(ubyte)((vector[j] << bit)
| vector[++j] >> delta);
}
vector[i] = cast(ubyte)(vector[j] << bit);
vector[$ - step ..$] = 0;
}
else static if (op == ">>")
{
for (i = vector.length - 1, j = i - step; j > 0; --i)
{
vector[i] = cast(ubyte)((vector[j] >> bit)
| vector[--j] << delta);
}
vector[i] = cast(ubyte)(vector[j] >> bit);
vector[0..step] = 0;
}
}
return this;
}
///
nothrow @safe @nogc unittest
{
ubyte[4] arr = [0b10111110, 0b11110010, 0b01010010, 0b01010011];
auto bits = BitVector(arr);
bits <<= 0;
assert(bits[][0] == 0b10111110 && bits[][1] == 0b11110010
&& bits[][2] == 0b01010010 && bits[][3] == 0b01010011);
bits <<= 2;
assert(bits[][0] == 0b11111011 && bits[][1] == 0b11001001
&& bits[][2] == 0b01001001 && bits[][3] == 0b01001100);
bits <<= 4;
assert(bits[][0] == 0b10111100 && bits[][1] == 0b10010100
&& bits[][2] == 0b10010100 && bits[][3] == 0b11000000);
bits <<= 8;
assert(bits[][0] == 0b10010100 && bits[][1] == 0b10010100
&& bits[][2] == 0b11000000 && bits[][3] == 0b00000000);
bits <<= 7;
assert(bits[][0] == 0b01001010 && bits[][1] == 0b01100000
&& bits[][2] == 0b00000000 && bits[][3] == 0b00000000);
bits <<= 25;
assert(bits[][0] == 0b00000000 && bits[][1] == 0b00000000
&& bits[][2] == 0b00000000 && bits[][3] == 0b00000000);
arr = [0b00110011, 0b11001100, 0b11111111, 0b01010101];
bits <<= 24;
assert(bits[][0] == 0b01010101 && bits[][1] == 0b00000000
&& bits[][2] == 0b00000000 && bits[][3] == 0b00000000);
arr[1] = 0b11001100;
arr[2] = 0b11111111;
arr[3] = 0b01010101;
bits <<= 12;
assert(bits[][0] == 0b11001111 && bits[][1] == 0b11110101
&& bits[][2] == 0b01010000 && bits[][3] == 0b00000000);
bits <<= 100;
assert(bits[][0] == 0b00000000 && bits[][1] == 0b00000000
&& bits[][2] == 0b00000000 && bits[][3] == 0b00000000);
arr = [0b10111110, 0b11110010, 0b01010010, 0b01010011];
bits >>= 0;
assert(bits[][0] == 0b10111110 && bits[][1] == 0b11110010
&& bits[][2] == 0b01010010 && bits[][3] == 0b01010011);
bits >>= 2;
assert(bits[][0] == 0b00101111 && bits[][1] == 0b10111100
&& bits[][2] == 0b10010100 && bits[][3] == 0b10010100);
bits >>= 4;
assert(bits[][0] == 0b00000010 && bits[][1] == 0b11111011
&& bits[][2] == 0b11001001 && bits[][3] == 0b01001001);
bits >>= 8;
assert(bits[][0] == 0b00000000 && bits[][1] == 0b00000010
&& bits[][2] == 0b11111011 && bits[][3] == 0b11001001);
bits >>= 7;
assert(bits[][0] == 0b00000000 && bits[][1] == 0b00000000
&& bits[][2] == 0b00000101 && bits[][3] == 0b11110111);
bits >>= 25;
assert(bits[][0] == 0b00000000 && bits[][1] == 0b00000000
&& bits[][2] == 0b00000000 && bits[][3] == 0b00000000);
arr = [0b00110011, 0b11001100, 0b11111111, 0b01010101];
bits >>= 24;
assert(bits[][0] == 0b00000000 && bits[][1] == 0b00000000
&& bits[][2] == 0b00000000 && bits[][3] == 0b00110011);
arr[1] = 0b11001100;
arr[2] = 0b11111111;
arr[3] = 0b01010101;
bits >>= 12;
assert(bits[][0] == 0b00000000 && bits[][1] == 0b00000000
&& bits[][2] == 0b00001100 && bits[][3] == 0b11001111);
bits >>= 100;
assert(bits[][0] == 0b00000000 && bits[][1] == 0b00000000
&& bits[][2] == 0b00000000 && bits[][3] == 0b00000000);
}
/**
* Negates all bits.
*
* Returns: $(D_KEYWORD this).
*/
BitVector opUnary(string op)() pure nothrow @safe @nogc
if (op == "~")
{
foreach (ref b; vector)
{
b = ~b;
}
return this;
}
///
unittest
{
// [01000001, 00001101, 10101101]
ubyte[3] arr = [65, 13, 173];
auto bits = BitVector(arr);
~bits;
assert(bits[][0] == 0b10111110);
assert(bits[][1] == 0b11110010);
assert(bits[][2] == 0b01010010);
}
/**
* Iterates through all bits.
*
* Params:
* dg = $(D_KEYWORD foreach) delegate.
*
* Returns: By $(D_PARAM dg) returned value.
*/
int opApply(int delegate(size_t, bool) dg)
{
int result;
foreach (i, ref v; vector)
{
foreach (c; 0..8)
{
result = dg(i * 8 + c, (v & (0x80 >> c)) != 0);
if (result)
{
return result;
}
}
}
return result;
}
/// Ditto.
int opApply(int delegate(bool) dg)
{
int result;
foreach (ref v; vector)
{
foreach (c; 0..8)
{
result = dg((v & (0x80 >> c)) != 0);
if (result)
{
return result;
}
}
}
return result;
}
///
unittest
{
ubyte[2] arr = [0b01000001, 0b00001101];
auto bits = BitVector(arr);
size_t c;
foreach (i, v; bits)
{
assert(i == c);
if (i == 1 || i == 7 || i == 15 || i == 13 || i == 12)
{
assert(v);
}
else
{
assert(!v);
}
++c;
}
assert(c == 16);
}
}

607
source/tanya/crypto/des.d Normal file
View File

@ -0,0 +1,607 @@
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
/**
* Copyright: Eugene Wissner 2016.
* License: $(LINK2 https://www.mozilla.org/en-US/MPL/2.0/,
* Mozilla Public License, v. 2.0).
* Authors: $(LINK2 mailto:belka@caraus.de, Eugene Wissner)
*/
module tanya.crypto.des;
import tanya.crypto.bit;
import tanya.crypto.symmetric;
/// Initial permutation table.
private immutable ubyte[64] ipTable = [58, 50, 42, 34, 26, 18, 10, 2,
60, 52, 44, 36, 28, 20, 12, 4,
62, 54, 46, 38, 30, 22, 14, 6,
64, 56, 48, 40, 32, 24, 16, 8,
57, 49, 41, 33, 25, 17, 9, 1,
59, 51, 43, 35, 27, 19, 11, 3,
61, 53, 45, 37, 29, 21, 13, 5,
63, 55, 47, 39, 31, 23, 15, 7];
/// Final permutation table.
private immutable ubyte[64] fpTable = [40, 8, 48, 16, 56, 24, 64, 32,
39, 7, 47, 15, 55, 23, 63, 31,
38, 6, 46, 14, 54, 22, 62, 30,
37, 5, 45, 13, 53, 21, 61, 29,
36, 4, 44, 12, 52, 20, 60, 28,
35, 3, 43, 11, 51, 19, 59, 27,
34, 2, 42, 10, 50, 18, 58, 26,
33, 1, 41, 9, 49, 17, 57, 25];
/// Key permutation table 1.
private immutable ubyte[64] pc1Table = [57, 49, 41, 33, 25, 17, 9, 1,
58, 50, 42, 34, 26, 18, 10, 2,
59, 51, 43, 35, 27, 19, 11, 3,
60, 52, 44, 36, 63, 55, 47, 39,
31, 23, 15, 7, 62, 54, 46, 38,
30, 22, 14, 6, 61, 53, 45, 37,
29, 21, 13, 5, 28, 20, 12, 4];
/// Key permutation table 2.
private immutable ubyte[48] pc2Table = [14, 17, 11, 24, 1, 5, 3, 28,
15, 6, 21, 10, 23, 19, 12, 4,
26, 8, 16, 7, 27, 20, 13, 2,
41, 52, 31, 37, 47, 55, 30, 40,
51, 45, 33, 48, 44, 49, 39, 56,
34, 53, 46, 42, 50, 36, 29, 32];
/// Expansion table.
private immutable ubyte[48] expansionTable = [32, 1, 2, 3, 4, 5, 4, 5,
6, 7, 8, 9, 8, 9, 10, 11,
12, 13, 12, 13, 14, 15, 16, 17,
16, 17, 18, 19, 20, 21, 20, 21,
22, 23, 24, 25, 24, 25, 26, 27,
28, 29, 28, 29, 30, 31, 32, 1];
/// Final input block permutation.
private immutable ubyte[32] pTable = [16, 7, 20, 21, 29, 12, 28, 17,
1, 15, 23, 26, 5, 18, 31, 10,
2, 8, 24, 14, 32, 27, 3, 9,
19, 13, 30, 6, 22, 11, 4, 25];
/// The (in)famous S-boxes.
private immutable ubyte[64][8] sBox = [[
14, 0, 4, 15, 13, 7, 1, 4, 2, 14, 15, 2, 11, 13, 8, 1,
3, 10, 10, 6, 6, 12, 12, 11, 5, 9, 9, 5, 0, 3, 7, 8,
4, 15, 1, 12, 14, 8, 8, 2, 13, 4, 6, 9, 2, 1, 11, 7,
15, 5, 12, 11, 9, 3, 7, 14, 3, 10, 10, 0, 5, 6, 0, 13,
],[
15, 3, 1, 13, 8, 4, 14, 7, 6, 15, 11, 2, 3, 8, 4, 14,
9, 12, 7, 0, 2, 1, 13, 10, 12, 6, 0, 9, 5, 11, 10, 5,
0, 13, 14, 8, 7, 10, 11, 1, 10, 3, 4, 15, 13, 4, 1, 2,
5, 11, 8, 6, 12, 7, 6, 12, 9, 0, 3, 5, 2, 14, 15, 9,
],[
10, 13, 0, 7, 9, 0, 14, 9, 6, 3, 3, 4, 15, 6, 5, 10,
1, 2, 13, 8, 12, 5, 7, 14, 11, 12, 4, 11, 2, 15, 8, 1,
13, 1, 6, 10, 4, 13, 9, 0, 8, 6, 15, 9, 3, 8, 0, 7,
11, 4, 1, 15, 2, 14, 12, 3, 5, 11, 10, 5, 14, 2, 7, 12,
],[
7, 13, 13, 8, 14, 11, 3, 5, 0, 6, 6, 15, 9, 0, 10, 3,
1, 4, 2, 7, 8, 2, 5, 12, 11, 1, 12, 10, 4, 14, 15, 9,
10, 3, 6, 15, 9, 0, 0, 6, 12, 10, 11, 1, 7, 13, 13, 8,
15, 9, 1, 4, 3, 5, 14, 11, 5, 12, 2, 7, 8, 2, 4, 14,
],[
2, 14, 12, 11, 4, 2, 1, 12, 7, 4, 10, 7, 11, 13, 6, 1,
8, 5, 5, 0, 3, 15, 15, 10, 13, 3, 0, 9, 14, 8, 9, 6,
4, 11, 2, 8, 1, 12, 11, 7, 10, 1, 13, 14, 7, 2, 8, 13,
15, 6, 9, 15, 12, 0, 5, 9, 6, 10, 3, 4, 0, 5, 14, 3,
],[
12, 10, 1, 15, 10, 4, 15, 2, 9, 7, 2, 12, 6, 9, 8, 5,
0, 6, 13, 1, 3, 13, 4, 14, 14, 0, 7, 11, 5, 3, 11, 8,
9, 4, 14, 3, 15, 2, 5, 12, 2, 9, 8, 5, 12, 15, 3, 10,
7, 11, 0, 14, 4, 1, 10, 7, 1, 6, 13, 0, 11, 8, 6, 13,
],[
4, 13, 11, 0, 2, 11, 14, 7, 15, 4, 0, 9, 8, 1, 13, 10,
3, 14, 12, 3, 9, 5, 7, 12, 5, 2, 10, 15, 6, 8, 1, 6,
1, 6, 4, 11, 11, 13, 13, 8, 12, 1, 3, 4, 7, 10, 14, 7,
10, 9, 15, 5, 6, 0, 8, 15, 0, 14, 5, 2, 9, 3, 2, 12,
],[
13, 1, 2, 15, 8, 13, 4, 8, 6, 10, 15, 3, 11, 7, 1, 4,
10, 12, 9, 5, 3, 6, 14, 11, 5, 0, 0, 14, 12, 9, 7, 2,
7, 2, 11, 1, 4, 14, 1, 7, 9, 4, 12, 10, 14, 8, 2, 13,
0, 15, 6, 12, 10, 9, 13, 0, 15, 3, 3, 5, 5, 6, 8, 11,
]];
/**
* Data Encryption Standard.
*
* Params:
* L = Number of keys.
*/
class DES(ushort L = 1) : BlockCipher
if (L == 1)
{
mixin FixedBlockSize!8;
mixin KeyLength!8;
private enum expansionBlockSize = 6;
private enum pc1KeyLength = 7;
private enum subkeyLength = 6;
private ubyte[] key_;
/**
* Params:
* key = Key.
*/
@property void key(ubyte[] key) pure nothrow @safe @nogc
in
{
assert(key.length >= minKeyLength);
assert(key.length <= maxKeyLength);
}
body
{
key_ = key;
}
/**
* Encrypts a block.
*
* Params:
* plain = Plain text, input.
* cipher = Cipher text, output.
*/
void encrypt(in ubyte[] plain, ubyte[] cipher)
nothrow
in
{
assert(plain.length == blockSize);
assert(cipher.length == blockSize);
}
body
{
operateBlock!(Direction.encryption)(plain, cipher);
}
/**
* Decrypts a block.
*
* Params:
* cipher = Cipher text, input.
* plain = Plain text, output.
*/
void decrypt(in ubyte[] cipher, ubyte[] plain)
nothrow
in
{
assert(plain.length == blockSize);
assert(cipher.length == blockSize);
}
body
{
operateBlock!(Direction.decryption)(cipher, plain);
}
private void operateBlock(Direction D)(in ubyte[] source, ref ubyte[] target)
{
ubyte[blockSize_] ipBlock;
ubyte[expansionBlockSize] expansionBlock;
ubyte[4] substitutionBlock;
ubyte[4] pBoxTarget;
ubyte[pc1KeyLength] pc1Key;
ubyte[subkeyLength] subkey;
// Initial permutation
permute(source, ipBlock, ipTable, blockSize);
// Key schedule computation
permute(key_, pc1Key, pc1Table, pc1KeyLength);
// Feistel function
for (ubyte round; round < 16; ++round)
{
auto bitVector = BitVector(expansionBlock);
/* Expansion. This permutation only looks at the first 4 bytes (32
bits of ipBlock); 16 of these are repeated in expansion table.*/
permute(BitVector(ipBlock[4..$]), bitVector, expansionTable, 6);
// Key mixing
static if (D == Direction.encryption)
{
rotateLeft(pc1Key);
if (!(round <= 1 || round == 8 || round == 15))
{
// Rotate twice.
rotateLeft(pc1Key);
}
}
permute(pc1Key, subkey, pc2Table, subkeyLength);
static if (D == Direction.decryption)
{
rotateRight(pc1Key);
if (!(round >= 14 || round == 7 || round == 0))
{
// Rotate twice.
rotateRight(pc1Key);
}
}
bitVector ^= subkey;
// Substitution; copy from updated expansion block to ciphertext block
substitutionBlock[0] = cast(ubyte) (sBox[0][(expansionBlock[0] & 0xfc ) >> 2] << 4);
substitutionBlock[0] |= sBox[1][(expansionBlock[0] & 0x03) << 4 | (expansionBlock[1] & 0xf0) >> 4];
substitutionBlock[1] = cast(ubyte) (sBox[2][(expansionBlock[1] & 0x0f) << 2 | (expansionBlock[2] & 0xc0) >> 6] << 4);
substitutionBlock[1] |= sBox[3][(expansionBlock[2] & 0x3f)];
substitutionBlock[2] = cast(ubyte) (sBox[4][(expansionBlock[3] & 0xfc) >> 2 ] << 4);
substitutionBlock[2] |= sBox[5][(expansionBlock[3] & 0x03) << 4 | (expansionBlock[4] & 0xf0) >> 4];
substitutionBlock[3] = cast(ubyte) (sBox[6][(expansionBlock[4] & 0x0F) << 2 | (expansionBlock[5] & 0xc0) >> 6] << 4);
substitutionBlock[3] |= sBox[7][(expansionBlock[5] & 0x3f)];
// Permutation
bitVector = BitVector(substitutionBlock);
permute(bitVector, pBoxTarget, pTable, blockSize / 2);
// Swap the halves.
substitutionBlock = ipBlock[0..4];
ipBlock[0..4] = ipBlock[4..$];
bitVector ^= pBoxTarget;
ipBlock[4..$] = substitutionBlock;
}
substitutionBlock = ipBlock[0..4];
ipBlock[0..4] = ipBlock[4..$];
ipBlock[4..$] = substitutionBlock;
// Final permutaion (undo initial permuation).
permute(ipBlock, target, fpTable, blockSize);
}
/**
* Performs the left rotation operation on the key.
*
* Params:
* key = The key to rotate.
*/
private void rotateLeft(ref ubyte[7] key) const pure nothrow @safe @nogc
{
immutable carryLeft = (key[0] & 0x80) >> 3;
key[0] = cast(ubyte) ((key[0] << 1) | ((key[1] & 0x80) >> 7));
key[1] = cast(ubyte) ((key[1] << 1) | ((key[2] & 0x80) >> 7));
key[2] = cast(ubyte) ((key[2] << 1) | ((key[3] & 0x80) >> 7));
immutable carryRight = (key[3] & 0x08) >> 3;
key[3] = cast(ubyte) ((((key[3] << 1) | ((key[4] & 0x80) >> 7)) & ~0x10) | carryLeft);
key[4] = cast(ubyte) ((key[4] << 1) | ((key[5] & 0x80) >> 7));
key[5] = cast(ubyte) ((key[5] << 1) | ((key[6] & 0x80) >> 7));
key[6] = cast(ubyte) ((key[6] << 1) | carryRight);
}
/**
* Performs the right rotation operation on the key.
*
* Params:
* key = The key to rotate.
*/
private void rotateRight(ref ubyte[7] key) const pure nothrow @safe @nogc
{
immutable carryRight = (key[6] & 0x01) << 3;
key[6] = cast(ubyte) ((key[6] >> 1) | ((key[5] & 0x01) << 7));
key[5] = cast(ubyte) ((key[5] >> 1) | ((key[4] & 0x01) << 7));
key[4] = cast(ubyte) ((key[4] >> 1) | ((key[3] & 0x01) << 7));
immutable carryLeft = (key[3] & 0x10) << 3;
key[3] = cast(ubyte) ((((key[3] >> 1) | ((key[2] & 0x01) << 7)) & ~0x08) | carryRight);
key[2] = cast(ubyte) ((key[2] >> 1) | ((key[1] & 0x01) << 7));
key[1] = cast(ubyte) ((key[1] >> 1) | ((key[0] & 0x01) << 7));
key[0] = cast(ubyte) ((key[0] >> 1) | carryLeft);
}
private void permute(in ubyte[] source, ubyte[] target, immutable(ubyte[]) permuteTable, size_t length)
const pure nothrow @safe @nogc
{
const sourceVector = const BitVector(source);
auto targetVector = BitVector(target);
permute(sourceVector, targetVector, permuteTable, length);
}
private void permute(in BitVector source, ubyte[] target, immutable(ubyte[]) permuteTable, size_t length)
const pure nothrow @safe @nogc
{
auto targetVector = BitVector(target);
permute(source, targetVector, permuteTable, length);
}
private void permute(in BitVector source, ref BitVector target, immutable(ubyte[]) permuteTable, size_t length)
const pure nothrow @safe @nogc
{
for (uint i; i < length * 8; ++i)
{
target[i] = source[permuteTable[i] - 1];
}
}
}
version (unittest)
{
import std.typecons;
/* Test vectors for DES. Source:
"Validating the Correctness of Hardware
Implementations of the NBS Data Encryption Standard"
NBS Special Publication 500-20, 1980. Appendix B */
// Initial and reverse Permutation and Expansion tests. Encrypt.
ubyte[8][64] desTestVectors1 = [
[0x95, 0xf8, 0xa5, 0xe5, 0xdd, 0x31, 0xd9, 0x00],
[0xdd, 0x7f, 0x12, 0x1c, 0xa5, 0x01, 0x56, 0x19],
[0x2e, 0x86, 0x53, 0x10, 0x4f, 0x38, 0x34, 0xea],
[0x4b, 0xd3, 0x88, 0xff, 0x6c, 0xd8, 0x1d, 0x4f],
[0x20, 0xb9, 0xe7, 0x67, 0xb2, 0xfb, 0x14, 0x56],
[0x55, 0x57, 0x93, 0x80, 0xd7, 0x71, 0x38, 0xef],
[0x6c, 0xc5, 0xde, 0xfa, 0xaf, 0x04, 0x51, 0x2f],
[0x0d, 0x9f, 0x27, 0x9b, 0xa5, 0xd8, 0x72, 0x60],
[0xd9, 0x03, 0x1b, 0x02, 0x71, 0xbd, 0x5a, 0x0a],
[0x42, 0x42, 0x50, 0xb3, 0x7c, 0x3d, 0xd9, 0x51],
[0xb8, 0x06, 0x1b, 0x7e, 0xcd, 0x9a, 0x21, 0xe5],
[0xf1, 0x5d, 0x0f, 0x28, 0x6b, 0x65, 0xbd, 0x28],
[0xad, 0xd0, 0xcc, 0x8d, 0x6e, 0x5d, 0xeb, 0xa1],
[0xe6, 0xd5, 0xf8, 0x27, 0x52, 0xad, 0x63, 0xd1],
[0xec, 0xbf, 0xe3, 0xbd, 0x3f, 0x59, 0x1a, 0x5e],
[0xf3, 0x56, 0x83, 0x43, 0x79, 0xd1, 0x65, 0xcd],
[0x2b, 0x9f, 0x98, 0x2f, 0x20, 0x03, 0x7f, 0xa9],
[0x88, 0x9d, 0xe0, 0x68, 0xa1, 0x6f, 0x0b, 0xe6],
[0xe1, 0x9e, 0x27, 0x5d, 0x84, 0x6a, 0x12, 0x98],
[0x32, 0x9a, 0x8e, 0xd5, 0x23, 0xd7, 0x1a, 0xec],
[0xe7, 0xfc, 0xe2, 0x25, 0x57, 0xd2, 0x3c, 0x97],
[0x12, 0xa9, 0xf5, 0x81, 0x7f, 0xf2, 0xd6, 0x5d],
[0xa4, 0x84, 0xc3, 0xad, 0x38, 0xdc, 0x9c, 0x19],
[0xfb, 0xe0, 0x0a, 0x8a, 0x1e, 0xf8, 0xad, 0x72],
[0x75, 0x0d, 0x07, 0x94, 0x07, 0x52, 0x13, 0x63],
[0x64, 0xfe, 0xed, 0x9c, 0x72, 0x4c, 0x2f, 0xaf],
[0xf0, 0x2b, 0x26, 0x3b, 0x32, 0x8e, 0x2b, 0x60],
[0x9d, 0x64, 0x55, 0x5a, 0x9a, 0x10, 0xb8, 0x52],
[0xd1, 0x06, 0xff, 0x0b, 0xed, 0x52, 0x55, 0xd7],
[0xe1, 0x65, 0x2c, 0x6b, 0x13, 0x8c, 0x64, 0xa5],
[0xe4, 0x28, 0x58, 0x11, 0x86, 0xec, 0x8f, 0x46],
[0xae, 0xb5, 0xf5, 0xed, 0xe2, 0x2d, 0x1a, 0x36],
[0xe9, 0x43, 0xd7, 0x56, 0x8a, 0xec, 0x0c, 0x5c],
[0xdf, 0x98, 0xc8, 0x27, 0x6f, 0x54, 0xb0, 0x4b],
[0xb1, 0x60, 0xe4, 0x68, 0x0f, 0x6c, 0x69, 0x6f],
[0xfa, 0x07, 0x52, 0xb0, 0x7d, 0x9c, 0x4a, 0xb8],
[0xca, 0x3a, 0x2b, 0x03, 0x6d, 0xbc, 0x85, 0x02],
[0x5e, 0x09, 0x05, 0x51, 0x7b, 0xb5, 0x9b, 0xcf],
[0x81, 0x4e, 0xeb, 0x3b, 0x91, 0xd9, 0x07, 0x26],
[0x4d, 0x49, 0xdb, 0x15, 0x32, 0x91, 0x9c, 0x9f],
[0x25, 0xeb, 0x5f, 0xc3, 0xf8, 0xcf, 0x06, 0x21],
[0xab, 0x6a, 0x20, 0xc0, 0x62, 0x0d, 0x1c, 0x6f],
[0x79, 0xe9, 0x0d, 0xbc, 0x98, 0xf9, 0x2c, 0xca],
[0x86, 0x6e, 0xce, 0xdd, 0x80, 0x72, 0xbb, 0x0e],
[0x8b, 0x54, 0x53, 0x6f, 0x2f, 0x3e, 0x64, 0xa8],
[0xea, 0x51, 0xd3, 0x97, 0x55, 0x95, 0xb8, 0x6b],
[0xca, 0xff, 0xc6, 0xac, 0x45, 0x42, 0xde, 0x31],
[0x8d, 0xd4, 0x5a, 0x2d, 0xdf, 0x90, 0x79, 0x6c],
[0x10, 0x29, 0xd5, 0x5e, 0x88, 0x0e, 0xc2, 0xd0],
[0x5d, 0x86, 0xcb, 0x23, 0x63, 0x9d, 0xbe, 0xa9],
[0x1d, 0x1c, 0xa8, 0x53, 0xae, 0x7c, 0x0c, 0x5f],
[0xce, 0x33, 0x23, 0x29, 0x24, 0x8f, 0x32, 0x28],
[0x84, 0x05, 0xd1, 0xab, 0xe2, 0x4f, 0xb9, 0x42],
[0xe6, 0x43, 0xd7, 0x80, 0x90, 0xca, 0x42, 0x07],
[0x48, 0x22, 0x1b, 0x99, 0x37, 0x74, 0x8a, 0x23],
[0xdd, 0x7c, 0x0b, 0xbd, 0x61, 0xfa, 0xfd, 0x54],
[0x2f, 0xbc, 0x29, 0x1a, 0x57, 0x0d, 0xb5, 0xc4],
[0xe0, 0x7c, 0x30, 0xd7, 0xe4, 0xe2, 0x6e, 0x12],
[0x09, 0x53, 0xe2, 0x25, 0x8e, 0x8e, 0x90, 0xa1],
[0x5b, 0x71, 0x1b, 0xc4, 0xce, 0xeb, 0xf2, 0xee],
[0xcc, 0x08, 0x3f, 0x1e, 0x6d, 0x9e, 0x85, 0xf6],
[0xd2, 0xfd, 0x88, 0x67, 0xd5, 0x0d, 0x2d, 0xfe],
[0x06, 0xe7, 0xea, 0x22, 0xce, 0x92, 0x70, 0x8f],
[0x16, 0x6b, 0x40, 0xb4, 0x4a, 0xba, 0x4b, 0xd6],
];
// Key Permutation test. Encrypt.
// Test of right-shifts. Decrypt.
ubyte[8][56] desTestVectors2 = [
[0x95, 0xa8, 0xd7, 0x28, 0x13, 0xda, 0xa9, 0x4d],
[0x0e, 0xec, 0x14, 0x87, 0xdd, 0x8c, 0x26, 0xd5],
[0x7a, 0xd1, 0x6f, 0xfb, 0x79, 0xc4, 0x59, 0x26],
[0xd3, 0x74, 0x62, 0x94, 0xca, 0x6a, 0x6c, 0xf3],
[0x80, 0x9f, 0x5f, 0x87, 0x3c, 0x1f, 0xd7, 0x61],
[0xc0, 0x2f, 0xaf, 0xfe, 0xc9, 0x89, 0xd1, 0xfc],
[0x46, 0x15, 0xaa, 0x1d, 0x33, 0xe7, 0x2f, 0x10],
[0x20, 0x55, 0x12, 0x33, 0x50, 0xc0, 0x08, 0x58],
[0xdf, 0x3b, 0x99, 0xd6, 0x57, 0x73, 0x97, 0xc8],
[0x31, 0xfe, 0x17, 0x36, 0x9b, 0x52, 0x88, 0xc9],
[0xdf, 0xdd, 0x3c, 0xc6, 0x4d, 0xae, 0x16, 0x42],
[0x17, 0x8c, 0x83, 0xce, 0x2b, 0x39, 0x9d, 0x94],
[0x50, 0xf6, 0x36, 0x32, 0x4a, 0x9b, 0x7f, 0x80],
[0xa8, 0x46, 0x8e, 0xe3, 0xbc, 0x18, 0xf0, 0x6d],
[0xa2, 0xdc, 0x9e, 0x92, 0xfd, 0x3c, 0xde, 0x92],
[0xca, 0xc0, 0x9f, 0x79, 0x7d, 0x03, 0x12, 0x87],
[0x90, 0xba, 0x68, 0x0b, 0x22, 0xae, 0xb5, 0x25],
[0xce, 0x7a, 0x24, 0xf3, 0x50, 0xe2, 0x80, 0xb6],
[0x88, 0x2b, 0xff, 0x0a, 0xa0, 0x1a, 0x0b, 0x87],
[0x25, 0x61, 0x02, 0x88, 0x92, 0x45, 0x11, 0xc2],
[0xc7, 0x15, 0x16, 0xc2, 0x9c, 0x75, 0xd1, 0x70],
[0x51, 0x99, 0xc2, 0x9a, 0x52, 0xc9, 0xf0, 0x59],
[0xc2, 0x2f, 0x0a, 0x29, 0x4a, 0x71, 0xf2, 0x9f],
[0xee, 0x37, 0x14, 0x83, 0x71, 0x4c, 0x02, 0xea],
[0xa8, 0x1f, 0xbd, 0x44, 0x8f, 0x9e, 0x52, 0x2f],
[0x4f, 0x64, 0x4c, 0x92, 0xe1, 0x92, 0xdf, 0xed],
[0x1a, 0xfa, 0x9a, 0x66, 0xa6, 0xdf, 0x92, 0xae],
[0xb3, 0xc1, 0xcc, 0x71, 0x5c, 0xb8, 0x79, 0xd8],
[0x19, 0xd0, 0x32, 0xe6, 0x4a, 0xb0, 0xbd, 0x8b],
[0x3c, 0xfa, 0xa7, 0xa7, 0xdc, 0x87, 0x20, 0xdc],
[0xb7, 0x26, 0x5f, 0x7f, 0x44, 0x7a, 0xc6, 0xf3],
[0x9d, 0xb7, 0x3b, 0x3c, 0x0d, 0x16, 0x3f, 0x54],
[0x81, 0x81, 0xb6, 0x5b, 0xab, 0xf4, 0xa9, 0x75],
[0x93, 0xc9, 0xb6, 0x40, 0x42, 0xea, 0xa2, 0x40],
[0x55, 0x70, 0x53, 0x08, 0x29, 0x70, 0x55, 0x92],
[0x86, 0x38, 0x80, 0x9e, 0x87, 0x87, 0x87, 0xa0],
[0x41, 0xb9, 0xa7, 0x9a, 0xf7, 0x9a, 0xc2, 0x08],
[0x7a, 0x9b, 0xe4, 0x2f, 0x20, 0x09, 0xa8, 0x92],
[0x29, 0x03, 0x8d, 0x56, 0xba, 0x6d, 0x27, 0x45],
[0x54, 0x95, 0xc6, 0xab, 0xf1, 0xe5, 0xdf, 0x51],
[0xae, 0x13, 0xdb, 0xd5, 0x61, 0x48, 0x89, 0x33],
[0x02, 0x4d, 0x1f, 0xfa, 0x89, 0x04, 0xe3, 0x89],
[0xd1, 0x39, 0x97, 0x12, 0xf9, 0x9b, 0xf0, 0x2e],
[0x14, 0xc1, 0xd7, 0xc1, 0xcf, 0xfe, 0xc7, 0x9e],
[0x1d, 0xe5, 0x27, 0x9d, 0xae, 0x3b, 0xed, 0x6f],
[0xe9, 0x41, 0xa3, 0x3f, 0x85, 0x50, 0x13, 0x03],
[0xda, 0x99, 0xdb, 0xbc, 0x9a, 0x03, 0xf3, 0x79],
[0xb7, 0xfc, 0x92, 0xf9, 0x1d, 0x8e, 0x92, 0xe9],
[0xae, 0x8e, 0x5c, 0xaa, 0x3c, 0xa0, 0x4e, 0x85],
[0x9c, 0xc6, 0x2d, 0xf4, 0x3b, 0x6e, 0xed, 0x74],
[0xd8, 0x63, 0xdb, 0xb5, 0xc5, 0x9a, 0x91, 0xa0],
[0xa1, 0xab, 0x21, 0x90, 0x54, 0x5b, 0x91, 0xd7],
[0x08, 0x75, 0x04, 0x1e, 0x64, 0xc5, 0x70, 0xf7],
[0x5a, 0x59, 0x45, 0x28, 0xbe, 0xbe, 0xf1, 0xcc],
[0xfc, 0xdb, 0x32, 0x91, 0xde, 0x21, 0xf0, 0xc0],
[0x86, 0x9e, 0xfd, 0x7f, 0x9f, 0x26, 0x5a, 0x09],
];
// Data permutation test. Encrypt.
ubyte[8][2][32] desTestVectors3 = [
[[0x10, 0x46, 0x91, 0x34, 0x89, 0x98, 0x01, 0x31], [0x88, 0xd5, 0x5e, 0x54, 0xf5, 0x4c, 0x97, 0xb4]],
[[0x10, 0x07, 0x10, 0x34, 0x89, 0x98, 0x80, 0x20], [0x0c, 0x0c, 0xc0, 0x0c, 0x83, 0xea, 0x48, 0xfd]],
[[0x10, 0x07, 0x10, 0x34, 0xc8, 0x98, 0x01, 0x20], [0x83, 0xbc, 0x8e, 0xf3, 0xa6, 0x57, 0x01, 0x83]],
[[0x10, 0x46, 0x10, 0x34, 0x89, 0x98, 0x80, 0x20], [0xdf, 0x72, 0x5d, 0xca, 0xd9, 0x4e, 0xa2, 0xe9]],
[[0x10, 0x86, 0x91, 0x15, 0x19, 0x19, 0x01, 0x01], [0xe6, 0x52, 0xb5, 0x3b, 0x55, 0x0b, 0xe8, 0xb0]],
[[0x10, 0x86, 0x91, 0x15, 0x19, 0x58, 0x01, 0x01], [0xaf, 0x52, 0x71, 0x20, 0xc4, 0x85, 0xcb, 0xb0]],
[[0x51, 0x07, 0xb0, 0x15, 0x19, 0x58, 0x01, 0x01], [0x0f, 0x04, 0xce, 0x39, 0x3d, 0xb9, 0x26, 0xd5]],
[[0x10, 0x07, 0xb0, 0x15, 0x19, 0x19, 0x01, 0x01], [0xc9, 0xf0, 0x0f, 0xfc, 0x74, 0x07, 0x90, 0x67]],
[[0x31, 0x07, 0x91, 0x54, 0x98, 0x08, 0x01, 0x01], [0x7c, 0xfd, 0x82, 0xa5, 0x93, 0x25, 0x2b, 0x4e]],
[[0x31, 0x07, 0x91, 0x94, 0x98, 0x08, 0x01, 0x01], [0xcb, 0x49, 0xa2, 0xf9, 0xe9, 0x13, 0x63, 0xe3]],
[[0x10, 0x07, 0x91, 0x15, 0xb9, 0x08, 0x01, 0x40], [0x00, 0xb5, 0x88, 0xbe, 0x70, 0xd2, 0x3f, 0x56]],
[[0x31, 0x07, 0x91, 0x15, 0x98, 0x08, 0x01, 0x40], [0x40, 0x6a, 0x9a, 0x6a, 0xb4, 0x33, 0x99, 0xae]],
[[0x10, 0x07, 0xd0, 0x15, 0x89, 0x98, 0x01, 0x01], [0x6c, 0xb7, 0x73, 0x61, 0x1d, 0xca, 0x9a, 0xda]],
[[0x91, 0x07, 0x91, 0x15, 0x89, 0x98, 0x01, 0x01], [0x67, 0xfd, 0x21, 0xc1, 0x7d, 0xbb, 0x5d, 0x70]],
[[0x91, 0x07, 0xd0, 0x15, 0x89, 0x19, 0x01, 0x01], [0x95, 0x92, 0xcb, 0x41, 0x10, 0x43, 0x07, 0x87]],
[[0x10, 0x07, 0xd0, 0x15, 0x98, 0x98, 0x01, 0x20], [0xa6, 0xb7, 0xff, 0x68, 0xa3, 0x18, 0xdd, 0xd3]],
[[0x10, 0x07, 0x94, 0x04, 0x98, 0x19, 0x01, 0x01], [0x4d, 0x10, 0x21, 0x96, 0xc9, 0x14, 0xca, 0x16]],
[[0x01, 0x07, 0x91, 0x04, 0x91, 0x19, 0x04, 0x01], [0x2d, 0xfa, 0x9f, 0x45, 0x73, 0x59, 0x49, 0x65]],
[[0x01, 0x07, 0x91, 0x04, 0x91, 0x19, 0x01, 0x01], [0xb4, 0x66, 0x04, 0x81, 0x6c, 0x0e, 0x07, 0x74]],
[[0x01, 0x07, 0x94, 0x04, 0x91, 0x19, 0x04, 0x01], [0x6e, 0x7e, 0x62, 0x21, 0xa4, 0xf3, 0x4e, 0x87]],
[[0x19, 0x07, 0x92, 0x10, 0x98, 0x1a, 0x01, 0x01], [0xaa, 0x85, 0xe7, 0x46, 0x43, 0x23, 0x31, 0x99]],
[[0x10, 0x07, 0x91, 0x19, 0x98, 0x19, 0x08, 0x01], [0x2e, 0x5a, 0x19, 0xdb, 0x4d, 0x19, 0x62, 0xd6]],
[[0x10, 0x07, 0x91, 0x19, 0x98, 0x1a, 0x08, 0x01], [0x23, 0xa8, 0x66, 0xa8, 0x09, 0xd3, 0x08, 0x94]],
[[0x10, 0x07, 0x92, 0x10, 0x98, 0x19, 0x01, 0x01], [0xd8, 0x12, 0xd9, 0x61, 0xf0, 0x17, 0xd3, 0x20]],
[[0x10, 0x07, 0x91, 0x15, 0x98, 0x19, 0x01, 0x0b], [0x05, 0x56, 0x05, 0x81, 0x6e, 0x58, 0x60, 0x8f]],
[[0x10, 0x04, 0x80, 0x15, 0x98, 0x19, 0x01, 0x01], [0xab, 0xd8, 0x8e, 0x8b, 0x1b, 0x77, 0x16, 0xf1]],
[[0x10, 0x04, 0x80, 0x15, 0x98, 0x19, 0x01, 0x02], [0x53, 0x7a, 0xc9, 0x5b, 0xe6, 0x9d, 0xa1, 0xe1]],
[[0x10, 0x04, 0x80, 0x15, 0x98, 0x19, 0x01, 0x08], [0xae, 0xd0, 0xf6, 0xae, 0x3c, 0x25, 0xcd, 0xd8]],
[[0x10, 0x02, 0x91, 0x14, 0x98, 0x10, 0x01, 0x04], [0xb3, 0xe3, 0x5a, 0x5e, 0xe5, 0x3e, 0x7b, 0x8d]],
[[0x10, 0x02, 0x91, 0x15, 0x98, 0x19, 0x01, 0x04], [0x61, 0xc7, 0x9c, 0x71, 0x92, 0x1a, 0x2e, 0xf8]],
[[0x10, 0x02, 0x91, 0x15, 0x98, 0x10, 0x02, 0x01], [0xe2, 0xf5, 0x72, 0x8f, 0x09, 0x95, 0x01, 0x3c]],
[[0x10, 0x02, 0x91, 0x16, 0x98, 0x10, 0x01, 0x01], [0x1a, 0xea, 0xc3, 0x9a, 0x61, 0xf0, 0xa4, 0x64]],
];
// S-Box test. Encrypt.
ubyte[8][3][19] desTestVectors4 = [
[[0x7c, 0xa1, 0x10, 0x45, 0x4a, 0x1a, 0x6e, 0x57], [0x01, 0xa1, 0xd6, 0xd0, 0x39, 0x77, 0x67, 0x42],
[0x69, 0x0f, 0x5b, 0x0d, 0x9a, 0x26, 0x93, 0x9b]],
[[0x01, 0x31, 0xd9, 0x61, 0x9d, 0xc1, 0x37, 0x6e], [0x5c, 0xd5, 0x4c, 0xa8, 0x3d, 0xef, 0x57, 0xda],
[0x7a, 0x38, 0x9d, 0x10, 0x35, 0x4b, 0xd2, 0x71]],
[[0x07, 0xa1, 0x13, 0x3e, 0x4a, 0x0b, 0x26, 0x86], [0x02, 0x48, 0xd4, 0x38, 0x06, 0xf6, 0x71, 0x72],
[0x86, 0x8e, 0xbb, 0x51, 0xca, 0xb4, 0x59, 0x9a]],
[[0x38, 0x49, 0x67, 0x4c, 0x26, 0x02, 0x31, 0x9e], [0x51, 0x45, 0x4b, 0x58, 0x2d, 0xdf, 0x44, 0x0a],
[0x71, 0x78, 0x87, 0x6e, 0x01, 0xf1, 0x9b, 0x2a]],
[[0x04, 0xb9, 0x15, 0xba, 0x43, 0xfe, 0xb5, 0xb6], [0x42, 0xfd, 0x44, 0x30, 0x59, 0x57, 0x7f, 0xa2],
[0xaf, 0x37, 0xfb, 0x42, 0x1f, 0x8c, 0x40, 0x95]],
[[0x01, 0x13, 0xb9, 0x70, 0xfd, 0x34, 0xf2, 0xce], [0x05, 0x9b, 0x5e, 0x08, 0x51, 0xcf, 0x14, 0x3a],
[0x86, 0xa5, 0x60, 0xf1, 0x0e, 0xc6, 0xd8, 0x5b]],
[[0x01, 0x70, 0xf1, 0x75, 0x46, 0x8f, 0xb5, 0xe6], [0x07, 0x56, 0xd8, 0xe0, 0x77, 0x47, 0x61, 0xd2],
[0x0c, 0xd3, 0xda, 0x02, 0x00, 0x21, 0xdc, 0x09]],
[[0x43, 0x29, 0x7f, 0xad, 0x38, 0xe3, 0x73, 0xfe], [0x76, 0x25, 0x14, 0xb8, 0x29, 0xbf, 0x48, 0x6a],
[0xea, 0x67, 0x6b, 0x2c, 0xb7, 0xdb, 0x2b, 0x7a]],
[[0x07, 0xa7, 0x13, 0x70, 0x45, 0xda, 0x2a, 0x16], [0x3b, 0xdd, 0x11, 0x90, 0x49, 0x37, 0x28, 0x02],
[0xdf, 0xd6, 0x4a, 0x81, 0x5c, 0xaf, 0x1a, 0x0f]],
[[0x04, 0x68, 0x91, 0x04, 0xc2, 0xfd, 0x3b, 0x2f], [0x26, 0x95, 0x5f, 0x68, 0x35, 0xaf, 0x60, 0x9a],
[0x5c, 0x51, 0x3c, 0x9c, 0x48, 0x86, 0xc0, 0x88]],
[[0x37, 0xd0, 0x6b, 0xb5, 0x16, 0xcb, 0x75, 0x46], [0x16, 0x4d, 0x5e, 0x40, 0x4f, 0x27, 0x52, 0x32],
[0x0a, 0x2a, 0xee, 0xae, 0x3f, 0xf4, 0xab, 0x77]],
[[0x1f, 0x08, 0x26, 0x0d, 0x1a, 0xc2, 0x46, 0x5e], [0x6b, 0x05, 0x6e, 0x18, 0x75, 0x9f, 0x5c, 0xca],
[0xef, 0x1b, 0xf0, 0x3e, 0x5d, 0xfa, 0x57, 0x5a]],
[[0x58, 0x40, 0x23, 0x64, 0x1a, 0xba, 0x61, 0x76], [0x00, 0x4b, 0xd6, 0xef, 0x09, 0x17, 0x60, 0x62],
[0x88, 0xbf, 0x0d, 0xb6, 0xd7, 0x0d, 0xee, 0x56]],
[[0x02, 0x58, 0x16, 0x16, 0x46, 0x29, 0xb0, 0x07], [0x48, 0x0d, 0x39, 0x00, 0x6e, 0xe7, 0x62, 0xf2],
[0xa1, 0xf9, 0x91, 0x55, 0x41, 0x02, 0x0b, 0x56]],
[[0x49, 0x79, 0x3e, 0xbc, 0x79, 0xb3, 0x25, 0x8f], [0x43, 0x75, 0x40, 0xc8, 0x69, 0x8f, 0x3c, 0xfa],
[0x6f, 0xbf, 0x1c, 0xaf, 0xcf, 0xfd, 0x05, 0x56]],
[[0x4f, 0xb0, 0x5e, 0x15, 0x15, 0xab, 0x73, 0xa7], [0x07, 0x2d, 0x43, 0xa0, 0x77, 0x07, 0x52, 0x92],
[0x2f, 0x22, 0xe4, 0x9b, 0xab, 0x7c, 0xa1, 0xac]],
[[0x49, 0xe9, 0x5d, 0x6d, 0x4c, 0xa2, 0x29, 0xbf], [0x02, 0xfe, 0x55, 0x77, 0x81, 0x17, 0xf1, 0x2a],
[0x5a, 0x6b, 0x61, 0x2c, 0xc2, 0x6c, 0xce, 0x4a]],
[[0x01, 0x83, 0x10, 0xdc, 0x40, 0x9b, 0x26, 0xd6], [0x1d, 0x9d, 0x5c, 0x50, 0x18, 0xf7, 0x28, 0xc2],
[0x5f, 0x4c, 0x03, 0x8e, 0xd1, 0x2b, 0x2e, 0x41]],
[[0x1c, 0x58, 0x7f, 0x1c, 0x13, 0x92, 0x4f, 0xef], [0x30, 0x55, 0x32, 0x28, 0x6d, 0x6f, 0x29, 0x5a],
[0x63, 0xfa, 0xc0, 0xd0, 0x34, 0xd9, 0xf7, 0x93]],
];
}
///
unittest
{
auto des = scoped!(DES!1);
ubyte[8] key = [0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01];
ubyte[8] plain = [0x80, 0, 0, 0, 0, 0, 0, 0];
ubyte[8] cipher;
des.key = key;
foreach (ubyte i; 0..64)
{
if (i != 0)
{
plain[i / 8] = i % 8 ? plain[i / 8] >> 1 : 0x80;
if (i % 8 == 0)
{
plain[i / 8 - 1] = 0;
}
}
// Initial Permutation and Expansion test.
des.encrypt(plain, cipher);
assert(cipher == desTestVectors1[i]);
// Inverse Permutation and Expansion test.
des.encrypt(cipher, cipher);
assert(cipher == plain);
}
plain[0..$] = 0;
foreach (ubyte i; 0..56)
{
key[i / 7] = i % 7 ? key[i / 7] >> 1 : 0x80;
if (i % 7 == 0 && i != 0)
{
key[i / 7 - 1] = 0x01;
}
des.key = key;
// Initial Permutation and Expansion test.
des.encrypt(plain, cipher);
assert(cipher == desTestVectors2[i]);
// Test of right-shifts in Decryption.
des.decrypt(desTestVectors2[i], cipher);
assert(cipher == plain);
}
// Data permutation test.
plain[0..$] = 0;
foreach (i; desTestVectors3)
{
des.key = i[0];
des.encrypt(plain, cipher);
assert(cipher == i[1]);
}
// S-Box test.
foreach (i; desTestVectors4)
{
des.key = i[0];
des.encrypt(i[1], cipher);
assert(cipher == i[2]);
}
}

279
source/tanya/crypto/mode.d Normal file
View File

@ -0,0 +1,279 @@
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
/**
* Block cipher modes of operation.
*
* Copyright: Eugene Wissner 2016.
* License: $(LINK2 https://www.mozilla.org/en-US/MPL/2.0/,
* Mozilla Public License, v. 2.0).
* Authors: $(LINK2 mailto:belka@caraus.de, Eugene Wissner)
*/
module tanya.crypto.mode;
import tanya.memory;
import std.algorithm.iteration;
import std.typecons;
/**
* Supported padding mode.
*
* See_Also:
* $(D_PSYMBOL pad)
*/
enum PaddingMode
{
zero,
pkcs7,
ansiX923,
}
/**
* Params:
* input = Sequence that should be padded.
* mode = Padding mode.
* blockSize = Block size.
* allocator = Allocator was used to allocate $(D_PARAM input).
*
* Returns: The function modifies the initial array and returns it.
*
* See_Also:
* $(D_PSYMBOL PaddingMode)
*/
ubyte[] pad(ref ubyte[] input,
in PaddingMode mode,
in ushort blockSize,
shared Allocator allocator = defaultAllocator)
in
{
assert(blockSize > 0 && blockSize <= 256);
assert(blockSize % 64 == 0);
assert(input.length > 0);
}
body
{
immutable rest = cast(ubyte) input.length % blockSize;
immutable size_t lastBlock = input.length - (rest > 0 ? rest : blockSize);
immutable needed = cast(ubyte) (rest > 0 ? blockSize - rest : 0);
final switch (mode) with (PaddingMode)
{
case zero:
allocator.resizeArray(input, input.length + needed);
break;
case pkcs7:
if (needed)
{
allocator.resizeArray(input, input.length + needed);
input[input.length - needed ..$].each!((ref e) => e = needed);
}
else
{
allocator.resizeArray(input, input.length + blockSize);
}
break;
case ansiX923:
allocator.resizeArray(input, input.length + (needed ? needed : blockSize));
input[$ - 1] = needed;
break;
}
return input;
}
///
unittest
{
{ // Zeros
auto input = defaultAllocator.makeArray!ubyte(50);
pad(input, PaddingMode.zero, 64);
assert(input.length == 64);
pad(input, PaddingMode.zero, 64);
assert(input.length == 64);
assert(input[63] == 0);
defaultAllocator.dispose(input);
}
{ // PKCS#7
auto input = defaultAllocator.makeArray!ubyte(50);
for (ubyte i; i < 40; ++i)
{
input[i] = i;
}
pad(input, PaddingMode.pkcs7, 64);
assert(input.length == 64);
for (ubyte i; i < 64; ++i)
{
if (i >= 40 && i < 50)
{
assert(input[i] == 0);
}
else if (i >= 50)
{
assert(input[i] == 14);
}
else
{
assert(input[i] == i);
}
}
pad(input, PaddingMode.pkcs7, 64);
assert(input.length == 128);
for (ubyte i; i < 128; ++i)
{
if (i >= 64 || (i >= 40 && i < 50))
{
assert(input[i] == 0);
}
else if (i >= 50 && i < 64)
{
assert(input[i] == 14);
}
else
{
assert(input[i] == i);
}
}
defaultAllocator.dispose(input);
}
{ // ANSI X.923
auto input = defaultAllocator.makeArray!ubyte(50);
for (ubyte i; i < 40; ++i)
{
input[i] = i;
}
pad(input, PaddingMode.ansiX923, 64);
assert(input.length == 64);
for (ubyte i; i < 64; ++i)
{
if (i < 40)
{
assert(input[i] == i);
}
else if (i == 63)
{
assert(input[i] == 14);
}
else
{
assert(input[i] == 0);
}
}
pad(input, PaddingMode.pkcs7, 64);
assert(input.length == 128);
for (ubyte i = 0; i < 128; ++i)
{
if (i < 40)
{
assert(input[i] == i);
}
else if (i == 63)
{
assert(input[i] == 14);
}
else
{
assert(input[i] == 0);
}
}
defaultAllocator.dispose(input);
}
}
/**
* Params:
* input = Sequence that should be padded.
* mode = Padding mode.
* blockSize = Block size.
* allocator = Allocator was used to allocate $(D_PARAM input).
*
* Returns: The function modifies the initial array and returns it.
*
* See_Also:
* $(D_PSYMBOL pad)
*/
ref ubyte[] unpad(ref ubyte[] input,
in PaddingMode mode,
in ushort blockSize,
shared Allocator allocator = defaultAllocator)
in
{
assert(input.length != 0);
assert(input.length % 64 == 0);
}
body
{
final switch (mode) with (PaddingMode)
{
case zero:
break;
case pkcs7:
case ansiX923:
immutable last = input[$ - 1];
allocator.resizeArray(input, input.length - (last ? last : blockSize));
break;
}
return input;
}
///
unittest
{
{ // Zeros
auto input = defaultAllocator.makeArray!ubyte(50);
auto inputDup = defaultAllocator.makeArray!ubyte(50);
pad(input, PaddingMode.zero, 64);
pad(inputDup, PaddingMode.zero, 64);
unpad(input, PaddingMode.zero, 64);
assert(input == inputDup);
defaultAllocator.dispose(input);
defaultAllocator.dispose(inputDup);
}
{ // PKCS#7
auto input = defaultAllocator.makeArray!ubyte(50);
auto inputDup = defaultAllocator.makeArray!ubyte(50);
for (ubyte i; i < 40; ++i)
{
input[i] = i;
inputDup[i] = i;
}
pad(input, PaddingMode.pkcs7, 64);
unpad(input, PaddingMode.pkcs7, 64);
assert(input == inputDup);
defaultAllocator.dispose(input);
defaultAllocator.dispose(inputDup);
}
{ // ANSI X.923
auto input = defaultAllocator.makeArray!ubyte(50);
auto inputDup = defaultAllocator.makeArray!ubyte(50);
for (ubyte i; i < 40; ++i)
{
input[i] = i;
inputDup[i] = i;
}
pad(input, PaddingMode.pkcs7, 64);
unpad(input, PaddingMode.pkcs7, 64);
assert(input == inputDup);
defaultAllocator.dispose(input);
defaultAllocator.dispose(inputDup);
}
}

View File

@ -8,19 +8,9 @@
* Mozilla Public License, v. 2.0).
* Authors: $(LINK2 mailto:belka@caraus.de, Eugene Wissner)
*/
module tanya.event.config;
module tanya.crypto;
package version (DisableBackends)
{
}
else
{
version (linux)
{
enum UseEpoll = true;
}
else
{
enum UseEpoll = false;
}
}
public import tanya.crypto.bit;
public import tanya.crypto.des;
public import tanya.crypto.mode;
public import tanya.crypto.symmetric;

View File

@ -1,191 +0,0 @@
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
/**
* Copyright: Eugene Wissner 2016.
* License: $(LINK2 https://www.mozilla.org/en-US/MPL/2.0/,
* Mozilla Public License, v. 2.0).
* Authors: $(LINK2 mailto:belka@caraus.de, Eugene Wissner)
*/
module tanya.crypto.padding;
import tanya.memory;
import std.algorithm.iteration;
import std.typecons;
@nogc:
/**
* Supported padding mode.
*
* See_Also:
* $(D_PSYMBOL applyPadding)
*/
enum Mode
{
zero,
pkcs7,
ansiX923,
}
/**
* Params:
* allocator = Allocator that should be used if the block should be extended
* or a new block should be added.
* input = Sequence that should be padded.
* mode = Padding mode.
* blockSize = Block size.
*
* Returns: The functions modifies the initial array and returns it.
*
* See_Also:
* $(D_PSYMBOL Mode)
*/
ubyte[] applyPadding(ref ubyte[] input,
in Mode mode,
in ushort blockSize,
Allocator allocator = defaultAllocator)
in
{
assert(blockSize > 0 && blockSize <= 256);
assert(blockSize % 64 == 0);
assert(input.length > 0);
}
body
{
immutable rest = cast(ubyte) input.length % blockSize;
immutable size_t lastBlock = input.length - (rest > 0 ? rest : blockSize);
immutable needed = cast(ubyte) (rest > 0 ? blockSize - rest : 0);
final switch (mode) with (Mode)
{
case zero:
allocator.expandArray(input, needed);
break;
case pkcs7:
if (needed)
{
allocator.expandArray(input, needed);
input[input.length - needed ..$].each!((ref e) => e = needed);
}
else
{
allocator.expandArray(input, blockSize);
}
break;
case ansiX923:
allocator.expandArray(input, needed ? needed : blockSize);
input[$ - 1] = needed;
break;
}
return input;
}
///
unittest
{
{ // Zeros
auto input = defaultAllocator.makeArray!ubyte(50);
applyPadding(input, Mode.zero, 64);
assert(input.length == 64);
applyPadding(input, Mode.zero, 64);
assert(input.length == 64);
assert(input[63] == 0);
defaultAllocator.finalize(input);
}
{ // PKCS#7
auto input = defaultAllocator.makeArray!ubyte(50);
for (ubyte i; i < 40; ++i)
{
input[i] = i;
}
applyPadding(input, Mode.pkcs7, 64);
assert(input.length == 64);
for (ubyte i; i < 64; ++i)
{
if (i >= 40 && i < 50)
{
assert(input[i] == 0);
}
else if (i >= 50)
{
assert(input[i] == 14);
}
else
{
assert(input[i] == i);
}
}
applyPadding(input, Mode.pkcs7, 64);
assert(input.length == 128);
for (ubyte i; i < 128; ++i)
{
if (i >= 64 || (i >= 40 && i < 50))
{
assert(input[i] == 0);
}
else if (i >= 50 && i < 64)
{
assert(input[i] == 14);
}
else
{
assert(input[i] == i);
}
}
defaultAllocator.finalize(input);
}
{ // ANSI X.923
auto input = defaultAllocator.makeArray!ubyte(50);
for (ubyte i; i < 40; ++i)
{
input[i] = i;
}
applyPadding(input, Mode.ansiX923, 64);
assert(input.length == 64);
for (ubyte i; i < 64; ++i)
{
if (i < 40)
{
assert(input[i] == i);
}
else if (i == 63)
{
assert(input[i] == 14);
}
else
{
assert(input[i] == 0);
}
}
applyPadding(input, Mode.pkcs7, 64);
assert(input.length == 128);
for (ubyte i = 0; i < 128; ++i)
{
if (i < 40)
{
assert(input[i] == i);
}
else if (i == 63)
{
assert(input[i] == 14);
}
else
{
assert(input[i] == 0);
}
}
defaultAllocator.finalize(input);
}
}

View File

@ -0,0 +1,177 @@
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
/**
* Interfaces for implementing secret key algorithms.
*
* Copyright: Eugene Wissner 2016.
* License: $(LINK2 https://www.mozilla.org/en-US/MPL/2.0/,
* Mozilla Public License, v. 2.0).
* Authors: $(LINK2 mailto:belka@caraus.de, Eugene Wissner)
*/
module tanya.crypto.symmetric;
/**
* Implemented by secret key algorithms.
*/
interface SymmetricCipher
{
/**
* Returns: Key length.
*/
@property inout(uint) keyLength() inout const pure nothrow @safe @nogc;
/**
* Returns: Minimum key length.
*/
@property inout(uint) minKeyLength() inout const pure nothrow @safe @nogc;
/**
* Returns: Maximum key length.
*/
@property inout(uint) maxKeyLength() inout const pure nothrow @safe @nogc;
/// Cipher direction.
protected enum Direction : ushort
{
encryption,
decryption,
}
/**
* Params:
* key = Key.
*/
@property void key(ubyte[] key) pure nothrow @safe @nogc
in
{
assert(key.length >= minKeyLength);
assert(key.length <= maxKeyLength);
}
}
/**
* Implemented by block ciphers.
*/
interface BlockCipher : SymmetricCipher
{
/**
* Returns: Block size.
*/
@property inout(uint) blockSize() inout const pure nothrow @safe @nogc;
/**
* Encrypts a block.
*
* Params:
* plain = Plain text, input.
* cipher = Cipher text, output.
*/
void encrypt(in ubyte[] plain, ubyte[] cipher)
in
{
assert(plain.length == blockSize);
assert(cipher.length == blockSize);
}
/**
* Decrypts a block.
*
* Params:
* cipher = Cipher text, input.
* plain = Plain text, output.
*/
void decrypt(in ubyte[] cipher, ubyte[] plain)
in
{
assert(plain.length == blockSize);
assert(cipher.length == blockSize);
}
}
/**
* Mixed in by algorithms with fixed block size.
*
* Params:
* N = Block size.
*/
mixin template FixedBlockSize(uint N)
if (N != 0)
{
private enum uint blockSize_ = N;
/**
* Returns: Fixed block size.
*/
final @property inout(uint) blockSize() inout const pure nothrow @safe @nogc
{
return blockSize_;
}
}
/**
* Mixed in by symmetric algorithms.
* If $(D_PARAM Min) equals $(D_PARAM Max) fixed key length is assumed.
*
* Params:
* Min = Minimum key length.
* Max = Maximum key length.
*/
mixin template KeyLength(uint Min, uint Max = Min)
if (Min != 0 && Max != 0)
{
static if (Min == Max)
{
private enum uint keyLength_ = Min;
/**
* Returns: Key length.
*/
final @property inout(uint) keyLength() inout const pure nothrow @safe @nogc
{
return keyLength_;
}
/**
* Returns: Minimum key length.
*/
final @property inout(uint) minKeyLength() inout const pure nothrow @safe @nogc
{
return keyLength_;
}
/**
* Returns: Maximum key length.
*/
final @property inout(uint) maxKeyLength() inout const pure nothrow @safe @nogc
{
return keyLength_;
}
}
else static if (Min < Max)
{
private enum uint minKeyLength_ = Min;
private enum uint maxKeyLength_ = Max;
/**
* Returns: Minimum key length.
*/
final @property inout(uint) minKeyLength() inout const pure nothrow @safe @nogc
{
return minKeyLength_;
}
/**
* Returns: Maximum key length.
*/
final @property inout(uint) maxKeyLength() inout const pure nothrow @safe @nogc
{
return maxKeyLength_;
}
}
else
{
static assert(false, "Max should be larger or equal to Min");
}
}

49
source/tanya/enums.d Normal file
View File

@ -0,0 +1,49 @@
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
/**
* Generic enum templates.
*
* Copyright: Eugene Wissner 2016.
* License: $(LINK2 https://www.mozilla.org/en-US/MPL/2.0/,
* Mozilla Public License, v. 2.0).
* Authors: $(LINK2 mailto:belka@caraus.de, Eugene Wissner)
*/
module tanya.enums;
import std.traits;
/**
* Initializer list.
*
* Generates a static array with elements from $(D_PARAM args). All elements
* should have the same type. It can be used in constructors which accept a
* list of the elements of the same type in the situations where variadic
* functions and templates can't be used.
*
* Params:
* Args = Argument type.
* args = Arguments.
*/
enum IL(Args...)(Args args)
if (Args.length > 0)
{
alias BaseType = typeof(args[0]);
BaseType[args.length] result;
foreach (i, a; args)
{
static assert(isImplicitlyConvertible!(typeof(a), BaseType));
result[i] = a;
}
return result;
}
///
unittest
{
static assert(IL(1, 5, 8).length == 3);
static assert(IL(1, 5, 8).sizeof == 3 * int.sizeof);
}

View File

@ -1,226 +0,0 @@
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
/**
* Copyright: Eugene Wissner 2016.
* License: $(LINK2 https://www.mozilla.org/en-US/MPL/2.0/,
* Mozilla Public License, v. 2.0).
* Authors: $(LINK2 mailto:belka@caraus.de, Eugene Wissner)
*/
module tanya.event.internal.epoll;
import tanya.event.config;
static if (UseEpoll):
public import core.sys.linux.epoll;
import tanya.event.internal.selector;
import tanya.event.protocol;
import tanya.event.transport;
import tanya.event.watcher;
import tanya.event.loop;
import tanya.container.list;
import tanya.memory;
import core.stdc.errno;
import core.sys.posix.fcntl;
import core.sys.posix.netinet.in_;
import core.time;
import std.algorithm.comparison;
@nogc:
extern (C) nothrow
{ // TODO: Make a pull request for Phobos to mark this extern functions as @nogc.
int epoll_create1(int __flags);
int epoll_ctl(int __epfd, int __op, int __fd, epoll_event *__event);
int epoll_wait(int __epfd, epoll_event *__events, int __maxevents, int __timeout);
int accept4(int, sockaddr*, socklen_t*, int flags);
}
private enum maxEvents = 128;
class EpollLoop : Loop
{
@nogc:
/**
* Initializes the loop.
*/
this()
{
super();
if ((fd = epoll_create1(EPOLL_CLOEXEC)) < 0)
{
return;
}
epollEvents = makeArray!epoll_event(defaultAllocator, maxEvents).ptr;
}
/**
* Frees loop internals.
*/
~this()
{
finalize(defaultAllocator, epollEvents);
}
/**
* Should be called if the backend configuration changes.
*
* Params:
* socket = Socket.
* oldEvents = The events were already set.
* events = The events should be set.
*
* Returns: $(D_KEYWORD true) if the operation was successful.
*/
protected override bool modify(int socket, EventMask oldEvents, EventMask events)
{
int op = EPOLL_CTL_DEL;
epoll_event ev;
if (events == oldEvents)
{
return true;
}
if (events && oldEvents)
{
op = EPOLL_CTL_MOD;
}
else if (events && !oldEvents)
{
op = EPOLL_CTL_ADD;
}
ev.data.fd = socket;
ev.events = (events & (Event.read | Event.accept) ? EPOLLIN | EPOLLPRI : 0)
| (events & Event.write ? EPOLLOUT : 0)
| EPOLLET;
return epoll_ctl(fd, op, socket, &ev) == 0;
}
/**
* Accept incoming connections.
*
* Params:
* protocolFactory = Protocol factory.
* socket = Socket.
*/
protected override void acceptConnection(Protocol delegate() @nogc protocolFactory,
int socket)
{
sockaddr_in client_addr;
socklen_t client_len = client_addr.sizeof;
int client = accept4(socket,
cast(sockaddr *)&client_addr,
&client_len,
O_NONBLOCK);
while (client >= 0)
{
auto transport = make!SocketTransport(defaultAllocator, this, client);
IOWatcher connection;
if (connections.length > client)
{
connection = cast(IOWatcher) connections[client];
// If it is a ConnectionWatcher
if (connection is null && connections[client] !is null)
{
finalize(defaultAllocator, connections[client]);
connections[client] = null;
}
}
if (connection !is null)
{
connection(protocolFactory, transport);
}
else
{
connections[client] = make!IOWatcher(defaultAllocator,
protocolFactory,
transport);
}
modify(client, EventMask(Event.none), EventMask(Event.read, Event.write));
swapPendings.insertBack(connections[client]);
client = accept4(socket,
cast(sockaddr *)&client_addr,
&client_len,
O_NONBLOCK);
}
}
/**
* Does the actual polling.
*/
protected override void poll()
{
// Don't block
immutable timeout = cast(immutable int) blockTime.total!"msecs";
auto eventCount = epoll_wait(fd, epollEvents, maxEvents, timeout);
if (eventCount < 0)
{
if (errno != EINTR)
{
throw make!BadLoopException(defaultAllocator);
}
return;
}
for (auto i = 0; i < eventCount; ++i)
{
epoll_event *ev = epollEvents + i;
auto connection = cast(IOWatcher) connections[ev.data.fd];
if (connection is null)
{
swapPendings.insertBack(connections[ev.data.fd]);
// acceptConnection(connections[ev.data.fd].protocol,
// connections[ev.data.fd].socket);
}
else
{
auto transport = cast(SocketTransport) connection.transport;
assert(transport !is null);
if (ev.events & (EPOLLIN | EPOLLPRI | EPOLLERR | EPOLLHUP))
{
try
{
while (!transport.receive())
{
}
swapPendings.insertBack(connection);
}
catch (TransportException e)
{
swapPendings.insertBack(connection);
finalize(defaultAllocator, e);
}
}
else if (ev.events & (EPOLLOUT | EPOLLERR | EPOLLHUP))
{
transport.writeReady = true;
}
}
}
}
/**
* Returns: The blocking time.
*/
override protected @property inout(Duration) blockTime()
inout @safe pure nothrow
{
return min(super.blockTime, 1.dur!"seconds");
}
private int fd;
private epoll_event* epollEvents;
}

View File

@ -1,217 +0,0 @@
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
/**
* Copyright: Eugene Wissner 2016.
* License: $(LINK2 https://www.mozilla.org/en-US/MPL/2.0/,
* Mozilla Public License, v. 2.0).
* Authors: $(LINK2 mailto:belka@caraus.de, Eugene Wissner)
*/
module tanya.event.internal.selector;
import tanya.memory;
import tanya.container.buffer;
import tanya.event.loop;
import tanya.event.protocol;
import tanya.event.transport;
import core.stdc.errno;
import core.sys.posix.netinet.in_;
import core.sys.posix.unistd;
/**
* Transport for stream sockets.
*/
class SocketTransport : DuplexTransport
{
@nogc:
private int socket_ = -1;
private Protocol protocol_;
/// Input buffer.
private WriteBuffer input_;
/// Output buffer.
private ReadBuffer output_;
private Loop loop;
private bool disconnected_;
package bool writeReady;
/**
* Params:
* loop = Event loop.
* socket = Socket.
* protocol = Protocol.
*/
this(Loop loop, int socket, Protocol protocol = null)
{
socket_ = socket;
protocol_ = protocol;
this.loop = loop;
input_ = make!WriteBuffer(defaultAllocator);
output_ = make!ReadBuffer(defaultAllocator);
}
/**
* Close the transport and deallocate the data buffers.
*/
~this()
{
close(socket);
finalize(defaultAllocator, input_);
finalize(defaultAllocator, output_);
finalize(defaultAllocator, protocol_);
}
/**
* Returns: Transport socket.
*/
int socket() const @safe pure nothrow
{
return socket_;
}
/**
* Returns: Protocol.
*/
@property Protocol protocol() @safe pure nothrow
{
return protocol_;
}
/**
* Returns: $(D_KEYWORD true) if the remote peer closed the connection,
* $(D_KEYWORD false) otherwise.
*/
@property immutable(bool) disconnected() const @safe pure nothrow
{
return disconnected_;
}
/**
* Params:
* protocol = Application protocol.
*/
@property void protocol(Protocol protocol) @safe pure nothrow
{
protocol_ = protocol;
}
/**
* Returns: Application protocol.
*/
@property inout(Protocol) protocol() inout @safe pure nothrow
{
return protocol_;
}
/**
* Write some data to the transport.
*
* Params:
* data = Data to send.
*/
void write(ubyte[] data)
{
// If the buffer wasn't empty the transport should be already there.
if (!input.length && data.length)
{
loop.feed(this);
}
input ~= data;
}
/**
* Returns: Input buffer.
*/
@property WriteBuffer input() @safe pure nothrow
{
return input_;
}
/**
* Returns: Output buffer.
*/
@property ReadBuffer output() @safe pure nothrow
{
return output_;
}
/**
* Read data from the socket. Returns $(D_KEYWORD true) if the reading
* is completed. In the case that the peer closed the connection, returns
* $(D_KEYWORD true) aswell.
*
* Returns: Whether the reading is completed.
*
* Throws: $(D_PSYMBOL TransportException) if a read error is occured.
*/
bool receive()
{
auto readCount = recv(socket, output.buffer, output.free, 0);
if (readCount > 0)
{
output_ ~= output.buffer[0..readCount];
return false;
}
else if (readCount == 0)
{
disconnected_ = true;
return true;
}
else if (errno == EAGAIN || errno == EWOULDBLOCK)
{
return true;
}
else
{
disconnected_ = true;
throw make!TransportException(defaultAllocator,
"Read from the socket failed.");
}
}
/**
* Returns: Whether the writing is completed.
*
* Throws: $(D_PSYMBOL TransportException) if a read error is occured.
*/
bool send()
{
auto sentCount = core.sys.posix.netinet.in_.send(socket,
input.buffer,
input.length,
0);
input.written = sentCount;
if (input.length == 0)
{
return true;
}
else if (sentCount >= 0)
{
loop.feed(this);
return false;
}
else if (errno == EAGAIN || errno == EWOULDBLOCK)
{
writeReady = false;
loop.feed(this);
return false;
}
else
{
disconnected_ = true;
loop.feed(this);
throw make!TransportException(defaultAllocator,
"Write to the socket failed.");
}
}
}

View File

@ -1,279 +0,0 @@
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
/**
* Copyright: Eugene Wissner 2016.
* License: $(LINK2 https://www.mozilla.org/en-US/MPL/2.0/,
* Mozilla Public License, v. 2.0).
* Authors: $(LINK2 mailto:belka@caraus.de, Eugene Wissner)
*/
module tanya.event.loop;
import tanya.memory;
import tanya.container.queue;
import tanya.container.vector;
import tanya.event.config;
import tanya.event.protocol;
import tanya.event.transport;
import tanya.event.watcher;
import tanya.container.buffer;
import core.thread;
import core.time;
import std.algorithm.iteration;
import std.algorithm.mutation;
import std.typecons;
static if (UseEpoll)
{
import tanya.event.internal.epoll;
}
@nogc:
/**
* Events.
*/
enum Event : uint
{
none = 0x00, /// No events.
read = 0x01, /// Non-blocking read call.
write = 0x02, /// Non-blocking write call.
accept = 0x04, /// Connection made.
}
alias EventMask = BitFlags!Event;
/**
* Event loop.
*/
abstract class Loop
{
@nogc:
/// Pending watchers.
protected Queue!Watcher pendings;
protected Queue!Watcher swapPendings;
/// Pending connections.
protected Vector!ConnectionWatcher connections;
/**
* Initializes the loop.
*/
this()
{
connections = make!(Vector!ConnectionWatcher)(defaultAllocator);
pendings = make!(Queue!Watcher)(defaultAllocator);
swapPendings = make!(Queue!Watcher)(defaultAllocator);
}
/**
* Frees loop internals.
*/
~this()
{
finalize(defaultAllocator, connections);
finalize(defaultAllocator, pendings);
finalize(defaultAllocator, swapPendings);
}
/**
* Starts the loop.
*/
void run()
{
done_ = false;
do
{
poll();
// Invoke pendings
swapPendings.each!((ref p) => p.invoke());
swap(pendings, swapPendings);
}
while (!done_);
}
/**
* Break out of the loop.
*/
void unloop() @safe pure nothrow
{
done_ = true;
}
/**
* Start watching.
*
* Params:
* watcher = Watcher.
*/
void start(ConnectionWatcher watcher)
{
if (watcher.active)
{
return;
}
watcher.active = true;
watcher.accept = &acceptConnection;
connections[watcher.socket] = watcher;
modify(watcher.socket, EventMask(Event.none), EventMask(Event.accept));
}
/**
* Stop watching.
*
* Params:
* watcher = Watcher.
*/
void stop(ConnectionWatcher watcher)
{
if (!watcher.active)
{
return;
}
watcher.active = false;
modify(watcher.socket, EventMask(Event.accept), EventMask(Event.none));
}
/**
* Feeds the given event set into the event loop, as if the specified event
* had happened for the specified watcher.
*
* Params:
* transport = Affected transport.
*/
void feed(DuplexTransport transport)
{
pendings.insertBack(connections[transport.socket]);
}
/**
* Should be called if the backend configuration changes.
*
* Params:
* socket = Socket.
* oldEvents = The events were already set.
* events = The events should be set.
*
* Returns: $(D_KEYWORD true) if the operation was successful.
*/
protected bool modify(int socket, EventMask oldEvents, EventMask events);
/**
* Returns: The blocking time.
*/
protected @property inout(Duration) blockTime()
inout @safe pure nothrow
{
// Don't block if we have to do.
return swapPendings.empty ? blockTime_ : Duration.zero;
}
/**
* Sets the blocking time for IO watchers.
*
* Params:
* blockTime = The blocking time. Cannot be larger than
* $(D_PSYMBOL maxBlockTime).
*/
protected @property void blockTime(in Duration blockTime) @safe pure nothrow
in
{
assert(blockTime <= 1.dur!"hours", "Too long to wait.");
assert(!blockTime.isNegative);
}
body
{
blockTime_ = blockTime;
}
/**
* Does the actual polling.
*/
protected void poll();
/**
* Accept incoming connections.
*
* Params:
* protocolFactory = Protocol factory.
* socket = Socket.
*/
protected void acceptConnection(Protocol delegate() @nogc protocolFactory,
int socket);
/// Whether the event loop should be stopped.
private bool done_;
/// Maximal block time.
protected Duration blockTime_ = 1.dur!"minutes";
}
/**
* Exception thrown on errors in the event loop.
*/
class BadLoopException : Exception
{
@nogc:
/**
* Params:
* file = The file where the exception occurred.
* line = The line number where the exception occurred.
* next = The previous exception in the chain of exceptions, if any.
*/
this(string file = __FILE__, size_t line = __LINE__, Throwable next = null)
pure @safe nothrow const
{
super("Event loop cannot be initialized.", file, line, next);
}
}
/**
* Returns the event loop used by default. If an event loop wasn't set with
* $(D_PSYMBOL defaultLoop) before, $(D_PSYMBOL getDefaultLoop()) will try to
* choose an event loop supported on the system.
*
* Returns: The default event loop.
*/
Loop getDefaultLoop()
{
if (_defaultLoop !is null)
{
return _defaultLoop;
}
static if (UseEpoll)
{
_defaultLoop = make!EpollLoop(defaultAllocator);
}
return _defaultLoop;
}
/**
* Sets the default event loop.
*
* This property makes it possible to implement your own backends or event
* loops, for example, if the system is not supported or if you want to
* extend the supported implementation. Just extend $(D_PSYMBOL Loop) and pass
* your implementation to this property.
*
* Params:
* loop = The event loop.
*/
@property void defaultLoop(Loop loop)
in
{
assert(loop !is null);
}
body
{
_defaultLoop = loop;
}
private Loop _defaultLoop;

View File

@ -1,138 +0,0 @@
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
/**
* Copyright: Eugene Wissner 2016.
* License: $(LINK2 https://www.mozilla.org/en-US/MPL/2.0/,
* Mozilla Public License, v. 2.0).
* Authors: $(LINK2 mailto:belka@caraus.de, Eugene Wissner)
*/
module tanya.event.transport;
import tanya.container.buffer;
import tanya.event.protocol;
/**
* Exception thrown on read/write errors.
*/
class TransportException : Exception
{
@nogc:
/**
* Params:
* msg = Message to output.
* file = The file where the exception occurred.
* line = The line number where the exception occurred.
* next = The previous exception in the chain of exceptions, if any.
*/
this(string msg,
string file = __FILE__,
size_t line = __LINE__,
Throwable next = null) pure @safe nothrow const
{
super(msg, file, line, next);
}
}
/**
* Base transport interface.
*/
interface Transport
{
@nogc:
/**
* Returns: Protocol.
*/
@property Protocol protocol() @safe pure nothrow;
/**
* Returns: $(D_KEYWORD true) if the peer closed the connection,
* $(D_KEYWORD false) otherwise.
*/
@property immutable(bool) disconnected() const @safe pure nothrow;
/**
* Params:
* protocol = Application protocol.
*/
@property void protocol(Protocol protocol) @safe pure nothrow
in
{
assert(protocol !is null, "protocolConnected cannot be unset.");
}
/**
* Returns: Application protocol.
*/
@property inout(Protocol) protocol() inout @safe pure nothrow;
/**
* Returns: Transport socket.
*/
int socket() const @safe pure nothrow;
}
/**
* Interface for read-only transports.
*/
interface ReadTransport : Transport
{
@nogc:
/**
* Returns: Underlying output buffer.
*/
@property ReadBuffer output();
/**
* Reads data into the buffer.
*
* Returns: Whether the reading is completed.
*
* Throws: $(D_PSYMBOL TransportException) if a read error is occured.
*/
bool receive()
in
{
assert(!disconnected);
}
}
/**
* Interface for write-only transports.
*/
interface WriteTransport : Transport
{
@nogc:
/**
* Returns: Underlying input buffer.
*/
@property WriteBuffer input();
/**
* Write some data to the transport.
*
* Params:
* data = Data to send.
*/
void write(ubyte[] data);
/**
* Returns: Whether the writing is completed.
*
* Throws: $(D_PSYMBOL TransportException) if a read error is occured.
*/
bool send()
in
{
assert(input.length);
assert(!disconnected);
}
}
/**
* Represents a bidirectional transport.
*/
abstract class DuplexTransport : ReadTransport, WriteTransport
{
}

View File

@ -1,210 +0,0 @@
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
/**
* Copyright: Eugene Wissner 2016.
* License: $(LINK2 https://www.mozilla.org/en-US/MPL/2.0/,
* Mozilla Public License, v. 2.0).
* Authors: $(LINK2 mailto:belka@caraus.de, Eugene Wissner)
*/
module tanya.event.watcher;
import tanya.event.protocol;
import tanya.event.transport;
import tanya.memory;
import std.functional;
/**
* A watcher is an opaque structure that you allocate and register to record
* your interest in some event.
*/
abstract class Watcher
{
@nogc:
/// Whether the watcher is active.
bool active;
/**
* Invoke some action on event.
*/
void invoke();
}
class ConnectionWatcher : Watcher
{
@nogc:
/// Watched file descriptor.
private int socket_;
/// Protocol factory.
protected Protocol delegate() protocolFactory;
/// Callback.
package void delegate(Protocol delegate() @nogc protocolFactory,
int socket) accept;
invariant
{
assert(socket_ >= 0, "Called with negative file descriptor.");
}
/**
* Params:
* protocolFactory = Function returning a new $(D_PSYMBOL Protocol) instance.
* socket = Socket.
*/
this(Protocol function() @nogc protocolFactory, int socket)
{
this.protocolFactory = toDelegate(protocolFactory);
socket_ = socket;
}
/// Ditto.
this(Protocol delegate() @nogc protocolFactory, int socket)
{
this.protocolFactory = protocolFactory;
socket_ = socket;
}
/// Ditto.
protected this(Protocol function() @nogc protocolFactory)
{
this.protocolFactory = toDelegate(protocolFactory);
}
/// Ditto.
protected this(Protocol delegate() @nogc protocolFactory)
{
this.protocolFactory = protocolFactory;
}
/**
* Returns: Socket.
*/
@property inout(int) socket() inout @safe pure nothrow
{
return socket_;
}
/**
* Returns: Application protocol factory.
*/
@property inout(Protocol delegate() @nogc) protocol() inout
{
return protocolFactory;
}
override void invoke()
{
accept(protocol, socket);
}
}
/**
* Contains a pending watcher with the invoked events or a transport can be
* read from.
*/
class IOWatcher : ConnectionWatcher
{
@nogc:
/// References a watcher or a transport.
DuplexTransport transport_;
/**
* Params:
* protocolFactory = Function returning application specific protocol.
* transport = Transport.
*/
this(Protocol delegate() @nogc protocolFactory,
DuplexTransport transport)
in
{
assert(transport !is null);
assert(protocolFactory !is null);
}
body
{
super(protocolFactory);
this.transport_ = transport;
}
~this()
{
finalize(defaultAllocator, transport_);
}
/**
* Assigns a transport.
*
* Params:
* protocolFactory = Function returning application specific protocol.
* transport = Transport.
*
* Returns: $(D_KEYWORD this).
*/
IOWatcher opCall(Protocol delegate() @nogc protocolFactory,
DuplexTransport transport) @safe pure nothrow
in
{
assert(transport !is null);
assert(protocolFactory !is null);
}
body
{
this.protocolFactory = protocolFactory;
this.transport_ = transport;
return this;
}
/**
* Returns: Transport used by this watcher.
*/
@property inout(DuplexTransport) transport() inout @safe pure nothrow
{
return transport_;
}
/**
* Returns: Socket.
*/
override @property inout(int) socket() inout @safe pure nothrow
{
return transport.socket;
}
/**
* Invokes the watcher callback.
*
* Finalizes the transport on disconnect.
*/
override void invoke()
{
if (transport.protocol is null)
{
transport.protocol = protocolFactory();
transport.protocol.connected(transport);
}
else if (transport.disconnected)
{
transport.protocol.disconnected();
finalize(defaultAllocator, transport_);
protocolFactory = null;
}
else if (transport.output.length)
{
transport.protocol.received(transport.output[]);
}
else if (transport.input.length)
{
try
{
transport.send();
}
catch (TransportException e)
{
finalize(defaultAllocator, e);
}
}
}
}

View File

@ -1,112 +0,0 @@
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
/**
* Copyright: Eugene Wissner 2016.
* License: $(LINK2 https://www.mozilla.org/en-US/MPL/2.0/,
* Mozilla Public License, v. 2.0).
* Authors: $(LINK2 mailto:belka@caraus.de, Eugene Wissner)
*/
module tanya.container.math;
version (unittest)
{
import std.algorithm.iteration;
}
@nogc:
/**
* Computes $(D_PARAM x) to the power $(D_PARAM y) modulo $(D_PARAM z).
*
* Params:
* x = Base.
* y = Exponent.
* z = Divisor.
*
* Returns: Reminder of the division of $(D_PARAM x) to the power $(D_PARAM y)
* by $(D_PARAM z).
*/
ulong pow(ulong x, ulong y, ulong z) @safe nothrow pure
in
{
assert(z > 0);
}
out (result)
{
assert(result >= 0);
}
body
{
ulong mask = ulong.max / 2 + 1, result;
if (y == 0)
{
return 1 % z;
}
else if (y == 1)
{
return x % z;
}
do
{
auto bit = y & mask;
if (!result && bit)
{
result = x;
continue;
}
result *= result;
if (bit)
{
result *= x;
}
result %= z;
}
while (mask >>= 1);
return result;
}
///
unittest
{
assert(pow(3, 5, 7) == 5);
assert(pow(2, 2, 1) == 0);
assert(pow(3, 3, 3) == 0);
assert(pow(7, 4, 2) == 1);
assert(pow(53, 0, 2) == 1);
assert(pow(53, 1, 3) == 2);
assert(pow(53, 2, 5) == 4);
assert(pow(0, 0, 5) == 1);
assert(pow(0, 5, 5) == 0);
}
/**
* Checks if $(D_PARAM x) is a prime.
*
* Params:
* x = The number should be checked.
*
* Returns: $(D_KEYWORD true) if $(D_PARAM x) is a prime number,
* $(D_KEYWORD false) otherwise.
*/
bool isPseudoprime(ulong x) @safe nothrow pure
{
return pow(2, x - 1, x) == 1;
}
///
unittest
{
uint[30] known = [74623, 74653, 74687, 74699, 74707, 74713, 74717, 74719,
74843, 74747, 74759, 74761, 74771, 74779, 74797, 74821,
74827, 9973, 104729, 15485867, 49979693, 104395303,
593441861, 104729, 15485867, 49979693, 104395303,
593441861, 899809363, 982451653];
known.each!((ref x) => assert(isPseudoprime(x)));
}

1060
source/tanya/math/mp.d Normal file

File diff suppressed because it is too large Load Diff

172
source/tanya/math/package.d Normal file
View File

@ -0,0 +1,172 @@
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
/**
* Copyright: Eugene Wissner 2016.
* License: $(LINK2 https://www.mozilla.org/en-US/MPL/2.0/,
* Mozilla Public License, v. 2.0).
* Authors: $(LINK2 mailto:belka@caraus.de, Eugene Wissner)
*/
module tanya.math;
import std.traits;
public import tanya.math.mp;
public import tanya.math.random;
version (unittest)
{
import std.algorithm.iteration;
}
/**
* Computes $(D_PARAM x) to the power $(D_PARAM y) modulo $(D_PARAM z).
*
* If $(D_PARAM I) is an $(D_PSYMBOL Integer), the allocator of $(D_PARAM x)
* is used to allocate the result.
*
* Params:
* I = Base type.
* G = Exponent type.
* H = Divisor type:
* x = Base.
* y = Exponent.
* z = Divisor.
*
* Returns: Reminder of the division of $(D_PARAM x) to the power $(D_PARAM y)
* by $(D_PARAM z).
*
* Precondition: $(D_INLINECODE z > 0)
*/
H pow(I, G, H)(in auto ref I x, in auto ref G y, in auto ref H z)
if (isIntegral!I && isIntegral!G && isIntegral!H)
in
{
assert(z > 0, "Division by zero.");
}
body
{
G mask = G.max / 2 + 1;
H result;
if (y == 0)
{
return 1 % z;
}
else if (y == 1)
{
return x % z;
}
do
{
immutable bit = y & mask;
if (!result && bit)
{
result = x;
continue;
}
result *= result;
if (bit)
{
result *= x;
}
result %= z;
}
while (mask >>= 1);
return result;
}
/// Ditto.
I pow(I)(in auto ref I x, in auto ref I y, in auto ref I z)
if (is(I == Integer))
in
{
assert(z.length > 0, "Division by zero.");
}
body
{
size_t i = y.length;
auto tmp2 = Integer(x.allocator), tmp1 = Integer(x, x.allocator);
Integer result = Integer(x.allocator);
if (x.length == 0 && i != 0)
{
i = 0;
}
else
{
result = 1;
}
while (i)
{
--i;
for (ubyte mask = 0x01; mask; mask <<= 1)
{
if (y.rep[i] & mask)
{
result *= tmp1;
result %= z;
}
tmp2 = tmp1;
tmp1 *= tmp2;
tmp1 %= z;
}
}
return result;
}
///
pure nothrow @safe @nogc unittest
{
assert(pow(3, 5, 7) == 5);
assert(pow(2, 2, 1) == 0);
assert(pow(3, 3, 3) == 0);
assert(pow(7, 4, 2) == 1);
assert(pow(53, 0, 2) == 1);
assert(pow(53, 1, 3) == 2);
assert(pow(53, 2, 5) == 4);
assert(pow(0, 0, 5) == 1);
assert(pow(0, 5, 5) == 0);
}
///
unittest
{
assert(cast(long) pow(Integer(3), Integer(5), Integer(7)) == 5);
assert(cast(long) pow(Integer(2), Integer(2), Integer(1)) == 0);
assert(cast(long) pow(Integer(3), Integer(3), Integer(3)) == 0);
assert(cast(long) pow(Integer(7), Integer(4), Integer(2)) == 1);
assert(cast(long) pow(Integer(53), Integer(0), Integer(2)) == 1);
assert(cast(long) pow(Integer(53), Integer(1), Integer(3)) == 2);
assert(cast(long) pow(Integer(53), Integer(2), Integer(5)) == 4);
assert(cast(long) pow(Integer(0), Integer(0), Integer(5)) == 1);
assert(cast(long) pow(Integer(0), Integer(5), Integer(5)) == 0);
}
/**
* Checks if $(D_PARAM x) is a prime.
*
* Params:
* x = The number should be checked.
*
* Returns: $(D_KEYWORD true) if $(D_PARAM x) is a prime number,
* $(D_KEYWORD false) otherwise.
*/
bool isPseudoprime(ulong x) nothrow pure @safe @nogc
{
return pow(2, x - 1, x) == 1;
}
///
unittest
{
uint[30] known = [74623, 74653, 74687, 74699, 74707, 74713, 74717, 74719,
74843, 74747, 74759, 74761, 74771, 74779, 74797, 74821,
74827, 9973, 104729, 15485867, 49979693, 104395303,
593441861, 104729, 15485867, 49979693, 104395303,
593441861, 899809363, 982451653];
known.each!((ref x) => assert(isPseudoprime(x)));
}

View File

@ -3,18 +3,18 @@
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
/**
* Random number generator.
*
* Copyright: Eugene Wissner 2016.
* License: $(LINK2 https://www.mozilla.org/en-US/MPL/2.0/,
* Mozilla Public License, v. 2.0).
* Authors: $(LINK2 mailto:belka@caraus.de, Eugene Wissner)
*/
module tanya.random;
module tanya.math.random;
import tanya.memory;
import std.digest.sha;
import std.typecons;
@nogc:
import tanya.memory;
/// Block size of entropy accumulator (SHA-512).
enum blockSize = 64;
@ -27,7 +27,6 @@ enum maxGather = 128;
*/
class EntropyException : Exception
{
@nogc:
/**
* Params:
* msg = Message to output.
@ -38,7 +37,7 @@ class EntropyException : Exception
this(string msg,
string file = __FILE__,
size_t line = __LINE__,
Throwable next = null) pure @safe nothrow const
Throwable next = null) pure @safe nothrow const @nogc
{
super(msg, file, line, next);
}
@ -49,7 +48,6 @@ class EntropyException : Exception
*/
abstract class EntropySource
{
@nogc:
/// Amount of already generated entropy.
protected ushort size_;
@ -103,7 +101,6 @@ version (linux)
*/
class PlatformEntropySource : EntropySource
{
@nogc:
/**
* Returns: Minimum bytes required from the entropy source.
*/
@ -153,7 +150,7 @@ version (linux)
/**
* Pseudorandom number generator.
* ---
* auto entropy = defaultAllocator.make!Entropy;
* auto entropy = defaultAllocator.make!Entropy();
*
* ubyte[blockSize] output;
*
@ -164,13 +161,12 @@ version (linux)
*/
class Entropy
{
@nogc:
/// Entropy sources.
protected EntropySource[] sources;
private ubyte sourceCount_;
private Allocator allocator;
private shared Allocator allocator;
/// Entropy accumulator.
protected SHA!(maxGather * 8, 512) accumulator;
@ -181,7 +177,7 @@ class Entropy
* allocator = Allocator to allocate entropy sources available on the
* system.
*/
this(size_t maxSources = 20, Allocator allocator = defaultAllocator)
this(size_t maxSources = 20, shared Allocator allocator = defaultAllocator)
in
{
assert(maxSources > 0 && maxSources <= ubyte.max);

View File

@ -11,22 +11,24 @@
module tanya.memory.allocator;
/**
* This interface should be similar to $(D_PSYMBOL
* std.experimental.allocator.IAllocator), but usable in
* $(D_KEYWORD @nogc)-code.
* Abstract class implementing a basic allocator.
*/
interface Allocator
{
@nogc:
/**
* Allocates $(D_PARAM s) bytes of memory.
* Returns: Alignment.
*/
@property uint alignment() const shared pure nothrow @safe @nogc;
/**
* Allocates $(D_PARAM size) bytes of memory.
*
* Params:
* s = Amount of memory to allocate.
* size = Amount of memory to allocate.
*
* Returns: The pointer to the new allocated memory.
* Returns: Pointer to the new allocated memory.
*/
void[] allocate(size_t s) @safe;
void[] allocate(size_t size) shared nothrow @nogc;
/**
* Deallocates a memory block.
@ -36,7 +38,7 @@ interface Allocator
*
* Returns: Whether the deallocation was successful.
*/
bool deallocate(void[] p) @safe;
bool deallocate(void[] p) shared nothrow @nogc;
/**
* Increases or decreases the size of a memory block.
@ -45,14 +47,70 @@ interface Allocator
* p = A pointer to the memory block.
* size = Size of the reallocated block.
*
* Returns: Whether the reallocation was successful.
* Returns: Pointer to the allocated memory.
*/
bool reallocate(ref void[] p, size_t s) @safe;
bool reallocate(ref void[] p, size_t size) shared nothrow @nogc;
}
/**
* Static allocator instance and initializer.
*
* Returns: An $(D_PSYMBOL Allocator) instance.
* The mixin generates common methods for classes and structs using
* allocators. It provides a protected member, constructor and a read-only property,
* that checks if an allocator was already set and sets it to the default
* one, if not (useful for structs which don't have a default constructor).
*/
static @property Allocator instance() @safe;
mixin template DefaultAllocator()
{
/// Allocator.
protected shared Allocator allocator_;
/**
* Params:
* allocator = The allocator should be used.
*/
this(shared Allocator allocator)
in
{
assert(allocator !is null);
}
body
{
this.allocator_ = allocator;
}
/**
* This property checks if the allocator was set in the constructor
* and sets it to the default one, if not.
*
* Returns: Used allocator.
*
* Postcondition: $(D_INLINECODE allocator_ !is null)
*/
protected @property shared(Allocator) allocator() nothrow @safe @nogc
out (allocator)
{
assert(allocator !is null);
}
body
{
if (allocator_ is null)
{
allocator_ = defaultAllocator;
}
return allocator_;
}
/// Ditto.
@property shared(Allocator) allocator() const nothrow @trusted @nogc
out (allocator)
{
assert(allocator !is null);
}
body
{
if (allocator_ is null)
{
return defaultAllocator;
}
return cast(shared Allocator) allocator_;
}
}

View File

@ -8,27 +8,32 @@
* Mozilla Public License, v. 2.0).
* Authors: $(LINK2 mailto:info@caraus.de, Eugene Wissner)
*/
module tanya.memory.ullocator;
module tanya.memory.mmappool;
import tanya.memory.allocator;
import core.atomic;
import core.exception;
@nogc:
version (Posix):
version (Posix)
{
import core.stdc.errno;
import core.sys.posix.sys.mman;
import core.sys.posix.unistd;
}
else version (Windows)
{
import core.sys.windows.winbase;
import core.sys.windows.windows;
}
/**
* Allocator for Posix systems with mmap/munmap support.
*
* This allocator allocates memory in regions (multiple of 4 KB for example).
* Each region is then splitted in blocks. So it doesn't request the memory
* from the operating system on each call, but only if there are no large
* enought free blocks in the available regions.
* enough free blocks in the available regions.
* Deallocation works in the same way. Deallocation doesn't immediately
* gives the memory back to the operating system, but marks the appropriate
* block as free and only if all blocks in the region are free, the complet
* block as free and only if all blocks in the region are free, the complete
* region is deallocated.
*
* ----------------------------------------------------------------------------
@ -43,15 +48,22 @@ import core.sys.posix.unistd;
* | | | | | || | | |
* --------------------------------------------------- ------------------------
*/
class Ullocator : Allocator
final class MmapPool : Allocator
{
@nogc:
@disable this();
shared static this() @safe nothrow
shared static this()
{
version (Posix)
{
pageSize = sysconf(_SC_PAGE_SIZE);
}
else version (Windows)
{
SYSTEM_INFO si;
GetSystemInfo(&si);
pageSize = si.dwPageSize;
}
}
/**
* Allocates $(D_PARAM size) bytes of memory.
@ -59,10 +71,14 @@ class Ullocator : Allocator
* Params:
* size = Amount of memory to allocate.
*
* Returns: The pointer to the new allocated memory.
* Returns: Pointer to the new allocated memory.
*/
void[] allocate(size_t size) @trusted nothrow
void[] allocate(size_t size) shared nothrow
{
if (!size)
{
return null;
}
immutable dataSize = addAlignment(size);
void* data = findBlock(dataSize);
@ -75,13 +91,13 @@ class Ullocator : Allocator
}
///
unittest
nothrow unittest
{
auto p = Ullocator.instance.allocate(20);
auto p = MmapPool.instance.allocate(20);
assert(p);
Ullocator.instance.deallocate(p);
MmapPool.instance.deallocate(p);
}
/**
@ -93,7 +109,7 @@ class Ullocator : Allocator
*
* Returns: Data the block points to or $(D_KEYWORD null).
*/
private void* findBlock(size_t size) nothrow
private void* findBlock(size_t size) shared nothrow
{
Block block1;
RegionLoop: for (auto r = head; r !is null; r = r.next)
@ -133,12 +149,12 @@ class Ullocator : Allocator
block1.size = size;
block2.region = block1.region;
++block1.region.blocks;
atomicOp!"+="(block1.region.blocks, 1);
}
else
{
block1.free = false;
++block1.region.blocks;
atomicOp!"+="(block1.region.blocks, 1);
}
return cast(void*) block1 + blockEntrySize;
}
@ -151,7 +167,7 @@ class Ullocator : Allocator
*
* Returns: Whether the deallocation was successful.
*/
bool deallocate(void[] p) @trusted nothrow
bool deallocate(void[] p) shared nothrow
{
if (p is null)
{
@ -173,22 +189,29 @@ class Ullocator : Allocator
{
block.region.next.prev = block.region.prev;
}
return munmap(block.region, block.region.size) == 0;
version (Posix)
{
return munmap(cast(void*) block.region, block.region.size) == 0;
}
version (Windows)
{
return VirtualFree(cast(void*) block.region, 0, MEM_RELEASE) == 0;
}
}
else
{
block.free = true;
--block.region.blocks;
atomicOp!"-="(block.region.blocks, 1);
return true;
}
}
///
unittest
nothrow unittest
{
auto p = Ullocator.instance.allocate(20);
auto p = MmapPool.instance.allocate(20);
assert(Ullocator.instance.deallocate(p));
assert(MmapPool.instance.deallocate(p));
}
/**
@ -200,18 +223,22 @@ class Ullocator : Allocator
*
* Returns: Whether the reallocation was successful.
*/
bool reallocate(ref void[] p, size_t size) @trusted nothrow
bool reallocate(ref void[] p, size_t size) shared nothrow
{
void[] reallocP;
if (size == p.length)
{
return true;
}
auto reallocP = allocate(size);
else if (size > 0)
{
reallocP = allocate(size);
if (reallocP is null)
{
return false;
}
}
if (p !is null)
{
@ -219,7 +246,7 @@ class Ullocator : Allocator
{
reallocP[0..p.length] = p[0..$];
}
else
else if (size > 0)
{
reallocP[0..size] = p[0..size];
}
@ -231,61 +258,59 @@ class Ullocator : Allocator
}
///
unittest
nothrow unittest
{
void[] p;
Ullocator.instance.reallocate(p, 10 * int.sizeof);
MmapPool.instance.reallocate(p, 10 * int.sizeof);
(cast(int[]) p)[7] = 123;
assert(p.length == 40);
Ullocator.instance.reallocate(p, 8 * int.sizeof);
MmapPool.instance.reallocate(p, 8 * int.sizeof);
assert(p.length == 32);
assert((cast(int[]) p)[7] == 123);
Ullocator.instance.reallocate(p, 20 * int.sizeof);
MmapPool.instance.reallocate(p, 20 * int.sizeof);
(cast(int[]) p)[15] = 8;
assert(p.length == 80);
assert((cast(int[]) p)[15] == 8);
assert((cast(int[]) p)[7] == 123);
Ullocator.instance.reallocate(p, 8 * int.sizeof);
MmapPool.instance.reallocate(p, 8 * int.sizeof);
assert(p.length == 32);
assert((cast(int[]) p)[7] == 123);
Ullocator.instance.deallocate(p);
MmapPool.instance.deallocate(p);
}
/**
* Static allocator instance and initializer.
*
* Returns: The global $(D_PSYMBOL Allocator) instance.
* Returns: Global $(D_PSYMBOL MmapPool) instance.
*/
static @property Ullocator instance() @trusted nothrow
static @property ref shared(MmapPool) instance() nothrow
{
if (instance_ is null)
{
immutable instanceSize = addAlignment(__traits(classInstanceSize, Ullocator));
immutable instanceSize = addAlignment(__traits(classInstanceSize, MmapPool));
Region head; // Will become soon our region list head
void* data = initializeRegion(instanceSize, head);
if (data is null)
if (data !is null)
{
return null;
}
data[0..instanceSize] = typeid(Ullocator).initializer[];
instance_ = cast(Ullocator) data;
data[0..instanceSize] = typeid(MmapPool).initializer[];
instance_ = cast(shared MmapPool) data;
instance_.head = head;
}
}
return instance_;
}
///
unittest
nothrow unittest
{
assert(instance is instance);
}
@ -299,11 +324,13 @@ class Ullocator : Allocator
*
* Returns: A pointer to the data.
*/
pragma(inline)
private static void* initializeRegion(size_t size,
ref Region head) nothrow
{
immutable regionSize = calculateRegionSize(size);
version (Posix)
{
void* p = mmap(null,
regionSize,
PROT_READ | PROT_WRITE,
@ -312,8 +339,28 @@ class Ullocator : Allocator
0);
if (p is MAP_FAILED)
{
if (errno == ENOMEM)
{
onOutOfMemoryError();
}
return null;
}
}
else version (Windows)
{
void* p = VirtualAlloc(null,
regionSize,
MEM_COMMIT,
PAGE_READWRITE);
if (p is null)
{
if (GetLastError() == ERROR_NOT_ENOUGH_MEMORY)
{
onOutOfMemoryError();
}
return null;
}
}
Region region = cast(Region) p;
region.blocks = 1;
@ -351,7 +398,7 @@ class Ullocator : Allocator
}
/// Ditto.
private void* initializeRegion(size_t size) nothrow
private void* initializeRegion(size_t size) shared nothrow
{
return initializeRegion(size, head);
}
@ -363,14 +410,15 @@ class Ullocator : Allocator
* Returns: Aligned size of $(D_PARAM x).
*/
pragma(inline)
private static immutable(size_t) addAlignment(size_t x) @safe pure nothrow
private static immutable(size_t) addAlignment(size_t x)
@safe pure nothrow
out (result)
{
assert(result > 0);
}
body
{
return (x - 1) / alignment * alignment + alignment;
return (x - 1) / alignment_ * alignment_ + alignment_;
}
/**
@ -392,32 +440,36 @@ class Ullocator : Allocator
return x / pageSize * pageSize + pageSize;
}
enum alignment = 8;
@property uint alignment() shared const pure nothrow @safe
{
return alignment_;
}
private enum alignment_ = 8;
private static Ullocator instance_;
private static shared MmapPool instance_;
private shared static immutable long pageSize;
private shared static immutable size_t pageSize;
private struct RegionEntry
private shared struct RegionEntry
{
Region prev;
Region next;
uint blocks;
ulong size;
size_t size;
}
private alias Region = RegionEntry*;
private alias Region = shared RegionEntry*;
private enum regionEntrySize = 32;
private Region head;
private shared Region head;
private struct BlockEntry
private shared struct BlockEntry
{
Block prev;
Block next;
bool free;
ulong size;
size_t size;
Region region;
}
private alias Block = BlockEntry*;
private alias Block = shared BlockEntry*;
private enum blockEntrySize = 40;
}

View File

@ -10,198 +10,225 @@
*/
module tanya.memory;
public import tanya.memory.allocator;
public import std.experimental.allocator : make, makeArray, expandArray, shrinkArray, IAllocator;
import core.atomic;
import core.stdc.stdlib;
import core.exception;
public import std.experimental.allocator : make, makeArray;
import std.traits;
public import tanya.memory.allocator;
public import tanya.memory.types;
version (Windows)
// From druntime
private extern (C) void _d_monitordelete(Object h, bool det) nothrow @nogc;
shared Allocator allocator;
shared static this() nothrow @trusted @nogc
{
import core.sys.windows.windows;
}
else version (Posix)
{
public import tanya.memory.ullocator;
import core.sys.posix.pthread;
import tanya.memory.mmappool;
allocator = MmapPool.instance;
}
@nogc:
version (Windows)
@property ref shared(Allocator) defaultAllocator() nothrow @safe @nogc
{
package alias Mutex = CRITICAL_SECTION;
package alias destroyMutex = DeleteCriticalSection;
}
else version (Posix)
{
package alias Mutex = pthread_mutex_t;
package void destroyMutex(pthread_mutex_t* mtx)
{
pthread_mutex_destroy(mtx) && assert(0);
}
return allocator;
}
@property void defaultAllocator(Allocator allocator) @safe nothrow
@property void defaultAllocator(shared(Allocator) allocator) nothrow @safe @nogc
{
_defaultAllocator = allocator;
}
@property Allocator defaultAllocator() @safe nothrow
{
return _defaultAllocator;
}
static this() @safe nothrow
{
defaultAllocator = Ullocator.instance;
}
package struct Monitor
{
Object.Monitor impl; // for user-level monitors
void delegate(Object) @nogc[] devt; // for internal monitors
size_t refs; // reference count
version (Posix)
{
Mutex mtx;
}
}
package @property ref shared(Monitor*) monitor(Object h) pure nothrow
{
return *cast(shared Monitor**)&h.__monitor;
.allocator = allocator;
}
/**
* Destroys and then deallocates (using $(D_PARAM allocator)) the class
* object referred to by a $(D_KEYWORD class) or $(D_KEYWORD interface)
* reference. It is assumed the respective entities had been allocated with
* the same allocator.
* Returns the size in bytes of the state that needs to be allocated to hold an
* object of type $(D_PARAM T).
*
* Params:
* A = The type of the allocator used for the ojbect allocation.
* T = The type of the object that should be destroyed.
* allocator = The allocator used for the object allocation.
* p = The object should be destroyed.
* T = Object type.
*/
void finalize(A, T)(auto ref A allocator, ref T p)
template stateSize(T)
{
static if (is(T == class) || is(T == interface))
{
enum stateSize = __traits(classInstanceSize, T);
}
else
{
enum stateSize = T.sizeof;
}
}
/**
* Params:
* size = Raw size.
* alignment = Alignment.
*
* Returns: Aligned size.
*/
size_t alignedSize(in size_t size, in size_t alignment = 8) pure nothrow @safe @nogc
{
return (size - 1) / alignment * alignment + alignment;
}
/**
* Internal function used to create, resize or destroy a dynamic array. It
* throws $(D_PSYMBOL OutOfMemoryError) if $(D_PARAM Throws) is set. The new
* allocated part of the array is initialized only if $(D_PARAM Init)
* is set. This function can be trusted only in the data structures that
* can ensure that the array is allocated/rellocated/deallocated with the
* same allocator.
*
* Params:
* T = Element type of the array being created.
* Init = If should be initialized.
* Throws = If $(D_PSYMBOL OutOfMemoryError) should be throwsn.
* allocator = The allocator used for getting memory.
* array = A reference to the array being changed.
* length = New array length.
*
* Returns: $(D_KEYWORD true) upon success, $(D_KEYWORD false) if memory could
* not be reallocated. In the latter
*/
package(tanya) bool resize(T,
bool Init = true,
bool Throws = true)
(shared Allocator allocator,
ref T[] array,
in size_t length) @trusted
{
void[] buf = array;
static if (Init)
{
immutable oldLength = array.length;
}
if (!allocator.reallocate(buf, length * T.sizeof))
{
static if (Throws)
{
onOutOfMemoryError;
}
return false;
}
// Casting from void[] is unsafe, but we know we cast to the original type.
array = cast(T[]) buf;
static if (Init)
{
if (oldLength < length)
{
array[oldLength .. $] = T.init;
}
}
return true;
}
package(tanya) alias resizeArray = resize;
///
unittest
{
int[] p;
defaultAllocator.resizeArray(p, 20);
assert(p.length == 20);
defaultAllocator.resizeArray(p, 30);
assert(p.length == 30);
defaultAllocator.resizeArray(p, 10);
assert(p.length == 10);
defaultAllocator.resizeArray(p, 0);
assert(p is null);
}
/**
* Destroys and deallocates $(D_PARAM p) of type $(D_PARAM T).
* It is assumed the respective entities had been allocated with the same
* allocator.
*
* Params:
* T = Type of $(D_PARAM p).
* allocator = Allocator the $(D_PARAM p) was allocated with.
* p = Object or array to be destroyed.
*/
void dispose(T)(shared Allocator allocator, auto ref T* p)
{
static if (hasElaborateDestructor!T)
{
destroy(*p);
}
() @trusted { allocator.deallocate((cast(void*) p)[0 .. T.sizeof]); }();
p = null;
}
/// Ditto.
void dispose(T)(shared Allocator allocator, auto ref T p)
if (is(T == class) || is(T == interface))
{
if (p is null)
{
return;
}
static if (is(T == interface))
{
version(Windows)
{
import core.sys.windows.unknwn : IUnknown;
static assert(!is(T: IUnknown), "COM interfaces can't be destroyed in "
~ __PRETTY_FUNCTION__);
}
auto ob = cast(Object) p;
}
else
{
alias ob = p;
}
auto pp = cast(void*) ob;
auto ppv = cast(void**) pp;
if (!pp || !*ppv)
auto ptr = cast(void *) ob;
auto support = ptr[0 .. typeid(ob).initializer.length];
scope (success)
{
() @trusted { allocator.deallocate(support); }();
p = null;
}
auto ppv = cast(void**) ptr;
if (!*ppv)
{
return;
}
auto support = (cast(void*) ob)[0 .. typeid(ob).initializer.length];
auto pc = cast(ClassInfo*) *ppv;
scope (exit)
{
*ppv = null;
}
auto c = *pc;
do
{
// Assume the destructor is @nogc. Leave it nothrow since the destructor
// shouldn't throw and if it does, it is an error anyway.
if (c.destructor)
{
(cast(void function(Object)) c.destructor)(ob);
(cast(void function (Object) nothrow @safe @nogc) c.destructor)(ob);
}
} while ((c = c.base) !is null);
}
while ((c = c.base) !is null);
// Take care of monitors for synchronized blocks
if (ppv[1])
if (ppv[1]) // if monitor is not null
{
shared(Monitor)* m = atomicLoad!(MemoryOrder.acq)(ob.monitor);
if (m !is null)
{
auto mc = cast(Monitor*) m;
if (!atomicOp!("-=")(m.refs, cast(size_t) 1))
{
foreach (v; mc.devt)
{
if (v)
{
v(ob);
_d_monitordelete(cast(Object) ptr, true);
}
}
if (mc.devt.ptr)
{
free(mc.devt.ptr);
}
destroyMutex(&mc.mtx);
free(mc);
atomicStore!(MemoryOrder.rel)(ob.monitor, null);
}
}
}
*ppv = null;
allocator.deallocate(support);
p = null;
}
/// Ditto.
void finalize(A, T)(auto ref A allocator, ref T *p)
if (is(T == struct))
void dispose(T)(shared Allocator allocator, auto ref T[] array)
{
if (p is null)
static if (hasElaborateDestructor!(typeof(array[0])))
{
return;
}
static if (hasElaborateDestructor!T)
foreach (ref e; array)
{
*p.__xdtor();
}
allocator.deallocate((cast(void*)p)[0 .. T.sizeof]);
p = null;
}
/// Ditto.
void finalize(A, T)(auto ref A allocator, ref T[] p)
{
static if (hasElaborateDestructor!T)
{
foreach (ref e; p)
{
finalize(allocator, e);
destroy(e);
}
}
allocator.deallocate(p);
p = null;
() @trusted { allocator.deallocate(array); }();
array = null;
}
bool resizeArray(T, A)(auto ref A allocator, ref T[] array, in size_t length)
@trusted
{
if (length == array.length)
{
return true;
}
if (array is null && length > 0)
{
array = makeArray!T(allocator, length);
return array !is null;
}
if (length == 0)
{
finalize(allocator, array);
return true;
}
void[] buf = array;
if (!allocator.reallocate(buf, length * T.sizeof))
{
return false;
}
array = cast(T[]) buf;
return true;
}
enum bool isFinalizable(T) = is(T == class) || is(T == interface)
|| hasElaborateDestructor!T || isDynamicArray!T;
private Allocator _defaultAllocator;

457
source/tanya/memory/types.d Normal file
View File

@ -0,0 +1,457 @@
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
/**
* Copyright: Eugene Wissner 2016.
* License: $(LINK2 https://www.mozilla.org/en-US/MPL/2.0/,
* Mozilla Public License, v. 2.0).
* Authors: $(LINK2 mailto:belka@caraus.de, Eugene Wissner)
*/
module tanya.memory.types;
import core.exception;
import std.algorithm.comparison;
import std.algorithm.mutation;
import std.conv;
import std.traits;
import tanya.memory;
/**
* Reference-counted object containing a $(D_PARAM T) value as payload.
* $(D_PSYMBOL RefCounted) keeps track of all references of an object, and
* when the reference count goes down to zero, frees the underlying store.
*
* Params:
* T = Type of the reference-counted value.
*/
struct RefCounted(T)
{
static if (is(T == class) || is(T == interface))
{
private alias Payload = T;
}
else
{
private alias Payload = T*;
}
private class Storage
{
private Payload payload;
private size_t counter = 1;
private final size_t opUnary(string op)() pure nothrow @safe @nogc
if (op == "--" || op == "++")
in
{
assert(counter > 0);
}
body
{
mixin("return " ~ op ~ "counter;");
}
private final int opCmp(size_t counter) const pure nothrow @safe @nogc
{
if (this.counter > counter)
{
return 1;
}
else if (this.counter < counter)
{
return -1;
}
else
{
return 0;
}
}
private final int opEquals(size_t counter) const pure nothrow @safe @nogc
{
return this.counter == counter;
}
}
private final class RefCountedStorage : Storage
{
private shared Allocator allocator;
this(shared Allocator allocator) pure nothrow @safe @nogc
in
{
assert(allocator !is null);
}
body
{
this.allocator = allocator;
}
~this() nothrow @nogc
{
allocator.dispose(payload);
}
}
private Storage storage;
invariant
{
assert(storage is null || allocator_ !is null);
}
/**
* Takes ownership over $(D_PARAM value), setting the counter to 1.
* $(D_PARAM value) may be a pointer, an object or a dynamic array.
*
* Params:
* value = Value whose ownership is taken over.
* allocator = Allocator used to destroy the $(D_PARAM value) and to
* allocate/deallocate internal storage.
*
* Precondition: $(D_INLINECODE allocator !is null)
*/
this(Payload value, shared Allocator allocator = defaultAllocator)
{
this(allocator);
storage = allocator.make!RefCountedStorage(allocator);
move(value, storage.payload);
}
/// Ditto.
this(shared Allocator allocator)
in
{
assert(allocator !is null);
}
body
{
this.allocator_ = allocator;
}
/**
* Increases the reference counter by one.
*/
this(this)
{
if (count != 0)
{
++storage;
}
}
/**
* Decreases the reference counter by one.
*
* If the counter reaches 0, destroys the owned object.
*/
~this()
{
if (storage !is null && !(storage.counter && --storage))
{
allocator_.dispose(storage);
}
}
/**
* Takes ownership over $(D_PARAM rhs). Initializes this
* $(D_PSYMBOL RefCounted) if needed.
*
* If it is the last reference of the previously owned object,
* it will be destroyed.
*
* To reset the $(D_PSYMBOL RefCounted) assign $(D_KEYWORD null).
*
* If the allocator wasn't set before, $(D_PSYMBOL defaultAllocator) will
* be used. If you need a different allocator, create a new
* $(D_PSYMBOL RefCounted) and assign it.
*
* Params:
* rhs = $(D_KEYWORD this).
*/
ref typeof(this) opAssign(Payload rhs)
{
if (storage is null)
{
storage = allocator.make!RefCountedStorage(allocator);
}
else if (storage > 1)
{
--storage;
storage = allocator.make!RefCountedStorage(allocator);
}
else if (cast(RefCountedStorage) storage is null)
{
// Created with refCounted. Always destroyed togethter with the pointer.
assert(storage.counter != 0);
allocator.dispose(storage);
storage = allocator.make!RefCountedStorage(allocator);
}
else
{
allocator.dispose(storage.payload);
}
move(rhs, storage.payload);
return this;
}
/// Ditto.
ref typeof(this) opAssign(typeof(null))
{
if (storage is null)
{
return this;
}
else if (storage > 1)
{
--storage;
storage = null;
}
else if (cast(RefCountedStorage) storage is null)
{
// Created with refCounted. Always destroyed togethter with the pointer.
assert(storage.counter != 0);
allocator.dispose(storage);
return this;
}
else
{
allocator.dispose(storage.payload);
}
return this;
}
/// Ditto.
ref typeof(this) opAssign(typeof(this) rhs)
{
swap(allocator_, rhs.allocator_);
swap(storage, rhs.storage);
return this;
}
/**
* Returns: Reference to the owned object.
*/
inout(Payload) get() inout pure nothrow @safe @nogc
in
{
assert(count > 0, "Attempted to access an uninitialized reference.");
}
body
{
return storage.payload;
}
static if (isPointer!Payload)
{
/**
* Params:
* op = Operation.
*
* Dereferences the pointer. It is defined only for pointers, not for
* reference types like classes, that can be accessed directly.
*
* Returns: Reference to the pointed value.
*/
ref T opUnary(string op)()
if (op == "*")
{
return *storage.payload;
}
}
/**
* Returns: Whether this $(D_PSYMBOL RefCounted) already has an internal
* storage.
*/
@property bool isInitialized() const
{
return storage !is null;
}
/**
* Returns: The number of $(D_PSYMBOL RefCounted) instances that share
* ownership over the same pointer (including $(D_KEYWORD this)).
* If this $(D_PSYMBOL RefCounted) isn't initialized, returns `0`.
*/
@property size_t count() const
{
return storage is null ? 0 : storage.counter;
}
mixin DefaultAllocator;
alias get this;
}
///
unittest
{
auto rc = RefCounted!int(defaultAllocator.make!int(5), defaultAllocator);
auto val = rc.get;
*val = 8;
assert(*rc.storage.payload == 8);
val = null;
assert(rc.storage.payload !is null);
assert(*rc.storage.payload == 8);
*rc = 9;
assert(*rc.storage.payload == 9);
}
version (unittest)
{
class A
{
uint *destroyed;
this(ref uint destroyed) @nogc
{
this.destroyed = &destroyed;
}
~this() @nogc
{
++(*destroyed);
}
}
struct B
{
int prop;
@disable this();
this(int param1) @nogc
{
prop = param1;
}
}
}
private unittest
{
uint destroyed;
auto a = defaultAllocator.make!A(destroyed);
assert(destroyed == 0);
{
auto rc = RefCounted!A(a, defaultAllocator);
assert(rc.count == 1);
void func(RefCounted!A rc)
{
assert(rc.count == 2);
}
func(rc);
assert(rc.count == 1);
}
assert(destroyed == 1);
RefCounted!int rc;
assert(rc.count == 0);
rc = defaultAllocator.make!int(8);
assert(rc.count == 1);
}
private unittest
{
static assert(is(typeof(RefCounted!int.storage.payload) == int*));
static assert(is(typeof(RefCounted!A.storage.payload) == A));
static assert(is(RefCounted!B));
static assert(is(RefCounted!A));
}
/**
* Constructs a new object of type $(D_PARAM T) and wraps it in a
* $(D_PSYMBOL RefCounted) using $(D_PARAM args) as the parameter list for
* the constructor of $(D_PARAM T).
*
* This function is more efficient than the using of $(D_PSYMBOL RefCounted)
* directly, since it allocates only ones (the internal storage and the
* object).
*
* Params:
* T = Type of the constructed object.
* A = Types of the arguments to the constructor of $(D_PARAM T).
* args = Constructor arguments of $(D_PARAM T).
*
* Returns: Newly created $(D_PSYMBOL RefCounted!T).
*/
RefCounted!T refCounted(T, A...)(shared Allocator allocator, auto ref A args)
if (!is(T == interface) && !isAbstractClass!T
&& !isArray!T && !isAssociativeArray!T)
{
auto rc = typeof(return)(allocator);
immutable storageSize = alignedSize(stateSize!(RefCounted!T.Storage));
immutable size = alignedSize(stateSize!T + storageSize);
auto mem = (() @trusted => allocator.allocate(size))();
if (mem is null)
{
onOutOfMemoryError();
}
scope (failure)
{
() @trusted { allocator.deallocate(mem); }();
}
rc.storage = emplace!(RefCounted!T.Storage)(mem[0 .. storageSize]);
static if (is(T == class))
{
rc.storage.payload = emplace!T(mem[storageSize .. $], args);
}
else
{
auto ptr = (() @trusted => (cast(T*) mem[storageSize .. $].ptr))();
rc.storage.payload = emplace!T(ptr, args);
}
return rc;
}
///
unittest
{
auto rc = defaultAllocator.refCounted!int(5);
assert(rc.count == 1);
void func(RefCounted!int param)
{
if (param.count == 2)
{
func(param);
}
else
{
assert(param.count == 3);
}
}
func(rc);
assert(rc.count == 1);
}
private @nogc unittest
{
struct E
{
}
auto b = defaultAllocator.refCounted!B(15);
static assert(is(typeof(b.storage.payload) == B*));
static assert(is(typeof(b.prop) == int));
static assert(!is(typeof(defaultAllocator.refCounted!B())));
static assert(is(typeof(defaultAllocator.refCounted!E())));
static assert(!is(typeof(defaultAllocator.refCounted!E(5))));
{
auto rc = defaultAllocator.refCounted!B(3);
assert(rc.get.prop == 3);
}
{
auto rc = defaultAllocator.refCounted!E();
assert(rc.count);
}
}

File diff suppressed because it is too large Load Diff

1191
source/tanya/network/url.d Normal file

File diff suppressed because it is too large Load Diff