Revert "Modify error type, remove unused code, and replace uuid code to uuid library."
[platform/core/multimedia/libmedia-service.git] / md5 / media-svc-hash.c
1 /* GLIB - Library of useful routines for C programming
2  *
3  * gconvert.c: Convert between character sets using iconv
4  * Copyright Red Hat Inc., 2000
5  * Authors: Havoc Pennington <hp@redhat.com>, Owen Taylor <otaylor@redhat.com
6  *
7  * This library is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU Lesser General Public
9  * License as published by the Free Software Foundation; either
10  * version 2 of the License, or (at your option) any later version.
11  *
12  * This library is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15  * Lesser General Public License for more details.
16  *
17  * You should have received a copy of the GNU Lesser General Public
18  * License along with this library; if not, write to the
19  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
20  * Boston, MA 02111-1307, USA.
21  */
22
23 /* The array below is taken from gconvert.c, which is licensed by GNU Lesser General Public License
24  * Code to escape string is also taken partially from gconvert.c
25  * File name is changed to media-svc-hash.c
26  */
27
28 #include "md5.h"
29 #include <string.h>
30 #include <alloca.h>
31 #include "media-svc-hash.h"
32 #include "media-svc-error.h"
33
34
35 static const char ACCEPTABLE_URI_CHARS[96] = {
36         /*      !    "    #    $    %    &    '    (    )    *    +    ,    -    .    / */
37         0x00, 0x3F, 0x20, 0x20, 0x28, 0x00, 0x2C, 0x3F, 0x3F, 0x3F, 0x3F, 0x2A,
38             0x28, 0x3F, 0x3F, 0x1C,
39         /* 0    1    2    3    4    5    6    7    8    9    :    ;    <    =    >    ? */
40         0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x38, 0x20,
41             0x20, 0x2C, 0x20, 0x20,
42         /* @    A    B    C    D    E    F    G    H    I    J    K    L    M    N    O */
43         0x38, 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    [    \    ]    ^    _ */
46         0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x20,
47             0x20, 0x20, 0x20, 0x3F,
48         /* `    a    b    c    d    e    f    g    h    i    j    k    l    m    n    o */
49         0x20, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F,
50             0x3F, 0x3F, 0x3F, 0x3F,
51         /* p    q    r    s    t    u    v    w    x    y    z    {    |    }    ~  DEL */
52         0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x20,
53             0x20, 0x20, 0x3F, 0x20
54 };
55
56 char *_mb_svc_generate_hash_name(const char *file)
57 {
58         int n;
59         MD5_CTX ctx;
60         static char md5out[(2 * MD5_HASHBYTES) + 1];
61         unsigned char hash[MD5_HASHBYTES];
62         static const char hex[] = "0123456789abcdef";
63
64         char *uri;
65         char *t;
66         const unsigned char *c;
67         int length;
68
69         if (!file) {
70                 return NULL;
71         }
72
73         length = 3 * strlen(file) + 9;
74
75         memset(md5out, 0, sizeof(md5out));
76
77 #define _check_uri_char(c) \
78 ((c) >= 32 && (c) < 128 && (ACCEPTABLE_URI_CHARS[(c) - 32] & 0x08))
79
80         uri = alloca(length);
81         if (uri == NULL) {
82                 return NULL;
83         }
84
85         strncpy(uri, "file://", length);
86         uri[length - 1] = '\0';
87         t = uri + sizeof("file://") - 1;
88
89         for (c = (const unsigned char *)file; *c != '\0'; c++) {
90                 if (!_check_uri_char(*c)) {
91                         *t++ = '%';
92                         *t++ = hex[*c >> 4];
93                         *t++ = hex[*c & 15];
94                 } else {
95                         *t++ = *c;
96                 }
97         }
98         *t = '\0';
99 #undef _check_uri_char
100
101         MD5Init(&ctx);
102         MD5Update(&ctx, (unsigned char const *)uri, (unsigned)strlen(uri));
103         MD5Final(hash, &ctx);
104
105         for (n = 0; n < MD5_HASHBYTES; n++) {
106                 md5out[2 * n] = hex[hash[n] >> 4];
107                 md5out[2 * n + 1] = hex[hash[n] & 0x0f];
108         }
109         md5out[2 * n] = '\0';
110
111         return md5out;
112 }
113
114 int mb_svc_generate_hash_code(const char *origin_path, char *hash_code, int max_length)
115 {
116         char *hash = NULL;
117
118         if (max_length < ((2 * MD5_HASHBYTES) + 1)) {
119                 return MEDIA_INFO_ERROR_INVALID_PARAMETER;
120         }
121
122         hash = _mb_svc_generate_hash_name(origin_path);
123
124         if (hash == NULL) {
125                 return MEDIA_INFO_ERROR_INTERNAL;
126         }
127
128         strncpy(hash_code, hash, max_length);
129         hash_code[strlen(hash_code)] ='\0';
130
131         return MEDIA_INFO_ERROR_NONE;
132 }
133