Code Cleanup for duplicated code of getting text encoding type 59/217359/8 accepted/tizen/unified/20191114.042534 submit/tizen/20191113.015438
authorjiyong.min <jiyong.min@samsung.com>
Thu, 7 Nov 2019 05:01:52 +0000 (14:01 +0900)
committerjiyong.min <jiyong.min@samsung.com>
Tue, 12 Nov 2019 05:41:36 +0000 (14:41 +0900)
 - Add new funtion to get text encoding type

Change-Id: I33d11fa4f59c52f25b20b221bfe02a8a0bb5231d

utils/mm_file_util_tag.c

index af7c4871fb815c15592882af5079a770d794ec5c..2e20671c932c45ff03efd43d4b0d6810efcfb0e2 100644 (file)
@@ -2567,6 +2567,76 @@ static void __id3tag_skip_newline(unsigned char *pTagVal, int *nTagLen, int *off
        }
 }
 
+static int __id3tag_get_text_encoding_v222(unsigned char *pTagVal, int offset)
+{
+       if (pTagVal[offset - 1] == 0x00)
+               return AV_ID3V2_ISO_8859;
+
+       return AV_ID3V2_UTF16;
+}
+
+static int __id3tag_get_text_encoding_v223(unsigned char *pTagVal, int *npTagLen, int nTextEnc, int *offset)
+{
+       if ((IS_ENCODEDBY_UTF16(pTagVal + *offset) || IS_ENCODEDBY_UTF16_R(pTagVal + *offset)) && *npTagLen > 2) {
+               __id3tag_skip_newline(pTagVal, npTagLen, offset);
+
+               if (IS_ENCODEDBY_UTF16(pTagVal + *offset) && (*npTagLen > 2)) {
+                       *npTagLen -= 2;
+                       *offset += 2;
+                       return AV_ID3V2_UTF16;
+               } else if (IS_ENCODEDBY_UTF16_R(pTagVal + *offset) && (*npTagLen > 2)) {
+                       *npTagLen -= 2;
+                       *offset += 2;
+                       return AV_ID3V2_UTF16_BE;
+               } else if (IS_ENCODEDBY_UTF16(pTagVal + *offset + 1) && (*npTagLen > 3)) {
+                       *npTagLen -= 3;
+                       *offset += 3;
+                       return AV_ID3V2_UTF16;
+               } else if (IS_ENCODEDBY_UTF16_R(pTagVal + *offset + 1)  && (*npTagLen > 3)) {
+                       *npTagLen -= 3;
+                       *offset += 3;
+                       return AV_ID3V2_UTF16_BE;
+               } else {
+                       debug_msg(RELEASE, "id3tag never get here!!\n");
+                       return nTextEnc;        /* default bypass */
+               }
+       } else {
+               while ((pTagVal[*offset] < 0x20) && (*offset < *npTagLen)) { /* text string encoded by ISO-8859-1 */
+                       (*npTagLen)--;
+                       (*offset)++;
+               }
+               return AV_ID3V2_ISO_8859;
+       }
+}
+
+static int __id3tag_get_text_encoding_v224(unsigned char *pTagVal, int *npTagLen, int nTextEnc, int *offset)
+{
+       if (nTextEnc == AV_ID3V2_UTF16 || nTextEnc == AV_ID3V2_UTF16_BE) {
+               __id3tag_skip_newline(pTagVal, npTagLen, offset);
+
+               if ((IS_ENCODEDBY_UTF16(pTagVal + *offset) || IS_ENCODEDBY_UTF16_R(pTagVal + *offset)) && *npTagLen > 2) {
+                       *npTagLen -= 2;
+                       *offset += 2;
+                       return AV_ID3V2_UTF16;
+               } else {
+                       debug_msg(RELEASE, "id3tag never get here!!\n");
+                       return nTextEnc;        /* default bypass */
+               }
+       } else if (nTextEnc == AV_ID3V2_UTF8) {
+               while (pTagVal[*offset] < 0x20 && (*offset < *npTagLen)) { /* text string encoded by UTF-8 */
+                       (*npTagLen)--;
+                       (*offset)++;
+               }
+               return AV_ID3V2_UTF8;
+       } else {
+               while (pTagVal[*offset] < 0x20 && (*offset < *npTagLen)) { /* text string encoded by ISO-8859-1 */
+                       (*npTagLen)--;
+                       (*offset)++;
+               }
+               return AV_ID3V2_ISO_8859;
+       }
+}
+
 static void __id3tag_parse_SYLT(AvFileContentInfo *pInfo, unsigned char *pTagVal, int nTagLen, const char *pCharSet, int textEncodingType, int offset)
 {
        int idx = 0;
@@ -3020,10 +3090,7 @@ bool mm_file_id3tag_parse_v222(AvFileContentInfo *pInfo, unsigned char *buffer)
 
                                                                /*pExtContent[tmp+1] value should't have encoding value */
                                                                if (pExtContent[tmp] > 0x20 && (pExtContent[tmp - 1] == 0x00 || pExtContent[tmp - 1] == 0x01)) {
-                                                                       if (pExtContent[tmp - 1] == 0x00)
-                                                                               textEncodingType = AV_ID3V2_ISO_8859;
-                                                                       else
-                                                                               textEncodingType = AV_ID3V2_UTF16;
+                                                                       textEncodingType = __id3tag_get_text_encoding_v222(pExtContent, tmp);
 
                                                                        pInfo->pComment = mmfile_string_convert((char *)&pExtContent[tmp], realCpyFrameNum, "UTF-8", charset_array[textEncodingType], NULL, (unsigned int *)&pInfo->commentLen);
                                                                        debug_msg(RELEASE, "pInfo->pComment returned = (%s), pInfo->commentLen(%d)\n", pInfo->pComment, pInfo->commentLen);
@@ -3057,10 +3124,7 @@ bool mm_file_id3tag_parse_v222(AvFileContentInfo *pInfo, unsigned char *buffer)
 
                                                                /*pExtContent[tmp+1] value should't have null value */
                                                                if (pExtContent[tmp] > 0x20 && (pExtContent[tmp - 1] == 0x00 || pExtContent[tmp - 1] == 0x01)) {
-                                                                       if (pExtContent[tmp - 1] == 0x00)
-                                                                               textEncodingType = AV_ID3V2_ISO_8859;
-                                                                       else
-                                                                               textEncodingType = AV_ID3V2_UTF16;
+                                                                       textEncodingType = __id3tag_get_text_encoding_v222(pExtContent, tmp);
 
                                                                        pInfo->pURL = mmfile_string_convert((const char *)pExtContent, realCpyFrameNum, "UTF-8", charset_array[textEncodingType], NULL, (unsigned int *)&pInfo->urlLen);
                                                                        debug_msg(RELEASE, "pInfo->pURL returned = (%s), pInfo->urlLen(%d)\n", pInfo->pURL, pInfo->urlLen);
@@ -3282,35 +3346,7 @@ bool mm_file_id3tag_parse_v223(AvFileContentInfo *pInfo, unsigned char *buffer)
 
                                                                /*pExtContent[tmp+1] value should't have encoding value */
                                                                if (pExtContent[tmp] == 0x00 || pExtContent[tmp] == 0xFF || pExtContent[tmp] == 0xFE) {
-                                                                       if ((IS_ENCODEDBY_UTF16(pExtContent + tmp) || IS_ENCODEDBY_UTF16_R(pExtContent + tmp)) && realCpyFrameNum > 2) {
-                                                                               __id3tag_skip_newline(pExtContent, &realCpyFrameNum, &tmp);
-
-                                                                               if (IS_ENCODEDBY_UTF16(pExtContent + tmp) && (realCpyFrameNum > 2)) {
-                                                                                       realCpyFrameNum -= 2;
-                                                                                       tmp += 2;
-                                                                                       textEncodingType = AV_ID3V2_UTF16;
-                                                                               } else if (IS_ENCODEDBY_UTF16_R(pExtContent + tmp) && (realCpyFrameNum > 2)) {
-                                                                                       realCpyFrameNum -= 2;
-                                                                                       tmp += 2;
-                                                                                       textEncodingType = AV_ID3V2_UTF16_BE;
-                                                                               } else if (IS_ENCODEDBY_UTF16(pExtContent + tmp + 1) && (realCpyFrameNum > 3)) {
-                                                                                       realCpyFrameNum -= 3;
-                                                                                       tmp += 3;
-                                                                                       textEncodingType = AV_ID3V2_UTF16;
-                                                                               } else if (IS_ENCODEDBY_UTF16_R(pExtContent + tmp + 1)  && (realCpyFrameNum > 3)) {
-                                                                                       realCpyFrameNum -= 3;
-                                                                                       tmp += 3;
-                                                                                       textEncodingType = AV_ID3V2_UTF16_BE;
-                                                                               } else {
-                                                                                       debug_msg(RELEASE, "pInfo->pComment Never Get Here!!\n");
-                                                                               }
-                                                                       } else {
-                                                                               while ((pExtContent[tmp] < 0x20) && (tmp < realCpyFrameNum)) { /* text string encoded by ISO-8859-1 */
-                                                                                       realCpyFrameNum--;
-                                                                                       tmp++;
-                                                                               }
-                                                                               textEncodingType = AV_ID3V2_ISO_8859;
-                                                                       }
+                                                                       textEncodingType = __id3tag_get_text_encoding_v223(pExtContent, &realCpyFrameNum, textEncodingType, &tmp);
 
                                                                        debug_msg(RELEASE, "tmp(%d) textEncodingType(%d), realCpyFrameNum(%d)\n", tmp, textEncodingType, realCpyFrameNum);
                                                                        pInfo->pComment = mmfile_string_convert((const char *)&pExtContent[tmp], realCpyFrameNum, "UTF-8", charset_array[textEncodingType], NULL, (unsigned int *)&pInfo->commentLen);
@@ -3333,35 +3369,7 @@ bool mm_file_id3tag_parse_v223(AvFileContentInfo *pInfo, unsigned char *buffer)
 
                                                                /*pExtContent[tmp+1] value should't have encoding value */
                                                                if (pExtContent[tmp] == 0x00 || pExtContent[tmp] == 0xFF || pExtContent[tmp] == 0xFE) {
-                                                                       if ((IS_ENCODEDBY_UTF16(pExtContent + tmp) || IS_ENCODEDBY_UTF16_R(pExtContent + tmp)) && realCpyFrameNum > 2) {
-                                                                               __id3tag_skip_newline(pExtContent, &realCpyFrameNum, &tmp);
-
-                                                                               if (IS_ENCODEDBY_UTF16(pExtContent + tmp) && (realCpyFrameNum > 2)) {
-                                                                                       realCpyFrameNum -= 2;
-                                                                                       tmp += 2;
-                                                                                       textEncodingType = AV_ID3V2_UTF16;
-                                                                               } else if (IS_ENCODEDBY_UTF16_R(pExtContent + tmp) && (realCpyFrameNum > 2)) {
-                                                                                       realCpyFrameNum -= 2;
-                                                                                       tmp += 2;
-                                                                                       textEncodingType = AV_ID3V2_UTF16_BE;
-                                                                               } else if (IS_ENCODEDBY_UTF16(pExtContent + tmp + 1) && (realCpyFrameNum > 3)) {
-                                                                                       realCpyFrameNum -= 3;
-                                                                                       tmp += 3;
-                                                                                       textEncodingType = AV_ID3V2_UTF16;
-                                                                               } else if (IS_ENCODEDBY_UTF16_R(pExtContent + tmp + 1)  && (realCpyFrameNum > 3)) {
-                                                                                       realCpyFrameNum -= 3;
-                                                                                       tmp += 3;
-                                                                                       textEncodingType = AV_ID3V2_UTF16_BE;
-                                                                               } else {
-                                                                                       debug_msg(RELEASE, "pInfo->pSyncLyrics Never Get Here!!\n");
-                                                                               }
-                                                                       } else {
-                                                                               while ((pExtContent[tmp] < 0x20) && (tmp < realCpyFrameNum)) { /* text string encoded by ISO-8859-1 */
-                                                                                       realCpyFrameNum--;
-                                                                                       tmp++;
-                                                                               }
-                                                                               textEncodingType = AV_ID3V2_ISO_8859;
-                                                                       }
+                                                                       textEncodingType = __id3tag_get_text_encoding_v223(pExtContent, &realCpyFrameNum, textEncodingType, &tmp);
 
                                                                        debug_msg(RELEASE, "tmp(%d) textEncodingType(%d), realCpyFrameNum(%d)\n", tmp, textEncodingType, realCpyFrameNum);
 
@@ -3404,35 +3412,7 @@ bool mm_file_id3tag_parse_v223(AvFileContentInfo *pInfo, unsigned char *buffer)
                                                                debug_msg(RELEASE, "tpExtContent[%d] %x\n", tmp, pExtContent[tmp]);
 
                                                                if (pExtContent[tmp] == 0x00 || pExtContent[tmp] == 0xFF || pExtContent[tmp] == 0xFE) {
-                                                                       if ((IS_ENCODEDBY_UTF16(pExtContent + tmp) || IS_ENCODEDBY_UTF16_R(pExtContent + tmp)) && realCpyFrameNum > 2) {
-                                                                               __id3tag_skip_newline(pExtContent, &realCpyFrameNum, &tmp);
-
-                                                                               if (IS_ENCODEDBY_UTF16(pExtContent + tmp) && (realCpyFrameNum > 2)) {
-                                                                                       realCpyFrameNum -= 2;
-                                                                                       tmp += 2;
-                                                                                       textEncodingType = AV_ID3V2_UTF16;
-                                                                               } else if (IS_ENCODEDBY_UTF16_R(pExtContent + tmp) && (realCpyFrameNum > 2)) {
-                                                                                       realCpyFrameNum -= 2;
-                                                                                       tmp += 2;
-                                                                                       textEncodingType = AV_ID3V2_UTF16_BE;
-                                                                               } else if (IS_ENCODEDBY_UTF16(pExtContent + tmp + 1) && (realCpyFrameNum > 3)) {
-                                                                                       realCpyFrameNum -= 3;
-                                                                                       tmp += 3;
-                                                                                       textEncodingType = AV_ID3V2_UTF16;
-                                                                               } else if (IS_ENCODEDBY_UTF16_R(pExtContent + tmp + 1)  && (realCpyFrameNum > 3)) {
-                                                                                       realCpyFrameNum -= 3;
-                                                                                       tmp += 3;
-                                                                                       textEncodingType = AV_ID3V2_UTF16_BE;
-                                                                               } else {
-                                                                                       debug_msg(RELEASE, "pInfo->pUnsyncLyrics Never Get Here!!\n");
-                                                                               }
-                                                                       } else {
-                                                                               while ((pExtContent[tmp] < 0x20) && (tmp < realCpyFrameNum)) { /* text string encoded by ISO-8859-1 */
-                                                                                       realCpyFrameNum--;
-                                                                                       tmp++;
-                                                                               }
-                                                                               textEncodingType = AV_ID3V2_ISO_8859;
-                                                                       }
+                                                                       textEncodingType = __id3tag_get_text_encoding_v223(pExtContent, &realCpyFrameNum, textEncodingType, &tmp);
 
                                                                        char *char_set = NULL;
 
@@ -3711,30 +3691,7 @@ bool mm_file_id3tag_parse_v224(AvFileContentInfo *pInfo, unsigned char *buffer)
                                                                realCpyFrameNum -= 3;
                                                                tmp = 3;
 
-                                                               if (textEncodingType == AV_ID3V2_UTF16 || textEncodingType == AV_ID3V2_UTF16_BE) {
-                                                                       __id3tag_skip_newline(pExtContent, &realCpyFrameNum, &tmp);
-
-                                                                       if ((IS_ENCODEDBY_UTF16(pExtContent + tmp) || IS_ENCODEDBY_UTF16_R(pExtContent + tmp)) && realCpyFrameNum > 2) {
-                                                                               realCpyFrameNum -= 2;
-                                                                               tmp += 2;
-                                                                               textEncodingType = AV_ID3V2_UTF16;
-                                                                       } else {
-                                                                               debug_msg(RELEASE, "pInfo->pComment Never Get Here!!\n");
-                                                                       }
-                                                               } else if (textEncodingType == AV_ID3V2_UTF8) {
-                                                                       while (pExtContent[tmp] < 0x20 && (tmp < realCpyFrameNum)) { /* text string encoded by ISO-8859-1 */
-                                                                               realCpyFrameNum--;
-                                                                               tmp++;
-                                                                       }
-                                                                       textEncodingType = AV_ID3V2_UTF8;
-                                                               } else {
-                                                                       while (pExtContent[tmp] < 0x20 && (tmp < realCpyFrameNum)) { /* text string encoded by ISO-8859-1 */
-                                                                               realCpyFrameNum--;
-                                                                               tmp++;
-                                                                       }
-                                                                       textEncodingType = AV_ID3V2_ISO_8859;
-                                                               }
-
+                                                               textEncodingType = __id3tag_get_text_encoding_v224(pExtContent, &realCpyFrameNum, textEncodingType, &tmp);
                                                                debug_msg(RELEASE, "tmp(%d) textEncodingType(%d), realCpyFrameNum(%d)\n", tmp, textEncodingType, realCpyFrameNum);
 
                                                                pInfo->pComment = _mm_file_string_convert_v224((const char *)&pExtContent[tmp], realCpyFrameNum, textEncodingType, charset_array[textEncodingType], &pInfo->commentLen);
@@ -3751,30 +3708,7 @@ bool mm_file_id3tag_parse_v224(AvFileContentInfo *pInfo, unsigned char *buffer)
                                                                realCpyFrameNum -= 5;
                                                                tmp = 5;
 
-                                                               if (textEncodingType == AV_ID3V2_UTF16 || textEncodingType == AV_ID3V2_UTF16_BE) {
-                                                                       __id3tag_skip_newline(pExtContent, &realCpyFrameNum, &tmp);
-
-                                                                       if ((IS_ENCODEDBY_UTF16(pExtContent + tmp) || IS_ENCODEDBY_UTF16_R(pExtContent + tmp)) && realCpyFrameNum > 2) {
-                                                                               realCpyFrameNum -= 2;
-                                                                               tmp += 2;
-                                                                               textEncodingType = AV_ID3V2_UTF16;
-                                                                       } else {
-                                                                               debug_msg(RELEASE, "pInfo->pSyncLyrics Never Get Here!!\n");
-                                                                       }
-                                                               } else if (textEncodingType == AV_ID3V2_UTF8) {
-                                                                       while (pExtContent[tmp] < 0x20 && (tmp < realCpyFrameNum)) { /* text string encoded by ISO-8859-1 */
-                                                                               realCpyFrameNum--;
-                                                                               tmp++;
-                                                                       }
-                                                                       textEncodingType = AV_ID3V2_UTF8;
-                                                               } else {
-                                                                       while (pExtContent[tmp] < 0x20 && (tmp < realCpyFrameNum)) { /* text string encoded by ISO-8859-1 */
-                                                                               realCpyFrameNum--;
-                                                                               tmp++;
-                                                                       }
-                                                                       textEncodingType = AV_ID3V2_ISO_8859;
-                                                               }
-
+                                                               textEncodingType = __id3tag_get_text_encoding_v224(pExtContent, &realCpyFrameNum, textEncodingType, &tmp);
                                                                debug_msg(RELEASE, "tmp(%d) textEncodingType(%d), realCpyFrameNum(%d)\n", tmp, textEncodingType, realCpyFrameNum);
 
                                                                __id3tag_parse_SYLT(pInfo, pExtContent, realCpyFrameNum, charset_array[textEncodingType], textEncodingType, tmp);
@@ -3789,30 +3723,7 @@ bool mm_file_id3tag_parse_v224(AvFileContentInfo *pInfo, unsigned char *buffer)
                                                                realCpyFrameNum -= 3;
                                                                tmp = 3;
 
-                                                               if (textEncodingType == AV_ID3V2_UTF16 || textEncodingType == AV_ID3V2_UTF16_BE) {
-                                                                       __id3tag_skip_newline(pExtContent, &realCpyFrameNum, &tmp);
-
-                                                                       if ((IS_ENCODEDBY_UTF16(pExtContent + tmp) || IS_ENCODEDBY_UTF16_R(pExtContent + tmp)) && realCpyFrameNum > 2) {
-                                                                               realCpyFrameNum -= 2;
-                                                                               tmp += 2;
-                                                                               textEncodingType = AV_ID3V2_UTF16;
-                                                                       } else {
-                                                                               debug_msg(RELEASE, "pInfo->pUnsyncLyrics Never Get Here!!\n");
-                                                                       }
-                                                               } else if (textEncodingType == AV_ID3V2_UTF8) {
-                                                                       while (pExtContent[tmp] < 0x20 && (tmp < realCpyFrameNum)) { /* text string encoded by ISO-8859-1 */
-                                                                               realCpyFrameNum--;
-                                                                               tmp++;
-                                                                       }
-                                                                       textEncodingType = AV_ID3V2_UTF8;
-                                                               } else {
-                                                                       while (pExtContent[tmp] < 0x20 && (tmp < realCpyFrameNum)) { /* text string encoded by ISO-8859-1 */
-                                                                               realCpyFrameNum--;
-                                                                               tmp++;
-                                                                       }
-                                                                       textEncodingType = AV_ID3V2_ISO_8859;
-                                                               }
-
+                                                               textEncodingType = __id3tag_get_text_encoding_v224(pExtContent, &realCpyFrameNum, textEncodingType, &tmp);
                                                                debug_msg(RELEASE, "tmp(%d) textEncodingType(%d), realCpyFrameNum(%d)\n", tmp, textEncodingType, realCpyFrameNum);
 
                                                                pInfo->pUnsyncLyrics = _mm_file_string_convert_v224((const char *)&pExtContent[tmp], realCpyFrameNum, textEncodingType, charset_array[textEncodingType], &pInfo->unsynclyricsLen);