1 /*
2  *
3  *  Copyright (C) 1994-2001, OFFIS
4  *
5  *  This software and supporting documentation were developed by
6  *
7  *    Kuratorium OFFIS e.V.
8  *    Healthcare Information and Communication Systems
9  *    Escherweg 2
10  *    D-26121 Oldenburg, Germany
11  *
12  *  THIS SOFTWARE IS MADE AVAILABLE,  AS IS,  AND OFFIS MAKES NO  WARRANTY
13  *  REGARDING  THE  SOFTWARE,  ITS  PERFORMANCE,  ITS  MERCHANTABILITY  OR
14  *  FITNESS FOR ANY PARTICULAR USE, FREEDOM FROM ANY COMPUTER DISEASES  OR
15  *  ITS CONFORMITY TO ANY SPECIFICATION. THE ENTIRE RISK AS TO QUALITY AND
16  *  PERFORMANCE OF THE SOFTWARE IS WITH THE USER.
17  *
18  *  Module:  dcmdata
19  *
20  *  Author:  Andreas Barth
21  *
22  *  Purpose: byte order functions
23  *
24  */
25 
26 #include "osconfig.h"    /* make sure OS specific configuration is included first */
27 #include "dcswap.h"
28 
swapIfNecessary(const E_ByteOrder newByteOrder,const E_ByteOrder oldByteOrder,void * value,const Uint32 byteLength,const size_t valWidth)29 OFCondition swapIfNecessary(const E_ByteOrder newByteOrder,
30                             const E_ByteOrder oldByteOrder,
31                             void * value, const Uint32 byteLength,
32                             const size_t valWidth)
33     /*
34      * This function swaps byteLength bytes in value if newByteOrder and oldByteOrder
35      * differ from each other. In case bytes have to be swapped, these bytes are seperated
36      * in valWidth elements which will be swapped seperately.
37      *
38      * Parameters:
39      *   newByteOrder - [in] The new byte ordering (little or big endian).
40      *   oldByteOrder - [in] The current old byte ordering (little or big endian).
41      *   value        - [in] Array that contains the actual bytes which might have to be swapped.
42      *   byteLength   - [in] Length of the above array.
43      *   valWidth     - [in] Specifies how many bytes shall be treated together as one element.
44      */
45 {
46     /* if the two byte orderings are unknown this is an illegal call */
47     if (oldByteOrder != EBO_unknown && newByteOrder != EBO_unknown )
48     {
49         /* and if they differ from each other and valWidth is not 1 */
50         if (oldByteOrder != newByteOrder && valWidth != 1)
51         {
52             /* in case the array length equals valWidth and only 2 or 4 bytes have to be swapped */
53             /* we can swiftly swap these bytes by calling the corresponding functions. If this is */
54             /* not the case we have to call a more sophisticated function. */
55             if (byteLength == valWidth)
56             {
57                 if (valWidth == 2)
58                     swap2Bytes((Uint8 *)value);
59                 else if (valWidth == 4)
60                     swap4Bytes((Uint8 *)value);
61                 else
62                     swapBytes(value, byteLength, valWidth);
63             }
64             else
65                 swapBytes(value, byteLength, valWidth);
66         }
67         return EC_Normal;
68     }
69     return EC_IllegalCall;
70 }
71 
72 
swapBytes(void * value,const Uint32 byteLength,const size_t valWidth)73 void swapBytes(void * value, const Uint32 byteLength,
74                            const size_t valWidth)
75     /*
76      * This function swaps byteLength bytes in value. These bytes are seperated
77      * in valWidth elements which will be swapped seperately.
78      *
79      * Parameters:
80      *   value        - [in] Array that contains the actual bytes which might have to be swapped.
81      *   byteLength   - [in] Length of the above array.
82      *   valWidth     - [in] Specifies how many bytes shall be treated together as one element.
83      */
84 {
85     /* use register (if available) to increase speed */
86     register Uint8 save;
87 
88     /* in case valWidth equals 2, swap correspondingly */
89     if (valWidth == 2)
90     {
91         register Uint8 * first = &((Uint8*)value)[0];
92         register Uint8 * second = &((Uint8*)value)[1];
93         register Uint32 times = byteLength/2;
94         while (times--)
95         {
96             save = *first;
97             *first = *second;
98             *second = save;
99             first +=2;
100             second +=2;
101         }
102     }
103     /* if valWidth is greater than 2, swap correspondingly */
104     else if (valWidth > 2)
105     {
106         register size_t i;
107         const size_t halfWidth = valWidth/2;
108         const size_t offset = valWidth-1;
109         register Uint8 *start;
110         register Uint8 *end;
111 
112         Uint32 times = byteLength/valWidth;
113         Uint8  *base = (Uint8 *)value;
114 
115         while (times--)
116         {
117             i = halfWidth;
118             start = base;
119             end = base+offset;
120             while (i--)
121             {
122                 save = *start;
123                 *start++ = *end;
124                 *end-- = save;
125             }
126             base += valWidth;
127         }
128     }
129 }
130 
131 
swapShort(const Uint16 toSwap)132 Uint16 swapShort(const Uint16 toSwap)
133 {
134         Uint8 * swapped = (Uint8 *)&toSwap;
135         swap2Bytes(swapped);
136         return * ((Uint16*)swapped);
137 }
138