Ejemplo n.º 1
0
	LinearAllocator::LinearAllocator(char *p_Buffer, UINT p_Size)
		: m_Buffer(p_Buffer), m_Size(p_Size), m_Original(false), m_Marker(0)
	{
		if (m_Size == 0)
			throw MemoryException("LinearAllocator, size was 0", __LINE__, __FILE__);
		if (m_Buffer == nullptr)
			throw MemoryException("LinearAllocator, buffer parameter was nullptr", __LINE__, __FILE__);
	}
Ejemplo n.º 2
0
	LinearAllocator::LinearAllocator(UINT p_Size)
		: m_Size(p_Size), m_Original(true), m_Marker(0)
	{
		if (m_Size == 0)
			throw MemoryException("LinearAllocator, size was 0", __LINE__, __FILE__);

		m_Buffer = (char*)malloc(m_Size + sizeof(void*));
		if (m_Buffer == nullptr)
			throw MemoryException("LinearAllocator, failed to allocate memory", __LINE__, __FILE__);
	}
Ejemplo n.º 3
0
static PyObject *tripledescbc_new(tripledescbc *self, PyObject *args) {
	tripledescbc *newself = NULL;
	try {
		byte *key;
		int keylength;
		if(!PyArg_ParseTuple(args, "s#", &key, &keylength)) {
			throw Exception(Exception::INVALID_ARGUMENT, "wrong type of parameters passed in from Python");
		}
		if(keylength != 24) {
			throw Exception(Exception::INVALID_ARGUMENT, "triple DES key length must be 24");
		}
		if(!(newself = PyObject_New(tripledescbc, &tripledescbc_type))) {
		  throw MemoryException();
		}
		byte *keycopy = new byte[24];
		memcpy(keycopy, key, 24);
		//		printf("in tripledescbc_new(); keycopy: %p, key: %p\n", keycopy, key);
		newself->key = keycopy;
		return (PyObject *)newself;
	}
	catch(CryptoPP::Exception &e) {
		tripledescbc_delete(newself);
		PyErr_SetString(TripleDESCBCError, e.what());
		return NULL;
	}
	catch(MemoryException &e)
	{
		tripledescbc_delete(newself);
		PyErr_SetString(PyExc_MemoryError, "Can't allocate memory to do set up keys for en/decryption");
		return NULL;
	}
}
Ejemplo n.º 4
0
static PyObject *tripledescbc_decrypt(tripledescbc *self, PyObject *args) {
	//	std::cout << "hello dec 0" << std::endl; std::cout.flush();
	PyObject *result = NULL;
	byte *plaintext = NULL;
	try {
		byte *iv;
		unsigned int ivlength;
		byte *text;
		unsigned int textlength;
		if(!PyArg_ParseTuple(args, "s#s#", &iv, &ivlength, &text, &textlength)) {
			throw Exception(Exception::INVALID_ARGUMENT, "wrong type of parameters passed in from Python");
		}
		if(ivlength != 8) {
			throw Exception(Exception::INVALID_ARGUMENT, "IV length must be 8");
		}
		CBC_CTS_Mode<DES_XEX3>::Decryption decryption(self->key, 24, iv);
		plaintext = new byte[textlength];
		if (plaintext == NULL) {
			throw MemoryException();
		}
		StreamTransformationFilter decryptor(decryption, new ArraySink(plaintext, textlength));
		decryptor.PutMessageEnd(text, textlength);
		result = Py_BuildValue("s#", plaintext, textlength);
		if(result == NULL) {
			throw MemoryException();
		}
		xdeletear(plaintext);
		return result;
	}
	catch(CryptoPP::Exception &e) {
		if(result != NULL) {
			PyMem_DEL(result);
		}
		xdeletear(plaintext);
		PyErr_SetString(TripleDESCBCError, e.what());
		return NULL;
	}
	catch(MemoryException &e) {
		if(result != NULL) {
			PyMem_DEL(result);
		}
		xdeletear(plaintext);
		PyErr_SetString(PyExc_MemoryError, "Can't allocate memory to do decryption");
		return NULL;
	}
}
Ejemplo n.º 5
0
 void MasterMemoryManager::registerSPtr(PtrBase *p) {
   if(_nStatics == 100) {
     throw MemoryException("Too many static pointers registered.");
   }
   
   _statics[_nStatics] = p;
   _nStatics++;
 }
Ejemplo n.º 6
0
void OverlayBatch::addOverlay(const AABB<2, float>& bounds, const AABB<2, float>& imageRegion, const Vector<4, float>& color)
{
	if (isFull())
	{
		throw MemoryException("Overlay batch exceeded capacity.");
	}

	float vertices[4][9];

	vertices[0][0] = bounds.getMax().x;
	vertices[0][1] = bounds.getMin().y;
	vertices[0][2] = 0.0f;
	vertices[0][3] = imageRegion.getMax().x;
	vertices[0][4] = imageRegion.getMax().y;
	vertices[0][5] = color.r;
	vertices[0][6] = color.g;
	vertices[0][7] = color.b;
	vertices[0][8] = color.a;

	vertices[1][0] = bounds.getMin().x;
	vertices[1][1] = bounds.getMin().y;
	vertices[1][2] = 0.0f;
	vertices[1][3] = imageRegion.getMin().x;
	vertices[1][4] = imageRegion.getMax().y;
	vertices[1][5] = color.r;
	vertices[1][6] = color.g;
	vertices[1][7] = color.b;
	vertices[1][8] = color.a;

	vertices[2][0] = bounds.getMin().x;
	vertices[2][1] = bounds.getMax().y;
	vertices[2][2] = 0.0f;
	vertices[2][3] = imageRegion.getMin().x;
	vertices[2][4] = imageRegion.getMin().y;
	vertices[2][5] = color.r;
	vertices[2][6] = color.g;
	vertices[2][7] = color.b;
	vertices[2][8] = color.a;

	vertices[3][0] = bounds.getMax().x;
	vertices[3][1] = bounds.getMax().y;
	vertices[3][2] = 0.0f;
	vertices[3][3] = imageRegion.getMax().x;
	vertices[3][4] = imageRegion.getMin().y;
	vertices[3][5] = color.r;
	vertices[3][6] = color.g;
	vertices[3][7] = color.b;
	vertices[3][8] = color.a;

	vertexBuffer->setData(vertices, overlayCount * 4, 4);	

	++overlayCount;
}
Ejemplo n.º 7
0
		void OldUDPSocket::send(const void *buf, int size){
			int r;
			r = ::sendto(tsock, buf, size, flags, (struct ::sockaddr *)&ipv4addr, sizeof(ipv4addr));
			if(r == -1){
				switch(errno){
					case ECONNRESET:
						throw ConnectionResetByPeer();
					case ENOMEM:
						throw MemoryException();
					default:
						throw NetworkException();
				}
			}
		}
Ejemplo n.º 8
0
void ProcessAddToVector::PushToStack(PolyObject *obj)
{
    if (asp == stackSize)
    {
        if (addStack == 0)
        {
            addStack = (PolyObject**)malloc(sizeof(PolyObject*) * 100);
            if (addStack == 0) throw MemoryException();
            stackSize = 100;
        }
        else
        {
            unsigned newSize = stackSize+100;
            PolyObject** newStack = (PolyObject**)realloc(addStack, sizeof(PolyObject*) * newSize);
            if (newStack == 0) throw MemoryException();
            stackSize = newSize;
            addStack = newStack;
        }
    }

    ASSERT(asp < stackSize);

    addStack[asp++] = obj;
}
Ejemplo n.º 9
0
DepthVector *ShareDataClass::AddDepth(POLYUNSIGNED depth)
{
    if (depth >= depthVectorSize) {
        POLYUNSIGNED newDepth = depth+1;
        DepthVector *newVec = (DepthVector *)realloc(depthVectors, sizeof(DepthVector)*newDepth);
        if (newVec == 0) throw MemoryException();
        depthVectors = newVec;
        for (POLYUNSIGNED d = depthVectorSize; d < newDepth; d++) {
            DepthVector *dv = &depthVectors[d];
            dv->depth = d;
            dv->nitems = dv->vsize = 0;
            dv->vector = 0;
        }
        depthVectorSize = newDepth;
    }
    return &depthVectors[depth];
}
Ejemplo n.º 10
0
// Add an object to a depth vector
void ShareDataClass::AddToVector(POLYUNSIGNED depth, POLYUNSIGNED L, PolyObject *pt)
{
    DepthVector *v = AddDepth (depth);

    ASSERT (v->nitems <= v->vsize);

    if (v->nitems == v->vsize)
    {
        // The vector is full or has not yet been allocated.  Grow it by 50%.
        POLYUNSIGNED new_vsize  = v->vsize + v->vsize / 2 + 1;
        if (new_vsize < 15)
            new_vsize = 15;

        Item *new_vector = (Item *)realloc (v->vector, new_vsize*sizeof(Item));

        if (new_vector == 0)
        {
            // The vectors can get large and we may not be able to grow them
            // particularly if the address space is limited in 32-bit mode.
            // Try again with just a small increase.
            new_vsize = v->vsize + 15;
            new_vector = (Item *)realloc (v->vector, new_vsize*sizeof(Item));
            // If that failed give up.
            if (new_vector == 0)
                throw MemoryException();
        }

        v->vector = new_vector;
        v->vsize  = new_vsize;
    }

    ASSERT (v->nitems < v->vsize);

    v->vector[v->nitems].L  = L;
    v->vector[v->nitems].pt = pt;

    v->nitems++;

    ASSERT (v->nitems <= v->vsize);
}
Ejemplo n.º 11
0
// Decodes a BER BIT STRING from the given buffer and stores
// the value in this object.
void AsnBits::BDecContent (const AsnBuf &b, AsnTag tagId, AsnLen elmtLen, AsnLen &bytesDecoded)
{
   FUNC("AsnBits::BDecContent");

	if (elmtLen == INDEFINITE_LEN || elmtLen > b.length())
	{
	   throw MemoryException(elmtLen, "elmtLen requests for too much data", STACK_ENTRY);
	}

   /*
    * tagId is encoded tag shifted into long int.
    * if CONS bit is set then constructed bit string
    */
   if (tagId & 0x20000000)
     BDecConsBits (b, elmtLen, bytesDecoded);

   else /* primitive octet string */
   {
     if (elmtLen == INDEFINITE_LEN)
        throw BoundsException("indefinite length on primitive", STACK_ENTRY);

	 if (elmtLen > b.length() || elmtLen <= 0)
        throw BoundsException("length problem on decoding content", STACK_ENTRY);

     bytesDecoded += elmtLen;
     elmtLen--;

	 unsigned int iUnusedBitLen= (unsigned int)b.GetByte();
	 if (iUnusedBitLen > 7)
        throw BoundsException("Length problem - Unused bits > 7", STACK_ENTRY);

     bitLen = (elmtLen * 8) - iUnusedBitLen;
     bits =  new unsigned char[elmtLen];
     b.GetUSeg (bits, elmtLen);
   }

} /* AsnBits::BDecContent */
Ejemplo n.º 12
0
Texture* FreeImageImageCodec::load(const RawDataContainer& data, Texture* result)
{
    int len = (int)data.getSize();
    FIMEMORY *mem = 0;
    FIBITMAP *img = 0;
    Texture *retval = 0;

    try
    {
        mem = FreeImage_OpenMemory(static_cast<BYTE*>(const_cast<std::uint8_t*>(data.getDataPtr())), len);
        if (mem == 0)
            throw MemoryException("Unable to open memory stream, FreeImage_OpenMemory failed");

        FREE_IMAGE_FORMAT fif = FreeImage_GetFileTypeFromMemory(mem, len);

        if (fif == FIF_UNKNOWN) // it may be that it's TARGA or MNG
        {
            fif = FIF_TARGA;
            img = FreeImage_LoadFromMemory(fif, mem, 0);

            if (img == 0)
            {
                fif = FIF_MNG;
                img = FreeImage_LoadFromMemory(fif, mem, 0);
            }
        }
        else
            img = FreeImage_LoadFromMemory(fif, mem, 0);

        if (img == 0)
            throw GenericException("Unable to load image, FreeImage_LoadFromMemory failed");

        FIBITMAP *newImg = FreeImage_ConvertTo32Bits(img);
        if (newImg == 0)
            throw GenericException("Unable to convert image, FreeImage_ConvertTo32Bits failed");
        FreeImage_Unload(img);
        img = newImg;
        newImg = 0;

        // FreeImage pixel format for little-endian architecture (which CEGUI
        // supports) is like BGRA. We need to convert that to RGBA.
        //
        // It is now:
        // RED_MASK		0x00FF0000
        // GREEN_MASK	0x0000FF00
        // BLUE_MASK	0x000000FF
        // ALPHA_MASK	0xFF000000
        //
        // It should be:
        // RED_MASK		0x000000FF
        // GREEN_MASK	0x0000FF00
        // BLUE_MASK	0x00FF0000
        // ALPHA_MASK	0xFF000000

        unsigned int pitch = FreeImage_GetPitch(img);
        unsigned int height = FreeImage_GetHeight(img);
        unsigned int width = FreeImage_GetWidth(img);
        std::uint8_t *rawBuf = new std::uint8_t[width * height << 2];

        // convert the bitmap to raw bits (top-left pixel first)
        FreeImage_ConvertToRawBits(rawBuf, img, pitch, 32,
                                   FI_RGBA_RED_MASK, FI_RGBA_GREEN_MASK, FI_RGBA_BLUE_MASK, true);

        // We need to convert pixel format a little
        // NB: little endian only - I think(!)
#if FREEIMAGE_COLORORDER == FREEIMAGE_COLORORDER_BGR
        for (unsigned int i = 0; i < height; ++i)
        {
            for (unsigned int j = 0; j < width; ++j)
            {
                unsigned int p = *(((unsigned int*)(rawBuf + i * pitch)) + j);
                unsigned int r = (p >> 16) & 0x000000FF;
                unsigned int b = (p << 16) & 0x00FF0000;
                p &= 0xFF00FF00;
                p |= r | b;
                // write the adjusted pixel back
                *(((unsigned int*)(rawBuf + i * pitch)) + j) = p;
            }
        }
#endif
        FreeImage_Unload(img);
        img = 0;

        result->loadFromMemory(rawBuf, Sizef(width, height), Texture::PF_RGBA);
        delete [] rawBuf;
        retval = result;
    }
    catch (Exception&)
    {
    }

    if (img != 0) FreeImage_Unload(img);
    if (mem != 0) FreeImage_CloseMemory(mem);

    return retval;
}
Ejemplo n.º 13
0
static PyObject *tripledescbc_encrypt(tripledescbc *self, PyObject *args) {
	//	std::cout << "hello enc 0" << std::endl; std::cout.flush();
	PyObject *result = NULL;
	byte *ciphertext = NULL;
	try {
		byte *iv;
		unsigned int ivlength;
		byte *text;
		unsigned int textlength;
		if(!PyArg_ParseTuple(args, "s#s#", &iv, &ivlength, &text, &textlength)) {
			throw Exception(Exception::INVALID_ARGUMENT, "wrong type of parameters passed in from Python");
		}
		if(ivlength != 8) {
			throw Exception(Exception::INVALID_ARGUMENT, "IV length must be 8");
		}
		//		std::cout << "hello enc 0.7" << std::endl; std::cout.flush();
		//		std::cout << "hello enc 0.7.1, self: " << self << std::endl; std::cout.flush();
		//		std::cout << "hello enc 0.7.2, self->key: ";
		//		std::cout << self->key;
		//		std::cout << std::endl;
		//		std::cout.flush();
		//	std::cout << "hello enc 0.7.3, iv: " << iv << std::endl; std::cout.flush();
		CBC_CTS_Mode<DES_XEX3>::Encryption encryption(self->key, 24, iv);
		//		std::cout << "hello enc 0.8" << std::endl; std::cout.flush();
		ciphertext = new byte[textlength];
		//		std::cout << "hello enc 0.9" << std::endl; std::cout.flush();
		if (ciphertext == NULL) {
			throw MemoryException();
		}
		//		std::cout << "hello enc 1" << std::endl; std::cout.flush();
		StreamTransformationFilter encryptor(encryption, new ArraySink(ciphertext, textlength));
		//		std::cout << "hello enc 2" << std::endl; std::cout.flush();
		encryptor.PutMessageEnd(text, textlength);
		//		std::cout << "hello enc 3" << std::endl; std::cout.flush();
		result = Py_BuildValue("s#", ciphertext, textlength);
		//		std::cout << "hello enc 4" << std::endl; std::cout.flush();
		if(result == NULL) {
			throw MemoryException();
		}
		//		std::cout << "hello enc 5" << std::endl; std::cout.flush();
		xdeletear(ciphertext);
		//		std::cout << "hello enc 6" << std::endl; std::cout.flush();
		return result;
	}
	catch(CryptoPP::Exception &e) {
		if(result != NULL) {
			PyMem_DEL(result);
		}
		xdeletear(ciphertext);
		PyErr_SetString(TripleDESCBCError, e.what());
		return NULL;
	}
	catch(MemoryException &e) {
		if(result != NULL) {
			PyMem_DEL(result);
		}
		xdeletear(ciphertext);
		PyErr_SetString(PyExc_MemoryError, "Can't allocate memory to do encryption");
		return NULL;
	}
	//	std::cout << "goodbye enc" << std::endl; std::cout.flush();
}
Ejemplo n.º 14
0
void PExport::exportStore(void)
{
    unsigned i;
    time_t now;
    time(&now);

    // Calculate a first guess for the map size based on the space size
    totalBytes = 0;
    void *startAddr = 0;
    for (i = 0; i < memTableEntries; i++)
    {
        if (i != ioMemEntry)
        {
            totalBytes += (unsigned long)memTable[i].mtLength;
            // Get the lowest address.
            if (startAddr == 0 || memTable[i].mtAddr < startAddr)
                startAddr = memTable[i].mtAddr;
        }
    }
    // Create a map entry for each entry.  Allow five words per object.
    nMapSize = totalBytes/(sizeof(PolyWord)*5);
    pMap = (PolyObject **)malloc(sizeof(PolyObject*)*nMapSize);

    if (pMap == 0)
        throw MemoryException();

    // We want the entries in pMap to be in ascending
    // order of address to make searching easy so we need to process the areas
    // in order of increasing address, which may not be the order in memTable.
    indexOrder = (unsigned*)calloc(sizeof(unsigned), memTableEntries-1);
    if (indexOrder == 0)
        throw MemoryException();

    unsigned items = 0;
    for (i = 0; i < memTableEntries; i++)
    {
        if (i != ioMemEntry)
        {
            unsigned j = items;
            while (j > 0 && memTable[i].mtAddr < memTable[indexOrder[j-1]].mtAddr)
            {
                indexOrder[j] = indexOrder[j-1];
                j--;
            }
            indexOrder[j] = i;
            items++;
        }
    }
    ASSERT(items == memTableEntries-1);

    // Process the area in order of ascending address.
    for (i = 0; i < items; i++)
    {
        unsigned index = indexOrder[i];
        char *start = (char*)memTable[index].mtAddr;
        char *end = start + memTable[index].mtLength;
        for (PolyWord *p = (PolyWord*)start; p < (PolyWord*)end; )
        {
            p++;
            PolyObject *obj = (PolyObject*)p;
            if (nObjects == nMapSize)
            {
                // Need to expand the array.
                PolyObject **newMap =
                    (PolyObject **)realloc(pMap, (nMapSize + nMapSize/2)*sizeof(PolyObject*));
                if (newMap == 0)
                    throw MemoryException();
                pMap = newMap;

            }
            POLYUNSIGNED length = obj->Length();
            pMap[nObjects++] = obj;
            p += length;
        }
    }

    /* Start writing the information. */
    fprintf(exportFile, "Objects\t%lu\n", nObjects);
    fprintf(exportFile, "Root\t%lu\n", getIndex(rootFunction));


    // Generate each of the areas apart from the IO area.
    for (i = 0; i < memTableEntries; i++)
    {
        if (i != ioMemEntry) // Don't relocate the IO area
        {
            char *start = (char*)memTable[i].mtAddr;
            char *end = start + memTable[i].mtLength;
            for (PolyWord *p = (PolyWord*)start; p < (PolyWord*)end; )
            {
                p++;
                PolyObject *obj = (PolyObject*)p;
                POLYUNSIGNED length = obj->Length();
                printObject(obj);
                p += length;
            }
        }
    }

    fclose(exportFile); exportFile = NULL;
}