1 | /* MiniDLNA media server |
---|
2 | * Copyright (C) 2009 Justin Maggard |
---|
3 | * |
---|
4 | * This file is part of MiniDLNA. |
---|
5 | * |
---|
6 | * MiniDLNA is free software; you can redistribute it and/or modify |
---|
7 | * it under the terms of the GNU General Public License version 2 as |
---|
8 | * published by the Free Software Foundation. |
---|
9 | * |
---|
10 | * MiniDLNA is distributed in the hope that it will be useful, |
---|
11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
---|
13 | * GNU General Public License for more details. |
---|
14 | * |
---|
15 | * You should have received a copy of the GNU General Public License |
---|
16 | * along with MiniDLNA. If not, see <http://www.gnu.org/licenses/>. |
---|
17 | */ |
---|
18 | #include "config.h" |
---|
19 | #ifdef TIVO_SUPPORT |
---|
20 | #include <stdlib.h> |
---|
21 | #include <stdint.h> |
---|
22 | #include <string.h> |
---|
23 | #include <ctype.h> |
---|
24 | #include <sqlite3.h> |
---|
25 | #include "tivo_utils.h" |
---|
26 | |
---|
27 | /* This function based on byRequest */ |
---|
28 | char * |
---|
29 | decodeString(char * string, int inplace) |
---|
30 | { |
---|
31 | if( !string ) |
---|
32 | return NULL; |
---|
33 | int alloc = (int)strlen(string)+1; |
---|
34 | char * ns = NULL; |
---|
35 | unsigned char in; |
---|
36 | int strindex=0; |
---|
37 | long hex; |
---|
38 | |
---|
39 | if( !inplace ) |
---|
40 | { |
---|
41 | if( !(ns = malloc(alloc)) ) |
---|
42 | return NULL; |
---|
43 | } |
---|
44 | |
---|
45 | while(--alloc > 0) |
---|
46 | { |
---|
47 | in = *string; |
---|
48 | if((in == '%') && isxdigit(string[1]) && isxdigit(string[2])) |
---|
49 | { |
---|
50 | /* this is two hexadecimal digits following a '%' */ |
---|
51 | char hexstr[3]; |
---|
52 | char *ptr; |
---|
53 | hexstr[0] = string[1]; |
---|
54 | hexstr[1] = string[2]; |
---|
55 | hexstr[2] = 0; |
---|
56 | |
---|
57 | hex = strtol(hexstr, &ptr, 16); |
---|
58 | |
---|
59 | in = (unsigned char)hex; /* this long is never bigger than 255 anyway */ |
---|
60 | if( inplace ) |
---|
61 | { |
---|
62 | *string = in; |
---|
63 | memmove(string+1, string+3, alloc-2); |
---|
64 | } |
---|
65 | else |
---|
66 | { |
---|
67 | string+=2; |
---|
68 | } |
---|
69 | alloc-=2; |
---|
70 | } |
---|
71 | if( !inplace ) |
---|
72 | ns[strindex++] = in; |
---|
73 | string++; |
---|
74 | } |
---|
75 | if( inplace ) |
---|
76 | { |
---|
77 | if( ns ) |
---|
78 | free(ns); |
---|
79 | return string; |
---|
80 | } |
---|
81 | else |
---|
82 | { |
---|
83 | ns[strindex] = '\0'; /* terminate it */ |
---|
84 | return ns; |
---|
85 | } |
---|
86 | } |
---|
87 | |
---|
88 | /* These next functions implement a repeatable random function with a user-provided seed */ |
---|
89 | static int |
---|
90 | seedRandomByte(uint32_t seed) |
---|
91 | { |
---|
92 | unsigned char t; |
---|
93 | |
---|
94 | if( !sqlite3Prng.isInit ) |
---|
95 | { |
---|
96 | int i; |
---|
97 | char k[256]; |
---|
98 | sqlite3Prng.j = 0; |
---|
99 | sqlite3Prng.i = 0; |
---|
100 | memset(&k, '\0', sizeof(k)); |
---|
101 | memcpy(&k, &seed, 4); |
---|
102 | for(i=0; i<256; i++) |
---|
103 | sqlite3Prng.s[i] = i; |
---|
104 | for(i=0; i<256; i++) |
---|
105 | { |
---|
106 | sqlite3Prng.j += sqlite3Prng.s[i] + k[i]; |
---|
107 | t = sqlite3Prng.s[sqlite3Prng.j]; |
---|
108 | sqlite3Prng.s[sqlite3Prng.j] = sqlite3Prng.s[i]; |
---|
109 | sqlite3Prng.s[i] = t; |
---|
110 | } |
---|
111 | sqlite3Prng.isInit = 1; |
---|
112 | } |
---|
113 | /* Generate and return single random byte */ |
---|
114 | sqlite3Prng.i++; |
---|
115 | t = sqlite3Prng.s[sqlite3Prng.i]; |
---|
116 | sqlite3Prng.j += t; |
---|
117 | sqlite3Prng.s[sqlite3Prng.i] = sqlite3Prng.s[sqlite3Prng.j]; |
---|
118 | sqlite3Prng.s[sqlite3Prng.j] = t; |
---|
119 | t += sqlite3Prng.s[sqlite3Prng.i]; |
---|
120 | |
---|
121 | return sqlite3Prng.s[t]; |
---|
122 | } |
---|
123 | |
---|
124 | void |
---|
125 | seedRandomness(int n, void *pbuf, uint32_t seed) |
---|
126 | { |
---|
127 | unsigned char *zbuf = pbuf; |
---|
128 | |
---|
129 | while( n-- ) |
---|
130 | *(zbuf++) = seedRandomByte(seed); |
---|
131 | } |
---|
132 | |
---|
133 | void |
---|
134 | TiVoRandomSeedFunc(sqlite3_context *context, int argc, sqlite3_value **argv) |
---|
135 | { |
---|
136 | sqlite_int64 r, seed; |
---|
137 | |
---|
138 | if( argc != 1 || sqlite3_value_type(argv[0]) != SQLITE_INTEGER ) |
---|
139 | return; |
---|
140 | seed = sqlite3_value_int64(argv[0]); |
---|
141 | seedRandomness(sizeof(r), &r, seed); |
---|
142 | sqlite3_result_int64(context, r); |
---|
143 | } |
---|
144 | #endif |
---|