X-Git-Url: http://git.osdn.net/view?a=blobdiff_plain;f=lib%2Fsrc%2Fhttp%2Fjson%2FNyLPC_cJsonRpcParser.c;h=eb471ed54d17af79cb376d382b02f087e75b32fa;hb=1b05d1d04e9cf891703a199b335f0a9d145f89db;hp=fb5ef5443185f5c9f345011275321db217440961;hpb=15b621b1cc9b0d8584f9483f3eedbf0786bb9b0a;p=mimic%2FMiMicSDK.git diff --git a/lib/src/http/json/NyLPC_cJsonRpcParser.c b/lib/src/http/json/NyLPC_cJsonRpcParser.c index fb5ef54..eb471ed 100644 --- a/lib/src/http/json/NyLPC_cJsonRpcParser.c +++ b/lib/src/http/json/NyLPC_cJsonRpcParser.c @@ -1,5 +1,5 @@ #include "NyLPC_cJsonRpcParser.h" - +#include // // NyLPC_TJsonRpcParserResult // @@ -27,7 +27,7 @@ NyLPC_TBool NyLPC_TJsonRpcParserResult_getInt32(const union NyLPC_TJsonRpcParser *o_val = *((NyLPC_TInt32*)(i_struct->method.param_buf + i_struct->method.param_index[i_idx])); return NyLPC_TBool_TRUE; } -NyLPC_TBool NyLPC_TJsonRpcParserResult_getStr(const union NyLPC_TJsonRpcParserResult* i_struct, NyLPC_TInt16 i_idx, const NyLPC_TChar** o_val) +NyLPC_TBool NyLPC_TJsonRpcParserResult_getStr(const union NyLPC_TJsonRpcParserResult* i_struct, NyLPC_TInt16 i_idx, const NyLPC_TChar** o_val, NyLPC_TUInt8* o_len) { if(i_struct->method.param_index[i_idx]==0xff){ return NyLPC_TBool_FALSE; @@ -35,9 +35,27 @@ NyLPC_TBool NyLPC_TJsonRpcParserResult_getStr(const union NyLPC_TJsonRpcParserRe if (i_struct->method.class_def->functions[i_struct->method.func_number].param_patt[i_idx] != NyLPC_cJsonRpcParser_TYPE_STRING){ return NyLPC_TBool_FALSE; } - *o_val = ((NyLPC_TChar*)(i_struct->method.param_buf + i_struct->method.param_index[i_idx])); + *o_val = ((NyLPC_TChar*)(i_struct->method.param_buf + i_struct->method.param_index[i_idx]+1)); + if (o_len){ + *o_len = *((NyLPC_TUInt8*)(*o_val-1)); + } + return NyLPC_TBool_TRUE; +} +NyLPC_TBool NyLPC_TJsonRpcParserResult_getByteArray(const union NyLPC_TJsonRpcParserResult* i_struct, NyLPC_TInt16 i_idx, const NyLPC_TUInt8** o_val, NyLPC_TUInt8* o_len) +{ + if (i_struct->method.param_index[i_idx] == 0xff){ + return NyLPC_TBool_FALSE; + } + if (i_struct->method.class_def->functions[i_struct->method.func_number].param_patt[i_idx] != NyLPC_cJsonRpcParser_TYPE_BSTRING){ + return NyLPC_TBool_FALSE; + } + *o_val = ((NyLPC_TUInt8*)(i_struct->method.param_buf + i_struct->method.param_index[i_idx] + 1)); + if (o_len){ + *o_len = *((NyLPC_TUInt8*)(*o_val - 1)); + } return NyLPC_TBool_TRUE; } + NyLPC_TBool NyLPC_TJsonRpcParserResult_getByte(const union NyLPC_TJsonRpcParserResult* i_struct, NyLPC_TInt16 i_idx, NyLPC_TUInt8* o_val) { if(i_struct->method.param_index[i_idx]==0xff){ @@ -140,17 +158,53 @@ static const struct NyLPC_TJsonRpcClassDef* findFunction(const struct NyLPC_TJso } return NULL; } -#define PARAM_ST_START 1 -#define PARAM_ST_VAL 2 -#define PARAM_ST_INT 3 -#define PARAM_ST_UINT 4 -#define PARAM_ST_STR 5 -#define PARAM_ST_NEXT 6 -#define PARAM_ST_BYTE 7 +/** + * putchar_paramのステータス値 + */ +#define PARAM_ST_START 1 +#define PARAM_ST_VAL 2 +#define PARAM_ST_INT 3 +#define PARAM_ST_UINT 4 +#define PARAM_ST_STR 5 +#define PARAM_ST_STR_ESCAPE_HEX 6 +#define PARAM_ST_STR_ESCAPE 7 +#define PARAM_ST_BYTE 8 +#define PARAM_ST_BSTR 9 +#define PARAM_ST_NEXT 11 -static void putchar_params(NyLPC_TcJsonRpcParser_t* i_inst, char i_c) + +/** + * シングルエスケープ文字列をエスケープされた文字列へ変換します。 + */ +static NyLPC_TBool convertSingleCharEscape(const char i_s, char* o_out) +{ + switch (i_s){ + case '0': *o_out = '\0'; break; + case 'r': *o_out = '\r'; break; + case 'n': *o_out = '\n'; break; + case 't': *o_out = '\t'; break; + case '\'': *o_out = '\''; break; + case '\"': *o_out = '\"'; break; + case '\\': *o_out = '\\'; break; + default: + return NyLPC_TBool_FALSE; + } + return NyLPC_TBool_TRUE; +} +/** + * 符号付整数値を構成する文字セットであるかを返します。 + */ +static NyLPC_TBool isSignedCharSet(const char i_c) { + return (strchr("-0123456789", i_c) != NULL); +} +/** + * 整数を構成する文字セットであるかを返します。 + */ +#define isUnSignedCharSet(i_c) isdigit(i_c) +static void putchar_params(NyLPC_TcJsonRpcParser_t* i_inst, char i_c) +{ switch (i_inst->_pst){ case PARAM_ST_START: if (i_c == '['){ @@ -180,7 +234,7 @@ static void putchar_params(NyLPC_TcJsonRpcParser_t* i_inst, char i_c) } switch (i_inst->_result->method.class_def->functions[i_inst->_result->method.func_number].param_patt[i_inst->_pcounter]){ case NyLPC_cJsonRpcParser_TYPE_INT32: - if (strchr("-0123456789", i_c) == NULL){ + if(!isSignedCharSet(i_c)){ goto ERROR; } i_inst->_pst = PARAM_ST_INT; @@ -189,7 +243,7 @@ static void putchar_params(NyLPC_TcJsonRpcParser_t* i_inst, char i_c) i_inst->_work.str.n = ((i_inst->_work.str.n + 3) / 4) * 4; break; case NyLPC_cJsonRpcParser_TYPE_UINT32: - if (strchr("0123456789", i_c) == NULL){ + if (!isUnSignedCharSet(i_c)){ goto ERROR; } i_inst->_pst = PARAM_ST_UINT; @@ -198,7 +252,7 @@ static void putchar_params(NyLPC_TcJsonRpcParser_t* i_inst, char i_c) i_inst->_work.str.n = ((i_inst->_work.str.n + 3) / 4) * 4; break; case NyLPC_cJsonRpcParser_TYPE_BYTE: - if (strchr("0123456789", i_c) == NULL){ + if (!isUnSignedCharSet(i_c)){ goto ERROR; } i_inst->_pst = PARAM_ST_BYTE; @@ -208,14 +262,67 @@ static void putchar_params(NyLPC_TcJsonRpcParser_t* i_inst, char i_c) if (i_c != '"'){ goto ERROR; } + i_inst->_result->method.param_index[i_inst->_pcounter] = i_inst->_work.str.n; + i_inst->_pcounter++; + if (i_inst->_work.str.n >= NyLPC_TJsonRpcParserResult_PARAM_BUF){ + goto ERROR; + } + i_inst->_work.str.n++;//文字数の記憶領域 i_inst->_pst = PARAM_ST_STR; - break; + return;//開始時に1バイト予約するから手順が違う + case NyLPC_cJsonRpcParser_TYPE_BSTRING: + if (i_c != '"'){ + goto ERROR; + } + i_inst->_result->method.param_index[i_inst->_pcounter] = i_inst->_work.str.n; + i_inst->_pcounter++; + if (i_inst->_work.str.n >= NyLPC_TJsonRpcParserResult_PARAM_BUF){ + goto ERROR; + } + i_inst->_work.str.n++;//文字数の記憶領域 + i_inst->_pst = PARAM_ST_BSTR; + i_inst->_work.str.st = 0; + return;//開始時に1バイト予約するから手順が違う default: goto ERROR; } i_inst->_result->method.param_index[i_inst->_pcounter] = i_inst->_work.str.n; i_inst->_pcounter++; return; + case PARAM_ST_STR_ESCAPE_HEX: + //16進数構成文字のみ + if (!isxdigit((int)i_c)){ + goto ERROR; + } + i_inst->_work.str.vt = (i_inst->_work.str.vt << 4) | NyLPC_ctox(i_c); + i_inst->_work.str.st++; + //2文字目で値を確定 + if (i_inst->_work.str.st>=2){ + if (i_inst->_work.str.n >= NyLPC_TJsonRpcParserResult_PARAM_BUF){ + goto ERROR; + } + i_inst->_result->method.param_buf[i_inst->_work.str.n] = i_inst->_work.str.vt; + i_inst->_work.str.n++; + i_inst->_pst = PARAM_ST_STR; + } + return; + case PARAM_ST_STR_ESCAPE: + if(i_c=='x'){ + i_inst->_pst = PARAM_ST_STR_ESCAPE_HEX; + i_inst->_work.str.st = 0; + i_inst->_work.str.vt = 0; + } + else{ + if (i_inst->_work.str.n >= NyLPC_TJsonRpcParserResult_PARAM_BUF){ + goto ERROR; + } + if (!convertSingleCharEscape(i_c,&i_inst->_result->method.param_buf[i_inst->_work.str.n])){ + goto ERROR; + } + i_inst->_work.str.n++; + i_inst->_pst = PARAM_ST_STR; + } + return; case PARAM_ST_STR: if (i_c == '"'){ //完了 @@ -224,9 +331,14 @@ static void putchar_params(NyLPC_TcJsonRpcParser_t* i_inst, char i_c) } i_inst->_result->method.param_buf[i_inst->_work.str.n] = '\0'; i_inst->_work.str.n++; + //文字数を更新 + *((NyLPC_TUInt8*)(i_inst->_result->method.param_buf+i_inst->_result->method.param_index[i_inst->_pcounter - 1])) = i_inst->_work.str.n - i_inst->_result->method.param_index[i_inst->_pcounter - 1]-2; i_inst->_pst = PARAM_ST_NEXT; return; } + else if (i_c=='\\'){ + i_inst->_pst = PARAM_ST_STR_ESCAPE; + } else{ if (i_inst->_work.str.n >= NyLPC_TJsonRpcParserResult_PARAM_BUF){ goto ERROR; @@ -235,8 +347,40 @@ static void putchar_params(NyLPC_TcJsonRpcParser_t* i_inst, char i_c) i_inst->_work.str.n++; } return; + case PARAM_ST_BSTR: + if (i_c == '"'){ + if (i_inst->_work.str.st != 0){ + goto ERROR; + } + //文字数を更新 + *((NyLPC_TUInt8*)(i_inst->_result->method.param_buf + i_inst->_result->method.param_index[i_inst->_pcounter - 1])) = i_inst->_work.str.n - i_inst->_result->method.param_index[i_inst->_pcounter - 1] - 1; + i_inst->_pst = PARAM_ST_NEXT; + return; + } + else{ + //16進数構成文字のみ + if (!isxdigit((int)i_c)){ + goto ERROR; + } + switch(i_inst->_work.str.st){ + case 0: + i_inst->_work.str.vt = NyLPC_ctox(i_c); + i_inst->_work.str.st++; + break; + case 1: + i_inst->_work.str.vt = (i_inst->_work.str.vt << 4) | NyLPC_ctox(i_c); + if (i_inst->_work.str.n >= NyLPC_TJsonRpcParserResult_PARAM_BUF){ + goto ERROR; + } + i_inst->_result->method.param_buf[i_inst->_work.str.n] = i_inst->_work.str.vt; + i_inst->_work.str.n++; + i_inst->_work.str.st = 0; + break; + } + } + return; case PARAM_ST_INT: - if (strchr("0123456789", i_c) != NULL){ + if (isUnSignedCharSet(i_c)){ i_inst->_work.int32.v = i_inst->_work.int32.v * 10 + (i_c - '0'); return; } @@ -258,7 +402,7 @@ static void putchar_params(NyLPC_TcJsonRpcParser_t* i_inst, char i_c) *((NyLPC_TInt32*)&(i_inst->_result->method.param_buf[i_inst->_work.str.n - 4])) = i_inst->_work.int32.v*i_inst->_work.int32.s; return; case PARAM_ST_UINT: - if (strchr("0123456789", i_c) != NULL){ + if (isUnSignedCharSet(i_c)){ i_inst->_work.uint32 = i_inst->_work.uint32 * 10 + (i_c - '0'); return; } @@ -279,7 +423,7 @@ static void putchar_params(NyLPC_TcJsonRpcParser_t* i_inst, char i_c) *((NyLPC_TUInt32*)&(i_inst->_result->method.param_buf[i_inst->_work.str.n - 4])) = i_inst->_work.uint32; return; case PARAM_ST_BYTE: - if (strchr("0123456789", i_c) != NULL){ + if (isUnSignedCharSet(i_c)){ i_inst->_work.uint32 = i_inst->_work.uint32 * 10 + (i_c - '0'); return; } @@ -426,16 +570,16 @@ void NyLPC_cJsonRpcParser_putChar(NyLPC_TcJsonRpcParser_t* i_inst,char i_c) //無視 return; } - if(strchr("0123456789",i_c)!=NULL){ - i_inst->_work.uint32=(i_c-'0'); + if (isUnSignedCharSet(i_c)){ + i_inst->_work.uint32 = (i_c - '0'); i_inst->_st=NyLPC_TcJsonRpcParser_ST_VAL_UINT; return; } //エラー goto ERROR; case NyLPC_TcJsonRpcParser_ST_VAL_UINT: - if(strchr("0123456789",i_c)!=NULL){ - i_inst->_work.uint32=i_inst->_work.uint32*10+(i_c-'0'); + if (isUnSignedCharSet(i_c)){ + i_inst->_work.uint32 = i_inst->_work.uint32 * 10 + (i_c - '0'); return; } if(strchr(" ,}",i_c)!=NULL){ @@ -526,6 +670,8 @@ const struct NyLPC_TJsonRpcMethodDef test_method[]= { "func9", "ss" }, { "f10", "suu" }, { "f11", "bbbb" }, + { "f12", "usu" }, + { "f13", "uBu" }, { NULL, "" } }; @@ -550,7 +696,7 @@ void NyLPC_cJsonRpcParser_putText(NyLPC_TcJsonRpcParser_t* i_inst, const NyLPC_T NyLPC_TUInt32 u32; NyLPC_TInt32 i32; NyLPC_TChar* c; - NyLPC_TUInt8 u8; + NyLPC_TUInt8 u8,l8; for (i = 0; i < i_size; i++){ @@ -571,7 +717,8 @@ void NyLPC_cJsonRpcParser_putText(NyLPC_TcJsonRpcParser_t* i_inst, const NyLPC_T switch (i_inst->_result->method.class_def->functions[i_inst->_result->method.func_number].param_patt[i]){ case 'u':NyLPC_TJsonRpcParserResult_getUInt32(i_inst->_result,i, &u32); printf("%u,", u32); break; case 'd':NyLPC_TJsonRpcParserResult_getInt32(i_inst->_result, i, &i32); printf("%d,", i32); break; - case 's':NyLPC_TJsonRpcParserResult_getStr(i_inst->_result, i, &c); printf("%s,", c); break; + case 's':NyLPC_TJsonRpcParserResult_getStr(i_inst->_result, i, &c, &l8); printf("%s:%d,", c, l8); break; + case 'B':NyLPC_TJsonRpcParserResult_getByteArray(i_inst->_result, i, &c, &l8); printf("--:%d,", l8); break; case 'b':NyLPC_TJsonRpcParserResult_getByte(i_inst->_result, i, &u8); printf("%u,", u8); break; } } @@ -615,7 +762,7 @@ void main(void) }; #else const char* t[] = { - "{\"version\":\"2.0\",\"method\":\"ns:class:func\",\"params\":[]}",//YES +/* "{\"version\":\"2.0\",\"method\":\"ns:class:func\",\"params\":[]}",//YES "{\"version\":\"2.0\",\"method\":\"ns:class:func\",\"params\": [] }",//YES "{\"version\":\"2.0\",\"method\":\"ns:class:func1\",\"params\": [-123] }",//YES "{\"version\":\"2.0\",\"method\":\"ns:class:func1\",\"params\": [\"123\"] }",//NG @@ -639,6 +786,9 @@ void main(void) "{\"version\":\"2.0\",\"method\":\"ns:class:f10\",\"params\": [\"01234567890123\",22,33] }",//OK "{\"version\":\"2.0\",\"method\":\"ns:class:f11\",\"params\": [ 0 , 1 ,2,3] }",//OK "{\"version\":\"2.0\",\"method\":\"ns:class:f11\",\"params\": [ 15 , 1 ,2,3a] }",//NG +*/ + "{\"version\":\"2.0\",\"method\":\"ns:class:f13\",\"params\": [2,\"0011223344ff\",1] }",//NG +// "{\"version\":\"2.0\",\"method\":\"ns:class:f12\",\"params\": [2,\"0123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789\",1] }",//NG NULL }; #endif @@ -648,6 +798,7 @@ void main(void) NyLPC_cJsonRpcParser_initialize(&inst, test_def_array); NyLPC_cJsonRpcParser_initParser(&inst, &ret); NyLPC_cJsonRpcParser_putText(&inst, t[i], strlen(t[i])); + continue; } printf("end\n"); return;