Changeset ea459d4 in mainline for uspace/lib/riff
- Timestamp:
- 2020-09-24T14:25:21Z (5 years ago)
- Branches:
- lfn, master, serial, ticket/834-toolchain-update, topic/msim-upgrade, topic/simplify-dev-export
- Children:
- 120031a5
- Parents:
- aaf962e6
- git-author:
- Jiri Svoboda <jiri@…> (2020-09-22 17:25:10)
- git-committer:
- Jiri Svoboda <jiri@…> (2020-09-24 14:25:21)
- Location:
- uspace/lib/riff
- Files:
-
- 3 edited
Legend:
- Unmodified
- Added
- Removed
-
uspace/lib/riff/include/riff/chunk.h
raaf962e6 rea459d4 55 55 extern errno_t riff_rchunk_list_match(riff_rchunk_t *, riff_ltype_t, 56 56 riff_rchunk_t *); 57 extern errno_t riff_rchunk_seek(riff_rchunk_t *, long, int); 57 58 extern errno_t riff_rchunk_end(riff_rchunk_t *); 58 59 extern errno_t riff_read(riff_rchunk_t *, void *, size_t, size_t *); -
uspace/lib/riff/src/chunk.c
raaf962e6 rea459d4 393 393 } 394 394 395 /** Seek to position in chunk. 396 * 397 * @param rchunk RIFF chunk 398 * @param offset Offset 399 * @param whence SEEK_SET, SEEK_CUR or SEEK_END 400 * @return EOK on success or an error code 401 */ 402 errno_t riff_rchunk_seek(riff_rchunk_t *rchunk, long offset, int whence) 403 { 404 long pos; 405 long dest; 406 int rv; 407 408 switch (whence) { 409 case SEEK_SET: 410 dest = rchunk->ckstart + offset; 411 break; 412 case SEEK_END: 413 dest = rchunk->ckstart + rchunk->cksize + offset; 414 break; 415 case SEEK_CUR: 416 pos = ftell(rchunk->riffr->f); 417 if (pos < 0) 418 return EIO; 419 dest = pos + offset; 420 break; 421 default: 422 return EINVAL; 423 } 424 425 if (dest < rchunk->ckstart || dest > rchunk->ckstart + rchunk->cksize) 426 return ELIMIT; 427 428 rv = fseek(rchunk->riffr->f, dest, SEEK_SET); 429 if (rv < 0) 430 return EIO; 431 432 return EOK; 433 } 434 395 435 /** Return chunk data size. 396 436 * … … 431 471 /** Finish reading RIFF chunk. 432 472 * 433 * Seek to the first byte after end of chunk. 473 * Seek to the first byte after end of chunk. It is allowed, though, 474 * to return to the chunk later, e.g. using riff_rchunk_seek(@a rchunk, ..). 434 475 * 435 476 * @param rchunk Chunk structure … … 444 485 return EIO; 445 486 446 rchunk->riffr = NULL;447 487 return EOK; 448 488 } -
uspace/lib/riff/test/chunk.c
raaf962e6 rea459d4 544 544 } 545 545 546 /** Seek back to different positions in a chunk */ 547 PCUT_TEST(rchunk_seek) 548 { 549 char fname[L_tmpnam]; 550 char *p; 551 riffw_t *rw; 552 riffr_t *rr; 553 riff_wchunk_t wriffck; 554 riff_wchunk_t wdatack; 555 riff_rchunk_t rriffck; 556 riff_rchunk_t rdatack; 557 uint32_t rword; 558 errno_t rc; 559 560 p = tmpnam(fname); 561 PCUT_ASSERT_NOT_NULL(p); 562 563 /* Write RIFF file */ 564 565 rc = riff_wopen(p, &rw); 566 PCUT_ASSERT_ERRNO_VAL(EOK, rc); 567 PCUT_ASSERT_NOT_NULL(rw); 568 569 rc = riff_wchunk_start(rw, CKID_RIFF, &wriffck); 570 PCUT_ASSERT_ERRNO_VAL(EOK, rc); 571 572 /* Write data chunk */ 573 574 rc = riff_wchunk_start(rw, CKID_dat1, &wdatack); 575 PCUT_ASSERT_ERRNO_VAL(EOK, rc); 576 577 rc = riff_write_uint32(rw, 1); 578 PCUT_ASSERT_ERRNO_VAL(EOK, rc); 579 580 rc = riff_write_uint32(rw, 2); 581 PCUT_ASSERT_ERRNO_VAL(EOK, rc); 582 583 rc = riff_write_uint32(rw, 3); 584 PCUT_ASSERT_ERRNO_VAL(EOK, rc); 585 586 rc = riff_write_uint32(rw, 4); 587 PCUT_ASSERT_ERRNO_VAL(EOK, rc); 588 589 rc = riff_wchunk_end(rw, &wdatack); 590 PCUT_ASSERT_ERRNO_VAL(EOK, rc); 591 592 rc = riff_wchunk_end(rw, &wriffck); 593 PCUT_ASSERT_ERRNO_VAL(EOK, rc); 594 595 rc = riff_wclose(rw); 596 PCUT_ASSERT_ERRNO_VAL(EOK, rc); 597 598 /* Read back RIFF file */ 599 600 rc = riff_ropen(p, &rriffck, &rr); 601 PCUT_ASSERT_ERRNO_VAL(EOK, rc); 602 PCUT_ASSERT_NOT_NULL(rr); 603 604 PCUT_ASSERT_INT_EQUALS(CKID_RIFF, rriffck.ckid); 605 606 /* Read data chunk */ 607 608 rc = riff_rchunk_start(&rriffck, &rdatack); 609 PCUT_ASSERT_ERRNO_VAL(EOK, rc); 610 PCUT_ASSERT_INT_EQUALS(CKID_dat1, rdatack.ckid); 611 612 rc = riff_read_uint32(&rdatack, &rword); 613 PCUT_ASSERT_ERRNO_VAL(EOK, rc); 614 PCUT_ASSERT_INT_EQUALS(1, rword); 615 616 rc = riff_rchunk_end(&rdatack); 617 PCUT_ASSERT_ERRNO_VAL(EOK, rc); 618 619 /* Try reading first word of data chunk again */ 620 621 rc = riff_rchunk_seek(&rdatack, 0, SEEK_SET); 622 PCUT_ASSERT_ERRNO_VAL(EOK, rc); 623 624 rc = riff_read_uint32(&rdatack, &rword); 625 PCUT_ASSERT_ERRNO_VAL(EOK, rc); 626 PCUT_ASSERT_INT_EQUALS(1, rword); 627 628 /* Try reading last word of data chunk */ 629 630 rc = riff_rchunk_seek(&rdatack, -4, SEEK_END); 631 PCUT_ASSERT_ERRNO_VAL(EOK, rc); 632 633 rc = riff_read_uint32(&rdatack, &rword); 634 PCUT_ASSERT_ERRNO_VAL(EOK, rc); 635 PCUT_ASSERT_INT_EQUALS(4, rword); 636 637 /* Try reading previous word of data chunk */ 638 639 rc = riff_rchunk_seek(&rdatack, -8, SEEK_CUR); 640 PCUT_ASSERT_ERRNO_VAL(EOK, rc); 641 642 rc = riff_read_uint32(&rdatack, &rword); 643 PCUT_ASSERT_ERRNO_VAL(EOK, rc); 644 PCUT_ASSERT_INT_EQUALS(3, rword); 645 646 rc = riff_rclose(rr); 647 PCUT_ASSERT_ERRNO_VAL(EOK, rc); 648 649 (void) remove(p); 650 } 651 546 652 PCUT_EXPORT(chunk);
Note:
See TracChangeset
for help on using the changeset viewer.