[Fusionforge-commits] r16225 - branches/Branch_5_1/src/common/include branches/Branch_5_2/src/common/include trunk/src/common/include

Thorsten Glaser mirabilos at fusionforge.org
Fri Sep 14 17:28:16 CEST 2012


Author: mirabilos
Date: 2012-09-14 17:28:15 +0200 (Fri, 14 Sep 2012)
New Revision: 16225

Modified:
   branches/Branch_5_1/src/common/include/minijson.php
   branches/Branch_5_2/src/common/include/minijson.php
   trunk/src/common/include/minijson.php
Log:
* make minijson_encode recursion depth safe
* sync with FF trunk version (use "elseif" PHP keyword)


Modified: branches/Branch_5_1/src/common/include/minijson.php
===================================================================
--- branches/Branch_5_1/src/common/include/minijson.php	2012-09-14 14:02:30 UTC (rev 16224)
+++ branches/Branch_5_1/src/common/include/minijson.php	2012-09-14 15:28:15 UTC (rev 16225)
@@ -1,8 +1,8 @@
 <?php
 /**
- * Minimal JSON generator and parser for FusionForge
+ * Minimal complete JSON generator and parser for FusionForge
  *
- * Copyright © 2010, 2011
+ * Copyright © 2010, 2011, 2012
  *	Thorsten “mirabilos” Glaser <t.glaser at tarent.de>
  * All rights reserved.
  *
@@ -35,12 +35,13 @@
 /**
  * Encodes an array (indexed or associative) as JSON.
  *
- * in:	array x
+ * in:	array x (Value to be encoded)
  * in:	string indent or bool false to skip beautification
+ * in:	integer	(optional) recursion depth (default: 32)
  * out:	string encoded
  */
-function minijson_encode($x, $ri="") {
-	if (!isset($x) || is_null($x) || (is_float($x) &&
+function minijson_encode($x, $ri="", $depth=32) {
+	if (!$depth-- || !isset($x) || is_null($x) || (is_float($x) &&
 	    (is_nan($x) || is_infinite($x))))
 		return "null";
 	if ($x === true)
@@ -55,8 +56,8 @@
 		$x = (string)$x;
 	}
 	if (is_float($x)) {
-		$rs = sprintf("%.14E", $x);
-		$v = explode("E", $rs);
+		$rs = sprintf("%.14e", $x);
+		$v = explode("e", $rs);
 		$rs = rtrim($v[0], "0");
 		if (substr($rs, -1) == ".") {
 			$rs .= "0";
@@ -98,25 +99,25 @@
 			}
 			if ($y == 0) {
 				break;
-			} else if ($y == 8) {
+			} elseif ($y == 8) {
 				$rs .= "\\b";
-			} else if ($y == 9) {
+			} elseif ($y == 9) {
 				$rs .= "\\t";
-			} else if ($y == 10) {
+			} elseif ($y == 10) {
 				$rs .= "\\n";
-			} else if ($y == 12) {
+			} elseif ($y == 12) {
 				$rs .= "\\f";
-			} else if ($y == 13) {
+			} elseif ($y == 13) {
 				$rs .= "\\r";
-			} else if ($y == 34) {
+			} elseif ($y == 34) {
 				$rs .= "\\\"";
-			} else if ($y == 92) {
+			} elseif ($y == 92) {
 				$rs .= "\\\\";
-			} else if ($y < 0x20 || $y > 0xFFFD ||
+			} elseif ($y < 0x20 || $y > 0xFFFD ||
 			    ($y >= 0xD800 && $y <= 0xDFFF) ||
 			    ($y > 0x7E && (!$isunicode || $y < 0xA0))) {
 				$rs .= sprintf("\\u%04X", $y);
-			} else if ($isunicode && $y > 0x7E) {
+			} elseif ($isunicode && $y > 0x7E) {
 				$rs .= mb_convert_encoding($v, "UTF-8",
 				    "UTF-16LE");
 			} else {
@@ -171,13 +172,13 @@
 			foreach ($s as $v) {
 				if ($first)
 					$first = false;
-				else if ($ri === false)
+				elseif ($ri === false)
 					$rs .= ",";
 				else
 					$rs .= ",\n";
 				if ($si !== false)
 					$rs .= $si;
-				$rs .= minijson_encode($x[$v], $si);
+				$rs .= minijson_encode($x[$v], $si, $depth);
 			}
 			if ($ri !== false)
 				$rs .= "\n" . $ri;
@@ -188,25 +189,22 @@
 		$rs = "{";
 		if ($ri !== false)
 			$rs .= "\n";
+		sort($k, SORT_STRING);
 		foreach ($k as $v) {
-			if (!isset($x[$v])) {
-				continue;
-			}
-
 			if ($first)
 				$first = false;
-			else if ($ri === false)
+			elseif ($ri === false)
 				$rs .= ",";
 			else
 				$rs .= ",\n";
 			if ($si !== false)
 				$rs .= $si;
-			$rs .= minijson_encode((string)$v, false);
+			$rs .= minijson_encode((string)$v, false, $depth);
 			if ($ri === false)
 				$rs .= ":";
 			else
 				$rs .= ": ";
-			$rs .= minijson_encode($x[$v], $si);
+			$rs .= minijson_encode($x[$v], $si, $depth);
 		}
 		if ($ri !== false)
 			$rs .= "\n" . $ri;
@@ -216,8 +214,8 @@
 
 	/* treat everything else as array or string */
 	if (!is_scalar($x))
-		return minijson_encode((array)$x, $ri);
-	return minijson_encode((string)$x, $ri);
+		return minijson_encode((array)$x, $ri, $depth);
+	return minijson_encode((string)$x, $ri, $depth);
 }
 
 /**
@@ -294,9 +292,9 @@
 	$wc = $j[$p++];
 	if ($wc >= 0x30 && $wc <= 0x39) {
 		$v += $wc - 0x30;
-	} else if ($wc >= 0x41 && $wc <= 0x46) {
+	} elseif ($wc >= 0x41 && $wc <= 0x46) {
 		$v += $wc - 0x37;
-	} else if ($wc >= 0x61 && $wc <= 0x66) {
+	} elseif ($wc >= 0x61 && $wc <= 0x66) {
 		$v += $wc - 0x57;
 	} else {
 		$ov = sprintf("invalid hex in unicode escape" .
@@ -331,7 +329,7 @@
 				$ov = "unexpected comma at wchar #" . $p;
 				return false;
 			}
-		} else if (!$first) {
+		} elseif (!$first) {
 			/*
 			 * all but the first member require a separating
 			 * comma; this also catches e.g. trailing
@@ -377,7 +375,7 @@
 				$ov = "unexpected comma at wchar #" . $p;
 				return false;
 			}
-		} else if (!$first) {
+		} elseif (!$first) {
 			/*
 			 * all but the first member require a separating
 			 * comma; this also catches e.g. trailing
@@ -433,7 +431,7 @@
 	/* style: falling through exits with false */
 	if ($wc == 0) {
 		$ov = "unexpected EOS at wchar #" . $p;
-	} else if ($wc == 0x6E) {
+	} elseif ($wc == 0x6E) {
 		/* literal null? */
 		if ($j[$p++] == 0x75 &&
 		    $j[$p++] == 0x6C &&
@@ -442,7 +440,7 @@
 			return true;
 		}
 		$ov = "expected ull after n near wchar #" . $p;
-	} else if ($wc == 0x74) {
+	} elseif ($wc == 0x74) {
 		/* literal true? */
 		if ($j[$p++] == 0x72 &&
 		    $j[$p++] == 0x75 &&
@@ -451,7 +449,7 @@
 			return true;
 		}
 		$ov = "expected rue after t near wchar #" . $p;
-	} else if ($wc == 0x66) {
+	} elseif ($wc == 0x66) {
 		/* literal false? */
 		if ($j[$p++] == 0x61 &&
 		    $j[$p++] == 0x6C &&
@@ -461,19 +459,19 @@
 			return true;
 		}
 		$ov = "expected alse after f near wchar #" . $p;
-	} else if ($wc == 0x5B) {
+	} elseif ($wc == 0x5B) {
 		if (--$depth > 0) {
 			return minijson_decode_array($j, $p, $ov, $depth);
 		}
 		$ov = "recursion limit exceeded at wchar #" . $p;
-	} else if ($wc == 0x7B) {
+	} elseif ($wc == 0x7B) {
 		if (--$depth > 0) {
 			return minijson_decode_object($j, $p, $ov, $depth);
 		}
 		$ov = "recursion limit exceeded at wchar #" . $p;
-	} else if ($wc == 0x22) {
+	} elseif ($wc == 0x22) {
 		return minijson_decode_string($j, $p, $ov);
-	} else if ($wc == 0x2D || ($wc >= 0x30 && $wc <= 0x39)) {
+	} elseif ($wc == 0x2D || ($wc >= 0x30 && $wc <= 0x39)) {
 		$p--;
 		return minijson_decode_number($j, $p, $ov);
 	} else {
@@ -491,7 +489,7 @@
 		if ($wc < 0x20) {
 			$ov = "unescaped control character $wc at wchar #" . $p;
 			return false;
-		} else if ($wc == 0x22) {
+		} elseif ($wc == 0x22) {
 			/* regular exit point for the loop */
 
 			/* convert to UTF-8, then re-check against UTF-16 */
@@ -502,23 +500,23 @@
 				return false;
 			}
 			return true;
-		} else if ($wc == 0x5C) {
+		} elseif ($wc == 0x5C) {
 			$wc = $j[$p++];
 			if ($wc == 0x22 ||
 			    $wc == 0x2F ||
 			    $wc == 0x5C) {
 				$s .= chr($wc) . chr(0);
-			} else if ($wc == 0x62) {
+			} elseif ($wc == 0x62) {
 				$s .= chr(0x08) . chr(0);
-			} else if ($wc == 0x66) {
+			} elseif ($wc == 0x66) {
 				$s .= chr(0x0C) . chr(0);
-			} else if ($wc == 0x6E) {
+			} elseif ($wc == 0x6E) {
 				$s .= chr(0x0A) . chr(0);
-			} else if ($wc == 0x72) {
+			} elseif ($wc == 0x72) {
 				$s .= chr(0x0D) . chr(0);
-			} else if ($wc == 0x74) {
+			} elseif ($wc == 0x74) {
 				$s .= chr(0x09) . chr(0);
-			} else if ($wc == 0x75) {
+			} elseif ($wc == 0x75) {
 				$v = 0;
 				for ($tmp = 1; $tmp <= 4; $tmp++) {
 					$v <<= 4;
@@ -537,10 +535,10 @@
 				$ov = "invalid escape sequence at wchar #" . $p;
 				return false;
 			}
-		} else if ($wc > 0xD7FF && $wc < 0xE000) {
+		} elseif ($wc > 0xD7FF && $wc < 0xE000) {
 			$ov = "surrogate $wc at wchar #" . $p;
 			return false;
-		} else if ($wc > 0xFFFD) {
+		} elseif ($wc > 0xFFFD) {
 			$ov = "non-Unicode char $wc at wchar #" . $p;
 			return false;
 		} else {
@@ -568,7 +566,7 @@
 			$ov = "no leading zeroes please at wchar #" . $p;
 			return false;
 		}
-	} else if ($wc >= 0x31 && $wc <= 0x39) {
+	} elseif ($wc >= 0x31 && $wc <= 0x39) {
 		/* begins with 1‥9 */
 		while ($wc >= 0x30 && $wc <= 0x39) {
 			$s .= chr($wc);
@@ -630,5 +628,3 @@
 	$ov = (float)$s;
 	return true;
 }
-
-?>

Modified: branches/Branch_5_2/src/common/include/minijson.php
===================================================================
--- branches/Branch_5_2/src/common/include/minijson.php	2012-09-14 14:02:30 UTC (rev 16224)
+++ branches/Branch_5_2/src/common/include/minijson.php	2012-09-14 15:28:15 UTC (rev 16225)
@@ -2,7 +2,7 @@
 /**
  * Minimal complete JSON generator and parser for FusionForge
  *
- * Copyright © 2010, 2011
+ * Copyright © 2010, 2011, 2012
  *	Thorsten “mirabilos” Glaser <t.glaser at tarent.de>
  * All rights reserved.
  *
@@ -37,10 +37,11 @@
  *
  * in:	array x (Value to be encoded)
  * in:	string indent or bool false to skip beautification
+ * in:	integer	(optional) recursion depth (default: 32)
  * out:	string encoded
  */
-function minijson_encode($x, $ri="") {
-	if (!isset($x) || is_null($x) || (is_float($x) &&
+function minijson_encode($x, $ri="", $depth=32) {
+	if (!$depth-- || !isset($x) || is_null($x) || (is_float($x) &&
 	    (is_nan($x) || is_infinite($x))))
 		return "null";
 	if ($x === true)
@@ -98,25 +99,25 @@
 			}
 			if ($y == 0) {
 				break;
-			} else if ($y == 8) {
+			} elseif ($y == 8) {
 				$rs .= "\\b";
-			} else if ($y == 9) {
+			} elseif ($y == 9) {
 				$rs .= "\\t";
-			} else if ($y == 10) {
+			} elseif ($y == 10) {
 				$rs .= "\\n";
-			} else if ($y == 12) {
+			} elseif ($y == 12) {
 				$rs .= "\\f";
-			} else if ($y == 13) {
+			} elseif ($y == 13) {
 				$rs .= "\\r";
-			} else if ($y == 34) {
+			} elseif ($y == 34) {
 				$rs .= "\\\"";
-			} else if ($y == 92) {
+			} elseif ($y == 92) {
 				$rs .= "\\\\";
-			} else if ($y < 0x20 || $y > 0xFFFD ||
+			} elseif ($y < 0x20 || $y > 0xFFFD ||
 			    ($y >= 0xD800 && $y <= 0xDFFF) ||
 			    ($y > 0x7E && (!$isunicode || $y < 0xA0))) {
 				$rs .= sprintf("\\u%04X", $y);
-			} else if ($isunicode && $y > 0x7E) {
+			} elseif ($isunicode && $y > 0x7E) {
 				$rs .= mb_convert_encoding($v, "UTF-8",
 				    "UTF-16LE");
 			} else {
@@ -171,13 +172,13 @@
 			foreach ($s as $v) {
 				if ($first)
 					$first = false;
-				else if ($ri === false)
+				elseif ($ri === false)
 					$rs .= ",";
 				else
 					$rs .= ",\n";
 				if ($si !== false)
 					$rs .= $si;
-				$rs .= minijson_encode($x[$v], $si);
+				$rs .= minijson_encode($x[$v], $si, $depth);
 			}
 			if ($ri !== false)
 				$rs .= "\n" . $ri;
@@ -188,21 +189,22 @@
 		$rs = "{";
 		if ($ri !== false)
 			$rs .= "\n";
+		sort($k, SORT_STRING);
 		foreach ($k as $v) {
 			if ($first)
 				$first = false;
-			else if ($ri === false)
+			elseif ($ri === false)
 				$rs .= ",";
 			else
 				$rs .= ",\n";
 			if ($si !== false)
 				$rs .= $si;
-			$rs .= minijson_encode((string)$v, false);
+			$rs .= minijson_encode((string)$v, false, $depth);
 			if ($ri === false)
 				$rs .= ":";
 			else
 				$rs .= ": ";
-			$rs .= minijson_encode($x[$v], $si);
+			$rs .= minijson_encode($x[$v], $si, $depth);
 		}
 		if ($ri !== false)
 			$rs .= "\n" . $ri;
@@ -212,8 +214,8 @@
 
 	/* treat everything else as array or string */
 	if (!is_scalar($x))
-		return minijson_encode((array)$x, $ri);
-	return minijson_encode((string)$x, $ri);
+		return minijson_encode((array)$x, $ri, $depth);
+	return minijson_encode((string)$x, $ri, $depth);
 }
 
 /**
@@ -290,9 +292,9 @@
 	$wc = $j[$p++];
 	if ($wc >= 0x30 && $wc <= 0x39) {
 		$v += $wc - 0x30;
-	} else if ($wc >= 0x41 && $wc <= 0x46) {
+	} elseif ($wc >= 0x41 && $wc <= 0x46) {
 		$v += $wc - 0x37;
-	} else if ($wc >= 0x61 && $wc <= 0x66) {
+	} elseif ($wc >= 0x61 && $wc <= 0x66) {
 		$v += $wc - 0x57;
 	} else {
 		$ov = sprintf("invalid hex in unicode escape" .
@@ -327,7 +329,7 @@
 				$ov = "unexpected comma at wchar #" . $p;
 				return false;
 			}
-		} else if (!$first) {
+		} elseif (!$first) {
 			/*
 			 * all but the first member require a separating
 			 * comma; this also catches e.g. trailing
@@ -373,7 +375,7 @@
 				$ov = "unexpected comma at wchar #" . $p;
 				return false;
 			}
-		} else if (!$first) {
+		} elseif (!$first) {
 			/*
 			 * all but the first member require a separating
 			 * comma; this also catches e.g. trailing
@@ -429,7 +431,7 @@
 	/* style: falling through exits with false */
 	if ($wc == 0) {
 		$ov = "unexpected EOS at wchar #" . $p;
-	} else if ($wc == 0x6E) {
+	} elseif ($wc == 0x6E) {
 		/* literal null? */
 		if ($j[$p++] == 0x75 &&
 		    $j[$p++] == 0x6C &&
@@ -438,7 +440,7 @@
 			return true;
 		}
 		$ov = "expected ull after n near wchar #" . $p;
-	} else if ($wc == 0x74) {
+	} elseif ($wc == 0x74) {
 		/* literal true? */
 		if ($j[$p++] == 0x72 &&
 		    $j[$p++] == 0x75 &&
@@ -447,7 +449,7 @@
 			return true;
 		}
 		$ov = "expected rue after t near wchar #" . $p;
-	} else if ($wc == 0x66) {
+	} elseif ($wc == 0x66) {
 		/* literal false? */
 		if ($j[$p++] == 0x61 &&
 		    $j[$p++] == 0x6C &&
@@ -457,19 +459,19 @@
 			return true;
 		}
 		$ov = "expected alse after f near wchar #" . $p;
-	} else if ($wc == 0x5B) {
+	} elseif ($wc == 0x5B) {
 		if (--$depth > 0) {
 			return minijson_decode_array($j, $p, $ov, $depth);
 		}
 		$ov = "recursion limit exceeded at wchar #" . $p;
-	} else if ($wc == 0x7B) {
+	} elseif ($wc == 0x7B) {
 		if (--$depth > 0) {
 			return minijson_decode_object($j, $p, $ov, $depth);
 		}
 		$ov = "recursion limit exceeded at wchar #" . $p;
-	} else if ($wc == 0x22) {
+	} elseif ($wc == 0x22) {
 		return minijson_decode_string($j, $p, $ov);
-	} else if ($wc == 0x2D || ($wc >= 0x30 && $wc <= 0x39)) {
+	} elseif ($wc == 0x2D || ($wc >= 0x30 && $wc <= 0x39)) {
 		$p--;
 		return minijson_decode_number($j, $p, $ov);
 	} else {
@@ -487,7 +489,7 @@
 		if ($wc < 0x20) {
 			$ov = "unescaped control character $wc at wchar #" . $p;
 			return false;
-		} else if ($wc == 0x22) {
+		} elseif ($wc == 0x22) {
 			/* regular exit point for the loop */
 
 			/* convert to UTF-8, then re-check against UTF-16 */
@@ -498,23 +500,23 @@
 				return false;
 			}
 			return true;
-		} else if ($wc == 0x5C) {
+		} elseif ($wc == 0x5C) {
 			$wc = $j[$p++];
 			if ($wc == 0x22 ||
 			    $wc == 0x2F ||
 			    $wc == 0x5C) {
 				$s .= chr($wc) . chr(0);
-			} else if ($wc == 0x62) {
+			} elseif ($wc == 0x62) {
 				$s .= chr(0x08) . chr(0);
-			} else if ($wc == 0x66) {
+			} elseif ($wc == 0x66) {
 				$s .= chr(0x0C) . chr(0);
-			} else if ($wc == 0x6E) {
+			} elseif ($wc == 0x6E) {
 				$s .= chr(0x0A) . chr(0);
-			} else if ($wc == 0x72) {
+			} elseif ($wc == 0x72) {
 				$s .= chr(0x0D) . chr(0);
-			} else if ($wc == 0x74) {
+			} elseif ($wc == 0x74) {
 				$s .= chr(0x09) . chr(0);
-			} else if ($wc == 0x75) {
+			} elseif ($wc == 0x75) {
 				$v = 0;
 				for ($tmp = 1; $tmp <= 4; $tmp++) {
 					$v <<= 4;
@@ -533,10 +535,10 @@
 				$ov = "invalid escape sequence at wchar #" . $p;
 				return false;
 			}
-		} else if ($wc > 0xD7FF && $wc < 0xE000) {
+		} elseif ($wc > 0xD7FF && $wc < 0xE000) {
 			$ov = "surrogate $wc at wchar #" . $p;
 			return false;
-		} else if ($wc > 0xFFFD) {
+		} elseif ($wc > 0xFFFD) {
 			$ov = "non-Unicode char $wc at wchar #" . $p;
 			return false;
 		} else {
@@ -564,7 +566,7 @@
 			$ov = "no leading zeroes please at wchar #" . $p;
 			return false;
 		}
-	} else if ($wc >= 0x31 && $wc <= 0x39) {
+	} elseif ($wc >= 0x31 && $wc <= 0x39) {
 		/* begins with 1‥9 */
 		while ($wc >= 0x30 && $wc <= 0x39) {
 			$s .= chr($wc);
@@ -626,5 +628,3 @@
 	$ov = (float)$s;
 	return true;
 }
-
-?>

Modified: trunk/src/common/include/minijson.php
===================================================================
--- trunk/src/common/include/minijson.php	2012-09-14 14:02:30 UTC (rev 16224)
+++ trunk/src/common/include/minijson.php	2012-09-14 15:28:15 UTC (rev 16225)
@@ -37,10 +37,11 @@
  *
  * in:	array x (Value to be encoded)
  * in:	string indent or bool false to skip beautification
+ * in:	integer	(optional) recursion depth (default: 32)
  * out:	string encoded
  */
-function minijson_encode($x, $ri="") {
-	if (!isset($x) || is_null($x) || (is_float($x) &&
+function minijson_encode($x, $ri="", $depth=32) {
+	if (!$depth-- || !isset($x) || is_null($x) || (is_float($x) &&
 	    (is_nan($x) || is_infinite($x))))
 		return "null";
 	if ($x === true)
@@ -171,13 +172,13 @@
 			foreach ($s as $v) {
 				if ($first)
 					$first = false;
-				else if ($ri === false)
+				elseif ($ri === false)
 					$rs .= ",";
 				else
 					$rs .= ",\n";
 				if ($si !== false)
 					$rs .= $si;
-				$rs .= minijson_encode($x[$v], $si);
+				$rs .= minijson_encode($x[$v], $si, $depth);
 			}
 			if ($ri !== false)
 				$rs .= "\n" . $ri;
@@ -192,18 +193,18 @@
 		foreach ($k as $v) {
 			if ($first)
 				$first = false;
-			else if ($ri === false)
+			elseif ($ri === false)
 				$rs .= ",";
 			else
 				$rs .= ",\n";
 			if ($si !== false)
 				$rs .= $si;
-			$rs .= minijson_encode((string)$v, false);
+			$rs .= minijson_encode((string)$v, false, $depth);
 			if ($ri === false)
 				$rs .= ":";
 			else
 				$rs .= ": ";
-			$rs .= minijson_encode($x[$v], $si);
+			$rs .= minijson_encode($x[$v], $si, $depth);
 		}
 		if ($ri !== false)
 			$rs .= "\n" . $ri;
@@ -213,8 +214,8 @@
 
 	/* treat everything else as array or string */
 	if (!is_scalar($x))
-		return minijson_encode((array)$x, $ri);
-	return minijson_encode((string)$x, $ri);
+		return minijson_encode((array)$x, $ri, $depth);
+	return minijson_encode((string)$x, $ri, $depth);
 }
 
 /**




More information about the Fusionforge-commits mailing list