File: io.m

package info (click to toggle)
pantomime 1.4.0%2Bdfsg-4
  • links: PTS, VCS
  • area: main
  • in suites: sid, trixie
  • size: 2,288 kB
  • sloc: objc: 22,039; makefile: 11; sh: 4
file content (303 lines) | stat: -rw-r--r-- 5,600 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
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
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
/*
**  io.m
**
**  Copyright (c) 2004-2007 Ludovic Marcotte
**  Copyright (C) 2014-2020 Riccardo Mottola
**
**  Author: Ludovic Marcotte <ludovic@Sophos.ca>
**          Riccardo Mottola <rm@gnu.org>
**
**  This library is free software; you can redistribute it and/or
**  modify it under the terms of the GNU Lesser General Public
**  License as published by the Free Software Foundation; either
**  version 2.1 of the License, or (at your option) any later version.
**  
**  This library 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
**  Lesser General Public License for more details.
**  
** You should have received a copy of the GNU General Public License
** along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/

#import <Pantomime/io.h>

#import <Foundation/NSData.h>
#import <Foundation/NSString.h>

#include <errno.h>
#ifdef __MINGW32__
#include <io.h> 	// For _read(), _write() and _close() on MinGW
#include <winsock2.h>	// For recv() on MinGW
#else
#include <sys/ioctl.h>
#include <sys/socket.h>
#endif

#include <stdlib.h>	// For abort()
#include <stdio.h>
#include <string.h>     // For memset()
#include <netinet/in.h> // For ntohs() and friends. 

#include <unistd.h>	// For read(), write() and close()

#ifdef MACOSX
#include <sys/uio.h>	// For read() and write() on OS X
#endif

#if !defined(FIONBIO) && !defined(__MINGW32__)
#include <sys/filio.h>  // For FIONBIO on Solaris
#endif

//
//
//
ssize_t read_block(int fd, void *buf, size_t count)
{
  size_t tot;
  ssize_t bytes;
  
  tot = 0;

  while (tot < count)
    {
#ifdef __MINGW32__ 
      if ((bytes = _read(fd, buf+tot, count-tot)) == -1)
#else
      if ((bytes = read(fd, buf+tot, count-tot)) == -1)
#endif
        {
	  if (errno != EINTR)
	    {
	      return -1;
	    }
	}
      else
	{
	  tot += (size_t)bytes;
	}
    }
  
  return (ssize_t)tot;
}


//
//
//
int safe_close(int fd)
{
  int value;
#ifdef __MINGW32__
  while (value = _close(fd), value == -1 && errno == EINTR);
#else
  while (value = close(fd), value == -1 && errno == EINTR);
#endif
  return value;
}

//
//
//
ssize_t safe_read(int fd, void *buf, size_t count)
{
  ssize_t value;
#ifdef __MINGW32__
  while (value = _read(fd, buf, count), value == -1 && errno == EINTR);
#else
  while (value = read(fd, buf, count), value == -1 && errno == EINTR);
#endif
  return value;
}

//
//
//
ssize_t safe_recv(int fd, void *buf, size_t count, int flags)
{
  ssize_t value;
  while (value = recv(fd, buf, count, flags), value == -1 && errno == EINTR);
  return value;
}

//
// 
//
NSString *read_string_memory(unsigned char *m, uint16_t *count)
{
  uint16_t c0, c1, r;

  c0 = *m;
  c1 = *(m+1);

  *count = r = (c0<<8)|c1;
  m += 2;

  return [[[NSString alloc] initWithBytes:m length:r encoding: NSUTF8StringEncoding] autorelease];
}

//
//
//
NSData *read_data_memory(unsigned char *m, uint16_t *count)
{
  uint16_t c0, c1, r;
  
  c0 = *m;
  c1 = *(m+1);
  
  *count = r = (c0<<8)|c1;
  m += 2;
  
  // when used on a file buffer, it should outlive the NSData object itself
  return [NSData dataWithBytesNoCopy: m  length: r freeWhenDone:NO];  
}


//
//
//
uint32_t read_uint32_memory(unsigned char *m)
{
  uint32_t c0, c1, c2, c3, r;

  c0 = *m;
  c1 = *(m+1);
  c2 = *(m+2);
  c3 = *(m+3);

  r = ((c0<<24)|(c1<<16)|(c2<<8)|c3);
  
  return r;
}

//
//
//
uint16_t read_uint16(int fd)
{
  uint16_t v;
  
  if (read(fd, &v, 2) != 2) abort();

  return ntohs(v);
}

//
//
//
void write_uint16(int fd, uint16_t value)
{
  uint16_t v;
  
  v = htons(value);
  
  if (write(fd, &v, 2) != 2)
    {
      //printf("Error writing cache, aborting.");
      abort();
    }
} 

//
//
//
ssize_t read_string(int fd, char *buf, uint16_t *count)
{
  *count = read_uint16(fd);
  
  if (*count)
    {
      ssize_t nbytes;
      nbytes = read(fd, buf, *count);
      if (nbytes == (ssize_t)*count)
        return nbytes;
      if (nbytes == 0)
        {
          printf("read_string: EOF\n");
        }
      else if (nbytes == -1)
        {
          printf("read_string: error\n");
        }
      printf("read_string: read less bytes than expected\n");
      return nbytes;
    }
  return 0; // FIXME this might be a legitimate 0 or an issue with read_uint16 failed
}

//
//
//
void write_string(int fd, unsigned char *s, size_t len)
{
  if (s && len > 0)
    { 
      write_uint16(fd, len);
      if (write(fd, s, len) != (ssize_t)len)
	{
	  //NSLog(@"FAILED TO WRITE BYTES, ABORT");
	  abort();
	}
    }
  else
    {
      write_uint16(fd, 0);
    }
}

void write_data(int fd, NSData *d)
{
  write_string (fd, (unsigned char *)[d bytes], [d length]);
}

//
//
//
int read_uint32(int fd, uint32_t *val)
{
  uint32_t v;
  int c;
  
  if ((c = (int)read(fd, &v, 4)) != 4)
    {
      if (c == 0)
        printf("read_unsinged_int: EOF\n");
      else 
        printf("read_unisnged_int unexpected :read = %d\n", c);
    }

  *val = htonl(v);
  return c;
}

//
//
//
void write_uint32(int fd, uint32_t value)
{
  uint32_t v;
  ssize_t r;
  
  v = htonl(value);

  //printf("b|%d| a|%d|", value, v);

  r = write(fd, &v, 4);
  if (r < 0)
    {
      if (errno == EFBIG)
        printf("ERROR WRITING CACHE: file too big");
      else if (errno == EACCES)
        printf("ERROR WRITING CACHE: permission denied");
      else
        printf("ERROR WRITING CACHE: %d", errno);
      abort();
    }
  else if (r != 4)
    {
      printf("ERROR WRITING CACHE: Wrote %ld out of 4", (long)r);
      abort();
    }
}