Merge branch 'minidlna' into tomato-ND-USBmod
[tomato.git] / release / src / router / minidlna / tivo_utils.c
blob5039c93086be665fc10fd8c34375f323ec42df27
1 /* MiniDLNA media server
2 * Copyright (C) 2009 Justin Maggard
4 * This file is part of MiniDLNA.
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.
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.
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/>.
18 #include "config.h"
19 #ifdef TIVO_SUPPORT
20 #include <stdlib.h>
21 #include <string.h>
22 #include <ctype.h>
23 #include <linux/types.h> // Defines __u32
24 #include <sqlite3.h>
25 #include "tivo_utils.h"
27 /* This function based on byRequest */
28 char *
29 decodeString(char * string, int inplace)
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;
39 if( !inplace )
41 if( !(ns = malloc(alloc)) )
42 return NULL;
45 while(--alloc > 0)
47 in = *string;
48 if((in == '%') && isxdigit(string[1]) && isxdigit(string[2]))
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;
57 hex = strtol(hexstr, &ptr, 16);
59 in = (unsigned char)hex; /* this long is never bigger than 255 anyway */
60 if( inplace )
62 *string = in;
63 memmove(string+1, string+3, alloc-2);
65 else
67 string+=2;
69 alloc-=2;
71 if( !inplace )
72 ns[strindex++] = in;
73 string++;
75 if( inplace )
77 return string;
79 else
81 ns[strindex] = '\0'; /* terminate it */
82 return ns;
86 /* These next functions implement a repeatable random function with a user-provided seed */
87 static int
88 seedRandomByte(__u32 seed) {
89 unsigned char t;
90 if( !sqlite3Prng.isInit )
92 int i;
93 char k[256];
94 sqlite3Prng.j = 0;
95 sqlite3Prng.i = 0;
96 memset(&k, 0, 256);
97 memcpy(&k, &seed, 4);
98 for(i=0; i<256; i++){
99 sqlite3Prng.s[i] = i;
101 for(i=0; i<256; i++){
102 sqlite3Prng.j += sqlite3Prng.s[i] + k[i];
103 t = sqlite3Prng.s[sqlite3Prng.j];
104 sqlite3Prng.s[sqlite3Prng.j] = sqlite3Prng.s[i];
105 sqlite3Prng.s[i] = t;
107 sqlite3Prng.isInit = 1;
109 /* Generate and return single random byte */
110 sqlite3Prng.i++;
111 t = sqlite3Prng.s[sqlite3Prng.i];
112 sqlite3Prng.j += t;
113 sqlite3Prng.s[sqlite3Prng.i] = sqlite3Prng.s[sqlite3Prng.j];
114 sqlite3Prng.s[sqlite3Prng.j] = t;
115 t += sqlite3Prng.s[sqlite3Prng.i];
117 return sqlite3Prng.s[t];
120 void
121 seedRandomness(int N, void *pBuf, __u32 seed){
122 unsigned char *zBuf = pBuf;
124 while( N-- ){
125 *(zBuf++) = seedRandomByte(seed);
129 void
130 TiVoRandomSeedFunc(sqlite3_context *context, int argc, sqlite3_value **argv)
132 sqlite_int64 r, seed;
133 if( argc != 1 || sqlite3_value_type(argv[0]) != SQLITE_INTEGER )
134 return;
135 seed = sqlite3_value_int64(argv[0]);
136 seedRandomness(sizeof(r), &r, seed);
137 sqlite3_result_int64(context, r);
139 #endif