Index: lib/json.c
===================================================================
--- lib/json.c	(revision e76cf264de6caaca24fa308cab78e770fc4a6508)
+++ lib/json.c	(revision 7a80925eabe969ffc1e8e24c2ad31c80c33ab3cd)
@@ -1,6 +1,5 @@
-
-/* vim: set et ts=3 sw=3 ft=c:
+/* vim: set et ts=3 sw=3 sts=3 ft=c:
  *
- * Copyright (C) 2012 James McLaughlin et al.  All rights reserved.
+ * Copyright (C) 2012, 2013, 2014 James McLaughlin et al.  All rights reserved.
  * https://github.com/udp/json-parser
  *
@@ -29,4 +28,6 @@
  */
 
+#include <glib.h>
+
 #include "json.h"
 
@@ -43,9 +44,8 @@
 #endif
 
-#include <glib.h>
-#include <stdlib.h>
 #include <stdio.h>
 #include <string.h>
 #include <ctype.h>
+#include <math.h>
 
 typedef unsigned short json_uchar;
@@ -53,32 +53,42 @@
 static unsigned char hex_value (json_char c)
 {
-   if (c >= 'A' && c <= 'F')
-      return (c - 'A') + 10;
-
-   if (c >= 'a' && c <= 'f')
-      return (c - 'a') + 10;
-
-   if (c >= '0' && c <= '9')
+   if (isdigit(c))
       return c - '0';
 
-   return 0xFF;
+   switch (c) {
+      case 'a': case 'A': return 0x0A;
+      case 'b': case 'B': return 0x0B;
+      case 'c': case 'C': return 0x0C;
+      case 'd': case 'D': return 0x0D;
+      case 'e': case 'E': return 0x0E;
+      case 'f': case 'F': return 0x0F;
+      default: return 0xFF;
+   }
 }
 
 typedef struct
 {
+   unsigned long used_memory;
+
+   unsigned int uint_max;
+   unsigned long ulong_max;
+
    json_settings settings;
    int first_pass;
 
-   unsigned long used_memory;
-
-   unsigned int uint_max;
-   unsigned long ulong_max;
-
 } json_state;
+
+static void * default_alloc (size_t size, int zero, void * user_data)
+{
+   return zero ? calloc (1, size) : malloc (size);
+}
+
+static void default_free (void * ptr, void * user_data)
+{
+   free (ptr);
+}
 
 static void * json_alloc (json_state * state, unsigned long size, int zero)
 {
-   void * mem;
-
    if ((state->ulong_max - state->used_memory) < size)
       return 0;
@@ -90,8 +100,5 @@
    }
 
-   if (! (mem = zero ? calloc (size, 1) : malloc (size)))
-      return 0;
-
-   return mem;
+   return state->settings.mem_alloc (size, zero, state->settings.user_data);
 }
 
@@ -120,4 +127,5 @@
             }
 
+            value->u.array.length = 0;
             break;
 
@@ -134,4 +142,5 @@
             value->_reserved.object_mem = (*(char **) &value->u.object.values) + values_size;
 
+            value->u.object.length = 0;
             break;
 
@@ -144,4 +153,5 @@
             }
 
+            value->u.string.length = 0;
             break;
 
@@ -150,6 +160,4 @@
       };
 
-      value->u.array.length = 0;
-
       return 1;
    }
@@ -184,22 +192,55 @@
    do { if (!state.first_pass) string [string_length] = b;  ++ string_length; } while (0);
 
-const static int
-   flag_next = 1, flag_reproc = 2, flag_need_comma = 4, flag_seek_value = 8, flag_exponent = 16,
-   flag_got_exponent_sign = 32, flag_escaped = 64, flag_string = 128, flag_need_colon = 256,
-   flag_done = 512;
-
-json_value * json_parse_ex (json_settings * settings, const json_char * json, char * error_buf)
+static const long
+   flag_next             = 1 << 0,
+   flag_reproc           = 1 << 1,
+   flag_need_comma       = 1 << 2,
+   flag_seek_value       = 1 << 3, 
+   flag_escaped          = 1 << 4,
+   flag_string           = 1 << 5,
+   flag_need_colon       = 1 << 6,
+   flag_done             = 1 << 7,
+   flag_num_negative     = 1 << 8,
+   flag_num_zero         = 1 << 9,
+   flag_num_e            = 1 << 10,
+   flag_num_e_got_sign   = 1 << 11,
+   flag_num_e_negative   = 1 << 12,
+   flag_line_comment     = 1 << 13,
+   flag_block_comment    = 1 << 14;
+
+json_value * json_parse_ex (json_settings * settings,
+                            const json_char * json,
+                            size_t length,
+                            char * error_buf)
 {
-   json_char error [128];
+   json_char error [json_error_max];
    unsigned int cur_line;
-   const json_char * cur_line_begin, * i;
+   const json_char * cur_line_begin, * i, * end;
    json_value * top, * root, * alloc = 0;
-   json_state state;
-   int flags;
+   json_state state = { 0 };
+   long flags;
+   long num_digits = 0, num_e = 0;
+   json_int_t num_fraction = 0;
+
+   /* Skip UTF-8 BOM
+    */
+   if (length >= 3 && ((unsigned char) json [0]) == 0xEF
+                   && ((unsigned char) json [1]) == 0xBB
+                   && ((unsigned char) json [2]) == 0xBF)
+   {
+      json += 3;
+      length -= 3;
+   }
 
    error[0] = '\0';
-
-   memset (&state, 0, sizeof (json_state));
+   end = (json + length);
+
    memcpy (&state.settings, settings, sizeof (json_settings));
+
+   if (!state.settings.mem_alloc)
+      state.settings.mem_alloc = default_alloc;
+
+   if (!state.settings.mem_free)
+      state.settings.mem_free = default_free;
 
    memset (&state.uint_max, 0xFF, sizeof (state.uint_max));
@@ -224,22 +265,6 @@
       for (i = json ;; ++ i)
       {
-         json_char b = *i;
-
-         if (flags & flag_done)
-         {
-            if (!b)
-               break;
-
-            switch (b)
-            {
-               whitespace:
-                  continue;
-
-               default:
-                  sprintf (error, "%d:%d: Trailing garbage: `%c`", cur_line, e_off, b);
-                  goto e_failed;
-            };
-         }
-
+         json_char b = (i == end ? 0 : *i);
+         
          if (flags & flag_string)
          {
@@ -265,5 +290,6 @@
                   case 'u':
 
-                    if ((uc_b1 = hex_value (*++ i)) == 0xFF || (uc_b2 = hex_value (*++ i)) == 0xFF
+                    if (end - i < 4 || 
+                        (uc_b1 = hex_value (*++ i)) == 0xFF || (uc_b2 = hex_value (*++ i)) == 0xFF
                           || (uc_b3 = hex_value (*++ i)) == 0xFF || (uc_b4 = hex_value (*++ i)) == 0xFF)
                     {
@@ -344,4 +370,7 @@
                            = (json_char *) top->_reserved.object_mem;
 
+                        top->u.object.values [top->u.object.length].name_length
+                           = string_length;
+
                         (*(json_char **) &top->_reserved.object_mem) += string_length + 1;
                      }
@@ -361,4 +390,81 @@
          }
 
+         if (state.settings.settings & json_enable_comments)
+         {
+            if (flags & (flag_line_comment | flag_block_comment))
+            {
+               if (flags & flag_line_comment)
+               {
+                  if (b == '\r' || b == '\n' || !b)
+                  {
+                     flags &= ~ flag_line_comment;
+                     -- i;  /* so null can be reproc'd */
+                  }
+
+                  continue;
+               }
+
+               if (flags & flag_block_comment)
+               {
+                  if (!b)
+                  {  sprintf (error, "%d:%d: Unexpected EOF in block comment", cur_line, e_off);
+                     goto e_failed;
+                  }
+
+                  if (b == '*' && i < (end - 1) && i [1] == '/')
+                  {
+                     flags &= ~ flag_block_comment;
+                     ++ i;  /* skip closing sequence */
+                  }
+
+                  continue;
+               }
+            }
+            else if (b == '/')
+            {
+               if (! (flags & (flag_seek_value | flag_done)) && top->type != json_object)
+               {
+                  sprintf (error, "%d:%d: Comment not allowed here", cur_line, e_off);
+                  goto e_failed;
+               }
+
+               if (++ i == end)
+               {  sprintf (error, "%d:%d: EOF unexpected", cur_line, e_off);
+                  goto e_failed;
+               }
+
+               switch (b = *i)
+               {
+                  case '/':
+                     flags |= flag_line_comment;
+                     continue;
+
+                  case '*':
+                     flags |= flag_block_comment;
+                     continue;
+
+                  default:
+                     sprintf (error, "%d:%d: Unexpected `%c` in comment opening sequence", cur_line, e_off, b);
+                     goto e_failed;
+               };
+            }
+         }
+
+         if (flags & flag_done)
+         {
+            if (!b)
+               break;
+
+            switch (b)
+            {
+               whitespace:
+                  continue;
+
+               default:
+                  sprintf (error, "%d:%d: Trailing garbage: `%c`", cur_line, e_off, b);
+                  goto e_failed;
+            };
+         }
+
          if (flags & flag_seek_value)
          {
@@ -372,5 +478,5 @@
                   if (top->type == json_array)
                      flags = (flags & ~ (flag_need_comma | flag_seek_value)) | flag_next;
-                  else if (!state.settings.settings & json_relaxed_commas)
+                  else
                   {  sprintf (error, "%d:%d: Unexpected ]", cur_line, e_off);
                      goto e_failed;
@@ -438,5 +544,5 @@
                      case 't':
 
-                        if (*(++ i) != 'r' || *(++ i) != 'u' || *(++ i) != 'e')
+                        if ((end - i) < 3 || *(++ i) != 'r' || *(++ i) != 'u' || *(++ i) != 'e')
                            goto e_unknown_value;
 
@@ -451,5 +557,5 @@
                      case 'f':
 
-                        if (*(++ i) != 'a' || *(++ i) != 'l' || *(++ i) != 's' || *(++ i) != 'e')
+                        if ((end - i) < 4 || *(++ i) != 'a' || *(++ i) != 'l' || *(++ i) != 's' || *(++ i) != 'e')
                            goto e_unknown_value;
 
@@ -462,5 +568,5 @@
                      case 'n':
 
-                        if (*(++ i) != 'u' || *(++ i) != 'l' || *(++ i) != 'l')
+                        if ((end - i) < 3 || *(++ i) != 'u' || *(++ i) != 'l' || *(++ i) != 'l')
                            goto e_unknown_value;
 
@@ -478,15 +584,38 @@
                               goto e_alloc_failure;
 
-                           flags &= ~ (flag_exponent | flag_got_exponent_sign);
-
-                           if (state.first_pass)
-                              continue;
-
-                           if (top->type == json_double)
-                              top->u.dbl = g_ascii_strtod (i, (json_char **) &i);
-                           else
-                              top->u.integer = g_ascii_strtoll (i, (json_char **) &i, 10);
-
-                           flags |= flag_next | flag_reproc;
+                           if (!state.first_pass)
+                           {
+                              while (isdigit (b) || b == '+' || b == '-'
+                                        || b == 'e' || b == 'E' || b == '.')
+                              {
+                                 if ( (++ i) == end)
+                                 {
+                                    b = 0;
+                                    break;
+                                 }
+
+                                 b = *i;
+                              }
+
+                              flags |= flag_next | flag_reproc;
+                              break;
+                           }
+
+                           flags &= ~ (flag_num_negative | flag_num_e |
+                                        flag_num_e_got_sign | flag_num_e_negative |
+                                           flag_num_zero);
+
+                           num_digits = 0;
+                           num_fraction = 0;
+                           num_e = 0;
+
+                           if (b != '-')
+                           {
+                              flags |= flag_reproc;
+                              break;
+                           }
+
+                           flags |= flag_num_negative;
+                           continue;
                         }
                         else
@@ -510,5 +639,5 @@
                   case '"':
 
-                     if (flags & flag_need_comma && (!state.settings.settings & json_relaxed_commas))
+                     if (flags & flag_need_comma)
                      {
                         sprintf (error, "%d:%d: Expected , before \"", cur_line, e_off);
@@ -548,21 +677,43 @@
 
                if (isdigit (b))
+               {
+                  ++ num_digits;
+
+                  if (top->type == json_integer || flags & flag_num_e)
+                  {
+                     if (! (flags & flag_num_e))
+                     {
+                        if (flags & flag_num_zero)
+                        {  sprintf (error, "%d:%d: Unexpected `0` before `%c`", cur_line, e_off, b);
+                           goto e_failed;
+                        }
+
+                        if (num_digits == 1 && b == '0')
+                           flags |= flag_num_zero;
+                     }
+                     else
+                     {
+                        flags |= flag_num_e_got_sign;
+                        num_e = (num_e * 10) + (b - '0');
+                        continue;
+                     }
+
+                     top->u.integer = (top->u.integer * 10) + (b - '0');
+                     continue;
+                  }
+
+                  num_fraction = (num_fraction * 10) + (b - '0');
                   continue;
-
-               if (b == 'e' || b == 'E')
-               {
-                  if (!(flags & flag_exponent))
+               }
+
+               if (b == '+' || b == '-')
+               {
+                  if ( (flags & flag_num_e) && !(flags & flag_num_e_got_sign))
                   {
-                     flags |= flag_exponent;
-                     top->type = json_double;
-
-                     continue;
-                  }
-               }
-               else if (b == '+' || b == '-')
-               {
-                  if (flags & flag_exponent && !(flags & flag_got_exponent_sign))
-                  {
-                     flags |= flag_got_exponent_sign;
+                     flags |= flag_num_e_got_sign;
+
+                     if (b == '-')
+                        flags |= flag_num_e_negative;
+
                      continue;
                   }
@@ -570,6 +721,60 @@
                else if (b == '.' && top->type == json_integer)
                {
+                  if (!num_digits)
+                  {  sprintf (error, "%d:%d: Expected digit before `.`", cur_line, e_off);
+                     goto e_failed;
+                  }
+
                   top->type = json_double;
+                  top->u.dbl = (double) top->u.integer;
+
+                  num_digits = 0;
                   continue;
+               }
+
+               if (! (flags & flag_num_e))
+               {
+                  if (top->type == json_double)
+                  {
+                     if (!num_digits)
+                     {  sprintf (error, "%d:%d: Expected digit after `.`", cur_line, e_off);
+                        goto e_failed;
+                     }
+
+                     top->u.dbl += ((double) num_fraction) / (pow (10, (double) num_digits));
+                  }
+
+                  if (b == 'e' || b == 'E')
+                  {
+                     flags |= flag_num_e;
+
+                     if (top->type == json_integer)
+                     {
+                        top->type = json_double;
+                        top->u.dbl = (double) top->u.integer;
+                     }
+
+                     num_digits = 0;
+                     flags &= ~ flag_num_zero;
+
+                     continue;
+                  }
+               }
+               else
+               {
+                  if (!num_digits)
+                  {  sprintf (error, "%d:%d: Expected digit after `e`", cur_line, e_off);
+                     goto e_failed;
+                  }
+
+                  top->u.dbl *= pow (10, (double) (flags & flag_num_e_negative ? - num_e : num_e));
+               }
+
+               if (flags & flag_num_negative)
+               {
+                  if (top->type == json_integer)
+                     top->u.integer = - top->u.integer;
+                  else
+                     top->u.dbl = - top->u.dbl;
                }
 
@@ -673,23 +878,21 @@
    {
       top = alloc->_reserved.next_alloc;
-      free (alloc);
+      state.settings.mem_free (alloc, state.settings.user_data);
       alloc = top;
    }
 
    if (!state.first_pass)
-      json_value_free (root);
+      json_value_free_ex (&state.settings, root);
 
    return 0;
 }
 
-json_value * json_parse (const json_char * json)
+json_value * json_parse (const json_char * json, size_t length)
 {
-   json_settings settings;
-   memset (&settings, 0, sizeof (json_settings));
-
-   return json_parse_ex (&settings, json, 0);
+   json_settings settings = { 0 };
+   return json_parse_ex (&settings, json, length, 0);
 }
 
-void json_value_free (json_value * value)
+void json_value_free_ex (json_settings * settings, json_value * value)
 {
    json_value * cur_value;
@@ -708,5 +911,5 @@
             if (!value->u.array.length)
             {
-               free (value->u.array.values);
+               settings->mem_free (value->u.array.values, settings->user_data);
                break;
             }
@@ -719,5 +922,5 @@
             if (!value->u.object.length)
             {
-               free (value->u.object.values);
+               settings->mem_free (value->u.object.values, settings->user_data);
                break;
             }
@@ -728,5 +931,5 @@
          case json_string:
 
-            free (value->u.string.ptr);
+            settings->mem_free (value->u.string.ptr, settings->user_data);
             break;
 
@@ -737,7 +940,13 @@
       cur_value = value;
       value = value->parent;
-      free (cur_value);
+      settings->mem_free (cur_value, settings->user_data);
    }
 }
 
-
+void json_value_free (json_value * value)
+{
+   json_settings settings = { 0 };
+   settings.mem_free = default_free;
+   json_value_free_ex (&settings, value);
+}
+
Index: lib/json.h
===================================================================
--- lib/json.h	(revision e76cf264de6caaca24fa308cab78e770fc4a6508)
+++ lib/json.h	(revision 7a80925eabe969ffc1e8e24c2ad31c80c33ab3cd)
@@ -1,6 +1,6 @@
 
-/* vim: set et ts=3 sw=3 ft=c:
- *
- * Copyright (C) 2012 James McLaughlin et al.  All rights reserved.
+/* vim: set et ts=3 sw=3 sts=3 ft=c:
+ *
+ * Copyright (C) 2012, 2013, 2014 James McLaughlin et al.  All rights reserved.
  * https://github.com/udp/json-parser
  *
@@ -36,4 +36,15 @@
 #endif
 
+#ifndef json_int_t
+   #ifndef _MSC_VER
+      #include <inttypes.h>
+      #define json_int_t int64_t
+   #else
+      #define json_int_t __int64
+   #endif
+#endif
+
+#include <stdlib.h>
+
 #ifdef __cplusplus
 
@@ -50,7 +61,15 @@
    int settings;
 
+   /* Custom allocator support (leave null to use malloc/free)
+    */
+
+   void * (* mem_alloc) (size_t, int zero, void * user_data);
+   void (* mem_free) (void *, void * user_data);
+
+   void * user_data;  /* will be passed to mem_alloc and mem_free */
+
 } json_settings;
 
-#define json_relaxed_commas 1
+#define json_enable_comments  0x01
 
 typedef enum
@@ -78,5 +97,5 @@
    {
       int boolean;
-      long long integer;
+      json_int_t integer;
       double dbl;
 
@@ -95,7 +114,18 @@
          {
             json_char * name;
+            unsigned int name_length;
+
             struct _json_value * value;
 
          } * values;
+
+         #if defined(__cplusplus) && __cplusplus >= 201103L
+         decltype(values) begin () const
+         {  return values;
+         }
+         decltype(values) end () const
+         {  return values + length;
+         }
+         #endif
 
       } object;
@@ -106,4 +136,13 @@
          struct _json_value ** values;
 
+         #if defined(__cplusplus) && __cplusplus >= 201103L
+         decltype(values) begin () const
+         {  return values;
+         }
+         decltype(values) end () const
+         {  return values + length;
+         }
+         #endif
+
       } array;
 
@@ -163,10 +202,40 @@
          }
 
-         inline operator long () const
-         {  return u.integer;
+         inline operator json_int_t () const
+         {  
+            switch (type)
+            {
+               case json_integer:
+                  return u.integer;
+
+               case json_double:
+                  return (json_int_t) u.dbl;
+
+               default:
+                  return 0;
+            };
          }
 
          inline operator bool () const
-         {  return u.boolean != 0;
+         {  
+            if (type != json_boolean)
+               return false;
+
+            return u.boolean != 0;
+         }
+
+         inline operator double () const
+         {  
+            switch (type)
+            {
+               case json_integer:
+                  return (double) u.integer;
+
+               case json_double:
+                  return u.dbl;
+
+               default:
+                  return 0;
+            };
          }
 
@@ -175,11 +244,21 @@
 } json_value;
 
-json_value * json_parse
-   (const json_char * json);
-
-json_value * json_parse_ex
-   (json_settings * settings, const json_char * json, char * error);
+json_value * json_parse (const json_char * json,
+                         size_t length);
+
+#define json_error_max 128
+json_value * json_parse_ex (json_settings * settings,
+                            const json_char * json,
+                            size_t length,
+                            char * error);
 
 void json_value_free (json_value *);
+
+
+/* Not usually necessary, unless you used a custom mem_alloc and now want to
+ * use a custom mem_free.
+ */
+void json_value_free_ex (json_settings * settings,
+                         json_value *);
 
 
Index: lib/oauth2.c
===================================================================
--- lib/oauth2.c	(revision e76cf264de6caaca24fa308cab78e770fc4a6508)
+++ lib/oauth2.c	(revision 7a80925eabe969ffc1e8e24c2ad31c80c33ab3cd)
@@ -154,5 +154,5 @@
 	                      strstr( content_type, "text/javascript" ) ) )
 	{
-		json_value *js = json_parse( req->reply_body );
+		json_value *js = json_parse( req->reply_body, req->body_size );
 		if( js && js->type == json_object )
 		{
