• C macros for reading PostScript binary object format

    From news@zzo38computer.org.invalid@21:1/5 to All on Mon Jun 8 12:03:12 2020
    As part of TeXnicard, I wrote a set of C macros to read the PostScript
    binary object format. I am also including a copy of them here. TeXnicard
    is in the public domain, so you can use this for whatever you want to do.

    #define TY_NULL 0
    #define TY_INT 1
    #define TY_REAL 2
    #define TY_NAME 3
    #define TY_BOOL 4
    #define TY_STRING 5
    #define TY_ARRAY 9
    #define TY_MARK 10

    #define obj_float(x) ({ int x__=(x); int t_=obj_type(x__); t_==TY_INT?(float)obj_rawvalue(x__):t_==TY_REAL?obj_ufloat(x__):0; })
    #define obj_index(x,y) (obj_rawvalue(x)+(y)*8)
    #define obj_int(x) ({ int x__=(x); int t_=obj_type(x__); t_==TY_INT||t_==TY_BOOL?obj_rawvalue(x__):t_==TY_REAL?(int)obj_ufloat(x__):0; })
    #define obj_int64(x) ({ int x__=(x); int t_=obj_type(x__); t_==TY_INT||t_==TY_BOOL?obj_rawvalue(x__):t_==TY_REAL?(sqlite3_int64)obj_ufloat(x__):0; })
    #define obj_isnum(x) ({ int x__=(x); obj_type(x__)==TY_INT || obj_type(x__)==TY_REAL; })
    #define obj_length(x) ({ int x__=(x); (object[x__+2]<<8)|object[x__+3]; }) #define obj_ptr(x) (object+obj_rawvalue(x))
    #define obj_rawvalue(x) ({ int y__=(x); (int)((object[y__+4]<<24)|(object[y__+5]<<16)|(object[y__+6]<<8)|object[y__+7]); })
    #define obj_tag() (object[1])
    #define obj_type(x) (object[x]&127)
    #define obj_ufloat(x) ({ union { int i; float f; } f__; f__.i=obj_rawvalue(x); f__.f; }) // see psi/ibnum.h in Ghostscript for an explanation of this bug

    You can read the header of the PostScript binary object data to find the
    total size of the data; the pointer "object" is of type "unsigned char*"
    and points to just after the header.

    Note that the above uses "sqlite3_int64" as the 64-bit integer type. If
    your program does not use SQLite, then you must change this.

    --
    This signature intentionally left blank.
    (But if it has these words, then actually it isn't blank, isn't it?)

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From luser droog@21:1/5 to ne...@zzo38computer.org.invalid on Tue Jun 9 11:42:01 2020
    On Monday, June 8, 2020 at 2:03:16 PM UTC-5, ne...@zzo38computer.org.invalid wrote:
    As part of TeXnicard, I wrote a set of C macros to read the PostScript
    binary object format. I am also including a copy of them here. TeXnicard
    is in the public domain, so you can use this for whatever you want to do.


    I rewrote these macros as K&R style functions. Modest increase in size but
    I find it easier to read this way. I think I got all the types in that
    look like they're not int.

    #include <stdint.h>

    enum { TY_NULL, TY_INT, TY_REAL, TY_NAME, TY_BOOL, TY_STRING, TY_ARRAY = 9, TY_MARK };

    float obj_float(object,x)unsigned char *object;{
    int t = obj_type(object,x);
    return t==TY_INT ? (float)x
    : t==TY_REAL ? obj_ufloat(object,x)
    : 0;
    }

    obj_index(object,x,y)unsigned char *object;{
    return obj_rawvalue(object,x) + y*8;
    }

    obj_int(object,x)unsigned char *object;{
    int t = obj_type(x);
    return t==TY_INT ||
    t==TY_BOOL ? obj_rawvalue(object,x)
    : t==TY_REAL ? (int)obj_ufloat(x)
    : 0;
    }

    int_64_t obj_int64(object,x)unsigned char *object;{
    int t = obj_type(x);
    return t==TY_INT ||
    t==TY_BOOL ? obj_rawvalue(object,x)
    : t==TY_REAL ? (int64_t)obj_ufloat(x)
    : 0;
    }

    obj_isnum(object,x)unsigned char *object;{
    return obj_type(x)==TY_INT || obj_type(x)==TY_REAL;
    }

    obj_length(object,x)unsigned char *object;{
    return object[x+2]<<8 | object[x+3];
    }

    obj_ptr(object,x)unsigned char *object;{
    return object + obj_rawvalue(x);
    }

    obj_rawvalue(object,x)unsigned char *object;{
    return object[x+4]<<24 | object[x+5]<<16 | object[x+6]<<8 | object[x+7];
    }

    obj_tag(object)unsigned char *object;{
    return object[1];
    }

    obj_type(object,x)unsigned char *object;{
    return object[x]&127;
    }

    float obj_ufloat(object,x)unsigned char *object;{
    union{ int i; float f; } f;
    f.i = obj_rawvalue(object,x);
    return f.f;
    }

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)