/* JSON_parser.c */
/* 2005-12-30 */
/*
Copyright (c) 2005 JSON.org
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
The Software shall be used for Good, not Evil.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/
#include "JSON_parser.h"
#include <stdio.h>
#define true 1
#define false 0
/*
Characters are mapped into these 32 symbol classes. This allows for
significant reductions in the size of the state transition table.
*/
/* error */
#define S_ERR -1
/* space */
#define S_SPA 0
/* other whitespace */
#define S_WSP 1
/* { */
#define S_LBE 2
/* } */
#define S_RBE 3
/* [ */
#define S_LBT 4
/* ] */
#define S_RBT 5
/* : */
#define S_COL 6
/* , */
#define S_COM 7
/* " */
#define S_QUO 8
/* \ */
#define S_BAC 9
/* / */
#define S_SLA 10
/* + */
#define S_PLU 11
/* - */
#define S_MIN 12
/* . */
#define S_DOT 13
/* 0 */
#define S_ZER 14
/* 123456789 */
#define S_DIG 15
/* a */
#define S__A_ 16
/* b */
#define S__B_ 17
/* c */
#define S__C_ 18
/* d */
#define S__D_ 19
/* e */
#define S__E_ 20
/* f */
#define S__F_ 21
/* l */
#define S__L_ 22
/* n */
#define S__N_ 23
/* r */
#define S__R_ 24
/* s */
#define S__S_ 25
/* t */
#define S__T_ 26
/* u */
#define S__U_ 27
/* ABCDF */
#define S_A_F 28
/* E */
#define S_E 29
/* everything else */
#define S_ETC 30
/*
This table maps the 128 ASCII characters into the 32 character classes.
The remaining Unicode characters should be mapped to S_ETC.
*/
static int ascii_class[128] = {
S_ERR, S_ERR, S_ERR, S_ERR, S_ERR, S_ERR, S_ERR, S_ERR,
S_ERR, S_WSP, S_WSP, S_ERR, S_ERR, S_WSP, S_ERR, S_ERR,
S_ERR, S_ERR, S_ERR, S_ERR, S_ERR, S_ERR, S_ERR, S_ERR,
S_ERR, S_ERR, S_ERR, S_ERR, S_ERR, S_ERR, S_ERR, S_ERR,
S_SPA, S_ETC, S_QUO, S_ETC, S_ETC, S_ETC, S_ETC, S_ETC,
S_ETC, S_ETC, S_ETC, S_PLU, S_COM, S_MIN, S_DOT, S_SLA,
S_ZER, S_DIG, S_DIG, S_DIG, S_DIG, S_DIG, S_DIG, S_DIG,
S_DIG, S_DIG, S_COL, S_ETC, S_ETC, S_ETC, S_ETC, S_ETC,
S_ETC, S_A_F, S_A_F, S_A_F, S_A_F, S_E , S_A_F, S_ETC,
S_ETC, S_ETC, S_ETC, S_ETC, S_ETC, S_ETC, S_ETC, S_ETC,
S_ETC, S_ETC, S_ETC, S_ETC, S_ETC, S_ETC, S_ETC, S_ETC,
S_ETC, S_ETC, S_ETC, S_LBT, S_BAC, S_RBT, S_ETC, S_ETC,
S_ETC, S__A_, S__B_, S__C_, S__D_, S__E_, S__F_, S_ETC,
S_ETC, S_ETC, S_ETC, S_ETC, S__L_, S_ETC, S__N_, S_ETC,
S_ETC, S_ETC, S__R_, S__S_, S__T_, S__U_, S_ETC, S_ETC,
S_ETC, S_ETC, S_ETC, S_LBE, S_ETC, S_RBE, S_ETC, S_ETC
};
/*
The state transition table takes the current state and the current symbol,
and returns either a new state or an action. A new state is a number between
0 and 29. An action is a negative number between -1 and -9. A JSON text is
accepted if the end of the text is in state 9 and mode is MODE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{-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,21,22,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1},
/*21*/ { 9, 9,-1,-7,-1,-5,-1,-3,-1,-1,-1,-1,-1,23,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1},
/*22*/ { 9, 9,-1,-7,-1,-5,-1,-3,-1,-1,-1,-1,-1,23,22,22,-1,-1,-1,-1,24,-1,-1,-1,-1,-1,-1,-1,-1,24,-1},
/*23*/ { 9, 9,-1,-7,-1,-5,-1,-3,-1,-1,-1,-1,-1,-1,23,23,-1,-1,-1,-1,24,-1,-1,-1,-1,-1,-1,-1,-1,24,-1},
/*24*/ {-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,25,25,-1,26,26,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1},
/*25*/ {-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,26,26,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1},
/*26*/ { 9, 9,-1,-7,-1,-5,-1,-3,-1,-1,-1,-1,-1,-1,26,26,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1},
/*27*/ {27,27,-1,-1,-1,-1,-2,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1},
/*28*/ {28,28,-8,-1,-6,-1,-1,-1, 3,-1,-1,-1,20,-1,21,22,-1,-1,-1,-1,-1,13,-1,17,-1,-1,10,-1,-1,-1,-1},
/*29*/ {29,29,-1,-1,-1,-1,-1,-1, 3,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1}
};
#define JSON_PARSER_MAX_DEPTH 20
/*
A stack maintains the states of nested structures.
*/
typedef struct json_parser
{
int the_stack[JSON_PARSER_MAX_DEPTH];
zval *the_zstack[JSON_PARSER_MAX_DEPTH];
int the_top;
} json_parser;
/*
These modes can be pushed on the PDA stack.
*/
#define MODE_DONE 1
#define MODE_KEY 2
#define MODE_OBJECT 3
#define MODE_ARRAY 4
/*
Push a mode onto the stack. Return false if there is overflow.
*/
static int
push(json_parser *json, zval *z, int mode)
{
json->the_top += 1;
if (json->the_top >= JSON_PARSER_MAX_DEPTH) {
return false;
}
json->the_stack[json->the_top] = mode;
return true;
}
/*
Pop the stack, assuring that the current mode matches the expectation.
Return false if there is underflow or if the modes mismatch.
*/
static int
pop(json_parser *json, zval *z, int mode)
{
if (json->the_top < 0 || json->the_stack[json->the_top] != mode) {
return false;
}
json->the_stack[json->the_top] = 0;
json->the_top -= 1;
return true;
}
static int dehexchar(char c)
{
if (c >= '0' && c <= '9')
{
return
没有合适的资源?快使用搜索试试~ 我知道了~
linux 安装 php json-1.2.1.tgz扩展包(OS 64位 apache 5.1.6)
需积分: 50 19 下载量 89 浏览量
2013-03-11
11:17:36
上传
评论
收藏 17KB TGZ 举报
温馨提示
共18个文件
phpt:5个
c:4个
h:4个
环境:centos 5.0 ,php 5.1.6 因为需要phpize,所以安装php-devel。phpize 是为php扩展提供编译环境。 tar -xf json-1.2.1.tgz cd json-1.2.1 phpize ./configure make make install echo "; Enable json extension module" >> /etc/php.d/json.ini echo "extension=json.so" >/etc/php.d/json.ini
资源推荐
资源详情
资源评论
收起资源包目录
json-1.2.1.tgz (18个子文件)
json-1.2.1
json.dsp 5KB
tests
pass001.1.phpt 19KB
pass003.phpt 2KB
pass001.phpt 14KB
fail001.phpt 3KB
pass002.phpt 7KB
php_json.h 2KB
README 1KB
utf8_to_utf16.c 2KB
utf8_decode.c 5KB
JSON_parser.h 210B
json.c 12KB
config.w32 319B
utf8_decode.h 466B
JSON_parser.c 21KB
utf8_to_utf16.h 91B
config.m4 2KB
package.xml 5KB
共 18 条
- 1
资源评论
zhoushengbin3
- 粉丝: 17
- 资源: 8
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功