2 * Copyright (c) 2014 Samsung Electronics Co., Ltd. All rights reserved
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
18 #include "MsgDrmWrapper.h"
19 #include "MsgUtilFile.h"
27 #include <sys/types.h>
36 #include <drm_client_types.h>
37 #include <drm_client.h>
39 #endif /* MSG_DRM_SUPPORT */
41 #define MSG_MAX_DRM_FILE_PATH MSG_FILEPATH_LEN_MAX
43 bool MsgDrmRegisterFile(MSG_DRM_OPENMODE eMode, char *pBuffer, int nSize)
46 if (eMode == MSG_MODE_STREAM) {
47 MSG_DEBUG("Fail(eMode == MSG_MODE_STREAM)");
51 if (pBuffer == NULL) {
52 MSG_DEBUG("[Error] pBuffer is NULL");
56 MSG_DEBUG("buffer = %s, nSize = %d", pBuffer, nSize);
58 drm_bool_type_e isDrm;
59 int eDRMResult = drm_is_drm_file(pBuffer, &isDrm);
61 if (eDRMResult != DRM_RETURN_SUCCESS || isDrm != DRM_TRUE) {
62 MSG_DEBUG("file is not drm file");
66 drm_request_type_e request_type = DRM_REQUEST_TYPE_REGISTER_FILE;
68 eDRMResult = drm_process_request(request_type, pBuffer, NULL);
69 if (DRM_RETURN_SUCCESS != eDRMResult) {
70 MSG_DEBUG("drm_process_request is failed : 0x%x", eDRMResult);
80 bool MsgDrmUnregisterFile(char *szFilename)
83 if (szFilename == NULL) {
84 MSG_DEBUG("[Error] szFilename is NULL");
88 MSG_DEBUG("szFilename = %s", szFilename);
90 drm_request_type_e request_type = DRM_REQUEST_TYPE_UNREGISTER_FILE;
92 int eDRMResult = drm_process_request(request_type, szFilename, NULL); /* Unregister a DCF file */
93 if (DRM_RETURN_SUCCESS != eDRMResult) {
94 MSG_DEBUG("drm_process_request : %d", eDRMResult);
104 bool MsgDrmIsDrmFile(const char *szFilePath)
107 drm_bool_type_e isDrm;
108 int eDRMResult = drm_is_drm_file(szFilePath, &isDrm);
110 if (eDRMResult != DRM_RETURN_SUCCESS || isDrm != DRM_TRUE) {
111 MSG_DEBUG("file is not drm file");
121 /*Added to convert the .dm files in to .dcf files since our platform supports only .dcf :: Start*/
122 bool MsgDrmConvertDmtoDcfType(char *inputFile, char *outputFile)
125 if ((NULL == inputFile) || (NULL == outputFile)) {
126 MSG_DEBUG("Invalid Input parameters");
130 if (strstr(inputFile, ".dm")) {
131 MSG_SEC_DEBUG("Current File extension is .dm %s", inputFile);
134 FILE *fp = MsgOpenFile(inputFile, "rb");/* Check fp */
137 MSG_DEBUG("[File Open Fail(Errno=%d)][ErrStr=%s]", errno, strerror(errno));
138 strncpy(outputFile, inputFile, MSG_MAX_DRM_FILE_PATH);
142 if (MsgFseek(fp, 0L, SEEK_END) < 0) {
144 MSG_DEBUG("MsgFseek() returns negative value!!!");
147 long retVal = MsgFtell(fp);
151 MSG_DEBUG("ftell() returns negative value: [%ld]!!!", retVal);
152 strncpy(outputFile, inputFile, MSG_MAX_DRM_FILE_PATH);
156 unsigned long bufLen = retVal;
157 MSG_DEBUG("fopen buffer len = %d", bufLen);
158 if (MsgFseek(fp, 0, SEEK_SET) < 0) {
160 MSG_DEBUG("MsgFseek() returns negative value!!!");
164 unsigned char *buffer = (unsigned char*)malloc(bufLen);
166 int pathLen = strlen(inputFile);
168 if (buffer == NULL) {
170 MSG_DEBUG("malloc is failed ");
171 strncpy(outputFile, inputFile, MSG_MAX_DRM_FILE_PATH);
175 strncpy(outputFile, inputFile, pathLen - 2);
176 strncat(outputFile, "dcf", 3);
178 readed_size = MsgReadFile(buffer, 1, bufLen, fp);/* Check for error */
179 MSG_DEBUG("fread read size = %d", readed_size);
180 if (readed_size == 0) {
183 MSG_DEBUG("MsgReadFile returns 0");
187 DRM_TRUSTED_CONVERT_HANDLE hConvert = NULL;
188 drm_trusted_opn_conv_info_s trusted_open_conv_input;
189 bzero(&trusted_open_conv_input, sizeof(drm_trusted_opn_conv_info_s));
191 strncpy(trusted_open_conv_input.filePath, outputFile, DRM_TRUSTED_MAX_FILEPATH_LEN-1);
192 trusted_open_conv_input.install_RO = DRM_TRUSTED_TRUE;
194 ret = drm_trusted_open_convert(&trusted_open_conv_input, &hConvert);
195 if (ret != DRM_RETURN_SUCCESS) {
198 MSG_DEBUG("drm_trusted_open_convert() return = failed (0x%x)", ret);
200 strncpy(outputFile, inputFile, MSG_MAX_DRM_FILE_PATH);
204 drm_trusted_write_conv_info_s trusted_write_conv_input;
205 drm_trusted_write_conv_resp_s trusted_write_conv_output;
207 bzero(&trusted_write_conv_input, sizeof(drm_trusted_write_conv_info_s));
208 bzero(&trusted_write_conv_output, sizeof(drm_trusted_write_conv_resp_s));
210 trusted_write_conv_input.data = buffer;
211 trusted_write_conv_input.data_len = bufLen;
213 /*We can call drm_trusted_write_convert in loop if file size is large*/
214 ret = drm_trusted_write_convert(&trusted_write_conv_input, &trusted_write_conv_output, hConvert);
215 if (ret != DRM_RETURN_SUCCESS) {
218 MSG_DEBUG("drm_trusted_write_convert() return = failed (0x%x)", ret);
220 strncpy(outputFile, inputFile, MSG_MAX_DRM_FILE_PATH);
224 ret = drm_trusted_close_convert(&hConvert);
225 if (ret != DRM_RETURN_SUCCESS) {
228 MSG_DEBUG("drm_trusted_close_convert() return = failed (0x%x)", ret);
230 strncpy(outputFile, inputFile, MSG_MAX_DRM_FILE_PATH);
237 MSG_DEBUG("Current File extension is not .dm");
239 MSG_DEBUG("inputFile = (%s)", inputFile);
240 strncpy(outputFile, inputFile, MSG_MAX_DRM_FILE_PATH);
251 bool MsgDrmGetDrmType(const char *szFileName, MSG_DRM_TYPE *eDRMType)
254 if (szFileName == NULL || eDRMType == NULL) {
255 MSG_DEBUG("Param is NULL");
259 drm_file_type_e file_type;
260 int result = drm_get_file_type(szFileName, &file_type);
261 if (result != DRM_RETURN_SUCCESS) {
262 MSG_DEBUG("drm_get_file_type is failed %d", result);
266 if (file_type == DRM_TYPE_OMA_V1) {
267 drm_file_info_s drmInfo;
268 bzero(&drmInfo, sizeof(drm_file_info_s));
269 int eDRMResult = drm_get_file_info(szFileName, &drmInfo);
270 if (DRM_RETURN_SUCCESS != eDRMResult) {
271 MSG_DEBUG("drm_get_file_info is Fail eDRMResult = %d", eDRMResult);
275 /* Convert DRM_METHOD into MSG_DRM_TYPE */
276 switch (drmInfo.oma_info.method) {
277 case DRM_METHOD_TYPE_FORWARD_LOCK:
278 *eDRMType = MSG_DRM_FORWARD_LOCK;
280 case DRM_METHOD_TYPE_COMBINED_DELIVERY:
281 *eDRMType = MSG_DRM_COMBINED_DELIVERY;
283 case DRM_METHOD_TYPE_SEPARATE_DELIVERY:
284 *eDRMType = MSG_DRM_SEPARATE_DELIVERY;
287 *eDRMType = MSG_DRM_NONE;
290 MSG_DEBUG("eDRMType : %d", *eDRMType);
292 MSG_DEBUG("This is not a DRM_TYPE_OMA_V1 type");
302 bool MsgDrmGetMimeTypeEx(const char *szFileName, char *szMimeType, int nMimeTypeLen)
305 if (!szFileName || !szMimeType || !nMimeTypeLen) {
306 MSG_DEBUG("param is NULL");
310 char strTemp[MSG_MAX_DRM_FILE_PATH + 1] = {0, };
312 strncpy(strTemp, szFileName, strlen(szFileName));
314 drm_content_info_s tdcfContentinfo;
315 memset(&tdcfContentinfo, 0x00, sizeof(drm_content_info_s));
316 int eDRMResult = drm_get_content_info(strTemp, &tdcfContentinfo); /* Get attribute of DRM File */
317 if (DRM_RETURN_SUCCESS == eDRMResult) {
318 MSG_DEBUG("contentType = %s", tdcfContentinfo.mime_type);
319 snprintf(szMimeType, nMimeTypeLen, "%s", tdcfContentinfo.mime_type);
322 MSG_DEBUG("drm_get_content_info is failed %d", eDRMResult);
332 bool MsgDrmGetContentID(const char *szFileName, char *szContentID, int nContentIDLen)
335 if (!szFileName || !szContentID || !nContentIDLen) {
336 MSG_DEBUG("param is NULL");
340 char strTemp[MSG_MAX_DRM_FILE_PATH + 1] = {0, };
342 strncpy(strTemp, szFileName, sizeof(strTemp)-1);
344 drm_content_info_s content_info;
345 memset(&content_info, 0x00, sizeof(drm_content_info_s));
347 int result = drm_get_content_info(strTemp, &content_info);
348 if (DRM_RETURN_SUCCESS == result) {
349 MSG_SEC_DEBUG("contentID = %s", content_info.content_id);
350 snprintf(szContentID, nContentIDLen, "%s", content_info.content_id);
352 MSG_DEBUG("drm_get_content_info is failed %d", result);
362 bool MsgDrmCheckRingtone(const char *ringtonePath)
368 drm_bool_type_e allowed = DRM_UNKNOWN;
369 drm_action_allowed_data_s data;
370 memset(&data, 0x00, sizeof(drm_action_allowed_data_s));
371 strncpy(data.file_path, ringtonePath, strlen(ringtonePath));
372 data.data = (int)DRM_SETAS_RINGTONE;
374 int res = drm_is_action_allowed(DRM_HAS_VALID_SETAS_STATUS, &data, &allowed);
376 if (res == DRM_RETURN_SUCCESS) {
377 if (allowed == DRM_TRUE) {
378 MSG_DEBUG("allowed [DRM_TRUE]");
382 MSG_DEBUG("fail to drm_is_action_allowed [0x%x]", res);
385 MSG_DEBUG("ringtonePath is NULL.");