summaryrefslogtreecommitdiffstats
path: root/tksao/fitsy++/outsocket.C
blob: d501ca91c13f2f00dd8865fab0b6512113f2de4b (plain)
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
// Copyright (C) 1999-2017
// Smithsonian Astrophysical Observatory, Cambridge, MA, USA
// For conditions of distribution and use, see copyright notice in "copyright"

#include "outsocket.h"

#ifndef __WIN32

#include <sys/types.h>
#include <sys/socket.h>

#include "file.h"

OutFitsSocket::OutFitsSocket(int s)
{
  id_ = s;
  valid_ = 1;
}

int OutFitsSocket::write(char* where, size_t size)
{
  // size_t size is unsigned
  long long ss = size;
  size_t rr =0;
  int r;
  do {
    r = (ss>B4KB) ? B4KB : ss;
    send(id_, where+rr, r, 0);
    if (r == -1) {
      internalError("Fitsy++ outsocket write error");
      return -1;
    }
    ss -= r;
    rr += r;
  } while (r>0 && rr<size);

  return rr;
}

OutFitsSocketGZ::OutFitsSocketGZ(int s)
{
  id_ = s;
  stream_ = new z_stream;
  buf_ = new unsigned char[B4KB];

  crc_ = crc32(0L, Z_NULL, 0);

  stream_->next_in = NULL;
  stream_->avail_in = 0;
  stream_->next_out = NULL;
  stream_->avail_out = 0;

  stream_->zalloc = NULL;
  stream_->zfree = NULL;
  stream_->opaque = NULL;

  if (deflateInit2(stream_, Z_DEFAULT_COMPRESSION, Z_DEFLATED, 
		   -MAX_WBITS, 8, Z_DEFAULT_STRATEGY) != Z_OK) {
    internalError("Fitsy++ outsocket deflateInit error");
    return;
  }

  // dump simple header
  unsigned char header[10] =
    {0x1f,0x8b,0x08,0x00,0x00,0x00,0x00,0x00,0x00,0x03};
  send(id_, header, 10, 0);
  
  stream_->next_out = buf_;
  stream_->avail_out = B4KB;

  valid_ = 1;
}

OutFitsSocketGZ::~OutFitsSocketGZ()
{
  // flush any pending output
  while (deflategz(Z_FINISH) == Z_OK);

  // output crc/length
  putlong(crc_);
  putlong(stream_->total_in);

  if (deflateEnd(stream_) != Z_OK)
    internalError("Fitsy++ outsocket deflateEnd error");

  if (stream_)
    delete stream_;

  if (buf_)
    delete [] buf_;
}

int OutFitsSocketGZ::write(char* where, size_t size)
{
  stream_->next_in = (unsigned char*)where;
  stream_->avail_in = size;

  if (DebugGZ)
    cerr << "write " << size << endl;

  while (stream_->avail_in > 0 && deflategz(Z_NO_FLUSH) == Z_OK);

  // update crc
  crc_ = crc32(crc_, (const Bytef *)where, size);

  return size - stream_->avail_in;
}

int OutFitsSocketGZ::deflategz(int flush)
{
  int result = deflate(stream_, flush);

  switch (result) {
  case Z_OK:
    if (DebugGZ)
      cerr << "deflate OK: avail_in " << stream_->avail_in
	   << " avail_out " << stream_->avail_out << endl;
    break;
  case Z_STREAM_END:
    if (DebugGZ)
      cerr << "deflate STRM_END: avail_in " << stream_->avail_in
	   << " avail_out " << stream_->avail_out << endl;
    break;
  default:
    if (DebugGZ)
      cerr << "deflate Error " << result << endl;
    return result;
  }

  if (stream_->avail_out == 0 || result != Z_OK) {
    int s = B4KB - stream_->avail_out;
    unsigned char* d = buf_;

    while (s>0) {
      int r = send(id_, d, s, 0);

      if (r == -1) {
	internalError("Fitsy++ outsocket deflate send error");
	return Z_ERRNO;
      }
	  
      if (DebugGZ)
	cerr << "deflate send " << r << " out of " << s << endl;

      s -= r;
      d += r;
    }

    stream_->next_out = buf_;
    stream_->avail_out = B4KB;
  }

  return result;
}

void OutFitsSocketGZ::putlong(unsigned long l)
{
  // dump in LSB order
  for (int n = 0; n < 4; n++) {
    unsigned char foo = (int)(l & 0xff);
    send(id_, &foo, 1, 0);
    l >>= 8;
  }
}

#else

OutFitsSocket::OutFitsSocket(int s)
{
  id_ = s;
  valid_ = 0;
}

int OutFitsSocket::write(char* where, size_t size)
{
  return 0;
}

OutFitsSocketGZ::OutFitsSocketGZ(int s)
{
  id_ = s;
  valid_ = 0;
}

OutFitsSocketGZ::~OutFitsSocketGZ() {}

int OutFitsSocketGZ::write(char* where, size_t size)
{
  return 0;
}

int OutFitsSocketGZ::deflategz(int flush)
{
  return 0;
}

void OutFitsSocketGZ::putlong(unsigned long l) {}

#endif