Variable Length PWSTR two dimensional array

 

“Development for AD RMS it is Programing Nightmare, because of weak SDK documentation if any”

Hello everybody

Its been awhile since i posted any technical jargon, so today i will share with you something i was working on lately which is related to AD RMS “Active Directory Rights Management System”.

What i want to share with you today is a function which reads a structured PWSTR string and parse into 2 dimensional array of PWSTR; kindly note that PWSTR is a typedef for WCHAR *. The funny thing that i needed to do 2 dimensional array with a variable length because the size of the every arguments is not the same

Note the Structured String has EOF at the end which will identify that the string is finished “I know that this means EOF but i got used to use it :D”

I hope that you will enjoy the code as i did

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

#include "stdafx.h"

/*
 * Get Users Count because it will be used in calculating buffer size
 */
int GetUsersIDCount(PWSTR listOfRightsPerUser)
{
    int usersCount = 0; 

    for ( int i=0 ; i <= wcslen(listOfRightsPerUser) ; i++ )
    {
        if ( *(listOfRightsPerUser+i) == ';')
            usersCount+=1;
    }
    return usersCount;
}

/* 
 * listOfRightsPerUser should be structured like the following 
 * "UserID1:ListOfRights1;UserID2:ListOfRights2; ..."
 */
void GetUserAndRights(PWSTR listOfRightsPerUser, OUT PWSTR *(&Users), OUT PWSTR *(&Rights))
{
    PWSTR pch = NULL; // this will mark the location of the substrings

    int USERSCOUNT = GetUsersIDCount(listOfRightsPerUser);

    Users = (PWSTR*)malloc(USERSCOUNT);
    Rights = (PWSTR*)malloc(USERSCOUNT);

    memset(Users,0,USERSCOUNT);
    memset(Rights,0,USERSCOUNT);

    WCHAR *nextUserPosition = NULL;
    WCHAR *uPosition = NULL;
    WCHAR *rPosition = NULL;

    int nextPosition = 0;

    for (int NumOfUsers = 0 ; NumOfUsers <= GetUsersIDCount(listOfRightsPerUser) ; NumOfUsers++ )
    {

        if (NumOfUsers > 0)
        {
            listOfRightsPerUser = nextUserPosition;
        }

        uPosition = wcspbrk(listOfRightsPerUser,L":");                    // User position
        rPosition = uPosition + 1;                                        // Rights Postion
        nextUserPosition = wcspbrk(listOfRightsPerUser,L";") + 1;        // Next user and rights position

        nextPosition = wcslen(nextUserPosition);

        WCHAR *uBuffer = (WCHAR*)malloc((wcslen(listOfRightsPerUser) - wcslen(uPosition))*sizeof(WCHAR));        // Allocating User buffer
        WCHAR *rBuffer = (WCHAR*)malloc((wcslen(uPosition) - nextPosition -1))*sizeof(WCHAR);                    // Allocating Rights buffer

        memset(uBuffer , 0 , wcslen(listOfRightsPerUser) - wcslen(uPosition) -1);                 // initializing uBuffer
        memset(rBuffer , 0 , wcslen(uPosition) - nextPosition -1);                                // initializing rBuffer

        // Loop to fill the user
        for (int uIndex = 0 ; uIndex <= wcslen(listOfRightsPerUser) - wcslen(uPosition); uIndex++)
        {
            if (*(listOfRightsPerUser + uIndex) == L':')
            {
                *(uBuffer + uIndex) = 0;
                Users[NumOfUsers]=uBuffer;
                break;
            }
            else
                *(uBuffer + uIndex) = *(listOfRightsPerUser + uIndex);
        }

        for (int rIndex = 0, startingPoint = wcslen(uBuffer)+1 ; rIndex <= nextPosition - wcslen(listOfRightsPerUser) ; rIndex ++ , startingPoint++)
        {
            if (*(listOfRightsPerUser + startingPoint) == L';')
            {
                *(rBuffer + rIndex) = 0;
                Rights[NumOfUsers] = rBuffer;
                break;
            }else
                *(rBuffer + rIndex) = *(listOfRightsPerUser + startingPoint);
        }

        // deallocating user abd rights buffers 
        uBuffer = NULL;
        rBuffer = NULL;
    }
}

int main()
{
    PWSTR *userID = NULL;
    PWSTR *rightsPerUser = NULL;

    PWSTR UsersRightsList = L"User1@xxx.com:DOCEDIT,EDIT,VIEW,EXPORT;User2@xxx.com:VIEW,EXPORT;User3@xxx.com:DOCEDIT,EDIT;EOF";

    // return number of users in the list
    int usersCount = GetUsersIDCount(UsersRightsList);

    GetUserAndRights(UsersRightsList ,userID,rightsPerUser);
    for (int i = 0; i<usersCount;i++)
    {
        printf("%S\n",userID[i]);
        printf("%S\n",rightsPerUser[i]);

    }

    return 0;
}
Posted in Programming, Technology and tagged , , , , , , , , , , , , , . Bookmark the permalink. RSS feed for this post. Leave a trackback.

Leave a Reply

Your email address will not be published. Required fields are marked *

Time limit is exhausted. Please reload CAPTCHA.

Swedish Greys - a WordPress theme from Nordic Themepark.