Thumbnail-server code refactoring
[platform/core/multimedia/libmedia-thumbnail.git] / md5 / media-thumb-hash.c
1 /*
2  * libmedia-thumbnail
3  *
4  * Copyright (c) 2000 - 2011 Samsung Electronics Co., Ltd. All rights reserved.
5  *
6  * Contact: Hyunjun Ko <zzoon.ko@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-util-err.h>
26
27
28 static const char ACCEPTABLE_URI_CHARS[96] = {
29         /*      !    "    #    $    %    &    '    (    )    *    +    ,    -    .    / */
30         0x00, 0x3F, 0x20, 0x20, 0x28, 0x00, 0x2C, 0x3F, 0x3F, 0x3F, 0x3F, 0x2A,
31             0x28, 0x3F, 0x3F, 0x1C,
32         /* 0    1    2    3    4    5    6    7    8    9    :    ;    <    =    >    ? */
33         0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x38, 0x20,
34             0x20, 0x2C, 0x20, 0x20,
35         /* @    A    B    C    D    E    F    G    H    I    J    K    L    M    N    O */
36         0x38, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F,
37             0x3F, 0x3F, 0x3F, 0x3F,
38         /* P    Q    R    S    T    U    V    W    X    Y    Z    [    \    ]    ^    _ */
39         0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x20,
40             0x20, 0x20, 0x20, 0x3F,
41         /* `    a    b    c    d    e    f    g    h    i    j    k    l    m    n    o */
42         0x20, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F,
43             0x3F, 0x3F, 0x3F, 0x3F,
44         /* p    q    r    s    t    u    v    w    x    y    z    {    |    }    ~  DEL */
45         0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x20,
46             0x20, 0x20, 0x3F, 0x20
47 };
48
49 char *_media_thumb_generate_hash_name(const char *file)
50 {
51         int n;
52         MD5_CTX ctx;
53         static char md5out[(2 * MD5_HASHBYTES) + 1];
54         unsigned char hash[MD5_HASHBYTES];
55         static const char hex[] = "0123456789abcdef";
56
57         char *uri;
58         char *t;
59         const unsigned char *c;
60         int length;
61
62         if (!file) {
63                 return NULL;
64         }
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
78         strncpy(uri, "file://", length);
79         uri[length - 1] = '\0';
80         t = uri + sizeof("file://") - 1;
81
82         for (c = (const unsigned char *)file; *c != '\0'; c++) {
83                 if (!_check_uri_char(*c)) {
84                         *t++ = '%';
85                         *t++ = hex[*c >> 4];
86                         *t++ = hex[*c & 15];
87                 } else {
88                         *t++ = *c;
89                 }
90         }
91         *t = '\0';
92 #undef _check_uri_char
93
94         memset(&ctx, 0x00, sizeof(MD5_CTX));
95
96         media_thumb_MD5Init(&ctx);
97         media_thumb_MD5Update(&ctx, (unsigned char const *)uri, (unsigned)strlen(uri));
98         media_thumb_MD5Final(hash, &ctx);
99
100         for (n = 0; n < MD5_HASHBYTES; n++) {
101                 md5out[2 * n] = hex[hash[n] >> 4];
102                 md5out[2 * n + 1] = hex[hash[n] & 0x0f];
103         }
104         md5out[2 * n] = '\0';
105
106         return md5out;
107 }