comparison tools/lib64gfx.c @ 1853:2081bb4a998a

Improve error messaging in generic c64 image encode/decode.
author Matti Hamalainen <ccr@tnsp.org>
date Fri, 22 Jun 2018 23:44:22 +0300
parents 219417325036
children 1816211341a0
comparison
equal deleted inserted replaced
1852:219417325036 1853:2081bb4a998a
870 for (int i = 0; i < D64_MAX_ENCDEC_OPS; i++) 870 for (int i = 0; i < D64_MAX_ENCDEC_OPS; i++)
871 { 871 {
872 const DMC64EncDecOp *op = fmtGetEncDecOp(fmt, i); 872 const DMC64EncDecOp *op = fmtGetEncDecOp(fmt, i);
873 const Uint8 *src; 873 const Uint8 *src;
874 DMC64MemBlock *blk = NULL; 874 DMC64MemBlock *blk = NULL;
875 const char *blkname = NULL; 875 const char *subjname = dmC64GetOpSubjectName(op->subject);
876 size_t size; 876 size_t size;
877 Uint8 value; 877 Uint8 value;
878 878
879 // Check for last operator 879 // Check for last operator
880 if (op->type == DO_LAST) 880 if (op->type == DO_LAST)
886 886
887 // Check size 887 // Check size
888 if (!dmC64GetOpSize(op, fmt, &size)) 888 if (!dmC64GetOpSize(op, fmt, &size))
889 { 889 {
890 return dmError(DMERR_INVALID_DATA, 890 return dmError(DMERR_INVALID_DATA,
891 "Decode op SIZE out of bounds, op #%d type=%d, subj=%d, offs=%d ($%04x), " 891 "Decode op SIZE out of bounds, op #%d type=%d, subj=%s, offs=%d ($%04x), "
892 "bank=%d, size=%d ($%04x) vs. allocated %d ($%04x)\n", 892 "bank=%d, size=%d ($%04x) vs. allocated %d ($%04x)\n",
893 i, op->type, op->subject, op->offs, op->offs, op->bank, size, size, op->size, op->size); 893 i, op->type, subjname, op->offs, op->offs, op->bank,
894 size, size, op->size, op->size);
894 } 895 }
895 896
896 // Is the operation inside the bounds? 897 // Is the operation inside the bounds?
897 if (op->offs + size > buf->len + 1) 898 if (op->offs + size > buf->len + 1)
898 { 899 {
899 return dmError(DMERR_INVALID_DATA, 900 return dmError(DMERR_INVALID_DATA,
900 "Decode DATA out of bounds, op #%d type=%d, subj=%d, offs=%d ($%04x), " 901 "Decode DATA out of bounds, op #%d type=%d, subj=%s, offs=%d ($%04x), "
901 "bank=%d, size=%d ($%04x) @ %d ($%04x)\n", 902 "bank=%d, size=%d ($%04x) @ %d ($%04x)\n",
902 i, op->type, op->subject, op->offs, op->offs, op->bank, 903 i, op->type, subjname, op->offs, op->offs, op->bank,
903 size, size, buf->len, buf->len); 904 size, size, buf->len, buf->len);
904 } 905 }
905 906
906 src = buf->data + op->offs; 907 src = buf->data + op->offs;
907 908
920 case DS_BITMAP_RAM: 921 case DS_BITMAP_RAM:
921 case DS_CHAR_DATA: 922 case DS_CHAR_DATA:
922 case DS_EXTRA_DATA: 923 case DS_EXTRA_DATA:
923 // XXX BZZZT .. a nasty cast here --v 924 // XXX BZZZT .. a nasty cast here --v
924 dmC64GetOpMemBlock(img, op->subject, op->bank, (const DMC64MemBlock **) &blk); 925 dmC64GetOpMemBlock(img, op->subject, op->bank, (const DMC64MemBlock **) &blk);
925 blkname = dmC64GetOpSubjectName(op->subject);
926 926
927 if ((dmC64MemBlockReAlloc(blk, op->offs2 + size)) != DMERR_OK) 927 if ((dmC64MemBlockReAlloc(blk, op->offs2 + size)) != DMERR_OK)
928 { 928 {
929 return dmError(DMERR_MALLOC, 929 return dmError(DMERR_MALLOC,
930 "Could not allocate '%s' block! " 930 "Could not allocate '%s' block! "
931 "op #%d, offs=%d ($%04x), bank=%d, size=%d ($%04x) @ %d ($%04x)\n", 931 "op #%d, offs=%d ($%04x), bank=%d, size=%d ($%04x) @ %d ($%04x)\n",
932 blkname, i, op->offs, op->offs, op->bank, size, size, buf->len, buf->len); 932 subjname, i, op->offs, op->offs, op->bank, op->offs2 + size, op->offs2 + size, buf->len, buf->len);
933 } 933 }
934 switch (op->type) 934 switch (op->type)
935 { 935 {
936 case DO_COPY: 936 case DO_COPY:
937 memcpy(blk->data + op->offs2, src, size); 937 memcpy(blk->data + op->offs2, src, size);
1077 for (int i = 0; i < D64_MAX_ENCDEC_OPS; i++) 1077 for (int i = 0; i < D64_MAX_ENCDEC_OPS; i++)
1078 { 1078 {
1079 const DMC64EncDecOp *op = fmtGetEncDecOp(fmt, i); 1079 const DMC64EncDecOp *op = fmtGetEncDecOp(fmt, i);
1080 size_t size, chksize; 1080 size_t size, chksize;
1081 const DMC64MemBlock *blk = NULL; 1081 const DMC64MemBlock *blk = NULL;
1082 const char *blkname = NULL; 1082 const char *subjname = dmC64GetOpSubjectName(op->subject);
1083 Uint8 value; 1083 Uint8 value;
1084 1084
1085 // Check for last operator 1085 // Check for last operator
1086 if (op->type == DO_LAST) 1086 if (op->type == DO_LAST)
1087 break; 1087 break;
1092 1092
1093 // Check size 1093 // Check size
1094 if (!dmC64GetOpSize(op, fmt, &size)) 1094 if (!dmC64GetOpSize(op, fmt, &size))
1095 { 1095 {
1096 res = dmError(DMERR_INVALID_DATA, 1096 res = dmError(DMERR_INVALID_DATA,
1097 "Encode op SIZE out of bounds, op #%d type=%d, offs=%d ($%04x), " 1097 "Encode op SIZE out of bounds, op #%d type=%d, subj=%s, offs=%d ($%04x), "
1098 "bank=%d, size=%d ($%04x) vs. allocated %d ($%04x)\n", 1098 "bank=%d, size=%d ($%04x) vs. allocated %d ($%04x)\n",
1099 i, op->type, op->offs, op->offs, op->bank, size, size, buf->size, buf->size); 1099 subjname, i, op->type, subjname, op->offs, op->offs, op->bank, size, size, buf->size, buf->size);
1100 goto err; 1100 goto err;
1101 } 1101 }
1102 1102
1103 // Do we need to reallocate some more space? 1103 // Do we need to reallocate some more space?
1104 chksize = buf->offs + op->offs + size; 1104 chksize = buf->offs + op->offs + size;
1123 case DS_SCREEN_RAM: 1123 case DS_SCREEN_RAM:
1124 case DS_BITMAP_RAM: 1124 case DS_BITMAP_RAM:
1125 case DS_CHAR_DATA: 1125 case DS_CHAR_DATA:
1126 case DS_EXTRA_DATA: 1126 case DS_EXTRA_DATA:
1127 dmC64GetOpMemBlock(img, op->subject, op->bank, &blk); 1127 dmC64GetOpMemBlock(img, op->subject, op->bank, &blk);
1128 blkname = dmC64GetOpSubjectName(op->subject);
1129
1130 switch (op->type) 1128 switch (op->type)
1131 { 1129 {
1132 case DO_COPY: 1130 case DO_COPY:
1133 if (blk->data == NULL) 1131 if (blk->data == NULL)
1134 { 1132 {
1135 res = dmError(DMERR_NULLPTR, 1133 res = dmError(DMERR_NULLPTR,
1136 "'%s' block is NULL in " 1134 "'%s' block is NULL in "
1137 "op #%d, offs=%d ($%04x), bank=%d, size=%d ($%04x) @ %d ($%04x)\n", 1135 "op #%d, offs=%d ($%04x), bank=%d, size=%d ($%04x) @ %d ($%04x)\n",
1138 blkname, i, op->offs, op->offs, op->bank, size, size, buf->len, buf->len); 1136 subjname, i, op->offs, op->offs, op->bank, size, size, buf->len, buf->len);
1139 goto err; 1137 goto err;
1140 } 1138 }
1141 if (op->offs2 + size > blk->size) 1139 if (op->offs2 + size > blk->size)
1142 { 1140 {
1143 res = dmError(DMERR_INTERNAL, 1141 res = dmError(DMERR_INTERNAL,
1144 "'%s' size mismatch %d <> %d in " 1142 "'%s' size mismatch %d <> %d in "
1145 "op #%d, offs=%d ($%04x), bank=%d, offs2=%d ($%02x), size=%d ($%04x)\n", 1143 "op #%d, offs=%d ($%04x), bank=%d, offs2=%d ($%02x), size=%d ($%04x)\n",
1146 blkname, op->offs2 + size, blk->size, i, op->offs, op->offs, op->bank, op->offs2, op->offs2, size, size); 1144 subjname, op->offs2 + size, blk->size, i, op->offs, op->offs, op->bank, op->offs2, op->offs2, size, size);
1147 goto err; 1145 goto err;
1148 } 1146 }
1149 memcpy(dst, blk->data + op->offs2, size); 1147 memcpy(dst, blk->data + op->offs2, size);
1150 break; 1148 break;
1151 1149
1195 } 1193 }
1196 break; 1194 break;
1197 1195
1198 default: 1196 default:
1199 return dmError(DMERR_INTERNAL, 1197 return dmError(DMERR_INTERNAL,
1200 "Unhandled subject %d in " 1198 "Unhandled subject '%s' in "
1201 "op #%d, offs=%d ($%04x), bank=%d, size=%d ($%04x) @ %d ($%04x)\n", 1199 "op #%d, offs=%d ($%04x), bank=%d, size=%d ($%04x) @ %d ($%04x)\n",
1202 op->subject, i, op->offs, op->offs, op->bank, size, size, buf->len, buf->len); 1200 subjname, i, op->offs, op->offs, op->bank, size, size, buf->len, buf->len);
1203 } 1201 }
1204 break; 1202 break;
1205 1203
1206 case DO_ENC_FUNC: 1204 case DO_ENC_FUNC:
1207 if (op->encFunction == NULL) 1205 if (op->encFunction == NULL)