4 * Copyright (c) 2000 - 2011 Samsung Electronics Co., Ltd. All rights reserved.
6 * Contact: Hyunjun Ko <zzoon.ko@samsung.com>, Haejeong Kim <backto.kim@samsung.com>
8 * Licensed under the Apache License, Version 2.0 (the "License");
9 * you may not use this file except in compliance with the License.
10 * You may obtain a copy of the License at
12 * http://www.apache.org/licenses/LICENSE-2.0
14 * Unless required by applicable law or agreed to in writing, software
15 * distributed under the License is distributed on an "AS IS" BASIS,
16 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17 * See the License for the specific language governing permissions and
18 * limitations under the License.
25 #include "media-svc-hash.h"
26 #include "media-util-err.h"
29 static const char ACCEPTABLE_URI_CHARS[96] = {
30 /* ! " # $ % & ' ( ) * + , - . / */
31 0x00, 0x3F, 0x20, 0x20, 0x28, 0x00, 0x2C, 0x3F, 0x3F, 0x3F, 0x3F, 0x2A,
32 0x28, 0x3F, 0x3F, 0x1C,
33 /* 0 1 2 3 4 5 6 7 8 9 : ; < = > ? */
34 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x38, 0x20,
35 0x20, 0x2C, 0x20, 0x20,
36 /* @ A B C D E F G H I J K L M N O */
37 0x38, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F,
38 0x3F, 0x3F, 0x3F, 0x3F,
39 /* P Q R S T U V W X Y Z [ \ ] ^ _ */
40 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x20,
41 0x20, 0x20, 0x20, 0x3F,
42 /* ` a b c d e f g h i j k l m n o */
43 0x20, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F,
44 0x3F, 0x3F, 0x3F, 0x3F,
45 /* p q r s t u v w x y z { | } ~ DEL */
46 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x20,
47 0x20, 0x20, 0x3F, 0x20
50 char *_mb_svc_generate_hash_name(const char *file)
54 static char md5out[(2 * MD5_HASHBYTES) + 1];
55 unsigned char hash[MD5_HASHBYTES];
56 static const char hex[] = "0123456789abcdef";
60 const unsigned char *c;
67 length = 3 * strlen(file) + 9;
69 memset(md5out, 0, sizeof(md5out));
71 #define _check_uri_char(c) \
72 ((c) >= 32 && (c) < 128 && (ACCEPTABLE_URI_CHARS[(c) - 32] & 0x08))
79 strncpy(uri, "file://", length);
80 uri[length - 1] = '\0';
81 t = uri + sizeof("file://") - 1;
83 for (c = (const unsigned char *)file; *c != '\0'; c++) {
84 if (!_check_uri_char(*c)) {
93 #undef _check_uri_char
96 MD5Update(&ctx, (unsigned char const *)uri, (unsigned)strlen(uri));
99 for (n = 0; n < MD5_HASHBYTES; n++) {
100 md5out[2 * n] = hex[hash[n] >> 4];
101 md5out[2 * n + 1] = hex[hash[n] & 0x0f];
103 md5out[2 * n] = '\0';
108 int mb_svc_generate_hash_code(const char *origin_path, char *hash_code, int max_length)
112 if (max_length < ((2 * MD5_HASHBYTES) + 1)) {
113 return MS_MEDIA_ERR_INVALID_PARAMETER;
116 hash = _mb_svc_generate_hash_name(origin_path);
119 return MS_MEDIA_ERR_INTERNAL;
122 strncpy(hash_code, hash, max_length);
123 hash_code[strlen(hash_code)] = '\0';
125 return MS_MEDIA_ERR_NONE;