Пример #1
0
stack_char_ptr_t *stack_ptr_push(stack_char_ptr_t *stack, char *ptr) {
    if (NULL != stack) {
        stack->count++;
        stack->pointers = (char **) memory_realloc(stack->pointers, stack->count * sizeof(char *));
        stack->pointers[stack->count - 1] = ptr;
    }

    return stack;
}
Пример #2
0
void *
_memory_push(Memory *buffer, memi count, memi capacity)
{
    memi c = buffer->count + count;
    if (c > buffer->capacity) {
        assert(capacity >= count);
        memory_realloc(buffer, buffer->capacity + capacity);
    }

    void *ptr = buffer->ptr + buffer->count;
    buffer->count = c;
    return ptr;
}
Пример #3
0
static int
trace_realloc (void *buf)
{
  uintptr_t ptr = buffer_get(uintptr_t);
  size_t size = buffer_get(size_t);
  uintptr_t caller = buffer_get(uintptr_t);
  uintptr_t result = buffer_get(uintptr_t);
  unsigned long long time = buffer_get(unsigned long long);

  int res = memory_realloc(ptr, size, caller, result, time);
  assert_inner(!res, "memory_realloc");

  return 0;
}
Пример #4
0
/*******************************************************************************
 * Brief - This is the main function in which various functions are tested using 
 * switch cases ,user is given with menu where every function is tested and by 
 * calling memory_information each details are shown to the user. 
 ******************************************************************************/
int main()
{	
    int menuChoice = 0 , subMenuChoice = 0, size = 0 , totalElements = 0;
    char choice ='y';
    int **pointer;
    int	index , number = 0, *arrayPointer[MAX_SIZE], numFree = 0, numRealloc, random, *previousAddress, num;
    pointer = arrayPointer;
    do
    {
    __fpurge(stdin);
    system("clear");
    printf("\n\n\t\tMENU\n\t1. Use memory_alloc or Use memory_calloc\n\t2. Use memory_free\n\t3. Use memory_realloc");
    printf("\n\t4. memory_information\n\t5. free everything");
    printf("\n\nEnter your menuChoice :");
    scanf("%d" , &menuChoice);
    switch(menuChoice)
    {
	case 1:	
	    system("clear");
	    printf("\n\n\t\tSUB-MENU\n\t1. Use memory_alloc \n\t2. Use memory_calloc\n\nEnter your menuChoice :");
	    scanf("%d" , &subMenuChoice);
            switch(subMenuChoice)
            {
                case 1:
            	    system("clear");
	            printf("How many memory_alloc calls u want :");
	    	    scanf("%d",&num);
            	    printf("Size\tAddress\n-----------------\n");
            	    for(index = number ; index < number + num ; index++)
            	    {
 			       size = rand() % MALLOC_MAX_SIZE;
 	        	*(pointer + index) = memory_alloc(size);
            		printf("%d\t%p\n",size,*(pointer +index));
	    	    }
                    printf("\nTotal memory_alloc calls :%d\n",num);
                    number = number + num;
		break;
		case 2:
            	    system("clear");
	            printf("How many memory_calloc calls u want :");
	    	    scanf("%d",&num);
            	    printf("Size\tElements\tAdress\n-----------------------------------\n");
	    	    for(index = number ; index < number+num ;index++)
	    	    {
	        	size = rand() % CALLOC_MAX_SIZE;
	    		totalElements = rand() % CALLOC_MAX_ELEMENTS ;
 	    		*(pointer + index) = memory_calloc(totalElements , size);
            		printf("%d\t%d\t\t%p\n",size,totalElements,*(pointer + index));
            	    }
                    printf("\nTotal memory_calloc calls :%d\n",num);
                    number = number + num;
		break;
                default:
		    system("clear");
		    printf("\nWrong Choice !!!");
                break;
	    }	
	break;

  	case 2:
	    system("clear");
            do
            {
	        printf("Out of %d allocated memories how many you want to free :",number);
 		scanf("%d",&numFree);
	    }while(numFree > number || numFree <= 0);
	    printf("Number\tAddress\n-----------------\n");
	    for(index = number-1 ; index >= (number - numFree) ;index--)
	    {
		printf("%d\t%p\n",index,*(pointer +index));
                if(NULL != *(pointer +index))
	            memory_free(*(pointer +index));
	    }
            printf("\nTotal memory_free calls :%d\n",numFree);
	    number=number-numFree;
	break;

	case 3:
	    system("clear");
	    do
            {
	        printf("Out of %d allocated memories how many you want to reallocate :",number);
 		scanf("%d",&numRealloc);
	    }while(numRealloc > number || numRealloc <= 0);
            printf("Previous_Address\tNew_SIZE\tNew_Adress\n----------------------------------------------------\n");
            for(index = number-1 ; index >= (number - numRealloc) ;index--)
            {
                size=rand() % REALLOC_MAX_SIZE;
		previousAddress = *(pointer + index);
 	        *(pointer + index)=memory_realloc(*(pointer + index),size);
            	printf("%p\t\t%d\t\t%p\n",previousAddress,size,*(pointer + index));
            }
            printf("\nTotal memory_realloc calls :%d\n",numRealloc);
	break;
	case 4:
            system("clear");
	    block_information();
	break;
	case 5:
            system("clear");
	    for(index = 0 ; index < number ; index++)
    	    {
                if(NULL != *(pointer +index))
 		    memory_free(*(pointer + index));
    	    }
            number = 0;
        break;
        default:
            system("clear");
	    printf("\nWrong Choice !!!!!!");
 	break;
    }
    printf("\nPress y to continue :");
    scanf(" %c",&choice);
    }while(choice == 'y' || choice == 'Y');
    return 0;
}
Пример #5
0
int http_read(const char *hostName, const void *requestString, int requestLength, int port, int readTimeOut,
		int contentOnly, void **resultBuffer, int *resultBufferLength)
{
	int responseStatusCode = 0;
	SOCKET_DESCRIPTOR socketDescriptor;
    struct hostent *host;
    struct sockaddr_in serverAddr;

	char *buffer = NULL;
	int bytes_read = 0;
	int count = 0;
	int readCount = 0;
	int res;

	char *r_start, *r_end;

#ifdef WIN32
	WSADATA wsaData;
    WSAStartup(MAKEWORD(2, 2), &wsaData);
#endif

    socketDescriptor = socket(AF_INET, SOCK_STREAM, 0);
	if (socketDescriptor == INVALID_SOCKET)
	{
#ifdef WIN32
		WSACleanup();
#endif
		return HTTP_SOCKET_ERROR;
	}

	host = (struct hostent *) gethostbyname(hostName);

    if (host == NULL)
    {
		res = HTTP_NOLIVEINTERNET_ERROR;
		goto exit_sopened;
    }

    memset(&serverAddr, 0, sizeof(serverAddr));
    serverAddr.sin_family = (short)host->h_addrtype;
	serverAddr.sin_port = htons((unsigned short)port);
    serverAddr.sin_addr.s_addr = *(unsigned long*)host->h_addr;

    if (connect(socketDescriptor, (struct sockaddr *)&serverAddr, sizeof(serverAddr)) < 0)
    {
		res = HTTP_CONNECT_ERROR;
		goto exit_sopened;
    }

    /* Send request */

    if (safe_send(socketDescriptor, requestString, requestLength, 0) != requestLength)
    {
		res = HTTP_SEND_ERROR;
		goto exit_sopened;
	}

	buffer = (char*)memory_alloc(0);
	count = 0;
	for(;;)
	{
		fd_set rfds;
		struct timeval tv;
		int selectResult;

		tv.tv_sec = readTimeOut;
		tv.tv_usec = 0;

		FD_ZERO(&rfds);
		FD_SET(socketDescriptor, &rfds);

		selectResult = select((int)socketDescriptor + 1, &rfds, NULL, NULL, &tv);
		if ((selectResult == 0) || (selectResult == -1))
		{
			res = HTTP_READTIMEOUT_ERROR;
			goto exit_sopened;
		}

		count++;
		buffer = (char*)memory_realloc((void*)buffer, HTTP_READ_BUFFER_SIZE * count);

		if (!buffer)
		{
			res = HTTP_RECEIVE_ERROR;
			goto exit_sopened;
		}

#ifdef WIN32
		readCount = recv(socketDescriptor, buffer + bytes_read, HTTP_READ_BUFFER_SIZE, 0);
#else
		readCount = read(socketDescriptor, buffer + bytes_read, HTTP_READ_BUFFER_SIZE);
#endif

		if (readCount == 0)
		{
			buffer[bytes_read] = '\0';
			break;
		}
		if (readCount < 0)
		{
			res = HTTP_RECEIVE_ERROR;
			goto exit_sopened;
		}
		bytes_read += readCount;

	}

	close_socket(socketDescriptor);

	if (buffer)
	{
		if (sscanf(buffer, "%*s %d",  &responseStatusCode) != 1)
		{
			res = HTTP_RECEIVE_ERROR;
			goto exit_buffree;
		}
	}

	switch (responseStatusCode)
	{
		case 200:
		{
			/* Status is OK */
			break;
		}
		case 301:
		{
			/* Permanent redirection, extract new location */
			if (buffer)
			{
				r_start = strstr(buffer, HTTP_Header_Location);
				if (r_start)
				{
					r_start += strlen(HTTP_Header_Location);
					r_end = strstr(r_start, "\r\n");
					if (r_end && r_end > r_start)
					{
						*resultBufferLength = r_end - r_start;
						*resultBuffer = memory_alloc(*resultBufferLength + 1);
						if (!*resultBuffer)
						{
							res = HTTP_RECEIVE_ERROR;
							goto exit_buffree;
						}
						memcpy(*resultBuffer, r_start, *resultBufferLength);
						/* Put null terminating character */
						((char*)*resultBuffer)[*resultBufferLength] = '\0';
						*resultBufferLength += 1;
						res = HTTP_REDIRECTION;
						goto exit_buffree;
					}
				}
			}
			break;
		}
		default:
		{
			/* No HTTP return code */
			res = HTTP_RESPONSESTATUS_ERROR;
			goto exit_buffree;
		}
	}

	if (contentOnly == 0)
	{
		*resultBuffer = buffer;
		*resultBufferLength = bytes_read;
	}
	else
	{
		int headerLength;
		char* temp = strstr(buffer, "\r\n\r\n");
		headerLength = (int)(temp - buffer + 4);
		res = HTTP_CONTENT_ERROR;

		if (!temp) goto exit_buffree;

		*resultBuffer = memory_alloc(bytes_read - headerLength);

		if (!*resultBuffer) goto exit_buffree;

		*resultBuffer = memcpy(*resultBuffer, temp + 4, bytes_read - headerLength);
		*resultBufferLength = bytes_read - headerLength;

		res = HTTP_NOERROR;
		goto exit_buffree;
	}

	res = HTTP_NOERROR;
	goto exit;

exit_sopened:
	close_socket(socketDescriptor);
exit_buffree:
	if (buffer)
	{
		memory_free(buffer);
	}
exit:
	return res;
}
Пример #6
0
void* EPLIB_realloc(void* ptr, size_t new_size)
{
    return memory_realloc(ptr, new_size);
}
UNSIGNED32 get_timestamp_response(const char* urlStr, char* hash, UNSIGNED32 hash_size, UNSIGNED32 httpTimeOut, TS_RESP** tsResponse)
{
	UNSIGNED32 result = TINTERNALERROR;
	BIO* responseBio = NULL;
	Url* url = NULL;
	TS_REQ* tsRequest = NULL;
	BIO* requestBio = NULL;
	int requestHeaderLength;
	int requestLength;
	int requestContentLength;
	char requestHeader[2048 + 256];
	char* request = NULL;
	void* contentBuffer = NULL;
	void* resultBuffer = NULL;
	int resultLength;
	TS_MSG_IMPRINT* msgImprint = NULL;
	ASN1_OCTET_STRING* hashedMessage = NULL;
	int hashedMessageLength;
	int httpResult;
	char *urlBuffer = NULL;
	int redirection = 0;

	/* Check if TS url is specified */
	if (!urlStr)
	{
		goto end;
	}

	/* Get Request for timestamp */
	tsRequest = get_timestamp_request(hash, hash_size, create_nonce(NONCE_LENGTH));
	msgImprint = TS_REQ_get_msg_imprint(tsRequest);
	hashedMessage = TS_MSG_IMPRINT_get_msg(msgImprint);
	hashedMessageLength = ASN1_STRING_length((ASN1_STRING*)hashedMessage);
	if ((int)hash_size != hashedMessageLength)
	{
		goto end;
	}

	requestBio = BIO_new(BIO_s_mem());
	if (requestBio == NULL)
	{
		goto end;
	}

	if (!i2d_TS_REQ_bio(requestBio, tsRequest))
	{
		goto end;
	}

	contentBuffer = memory_alloc(BIO_number_written(requestBio));
	if (contentBuffer == NULL)
	{
		goto end;
	}

    requestContentLength = BIO_read(requestBio, contentBuffer, BIO_number_written(requestBio));

    /* Allocate memory buffer for timestamp server url */
    urlBuffer = memory_alloc(strlen(urlStr) + 1);
    if (!urlBuffer)
    {
    	goto end;
    }
    /* Copy TS url to allocated buffer */
    strcpy(urlBuffer, urlStr);

http_redirect:

	/* Parse and check URL */
	url = parse_url(urlBuffer);
	if (url == NULL)
	{
		goto end;
	}
	if (strcmp(url->Scheme, "http") != 0)
	{
		goto end;
	}

    requestHeaderLength = sprintf(requestHeader, "POST %s HTTP/1.0\r\nHOST: %s\r\nPragma: no-cache\r\nContent-Type: application/timestamp-query\r\nAccept: application/timestamp-reply\r\nContent-Length: %d\r\n\r\n",
    		urlBuffer, url->Host, requestContentLength);

	requestLength = requestHeaderLength + requestContentLength;

	request = (char*)memory_alloc(requestLength);
	if (request == NULL)
	{
		goto end;
	}

	memcpy(request, requestHeader, requestHeaderLength);
	memcpy(request + requestHeaderLength, contentBuffer, requestContentLength);

	httpResult = http_read(url->Host, request, requestLength, url->Port, httpTimeOut, 1, &resultBuffer, &resultLength);
	if (httpResult == HTTP_REDIRECTION && (resultBuffer) && !redirection)
	{
		free_url(url);
		url = NULL;
		memory_free(request);
		request = NULL;
		/* Allocated buffer for redirected url */
	    urlBuffer = memory_realloc(urlBuffer, resultLength);
	    if (!urlBuffer)
	    {
	    	goto end;
	    }
	    memcpy(urlBuffer, resultBuffer, resultLength);
	    memory_free(resultBuffer);
	    redirection++;
		goto http_redirect;
	} else
	if ((httpResult == HTTP_NOERROR) && (resultBuffer))
	{
		responseBio = BIO_new(BIO_s_mem());
		if (responseBio == NULL)
		{
			goto end;
		}
		BIO_write(responseBio, resultBuffer, resultLength);

		*tsResponse = d2i_TS_RESP_bio(responseBio, NULL);
		if (*tsResponse == NULL)
		{
			goto end;
		}

		result = TNOERR;
	}
	else
	{
		switch (httpResult)
		{
			case HTTP_NOLIVEINTERNET_ERROR:
				result = TNONET;
				break;
			case HTTP_TIMEOUT_ERROR:
				result = TTIMEOUT;
				break;
			case HTTP_RESPONSESTATUS_ERROR:
				result = TSERVERERROR;
				break;
			default:
				result = TINTERNALERROR;
				break;
		}
	}

end:
	free_url(url);
	if (tsRequest != NULL)
	{
		TS_REQ_free(tsRequest);
	}
	if (requestBio != NULL)
	{
		BIO_free_all(requestBio);
	}
	if (responseBio != NULL)
	{
		BIO_free_all(responseBio);
	}
	if (request != NULL)
	{
		memory_free(request);
	}
	if (contentBuffer != NULL)
	{
		memory_free(contentBuffer);
	}
	if (resultBuffer != NULL)
	{
		memory_free(resultBuffer);
	}
	if (urlBuffer != NULL)
	{
		memory_free(urlBuffer);
	}

	return result;
}
Пример #8
0
bool dino_strmap_put(str_map_t *map, const char *key, const char *value) {
    if (NULL == map || NULL == key || NULL == value) {
        return false;
    }

    size_t key_len = strlen(key);
    size_t value_len = strlen(value);
    // Get a pointer to the bucket the key string hashes to
    //
    size_t index = hash(key) % map->count;
    bucket_t *bucket = &(map->buckets[index]);

    // Check if we can handle insertion by simply replacing
    // an existing value in a key-value pair in the bucket.
    //
    pair_t *pair = NULL;
    if (NULL != (pair = get_pair(bucket, key))) {
        // The bucket contains a pair that matches the provided key,
        //change the value for that pair to the new value.
        //
        if (strlen(pair->value) < value_len) {
            // If the new value is larger than the old value, re-allocate
            // space for the new larger value.
            //
            char *tmp_value = memory_realloc(pair->value, (value_len + 1) * sizeof(char));
            if (NULL == tmp_value) {
                return false;
            }
            pair->value = tmp_value;
        }
        /* Copy the new value into the pair that matches the key */
        strcpy(pair->value, value);
        return true;
    }

    // Allocate space for a new key and value
    //
    char *new_key = memory_alloc((key_len + 1) * sizeof(char));
    if (NULL == new_key) {
        return false;
    }

    char *new_value = memory_alloc((value_len + 1) * sizeof(char));
    if (NULL == new_value) {
        memory_free(new_key);
        return false;
    }

    // Create a key-value pair
    //
    if (bucket->count == 0) {
        // The bucket is empty, lazily allocate space for a single
        // key-value pair.
        //
        bucket->pairs = memory_alloc(sizeof(pair_t));
        if (NULL == bucket->pairs) {
            memory_free(new_key);
            memory_free(new_value);
            return false;
        }
        bucket->count = 1;
    }
    else {
        // The bucket wasn't empty but no pair existed that matches the provided
        // key, so create a new key-value pair.
        //
        pair_t *tmp_pairs = memory_realloc(bucket->pairs, (bucket->count + 1) * sizeof(pair_t));
        if (NULL == tmp_pairs) {
            memory_free(new_key);
            memory_free(new_value);
            return false;
        }
        bucket->pairs = tmp_pairs;
        bucket->count++;
    }
    // Get the last pair in the chain for the bucket
    //
    pair = &(bucket->pairs[bucket->count - 1]);
    pair->key = new_key;
    pair->value = new_value;

    // Copy the key and its value into the key-value pair
    //
    strcpy(pair->key, key);
    strcpy(pair->value, value);
    return true;
}