OB.DAAC Logo
NASA Logo
Ocean Color Science Software

ocssw V2022
epr_swap.c
Go to the documentation of this file.
1 /*
2  * $Id: epr_swap.c,v 1.1.1.1 2004-10-28 19:22:23 norman Exp $
3  *
4  * Copyright (C) 2002 by Brockmann Consult (info@brockmann-consult.de)
5  *
6  * This program is free software; you can redistribute it and/or modify it
7  * under the terms of the GNU General Public License as published by the
8  * Free Software Foundation. This program is distributed in the hope it will
9  * be useful, but WITHOUT ANY WARRANTY; without even the implied warranty
10  * of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
11  * See the GNU General Public License for more details.
12  *
13  * You should have received a copy of the GNU General Public License
14  * along with this program; if not, write to the Free Software
15  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
16  */
17 
18 #include <assert.h>
19 #include <errno.h>
20 #include <stdio.h>
21 #include <stdlib.h>
22 #include <string.h>
23 
24 #include "epr_api.h"
25 #include "epr_core.h"
26 #include "epr_field.h"
27 
28 
29 /*
30  * Function: byte_swap_short.c
31 */
43 void byte_swap_short(short *buffer, epr_uint number_of_swaps)
44 {
45  short* temp = buffer;
46  epr_uint swap_loop;
47 
48  for (swap_loop = 0, temp = buffer; swap_loop < number_of_swaps; swap_loop++, temp++) {
49  *temp = (short)(((*temp & 0x00ff) << 8) |
50  ((*temp & 0xff00) >> 8));
51  }
52 }
53 
54 
55 /*
56  Function: byte_swap_int.c
57 */
65 void byte_swap_int(int *buffer, epr_uint number_of_swaps)
66 {
67  int *temp = buffer;
68  epr_uint swap_loop;
69 
70  for (swap_loop = 0, temp = buffer; swap_loop < number_of_swaps; swap_loop++, temp++) {
71  *temp = ((*temp & 0x000000ff) << 24) |
72  ((*temp & 0x0000ff00) << 8) |
73  ((*temp & 0x00ff0000) >> 8) |
74  ((*temp & 0xff000000) >> 24);
75  }
76 }
77 
78 
79 /*
80  Function: byte_swap_short.c
81 */
93 void byte_swap_ushort(epr_ushort* buffer, epr_uint number_of_swaps)
94 {
95  byte_swap_short((short*) buffer, number_of_swaps);
96 }
97 
98 /*
99  * Function: byte_swap_uint.c
100  */
112 void byte_swap_uint(epr_uint* buffer, epr_uint number_of_swaps)
113 {
114  byte_swap_int((int*) buffer, number_of_swaps);
115 }
116 
117 /*
118  * Function: byte_swap_int.c
119  */
131 void byte_swap_float(float* buffer, epr_uint number_of_swaps)
132 {
133  byte_swap_int((int*) buffer, number_of_swaps);
134 }
135 
142 /*
143  Function: epr_is_little_endian_order
144  Access: public API
145  Changelog: 2002/02/04 nf nitial version
146  */
153 {
154  epr_uint le_value = EPR_LE_MAGIC_NUMBER;
155  return (((epr_uchar*)(&le_value))[0] == EPR_LE_MAGIC_BYTE_0)
156  && (((epr_uchar*)(&le_value))[1] == EPR_LE_MAGIC_BYTE_1)
157  && (((epr_uchar*)(&le_value))[2] == EPR_LE_MAGIC_BYTE_2)
158  && (((epr_uchar*)(&le_value))[3] == EPR_LE_MAGIC_BYTE_3);
159 }
160 
161 /*
162  Function: epr_is_big_endian_order
163  Access: public API
164  Changelog: 2002/02/04 nf nitial version
165  */
172 {
173  epr_uint be_value = EPR_BE_MAGIC_NUMBER;
174  return (((epr_uchar*)(&be_value))[0] == EPR_LE_MAGIC_BYTE_0)
175  && (((epr_uchar*)(&be_value))[1] == EPR_LE_MAGIC_BYTE_1)
176  && (((epr_uchar*)(&be_value))[2] == EPR_LE_MAGIC_BYTE_2)
177  && (((epr_uchar*)(&be_value))[3] == EPR_LE_MAGIC_BYTE_3);
178 }
179 
180 
181 /*
182  Function: epr_swap_endian_order
183  Access: public API
184  Changelog: 2002/02/04 mp nitial version
185  */
192 void epr_swap_endian_order(const EPR_SField* field)
193 {
194  switch (field->info->data_type_id) {
195  case e_tid_uchar:
196  case e_tid_char:
197  case e_tid_string:
198  /* no conversion required */
199  break;
200  case e_tid_time:
201  byte_swap_uint((epr_uint*)field->elems, 3);
202  break;
203  case e_tid_spare:
204  /* no conversion required */
205  break;
206  case e_tid_ushort:
207  byte_swap_ushort((epr_ushort*) field->elems, field->info->num_elems);
208  break;
209  case e_tid_short:
210  byte_swap_short((short*) field->elems, field->info->num_elems);
211  break;
212  case e_tid_uint:
213  byte_swap_uint((epr_uint*) field->elems, field->info->num_elems);
214  break;
215  case e_tid_int:
216  byte_swap_int((int*) field->elems, field->info->num_elems);
217  break;
218  case e_tid_float:
219  byte_swap_float((float*) field->elems, field->info->num_elems);
220  break;
221  case e_tid_double:
223  "epr_swap_endian_order: DOUBLE type was not yet processed");
224  break;
225  default:
227  "epr_swap_endian_order: unknown data type");
228  }
229 }
unsigned int epr_uint
Definition: epr_api.h:188
#define EPR_LE_MAGIC_BYTE_1
Definition: epr_core.h:70
unsigned short epr_ushort
Definition: epr_api.h:187
#define EPR_LE_MAGIC_BYTE_3
Definition: epr_core.h:72
void byte_swap_float(float *buffer, epr_uint number_of_swaps)
Definition: epr_swap.c:131
void byte_swap_uint(epr_uint *buffer, epr_uint number_of_swaps)
Definition: epr_swap.c:112
@ e_tid_float
Definition: epr_api.h:60
@ e_err_invalid_data_format
Definition: epr_api.h:107
@ e_tid_int
Definition: epr_api.h:58
@ e_tid_ushort
Definition: epr_api.h:52
int epr_is_little_endian_order()
Definition: epr_swap.c:152
@ e_tid_uint
Definition: epr_api.h:56
@ e_tid_spare
Definition: epr_api.h:66
@ e_tid_char
Definition: epr_api.h:50
void epr_set_err(EPR_EErrCode err_code, const char *err_message)
Definition: epr_core.c:221
@ e_tid_double
Definition: epr_api.h:62
void epr_swap_endian_order(const EPR_SField *field)
Definition: epr_swap.c:192
@ e_tid_string
Definition: epr_api.h:64
#define EPR_LE_MAGIC_BYTE_0
Definition: epr_core.h:69
@ e_tid_short
Definition: epr_api.h:54
#define EPR_LE_MAGIC_NUMBER
Definition: epr_core.h:68
void byte_swap_ushort(epr_ushort *buffer, epr_uint number_of_swaps)
Definition: epr_swap.c:93
@ e_tid_time
Definition: epr_api.h:68
#define EPR_BE_MAGIC_NUMBER
Definition: epr_core.h:67
#define EPR_LE_MAGIC_BYTE_2
Definition: epr_core.h:71
void byte_swap_int(int *buffer, epr_uint number_of_swaps)
Definition: epr_swap.c:65
unsigned char epr_uchar
Definition: epr_api.h:186
@ e_tid_uchar
Definition: epr_api.h:48
int epr_is_big_endian_order()
Definition: epr_swap.c:171
void byte_swap_short(short *buffer, epr_uint number_of_swaps)
Definition: epr_swap.c:43