| 1 |
/* reader.c - provides the Reader object |
| 2 |
* Copyright (C) 2001, 2010, 2012 g10 Code GmbH |
| 3 |
* |
| 4 |
* This file is part of KSBA. |
| 5 |
* |
| 6 |
* KSBA is free software; you can redistribute it and/or modify |
| 7 |
* it under the terms of either |
| 8 |
* |
| 9 |
* - the GNU Lesser General Public License as published by the Free |
| 10 |
* Software Foundation; either version 3 of the License, or (at |
| 11 |
* your option) any later version. |
| 12 |
* |
| 13 |
* or |
| 14 |
* |
| 15 |
* - the GNU General Public License as published by the Free |
| 16 |
* Software Foundation; either version 2 of the License, or (at |
| 17 |
* your option) any later version. |
| 18 |
* |
| 19 |
* or both in parallel, as here. |
| 20 |
* |
| 21 |
* KSBA is distributed in the hope that it will be useful, but WITHOUT |
| 22 |
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY |
| 23 |
* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public |
| 24 |
* License for more details. |
| 25 |
* |
| 26 |
* You should have received a copies of the GNU General Public License |
| 27 |
* and the GNU Lesser General Public License along with this program; |
| 28 |
* if not, see <http://www.gnu.org/licenses/>. |
| 29 |
*/ |
| 30 |
|
| 31 |
#include <config.h> |
| 32 |
#include <stdio.h> |
| 33 |
#include <stdlib.h> |
| 34 |
#include <unistd.h> |
| 35 |
#include <string.h> |
| 36 |
#include <assert.h> |
| 37 |
#include <errno.h> |
| 38 |
#include "util.h" |
| 39 |
|
| 40 |
#include "ksba.h" |
| 41 |
#include "reader.h" |
| 42 |
|
| 43 |
/** |
| 44 |
* ksba_reader_new: |
| 45 |
* |
| 46 |
* Create a new but uninitialized ksba_reader_t Object. Using this |
| 47 |
* reader object in unitialized state does always yield eof. |
| 48 |
* |
| 49 |
* Return value: ksba_reader_t object or an error code. |
| 50 |
**/ |
| 51 |
gpg_error_t |
| 52 |
ksba_reader_new (ksba_reader_t *r_r) |
| 53 |
{ |
| 54 |
*r_r = xtrycalloc (1, sizeof **r_r); |
| 55 |
if (!*r_r) |
| 56 |
return gpg_error_from_errno (errno); |
| 57 |
return 0; |
| 58 |
} |
| 59 |
|
| 60 |
|
| 61 |
/** |
| 62 |
* ksba_reader_release: |
| 63 |
* @r: Reader Object (or NULL) |
| 64 |
* |
| 65 |
* Release this object |
| 66 |
**/ |
| 67 |
void |
| 68 |
ksba_reader_release (ksba_reader_t r) |
| 69 |
{ |
| 70 |
if (!r) |
| 71 |
return; |
| 72 |
if (r->notify_cb) |
| 73 |
{ |
| 74 |
void (*notify_fnc)(void*,ksba_reader_t) = r->notify_cb; |
| 75 |
|
| 76 |
r->notify_cb = NULL; |
| 77 |
notify_fnc (r->notify_cb_value, r); |
| 78 |
} |
| 79 |
if (r->type == READER_TYPE_MEM) |
| 80 |
xfree (r->u.mem.buffer); |
| 81 |
xfree (r->unread.buf); |
| 82 |
xfree (r); |
| 83 |
} |
| 84 |
|
| 85 |
|
| 86 |
/* Set NOTIFY as function to be called by ksba_reader_release before |
| 87 |
resources are actually deallocated. NOTIFY_VALUE is passed to the |
| 88 |
called function as its first argument. Note that only the last |
| 89 |
registered function will be called; passing NULL for NOTIFY removes |
| 90 |
the notification. */ |
| 91 |
gpg_error_t |
| 92 |
ksba_reader_set_release_notify (ksba_reader_t r, |
| 93 |
void (*notify)(void*,ksba_reader_t), |
| 94 |
void *notify_value) |
| 95 |
{ |
| 96 |
if (!r) |
| 97 |
return gpg_error (GPG_ERR_INV_VALUE); |
| 98 |
r->notify_cb = notify; |
| 99 |
r->notify_cb_value = notify_value; |
| 100 |
return 0; |
| 101 |
} |
| 102 |
|
| 103 |
|
| 104 |
/* Clear the error and eof indicators for READER, so that it can be |
| 105 |
continued to use. Also discards any unread bytes. This is usually |
| 106 |
required if the upper layer wants to send an EOF to indicate |
| 107 |
the logical end of one part of a file. If BUFFER and BUFLEN are |
| 108 |
not NULL, possible unread data is copied to a newly allocated |
| 109 |
buffer and this buffer is assigned to BUFFER, BUFLEN will be set to |
| 110 |
the length of the unread bytes. */ |
| 111 |
gpg_error_t |
| 112 |
ksba_reader_clear (ksba_reader_t r, unsigned char **buffer, size_t *buflen) |
| 113 |
{ |
| 114 |
size_t n; |
| 115 |
|
| 116 |
if (!r) |
| 117 |
return gpg_error (GPG_ERR_INV_VALUE); |
| 118 |
|
| 119 |
r->eof = 0; |
| 120 |
r->error = 0; |
| 121 |
r->nread = 0; |
| 122 |
n = r->unread.length; |
| 123 |
r->unread.length = 0; |
| 124 |
|
| 125 |
if (buffer && buflen) |
| 126 |
{ |
| 127 |
*buffer = NULL; |
| 128 |
*buflen = 0; |
| 129 |
if (n) |
| 130 |
{ |
| 131 |
*buffer = xtrymalloc (n); |
| 132 |
if (!*buffer) |
| 133 |
return gpg_error_from_errno (errno); |
| 134 |
memcpy (*buffer, r->unread.buf, n); |
| 135 |
*buflen = n; |
| 136 |
} |
| 137 |
} |
| 138 |
|
| 139 |
return 0; |
| 140 |
} |
| 141 |
|
| 142 |
|
| 143 |
gpg_error_t |
| 144 |
ksba_reader_error (ksba_reader_t r) |
| 145 |
{ |
| 146 |
return r? gpg_error_from_errno (r->error) : gpg_error (GPG_ERR_INV_VALUE); |
| 147 |
} |
| 148 |
|
| 149 |
unsigned long |
| 150 |
ksba_reader_tell (ksba_reader_t r) |
| 151 |
{ |
| 152 |
return r? r->nread : 0; |
| 153 |
} |
| 154 |
|
| 155 |
|
| 156 |
/** |
| 157 |
* ksba_reader_set_mem: |
| 158 |
* @r: Reader object |
| 159 |
* @buffer: Data |
| 160 |
* @length: Length of Data (bytes) |
| 161 |
* |
| 162 |
* Intialize the reader object with @length bytes from @buffer and set |
| 163 |
* the read position to the beginning. It is possible to reuse this |
| 164 |
* reader object with another buffer if the reader object has |
| 165 |
* already been initialized using this function. |
| 166 |
* |
| 167 |
* Return value: 0 on success or an error code. |
| 168 |
**/ |
| 169 |
gpg_error_t |
| 170 |
ksba_reader_set_mem (ksba_reader_t r, const void *buffer, size_t length) |
| 171 |
{ |
| 172 |
if (!r || !buffer) |
| 173 |
return gpg_error (GPG_ERR_INV_VALUE); |
| 174 |
if (r->type == READER_TYPE_MEM) |
| 175 |
{ /* Reuse this reader */ |
| 176 |
xfree (r->u.mem.buffer); |
| 177 |
r->type = 0; |
| 178 |
} |
| 179 |
if (r->type) |
| 180 |
return gpg_error (GPG_ERR_CONFLICT); |
| 181 |
|
| 182 |
r->u.mem.buffer = xtrymalloc (length); |
| 183 |
if (!r->u.mem.buffer) |
| 184 |
return gpg_error (GPG_ERR_ENOMEM); |
| 185 |
memcpy (r->u.mem.buffer, buffer, length); |
| 186 |
r->u.mem.size = length; |
| 187 |
r->u.mem.readpos = 0; |
| 188 |
r->type = READER_TYPE_MEM; |
| 189 |
r->eof = 0; |
| 190 |
|
| 191 |
return 0; |
| 192 |
} |
| 193 |
|
| 194 |
|
| 195 |
/** |
| 196 |
* ksba_reader_set_fd: |
| 197 |
* @r: Reader object |
| 198 |
* @fd: file descriptor |
| 199 |
* |
| 200 |
* Initialize the Reader object with a file descriptor, so that read |
| 201 |
* operations on this object are excuted on this file descriptor. |
| 202 |
* |
| 203 |
* Return value: |
| 204 |
**/ |
| 205 |
gpg_error_t |
| 206 |
ksba_reader_set_fd (ksba_reader_t r, int fd) |
| 207 |
{ |
| 208 |
if (!r || fd == -1) |
| 209 |
return gpg_error (GPG_ERR_INV_VALUE); |
| 210 |
if (r->type) |
| 211 |
return gpg_error (GPG_ERR_CONFLICT); |
| 212 |
|
| 213 |
r->eof = 0; |
| 214 |
r->type = READER_TYPE_FD; |
| 215 |
r->u.fd = fd; |
| 216 |
|
| 217 |
return 0; |
| 218 |
} |
| 219 |
|
| 220 |
/** |
| 221 |
* ksba_reader_set_file: |
| 222 |
* @r: Reader object |
| 223 |
* @fp: file pointer |
| 224 |
* |
| 225 |
* Initialize the Reader object with a stdio file pointer, so that read |
| 226 |
* operations on this object are excuted on this stream |
| 227 |
* |
| 228 |
* Return value: |
| 229 |
**/ |
| 230 |
gpg_error_t |
| 231 |
ksba_reader_set_file (ksba_reader_t r, FILE *fp) |
| 232 |
{ |
| 233 |
if (!r || !fp) |
| 234 |
return gpg_error (GPG_ERR_INV_VALUE); |
| 235 |
if (r->type) |
| 236 |
return gpg_error (GPG_ERR_CONFLICT); |
| 237 |
|
| 238 |
r->eof = 0; |
| 239 |
r->type = READER_TYPE_FILE; |
| 240 |
r->u.file = fp; |
| 241 |
return 0; |
| 242 |
} |
| 243 |
|
| 244 |
|
| 245 |
|
| 246 |
/** |
| 247 |
* ksba_reader_set_cb: |
| 248 |
* @r: Reader object |
| 249 |
* @cb: Callback function |
| 250 |
* @cb_value: Value passed to the callback function |
| 251 |
* |
| 252 |
* Initialize the reader object with a callback function. |
| 253 |
* This callback function is defined as: |
| 254 |
* <literal> |
| 255 |
* typedef int (*cb) (void *cb_value, |
| 256 |
* char *buffer, size_t count, |
| 257 |
* size_t *nread); |
| 258 |
* </literal> |
| 259 |
* |
| 260 |
* The callback should return a maximium of @count bytes in @buffer |
| 261 |
* and the number actually read in @nread. It may return 0 in @nread |
| 262 |
* if there are no bytes currently available. To indicate EOF the |
| 263 |
* callback should return with an error code of GPG_ERR_EOF and set @nread to |
| 264 |
* 0. The callback may support passing %NULL for @buffer and @nread |
| 265 |
* and %0 for count as an indication to reset its internal read |
| 266 |
* pointer. |
| 267 |
* |
| 268 |
* Return value: 0 on success or an error code |
| 269 |
**/ |
| 270 |
gpg_error_t |
| 271 |
ksba_reader_set_cb (ksba_reader_t r, |
| 272 |
int (*cb)(void*,char *,size_t,size_t*), void *cb_value ) |
| 273 |
{ |
| 274 |
if (!r || !cb) |
| 275 |
return gpg_error (GPG_ERR_INV_VALUE); |
| 276 |
if (r->type) |
| 277 |
return gpg_error (GPG_ERR_CONFLICT); |
| 278 |
|
| 279 |
r->eof = 0; |
| 280 |
r->type = READER_TYPE_CB; |
| 281 |
r->u.cb.fnc = cb; |
| 282 |
r->u.cb.value = cb_value; |
| 283 |
|
| 284 |
return 0; |
| 285 |
} |
| 286 |
|
| 287 |
|
| 288 |
/** |
| 289 |
* ksba_reader_read: |
| 290 |
* @r: Readder object |
| 291 |
* @buffer: A buffer for returning the data |
| 292 |
* @length: The length of this buffer |
| 293 |
* @nread: Number of bytes actually read. |
| 294 |
* |
| 295 |
* Read data from the current read position to the supplied @buffer, |
| 296 |
* max. @length bytes are read and the actual number of bytes read are |
| 297 |
* returned in @nread. If there are no more bytes available %GPG_ERR_EOF is |
| 298 |
* returned and @nread is set to 0. |
| 299 |
* |
| 300 |
* If a @buffer of NULL is specified, the function does only return |
| 301 |
* the number of bytes available and does not move the read pointer. |
| 302 |
* This does only work for objects initialized from memory; if the |
| 303 |
* object is not capable of this it will return the error |
| 304 |
* GPG_ERR_NOT_IMPLEMENTED |
| 305 |
* |
| 306 |
* Return value: 0 on success, GPG_ERR_EOF or another error code |
| 307 |
**/ |
| 308 |
gpg_error_t |
| 309 |
ksba_reader_read (ksba_reader_t r, char *buffer, size_t length, size_t *nread) |
| 310 |
{ |
| 311 |
size_t nbytes; |
| 312 |
|
| 313 |
if (!r || !nread) |
| 314 |
return gpg_error (GPG_ERR_INV_VALUE); |
| 315 |
|
| 316 |
|
| 317 |
if (!buffer) |
| 318 |
{ |
| 319 |
if (r->type != READER_TYPE_MEM) |
| 320 |
return gpg_error (GPG_ERR_NOT_IMPLEMENTED); |
| 321 |
*nread = r->u.mem.size - r->u.mem.readpos; |
| 322 |
if (r->unread.buf) |
| 323 |
*nread += r->unread.length - r->unread.readpos; |
| 324 |
return *nread? 0 : gpg_error (GPG_ERR_EOF); |
| 325 |
} |
| 326 |
|
| 327 |
*nread = 0; |
| 328 |
|
| 329 |
if (r->unread.buf && r->unread.length) |
| 330 |
{ |
| 331 |
nbytes = r->unread.length - r->unread.readpos; |
| 332 |
if (!nbytes) |
| 333 |
return gpg_error (GPG_ERR_BUG); |
| 334 |
|
| 335 |
if (nbytes > length) |
| 336 |
nbytes = length; |
| 337 |
memcpy (buffer, r->unread.buf + r->unread.readpos, nbytes); |
| 338 |
r->unread.readpos += nbytes; |
| 339 |
if (r->unread.readpos == r->unread.length) |
| 340 |
r->unread.readpos = r->unread.length = 0; |
| 341 |
*nread = nbytes; |
| 342 |
r->nread += nbytes; |
| 343 |
return 0; |
| 344 |
} |
| 345 |
|
| 346 |
|
| 347 |
if (!r->type) |
| 348 |
{ |
| 349 |
r->eof = 1; |
| 350 |
return gpg_error (GPG_ERR_EOF); |
| 351 |
} |
| 352 |
else if (r->type == READER_TYPE_MEM) |
| 353 |
{ |
| 354 |
nbytes = r->u.mem.size - r->u.mem.readpos; |
| 355 |
if (!nbytes) |
| 356 |
{ |
| 357 |
r->eof = 1; |
| 358 |
return gpg_error (GPG_ERR_EOF); |
| 359 |
} |
| 360 |
|
| 361 |
if (nbytes > length) |
| 362 |
nbytes = length; |
| 363 |
memcpy (buffer, r->u.mem.buffer + r->u.mem.readpos, nbytes); |
| 364 |
*nread = nbytes; |
| 365 |
r->nread += nbytes; |
| 366 |
r->u.mem.readpos += nbytes; |
| 367 |
} |
| 368 |
else if (r->type == READER_TYPE_FILE) |
| 369 |
{ |
| 370 |
size_t n; |
| 371 |
|
| 372 |
if (r->eof) |
| 373 |
return gpg_error (GPG_ERR_EOF); |
| 374 |
|
| 375 |
if (!length) |
| 376 |
{ |
| 377 |
*nread = 0; |
| 378 |
return 0; |
| 379 |
} |
| 380 |
|
| 381 |
n = fread (buffer, 1, length, r->u.file); |
| 382 |
if (n) |
| 383 |
{ |
| 384 |
r->nread += n; |
| 385 |
*nread = n; |
| 386 |
} |
| 387 |
else |
| 388 |
*nread = 0; |
| 389 |
if (n < length) |
| 390 |
{ |
| 391 |
if (ferror(r->u.file)) |
| 392 |
r->error = errno; |
| 393 |
r->eof = 1; |
| 394 |
if (!n) |
| 395 |
return gpg_error (GPG_ERR_EOF); |
| 396 |
} |
| 397 |
} |
| 398 |
else if (r->type == READER_TYPE_CB) |
| 399 |
{ |
| 400 |
if (r->eof) |
| 401 |
return gpg_error (GPG_ERR_EOF); |
| 402 |
|
| 403 |
if (r->u.cb.fnc (r->u.cb.value, buffer, length, nread)) |
| 404 |
{ |
| 405 |
*nread = 0; |
| 406 |
r->eof = 1; |
| 407 |
return gpg_error (GPG_ERR_EOF); |
| 408 |
} |
| 409 |
r->nread += *nread; |
| 410 |
} |
| 411 |
else if (r->type == READER_TYPE_FD) |
| 412 |
{ |
| 413 |
ssize_t n; |
| 414 |
|
| 415 |
if (r->eof) |
| 416 |
return gpg_error (GPG_ERR_EOF); |
| 417 |
|
| 418 |
if (!length) |
| 419 |
{ |
| 420 |
*nread = 0; |
| 421 |
return 0; |
| 422 |
} |
| 423 |
|
| 424 |
n = read (r->u.fd, buffer, length); |
| 425 |
if (n > 0) |
| 426 |
{ |
| 427 |
r->nread += n; |
| 428 |
*nread = n; |
| 429 |
} |
| 430 |
else |
| 431 |
{ |
| 432 |
*nread = 0; |
| 433 |
|
| 434 |
if (n < 0) |
| 435 |
{ |
| 436 |
r->error = errno; |
| 437 |
return gpg_error_from_errno (errno); |
| 438 |
} |
| 439 |
else |
| 440 |
{ |
| 441 |
r->eof = 1; |
| 442 |
return gpg_error (GPG_ERR_EOF); |
| 443 |
} |
| 444 |
} |
| 445 |
} |
| 446 |
else |
| 447 |
return gpg_error (GPG_ERR_BUG); |
| 448 |
|
| 449 |
return 0; |
| 450 |
} |
| 451 |
|
| 452 |
gpg_error_t |
| 453 |
ksba_reader_unread (ksba_reader_t r, const void *buffer, size_t count) |
| 454 |
{ |
| 455 |
if (!r || !buffer) |
| 456 |
return gpg_error (GPG_ERR_INV_VALUE); |
| 457 |
if (!count) |
| 458 |
return 0; |
| 459 |
|
| 460 |
/* Make sure that we do not push more bytes back than we have read. |
| 461 |
Otherwise r->nread won't have a clear semantic. */ |
| 462 |
if (r->nread < count) |
| 463 |
return gpg_error (GPG_ERR_CONFLICT); |
| 464 |
|
| 465 |
if (!r->unread.buf) |
| 466 |
{ |
| 467 |
r->unread.size = count + 100; |
| 468 |
r->unread.buf = xtrymalloc (r->unread.size); |
| 469 |
if (!r->unread.buf) |
| 470 |
return gpg_error (GPG_ERR_ENOMEM); |
| 471 |
r->unread.length = count; |
| 472 |
r->unread.readpos = 0; |
| 473 |
memcpy (r->unread.buf, buffer, count); |
| 474 |
r->nread -= count; |
| 475 |
} |
| 476 |
else if (r->unread.length + count < r->unread.size) |
| 477 |
{ |
| 478 |
memcpy (r->unread.buf+r->unread.length, buffer, count); |
| 479 |
r->unread.length += count; |
| 480 |
r->nread -= count; |
| 481 |
} |
| 482 |
else |
| 483 |
return gpg_error (GPG_ERR_NOT_IMPLEMENTED); /* fixme: easy to do */ |
| 484 |
|
| 485 |
return 0; |
| 486 |
} |