2 * @brief Communication with worker processes
4 /* Copyright (C) 2011,2022,2023 Olly Betts
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License as
8 * published by the Free Software Foundation; either version 2 of the
9 * License, or (at your option) any later version.
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.
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 02110-1301
24 #include "worker_comms.h"
28 const int buffer_size
= 4096;
31 read_string(FILE* f
, string
& s
)
33 // Extracting the length of the string
35 if (ch
< 0) return false;
38 unsigned i
= len
- 251;
42 if (ch
< 0) return false;
43 len
= (len
<< 8) | ch
;
51 char buf
[buffer_size
];
54 size_t n
= fread(buf
, 1, min(sizeof(buf
), len
), f
);
59 s
.append(buf
, size_t(n
));
67 write_string(FILE* f
, const char* p
, size_t len
)
70 putc(static_cast<unsigned char>(len
), f
);
71 } else if (len
< 0x10000) {
73 putc(static_cast<unsigned char>(len
>> 8), f
);
74 putc(static_cast<unsigned char>(len
), f
);
75 } else if (len
< 0x1000000) {
77 putc(static_cast<unsigned char>(len
>> 16), f
);
78 putc(static_cast<unsigned char>(len
>> 8), f
);
79 putc(static_cast<unsigned char>(len
), f
);
82 putc(static_cast<unsigned char>(len
>> 24), f
);
83 putc(static_cast<unsigned char>(len
>> 16), f
);
84 putc(static_cast<unsigned char>(len
>> 8), f
);
85 putc(static_cast<unsigned char>(len
), f
);
89 size_t n
= fwrite(p
, 1, len
, f
);
102 read_unsigned(FILE* f
, unsigned& v
)
104 unsigned char data
[(sizeof(v
) * 8 + 6) / 7];
105 unsigned char* p
= data
;
109 if (p
- data
== sizeof(data
) || ch
< 0) return false;
121 read_unsigned(FILE* f
, unsigned long& v
)
123 unsigned char data
[(sizeof(v
) * 8 + 6) / 7];
124 unsigned char* p
= data
;
128 if (p
- data
== sizeof(data
) || ch
< 0) return false;
140 write_unsigned(FILE* f
, unsigned long v
)
143 if (putc(v
| 0x80, f
) < 0) return false;
146 return !(putc(v
, f
) < 0);
150 write_unsigned(FILE* f
, unsigned v
)
153 if (putc(v
| 0x80, f
) < 0) return false;
156 return !(putc(v
, f
) < 0);