Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 89ecdf47bfd88ad64f7d624989424ba78ccd60b8 (plain) (blame)
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
/*******************************************************************************
 * Copyright (c) 2000, 2005 IBM Corporation and others.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at 
 * http://www.eclipse.org/legal/epl-v10.html
 * 
 * Contributors:
 *     IBM Corporation - initial API and implementation
 *******************************************************************************/

#include <stdlib.h>
#include <string.h>
#include "NgCommon.h"

/* Non-zero = big-endian architecture */
static BYTE4 hostIsMSB = 0;

/* Store last error msg */
#define MAX_MSG_SIZE 100
char errorMsg[MAX_MSG_SIZE];

/* Library initialization */
void NgInit()
{
	BYTE4 result = (BYTE4) 'A';
	
	/* determine the byte ordering of the host machine */
	hostIsMSB = (BYTE4) (*((char *) &result) != 'A');
	
	errorMsg[0] = 0;
}

/**
 * Memory allocation routine
 */
void *NgMalloc (UBYTE4 size) 
{
	return malloc (size);
}

/**
 * Memory allocation routine
 */
void NgFree (void *memblock) 
{
	if (memblock != NULL)
		free (memblock);
}

void NgMemSet (void *dest, UBYTE1 c, BYTE4 count)
{
	memset (dest, c, count);
}

void NgMemCpy (void *dest, void *src, BYTE4 count)
{
	memcpy (dest, src, count);
}

/**
 * Error Reporting
 */

ng_err_t NgError (ng_err_t error_type, char* msg) {
	if (msg != NULL)
	{
		/* Store a copy of the last error msg - truncate if necessary */
		size_t size = strlen (msg);
		if (size >= MAX_MSG_SIZE) size = MAX_MSG_SIZE - 1;
		NgMemCpy (errorMsg, msg, size);
		errorMsg[size] = 0;
	}	
	return error_type;
}

const char *NgGetLastErrorMsg()
{
	return errorMsg;
}

/**
 * Stream manipulation routines
 */
ng_err_t NgStreamInit (ng_stream_t *stream, char *fullname)
{
	stream->file = fopen (fullname, "rb");
	stream->size = 0;
	stream->pos = 0;
	if (stream->file == NULL) return NgError (ERR_NG, "Can't open file");
	return ERR_OK;
}

void NgStreamClose (ng_stream_t *stream)
{
	if (stream->file != NULL)
	{
		fclose (stream->file);
		stream->file = NULL;
	}
	stream->size = -1;
}

char NgStreamEof (ng_stream_t *stream) 
{
	return stream->size == -1;
}

BYTE4 NgStreamGetPosition (ng_stream_t *stream)
{
	return stream->pos;
}

BYTE4 NgStreamSkip (ng_stream_t *stream, BYTE4 nbr)
{
	if (stream->size == -1) return 0;
	if (fseek (stream->file, nbr, SEEK_CUR))
	{
		NgStreamClose (stream);
		return 0;
	}
	stream->pos += nbr;
	return nbr;
}

BYTE4 NgStreamRead (ng_stream_t *stream, char *buffer, BYTE4 nbr)
{
	size_t cnt;
	if (stream->size == -1) return 0;
	cnt = fread (buffer, sizeof (char), nbr, stream->file);
	if (cnt != nbr)
	{
		NgStreamClose (stream);
		return 0;
	}
	stream->pos += nbr;
	return nbr;
}

BYTE1 NgIsMSB()
{
	return hostIsMSB != 0;
}

UBYTE2 SystemToLittleEndianUBYTE2 (UBYTE2 value)
{
	return hostIsMSB ? ((value&0xFF) << 8)|((value&0xFF00)>>8) : value;
}

UBYTE4 SystemToLittleEndianUBYTE4 (UBYTE4 value)
{
	return hostIsMSB ? ((value&0xFF000000L)>>24)|((value&0xFF0000L)>>8) | ((value&0xFF00L)<<8) | ((value&0xFFL)<<24) : value;
}

UBYTE2 SystemToBigEndianUBYTE2 (UBYTE2 value)
{
	return hostIsMSB ? value : ((value&0xFF) << 8)|((value&0xFF00)>>8);
}

UBYTE2 LittleEndianToSystemUBYTE2 (UBYTE2 value)
{
	return hostIsMSB ? ((value&0xFF) << 8)|((value&0xFF00)>>8) : value;
}

UBYTE4 LittleEndianToSystemUBYTE4 (UBYTE4 value)
{
	return hostIsMSB ? ((value&0xFF000000L)>>24)|((value&0xFF0000L)>>8) | ((value&0xFF00L)<<8) | ((value&0xFFL)<<24) : value;
}

UBYTE2 BigEndianToSystemUBYTE2 (UBYTE2 value)
{
	return hostIsMSB ? value : ((value&0xFF) << 8)|((value&0xFF00)>>8);
}

UBYTE4 BigEndianToSystemUBYTE4 (UBYTE4 value)
{
	return hostIsMSB ? value : ((value&0xFF000000L)>>24)|((value&0xFF0000L)>>8)|((value&0xFF00L)<<8) | ((value&0xFFL)<<24);
}

Back to the top