comparison tools/lib64gfx.c @ 958:985225a93aeb

Add error code parameter to dmError() and dmErrorVA().
author Matti Hamalainen <ccr@tnsp.org>
date Fri, 27 Feb 2015 03:58:25 +0200
parents 93898b9a04ad
children d0898867ec4c
comparison
equal deleted inserted replaced
957:b66653c9acb3 958:985225a93aeb
666 666
667 static int dmC64SanityCheckEncDecOp(const int i, const DMC64EncDecOp *op) 667 static int dmC64SanityCheckEncDecOp(const int i, const DMC64EncDecOp *op)
668 { 668 {
669 if (op->bank < 0 || op->bank >= C64_SCR_MAX_BANK) 669 if (op->bank < 0 || op->bank >= C64_SCR_MAX_BANK)
670 { 670 {
671 dmError("Invalid bank %d definition in generic encode/decode operator %d @ #%d.\n", 671 return dmError(DMERR_INTERNAL,
672 "Invalid bank %d definition in generic encode/decode operator %d @ #%d.\n",
672 op->bank, op->type, i); 673 op->bank, op->type, i);
673 return DMERR_INTERNAL;
674 } 674 }
675 675
676 if (op->type < 0 || op->type >= DT_LAST) 676 if (op->type < 0 || op->type >= DT_LAST)
677 { 677 {
678 dmError("Invalid encode/decode operator type %d @ #%d.\n", 678 return dmError(DMERR_INTERNAL,
679 "Invalid encode/decode operator type %d @ #%d.\n",
679 op->type, i); 680 op->type, i);
680 return DMERR_INTERNAL;
681 } 681 }
682 682
683 return DMERR_OK; 683 return DMERR_OK;
684 } 684 }
685 685
740 if (buf == NULL || img == NULL || fmt == NULL) 740 if (buf == NULL || img == NULL || fmt == NULL)
741 return DMERR_NULLPTR; 741 return DMERR_NULLPTR;
742 742
743 if (fmt->nencdecOps < 0 || fmt->nencdecOps >= D64_MAX_ENCDEC_OPS) 743 if (fmt->nencdecOps < 0 || fmt->nencdecOps >= D64_MAX_ENCDEC_OPS)
744 { 744 {
745 dmError("Invalid number of enc/dec ops in format. Internal error.\n"); 745 return dmError(DMERR_INTERNAL,
746 return DMERR_INTERNAL; 746 "Invalid number of enc/dec ops in format. Internal error.\n");
747 } 747 }
748 748
749 // Clear the image structure, set basics 749 // Clear the image structure, set basics
750 img->type = fmt->type; 750 img->type = fmt->type;
751 img->width = fmt->width; 751 img->width = fmt->width;
766 return res; 766 return res;
767 767
768 // Check size 768 // Check size
769 if (!dmC64GetOpSize(op, fmt, &size)) 769 if (!dmC64GetOpSize(op, fmt, &size))
770 { 770 {
771 dmError("Decode op SIZE out of bounds, op #%d type=%d, offs=%d ($%04x), " 771 return dmError(DMERR_INVALID_DATA,
772 "Decode op SIZE out of bounds, op #%d type=%d, offs=%d ($%04x), "
772 "bank=%d, size=%d ($%04x) vs. allocated %d ($%04x)\n", 773 "bank=%d, size=%d ($%04x) vs. allocated %d ($%04x)\n",
773 i, op->type, op->offs, op->offs, op->bank, size, size, op->size, op->size); 774 i, op->type, op->offs, op->offs, op->bank, size, size, op->size, op->size);
774 return DMERR_INVALID_DATA;
775 } 775 }
776 776
777 // Do we need to reallocate some more space? 777 // Do we need to reallocate some more space?
778 if (op->offs + size > len) 778 if (op->offs + size > len)
779 { 779 {
780 dmError("Decode out of bounds, op #%d type=%d, offs=%d ($%04x), " 780 return dmError(DMERR_INVALID_DATA,
781 "Decode out of bounds, op #%d type=%d, offs=%d ($%04x), "
781 "bank=%d, size=%d ($%04x) @ %d ($%04x)\n", 782 "bank=%d, size=%d ($%04x) @ %d ($%04x)\n",
782 i, op->type, op->offs, op->offs, op->bank, size, size, len, len); 783 i, op->type, op->offs, op->offs, op->bank, size, size, len, len);
783 return DMERR_INVALID_DATA;
784 } 784 }
785 785
786 src = buf + op->offs; 786 src = buf + op->offs;
787 787
788 // Perform operation 788 // Perform operation
802 case DC_D021: img->bgcolor = *src; break; 802 case DC_D021: img->bgcolor = *src; break;
803 case DC_D022: img->d022 = *src; break; 803 case DC_D022: img->d022 = *src; break;
804 case DC_D023: img->d023 = *src; break; 804 case DC_D023: img->d023 = *src; break;
805 case DC_D024: img->d024 = *src; break; 805 case DC_D024: img->d024 = *src; break;
806 default: 806 default:
807 dmError("Unhandled DT_COLOR_REG mode %d in ", 807 return dmError(DMERR_INTERNAL,
808 "op #%d, offs=%d ($%04x), bank=%d, size=%d ($%04x) @ %d ($%04x)\n", 808 "Unhandled DT_COLOR_REG mode %d in ",
809 op->size, i, op->offs, op->offs, op->bank, size, size, len, len); 809 "op #%d, offs=%d ($%04x), bank=%d, size=%d ($%04x) @ %d ($%04x)\n",
810 return DMERR_INTERNAL; 810 op->size, i, op->offs, op->offs, op->bank, size, size, len, len);
811 } 811 }
812 break; 812 break;
813 813
814 case DT_COLOR_SET: 814 case DT_COLOR_SET:
815 switch (op->size) 815 switch (op->size)
819 case DC_D021: img->bgcolor = op->offs; break; 819 case DC_D021: img->bgcolor = op->offs; break;
820 case DC_D022: img->d022 = op->offs; break; 820 case DC_D022: img->d022 = op->offs; break;
821 case DC_D023: img->d023 = op->offs; break; 821 case DC_D023: img->d023 = op->offs; break;
822 case DC_D024: img->d024 = op->offs; break; 822 case DC_D024: img->d024 = op->offs; break;
823 default: 823 default:
824 dmError("Unhandled DT_COLOR_SET mode %d in ", 824 return dmError(DMERR_INTERNAL,
825 "op #%d, offs=%d ($%04x), bank=%d, size=%d ($%04x) @ %d ($%04x)\n", 825 "Unhandled DT_COLOR_SET mode %d in ",
826 op->size, i, op->offs, op->offs, op->bank, size, size, len, len); 826 "op #%d, offs=%d ($%04x), bank=%d, size=%d ($%04x) @ %d ($%04x)\n",
827 return DMERR_INTERNAL; 827 op->size, i, op->offs, op->offs, op->bank, size, size, len, len);
828 } 828 }
829 break; 829 break;
830 830
831 case DT_CHAR_CONFIG: 831 case DT_CHAR_CONFIG:
832 switch (op->offs) 832 switch (op->offs)
842 img->screen[bank][offs] = offs & 0xff; 842 img->screen[bank][offs] = offs & 0xff;
843 } 843 }
844 break; 844 break;
845 845
846 default: 846 default:
847 dmError("Unhandled DT_CHAR_CONFIG mode %d in ", 847 return dmError(DMERR_INTERNAL,
848 "op #%d, bank=%d, size=%d ($%04x) @ %d ($%04x)\n", 848 "Unhandled DT_CHAR_CONFIG mode %d in ",
849 op->offs, i, op->bank, size, size, len, len); 849 "op #%d, bank=%d, size=%d ($%04x) @ %d ($%04x)\n",
850 return DMERR_INTERNAL; 850 op->offs, i, op->bank, size, size, len, len);
851 } 851 }
852 break; 852 break;
853 853
854 case DT_DEC_FUNCTION: 854 case DT_DEC_FUNCTION:
855 if (op->decfunction == NULL) 855 if (op->decfunction == NULL)
856 { 856 {
857 dmError("Decode op is a function, but function ptr is NULL: " 857 return dmError(DMERR_INTERNAL,
858 "Decode op is a function, but function ptr is NULL: "
858 "op #%d, offs=%d ($%04x), bank=%d, size=%d ($%04x) @ %d ($%04x)\n", 859 "op #%d, offs=%d ($%04x), bank=%d, size=%d ($%04x) @ %d ($%04x)\n",
859 i, op->offs, op->offs, op->bank, size, size, len, len); 860 i, op->offs, op->offs, op->bank, size, size, len, len);
860 return DMERR_INTERNAL;
861 } 861 }
862 if (!op->decfunction(img, op, buf, len)) 862 if (!op->decfunction(img, op, buf, len))
863 { 863 {
864 dmError("Decode op custom function failed: op #%d, " 864 return dmError(DMERR_INTERNAL,
865 "Decode op custom function failed: op #%d, "
865 "offs=%d ($%04x), bank=%d, size=%d ($%04x) @ %d ($%04x)\n", 866 "offs=%d ($%04x), bank=%d, size=%d ($%04x) @ %d ($%04x)\n",
866 i, op->offs, op->offs, op->bank, size, size, len, len); 867 i, op->offs, op->offs, op->bank, size, size, len, len);
867 return DMERR_INTERNAL;
868 } 868 }
869 break; 869 break;
870 } 870 }
871 } 871 }
872 872
883 if (pbuf == NULL || plen == NULL || img == NULL || fmt == NULL) 883 if (pbuf == NULL || plen == NULL || img == NULL || fmt == NULL)
884 return DMERR_NULLPTR; 884 return DMERR_NULLPTR;
885 885
886 if (fmt->nencdecOps < 0 || fmt->nencdecOps >= D64_MAX_ENCDEC_OPS) 886 if (fmt->nencdecOps < 0 || fmt->nencdecOps >= D64_MAX_ENCDEC_OPS)
887 { 887 {
888 dmError("Invalid number of enc/dec ops in format. Internal error.\n"); 888 return dmError(DMERR_INTERNAL,
889 return DMERR_INTERNAL; 889 "Invalid number of enc/dec ops in format. Internal error.\n");
890 } 890 }
891 891
892 // Allocate the output buffer 892 // Allocate the output buffer
893 *plen = 0; 893 *plen = 0;
894 if (fmt->size > 0) 894 if (fmt->size > 0)
896 else 896 else
897 allocated = 16 * 1024; 897 allocated = 16 * 1024;
898 898
899 if ((buf = dmMalloc(allocated)) == NULL) 899 if ((buf = dmMalloc(allocated)) == NULL)
900 { 900 {
901 dmError("Could not allocate %d bytes of memory for C64 image encoding buffer.\n", 901 return dmError(DMERR_MALLOC,
902 "Could not allocate %d bytes of memory for C64 image encoding buffer.\n",
902 allocated); 903 allocated);
903 res = DMERR_MALLOC;
904 goto error; 904 goto error;
905 } 905 }
906 906
907 // Perform encoding 907 // Perform encoding
908 for (i = 0; i < fmt->nencdecOps; i++) 908 for (i = 0; i < fmt->nencdecOps; i++)
916 goto error; 916 goto error;
917 917
918 // Check size 918 // Check size
919 if (!dmC64GetOpSize(op, fmt, &size)) 919 if (!dmC64GetOpSize(op, fmt, &size))
920 { 920 {
921 dmError("Decode op SIZE out of bounds, op #%d type=%d, offs=%d ($%04x), " 921 res = dmError(DMERR_INVALID_DATA,
922 "Decode op SIZE out of bounds, op #%d type=%d, offs=%d ($%04x), "
922 "bank=%d, size=%d ($%04x) vs. allocated %d ($%04x)\n", 923 "bank=%d, size=%d ($%04x) vs. allocated %d ($%04x)\n",
923 i, op->type, op->offs, op->offs, op->bank, size, size, op->size, op->size); 924 i, op->type, op->offs, op->offs, op->bank, size, size, op->size, op->size);
924 return DMERR_INVALID_DATA; 925 goto error;
925 } 926 }
926 927
927 // Do we need to reallocate some more space? 928 // Do we need to reallocate some more space?
928 if (2 + op->offs + size > allocated) 929 if (2 + op->offs + size > allocated)
929 { 930 {
932 933
933 allocated += grow; 934 allocated += grow;
934 935
935 if ((buf = dmRealloc(buf, allocated)) == NULL) 936 if ((buf = dmRealloc(buf, allocated)) == NULL)
936 { 937 {
937 dmError("Could not re-allocate %d bytes of memory for C64 image encoding buffer.\n", 938 res = dmError(DMERR_MALLOC,
939 "Could not re-allocate %d bytes of memory for C64 image encoding buffer.\n",
938 allocated); 940 allocated);
939 res = DMERR_MALLOC;
940 goto error; 941 goto error;
941 } 942 }
942 } 943 }
943 944
944 if (fmt->size == 0 && op->offs + size + 2 > *plen) 945 if (fmt->size == 0 && op->offs + size + 2 > *plen)
961 case DC_D021: *dst = img->bgcolor; break; 962 case DC_D021: *dst = img->bgcolor; break;
962 case DC_D022: *dst = img->d022; break; 963 case DC_D022: *dst = img->d022; break;
963 case DC_D023: *dst = img->d023; break; 964 case DC_D023: *dst = img->d023; break;
964 case DC_D024: *dst = img->d024; break; 965 case DC_D024: *dst = img->d024; break;
965 default: 966 default:
966 dmError("Unhandled DT_COLOR_REG mode %d in ", 967 res = dmError(DMERR_INTERNAL,
967 "op #%d, offs=%d ($%04x), bank=%d, size=%d ($%04x) @ %d ($%04x)\n", 968 "Unhandled DT_COLOR_REG mode %d in ",
968 op->size, i, op->offs, op->offs, op->bank, size, size, *plen, *plen); 969 "op #%d, offs=%d ($%04x), bank=%d, size=%d ($%04x) @ %d ($%04x)\n",
969 return DMERR_INTERNAL; 970 op->size, i, op->offs, op->offs, op->bank, size, size, *plen, *plen);
971 goto error;
970 } 972 }
971 break; 973 break;
972 974
973 case DT_ENC_FUNCTION: 975 case DT_ENC_FUNCTION:
974 if (op->encfunction == NULL) 976 if (op->encfunction == NULL)
975 { 977 {
976 dmError("Encode op is a function, but function ptr is NULL: " 978 res = dmError(DMERR_INTERNAL,
979 "Encode op is a function, but function ptr is NULL: "
977 "op #%d, offs=%d ($%04x), bank=%d, size=%d ($%04x) @ %d ($%04x)\n", 980 "op #%d, offs=%d ($%04x), bank=%d, size=%d ($%04x) @ %d ($%04x)\n",
978 i, op->offs, op->offs, op->bank, size, size, *plen, *plen); 981 i, op->offs, op->offs, op->bank, size, size, *plen, *plen);
979 return DMERR_INTERNAL; 982 goto error;
980 } 983 }
981 /* 984 /*
982 if (!op->encfunction(op, buf, len)) 985 if (!op->encfunction(op, buf, len))
983 { 986 {
984 dmError("Encode op custom function failed: op #%d, " 987 res = dmError(DMERR_INTERNAL,
988 "Encode op custom function failed: op #%d, "
985 "offs=%d ($%04x), bank=%d, size=%d ($%04x) @ %d ($%04x)\n", 989 "offs=%d ($%04x), bank=%d, size=%d ($%04x) @ %d ($%04x)\n",
986 i, op->offs, op->offs, op->bank, size, size, len, len); 990 i, op->offs, op->offs, op->bank, size, size, len, len);
987 return DMERR_INTERNAL; 991 goto out;
988 } 992 }
989 */ 993 */
990 break; 994 break;
991 } 995 }
992 } 996 }