1 /* GLIB - Library of useful routines for C programming
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
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.
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.
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.
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
31 #include "media-svc-hash.h"
32 #include "media-util-err.h"
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
56 char *_mb_svc_generate_hash_name(const char *file)
60 static char md5out[(2 * MD5_HASHBYTES) + 1];
61 unsigned char hash[MD5_HASHBYTES];
62 static const char hex[] = "0123456789abcdef";
66 const unsigned char *c;
73 length = 3 * strlen(file) + 9;
75 memset(md5out, 0, sizeof(md5out));
77 #define _check_uri_char(c) \
78 ((c) >= 32 && (c) < 128 && (ACCEPTABLE_URI_CHARS[(c) - 32] & 0x08))
85 strncpy(uri, "file://", length);
86 uri[length - 1] = '\0';
87 t = uri + sizeof("file://") - 1;
89 for (c = (const unsigned char *)file; *c != '\0'; c++) {
90 if (!_check_uri_char(*c)) {
99 #undef _check_uri_char
102 MD5Update(&ctx, (unsigned char const *)uri, (unsigned)strlen(uri));
103 MD5Final(hash, &ctx);
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];
109 md5out[2 * n] = '\0';
114 int mb_svc_generate_hash_code(const char *origin_path, char *hash_code, int max_length)
118 if (max_length < ((2 * MD5_HASHBYTES) + 1)) {
119 return MS_MEDIA_ERR_INVALID_PARAMETER;
122 hash = _mb_svc_generate_hash_name(origin_path);
125 return MS_MEDIA_ERR_INTERNAL;
128 strncpy(hash_code, hash, max_length);
129 hash_code[strlen(hash_code)] ='\0';
131 return MS_MEDIA_ERR_NONE;