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/>.
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);
83 ns
[strindex
] = '\0'; /* terminate it */
88 /* These next functions implement a repeatable random function with a user-provided seed */
90 seedRandomByte(uint32_t seed
)
94 if( !sqlite3Prng
.isInit
)
100 memset(&k
, '\0', sizeof(k
));
101 memcpy(&k
, &seed
, 4);
103 sqlite3Prng
.s
[i
] = 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 */
115 t
= sqlite3Prng
.s
[sqlite3Prng
.i
];
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
];
125 seedRandomness(int n
, void *pbuf
, uint32_t seed
)
127 unsigned char *zbuf
= pbuf
;
130 *(zbuf
++) = seedRandomByte(seed
);
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
)
140 seed
= sqlite3_value_int64(argv
[0]);
141 seedRandomness(sizeof(r
), &r
, seed
);
142 sqlite3_result_int64(context
, r
);