/*
 * UPIWIN - Micro Pi Windowing Framework Kernel
 * Copyright (C) 2019 Amy Bowersox/Erbosoft Metaverse Design Solutions
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License along
 * with this program; if not, write to the Free Software Foundation, Inc.,
 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
 *-------------------------------------------------------------------------
 */
#ifndef __SCODE_H_INCLUDED
#define __SCODE_H_INCLUDED

#include "wintype.h"

/*-------------------------------------------------------------------
 * Status codes are defined as follows:
 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
 * |S|        Facility             |            Code               |
 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
 *  3 3 2 2 2 2 2 2 2 2 2 2 1 1 1 1 1 1 1 1 1 1 9 8 7 6 5 4 3 2 1 0
 *  1 0 9 8 7 6 5 4 3 2 1 0 9 8 7 6 5 4 3 2 1 0
 *
 * S = Severity: 0 = success, 1 = error
 * Facility = Facility code
 * Code = Specific error code
 *-------------------------------------------------------------------
 */

/* Severity codes */
#define SEVERITY_SUCCESS 0x00000000
#define SEVERITY_ERROR   0x80000000

/* Facility codes - compatible with M$ */
#define FACILITY_NULL       0
#define FACILITY_RPC        1
#define FACILITY_STORAGE    3
#define FACILITY_ITF        4
#define FACILITY_UNIX       5
#define FACILITY_UPIWIN     6
#define FACILITY_ZIP        78

#define SUCCEEDED(s)  (((s) & SEVERITY_ERROR) == 0)
#define FAILED(s)     (((s) & SEVERITY_ERROR) != 0)

#define SCODE_FACILITY(s)  (((s) >> 16) & 0x7FFF)
#define SCODE_CODE(s)      ((s) & 0xFFFF)

#define MAKE_SCODE(sev, fac, err) ((SCODE)((sev) | (((fac) & 0x7FFF) << 16) | ((err) & 0xFFFF)))

#define SCODE_FROM_ERRNO(x)    MAKE_SCODE(SEVERITY_ERROR, FACILITY_UNIX, (x))
#define ERRNO_AS_SCODE         SCODE_FROM_ERRNO(errno)

#define SCODE_CAST(x) ((SCODE)(x))

/* Basic success codes */
#define S_OK                         SCODE_CAST(0x00000000)    /* OK return */
#define S_FALSE                      SCODE_CAST(0x00000001)    /* "False" return */

/* Basic error codes */
#define E_NOTIMPL                    SCODE_CAST(0x80000001)    /* not implemented */
#define E_OUTOFMEMORY                SCODE_CAST(0x80000002)    /* out of memory */
#define E_INVALIDARG                 SCODE_CAST(0x80000003)    /* invalid argument */
#define E_NOINTERFACE                SCODE_CAST(0x80000004)    /* no such interface */
#define E_POINTER                    SCODE_CAST(0x80000005)    /* invalid pointer */
#define E_HANDLE                     SCODE_CAST(0x80000006)    /* invalid handle */
#define E_ABORT                      SCODE_CAST(0x80000007)    /* aborted operation */
#define E_FAIL                       SCODE_CAST(0x80000008)    /* unspecified failure */
#define E_ACCESSDENIED               SCODE_CAST(0x80000009)    /* access denied */
#define E_PENDING                    SCODE_CAST(0x8000000A)    /* data not yet available */
#define E_BOUNDS                     SCODE_CAST(0x8000000B)    /* access outside valid range */
#define E_CHANGED_STATE              SCODE_CAST(0x8000000C)    /* object changed state, result now invalid */
#define E_ILLEGAL_STATE_CHANGE       SCODE_CAST(0x8000000D)    /* illegal state change */
#define E_ILLEGAL_METHOD_CALL        SCODE_CAST(0x8000000E)    /* illegal method call */
#define E_UNEXPECTED                 SCODE_CAST(0x8000FFFF)    /* unexpected error */

/* Storage error codes */
#define STG_E_INVALIDFUNCTION        SCODE_CAST(0x80030001)    /* invalid function */
#define STG_E_FILENOTFOUND           SCODE_CAST(0x80030002)    /* file not found */
#define STG_E_PATHNOTFOUND           SCODE_CAST(0x80030003)    /* path not found */
#define STG_E_TOOMANYOPENFILES       SCODE_CAST(0x80030004)    /* too many open files */
#define STG_E_ACCESSDENIED           SCODE_CAST(0x80030005)    /* access denied */
#define STG_E_INVALIDHANDLE          SCODE_CAST(0x80030006)    /* invalid handle */
#define STG_E_INSUFFICIENTMEMORY     SCODE_CAST(0x80030008)    /* insufficient memory */
#define STG_E_INVALIDPOINTER         SCODE_CAST(0x80030009)    /* invalid pointer */
#define STG_E_NOMOREFILES            SCODE_CAST(0x80030012)    /* no more files to return */
#define STG_E_DISKISWRITEPROTECTED   SCODE_CAST(0x80030013)    /* disk is write protected */
#define STG_E_SEEKERROR              SCODE_CAST(0x80030019)    /* error in seek operation */
#define STG_E_WRITEFAULT             SCODE_CAST(0x8003001D)    /* error in write operation */
#define STG_E_READFAULT              SCODE_CAST(0x8003001E)    /* error in read operation */
#define STG_E_SHAREVIOLATION         SCODE_CAST(0x80030020)    /* sharing violation */
#define STG_E_LOCKVIOLATION          SCODE_CAST(0x80030021)    /* lock violation */
#define STG_E_INVALIDPARAMETER       SCODE_CAST(0x80030057)    /* invalid parameter */
#define STG_E_MEDIUMFULL             SCODE_CAST(0x80030070)    /* insufficient disk space */
#define STG_E_UNKNOWN                SCODE_CAST(0x800300FD)    /* unexpected error */

/* UPIWIN-specific errorcodes */
#define UPIWIN_E_INVALIDSCRIPT       SCODE_CAST(0x80060000)    /* invalid script file */
#define UPIWIN_E_NOSCRIPT            SCODE_CAST(0x80060001)    /* no script specified */

/* libzip error codes */
#define ZIP_E_MULTIDISK              MAKE_SCODE(SEVERITY_ERROR, FACILITY_ZIP, 1)  /* multidisk not supported */
#define ZIP_E_RENAME                 MAKE_SCODE(SEVERITY_ERROR, FACILITY_ZIP, 2)  /* rename temp file failed */
#define ZIP_E_CLOSE                  MAKE_SCODE(SEVERITY_ERROR, FACILITY_ZIP, 3)  /* close failed */
#define ZIP_E_CRC                    MAKE_SCODE(SEVERITY_ERROR, FACILITY_ZIP, 7)  /* CRC error */
#define ZIP_E_WASCLOSED              MAKE_SCODE(SEVERITY_ERROR, FACILITY_ZIP, 8)  /* zip file was closed */
#define ZIP_E_EXISTS                 MAKE_SCODE(SEVERITY_ERROR, FACILITY_ZIP, 10)  /* file already exists */
#define ZIP_E_OPEN                   MAKE_SCODE(SEVERITY_ERROR, FACILITY_ZIP, 11)  /* unable to open */
#define ZIP_E_TMPOPEN                MAKE_SCODE(SEVERITY_ERROR, FACILITY_ZIP, 12)  /* unable to open temp file */
#define ZIP_E_ZLIB                   MAKE_SCODE(SEVERITY_ERROR, FACILITY_ZIP, 13)  /* Zlib error */
#define ZIP_E_CHANGED                MAKE_SCODE(SEVERITY_ERROR, FACILITY_ZIP, 15)  /* entry was changed */
#define ZIP_E_NOCMP                  MAKE_SCODE(SEVERITY_ERROR, FACILITY_ZIP, 16)  /* compression method unsupported */
#define ZIP_E_EOF                    MAKE_SCODE(SEVERITY_ERROR, FACILITY_ZIP, 17)  /* hit end of file */
#define ZIP_E_NOTZIP                 MAKE_SCODE(SEVERITY_ERROR, FACILITY_ZIP, 19)  /* not a ZIP file */
#define ZIP_E_INCONSISTENT           MAKE_SCODE(SEVERITY_ERROR, FACILITY_ZIP, 21)  /* inconsistent archive */
#define ZIP_E_REMOVE                 MAKE_SCODE(SEVERITY_ERROR, FACILITY_ZIP, 22)  /* remove failed */
#define ZIP_E_DELETED                MAKE_SCODE(SEVERITY_ERROR, FACILITY_ZIP, 23)  /* entry deleted */
#define ZIP_E_NOCRYPT                MAKE_SCODE(SEVERITY_ERROR, FACILITY_ZIP, 24)  /* encryption not supported */
#define ZIP_E_RDONLY                 MAKE_SCODE(SEVERITY_ERROR, FACILITY_ZIP, 25)  /* read-only archive */
#define ZIP_E_NOPASSWD               MAKE_SCODE(SEVERITY_ERROR, FACILITY_ZIP, 26)  /* no password */
#define ZIP_E_BADPASSWD              MAKE_SCODE(SEVERITY_ERROR, FACILITY_ZIP, 27)  /* wrong password */
#define ZIP_E_NOTSUPPORTED           MAKE_SCODE(SEVERITY_ERROR, FACILITY_ZIP, 28)  /* operation not supported */
#define ZIP_E_BUSY                   MAKE_SCODE(SEVERITY_ERROR, FACILITY_ZIP, 29)  /* still in use */
#define ZIP_E_TELL                   MAKE_SCODE(SEVERITY_ERROR, FACILITY_ZIP, 30)  /* tell failed */
#define ZIP_E_CMPDATA                MAKE_SCODE(SEVERITY_ERROR, FACILITY_ZIP, 31)  /* compressed data invalid */
#define ZIP_E_CANCELLED              MAKE_SCODE(SEVERITY_ERROR, FACILITY_ZIP, 32)  /* operation canceled */

#endif /* __SCODE_H_INCLUDED */
