comparison tools/lib64gfx.c @ 1916:34ba8e2d2dd7

Split dmC64GetOpSize() to dmC64GetSubjectSize() and dmC64GetOpSubjectSize().
author Matti Hamalainen <ccr@tnsp.org>
date Wed, 27 Jun 2018 18:36:03 +0300
parents b052754a1a23
children ade671278806
comparison
equal deleted inserted replaced
1915:788cfc7096f3 1916:34ba8e2d2dd7
766 766
767 return DMERR_OK; 767 return DMERR_OK;
768 } 768 }
769 769
770 770
771 BOOL dmC64GetOpSize(const DMC64EncDecOp *op, const DMC64ImageFormat *fmt, size_t *size) 771 size_t dmC64GetSubjectSize(const int subject, const DMC64ImageFormat *fmt)
772 { 772 {
773 switch (op->subject) 773 switch (subject)
774 { 774 {
775 case DS_SCREEN_RAM: 775 case DS_SCREEN_RAM:
776 case DS_COLOR_RAM: 776 case DS_COLOR_RAM:
777 *size = fmt->format->chHeight * fmt->format->chWidth; 777 return fmt->format->chHeight * fmt->format->chWidth;
778 break; 778 break;
779 779
780 case DS_BITMAP_RAM: 780 case DS_BITMAP_RAM:
781 *size = fmt->format->chHeight * fmt->format->chWidth * 8; 781 return fmt->format->chHeight * fmt->format->chWidth * 8;
782 break; 782 break;
783 783
784 case DS_CHAR_DATA: 784 case DS_CHAR_DATA:
785 *size = C64_MAX_CHARS * C64_CHR_SIZE; 785 return C64_MAX_CHARS * C64_CHR_SIZE;
786 break; 786 break;
787 787
788 case DS_D020: 788 case DS_D020:
789 case DS_BGCOL: 789 case DS_BGCOL:
790 case DS_D021: 790 case DS_D021:
791 case DS_D022: 791 case DS_D022:
792 case DS_D023: 792 case DS_D023:
793 case DS_D024: 793 case DS_D024:
794 *size = 1; 794 return 1;
795 break; 795 break;
796 796
797 default: 797 default:
798 // Default to size of 0 798 // Default to size of 0
799 *size = 0; 799 return 0;
800 break; 800 break;
801 } 801 }
802 }
803
804
805 size_t dmC64GetOpSubjectSize(const DMC64EncDecOp *op, const DMC64ImageFormat *fmt)
806 {
807 size_t size = dmC64GetSubjectSize(op->subject, fmt);
802 808
803 // If the operator specified size is larger, use it. 809 // If the operator specified size is larger, use it.
804 if (op->size > *size) 810 if (op->size > size)
805 *size = op->size; 811 size = op->size;
806 812
807 return TRUE; 813 return size;
808 } 814 }
809 815
810 816
811 const char *dmC64GetOpSubjectName(const int subject) 817 const char *dmC64GetOpSubjectName(const int subject)
812 { 818 {
880 886
881 // Check operation validity 887 // Check operation validity
882 if ((res = dmC64SanityCheckEncDecOp(i, op, img)) != DMERR_OK) 888 if ((res = dmC64SanityCheckEncDecOp(i, op, img)) != DMERR_OK)
883 return res; 889 return res;
884 890
885 // Check size
886 if (!dmC64GetOpSize(op, fmt, &size))
887 {
888 return dmError(DMERR_INVALID_DATA,
889 "Decode op SIZE out of bounds, op #%d type=%d, subj=%s, offs=%d ($%04x), "
890 "bank=%d, size=%d ($%04x) vs. allocated %d ($%04x)\n",
891 i, op->type, subjname, op->offs, op->offs, op->bank,
892 size, size, op->size, op->size);
893 }
894
895 // Is the operation inside the bounds? 891 // Is the operation inside the bounds?
892 size = dmC64GetOpSubjectSize(op, fmt);
896 if (op->type == DO_COPY && op->offs + size > buf->len + 1) 893 if (op->type == DO_COPY && op->offs + size > buf->len + 1)
897 { 894 {
898 return dmError(DMERR_INVALID_DATA, 895 return dmError(DMERR_INVALID_DATA,
899 "Decode SRC out of bounds, op #%d type=%d, subj=%s, offs=%d ($%04x), " 896 "Decode SRC out of bounds, op #%d type=%d, subj=%s, offs=%d ($%04x), "
900 "bank=%d, size=%d ($%04x) @ %d ($%04x)\n", 897 "bank=%d, size=%d ($%04x) @ %d ($%04x)\n",
1086 1083
1087 // Check operation validity 1084 // Check operation validity
1088 if ((res = dmC64SanityCheckEncDecOp(i, op, img)) != DMERR_OK) 1085 if ((res = dmC64SanityCheckEncDecOp(i, op, img)) != DMERR_OK)
1089 goto err; 1086 goto err;
1090 1087
1091 // Check size
1092 if (!dmC64GetOpSize(op, fmt, &size))
1093 {
1094 res = dmError(DMERR_INVALID_DATA,
1095 "Encode op SIZE out of bounds, op #%d type=%d, subj=%s, offs=%d ($%04x), "
1096 "bank=%d, size=%d ($%04x) vs. allocated %d ($%04x)\n",
1097 subjname, i, op->type, subjname, op->offs, op->offs, op->bank, size, size, buf->size, buf->size);
1098 goto err;
1099 }
1100
1101 // Do we need to reallocate some more space? 1088 // Do we need to reallocate some more space?
1089 size = dmC64GetOpSubjectSize(op, fmt);
1102 chksize = buf->offs + op->offs + size; 1090 chksize = buf->offs + op->offs + size;
1103 if (!dmGrowBufCheckGrow(buf, chksize)) 1091 if (!dmGrowBufCheckGrow(buf, chksize))
1104 { 1092 {
1105 res = dmError(DMERR_MALLOC, 1093 res = dmError(DMERR_MALLOC,
1106 "Could not re-allocate %d bytes of memory for C64 image encoding buffer.\n", 1094 "Could not re-allocate %d bytes of memory for C64 image encoding buffer.\n",