Underc0de

Programación General => C / C++ => Códigos Fuentes => Mensaje iniciado por: Karcrack en Septiembre 18, 2013, 04:58:59 PM

Título: [SRC] andom_access_array - Evitar patrones de lectura/escritura
Publicado por: Karcrack en Septiembre 18, 2013, 04:58:59 PM
Código (cpp) [Seleccionar]
#ifndef __RA_ARRAY__
#define __RA_ARRAY__

#include <intrin.h>
#define ULONG unsigned long

template <typename T>
class random_access_array{
    const ULONG elements;
    const ULONG R;
    ULONG       a;
    ULONG       c;
    T*          buffer;

    const ULONG nextPowerOf2(ULONG n)const{
        if ((n & (n - 1)) == 0)
            return n;

        n |= (n >> 1);
        n |= (n >> 2);
        n |= (n >> 4);
        n |= (n >> 8);
        n |= (n >> 16);
        return n + 1;
    }

    ULONG LCG(ULONG X, ULONG a, ULONG c, ULONG R){
        return (a*X + c) % R;
    }

    ULONG randmax(ULONG max){
        return LCG((ULONG) __rdtsc(), 0x43FD43FDUL, 0xC39EC3UL, max);
    }

    size_t getRealIdx(int index){
        ULONG X = 0;
        while (index-- >= 0)
            while ((X = LCG(X, a, c, R)) >= elements){}

        return X;
    }

public:
    random_access_array(T* buffer, size_t elements) : elements(elements), buffer(buffer), R(nextPowerOf2(elements)){
        a = 2 * randmax(R / 2) + 1;
        c = 4 * randmax(R / 4) + 1;
    }

    random_access_array(T* buffer, size_t elements, ULONG a, ULONG c) : elements(elements), buffer(buffer), R(nextPowerOf2(elements)), a(a), c(c){}

    T& operator [](size_t i){
        return buffer[getRealIdx(i)];
    }

    T  operator [](const size_t i) const{
        return buffer[getRealIdx(i)];
    }
};

#endif//__RA_ARRAY__


Ejemplo de uso:
Código (cpp) [Seleccionar]
    #define SIZE 573

    random_access_array<int> ra(new int[SIZE], SIZE);

    for (int i = 0; i < SIZE; i++)
        ra[i] = i;

    for (int i = 0; i < SIZE; i++)
        cout << ra[i] << endl;


Utilidad real:
Código (cpp) [Seleccionar]
    #define SIZE sizeof(s) - 1

    char s[] = "!eru!akklccr !rza";

    random_access_array<char> ra(s, SIZE, 5, 25);

    for (int i = 0; i < SIZE; i++)
        cout << ra[i];


Saludos :)
Título: Re:[SRC] andom_access_array - Evitar patrones de lectura/escritura
Publicado por: [L]ord [R]NA en Septiembre 19, 2013, 10:45:01 AM
Pense que lo harias de manera mas inteligente... No validas la longitud del array dentro del template.
Título: Re:[SRC] andom_access_array - Evitar patrones de lectura/escritura
Publicado por: Karcrack en Septiembre 19, 2013, 02:46:07 PM
¿Como voy a validar el tamaño del array? Es el usuario quien me lo da.

Si el índice que se pasa es superior al tamaño no importa ya que si te fijas verás que getRealIdx() sólo devuelve un índice dentro del rango.
No habría jamás un buffer overflow.
Título: Re:[SRC] andom_access_array - Evitar patrones de lectura/escritura
Publicado por: [L]ord [R]NA en Septiembre 21, 2013, 10:24:14 AM
:P no habia visto la validación.