Fix minor issue
[platform/core/multimedia/libmedia-service.git] / md5 / media-svc-hash.c
1 /*
2  * libmedia-service
3  *
4  * Copyright (c) 2000 - 2011 Samsung Electronics Co., Ltd. All rights reserved.
5  *
6  * Contact: Hyunjun Ko <zzoon.ko@samsung.com>, Haejeong Kim <backto.kim@samsung.com>
7  *
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
11  *
12  * http://www.apache.org/licenses/LICENSE-2.0
13  *
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.
19  *
20  */
21
22 #include "md5.h"
23 #include <string.h>
24 #include <alloca.h>
25 #include "media-svc-hash.h"
26 #include "media-util-err.h"
27
28
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
48 };
49
50 char *_mb_svc_generate_hash_name(const char *file)
51 {
52         int n;
53         MD5_CTX ctx;
54         static char md5out[(2 * MD5_HASHBYTES) + 1];
55         unsigned char hash[MD5_HASHBYTES];
56         static const char hex[] = "0123456789abcdef";
57
58         char *uri;
59         char *t;
60         const unsigned char *c;
61         int length;
62
63         if (!file)
64                 return NULL;
65
66         length = 3 * strlen(file) + 9;
67
68         memset(md5out, 0, sizeof(md5out));
69
70 #define _check_uri_char(c) \
71         ((c) >= 32 && (c) < 128 && (ACCEPTABLE_URI_CHARS[(c) - 32] & 0x08))
72
73         uri = alloca(length);
74         if (uri == NULL)
75                 return NULL;
76
77         strncpy(uri, "file://", length);
78         uri[length - 1] = '\0';
79         t = uri + sizeof("file://") - 1;
80
81         for (c = (const unsigned char *)file; *c != '\0'; c++) {
82                 if (!_check_uri_char(*c)) {
83                         *t++ = '%';
84                         *t++ = hex[*c >> 4];
85                         *t++ = hex[*c & 15];
86                 } else {
87                         *t++ = *c;
88                 }
89         }
90         *t = '\0';
91 #undef _check_uri_char
92
93         media_svc_MD5Init(&ctx);
94         media_svc_MD5Update(&ctx, (unsigned char const *)uri, (unsigned)strlen(uri));
95         media_svc_MD5Final(hash, &ctx);
96
97         for (n = 0; n < MD5_HASHBYTES; n++) {
98                 md5out[2 * n] = hex[hash[n] >> 4];
99                 md5out[2 * n + 1] = hex[hash[n] & 0x0f];
100         }
101         md5out[2 * n] = '\0';
102
103         return md5out;
104 }
105
106 int mb_svc_generate_hash_code(const char *origin_path, char *hash_code, int max_length)
107 {
108         char *hash = NULL;
109
110         if (max_length < ((2 * MD5_HASHBYTES) + 1))
111                 return MS_MEDIA_ERR_INVALID_PARAMETER;
112
113         hash = _mb_svc_generate_hash_name(origin_path);
114
115         if (hash == NULL)
116                 return MS_MEDIA_ERR_INTERNAL;
117
118         strncpy(hash_code, hash, max_length);
119         hash_code[strlen(hash_code)] = '\0';
120
121         return MS_MEDIA_ERR_NONE;
122 }
123