Remove contact in boilerplate
[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  * Licensed under the Apache License, Version 2.0 (the "License");
7  * you may not use this file except in compliance with the License.
8  * You may obtain a copy of the License at
9  *
10  * http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  *
18  */
19
20 #include "md5.h"
21 #include <string.h>
22 #include <alloca.h>
23 #include "media-svc-hash.h"
24 #include "media-util-err.h"
25
26
27 static const char ACCEPTABLE_URI_CHARS[96] = {
28         /*      !    "    #    $    %    &    '    (    )    *    +   ,    -    .    / */
29         0x00, 0x3F, 0x20, 0x20, 0x28, 0x00, 0x2C, 0x3F, 0x3F, 0x3F, 0x3F, 0x2A,
30         0x28, 0x3F, 0x3F, 0x1C,
31         /* 0    1    2    3    4    5    6    7    8    9    :    ;    <    =    >    ? */
32         0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x38, 0x20,
33         0x20, 0x2C, 0x20, 0x20,
34         /* @    A    B    C    D    E    F    G    H    I    J    K    L    M    N    O */
35         0x38, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F,
36         0x3F, 0x3F, 0x3F, 0x3F,
37         /* P    Q    R    S    T    U    V    W    X    Y    Z    [    \    ]    ^    _ */
38         0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x20,
39         0x20, 0x20, 0x20, 0x3F,
40         /* `    a    b    c    d    e    f    g    h    i    j    k    l    m    n    o */
41         0x20, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F,
42         0x3F, 0x3F, 0x3F, 0x3F,
43         /* p    q    r    s    t    u    v    w    x    y    z    {    |    }    ~  DEL */
44         0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x20,
45         0x20, 0x20, 0x3F, 0x20
46 };
47
48 char *_mb_svc_generate_hash_name(const char *file)
49 {
50         int n;
51         MD5_CTX ctx;
52         static char md5out[(2 * MD5_HASHBYTES) + 1];
53         unsigned char hash[MD5_HASHBYTES];
54         static const char hex[] = "0123456789abcdef";
55
56         char *uri;
57         char *t;
58         const unsigned char *c;
59         int length;
60
61         if (!file)
62                 return NULL;
63
64         length = 3 * strlen(file) + 9;
65
66         memset(md5out, 0, sizeof(md5out));
67
68 #define _check_uri_char(c) \
69         ((c) >= 32 && (c) < 128 && (ACCEPTABLE_URI_CHARS[(c) - 32] & 0x08))
70
71         uri = alloca(length);
72         if (uri == NULL)
73                 return NULL;
74
75         strncpy(uri, "file://", length);
76         uri[length - 1] = '\0';
77         t = uri + sizeof("file://") - 1;
78
79         for (c = (const unsigned char *)file; *c != '\0'; c++) {
80                 if (!_check_uri_char(*c)) {
81                         *t++ = '%';
82                         *t++ = hex[*c >> 4];
83                         *t++ = hex[*c & 15];
84                 } else {
85                         *t++ = *c;
86                 }
87         }
88         *t = '\0';
89 #undef _check_uri_char
90
91         media_svc_MD5Init(&ctx);
92         media_svc_MD5Update(&ctx, (unsigned char const *)uri, (unsigned)strlen(uri));
93         media_svc_MD5Final(hash, &ctx);
94
95         for (n = 0; n < MD5_HASHBYTES; n++) {
96                 md5out[2 * n] = hex[hash[n] >> 4];
97                 md5out[2 * n + 1] = hex[hash[n] & 0x0f];
98         }
99         md5out[2 * n] = '\0';
100
101         return md5out;
102 }
103
104 int mb_svc_generate_hash_code(const char *origin_path, char *hash_code, int max_length)
105 {
106         char *hash = NULL;
107
108         if (max_length < ((2 * MD5_HASHBYTES) + 1))
109                 return MS_MEDIA_ERR_INVALID_PARAMETER;
110
111         hash = _mb_svc_generate_hash_name(origin_path);
112
113         if (hash == NULL)
114                 return MS_MEDIA_ERR_INTERNAL;
115
116         strncpy(hash_code, hash, max_length);
117         hash_code[strlen(hash_code)] = '\0';
118
119         return MS_MEDIA_ERR_NONE;
120 }