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/>.
23 #include <linux/types.h> // Defines __u32
25 #include "tivo_utils.h"
27 /* This function based on byRequest */
29 decodeString(char * string
, int inplace
)
33 int alloc
= (int)strlen(string
)+1;
41 if( !(ns
= malloc(alloc
)) )
48 if((in
== '%') && isxdigit(string
[1]) && isxdigit(string
[2]))
50 /* this is two hexadecimal digits following a '%' */
53 hexstr
[0] = string
[1];
54 hexstr
[1] = string
[2];
57 hex
= strtol(hexstr
, &ptr
, 16);
59 in
= (unsigned char)hex
; /* this long is never bigger than 255 anyway */
63 memmove(string
+1, string
+3, alloc
-2);
81 ns
[strindex
] = '\0'; /* terminate it */
86 /* These next functions implement a repeatable random function with a user-provided seed */
88 seedRandomByte(__u32 seed
) {
90 if( !sqlite3Prng
.isInit
)
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 */
111 t
= sqlite3Prng
.s
[sqlite3Prng
.i
];
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
];
121 seedRandomness(int N
, void *pBuf
, __u32 seed
){
122 unsigned char *zBuf
= pBuf
;
125 *(zBuf
++) = seedRandomByte(seed
);
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
)
135 seed
= sqlite3_value_int64(argv
[0]);
136 seedRandomness(sizeof(r
), &r
, seed
);
137 sqlite3_result_int64(context
, r
);