File: serializeio.h

package info (click to toggle)
xdelta 1.0.0-1
  • links: PTS
  • area: main
  • in suites: slink
  • size: 876 kB
  • ctags: 696
  • sloc: ansic: 5,154; sh: 4,717; lisp: 777; makefile: 64
file content (169 lines) | stat: -rw-r--r-- 7,384 bytes parent folder | download
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
/* -*-Mode: C;-*-
 * $Id: serializeio.h 1.15 Thu, 01 Oct 1998 04:56:35 -0700 jmacd $
 *
 * Copyright (C) 1998, Josh MacDonald.
 * All Rights Reserved.
 *
 * Author: Josh MacDonald <jmacd@CS.Berkeley.EDU>
 */

#ifndef _SERIALIZEIO_H_
#define _SERIALIZEIO_H_

#ifdef HAVE_CONFIG_H
#include <config.h>
#endif

#include <glib.h>

#if HAVE_SSLEAY
#include <stdio.h>
/* zlib typedefs free_func! */
#define free_func ssl_free_func

#include <ssl.h>

#else

#define SSL_ERROR_NONE			0
#define SSL_ERROR_SSL			1
#define SSL_ERROR_WANT_READ		2
#define SSL_ERROR_WANT_WRITE		3
#define SSL_ERROR_WANT_X509_LOOKUP	4
#define SSL_ERROR_SYSCALL		5 /* look at errno */
#define SSL_ERROR_ZERO_RETURN		6
#define SSL_ERROR_WANT_CONNECT		7

#endif

typedef struct _SerialSource    SerialSource;
typedef struct _SerialSink      SerialSink;
typedef enum   _SerialStatus    SerialStatus;
typedef guint32                 SerialType;
typedef struct _HandleFuncTable HandleFuncTable;

#if !defined(HAVE_FILE_HANDLE_TYPEDEF)
typedef struct _FileHandle    FileHandle;
#define HAVE_FILE_HANDLE_TYPEDEF
#endif

#define ST_Error 0
#define ST_NotFound 1

#define SER_LIBRARY_OFFSET_BITS 8
#define SER_LIBRARY_OFFSET_MASK 0xff

#define ALIGN_8(v) if (((v) % 8) != 0) { (v) += 8; (v) &= ~7; }

enum _SerialStatus {
  SerialSuccess    = SSL_ERROR_NONE,
  SerialWantWrite  = SSL_ERROR_WANT_WRITE,
  SerialWantRead   = SSL_ERROR_WANT_READ,
  SerialWantLookup = SSL_ERROR_WANT_X509_LOOKUP,
  SerialZero       = SSL_ERROR_ZERO_RETURN,
  SerialSyscall    = SSL_ERROR_SYSCALL,
  SerialSSL        = SSL_ERROR_SSL,
  SerialFailure    = -1,
  SerialIncorrectType  = -2
};

/* This serves as a virtual table for I/O to the FileHandle */

struct _HandleFuncTable
{
  gssize            (* table_handle_length)       (FileHandle *fh);
  gssize            (* table_handle_pages)        (FileHandle *fh);
  gssize            (* table_handle_pagesize)     (FileHandle *fh);
  gssize            (* table_handle_map_page)     (FileHandle *fh, guint pgno, const guint8** mem);
  gboolean          (* table_handle_unmap_page)   (FileHandle *fh, guint pgno, const guint8** mem);
  const guint8*     (* table_handle_checksum_md5) (FileHandle *fh);
  gboolean          (* table_handle_close)        (FileHandle *fh, gint flags);
  gboolean          (* table_handle_write)        (FileHandle *fh, const guint8 *buf, gsize nbyte);
  gboolean          (* table_handle_copy)         (FileHandle *from, FileHandle *to, guint off, guint len);
  gboolean          (* table_handle_getui)        (FileHandle *fh, guint32* i);
  gboolean          (* table_handle_putui)        (FileHandle *fh, guint32 i);
  gssize            (* table_handle_read)         (FileHandle *fh, guint8 *buf, gsize nbyte);
};

struct _SerialSource {
  SerialStatus status;

  const HandleFuncTable* table;

  guint total_alloc;
  guint alloc_pos;
  void* alloc;

  SerialType (* source_type)           (SerialSource* source);
  gboolean   (* source_close)          (SerialSource* source);
  gboolean   (* source_read)           (SerialSource* source, guint8 *ptr, guint32 len);
  void       (* source_free)           (SerialSource* source);
  void       (* source_reset)          (SerialSource* source);

  /* more or less standard, use the functions below, these call source_read.
   * memory is allocated using alloc(). */
  void*      (* source_alloc)        (SerialSource* source, guint32        len);
  gboolean   (* next_bytes_known)    (SerialSource* source, guint8        *ptr, guint32  len);
  gboolean   (* next_bytes)          (SerialSource* source, const guint8 **ptr, guint32 *len);
  gboolean   (* next_uint)           (SerialSource* source, guint32       *ptr);
  gboolean   (* next_uint32)         (SerialSource* source, guint32       *ptr);
  gboolean   (* next_uint16)         (SerialSource* source, guint16       *ptr);
  gboolean   (* next_uint8)          (SerialSource* source, guint8        *ptr);
  gboolean   (* next_bool)           (SerialSource* source, gboolean      *ptr);
  gboolean   (* next_string)         (SerialSource* source, const char   **ptr);
};

struct _SerialSink {
  SerialStatus status;

  const HandleFuncTable* table;

  gboolean   (* sink_type)          (SerialSink* sink, SerialType type, guint mem_size);
  gboolean   (* sink_close)         (SerialSink* sink);
  gboolean   (* sink_write)         (SerialSink* sink, const guint8 *ptr, guint32 len);
  void       (* sink_free)          (SerialSink* sink);

  SerialStatus (* sink_quantum)     (SerialSink* sink);

  /* more or less standard, use the functions below, these call sink_write */
  gboolean   (* next_bytes_known)   (SerialSink* sink, const guint8 *ptr, guint32 len);
  gboolean   (* next_bytes)         (SerialSink* sink, const guint8 *ptr, guint32 len);
  gboolean   (* next_uint)          (SerialSink* sink, guint32       ptr);
  gboolean   (* next_uint32)        (SerialSink* sink, guint32       ptr);
  gboolean   (* next_uint16)        (SerialSink* sink, guint16       ptr);
  gboolean   (* next_uint8)         (SerialSink* sink, guint8        ptr);
  gboolean   (* next_bool)          (SerialSink* sink, gboolean      ptr);
  gboolean   (* next_string)        (SerialSink* sink, const char   *ptr);
};

void     source_default_reset               (SerialSource* source);
void*    source_default_alloc               (SerialSource* source, guint len);
gboolean source_next_uint16                 (SerialSource* source, guint16 *ptr);
gboolean source_next_uint32                 (SerialSource* source, guint32 *ptr);
gboolean source_next_uint8                  (SerialSource* source, guint8 *ptr);
gboolean source_next_uint                   (SerialSource* source, guint32 *ptr);
gboolean source_next_bool                   (SerialSource* source, gboolean *ptr);
gboolean source_next_string                 (SerialSource* source, const char   **ptr);
gboolean source_next_bytes                  (SerialSource* source, const guint8 **ptr, guint32 *len);
gboolean source_next_bytes_known            (SerialSource* source, guint8 *ptr, guint32 len);

gboolean sink_next_uint                     (SerialSink* sink, guint32       ptr);
gboolean sink_next_uint32                   (SerialSink* sink, guint32       ptr);
gboolean sink_next_uint16                   (SerialSink* sink, guint16       ptr);
gboolean sink_next_uint8                    (SerialSink* sink, guint8        ptr);
gboolean sink_next_bool                     (SerialSink* sink, gboolean      ptr);
gboolean sink_next_string                   (SerialSink* sink, const char   *ptr);
gboolean sink_next_bytes                    (SerialSink* sink, const guint8   *ptr, guint32 len);
gboolean sink_next_bytes_known              (SerialSink* sink, const guint8 *ptr, guint32 len);

void         serializeio_initialize_type    (guint32 val, SerialStatus (*func) ());
SerialStatus unserialize_generic            (SerialSource* source, SerialType* object_type, void** object);
SerialStatus unserialize_generic_acceptable (SerialSource* source, guint32 acceptable, SerialType* object_type, void** object);

void ser_sink_init   (SerialSink* sink);
void ser_source_init (SerialSource* source);

SerialSink*   serializeio_handle_sink       (FileHandle *fh, const HandleFuncTable* table);
SerialSource* serializeio_handle_source     (FileHandle *fh, const HandleFuncTable* table);

#endif /* _SERIALIZEIO_H_ */