Exemple #1
0
/*============= main ================*/
int main(int argc, char *argv[]) 
{

	// Allocate  buffer to bs bytes then increase by bs bytes.
	// Allocate a to 1 int then increase to 2 int.

	struct BUFFER buffer = {0,NULL,256};

	bufferAllocate(  &buffer);
	bufferAllocate(  &buffer);

	int *a = NULL; // Set ptr to NULL so realloc initially acts like malloc on empty block. 
	
	fAllocate(&a, 1); 
	a[0]=1;

	fAllocate(&a, 2);
	a[1]=2;	

	printf("a[0] = %i\n", a[0]);
	printf("a[1] = %i\n", a[1]);

	free(a);
	free(buffer.data);
	return 0;
}
Exemple #2
0
// Function stores Board's in memory,
void* boardStore(board_p board, int* size)
{
    void* buffer = NULL;
    state_p header = NULL;

    ASSERT(NULL != board, "boardStore");
    ASSERT(NULL != size, "boardStore");

    *size = board->prev ? sizeof(state_11_t)
                        : sizeof(state_10_t);

    buffer = bufferAllocate(*size, NULL);

    if (board->prev) {
        state_11_p state = (state_11_p)buffer;

        state->header.version = TAG_VERSION_11;

        boardCopy(&state->current, board);
        boardCopy(&state->previous, board->prev);
    } else {
        state_10_p state = (state_10_p)buffer;

        state->header.version = TAG_VERSION_10;

        boardCopy(&state->current, board);
    }

    header = (state_p)buffer;
    header->size = *size;
    header->crc = Crc16CalcBlock(
        header + 1, *size - sizeof(*header), 0);

    return buffer;
};
bool protocolInit(int pSocket, fd_set *pSocketSet, int pMaxSocket, int pBufferSize)
{
  UNUSED(pSocket);
  UNUSED(pSocketSet);
  UNUSED(pMaxSocket);
  return bufferAllocate(&gMessageBuffer, pBufferSize);
}
Exemple #4
0
bool rtspRequestSetContent(RTSPRequest *rtspRequest, uint8_t *content, size_t contentSize, char *contentType) {
	char contentSizeString[MAX_NUMBER_STRING_SIZE];

	/* Add header field for content type */
	if(!rtspRequestAddHeaderField(rtspRequest, "Content-Type", contentType)) {
		return false;
	}

	/* Add header field for content size */
	sprintf(contentSizeString, "%lu", (unsigned long)contentSize);
	if(!rtspRequestAddHeaderField(rtspRequest, "Content-Length", contentSizeString)) {
		return false;
	}

	/* Free any existing buffer */
	if(rtspRequest->contentBuffer != NULL) {
		if(!bufferFree(&rtspRequest->contentBuffer)) {
			return false;
		}
	}

	/* Allocate buffer */
	if(!bufferAllocate(&rtspRequest->contentBuffer, contentSize, "RTSP request content buffer")) {
		return false;
	}

	/* Copy buffer */
	memcpy(rtspRequest->contentBuffer, content, contentSize);
	rtspRequest->contentBufferSize = contentSize;

	return true;
}
Exemple #5
0
char* bufferAdd(Buffer* buffer, const char* string, unsigned long size) {

	if (size == 0) {
		return buffer->buffer;
	}

	bufferAllocate(buffer, size);

	char* returnAddress = buffer->buffer+buffer->currentsize;
	memcpy(buffer->buffer+buffer->currentsize, string, size);
	
	buffer->currentsize += size;

	return returnAddress;
}
Exemple #6
0
RTSPRequest *rtspRequestCreate(RTSPRequestMethod requestMethod) {
	RTSPRequest *rtspRequest;

	/* Create rtsp request structure */
	if(!bufferAllocate(&rtspRequest, sizeof(RTSPRequest), "RTSP request")) {
		return NULL;
	}

	/* Initialize structure */
	rtspRequest->requestMethod = requestMethod;
	rtspRequest->headerBuffer = NULL;
	rtspRequest->headerBufferSize = 0;
	rtspRequest->maxHeaderBufferSize = 0;
	rtspRequest->contentBuffer = NULL;
	rtspRequest->contentBufferSize = 0;

	return rtspRequest;
}
Exemple #7
0
// Function tries to load a Game
game_p gameLoad(engine_p engine)
{
    game_p game = NULL;
    UInt16 size = 0;
    int version = 0;

    version = PrefGetAppPreferences(
        CREATOR, SAVED_GAME, NULL, &size, 1);
    if (VERSION == version && 0 < size) {
        board_p board = NULL;

        void* data = bufferAllocate(size, NULL);

        PrefGetAppPreferences(CREATOR, SAVED_GAME,
                              data, &size, 1);

        // ignore if game can't be loaded
        TRY
        {
            board = boardRetain(
                boardLoad(data, size));
        }
        CATCH {}
        END;

        if (NULL == board)
            return NULL;
        else
            board = boardAutorelease(board);

        switch (boardState(board)) {
        case State_Victory:
        case State_Defeat:
            break;
        default:
            game = gameNew(engine, board);
            break;
        }
    }
Exemple #8
0
bool rtspRequestAddHeaderField(RTSPRequest *rtspRequest, const char *fieldName, const char *fieldValue) {
	size_t fieldNameLength;
	size_t fieldValueLength;
	int charsWritten;

	/* Allocate initial buffer if required */
	if(rtspRequest->headerBuffer == NULL) {
		rtspRequest->maxHeaderBufferSize = HEADER_BUFFER_INITIAL_SIZE;
		if(!bufferAllocate(&rtspRequest->headerBuffer, rtspRequest->maxHeaderBufferSize, "RTSP request header buffer")) {
			return false;
		}
		rtspRequest->headerBuffer[0] = '\0';
		rtspRequest->headerBufferSize = 1;	/* The '\0' byte */
	}

	/* Decide if enough space is available in buffer and add space if necessary. Add 4 bytes for ": " and "\r\n". */
	fieldNameLength = strlen(fieldName);
	fieldValueLength = strlen(fieldValue);
	if(!bufferMakeRoom(&rtspRequest->headerBuffer, &rtspRequest->maxHeaderBufferSize, rtspRequest->headerBufferSize, fieldNameLength + fieldValueLength + 4, HEADER_BUFFER_INCREMENT_SIZE)) {
		logWrite(LOG_LEVEL_ERROR, LOG_COMPONENT_NAME, "Cannot allocate memory to add field \"%s\" to RTSP Request header.", fieldName);
		bufferFree(&rtspRequest->headerBuffer);
		rtspRequest->headerBuffer = NULL;
		rtspRequest->headerBufferSize = 0;
		return false;
	}

	/* Add field name and value to buffer */
	/* Offet (headerBufferSize - 1) to overwrite existing '\0' byte. A new '\0' byte will be added at the end. */
	charsWritten = snprintf((char *)rtspRequest->headerBuffer + rtspRequest->headerBufferSize - 1, rtspRequest->maxHeaderBufferSize - (rtspRequest->headerBufferSize - 1), "%s: %s\r\n", fieldName, fieldValue);
	if(charsWritten != fieldNameLength + fieldValueLength + 4) {
		logWrite(LOG_LEVEL_ERROR, LOG_COMPONENT_NAME, "Cannot add field \"%s\" to RTSP Request header.", fieldName);
		return false;
	}

	/* Update buffer data */
	rtspRequest->headerBufferSize += charsWritten;

	return true;
}
Exemple #9
0
bool rtspRequestSend(RTSPRequest *rtspRequest, char *url, NetworkConnection *networkConnection) {
	uint8_t *requestBuffer;
	size_t maxRequestBufferSize;
	int charsWritten;

	/* Create buffer for full request (optimizer will get rid of all the individual constants) */
	maxRequestBufferSize = 12			/* "%s %s RTSP/1.0\r\n" printable characters */
		+ MAX_COMMAND_STRING_SIZE		/* command (first "%s" above) */
		+ MAX_URL_STRING_SIZE			/* url (second "%s" above) */
		+ rtspRequest->headerBufferSize - 1	/* header (excluding the terminating '\0') */
		+ 2					/* CR/LF */
		+ rtspRequest->contentBufferSize;	/* content */
	if(!bufferAllocate(&requestBuffer, maxRequestBufferSize, "RTSP request buffer")) {
		return false;
	}

	/* Write command */
	charsWritten = snprintf((char *)requestBuffer, maxRequestBufferSize, "%s %s RTSP/1.0\r\n", METHOD_NAMES[rtspRequest->requestMethod], rtspRequest->requestMethod == RTSP_METHOD_OPTIONS ? "*" : url);
	if(charsWritten < 0) {
		logWrite(LOG_LEVEL_ERROR, LOG_COMPONENT_NAME, "Cannot write command to request buffer.");
		bufferFree(&requestBuffer);
		return false;
	}

	/* Validate if amount of buffer is still enough (see explanation above for the following calculation) */
	if(charsWritten + rtspRequest->headerBufferSize - 1 + 2 + rtspRequest->contentBufferSize > maxRequestBufferSize) {
		logWrite(LOG_LEVEL_ERROR, LOG_COMPONENT_NAME, "Request buffer is not big enough to hold header and content.");
		bufferFree(&requestBuffer);
		return false;
	}

	/* Write header fields */
	if(rtspRequest->headerBuffer != NULL) {
		memcpy(requestBuffer + charsWritten, rtspRequest->headerBuffer, rtspRequest->headerBufferSize - 1);	/* No need for '\0' so -1 */
		charsWritten += rtspRequest->headerBufferSize - 1;
	}

	/* Write header/content separator (length validation done above) */
	requestBuffer[charsWritten] = '\r';
	charsWritten++;
	requestBuffer[charsWritten] = '\n';
	charsWritten++;

	/* Write content */
	if(rtspRequest->contentBuffer != NULL) {
		memcpy(requestBuffer + charsWritten, rtspRequest->contentBuffer, rtspRequest->contentBufferSize);
		charsWritten += rtspRequest->contentBufferSize;
	}

	/* Send out request */
	if(!networkSendMessage(networkConnection, requestBuffer, charsWritten)) {
		bufferFree(&requestBuffer);
		return false;
	}

	/* Write info from this message */
	logWrite(LOG_LEVEL_DEBUG, LOG_COMPONENT_NAME, "Sent out RTSP request:\n%.*s", charsWritten, requestBuffer);

	/* Free up resources */
	if(!bufferFree(&requestBuffer)) {
		return false;
	}

	return true;
}