#define WH_LOAD_SUCCESS 0 #define WH_LOAD_FAILED -1 #define WH_PARSE_SUCCESS 0 #define WH_PARSE_FAILED -1 int parse_webhooks( webhooks_data_t *whdt, size_t wh_file_len ){ if ( whdt->webhooks_config == NULL || (uint64_t)strlen(whdt->webhooks_config) <= 0 ) return WH_PARSE_FAILED; if ( wh_file_len <= 0 ) return WH_PARSE_FAILED; whdt->webhooks_parsed = cJSON_ParseWithLength( whdt->webhooks_config, wh_file_len ); if ( whdt->webhooks_parsed == NULL ) return WH_PARSE_FAILED; return WH_PARSE_SUCCESS; } int load_webhooks( char *wh_file_name, webhooks_data_t *wdt ){ if ( wh_file_name == NULL || (uint64_t)strlen(wh_file_name) <= 0 ) return WH_LOAD_FAILED; FILE *whconf = fopen( wh_file_name, "r" ); if ( whconf == NULL ) return WH_LOAD_FAILED; if ( fseek(whconf, 0, SEEK_END) == -1 ){ fclose( whconf ); return WH_LOAD_FAILED; } int64_t wh_file_size = (int64_t)ftell( whconf ); if ( wh_file_size <= 0 ){ wh_file_size = 0; fclose( whconf ); return WH_LOAD_FAILED; } rewind( whconf ); wdt->webhooks_config = (char*)calloc( (size_t)wh_file_size, sizeof(char) ); if ( wdt->webhooks_config == NULL ){ wh_file_size = 0; fclose( whconf ); return WH_LOAD_FAILED; } if ( (int64_t)fread(wdt->webhooks_config, sizeof(char), (size_t)(wh_file_size * sizeof(char)), whconf) == (int64_t)-1 ){ wh_file_size = 0; free( wdt->webhooks_config ); fclose( whconf ); return WH_LOAD_FAILED; } fclose( whconf ); if ( parse_webhooks(wdt, wh_file_size) != WH_PARSE_SUCCESS ){ free( wdt->webhooks_config ); wh_file_size = 0; return WH_LOAD_FAILED; } wh_file_size = 0; free( wdt->webhooks_config ); return WH_LOAD_SUCCESS; }