3 * Copyright (c) 2000-2012 Samsung Electronics Co., Ltd. All Rights Reserved.
5 * This file is part of msg-service.
7 * Contact: Jaeyun Jeong <jyjeong@samsung.com>
8 * Sangkoo Kim <sangkoo.kim@samsung.com>
9 * Seunghwan Lee <sh.cat.lee@samsung.com>
10 * SoonMin Jung <sm0415.jung@samsung.com>
11 * Jae-Young Lee <jy4710.lee@samsung.com>
12 * KeeBum Kim <keebum.kim@samsung.com>
14 * PROPRIETARY/CONFIDENTIAL
16 * This software is the confidential and proprietary information of
17 * SAMSUNG ELECTRONICS ("Confidential Information"). You shall not
18 * disclose such Confidential Information and shall use it only in
19 * accordance with the terms of the license agreement you entered
20 * into with SAMSUNG ELECTRONICS.
22 * SAMSUNG make no representations or warranties about the suitability
23 * of the software, either express or implied, including but not limited
24 * to the implied warranties of merchantability, fitness for a particular
25 * purpose, or non-infringement. SAMSUNG shall not be liable for any
26 * damages suffered by licensee as a result of using, modifying or
27 * distributing this software or its derivatives.
32 #include "MsgDrmWrapper.h"
33 #include "MsgUtilFile.h"
41 #include <sys/types.h>
47 #include "drm-service.h"
51 DRM_RIGHTS_CONSUME_HANDLE hRightsConsume;
52 MSG_DRM_TYPE eDRMType; //to have a drm type
53 char *szOpenedDRMFileName;
54 } MSG_OPENEDDRM_INFO_T;
57 #define MSG_MAX_DRM_FILE_PATH MSG_FILEPATH_LEN_MAX
60 bool MsgDrmRegisterFile(MSG_DRM_OPENMODE eMode, char *pBuffer, int nSize)
62 if (eMode == MSG_MODE_STREAM) {
63 MSG_DEBUG("Fail(eMode == MSG_MODE_STREAM)");
67 if (pBuffer == NULL) {
68 MSG_DEBUG("[Error] pBuffer is NULL");
72 MSG_DEBUG("buffer = %s, nSize = %d", pBuffer, nSize);
74 if (!drm_svc_is_drm_file(pBuffer)) { // Check whether DRM file or not
75 MSG_DEBUG("file is not drm file");
79 DRM_RESULT eDRMResult = drm_svc_register_file(pBuffer); // Register a DCF file
80 if (DRM_RESULT_SUCCESS != eDRMResult) {
81 MSG_DEBUG("drm_svc_register_file is failed : %d", eDRMResult);
88 bool MsgDrmUnregisterFile(char *szFilename)
90 if (szFilename == NULL) {
91 MSG_DEBUG("[Error] szFilename is NULL");
95 MSG_DEBUG("szFilename = %s", szFilename);
97 DRM_RESULT eDRMResult = drm_svc_unregister_file(szFilename, DRM_TRUE); // Unregister a DCF file
98 if (DRM_RESULT_SUCCESS != eDRMResult) {
99 MSG_DEBUG("drm_svc_unregister_file : %d", eDRMResult);
106 bool MsgDrmIsDrmFile(const char *szFilePath)
108 if (drm_svc_is_drm_file(szFilePath) == DRM_FALSE) {
109 MSG_DEBUG("file is not drm file");
116 /*Added to convert the .dm files in to .dcf files since our platform supports only .dcf :: Start*/
117 bool MsgDrmConvertDmtoDcfType(char *inputFile, char *outputFile)
119 if ((NULL == inputFile) || (NULL == outputFile)) {
120 MSG_DEBUG("Invalid Input parameters");
124 if (strstr(inputFile, ".dm")) {
125 MSG_DEBUG("Current File extension is .dm %s", inputFile);
127 DRM_CONVERT_HANDLE hConvert = NULL;
128 unsigned long written;
130 FILE *fp = MsgOpenFile(inputFile, "rb");//Check fp
133 MSG_DEBUG("[File Open Fail(Errno=%d)][ErrStr=%s]", errno, strerror(errno));
134 strncpy(outputFile, inputFile, MSG_MAX_DRM_FILE_PATH);
138 if (MsgFseek(fp, 0L, SEEK_END) < 0) {
140 MSG_DEBUG("MsgFseek() returns negative value!!!");
143 long retVal = MsgFtell(fp);
147 MSG_DEBUG("ftell() returns negative value: [%ld]!!!", retVal);
148 strncpy(outputFile, inputFile, MSG_MAX_DRM_FILE_PATH);
152 unsigned long bufLen = retVal;
153 MSG_DEBUG("fopen buffer len = %d", bufLen);
154 if (MsgFseek(fp, 0, SEEK_SET) < 0) {
156 MSG_DEBUG("MsgFseek() returns negative value!!!");
160 char *buffer = (char *)malloc(bufLen);
162 int pathLen = strlen(inputFile);
164 if (buffer == NULL) {
166 MSG_DEBUG("malloc is failed ");
167 strncpy(outputFile, inputFile, MSG_MAX_DRM_FILE_PATH);
171 strncpy(outputFile, inputFile, pathLen - 2);
172 strncat(outputFile, "dcf", 3);
174 readed_size = MsgReadFile(buffer, 1, bufLen, fp);//Check for error
175 MSG_DEBUG("fread read size = %d", readed_size);
176 if (readed_size == 0) {
179 MSG_DEBUG("MsgReadFile returns 0");
183 ret = drm_svc_open_convert(outputFile, DRM_TRUE, &hConvert);//Check return value
184 if (ret != DRM_RESULT_SUCCESS) {
187 MSG_DEBUG("drm_svc_open_convert() return = failed (%d)", ret);
188 strncpy(outputFile, inputFile, MSG_MAX_DRM_FILE_PATH);
192 /*We can call drm_svc_write_convert in loop if file size is large*/
193 ret = drm_svc_write_convert(hConvert, (unsigned char *)buffer, bufLen, &written);//check for error
194 if (ret != DRM_RESULT_SUCCESS) {
197 MSG_DEBUG("drm_svc_write_convert() return = failed (%d)", ret);
198 strncpy(outputFile, inputFile, MSG_MAX_DRM_FILE_PATH);
202 ret = drm_svc_close_convert(hConvert);//check for error
203 if (ret != DRM_RESULT_SUCCESS) {
206 MSG_DEBUG("drm_svc_close_convert() return = failed (%d)", ret);
207 strncpy(outputFile, inputFile, MSG_MAX_DRM_FILE_PATH);
214 MSG_DEBUG("Current File extension is not .dm");
216 MSG_DEBUG("inputFile = (%s)", inputFile);
217 strncpy(outputFile, inputFile, MSG_MAX_DRM_FILE_PATH);
225 bool MsgDrmIsConvertedFL(char *szFilePath)
229 if (szFilePath == NULL) {
230 MSG_DEBUG("szFilePath is NULL.");
234 MSG_DEBUG("szFilePath = [%s]", szFilePath);
236 ret = drm_svc_is_converted_fl(szFilePath);
238 if (ret != DRM_RESULT_SUCCESS) {
239 MSG_DEBUG("Drm2IsConvertedEmbeddedFile returns false ret = %d", ret);
246 int MsgDrmGetStreamSize(MSG_DRMHANDLE pHandle)
247 { // Get DRM buffer size
249 MSG_OPENEDDRM_INFO_T *pOpenDRMInfo = (MSG_OPENEDDRM_INFO_T *)pHandle;
250 if (pOpenDRMInfo == NULL) {
251 MSG_DEBUG("end : Fail");
255 drm_file_attribute_t tDRMAttribute = {0,};
256 DRM_RESULT eDRMResult = drm_svc_get_fileattribute(pOpenDRMInfo->szOpenedDRMFileName, &tDRMAttribute); // Get attribute of DRM File
257 MSG_DEBUG("drm_svc_get_fileattribute : %d", eDRMResult);
258 if (DRM_RESULT_SUCCESS == eDRMResult) {
259 return tDRMAttribute.size;
261 MSG_DEBUG("end : Fail");
265 bool MsgDrmGetStream(MSG_DRMHANDLE pHandle, int nStreamSize, unsigned char *pStream)
267 MSG_DEBUG("start %d", nStreamSize);
268 MSG_OPENEDDRM_INFO_T *pOpenDRMInfo = (MSG_OPENEDDRM_INFO_T *)pHandle;
269 if (pOpenDRMInfo == NULL) {
270 MSG_DEBUG("end : Fail");
274 DRM_RESULT eDRMResult = DRM_RESULT_UNKNOWN_ERROR;
275 DRM_FILE_HANDLE hFileHandle = NULL;
276 eDRMResult = drm_svc_open_file(pOpenDRMInfo->szOpenedDRMFileName, DRM_PERMISSION_ANY, &hFileHandle); // Opens a DRM file
277 MSG_DEBUG("drm_svc_open_file(%s) : %d", pOpenDRMInfo->szOpenedDRMFileName, eDRMResult);
279 unsigned int nRealReadSize = 0;
281 eDRMResult = drm_svc_read_file(hFileHandle, pStream, nStreamSize, &nRealReadSize); // Read the Decrypted Data from File Handle
282 drm_svc_close_file(hFileHandle); // Close a DRM File which was opened before
283 if (DRM_RESULT_SUCCESS == eDRMResult) {
284 MSG_DEBUG("end : Success");
288 MSG_DEBUG("\n end : Fail \n=========================================\n");
292 bool MsgDrmOpen(MSG_DRM_OPENMODE eMode, const char *pBuffer, int nSize, MSG_DRMHANDLE *pHandle)
294 MSG_DEBUG("start (%d, %s, %d)", eMode, pBuffer, nSize);
295 if (eMode == MSG_MODE_STREAM) {
296 MSG_DEBUG("end : Fail(eMode == MSG_MODE_STREAM)");
300 char szFullFilePath[MSG_MAX_DRM_FILE_PATH] = {0,};
301 char szFinalFullFilePath[MSG_MAX_DRM_FILE_PATH] = {0,};
302 memset(szFullFilePath, 0x00, sizeof(char) * MSG_MAX_DRM_FILE_PATH);
304 strncpy(szFullFilePath, pBuffer, strlen(pBuffer));
306 MsgDrmConvertDmtoDcfType(szFullFilePath, szFinalFullFilePath);
308 if (!drm_svc_is_drm_file(szFinalFullFilePath)) { // Check whether DRM file or not
309 MSG_DEBUG("return eDRM_NOT_DRM_FILE");
314 MSG_OPENEDDRM_INFO_T *pOpenDRMInfo = (MSG_OPENEDDRM_INFO_T *)malloc(sizeof(MSG_OPENEDDRM_INFO_T));
316 memset(pOpenDRMInfo, 0x0, sizeof(MSG_OPENEDDRM_INFO_T));
317 pOpenDRMInfo->szOpenedDRMFileName = (char *)malloc(sizeof(char) * (strlen(szFinalFullFilePath) + 1));
318 if (pOpenDRMInfo->szOpenedDRMFileName) {
319 memset(pOpenDRMInfo->szOpenedDRMFileName, 0x0, sizeof(char) * (strlen(szFinalFullFilePath) + 1));
320 strncpy(pOpenDRMInfo->szOpenedDRMFileName, szFinalFullFilePath, strlen(szFinalFullFilePath));
321 pOpenDRMInfo->eDRMType = MSG_DRM_NONE;
322 *pHandle = (MSG_DRMHANDLE)pOpenDRMInfo;
323 MSG_DEBUG("end : Success");
329 MSG_DEBUG("end : Fail");
333 bool MsgDrmClose(MSG_DRMHANDLE pHandle)
336 MSG_OPENEDDRM_INFO_T *pOpenDRMInfo = (MSG_OPENEDDRM_INFO_T *)pHandle;
337 if (pOpenDRMInfo == NULL) {
338 MSG_DEBUG("end : Fail");
341 //free allocated memory from MsgDrmOpen
342 if (pOpenDRMInfo->szOpenedDRMFileName) {
343 free(pOpenDRMInfo->szOpenedDRMFileName);
344 pOpenDRMInfo->szOpenedDRMFileName = NULL;
349 MSG_DEBUG("end : Success");
353 bool MsgDrmGetMimeType(MSG_DRMHANDLE pHandle, char *szMimeType, int nMimeTypeLen)
356 MSG_OPENEDDRM_INFO_T *pOpenDRMInfo = (MSG_OPENEDDRM_INFO_T *)pHandle;
357 if (pOpenDRMInfo == NULL) {
358 MSG_DEBUG("end : Fail");
362 drm_content_info_t tdcfContentinfo;
363 memset(&tdcfContentinfo, 0x00, sizeof(drm_content_info_t));
364 DRM_RESULT eDRMResult = drm_svc_get_content_info(pOpenDRMInfo->szOpenedDRMFileName, &tdcfContentinfo); // Get attribute of DRM File
365 MSG_DEBUG("drm_svc_get_content_info : %d", eDRMResult);
366 if (DRM_RESULT_SUCCESS == eDRMResult) {
367 MSG_DEBUG("end Success (%s)", tdcfContentinfo.contentType);
368 snprintf(szMimeType, nMimeTypeLen, "%s", tdcfContentinfo.contentType);
371 MSG_DEBUG("end : Fail");
375 bool MsgDrmGetDrmType(const char *szFileName, MSG_DRM_TYPE *eDRMType)
377 if (szFileName == NULL || eDRMType == NULL) {
378 MSG_DEBUG("Param is NULL");
382 if (drm_svc_get_drm_type(szFileName) == DRM_FILE_TYPE_OMA) {
383 drm_dcf_info_t drmInfo;
384 DRM_RESULT eDRMResult = drm_svc_get_dcf_file_info(szFileName, &drmInfo); // Get information of DRM contents
385 if (DRM_RESULT_SUCCESS != eDRMResult) {
386 MSG_DEBUG("drm_svc_get_dcf_file_info is Fail eDRMResult = %d", eDRMResult);
390 // Convert DRM_METHOD into MSG_DRM_TYPE
391 switch (drmInfo.method) {
393 *eDRMType = MSG_DRM_FORWARD_LOCK;
396 *eDRMType = MSG_DRM_COMBINED_DELIVERY;
399 *eDRMType = MSG_DRM_SEPARATE_DELIVERY;
402 *eDRMType = MSG_DRM_SEPARATE_DELIVERY;
405 *eDRMType = MSG_DRM_NONE;
408 MSG_DEBUG("eDRMType : %d", *eDRMType);
410 MSG_DEBUG("This is not a DRM_FILE_TYPE_OMA type");
417 bool MsgDrmConsumeRights(MSG_DRMHANDLE pHandle, MSG_DRM_RIGHT_TYPE eRightType, long nMiliSecs)
419 MSG_DEBUG("start %d, %ld", eRightType, nMiliSecs);
420 MSG_DEBUG("end : Success");
424 bool MsgDrmIsAvailable(MSG_DRMHANDLE pHandle)
427 MSG_OPENEDDRM_INFO_T *pOpenDRMInfo = (MSG_OPENEDDRM_INFO_T *)pHandle;
428 if (pOpenDRMInfo == NULL) {
429 MSG_DEBUG("end : Fail");
433 if (pOpenDRMInfo->eDRMType == MSG_DRM_FORWARD_LOCK) { //fl Àº ro ¾øÀ̵µ »ç¿ë°¡´É
434 MSG_DEBUG("end : Success");
438 DRM_PERMISSION_TYPE ePerType = DRM_PERMISSION_ANY;
439 drm_best_rights_t tBestRight; //ro ÀÖ´ÂÁö ¿©ºÎ¸¸ ¾Ë¸é µÇ¹Ç·Î drm_svc_get_best_ro ·Î üũÇϵµ·Ï ¼öÁ¤
440 memset(&tBestRight, 0x00, sizeof(drm_best_rights_t));
441 bool eReturn = false;
442 DRM_RESULT eDRMResult = drm_svc_get_best_ro(pOpenDRMInfo->szOpenedDRMFileName, ePerType, &tBestRight); // Check the valided Rights or not by DCF file
443 MSG_DEBUG("drm_svc_get_best_ro : %d", eDRMResult);
444 if (DRM_RESULT_SUCCESS == eDRMResult && tBestRight.rightStatus == DRM_RIGHT_VALID) {
446 MSG_DEBUG("end : Success");
448 MSG_DEBUG("end : Fail");
453 bool MsgDrmOnStart(MSG_DRMHANDLE pHandle, MSG_DRM_RIGHT_TYPE eRightType)
455 MSG_DEBUG("start %d", eRightType);
456 MSG_OPENEDDRM_INFO_T *pOpenDRMInfo = (MSG_OPENEDDRM_INFO_T *)pHandle;
457 if (pOpenDRMInfo == NULL) {
458 MSG_DEBUG("end : Fail");
462 if (pOpenDRMInfo->eDRMType == MSG_DRM_FORWARD_LOCK) {
463 MSG_DEBUG("end : Success");
467 DRM_PERMISSION_TYPE eDRMUsage = DRM_PERMISSION_ANY;
468 DRM_RIGHTS_CONSUME_HANDLE hRightsConsume = 0;
469 switch (eRightType) {
470 case MMS_DRM_RIGHT_PLAY:
471 eDRMUsage = DRM_PERMISSION_PLAY;
473 case MSG_DRM_RIGHT_DISPLAY:
474 eDRMUsage = DRM_PERMISSION_DISPLAY;
476 case MSG_DRM_RIGHT_EXECUTE:
477 eDRMUsage = DRM_PERMISSION_EXECUTE;
479 case MSG_DRM_RIGHT_PRINT:
480 eDRMUsage = DRM_PERMISSION_PRINT;
482 case MSG_DRM_RIGHT_EXPORT:
483 eDRMUsage = DRM_PERMISSION_EXPORT_MOVE; // DRM_PERMISSION_EXPORT_COPY µµ »ç¿ëÇؾßÇÔ
486 DRM_RESULT eDRMResult = drm_svc_open_consumption(pOpenDRMInfo->szOpenedDRMFileName, eDRMUsage, &hRightsConsume); // Open for consumption
487 MSG_DEBUG("drm_svc_open_consumption : %d", eDRMResult);
488 if (DRM_RESULT_SUCCESS != eDRMResult) {
489 MSG_DEBUG("end : Fail");
492 pOpenDRMInfo->hRightsConsume = hRightsConsume;
493 eDRMResult = drm_svc_start_consumption(pOpenDRMInfo->hRightsConsume); // Start the consumption
494 MSG_DEBUG("drm_svc_start_consumption : %d", eDRMResult);
495 if (DRM_RESULT_SUCCESS == eDRMResult) {
496 MSG_DEBUG("end : Success");
499 MSG_DEBUG("end : Fail");
503 bool MsgDrmOnPause(MSG_DRMHANDLE pHandle, MSG_DRM_RIGHT_TYPE eRightType)
505 MSG_DEBUG("start %d", eRightType);
506 MSG_OPENEDDRM_INFO_T *pOpenDRMInfo = (MSG_OPENEDDRM_INFO_T *)pHandle;
507 if (pOpenDRMInfo == NULL) {
508 MSG_DEBUG("end : Fail");
512 if (pOpenDRMInfo->eDRMType == MSG_DRM_FORWARD_LOCK) {
513 MSG_DEBUG("end : Success");
517 DRM_RESULT eDRMResult = drm_svc_pause_consumption(pOpenDRMInfo->hRightsConsume); // Pause the consumption
518 MSG_DEBUG("drm_svc_pause_consumption : %d", eDRMResult);
519 if (DRM_RESULT_SUCCESS == eDRMResult) {
520 MSG_DEBUG("end : Success");
523 MSG_DEBUG("end : Fail");
527 bool MsgDrmOnResume(MSG_DRMHANDLE pHandle, MSG_DRM_RIGHT_TYPE eRightType)
529 MSG_DEBUG("start %d", eRightType);
530 MSG_DEBUG("end : Success");
534 bool MsgDrmOnStop(MSG_DRMHANDLE pHandle, MSG_DRM_RIGHT_TYPE eRightType)
536 MSG_DEBUG("start %d", eRightType);
537 MSG_OPENEDDRM_INFO_T *pOpenDRMInfo = (MSG_OPENEDDRM_INFO_T *)pHandle;
538 if (pOpenDRMInfo == NULL) {
539 MSG_DEBUG("end : Fail");
543 if (pOpenDRMInfo->eDRMType == MSG_DRM_FORWARD_LOCK) {
544 MSG_DEBUG("end : Success");
548 DRM_RESULT eDRMResult = drm_svc_stop_consumption(pOpenDRMInfo->hRightsConsume); // Stop the consumption
549 MSG_DEBUG("drm_svc_stop_consumption : %d", eDRMResult);
550 if (DRM_RESULT_SUCCESS != eDRMResult) {
551 MSG_DEBUG("end : Fail");
555 eDRMResult = drm_svc_close_consumption(&(pOpenDRMInfo->hRightsConsume)); // Close the consumption
556 MSG_DEBUG("drm_svc_close_consumption : %d", eDRMResult);
557 if (DRM_RESULT_SUCCESS == eDRMResult) {
558 pOpenDRMInfo->hRightsConsume = 0;
559 MSG_DEBUG("end : Success");
562 MSG_DEBUG("end : Fail");
566 bool MsgDrmGetMimeTypeEx(const char *szFileName, char *szMimeType, int nMimeTypeLen)
568 if (!szFileName || !szMimeType || !nMimeTypeLen) {
569 MSG_DEBUG("param is NULL");
573 char strTemp[MSG_MAX_DRM_FILE_PATH + 1] = {0,};
575 strncpy(strTemp, szFileName, strlen(szFileName));
577 drm_content_info_t tdcfContentinfo;
578 memset(&tdcfContentinfo, 0x00, sizeof(drm_content_info_t));
579 DRM_RESULT eDRMResult = drm_svc_get_content_info(strTemp, &tdcfContentinfo); // Get attribute of DRM File
580 if (DRM_RESULT_SUCCESS == eDRMResult) {
581 MSG_DEBUG("contentType = %s", tdcfContentinfo.contentType);
582 snprintf(szMimeType, nMimeTypeLen, "%s", tdcfContentinfo.contentType);
586 MSG_DEBUG("drm_svc_get_content_info is failed %d", eDRMResult);
592 bool MsgDrmGetContentID(const char *szFileName, char *szContentID, int nContentIDLen)
594 if (!szFileName || !szContentID || !nContentIDLen) {
595 MSG_DEBUG("param is NULL");
599 char strTemp[MSG_MAX_DRM_FILE_PATH + 1] = {0,};
601 strncpy(strTemp, szFileName, sizeof(strTemp));
603 drm_content_info_t tdcfContentinfo;
604 memset(&tdcfContentinfo, 0x00, sizeof(drm_content_info_t));
605 DRM_RESULT eDRMResult = drm_svc_get_content_info(strTemp, &tdcfContentinfo);
606 if (DRM_RESULT_SUCCESS == eDRMResult) {
607 MSG_DEBUG("contentID = %s", tdcfContentinfo.contentID);
608 snprintf(szContentID, nContentIDLen, "%s", tdcfContentinfo.contentID);
612 MSG_DEBUG("drm_svc_get_content_info is failed %d", eDRMResult);