json-c: add version 0.11 to align with stx3.0
[pti/rtp.git] / meta-stx / recipes-devtools / json-c / files / json-c-CVE-2013-6371.patch
1 https://github.com/json-c/json-c/commit/64e36901a0614bf64a19bc3396469c66dcd0b015
2
3 * CVE-2013-6371: hash collision denial of service
4 * CVE-2013-6370: buffer overflow if size_t is larger than int
5
6 diff --git a/Makefile.am b/Makefile.am
7 index 24b9bdf..26ced27 100644
8 --- a/Makefile.am
9 +++ b/Makefile.am
10 @@ -23,7 +23,8 @@ libjson_cinclude_HEADERS = \
11         json_tokener.h \
12         json_util.h \
13         linkhash.h \
14 -       printbuf.h
15 +       printbuf.h \
16 +       random_seed.h
17  
18  #libjsonx_includedir = $(libdir)/json-c-@VERSION@
19  #
20 @@ -41,7 +42,8 @@ libjson_c_la_SOURCES = \
21         json_tokener.c \
22         json_util.c \
23         linkhash.c \
24 -       printbuf.c
25 +       printbuf.c \
26 +       random_seed.c
27  
28  
29  distclean-local:
30 diff --git a/Makefile.am.inc b/Makefile.am.inc
31 index fd68a25..fec591b 100644
32 --- a/Makefile.am.inc
33 +++ b/Makefile.am.inc
34 @@ -1,2 +1,2 @@
35 -AM_CFLAGS = -Wall -Werror -Wextra -Wwrite-strings -Wno-unused-parameter -std=gnu99 -D_GNU_SOURCE -D_REENTRANT
36 +AM_CFLAGS = -Wall -Werror -Wno-error=deprecated-declarations -Wextra -Wwrite-strings -Wno-unused-parameter -std=gnu99 -D_GNU_SOURCE -D_REENTRANT
37  
38 diff --git a/json_object.h b/json_object.h
39 index 1005734..200ac40 100644
40 --- a/json_object.h
41 +++ b/json_object.h
42 @@ -13,6 +13,14 @@
43  #ifndef _json_object_h_
44  #define _json_object_h_
45  
46 +#ifdef __GNUC__
47 +#define THIS_FUNCTION_IS_DEPRECATED(func) func __attribute__ ((deprecated))
48 +#elif defined(_MSC_VER)
49 +#define THIS_FUNCTION_IS_DEPRECATED(func) __declspec(deprecated) func
50 +#else
51 +#define THIS_FUNCTION_IS_DEPRECATED(func) func
52 +#endif
53 +
54  #include "json_inttypes.h"
55  
56  #ifdef __cplusplus
57 @@ -279,8 +287,8 @@ extern void json_object_object_add(struct json_object* obj, const char *key,
58   * @returns the json_object associated with the given field name
59   * @deprecated Please use json_object_object_get_ex
60   */
61 -extern struct json_object* json_object_object_get(struct json_object* obj,
62 -                                                 const char *key);
63 +THIS_FUNCTION_IS_DEPRECATED(extern struct json_object* json_object_object_get(struct json_object* obj,
64 +                                                 const char *key));
65  
66  /** Get the json_object associated with a given object field.  
67   *
68 diff --git a/json_tokener.c b/json_tokener.c
69 index a1019c0..19de8ef 100644
70 --- a/json_tokener.c
71 +++ b/json_tokener.c
72 @@ -81,6 +81,7 @@ static const char* json_tokener_errors[] = {
73    "object value separator ',' expected",
74    "invalid string sequence",
75    "expected comment",
76 +  "buffer size overflow"
77  };
78  
79  const char *json_tokener_error_desc(enum json_tokener_error jerr)
80 @@ -243,6 +244,16 @@ struct json_object* json_tokener_parse_ex(struct json_tokener *tok,
81    tok->char_offset = 0;
82    tok->err = json_tokener_success;
83  
84 +  /* this interface is presently not 64-bit clean due to the int len argument
85 +     and the internal printbuf interface that takes 32-bit int len arguments
86 +     so the function limits the maximum string size to INT32_MAX (2GB).
87 +     If the function is called with len == -1 then strlen is called to check
88 +     the string length is less than INT32_MAX (2GB) */
89 +  if ((len < -1) || (len == -1 && strlen(str) > INT32_MAX)) {
90 +    tok->err = json_tokener_error_size;
91 +    return NULL;
92 +  }
93 +
94    while (PEEK_CHAR(c, tok)) {
95  
96    redo_char:
97 diff --git a/json_tokener.h b/json_tokener.h
98 index 5471d97..a72d2bd 100644
99 --- a/json_tokener.h
100 +++ b/json_tokener.h
101 @@ -33,7 +33,8 @@ enum json_tokener_error {
102    json_tokener_error_parse_object_key_sep,
103    json_tokener_error_parse_object_value_sep,
104    json_tokener_error_parse_string,
105 -  json_tokener_error_parse_comment
106 +  json_tokener_error_parse_comment,
107 +  json_tokener_error_size
108  };
109  
110  enum json_tokener_state {
111 @@ -163,6 +164,11 @@ extern void json_tokener_set_flags(struct json_tokener *tok, int flags);
112   * responsible for calling json_tokener_parse_ex with an appropriate str
113   * parameter starting with the extra characters.
114   *
115 + * This interface is presently not 64-bit clean due to the int len argument
116 + * so the function limits the maximum string size to INT32_MAX (2GB).
117 + * If the function is called with len == -1 then strlen is called to check
118 + * the string length is less than INT32_MAX (2GB)
119 + *
120   * Example:
121   * @code
122  json_object *jobj = NULL;
123 diff --git a/linkhash.c b/linkhash.c
124 index 5043148..712c387 100644
125 --- a/linkhash.c
126 +++ b/linkhash.c
127 @@ -17,6 +17,11 @@
128  #include <stddef.h>
129  #include <limits.h>
130  
131 +#ifdef HAVE_ENDIAN_H
132 +# include <endian.h>    /* attempt to define endianness */
133 +#endif
134 +
135 +#include "random_seed.h"
136  #include "linkhash.h"
137  
138  void lh_abort(const char *msg, ...)
139 @@ -39,14 +44,378 @@ int lh_ptr_equal(const void *k1, const void *k2)
140         return (k1 == k2);
141  }
142  
143 +/* 
144 + * hashlittle from lookup3.c, by Bob Jenkins, May 2006, Public Domain.
145 + * http://burtleburtle.net/bob/c/lookup3.c
146 + * minor modifications to make functions static so no symbols are exported
147 + * minor mofifications to compile with -Werror
148 + */
149 +
150 +/*
151 +-------------------------------------------------------------------------------
152 +lookup3.c, by Bob Jenkins, May 2006, Public Domain.
153 +
154 +These are functions for producing 32-bit hashes for hash table lookup.
155 +hashword(), hashlittle(), hashlittle2(), hashbig(), mix(), and final() 
156 +are externally useful functions.  Routines to test the hash are included 
157 +if SELF_TEST is defined.  You can use this free for any purpose.  It's in
158 +the public domain.  It has no warranty.
159 +
160 +You probably want to use hashlittle().  hashlittle() and hashbig()
161 +hash byte arrays.  hashlittle() is is faster than hashbig() on
162 +little-endian machines.  Intel and AMD are little-endian machines.
163 +On second thought, you probably want hashlittle2(), which is identical to
164 +hashlittle() except it returns two 32-bit hashes for the price of one.  
165 +You could implement hashbig2() if you wanted but I haven't bothered here.
166 +
167 +If you want to find a hash of, say, exactly 7 integers, do
168 +  a = i1;  b = i2;  c = i3;
169 +  mix(a,b,c);
170 +  a += i4; b += i5; c += i6;
171 +  mix(a,b,c);
172 +  a += i7;
173 +  final(a,b,c);
174 +then use c as the hash value.  If you have a variable length array of
175 +4-byte integers to hash, use hashword().  If you have a byte array (like
176 +a character string), use hashlittle().  If you have several byte arrays, or
177 +a mix of things, see the comments above hashlittle().  
178 +
179 +Why is this so big?  I read 12 bytes at a time into 3 4-byte integers, 
180 +then mix those integers.  This is fast (you can do a lot more thorough
181 +mixing with 12*3 instructions on 3 integers than you can with 3 instructions
182 +on 1 byte), but shoehorning those bytes into integers efficiently is messy.
183 +-------------------------------------------------------------------------------
184 +*/
185 +
186 +/*
187 + * My best guess at if you are big-endian or little-endian.  This may
188 + * need adjustment.
189 + */
190 +#if (defined(__BYTE_ORDER) && defined(__LITTLE_ENDIAN) && \
191 +     __BYTE_ORDER == __LITTLE_ENDIAN) || \
192 +    (defined(i386) || defined(__i386__) || defined(__i486__) || \
193 +     defined(__i586__) || defined(__i686__) || defined(vax) || defined(MIPSEL))
194 +# define HASH_LITTLE_ENDIAN 1
195 +# define HASH_BIG_ENDIAN 0
196 +#elif (defined(__BYTE_ORDER) && defined(__BIG_ENDIAN) && \
197 +       __BYTE_ORDER == __BIG_ENDIAN) || \
198 +      (defined(sparc) || defined(POWERPC) || defined(mc68000) || defined(sel))
199 +# define HASH_LITTLE_ENDIAN 0
200 +# define HASH_BIG_ENDIAN 1
201 +#else
202 +# define HASH_LITTLE_ENDIAN 0
203 +# define HASH_BIG_ENDIAN 0
204 +#endif
205 +
206 +#define hashsize(n) ((uint32_t)1<<(n))
207 +#define hashmask(n) (hashsize(n)-1)
208 +#define rot(x,k) (((x)<<(k)) | ((x)>>(32-(k))))
209 +
210 +/*
211 +-------------------------------------------------------------------------------
212 +mix -- mix 3 32-bit values reversibly.
213 +
214 +This is reversible, so any information in (a,b,c) before mix() is
215 +still in (a,b,c) after mix().
216 +
217 +If four pairs of (a,b,c) inputs are run through mix(), or through
218 +mix() in reverse, there are at least 32 bits of the output that
219 +are sometimes the same for one pair and different for another pair.
220 +This was tested for:
221 +* pairs that differed by one bit, by two bits, in any combination
222 +  of top bits of (a,b,c), or in any combination of bottom bits of
223 +  (a,b,c).
224 +* "differ" is defined as +, -, ^, or ~^.  For + and -, I transformed
225 +  the output delta to a Gray code (a^(a>>1)) so a string of 1's (as
226 +  is commonly produced by subtraction) look like a single 1-bit
227 +  difference.
228 +* the base values were pseudorandom, all zero but one bit set, or 
229 +  all zero plus a counter that starts at zero.
230 +
231 +Some k values for my "a-=c; a^=rot(c,k); c+=b;" arrangement that
232 +satisfy this are
233 +    4  6  8 16 19  4
234 +    9 15  3 18 27 15
235 +   14  9  3  7 17  3
236 +Well, "9 15 3 18 27 15" didn't quite get 32 bits diffing
237 +for "differ" defined as + with a one-bit base and a two-bit delta.  I
238 +used http://burtleburtle.net/bob/hash/avalanche.html to choose 
239 +the operations, constants, and arrangements of the variables.
240 +
241 +This does not achieve avalanche.  There are input bits of (a,b,c)
242 +that fail to affect some output bits of (a,b,c), especially of a.  The
243 +most thoroughly mixed value is c, but it doesn't really even achieve
244 +avalanche in c.
245 +
246 +This allows some parallelism.  Read-after-writes are good at doubling
247 +the number of bits affected, so the goal of mixing pulls in the opposite
248 +direction as the goal of parallelism.  I did what I could.  Rotates
249 +seem to cost as much as shifts on every machine I could lay my hands
250 +on, and rotates are much kinder to the top and bottom bits, so I used
251 +rotates.
252 +-------------------------------------------------------------------------------
253 +*/
254 +#define mix(a,b,c) \
255 +{ \
256 +  a -= c;  a ^= rot(c, 4);  c += b; \
257 +  b -= a;  b ^= rot(a, 6);  a += c; \
258 +  c -= b;  c ^= rot(b, 8);  b += a; \
259 +  a -= c;  a ^= rot(c,16);  c += b; \
260 +  b -= a;  b ^= rot(a,19);  a += c; \
261 +  c -= b;  c ^= rot(b, 4);  b += a; \
262 +}
263 +
264 +/*
265 +-------------------------------------------------------------------------------
266 +final -- final mixing of 3 32-bit values (a,b,c) into c
267 +
268 +Pairs of (a,b,c) values differing in only a few bits will usually
269 +produce values of c that look totally different.  This was tested for
270 +* pairs that differed by one bit, by two bits, in any combination
271 +  of top bits of (a,b,c), or in any combination of bottom bits of
272 +  (a,b,c).
273 +* "differ" is defined as +, -, ^, or ~^.  For + and -, I transformed
274 +  the output delta to a Gray code (a^(a>>1)) so a string of 1's (as
275 +  is commonly produced by subtraction) look like a single 1-bit
276 +  difference.
277 +* the base values were pseudorandom, all zero but one bit set, or 
278 +  all zero plus a counter that starts at zero.
279 +
280 +These constants passed:
281 + 14 11 25 16 4 14 24
282 + 12 14 25 16 4 14 24
283 +and these came close:
284 +  4  8 15 26 3 22 24
285 + 10  8 15 26 3 22 24
286 + 11  8 15 26 3 22 24
287 +-------------------------------------------------------------------------------
288 +*/
289 +#define final(a,b,c) \
290 +{ \
291 +  c ^= b; c -= rot(b,14); \
292 +  a ^= c; a -= rot(c,11); \
293 +  b ^= a; b -= rot(a,25); \
294 +  c ^= b; c -= rot(b,16); \
295 +  a ^= c; a -= rot(c,4);  \
296 +  b ^= a; b -= rot(a,14); \
297 +  c ^= b; c -= rot(b,24); \
298 +}
299 +
300 +
301 +/*
302 +-------------------------------------------------------------------------------
303 +hashlittle() -- hash a variable-length key into a 32-bit value
304 +  k       : the key (the unaligned variable-length array of bytes)
305 +  length  : the length of the key, counting by bytes
306 +  initval : can be any 4-byte value
307 +Returns a 32-bit value.  Every bit of the key affects every bit of
308 +the return value.  Two keys differing by one or two bits will have
309 +totally different hash values.
310 +
311 +The best hash table sizes are powers of 2.  There is no need to do
312 +mod a prime (mod is sooo slow!).  If you need less than 32 bits,
313 +use a bitmask.  For example, if you need only 10 bits, do
314 +  h = (h & hashmask(10));
315 +In which case, the hash table should have hashsize(10) elements.
316 +
317 +If you are hashing n strings (uint8_t **)k, do it like this:
318 +  for (i=0, h=0; i<n; ++i) h = hashlittle( k[i], len[i], h);
319 +
320 +By Bob Jenkins, 2006.  bob_jenkins@burtleburtle.net.  You may use this
321 +code any way you wish, private, educational, or commercial.  It's free.
322 +
323 +Use for hash table lookup, or anything where one collision in 2^^32 is
324 +acceptable.  Do NOT use for cryptographic purposes.
325 +-------------------------------------------------------------------------------
326 +*/
327 +
328 +static uint32_t hashlittle( const void *key, size_t length, uint32_t initval)
329 +{
330 +  uint32_t a,b,c;                                          /* internal state */
331 +  union { const void *ptr; size_t i; } u;     /* needed for Mac Powerbook G4 */
332 +
333 +  /* Set up the internal state */
334 +  a = b = c = 0xdeadbeef + ((uint32_t)length) + initval;
335 +
336 +  u.ptr = key;
337 +  if (HASH_LITTLE_ENDIAN && ((u.i & 0x3) == 0)) {
338 +    const uint32_t *k = (const uint32_t *)key;         /* read 32-bit chunks */
339 +
340 +    /*------ all but last block: aligned reads and affect 32 bits of (a,b,c) */
341 +    while (length > 12)
342 +    {
343 +      a += k[0];
344 +      b += k[1];
345 +      c += k[2];
346 +      mix(a,b,c);
347 +      length -= 12;
348 +      k += 3;
349 +    }
350 +
351 +    /*----------------------------- handle the last (probably partial) block */
352 +    /* 
353 +     * "k[2]&0xffffff" actually reads beyond the end of the string, but
354 +     * then masks off the part it's not allowed to read.  Because the
355 +     * string is aligned, the masked-off tail is in the same word as the
356 +     * rest of the string.  Every machine with memory protection I've seen
357 +     * does it on word boundaries, so is OK with this.  But VALGRIND will
358 +     * still catch it and complain.  The masking trick does make the hash
359 +     * noticably faster for short strings (like English words).
360 +     */
361 +#ifndef VALGRIND
362 +
363 +    switch(length)
364 +    {
365 +    case 12: c+=k[2]; b+=k[1]; a+=k[0]; break;
366 +    case 11: c+=k[2]&0xffffff; b+=k[1]; a+=k[0]; break;
367 +    case 10: c+=k[2]&0xffff; b+=k[1]; a+=k[0]; break;
368 +    case 9 : c+=k[2]&0xff; b+=k[1]; a+=k[0]; break;
369 +    case 8 : b+=k[1]; a+=k[0]; break;
370 +    case 7 : b+=k[1]&0xffffff; a+=k[0]; break;
371 +    case 6 : b+=k[1]&0xffff; a+=k[0]; break;
372 +    case 5 : b+=k[1]&0xff; a+=k[0]; break;
373 +    case 4 : a+=k[0]; break;
374 +    case 3 : a+=k[0]&0xffffff; break;
375 +    case 2 : a+=k[0]&0xffff; break;
376 +    case 1 : a+=k[0]&0xff; break;
377 +    case 0 : return c;              /* zero length strings require no mixing */
378 +    }
379 +
380 +#else /* make valgrind happy */
381 +
382 +    const uint8_t  *k8 = (const uint8_t *)k;
383 +    switch(length)
384 +    {
385 +    case 12: c+=k[2]; b+=k[1]; a+=k[0]; break;
386 +    case 11: c+=((uint32_t)k8[10])<<16;  /* fall through */
387 +    case 10: c+=((uint32_t)k8[9])<<8;    /* fall through */
388 +    case 9 : c+=k8[8];                   /* fall through */
389 +    case 8 : b+=k[1]; a+=k[0]; break;
390 +    case 7 : b+=((uint32_t)k8[6])<<16;   /* fall through */
391 +    case 6 : b+=((uint32_t)k8[5])<<8;    /* fall through */
392 +    case 5 : b+=k8[4];                   /* fall through */
393 +    case 4 : a+=k[0]; break;
394 +    case 3 : a+=((uint32_t)k8[2])<<16;   /* fall through */
395 +    case 2 : a+=((uint32_t)k8[1])<<8;    /* fall through */
396 +    case 1 : a+=k8[0]; break;
397 +    case 0 : return c;
398 +    }
399 +
400 +#endif /* !valgrind */
401 +
402 +  } else if (HASH_LITTLE_ENDIAN && ((u.i & 0x1) == 0)) {
403 +    const uint16_t *k = (const uint16_t *)key;         /* read 16-bit chunks */
404 +    const uint8_t  *k8;
405 +
406 +    /*--------------- all but last block: aligned reads and different mixing */
407 +    while (length > 12)
408 +    {
409 +      a += k[0] + (((uint32_t)k[1])<<16);
410 +      b += k[2] + (((uint32_t)k[3])<<16);
411 +      c += k[4] + (((uint32_t)k[5])<<16);
412 +      mix(a,b,c);
413 +      length -= 12;
414 +      k += 6;
415 +    }
416 +
417 +    /*----------------------------- handle the last (probably partial) block */
418 +    k8 = (const uint8_t *)k;
419 +    switch(length)
420 +    {
421 +    case 12: c+=k[4]+(((uint32_t)k[5])<<16);
422 +             b+=k[2]+(((uint32_t)k[3])<<16);
423 +             a+=k[0]+(((uint32_t)k[1])<<16);
424 +             break;
425 +    case 11: c+=((uint32_t)k8[10])<<16;     /* fall through */
426 +    case 10: c+=k[4];
427 +             b+=k[2]+(((uint32_t)k[3])<<16);
428 +             a+=k[0]+(((uint32_t)k[1])<<16);
429 +             break;
430 +    case 9 : c+=k8[8];                      /* fall through */
431 +    case 8 : b+=k[2]+(((uint32_t)k[3])<<16);
432 +             a+=k[0]+(((uint32_t)k[1])<<16);
433 +             break;
434 +    case 7 : b+=((uint32_t)k8[6])<<16;      /* fall through */
435 +    case 6 : b+=k[2];
436 +             a+=k[0]+(((uint32_t)k[1])<<16);
437 +             break;
438 +    case 5 : b+=k8[4];                      /* fall through */
439 +    case 4 : a+=k[0]+(((uint32_t)k[1])<<16);
440 +             break;
441 +    case 3 : a+=((uint32_t)k8[2])<<16;      /* fall through */
442 +    case 2 : a+=k[0];
443 +             break;
444 +    case 1 : a+=k8[0];
445 +             break;
446 +    case 0 : return c;                     /* zero length requires no mixing */
447 +    }
448 +
449 +  } else {                        /* need to read the key one byte at a time */
450 +    const uint8_t *k = (const uint8_t *)key;
451 +
452 +    /*--------------- all but the last block: affect some 32 bits of (a,b,c) */
453 +    while (length > 12)
454 +    {
455 +      a += k[0];
456 +      a += ((uint32_t)k[1])<<8;
457 +      a += ((uint32_t)k[2])<<16;
458 +      a += ((uint32_t)k[3])<<24;
459 +      b += k[4];
460 +      b += ((uint32_t)k[5])<<8;
461 +      b += ((uint32_t)k[6])<<16;
462 +      b += ((uint32_t)k[7])<<24;
463 +      c += k[8];
464 +      c += ((uint32_t)k[9])<<8;
465 +      c += ((uint32_t)k[10])<<16;
466 +      c += ((uint32_t)k[11])<<24;
467 +      mix(a,b,c);
468 +      length -= 12;
469 +      k += 12;
470 +    }
471 +
472 +    /*-------------------------------- last block: affect all 32 bits of (c) */
473 +    switch(length)                   /* all the case statements fall through */
474 +    {
475 +    case 12: c+=((uint32_t)k[11])<<24;
476 +    case 11: c+=((uint32_t)k[10])<<16;
477 +    case 10: c+=((uint32_t)k[9])<<8;
478 +    case 9 : c+=k[8];
479 +    case 8 : b+=((uint32_t)k[7])<<24;
480 +    case 7 : b+=((uint32_t)k[6])<<16;
481 +    case 6 : b+=((uint32_t)k[5])<<8;
482 +    case 5 : b+=k[4];
483 +    case 4 : a+=((uint32_t)k[3])<<24;
484 +    case 3 : a+=((uint32_t)k[2])<<16;
485 +    case 2 : a+=((uint32_t)k[1])<<8;
486 +    case 1 : a+=k[0];
487 +             break;
488 +    case 0 : return c;
489 +    }
490 +  }
491 +
492 +  final(a,b,c);
493 +  return c;
494 +}
495 +
496  unsigned long lh_char_hash(const void *k)
497  {
498 -       unsigned int h = 0;
499 -       const char* data = (const char*)k;
500
501 -       while( *data!=0 ) h = h*129 + (unsigned int)(*data++) + LH_PRIME;
502 +       static volatile int random_seed = -1;
503 +
504 +       if (random_seed == -1) {
505 +               int seed;
506 +               /* we can't use -1 as it is the unitialized sentinel */
507 +               while ((seed = json_c_get_random_seed()) == -1);
508 +#if defined __GNUC__
509 +               __sync_val_compare_and_swap(&random_seed, -1, seed);
510 +#elif defined _MSC_VER
511 +               InterlockedCompareExchange(&random_seed, seed, -1);
512 +#else
513 +#warning "racy random seed initializtion if used by multiple threads"
514 +               random_seed = seed; /* potentially racy */
515 +#endif
516 +       }
517  
518 -       return h;
519 +       return hashlittle((const char*)k, strlen((const char*)k), random_seed); 
520  }
521  
522  int lh_char_equal(const void *k1, const void *k2)
523 diff --git a/linkhash.h b/linkhash.h
524 index 378de0b..950d09f 100644
525 --- a/linkhash.h
526 +++ b/linkhash.h
527 @@ -246,7 +246,7 @@ extern struct lh_entry* lh_table_lookup_entry(struct lh_table *t, const void *k)
528   * @return a pointer to the found value or NULL if it does not exist.
529   * @deprecated Use lh_table_lookup_ex instead.
530   */
531 -extern const void* lh_table_lookup(struct lh_table *t, const void *k);
532 +THIS_FUNCTION_IS_DEPRECATED(extern const void* lh_table_lookup(struct lh_table *t, const void *k));
533  
534  /**
535   * Lookup a record in the table
536 diff --git a/random_seed.c b/random_seed.c
537 new file mode 100644
538 index 0000000..3b520d4
539 --- /dev/null
540 +++ b/random_seed.c
541 @@ -0,0 +1,237 @@
542 +/*
543 + * random_seed.c
544 + *
545 + * Copyright (c) 2013 Metaparadigm Pte. Ltd.
546 + * Michael Clark <michael@metaparadigm.com>
547 + *
548 + * This library is free software; you can redistribute it and/or modify
549 + * it under the terms of the MIT license. See COPYING for details.
550 + *
551 + */
552 +
553 +#include <stdio.h>
554 +#include "config.h"
555 +
556 +#define DEBUG_SEED(s)
557 +
558 +
559 +#if defined ENABLE_RDRAND
560 +
561 +/* cpuid */
562 +
563 +#if defined __GNUC__ && (defined __i386__ || defined __x86_64__)
564 +#define HAS_X86_CPUID 1
565 +
566 +static void do_cpuid(int regs[], int h)
567 +{
568 +    __asm__ __volatile__(
569 +#if defined __x86_64__
570 +                         "pushq %%rbx;\n"
571 +#else
572 +                         "pushl %%ebx;\n"
573 +#endif
574 +                         "cpuid;\n"
575 +#if defined __x86_64__
576 +                         "popq %%rbx;\n"
577 +#else
578 +                         "popl %%ebx;\n"
579 +#endif
580 +                         : "=a"(regs[0]), [ebx] "=r"(regs[1]), "=c"(regs[2]), "=d"(regs[3])
581 +                         : "a"(h));
582 +}
583 +
584 +#elif defined _MSC_VER
585 +
586 +#define HAS_X86_CPUID 1
587 +#define do_cpuid __cpuid
588 +
589 +#endif
590 +
591 +/* has_rdrand */
592 +
593 +#if HAS_X86_CPUID
594 +
595 +static int has_rdrand()
596 +{
597 +    // CPUID.01H:ECX.RDRAND[bit 30] == 1
598 +    int regs[4];
599 +    do_cpuid(regs, 1);
600 +    return (regs[2] & (1 << 30)) != 0;
601 +}
602 +
603 +#endif
604 +
605 +/* get_rdrand_seed - GCC x86 and X64 */
606 +
607 +#if defined __GNUC__ && (defined __i386__ || defined __x86_64__)
608 +
609 +#define HAVE_RDRAND 1
610 +
611 +static int get_rdrand_seed()
612 +{
613 +    DEBUG_SEED("get_rdrand_seed");
614 +    int _eax;
615 +    // rdrand eax
616 +    __asm__ __volatile__("1: .byte 0x0F\n"
617 +                         "   .byte 0xC7\n"
618 +                         "   .byte 0xF0\n"
619 +                         "   jnc 1b;\n"
620 +                         : "=a" (_eax));
621 +    return _eax;
622 +}
623 +
624 +#endif
625 +
626 +#if defined _MSC_VER
627 +
628 +#if _MSC_VER >= 1700
629 +#define HAVE_RDRAND 1
630 +
631 +/* get_rdrand_seed - Visual Studio 2012 and above */
632 +
633 +static int get_rdrand_seed()
634 +{
635 +    DEBUG_SEED("get_rdrand_seed");
636 +    int r;
637 +    while (_rdrand32_step(&r) == 0);
638 +    return r;
639 +}
640 +
641 +#elif defined _M_IX86
642 +#define HAVE_RDRAND 1
643 +
644 +/* get_rdrand_seed - Visual Studio 2010 and below - x86 only */
645 +
646 +static int get_rdrand_seed()
647 +{
648 +       DEBUG_SEED("get_rdrand_seed");
649 +       int _eax;
650 +retry:
651 +       // rdrand eax
652 +       __asm _emit 0x0F __asm _emit 0xC7 __asm _emit 0xF0
653 +       __asm jnc retry
654 +       __asm mov _eax, eax
655 +       return _eax;
656 +}
657 +
658 +#endif
659 +#endif
660 +
661 +#endif /* defined ENABLE_RDRAND */
662 +
663 +
664 +/* has_dev_urandom */
665 +
666 +#if defined (__APPLE__) || defined(__unix__) || defined(__linux__)
667 +
668 +#include <string.h>
669 +#include <fcntl.h>
670 +#include <unistd.h>
671 +#include <errno.h>
672 +#include <stdlib.h>
673 +#include <sys/stat.h>
674 +
675 +#define HAVE_DEV_RANDOM 1
676 +
677 +static const char *dev_random_file = "/dev/urandom";
678 +
679 +static int has_dev_urandom()
680 +{
681 +    struct stat buf;
682 +    if (stat(dev_random_file, &buf)) {
683 +        return 0;
684 +    }
685 +    return ((buf.st_mode & S_IFCHR) != 0);
686 +}
687 +
688 +
689 +/* get_dev_random_seed */
690 +
691 +static int get_dev_random_seed()
692 +{
693 +    DEBUG_SEED("get_dev_random_seed");
694 +    
695 +    int fd = open(dev_random_file, O_RDONLY);
696 +    if (fd < 0) {
697 +        fprintf(stderr, "error opening %s: %s", dev_random_file, strerror(errno));
698 +        exit(1);
699 +    }
700 +    
701 +    int r;
702 +    ssize_t nread = read(fd, &r, sizeof(r));
703 +    if (nread != sizeof(r)) {
704 +        fprintf(stderr, "error read %s: %s", dev_random_file, strerror(errno));
705 +        exit(1);
706 +    }
707 +    else if (nread != sizeof(r)) {
708 +        fprintf(stderr, "error short read %s", dev_random_file);
709 +        exit(1);
710 +    }
711 +    close(fd);
712 +    return r;
713 +}
714 +
715 +#endif
716 +
717 +
718 +/* get_cryptgenrandom_seed */
719 +
720 +#ifdef WIN32
721 +
722 +#define HAVE_CRYPTGENRANDOM 1
723 +
724 +#include <windows.h>
725 +#pragma comment(lib, "advapi32.lib")
726 +
727 +static int get_cryptgenrandom_seed()
728 +{
729 +    DEBUG_SEED("get_cryptgenrandom_seed");
730 +    
731 +    HCRYPTPROV hProvider = 0;
732 +    int r;
733 +    
734 +    if (!CryptAcquireContextW(&hProvider, 0, 0, PROV_RSA_FULL, CRYPT_VERIFYCONTEXT | CRYPT_SILENT)) {
735 +        fprintf(stderr, "error CryptAcquireContextW");
736 +        exit(1);
737 +    }
738 +    
739 +    if (!CryptGenRandom(hProvider, sizeof(r), (BYTE*)&r)) {
740 +        fprintf(stderr, "error CryptGenRandom");
741 +        exit(1);
742 +    }
743 +    
744 +    CryptReleaseContext(hProvider, 0);
745 +    
746 +    return r;
747 +}
748 +
749 +#endif
750 +
751 +
752 +/* get_time_seed */
753 +
754 +#include <time.h>
755 +
756 +static int get_time_seed()
757 +{
758 +    DEBUG_SEED("get_time_seed");
759 +    
760 +    return (int)time(NULL) * 433494437;
761 +}
762 +
763 +
764 +/* json_c_get_random_seed */
765 +
766 +int json_c_get_random_seed()
767 +{
768 +#if HAVE_RDRAND
769 +    if (has_rdrand()) return get_rdrand_seed();
770 +#endif
771 +#if HAVE_DEV_RANDOM
772 +    if (has_dev_urandom()) return get_dev_random_seed();
773 +#endif
774 +#if HAVE_CRYPTGENRANDOM
775 +    return get_cryptgenrandom_seed();
776 +#endif
777 +    return get_time_seed();
778 +}
779 diff --git a/random_seed.h b/random_seed.h
780 new file mode 100644
781 index 0000000..7362d67
782 --- /dev/null
783 +++ b/random_seed.h
784 @@ -0,0 +1,25 @@
785 +/*
786 + * random_seed.h
787 + *
788 + * Copyright (c) 2013 Metaparadigm Pte. Ltd.
789 + * Michael Clark <michael@metaparadigm.com>
790 + *
791 + * This library is free software; you can redistribute it and/or modify
792 + * it under the terms of the MIT license. See COPYING for details.
793 + *
794 + */
795 +
796 +#ifndef seed_h
797 +#define seed_h
798 +
799 +#ifdef __cplusplus
800 +extern "C" {
801 +#endif
802 +
803 +extern int json_c_get_random_seed();
804 +
805 +#ifdef __cplusplus
806 +}
807 +#endif
808 +
809 +#endif
810 -- 
811 1.9.1
812
813 --- a/config.h.in       2013-04-03 04:04:18.000000000 +0200
814 +++ b/config.h.in       2014-04-10 10:32:09.318409377 +0200
815 @@ -3,12 +3,18 @@
816  /* Define if .gnu.warning accepts long strings. */
817  #undef HAS_GNU_WARNING_LONG
818  
819 +/* Enable RDRANR Hardware RNG Hash Seed */
820 +#undef ENABLE_RDRAND
821 +
822  /* Define to 1 if you have the <dlfcn.h> header file. */
823  #undef HAVE_DLFCN_H
824  
825  /* Define to 1 if you don't have `vprintf' but do have `_doprnt.' */
826  #undef HAVE_DOPRNT
827  
828 +/* Define to 1 if you have the <endian.h> header file. */
829 +#undef HAVE_ENDIAN_H
830 +
831  /* Define to 1 if you have the <fcntl.h> header file. */
832  #undef HAVE_FCNTL_H
833  
834 --- a/configure.in      2014-04-10 10:32:56.443006786 +0200
835 +++ b/configure.in      2014-04-10 10:34:26.480080755 +0200
836 @@ -15,6 +15,20 @@
837  )
838  AM_CONDITIONAL(ENABLE_OLDNAME_COMPAT, [test "x${enable_oldname_compat}" != "xno"])
839  
840 +AC_ARG_ENABLE(rdrand,
841 + AS_HELP_STRING([--enable-rdrand],
842 +   [Enable RDRAND Hardware RNG Hash Seed generation on supported x86/x64 platforms.]),
843 + [if test x$enableval = xyes; then
844 +  enable_rdrand=yes
845 +  AC_DEFINE(ENABLE_RDRAND, 1, [Enable RDRANR Hardware RNG Hash Seed])
846 + fi])
847 +
848 +if test "x$enable_rdrand" = "xyes"; then
849 +  AC_MSG_RESULT([RDRAND Hardware RNG Hash Seed enabled on supported x86/x64 platforms])
850 +else
851 +  AC_MSG_RESULT([RDRAND Hardware RNG Hash Seed disabled. Use --enable-rdrand to enable])
852 +fi
853 +
854  # Checks for programs.
855  
856  # Checks for libraries.
857 @@ -23,7 +37,7 @@
858  AC_CONFIG_HEADER(config.h)
859  AC_CONFIG_HEADER(json_config.h)
860  AC_HEADER_STDC
861 -AC_CHECK_HEADERS(fcntl.h limits.h strings.h syslog.h unistd.h [sys/cdefs.h] [sys/param.h] stdarg.h locale.h)
862 +AC_CHECK_HEADERS(fcntl.h limits.h strings.h syslog.h unistd.h [sys/cdefs.h] [sys/param.h] stdarg.h locale.h endian.h)
863  AC_CHECK_HEADER(inttypes.h,[AC_DEFINE([JSON_C_HAVE_INTTYPES_H],[1],[Public define for json_inttypes.h])])
864  
865  # Checks for typedefs, structures, and compiler characteristics.