• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /**********************************************************************
2  * File:        serialis.h  (Formerly serialmac.h)
3  * Description: Inline routines and macros for serialisation functions
4  * Author:      Phil Cheatle
5  * Created:     Tue Oct 08 08:33:12 BST 1991
6  *
7  * (C) Copyright 1990, Hewlett-Packard Ltd.
8  ** Licensed under the Apache License, Version 2.0 (the "License");
9  ** you may not use this file except in compliance with the License.
10  ** You may obtain a copy of the License at
11  ** http://www.apache.org/licenses/LICENSE-2.0
12  ** Unless required by applicable law or agreed to in writing, software
13  ** distributed under the License is distributed on an "AS IS" BASIS,
14  ** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  ** See the License for the specific language governing permissions and
16  ** limitations under the License.
17  *
18  **********************************************************************/
19 
20 #include          "mfcpch.h"     //precompiled headers
21 #include "serialis.h"
22 #include "scanutils.h"
23 
24 /* **************************************************************************
25 
26 These are the only routines that write/read data to/from the serialisation.
27 
28 "serialise_bytes" and "de_serialise_bytes" are used to serialise NON class
29 items.  The "make_serialise" macro generates "serialise" and "de_serialise"
30 member functions for the class name specified in the macro parameter.
31 
32 ************************************************************************** */
33 
de_serialise_bytes(FILE * f,int size)34 DLLSYM void *de_serialise_bytes(FILE *f, int size) {
35   void *ptr;
36 
37   ptr = alloc_mem (size);
38   /*
39   printf( "De_serialising bytes\n" );
40   printf( "  Addr: %d    Size: %d\n", int(ptr), size );
41   */
42   if (fread (ptr, size, 1, f) != 1)
43     READFAILED.error ("de_serialise_bytes", ABORT, NULL);
44   return ptr;
45 }
46 
47 
serialise_bytes(FILE * f,void * ptr,int size)48 DLLSYM void serialise_bytes(FILE *f, void *ptr, int size) {
49   /*
50   printf( "Serialising bytes\n" );
51   printf( "  Addr: %d    Size: %d\n", int(ptr), size );
52   */
53   if (fwrite (ptr, size, 1, f) != 1)
54     WRITEFAILED.error ("serialise_bytes", ABORT, NULL);
55 }
56 
57 
serialise_INT32(FILE * f,inT32 the_int)58 DLLSYM void serialise_INT32(FILE *f, inT32 the_int) {
59   if (fprintf (f, INT32FORMAT "\n", the_int) < 0)
60     WRITEFAILED.error ("serialise_INT32", ABORT, NULL);
61 }
62 
63 
de_serialise_INT32(FILE * f)64 DLLSYM inT32 de_serialise_INT32(FILE *f) {
65   inT32 the_int;
66 
67   if (fscanf (f, INT32FORMAT, &the_int) != 1)
68     READFAILED.error ("de_serialise_INT32", ABORT, NULL);
69   return the_int;
70 }
71 
72 
serialise_FLOAT64(FILE * f,double the_float)73 DLLSYM void serialise_FLOAT64(FILE *f, double the_float) {
74   if (fprintf (f, "%g\n", the_float) < 0)
75     WRITEFAILED.error ("serialise_FLOAT64", ABORT, NULL);
76 }
77 
78 
de_serialise_FLOAT64(FILE * f)79 DLLSYM double de_serialise_FLOAT64(FILE *f) {
80   double the_float;
81 
82   if (fscanf (f, "%lg", &the_float) != 1)
83     READFAILED.error ("de_serialise_FLOAT64", ABORT, NULL);
84   return the_float;
85 }
86 
87 // Byte swap an inT64 or uinT64.
reverse64(uinT64 num)88 DLLSYM uinT64 reverse64(uinT64 num) {
89   return ((uinT64)reverse32((uinT32)(num & 0xffffffff)) << 32)
90     | reverse32((uinT32)((num >> 32) & 0xffffffff));
91 }
92 
93 /**********************************************************************
94  * reverse32
95  *
96  * Byte swap an inT32 or uinT32.
97  **********************************************************************/
98 
reverse32(uinT32 num)99 DLLSYM uinT32 reverse32(            //switch endian
100                         uinT32 num  //number to fix
101                        ) {
102   return (reverse16 ((uinT16) (num & 0xffff)) << 16)
103     | reverse16 ((uinT16) ((num >> 16) & 0xffff));
104 }
105 
106 
107 /**********************************************************************
108  * reverse16
109  *
110  * Byte swap an inT16 or uinT16.
111  **********************************************************************/
112 
reverse16(uinT16 num)113 DLLSYM uinT16 reverse16(            //switch endian
114                         uinT16 num  //number to fix
115                        ) {
116   return ((num & 0xff) << 8) | ((num >> 8) & 0xff);
117 }
118