File: conversions.m

package info (click to toggle)
gnustep-netclasses 1.06.dfsg%2Breally1.1.0-3
  • links: PTS, VCS
  • area: main
  • in suites: sid, trixie
  • size: 668 kB
  • sloc: objc: 4,272; makefile: 27; sh: 24
file content (103 lines) | stat: -rw-r--r-- 2,699 bytes parent folder | download | duplicates (4)
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
/***************************************************************************
                                conversions.m
                          -------------------
    begin                : Sun Dec 21 01:37:22 CST 2003
    copyright            : (C) 2003 by Andy Ruder
    email                : aeruder@ksu.edu
 ***************************************************************************/

/***************************************************************************
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU General Public License as published by  *
 *   the Free Software Foundation; either version 2 of the License, or     *
 *   (at your option) any later version.                                   *
 *                                                                         *
 ***************************************************************************/

#import <netclasses/NetTCP.h>
#import <netclasses/NetBase.h>
#import "testsuite.h"

#import <Foundation/Foundation.h>

NSString *num_to_hex_le(uint32_t num)
{
	unsigned char y[4];
	uint32_t *t;
	NSMutableString *string;
	int z;

	t = (uint32_t *)y;
	
	*t = num;

	string = [NSMutableString stringWithString: @"0x"];
	for (z = 3; z >= 0 ; z--)
	{
		[string appendString: [NSString stringWithFormat: @"%02x",
		  (unsigned)y[z]]];
	}
	
	return string;
}
	
int main(void)
{
	CREATE_AUTORELEASE_POOL(apr);
	TCPSystem *system;
	NSEnumerator *iter;
	id object;
	uint32_t num;
	NSDictionary *dict;
	
	system = [TCPSystem sharedInstance];

	NSLog(@"This is a cruddy test, it will only work correctly on machines"
	@" where host order != network order :)");
	dict = 
	  [NSDictionary dictionaryWithObjectsAndKeys:
	  @"0x4466dc75", @"68.102.220.117",
	  @"0x7f000001", @"127.0.0.1",
	  @"0xffffffff", @"255.255.255.255",
	  nil];

	iter = [dict keyEnumerator];

	while ((object = [iter nextObject]))
	{
		id val;

		val = [dict objectForKey: object];
		num = 0;
		[system hostOrderInteger: &num fromHost: [NSHost hostWithAddress: object]];
		testEqual(@"Host order",
		  num_to_hex_le(num), val);
	}

	dict = 
	  [NSDictionary dictionaryWithObjectsAndKeys:
	  @"0x75dc6644", @"68.102.220.117", 
	  @"0x0100007f", @"127.0.0.1",
	  @"0xffffffff", @"255.255.255.255",
	  nil];

	iter = [dict keyEnumerator];

	while ((object = [iter nextObject]))
	{
		id val;

		val = [dict objectForKey: object];
		num = 0;
		[system networkOrderInteger: &num fromHost: [NSHost hostWithAddress: object]];
		testEqual(@"Network order",
		  num_to_hex_le(num), val);
	}

	FINISH();

	RELEASE(apr);
	
	return 0;
}