VISIR Pipeline Reference Manual  4.1.0
visir_serialize.c
1 /* $Id: visir_serialize.c,v 1.2 2013-01-15 11:04:42 jtaylor Exp $
2  *
3  * This file is part of the VISIR Pipeline
4  * Copyright (C) 2012 European Southern Observatory
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software
18  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02111-1307 USA
19  */
20 
21 /*
22  * $Author: jtaylor $
23  * $Date: 2013-01-15 11:04:42 $
24  * $Revision: 1.2 $
25  * $Name: not supported by cvs2svn $
26  */
27 
28 #ifdef HAVE_CONFIG_H
29 #include "config.h"
30 #endif
31 #include "visir_utils.h"
32 
33 #include <cpl.h>
34 
35 #include <string.h>
36 #include <stddef.h>
37 #include <stdint.h>
38 #include <arpa/inet.h>
39 
40 typedef struct {
41  size_t size;
42  char * base_buffer;
43  char * pos;
44 } visir_stream;
45 
46 static visir_stream *
47 visir_stream_new(char * block, size_t size)
48 {
49  visir_stream * stream = cpl_malloc(sizeof(visir_stream));
50  stream->size = size;
51  stream->base_buffer = block;
52  stream->pos = stream->base_buffer;
53  return stream;
54 }
55 
56 static size_t
57 visir_stream_get_size(visir_stream * stream)
58 {
59  return stream->pos - stream->base_buffer;
60 }
61 
62 static void
63 visir_stream_ensure_space(visir_stream * stream, size_t extra)
64 {
65  const size_t nused = visir_stream_get_size(stream);
66 
67  if (stream->size < (nused + extra)) {
68  stream->base_buffer = cpl_realloc(stream->base_buffer,
69  stream->size * 2);
70  stream->size *= 2;
71  stream->pos = stream->base_buffer + nused;
72  }
73 }
74 
75 static void
76 serialize_uint32(visir_stream * stream, uint32_t v_)
77 {
78  uint32_t v = htonl(v_);
79 
80  visir_stream_ensure_space(stream, sizeof(v));
81  memcpy(stream->pos, &v, sizeof(v));
82  stream->pos += sizeof(v);
83 }
84 
85 static void
86 serialize_string(visir_stream * stream, const char * s)
87 {
88  size_t n = strlen(s);
89  serialize_uint32(stream, n);
90  visir_stream_ensure_space(stream, n);
91  memcpy(stream->pos, s, n);
92  stream->pos += n;
93 }
94 
95 
96 static uint32_t
97 deserialize_uint32(visir_stream * stream)
98 {
99  uint32_t r;
100  memcpy(&r, stream->pos, sizeof(r));
101  stream->pos += sizeof(r);
102  return ntohl(r);
103 }
104 
105 static char *
106 deserialize_string(visir_stream * stream)
107 {
108  uint32_t n = deserialize_uint32(stream);
109  char * s = cpl_malloc(n + 1);
110  memcpy(s, stream->pos, n);
111  s[n] = 0;
112  stream->pos += n;
113  return s;
114 }
115 
116 char *
117 visir_frameset_serialize(const cpl_frameset * frames, size_t * size)
118 {
119  char * block = NULL;
120  visir_stream * stream = NULL;
121 
122  cpl_ensure(size != NULL, CPL_ERROR_NULL_INPUT, NULL);
123  cpl_ensure(frames != NULL, CPL_ERROR_NULL_INPUT, NULL);
124 
125  block = cpl_malloc(1000);
126  stream = visir_stream_new(block, 1000);
127 
128  serialize_uint32(stream, cpl_frameset_get_size(frames));
129  FOR_EACH_FRAMESET_C(frm, frames) {
130  serialize_uint32(stream, cpl_frame_get_type(frm));
131  serialize_uint32(stream, cpl_frame_get_group(frm));
132  serialize_uint32(stream, cpl_frame_get_level(frm));
133  serialize_string(stream, cpl_frame_get_tag(frm));
134  serialize_string(stream, cpl_frame_get_filename(frm));
135  }
136 
137  *size = visir_stream_get_size(stream);
138  block = stream->base_buffer;
139  cpl_free(stream);
140 
141  return block;
142 }
143 
144 cpl_frameset *
145 visir_frameset_deserialize(char * block, size_t * size)
146 {
147  visir_stream * stream = visir_stream_new(block, 0);
148  uint32_t nframes = deserialize_uint32(stream);
149  cpl_frameset * frames = cpl_frameset_new();
150 
151  for (uint32_t i = 0; i < nframes; i++) {
152  uint32_t type = deserialize_uint32(stream);
153  uint32_t group = deserialize_uint32(stream);
154  uint32_t level = deserialize_uint32(stream);
155  char * tag = deserialize_string(stream);
156  char * filename = deserialize_string(stream);
157  cpl_frame * frm = cpl_frame_new();
158  cpl_frame_set_type(frm, type);
159  cpl_frame_set_group(frm, group);
160  cpl_frame_set_level(frm, level);
161  cpl_frame_set_filename(frm, filename);
162  cpl_frame_set_tag(frm, tag);
163  cpl_free(filename);
164  cpl_free(tag);
165  cpl_frameset_insert(frames, frm);
166  }
167 
168  if (size)
169  *size = visir_stream_get_size(stream);
170 
171  cpl_free(stream);
172  return frames;
173 }
174 
175 /* convinience function to send framset and error code over a stream */
176 cpl_error_code
177 visir_send_frameset(FILE * stream, const cpl_frameset * frames)
178 {
179  size_t size;
180  char * block = visir_frameset_serialize(frames, &size);
181  cpl_error_code err = cpl_error_get_code();
182 
183  skip_if(fwrite(&err, sizeof(err), 1, stream) != 1);
184  skip_if(fwrite(&size, sizeof(size), 1, stream) != 1);
185  skip_if(fwrite(block, size, 1, stream) != 1);
186 
187  end_skip;
188 
189  cpl_free(block);
190  return cpl_error_get_code();
191 }