mirror of https://github.com/GOSTSec/sgminer
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
117 lines
2.6 KiB
117 lines
2.6 KiB
14 years ago
|
/*
|
||
11 years ago
|
* Copyright (c) 2009-2013 Petri Lehtinen <petri@digip.org>
|
||
14 years ago
|
*
|
||
|
* Jansson is free software; you can redistribute it and/or modify
|
||
|
* it under the terms of the MIT license. See LICENSE for details.
|
||
|
*/
|
||
|
|
||
11 years ago
|
#ifndef _GNU_SOURCE
|
||
14 years ago
|
#define _GNU_SOURCE
|
||
11 years ago
|
#endif
|
||
|
|
||
14 years ago
|
#include <stdlib.h>
|
||
|
#include <string.h>
|
||
14 years ago
|
#include "jansson_private.h"
|
||
14 years ago
|
#include "strbuffer.h"
|
||
|
|
||
|
#define STRBUFFER_MIN_SIZE 16
|
||
|
#define STRBUFFER_FACTOR 2
|
||
11 years ago
|
#define STRBUFFER_SIZE_MAX ((size_t)-1)
|
||
14 years ago
|
|
||
|
int strbuffer_init(strbuffer_t *strbuff)
|
||
|
{
|
||
|
strbuff->size = STRBUFFER_MIN_SIZE;
|
||
|
strbuff->length = 0;
|
||
|
|
||
14 years ago
|
strbuff->value = jsonp_malloc(strbuff->size);
|
||
14 years ago
|
if(!strbuff->value)
|
||
|
return -1;
|
||
|
|
||
|
/* initialize to empty */
|
||
|
strbuff->value[0] = '\0';
|
||
|
return 0;
|
||
|
}
|
||
|
|
||
|
void strbuffer_close(strbuffer_t *strbuff)
|
||
|
{
|
||
11 years ago
|
if(strbuff->value)
|
||
|
jsonp_free(strbuff->value);
|
||
|
|
||
14 years ago
|
strbuff->size = 0;
|
||
|
strbuff->length = 0;
|
||
|
strbuff->value = NULL;
|
||
|
}
|
||
|
|
||
|
void strbuffer_clear(strbuffer_t *strbuff)
|
||
|
{
|
||
|
strbuff->length = 0;
|
||
|
strbuff->value[0] = '\0';
|
||
|
}
|
||
|
|
||
|
const char *strbuffer_value(const strbuffer_t *strbuff)
|
||
|
{
|
||
|
return strbuff->value;
|
||
|
}
|
||
|
|
||
|
char *strbuffer_steal_value(strbuffer_t *strbuff)
|
||
|
{
|
||
|
char *result = strbuff->value;
|
||
11 years ago
|
strbuff->value = NULL;
|
||
14 years ago
|
return result;
|
||
|
}
|
||
|
|
||
|
int strbuffer_append(strbuffer_t *strbuff, const char *string)
|
||
|
{
|
||
|
return strbuffer_append_bytes(strbuff, string, strlen(string));
|
||
|
}
|
||
|
|
||
|
int strbuffer_append_byte(strbuffer_t *strbuff, char byte)
|
||
|
{
|
||
|
return strbuffer_append_bytes(strbuff, &byte, 1);
|
||
|
}
|
||
|
|
||
11 years ago
|
int strbuffer_append_bytes(strbuffer_t *strbuff, const char *data, size_t size)
|
||
14 years ago
|
{
|
||
11 years ago
|
if(size >= strbuff->size - strbuff->length)
|
||
14 years ago
|
{
|
||
14 years ago
|
size_t new_size;
|
||
|
char *new_value;
|
||
14 years ago
|
|
||
11 years ago
|
/* avoid integer overflow */
|
||
|
if (strbuff->size > STRBUFFER_SIZE_MAX / STRBUFFER_FACTOR
|
||
|
|| size > STRBUFFER_SIZE_MAX - 1
|
||
|
|| strbuff->length > STRBUFFER_SIZE_MAX - 1 - size)
|
||
|
return -1;
|
||
|
|
||
14 years ago
|
new_size = max(strbuff->size * STRBUFFER_FACTOR,
|
||
|
strbuff->length + size + 1);
|
||
|
|
||
|
new_value = jsonp_malloc(new_size);
|
||
|
if(!new_value)
|
||
14 years ago
|
return -1;
|
||
14 years ago
|
|
||
|
memcpy(new_value, strbuff->value, strbuff->length);
|
||
|
|
||
|
jsonp_free(strbuff->value);
|
||
|
strbuff->value = new_value;
|
||
|
strbuff->size = new_size;
|
||
14 years ago
|
}
|
||
|
|
||
|
memcpy(strbuff->value + strbuff->length, data, size);
|
||
|
strbuff->length += size;
|
||
|
strbuff->value[strbuff->length] = '\0';
|
||
|
|
||
|
return 0;
|
||
|
}
|
||
|
|
||
|
char strbuffer_pop(strbuffer_t *strbuff)
|
||
|
{
|
||
|
if(strbuff->length > 0) {
|
||
|
char c = strbuff->value[--strbuff->length];
|
||
|
strbuff->value[strbuff->length] = '\0';
|
||
|
return c;
|
||
|
}
|
||
|
else
|
||
|
return '\0';
|
||
|
}
|