Merge pull request #97 from pascal-bach/master

Add const qualifiers to json_object_to_file and json_object_to_file_ext
diff --git a/config.h.in b/config.h.in
index b8b2d17..a912bb0 100644
--- a/config.h.in
+++ b/config.h.in
@@ -68,9 +68,6 @@
 /* Define to 1 if you have the `strncasecmp' function. */
 #undef HAVE_STRNCASECMP
 
-/* Define to 1 if you have the `strndup' function. */
-#undef HAVE_STRNDUP
-
 /* Define to 1 if you have the <syslog.h> header file. */
 #undef HAVE_SYSLOG_H
 
diff --git a/configure.in b/configure.in
index c5494a9..64f9cbe 100644
--- a/configure.in
+++ b/configure.in
@@ -35,7 +35,7 @@
 AC_FUNC_MEMCMP
 AC_FUNC_MALLOC
 AC_FUNC_REALLOC
-AC_CHECK_FUNCS(strcasecmp strdup strndup strerror snprintf vsnprintf vasprintf open vsyslog strncasecmp setlocale)
+AC_CHECK_FUNCS(strcasecmp strdup strerror snprintf vsnprintf vasprintf open vsyslog strncasecmp setlocale)
 
 #check if .section.gnu.warning accepts long strings (for __warn_references)
 AC_LANG_PUSH([C])
diff --git a/json_object.c b/json_object.c
index e12c440..1b3fc76 100644
--- a/json_object.c
+++ b/json_object.c
@@ -34,10 +34,6 @@
 # error You do not have strdup on your system.
 #endif /* HAVE_STRDUP */
 
-#if !defined(HAVE_STRNDUP)
-  char* strndup(const char* str, size_t n);
-#endif /* !HAVE_STRNDUP */
-
 #if !defined(HAVE_SNPRINTF) && defined(_MSC_VER)
   /* MSC has the version as _snprintf */
 # define snprintf _snprintf
@@ -575,10 +571,11 @@
      how to handle these cases as strings */
   if(isnan(jso->o.c_double))
     size = snprintf(buf, 128, "NaN");
-  else if(isinf(jso->o.c_double) == 1)
-    size = snprintf(buf, 128, "Infinity");
-  else if(isinf(jso->o.c_double) == -1)
-    size = snprintf(buf, 128, "-Infinity");
+  else if(isinf(jso->o.c_double))
+    if(jso->o.c_double > 0)
+      size = snprintf(buf, 128, "Infinity");
+    else
+      size = snprintf(buf, 128, "-Infinity");
   else
     size = snprintf(buf, 128, "%f", jso->o.c_double);
 
diff --git a/json_object.h b/json_object.h
index 6270309..df958bf 100644
--- a/json_object.h
+++ b/json_object.h
@@ -192,7 +192,7 @@
  * @param userdata an optional opaque cookie
  * @param user_delete an optional function from freeing userdata
  */
-void json_object_set_serializer(json_object *jso,
+extern void json_object_set_serializer(json_object *jso,
 	json_object_to_json_string_fn to_string_func,
 	void *userdata,
 	json_object_delete_fn *user_delete);
diff --git a/json_tokener.c b/json_tokener.c
index a6924a1..7e1fb68 100644
--- a/json_tokener.c
+++ b/json_tokener.c
@@ -74,7 +74,7 @@
 const char *json_tokener_error_desc(enum json_tokener_error jerr)
 {
 	int jerr_int = (int)jerr;
-	if (jerr_int < 0 || jerr_int > (int)sizeof(json_tokener_errors))
+	if (jerr_int < 0 || jerr_int > (int)(sizeof(json_tokener_errors) / sizeof(json_tokener_errors[0])))
 		return "Unknown error, invalid json_tokener_error value passed to json_tokener_error_desc()";
 	return json_tokener_errors[jerr];
 }
@@ -170,29 +170,6 @@
     return obj;
 }
 
-
-#if !HAVE_STRNDUP
-/* CAW: compliant version of strndup() */
-char* strndup(const char* str, size_t n)
-{
-  if(str) {
-    size_t len = strlen(str);
-    size_t nn = json_min(len,n);
-    char* s = (char*)malloc(sizeof(char) * (nn + 1));
-
-    if(s) {
-      memcpy(s, str, nn);
-      s[nn] = '\0';
-    }
-
-    return s;
-  }
-
-  return NULL;
-}
-#endif
-
-
 #define state  tok->stack[tok->depth].state
 #define saved_state  tok->stack[tok->depth].saved_state
 #define current tok->stack[tok->depth].current
@@ -265,7 +242,7 @@
 	if ((!ADVANCE_CHAR(str, tok)) || (!PEEK_CHAR(c, tok)))
 	  goto out;
       }
-      if(c == '/') {
+      if(c == '/' && !(tok->flags & JSON_TOKENER_STRICT)) {
 	printbuf_reset(tok->pb);
 	printbuf_memappend_fast(tok->pb, &c, 1);
 	state = json_tokener_state_comment_start;
@@ -293,8 +270,13 @@
 	printbuf_reset(tok->pb);
 	tok->st_pos = 0;
 	goto redo_char;
-      case '"':
       case '\'':
+        if (tok->flags & JSON_TOKENER_STRICT) {
+            /* in STRICT mode only double-quote are allowed */
+            tok->err = json_tokener_error_parse_unexpected;
+            goto out;
+        }
+      case '"':
 	state = json_tokener_state_string;
 	printbuf_reset(tok->pb);
 	tok->quote_char = c;
@@ -764,6 +746,13 @@
   } /* while(POP_CHAR) */
 
  out:
+  if (c &&
+     (state == json_tokener_state_finish) &&
+     (tok->depth == 0) &&
+     (tok->flags & JSON_TOKENER_STRICT)) {
+      /* unexpected char after JSON data */
+      tok->err = json_tokener_error_parse_unexpected;
+  }
   if (!c) { /* We hit an eof char (0) */
     if(state != json_tokener_state_finish &&
        saved_state != json_tokener_state_finish)