Generate large prime numbers using boost library

Recently I am working on a RSA like algorithm project, which needs to generate large secret prime numbers, e.g. 512 bits, or 1024 bits. Luckily we are using boost library, which is inbuilt lots of functions to assist this generation.

#include <boost/multiprecision/miller_rabin.hpp>
#include <boost/random/mersenne_twister.hpp>

using namespace boost::multiprecision;
using namespace boost::random;

...
//Generate a secret RSA-like 512 bits primes p
cpp_int p = GetPrime();
...
cpp_int GetPrime()
{
    mt11213b base_gen(clock());
    independent_bits_engine<mt11213b, 512, cpp_int> gen(base_gen);

    // Generate some large random primes
    // Note 25 trials of Miller-Rabin 
    // likelihood that number is prime
    cpp_int n;
    do
    {
        n = gen();
    }while (!miller_rabin_test(n, 25));
    return n;
}

Change IP address & Mask address (C++) Windows

Introduction

This is one way of changing IP address & mask address in C++ using Iphlpapi.h. It’s a simple program, no UI for you, sorry…

  • Use GetAdaptersInfo to get adapters information.
  • Then use GetPerAdapterInfo to get information for every ethernet network card. The information contains IP address.
  • Use DeleteIPAddress remove the old IP address.
  • Use AddIPAddress to add new IP&Mask address.
// ChangeIPAddress("192.168.0.123", "192.168.0.44","255,255,255,0");
bool ChangeIPAddress(char oldIPAddress[], char newIPAddress[], char newMaskAddress[])
{
    DWORD dwRetVal = 0;
    PIP_ADAPTER_INFO pAdapter = NULL;

    ULONG ulOutBufLen = sizeof(IP_ADAPTER_INFO);
    PIP_ADAPTER_INFO pAdapterInfo = (IP_ADAPTER_INFO *)malloc(sizeof(IP_ADAPTER_INFO));
    if (pAdapterInfo == NULL)
    {
        return false;
    }

    if (GetAdaptersInfo(pAdapterInfo, &ulOutBufLen) == ERROR_BUFFER_OVERFLOW)
    {
        free(pAdapterInfo);
        pAdapterInfo = (IP_ADAPTER_INFO *)malloc(ulOutBufLen);
        if (pAdapterInfo == NULL)
        {
            return false;
        }
    }

    if ((dwRetVal = GetAdaptersInfo(pAdapterInfo, &ulOutBufLen)) == NO_ERROR)
    {
        pAdapter = pAdapterInfo;
        while (pAdapter)
        {
            if (strcmp(oldIPAddress, pAdapter->IpAddressList.IpAddress.String) == 0)
            {
                IPAddr addr = inet_addr(newIPAddress);
                IPMask mask = inet_addr(newMaskAddress);
                ULONG context, instance;

                if (DeleteIPAddress(pAdapter->IpAddressList.Context) != NO_ERROR
                    || AddIPAddress(addr, mask, pAdapter->Index, &context, &instance) != NO_ERROR)
                {
                    return false;
                }
                return true;
            }
            pAdapter = pAdapter->Next;
        }
        return false;
    }
}

Windows physical disk image writer in C

My recent project is to write a raw image file into CFast card, and I was looking for a physical disk writer on google. Finally I found this http://m0n0.ch/wall/physdiskwrite.php, but it doesn’t work properly.

  1. GUI is not Englisht
  2. When image size is too big, it doesn’t show the correct size.

Then I decide to make my own physical disk image writer in C.

Be extremely careful to use this tool, incorrect disk number can completely corrupt your disk data! I am not responsible for this.

Download

PhyDiskWrite.zip (4k)

#include <windows.h>

void PrintHelp()
{
    printf("Usage: phydiskwrite [imagefile]\r\n\r\n");
}

HANDLE OpenDisk(int deviceID)
{
    HANDLE hDisk;
    TCHAR diskName[MAX_PATH];
    _stprintf_s(diskName, _T("\\\\.\\PhysicalDrive%d"), deviceID);
    hDisk = CreateFile(diskName, 
        GENERIC_READ | GENERIC_WRITE, 
        FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, OPEN_EXISTING, 0, NULL);
    return hDisk;
}

int PrintDisks()
{
    printf("Searching for physical drives...\r\n\r\n");
    DWORD bytes;
    TCHAR diskName[MAX_PATH];
    HANDLE hDisk;
    int selectedDisk = -1;
    for (int i = 0; i < 255; i++)
    {
        hDisk = OpenDisk(i);
        if (hDisk == INVALID_HANDLE_VALUE)
            continue;
        printf("Information for Device %d\r\n", i);

        DISK_GEOMETRY pdg = { 0 };
        if (!DeviceIoControl(hDisk, IOCTL_DISK_GET_DRIVE_GEOMETRY, NULL, 0, &pdg, sizeof(pdg), &bytes, NULL))
        {
            printf("error: Get disk info failed %d\r\n", GetLastError());
            continue;
        }
        ULONGLONG DiskSize = pdg.Cylinders.QuadPart 
            * (ULONG)pdg.TracksPerCylinder 
            * (ULONG)pdg.SectorsPerTrack 
            * (ULONG)pdg.BytesPerSector;
        CloseHandle(hDisk);
        printf("\tByte/Sec %d, Cylinders: %d, Sector/Track %d, Track/Cylinder %d\r\n", 
            pdg.BytesPerSector, pdg.Cylinders, pdg.SectorsPerTrack, pdg.TracksPerCylinder);
        printf("\tDevice:%d, Media Type:%d Disk Size: %.2f (Gb)\r\n\r\n", 
            i, pdg.MediaType, (double)DiskSize / (1024.0f * 1024.0f * 1024.0f));
    }
    printf("Which disk do you want to write?\r\n");
    printf("(Be careful, wrong disk number could corrupt your entire disk!)\r\n");
    scanf("%d", &selectedDisk);
    return selectedDisk;
}

int _tmain(int argc, _TCHAR* argv[])
{   
    if (argc != 2)
    {
        PrintHelp();
        return 0;
    }
    
    int devID = PrintDisks();
    if (devID != -1)
    {
        HANDLE hDisk = OpenDisk(devID);
        BYTE buffer[65536];
        DWORD byteRead, byteWrite, status;
        ULONGLONG totalWrite = 0;
        LARGE_INTEGER fileSize;
        HANDLE hImage = CreateFile(argv[1], 
            GENERIC_READ, FILE_SHARE_READ, NULL, 
            OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
        if (hImage == INVALID_HANDLE_VALUE)
        {
            printf("error: Open file failed %d", GetLastError());
            return -1;
        }

        GetFileSizeEx(hImage, &fileSize);
        while (ReadFile(hImage, buffer, 65536, &byteRead, NULL) && byteRead > 0)
        {
            if (!WriteFile(hDisk, buffer, byteRead, &byteWrite, NULL))
            {
                printf("error: Write to disk failed %d", GetLastError());
                break;
            }
            else
            {
                printf("\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b");
                totalWrite += byteRead;
                printf("%f%%", totalWrite * 100.0f / fileSize.QuadPart);
            }
        }
        CloseHandle(hImage);
        CloseHandle(hDisk);
    }
    return 0;
}

SIMD high performace xorcpy in C

In my current project I need a high performace xorcpy implemented in C. Here is my implementation to share and it is tested on my Xeon E5-2680 PC, it reaches almost 1.7GB/s.

#include <emmintrin.h>
...
__forceinline unsigned char* xorcpy(unsigned char* dst, const unsigned char* src, unsigned block_size)
{
    // Do the bulk of the copy a __m128i at a time, for faster speed
    __m128i* mto = (__m128i*)dst;
    const __m128i* mfrom = (__m128i*)(src);
    for(int i=(block_size / sizeof(__m128i) - 1); i>=0; i--)
    {
        __m128i xmm1 = _mm_loadu_si128(mto);
        __m128i xmm2 = _mm_loadu_si128(mfrom);

        xmm1 = _mm_xor_si128(xmm1, xmm2);     //  XOR 16 bytes
        _mm_storeu_si128(mto, xmm1);
        ++mto;
        ++mfrom;
    }

    // The rest bytes we have to do a byte a time though
    unsigned char* cto = (unsigned char*) mto;
    const unsigned char* cfrom = (const unsigned char*)mfrom;
    for(int i=(block_size % sizeof(__m128i)) - 1; i>=0; i--)
    {
        *cto++ ^= (*cfrom++);
    }
    return dst;
}

Convert DIB or BMP into JPEG in memory (diskless) using Windows GDIPlus

    
    #include 
    #include 

    using namespace Gdiplus;

    GdiplusStartupInput gdiplusStartupInput;
    ULONG_PTR gdiplusToken;
    GdiplusStartup(&gdiplusToken, &gdiplusStartupInput, NULL);
    ...
    IStream* pJpegStream = NULL;  //declare a memory stream
    CreateStreamOnHGlobal(NULL, TRUE, (LPSTREAM*)&pJpegStream);

    GpBitmap* pBitmap = NULL;
    unsigned char* dib = GetDib(); //get DIB or BMP data buffer
    DllExports::GdipCreateBitmapFromGdiDib((LPBITMAPINFO)dib, dib + sizeof(BITMAPINFOHEADER), &pBitmap);

    CLSID imageCLSID;
    GetEncoderClsid(L"image/jpeg", &imageCLSID);

    int jpegQuality = 100;

    EncoderParameters encoderParams; //setup jpeg encoder parameters
    encoderParams.Count = 1;
    encoderParams.Parameter[0].NumberOfValues = 1;
    encoderParams.Parameter[0].Guid = EncoderQuality;
    encoderParams.Parameter[0].Type = EncoderParameterValueTypeLong;
    encoderParams.Parameter[0].Value = &jpegQuality;

    //save jpeg into memory stream
    DllExports::GdipSaveImageToStream(pBitmap, pJpegStream, &imageCLSID, &encoderParams);

    LARGE_INTEGER lnOffset;
    lnOffset.QuadPart = 0;
    ULARGE_INTEGER ulnSize;
    //determine memory stream length
    pJpegStream->Seek(lnOffset, STREAM_SEEK_END, &ulnSize);
    pJpegStream->Seek(lnOffset, STREAM_SEEK_SET, NULL);
    int encodedBytes = ulnSize.QuadPart;
    
    //store jpeg memory stream into jpeg data buffer
    pJpegStream->Read(racBuf_, ulnSize.QuadPart, NULL);
    ...
    GdiplusShutdown(gdiplusToken);

Programmably dislable Windows Firewall in C/C++

// firewall.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
/********************************************************************++
Copyright (C) Microsoft. All Rights Reserved.

Abstract:
    This C++ file includes sample code for disabling Windows Firewall 
    per profile using the Microsoft Windows Firewall APIs.

--********************************************************************/
#include 
#include 
#include 

#pragma comment( lib, "ole32.lib" )

// Forward declarations
HRESULT     WFCOMInitialize(INetFwPolicy2** ppNetFwPolicy2);

int __cdecl main()
{
    HRESULT hrComInit = S_OK;
    HRESULT hr = S_OK;

    INetFwPolicy2 *pNetFwPolicy2 = NULL;

    // Initialize COM.
    hrComInit = CoInitializeEx(
                    0,
                    COINIT_APARTMENTTHREADED
                    );

    // Ignore RPC_E_CHANGED_MODE; this just means that COM has already been
    // initialized with a different mode. Since we don't care what the mode is,
    // we'll just use the existing mode.
    if (hrComInit != RPC_E_CHANGED_MODE)
    {
        if (FAILED(hrComInit))
        {
            printf("CoInitializeEx failed: 0x%08lxn", hrComInit);
            goto Cleanup;
        }
    }

    // Retrieve INetFwPolicy2
    hr = WFCOMInitialize(&pNetFwPolicy2);
    if (FAILED(hr))
    {
        goto Cleanup;
    }

    // Disable Windows Firewall for the Domain profile
    hr = pNetFwPolicy2->put_FirewallEnabled(NET_FW_PROFILE2_DOMAIN, FALSE);
    if (FAILED(hr))
    {
        printf("put_FirewallEnabled failed for Domain: 0x%08lxn", hr);
        goto Cleanup;
    }

    // Disable Windows Firewall for the Private profile
    hr = pNetFwPolicy2->put_FirewallEnabled(NET_FW_PROFILE2_PRIVATE, FALSE);
    if (FAILED(hr))
    {
        printf("put_FirewallEnabled failed for Private: 0x%08lxn", hr);
        goto Cleanup;
    }

    // Disable Windows Firewall for the Public profile
    hr = pNetFwPolicy2->put_FirewallEnabled(NET_FW_PROFILE2_PUBLIC, FALSE);
    if (FAILED(hr))
    {
        printf("put_FirewallEnabled failed for Public: 0x%08lxn", hr);
        goto Cleanup;
    }

Cleanup:

    // Release INetFwPolicy2
    if (pNetFwPolicy2 != NULL)
    {
        pNetFwPolicy2->Release();
    }

    // Uninitialize COM.
    if (SUCCEEDED(hrComInit))
    {
        CoUninitialize();
    }
   
    return 0;
}

// Instantiate INetFwPolicy2
HRESULT WFCOMInitialize(INetFwPolicy2** ppNetFwPolicy2)
{
    HRESULT hr = S_OK;

    hr = CoCreateInstance(
        __uuidof(NetFwPolicy2), 
        NULL, 
        CLSCTX_INPROC_SERVER, 
        __uuidof(INetFwPolicy2), 
        (void**)ppNetFwPolicy2);

    if (FAILED(hr))
    {
        printf("CoCreateInstance for INetFwPolicy2 failed: 0x%08lxn", hr);
        goto Cleanup;        
    }

Cleanup:
    return hr;
}

Maximize the output of your CPU (Keep your CPU in full power mode)

Recently I am working on UI decoding optimization. I found this program, Full Throttle Override, is very useful, and it can fully release the power of your CPU.

To balance of power consumption and performance, almost all x86 CPUs support either Cool’n’Quiet or SpeedStep or PowerNow! technology, which can dynamically adjust the CPU frequency based on the loading.

I found it’s pretty easy to implement Full Throttle Override and here is the core C++ code

		
void FullThrottle()
{
    OSVERSIONINFO osvi;
    memset(&osvi, 0, sizeof(OSVERSIONINFO));
    osvi.dwOSVersionInfoSize = sizeof(OSVERSIONINFO);
    GetVersionEx(&osvi);
    // For Vista and above
    if (osvi.dwMajorVersion >= 6)
    {
	GUID *scheme;
	PowerGetActiveScheme(NULL, &scheme);
	PowerWriteACValueIndex(NULL
            , scheme
            , &GUID_PROCESSOR_SETTINGS_SUBGROUP
            , &GUID_PROCESSOR_THROTTLE_MINIMUM
            , 100);
	PowerSetActiveScheme(NULL, scheme);
    }
    else
    {
	MessageBox(NULL, L"Not supported by your OS!",L"",0);
    }
}

Feel the power of parallel computing (OpenMP)

These two weeks, I am working on our product UI side to improve the performance of animation rendering. Previously, there is only one single thread to decode the animation line by line, and it takes around 50ms for the whole frame.

org

Now, I change the way of rendering, and let all lines parallel decode to fully take advantage of modern multi-core CPU.

new

 

Visual Studio natively supports OpenMP, it gives me a easy way to access this powerful tool.

To set this compiler option in the Visual Studio development environment

  1. Open the project’s Property Pages dialog box. For details, see How to: Open Project Property Pages.
  2. Expand the Configuration Properties node.
  3. Expand the C/C++ node.
  4. Select the Language property page.
  5. Modify the OpenMP Support property.

After some simple code update, surprisingly, I found that my frame decoding performance boosts 950% (almost 10 times faster), from 8 FPS to 76 FPS!

 

Let’s do simple test with the following code:

#define TEST_LENGTH 0x3fffffff

double mptest()
{
    LARGE_INTEGER  large_interger;
    double dff;
    __int64  c1, c2;
    QueryPerformanceFrequency(&large_interger);
    dff = large_interger.QuadPart;
    //
    unsigned char *test = new unsigned char[TEST_LENGTH];
    QueryPerformanceCounter(&large_interger);
    c1 = large_interger.QuadPart;
    #pragma omp parallel for
    for (int i = 0; i<TEST_LENGTH; i++)
    {
        test[i] = rand();
    }
    QueryPerformanceCounter(&large_interger);
    c2 = large_interger.QuadPart;
    delete test;
    return (c2 - c1) * 1000.0f / dff;
}

double test()
{
    LARGE_INTEGER  large_interger;
    double dff;
    __int64  c1, c2;
    QueryPerformanceFrequency(&large_interger);
    dff = large_interger.QuadPart;
    //
    unsigned char *test = new unsigned char[TEST_LENGTH];
    QueryPerformanceCounter(&large_interger);
    c1 = large_interger.QuadPart;
    for (int i = 0; i<TEST_LENGTH; i++)
    {
        test[i] = rand();
    }
    QueryPerformanceCounter(&large_interger);
    c2 = large_interger.QuadPart;
    delete test;
    return (c2 - c1) * 1000.0f / dff;
}

int _tmain(int argc, _TCHAR* argv[])
{
    printf("Random generation cost with MP %lfmsn", mptest());
    printf("Random generation cost without MP %lfmsn", test());
    _getch();
    return 0;
}

Look at the huge difference!

result

A funny joke application makes your window dance

These days I am so busy with work, and haven’t got time to write anything.

This is a funny joke application I wrote long time back. It will make your front window dance. If you press any key, this application will exit.

You can download here
This is the code, in case if you want to make your own.

#include "stdafx.h"
#include "math.h"
#include "windows.h"
#include "time.h"
#include "conio.h"

int _tmain(int argc, _TCHAR* argv[])
{
	RECT rect;
	int offset;
	while(!kbhit())
	{
		HWND hWnd = GetForegroundWindow();
		GetWindowRect(hWnd,&rect);
		offset = sin((float)rand())*10;
		rect.left += offset;
		rect.bottom += offset;
		rect.right += offset;
		rect.top += offset;
		MoveWindow(hWnd,rect.left,rect.top,rect.right-rect.left,rect.bottom-rect.top,true);
		Sleep(20);
	}
	return 0;
}

Web (HTTP) based remote PC monitor

VNC and Microsoft RemoteDesktop both provide remote control software which lets you see and interact with desktop applications across any network, however either software requires to install a client application on control side.

Here I have developed a small tool, WebPCMonitor. It will allow you to see and interact with your own PC desktop through HTTP protocol. It means that on client side, you don’t need install any 3rd party tool but a internet browser. Simply type your remote PC’s name or IP address, and you will see your PC’s desktop. You even can remote control your PC through PDA or SmartPhone (eg iPhone, iPad, Android, etc) as long as you have internet access. This tool works with Win98, Win2000, WinXP, and Win7.

You can download here.

Run the application, and open your browser and type “http://localhost:8000“. If you know your PC’s IP address, you can remote access through PDA or SmartPhone by typing “http://address:8000