[go: up one dir, main page]

Menu

[904618]: / src / str.h  Maximize  Restore  History

Download this file

236 lines (199 with data), 7.9 kB

  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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
// -*- Mode: C; indent-tabs-mode: t; c-basic-offset: 4; tab-width: 4 -*-
// ----------------------------------------------------------------------------
// :oCCCCOCoc.
// .cCO8OOOOOOOOO8Oo:
// .oOO8OOOOOOOOOOOOOOOCc
// cO8888: .:oOOOOC. TM
// :888888: :CCCc .oOOOOC. ### ### #########
// C888888: .ooo: .C######## ##### ##### ###### ###### ##########
// O888888: .oO### ### ##### ##### ######## ######## #### ###
// C888888: :8O. .C########## ### #### ### ## ## ## ## #### ###
// :8@@@@8: :888c o### ### #### ### ######## ######## ##########
// :8@@@@C C@@@@ oo######## ### ## ### ###### ###### #########
// cO@@@@@@@@@@@@@@@@@Oc0
// :oO8@@@@@@@@@@Oo.
// .oCOOOOOCc. http://remood.org/
// ----------------------------------------------------------------------------
// Copyright (C) 2013-2013 GhostlyDeath <ghostlydeath@remood.org>
// <ghostlydeath@gmail.com>
// ----------------------------------------------------------------------------
// This program is free software; you can redistribute it and/or
// modify it under the terms of the GNU General Public License
// as published by the Free Software Foundation; either version 3
// of the License, or (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
// ----------------------------------------------------------------------------
// DESCRIPTION: Stream Reader/Writer
// Replaces:
// * c_lib.h : Raw Byte Read/Write
// * w_wad.h : WL_ES_t, reader for WAD entries
// * i_util.h: FILE and Network wrappers
#ifndef __STR_H__
#define __STR_H__
/*****************************************************************************/
/***************
*** INCLUDES ***
***************/
#include "doomtype.h"
/****************
*** CONSTANTS ***
****************/
/* STR_Type_t -- Stream Type */
typedef enum STR_Type_e
{
STRT_MEM, // Memory Stream
STRT_ROFILE, // File Stream (Read Only)
STRT_RWFILE, // File Stream (Read/Write Overwrite)
STRT_WAD, // WAD Stream
STRT_NET, // IPv4 Network Stream
STRT_NETSIX, // IPv6 Network Stream
STRT_UNIX, // UNIX Sockets
NUMSTRT
} STR_Type_t;
/* STR_Class_t -- Stream Class */
typedef enum STR_Class_e
{
STRC_STREAM,
STRC_DATAGRAM,
NUMSTRC
} STR_Class_t;
/*****************
*** STRUCTURES ***
*****************/
typedef struct STR_s STR_t;
/* STR_Addr_t -- Address */
typedef struct STR_Addr_s
{
STR_Type_t Type; // Type it belongs to
union
{
struct
{
void* Addr; // Address of buffer
uint32_t Size; // Size of buffer
uint32_t Pos; // Position in buffer
} Raw;
struct
{
char Path[PATH_MAX]; // Path of file
uint32_t Pos; // Position in file
} File;
struct
{
uint16_t Port; // Port Number
union
{
uint32_t u; // Integer
uint16_t s[2]; // Shorts
uint8_t b[4]; // Bytes
} Addr; // IPv4 Address
} Net; // IPv4 Net Address
struct
{
uint16_t Port; // Port Number
uint32_t Scope; // Scope
union
{
uint32_t ll[2]; // Long Longs
uint32_t u[4]; // Integers
uint16_t s[8]; // Shorts
uint8_t b[16]; // Bytes
} Addr; // Address
} Net6; // IPv6 Net Address
} Addr; // Address Data
} STR_Addr_t;
/* STR_AddrInfo_t -- Address Info */
typedef struct STR_AddrInfo_s
{
STR_Type_t Type; // Type of address
STR_Addr_t Addr; // Address Info
struct STR_AddrInfo_s* Next; // Next info
} STR_AddrInfo_t;
/* Define WL_WADEntry_t */
#if !defined(__REMOOD_WLWADENT_DEFINED)
typedef struct WL_WADEntry_s WL_WADEntry_t;
#define __REMOOD_WLWADENT_DEFINED
#endif
/****************
*** FUNCTIONS ***
****************/
/* Basic */
STR_t* STR_Open(const STR_Type_t a_Type, const STR_Class_t a_Class);
bool_t STR_Bind(STR_t* const a_Str, STR_Addr_t* const a_Addr);
bool_t STR_Listen(STR_t* const a_Str, const uint32_t a_ConnCap);
STR_t* STR_Accept(STR_t* const a_Str, STR_Addr_t* const a_Addr);
bool_t STR_Connect(STR_t* const a_Str, STR_Addr_t* const a_Addr);
void STR_Close(STR_t* const a_Str);
/* Wrapped */
STR_t* STR_OpenFile(const char* const a_PathName, const char* const a_Mode);
STR_t* STR_OpenEntry(WL_WADEntry_t* const a_Entry);
/* Obtain generic info */
STR_Type_t STR_GetType(STR_t* const a_Str);
STR_Class_t STR_GetClass(STR_t* const a_Str);
/* Name/Address Resolution */
bool_t STR_GetAddrInfo(const char* const a_Name, STR_AddrInfo_t** const a_Out);
void STR_FreeAddrInfo(STR_AddrInfo_t* const a_Addresses);
bool_t STR_GetNameInfo(STR_AddrInfo_t* const a_In, char* const a_Name, const size_t a_Len);
bool_t STR_GetAddr(const char* const a_Name, STR_Addr_t* const a_Addr);
/* Location (if supported) */
uint32_t STR_Tell(STR_t* const a_Str);
uint32_t STR_Seek(STR_t* const a_Str, const uint32_t a_Where, const bool_t a_End);
uint32_t STR_Wait(STR_t* const a_Str);
/* Basic Read/Write */
int32_t STR_ReadFrom(STR_t* const a_Str, void* const a_In, const uint32_t a_Len, STR_Addr_t* const a_SrcAddr);
int32_t STR_WriteTo(STR_t* const a_Str, const void* const a_Out, const uint32_t a_Len, STR_Addr_t* const a_DestAddr);
#define STR_Read(s,i,l) STR_ReadFrom((s), (i), (l), NULL)
#define STR_Write(s,o,l) STR_WriteTo((s), (o), (l), NULL)
/* Read */
// Non Swapped
int8_t STR_rxi8(STR_t* const a_Str);
int16_t STR_rxi16(STR_t* const a_Str);
int32_t STR_rxi32(STR_t* const a_Str);
int64_t STR_rxi64(STR_t* const a_Str);
uint8_t STR_rxu8(STR_t* const a_Str);
uint16_t STR_rxu16(STR_t* const a_Str);
uint32_t STR_rxu32(STR_t* const a_Str);
uint64_t STR_rxu64(STR_t* const a_Str);
// Little Swapped
int16_t STR_rli16(STR_t* const a_Str);
int32_t STR_rli32(STR_t* const a_Str);
int64_t STR_rli64(STR_t* const a_Str);
uint16_t STR_rlu16(STR_t* const a_Str);
uint32_t STR_rlu32(STR_t* const a_Str);
uint64_t STR_rlu64(STR_t* const a_Str);
// Big Swapped
int16_t STR_rbi16(STR_t* const a_Str);
int32_t STR_rbi32(STR_t* const a_Str);
int64_t STR_rbi64(STR_t* const a_Str);
uint16_t STR_rbu16(STR_t* const a_Str);
uint32_t STR_rbu32(STR_t* const a_Str);
uint64_t STR_rbu64(STR_t* const a_Str);
/* Write */
void STR_wxi8(STR_t* const a_Str, const int8_t a_Val);
void STR_wxi16(STR_t* const a_Str, const int16_t a_Val);
void STR_wxi32(STR_t* const a_Str, const int32_t a_Val);
void STR_wxi64(STR_t* const a_Str, const int64_t a_Val);
void STR_wxu8(STR_t* const a_Str, const uint8_t a_Val);
void STR_wxu16(STR_t* const a_Str, const uint16_t a_Val);
void STR_wxu32(STR_t* const a_Str, const uint32_t a_Val);
void STR_wxu64(STR_t* const a_Str, const uint64_t a_Val);
// Little Swapped
void STR_wli16(STR_t* const a_Str, const int16_t a_Val);
void STR_wli32(STR_t* const a_Str, const int32_t a_Val);
void STR_wli64(STR_t* const a_Str, const int64_t a_Val);
void STR_wlu16(STR_t* const a_Str, const uint16_t a_Val);
void STR_wlu32(STR_t* const a_Str, const uint32_t a_Val);
void STR_wlu64(STR_t* const a_Str, const uint64_t a_Val);
// Big Swapped
void STR_wbi16(STR_t* const a_Str, const int16_t a_Val);
void STR_wbi32(STR_t* const a_Str, const int32_t a_Val);
void STR_wbi64(STR_t* const a_Str, const int64_t a_Val);
void STR_wbu16(STR_t* const a_Str, const uint16_t a_Val);
void STR_wbu32(STR_t* const a_Str, const uint32_t a_Val);
void STR_wbu64(STR_t* const a_Str, const uint64_t a_Val);
/*****************************************************************************/
#endif /* __STR_H__ */