<commit_before>/**
 * output.c -- output json to the standard output stream
 * Copyright (C) 2009-2014 by <NAME> <<EMAIL>>
 * GoAccess - An Ncurses apache weblog analyzer & interactive viewer
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License as
 * published by the Free Software Foundation; either version 2 of
 * the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * A copy of the GNU General Public License is attached to this
 * source distribution for its full text.
 *
 * Visit http://goaccess.prosoftcorp.com for new releases.
 */
#define _LARGEFILE_SOURCE
#define _LARGEFILE64_SOURCE
#define _FILE_OFFSET_BITS 64

#include <ctype.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/stat.h>
#include <time.h>
#include <unistd.h>
#include <inttypes.h>

#include "json.h"

#ifdef HAVE_LIBTOKYOCABINET
#include "tcabinet.h"
#else
#include "glibht.h"
#endif

#ifdef HAVE_LIBGEOIP
#include "geolocation.h"
#endif

#include "commons.h"
#include "error.h"
#include "gdns.h"
#include "settings.h"
#include "ui.h"
#include "util.h"
#include "xmalloc.h"

static void
escape_json_output (FILE * fp, char *s)
{
  while (*s) {
    switch (*s) {
     case '"':
       fprintf (fp, "\\\"");
       break;
     case '\\':
       fprintf (fp, "\\\\");
       break;
       /* unusual to be on a log */
     case '\b':
       fprintf (fp, "\\\b");
       break;
     case '\f':
       fprintf (fp, "\\\f");
       break;
     case '\n':
       fprintf (fp, "\\\n");
       break;
     case '\r':
       fprintf (fp, "\\\r");
       break;
     case '\t':
       fprintf (fp, "\\\t");
       break;
       /* TODO: escape four-hex-digits, \u */
     default:
       fputc (*s, fp);
       break;
    }
    s++;
  }
}<commit_msg>Escape JSON string correctly according to [rfc4627]<commit_after>/**
 * output.c -- output json to the standard output stream
 * Copyright (C) 2009-2014 by <NAME> <<EMAIL>>
 * GoAccess - An Ncurses apache weblog analyzer & interactive viewer
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License as
 * published by the Free Software Foundation; either version 2 of
 * the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * A copy of the GNU General Public License is attached to this
 * source distribution for its full text.
 *
 * Visit http://goaccess.prosoftcorp.com for new releases.
 */
#define _LARGEFILE_SOURCE
#define _LARGEFILE64_SOURCE
#define _FILE_OFFSET_BITS 64

#include <ctype.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/stat.h>
#include <time.h>
#include <unistd.h>
#include <inttypes.h>

#include "json.h"

#ifdef HAVE_LIBTOKYOCABINET
#include "tcabinet.h"
#else
#include "glibht.h"
#endif

#ifdef HAVE_LIBGEOIP
#include "geolocation.h"
#endif

#include "commons.h"
#include "error.h"
#include "gdns.h"
#include "settings.h"
#include "ui.h"
#include "util.h"
#include "xmalloc.h"

static void
escape_json_output (FILE * fp, char *s)
{
  while (*s) {
    switch (*s) {
     case '"':
       fprintf (fp, "\\\"");
       break;
     case '\\':
       fprintf (fp, "\\\\");
       break;
     case '\b':
       fprintf (fp, "\\\b");
       break;
     case '\f':
       fprintf (fp, "\\\f");
       break;
     case '\n':
       fprintf (fp, "\\\n");
       break;
     case '\r':
       fprintf (fp, "\\\r");
       break;
     case '\t':
       fprintf (fp, "\\\t");
       break;
     case '/':
       fprintf (fp, "\\/");
       break;
     default:
       if ((uint8_t)*s <= 0x1f) {
         //CONTROL CHARACTERS (U+0000 through U+001F)
         char buf[8];
         snprintf(buf, sizeof buf, "\\u%04x", *s);
         fprintf (fp, "%s", buf);
       } else if ((uint8_t)*s == 0xe2 && (uint8_t)*(s + 1) == 0x80 &&
                  (uint8_t)*(s + 2) == 0xa8) {
         //LINE SEPARATOR (U+2028)
         fprintf (fp, "\\u2028");
         s += 2;
       } else if ((uint8_t)*s == 0xe2 && (uint8_t)*(s + 1) == 0x80 &&
                  (uint8_t)*(s + 2) == 0xa9) {
         //PARAGRAPH SEPARATOR (U+2019)
         fprintf (fp, "\\u2029");
         s += 2;
       } else {
         fputc (*s, fp);
       }
       break;
    }
    s++;
  }
}
{'fraction_non_alphanumeric': 0.1462854757929883, 'fraction_numerical': 0.017737896494156927, 'mean_word_length': 2.485818181818182, 'pattern_counts': {'":': 0, '<': 26, '<?xml version=': 0, '>': 25, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7157303', 'n_tokens_mistral': 1685, 'n_tokens_neox': 1605, 'n_words': 540}