#include #include #include #include #include #include #include #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; iitemNum; 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; imodelNum; 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;iitemNum; 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; iitemNum; 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; iitemNum; 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; jdummyByteNums; 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; imodelNum; 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; imodelNum; 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; imodelNum; 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; }