| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729 |
- #include <stdio.h>
- #include <stdlib.h>
- #include <regex.h>
- #include <sys/types.h>
- #include <sys/stat.h>
- #include <unistd.h>
- #include <string.h>
- #include "mt_mt.h"
- #include "mt_aq.h"
- #include "mt_pq.h"
- //static MultiTable_t *gMT;
- static regex_t regex_comment;
- static regex_t regex;
- static unsigned int currLine = 0;
- void ReleaseSubTableItem(Item_t *pItem)
- {
- if (pItem != NULL)
- {
- if (pItem->filepath != NULL)
- {
- free(pItem->filepath);
- pItem->filepath = NULL;
- }
- if (pItem->pData != NULL)
- {
- free(pItem->pData);
- }
- memset(pItem, 0, sizeof(Item_t));
- }
- }
- void ReleaseSubTable(SubTable_t *pSubTable)
- {
- if (pSubTable != NULL)
- {
- int i;
- for (i=0; i<pSubTable->itemNum; i++)
- {
- if(pSubTable->itemList[i] != NULL)
- {
- ReleaseSubTableItem(pSubTable->itemList[i]);
- free(pSubTable->itemList[i]);
- }
- }
- memset(pSubTable, 0, sizeof(SubTable_t));
- }
- }
- void ReleaseMultiTable(MultiTable_t *pMT)
- {
- if (pMT != NULL)
- {
- int i;
- for (i=0; i<pMT->modelNum; i++)
- {
- if(pMT->modelList[i] != NULL)
- {
- ReleaseSubTable(pMT->modelList[i]);
- free(pMT->modelList[i]);
- }
- }
- memset(pMT, 0, sizeof(SubTable_t));
- }
- }
- int GetValue(char *buf, char *StringID, char *StringValue)
- {
- char *pch;
- int retval = -1;
- regmatch_t match[2];
- retval = regexec(®ex, buf, 1, match, 0);
- if( !retval ){
- //puts("Match");
- //printf("==>%s", buf);
- }
- else if( retval == REG_NOMATCH ){
- //puts("No match");
- //printf("==>%s", buf);
- }
- else{
- //regerror(reti, ®ex, msgbuf, sizeof(msgbuf));
- //fprintf(stderr, "Error at %s line %d\n", filename, currLine);
- }
- pch = strtok (buf," =");
- if (pch && strcmp(pch,StringID) == 0)
- {
- //printf ("%s=\n",pch);
- pch = strtok (NULL, " =\"");
- //printf ("\"%s\"\n",pch);
- strcpy(StringValue, pch);
- retval = 0;
- }
- else
- {
- retval = -1;
- }
- return retval;
- }
- int SubTable_AddItem(SubTable_t *pSubTable, char *itemName, char *itemFile)
- {
- int retval = 0;
- int currItemIndex;
- if (pSubTable == NULL)
- {
- return -1;
- }
- if (itemName == NULL)
- {
- return -1;
- }
- if (itemFile == NULL)
- {
- return -1;
- }
- currItemIndex = pSubTable->itemNum;
- pSubTable->itemList[currItemIndex] = malloc(sizeof(Item_t));
- if (pSubTable->itemList[currItemIndex] == NULL)
- {
- retval = -1;
- }
- else
- {
- pSubTable->itemNum++;
- memset(pSubTable->itemList[currItemIndex], 0 , sizeof(Item_t));
- strcpy(pSubTable->itemList[currItemIndex]->name, itemName);
- //check error
- pSubTable->itemList[currItemIndex]->filepath = malloc(strlen(itemFile)+1);
- if (pSubTable->itemList[currItemIndex]->filepath == NULL)
- {
- fprintf(stderr, "%s %d malloc error\n", __FUNCTION__, __LINE__);
- pSubTable->itemNum--;
- retval = -1;
- }
- else
- {
- strcpy(pSubTable->itemList[currItemIndex]->filepath, itemFile);
- }
- }
- return retval;
- }
- int parseItem(Item_t *pItem)
- {
- int retval = 0;
- if (pItem != NULL)
- {
- if (strcmp(pItem->name, "AQ_OSD_TAB") == 0)
- {
- retval = parseAQ(pItem);
- }
- else if (strcmp(pItem->name, "PQ_OSD_TAB") == 0)
- {
- retval = parsePQ(pItem);
- }
- else if (strcmp(pItem->name, "COLOR_TMP_TAB") == 0)
- {
- retval = parsePQ(pItem);
- }
- else
- {
- retval = -1;
- }
- }
- return retval;
- }
- int parseSubTable(SubTable_t *pSubTable)
- {
- int retval = 0;
- int i;
- if (pSubTable == NULL)
- {
- return -1;
- }
- for (i=0;i<pSubTable->itemNum; i++)
- {
- retval = parseItem(pSubTable->itemList[i]);
- if (retval == -1)
- {
- fprintf(stderr, "%s %d parse Item %d fail\n", __FUNCTION__, __LINE__, i);
- break;
- }
- }
- return retval;
- }
- MultiTable_t *ParseMultiTableINI(char *filename)
- {
- FILE *fp = NULL;
- MultiTable_t *pMT = NULL;
- int retval;
- char buf[LINEMAX];
- regmatch_t match[2];
- int step = 0;
- char StringValue[256];
- char ItemNameStr[256];
- int currModelIndex=0;
- if (filename == NULL)
- {
- fprintf(stderr, "%s filename == NULL\n", __FUNCTION__);
- return NULL;
- }
- printf("Read ini from %s\n", filename);
- fp = fopen(filename, "rb");
- if (fp == NULL)
- {
- fprintf(stderr, "%s Can't open file %s\n", __FUNCTION__, filename);
- return NULL;
- }
- /* Compile regular expression */
- //retval = regcomp(®ex, "^(TableName|DefaultModel|ModelName|ItemName|ItemFile)+[ \t]*=[ \t]*\"[a-zA-Z0-9._/\\]*\"[ \t]*", 0);
- retval = regcomp(®ex, "[a-zA-Z][ \t]*=[ \t]*\"[a-zA-Z0-9._/\\]*\"[ \t]*", 0);
- if(retval)
- {
- fprintf(stderr, "Could not compile regex\n");
- fclose(fp);
- return NULL;
- }
- retval = regcomp(®ex_comment, "^[ \t]*#[.]*", 0);
- if(retval)
- {
- regfree(®ex);
- fclose(fp);
- fprintf(stderr, "Could not compile regex\n");
- return NULL;
- }
- pMT = (MultiTable_t *)malloc(sizeof(MultiTable_t));
- if (pMT == NULL)
- {
- regfree(®ex);
- regfree(®ex_comment);
- fclose(fp);
- fprintf(stderr, "MultiTable_t malloc fail\n");
- return NULL;
- }
- memset(pMT, 0, sizeof(MultiTable_t));
- currLine = 0;
- while( (fgets (buf , LINEMAX , fp) != NULL))
- {
- currLine++;
- retval = regexec(®ex_comment, buf, 1, match, 0);
- if( !retval ){
- //puts("Match comment");
- //printf("==>%s", buf);
- }
- else if( retval == REG_NOMATCH ){
- //puts("No match");
- //printf("==>%s", buf);
- //printf("step %d\n", step);
- switch (step)
- {
- case 0:
- retval=GetValue(buf, "TableName", StringValue);
- if (retval == 0)
- {
- if (strlen(StringValue) < TABLENAMELEN)
- {
- strcpy(pMT->name,StringValue);
- }
- else
- {
- fprintf(stderr, "Tablename too long, max len is %d\n", TABLENAMELEN);
- memcpy(pMT->name,StringValue, TABLENAMELEN);
- pMT->name[TABLENAMELEN]='\0';
- }
- }
- else
- {
- fprintf(stderr, "Multitable file expect string: TableName=\"abcd\"\n");
- }
- break;
- case 1:
- retval=GetValue(buf, "DefaultModel", StringValue);
- if (retval == 0)
- {
- if (strlen(StringValue) < MODELNAMELEN)
- {
- strcpy(pMT->defaultModelName,StringValue);
- }
- else
- {
- fprintf(stderr, "DefaultModelName too long, max len is %d\n", MODELNAMELEN);
- memcpy(pMT->defaultModelName,StringValue, MODELNAMELEN);
- pMT->defaultModelName[MODELNAMELEN]='\0';
- }
- }
- else
- {
- fprintf(stderr,"Multitable file expect string: DefaultModel=\"abcd\"\n");
- }
- break;
- case 2:
- retval=GetValue(buf, "ModelName", StringValue);
- if (retval == 0)
- {
- SubTable_t *pSubTable = NULL;
- pMT->modelNum++;
- currModelIndex = pMT->modelNum-1;
- pSubTable = (SubTable_t*)malloc(sizeof(SubTable_t));
- if (pSubTable == NULL)
- {
- fprintf(stderr, "Unable to allocate SubTable_t memory %s %d\n", __FUNCTION__, __LINE__);
- retval = -1;
- }
- else
- {
- memset(pSubTable, 0, sizeof(SubTable_t));
- pMT->modelList[currModelIndex] = pSubTable;
- if (strlen(StringValue) < MODELNAMELEN)
- {
- strcpy(pSubTable->modelName,StringValue);
- }
- else
- {
- fprintf(stderr, "ModelName too long, max len is %d\n", MODELNAMELEN);
- memcpy(pSubTable->modelName,StringValue, MODELNAMELEN);
- pSubTable->modelName[MODELNAMELEN]='\0';
- }
- }
- }
- else
- {
- fprintf(stderr, "Multitable file expect string: DefaultModel=\"abcd\"\n");
- }
- break;
- case 3:
- case 5:
- case 7:
- retval=GetValue(buf, "ItemName", ItemNameStr);
- break;
- case 4:
- case 6:
- case 8:
- retval=GetValue(buf, "ItemFile", StringValue);
- if (retval == 0)
- {
- SubTable_AddItem(pMT->modelList[currModelIndex], ItemNameStr, StringValue);
- }
- break;
- }
- if (retval == 0)
- {
- step++;
- }
- else
- {
- //Error happen break
- fprintf(stderr, "Error at %s line %d\n", filename, currLine);
- break;
- }
- if (step >= 9)
- {
- break;
- }
- }
- else{
- //regerror(reti, ®ex, msgbuf, sizeof(msgbuf));
- fprintf(stderr, "Error at %s line %d\n", filename, currLine);
- fclose(fp);
- regfree(®ex);
- regfree(®ex_comment);
- free(pMT);
- pMT = NULL;
- return NULL;
- }
- }
- if (step != 9)
- {
- fprintf(stderr, "Error Multitable ini missing item\n");
- free(pMT);
- pMT = NULL;
- }
- fclose(fp);
- regfree(®ex);
- regfree(®ex_comment);
- return pMT;
- }
- int GenSubTableBuf(SubTable_t *pSubTable)
- {
- int i;
- Item_t *pItem = NULL;
- unsigned char *pOutBuf = NULL;
- if (pSubTable == NULL)
- {
- fprintf(stderr, "%s error, pSubTable == NULL\n", __FUNCTION__);
- return -1;
- }
- pOutBuf = malloc(MAXBINSIZE);
- if (pOutBuf== NULL)
- {
- fprintf(stderr, "%s error, pSubTable->pData malloc error!\n", __FUNCTION__);
- return -1;
- }
- pSubTable->pData = pOutBuf;
- pSubTable->subTableSize = 0;
- //fwrite(pSubTable->modelName, sizeof(char)*MODELNAMELEN, 1, fp);
- writeTempBuf(pSubTable->modelName, sizeof(char)*MODELNAMELEN, &pOutBuf);
- pSubTable->subTableSize += sizeof(char)*MODELNAMELEN;
- //fwrite(&pSubTable->subTableSize, sizeof(unsigned int), 1, fp);
- writeTempBuf(&pSubTable->subTableSize, sizeof(unsigned int), &pOutBuf);
- pSubTable->subTableSize += sizeof(unsigned int);
- //fwrite(&pSubTable->currTableStartOffset, sizeof(unsigned int), 1, fp);
- writeTempBuf(&pSubTable->currTableStartOffset, sizeof(unsigned int), &pOutBuf);
- pSubTable->subTableSize += sizeof(unsigned int);
- //fwrite(&pSubTable->itemNum, sizeof(unsigned int), 1, fp);
- writeTempBuf(&pSubTable->itemNum, sizeof(unsigned int), &pOutBuf);
- pSubTable->subTableSize += sizeof(unsigned int);
- //fwrite(&pSubTable->subTableNo, sizeof(unsigned int), 1, fp);
- writeTempBuf(&pSubTable->subTableNo, sizeof(unsigned int), &pOutBuf);
- pSubTable->subTableSize += sizeof(unsigned int);
- for (i=0; i<pSubTable->itemNum; i++)
- {
- pItem = pSubTable->itemList[i];
- if (i==0)
- {
- pItem->offset = 16+16+(16+8)*pSubTable->itemNum;
- }
- else
- {
- pItem->offset = pSubTable->itemList[i-1]->offset +
- pSubTable->itemList[i-1]->size + pSubTable->itemList[i-1]->dummyByteNums;
- }
- //fwrite(pItem->name, sizeof(char)*ITEMNAMELEN, 1, fp);
- writeTempBuf(pItem->name, sizeof(char)*ITEMNAMELEN, &pOutBuf);
- pSubTable->subTableSize += sizeof(char)*ITEMNAMELEN;
- //fwrite(&pItem->offset, sizeof(unsigned int), 1, fp);
- writeTempBuf(&pItem->offset, sizeof(unsigned int), &pOutBuf);
- pSubTable->subTableSize += sizeof(unsigned int);
- //fwrite(&pItem->size, sizeof(unsigned int), 1, fp);
- writeTempBuf(&pItem->size, sizeof(unsigned int), &pOutBuf);
- pSubTable->subTableSize += sizeof(unsigned int);
- }
- for (i=0; i<pSubTable->itemNum; i++)
- {
- pItem = pSubTable->itemList[i];
- //fwrite(pItem->pData, pItem->size, 1, fp);
- writeTempBuf(pItem->pData, pItem->size, &pOutBuf);
- pSubTable->subTableSize += pItem->size;
- //for 4 bytes aligment
- if (pItem->dummyByteNums != 0)
- {
- char dummy = 0;
- int j;
- for(j=0; j<pItem->dummyByteNums; j++)
- {
- //fwrite(&dummy, sizeof(char), 1, fp);
- writeTempBuf(&dummy,sizeof(char), &pOutBuf);
- }
- pSubTable->subTableSize += pItem->dummyByteNums;
- }
- }
- pOutBuf = pSubTable->pData + (sizeof (char)*MODELNAMELEN);
- writeTempBuf(&pSubTable->subTableSize, sizeof(unsigned int), &pOutBuf);
- //pSubTable->checksum = CalCulate_XOR_CheckSum(pSubTable->pData, pSubTable->subTableSize);
- return 0;
- }
- int writeMultiTable(MultiTable_t *pMTab, char *outputFilename)
- {
- FILE *fp = 0;
- unsigned char *pOutBuf = NULL;
- int i;
- if (pMTab == NULL)
- {
- fprintf(stderr, "%s Error! pMTab == NULL\n", __FUNCTION__);
- return -1;
- }
- pOutBuf = malloc(MAXMULTITABLEBINSIZE);
- if (pOutBuf== NULL)
- {
- fprintf(stderr, "%s error, pMTab->pData malloc error!\n", __FUNCTION__);
- return -1;
- }
- pMTab->pData = pOutBuf;
- pMTab->totalSize = 0;
- fp = fopen(outputFilename, "wb");
- if (fp == NULL)
- {
- fprintf(stderr, "Unable to write file %s\n", outputFilename);
- return -1;
- }
- for (i=0; i<pMTab->modelNum; i++)
- {
- pMTab->modelList[i]->subTableNo = i;
- if (i==0)
- {
- pMTab->subTableOffset[0] = 24 + 8*pMTab->modelNum;
- pMTab->modelList[0]->currTableStartOffset = pMTab->subTableOffset[0];
- }
- else
- {
- pMTab->subTableOffset[i] = pMTab->subTableOffset[i-1]+pMTab->modelList[i-1]->subTableSize;
- pMTab->modelList[i]->currTableStartOffset = pMTab->subTableOffset[i];
- }
- GenSubTableBuf(pMTab->modelList[i]);
- pMTab->modelList[i]->checksum = CalCulate_XOR_CheckSum(pMTab->modelList[i]->pData, pMTab->modelList[i]->subTableSize/sizeof(unsigned int));
- if (i==0)
- {
- pMTab->subTableOffset[0] = 24 + 8*pMTab->modelNum;
- }
- else
- {
- pMTab->subTableOffset[i] = pMTab->subTableOffset[i-1]+pMTab->modelList[i-1]->subTableSize;
- }
- }
- //fwrite(pMTab->name, sizeof(char)*TABLENAMELEN, 1, fp);
- writeTempBuf(pMTab->name, sizeof(char)*TABLENAMELEN, &pOutBuf);
- pMTab->totalSize += sizeof(char)*TABLENAMELEN;
- //fwrite(&pMTab->modelNum, sizeof(unsigned int), 1, fp);
- writeTempBuf(&pMTab->modelNum, sizeof(unsigned int), &pOutBuf);
- pMTab->totalSize += sizeof(unsigned int);
- //fwrite(&pMTab->totalSize, sizeof(unsigned int), 1, fp);
- writeTempBuf(&pMTab->totalSize, sizeof(unsigned int), &pOutBuf);
- pMTab->totalSize += sizeof(unsigned int);
- //fwrite(&pMTab->tableChecksum, sizeof(unsigned int), 1, fp);
- writeTempBuf(&pMTab->tableChecksum, sizeof(unsigned int), &pOutBuf);
- pMTab->totalSize += sizeof(unsigned int);
- for (i=0; i<pMTab->modelNum; i++)
- {
- //fwrite(&pMTab->subTableOffset[i], sizeof(unsigned int), 1, fp);
- writeTempBuf(&pMTab->subTableOffset[i], sizeof(unsigned int), &pOutBuf);
- pMTab->totalSize += sizeof(unsigned int);
- //fwrite(&pMTab->modelList[i]->checksum, sizeof(unsigned int), 1, fp);
- writeTempBuf(&pMTab->modelList[i]->checksum, sizeof(unsigned int), &pOutBuf);
- pMTab->totalSize += sizeof(unsigned int);
- }
- for (i=0; i<pMTab->modelNum; i++)
- {
- writeTempBuf(pMTab->modelList[i]->pData, pMTab->modelList[i]->subTableSize, &pOutBuf);
- pMTab->totalSize += pMTab->modelList[i]->subTableSize;
- }
- pOutBuf = pMTab->pData + 16; // point to Main Table total size
- writeTempBuf(&pMTab->totalSize, sizeof(unsigned int), &pOutBuf);
- pOutBuf = pMTab->pData + 24; // point to SubTable 0 start offset position
- pMTab->tableChecksum = CalCulate_XOR_CheckSum(pOutBuf, pMTab->modelNum*2);
- pOutBuf = pMTab->pData + 20; // point to Table check sum
- writeTempBuf(&pMTab->tableChecksum, sizeof(unsigned int), &pOutBuf);
- fwrite(pMTab->pData, pMTab->totalSize, 1, fp);
- fflush(fp);
- fclose(fp);
- return 0;
- }
- int writeMultiTableFlag(MultiTable_t *pMTab, char *outputFilename)
- {
- unsigned char updated = 0;
- FILE *fp;
-
- if (pMTab == NULL)
- {
- fprintf(stderr, "%s Error! pMTab == NULL\n", __FUNCTION__);
- return -1;
- }
- fp = fopen(outputFilename, "wb");
- if (fp == NULL)
- {
- fprintf(stderr, "Unable to write file %s\n", outputFilename);
- return -1;
- }
- fwrite(&updated, sizeof(unsigned char), 1, fp);
-
- fwrite(pMTab->defaultModelName, sizeof(char)*MODELNAMELEN, 1, fp);
- fflush(fp);
- fclose(fp);
- return 0;
-
- }
- int writeOutputBuf(void *pData, unsigned int size, unsigned char **pOutBuf, FILE *fp)
- {
- int retval =0;
- #if WRITEITEMTOFILE
- if (fp != NULL)
- {
- fwrite(pData, size, 1, fp);
- }
- else
- {
- fprintf(stderr, "%s %d error\n", __FUNCTION__, __LINE__);
- retval = -1;
- goto out;
- }
- #endif
- if (*pOutBuf != NULL)
- {
- memcpy(*pOutBuf,pData, size);
- *pOutBuf = *pOutBuf + size;
- }
- else
- {
- fprintf(stderr, "%s %d error\n", __FUNCTION__, __LINE__);
- retval = -1;
- }
- out:
- return retval;
- }
- int writeTempBuf(void *pData, unsigned int size, unsigned char **pOutBuf)
- {
- int retval =0;
- if (*pOutBuf != NULL)
- {
- memcpy(*pOutBuf,pData, size);
- *pOutBuf = *pOutBuf + size;
- }
- else
- {
- fprintf(stderr, "%s %d error\n", __FUNCTION__, __LINE__);
- retval = -1;
- }
- return retval;
- }
- unsigned int CalCulate_XOR_CheckSum(void *pBuffer, unsigned int size)
- {
- unsigned int idx = 0, checksum = 0;
- for (idx = 0; idx < size; idx++)
- {
- checksum = checksum ^ ((unsigned int *)pBuffer)[idx];
- }
- return checksum;
- }
|