MyBB Community Forums

Full Version: Custom PHP modules
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
I've decided that I would get into writing modules for PHP for a brief moment.

So far I've added two modules to PHP 7.3.9 (and I'm not sure if these already exist but anywho...)

Here's my favorite of the two modules: PCG-64 in PHP

Quote:PCG (Permutable Congruence Generator) is a PRNG method that was written by Melissa O'Neill and it is supposedly a more robust and lightweight PRNG method when compared to the competition (including Mersenne Twister).

I decided to take the 64-bit implementation (making this a module that solely functions with 64-bit versions of PHP) and I changed the seeding method from the standard system timestamp to the value that is returned by the RDTSC assembly instruction.

This means that the extension will only function on CPUs that support either the x86_64 or the AMD64 instruction set. I can't guarantee the robustness of this method when compared to the timestamp method in the original sources, but I know it will return a different value every time.

Here's my other module: Base32 in PHP

Quote:The Base32 encoding method is one of those encoding methods that we don't tend to find useful unless it's in the case of generating 2FA tokens (that's very important).

I did make a PHP-only variant of this and I'm going to implement that into a future plugin, but I also wanted a module that would outperform a PHP-only variant of Base32 encoding/decoding.

Maybe the PHP 8 JIT will push PHP into speeds closer to C, but I don't have too high of hopes of that becoming a reality based on the previous track record for Zend (not to hate on Zend or anything, but I've lost a lot of faith in them over the years).

Let me know if you want me to continue down this road or if you have anything you want to see converted into a PHP module. I don't know if the staff on this forum will allow me to take requests like this, but if they do then I'd like for all requests to follow an organized structure:

Module Name
Target PHP Version

function_name_1(type arg1, type arg2, type arg3, ...) : return_type;
function_name_2(type arg1, type arg2, type arg3, ...) : return_type;
function_name_3(type arg1, type arg2, type arg3, ...) : return_type;

Just bear in mind that this is just an offer from me to get some experience with writing modules for PHP, don't expect me to produce something quickly unless it's a small module. I'll retain the right to upload the module to my GitHub page and that's where I'll link you so that you can download the sources to compile for yourself since I only have access to a Linux machine at the moment.
That's pretty neat! Is it cross platform?
Thinking about what modules to create, I guess anything that could use a speed in PHP.
(2019-09-09, 07:33 PM)vbgamer45 Wrote: [ -> ]That's pretty neat!  Is it cross platform? 
Thinking about what modules to create, I guess anything that  could use a speed in PHP.

The Base32 plugin is definitely cross-platform, but the PCG plugin might not work on processors that don't have the RDTSC instruction (AMD64 and x86_64 have this instruction from what I can tell).

The original sources by Melissa O'Neal used the system time as a seed, but I figured that I could make the whole thing faster and more portable by implementing some good ol' inline assembly that should work with GCC.

I haven't tested it with MSVC or Clang and if it breaks on those compilers, then it's a simple matter of replacing the inline assembly with:
#include <time.h>

void seed(unsigned long long output) {
    output = (unsigned long long) time(NULL);
}

And that should get the job done just fine, I am somewhat worried about testing it with values higher than a signed 64-bit integer since that's PHP's limitation with 64-bit installations. I don't know why they limit it like that, but hopefully it handles it just fine or else you might end up getting an Infinity or a negative value due to buffer overflow when binding values larger than 9223372036854775807 (-1 + 1 << 63).

[EDIT]

Turns out that attempting to run pcg_random() with a value over 9223372036854775807 will return an error regarding the use of a float instead of an integer. I'll look into dealing with larger values than this and I'll update the PCG module sources and release a 3rd module for handling data types larger than 64-bits in length with PHP.