SmartAudio/lichee/brandy/pack_tools/merge_package/script.c

373 lines
8.7 KiB
C
Raw Normal View History

2018-07-13 01:31:50 +00:00
/*
**********************************************************************************************************************
* eGon
* the Embedded System
* script parser sub-system
*
* Copyright(C), 2006-2010, SoftWinners Microelectronic Co., Ltd.
* All Rights Reserved
*
* File : script.c
*
* By : Jerry
*
* Version : V2.00
*
* Date :
*
* Descript:
**********************************************************************************************************************
*/
#include <malloc.h>
#include "types.h"
#include <string.h>
#include "script.h"
//#include "crc.h"
//#include "sunxi_mbr.h"
#include <ctype.h>
#include <unistd.h>
static char *script_mod_buf = NULL; //ָ<><D6B8><EFBFBD><EFBFBD>һ<EFBFBD><D2BB><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
static int script_main_key_count = 0; //<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ĸ<EFBFBD><C4B8><EFBFBD>
static int partition_start;
static int partition_next;
static int _test_str_length(char *str)
{
int length = 0;
while(str[length++])
{
if(length > 32)
{
length = 32;
break;
}
}
return length;
}
/*
************************************************************************************************************
*
* script_parser_init
*
* <EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ƣ<EFBFBD>
*
* <EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>б<EFBFBD><EFBFBD><EFBFBD>script_buf: <EFBFBD>ű<EFBFBD><EFBFBD><EFBFBD><EFBFBD>ݳ<EFBFBD>
*
* <EFBFBD><EFBFBD><EFBFBD><EFBFBD>ֵ <EFBFBD><EFBFBD>
*
* ˵<EFBFBD><EFBFBD> <EFBFBD><EFBFBD>
*
*
************************************************************************************************************
*/
int script_parser_init(char *script_buf)
{
script_head_t *script_head;
if(script_buf)
{
script_mod_buf = script_buf;
script_head = (script_head_t *)script_mod_buf;
script_main_key_count = script_head->main_key_count;
return SCRIPT_PARSER_OK;
}
else
{
printf("the input script data buffer is null\n");
return SCRIPT_PARSER_EMPTY_BUFFER;
}
}
/*
************************************************************************************************************
*
* script_parser_exit
*
* <EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ƣ<EFBFBD>
*
* <EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>б<EFBFBD><EFBFBD><EFBFBD>NULL
*
* <EFBFBD><EFBFBD><EFBFBD><EFBFBD>ֵ <EFBFBD><EFBFBD>
*
* ˵<EFBFBD><EFBFBD> <EFBFBD><EFBFBD>
*
*
************************************************************************************************************
*/
int script_parser_exit(void)
{
script_mod_buf = NULL;
script_main_key_count = 0;
return SCRIPT_PARSER_OK;
}
/*
************************************************************************************************************
*
* script_parser_fetch
*
* <EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ƣ<EFBFBD>
*
* <EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>б<EFBFBD><EFBFBD><EFBFBD>
*
* <EFBFBD><EFBFBD><EFBFBD><EFBFBD>ֵ <EFBFBD><EFBFBD>
*
* ˵<EFBFBD><EFBFBD> <EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ݴ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ӽ<EFBFBD><EFBFBD><EFBFBD>ȡ<EFBFBD>ö<EFBFBD>Ӧ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>ֵ
*
*
************************************************************************************************************
*/
int script_parser_fetch(char *main_name, char *sub_name, int value[])
{
char main_char[32], sub_char[32];
script_main_key_t *main_key;
script_sub_key_t *sub_key;
int i, j, k;
int pattern, word_count;
//<2F><><EFBFBD><EFBFBD><EFBFBD>ű<EFBFBD>buffer<65>Ƿ<EFBFBD><C7B7><EFBFBD><EFBFBD><EFBFBD>
if(!script_mod_buf)
{
printf("the script data buffer is null, unable to parser\n");
return SCRIPT_PARSER_EMPTY_BUFFER;
}
//<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ƺ<EFBFBD><C6BA>Ӽ<EFBFBD><D3BC><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ƿ<EFBFBD>Ϊ<EFBFBD><CEAA>
if((main_name == NULL) || (sub_name == NULL))
{
printf("the input main key name or sub key name is null\n");
return SCRIPT_PARSER_KEYNAME_NULL;
}
//<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>buffer<65>Ƿ<EFBFBD>Ϊ<EFBFBD><CEAA>
if(value == NULL)
{
printf("the input data value is null\n");
return SCRIPT_PARSER_DATA_VALUE_NULL;
}
//<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ƺ<EFBFBD><C6BA>Ӽ<EFBFBD><D3BC><EFBFBD><EFBFBD>ƣ<EFBFBD><C6A3><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>16<31>ֽ<EFBFBD><D6BD><EFBFBD><EFBFBD><EFBFBD>ȡ16<31>ֽ<EFBFBD>
memset(main_char, 0, sizeof(main_char));
memset(sub_char, 0, sizeof(sub_char));
if(_test_str_length(main_name) <= 32)
{
strcpy(main_char, main_name);
}
else
{
strncpy(main_char, main_name, 31);
}
if(_test_str_length(sub_name) <= 32)
{
strcpy(sub_char, sub_name);
}
else
{
strncpy(sub_char, sub_name, 31);
}
for(i=0;i<script_main_key_count;i++)
{
main_key = (script_main_key_t *)(script_mod_buf + (sizeof(script_head_t)) + i * sizeof(script_main_key_t));
if(strcmp(main_key->main_name, main_char)) //<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ƥ<EFBFBD>䣬Ѱ<E4A3AC><D1B0><EFBFBD><EFBFBD>һ<EFBFBD><D2BB><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
{
continue;
}
//<2F><><EFBFBD><EFBFBD>ƥ<EFBFBD>䣬Ѱ<E4A3AC><D1B0><EFBFBD>Ӽ<EFBFBD><D3BC><EFBFBD><EFBFBD><EFBFBD>ƥ<EFBFBD><C6A5>
for(j=0;j<main_key->lenth;j++)
{
sub_key = (script_sub_key_t *)(script_mod_buf + (main_key->offset<<2) + (j * sizeof(script_sub_key_t)));
if(strcmp(sub_key->sub_name, sub_char)) //<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ƥ<EFBFBD>䣬Ѱ<E4A3AC><D1B0><EFBFBD><EFBFBD>һ<EFBFBD><D2BB><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
{
continue;
}
pattern = (sub_key->pattern>>16) & 0xffff; //<2F><>ȡ<EFBFBD><C8A1><EFBFBD>ݵ<EFBFBD><DDB5><EFBFBD><EFBFBD><EFBFBD>
word_count = (sub_key->pattern>> 0) & 0xffff; //<2F><>ȡ<EFBFBD><C8A1>ռ<EFBFBD>õ<EFBFBD>word<72><64><EFBFBD><EFBFBD>
//ȡ<><C8A1><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
switch(pattern)
{
case DATA_TYPE_SINGLE_WORD: //<2F><>word<72><64><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
value[0] = *(int *)(script_mod_buf + (sub_key->offset<<2));
break;
case DATA_TYPE_STRING: //<2F>ַ<EFBFBD><D6B7><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
memcpy((char *)value, script_mod_buf + (sub_key->offset<<2), word_count << 2);
break;
case DATA_TYPE_GPIO_WORD: //<2F><>word<72><64><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
k = 0;
while(k < word_count)
{
value[k] = *(int *)(script_mod_buf + (sub_key->offset<<2) + (k<<2));
k ++;
}
break;
}
return SCRIPT_PARSER_OK;
}
}
return SCRIPT_PARSER_KEY_NOT_FIND;
}
/*
************************************************************************************************************
*
* script_parser_fetch
*
* <EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ƣ<EFBFBD>
*
* <EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>б<EFBFBD><EFBFBD><EFBFBD>
*
* <EFBFBD><EFBFBD><EFBFBD><EFBFBD>ֵ <EFBFBD><EFBFBD>
*
* ˵<EFBFBD><EFBFBD> <EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ݴ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ӽ<EFBFBD><EFBFBD><EFBFBD>ȡ<EFBFBD>ö<EFBFBD>Ӧ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>ֵ
*
*
************************************************************************************************************
*/
int script_parser_fetch_partition(void)
{
script_main_key_t *main_key;
int i;
//<2F><><EFBFBD><EFBFBD><EFBFBD>ű<EFBFBD>buffer<65>Ƿ<EFBFBD><C7B7><EFBFBD><EFBFBD><EFBFBD>
if(!partition_start) //<2F><><EFBFBD><EFBFBD>partition_start<72><74>Ȼ<EFBFBD><C8BB><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>partition
{
for(i=0;i<script_main_key_count;i++)
{
main_key = (script_main_key_t *)(script_mod_buf + (sizeof(script_head_t)) + i * sizeof(script_main_key_t));
if(strcmp(main_key->main_name, "partition_start")) //<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ƥ<EFBFBD>䣬Ѱ<E4A3AC><D1B0><EFBFBD><EFBFBD>һ<EFBFBD><D2BB><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
{
continue;
}
else
{
partition_start = i;
partition_next = partition_start;
break;
}
}
if(!partition_start)
{
printf("unable to find key partition_start\n");
return -1;
}
}
partition_next ++;
main_key = (script_main_key_t *)(script_mod_buf + (sizeof(script_head_t)) + partition_next * sizeof(script_main_key_t));
if(strcmp(main_key->main_name, "partition"))
{
printf("this is not a partition key\n");
return 0;
}
return partition_next;
}
/*
************************************************************************************************************
*
* script_parser_fetch
*
* <EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ƣ<EFBFBD>
*
* <EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>б<EFBFBD><EFBFBD><EFBFBD>
*
* <EFBFBD><EFBFBD><EFBFBD><EFBFBD>ֵ <EFBFBD><EFBFBD>
*
* ˵<EFBFBD><EFBFBD> <EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ݴ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ӽ<EFBFBD><EFBFBD><EFBFBD>ȡ<EFBFBD>ö<EFBFBD>Ӧ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>ֵ
*
*
************************************************************************************************************
*/
int script_parser_fetch_mainkey_sub(char *sub_name, int index, int *value)
{
char sub_char[32];
script_main_key_t *main_key;
script_sub_key_t *sub_key;
int j, k;
int pattern, word_count;
//<2F><><EFBFBD><EFBFBD><EFBFBD>ű<EFBFBD>buffer<65>Ƿ<EFBFBD><C7B7><EFBFBD><EFBFBD><EFBFBD>
if(!script_mod_buf)
{
printf("the script data buffer is null, unable to parser\n");
return SCRIPT_PARSER_EMPTY_BUFFER;
}
//<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ƺ<EFBFBD><C6BA>Ӽ<EFBFBD><D3BC><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ƿ<EFBFBD>Ϊ<EFBFBD><CEAA>
if(sub_name == NULL)
{
printf("the input sub key name is null\n");
return SCRIPT_PARSER_KEYNAME_NULL;
}
//<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ƺ<EFBFBD><C6BA>Ӽ<EFBFBD><D3BC><EFBFBD><EFBFBD>ƣ<EFBFBD><C6A3><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>16<31>ֽ<EFBFBD><D6BD><EFBFBD><EFBFBD><EFBFBD>ȡ16<31>ֽ<EFBFBD>
memset(sub_char, 0, sizeof(sub_char));
if(_test_str_length(sub_name) <= 32)
{
strcpy(sub_char, sub_name);
}
main_key = (script_main_key_t *)(script_mod_buf + (sizeof(script_head_t)) + index * sizeof(script_main_key_t));
if(strcmp(main_key->main_name, "partition")) //<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ƥ<EFBFBD>䣬Ѱ<E4A3AC><D1B0><EFBFBD><EFBFBD>һ<EFBFBD><D2BB><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
{
printf("this is not a good partition key\n");
return -1;
}
//<2F><><EFBFBD><EFBFBD>ƥ<EFBFBD>䣬Ѱ<E4A3AC><D1B0><EFBFBD>Ӽ<EFBFBD><D3BC><EFBFBD><EFBFBD><EFBFBD>ƥ<EFBFBD><C6A5>
for(j=0;j<main_key->lenth;j++)
{
sub_key = (script_sub_key_t *)(script_mod_buf + (main_key->offset<<2) + (j * sizeof(script_sub_key_t)));
if(strcmp(sub_key->sub_name, sub_char)) //<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ƥ<EFBFBD>䣬Ѱ<E4A3AC><D1B0><EFBFBD><EFBFBD>һ<EFBFBD><D2BB><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
{
continue;
}
pattern = (sub_key->pattern>>16) & 0xffff; //<2F><>ȡ<EFBFBD><C8A1><EFBFBD>ݵ<EFBFBD><DDB5><EFBFBD><EFBFBD><EFBFBD>
word_count = (sub_key->pattern>> 0) & 0xffff; //<2F><>ȡ<EFBFBD><C8A1>ռ<EFBFBD>õ<EFBFBD>word<72><64><EFBFBD><EFBFBD>
//ȡ<><C8A1><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
switch(pattern)
{
case DATA_TYPE_SINGLE_WORD: //<2F><>word<72><64><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
value[0] = *(int *)(script_mod_buf + (sub_key->offset<<2));
break;
case DATA_TYPE_STRING: //<2F>ַ<EFBFBD><D6B7><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
if(!word_count)
{
return 1;
}
memcpy((char *)value, script_mod_buf + (sub_key->offset<<2), word_count << 2);
break;
case DATA_TYPE_GPIO_WORD: //<2F><>word<72><64><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
k = 0;
while(k < word_count)
{
value[k] = *(int *)(script_mod_buf + (sub_key->offset<<2) + (k<<2));
k ++;
}
break;
}
return SCRIPT_PARSER_OK;
}
return SCRIPT_PARSER_KEY_NOT_FIND;
}