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/>.
26 #include <sys/types.h>
28 #include "tivo_utils.h"
30 /* This function based on byRequest */
32 decodeString(char *string
, int inplace
)
36 int alloc
= (int)strlen(string
)+1;
44 if( !(ns
= malloc(alloc
)) )
51 if((in
== '%') && isxdigit(string
[1]) && isxdigit(string
[2]))
53 /* this is two hexadecimal digits following a '%' */
56 hexstr
[0] = string
[1];
57 hexstr
[1] = string
[2];
60 hex
= strtol(hexstr
, &ptr
, 16);
62 in
= (unsigned char)hex
; /* this long is never bigger than 255 anyway */
66 memmove(string
+1, string
+3, alloc
-2);
85 ns
[strindex
] = '\0'; /* terminate it */
90 /* These next functions implement a repeatable random function with a user-provided seed */
92 seedRandomByte(uint32_t seed
)
96 if( !sqlite3Prng
.isInit
)
102 memset(&k
, '\0', sizeof(k
));
103 memcpy(&k
, &seed
, 4);
105 sqlite3Prng
.s
[i
] = i
;
108 sqlite3Prng
.j
+= sqlite3Prng
.s
[i
] + k
[i
];
109 t
= sqlite3Prng
.s
[sqlite3Prng
.j
];
110 sqlite3Prng
.s
[sqlite3Prng
.j
] = sqlite3Prng
.s
[i
];
111 sqlite3Prng
.s
[i
] = t
;
113 sqlite3Prng
.isInit
= 1;
115 /* Generate and return single random byte */
117 t
= sqlite3Prng
.s
[sqlite3Prng
.i
];
119 sqlite3Prng
.s
[sqlite3Prng
.i
] = sqlite3Prng
.s
[sqlite3Prng
.j
];
120 sqlite3Prng
.s
[sqlite3Prng
.j
] = t
;
121 t
+= sqlite3Prng
.s
[sqlite3Prng
.i
];
123 return sqlite3Prng
.s
[t
];
127 seedRandomness(int n
, void *pbuf
, uint32_t seed
)
129 unsigned char *zbuf
= pbuf
;
132 *(zbuf
++) = seedRandomByte(seed
);
136 TiVoRandomSeedFunc(sqlite3_context
*context
, int argc
, sqlite3_value
**argv
)
140 if( argc
!= 1 || sqlite3_value_type(argv
[0]) != SQLITE_INTEGER
)
142 seed
= sqlite3_value_int64(argv
[0]);
143 seedRandomness(sizeof(r
), &r
, seed
);
144 sqlite3_result_int64(context
, r
);
148 is_tivo_file(const char *path
)
150 unsigned char buf
[5];
151 unsigned char hdr
[5] = { 'T','i','V','o','\0' };
154 /* read file header */
155 fd
= open(path
, O_RDONLY
);
158 if( read(fd
, buf
, 5) < 5 )
162 return !memcmp(buf
, hdr
, 5);