Skip to content

Commit

Permalink
Swap bool_ enum for stdbool.h (#72)
Browse files Browse the repository at this point in the history
  • Loading branch information
dan4thewin authored Dec 7, 2020
1 parent 9b6b87e commit cb7b177
Show file tree
Hide file tree
Showing 22 changed files with 214 additions and 177 deletions.
14 changes: 14 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -146,3 +146,17 @@ jobs:
run: |
git-secrets --register-aws
git-secrets --scan
custom-standard-c-headers:
runs-on: ubuntu-latest
steps:
- name: Clone This Repo
uses: actions/checkout@v2
- name: Build
run: |
mkdir -p override-include
cp source/include/stdbool.readme override-include/stdbool.h
cmake -S test -B build/ \
-G "Unix Makefiles" \
-DBUILD_CLONE_SUBMODULES=ON \
-DCMAKE_C_FLAGS='-Wall -Wextra -I../override-include'
make -C build/ coverity_analysis
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,8 @@ A search may descend through nested objects when the `queryKey` contains matchin

A compiler that supports **C90 or later** such as *gcc* is required to build the library.

The library uses a header file introduced in ISO C99, `stdbool.h`. For compilers that do not provide this header file, the [source/include](source/include) directory contains [stdbool.readme](source/include/stdbool.readme), which can be renamed to `stdbool.h`.

For instance, if the example above is copied to a file named `example.c`, *gcc* can be used like so:
```bash
gcc -I source/include example.c source/core_json.c -o example
Expand Down
191 changes: 93 additions & 98 deletions source/core_json.c
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,6 @@
#include "core_json.h"

/** @cond DO_NOT_DOCUMENT */
typedef enum
{
true = 1,
false = 0
} bool_;

/* A compromise to satisfy both MISRA and CBMC */
typedef union
Expand Down Expand Up @@ -124,10 +119,10 @@ static size_t countHighBits( uint8_t c )
*
* @note Disallow ASCII, as this is called only for multibyte sequences.
*/
static bool_ shortestUTF8( size_t length,
uint32_t value )
static bool shortestUTF8( size_t length,
uint32_t value )
{
bool_ ret = false;
bool ret = false;
uint32_t min, max;

assert( ( length >= 2U ) && ( length <= 4U ) );
Expand Down Expand Up @@ -182,11 +177,11 @@ static bool_ shortestUTF8( size_t length,
* would introduce a non-shortest sequence, and F5 or above would
* introduce a value greater than the last code point, 0x10FFFF.
*/
static bool_ skipUTF8MultiByte( const char * buf,
size_t * start,
size_t max )
static bool skipUTF8MultiByte( const char * buf,
size_t * start,
size_t max )
{
bool_ ret = false;
bool ret = false;
size_t i, bitCount, j;
uint32_t value = 0;
char_ c;
Expand Down Expand Up @@ -246,11 +241,11 @@ static bool_ skipUTF8MultiByte( const char * buf,
* @return true if a valid code point was present;
* false otherwise.
*/
static bool_ skipUTF8( const char * buf,
size_t * start,
size_t max )
static bool skipUTF8( const char * buf,
size_t * start,
size_t max )
{
bool_ ret = false;
bool ret = false;

assert( ( buf != NULL ) && ( start != NULL ) && ( max > 0U ) );

Expand Down Expand Up @@ -321,12 +316,12 @@ static uint8_t hexToInt( char c )
*
* @note For the sake of security, \u0000 is disallowed.
*/
static bool_ skipOneHexEscape( const char * buf,
size_t * start,
size_t max,
uint16_t * outValue )
static bool skipOneHexEscape( const char * buf,
size_t * start,
size_t max,
uint16_t * outValue )
{
bool_ ret = false;
bool ret = false;
size_t i, end;
uint16_t value = 0;

Expand Down Expand Up @@ -382,11 +377,11 @@ static bool_ skipOneHexEscape( const char * buf,
#define isHighSurrogate( x ) ( ( ( x ) >= 0xD800U ) && ( ( x ) <= 0xDBFFU ) )
#define isLowSurrogate( x ) ( ( ( x ) >= 0xDC00U ) && ( ( x ) <= 0xDFFFU ) )

static bool_ skipHexEscape( const char * buf,
size_t * start,
size_t max )
static bool skipHexEscape( const char * buf,
size_t * start,
size_t max )
{
bool_ ret = false;
bool ret = false;
size_t i;
uint16_t value;

Expand Down Expand Up @@ -434,11 +429,11 @@ static bool_ skipHexEscape( const char * buf,
*
* @note For the sake of security, \NUL is disallowed.
*/
static bool_ skipEscape( const char * buf,
size_t * start,
size_t max )
static bool skipEscape( const char * buf,
size_t * start,
size_t max )
{
bool_ ret = false;
bool ret = false;
size_t i;

assert( ( buf != NULL ) && ( start != NULL ) && ( max > 0U ) );
Expand Down Expand Up @@ -501,11 +496,11 @@ static bool_ skipEscape( const char * buf,
* @return true if a valid string was present;
* false otherwise.
*/
static bool_ skipString( const char * buf,
size_t * start,
size_t max )
static bool skipString( const char * buf,
size_t * start,
size_t max )
{
bool_ ret = false;
bool ret = false;
size_t i;

assert( ( buf != NULL ) && ( start != NULL ) && ( max > 0U ) );
Expand Down Expand Up @@ -566,9 +561,9 @@ static bool_ skipString( const char * buf,
* @return true if the sequences are the same;
* false otherwise
*/
static bool_ strnEq( const char * a,
const char * b,
size_t n )
static bool strnEq( const char * a,
const char * b,
size_t n )
{
size_t i;

Expand Down Expand Up @@ -597,13 +592,13 @@ static bool_ strnEq( const char * a,
* @return true if the literal was present;
* false otherwise.
*/
static bool_ skipLiteral( const char * buf,
size_t * start,
size_t max,
const char * literal,
size_t length )
static bool skipLiteral( const char * buf,
size_t * start,
size_t max,
const char * literal,
size_t length )
{
bool_ ret = false;
bool ret = false;

assert( ( buf != NULL ) && ( start != NULL ) && ( max > 0U ) );
assert( literal != NULL );
Expand Down Expand Up @@ -631,14 +626,14 @@ static bool_ skipLiteral( const char * buf,
* @return true if a valid literal was present;
* false otherwise.
*/
static bool_ skipAnyLiteral( const char * buf,
size_t * start,
size_t max )
static bool skipAnyLiteral( const char * buf,
size_t * start,
size_t max )
{
bool_ ret = false;
bool ret = false;

#define skipLit_( x ) \
( skipLiteral( buf, start, max, ( x ), ( sizeof( x ) - 1U ) ) == true )
( skipLiteral( buf, start, max, ( x ), ( sizeof( x ) - 1UL ) ) == true )

if( skipLit_( "true" ) || skipLit_( "false" ) || skipLit_( "null" ) )
{
Expand All @@ -664,12 +659,12 @@ static bool_ skipAnyLiteral( const char * buf,
* false otherwise.
*/
#define MAX_FACTOR ( MAX_INDEX_VALUE / 10 )
static bool_ skipDigits( const char * buf,
size_t * start,
size_t max,
int32_t * outValue )
static bool skipDigits( const char * buf,
size_t * start,
size_t max,
int32_t * outValue )
{
bool_ ret = false;
bool ret = false;
size_t i, saveStart;
int32_t value = 0;

Expand Down Expand Up @@ -784,11 +779,11 @@ static void skipExponent( const char * buf,
* @return true if a valid number was present;
* false otherwise.
*/
static bool_ skipNumber( const char * buf,
size_t * start,
size_t max )
static bool skipNumber( const char * buf,
size_t * start,
size_t max )
{
bool_ ret = false;
bool ret = false;
size_t i;

assert( ( buf != NULL ) && ( start != NULL ) && ( max > 0U ) );
Expand Down Expand Up @@ -840,11 +835,11 @@ static bool_ skipNumber( const char * buf,
* @return true if a scalar value was present;
* false otherwise.
*/
static bool_ skipAnyScalar( const char * buf,
size_t * start,
size_t max )
static bool skipAnyScalar( const char * buf,
size_t * start,
size_t max )
{
bool_ ret = false;
bool ret = false;

if( ( skipString( buf, start, max ) == true ) ||
( skipAnyLiteral( buf, start, max ) == true ) ||
Expand All @@ -870,11 +865,11 @@ static bool_ skipAnyScalar( const char * buf,
* @return true if a non-terminal comma was present;
* false otherwise.
*/
static bool_ skipSpaceAndComma( const char * buf,
size_t * start,
size_t max )
static bool skipSpaceAndComma( const char * buf,
size_t * start,
size_t max )
{
bool_ ret = false;
bool ret = false;
size_t i;

assert( ( buf != NULL ) && ( start != NULL ) && ( max > 0U ) );
Expand Down Expand Up @@ -952,7 +947,7 @@ static void skipObjectScalars( const char * buf,
size_t max )
{
size_t i;
bool_ comma;
bool comma;

assert( ( buf != NULL ) && ( start != NULL ) && ( max > 0U ) );

Expand Down Expand Up @@ -1183,13 +1178,13 @@ JSONStatus_t JSON_Validate( const char * buf,
* @return true if a value was present;
* false otherwise.
*/
static bool_ nextValue( const char * buf,
size_t * start,
size_t max,
size_t * value,
size_t * valueLength )
static bool nextValue( const char * buf,
size_t * start,
size_t max,
size_t * value,
size_t * valueLength )
{
bool_ ret = true;
bool ret = true;
size_t i, valueStart;

assert( ( buf != NULL ) && ( start != NULL ) && ( max > 0U ) );
Expand Down Expand Up @@ -1234,15 +1229,15 @@ static bool_ nextValue( const char * buf,
* @return true if a key-value pair was present;
* false otherwise.
*/
static bool_ nextKeyValuePair( const char * buf,
size_t * start,
size_t max,
size_t * key,
size_t * keyLength,
size_t * value,
size_t * valueLength )
static bool nextKeyValuePair( const char * buf,
size_t * start,
size_t max,
size_t * key,
size_t * keyLength,
size_t * value,
size_t * valueLength )
{
bool_ ret = true;
bool ret = true;
size_t i, keyStart;

assert( ( buf != NULL ) && ( start != NULL ) && ( max > 0U ) );
Expand Down Expand Up @@ -1307,14 +1302,14 @@ static bool_ nextKeyValuePair( const char * buf,
*
* @note Parsing stops upon finding a match.
*/
static bool_ objectSearch( const char * buf,
size_t max,
const char * query,
size_t queryLength,
size_t * outValue,
size_t * outValueLength )
static bool objectSearch( const char * buf,
size_t max,
const char * query,
size_t queryLength,
size_t * outValue,
size_t * outValueLength )
{
bool_ ret = false;
bool ret = false;

size_t i = 0, key, keyLength, value = 0, valueLength = 0;

Expand Down Expand Up @@ -1375,13 +1370,13 @@ static bool_ objectSearch( const char * buf,
*
* @note Parsing stops upon finding a match.
*/
static bool_ arraySearch( const char * buf,
size_t max,
uint32_t queryIndex,
size_t * outValue,
size_t * outValueLength )
static bool arraySearch( const char * buf,
size_t max,
uint32_t queryIndex,
size_t * outValue,
size_t * outValueLength )
{
bool_ ret = false;
bool ret = false;
size_t i = 0, value = 0, valueLength = 0;
uint32_t currentIndex = 0;

Expand Down Expand Up @@ -1442,12 +1437,12 @@ static bool_ arraySearch( const char * buf,
*/
#define JSON_QUERY_KEY_SEPARATOR '.'
#define isSeparator_( x ) ( ( x ) == JSON_QUERY_KEY_SEPARATOR )
static bool_ skipQueryPart( const char * buf,
size_t * start,
size_t max,
size_t * outLength )
static bool skipQueryPart( const char * buf,
size_t * start,
size_t max,
size_t * outLength )
{
bool_ ret = false;
bool ret = false;
size_t i;

assert( ( buf != NULL ) && ( start != NULL ) && ( outLength != NULL ) );
Expand Down Expand Up @@ -1505,7 +1500,7 @@ static JSONStatus_t multiSearch( const char * buf,

while( i < queryLength )
{
bool_ found = false;
bool found = false;

if( isSquareOpen_( query[ i ] ) )
{
Expand Down
1 change: 1 addition & 0 deletions source/include/core_json.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
#ifndef CORE_JSON_H_
#define CORE_JSON_H_

#include <stdbool.h>
#include <stddef.h>

/**
Expand Down
Loading

0 comments on commit cb7b177

Please sign in to comment.