File: gsUtilPSP.c

package info (click to toggle)
openmohaa 0.82.1%2Bdfsg-1
  • links: PTS, VCS
  • area: contrib
  • in suites: forky, sid
  • size: 34,192 kB
  • sloc: cpp: 315,720; ansic: 275,789; sh: 312; xml: 246; asm: 141; makefile: 7
file content (275 lines) | stat: -rw-r--r-- 6,332 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
#if defined(_PSP)
///////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////
#include "../gsCommon.h"


///////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////
#if !defined(GSI_NO_THREADS)

static void gsiResolveHostnameThread(void * arg)
{
	int result = 0;
	int resolverID = 0;
	char buf[1024]; // PSP documentation recommends 1024
	in_addr addr;
	GSIResolveHostnameHandle * info = (GSIResolveHostnameHandle)arg;

	result = sceNetResolverCreate(&resolverID, buf, sizeof(buf));
	if (result < 0)
	{
		// failed to create resolver, did you call sceNetResolverInit() ?
		info->ip = GSI_ERROR_RESOLVING_HOSTNAME;
		return -1;
	}
	else
	{
		// this will block until completed
		result = sceNetResolverStartNtoA(resolverID, info->hostname, &addr, &info->ip, GSI_RESOLVER_TIMEOUT, GSI_RESOLVER_RETRY);
		if (result < 0)
			info->ip = GSI_ERROR_RESOLVING_HOSTNAME;
		sceNetResolverDelete(resolverID);
	}
}

int gsiStartResolvingHostname(const char * hostname, GSIResolveHostnameHandle * handle)
{
	GSIResolveHostnameInfo * info;

	// allocate a handle
	info = (GSIResolveHostnameInfo *)gsimalloc(sizeof(GSIResolveHostnameInfo));
	if(!info)
		return -1;

	// make a copy of the hostname so the thread has access to it
	info->hostname = goastrdup(hostname);
	if(!info->hostname)
	{
		gsifree(info);
		return -1;
	}

	// not resolved yet
	info->finishedResolving = 0;

	// start the thread
	if(gsiStartThread(gsiResolveHostnameThread, (0x1000), info, &info->threadID) == -1)
	{
		gsifree(info->hostname);
		gsifree(info);
		return -1;
	}

	// set the handle to the info
	*handle = info;

	return 0;
}

void gsiCancelResolvingHostname(GSIResolveHostnameHandle handle)
{
	if (0 == handle->finishedResolving)
	{
		sceNetResolverStop(handle->resolverID); // safe to call from separate thread
		gsiCancelThread(handle->threadID);
	}
}

unsigned int gsiGetResolvedIP(GSIResolveHostnameHandle handle)
{
	return handle->ip;
}

#endif // (GSI_NO_THREADS)


///////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////
#if defined(UNIQUEID)

static const char * GetMAC(void)
{
	static struct SceNetEtherAddr mac;
	int result = 0;

	result = sceNetGetLocalEtherAddr(&mac);
	if (result == 0)
		return (char *)&mac.data;
	else
		return NULL;
}

const char * GOAGetUniqueID_Internal(void)
{
	static char keyval[17];
	const char * MAC;

	// check if we already have the Unique ID
	if(keyval[0])
		return keyval;

	// get the MAC
	MAC = GetMAC();
	if(!MAC)
	{
		// error getting the MAC
		static char errorMAC[6] = { 1, 2, 3, 4, 5, 6 };
		MAC = errorMAC;
	}

	// format it
	sprintf(keyval, "%02X%02X%02X%02X%02X%02X0000",
		MAC[0] & 0xFF,
		MAC[1] & 0xFF,
		MAC[2] & 0xFF,
		MAC[3] & 0xFF,
		MAC[4] & 0xFF,
		MAC[5] & 0xFF);

	return keyval;
}

#endif

//NEED These again since MAX INTEGRAL BITS is not defined 
#define GSI_MIN_I64       LONG_LONG_MIN
#define GSI_MAX_I64       LONG_LONG_MAX
#define GSI_MAX_U64       ULONG_LONG_MAX
/* flag values */
#define FL_UNSIGNED   1       /* strtouq called */
#define FL_NEG        2       /* negative sign found */
#define FL_OVERFLOW   4       /* overflow occured */
#define FL_READDIGIT  8       /* we've read at least one correct digit */

gsi_i64 gsiStringToInt64(const char *theNumberStr)
{
	const char *p;
	char c;
	gsi_i64 number;
	unsigned digval;
	gsi_u64 maxval;
	int flags = 0;
	// Added for compatibility reasons
	int ibase = 10;
	
	p = theNumberStr; // p is our scanning pointer
	number = 0;       // start with zero

	// read char 
	c = *p++;            

	// skip whitespace 
	while ( isspace(c) )
		c = *p++;        

	 
	if (c == '-') {
		flags |= FL_NEG;    // remember minus sign
		c = *p++;
	}
	// skip sign
	else if (c == '+')
		c = *p++;        

	if (ibase == 0) 
	{
		// determine base free-lance, based on first two chars of
		// string
		if (c != '0')
			ibase = 10;
		else if (*p == 'x' || *p == 'X')
			ibase = 16;
		else
			ibase = 8;
	}

	if (ibase == 16) 
	{
		// we might have 0x in front of number; remove if there
		if (c == '0' && (*p == 'x' || *p == 'X')) {
			++p;
			c = *p++;    /* advance past prefix */
		}
	}

	// if our number exceeds this, we will overflow on multiply 
	maxval = GSI_MAX_U64 / ibase;


	// exit in middle of loop
	for (;;) 
	{    
		// convert c to value
		if ( isdigit(c) )
			digval = c - '0';
		else if ( isalpha(c) )
			digval = toupper(c) - 'A' + 10;
		else
			break;

		// exit loop if bad digit found
		if (digval >= (unsigned)ibase)
			break;        

		/* record the fact we have read one digit */
		flags |= FL_READDIGIT;

		// we now need to compute number = number * base + digval,
		// but we need to know if overflow occurred.  This requires
		// a tricky pre-check.

		if (number < maxval || (number == maxval &&
			(gsi_u64)digval <= GSI_MAX_U64 % ibase)) 
		{
				// we won't overflow, go ahead and multiply
				number = number * ibase + digval;
		}
		else 
		{
			// we have overflowed, set the flag
			flags |= FL_OVERFLOW;
			break;
		}

		c = *p++;        /* read next digit */
	}

	--p;                /* point to place that stopped scan */
	
	if (!(flags & FL_READDIGIT)) {
		/* no number there; return 0 and point to beginning of
		string */
		number = 0L;        /* return 0 */
	}
	else if ( (flags & FL_OVERFLOW) ||
		( !(flags & FL_UNSIGNED) &&
		( ( (flags & FL_NEG) && (number > GSI_MIN_I64) ) ||
		( !(flags & FL_NEG) && (number > GSI_MAX_I64) ) ) ) )
	{
		/* overflow or signed overflow occurred */
		errno = ERANGE;
		if ( flags & FL_NEG )
			number = GSI_MIN_I64;
		else
			number = GSI_MAX_I64;
	}
	
	if (flags & FL_NEG)
		/* negate result if there was a neg sign */
		number = -number;

	return number;            /* done. */
}

void gsiInt64ToString(char theNumberStr[33], gsi_i64 theNumber)
{
	// you want to fit the number! 
	// give me a valid string!
	GS_ASSERT(theNumberStr != NULL);

	sprintf(theNumberStr, "%lld", theNumber);
}
///////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////
#endif // _PSP only