MiniDLNA update: 1.0.19.1 to 1.0.20
[tomato.git] / release / src / router / minidlna / tivo_utils.c
blob2ae6247aaeeb3b37c37050bdc811d87d58fa466c
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 <stdint.h>
22 #include <string.h>
23 #include <ctype.h>
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 if( ns )
78 free(ns);
79 return string;
81 else
83 ns[strindex] = '\0'; /* terminate it */
84 return ns;
88 /* These next functions implement a repeatable random function with a user-provided seed */
89 static int
90 seedRandomByte(uint32_t seed)
92 unsigned char t;
94 if( !sqlite3Prng.isInit )
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++)
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;
111 sqlite3Prng.isInit = 1;
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];
121 return sqlite3Prng.s[t];
124 void
125 seedRandomness(int n, void *pbuf, uint32_t seed)
127 unsigned char *zbuf = pbuf;
129 while( n-- )
130 *(zbuf++) = seedRandomByte(seed);
133 void
134 TiVoRandomSeedFunc(sqlite3_context *context, int argc, sqlite3_value **argv)
136 sqlite_int64 r, seed;
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);
144 #endif