Mercurial > hg > sidinfo
comparison sidlib.c @ 231:bb8ce494267b
Remove currently dead code.
author | Matti Hamalainen <ccr@tnsp.org> |
---|---|
date | Sat, 04 Jan 2020 11:34:46 +0200 |
parents | 1e860046a4cc |
children | 03b6f8604a7f |
comparison
equal
deleted
inserted
replaced
230:1e860046a4cc | 231:bb8ce494267b |
---|---|
4 * (C) Copyright 2014-2020 Tecnic Software productions (TNSP) | 4 * (C) Copyright 2014-2020 Tecnic Software productions (TNSP) |
5 */ | 5 */ |
6 #include "sidlib.h" | 6 #include "sidlib.h" |
7 #include "th_endian.h" | 7 #include "th_endian.h" |
8 #include "th_string.h" | 8 #include "th_string.h" |
9 | |
10 | |
11 #define SIDLIB_DB_MAGIC "SIDLibDB" | |
12 #define SIDLIB_DB_VERSION 0x0100 | |
13 | |
14 | |
15 typedef struct | |
16 { | |
17 char magic[8]; // File magic ID | |
18 uint16_t version; // Version | |
19 uint32_t nnodes; // Number of song nodes | |
20 // struct { | |
21 // th_md5hash_t hash; // Hash for this SID file | |
22 // uint16_t nlengths; // Number of song (lengths) | |
23 // uint16_t length[nlengths]; // Length in seconds | |
24 // } * nnodes; | |
25 th_md5hash_t hash; | |
26 } PSIDLibHdr; | |
27 | 9 |
28 | 10 |
29 BOOL sidlib_fread_str(th_ioctx *ctx, char **str, const size_t len) | 11 BOOL sidlib_fread_str(th_ioctx *ctx, char **str, const size_t len) |
30 { | 12 { |
31 char *tmp = th_malloc(len + 1); | 13 char *tmp = th_malloc(len + 1); |
559 | 541 |
560 return THERR_OK; | 542 return THERR_OK; |
561 } | 543 } |
562 | 544 |
563 | 545 |
564 // | |
565 // Read binary format SLDB | |
566 // | |
567 int sidlib_sldb_read_bin(th_ioctx *ctx, SIDLibSLDB *dbh) | |
568 { | |
569 PSIDLibHdr hdr; | |
570 th_md5state_t state; | |
571 th_md5hash_t hash; | |
572 size_t n; | |
573 | |
574 // Check pointers | |
575 if (ctx == NULL || dbh == NULL) | |
576 return THERR_NULLPTR; | |
577 | |
578 if (!thfread_str(ctx, &hdr.magic, sizeof(hdr.magic)) || | |
579 !thfread_le16(ctx, &hdr.version) || | |
580 !thfread_le32(ctx, &hdr.nnodes)) | |
581 return THERR_FREAD; | |
582 | |
583 // Check magic and version | |
584 if (memcmp(hdr.magic, SIDLIB_DB_MAGIC, sizeof(hdr.magic)) != 0) | |
585 return THERR_NOT_SUPPORTED; | |
586 | |
587 if (hdr.version != SIDLIB_DB_VERSION) | |
588 return THERR_NOT_SUPPORTED; | |
589 | |
590 // Make some reasonable assumptions about validity | |
591 if (hdr.nnodes == 0 || hdr.nnodes > 256*1024) | |
592 return THERR_INVALID_DATA; | |
593 | |
594 // Initialize MD5 state | |
595 th_md5_init(&state); | |
596 th_md5_append_ne16(&state, hdr.version); | |
597 th_md5_append_ne32(&state, hdr.nnodes); | |
598 | |
599 // Allocate index | |
600 dbh->nnodes = hdr.nnodes; | |
601 dbh->pindex = (SIDLibSLDBNode **) th_malloc(sizeof(SIDLibSLDBNode *) * dbh->nnodes); | |
602 if (dbh->pindex == NULL) | |
603 return THERR_MALLOC; | |
604 | |
605 // Read nodes | |
606 for (n = 0; n < dbh->nnodes; n++) | |
607 { | |
608 SIDLibSLDBNode *node; | |
609 th_md5hash_t mhash; | |
610 uint16_t tmpn; | |
611 int index; | |
612 | |
613 // Read node hash and nlengths | |
614 if (!thfread_str(ctx, mhash, TH_MD5HASH_LENGTH) || | |
615 !thfread_le16(ctx, &tmpn)) | |
616 return THERR_FREAD; | |
617 | |
618 // Sanity check | |
619 if (tmpn == 0 || tmpn > 2048) | |
620 return THERR_INVALID_DATA; | |
621 | |
622 // Append to hash | |
623 th_md5_append(&state, mhash, TH_MD5HASH_LENGTH); | |
624 th_md5_append_ne16(&state, tmpn); | |
625 | |
626 // Allocate node | |
627 node = (SIDLibSLDBNode *) th_malloc0(sizeof(SIDLibSLDBNode)); | |
628 if (node == NULL) | |
629 return THERR_MALLOC; | |
630 | |
631 node->nlengths = tmpn; | |
632 memcpy(node->hash, mhash, sizeof(mhash)); | |
633 | |
634 node->lengths = (int *) th_calloc(node->nlengths, sizeof(int)); | |
635 if (node->lengths == NULL) | |
636 { | |
637 th_free(node); | |
638 th_io_error(ctx, THERR_MALLOC, | |
639 "Could not allocate memory for node.\n"); | |
640 return ctx->status; | |
641 } | |
642 | |
643 // Read node lenghts | |
644 for (index = 0; index < node->nlengths; index++) | |
645 { | |
646 uint16_t tmpl; | |
647 if (!thfread_le16(ctx, &tmpl)) | |
648 return THERR_FREAD; | |
649 | |
650 th_md5_append_ne16(&state, tmpl); | |
651 node->lengths[index] = tmpl; | |
652 } | |
653 | |
654 th_llist_append_node((th_llist_t **) &dbh->nodes, (th_llist_t *) node); | |
655 dbh->pindex[n] = node; | |
656 } | |
657 | |
658 // Read stored checksum hash | |
659 if (!thfread_str(ctx, hdr.hash, sizeof(hdr.hash))) | |
660 return THERR_FREAD; | |
661 | |
662 // Compare to what we get | |
663 th_md5_finish(&state, hash); | |
664 if (memcmp(hash, hdr.hash, sizeof(hdr.hash)) != 0) | |
665 return THERR_INVALID_DATA; | |
666 | |
667 return THERR_OK; | |
668 } | |
669 | |
670 | |
671 int sidlib_sldb_write_bin(th_ioctx *ctx, const SIDLibSLDB *dbh) | |
672 { | |
673 th_md5state_t state; | |
674 th_md5hash_t hash; | |
675 size_t n; | |
676 | |
677 // Check pointers | |
678 if (ctx == NULL || dbh == NULL) | |
679 return THERR_NULLPTR; | |
680 | |
681 // Write header | |
682 if (!thfwrite_str(ctx, SIDLIB_DB_MAGIC, 8) || | |
683 !thfwrite_le16(ctx, SIDLIB_DB_VERSION) || | |
684 !thfwrite_le32(ctx, dbh->nnodes)) | |
685 return THERR_FWRITE; | |
686 | |
687 th_md5_init(&state); | |
688 th_md5_append_ne16(&state, SIDLIB_DB_VERSION); | |
689 th_md5_append_ne32(&state, dbh->nnodes); | |
690 | |
691 // Write nodes | |
692 for (n = 0; n < dbh->nnodes; n++) | |
693 { | |
694 const SIDLibSLDBNode *node = dbh->pindex[n]; | |
695 uint16_t tmpn; | |
696 int index; | |
697 | |
698 // Sanity check | |
699 if (node->nlengths <= 0 || node->nlengths > 2048) | |
700 return THERR_INVALID_DATA; | |
701 | |
702 tmpn = node->nlengths; | |
703 th_md5_append(&state, node->hash, TH_MD5HASH_LENGTH); | |
704 th_md5_append_ne16(&state, tmpn); | |
705 | |
706 // Write node data | |
707 if (!thfwrite_str(ctx, node->hash, TH_MD5HASH_LENGTH) || | |
708 !thfwrite_le16(ctx, tmpn)) | |
709 return THERR_FWRITE; | |
710 | |
711 for (index = 0; index < node->nlengths; index++) | |
712 { | |
713 if (node->lengths[index] > 16378) | |
714 return THERR_INVALID_DATA; | |
715 | |
716 if (!thfwrite_le16(ctx, node->lengths[index])) | |
717 return THERR_FWRITE; | |
718 | |
719 th_md5_append_ne16(&state, node->lengths[index]); | |
720 } | |
721 } | |
722 | |
723 th_md5_finish(&state, hash); | |
724 | |
725 if (!thfwrite_str(ctx, hash, sizeof(hash))) | |
726 return THERR_FWRITE; | |
727 | |
728 return THERR_OK; | |
729 } | |
730 | |
731 | |
732 // Free a given song-length database | 546 // Free a given song-length database |
733 // | 547 // |
734 void sidlib_sldb_free(SIDLibSLDB *dbh) | 548 void sidlib_sldb_free(SIDLibSLDB *dbh) |
735 { | 549 { |
736 if (dbh != NULL) | 550 if (dbh != NULL) |