Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixed compile-time errors for test.c and example.c, Fixed buffer overflow, Added new function #12

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
57 changes: 29 additions & 28 deletions samples/src/example.c
Original file line number Diff line number Diff line change
Expand Up @@ -51,55 +51,55 @@ struct data {

/* Add a time object property in a JSON string.
"name":{"temp":-5,"hum":48}, */
char* json_weather( char* dest, char const* name, struct weather const* weather ) {
dest = json_objOpen( dest, name ); // --> "name":{\0
dest = json_int( dest, "temp", weather->temp ); // --> "name":{"temp":22,\0
dest = json_int( dest, "hum", weather->hum ); // --> "name":{"temp":22,"hum":45,\0
dest = json_objClose( dest ); // --> "name":{"temp":22,"hum":45},\0
char* json_weather( char* dest, char const* name, struct weather const* weather, size_t* remLen ) {
dest = json_objOpen( dest, name, remLen ); // --> "name":{\0
dest = json_int( dest, "temp", weather->temp, remLen ); // --> "name":{"temp":22,\0
dest = json_int( dest, "hum", weather->hum, remLen ); // --> "name":{"temp":22,"hum":45,\0
dest = json_objClose( dest, remLen ); // --> "name":{"temp":22,"hum":45},\0
return dest;
}

/* Add a time object property in a JSON string.
"name":{"hour":18,"minute":32}, */
char* json_time( char* dest, char const* name, struct time const* time ) {
dest = json_objOpen( dest, name );
dest = json_int( dest, "hour", time->hour );
dest = json_int( dest, "minute", time->minute );
dest = json_objClose( dest );
char* json_time( char* dest, char const* name, struct time const* time, size_t* remLen ) {
dest = json_objOpen( dest, name, remLen );
dest = json_int( dest, "hour", time->hour, remLen );
dest = json_int( dest, "minute", time->minute, remLen );
dest = json_objClose( dest, remLen );
return dest;
}

/* Add a measure object property in a JSON string.
"name":{"weather":{"temp":-5,"hum":48},"time":{"hour":18,"minute":32}}, */
char* json_measure( char* dest, char const* name, struct measure const* measure ) {
dest = json_objOpen( dest, name );
dest = json_weather( dest, "weather", &measure->weather );
dest = json_time( dest, "time", &measure->time );
dest = json_objClose( dest );
char* json_measure( char* dest, char const* name, struct measure const* measure, size_t* remLen ) {
dest = json_objOpen( dest, name, remLen );
dest = json_weather( dest, "weather", &measure->weather, remLen );
dest = json_time( dest, "time", &measure->time, remLen );
dest = json_objClose( dest, remLen );
return dest;
}

/* Add a data object property in a JSON string. */
char* json_data( char* dest, char const* name, struct data const* data ) {
dest = json_objOpen( dest, NULL );
dest = json_str( dest, "city", data->city );
dest = json_str( dest, "street", data->street );
dest = json_measure( dest, "measure", &data->measure );
dest = json_arrOpen( dest, "samples" );
char* json_data( char* dest, char const* name, struct data const* data, size_t* remLen ) {
dest = json_objOpen( dest, NULL, remLen );
dest = json_str( dest, "city", data->city, remLen );
dest = json_str( dest, "street", data->street, remLen );
dest = json_measure( dest, "measure", &data->measure, remLen );
dest = json_arrOpen( dest, "samples", remLen );
for( int i = 0; i < 4; ++i )
dest = json_int( dest, NULL, data->samples[i] );
dest = json_arrClose( dest );
dest = json_objClose( dest );
dest = json_int( dest, NULL, data->samples[i], remLen );
dest = json_arrClose( dest, remLen );
dest = json_objClose( dest, remLen );
return dest;
}

/** Convert a data structure to a root JSON object.
* @param dest Destination memory block.
* @param data Source data structure.
* @return The JSON string length. */
int data_to_json( char* dest, struct data const* data ) {
char* p = json_data( dest, NULL, data );
p = json_end( p );
int data_to_json( char* dest, struct data const* data, size_t* remLen ) {
char* p = json_data( dest, NULL, data, remLen );
p = json_end( p, remLen );
return p - dest;
}

Expand Down Expand Up @@ -149,7 +149,8 @@ int main(int argc, char** argv) {
}
};
char buff[512];
int len = data_to_json( buff, &data );
size_t remLen = sizeof(buff);
int len = data_to_json( buff, &data, &remLen );
if( len >= sizeof buff ) {
fprintf( stderr, "%s%d%s%d\n", "Error. Len: ", len, " Max: ", (int)sizeof buff - 1 );
return EXIT_FAILURE;
Expand Down
8 changes: 8 additions & 0 deletions src/include/json-maker/json-maker.h
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,14 @@ char* json_ulong( char* dest, char const* name, unsigned long int value, size_t*
* @return Pointer to the new end of JSON under construction. */
char* json_verylong( char* dest, char const* name, long long int value, size_t* remLen );

/** Add a unsigned long long integer property in a JSON string.
* @param dest Pointer to the end of JSON under construction.
* @param name Pointer to null-terminated string or null for unnamed.
* @param value Value of the property.
* @param remLen Pointer to remaining length of dest
* @return Pointer to the new end of JSON under construction. */
char* json_uverylong( char* dest, char const* name, unsigned long long int value, size_t* remLen );

/** Add a double precision number property in a JSON string.
* @param dest Pointer to the end of JSON under construction.
* @param name Pointer to null-terminated string or null for unnamed.
Expand Down
110 changes: 66 additions & 44 deletions src/json-maker.c
Original file line number Diff line number Diff line change
Expand Up @@ -32,11 +32,16 @@
* @param remLen Pointer to remaining length of dest
* @return Pointer to the null character of the destination string. */
static char* chtoa( char* dest, char ch, size_t* remLen ) {
if (*remLen != 0) {
if (0 != *remLen) {
--*remLen;
*dest = ch;
*++dest = '\0';
*dest++ = ch;
}
if (0 == *remLen)
{
++*remLen;
--dest;
}
*dest = '\0';
return dest;
}

Expand All @@ -48,6 +53,11 @@ static char* chtoa( char* dest, char ch, size_t* remLen ) {
static char* atoa( char* dest, char const* src, size_t* remLen ) {
for( ; *src != '\0' && *remLen != 0; ++dest, ++src, --*remLen )
*dest = *src;
if (0 == *remLen)
{
++*remLen;
--dest;
}
*dest = '\0';
return dest;
}
Expand Down Expand Up @@ -222,16 +232,25 @@ char* json_end( char* dest, size_t* remLen ) {
if ( ',' == dest[-1] ) {
dest[-1] = '\0';
--dest;
*dest = '\0';
++*remLen;
}
return dest;
}

#ifdef NO_SPRINTF

static char* format( char* dest, int len, int isnegative ) {
if ( isnegative )
static char* format( char* dest, int len, int isnegative, size_t *remLen ) {
if ( isnegative && 0 != *remLen )
{
dest[ len++ ] = '-';
--*remLen;
}
if (0 == *remLen)
{
--len;
++*remLen;
}
dest[ len ] = '\0';
int head = 0;
int tail = len - 1;
Expand All @@ -245,36 +264,38 @@ static char* format( char* dest, int len, int isnegative ) {
return dest + len;
}

#define numtoa( func, type, utype ) \
static char* func( char* dest, type val ) { \
enum { base = 10 }; \
if ( 0 == val ) \
return chtoa( dest, '0' ); \
int const isnegative = 0 > val; \
utype num = isnegative ? -val : val; \
int len = 0; \
while( 0 != num ) { \
int rem = num % base; \
dest[ len++ ] = rem + '0'; \
num = num / base; \
} \
return format( dest, len, isnegative ); \
} \

#define json_num( func, func2, type ) \
char* func( char* dest, char const* name, type value ) { \
dest = primitivename( dest, name ); \
dest = func2( dest, value ); \
dest = chtoa( dest, ',' ); \
return dest; \
} \
#define numtoa( func, type, utype ) \
static char* func( char* dest, type val, size_t *remLen ) { \
enum { base = 10 }; \
if ( 0 == val ) \
return chtoa( dest, '0', remLen ); \
int const isnegative = 0 > val; \
utype num = isnegative ? -val : val; \
int len = 0; \
while( 0 != num && *remLen != 0 ) { \
int rem = num % base; \
dest[ len++ ] = rem + '0'; \
--*remLen; \
num = num / base; \
} \
return format( dest, len, isnegative, remLen ); \
}

#define json_num( func, func2, type ) \
char* func( char* dest, char const* name, type value, size_t *remLen ) { \
dest = primitivename( dest, name, remLen ); \
dest = func2( dest, value, remLen ); \
dest = chtoa( dest, ',', remLen ); \
return dest; \
}

#define ALL_TYPES \
X( int, int, unsigned int ) \
X( long, long, unsigned long ) \
X( uint, unsigned int, unsigned int ) \
X( ulong, unsigned long, unsigned long ) \
X( verylong, long long, unsigned long long ) \
X( int, int, unsigned int ) \
X( long, long, unsigned long ) \
X( uint, unsigned int, unsigned int ) \
X( ulong, unsigned long, unsigned long ) \
X( verylong, long long, unsigned long long ) \
X( uverylong, unsigned long long, unsigned long long )

#define X( name, type, utype ) numtoa( name##toa, type, utype )
ALL_TYPES
Expand All @@ -284,30 +305,31 @@ ALL_TYPES
ALL_TYPES
#undef X

char* json_double( char* dest, char const* name, double value ) {
return json_verylong( dest, name, value );
char* json_double( char* dest, char const* name, double value, size_t* remLen ) {
return json_verylong( dest, name, value, remLen );
}

#else

#include <stdio.h>

#define ALL_TYPES \
X( json_int, int, "%d" ) \
X( json_long, long, "%ld" ) \
X( json_uint, unsigned int, "%u" ) \
X( json_ulong, unsigned long, "%lu" ) \
X( json_verylong, long long, "%lld" ) \
X( json_double, double, "%g" ) \
#define ALL_TYPES \
X( json_int, int, "%d" ) \
X( json_long, long, "%ld" ) \
X( json_uint, unsigned int, "%u" ) \
X( json_ulong, unsigned long, "%lu" ) \
X( json_verylong, long long, "%lld" ) \
X( json_uverylong, unsigned long long, "%llu" ) \
X( json_double, double, "%g" )


#define json_num( funcname, type, fmt ) \
#define json_num( funcname, type, fmt ) \
char* funcname( char* dest, char const* name, type value, size_t* remLen ) { \
int digitLen; \
dest = primitivename( dest, name, remLen ); \
digitLen = snprintf( dest, *remLen, fmt, value ); \
if(digitLen >= (int)*remLen+1){ \
digitLen = (int)*remLen;} \
digitLen = (int)*remLen;} \
*remLen -= (size_t)digitLen; \
dest += digitLen; \
dest = chtoa( dest, ',', remLen ); \
Expand Down
Loading