merge upstream changes v3.0

Mon, 24 Apr 2023 21:01:41 +0200

author
Mike Becker <universe@uap-core.de>
date
Mon, 24 Apr 2023 21:01:41 +0200
changeset 67
5da2cb5aea6b
parent 66
1b12cf799fee (diff)
parent 65
7dd4fd1e7071 (current diff)
child 68
4d1ce98e09cc

merge upstream changes

.hgignore file | annotate | diff | comparison | revisions
Makefile file | annotate | diff | comparison | revisions
src/c2html.c file | annotate | diff | comparison | revisions
src/frontend.c file | annotate | diff | comparison | revisions
src/highlighter.c file | annotate | diff | comparison | revisions
src/highlighter.h file | annotate | diff | comparison | revisions
test/ctest.c file | annotate | diff | comparison | revisions
test/ctestfile.c file | annotate | diff | comparison | revisions
test/empty.c file | annotate | diff | comparison | revisions
test/golden-master/bigtest.html file | annotate | diff | comparison | revisions
test/golden-master/ctest.html file | annotate | diff | comparison | revisions
test/golden-master/empty.html file | annotate | diff | comparison | revisions
test/golden-master/javatest.html file | annotate | diff | comparison | revisions
test/golden-master/plain.html file | annotate | diff | comparison | revisions
     1.1 --- a/Makefile	Mon Oct 03 12:56:28 2022 +0200
     1.2 +++ b/Makefile	Mon Apr 24 21:01:41 2023 +0200
     1.3 @@ -44,39 +44,23 @@
     1.4  	
     1.5  build/%$(OBJ_EXT): src/%.c
     1.6  	$(CC) -o $@ $(CFLAGS) -c $<
     1.7 -	
     1.8 +
     1.9  build:
    1.10  	$(MKDIR) $@
    1.11 -
    1.12 -test-c: all
    1.13 -	for f in ctest bigtest empty ; do \
    1.14 -  		echo "test/$$f.c" ; \
    1.15 -  		./build/$(BIN) -o "build/$$f.html" \
    1.16 -        	-H test/header.html \
    1.17 -        	-F test/footer.html \
    1.18 -        	"test/$$f.c" && \
    1.19 -        diff "build/$$f.html" "test/golden-master/$$f.html" ; \
    1.20 -    done
    1.21 -
    1.22 -test-java: all
    1.23 -	for f in javatest ; do \
    1.24 -  		./build/$(BIN) -j -o "build/$$f.html" \
    1.25 -        	-H test/jheader.html \
    1.26 -        	-F test/footer.html \
    1.27 -        	"test/$$f.java" && \
    1.28 -        diff "build/$$f.html" "test/golden-master/$$f.html" ; \
    1.29 -    done
    1.30 -
    1.31 -test-plain: all
    1.32 -	for f in plain ; do \
    1.33 -  		./build/$(BIN) -p -o "build/$$f.html" \
    1.34 -        	-H test/header.html \
    1.35 -        	-F test/footer.html \
    1.36 -        	"test/$$f.txt" && \
    1.37 -        diff "build/$$f.html" "test/golden-master/$$f.html" ; \
    1.38 -    done
    1.39 -
    1.40 -test: test-c test-java test-plain
    1.41 +	
    1.42 +test: all
    1.43 +	./build/$(BIN) test/ctest.c -o build/ctest.html \
    1.44 +	-H test/header.html -F test/footer.html
    1.45 +	./build/$(BIN) -j test/javatest.java -o build/javatest.html \
    1.46 +	-H test/jheader.html -F test/footer.html
    1.47 +	./build/$(BIN) test/bigtest.c -o build/bigtest.html \
    1.48 +	-H test/header.html -F test/footer.html
    1.49 +	./build/$(BIN) -p test/plain.txt -o build/plain.html \
    1.50 +	-H test/header.html -F test/footer.html
    1.51 +	diff build/ctest.html test/gs/ctest.html && \
    1.52 +	diff build/javatest.html test/gs/javatest.html && \
    1.53 +	diff build/bigtest.html test/gs/bigtest.html && \
    1.54 +	diff build/plain.html test/gs/plain.html
    1.55  	@echo "Tests successful."
    1.56  	
    1.57  clean:
     2.1 --- a/src/c2html.c	Mon Oct 03 12:56:28 2022 +0200
     2.2 +++ b/src/c2html.c	Mon Apr 24 21:01:41 2023 +0200
     2.3 @@ -29,131 +29,93 @@
     2.4  
     2.5  #include "c2html.h"
     2.6  
     2.7 -#include <ucx/list.h>
     2.8 -#include <ucx/utils.h>
     2.9 +#include <cx/array_list.h>
    2.10 +#include <cx/printf.h>
    2.11  
    2.12 -#define try_write(wfnc, str, n, buf, written, maxlen) \
    2.13 -    {                                              \
    2.14 -        size_t m = maxlen-written;                    \
    2.15 -        written += wfnc(str, 1, n > m ? m : n, buf);  \
    2.16 +size_t c2html_format(
    2.17 +        CxList const *lines,
    2.18 +        void *outbuf,
    2.19 +        cx_write_func wfnc,
    2.20 +        c2html_highlighter_func highlighter,
    2.21 +        int showln
    2.22 +) {
    2.23 +    /* total written bytes */
    2.24 +    size_t written = 0;
    2.25 +
    2.26 +    /* compute width of line numbering */
    2.27 +    int lnw = 0;
    2.28 +    if (showln) {
    2.29 +        size_t no_lines = cxListSize(lines);
    2.30 +        for (size_t p = 1; p < no_lines; p *= 10) lnw++;
    2.31      }
    2.32  
    2.33 -static size_t formatlines(c2html_highlighter_func highlighter, UcxList *in,
    2.34 -        void *outbuf, write_func wfnc, size_t maxlen, int showlineno) {
    2.35 -    /* total written bytes */
    2.36 -    size_t written = 0;
    2.37 -    
    2.38 -    /* compute width of line numbering */
    2.39 -    int lnw = 0;
    2.40 -    if (showlineno) {
    2.41 -        size_t lines = ucx_list_size(in);
    2.42 -        for (size_t p = 1; p < lines ; p*=10) lnw++;
    2.43 -    }
    2.44 -    
    2.45 -    /* start monospace formatting */
    2.46 -    try_write(wfnc, "<pre>\n", 6, outbuf, written, maxlen);
    2.47 +    /* start code formatting */
    2.48 +    written += wfnc("<div class=\"c2html-code\">\n", 1, 26, outbuf);
    2.49  
    2.50      /* process lines */
    2.51 -    size_t lineno = 0;
    2.52 -    c2html_highlighter_data* hd = malloc(sizeof(c2html_highlighter_data));
    2.53 -    hd->multiline_comment = 0;
    2.54 -    hd->primary_buffer = ucx_buffer_new(NULL, 256, UCX_BUFFER_AUTOEXTEND);
    2.55 -    hd->secondary_buffer = ucx_buffer_new(NULL, 32, UCX_BUFFER_AUTOEXTEND);
    2.56 -    UcxBuffer *line = ucx_buffer_new(NULL, 1024, UCX_BUFFER_AUTOEXTEND);
    2.57 -    
    2.58 -    UCX_FOREACH(sourceline, in) {
    2.59 +    int lineno = 0;
    2.60 +    c2html_highlighter_data hd;
    2.61 +    hd.multiline_comment = 0;
    2.62 +    cxBufferInit(&hd.primary_buffer, NULL, 256, NULL, CX_BUFFER_AUTO_EXTEND);
    2.63 +    cxBufferInit(&hd.secondary_buffer, NULL, 32, NULL, CX_BUFFER_AUTO_EXTEND);
    2.64 +    CxBuffer out_line;
    2.65 +    cxBufferInit(&out_line, NULL, 128, NULL, CX_BUFFER_AUTO_EXTEND);
    2.66 +
    2.67 +    CxIterator in_lines = cxListIterator(lines);
    2.68 +    cx_foreach(char*, in_line, in_lines) {
    2.69          /* increase line number and clean line buffer */
    2.70          lineno++;
    2.71 -        ucx_buffer_clear(line);
    2.72 -        
    2.73 +        cxBufferClear(&out_line);
    2.74 +
    2.75          /* write line number */
    2.76 -        if (showlineno) {
    2.77 -            ucx_bprintf(line, "<a class=\"c2html-lineno\" name=\"l%d\" "
    2.78 -                    "href=\"#l%d\">%*d </a>",
    2.79 -                    lineno, lineno, lnw, lineno);
    2.80 +        if (showln) {
    2.81 +            cx_bprintf(&out_line, "<a class=\"c2html-lineno\" name=\"l%d\" "
    2.82 +                                  "href=\"#l%d\">%*d </a>",
    2.83 +                       lineno, lineno, lnw, lineno);
    2.84          }
    2.85 -        
    2.86 +
    2.87          /* process code line */
    2.88 -        highlighter(sourceline->data, line, hd);
    2.89 -        
    2.90 +        highlighter(in_line, &out_line, &hd);
    2.91 +
    2.92          /* write code line */
    2.93 -        try_write(wfnc, line->space, line->size, outbuf, written, maxlen);
    2.94 -        
    2.95 -        if (written == maxlen) break;
    2.96 +        written += wfnc(out_line.space, 1, out_line.size, outbuf);
    2.97      }
    2.98 -    
    2.99 -    /* end monospace formatting */
   2.100 -    try_write(wfnc, "</pre>\n", 7, outbuf, written, maxlen);
   2.101 -    
   2.102 +
   2.103 +    /* end code formatting */
   2.104 +    written += wfnc("</div>\n", 1, 7, outbuf);
   2.105 +
   2.106      /* cleanup and return */
   2.107 -    ucx_buffer_free(hd->primary_buffer);
   2.108 -    ucx_buffer_free(hd->secondary_buffer);
   2.109 -    free(hd);
   2.110 -    ucx_buffer_free(line);
   2.111 -    
   2.112 +    cxBufferDestroy(&hd.primary_buffer);
   2.113 +    cxBufferDestroy(&hd.secondary_buffer);
   2.114 +    cxBufferDestroy(&out_line);
   2.115 +
   2.116      return written;
   2.117  }
   2.118  
   2.119 -size_t c2html_formatn(void* inputbuffer, read_func rfnc,
   2.120 -        char* ibuf, size_t ibuflen, void* outputbuffer, write_func wfnc,
   2.121 -        size_t maxlen, c2html_highlighter_func hltr, int showln) {
   2.122 -    
   2.123 -    UcxBuffer *content = ucx_buffer_new(NULL, ibuflen*2, UCX_BUFFER_AUTOEXTEND);
   2.124 -    ucx_stream_bcopy(inputbuffer, content, rfnc, (write_func) ucx_buffer_write,
   2.125 -            ibuf, ibuflen);
   2.126 +size_t c2html_bformat(
   2.127 +        char const *inputbuffer,
   2.128 +        size_t inputbuflen,
   2.129 +        void *outbuf,
   2.130 +        cx_write_func wfnc,
   2.131 +        c2html_highlighter_func highlighter,
   2.132 +        int showln
   2.133 +) {
   2.134 +    /* a rough estimate for the number of lines */
   2.135 +    size_t est_cap = 16 + inputbuflen / 40;
   2.136  
   2.137 -    size_t n = c2html_bformatn(content->space, content->size,
   2.138 -            outputbuffer, wfnc, maxlen, hltr, showln);
   2.139 -    
   2.140 -    ucx_buffer_free(content);
   2.141 -    return n;
   2.142 -}
   2.143 -
   2.144 -size_t c2html_format(void* inputbuffer, read_func rfnc,
   2.145 -        char* ibuf, size_t ibuflen, void* outputbuffer, write_func wfnc,
   2.146 -        c2html_highlighter_func hltr, int showln) {
   2.147 -    return c2html_formatn(inputbuffer, rfnc, ibuf, ibuflen,
   2.148 -            outputbuffer, wfnc, (size_t)-1, hltr, showln);
   2.149 -}
   2.150 -
   2.151 -size_t c2html_fformat(FILE* inputfile, char *ibuf, size_t ibuflen,
   2.152 -        void* outputbuffer, write_func wfnc,
   2.153 -        c2html_highlighter_func hltr, int showln) {
   2.154 -    return c2html_format(inputfile, (read_func) fread, ibuf, ibuflen,
   2.155 -            outputbuffer, wfnc, hltr, showln);
   2.156 -}
   2.157 -
   2.158 -void c2html_fformatf(FILE *inputfile, char *ibuf, size_t ibuflen,
   2.159 -        FILE* outputfile, c2html_highlighter_func hltr, int showln) {
   2.160 -    c2html_format(inputfile, (read_func) fread, ibuf, ibuflen,
   2.161 -            outputfile, (write_func) fwrite, hltr, showln);
   2.162 -}
   2.163 -
   2.164 -size_t c2html_bformatn(const char* inputbuffer, size_t inputbuflen,
   2.165 -        void* outputbuffer, write_func wfnc,
   2.166 -        size_t maxlen, c2html_highlighter_func hltr, int showln) {
   2.167 -    UcxList *lines = ucx_list_append(NULL, (char*)inputbuffer);
   2.168 -    for (size_t i = 1 ; i < inputbuflen ; i++) {
   2.169 -        if (inputbuffer[i] == '\n' && i+1 < inputbuflen) {
   2.170 -            ucx_list_append(lines, (char*)inputbuffer+i+1);
   2.171 +    /* create the line pointer array */
   2.172 +    CxList *lines = cxArrayListCreateSimple(CX_STORE_POINTERS, est_cap);
   2.173 +    cxListAdd(lines, inputbuffer);
   2.174 +    for (size_t i = 1; i < inputbuflen; i++) {
   2.175 +        if (inputbuffer[i] == '\n' && i + 1 < inputbuflen) {
   2.176 +            cxListAdd(lines, inputbuffer + i + 1);
   2.177          }
   2.178      }
   2.179 -    
   2.180 -    size_t n = formatlines(hltr, lines, outputbuffer, wfnc, maxlen, showln);
   2.181 -    
   2.182 -    ucx_list_free(lines);
   2.183 +
   2.184 +    /* invoke the other function */
   2.185 +    size_t n = c2html_format(lines, outbuf, wfnc, highlighter, showln);
   2.186 +
   2.187 +    /* cleanup and return */
   2.188 +    cxListDestroy(lines);
   2.189      return n;
   2.190 -}
   2.191 -
   2.192 -size_t c2html_bformat(const char* inputbuffer, size_t inputbuflen,
   2.193 -        void* outputbuffer, write_func wfnc,
   2.194 -        c2html_highlighter_func hltr, int showln) {
   2.195 -    return c2html_bformatn(inputbuffer, inputbuflen, outputbuffer, wfnc,
   2.196 -            (size_t)-1, hltr, showln);
   2.197 -}
   2.198 -
   2.199 -void c2html_bformatf(const char* inputbuffer, size_t inputbuflen,
   2.200 -        FILE* outputfile, c2html_highlighter_func hltr, int showln) {
   2.201 -    c2html_bformatn(inputbuffer, inputbuflen, outputfile,
   2.202 -            (write_func) fwrite, (size_t)-1, hltr, showln);
   2.203 -}
   2.204 +}
   2.205 \ No newline at end of file
     3.1 --- a/src/c2html.h	Mon Oct 03 12:56:28 2022 +0200
     3.2 +++ b/src/c2html.h	Mon Apr 24 21:01:41 2023 +0200
     3.3 @@ -31,61 +31,23 @@
     3.4  #define C2HTML_H
     3.5  
     3.6  #include <stdio.h>
     3.7 +#include <cx/list.h>
     3.8  #include "highlighter.h"
     3.9  
    3.10  #ifdef __cplusplus
    3.11  extern "C" {
    3.12  #endif
    3.13      
    3.14 -#define VERSION_MAJOR   2
    3.15 +#define VERSION_MAJOR   3
    3.16  #define VERSION_MINOR   0
    3.17  #define VERSION_DEVELOP 0 /* set this to zero for release version */
    3.18  
    3.19  /**
    3.20 - * Reads a source file from the input buffer and writes at most
    3.21 - * <code>maxlen</code> bytes of the formatted output to the output buffer.
    3.22 + * Writes the formatted source data to the output buffer.
    3.23   * 
    3.24 - * The output buffer must either be large enough to hold <code>maxlen</code>
    3.25 - * bytes or the write function must trigger an automatic extension of the
    3.26 - * buffer.
    3.27 - * 
    3.28 - * The input is copied via an intermediate buffer to an internal buffer.
    3.29 - * 
    3.30 - * @param inputbuffer the input buffer
    3.31 - * @param rfnc a read function operating for the input buffer
    3.32 - * @param ibuf intermediate processing buffer
    3.33 - * @param ibuflen length of intermediate processing buffer
    3.34 - * @param outputbuffer the output buffer
    3.35 - * @param wfnc a write function for the output buffer
    3.36 - * @param maxlen the maximum amount bytes which will be written to the
    3.37 - * output buffer
    3.38 - * @param hltr the highlighter function
    3.39 - * @param showln zero, if line numbers shall be omitted, nonzero otherwise
    3.40 - * 
    3.41 - * @return total amount of bytes written to the output buffer
    3.42 - * 
    3.43 - * @see c2html_plain_highlighter()
    3.44 - * @see c2html_c_highlighter()
    3.45 - * @see c2html_java_highlighter()
    3.46 - */
    3.47 -size_t c2html_formatn(void* inputbuffer, read_func rfnc,
    3.48 -        char* ibuf, size_t ibuflen, void* outputbuffer, write_func wfnc,
    3.49 -        size_t maxlen, c2html_highlighter_func hltr, int showln);
    3.50 -    
    3.51 -/**
    3.52 - * Reads a source file from the input buffer and writes the formatted output
    3.53 - * to an output buffer.
    3.54 - * 
    3.55 - * The output buffer must either be large enough to hold the formatted data
    3.56 - * or the write function must trigger an automatic extension of the buffer.
    3.57 - * 
    3.58 - * The input is copied via an intermediate buffer to an internal buffer.
    3.59 - * 
    3.60 - * @param inputbuffer the input buffer
    3.61 - * @param rfnc a read function operating for the input buffer
    3.62 - * @param ibuf intermediate processing buffer
    3.63 - * @param ibuflen length of intermediate processing buffer
    3.64 - * @param outputbuffer the output buffer
    3.65 + * @param inputbuffer the source file data as string
    3.66 + * @param inputbuflen the length of the source file
    3.67 + * @param outbuf the output buffer
    3.68   * @param wfnc a write function for the output buffer
    3.69   * @param hltr the highlighter function
    3.70   * @param showln zero, if line numbers shall be omitted, nonzero otherwise
    3.71 @@ -96,121 +58,32 @@
    3.72   * @see c2html_c_highlighter()
    3.73   * @see c2html_java_highlighter()
    3.74   */
    3.75 -size_t c2html_format(void* inputbuffer, read_func rfnc,
    3.76 -        char* ibuf, size_t ibuflen, void* outputbuffer, write_func wfnc,
    3.77 -        c2html_highlighter_func hltr, int showln);
    3.78 +size_t c2html_bformat(char const* inputbuffer, size_t inputbuflen,
    3.79 +                      void* outbuf, cx_write_func wfnc,
    3.80 +                      c2html_highlighter_func hltr, int showln);
    3.81  
    3.82  /**
    3.83 - * Reads a source file from the specified input file stream and writes the
    3.84 - * formatted output to an output buffer.
    3.85 - * 
    3.86 - * The output buffer must either be large enough to hold the formatted data
    3.87 - * or the write function must trigger an automatic extension of the buffer.
    3.88 - * 
    3.89 - * The input is copied via an intermediate buffer to an internal buffer.
    3.90 - * For files, the recommended intermediate buffer length is the file system
    3.91 - * block size.
    3.92 - * 
    3.93 - * @param inputfile the input file stream
    3.94 - * @param ibuf intermediate processing buffer
    3.95 - * @param ibuflen length of intermediate processing buffer
    3.96 - * @param outputbuffer the output buffer
    3.97 + * Writes the formatted source data to the output buffer.
    3.98 + *
    3.99 + * This function takes a list of \c char* that point to the beginning of each
   3.100 + * line. These pointers may point directly into the source text and the strings
   3.101 + * do not need to be zero-terminated, but the line-breaks must be included.
   3.102 + *
   3.103 + * @param lines a list of pointers to the beginning of each line
   3.104 + * @param outbuf the output buffer
   3.105   * @param wfnc a write function for the output buffer
   3.106   * @param hltr the highlighter function
   3.107   * @param showln zero, if line numbers shall be omitted, nonzero otherwise
   3.108 - * 
   3.109 + *
   3.110   * @return total amount of bytes written to the output buffer
   3.111 - * 
   3.112 + *
   3.113   * @see c2html_plain_highlighter()
   3.114   * @see c2html_c_highlighter()
   3.115   * @see c2html_java_highlighter()
   3.116   */
   3.117 -size_t c2html_fformat(FILE* inputfile, char *ibuf, size_t ibuflen,
   3.118 -        void* outputbuffer, write_func wfnc,
   3.119 -        c2html_highlighter_func hltr, int showln);
   3.120 -
   3.121 -/**
   3.122 - * Reads a source file from the specified input file stream and directly writes
   3.123 - * the formatted output to the output file stream.
   3.124 - * 
   3.125 - * For files, the recommended intermediate buffer length is the file system
   3.126 - * block size.
   3.127 - * 
   3.128 - * @param inputfile the input file stream
   3.129 - * @param ibuf intermediate processing buffer
   3.130 - * @param ibuflen length of intermediate processing buffer (recommended: 4 KB)
   3.131 - * @param outputfile the output file stream
   3.132 - * @param hltr the highlighter function
   3.133 - * @param showln zero, if line numbers shall be omitted, nonzero otherwise
   3.134 - * 
   3.135 - * @see c2html_plain_highlighter()
   3.136 - * @see c2html_c_highlighter()
   3.137 - * @see c2html_java_highlighter()
   3.138 - */
   3.139 -void c2html_fformatf(FILE *inputfile, char *ibuf, size_t ibuflen,
   3.140 -        FILE* outputfile, c2html_highlighter_func hltr, int showln);
   3.141 -
   3.142 -
   3.143 -/**
   3.144 - * Writes at most <code>maxlen</code> bytes of formatted source data to the
   3.145 - * output buffer.
   3.146 - * 
   3.147 - * @param inputbuffer the source file data as string
   3.148 - * @param inputbuflen the length of the source file
   3.149 - * @param outputbuffer the output buffer
   3.150 - * @param wfnc a write function for the output buffer
   3.151 - * @param maxlen the maximum amount bytes which will be written to the
   3.152 - * output buffer
   3.153 - * @param hltr the highlighter function
   3.154 - * @param showln zero, if line numbers shall be omitted, nonzero otherwise
   3.155 - * 
   3.156 - * @return total amount of bytes written to the output buffer
   3.157 - * 
   3.158 - * @see c2html_plain_highlighter()
   3.159 - * @see c2html_c_highlighter()
   3.160 - * @see c2html_java_highlighter()
   3.161 - */
   3.162 -size_t c2html_bformatn(const char* inputbuffer, size_t inputbuflen,
   3.163 -        void* outputbuffer, write_func wfnc,
   3.164 -        size_t maxlen, c2html_highlighter_func hltr, int showln);
   3.165 -    
   3.166 -
   3.167 -/**
   3.168 - * Writes the formatted source data to the output buffer.
   3.169 - * 
   3.170 - * @param inputbuffer the source file data as string
   3.171 - * @param inputbuflen the length of the source file
   3.172 - * @param outputbuffer the output buffer
   3.173 - * @param wfnc a write function for the output buffer
   3.174 - * @param hltr the highlighter function
   3.175 - * @param showln zero, if line numbers shall be omitted, nonzero otherwise
   3.176 - * 
   3.177 - * @return total amount of bytes written to the output buffer
   3.178 - * 
   3.179 - * @see c2html_plain_highlighter()
   3.180 - * @see c2html_c_highlighter()
   3.181 - * @see c2html_java_highlighter()
   3.182 - */
   3.183 -size_t c2html_bformat(const char* inputbuffer, size_t inputbuflen,
   3.184 -        void* outputbuffer, write_func wfnc,
   3.185 -        c2html_highlighter_func hltr, int showln);
   3.186 -
   3.187 -
   3.188 -/**
   3.189 - * Writes the formatted source data directly to the specified file stream.
   3.190 - * 
   3.191 - * @param inputbuffer the source file data as string
   3.192 - * @param inputbuflen the length of the source file
   3.193 - * @param outputfile the output file stream
   3.194 - * @param hltr the highlighter function
   3.195 - * @param showln zero, if line numbers shall be omitted, nonzero otherwise
   3.196 - * 
   3.197 - * @see c2html_plain_highlighter()
   3.198 - * @see c2html_c_highlighter()
   3.199 - * @see c2html_java_highlighter()
   3.200 - */
   3.201 -void c2html_bformatf(const char* inputbuffer, size_t inputbuflen,
   3.202 -        FILE* outputfile, c2html_highlighter_func hltr, int showln);
   3.203 +size_t c2html_format(CxList const* lines,
   3.204 +                     void* outbuf, cx_write_func wfnc,
   3.205 +                     c2html_highlighter_func hltr, int showln);
   3.206  
   3.207  #ifdef __cplusplus
   3.208  }
     4.1 --- a/src/frontend.c	Mon Oct 03 12:56:28 2022 +0200
     4.2 +++ b/src/frontend.c	Mon Apr 24 21:01:41 2023 +0200
     4.3 @@ -33,9 +33,7 @@
     4.4  #include <string.h>
     4.5  
     4.6  #include "c2html.h"
     4.7 -#include <ucx/utils.h>
     4.8 -
     4.9 -#define FILEBUF_SIZE 4096
    4.10 +#include <cx/utils.h>
    4.11  
    4.12  typedef struct {
    4.13      char* outfilename;
    4.14 @@ -45,20 +43,17 @@
    4.15      int showlinenumbers;
    4.16  } Settings;
    4.17  
    4.18 -static int appendfile(const char *filename, FILE *fout,
    4.19 -        char *copybuf, size_t copybuflen, const char *errmsg) {
    4.20 -    FILE *headerfile = fopen(filename, "r");
    4.21 -    if (!headerfile) {
    4.22 +static int appendfile(const char *filename, FILE *fout, const char *errmsg) {
    4.23 +    FILE *fin = fopen(filename, "r");
    4.24 +    if (!fin) {
    4.25          perror(errmsg);
    4.26          if (fout != stdout) {
    4.27              fclose(fout);
    4.28          }
    4.29          return 1;
    4.30      }
    4.31 -    ucx_stream_bncopy(headerfile, fout,
    4.32 -            (read_func) fread, (write_func) fwrite,
    4.33 -            copybuf, copybuflen, (size_t)-1);
    4.34 -    fclose(headerfile);
    4.35 +    cx_stream_copy(fin, fout, (cx_read_func) fread, (cx_write_func) fwrite);
    4.36 +    fclose(fin);
    4.37      return 0;
    4.38  }
    4.39  
    4.40 @@ -86,7 +81,7 @@
    4.41      c2html_highlighter_func hltr = c2html_c_highlighter;
    4.42  
    4.43      /* Parse command line */
    4.44 -    char optc;
    4.45 +    int optc;
    4.46      while ((optc = getopt(argc, argv, "hljo:pH:F:vV")) != -1) {
    4.47          switch (optc) {
    4.48              case 'o':
    4.49 @@ -142,15 +137,8 @@
    4.50              fout = stdout;
    4.51          }
    4.52          
    4.53 -        /* Allocate file buffer  */
    4.54 -        char *filebuf = malloc(FILEBUF_SIZE);
    4.55 -        if (!filebuf) {
    4.56 -            perror("Error allocating file buffer");
    4.57 -            return EXIT_FAILURE;
    4.58 -        }
    4.59 -        
    4.60          /* Prepend header file */
    4.61 -        if (appendfile(settings.headerfile, fout, filebuf, FILEBUF_SIZE,
    4.62 +        if (appendfile(settings.headerfile, fout,
    4.63                  "Error opening header file")) {
    4.64              return EXIT_FAILURE;
    4.65          }
    4.66 @@ -158,11 +146,17 @@
    4.67          /* Process input file */
    4.68          FILE *inputfile = fopen(settings.infilename, "r");
    4.69          if (inputfile) {
    4.70 -            c2html_fformatf(
    4.71 -                    inputfile, filebuf, FILEBUF_SIZE,
    4.72 -                    fout, hltr, settings.showlinenumbers
    4.73 +            CxBuffer fbuf;
    4.74 +            cxBufferInit(&fbuf, NULL, 4096, NULL, CX_BUFFER_AUTO_EXTEND);
    4.75 +            cx_stream_copy(inputfile, &fbuf, (cx_read_func) fread,
    4.76 +                           (cx_write_func) cxBufferWrite);
    4.77 +            fclose(inputfile);
    4.78 +            c2html_bformat(
    4.79 +                    fbuf.space, fbuf.size,
    4.80 +                    fout, (cx_write_func ) fwrite, hltr,
    4.81 +                    settings.showlinenumbers
    4.82              );
    4.83 -            fclose(inputfile);
    4.84 +            cxBufferDestroy(&fbuf);
    4.85          } else {
    4.86              perror("Error opening input file");
    4.87              if (fout != stdout) {
    4.88 @@ -172,12 +166,10 @@
    4.89          }
    4.90          
    4.91          /* Append footer file */
    4.92 -        if (appendfile(settings.footerfile, fout, filebuf, FILEBUF_SIZE,
    4.93 +        if (appendfile(settings.footerfile, fout,
    4.94                  "Error opening footer file")) {
    4.95              return EXIT_FAILURE;
    4.96          }
    4.97 -        
    4.98 -        free(filebuf);
    4.99  
   4.100          return EXIT_SUCCESS;
   4.101      }
     5.1 --- a/src/highlighter.c	Mon Oct 03 12:56:28 2022 +0200
     5.2 +++ b/src/highlighter.c	Mon Apr 24 21:01:41 2023 +0200
     5.3 @@ -33,35 +33,35 @@
     5.4  #include <stdio.h>
     5.5  #include <string.h>
     5.6  #include <ctype.h>
     5.7 -#include <ucx/string.h>
     5.8 -#include <ucx/utils.h>
     5.9  
    5.10 -static void put_htmlescaped(UcxBuffer *dest, char c) {
    5.11 +#include <cx/string.h>
    5.12 +
    5.13 +static void put_htmlescaped(CxBuffer *dest, char c) {
    5.14      if (c == '>') {
    5.15 -        ucx_buffer_puts(dest, "&gt;");
    5.16 +        cxBufferPutString(dest, "&gt;");
    5.17      } else if (c == '<') {
    5.18 -        ucx_buffer_puts(dest, "&lt;");
    5.19 +        cxBufferPutString(dest, "&lt;");
    5.20      } else if (c) {
    5.21 -        ucx_buffer_putc(dest, c);
    5.22 +        cxBufferPut(dest, c);
    5.23      }
    5.24  }
    5.25  
    5.26 -static void put_htmlescapedstr(UcxBuffer *dest, sstr_t s) {
    5.27 +static void put_htmlescapedstr(CxBuffer *dest, cxstring s) {
    5.28      for (int i = 0 ; i < s.length ; i++) {
    5.29          put_htmlescaped(dest, s.ptr[i]);
    5.30      }
    5.31  }
    5.32  
    5.33 -static int check_keyword(sstr_t word, const char** keywords) {
    5.34 +static int check_keyword(cxstring word, const char** keywords) {
    5.35      for (int i = 0 ; keywords[i] ; i++) {
    5.36 -        if (sstrcmp(word, sstr((char*)keywords[i])) == 0) {
    5.37 +        if (cx_strcmp(word, cx_str(keywords[i])) == 0) {
    5.38              return 1;
    5.39          }
    5.40      }
    5.41      return 0;
    5.42  }
    5.43  
    5.44 -static int check_capsonly(sstr_t word) {
    5.45 +static int check_capsonly(cxstring word) {
    5.46      for (size_t i = 0 ; i < word.length ; i++) {
    5.47          if (!isupper(word.ptr[i]) && !isdigit(word.ptr[i])
    5.48                  && word.ptr[i] != '_') {
    5.49 @@ -73,7 +73,7 @@
    5.50  
    5.51  /* Plaintext Highlighter */
    5.52  
    5.53 -void c2html_plain_highlighter(char *src, UcxBuffer *dest,
    5.54 +void c2html_plain_highlighter(char const *src, CxBuffer *dest,
    5.55          c2html_highlighter_data *hd) {
    5.56      while (*src && *src != '\n') {
    5.57          if (*src != '\r') {
    5.58 @@ -81,7 +81,7 @@
    5.59          }
    5.60          src++;
    5.61      }
    5.62 -    ucx_buffer_putc(dest, '\n');
    5.63 +    cxBufferPut(dest, '\n');
    5.64  }
    5.65  
    5.66  /* C Highlighter */
    5.67 @@ -94,15 +94,15 @@
    5.68      "while", NULL
    5.69  };
    5.70  
    5.71 -void c2html_c_highlighter(char *src, UcxBuffer *dest,
    5.72 +void c2html_c_highlighter(char const *src, CxBuffer *dest,
    5.73          c2html_highlighter_data *hd) {
    5.74      /* reset buffers without clearing them */
    5.75 -    hd->primary_buffer->size = hd->primary_buffer->pos = 0;
    5.76 -    hd->secondary_buffer->size = hd->secondary_buffer->pos = 0;
    5.77 +    hd->primary_buffer.size = hd->primary_buffer.pos = 0;
    5.78 +    hd->secondary_buffer.size = hd->secondary_buffer.pos = 0;
    5.79      
    5.80      /* alias the buffers for better handling */
    5.81 -    UcxBuffer *wbuf = hd->primary_buffer;
    5.82 -    UcxBuffer *ifilebuf = hd->secondary_buffer;
    5.83 +    CxBuffer *wbuf = &hd->primary_buffer;
    5.84 +    CxBuffer *ifilebuf = &hd->secondary_buffer;
    5.85      
    5.86      /* local information */
    5.87      size_t sp = (size_t)-1;
    5.88 @@ -113,7 +113,7 @@
    5.89      /* continue a multi line comment highlighting */
    5.90      if (hd->multiline_comment) {
    5.91          iscomment = 1;
    5.92 -        ucx_buffer_puts(dest, "<span class=\"c2html-comment\">");
    5.93 +        cxBufferPutString(dest, "<span class=\"c2html-comment\">");
    5.94      }
    5.95  
    5.96      char c;
    5.97 @@ -126,42 +126,42 @@
    5.98              if (hd->multiline_comment && sp > 0 && src[sp-1] == '*') {
    5.99                  iscomment = 0;
   5.100                  hd->multiline_comment = 0;
   5.101 -                ucx_buffer_puts(dest, "/</span>");
   5.102 +                cxBufferPutString(dest, "/</span>");
   5.103                  continue;
   5.104              } else if (!iscomment && (src[sp+1] == '/' || src[sp+1] == '*')) {
   5.105                  iscomment = 1;
   5.106                  hd->multiline_comment = (src[sp+1] == '*');
   5.107 -                ucx_buffer_puts(dest, "<span class=\"c2html-comment\">");
   5.108 +                cxBufferPutString(dest, "<span class=\"c2html-comment\">");
   5.109              }
   5.110          }
   5.111  
   5.112          if (iscomment) {
   5.113              if (c == '\n') {
   5.114 -                ucx_buffer_puts(dest, "</span>\n");
   5.115 +                cxBufferPutString(dest, "</span>\n");
   5.116              } else {
   5.117                  put_htmlescaped(dest, c);
   5.118              }
   5.119          } else if (isinclude) {
   5.120              if (c == '<') {
   5.121 -                ucx_buffer_puts(dest,
   5.122 +                cxBufferPutString(dest,
   5.123                          "<span class=\"c2html-stdinclude\">&lt;");
   5.124              } else if (c == '\"') {
   5.125                  if (parseinclude) {
   5.126 -                    ucx_buffer_puts(dest, "\">");
   5.127 -                    ucx_buffer_write(ifilebuf->space, 1, ifilebuf->size, dest);
   5.128 -                    ucx_buffer_puts(dest, "\"</a>");
   5.129 +                    cxBufferPutString(dest, "\">");
   5.130 +                    cxBufferWrite(ifilebuf->space, 1, ifilebuf->size, dest);
   5.131 +                    cxBufferPutString(dest, "\"</a>");
   5.132                      parseinclude = 0;
   5.133                  } else {
   5.134 -                    ucx_buffer_puts(dest,
   5.135 +                    cxBufferPutString(dest,
   5.136                              "<a class=\"c2html-userinclude\" href=\"");
   5.137 -                    ucx_buffer_putc(ifilebuf, '\"');
   5.138 +                    cxBufferPut(ifilebuf, '\"');
   5.139                      parseinclude = 1;
   5.140                  }
   5.141              } else if (c == '>') {
   5.142 -                ucx_buffer_puts(dest,  "&gt;</span>");
   5.143 +                cxBufferPutString(dest,  "&gt;</span>");
   5.144              } else {
   5.145                  if (parseinclude) {
   5.146 -                    ucx_buffer_putc(ifilebuf, c);
   5.147 +                    cxBufferPut(ifilebuf, c);
   5.148                  }
   5.149                  put_htmlescaped(dest, c);
   5.150              }
   5.151 @@ -172,14 +172,14 @@
   5.152                      put_htmlescaped(dest, c);
   5.153                      if (c == quote) {
   5.154                          isstring = 0;
   5.155 -                        ucx_buffer_puts(dest, "</span>");
   5.156 +                        cxBufferPutString(dest, "</span>");
   5.157                      } else {
   5.158                          put_htmlescaped(dest, c);
   5.159                      }
   5.160                  } else {
   5.161                      isstring = 1;
   5.162                      quote = c;
   5.163 -                    ucx_buffer_puts(dest, "<span class=\"c2html-string\">");
   5.164 +                    cxBufferPutString(dest, "<span class=\"c2html-string\">");
   5.165                      put_htmlescaped(dest, c);
   5.166                  }
   5.167              } else {
   5.168 @@ -187,32 +187,32 @@
   5.169                      put_htmlescaped(dest, c);
   5.170                  } else if (isalnum(c) ||  c == '_' || c == '#') {
   5.171                      /* buffer the current word */
   5.172 -                    ucx_buffer_putc(wbuf, c);
   5.173 +                    cxBufferPut(wbuf, c);
   5.174                  } else {
   5.175                      /* write buffered word, if any */
   5.176                      if (wbuf->size > 0) {
   5.177 -                        sstr_t word = sstrn(wbuf->space, wbuf->size);
   5.178 +                        cxstring word = cx_strn(wbuf->space, wbuf->size);
   5.179                          int closespan = 1;
   5.180 -                        sstr_t typesuffix = ST("_t");
   5.181 +                        cxstring typesuffix = CX_STR("_t");
   5.182                          if (check_keyword(word, ckeywords)) {
   5.183 -                            ucx_buffer_puts(dest,
   5.184 +                            cxBufferPutString(dest,
   5.185                                      "<span class=\"c2html-keyword\">");
   5.186 -                        } else if (sstrsuffix(word, typesuffix)) {
   5.187 -                            ucx_buffer_puts(dest,
   5.188 +                        } else if (cx_strsuffix(word, typesuffix)) {
   5.189 +                            cxBufferPutString(dest,
   5.190                                  "<span class=\"c2html-type\">");
   5.191                          } else if (word.ptr[0] == '#') {
   5.192 -                            isinclude = !sstrcmp(word, S("#include"));
   5.193 -                            ucx_buffer_puts(dest,
   5.194 +                            isinclude = !cx_strcmp(word, CX_STR("#include"));
   5.195 +                            cxBufferPutString(dest,
   5.196                                  "<span class=\"c2html-directive\">");
   5.197                          } else if (check_capsonly(word)) {
   5.198 -                            ucx_buffer_puts(dest,
   5.199 +                            cxBufferPutString(dest,
   5.200                                  "<span class=\"c2html-macroconst\">");
   5.201                          } else {
   5.202                              closespan = 0;
   5.203                          }
   5.204                          put_htmlescapedstr(dest, word);
   5.205                          if (closespan) {
   5.206 -                            ucx_buffer_puts(dest, "</span>");
   5.207 +                            cxBufferPutString(dest, "</span>");
   5.208                          }
   5.209                      }
   5.210                      wbuf->pos = wbuf->size = 0; /* reset word buffer */
   5.211 @@ -239,14 +239,14 @@
   5.212      "volatile", "const", "float", "native", "super", "while", NULL
   5.213  };
   5.214  
   5.215 -void c2html_java_highlighter(char *src, UcxBuffer *dest,
   5.216 +void c2html_java_highlighter(char const *src, CxBuffer *dest,
   5.217          c2html_highlighter_data *hd) {
   5.218      /* reset buffers without clearing them */
   5.219 -    hd->primary_buffer->size = hd->primary_buffer->pos = 0;
   5.220 -    hd->secondary_buffer->size = hd->secondary_buffer->pos = 0;
   5.221 +    hd->primary_buffer.size = hd->primary_buffer.pos = 0;
   5.222 +    hd->secondary_buffer.size = hd->secondary_buffer.pos = 0;
   5.223  
   5.224      /* alias the buffers for better handling */
   5.225 -    UcxBuffer *wbuf = hd->primary_buffer;
   5.226 +    CxBuffer *wbuf = &hd->primary_buffer;
   5.227      
   5.228      /* local information */
   5.229      size_t sp = (size_t)-1;
   5.230 @@ -256,7 +256,7 @@
   5.231  
   5.232      if (hd->multiline_comment) {
   5.233          iscomment = 1;
   5.234 -        ucx_buffer_puts(dest, "<span class=\"c2html-comment\">");
   5.235 +        cxBufferPutString(dest, "<span class=\"c2html-comment\">");
   5.236      }
   5.237  
   5.238      char c;
   5.239 @@ -269,18 +269,18 @@
   5.240              if (hd->multiline_comment && sp > 0 && src[sp-1] == '*') {
   5.241                  iscomment = 0;
   5.242                  hd->multiline_comment = 0;
   5.243 -                ucx_buffer_puts(dest, "/</span>");
   5.244 +                cxBufferPutString(dest, "/</span>");
   5.245                  continue;
   5.246              } else if (!iscomment && (src[sp+1] == '/' || src[sp+1] == '*')) {
   5.247                  iscomment = 1;
   5.248                  hd->multiline_comment = (src[sp+1] == '*');
   5.249 -                ucx_buffer_puts(dest, "<span class=\"c2html-comment\">");
   5.250 +                cxBufferPutString(dest, "<span class=\"c2html-comment\">");
   5.251              }
   5.252          }
   5.253  
   5.254          if (iscomment) {
   5.255              if (c == '\n') {
   5.256 -                ucx_buffer_puts(dest, "</span>\n");
   5.257 +                cxBufferPutString(dest, "</span>\n");
   5.258              } else {
   5.259                  put_htmlescaped(dest, c);
   5.260              }
   5.261 @@ -293,14 +293,14 @@
   5.262                      put_htmlescaped(dest, c);
   5.263                      if (c == quote) {
   5.264                          isstring = 0;
   5.265 -                        ucx_buffer_puts(dest, "</span>");
   5.266 +                        cxBufferPutString(dest, "</span>");
   5.267                      } else {
   5.268                          put_htmlescaped(dest, c);
   5.269                      }
   5.270                  } else {
   5.271                      isstring = 1;
   5.272                      quote = c;
   5.273 -                    ucx_buffer_puts(dest,
   5.274 +                    cxBufferPutString(dest,
   5.275                          "<span class=\"c2html-string\">");
   5.276                      put_htmlescaped(dest, c);
   5.277                  }
   5.278 @@ -309,23 +309,23 @@
   5.279                      put_htmlescaped(dest, c);
   5.280                  } else if (isalnum(c) || c == '_' || c == '@') {
   5.281                      /* buffer the current word */
   5.282 -                    ucx_buffer_putc(wbuf, c);
   5.283 +                    cxBufferPut(wbuf, c);
   5.284                  } else {
   5.285                      /* write buffered word, if any */
   5.286                      if (wbuf->size > 0) {
   5.287 -                        sstr_t word = sstrn(wbuf->space, wbuf->size);
   5.288 +                        cxstring word = cx_strn(wbuf->space, wbuf->size);
   5.289                          int closespan = 1;
   5.290                          if (check_keyword(word, jkeywords)) {
   5.291 -                            ucx_buffer_puts(dest,
   5.292 +                            cxBufferPutString(dest,
   5.293                                  "<span class=\"c2html-keyword\">");
   5.294                          } else if (isupper(word.ptr[0])) {
   5.295 -                            ucx_buffer_puts(dest,
   5.296 +                            cxBufferPutString(dest,
   5.297                                  "<span class=\"c2html-type\">");
   5.298                          } else if (word.ptr[0] == '@') {
   5.299 -                            ucx_buffer_puts(dest,
   5.300 +                            cxBufferPutString(dest,
   5.301                                  "<span class=\"c2html-directive\">");
   5.302                          } else if (check_capsonly(word)) {
   5.303 -                            ucx_buffer_puts(dest,
   5.304 +                            cxBufferPutString(dest,
   5.305                                  "<span class=\"c2html-macroconst\">");
   5.306                          } else {
   5.307                              closespan = 0;
   5.308 @@ -333,7 +333,7 @@
   5.309                          put_htmlescapedstr(dest, word);
   5.310                          
   5.311                          if (closespan) {
   5.312 -                            ucx_buffer_puts(dest, "</span>");
   5.313 +                            cxBufferPutString(dest, "</span>");
   5.314                          }
   5.315                      }
   5.316                      wbuf->pos = wbuf->size = 0; /* reset buffer */
     6.1 --- a/src/highlighter.h	Mon Oct 03 12:56:28 2022 +0200
     6.2 +++ b/src/highlighter.h	Mon Apr 24 21:01:41 2023 +0200
     6.3 @@ -30,7 +30,7 @@
     6.4  #ifndef C2HTML_HIGHLIGHTER_H
     6.5  #define C2HTML_HIGHLIGHTER_H
     6.6  
     6.7 -#include <ucx/buffer.h>
     6.8 +#include <cx/buffer.h>
     6.9  
    6.10  #ifdef __cplusplus
    6.11  extern "C" {
    6.12 @@ -38,12 +38,12 @@
    6.13      
    6.14  typedef struct {
    6.15      int multiline_comment;
    6.16 -    UcxBuffer* primary_buffer;
    6.17 -    UcxBuffer* secondary_buffer;
    6.18 +    CxBuffer primary_buffer;
    6.19 +    CxBuffer secondary_buffer;
    6.20  } c2html_highlighter_data;
    6.21  
    6.22  #define C2HTML_HIGHLIGHTER_SIGNATURE \
    6.23 -char*,UcxBuffer*, c2html_highlighter_data*
    6.24 +char const*, CxBuffer*, c2html_highlighter_data*
    6.25  
    6.26  typedef void(*c2html_highlighter_func)(C2HTML_HIGHLIGHTER_SIGNATURE);
    6.27  
     7.1 --- a/test/ctest.c	Mon Oct 03 12:56:28 2022 +0200
     7.2 +++ b/test/ctest.c	Mon Apr 24 21:01:41 2023 +0200
     7.3 @@ -199,7 +199,7 @@
     7.4  
     7.5  char* util_path_to_url(DavSession *sn, char *path) {
     7.6      char *space = malloc(256);
     7.7 -    UcxBuffer *url = ucx_buffer_new(space, 256, UCX_BUFFER_AUTOEXTEND);
     7.8 +    UcxBuffer *url = ucx_buffer_new(space, 256, CX_BUFFER_AUTO_EXTEND);
     7.9      
    7.10      // add base url
    7.11      ucx_buffer_write(sn->base_url, 1, strlen(sn->base_url), url);
     8.1 --- a/test/golden-master/bigtest.html	Mon Oct 03 12:56:28 2022 +0200
     8.2 +++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
     8.3 @@ -1,861 +0,0 @@
     8.4 -<!DOCTYPE html>
     8.5 -<html>
     8.6 -  <head>
     8.7 -    <title>c2html</title>
     8.8 -    <style type="text/css">
     8.9 -      a.c2html-lineno {
    8.10 -        /* as long as user-select isn't widely spread, we throw the bomb */
    8.11 -        -webkit-user-select: none;
    8.12 -        -moz-user-select: none;
    8.13 -        -ms-user-select: none;
    8.14 -        user-select: none;
    8.15 -        display: inline-block;
    8.16 -        font-style: italic;
    8.17 -        text-decoration: none;
    8.18 -        color: grey;
    8.19 -      }
    8.20 -      span.c2html-keyword {
    8.21 -        color: blue;
    8.22 -      }
    8.23 -      span.c2html-macroconst {
    8.24 -        color: cornflowerblue;
    8.25 -      }
    8.26 -      span.c2html-type {
    8.27 -        color: cornflowerblue;
    8.28 -      }
    8.29 -      span.c2html-directive {
    8.30 -        color: green;
    8.31 -      }
    8.32 -      span.c2html-string {
    8.33 -        color: darkorange;
    8.34 -      }
    8.35 -      span.c2html-comment {
    8.36 -        color: grey;
    8.37 -      }
    8.38 -      span.c2html-stdinclude {
    8.39 -        color: darkorange;
    8.40 -      }
    8.41 -      span.c2html-userinclude {
    8.42 -        color: darkorange;
    8.43 -      }
    8.44 -      a.c2html-userinclude {
    8.45 -        color: darkorange;
    8.46 -        text-decoration: underline;
    8.47 -      }
    8.48 -    </style>
    8.49 -  </head>
    8.50 -  <body>
    8.51 -
    8.52 -<pre>
    8.53 -<a class="c2html-lineno" name="l1" href="#l1">  1 </a><span class="c2html-comment">/*</span>
    8.54 -<a class="c2html-lineno" name="l2" href="#l2">  2 </a><span class="c2html-comment"> * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.</span>
    8.55 -<a class="c2html-lineno" name="l3" href="#l3">  3 </a><span class="c2html-comment"> *</span>
    8.56 -<a class="c2html-lineno" name="l4" href="#l4">  4 </a><span class="c2html-comment"> * Copyright 2014 Mike Becker. All rights reserved.</span>
    8.57 -<a class="c2html-lineno" name="l5" href="#l5">  5 </a><span class="c2html-comment"> *</span>
    8.58 -<a class="c2html-lineno" name="l6" href="#l6">  6 </a><span class="c2html-comment"> * Redistribution and use in source and binary forms, with or without</span>
    8.59 -<a class="c2html-lineno" name="l7" href="#l7">  7 </a><span class="c2html-comment"> * modification, are permitted provided that the following conditions are met:</span>
    8.60 -<a class="c2html-lineno" name="l8" href="#l8">  8 </a><span class="c2html-comment"> *</span>
    8.61 -<a class="c2html-lineno" name="l9" href="#l9">  9 </a><span class="c2html-comment"> *   1. Redistributions of source code must retain the above copyright</span>
    8.62 -<a class="c2html-lineno" name="l10" href="#l10"> 10 </a><span class="c2html-comment"> *      notice, this list of conditions and the following disclaimer.</span>
    8.63 -<a class="c2html-lineno" name="l11" href="#l11"> 11 </a><span class="c2html-comment"> *</span>
    8.64 -<a class="c2html-lineno" name="l12" href="#l12"> 12 </a><span class="c2html-comment"> *   2. Redistributions in binary form must reproduce the above copyright</span>
    8.65 -<a class="c2html-lineno" name="l13" href="#l13"> 13 </a><span class="c2html-comment"> *      notice, this list of conditions and the following disclaimer in the</span>
    8.66 -<a class="c2html-lineno" name="l14" href="#l14"> 14 </a><span class="c2html-comment"> *      documentation and/or other materials provided with the distribution.</span>
    8.67 -<a class="c2html-lineno" name="l15" href="#l15"> 15 </a><span class="c2html-comment"> *</span>
    8.68 -<a class="c2html-lineno" name="l16" href="#l16"> 16 </a><span class="c2html-comment"> * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"</span>
    8.69 -<a class="c2html-lineno" name="l17" href="#l17"> 17 </a><span class="c2html-comment"> * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE</span>
    8.70 -<a class="c2html-lineno" name="l18" href="#l18"> 18 </a><span class="c2html-comment"> * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE</span>
    8.71 -<a class="c2html-lineno" name="l19" href="#l19"> 19 </a><span class="c2html-comment"> * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE</span>
    8.72 -<a class="c2html-lineno" name="l20" href="#l20"> 20 </a><span class="c2html-comment"> * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR</span>
    8.73 -<a class="c2html-lineno" name="l21" href="#l21"> 21 </a><span class="c2html-comment"> * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF</span>
    8.74 -<a class="c2html-lineno" name="l22" href="#l22"> 22 </a><span class="c2html-comment"> * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS</span>
    8.75 -<a class="c2html-lineno" name="l23" href="#l23"> 23 </a><span class="c2html-comment"> * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN</span>
    8.76 -<a class="c2html-lineno" name="l24" href="#l24"> 24 </a><span class="c2html-comment"> * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)</span>
    8.77 -<a class="c2html-lineno" name="l25" href="#l25"> 25 </a><span class="c2html-comment"> * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE</span>
    8.78 -<a class="c2html-lineno" name="l26" href="#l26"> 26 </a><span class="c2html-comment"> * POSSIBILITY OF SUCH DAMAGE.</span>
    8.79 -<a class="c2html-lineno" name="l27" href="#l27"> 27 </a><span class="c2html-comment"> *</span>
    8.80 -<a class="c2html-lineno" name="l28" href="#l28"> 28 </a><span class="c2html-comment"> */</span>
    8.81 -<a class="c2html-lineno" name="l29" href="#l29"> 29 </a>
    8.82 -<a class="c2html-lineno" name="l30" href="#l30"> 30 </a><span class="c2html-directive">#include</span> <a class="c2html-userinclude" href="rules.h">"rules.h"</a>
    8.83 -<a class="c2html-lineno" name="l31" href="#l31"> 31 </a><span class="c2html-directive">#include</span> <a class="c2html-userinclude" href="chess.h">"chess.h"</a>
    8.84 -<a class="c2html-lineno" name="l32" href="#l32"> 32 </a><span class="c2html-directive">#include</span> <span class="c2html-stdinclude">&lt;string.h&gt;</span>
    8.85 -<a class="c2html-lineno" name="l33" href="#l33"> 33 </a><span class="c2html-directive">#include</span> <span class="c2html-stdinclude">&lt;stdlib.h&gt;</span>
    8.86 -<a class="c2html-lineno" name="l34" href="#l34"> 34 </a><span class="c2html-directive">#include</span> <span class="c2html-stdinclude">&lt;sys/time.h&gt;</span>
    8.87 -<a class="c2html-lineno" name="l35" href="#l35"> 35 </a>
    8.88 -<a class="c2html-lineno" name="l36" href="#l36"> 36 </a><span class="c2html-keyword">static</span> GameState gamestate_copy_sim(GameState *gamestate) {
    8.89 -<a class="c2html-lineno" name="l37" href="#l37"> 37 </a>    GameState simulation = *gamestate;
    8.90 -<a class="c2html-lineno" name="l38" href="#l38"> 38 </a>    <span class="c2html-keyword">if</span> (simulation.lastmove) {
    8.91 -<a class="c2html-lineno" name="l39" href="#l39"> 39 </a>        MoveList *lastmovecopy = malloc(<span class="c2html-keyword">sizeof</span>(MoveList));
    8.92 -<a class="c2html-lineno" name="l40" href="#l40"> 40 </a>        *lastmovecopy = *(simulation.lastmove);
    8.93 -<a class="c2html-lineno" name="l41" href="#l41"> 41 </a>        simulation.movelist = simulation.lastmove = lastmovecopy;
    8.94 -<a class="c2html-lineno" name="l42" href="#l42"> 42 </a>    }
    8.95 -<a class="c2html-lineno" name="l43" href="#l43"> 43 </a>
    8.96 -<a class="c2html-lineno" name="l44" href="#l44"> 44 </a>    <span class="c2html-keyword">return</span> simulation;
    8.97 -<a class="c2html-lineno" name="l45" href="#l45"> 45 </a>}
    8.98 -<a class="c2html-lineno" name="l46" href="#l46"> 46 </a>
    8.99 -<a class="c2html-lineno" name="l47" href="#l47"> 47 </a><span class="c2html-keyword">void</span> gamestate_init(GameState *gamestate) {
   8.100 -<a class="c2html-lineno" name="l48" href="#l48"> 48 </a>    memset(gamestate, <span class="c2html-macroconst">0</span>, <span class="c2html-keyword">sizeof</span>(GameState));
   8.101 -<a class="c2html-lineno" name="l49" href="#l49"> 49 </a>    
   8.102 -<a class="c2html-lineno" name="l50" href="#l50"> 50 </a>    Board initboard = {
   8.103 -<a class="c2html-lineno" name="l51" href="#l51"> 51 </a>        {<span class="c2html-macroconst">WROOK</span>, <span class="c2html-macroconst">WKNIGHT</span>, <span class="c2html-macroconst">WBISHOP</span>, <span class="c2html-macroconst">WQUEEN</span>, <span class="c2html-macroconst">WKING</span>, <span class="c2html-macroconst">WBISHOP</span>, <span class="c2html-macroconst">WKNIGHT</span>, <span class="c2html-macroconst">WROOK</span>},
   8.104 -<a class="c2html-lineno" name="l52" href="#l52"> 52 </a>        {<span class="c2html-macroconst">WPAWN</span>, <span class="c2html-macroconst">WPAWN</span>,   <span class="c2html-macroconst">WPAWN</span>,   <span class="c2html-macroconst">WPAWN</span>,  <span class="c2html-macroconst">WPAWN</span>, <span class="c2html-macroconst">WPAWN</span>,   <span class="c2html-macroconst">WPAWN</span>,   <span class="c2html-macroconst">WPAWN</span>},
   8.105 -<a class="c2html-lineno" name="l53" href="#l53"> 53 </a>        {<span class="c2html-macroconst">0</span>,     <span class="c2html-macroconst">0</span>,       <span class="c2html-macroconst">0</span>,       <span class="c2html-macroconst">0</span>,      <span class="c2html-macroconst">0</span>,     <span class="c2html-macroconst">0</span>,       <span class="c2html-macroconst">0</span>,       <span class="c2html-macroconst">0</span>},
   8.106 -<a class="c2html-lineno" name="l54" href="#l54"> 54 </a>        {<span class="c2html-macroconst">0</span>,     <span class="c2html-macroconst">0</span>,       <span class="c2html-macroconst">0</span>,       <span class="c2html-macroconst">0</span>,      <span class="c2html-macroconst">0</span>,     <span class="c2html-macroconst">0</span>,       <span class="c2html-macroconst">0</span>,       <span class="c2html-macroconst">0</span>},
   8.107 -<a class="c2html-lineno" name="l55" href="#l55"> 55 </a>        {<span class="c2html-macroconst">0</span>,     <span class="c2html-macroconst">0</span>,       <span class="c2html-macroconst">0</span>,       <span class="c2html-macroconst">0</span>,      <span class="c2html-macroconst">0</span>,     <span class="c2html-macroconst">0</span>,       <span class="c2html-macroconst">0</span>,       <span class="c2html-macroconst">0</span>},
   8.108 -<a class="c2html-lineno" name="l56" href="#l56"> 56 </a>        {<span class="c2html-macroconst">0</span>,     <span class="c2html-macroconst">0</span>,       <span class="c2html-macroconst">0</span>,       <span class="c2html-macroconst">0</span>,      <span class="c2html-macroconst">0</span>,     <span class="c2html-macroconst">0</span>,       <span class="c2html-macroconst">0</span>,       <span class="c2html-macroconst">0</span>},
   8.109 -<a class="c2html-lineno" name="l57" href="#l57"> 57 </a>        {<span class="c2html-macroconst">BPAWN</span>, <span class="c2html-macroconst">BPAWN</span>,   <span class="c2html-macroconst">BPAWN</span>,   <span class="c2html-macroconst">BPAWN</span>,  <span class="c2html-macroconst">BPAWN</span>, <span class="c2html-macroconst">BPAWN</span>,   <span class="c2html-macroconst">BPAWN</span>,   <span class="c2html-macroconst">BPAWN</span>},
   8.110 -<a class="c2html-lineno" name="l58" href="#l58"> 58 </a>        {<span class="c2html-macroconst">BROOK</span>, <span class="c2html-macroconst">BKNIGHT</span>, <span class="c2html-macroconst">BBISHOP</span>, <span class="c2html-macroconst">BQUEEN</span>, <span class="c2html-macroconst">BKING</span>, <span class="c2html-macroconst">BBISHOP</span>, <span class="c2html-macroconst">BKNIGHT</span>, <span class="c2html-macroconst">BROOK</span>}
   8.111 -<a class="c2html-lineno" name="l59" href="#l59"> 59 </a>    };
   8.112 -<a class="c2html-lineno" name="l60" href="#l60"> 60 </a>    memcpy(gamestate-&gt;board, initboard, <span class="c2html-keyword">sizeof</span>(Board));
   8.113 -<a class="c2html-lineno" name="l61" href="#l61"> 61 </a>}
   8.114 -<a class="c2html-lineno" name="l62" href="#l62"> 62 </a>
   8.115 -<a class="c2html-lineno" name="l63" href="#l63"> 63 </a><span class="c2html-keyword">void</span> gamestate_cleanup(GameState *gamestate) {
   8.116 -<a class="c2html-lineno" name="l64" href="#l64"> 64 </a>    MoveList *elem;
   8.117 -<a class="c2html-lineno" name="l65" href="#l65"> 65 </a>    elem = gamestate-&gt;movelist;
   8.118 -<a class="c2html-lineno" name="l66" href="#l66"> 66 </a>    <span class="c2html-keyword">while</span> (elem) {
   8.119 -<a class="c2html-lineno" name="l67" href="#l67"> 67 </a>        MoveList *cur = elem;
   8.120 -<a class="c2html-lineno" name="l68" href="#l68"> 68 </a>        elem = elem-&gt;next;
   8.121 -<a class="c2html-lineno" name="l69" href="#l69"> 69 </a>        free(cur);
   8.122 -<a class="c2html-lineno" name="l70" href="#l70"> 70 </a>    };
   8.123 -<a class="c2html-lineno" name="l71" href="#l71"> 71 </a>}
   8.124 -<a class="c2html-lineno" name="l72" href="#l72"> 72 </a>
   8.125 -<a class="c2html-lineno" name="l73" href="#l73"> 73 </a><span class="c2html-comment">/* MUST be called IMMEDIATLY after applying a move to work correctly */</span>
   8.126 -<a class="c2html-lineno" name="l74" href="#l74"> 74 </a><span class="c2html-keyword">static</span> <span class="c2html-keyword">void</span> format_move(GameState *gamestate, Move *move) {
   8.127 -<a class="c2html-lineno" name="l75" href="#l75"> 75 </a>    <span class="c2html-keyword">char</span> *string = move-&gt;string;
   8.128 -<a class="c2html-lineno" name="l76" href="#l76"> 76 </a>    
   8.129 -<a class="c2html-lineno" name="l77" href="#l77"> 77 </a>    <span class="c2html-comment">/* at least 8 characters should be available, wipe them out */</span>
   8.130 -<a class="c2html-lineno" name="l78" href="#l78"> 78 </a>    memset(string, <span class="c2html-macroconst">0</span>, <span class="c2html-macroconst">8</span>);
   8.131 -<a class="c2html-lineno" name="l79" href="#l79"> 79 </a>    
   8.132 -<a class="c2html-lineno" name="l80" href="#l80"> 80 </a>    <span class="c2html-comment">/* special formats for castling */</span>
   8.133 -<a class="c2html-lineno" name="l81" href="#l81"> 81 </a>    <span class="c2html-keyword">if</span> ((move-&gt;piece&<span class="c2html-macroconst">PIECE_MASK</span>) == <span class="c2html-macroconst">KING</span> &&
   8.134 -<a class="c2html-lineno" name="l82" href="#l82"> 82 </a>            abs(move-&gt;tofile-move-&gt;fromfile) == <span class="c2html-macroconst">2</span>) {
   8.135 -<a class="c2html-lineno" name="l83" href="#l83"> 83 </a>        <span class="c2html-keyword">if</span> (move-&gt;tofile==fileidx(<span class="c2html-string">'c'</span>)) {
   8.136 -<a class="c2html-lineno" name="l84" href="#l84"> 84 </a>            memcpy(string, <span class="c2html-string">"O-O-O"</span>, <span class="c2html-macroconst">5</span>);
   8.137 -<a class="c2html-lineno" name="l85" href="#l85"> 85 </a>        } <span class="c2html-keyword">else</span> {
   8.138 -<a class="c2html-lineno" name="l86" href="#l86"> 86 </a>            memcpy(string, <span class="c2html-string">"O-O"</span>, <span class="c2html-macroconst">3</span>);
   8.139 -<a class="c2html-lineno" name="l87" href="#l87"> 87 </a>        }
   8.140 -<a class="c2html-lineno" name="l88" href="#l88"> 88 </a>    }
   8.141 -<a class="c2html-lineno" name="l89" href="#l89"> 89 </a>
   8.142 -<a class="c2html-lineno" name="l90" href="#l90"> 90 </a>    <span class="c2html-comment">/* start by notating the piece character */</span>
   8.143 -<a class="c2html-lineno" name="l91" href="#l91"> 91 </a>    string[<span class="c2html-macroconst">0</span>] = getpiecechr(move-&gt;piece);
   8.144 -<a class="c2html-lineno" name="l92" href="#l92"> 92 </a>    <span class="c2html-keyword">int</span> idx = string[<span class="c2html-macroconst">0</span>] ? <span class="c2html-macroconst">1</span> : <span class="c2html-macroconst">0</span>;
   8.145 -<a class="c2html-lineno" name="l93" href="#l93"> 93 </a>    
   8.146 -<a class="c2html-lineno" name="l94" href="#l94"> 94 </a>    <span class="c2html-comment">/* find out how many source information we do need */</span>
   8.147 -<a class="c2html-lineno" name="l95" href="#l95"> 95 </a>    <span class="c2html-type">uint8_t</span> piece = move-&gt;piece & <span class="c2html-macroconst">PIECE_MASK</span>;
   8.148 -<a class="c2html-lineno" name="l96" href="#l96"> 96 </a>    <span class="c2html-keyword">if</span> (piece == <span class="c2html-macroconst">PAWN</span>) {
   8.149 -<a class="c2html-lineno" name="l97" href="#l97"> 97 </a>        <span class="c2html-keyword">if</span> (move-&gt;capture) {
   8.150 -<a class="c2html-lineno" name="l98" href="#l98"> 98 </a>            string[idx++] = filechr(move-&gt;fromfile);
   8.151 -<a class="c2html-lineno" name="l99" href="#l99"> 99 </a>        }
   8.152 -<a class="c2html-lineno" name="l100" href="#l100">100 </a>    } <span class="c2html-keyword">else</span> <span class="c2html-keyword">if</span> (piece != <span class="c2html-macroconst">KING</span>) {
   8.153 -<a class="c2html-lineno" name="l101" href="#l101">101 </a>        Move threats[<span class="c2html-macroconst">16</span>];
   8.154 -<a class="c2html-lineno" name="l102" href="#l102">102 </a>        <span class="c2html-type">uint8_t</span> threatcount;
   8.155 -<a class="c2html-lineno" name="l103" href="#l103">103 </a>        get_real_threats(gamestate, move-&gt;torow, move-&gt;tofile,
   8.156 -<a class="c2html-lineno" name="l104" href="#l104">104 </a>            move-&gt;piece&<span class="c2html-macroconst">COLOR_MASK</span>, threats, &threatcount);
   8.157 -<a class="c2html-lineno" name="l105" href="#l105">105 </a>        <span class="c2html-keyword">if</span> (threatcount &gt; <span class="c2html-macroconst">1</span>) {
   8.158 -<a class="c2html-lineno" name="l106" href="#l106">106 </a>            <span class="c2html-keyword">int</span> ambrows = <span class="c2html-macroconst">0</span>, ambfiles = <span class="c2html-macroconst">0</span>;
   8.159 -<a class="c2html-lineno" name="l107" href="#l107">107 </a>            <span class="c2html-keyword">for</span> (<span class="c2html-type">uint8_t</span> i = <span class="c2html-macroconst">0</span> ; i &lt; threatcount ; i++) {
   8.160 -<a class="c2html-lineno" name="l108" href="#l108">108 </a>                <span class="c2html-keyword">if</span> (threats[i].fromrow == move-&gt;fromrow) {
   8.161 -<a class="c2html-lineno" name="l109" href="#l109">109 </a>                    ambrows++;
   8.162 -<a class="c2html-lineno" name="l110" href="#l110">110 </a>                }
   8.163 -<a class="c2html-lineno" name="l111" href="#l111">111 </a>                <span class="c2html-keyword">if</span> (threats[i].fromfile == move-&gt;fromfile) {
   8.164 -<a class="c2html-lineno" name="l112" href="#l112">112 </a>                    ambfiles++;
   8.165 -<a class="c2html-lineno" name="l113" href="#l113">113 </a>                }
   8.166 -<a class="c2html-lineno" name="l114" href="#l114">114 </a>            }
   8.167 -<a class="c2html-lineno" name="l115" href="#l115">115 </a>            <span class="c2html-comment">/* ambiguous row, name file */</span>
   8.168 -<a class="c2html-lineno" name="l116" href="#l116">116 </a>            <span class="c2html-keyword">if</span> (ambrows &gt; <span class="c2html-macroconst">1</span>) {
   8.169 -<a class="c2html-lineno" name="l117" href="#l117">117 </a>                string[idx++] = filechr(move-&gt;fromfile);
   8.170 -<a class="c2html-lineno" name="l118" href="#l118">118 </a>            }
   8.171 -<a class="c2html-lineno" name="l119" href="#l119">119 </a>            <span class="c2html-comment">/* ambiguous file, name row */</span>
   8.172 -<a class="c2html-lineno" name="l120" href="#l120">120 </a>            <span class="c2html-keyword">if</span> (ambfiles &gt; <span class="c2html-macroconst">1</span>) {
   8.173 -<a class="c2html-lineno" name="l121" href="#l121">121 </a>                string[idx++] = filechr(move-&gt;fromrow);
   8.174 -<a class="c2html-lineno" name="l122" href="#l122">122 </a>            }
   8.175 -<a class="c2html-lineno" name="l123" href="#l123">123 </a>        }
   8.176 -<a class="c2html-lineno" name="l124" href="#l124">124 </a>    }
   8.177 -<a class="c2html-lineno" name="l125" href="#l125">125 </a>    
   8.178 -<a class="c2html-lineno" name="l126" href="#l126">126 </a>    <span class="c2html-comment">/* capturing? */</span>
   8.179 -<a class="c2html-lineno" name="l127" href="#l127">127 </a>    <span class="c2html-keyword">if</span> (move-&gt;capture) {
   8.180 -<a class="c2html-lineno" name="l128" href="#l128">128 </a>        string[idx++] = <span class="c2html-string">'x'</span>;
   8.181 -<a class="c2html-lineno" name="l129" href="#l129">129 </a>    }
   8.182 -<a class="c2html-lineno" name="l130" href="#l130">130 </a>    
   8.183 -<a class="c2html-lineno" name="l131" href="#l131">131 </a>    <span class="c2html-comment">/* destination */</span>
   8.184 -<a class="c2html-lineno" name="l132" href="#l132">132 </a>    string[idx++] = filechr(move-&gt;tofile);
   8.185 -<a class="c2html-lineno" name="l133" href="#l133">133 </a>    string[idx++] = rowchr(move-&gt;torow);
   8.186 -<a class="c2html-lineno" name="l134" href="#l134">134 </a>    
   8.187 -<a class="c2html-lineno" name="l135" href="#l135">135 </a>    <span class="c2html-comment">/* promotion? */</span>
   8.188 -<a class="c2html-lineno" name="l136" href="#l136">136 </a>    <span class="c2html-keyword">if</span> (move-&gt;promotion) {
   8.189 -<a class="c2html-lineno" name="l137" href="#l137">137 </a>        string[idx++] = <span class="c2html-string">'='</span>;
   8.190 -<a class="c2html-lineno" name="l138" href="#l138">138 </a>        string[idx++] = getpiecechr(move-&gt;promotion);
   8.191 -<a class="c2html-lineno" name="l139" href="#l139">139 </a>    }
   8.192 -<a class="c2html-lineno" name="l140" href="#l140">140 </a>    
   8.193 -<a class="c2html-lineno" name="l141" href="#l141">141 </a>    <span class="c2html-comment">/* check? */</span>
   8.194 -<a class="c2html-lineno" name="l142" href="#l142">142 </a>    <span class="c2html-keyword">if</span> (move-&gt;check) {
   8.195 -<a class="c2html-lineno" name="l143" href="#l143">143 </a>        <span class="c2html-comment">/* works only, if this function is called when applying the move */</span>
   8.196 -<a class="c2html-lineno" name="l144" href="#l144">144 </a>        string[idx++] = gamestate-&gt;checkmate?<span class="c2html-string">'#'</span>:<span class="c2html-string">'+'</span>;
   8.197 -<a class="c2html-lineno" name="l145" href="#l145">145 </a>    }
   8.198 -<a class="c2html-lineno" name="l146" href="#l146">146 </a>}
   8.199 -<a class="c2html-lineno" name="l147" href="#l147">147 </a>
   8.200 -<a class="c2html-lineno" name="l148" href="#l148">148 </a><span class="c2html-keyword">static</span> <span class="c2html-keyword">void</span> addmove(GameState* gamestate, Move *move) {
   8.201 -<a class="c2html-lineno" name="l149" href="#l149">149 </a>    MoveList *elem = malloc(<span class="c2html-keyword">sizeof</span>(MoveList));
   8.202 -<a class="c2html-lineno" name="l150" href="#l150">150 </a>    elem-&gt;next = <span class="c2html-macroconst">NULL</span>;
   8.203 -<a class="c2html-lineno" name="l151" href="#l151">151 </a>    elem-&gt;move = *move;
   8.204 -<a class="c2html-lineno" name="l152" href="#l152">152 </a>    
   8.205 -<a class="c2html-lineno" name="l153" href="#l153">153 </a>    <span class="c2html-keyword">struct</span> timeval curtimestamp;
   8.206 -<a class="c2html-lineno" name="l154" href="#l154">154 </a>    gettimeofday(&curtimestamp, <span class="c2html-macroconst">NULL</span>);
   8.207 -<a class="c2html-lineno" name="l155" href="#l155">155 </a>    elem-&gt;move.timestamp.tv_sec = curtimestamp.tv_sec;
   8.208 -<a class="c2html-lineno" name="l156" href="#l156">156 </a>    elem-&gt;move.timestamp.tv_usec = curtimestamp.tv_usec;
   8.209 -<a class="c2html-lineno" name="l157" href="#l157">157 </a>    
   8.210 -<a class="c2html-lineno" name="l158" href="#l158">158 </a>    <span class="c2html-keyword">if</span> (gamestate-&gt;lastmove) {
   8.211 -<a class="c2html-lineno" name="l159" href="#l159">159 </a>        <span class="c2html-keyword">struct</span> movetimeval *lasttstamp = &(gamestate-&gt;lastmove-&gt;move.timestamp);
   8.212 -<a class="c2html-lineno" name="l160" href="#l160">160 </a>        <span class="c2html-type">uint64_t</span> sec = curtimestamp.tv_sec - lasttstamp-&gt;tv_sec;
   8.213 -<a class="c2html-lineno" name="l161" href="#l161">161 </a>        <span class="c2html-type">suseconds_t</span> micros;
   8.214 -<a class="c2html-lineno" name="l162" href="#l162">162 </a>        <span class="c2html-keyword">if</span> (curtimestamp.tv_usec &lt; lasttstamp-&gt;tv_usec) {
   8.215 -<a class="c2html-lineno" name="l163" href="#l163">163 </a>            micros = 1e6L-(lasttstamp-&gt;tv_usec - curtimestamp.tv_usec);
   8.216 -<a class="c2html-lineno" name="l164" href="#l164">164 </a>            sec--;
   8.217 -<a class="c2html-lineno" name="l165" href="#l165">165 </a>        } <span class="c2html-keyword">else</span> {
   8.218 -<a class="c2html-lineno" name="l166" href="#l166">166 </a>            micros = curtimestamp.tv_usec - lasttstamp-&gt;tv_usec;
   8.219 -<a class="c2html-lineno" name="l167" href="#l167">167 </a>        }
   8.220 -<a class="c2html-lineno" name="l168" href="#l168">168 </a>        
   8.221 -<a class="c2html-lineno" name="l169" href="#l169">169 </a>        elem-&gt;move.movetime.tv_sec = sec;
   8.222 -<a class="c2html-lineno" name="l170" href="#l170">170 </a>        elem-&gt;move.movetime.tv_usec = micros;
   8.223 -<a class="c2html-lineno" name="l171" href="#l171">171 </a>        
   8.224 -<a class="c2html-lineno" name="l172" href="#l172">172 </a>        gamestate-&gt;lastmove-&gt;next = elem;
   8.225 -<a class="c2html-lineno" name="l173" href="#l173">173 </a>        gamestate-&gt;lastmove = elem;
   8.226 -<a class="c2html-lineno" name="l174" href="#l174">174 </a>    } <span class="c2html-keyword">else</span> {
   8.227 -<a class="c2html-lineno" name="l175" href="#l175">175 </a>        elem-&gt;move.movetime.tv_usec = <span class="c2html-macroconst">0</span>;
   8.228 -<a class="c2html-lineno" name="l176" href="#l176">176 </a>        elem-&gt;move.movetime.tv_sec = <span class="c2html-macroconst">0</span>;
   8.229 -<a class="c2html-lineno" name="l177" href="#l177">177 </a>        gamestate-&gt;movelist = gamestate-&gt;lastmove = elem;
   8.230 -<a class="c2html-lineno" name="l178" href="#l178">178 </a>    }
   8.231 -<a class="c2html-lineno" name="l179" href="#l179">179 </a>}
   8.232 -<a class="c2html-lineno" name="l180" href="#l180">180 </a>
   8.233 -<a class="c2html-lineno" name="l181" href="#l181">181 </a><span class="c2html-keyword">char</span> getpiecechr(<span class="c2html-type">uint8_t</span> piece) {
   8.234 -<a class="c2html-lineno" name="l182" href="#l182">182 </a>    <span class="c2html-keyword">switch</span> (piece & <span class="c2html-macroconst">PIECE_MASK</span>) {
   8.235 -<a class="c2html-lineno" name="l183" href="#l183">183 </a>    <span class="c2html-keyword">case</span> <span class="c2html-macroconst">ROOK</span>: <span class="c2html-keyword">return</span> <span class="c2html-string">'R'</span>;
   8.236 -<a class="c2html-lineno" name="l184" href="#l184">184 </a>    <span class="c2html-keyword">case</span> <span class="c2html-macroconst">KNIGHT</span>: <span class="c2html-keyword">return</span> <span class="c2html-string">'N'</span>;
   8.237 -<a class="c2html-lineno" name="l185" href="#l185">185 </a>    <span class="c2html-keyword">case</span> <span class="c2html-macroconst">BISHOP</span>: <span class="c2html-keyword">return</span> <span class="c2html-string">'B'</span>;
   8.238 -<a class="c2html-lineno" name="l186" href="#l186">186 </a>    <span class="c2html-keyword">case</span> <span class="c2html-macroconst">QUEEN</span>: <span class="c2html-keyword">return</span> <span class="c2html-string">'Q'</span>;
   8.239 -<a class="c2html-lineno" name="l187" href="#l187">187 </a>    <span class="c2html-keyword">case</span> <span class="c2html-macroconst">KING</span>: <span class="c2html-keyword">return</span> <span class="c2html-string">'K'</span>;
   8.240 -<a class="c2html-lineno" name="l188" href="#l188">188 </a>    <span class="c2html-keyword">default</span>: <span class="c2html-keyword">return</span> <span class="c2html-string">'\0'</span>;
   8.241 -<a class="c2html-lineno" name="l189" href="#l189">189 </a>    }
   8.242 -<a class="c2html-lineno" name="l190" href="#l190">190 </a>}
   8.243 -<a class="c2html-lineno" name="l191" href="#l191">191 </a>
   8.244 -<a class="c2html-lineno" name="l192" href="#l192">192 </a><span class="c2html-type">uint8_t</span> getpiece(<span class="c2html-keyword">char</span> c) {
   8.245 -<a class="c2html-lineno" name="l193" href="#l193">193 </a>    <span class="c2html-keyword">switch</span> (c) {
   8.246 -<a class="c2html-lineno" name="l194" href="#l194">194 </a>        <span class="c2html-keyword">case</span> <span class="c2html-string">'R'</span>: <span class="c2html-keyword">return</span> <span class="c2html-macroconst">ROOK</span>;
   8.247 -<a class="c2html-lineno" name="l195" href="#l195">195 </a>        <span class="c2html-keyword">case</span> <span class="c2html-string">'N'</span>: <span class="c2html-keyword">return</span> <span class="c2html-macroconst">KNIGHT</span>;
   8.248 -<a class="c2html-lineno" name="l196" href="#l196">196 </a>        <span class="c2html-keyword">case</span> <span class="c2html-string">'B'</span>: <span class="c2html-keyword">return</span> <span class="c2html-macroconst">BISHOP</span>;
   8.249 -<a class="c2html-lineno" name="l197" href="#l197">197 </a>        <span class="c2html-keyword">case</span> <span class="c2html-string">'Q'</span>: <span class="c2html-keyword">return</span> <span class="c2html-macroconst">QUEEN</span>;
   8.250 -<a class="c2html-lineno" name="l198" href="#l198">198 </a>        <span class="c2html-keyword">case</span> <span class="c2html-string">'K'</span>: <span class="c2html-keyword">return</span> <span class="c2html-macroconst">KING</span>;
   8.251 -<a class="c2html-lineno" name="l199" href="#l199">199 </a>        <span class="c2html-keyword">default</span>: <span class="c2html-keyword">return</span> <span class="c2html-macroconst">0</span>;
   8.252 -<a class="c2html-lineno" name="l200" href="#l200">200 </a>    }
   8.253 -<a class="c2html-lineno" name="l201" href="#l201">201 </a>}
   8.254 -<a class="c2html-lineno" name="l202" href="#l202">202 </a>
   8.255 -<a class="c2html-lineno" name="l203" href="#l203">203 </a><span class="c2html-keyword">static</span> <span class="c2html-keyword">void</span> apply_move_impl(GameState *gamestate, Move *move, _Bool simulate) {
   8.256 -<a class="c2html-lineno" name="l204" href="#l204">204 </a>    <span class="c2html-type">uint8_t</span> piece = move-&gt;piece & <span class="c2html-macroconst">PIECE_MASK</span>;
   8.257 -<a class="c2html-lineno" name="l205" href="#l205">205 </a>    <span class="c2html-type">uint8_t</span> color = move-&gt;piece & <span class="c2html-macroconst">COLOR_MASK</span>;
   8.258 -<a class="c2html-lineno" name="l206" href="#l206">206 </a>    
   8.259 -<a class="c2html-lineno" name="l207" href="#l207">207 </a>    <span class="c2html-comment">/* en passant capture */</span>
   8.260 -<a class="c2html-lineno" name="l208" href="#l208">208 </a>    <span class="c2html-keyword">if</span> (move-&gt;capture && piece == <span class="c2html-macroconst">PAWN</span> &&
   8.261 -<a class="c2html-lineno" name="l209" href="#l209">209 </a>        mdst(gamestate-&gt;board, move) == <span class="c2html-macroconst">0</span>) {
   8.262 -<a class="c2html-lineno" name="l210" href="#l210">210 </a>        gamestate-&gt;board[move-&gt;fromrow][move-&gt;tofile] = <span class="c2html-macroconst">0</span>;
   8.263 -<a class="c2html-lineno" name="l211" href="#l211">211 </a>    }
   8.264 -<a class="c2html-lineno" name="l212" href="#l212">212 </a>    
   8.265 -<a class="c2html-lineno" name="l213" href="#l213">213 </a>    <span class="c2html-comment">/* remove old en passant threats */</span>
   8.266 -<a class="c2html-lineno" name="l214" href="#l214">214 </a>    <span class="c2html-keyword">for</span> (<span class="c2html-type">uint8_t</span> file = <span class="c2html-macroconst">0</span> ; file &lt; <span class="c2html-macroconst">8</span> ; file++) {
   8.267 -<a class="c2html-lineno" name="l215" href="#l215">215 </a>        gamestate-&gt;board[<span class="c2html-macroconst">3</span>][file] &= ~<span class="c2html-macroconst">ENPASSANT_THREAT</span>;
   8.268 -<a class="c2html-lineno" name="l216" href="#l216">216 </a>        gamestate-&gt;board[<span class="c2html-macroconst">4</span>][file] &= ~<span class="c2html-macroconst">ENPASSANT_THREAT</span>;
   8.269 -<a class="c2html-lineno" name="l217" href="#l217">217 </a>    }
   8.270 -<a class="c2html-lineno" name="l218" href="#l218">218 </a>    
   8.271 -<a class="c2html-lineno" name="l219" href="#l219">219 </a>    <span class="c2html-comment">/* add new en passant threat */</span>
   8.272 -<a class="c2html-lineno" name="l220" href="#l220">220 </a>    <span class="c2html-keyword">if</span> (piece == <span class="c2html-macroconst">PAWN</span> && (
   8.273 -<a class="c2html-lineno" name="l221" href="#l221">221 </a>        (move-&gt;fromrow == <span class="c2html-macroconst">1</span> && move-&gt;torow == <span class="c2html-macroconst">3</span>) ||
   8.274 -<a class="c2html-lineno" name="l222" href="#l222">222 </a>        (move-&gt;fromrow == <span class="c2html-macroconst">6</span> && move-&gt;torow == <span class="c2html-macroconst">4</span>))) {
   8.275 -<a class="c2html-lineno" name="l223" href="#l223">223 </a>        move-&gt;piece |= <span class="c2html-macroconst">ENPASSANT_THREAT</span>;
   8.276 -<a class="c2html-lineno" name="l224" href="#l224">224 </a>    }
   8.277 -<a class="c2html-lineno" name="l225" href="#l225">225 </a>    
   8.278 -<a class="c2html-lineno" name="l226" href="#l226">226 </a>    <span class="c2html-comment">/* move (and maybe capture or promote) */</span>
   8.279 -<a class="c2html-lineno" name="l227" href="#l227">227 </a>    msrc(gamestate-&gt;board, move) = <span class="c2html-macroconst">0</span>;
   8.280 -<a class="c2html-lineno" name="l228" href="#l228">228 </a>    <span class="c2html-keyword">if</span> (move-&gt;promotion) {
   8.281 -<a class="c2html-lineno" name="l229" href="#l229">229 </a>        mdst(gamestate-&gt;board, move) = move-&gt;promotion;
   8.282 -<a class="c2html-lineno" name="l230" href="#l230">230 </a>    } <span class="c2html-keyword">else</span> {
   8.283 -<a class="c2html-lineno" name="l231" href="#l231">231 </a>        mdst(gamestate-&gt;board, move) = move-&gt;piece;
   8.284 -<a class="c2html-lineno" name="l232" href="#l232">232 </a>    }
   8.285 -<a class="c2html-lineno" name="l233" href="#l233">233 </a>    
   8.286 -<a class="c2html-lineno" name="l234" href="#l234">234 </a>    <span class="c2html-comment">/* castling */</span>
   8.287 -<a class="c2html-lineno" name="l235" href="#l235">235 </a>    <span class="c2html-keyword">if</span> (piece == <span class="c2html-macroconst">KING</span> && move-&gt;fromfile == fileidx(<span class="c2html-string">'e'</span>)) {
   8.288 -<a class="c2html-lineno" name="l236" href="#l236">236 </a>        
   8.289 -<a class="c2html-lineno" name="l237" href="#l237">237 </a>        <span class="c2html-keyword">if</span> (move-&gt;tofile == fileidx(<span class="c2html-string">'g'</span>)) {
   8.290 -<a class="c2html-lineno" name="l238" href="#l238">238 </a>            gamestate-&gt;board[move-&gt;torow][fileidx(<span class="c2html-string">'h'</span>)] = <span class="c2html-macroconst">0</span>;
   8.291 -<a class="c2html-lineno" name="l239" href="#l239">239 </a>            gamestate-&gt;board[move-&gt;torow][fileidx(<span class="c2html-string">'f'</span>)] = color|<span class="c2html-macroconst">ROOK</span>;
   8.292 -<a class="c2html-lineno" name="l240" href="#l240">240 </a>        } <span class="c2html-keyword">else</span> <span class="c2html-keyword">if</span> (move-&gt;tofile == fileidx(<span class="c2html-string">'c'</span>)) {
   8.293 -<a class="c2html-lineno" name="l241" href="#l241">241 </a>            gamestate-&gt;board[move-&gt;torow][fileidx(<span class="c2html-string">'a'</span>)] = <span class="c2html-macroconst">0</span>;
   8.294 -<a class="c2html-lineno" name="l242" href="#l242">242 </a>            gamestate-&gt;board[move-&gt;torow][fileidx(<span class="c2html-string">'d'</span>)] = color|<span class="c2html-macroconst">ROOK</span>;
   8.295 -<a class="c2html-lineno" name="l243" href="#l243">243 </a>        }
   8.296 -<a class="c2html-lineno" name="l244" href="#l244">244 </a>    }
   8.297 -<a class="c2html-lineno" name="l245" href="#l245">245 </a>
   8.298 -<a class="c2html-lineno" name="l246" href="#l246">246 </a>    <span class="c2html-keyword">if</span> (!simulate) {
   8.299 -<a class="c2html-lineno" name="l247" href="#l247">247 </a>        <span class="c2html-keyword">if</span> (!move-&gt;string[<span class="c2html-macroconst">0</span>]) {
   8.300 -<a class="c2html-lineno" name="l248" href="#l248">248 </a>            format_move(gamestate, move);
   8.301 -<a class="c2html-lineno" name="l249" href="#l249">249 </a>        }
   8.302 -<a class="c2html-lineno" name="l250" href="#l250">250 </a>    }
   8.303 -<a class="c2html-lineno" name="l251" href="#l251">251 </a>    <span class="c2html-comment">/* add move, even in simulation (checkmate test needs it) */</span>
   8.304 -<a class="c2html-lineno" name="l252" href="#l252">252 </a>    addmove(gamestate, move);
   8.305 -<a class="c2html-lineno" name="l253" href="#l253">253 </a>}
   8.306 -<a class="c2html-lineno" name="l254" href="#l254">254 </a>
   8.307 -<a class="c2html-lineno" name="l255" href="#l255">255 </a><span class="c2html-keyword">void</span> apply_move(GameState *gamestate, Move *move) {
   8.308 -<a class="c2html-lineno" name="l256" href="#l256">256 </a>    apply_move_impl(gamestate, move, <span class="c2html-macroconst">0</span>);
   8.309 -<a class="c2html-lineno" name="l257" href="#l257">257 </a>}
   8.310 -<a class="c2html-lineno" name="l258" href="#l258">258 </a>
   8.311 -<a class="c2html-lineno" name="l259" href="#l259">259 </a><span class="c2html-keyword">static</span> <span class="c2html-keyword">int</span> validate_move_rules(GameState *gamestate, Move *move) {
   8.312 -<a class="c2html-lineno" name="l260" href="#l260">260 </a>    <span class="c2html-comment">/* validate indices (don't trust opponent) */</span>
   8.313 -<a class="c2html-lineno" name="l261" href="#l261">261 </a>    <span class="c2html-keyword">if</span> (!chkidx(move)) {
   8.314 -<a class="c2html-lineno" name="l262" href="#l262">262 </a>        <span class="c2html-keyword">return</span> <span class="c2html-macroconst">INVALID_POSITION</span>;
   8.315 -<a class="c2html-lineno" name="l263" href="#l263">263 </a>    }
   8.316 -<a class="c2html-lineno" name="l264" href="#l264">264 </a>    
   8.317 -<a class="c2html-lineno" name="l265" href="#l265">265 </a>    <span class="c2html-comment">/* must move */</span>
   8.318 -<a class="c2html-lineno" name="l266" href="#l266">266 </a>    <span class="c2html-keyword">if</span> (move-&gt;fromfile == move-&gt;tofile && move-&gt;fromrow == move-&gt;torow) {
   8.319 -<a class="c2html-lineno" name="l267" href="#l267">267 </a>        <span class="c2html-keyword">return</span> <span class="c2html-macroconst">INVALID_MOVE_SYNTAX</span>;
   8.320 -<a class="c2html-lineno" name="l268" href="#l268">268 </a>    }
   8.321 -<a class="c2html-lineno" name="l269" href="#l269">269 </a>    
   8.322 -<a class="c2html-lineno" name="l270" href="#l270">270 </a>    <span class="c2html-comment">/* does piece exist */</span>
   8.323 -<a class="c2html-lineno" name="l271" href="#l271">271 </a>    <span class="c2html-keyword">if</span> ((msrc(gamestate-&gt;board, move)&(<span class="c2html-macroconst">PIECE_MASK</span>|<span class="c2html-macroconst">COLOR_MASK</span>))
   8.324 -<a class="c2html-lineno" name="l272" href="#l272">272 </a>           != (move-&gt;piece&(<span class="c2html-macroconst">PIECE_MASK</span>|<span class="c2html-macroconst">COLOR_MASK</span>))) {
   8.325 -<a class="c2html-lineno" name="l273" href="#l273">273 </a>        <span class="c2html-keyword">return</span> <span class="c2html-macroconst">INVALID_POSITION</span>;
   8.326 -<a class="c2html-lineno" name="l274" href="#l274">274 </a>    }
   8.327 -<a class="c2html-lineno" name="l275" href="#l275">275 </a>    
   8.328 -<a class="c2html-lineno" name="l276" href="#l276">276 </a>    <span class="c2html-comment">/* can't capture own pieces */</span>
   8.329 -<a class="c2html-lineno" name="l277" href="#l277">277 </a>    <span class="c2html-keyword">if</span> ((mdst(gamestate-&gt;board, move) & <span class="c2html-macroconst">COLOR_MASK</span>)
   8.330 -<a class="c2html-lineno" name="l278" href="#l278">278 </a>            == (move-&gt;piece & <span class="c2html-macroconst">COLOR_MASK</span>)) {
   8.331 -<a class="c2html-lineno" name="l279" href="#l279">279 </a>        <span class="c2html-keyword">return</span> <span class="c2html-macroconst">RULES_VIOLATED</span>;
   8.332 -<a class="c2html-lineno" name="l280" href="#l280">280 </a>    }
   8.333 -<a class="c2html-lineno" name="l281" href="#l281">281 </a>    
   8.334 -<a class="c2html-lineno" name="l282" href="#l282">282 </a>    <span class="c2html-comment">/* must capture, if and only if destination is occupied */</span>
   8.335 -<a class="c2html-lineno" name="l283" href="#l283">283 </a>    <span class="c2html-keyword">if</span> ((mdst(gamestate-&gt;board, move) == <span class="c2html-macroconst">0</span> && move-&gt;capture) ||
   8.336 -<a class="c2html-lineno" name="l284" href="#l284">284 </a>            (mdst(gamestate-&gt;board, move) != <span class="c2html-macroconst">0</span> && !move-&gt;capture)) {
   8.337 -<a class="c2html-lineno" name="l285" href="#l285">285 </a>        <span class="c2html-keyword">return</span> <span class="c2html-macroconst">INVALID_MOVE_SYNTAX</span>;
   8.338 -<a class="c2html-lineno" name="l286" href="#l286">286 </a>    }
   8.339 -<a class="c2html-lineno" name="l287" href="#l287">287 </a>    
   8.340 -<a class="c2html-lineno" name="l288" href="#l288">288 </a>    <span class="c2html-comment">/* validate individual rules */</span>
   8.341 -<a class="c2html-lineno" name="l289" href="#l289">289 </a>    _Bool chkrules;
   8.342 -<a class="c2html-lineno" name="l290" href="#l290">290 </a>    <span class="c2html-keyword">switch</span> (move-&gt;piece & <span class="c2html-macroconst">PIECE_MASK</span>) {
   8.343 -<a class="c2html-lineno" name="l291" href="#l291">291 </a>    <span class="c2html-keyword">case</span> <span class="c2html-macroconst">PAWN</span>:
   8.344 -<a class="c2html-lineno" name="l292" href="#l292">292 </a>        chkrules = pawn_chkrules(gamestate, move) &&
   8.345 -<a class="c2html-lineno" name="l293" href="#l293">293 </a>            !pawn_isblocked(gamestate, move);
   8.346 -<a class="c2html-lineno" name="l294" href="#l294">294 </a>        <span class="c2html-keyword">break</span>;
   8.347 -<a class="c2html-lineno" name="l295" href="#l295">295 </a>    <span class="c2html-keyword">case</span> <span class="c2html-macroconst">ROOK</span>:
   8.348 -<a class="c2html-lineno" name="l296" href="#l296">296 </a>        chkrules = rook_chkrules(move) &&
   8.349 -<a class="c2html-lineno" name="l297" href="#l297">297 </a>            !rook_isblocked(gamestate, move);
   8.350 -<a class="c2html-lineno" name="l298" href="#l298">298 </a>        <span class="c2html-keyword">break</span>;
   8.351 -<a class="c2html-lineno" name="l299" href="#l299">299 </a>    <span class="c2html-keyword">case</span> <span class="c2html-macroconst">KNIGHT</span>:
   8.352 -<a class="c2html-lineno" name="l300" href="#l300">300 </a>        chkrules = knight_chkrules(move); <span class="c2html-comment">/* knight is never blocked */</span>
   8.353 -<a class="c2html-lineno" name="l301" href="#l301">301 </a>        <span class="c2html-keyword">break</span>;
   8.354 -<a class="c2html-lineno" name="l302" href="#l302">302 </a>    <span class="c2html-keyword">case</span> <span class="c2html-macroconst">BISHOP</span>:
   8.355 -<a class="c2html-lineno" name="l303" href="#l303">303 </a>        chkrules = bishop_chkrules(move) &&
   8.356 -<a class="c2html-lineno" name="l304" href="#l304">304 </a>            !bishop_isblocked(gamestate, move);
   8.357 -<a class="c2html-lineno" name="l305" href="#l305">305 </a>        <span class="c2html-keyword">break</span>;
   8.358 -<a class="c2html-lineno" name="l306" href="#l306">306 </a>    <span class="c2html-keyword">case</span> <span class="c2html-macroconst">QUEEN</span>:
   8.359 -<a class="c2html-lineno" name="l307" href="#l307">307 </a>        chkrules = queen_chkrules(move) &&
   8.360 -<a class="c2html-lineno" name="l308" href="#l308">308 </a>            !queen_isblocked(gamestate, move);
   8.361 -<a class="c2html-lineno" name="l309" href="#l309">309 </a>        <span class="c2html-keyword">break</span>;
   8.362 -<a class="c2html-lineno" name="l310" href="#l310">310 </a>    <span class="c2html-keyword">case</span> <span class="c2html-macroconst">KING</span>:
   8.363 -<a class="c2html-lineno" name="l311" href="#l311">311 </a>        chkrules = king_chkrules(gamestate, move) &&
   8.364 -<a class="c2html-lineno" name="l312" href="#l312">312 </a>            !king_isblocked(gamestate, move);
   8.365 -<a class="c2html-lineno" name="l313" href="#l313">313 </a>        <span class="c2html-keyword">break</span>;
   8.366 -<a class="c2html-lineno" name="l314" href="#l314">314 </a>    <span class="c2html-keyword">default</span>:
   8.367 -<a class="c2html-lineno" name="l315" href="#l315">315 </a>        <span class="c2html-keyword">return</span> <span class="c2html-macroconst">INVALID_MOVE_SYNTAX</span>;
   8.368 -<a class="c2html-lineno" name="l316" href="#l316">316 </a>    }
   8.369 -<a class="c2html-lineno" name="l317" href="#l317">317 </a>    
   8.370 -<a class="c2html-lineno" name="l318" href="#l318">318 </a>    <span class="c2html-keyword">return</span> chkrules ? <span class="c2html-macroconst">VALID_MOVE_SEMANTICS</span> : <span class="c2html-macroconst">RULES_VIOLATED</span>;
   8.371 -<a class="c2html-lineno" name="l319" href="#l319">319 </a>}
   8.372 -<a class="c2html-lineno" name="l320" href="#l320">320 </a>
   8.373 -<a class="c2html-lineno" name="l321" href="#l321">321 </a><span class="c2html-keyword">int</span> validate_move(GameState *gamestate, Move *move) {
   8.374 -<a class="c2html-lineno" name="l322" href="#l322">322 </a>    
   8.375 -<a class="c2html-lineno" name="l323" href="#l323">323 </a>    <span class="c2html-keyword">int</span> result = validate_move_rules(gamestate, move);
   8.376 -<a class="c2html-lineno" name="l324" href="#l324">324 </a>    
   8.377 -<a class="c2html-lineno" name="l325" href="#l325">325 </a>    <span class="c2html-comment">/* cancel processing to save resources */</span>
   8.378 -<a class="c2html-lineno" name="l326" href="#l326">326 </a>    <span class="c2html-keyword">if</span> (result != <span class="c2html-macroconst">VALID_MOVE_SEMANTICS</span>) {
   8.379 -<a class="c2html-lineno" name="l327" href="#l327">327 </a>        <span class="c2html-keyword">return</span> result;
   8.380 -<a class="c2html-lineno" name="l328" href="#l328">328 </a>    }
   8.381 -<a class="c2html-lineno" name="l329" href="#l329">329 </a>    
   8.382 -<a class="c2html-lineno" name="l330" href="#l330">330 </a>    <span class="c2html-comment">/* find kings for check validation */</span>
   8.383 -<a class="c2html-lineno" name="l331" href="#l331">331 </a>    <span class="c2html-type">uint8_t</span> piececolor = (move-&gt;piece & <span class="c2html-macroconst">COLOR_MASK</span>);
   8.384 -<a class="c2html-lineno" name="l332" href="#l332">332 </a>    
   8.385 -<a class="c2html-lineno" name="l333" href="#l333">333 </a>    <span class="c2html-type">uint8_t</span> mykingfile = <span class="c2html-macroconst">0</span>, mykingrow = <span class="c2html-macroconst">0</span>, opkingfile = <span class="c2html-macroconst">0</span>, opkingrow = <span class="c2html-macroconst">0</span>;
   8.386 -<a class="c2html-lineno" name="l334" href="#l334">334 </a>    <span class="c2html-keyword">for</span> (<span class="c2html-type">uint8_t</span> row = <span class="c2html-macroconst">0</span> ; row &lt; <span class="c2html-macroconst">8</span> ; row++) {
   8.387 -<a class="c2html-lineno" name="l335" href="#l335">335 </a>        <span class="c2html-keyword">for</span> (<span class="c2html-type">uint8_t</span> file = <span class="c2html-macroconst">0</span> ; file &lt; <span class="c2html-macroconst">8</span> ; file++) {
   8.388 -<a class="c2html-lineno" name="l336" href="#l336">336 </a>            <span class="c2html-keyword">if</span> (gamestate-&gt;board[row][file] ==
   8.389 -<a class="c2html-lineno" name="l337" href="#l337">337 </a>                    (piececolor == <span class="c2html-macroconst">WHITE</span>?<span class="c2html-macroconst">WKING</span>:<span class="c2html-macroconst">BKING</span>)) {
   8.390 -<a class="c2html-lineno" name="l338" href="#l338">338 </a>                mykingfile = file;
   8.391 -<a class="c2html-lineno" name="l339" href="#l339">339 </a>                mykingrow = row;
   8.392 -<a class="c2html-lineno" name="l340" href="#l340">340 </a>            } <span class="c2html-keyword">else</span> <span class="c2html-keyword">if</span> (gamestate-&gt;board[row][file] ==
   8.393 -<a class="c2html-lineno" name="l341" href="#l341">341 </a>                    (piececolor == <span class="c2html-macroconst">WHITE</span>?<span class="c2html-macroconst">BKING</span>:<span class="c2html-macroconst">WKING</span>)) {
   8.394 -<a class="c2html-lineno" name="l342" href="#l342">342 </a>                opkingfile = file;
   8.395 -<a class="c2html-lineno" name="l343" href="#l343">343 </a>                opkingrow = row;
   8.396 -<a class="c2html-lineno" name="l344" href="#l344">344 </a>            }
   8.397 -<a class="c2html-lineno" name="l345" href="#l345">345 </a>        }
   8.398 -<a class="c2html-lineno" name="l346" href="#l346">346 </a>    }
   8.399 -<a class="c2html-lineno" name="l347" href="#l347">347 </a>    
   8.400 -<a class="c2html-lineno" name="l348" href="#l348">348 </a>    <span class="c2html-comment">/* simulate move for check validation */</span>
   8.401 -<a class="c2html-lineno" name="l349" href="#l349">349 </a>    GameState simulation = gamestate_copy_sim(gamestate);
   8.402 -<a class="c2html-lineno" name="l350" href="#l350">350 </a>    Move simmove = *move;
   8.403 -<a class="c2html-lineno" name="l351" href="#l351">351 </a>    apply_move_impl(&simulation, &simmove, <span class="c2html-macroconst">1</span>);
   8.404 -<a class="c2html-lineno" name="l352" href="#l352">352 </a>    
   8.405 -<a class="c2html-lineno" name="l353" href="#l353">353 </a>    <span class="c2html-comment">/* don't move into or stay in check position */</span>
   8.406 -<a class="c2html-lineno" name="l354" href="#l354">354 </a>    <span class="c2html-keyword">if</span> (is_covered(&simulation, mykingrow, mykingfile,
   8.407 -<a class="c2html-lineno" name="l355" href="#l355">355 </a>        opponent_color(piececolor))) {
   8.408 -<a class="c2html-lineno" name="l356" href="#l356">356 </a>        
   8.409 -<a class="c2html-lineno" name="l357" href="#l357">357 </a>        gamestate_cleanup(&simulation);
   8.410 -<a class="c2html-lineno" name="l358" href="#l358">358 </a>        <span class="c2html-keyword">if</span> ((move-&gt;piece & <span class="c2html-macroconst">PIECE_MASK</span>) == <span class="c2html-macroconst">KING</span>) {
   8.411 -<a class="c2html-lineno" name="l359" href="#l359">359 </a>            <span class="c2html-keyword">return</span> <span class="c2html-macroconst">KING_MOVES_INTO_CHECK</span>;
   8.412 -<a class="c2html-lineno" name="l360" href="#l360">360 </a>        } <span class="c2html-keyword">else</span> {
   8.413 -<a class="c2html-lineno" name="l361" href="#l361">361 </a>            <span class="c2html-comment">/* last move is always not null in this case */</span>
   8.414 -<a class="c2html-lineno" name="l362" href="#l362">362 </a>            <span class="c2html-keyword">return</span> gamestate-&gt;lastmove-&gt;move.check ?
   8.415 -<a class="c2html-lineno" name="l363" href="#l363">363 </a>                <span class="c2html-macroconst">KING_IN_CHECK</span> : <span class="c2html-macroconst">PIECE_PINNED</span>;
   8.416 -<a class="c2html-lineno" name="l364" href="#l364">364 </a>        }
   8.417 -<a class="c2html-lineno" name="l365" href="#l365">365 </a>    }
   8.418 -<a class="c2html-lineno" name="l366" href="#l366">366 </a>    
   8.419 -<a class="c2html-lineno" name="l367" href="#l367">367 </a>    <span class="c2html-comment">/* correct check and checkmate flags (move is still valid) */</span>
   8.420 -<a class="c2html-lineno" name="l368" href="#l368">368 </a>    Move threats[<span class="c2html-macroconst">16</span>];
   8.421 -<a class="c2html-lineno" name="l369" href="#l369">369 </a>    <span class="c2html-type">uint8_t</span> threatcount;
   8.422 -<a class="c2html-lineno" name="l370" href="#l370">370 </a>    move-&gt;check = get_threats(&simulation, opkingrow, opkingfile,
   8.423 -<a class="c2html-lineno" name="l371" href="#l371">371 </a>        piececolor, threats, &threatcount);
   8.424 -<a class="c2html-lineno" name="l372" href="#l372">372 </a>    
   8.425 -<a class="c2html-lineno" name="l373" href="#l373">373 </a>    <span class="c2html-keyword">if</span> (move-&gt;check) {
   8.426 -<a class="c2html-lineno" name="l374" href="#l374">374 </a>        <span class="c2html-comment">/* determine possible escape fields */</span>
   8.427 -<a class="c2html-lineno" name="l375" href="#l375">375 </a>        _Bool canescape = <span class="c2html-macroconst">0</span>;
   8.428 -<a class="c2html-lineno" name="l376" href="#l376">376 </a>        <span class="c2html-keyword">for</span> (<span class="c2html-keyword">int</span> dr = -<span class="c2html-macroconst">1</span> ; dr &lt;= <span class="c2html-macroconst">1</span> && !canescape ; dr++) {
   8.429 -<a class="c2html-lineno" name="l377" href="#l377">377 </a>            <span class="c2html-keyword">for</span> (<span class="c2html-keyword">int</span> df = -<span class="c2html-macroconst">1</span> ; df &lt;= <span class="c2html-macroconst">1</span> && !canescape ; df++) {
   8.430 -<a class="c2html-lineno" name="l378" href="#l378">378 </a>                <span class="c2html-keyword">if</span> (!(dr == <span class="c2html-macroconst">0</span> && df == <span class="c2html-macroconst">0</span>)  &&
   8.431 -<a class="c2html-lineno" name="l379" href="#l379">379 </a>                        isidx(opkingrow + dr) && isidx(opkingfile + df)) {
   8.432 -<a class="c2html-lineno" name="l380" href="#l380">380 </a>                    
   8.433 -<a class="c2html-lineno" name="l381" href="#l381">381 </a>                    <span class="c2html-comment">/* escape field neither blocked nor covered */</span>
   8.434 -<a class="c2html-lineno" name="l382" href="#l382">382 </a>                    <span class="c2html-keyword">if</span> ((simulation.board[opkingrow + dr][opkingfile + df]
   8.435 -<a class="c2html-lineno" name="l383" href="#l383">383 </a>                            & <span class="c2html-macroconst">COLOR_MASK</span>) != opponent_color(piececolor)) {
   8.436 -<a class="c2html-lineno" name="l384" href="#l384">384 </a>                        canescape |= !is_covered(&simulation,
   8.437 -<a class="c2html-lineno" name="l385" href="#l385">385 </a>                            opkingrow + dr, opkingfile + df, piececolor);
   8.438 -<a class="c2html-lineno" name="l386" href="#l386">386 </a>                    }
   8.439 -<a class="c2html-lineno" name="l387" href="#l387">387 </a>                }
   8.440 -<a class="c2html-lineno" name="l388" href="#l388">388 </a>            }
   8.441 -<a class="c2html-lineno" name="l389" href="#l389">389 </a>        }
   8.442 -<a class="c2html-lineno" name="l390" href="#l390">390 </a>        <span class="c2html-comment">/* can't escape, can he capture? */</span>
   8.443 -<a class="c2html-lineno" name="l391" href="#l391">391 </a>        <span class="c2html-keyword">if</span> (!canescape && threatcount == <span class="c2html-macroconst">1</span>) {
   8.444 -<a class="c2html-lineno" name="l392" href="#l392">392 </a>            canescape = is_attacked(&simulation, threats[<span class="c2html-macroconst">0</span>].fromrow,
   8.445 -<a class="c2html-lineno" name="l393" href="#l393">393 </a>                threats[<span class="c2html-macroconst">0</span>].fromfile, opponent_color(piececolor));
   8.446 -<a class="c2html-lineno" name="l394" href="#l394">394 </a>        }
   8.447 -<a class="c2html-lineno" name="l395" href="#l395">395 </a>        
   8.448 -<a class="c2html-lineno" name="l396" href="#l396">396 </a>        <span class="c2html-comment">/* can't capture, can he block? */</span>
   8.449 -<a class="c2html-lineno" name="l397" href="#l397">397 </a>        <span class="c2html-keyword">if</span> (!canescape && threatcount == <span class="c2html-macroconst">1</span>) {
   8.450 -<a class="c2html-lineno" name="l398" href="#l398">398 </a>            Move *threat = &(threats[<span class="c2html-macroconst">0</span>]);
   8.451 -<a class="c2html-lineno" name="l399" href="#l399">399 </a>            <span class="c2html-type">uint8_t</span> threatpiece = threat-&gt;piece & <span class="c2html-macroconst">PIECE_MASK</span>;
   8.452 -<a class="c2html-lineno" name="l400" href="#l400">400 </a>            
   8.453 -<a class="c2html-lineno" name="l401" href="#l401">401 </a>            <span class="c2html-comment">/* knight, pawns and the king cannot be blocked */</span>
   8.454 -<a class="c2html-lineno" name="l402" href="#l402">402 </a>            <span class="c2html-keyword">if</span> (threatpiece == <span class="c2html-macroconst">BISHOP</span> || threatpiece == <span class="c2html-macroconst">ROOK</span>
   8.455 -<a class="c2html-lineno" name="l403" href="#l403">403 </a>                || threatpiece == <span class="c2html-macroconst">QUEEN</span>) {
   8.456 -<a class="c2html-lineno" name="l404" href="#l404">404 </a>                <span class="c2html-keyword">if</span> (threat-&gt;fromrow == threat-&gt;torow) {
   8.457 -<a class="c2html-lineno" name="l405" href="#l405">405 </a>                    <span class="c2html-comment">/* rook aspect (on row) */</span>
   8.458 -<a class="c2html-lineno" name="l406" href="#l406">406 </a>                    <span class="c2html-keyword">int</span> d = threat-&gt;tofile &gt; threat-&gt;fromfile ? <span class="c2html-macroconst">1</span> : -<span class="c2html-macroconst">1</span>;
   8.459 -<a class="c2html-lineno" name="l407" href="#l407">407 </a>                    <span class="c2html-type">uint8_t</span> file = threat-&gt;fromfile;
   8.460 -<a class="c2html-lineno" name="l408" href="#l408">408 </a>                    <span class="c2html-keyword">while</span> (!canescape && file != threat-&gt;tofile - d) {
   8.461 -<a class="c2html-lineno" name="l409" href="#l409">409 </a>                        file += d;
   8.462 -<a class="c2html-lineno" name="l410" href="#l410">410 </a>                        canescape |= is_protected(&simulation,
   8.463 -<a class="c2html-lineno" name="l411" href="#l411">411 </a>                            threat-&gt;torow, file, opponent_color(piececolor));
   8.464 -<a class="c2html-lineno" name="l412" href="#l412">412 </a>                    }
   8.465 -<a class="c2html-lineno" name="l413" href="#l413">413 </a>                } <span class="c2html-keyword">else</span> <span class="c2html-keyword">if</span> (threat-&gt;fromfile == threat-&gt;tofile) {
   8.466 -<a class="c2html-lineno" name="l414" href="#l414">414 </a>                    <span class="c2html-comment">/* rook aspect (on file) */</span>
   8.467 -<a class="c2html-lineno" name="l415" href="#l415">415 </a>                    <span class="c2html-keyword">int</span> d = threat-&gt;torow &gt; threat-&gt;fromrow ? <span class="c2html-macroconst">1</span> : -<span class="c2html-macroconst">1</span>;
   8.468 -<a class="c2html-lineno" name="l416" href="#l416">416 </a>                    <span class="c2html-type">uint8_t</span> row = threat-&gt;fromrow;
   8.469 -<a class="c2html-lineno" name="l417" href="#l417">417 </a>                    <span class="c2html-keyword">while</span> (!canescape && row != threat-&gt;torow - d) {
   8.470 -<a class="c2html-lineno" name="l418" href="#l418">418 </a>                        row += d;
   8.471 -<a class="c2html-lineno" name="l419" href="#l419">419 </a>                        canescape |= is_protected(&simulation,
   8.472 -<a class="c2html-lineno" name="l420" href="#l420">420 </a>                            row, threat-&gt;tofile, opponent_color(piececolor));
   8.473 -<a class="c2html-lineno" name="l421" href="#l421">421 </a>                    }
   8.474 -<a class="c2html-lineno" name="l422" href="#l422">422 </a>                } <span class="c2html-keyword">else</span> {
   8.475 -<a class="c2html-lineno" name="l423" href="#l423">423 </a>                    <span class="c2html-comment">/* bishop aspect */</span>
   8.476 -<a class="c2html-lineno" name="l424" href="#l424">424 </a>                    <span class="c2html-keyword">int</span> dr = threat-&gt;torow &gt; threat-&gt;fromrow ? <span class="c2html-macroconst">1</span> : -<span class="c2html-macroconst">1</span>;
   8.477 -<a class="c2html-lineno" name="l425" href="#l425">425 </a>                    <span class="c2html-keyword">int</span> df = threat-&gt;tofile &gt; threat-&gt;fromfile ? <span class="c2html-macroconst">1</span> : -<span class="c2html-macroconst">1</span>;
   8.478 -<a class="c2html-lineno" name="l426" href="#l426">426 </a>
   8.479 -<a class="c2html-lineno" name="l427" href="#l427">427 </a>                    <span class="c2html-type">uint8_t</span> row = threat-&gt;fromrow;
   8.480 -<a class="c2html-lineno" name="l428" href="#l428">428 </a>                    <span class="c2html-type">uint8_t</span> file = threat-&gt;fromfile;
   8.481 -<a class="c2html-lineno" name="l429" href="#l429">429 </a>                    <span class="c2html-keyword">while</span> (!canescape && file != threat-&gt;tofile - df
   8.482 -<a class="c2html-lineno" name="l430" href="#l430">430 </a>                        && row != threat-&gt;torow - dr) {
   8.483 -<a class="c2html-lineno" name="l431" href="#l431">431 </a>                        row += dr;
   8.484 -<a class="c2html-lineno" name="l432" href="#l432">432 </a>                        file += df;
   8.485 -<a class="c2html-lineno" name="l433" href="#l433">433 </a>                        canescape |= is_protected(&simulation, row, file,
   8.486 -<a class="c2html-lineno" name="l434" href="#l434">434 </a>                            opponent_color(piececolor));
   8.487 -<a class="c2html-lineno" name="l435" href="#l435">435 </a>                    }
   8.488 -<a class="c2html-lineno" name="l436" href="#l436">436 </a>                }
   8.489 -<a class="c2html-lineno" name="l437" href="#l437">437 </a>            }
   8.490 -<a class="c2html-lineno" name="l438" href="#l438">438 </a>        }
   8.491 -<a class="c2html-lineno" name="l439" href="#l439">439 </a>            
   8.492 -<a class="c2html-lineno" name="l440" href="#l440">440 </a>        <span class="c2html-keyword">if</span> (!canescape) {
   8.493 -<a class="c2html-lineno" name="l441" href="#l441">441 </a>            gamestate-&gt;checkmate = <span class="c2html-macroconst">1</span>;
   8.494 -<a class="c2html-lineno" name="l442" href="#l442">442 </a>        }
   8.495 -<a class="c2html-lineno" name="l443" href="#l443">443 </a>    }
   8.496 -<a class="c2html-lineno" name="l444" href="#l444">444 </a>    
   8.497 -<a class="c2html-lineno" name="l445" href="#l445">445 </a>    gamestate_cleanup(&simulation);
   8.498 -<a class="c2html-lineno" name="l446" href="#l446">446 </a>    
   8.499 -<a class="c2html-lineno" name="l447" href="#l447">447 </a>    <span class="c2html-keyword">return</span> <span class="c2html-macroconst">VALID_MOVE_SEMANTICS</span>;
   8.500 -<a class="c2html-lineno" name="l448" href="#l448">448 </a>}
   8.501 -<a class="c2html-lineno" name="l449" href="#l449">449 </a>
   8.502 -<a class="c2html-lineno" name="l450" href="#l450">450 </a>_Bool get_threats(GameState *gamestate, <span class="c2html-type">uint8_t</span> row, <span class="c2html-type">uint8_t</span> file,
   8.503 -<a class="c2html-lineno" name="l451" href="#l451">451 </a>        <span class="c2html-type">uint8_t</span> color, Move *threats, <span class="c2html-type">uint8_t</span> *threatcount) {
   8.504 -<a class="c2html-lineno" name="l452" href="#l452">452 </a>    Move candidates[<span class="c2html-macroconst">32</span>];
   8.505 -<a class="c2html-lineno" name="l453" href="#l453">453 </a>    <span class="c2html-keyword">int</span> candidatecount = <span class="c2html-macroconst">0</span>;
   8.506 -<a class="c2html-lineno" name="l454" href="#l454">454 </a>    <span class="c2html-keyword">for</span> (<span class="c2html-type">uint8_t</span> r = <span class="c2html-macroconst">0</span> ; r &lt; <span class="c2html-macroconst">8</span> ; r++) {
   8.507 -<a class="c2html-lineno" name="l455" href="#l455">455 </a>        <span class="c2html-keyword">for</span> (<span class="c2html-type">uint8_t</span> f = <span class="c2html-macroconst">0</span> ; f &lt; <span class="c2html-macroconst">8</span> ; f++) {
   8.508 -<a class="c2html-lineno" name="l456" href="#l456">456 </a>            <span class="c2html-keyword">if</span> ((gamestate-&gt;board[r][f] & <span class="c2html-macroconst">COLOR_MASK</span>) == color) {
   8.509 -<a class="c2html-lineno" name="l457" href="#l457">457 </a>                <span class="c2html-comment">// non-capturing move</span>
   8.510 -<a class="c2html-lineno" name="l458" href="#l458">458 </a>                memset(&(candidates[candidatecount]), <span class="c2html-macroconst">0</span>, <span class="c2html-keyword">sizeof</span>(Move));
   8.511 -<a class="c2html-lineno" name="l459" href="#l459">459 </a>                candidates[candidatecount].piece = gamestate-&gt;board[r][f];
   8.512 -<a class="c2html-lineno" name="l460" href="#l460">460 </a>                candidates[candidatecount].fromrow = r;
   8.513 -<a class="c2html-lineno" name="l461" href="#l461">461 </a>                candidates[candidatecount].fromfile = f;
   8.514 -<a class="c2html-lineno" name="l462" href="#l462">462 </a>                candidates[candidatecount].torow = row;
   8.515 -<a class="c2html-lineno" name="l463" href="#l463">463 </a>                candidates[candidatecount].tofile = file;
   8.516 -<a class="c2html-lineno" name="l464" href="#l464">464 </a>                candidatecount++;
   8.517 -<a class="c2html-lineno" name="l465" href="#l465">465 </a>
   8.518 -<a class="c2html-lineno" name="l466" href="#l466">466 </a>                <span class="c2html-comment">// capturing move</span>
   8.519 -<a class="c2html-lineno" name="l467" href="#l467">467 </a>                memcpy(&(candidates[candidatecount]),
   8.520 -<a class="c2html-lineno" name="l468" href="#l468">468 </a>                    &(candidates[candidatecount-<span class="c2html-macroconst">1</span>]), <span class="c2html-keyword">sizeof</span>(Move));
   8.521 -<a class="c2html-lineno" name="l469" href="#l469">469 </a>                candidates[candidatecount].capture = <span class="c2html-macroconst">1</span>;
   8.522 -<a class="c2html-lineno" name="l470" href="#l470">470 </a>                candidatecount++;
   8.523 -<a class="c2html-lineno" name="l471" href="#l471">471 </a>            }
   8.524 -<a class="c2html-lineno" name="l472" href="#l472">472 </a>        }
   8.525 -<a class="c2html-lineno" name="l473" href="#l473">473 </a>    }
   8.526 -<a class="c2html-lineno" name="l474" href="#l474">474 </a>
   8.527 -<a class="c2html-lineno" name="l475" href="#l475">475 </a>    <span class="c2html-keyword">if</span> (threatcount) {
   8.528 -<a class="c2html-lineno" name="l476" href="#l476">476 </a>        *threatcount = <span class="c2html-macroconst">0</span>;
   8.529 -<a class="c2html-lineno" name="l477" href="#l477">477 </a>    }
   8.530 -<a class="c2html-lineno" name="l478" href="#l478">478 </a>    
   8.531 -<a class="c2html-lineno" name="l479" href="#l479">479 </a>    
   8.532 -<a class="c2html-lineno" name="l480" href="#l480">480 </a>    _Bool result = <span class="c2html-macroconst">0</span>;
   8.533 -<a class="c2html-lineno" name="l481" href="#l481">481 </a>    
   8.534 -<a class="c2html-lineno" name="l482" href="#l482">482 </a>    <span class="c2html-keyword">for</span> (<span class="c2html-keyword">int</span> i = <span class="c2html-macroconst">0</span> ; i &lt; candidatecount ; i++) {
   8.535 -<a class="c2html-lineno" name="l483" href="#l483">483 </a>        <span class="c2html-keyword">if</span> (validate_move_rules(gamestate, &(candidates[i]))
   8.536 -<a class="c2html-lineno" name="l484" href="#l484">484 </a>                == <span class="c2html-macroconst">VALID_MOVE_SEMANTICS</span>) {
   8.537 -<a class="c2html-lineno" name="l485" href="#l485">485 </a>            result = <span class="c2html-macroconst">1</span>;
   8.538 -<a class="c2html-lineno" name="l486" href="#l486">486 </a>            <span class="c2html-keyword">if</span> (threats && threatcount) {
   8.539 -<a class="c2html-lineno" name="l487" href="#l487">487 </a>                threats[(*threatcount)++] = candidates[i];
   8.540 -<a class="c2html-lineno" name="l488" href="#l488">488 </a>            }
   8.541 -<a class="c2html-lineno" name="l489" href="#l489">489 </a>        }
   8.542 -<a class="c2html-lineno" name="l490" href="#l490">490 </a>    }
   8.543 -<a class="c2html-lineno" name="l491" href="#l491">491 </a>    
   8.544 -<a class="c2html-lineno" name="l492" href="#l492">492 </a>    <span class="c2html-keyword">return</span> result;
   8.545 -<a class="c2html-lineno" name="l493" href="#l493">493 </a>}
   8.546 -<a class="c2html-lineno" name="l494" href="#l494">494 </a>
   8.547 -<a class="c2html-lineno" name="l495" href="#l495">495 </a>_Bool is_pinned(GameState *gamestate, Move *move) {
   8.548 -<a class="c2html-lineno" name="l496" href="#l496">496 </a>    <span class="c2html-type">uint8_t</span> color = move-&gt;piece & <span class="c2html-macroconst">COLOR_MASK</span>;
   8.549 -<a class="c2html-lineno" name="l497" href="#l497">497 </a>
   8.550 -<a class="c2html-lineno" name="l498" href="#l498">498 </a>    <span class="c2html-type">uint8_t</span> kingfile = <span class="c2html-macroconst">0</span>, kingrow = <span class="c2html-macroconst">0</span>;
   8.551 -<a class="c2html-lineno" name="l499" href="#l499">499 </a>    <span class="c2html-keyword">for</span> (<span class="c2html-type">uint8_t</span> row = <span class="c2html-macroconst">0</span> ; row &lt; <span class="c2html-macroconst">8</span> ; row++) {
   8.552 -<a class="c2html-lineno" name="l500" href="#l500">500 </a>        <span class="c2html-keyword">for</span> (<span class="c2html-type">uint8_t</span> file = <span class="c2html-macroconst">0</span> ; file &lt; <span class="c2html-macroconst">8</span> ; file++) {
   8.553 -<a class="c2html-lineno" name="l501" href="#l501">501 </a>            <span class="c2html-keyword">if</span> (gamestate-&gt;board[row][file] == (color|<span class="c2html-macroconst">KING</span>)) {
   8.554 -<a class="c2html-lineno" name="l502" href="#l502">502 </a>                kingfile = file;
   8.555 -<a class="c2html-lineno" name="l503" href="#l503">503 </a>                kingrow = row;
   8.556 -<a class="c2html-lineno" name="l504" href="#l504">504 </a>            }
   8.557 -<a class="c2html-lineno" name="l505" href="#l505">505 </a>        }
   8.558 -<a class="c2html-lineno" name="l506" href="#l506">506 </a>    }
   8.559 -<a class="c2html-lineno" name="l507" href="#l507">507 </a>
   8.560 -<a class="c2html-lineno" name="l508" href="#l508">508 </a>    GameState simulation = gamestate_copy_sim(gamestate);
   8.561 -<a class="c2html-lineno" name="l509" href="#l509">509 </a>    Move simmove = *move;
   8.562 -<a class="c2html-lineno" name="l510" href="#l510">510 </a>    apply_move(&simulation, &simmove);
   8.563 -<a class="c2html-lineno" name="l511" href="#l511">511 </a>    _Bool covered = is_covered(&simulation,
   8.564 -<a class="c2html-lineno" name="l512" href="#l512">512 </a>        kingrow, kingfile, opponent_color(color));
   8.565 -<a class="c2html-lineno" name="l513" href="#l513">513 </a>    gamestate_cleanup(&simulation);
   8.566 -<a class="c2html-lineno" name="l514" href="#l514">514 </a>    
   8.567 -<a class="c2html-lineno" name="l515" href="#l515">515 </a>    <span class="c2html-keyword">return</span> covered;
   8.568 -<a class="c2html-lineno" name="l516" href="#l516">516 </a>}
   8.569 -<a class="c2html-lineno" name="l517" href="#l517">517 </a>
   8.570 -<a class="c2html-lineno" name="l518" href="#l518">518 </a>_Bool get_real_threats(GameState *gamestate, <span class="c2html-type">uint8_t</span> row, <span class="c2html-type">uint8_t</span> file,
   8.571 -<a class="c2html-lineno" name="l519" href="#l519">519 </a>        <span class="c2html-type">uint8_t</span> color, Move *threats, <span class="c2html-type">uint8_t</span> *threatcount) {
   8.572 -<a class="c2html-lineno" name="l520" href="#l520">520 </a>    
   8.573 -<a class="c2html-lineno" name="l521" href="#l521">521 </a>    <span class="c2html-keyword">if</span> (threatcount) {
   8.574 -<a class="c2html-lineno" name="l522" href="#l522">522 </a>        *threatcount = <span class="c2html-macroconst">0</span>;
   8.575 -<a class="c2html-lineno" name="l523" href="#l523">523 </a>    }
   8.576 -<a class="c2html-lineno" name="l524" href="#l524">524 </a>
   8.577 -<a class="c2html-lineno" name="l525" href="#l525">525 </a>    Move candidates[<span class="c2html-macroconst">16</span>];
   8.578 -<a class="c2html-lineno" name="l526" href="#l526">526 </a>    <span class="c2html-type">uint8_t</span> candidatecount;
   8.579 -<a class="c2html-lineno" name="l527" href="#l527">527 </a>    <span class="c2html-keyword">if</span> (get_threats(gamestate, row, file, color, candidates, &candidatecount)) {
   8.580 -<a class="c2html-lineno" name="l528" href="#l528">528 </a>        
   8.581 -<a class="c2html-lineno" name="l529" href="#l529">529 </a>        _Bool result = <span class="c2html-macroconst">0</span>;
   8.582 -<a class="c2html-lineno" name="l530" href="#l530">530 </a>        <span class="c2html-type">uint8_t</span> kingfile = <span class="c2html-macroconst">0</span>, kingrow = <span class="c2html-macroconst">0</span>;
   8.583 -<a class="c2html-lineno" name="l531" href="#l531">531 </a>        <span class="c2html-keyword">for</span> (<span class="c2html-type">uint8_t</span> row = <span class="c2html-macroconst">0</span> ; row &lt; <span class="c2html-macroconst">8</span> ; row++) {
   8.584 -<a class="c2html-lineno" name="l532" href="#l532">532 </a>            <span class="c2html-keyword">for</span> (<span class="c2html-type">uint8_t</span> file = <span class="c2html-macroconst">0</span> ; file &lt; <span class="c2html-macroconst">8</span> ; file++) {
   8.585 -<a class="c2html-lineno" name="l533" href="#l533">533 </a>                <span class="c2html-keyword">if</span> (gamestate-&gt;board[row][file] == (color|<span class="c2html-macroconst">KING</span>)) {
   8.586 -<a class="c2html-lineno" name="l534" href="#l534">534 </a>                    kingfile = file;
   8.587 -<a class="c2html-lineno" name="l535" href="#l535">535 </a>                    kingrow = row;
   8.588 -<a class="c2html-lineno" name="l536" href="#l536">536 </a>                }
   8.589 -<a class="c2html-lineno" name="l537" href="#l537">537 </a>            }
   8.590 -<a class="c2html-lineno" name="l538" href="#l538">538 </a>        }
   8.591 -<a class="c2html-lineno" name="l539" href="#l539">539 </a>
   8.592 -<a class="c2html-lineno" name="l540" href="#l540">540 </a>        <span class="c2html-keyword">for</span> (<span class="c2html-type">uint8_t</span> i = <span class="c2html-macroconst">0</span> ; i &lt; candidatecount ; i++) {
   8.593 -<a class="c2html-lineno" name="l541" href="#l541">541 </a>            GameState simulation = gamestate_copy_sim(gamestate);
   8.594 -<a class="c2html-lineno" name="l542" href="#l542">542 </a>            Move simmove = candidates[i];
   8.595 -<a class="c2html-lineno" name="l543" href="#l543">543 </a>            apply_move(&simulation, &simmove);
   8.596 -<a class="c2html-lineno" name="l544" href="#l544">544 </a>            <span class="c2html-keyword">if</span> (!is_covered(&simulation, kingrow, kingfile,
   8.597 -<a class="c2html-lineno" name="l545" href="#l545">545 </a>                    opponent_color(color))) {
   8.598 -<a class="c2html-lineno" name="l546" href="#l546">546 </a>                result = <span class="c2html-macroconst">1</span>;
   8.599 -<a class="c2html-lineno" name="l547" href="#l547">547 </a>                <span class="c2html-keyword">if</span> (threats && threatcount) {
   8.600 -<a class="c2html-lineno" name="l548" href="#l548">548 </a>                    threats[(*threatcount)++] = candidates[i];
   8.601 -<a class="c2html-lineno" name="l549" href="#l549">549 </a>                }
   8.602 -<a class="c2html-lineno" name="l550" href="#l550">550 </a>            }
   8.603 -<a class="c2html-lineno" name="l551" href="#l551">551 </a>        }
   8.604 -<a class="c2html-lineno" name="l552" href="#l552">552 </a>        
   8.605 -<a class="c2html-lineno" name="l553" href="#l553">553 </a>        <span class="c2html-keyword">return</span> result;
   8.606 -<a class="c2html-lineno" name="l554" href="#l554">554 </a>    } <span class="c2html-keyword">else</span> {
   8.607 -<a class="c2html-lineno" name="l555" href="#l555">555 </a>        <span class="c2html-keyword">return</span> <span class="c2html-macroconst">0</span>;
   8.608 -<a class="c2html-lineno" name="l556" href="#l556">556 </a>    }
   8.609 -<a class="c2html-lineno" name="l557" href="#l557">557 </a>}
   8.610 -<a class="c2html-lineno" name="l558" href="#l558">558 </a>
   8.611 -<a class="c2html-lineno" name="l559" href="#l559">559 </a><span class="c2html-keyword">static</span> <span class="c2html-keyword">int</span> getlocation(GameState *gamestate, Move *move) {   
   8.612 -<a class="c2html-lineno" name="l560" href="#l560">560 </a>
   8.613 -<a class="c2html-lineno" name="l561" href="#l561">561 </a>    <span class="c2html-type">uint8_t</span> color = move-&gt;piece & <span class="c2html-macroconst">COLOR_MASK</span>;
   8.614 -<a class="c2html-lineno" name="l562" href="#l562">562 </a>    _Bool incheck = gamestate-&gt;lastmove?gamestate-&gt;lastmove-&gt;move.check:<span class="c2html-macroconst">0</span>;
   8.615 -<a class="c2html-lineno" name="l563" href="#l563">563 </a>    
   8.616 -<a class="c2html-lineno" name="l564" href="#l564">564 </a>    Move threats[<span class="c2html-macroconst">16</span>], *threat = <span class="c2html-macroconst">NULL</span>;
   8.617 -<a class="c2html-lineno" name="l565" href="#l565">565 </a>    <span class="c2html-type">uint8_t</span> threatcount;
   8.618 -<a class="c2html-lineno" name="l566" href="#l566">566 </a>    
   8.619 -<a class="c2html-lineno" name="l567" href="#l567">567 </a>    <span class="c2html-keyword">if</span> (get_threats(gamestate, move-&gt;torow, move-&gt;tofile, color,
   8.620 -<a class="c2html-lineno" name="l568" href="#l568">568 </a>            threats, &threatcount)) {
   8.621 -<a class="c2html-lineno" name="l569" href="#l569">569 </a>        
   8.622 -<a class="c2html-lineno" name="l570" href="#l570">570 </a>        <span class="c2html-keyword">int</span> reason = <span class="c2html-macroconst">INVALID_POSITION</span>;
   8.623 -<a class="c2html-lineno" name="l571" href="#l571">571 </a>        
   8.624 -<a class="c2html-lineno" name="l572" href="#l572">572 </a>        <span class="c2html-comment">// find threats for the specified position</span>
   8.625 -<a class="c2html-lineno" name="l573" href="#l573">573 </a>        <span class="c2html-keyword">for</span> (<span class="c2html-type">uint8_t</span> i = <span class="c2html-macroconst">0</span> ; i &lt; threatcount ; i++) {
   8.626 -<a class="c2html-lineno" name="l574" href="#l574">574 </a>            <span class="c2html-keyword">if</span> ((threats[i].piece & (<span class="c2html-macroconst">PIECE_MASK</span> | <span class="c2html-macroconst">COLOR_MASK</span>))
   8.627 -<a class="c2html-lineno" name="l575" href="#l575">575 </a>                    == move-&gt;piece &&
   8.628 -<a class="c2html-lineno" name="l576" href="#l576">576 </a>                    (move-&gt;fromrow == <span class="c2html-macroconst">POS_UNSPECIFIED</span> ||
   8.629 -<a class="c2html-lineno" name="l577" href="#l577">577 </a>                    move-&gt;fromrow == threats[i].fromrow) &&
   8.630 -<a class="c2html-lineno" name="l578" href="#l578">578 </a>                    (move-&gt;fromfile == <span class="c2html-macroconst">POS_UNSPECIFIED</span> ||
   8.631 -<a class="c2html-lineno" name="l579" href="#l579">579 </a>                    move-&gt;fromfile == threats[i].fromfile)) {
   8.632 -<a class="c2html-lineno" name="l580" href="#l580">580 </a>
   8.633 -<a class="c2html-lineno" name="l581" href="#l581">581 </a>                <span class="c2html-keyword">if</span> (threat) {
   8.634 -<a class="c2html-lineno" name="l582" href="#l582">582 </a>                    <span class="c2html-keyword">return</span> <span class="c2html-macroconst">AMBIGUOUS_MOVE</span>;
   8.635 -<a class="c2html-lineno" name="l583" href="#l583">583 </a>                } <span class="c2html-keyword">else</span> {
   8.636 -<a class="c2html-lineno" name="l584" href="#l584">584 </a>                    <span class="c2html-comment">// found threat is no real threat</span>
   8.637 -<a class="c2html-lineno" name="l585" href="#l585">585 </a>                    <span class="c2html-keyword">if</span> (is_pinned(gamestate, &(threats[i]))) {
   8.638 -<a class="c2html-lineno" name="l586" href="#l586">586 </a>                        reason = incheck?<span class="c2html-macroconst">KING_IN_CHECK</span>:<span class="c2html-macroconst">PIECE_PINNED</span>;
   8.639 -<a class="c2html-lineno" name="l587" href="#l587">587 </a>                    } <span class="c2html-keyword">else</span> {
   8.640 -<a class="c2html-lineno" name="l588" href="#l588">588 </a>                        threat = &(threats[i]);
   8.641 -<a class="c2html-lineno" name="l589" href="#l589">589 </a>                    }
   8.642 -<a class="c2html-lineno" name="l590" href="#l590">590 </a>                }
   8.643 -<a class="c2html-lineno" name="l591" href="#l591">591 </a>            }
   8.644 -<a class="c2html-lineno" name="l592" href="#l592">592 </a>        }
   8.645 -<a class="c2html-lineno" name="l593" href="#l593">593 </a>        
   8.646 -<a class="c2html-lineno" name="l594" href="#l594">594 </a>        <span class="c2html-comment">// can't threaten specified position</span>
   8.647 -<a class="c2html-lineno" name="l595" href="#l595">595 </a>        <span class="c2html-keyword">if</span> (!threat) {
   8.648 -<a class="c2html-lineno" name="l596" href="#l596">596 </a>            <span class="c2html-keyword">return</span> reason;
   8.649 -<a class="c2html-lineno" name="l597" href="#l597">597 </a>        }
   8.650 -<a class="c2html-lineno" name="l598" href="#l598">598 </a>
   8.651 -<a class="c2html-lineno" name="l599" href="#l599">599 </a>        memcpy(move, threat, <span class="c2html-keyword">sizeof</span>(Move));
   8.652 -<a class="c2html-lineno" name="l600" href="#l600">600 </a>        <span class="c2html-keyword">return</span> <span class="c2html-macroconst">VALID_MOVE_SYNTAX</span>;
   8.653 -<a class="c2html-lineno" name="l601" href="#l601">601 </a>    } <span class="c2html-keyword">else</span> {
   8.654 -<a class="c2html-lineno" name="l602" href="#l602">602 </a>        <span class="c2html-keyword">return</span> <span class="c2html-macroconst">INVALID_POSITION</span>;
   8.655 -<a class="c2html-lineno" name="l603" href="#l603">603 </a>    }
   8.656 -<a class="c2html-lineno" name="l604" href="#l604">604 </a>}
   8.657 -<a class="c2html-lineno" name="l605" href="#l605">605 </a>
   8.658 -<a class="c2html-lineno" name="l606" href="#l606">606 </a><span class="c2html-keyword">int</span> eval_move(GameState *gamestate, <span class="c2html-keyword">char</span> *mstr, Move *move, <span class="c2html-type">uint8_t</span> color) {
   8.659 -<a class="c2html-lineno" name="l607" href="#l607">607 </a>    memset(move, <span class="c2html-macroconst">0</span>, <span class="c2html-keyword">sizeof</span>(Move));
   8.660 -<a class="c2html-lineno" name="l608" href="#l608">608 </a>    move-&gt;fromfile = <span class="c2html-macroconst">POS_UNSPECIFIED</span>;
   8.661 -<a class="c2html-lineno" name="l609" href="#l609">609 </a>    move-&gt;fromrow = <span class="c2html-macroconst">POS_UNSPECIFIED</span>;
   8.662 -<a class="c2html-lineno" name="l610" href="#l610">610 </a>
   8.663 -<a class="c2html-lineno" name="l611" href="#l611">611 </a>    <span class="c2html-type">size_t</span> len = strlen(mstr);
   8.664 -<a class="c2html-lineno" name="l612" href="#l612">612 </a>    <span class="c2html-keyword">if</span> (len &lt; <span class="c2html-macroconst">1</span> || len &gt; <span class="c2html-macroconst">6</span>) {
   8.665 -<a class="c2html-lineno" name="l613" href="#l613">613 </a>        <span class="c2html-keyword">return</span> <span class="c2html-macroconst">INVALID_MOVE_SYNTAX</span>;
   8.666 -<a class="c2html-lineno" name="l614" href="#l614">614 </a>    }
   8.667 -<a class="c2html-lineno" name="l615" href="#l615">615 </a>    
   8.668 -<a class="c2html-lineno" name="l616" href="#l616">616 </a>    <span class="c2html-comment">/* evaluate check/checkmate flags */</span>
   8.669 -<a class="c2html-lineno" name="l617" href="#l617">617 </a>    <span class="c2html-keyword">if</span> (mstr[len-<span class="c2html-macroconst">1</span>] == <span class="c2html-string">'+'</span>) {
   8.670 -<a class="c2html-lineno" name="l618" href="#l618">618 </a>        len--; mstr[len] = <span class="c2html-string">'\0'</span>;
   8.671 -<a class="c2html-lineno" name="l619" href="#l619">619 </a>        move-&gt;check = <span class="c2html-macroconst">1</span>;
   8.672 -<a class="c2html-lineno" name="l620" href="#l620">620 </a>    } <span class="c2html-keyword">else</span> <span class="c2html-keyword">if</span> (mstr[len-<span class="c2html-macroconst">1</span>] == <span class="c2html-string">'#'</span>) {
   8.673 -<a class="c2html-lineno" name="l621" href="#l621">621 </a>        len--; mstr[len] = <span class="c2html-string">'\0'</span>;
   8.674 -<a class="c2html-lineno" name="l622" href="#l622">622 </a>        <span class="c2html-comment">/* ignore - validation should set game state */</span>
   8.675 -<a class="c2html-lineno" name="l623" href="#l623">623 </a>    }
   8.676 -<a class="c2html-lineno" name="l624" href="#l624">624 </a>    
   8.677 -<a class="c2html-lineno" name="l625" href="#l625">625 </a>    <span class="c2html-comment">/* evaluate promotion */</span>
   8.678 -<a class="c2html-lineno" name="l626" href="#l626">626 </a>    <span class="c2html-keyword">if</span> (len &gt; <span class="c2html-macroconst">3</span> && mstr[len-<span class="c2html-macroconst">2</span>] == <span class="c2html-string">'='</span>) {
   8.679 -<a class="c2html-lineno" name="l627" href="#l627">627 </a>        move-&gt;promotion = getpiece(mstr[len-<span class="c2html-macroconst">1</span>]);
   8.680 -<a class="c2html-lineno" name="l628" href="#l628">628 </a>        <span class="c2html-keyword">if</span> (!move-&gt;promotion) {
   8.681 -<a class="c2html-lineno" name="l629" href="#l629">629 </a>            <span class="c2html-keyword">return</span> <span class="c2html-macroconst">INVALID_MOVE_SYNTAX</span>;
   8.682 -<a class="c2html-lineno" name="l630" href="#l630">630 </a>        } <span class="c2html-keyword">else</span> {
   8.683 -<a class="c2html-lineno" name="l631" href="#l631">631 </a>            move-&gt;promotion |= color;
   8.684 -<a class="c2html-lineno" name="l632" href="#l632">632 </a>            len -= <span class="c2html-macroconst">2</span>;
   8.685 -<a class="c2html-lineno" name="l633" href="#l633">633 </a>            mstr[len] = <span class="c2html-macroconst">0</span>;
   8.686 -<a class="c2html-lineno" name="l634" href="#l634">634 </a>        }
   8.687 -<a class="c2html-lineno" name="l635" href="#l635">635 </a>    }
   8.688 -<a class="c2html-lineno" name="l636" href="#l636">636 </a>    
   8.689 -<a class="c2html-lineno" name="l637" href="#l637">637 </a>    <span class="c2html-keyword">if</span> (len == <span class="c2html-macroconst">2</span>) {
   8.690 -<a class="c2html-lineno" name="l638" href="#l638">638 </a>        <span class="c2html-comment">/* pawn move (e.g. "e4") */</span>
   8.691 -<a class="c2html-lineno" name="l639" href="#l639">639 </a>        move-&gt;piece = <span class="c2html-macroconst">PAWN</span>;
   8.692 -<a class="c2html-lineno" name="l640" href="#l640">640 </a>        move-&gt;tofile = fileidx(mstr[<span class="c2html-macroconst">0</span>]);
   8.693 -<a class="c2html-lineno" name="l641" href="#l641">641 </a>        move-&gt;torow = rowidx(mstr[<span class="c2html-macroconst">1</span>]);
   8.694 -<a class="c2html-lineno" name="l642" href="#l642">642 </a>    } <span class="c2html-keyword">else</span> <span class="c2html-keyword">if</span> (len == <span class="c2html-macroconst">3</span>) {
   8.695 -<a class="c2html-lineno" name="l643" href="#l643">643 </a>        <span class="c2html-keyword">if</span> (strcmp(mstr, <span class="c2html-string">"O-O"</span>) == <span class="c2html-macroconst">0</span>) {
   8.696 -<a class="c2html-lineno" name="l644" href="#l644">644 </a>            <span class="c2html-comment">/* king side castling */</span>
   8.697 -<a class="c2html-lineno" name="l645" href="#l645">645 </a>            move-&gt;piece = <span class="c2html-macroconst">KING</span>;
   8.698 -<a class="c2html-lineno" name="l646" href="#l646">646 </a>            move-&gt;fromfile = fileidx(<span class="c2html-string">'e'</span>);
   8.699 -<a class="c2html-lineno" name="l647" href="#l647">647 </a>            move-&gt;tofile = fileidx(<span class="c2html-string">'g'</span>);
   8.700 -<a class="c2html-lineno" name="l648" href="#l648">648 </a>            move-&gt;fromrow = move-&gt;torow = color == <span class="c2html-macroconst">WHITE</span> ? <span class="c2html-macroconst">0</span> : <span class="c2html-macroconst">7</span>;
   8.701 -<a class="c2html-lineno" name="l649" href="#l649">649 </a>        } <span class="c2html-keyword">else</span> {
   8.702 -<a class="c2html-lineno" name="l650" href="#l650">650 </a>            <span class="c2html-comment">/* move (e.g. "Nf3") */</span>
   8.703 -<a class="c2html-lineno" name="l651" href="#l651">651 </a>            move-&gt;piece = getpiece(mstr[<span class="c2html-macroconst">0</span>]);
   8.704 -<a class="c2html-lineno" name="l652" href="#l652">652 </a>            move-&gt;tofile = fileidx(mstr[<span class="c2html-macroconst">1</span>]);
   8.705 -<a class="c2html-lineno" name="l653" href="#l653">653 </a>            move-&gt;torow = rowidx(mstr[<span class="c2html-macroconst">2</span>]);
   8.706 -<a class="c2html-lineno" name="l654" href="#l654">654 </a>        }
   8.707 -<a class="c2html-lineno" name="l655" href="#l655">655 </a>    } <span class="c2html-keyword">else</span> <span class="c2html-keyword">if</span> (len == <span class="c2html-macroconst">4</span>) {
   8.708 -<a class="c2html-lineno" name="l656" href="#l656">656 </a>        move-&gt;piece = getpiece(mstr[<span class="c2html-macroconst">0</span>]);
   8.709 -<a class="c2html-lineno" name="l657" href="#l657">657 </a>        <span class="c2html-keyword">if</span> (!move-&gt;piece) {
   8.710 -<a class="c2html-lineno" name="l658" href="#l658">658 </a>            move-&gt;piece = <span class="c2html-macroconst">PAWN</span>;
   8.711 -<a class="c2html-lineno" name="l659" href="#l659">659 </a>            move-&gt;fromfile = fileidx(mstr[<span class="c2html-macroconst">0</span>]);
   8.712 -<a class="c2html-lineno" name="l660" href="#l660">660 </a>        }
   8.713 -<a class="c2html-lineno" name="l661" href="#l661">661 </a>        <span class="c2html-keyword">if</span> (mstr[<span class="c2html-macroconst">1</span>] == <span class="c2html-string">'x'</span>) {
   8.714 -<a class="c2html-lineno" name="l662" href="#l662">662 </a>            <span class="c2html-comment">/* capture (e.g. "Nxf3", "dxe5") */</span>
   8.715 -<a class="c2html-lineno" name="l663" href="#l663">663 </a>            move-&gt;capture = <span class="c2html-macroconst">1</span>;
   8.716 -<a class="c2html-lineno" name="l664" href="#l664">664 </a>        } <span class="c2html-keyword">else</span> {
   8.717 -<a class="c2html-lineno" name="l665" href="#l665">665 </a>            <span class="c2html-comment">/* move (e.g. "Ndf3", "N2c3", "e2e4") */</span>
   8.718 -<a class="c2html-lineno" name="l666" href="#l666">666 </a>            <span class="c2html-keyword">if</span> (isfile(mstr[<span class="c2html-macroconst">1</span>])) {
   8.719 -<a class="c2html-lineno" name="l667" href="#l667">667 </a>                move-&gt;fromfile = fileidx(mstr[<span class="c2html-macroconst">1</span>]);
   8.720 -<a class="c2html-lineno" name="l668" href="#l668">668 </a>                <span class="c2html-keyword">if</span> (move-&gt;piece == <span class="c2html-macroconst">PAWN</span>) {
   8.721 -<a class="c2html-lineno" name="l669" href="#l669">669 </a>                    move-&gt;piece = <span class="c2html-macroconst">0</span>;
   8.722 -<a class="c2html-lineno" name="l670" href="#l670">670 </a>                }
   8.723 -<a class="c2html-lineno" name="l671" href="#l671">671 </a>            } <span class="c2html-keyword">else</span> {
   8.724 -<a class="c2html-lineno" name="l672" href="#l672">672 </a>                move-&gt;fromrow = rowidx(mstr[<span class="c2html-macroconst">1</span>]);
   8.725 -<a class="c2html-lineno" name="l673" href="#l673">673 </a>            }
   8.726 -<a class="c2html-lineno" name="l674" href="#l674">674 </a>        }
   8.727 -<a class="c2html-lineno" name="l675" href="#l675">675 </a>        move-&gt;tofile = fileidx(mstr[<span class="c2html-macroconst">2</span>]);
   8.728 -<a class="c2html-lineno" name="l676" href="#l676">676 </a>        move-&gt;torow = rowidx(mstr[<span class="c2html-macroconst">3</span>]);
   8.729 -<a class="c2html-lineno" name="l677" href="#l677">677 </a>    } <span class="c2html-keyword">else</span> <span class="c2html-keyword">if</span> (len == <span class="c2html-macroconst">5</span>) {
   8.730 -<a class="c2html-lineno" name="l678" href="#l678">678 </a>        <span class="c2html-keyword">if</span> (strcmp(mstr, <span class="c2html-string">"O-O-O"</span>) == <span class="c2html-macroconst">0</span>) {
   8.731 -<a class="c2html-lineno" name="l679" href="#l679">679 </a>            <span class="c2html-comment">/* queen side castling "O-O-O" */</span>
   8.732 -<a class="c2html-lineno" name="l680" href="#l680">680 </a>            move-&gt;piece = <span class="c2html-macroconst">KING</span>;
   8.733 -<a class="c2html-lineno" name="l681" href="#l681">681 </a>            move-&gt;fromfile = fileidx(<span class="c2html-string">'e'</span>);
   8.734 -<a class="c2html-lineno" name="l682" href="#l682">682 </a>            move-&gt;tofile = fileidx(<span class="c2html-string">'c'</span>);
   8.735 -<a class="c2html-lineno" name="l683" href="#l683">683 </a>            move-&gt;fromrow = move-&gt;torow = color == <span class="c2html-macroconst">WHITE</span> ? <span class="c2html-macroconst">0</span> : <span class="c2html-macroconst">7</span>;
   8.736 -<a class="c2html-lineno" name="l684" href="#l684">684 </a>        } <span class="c2html-keyword">else</span> {
   8.737 -<a class="c2html-lineno" name="l685" href="#l685">685 </a>            move-&gt;piece = getpiece(mstr[<span class="c2html-macroconst">0</span>]);
   8.738 -<a class="c2html-lineno" name="l686" href="#l686">686 </a>            <span class="c2html-keyword">if</span> (mstr[<span class="c2html-macroconst">2</span>] == <span class="c2html-string">'x'</span>) {
   8.739 -<a class="c2html-lineno" name="l687" href="#l687">687 </a>                move-&gt;capture = <span class="c2html-macroconst">1</span>;
   8.740 -<a class="c2html-lineno" name="l688" href="#l688">688 </a>                <span class="c2html-keyword">if</span> (move-&gt;piece) {
   8.741 -<a class="c2html-lineno" name="l689" href="#l689">689 </a>                    <span class="c2html-comment">/* capture (e.g. "Ndxf3") */</span>
   8.742 -<a class="c2html-lineno" name="l690" href="#l690">690 </a>                    move-&gt;fromfile = fileidx(mstr[<span class="c2html-macroconst">1</span>]);
   8.743 -<a class="c2html-lineno" name="l691" href="#l691">691 </a>                } <span class="c2html-keyword">else</span> {
   8.744 -<a class="c2html-lineno" name="l692" href="#l692">692 </a>                    <span class="c2html-comment">/* long notation capture (e.g. "e5xf6") */</span>
   8.745 -<a class="c2html-lineno" name="l693" href="#l693">693 </a>                    move-&gt;piece = <span class="c2html-macroconst">PAWN</span>;
   8.746 -<a class="c2html-lineno" name="l694" href="#l694">694 </a>                    move-&gt;fromfile = fileidx(mstr[<span class="c2html-macroconst">0</span>]);
   8.747 -<a class="c2html-lineno" name="l695" href="#l695">695 </a>                    move-&gt;fromrow = rowidx(mstr[<span class="c2html-macroconst">1</span>]);
   8.748 -<a class="c2html-lineno" name="l696" href="#l696">696 </a>                }
   8.749 -<a class="c2html-lineno" name="l697" href="#l697">697 </a>            } <span class="c2html-keyword">else</span> {
   8.750 -<a class="c2html-lineno" name="l698" href="#l698">698 </a>                <span class="c2html-comment">/* long notation move (e.g. "Nc5a4") */</span>
   8.751 -<a class="c2html-lineno" name="l699" href="#l699">699 </a>                move-&gt;fromfile = fileidx(mstr[<span class="c2html-macroconst">1</span>]);
   8.752 -<a class="c2html-lineno" name="l700" href="#l700">700 </a>                move-&gt;fromrow = rowidx(mstr[<span class="c2html-macroconst">2</span>]);
   8.753 -<a class="c2html-lineno" name="l701" href="#l701">701 </a>            }
   8.754 -<a class="c2html-lineno" name="l702" href="#l702">702 </a>            move-&gt;tofile = fileidx(mstr[<span class="c2html-macroconst">3</span>]);
   8.755 -<a class="c2html-lineno" name="l703" href="#l703">703 </a>            move-&gt;torow = rowidx(mstr[<span class="c2html-macroconst">4</span>]);
   8.756 -<a class="c2html-lineno" name="l704" href="#l704">704 </a>        }
   8.757 -<a class="c2html-lineno" name="l705" href="#l705">705 </a>    } <span class="c2html-keyword">else</span> <span class="c2html-keyword">if</span> (len == <span class="c2html-macroconst">6</span>) {
   8.758 -<a class="c2html-lineno" name="l706" href="#l706">706 </a>        <span class="c2html-comment">/* long notation capture (e.g. "Nc5xf3") */</span>
   8.759 -<a class="c2html-lineno" name="l707" href="#l707">707 </a>        <span class="c2html-keyword">if</span> (mstr[<span class="c2html-macroconst">3</span>] == <span class="c2html-string">'x'</span>) {
   8.760 -<a class="c2html-lineno" name="l708" href="#l708">708 </a>            move-&gt;capture = <span class="c2html-macroconst">1</span>;
   8.761 -<a class="c2html-lineno" name="l709" href="#l709">709 </a>            move-&gt;piece = getpiece(mstr[<span class="c2html-macroconst">0</span>]);
   8.762 -<a class="c2html-lineno" name="l710" href="#l710">710 </a>            move-&gt;fromfile = fileidx(mstr[<span class="c2html-macroconst">1</span>]);
   8.763 -<a class="c2html-lineno" name="l711" href="#l711">711 </a>            move-&gt;fromrow = rowidx(mstr[<span class="c2html-macroconst">2</span>]);
   8.764 -<a class="c2html-lineno" name="l712" href="#l712">712 </a>            move-&gt;tofile = fileidx(mstr[<span class="c2html-macroconst">4</span>]);
   8.765 -<a class="c2html-lineno" name="l713" href="#l713">713 </a>            move-&gt;torow = rowidx(mstr[<span class="c2html-macroconst">5</span>]);
   8.766 -<a class="c2html-lineno" name="l714" href="#l714">714 </a>        }
   8.767 -<a class="c2html-lineno" name="l715" href="#l715">715 </a>    }
   8.768 -<a class="c2html-lineno" name="l716" href="#l716">716 </a>
   8.769 -<a class="c2html-lineno" name="l717" href="#l717">717 </a>    
   8.770 -<a class="c2html-lineno" name="l718" href="#l718">718 </a>    <span class="c2html-keyword">if</span> (move-&gt;piece) {
   8.771 -<a class="c2html-lineno" name="l719" href="#l719">719 </a>        <span class="c2html-keyword">if</span> (move-&gt;piece == <span class="c2html-macroconst">PAWN</span>
   8.772 -<a class="c2html-lineno" name="l720" href="#l720">720 </a>            && move-&gt;torow == (color==<span class="c2html-macroconst">WHITE</span>?<span class="c2html-macroconst">7</span>:<span class="c2html-macroconst">0</span>)
   8.773 -<a class="c2html-lineno" name="l721" href="#l721">721 </a>            && !move-&gt;promotion) {
   8.774 -<a class="c2html-lineno" name="l722" href="#l722">722 </a>            <span class="c2html-keyword">return</span> <span class="c2html-macroconst">NEED_PROMOTION</span>;
   8.775 -<a class="c2html-lineno" name="l723" href="#l723">723 </a>        }
   8.776 -<a class="c2html-lineno" name="l724" href="#l724">724 </a>        
   8.777 -<a class="c2html-lineno" name="l725" href="#l725">725 </a>        move-&gt;piece |= color;
   8.778 -<a class="c2html-lineno" name="l726" href="#l726">726 </a>        <span class="c2html-keyword">if</span> (move-&gt;fromfile == <span class="c2html-macroconst">POS_UNSPECIFIED</span>
   8.779 -<a class="c2html-lineno" name="l727" href="#l727">727 </a>            || move-&gt;fromrow == <span class="c2html-macroconst">POS_UNSPECIFIED</span>) {
   8.780 -<a class="c2html-lineno" name="l728" href="#l728">728 </a>            <span class="c2html-keyword">return</span> getlocation(gamestate, move);
   8.781 -<a class="c2html-lineno" name="l729" href="#l729">729 </a>        } <span class="c2html-keyword">else</span> {
   8.782 -<a class="c2html-lineno" name="l730" href="#l730">730 </a>            <span class="c2html-keyword">return</span> chkidx(move) ? <span class="c2html-macroconst">VALID_MOVE_SYNTAX</span> : <span class="c2html-macroconst">INVALID_POSITION</span>;
   8.783 -<a class="c2html-lineno" name="l731" href="#l731">731 </a>        }
   8.784 -<a class="c2html-lineno" name="l732" href="#l732">732 </a>    } <span class="c2html-keyword">else</span> {
   8.785 -<a class="c2html-lineno" name="l733" href="#l733">733 </a>        <span class="c2html-keyword">return</span> <span class="c2html-macroconst">INVALID_MOVE_SYNTAX</span>;
   8.786 -<a class="c2html-lineno" name="l734" href="#l734">734 </a>    }
   8.787 -<a class="c2html-lineno" name="l735" href="#l735">735 </a>}
   8.788 -<a class="c2html-lineno" name="l736" href="#l736">736 </a>
   8.789 -<a class="c2html-lineno" name="l737" href="#l737">737 </a>_Bool is_protected(GameState *gamestate, <span class="c2html-type">uint8_t</span> row, <span class="c2html-type">uint8_t</span> file,
   8.790 -<a class="c2html-lineno" name="l738" href="#l738">738 </a>        <span class="c2html-type">uint8_t</span> color) {
   8.791 -<a class="c2html-lineno" name="l739" href="#l739">739 </a>    
   8.792 -<a class="c2html-lineno" name="l740" href="#l740">740 </a>    Move threats[<span class="c2html-macroconst">16</span>];
   8.793 -<a class="c2html-lineno" name="l741" href="#l741">741 </a>    <span class="c2html-type">uint8_t</span> threatcount;
   8.794 -<a class="c2html-lineno" name="l742" href="#l742">742 </a>    <span class="c2html-keyword">if</span> (get_real_threats(gamestate, row, file, color, threats, &threatcount)) {
   8.795 -<a class="c2html-lineno" name="l743" href="#l743">743 </a>        <span class="c2html-keyword">for</span> (<span class="c2html-keyword">int</span> i = <span class="c2html-macroconst">0</span> ; i &lt; threatcount ; i++) {
   8.796 -<a class="c2html-lineno" name="l744" href="#l744">744 </a>            <span class="c2html-keyword">if</span> (threats[i].piece != (color|<span class="c2html-macroconst">KING</span>)) {
   8.797 -<a class="c2html-lineno" name="l745" href="#l745">745 </a>                <span class="c2html-keyword">return</span> <span class="c2html-macroconst">1</span>;
   8.798 -<a class="c2html-lineno" name="l746" href="#l746">746 </a>            }
   8.799 -<a class="c2html-lineno" name="l747" href="#l747">747 </a>        }
   8.800 -<a class="c2html-lineno" name="l748" href="#l748">748 </a>        <span class="c2html-keyword">return</span> <span class="c2html-macroconst">0</span>;
   8.801 -<a class="c2html-lineno" name="l749" href="#l749">749 </a>    } <span class="c2html-keyword">else</span> {
   8.802 -<a class="c2html-lineno" name="l750" href="#l750">750 </a>        <span class="c2html-keyword">return</span> <span class="c2html-macroconst">0</span>;
   8.803 -<a class="c2html-lineno" name="l751" href="#l751">751 </a>    }
   8.804 -<a class="c2html-lineno" name="l752" href="#l752">752 </a>}
   8.805 -<a class="c2html-lineno" name="l753" href="#l753">753 </a>
   8.806 -<a class="c2html-lineno" name="l754" href="#l754">754 </a><span class="c2html-type">uint16_t</span> remaining_movetime(GameInfo *gameinfo, GameState *gamestate,
   8.807 -<a class="c2html-lineno" name="l755" href="#l755">755 </a>        <span class="c2html-type">uint8_t</span> color) {
   8.808 -<a class="c2html-lineno" name="l756" href="#l756">756 </a>    <span class="c2html-keyword">if</span> (!gameinfo-&gt;timecontrol) {
   8.809 -<a class="c2html-lineno" name="l757" href="#l757">757 </a>        <span class="c2html-keyword">return</span> <span class="c2html-macroconst">0</span>;
   8.810 -<a class="c2html-lineno" name="l758" href="#l758">758 </a>    }
   8.811 -<a class="c2html-lineno" name="l759" href="#l759">759 </a>    
   8.812 -<a class="c2html-lineno" name="l760" href="#l760">760 </a>    <span class="c2html-keyword">if</span> (gamestate-&gt;movelist) {
   8.813 -<a class="c2html-lineno" name="l761" href="#l761">761 </a>        <span class="c2html-type">uint16_t</span> time = gameinfo-&gt;time;
   8.814 -<a class="c2html-lineno" name="l762" href="#l762">762 </a>        <span class="c2html-type">suseconds_t</span> micros = <span class="c2html-macroconst">0</span>;
   8.815 -<a class="c2html-lineno" name="l763" href="#l763">763 </a>        
   8.816 -<a class="c2html-lineno" name="l764" href="#l764">764 </a>        MoveList *movelist = color == <span class="c2html-macroconst">WHITE</span> ?
   8.817 -<a class="c2html-lineno" name="l765" href="#l765">765 </a>            gamestate-&gt;movelist : gamestate-&gt;movelist-&gt;next;
   8.818 -<a class="c2html-lineno" name="l766" href="#l766">766 </a>        
   8.819 -<a class="c2html-lineno" name="l767" href="#l767">767 </a>        <span class="c2html-keyword">while</span> (movelist) {
   8.820 -<a class="c2html-lineno" name="l768" href="#l768">768 </a>            time += gameinfo-&gt;addtime;
   8.821 -<a class="c2html-lineno" name="l769" href="#l769">769 </a>            
   8.822 -<a class="c2html-lineno" name="l770" href="#l770">770 </a>            <span class="c2html-keyword">struct</span> movetimeval *movetime = &(movelist-&gt;move.movetime);
   8.823 -<a class="c2html-lineno" name="l771" href="#l771">771 </a>            <span class="c2html-keyword">if</span> (movetime-&gt;tv_sec &gt;= time) {
   8.824 -<a class="c2html-lineno" name="l772" href="#l772">772 </a>                <span class="c2html-keyword">return</span> <span class="c2html-macroconst">0</span>;
   8.825 -<a class="c2html-lineno" name="l773" href="#l773">773 </a>            }
   8.826 -<a class="c2html-lineno" name="l774" href="#l774">774 </a>            
   8.827 -<a class="c2html-lineno" name="l775" href="#l775">775 </a>            time -= movetime-&gt;tv_sec;
   8.828 -<a class="c2html-lineno" name="l776" href="#l776">776 </a>            micros += movetime-&gt;tv_usec;
   8.829 -<a class="c2html-lineno" name="l777" href="#l777">777 </a>            
   8.830 -<a class="c2html-lineno" name="l778" href="#l778">778 </a>            movelist = movelist-&gt;next ? movelist-&gt;next-&gt;next : <span class="c2html-macroconst">NULL</span>;
   8.831 -<a class="c2html-lineno" name="l779" href="#l779">779 </a>        }
   8.832 -<a class="c2html-lineno" name="l780" href="#l780">780 </a>        
   8.833 -<a class="c2html-lineno" name="l781" href="#l781">781 </a>        <span class="c2html-type">time_t</span> sec;
   8.834 -<a class="c2html-lineno" name="l782" href="#l782">782 </a>        movelist = gamestate-&gt;lastmove;
   8.835 -<a class="c2html-lineno" name="l783" href="#l783">783 </a>        <span class="c2html-keyword">if</span> ((movelist-&gt;move.piece & <span class="c2html-macroconst">COLOR_MASK</span>) != color) {
   8.836 -<a class="c2html-lineno" name="l784" href="#l784">784 </a>            <span class="c2html-keyword">struct</span> movetimeval *lastmovetstamp = &(movelist-&gt;move.timestamp);
   8.837 -<a class="c2html-lineno" name="l785" href="#l785">785 </a>            <span class="c2html-keyword">struct</span> timeval currenttstamp;
   8.838 -<a class="c2html-lineno" name="l786" href="#l786">786 </a>            gettimeofday(&currenttstamp, <span class="c2html-macroconst">NULL</span>);
   8.839 -<a class="c2html-lineno" name="l787" href="#l787">787 </a>            micros += currenttstamp.tv_usec - lastmovetstamp-&gt;tv_usec;
   8.840 -<a class="c2html-lineno" name="l788" href="#l788">788 </a>            sec = currenttstamp.tv_sec - lastmovetstamp-&gt;tv_sec;
   8.841 -<a class="c2html-lineno" name="l789" href="#l789">789 </a>            <span class="c2html-keyword">if</span> (sec &gt;= time) {
   8.842 -<a class="c2html-lineno" name="l790" href="#l790">790 </a>                <span class="c2html-keyword">return</span> <span class="c2html-macroconst">0</span>;
   8.843 -<a class="c2html-lineno" name="l791" href="#l791">791 </a>            }
   8.844 -<a class="c2html-lineno" name="l792" href="#l792">792 </a>            
   8.845 -<a class="c2html-lineno" name="l793" href="#l793">793 </a>            time -= sec;
   8.846 -<a class="c2html-lineno" name="l794" href="#l794">794 </a>        }
   8.847 -<a class="c2html-lineno" name="l795" href="#l795">795 </a>        
   8.848 -<a class="c2html-lineno" name="l796" href="#l796">796 </a>        sec = micros / 1e6L;
   8.849 -<a class="c2html-lineno" name="l797" href="#l797">797 </a>        
   8.850 -<a class="c2html-lineno" name="l798" href="#l798">798 </a>        <span class="c2html-keyword">if</span> (sec &gt;= time) {
   8.851 -<a class="c2html-lineno" name="l799" href="#l799">799 </a>            <span class="c2html-keyword">return</span> <span class="c2html-macroconst">0</span>;
   8.852 -<a class="c2html-lineno" name="l800" href="#l800">800 </a>        }
   8.853 -<a class="c2html-lineno" name="l801" href="#l801">801 </a>
   8.854 -<a class="c2html-lineno" name="l802" href="#l802">802 </a>        time -= sec;
   8.855 -<a class="c2html-lineno" name="l803" href="#l803">803 </a>        
   8.856 -<a class="c2html-lineno" name="l804" href="#l804">804 </a>        <span class="c2html-keyword">return</span> time;
   8.857 -<a class="c2html-lineno" name="l805" href="#l805">805 </a>    } <span class="c2html-keyword">else</span> {
   8.858 -<a class="c2html-lineno" name="l806" href="#l806">806 </a>        <span class="c2html-keyword">return</span> gameinfo-&gt;time;
   8.859 -<a class="c2html-lineno" name="l807" href="#l807">807 </a>    }
   8.860 -<a class="c2html-lineno" name="l808" href="#l808">808 </a>}
   8.861 -</pre>
   8.862 -  </body>
   8.863 -</html>
   8.864 -
     9.1 --- a/test/golden-master/ctest.html	Mon Oct 03 12:56:28 2022 +0200
     9.2 +++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
     9.3 @@ -1,444 +0,0 @@
     9.4 -<!DOCTYPE html>
     9.5 -<html>
     9.6 -  <head>
     9.7 -    <title>c2html</title>
     9.8 -    <style type="text/css">
     9.9 -      a.c2html-lineno {
    9.10 -        /* as long as user-select isn't widely spread, we throw the bomb */
    9.11 -        -webkit-user-select: none;
    9.12 -        -moz-user-select: none;
    9.13 -        -ms-user-select: none;
    9.14 -        user-select: none;
    9.15 -        display: inline-block;
    9.16 -        font-style: italic;
    9.17 -        text-decoration: none;
    9.18 -        color: grey;
    9.19 -      }
    9.20 -      span.c2html-keyword {
    9.21 -        color: blue;
    9.22 -      }
    9.23 -      span.c2html-macroconst {
    9.24 -        color: cornflowerblue;
    9.25 -      }
    9.26 -      span.c2html-type {
    9.27 -        color: cornflowerblue;
    9.28 -      }
    9.29 -      span.c2html-directive {
    9.30 -        color: green;
    9.31 -      }
    9.32 -      span.c2html-string {
    9.33 -        color: darkorange;
    9.34 -      }
    9.35 -      span.c2html-comment {
    9.36 -        color: grey;
    9.37 -      }
    9.38 -      span.c2html-stdinclude {
    9.39 -        color: darkorange;
    9.40 -      }
    9.41 -      span.c2html-userinclude {
    9.42 -        color: darkorange;
    9.43 -      }
    9.44 -      a.c2html-userinclude {
    9.45 -        color: darkorange;
    9.46 -        text-decoration: underline;
    9.47 -      }
    9.48 -    </style>
    9.49 -  </head>
    9.50 -  <body>
    9.51 -
    9.52 -<pre>
    9.53 -<a class="c2html-lineno" name="l1" href="#l1">  1 </a><span class="c2html-comment">/*</span>
    9.54 -<a class="c2html-lineno" name="l2" href="#l2">  2 </a><span class="c2html-comment"> * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.</span>
    9.55 -<a class="c2html-lineno" name="l3" href="#l3">  3 </a><span class="c2html-comment"> *</span>
    9.56 -<a class="c2html-lineno" name="l4" href="#l4">  4 </a><span class="c2html-comment"> * Copyright 2015 Olaf Wintermann. All rights reserved.</span>
    9.57 -<a class="c2html-lineno" name="l5" href="#l5">  5 </a><span class="c2html-comment"> *</span>
    9.58 -<a class="c2html-lineno" name="l6" href="#l6">  6 </a><span class="c2html-comment"> * Redistribution and use in source and binary forms, with or without</span>
    9.59 -<a class="c2html-lineno" name="l7" href="#l7">  7 </a><span class="c2html-comment"> * modification, are permitted provided that the following conditions are met:</span>
    9.60 -<a class="c2html-lineno" name="l8" href="#l8">  8 </a><span class="c2html-comment"> *</span>
    9.61 -<a class="c2html-lineno" name="l9" href="#l9">  9 </a><span class="c2html-comment"> *   1. Redistributions of source code must retain the above copyright</span>
    9.62 -<a class="c2html-lineno" name="l10" href="#l10"> 10 </a><span class="c2html-comment"> *      notice, this list of conditions and the following disclaimer.</span>
    9.63 -<a class="c2html-lineno" name="l11" href="#l11"> 11 </a><span class="c2html-comment"> *</span>
    9.64 -<a class="c2html-lineno" name="l12" href="#l12"> 12 </a><span class="c2html-comment"> *   2. Redistributions in binary form must reproduce the above copyright</span>
    9.65 -<a class="c2html-lineno" name="l13" href="#l13"> 13 </a><span class="c2html-comment"> *      notice, this list of conditions and the following disclaimer in the</span>
    9.66 -<a class="c2html-lineno" name="l14" href="#l14"> 14 </a><span class="c2html-comment"> *      documentation and/or other materials provided with the distribution.</span>
    9.67 -<a class="c2html-lineno" name="l15" href="#l15"> 15 </a><span class="c2html-comment"> *</span>
    9.68 -<a class="c2html-lineno" name="l16" href="#l16"> 16 </a><span class="c2html-comment"> * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"</span>
    9.69 -<a class="c2html-lineno" name="l17" href="#l17"> 17 </a><span class="c2html-comment"> * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE</span>
    9.70 -<a class="c2html-lineno" name="l18" href="#l18"> 18 </a><span class="c2html-comment"> * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE</span>
    9.71 -<a class="c2html-lineno" name="l19" href="#l19"> 19 </a><span class="c2html-comment"> * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE</span>
    9.72 -<a class="c2html-lineno" name="l20" href="#l20"> 20 </a><span class="c2html-comment"> * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR</span>
    9.73 -<a class="c2html-lineno" name="l21" href="#l21"> 21 </a><span class="c2html-comment"> * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF</span>
    9.74 -<a class="c2html-lineno" name="l22" href="#l22"> 22 </a><span class="c2html-comment"> * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS</span>
    9.75 -<a class="c2html-lineno" name="l23" href="#l23"> 23 </a><span class="c2html-comment"> * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN</span>
    9.76 -<a class="c2html-lineno" name="l24" href="#l24"> 24 </a><span class="c2html-comment"> * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)</span>
    9.77 -<a class="c2html-lineno" name="l25" href="#l25"> 25 </a><span class="c2html-comment"> * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE</span>
    9.78 -<a class="c2html-lineno" name="l26" href="#l26"> 26 </a><span class="c2html-comment"> * POSSIBILITY OF SUCH DAMAGE.</span>
    9.79 -<a class="c2html-lineno" name="l27" href="#l27"> 27 </a><span class="c2html-comment"> */</span>
    9.80 -<a class="c2html-lineno" name="l28" href="#l28"> 28 </a>
    9.81 -<a class="c2html-lineno" name="l29" href="#l29"> 29 </a><span class="c2html-directive">#include</span> <span class="c2html-stdinclude">&lt;time.h&gt;</span>
    9.82 -<a class="c2html-lineno" name="l30" href="#l30"> 30 </a><span class="c2html-directive">#include</span> <span class="c2html-stdinclude">&lt;stdio.h&gt;</span>
    9.83 -<a class="c2html-lineno" name="l31" href="#l31"> 31 </a><span class="c2html-directive">#include</span> <span class="c2html-stdinclude">&lt;stdlib.h&gt;</span>
    9.84 -<a class="c2html-lineno" name="l32" href="#l32"> 32 </a><span class="c2html-directive">#include</span> <span class="c2html-stdinclude">&lt;string.h&gt;</span>
    9.85 -<a class="c2html-lineno" name="l33" href="#l33"> 33 </a><span class="c2html-directive">#include</span> <span class="c2html-stdinclude">&lt;ucx/string.h&gt;</span>
    9.86 -<a class="c2html-lineno" name="l34" href="#l34"> 34 </a><span class="c2html-directive">#include</span> <span class="c2html-stdinclude">&lt;ucx/buffer.h&gt;</span>
    9.87 -<a class="c2html-lineno" name="l35" href="#l35"> 35 </a><span class="c2html-directive">#include</span> <span class="c2html-stdinclude">&lt;ucx/utils.h&gt;</span>
    9.88 -<a class="c2html-lineno" name="l36" href="#l36"> 36 </a><span class="c2html-directive">#include</span> <span class="c2html-stdinclude">&lt;libxml/tree.h&gt;</span>
    9.89 -<a class="c2html-lineno" name="l37" href="#l37"> 37 </a><span class="c2html-directive">#include</span> <span class="c2html-stdinclude">&lt;curl/curl.h&gt;</span>
    9.90 -<a class="c2html-lineno" name="l38" href="#l38"> 38 </a>
    9.91 -<a class="c2html-lineno" name="l39" href="#l39"> 39 </a><span class="c2html-directive">#include</span> <span class="c2html-stdinclude">&lt;openssl/sha.h&gt;</span>
    9.92 -<a class="c2html-lineno" name="l40" href="#l40"> 40 </a><span class="c2html-directive">#include</span> <span class="c2html-stdinclude">&lt;openssl/hmac.h&gt;</span>
    9.93 -<a class="c2html-lineno" name="l41" href="#l41"> 41 </a><span class="c2html-directive">#include</span> <span class="c2html-stdinclude">&lt;openssl/evp.h&gt;</span>
    9.94 -<a class="c2html-lineno" name="l42" href="#l42"> 42 </a><span class="c2html-directive">#include</span> <span class="c2html-stdinclude">&lt;openssl/bio.h&gt;</span>
    9.95 -<a class="c2html-lineno" name="l43" href="#l43"> 43 </a><span class="c2html-directive">#include</span> <span class="c2html-stdinclude">&lt;openssl/buffer.h&gt;</span>
    9.96 -<a class="c2html-lineno" name="l44" href="#l44"> 44 </a><span class="c2html-directive">#include</span> <span class="c2html-stdinclude">&lt;openssl/rand.h&gt;</span>
    9.97 -<a class="c2html-lineno" name="l45" href="#l45"> 45 </a>
    9.98 -<a class="c2html-lineno" name="l46" href="#l46"> 46 </a><span class="c2html-directive">#include</span> <a class="c2html-userinclude" href="utils.h">"utils.h"</a>
    9.99 -<a class="c2html-lineno" name="l47" href="#l47"> 47 </a><span class="c2html-directive">#include</span> <a class="c2html-userinclude" href="crypto.h">"crypto.h"</a>
   9.100 -<a class="c2html-lineno" name="l48" href="#l48"> 48 </a><span class="c2html-directive">#include</span> <a class="c2html-userinclude" href="webdav.h">"webdav.h"</a>
   9.101 -<a class="c2html-lineno" name="l49" href="#l49"> 49 </a>
   9.102 -<a class="c2html-lineno" name="l50" href="#l50"> 50 </a><span class="c2html-directive">#define</span> <span class="c2html-macroconst">MACRO1337</span> <span class="c2html-macroconst">1337L</span>
   9.103 -<a class="c2html-lineno" name="l51" href="#l51"> 51 </a>
   9.104 -<a class="c2html-lineno" name="l52" href="#l52"> 52 </a><span class="c2html-comment">/* -------------------- This is a testing file. -------------------------- */</span>
   9.105 -<a class="c2html-lineno" name="l53" href="#l53"> 53 </a><span class="c2html-comment">/*</span>
   9.106 -<a class="c2html-lineno" name="l54" href="#l54"> 54 </a><span class="c2html-comment">time_t util_parse_creationdate(char *str) {</span>
   9.107 -<a class="c2html-lineno" name="l55" href="#l55"> 55 </a><span class="c2html-comment">    // example: 2012-11-29T21:35:35Z</span>
   9.108 -<a class="c2html-lineno" name="l56" href="#l56"> 56 </a><span class="c2html-comment">    if(!str) {</span>
   9.109 -<a class="c2html-lineno" name="l57" href="#l57"> 57 </a><span class="c2html-comment">        return 0;</span>
   9.110 -<a class="c2html-lineno" name="l58" href="#l58"> 58 </a><span class="c2html-comment">    }</span>
   9.111 -<a class="c2html-lineno" name="l59" href="#l59"> 59 </a><span class="c2html-comment">    // TODO</span>
   9.112 -<a class="c2html-lineno" name="l60" href="#l60"> 60 </a><span class="c2html-comment">    return 0;</span>
   9.113 -<a class="c2html-lineno" name="l61" href="#l61"> 61 </a><span class="c2html-comment">}</span>
   9.114 -<a class="c2html-lineno" name="l62" href="#l62"> 62 </a><span class="c2html-comment">*/</span>
   9.115 -<a class="c2html-lineno" name="l63" href="#l63"> 63 </a><span class="c2html-type">time_t</span> util_parse_lastmodified(<span class="c2html-keyword">char</span> *str) {
   9.116 -<a class="c2html-lineno" name="l64" href="#l64"> 64 </a>    <span class="c2html-comment">// example: Thu, 29 Nov 2012 21:35:35 GMT</span>
   9.117 -<a class="c2html-lineno" name="l65" href="#l65"> 65 </a>    <span class="c2html-keyword">if</span>(!str) {
   9.118 -<a class="c2html-lineno" name="l66" href="#l66"> 66 </a>        <span class="c2html-keyword">return</span> <span class="c2html-macroconst">0</span>;
   9.119 -<a class="c2html-lineno" name="l67" href="#l67"> 67 </a>    } <span class="c2html-keyword">else</span> {
   9.120 -<a class="c2html-lineno" name="l68" href="#l68"> 68 </a>        <span class="c2html-keyword">return</span> curl_getdate(str, <span class="c2html-macroconst">NULL</span>);
   9.121 -<a class="c2html-lineno" name="l69" href="#l69"> 69 </a>    }
   9.122 -<a class="c2html-lineno" name="l70" href="#l70"> 70 </a>}
   9.123 -<a class="c2html-lineno" name="l71" href="#l71"> 71 </a>
   9.124 -<a class="c2html-lineno" name="l72" href="#l72"> 72 </a><span class="c2html-keyword">int</span> util_getboolean(<span class="c2html-keyword">char</span> *v) {
   9.125 -<a class="c2html-lineno" name="l73" href="#l73"> 73 </a>    <span class="c2html-keyword">if</span>(v[<span class="c2html-macroconst">0</span>] == <span class="c2html-string">'T'</span> || v[<span class="c2html-macroconst">0</span>] == <span class="c2html-string">'t'</span>) {
   9.126 -<a class="c2html-lineno" name="l74" href="#l74"> 74 </a>        <span class="c2html-keyword">return</span> <span class="c2html-macroconst">1</span>;
   9.127 -<a class="c2html-lineno" name="l75" href="#l75"> 75 </a>    }
   9.128 -<a class="c2html-lineno" name="l76" href="#l76"> 76 </a>    <span class="c2html-keyword">return</span> <span class="c2html-macroconst">0</span>;
   9.129 -<a class="c2html-lineno" name="l77" href="#l77"> 77 </a>}
   9.130 -<a class="c2html-lineno" name="l78" href="#l78"> 78 </a>
   9.131 -<a class="c2html-lineno" name="l79" href="#l79"> 79 </a><span class="c2html-keyword">int</span> util_strtoint(<span class="c2html-keyword">char</span> *str, <span class="c2html-type">int64_t</span> *value) {
   9.132 -<a class="c2html-lineno" name="l80" href="#l80"> 80 </a>    <span class="c2html-keyword">char</span> *end;
   9.133 -<a class="c2html-lineno" name="l81" href="#l81"> 81 </a>    <span class="c2html-type">int64_t</span> val = strtoll(str, &end, <span class="c2html-macroconst">0</span>);
   9.134 -<a class="c2html-lineno" name="l82" href="#l82"> 82 </a>    <span class="c2html-keyword">if</span>(strlen(end) == <span class="c2html-macroconst">0</span>) {
   9.135 -<a class="c2html-lineno" name="l83" href="#l83"> 83 </a>        *value = val;
   9.136 -<a class="c2html-lineno" name="l84" href="#l84"> 84 </a>        <span class="c2html-keyword">return</span> <span class="c2html-macroconst">1</span>;
   9.137 -<a class="c2html-lineno" name="l85" href="#l85"> 85 </a>    } <span class="c2html-keyword">else</span> {
   9.138 -<a class="c2html-lineno" name="l86" href="#l86"> 86 </a>        <span class="c2html-keyword">return</span> <span class="c2html-macroconst">0</span>;
   9.139 -<a class="c2html-lineno" name="l87" href="#l87"> 87 </a>    }
   9.140 -<a class="c2html-lineno" name="l88" href="#l88"> 88 </a>}
   9.141 -<a class="c2html-lineno" name="l89" href="#l89"> 89 </a>
   9.142 -<a class="c2html-lineno" name="l90" href="#l90"> 90 </a><span class="c2html-keyword">char</span>* util_url_path(<span class="c2html-keyword">char</span> *url) { 
   9.143 -<a class="c2html-lineno" name="l91" href="#l91"> 91 </a>    <span class="c2html-keyword">char</span> *path = <span class="c2html-macroconst">NULL</span>;
   9.144 -<a class="c2html-lineno" name="l92" href="#l92"> 92 </a>    <span class="c2html-type">size_t</span> len = strlen(url);
   9.145 -<a class="c2html-lineno" name="l93" href="#l93"> 93 </a>    <span class="c2html-keyword">int</span> slashcount = <span class="c2html-macroconst">0</span>;
   9.146 -<a class="c2html-lineno" name="l94" href="#l94"> 94 </a>    <span class="c2html-keyword">int</span> slmax;
   9.147 -<a class="c2html-lineno" name="l95" href="#l95"> 95 </a>    <span class="c2html-keyword">if</span>(len &gt; <span class="c2html-macroconst">7</span> && !strncasecmp(url, <span class="c2html-string">"http://"</span>, <span class="c2html-macroconst">7</span>)) {
   9.148 -<a class="c2html-lineno" name="l96" href="#l96"> 96 </a>        slmax = <span class="c2html-macroconst">3</span>;
   9.149 -<a class="c2html-lineno" name="l97" href="#l97"> 97 </a>    } <span class="c2html-keyword">else</span> <span class="c2html-keyword">if</span>(len &gt; <span class="c2html-macroconst">8</span> && !strncasecmp(url, <span class="c2html-string">"https://"</span>, <span class="c2html-macroconst">8</span>)) {
   9.150 -<a class="c2html-lineno" name="l98" href="#l98"> 98 </a>        slmax = <span class="c2html-macroconst">3</span>;
   9.151 -<a class="c2html-lineno" name="l99" href="#l99"> 99 </a>    } <span class="c2html-keyword">else</span> {
   9.152 -<a class="c2html-lineno" name="l100" href="#l100">100 </a>        slmax = <span class="c2html-macroconst">1</span>;
   9.153 -<a class="c2html-lineno" name="l101" href="#l101">101 </a>    }
   9.154 -<a class="c2html-lineno" name="l102" href="#l102">102 </a>    <span class="c2html-keyword">char</span> c;
   9.155 -<a class="c2html-lineno" name="l103" href="#l103">103 </a>    <span class="c2html-keyword">for</span>(<span class="c2html-keyword">int</span> i=<span class="c2html-macroconst">0</span>;i&lt;len;i++) {
   9.156 -<a class="c2html-lineno" name="l104" href="#l104">104 </a>        c = url[i];
   9.157 -<a class="c2html-lineno" name="l105" href="#l105">105 </a>        <span class="c2html-keyword">if</span>(c == <span class="c2html-string">'/'</span>) {
   9.158 -<a class="c2html-lineno" name="l106" href="#l106">106 </a>            slashcount++;
   9.159 -<a class="c2html-lineno" name="l107" href="#l107">107 </a>            <span class="c2html-keyword">if</span>(slashcount == slmax) {
   9.160 -<a class="c2html-lineno" name="l108" href="#l108">108 </a>                path = url + i;
   9.161 -<a class="c2html-lineno" name="l109" href="#l109">109 </a>                <span class="c2html-keyword">break</span>;
   9.162 -<a class="c2html-lineno" name="l110" href="#l110">110 </a>            }
   9.163 -<a class="c2html-lineno" name="l111" href="#l111">111 </a>        }
   9.164 -<a class="c2html-lineno" name="l112" href="#l112">112 </a>    } 
   9.165 -<a class="c2html-lineno" name="l113" href="#l113">113 </a>    <span class="c2html-keyword">return</span> path;
   9.166 -<a class="c2html-lineno" name="l114" href="#l114">114 </a>}
   9.167 -<a class="c2html-lineno" name="l115" href="#l115">115 </a>
   9.168 -<a class="c2html-lineno" name="l116" href="#l116">116 </a><span class="c2html-keyword">char</span>* util_url_decode(DavSession *sn, <span class="c2html-keyword">char</span> *url) {
   9.169 -<a class="c2html-lineno" name="l117" href="#l117">117 </a>    <span class="c2html-keyword">char</span> *unesc = curl_easy_unescape(sn-&gt;handle, url, strlen(url), <span class="c2html-macroconst">NULL</span>);
   9.170 -<a class="c2html-lineno" name="l118" href="#l118">118 </a>    <span class="c2html-keyword">char</span> *ret = strdup(unesc);
   9.171 -<a class="c2html-lineno" name="l119" href="#l119">119 </a>    curl_free(unesc);
   9.172 -<a class="c2html-lineno" name="l120" href="#l120">120 </a>    <span class="c2html-keyword">return</span> ret;
   9.173 -<a class="c2html-lineno" name="l121" href="#l121">121 </a>}
   9.174 -<a class="c2html-lineno" name="l122" href="#l122">122 </a>
   9.175 -<a class="c2html-lineno" name="l123" href="#l123">123 </a><span class="c2html-keyword">char</span>* util_resource_name(<span class="c2html-keyword">char</span> *url) {
   9.176 -<a class="c2html-lineno" name="l124" href="#l124">124 </a>    <span class="c2html-keyword">int</span> si = <span class="c2html-macroconst">0</span>;
   9.177 -<a class="c2html-lineno" name="l125" href="#l125">125 </a>    <span class="c2html-keyword">int</span> osi = <span class="c2html-macroconst">0</span>;
   9.178 -<a class="c2html-lineno" name="l126" href="#l126">126 </a>    <span class="c2html-keyword">int</span> i = <span class="c2html-macroconst">0</span>;
   9.179 -<a class="c2html-lineno" name="l127" href="#l127">127 </a>    <span class="c2html-keyword">int</span> p = <span class="c2html-macroconst">0</span>;
   9.180 -<a class="c2html-lineno" name="l128" href="#l128">128 </a>    <span class="c2html-keyword">char</span> c;
   9.181 -<a class="c2html-lineno" name="l129" href="#l129">129 </a>    <span class="c2html-keyword">while</span>((c = url[i]) != <span class="c2html-macroconst">0</span>) {
   9.182 -<a class="c2html-lineno" name="l130" href="#l130">130 </a>        <span class="c2html-keyword">if</span>(c == <span class="c2html-string">'/'</span>) {
   9.183 -<a class="c2html-lineno" name="l131" href="#l131">131 </a>            osi = si;
   9.184 -<a class="c2html-lineno" name="l132" href="#l132">132 </a>            si = i;
   9.185 -<a class="c2html-lineno" name="l133" href="#l133">133 </a>            p = <span class="c2html-macroconst">1</span>;
   9.186 -<a class="c2html-lineno" name="l134" href="#l134">134 </a>        }
   9.187 -<a class="c2html-lineno" name="l135" href="#l135">135 </a>        i++;
   9.188 -<a class="c2html-lineno" name="l136" href="#l136">136 </a>    }
   9.189 -<a class="c2html-lineno" name="l137" href="#l137">137 </a>    
   9.190 -<a class="c2html-lineno" name="l138" href="#l138">138 </a>    <span class="c2html-keyword">char</span> *name = url + si + p;
   9.191 -<a class="c2html-lineno" name="l139" href="#l139">139 </a>    <span class="c2html-keyword">if</span>(name[<span class="c2html-macroconst">0</span>] == <span class="c2html-macroconst">0</span>) {
   9.192 -<a class="c2html-lineno" name="l140" href="#l140">140 </a>        name = url + osi + p;
   9.193 -<a class="c2html-lineno" name="l141" href="#l141">141 </a>        <span class="c2html-keyword">if</span>(name[<span class="c2html-macroconst">0</span>] == <span class="c2html-macroconst">0</span>) {
   9.194 -<a class="c2html-lineno" name="l142" href="#l142">142 </a>            <span class="c2html-keyword">return</span> url;
   9.195 -<a class="c2html-lineno" name="l143" href="#l143">143 </a>        }
   9.196 -<a class="c2html-lineno" name="l144" href="#l144">144 </a>    }
   9.197 -<a class="c2html-lineno" name="l145" href="#l145">145 </a>    
   9.198 -<a class="c2html-lineno" name="l146" href="#l146">146 </a>    <span class="c2html-keyword">return</span> name;
   9.199 -<a class="c2html-lineno" name="l147" href="#l147">147 </a>}
   9.200 -<a class="c2html-lineno" name="l148" href="#l148">148 </a>
   9.201 -<a class="c2html-lineno" name="l149" href="#l149">149 </a><span class="c2html-keyword">int</span> util_mkdir(<span class="c2html-keyword">char</span> *path, <span class="c2html-type">mode_t</span> mode) {
   9.202 -<a class="c2html-lineno" name="l150" href="#l150">150 </a><span class="c2html-directive">#ifdef</span> <span class="c2html-macroconst">_WIN32</span>
   9.203 -<a class="c2html-lineno" name="l151" href="#l151">151 </a>    <span class="c2html-keyword">return</span> mkdir(path);
   9.204 -<a class="c2html-lineno" name="l152" href="#l152">152 </a><span class="c2html-directive">#else</span>
   9.205 -<a class="c2html-lineno" name="l153" href="#l153">153 </a>    <span class="c2html-keyword">return</span> mkdir(path, mode);
   9.206 -<a class="c2html-lineno" name="l154" href="#l154">154 </a><span class="c2html-directive">#endif</span>
   9.207 -<a class="c2html-lineno" name="l155" href="#l155">155 </a>}
   9.208 -<a class="c2html-lineno" name="l156" href="#l156">156 </a>
   9.209 -<a class="c2html-lineno" name="l157" href="#l157">157 </a><span class="c2html-keyword">char</span>* util_concat_path(<span class="c2html-keyword">char</span> *url_base, <span class="c2html-keyword">char</span> *p) {
   9.210 -<a class="c2html-lineno" name="l158" href="#l158">158 </a>    <span class="c2html-type">sstr_t</span> base = sstr(url_base);
   9.211 -<a class="c2html-lineno" name="l159" href="#l159">159 </a>    <span class="c2html-type">sstr_t</span> path;
   9.212 -<a class="c2html-lineno" name="l160" href="#l160">160 </a>    <span class="c2html-keyword">if</span>(p) {
   9.213 -<a class="c2html-lineno" name="l161" href="#l161">161 </a>        path = sstr(p);
   9.214 -<a class="c2html-lineno" name="l162" href="#l162">162 </a>    } <span class="c2html-keyword">else</span> {
   9.215 -<a class="c2html-lineno" name="l163" href="#l163">163 </a>        path = sstrn(<span class="c2html-string">""</span>, <span class="c2html-macroconst">0</span>);
   9.216 -<a class="c2html-lineno" name="l164" href="#l164">164 </a>    }
   9.217 -<a class="c2html-lineno" name="l165" href="#l165">165 </a>    
   9.218 -<a class="c2html-lineno" name="l166" href="#l166">166 </a>    <span class="c2html-keyword">int</span> add_separator = <span class="c2html-macroconst">0</span>;
   9.219 -<a class="c2html-lineno" name="l167" href="#l167">167 </a>    <span class="c2html-keyword">if</span>(base.ptr[base.length-<span class="c2html-macroconst">1</span>] == <span class="c2html-string">'/'</span>) {
   9.220 -<a class="c2html-lineno" name="l168" href="#l168">168 </a>        <span class="c2html-keyword">if</span>(path.ptr[<span class="c2html-macroconst">0</span>] == <span class="c2html-string">'/'</span>) {
   9.221 -<a class="c2html-lineno" name="l169" href="#l169">169 </a>            base.length--;
   9.222 -<a class="c2html-lineno" name="l170" href="#l170">170 </a>        }
   9.223 -<a class="c2html-lineno" name="l171" href="#l171">171 </a>    } <span class="c2html-keyword">else</span> {
   9.224 -<a class="c2html-lineno" name="l172" href="#l172">172 </a>        <span class="c2html-keyword">if</span>(path.length == <span class="c2html-macroconst">0</span> || path.ptr[<span class="c2html-macroconst">0</span>] != <span class="c2html-string">'/'</span>) {
   9.225 -<a class="c2html-lineno" name="l173" href="#l173">173 </a>            add_separator = <span class="c2html-macroconst">1</span>;
   9.226 -<a class="c2html-lineno" name="l174" href="#l174">174 </a>        }
   9.227 -<a class="c2html-lineno" name="l175" href="#l175">175 </a>    }
   9.228 -<a class="c2html-lineno" name="l176" href="#l176">176 </a>    
   9.229 -<a class="c2html-lineno" name="l177" href="#l177">177 </a>    <span class="c2html-type">sstr_t</span> url;
   9.230 -<a class="c2html-lineno" name="l178" href="#l178">178 </a>    <span class="c2html-keyword">if</span>(add_separator) {
   9.231 -<a class="c2html-lineno" name="l179" href="#l179">179 </a>        url = sstrcat(<span class="c2html-macroconst">3</span>, base, sstr(<span class="c2html-string">"/"</span>), path);
   9.232 -<a class="c2html-lineno" name="l180" href="#l180">180 </a>    } <span class="c2html-keyword">else</span> {
   9.233 -<a class="c2html-lineno" name="l181" href="#l181">181 </a>        url = sstrcat(<span class="c2html-macroconst">2</span>, base, path);
   9.234 -<a class="c2html-lineno" name="l182" href="#l182">182 </a>    }
   9.235 -<a class="c2html-lineno" name="l183" href="#l183">183 </a>    
   9.236 -<a class="c2html-lineno" name="l184" href="#l184">184 </a>    <span class="c2html-keyword">return</span> url.ptr;
   9.237 -<a class="c2html-lineno" name="l185" href="#l185">185 </a>}
   9.238 -<a class="c2html-lineno" name="l186" href="#l186">186 </a>
   9.239 -<a class="c2html-lineno" name="l187" href="#l187">187 </a><span class="c2html-keyword">void</span> util_set_url(DavSession *sn, <span class="c2html-keyword">char</span> *href) {
   9.240 -<a class="c2html-lineno" name="l188" href="#l188">188 </a>    <span class="c2html-type">sstr_t</span> base = sstr(sn-&gt;base_url);
   9.241 -<a class="c2html-lineno" name="l189" href="#l189">189 </a>    <span class="c2html-type">sstr_t</span> href_str = sstr(href);
   9.242 -<a class="c2html-lineno" name="l190" href="#l190">190 </a>    
   9.243 -<a class="c2html-lineno" name="l191" href="#l191">191 </a>    <span class="c2html-keyword">char</span> *base_path = util_url_path(sn-&gt;base_url);
   9.244 -<a class="c2html-lineno" name="l192" href="#l192">192 </a>    base.length -= strlen(base_path);
   9.245 -<a class="c2html-lineno" name="l193" href="#l193">193 </a>    
   9.246 -<a class="c2html-lineno" name="l194" href="#l194">194 </a>    <span class="c2html-type">sstr_t</span> url = sstrcat(<span class="c2html-macroconst">2</span>, base, href_str);
   9.247 -<a class="c2html-lineno" name="l195" href="#l195">195 </a>    
   9.248 -<a class="c2html-lineno" name="l196" href="#l196">196 </a>    curl_easy_setopt(sn-&gt;handle, <span class="c2html-macroconst">CURLOPT_URL</span>, url.ptr);
   9.249 -<a class="c2html-lineno" name="l197" href="#l197">197 </a>    free(url.ptr);
   9.250 -<a class="c2html-lineno" name="l198" href="#l198">198 </a>}
   9.251 -<a class="c2html-lineno" name="l199" href="#l199">199 </a>
   9.252 -<a class="c2html-lineno" name="l200" href="#l200">200 </a><span class="c2html-keyword">char</span>* util_path_to_url(DavSession *sn, <span class="c2html-keyword">char</span> *path) {
   9.253 -<a class="c2html-lineno" name="l201" href="#l201">201 </a>    <span class="c2html-keyword">char</span> *space = malloc(<span class="c2html-macroconst">256</span>);
   9.254 -<a class="c2html-lineno" name="l202" href="#l202">202 </a>    UcxBuffer *url = ucx_buffer_new(space, <span class="c2html-macroconst">256</span>, <span class="c2html-macroconst">UCX_BUFFER_AUTOEXTEND</span>);
   9.255 -<a class="c2html-lineno" name="l203" href="#l203">203 </a>    
   9.256 -<a class="c2html-lineno" name="l204" href="#l204">204 </a>    <span class="c2html-comment">// add base url</span>
   9.257 -<a class="c2html-lineno" name="l205" href="#l205">205 </a>    ucx_buffer_write(sn-&gt;base_url, <span class="c2html-macroconst">1</span>, strlen(sn-&gt;base_url), url);
   9.258 -<a class="c2html-lineno" name="l206" href="#l206">206 </a>    <span class="c2html-comment">// remove trailing slash</span>
   9.259 -<a class="c2html-lineno" name="l207" href="#l207">207 </a>    ucx_buffer_seek(url, -<span class="c2html-macroconst">1</span>, <span class="c2html-macroconst">SEEK_CUR</span>);
   9.260 -<a class="c2html-lineno" name="l208" href="#l208">208 </a>    
   9.261 -<a class="c2html-lineno" name="l209" href="#l209">209 </a>    <span class="c2html-type">sstr_t</span> p = sstr(path);
   9.262 -<a class="c2html-lineno" name="l210" href="#l210">210 </a>    <span class="c2html-type">ssize_t</span> ntk = <span class="c2html-macroconst">0</span>;
   9.263 -<a class="c2html-lineno" name="l211" href="#l211">211 </a>    <span class="c2html-type">sstr_t</span> *tks = sstrsplit(p, <span class="c2html-macroconst">S</span>(<span class="c2html-string">"/"</span>), &ntk);
   9.264 -<a class="c2html-lineno" name="l212" href="#l212">212 </a>    
   9.265 -<a class="c2html-lineno" name="l213" href="#l213">213 </a>    <span class="c2html-keyword">for</span>(<span class="c2html-keyword">int</span> i=<span class="c2html-macroconst">0</span>;i&lt;ntk;i++) {
   9.266 -<a class="c2html-lineno" name="l214" href="#l214">214 </a>        <span class="c2html-type">sstr_t</span> node = tks[i];
   9.267 -<a class="c2html-lineno" name="l215" href="#l215">215 </a>        <span class="c2html-keyword">if</span>(node.length &gt; <span class="c2html-macroconst">0</span>) {
   9.268 -<a class="c2html-lineno" name="l216" href="#l216">216 </a>            <span class="c2html-keyword">char</span> *esc = curl_easy_escape(sn-&gt;handle, node.ptr, node.length);
   9.269 -<a class="c2html-lineno" name="l217" href="#l217">217 </a>            ucx_buffer_putc(url, <span class="c2html-string">'/'</span>);
   9.270 -<a class="c2html-lineno" name="l218" href="#l218">218 </a>            ucx_buffer_write(esc, <span class="c2html-macroconst">1</span>, strlen(esc), url);
   9.271 -<a class="c2html-lineno" name="l219" href="#l219">219 </a>            curl_free(esc);
   9.272 -<a class="c2html-lineno" name="l220" href="#l220">220 </a>        }
   9.273 -<a class="c2html-lineno" name="l221" href="#l221">221 </a>        free(node.ptr);
   9.274 -<a class="c2html-lineno" name="l222" href="#l222">222 </a>    }
   9.275 -<a class="c2html-lineno" name="l223" href="#l223">223 </a>    free(tks);
   9.276 -<a class="c2html-lineno" name="l224" href="#l224">224 </a>    <span class="c2html-keyword">if</span>(path[p.length-<span class="c2html-macroconst">1</span>] == <span class="c2html-string">'/'</span>) {
   9.277 -<a class="c2html-lineno" name="l225" href="#l225">225 </a>        ucx_buffer_putc(url, <span class="c2html-string">'/'</span>);
   9.278 -<a class="c2html-lineno" name="l226" href="#l226">226 </a>    }
   9.279 -<a class="c2html-lineno" name="l227" href="#l227">227 </a>    ucx_buffer_putc(url, <span class="c2html-macroconst">0</span>);
   9.280 -<a class="c2html-lineno" name="l228" href="#l228">228 </a>    
   9.281 -<a class="c2html-lineno" name="l229" href="#l229">229 </a>    space = url-&gt;space;
   9.282 -<a class="c2html-lineno" name="l230" href="#l230">230 </a>    ucx_buffer_free(url);
   9.283 -<a class="c2html-lineno" name="l231" href="#l231">231 </a>    
   9.284 -<a class="c2html-lineno" name="l232" href="#l232">232 </a>    <span class="c2html-keyword">return</span> space;
   9.285 -<a class="c2html-lineno" name="l233" href="#l233">233 </a>}
   9.286 -<a class="c2html-lineno" name="l234" href="#l234">234 </a>
   9.287 -<a class="c2html-lineno" name="l235" href="#l235">235 </a><span class="c2html-keyword">char</span>* util_parent_path(<span class="c2html-keyword">char</span> *path) {
   9.288 -<a class="c2html-lineno" name="l236" href="#l236">236 </a>    <span class="c2html-keyword">char</span> *name = util_resource_name(path);
   9.289 -<a class="c2html-lineno" name="l237" href="#l237">237 </a>    <span class="c2html-type">size_t</span> namelen = strlen(name);
   9.290 -<a class="c2html-lineno" name="l238" href="#l238">238 </a>    <span class="c2html-type">size_t</span> pathlen = strlen(path);
   9.291 -<a class="c2html-lineno" name="l239" href="#l239">239 </a>    <span class="c2html-type">size_t</span> parentlen = pathlen - namelen;
   9.292 -<a class="c2html-lineno" name="l240" href="#l240">240 </a>    <span class="c2html-keyword">char</span> *parent = malloc(parentlen + <span class="c2html-macroconst">1</span>);
   9.293 -<a class="c2html-lineno" name="l241" href="#l241">241 </a>    memcpy(parent, path, parentlen);
   9.294 -<a class="c2html-lineno" name="l242" href="#l242">242 </a>    parent[parentlen] = <span class="c2html-string">'\0'</span>;
   9.295 -<a class="c2html-lineno" name="l243" href="#l243">243 </a>    <span class="c2html-keyword">return</span> parent;
   9.296 -<a class="c2html-lineno" name="l244" href="#l244">244 </a>}
   9.297 -<a class="c2html-lineno" name="l245" href="#l245">245 </a>
   9.298 -<a class="c2html-lineno" name="l246" href="#l246">246 </a>
   9.299 -<a class="c2html-lineno" name="l247" href="#l247">247 </a><span class="c2html-keyword">char</span>* util_xml_get_text(xmlNode *elm) {
   9.300 -<a class="c2html-lineno" name="l248" href="#l248">248 </a>    xmlNode *node = elm-&gt;children;
   9.301 -<a class="c2html-lineno" name="l249" href="#l249">249 </a>    <span class="c2html-keyword">while</span>(node) {
   9.302 -<a class="c2html-lineno" name="l250" href="#l250">250 </a>        <span class="c2html-keyword">if</span>(node-&gt;type == <span class="c2html-macroconst">XML_TEXT_NODE</span>) {
   9.303 -<a class="c2html-lineno" name="l251" href="#l251">251 </a>            <span class="c2html-keyword">return</span> (<span class="c2html-keyword">char</span>*)node-&gt;content;
   9.304 -<a class="c2html-lineno" name="l252" href="#l252">252 </a>        }
   9.305 -<a class="c2html-lineno" name="l253" href="#l253">253 </a>        node = node-&gt;next;
   9.306 -<a class="c2html-lineno" name="l254" href="#l254">254 </a>    }
   9.307 -<a class="c2html-lineno" name="l255" href="#l255">255 </a>    <span class="c2html-keyword">return</span> <span class="c2html-macroconst">NULL</span>;
   9.308 -<a class="c2html-lineno" name="l256" href="#l256">256 </a>}
   9.309 -<a class="c2html-lineno" name="l257" href="#l257">257 </a>
   9.310 -<a class="c2html-lineno" name="l258" href="#l258">258 </a>
   9.311 -<a class="c2html-lineno" name="l259" href="#l259">259 </a><span class="c2html-keyword">char</span>* util_base64decode(<span class="c2html-keyword">char</span> *in) {
   9.312 -<a class="c2html-lineno" name="l260" href="#l260">260 </a>    <span class="c2html-keyword">int</span> len = <span class="c2html-macroconst">0</span>;
   9.313 -<a class="c2html-lineno" name="l261" href="#l261">261 </a>    <span class="c2html-keyword">return</span> util_base64decode_len(in, &len);
   9.314 -<a class="c2html-lineno" name="l262" href="#l262">262 </a>}
   9.315 -<a class="c2html-lineno" name="l263" href="#l263">263 </a>
   9.316 -<a class="c2html-lineno" name="l264" href="#l264">264 </a><span class="c2html-keyword">char</span>* util_base64decode_len(<span class="c2html-keyword">char</span>* in, <span class="c2html-keyword">int</span> *outlen) {
   9.317 -<a class="c2html-lineno" name="l265" href="#l265">265 </a>    <span class="c2html-type">size_t</span> len = strlen(in);
   9.318 -<a class="c2html-lineno" name="l266" href="#l266">266 </a>    <span class="c2html-keyword">char</span> *out = calloc(<span class="c2html-macroconst">1</span>, len);
   9.319 -<a class="c2html-lineno" name="l267" href="#l267">267 </a>    
   9.320 -<a class="c2html-lineno" name="l268" href="#l268">268 </a>    <span class="c2html-macroconst">BIO</span>* b = BIO_new_mem_buf(in, len);
   9.321 -<a class="c2html-lineno" name="l269" href="#l269">269 </a>    <span class="c2html-macroconst">BIO</span> *d = BIO_new(BIO_f_base64());
   9.322 -<a class="c2html-lineno" name="l270" href="#l270">270 </a>    BIO_set_flags(d, <span class="c2html-macroconst">BIO_FLAGS_BASE64_NO_NL</span>);
   9.323 -<a class="c2html-lineno" name="l271" href="#l271">271 </a>    b = BIO_push(d, b);
   9.324 -<a class="c2html-lineno" name="l272" href="#l272">272 </a>
   9.325 -<a class="c2html-lineno" name="l273" href="#l273">273 </a>    *outlen = BIO_read(b, out, len);
   9.326 -<a class="c2html-lineno" name="l274" href="#l274">274 </a>    BIO_free_all(b);
   9.327 -<a class="c2html-lineno" name="l275" href="#l275">275 </a>    
   9.328 -<a class="c2html-lineno" name="l276" href="#l276">276 </a>    <span class="c2html-keyword">return</span> out;
   9.329 -<a class="c2html-lineno" name="l277" href="#l277">277 </a>}
   9.330 -<a class="c2html-lineno" name="l278" href="#l278">278 </a>
   9.331 -<a class="c2html-lineno" name="l279" href="#l279">279 </a><span class="c2html-keyword">char</span>* util_base64encode(<span class="c2html-keyword">char</span> *in, <span class="c2html-type">size_t</span> len) { 
   9.332 -<a class="c2html-lineno" name="l280" href="#l280">280 </a>    <span class="c2html-macroconst">BIO</span> *b;
   9.333 -<a class="c2html-lineno" name="l281" href="#l281">281 </a>    <span class="c2html-macroconst">BIO</span> *e;
   9.334 -<a class="c2html-lineno" name="l282" href="#l282">282 </a>    <span class="c2html-macroconst">BUF_MEM</span> *mem;
   9.335 -<a class="c2html-lineno" name="l283" href="#l283">283 </a>
   9.336 -<a class="c2html-lineno" name="l284" href="#l284">284 </a>    e = BIO_new(BIO_f_base64());
   9.337 -<a class="c2html-lineno" name="l285" href="#l285">285 </a>    b = BIO_new(BIO_s_mem());
   9.338 -<a class="c2html-lineno" name="l286" href="#l286">286 </a>    
   9.339 -<a class="c2html-lineno" name="l287" href="#l287">287 </a>    e = BIO_push(e, b);
   9.340 -<a class="c2html-lineno" name="l288" href="#l288">288 </a>    BIO_write(e, in, len);
   9.341 -<a class="c2html-lineno" name="l289" href="#l289">289 </a>    BIO_flush(e);
   9.342 -<a class="c2html-lineno" name="l290" href="#l290">290 </a>    
   9.343 -<a class="c2html-lineno" name="l291" href="#l291">291 </a>    BIO_get_mem_ptr(e, &mem);
   9.344 -<a class="c2html-lineno" name="l292" href="#l292">292 </a>    <span class="c2html-keyword">char</span> *out = malloc(mem-&gt;length);
   9.345 -<a class="c2html-lineno" name="l293" href="#l293">293 </a>    memcpy(out, mem-&gt;data, mem-&gt;length -<span class="c2html-macroconst">1</span>);
   9.346 -<a class="c2html-lineno" name="l294" href="#l294">294 </a>    out[mem-&gt;length - <span class="c2html-macroconst">1</span>] = <span class="c2html-string">'\0'</span>;
   9.347 -<a class="c2html-lineno" name="l295" href="#l295">295 </a>
   9.348 -<a class="c2html-lineno" name="l296" href="#l296">296 </a>    BIO_free_all(e);
   9.349 -<a class="c2html-lineno" name="l297" href="#l297">297 </a>
   9.350 -<a class="c2html-lineno" name="l298" href="#l298">298 </a>    <span class="c2html-keyword">return</span> out;
   9.351 -<a class="c2html-lineno" name="l299" href="#l299">299 </a>}
   9.352 -<a class="c2html-lineno" name="l300" href="#l300">300 </a>
   9.353 -<a class="c2html-lineno" name="l301" href="#l301">301 </a><span class="c2html-keyword">char</span>* util_encrypt_str(DavSession *sn, <span class="c2html-keyword">char</span> *str, <span class="c2html-keyword">char</span> *key) {
   9.354 -<a class="c2html-lineno" name="l302" href="#l302">302 </a>    DavKey *k = dav_context_get_key(sn-&gt;context, key);
   9.355 -<a class="c2html-lineno" name="l303" href="#l303">303 </a>    <span class="c2html-keyword">if</span>(!k) {
   9.356 -<a class="c2html-lineno" name="l304" href="#l304">304 </a>        <span class="c2html-comment">// TODO: session error</span>
   9.357 -<a class="c2html-lineno" name="l305" href="#l305">305 </a>        <span class="c2html-keyword">return</span> <span class="c2html-macroconst">NULL</span>;
   9.358 -<a class="c2html-lineno" name="l306" href="#l306">306 </a>    }
   9.359 -<a class="c2html-lineno" name="l307" href="#l307">307 </a>    
   9.360 -<a class="c2html-lineno" name="l308" href="#l308">308 </a>    <span class="c2html-keyword">char</span> *enc_str = aes_encrypt(str, k);
   9.361 -<a class="c2html-lineno" name="l309" href="#l309">309 </a>    <span class="c2html-keyword">char</span> *ret_str = dav_session_strdup(sn, enc_str);
   9.362 -<a class="c2html-lineno" name="l310" href="#l310">310 </a>    free(enc_str);
   9.363 -<a class="c2html-lineno" name="l311" href="#l311">311 </a>    <span class="c2html-keyword">return</span> ret_str;
   9.364 -<a class="c2html-lineno" name="l312" href="#l312">312 </a>}
   9.365 -<a class="c2html-lineno" name="l313" href="#l313">313 </a>
   9.366 -<a class="c2html-lineno" name="l314" href="#l314">314 </a><span class="c2html-comment">/* commented out for testing reasons */</span>
   9.367 -<a class="c2html-lineno" name="l315" href="#l315">315 </a><span class="c2html-comment">/*</span>
   9.368 -<a class="c2html-lineno" name="l316" href="#l316">316 </a><span class="c2html-comment">char* util_decrypt_str(DavSession *sn, char *str, char *key) {</span>
   9.369 -<a class="c2html-lineno" name="l317" href="#l317">317 </a><span class="c2html-comment">    DavKey *k = dav_context_get_key(sn-&gt;context, key);</span>
   9.370 -<a class="c2html-lineno" name="l318" href="#l318">318 </a><span class="c2html-comment">    if(!k) {</span>
   9.371 -<a class="c2html-lineno" name="l319" href="#l319">319 </a><span class="c2html-comment">        // TODO: session error</span>
   9.372 -<a class="c2html-lineno" name="l320" href="#l320">320 </a><span class="c2html-comment">        return NULL;</span>
   9.373 -<a class="c2html-lineno" name="l321" href="#l321">321 </a><span class="c2html-comment">    }</span>
   9.374 -<a class="c2html-lineno" name="l322" href="#l322">322 </a><span class="c2html-comment">    </span>
   9.375 -<a class="c2html-lineno" name="l323" href="#l323">323 </a><span class="c2html-comment">    char *dec_str = aes_decrypt(str, k);</span>
   9.376 -<a class="c2html-lineno" name="l324" href="#l324">324 </a><span class="c2html-comment">    char *ret_str = dav_session_strdup(sn, dec_str);</span>
   9.377 -<a class="c2html-lineno" name="l325" href="#l325">325 </a><span class="c2html-comment">    free(dec_str);</span>
   9.378 -<a class="c2html-lineno" name="l326" href="#l326">326 </a><span class="c2html-comment">    return ret_str;</span>
   9.379 -<a class="c2html-lineno" name="l327" href="#l327">327 </a><span class="c2html-comment">}</span>
   9.380 -<a class="c2html-lineno" name="l328" href="#l328">328 </a><span class="c2html-comment">*/</span>
   9.381 -<a class="c2html-lineno" name="l329" href="#l329">329 </a><span class="c2html-keyword">char</span>* util_random_str() {
   9.382 -<a class="c2html-lineno" name="l330" href="#l330">330 </a>    <span class="c2html-keyword">unsigned</span> <span class="c2html-keyword">char</span> *str = malloc(<span class="c2html-macroconst">25</span>);
   9.383 -<a class="c2html-lineno" name="l331" href="#l331">331 </a>    str[<span class="c2html-macroconst">24</span>] = <span class="c2html-string">'\0'</span>;
   9.384 -<a class="c2html-lineno" name="l332" href="#l332">332 </a>    
   9.385 -<a class="c2html-lineno" name="l333" href="#l333">333 </a>    <span class="c2html-type">sstr_t</span> t = <span class="c2html-macroconst">S</span>(
   9.386 -<a class="c2html-lineno" name="l334" href="#l334">334 </a>            <span class="c2html-string">"01234567890"</span>
   9.387 -<a class="c2html-lineno" name="l335" href="#l335">335 </a>            <span class="c2html-string">"abcdefghijklmnopqrstuvwxyz"</span>
   9.388 -<a class="c2html-lineno" name="l336" href="#l336">336 </a>            <span class="c2html-string">"ABCDEFGHIJKLMNOPQRSTUVWXYZ"</span>);
   9.389 -<a class="c2html-lineno" name="l337" href="#l337">337 </a>    <span class="c2html-keyword">const</span> <span class="c2html-keyword">unsigned</span> <span class="c2html-keyword">char</span> *table = (<span class="c2html-keyword">const</span> <span class="c2html-keyword">unsigned</span> <span class="c2html-keyword">char</span>*)t.ptr;
   9.390 -<a class="c2html-lineno" name="l338" href="#l338">338 </a>    
   9.391 -<a class="c2html-lineno" name="l339" href="#l339">339 </a>    RAND_pseudo_bytes(str, <span class="c2html-macroconst">24</span>);
   9.392 -<a class="c2html-lineno" name="l340" href="#l340">340 </a>    <span class="c2html-keyword">for</span>(<span class="c2html-keyword">int</span> i=<span class="c2html-macroconst">0</span>;i&lt;<span class="c2html-macroconst">24</span>;i++) {
   9.393 -<a class="c2html-lineno" name="l341" href="#l341">341 </a>        <span class="c2html-keyword">int</span> c = str[i] % t.length;
   9.394 -<a class="c2html-lineno" name="l342" href="#l342">342 </a>        str[i] = table[c];
   9.395 -<a class="c2html-lineno" name="l343" href="#l343">343 </a>    }
   9.396 -<a class="c2html-lineno" name="l344" href="#l344">344 </a>    
   9.397 -<a class="c2html-lineno" name="l345" href="#l345">345 </a>    <span class="c2html-keyword">return</span> (<span class="c2html-keyword">char</span>*)str;
   9.398 -<a class="c2html-lineno" name="l346" href="#l346">346 </a>}
   9.399 -<a class="c2html-lineno" name="l347" href="#l347">347 </a>
   9.400 -<a class="c2html-lineno" name="l348" href="#l348">348 </a><span class="c2html-comment">/*</span>
   9.401 -<a class="c2html-lineno" name="l349" href="#l349">349 </a><span class="c2html-comment"> * gets a substring from 0 to the appearance of the token</span>
   9.402 -<a class="c2html-lineno" name="l350" href="#l350">350 </a><span class="c2html-comment"> * tokens are separated by space</span>
   9.403 -<a class="c2html-lineno" name="l351" href="#l351">351 </a><span class="c2html-comment"> * sets sub to the substring and returns the remaining string</span>
   9.404 -<a class="c2html-lineno" name="l352" href="#l352">352 </a><span class="c2html-comment"> */</span>
   9.405 -<a class="c2html-lineno" name="l353" href="#l353">353 </a><span class="c2html-type">sstr_t</span> util_getsubstr_until_token(<span class="c2html-type">sstr_t</span> str, <span class="c2html-type">sstr_t</span> token, <span class="c2html-type">sstr_t</span> *sub) {  
   9.406 -<a class="c2html-lineno" name="l354" href="#l354">354 </a>    <span class="c2html-keyword">int</span> i;
   9.407 -<a class="c2html-lineno" name="l355" href="#l355">355 </a>    <span class="c2html-keyword">int</span> token_start = -<span class="c2html-macroconst">1</span>;
   9.408 -<a class="c2html-lineno" name="l356" href="#l356">356 </a>    <span class="c2html-keyword">int</span> token_end = -<span class="c2html-macroconst">1</span>;
   9.409 -<a class="c2html-lineno" name="l357" href="#l357">357 </a>    <span class="c2html-keyword">for</span>(i=<span class="c2html-macroconst">0</span>;i&lt;=str.length;i++) {
   9.410 -<a class="c2html-lineno" name="l358" href="#l358">358 </a>        <span class="c2html-keyword">int</span> c;
   9.411 -<a class="c2html-lineno" name="l359" href="#l359">359 </a>        <span class="c2html-keyword">if</span>(i == str.length) {
   9.412 -<a class="c2html-lineno" name="l360" href="#l360">360 </a>            c = <span class="c2html-string">' '</span>;
   9.413 -<a class="c2html-lineno" name="l361" href="#l361">361 </a>        } <span class="c2html-keyword">else</span> {
   9.414 -<a class="c2html-lineno" name="l362" href="#l362">362 </a>            c = str.ptr[i];
   9.415 -<a class="c2html-lineno" name="l363" href="#l363">363 </a>        }
   9.416 -<a class="c2html-lineno" name="l364" href="#l364">364 </a>        <span class="c2html-keyword">if</span>(c &lt; <span class="c2html-macroconst">33</span>) {
   9.417 -<a class="c2html-lineno" name="l365" href="#l365">365 </a>            <span class="c2html-keyword">if</span>(token_start != -<span class="c2html-macroconst">1</span>) {
   9.418 -<a class="c2html-lineno" name="l366" href="#l366">366 </a>                token_end = i;
   9.419 -<a class="c2html-lineno" name="l367" href="#l367">367 </a>                <span class="c2html-type">size_t</span> len = token_end - token_start;
   9.420 -<a class="c2html-lineno" name="l368" href="#l368">368 </a>                <span class="c2html-type">sstr_t</span> tk = sstrsubsl(str, token_start, len);
   9.421 -<a class="c2html-lineno" name="l369" href="#l369">369 </a>                <span class="c2html-comment">//printf("token: {%.*s}\n", token.length, token.ptr);</span>
   9.422 -<a class="c2html-lineno" name="l370" href="#l370">370 </a>                <span class="c2html-keyword">if</span>(!sstrcmp(tk, token)) {
   9.423 -<a class="c2html-lineno" name="l371" href="#l371">371 </a>                    *sub = sstrtrim(sstrsubsl(str, <span class="c2html-macroconst">0</span>, token_start));
   9.424 -<a class="c2html-lineno" name="l372" href="#l372">372 </a>                    <span class="c2html-keyword">break</span>;
   9.425 -<a class="c2html-lineno" name="l373" href="#l373">373 </a>                }
   9.426 -<a class="c2html-lineno" name="l374" href="#l374">374 </a>                token_start = -<span class="c2html-macroconst">1</span>;
   9.427 -<a class="c2html-lineno" name="l375" href="#l375">375 </a>                token_end = -<span class="c2html-macroconst">1</span>;
   9.428 -<a class="c2html-lineno" name="l376" href="#l376">376 </a>            }
   9.429 -<a class="c2html-lineno" name="l377" href="#l377">377 </a>        } <span class="c2html-keyword">else</span> {
   9.430 -<a class="c2html-lineno" name="l378" href="#l378">378 </a>            <span class="c2html-keyword">if</span>(token_start == -<span class="c2html-macroconst">1</span>) {
   9.431 -<a class="c2html-lineno" name="l379" href="#l379">379 </a>                token_start = i;
   9.432 -<a class="c2html-lineno" name="l380" href="#l380">380 </a>            }
   9.433 -<a class="c2html-lineno" name="l381" href="#l381">381 </a>        }
   9.434 -<a class="c2html-lineno" name="l382" href="#l382">382 </a>    }
   9.435 -<a class="c2html-lineno" name="l383" href="#l383">383 </a>    
   9.436 -<a class="c2html-lineno" name="l384" href="#l384">384 </a>    <span class="c2html-keyword">if</span>(i &lt; str.length) {
   9.437 -<a class="c2html-lineno" name="l385" href="#l385">385 </a>        <span class="c2html-keyword">return</span> sstrtrim(sstrsubs(str, i));
   9.438 -<a class="c2html-lineno" name="l386" href="#l386">386 </a>    } <span class="c2html-keyword">else</span> {
   9.439 -<a class="c2html-lineno" name="l387" href="#l387">387 </a>        str.ptr = <span class="c2html-macroconst">NULL</span>;
   9.440 -<a class="c2html-lineno" name="l388" href="#l388">388 </a>        str.length = <span class="c2html-macroconst">0</span>;
   9.441 -<a class="c2html-lineno" name="l389" href="#l389">389 </a>        <span class="c2html-keyword">return</span> str;
   9.442 -<a class="c2html-lineno" name="l390" href="#l390">390 </a>    }
   9.443 -<a class="c2html-lineno" name="l391" href="#l391">391 </a>}
   9.444 -</pre>
   9.445 -  </body>
   9.446 -</html>
   9.447 -
    10.1 --- a/test/golden-master/empty.html	Mon Oct 03 12:56:28 2022 +0200
    10.2 +++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
    10.3 @@ -1,53 +0,0 @@
    10.4 -<!DOCTYPE html>
    10.5 -<html>
    10.6 -  <head>
    10.7 -    <title>c2html</title>
    10.8 -    <style type="text/css">
    10.9 -      a.c2html-lineno {
   10.10 -        /* as long as user-select isn't widely spread, we throw the bomb */
   10.11 -        -webkit-user-select: none;
   10.12 -        -moz-user-select: none;
   10.13 -        -ms-user-select: none;
   10.14 -        user-select: none;
   10.15 -        display: inline-block;
   10.16 -        font-style: italic;
   10.17 -        text-decoration: none;
   10.18 -        color: grey;
   10.19 -      }
   10.20 -      span.c2html-keyword {
   10.21 -        color: blue;
   10.22 -      }
   10.23 -      span.c2html-macroconst {
   10.24 -        color: cornflowerblue;
   10.25 -      }
   10.26 -      span.c2html-type {
   10.27 -        color: cornflowerblue;
   10.28 -      }
   10.29 -      span.c2html-directive {
   10.30 -        color: green;
   10.31 -      }
   10.32 -      span.c2html-string {
   10.33 -        color: darkorange;
   10.34 -      }
   10.35 -      span.c2html-comment {
   10.36 -        color: grey;
   10.37 -      }
   10.38 -      span.c2html-stdinclude {
   10.39 -        color: darkorange;
   10.40 -      }
   10.41 -      span.c2html-userinclude {
   10.42 -        color: darkorange;
   10.43 -      }
   10.44 -      a.c2html-userinclude {
   10.45 -        color: darkorange;
   10.46 -        text-decoration: underline;
   10.47 -      }
   10.48 -    </style>
   10.49 -  </head>
   10.50 -  <body>
   10.51 -
   10.52 -<pre>
   10.53 -<a class="c2html-lineno" name="l1" href="#l1">1 </a></pre>
   10.54 -  </body>
   10.55 -</html>
   10.56 -
    11.1 --- a/test/golden-master/javatest.html	Mon Oct 03 12:56:28 2022 +0200
    11.2 +++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
    11.3 @@ -1,221 +0,0 @@
    11.4 -<!DOCTYPE html>
    11.5 -<html>
    11.6 -  <head>
    11.7 -    <title>c2html</title>
    11.8 -    <style type="text/css">
    11.9 -      a.c2html-lineno {
   11.10 -        /* as long as user-select isn't widely spread, we throw the bomb */
   11.11 -        -webkit-user-select: none;
   11.12 -        -moz-user-select: none;
   11.13 -        -ms-user-select: none;
   11.14 -        user-select: none;
   11.15 -        display: inline-block;
   11.16 -        font-style: italic;
   11.17 -        text-decoration: none;
   11.18 -        color: grey;
   11.19 -      }
   11.20 -      span.c2html-keyword {
   11.21 -        color: blue;
   11.22 -      }
   11.23 -      span.c2html-macroconst {
   11.24 -        color: cornflowerblue;
   11.25 -      }
   11.26 -      span.c2html-type {
   11.27 -        color: teal;
   11.28 -      }
   11.29 -      span.c2html-directive {
   11.30 -        color: silver;
   11.31 -      }
   11.32 -      span.c2html-string {
   11.33 -        color: darkorange;
   11.34 -      }
   11.35 -      span.c2html-comment {
   11.36 -        color: grey;
   11.37 -      }
   11.38 -      span.c2html-stdinclude, span.c2html-userinclude, a.c2html-userinclude {
   11.39 -      }
   11.40 -    </style>
   11.41 -  </head>
   11.42 -  <body>
   11.43 -
   11.44 -<pre>
   11.45 -<a class="c2html-lineno" name="l1" href="#l1">  1 </a><span class="c2html-comment">/*</span>
   11.46 -<a class="c2html-lineno" name="l2" href="#l2">  2 </a><span class="c2html-comment"> * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.</span>
   11.47 -<a class="c2html-lineno" name="l3" href="#l3">  3 </a><span class="c2html-comment"> *</span>
   11.48 -<a class="c2html-lineno" name="l4" href="#l4">  4 </a><span class="c2html-comment"> * Copyright 2014 Mike Becker. All rights reserved.</span>
   11.49 -<a class="c2html-lineno" name="l5" href="#l5">  5 </a><span class="c2html-comment"> *</span>
   11.50 -<a class="c2html-lineno" name="l6" href="#l6">  6 </a><span class="c2html-comment"> * Redistribution and use in source and binary forms, with or without</span>
   11.51 -<a class="c2html-lineno" name="l7" href="#l7">  7 </a><span class="c2html-comment"> * modification, are permitted provided that the following conditions are met:</span>
   11.52 -<a class="c2html-lineno" name="l8" href="#l8">  8 </a><span class="c2html-comment"> *</span>
   11.53 -<a class="c2html-lineno" name="l9" href="#l9">  9 </a><span class="c2html-comment"> *   1. Redistributions of source code must retain the above copyright</span>
   11.54 -<a class="c2html-lineno" name="l10" href="#l10"> 10 </a><span class="c2html-comment"> *      notice, this list of conditions and the following disclaimer.</span>
   11.55 -<a class="c2html-lineno" name="l11" href="#l11"> 11 </a><span class="c2html-comment"> *</span>
   11.56 -<a class="c2html-lineno" name="l12" href="#l12"> 12 </a><span class="c2html-comment"> *   2. Redistributions in binary form must reproduce the above copyright</span>
   11.57 -<a class="c2html-lineno" name="l13" href="#l13"> 13 </a><span class="c2html-comment"> *      notice, this list of conditions and the following disclaimer in the</span>
   11.58 -<a class="c2html-lineno" name="l14" href="#l14"> 14 </a><span class="c2html-comment"> *      documentation and/or other materials provided with the distribution.</span>
   11.59 -<a class="c2html-lineno" name="l15" href="#l15"> 15 </a><span class="c2html-comment"> *</span>
   11.60 -<a class="c2html-lineno" name="l16" href="#l16"> 16 </a><span class="c2html-comment"> * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"</span>
   11.61 -<a class="c2html-lineno" name="l17" href="#l17"> 17 </a><span class="c2html-comment"> * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE</span>
   11.62 -<a class="c2html-lineno" name="l18" href="#l18"> 18 </a><span class="c2html-comment"> * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE</span>
   11.63 -<a class="c2html-lineno" name="l19" href="#l19"> 19 </a><span class="c2html-comment"> * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE</span>
   11.64 -<a class="c2html-lineno" name="l20" href="#l20"> 20 </a><span class="c2html-comment"> * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR</span>
   11.65 -<a class="c2html-lineno" name="l21" href="#l21"> 21 </a><span class="c2html-comment"> * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF</span>
   11.66 -<a class="c2html-lineno" name="l22" href="#l22"> 22 </a><span class="c2html-comment"> * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS</span>
   11.67 -<a class="c2html-lineno" name="l23" href="#l23"> 23 </a><span class="c2html-comment"> * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN</span>
   11.68 -<a class="c2html-lineno" name="l24" href="#l24"> 24 </a><span class="c2html-comment"> * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)</span>
   11.69 -<a class="c2html-lineno" name="l25" href="#l25"> 25 </a><span class="c2html-comment"> * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE</span>
   11.70 -<a class="c2html-lineno" name="l26" href="#l26"> 26 </a><span class="c2html-comment"> * POSSIBILITY OF SUCH DAMAGE.</span>
   11.71 -<a class="c2html-lineno" name="l27" href="#l27"> 27 </a><span class="c2html-comment"> *</span>
   11.72 -<a class="c2html-lineno" name="l28" href="#l28"> 28 </a><span class="c2html-comment"> */</span>
   11.73 -<a class="c2html-lineno" name="l29" href="#l29"> 29 </a>
   11.74 -<a class="c2html-lineno" name="l30" href="#l30"> 30 </a><span class="c2html-keyword">package</span> de.uapcore.sigred.doc.base;
   11.75 -<a class="c2html-lineno" name="l31" href="#l31"> 31 </a>
   11.76 -<a class="c2html-lineno" name="l32" href="#l32"> 32 </a><span class="c2html-keyword">import</span> de.uapcore.sigred.doc.<span class="c2html-type">Resources</span>;
   11.77 -<a class="c2html-lineno" name="l33" href="#l33"> 33 </a><span class="c2html-keyword">import</span> de.uapcore.sigrapi.impl.<span class="c2html-type">Digraph</span>;
   11.78 -<a class="c2html-lineno" name="l34" href="#l34"> 34 </a><span class="c2html-keyword">import</span> de.uapcore.sigrapi.impl.<span class="c2html-type">Graph</span>;
   11.79 -<a class="c2html-lineno" name="l35" href="#l35"> 35 </a><span class="c2html-keyword">import</span> de.uapcore.sigrapi.<span class="c2html-type">IGraph</span>;
   11.80 -<a class="c2html-lineno" name="l36" href="#l36"> 36 </a><span class="c2html-keyword">import</span> java.io.<span class="c2html-type">IOException</span>;
   11.81 -<a class="c2html-lineno" name="l37" href="#l37"> 37 </a><span class="c2html-keyword">import</span> java.io.<span class="c2html-type">InputStream</span>;
   11.82 -<a class="c2html-lineno" name="l38" href="#l38"> 38 </a><span class="c2html-keyword">import</span> java.io.<span class="c2html-type">OutputStream</span>;
   11.83 -<a class="c2html-lineno" name="l39" href="#l39"> 39 </a><span class="c2html-keyword">import</span> java.util.concurrent.atomic.<span class="c2html-type">AtomicBoolean</span>;
   11.84 -<a class="c2html-lineno" name="l40" href="#l40"> 40 </a><span class="c2html-keyword">import</span> java.util.concurrent.atomic.<span class="c2html-type">AtomicReference</span>;
   11.85 -<a class="c2html-lineno" name="l41" href="#l41"> 41 </a><span class="c2html-keyword">import</span> org.apache.xerces.impl.<span class="c2html-type">Constants</span>;
   11.86 -<a class="c2html-lineno" name="l42" href="#l42"> 42 </a><span class="c2html-keyword">import</span> org.dom4j.<span class="c2html-type">Document</span>;
   11.87 -<a class="c2html-lineno" name="l43" href="#l43"> 43 </a><span class="c2html-keyword">import</span> org.dom4j.<span class="c2html-type">DocumentException</span>;
   11.88 -<a class="c2html-lineno" name="l44" href="#l44"> 44 </a><span class="c2html-keyword">import</span> org.dom4j.<span class="c2html-type">DocumentHelper</span>;
   11.89 -<a class="c2html-lineno" name="l45" href="#l45"> 45 </a><span class="c2html-keyword">import</span> org.dom4j.<span class="c2html-type">Element</span>;
   11.90 -<a class="c2html-lineno" name="l46" href="#l46"> 46 </a><span class="c2html-keyword">import</span> org.dom4j.<span class="c2html-type">Namespace</span>;
   11.91 -<a class="c2html-lineno" name="l47" href="#l47"> 47 </a><span class="c2html-keyword">import</span> org.dom4j.<span class="c2html-type">QName</span>;
   11.92 -<a class="c2html-lineno" name="l48" href="#l48"> 48 </a><span class="c2html-keyword">import</span> org.dom4j.io.<span class="c2html-type">OutputFormat</span>;
   11.93 -<a class="c2html-lineno" name="l49" href="#l49"> 49 </a><span class="c2html-keyword">import</span> org.dom4j.io.<span class="c2html-type">SAXReader</span>;
   11.94 -<a class="c2html-lineno" name="l50" href="#l50"> 50 </a><span class="c2html-keyword">import</span> org.dom4j.io.<span class="c2html-type">XMLWriter</span>;
   11.95 -<a class="c2html-lineno" name="l51" href="#l51"> 51 </a><span class="c2html-keyword">import</span> org.xml.sax.<span class="c2html-type">ErrorHandler</span>;
   11.96 -<a class="c2html-lineno" name="l52" href="#l52"> 52 </a><span class="c2html-keyword">import</span> org.xml.sax.<span class="c2html-type">SAXException</span>;
   11.97 -<a class="c2html-lineno" name="l53" href="#l53"> 53 </a><span class="c2html-keyword">import</span> org.xml.sax.<span class="c2html-type">SAXParseException</span>;
   11.98 -<a class="c2html-lineno" name="l54" href="#l54"> 54 </a>
   11.99 -<a class="c2html-lineno" name="l55" href="#l55"> 55 </a><span class="c2html-keyword">public</span> <span class="c2html-keyword">abstract</span> <span class="c2html-keyword">class</span> <span class="c2html-type">AbstractGraphDocument</span>&lt;<span class="c2html-type">T</span> <span class="c2html-keyword">extends</span> <span class="c2html-type">IGraph</span>&gt;
  11.100 -<a class="c2html-lineno" name="l56" href="#l56"> 56 </a>        <span class="c2html-keyword">extends</span> <span class="c2html-type">FileBackedDocument</span> {
  11.101 -<a class="c2html-lineno" name="l57" href="#l57"> 57 </a>    
  11.102 -<a class="c2html-lineno" name="l58" href="#l58"> 58 </a>    <span class="c2html-keyword">protected</span> <span class="c2html-keyword">static</span> <span class="c2html-keyword">final</span> <span class="c2html-type">Namespace</span> <span class="c2html-type">NAMESPACE</span> = <span class="c2html-type">Namespace</span>.get(<span class="c2html-string">"sigred"</span>,
  11.103 -<a class="c2html-lineno" name="l59" href="#l59"> 59 </a>        <span class="c2html-string">"http://develop.uap-core.de/sigred/"</span>);
  11.104 -<a class="c2html-lineno" name="l60" href="#l60"> 60 </a>    
  11.105 -<a class="c2html-lineno" name="l61" href="#l61"> 61 </a>    <span class="c2html-keyword">private</span> <span class="c2html-keyword">static</span> <span class="c2html-keyword">final</span>
  11.106 -<a class="c2html-lineno" name="l62" href="#l62"> 62 </a>        <span class="c2html-type">QName</span> <span class="c2html-type">TAG_GRAPHDOC</span> = <span class="c2html-type">QName</span>.get(<span class="c2html-string">"graph-document"</span>, <span class="c2html-type">NAMESPACE</span>);
  11.107 -<a class="c2html-lineno" name="l63" href="#l63"> 63 </a>    <span class="c2html-keyword">private</span> <span class="c2html-keyword">static</span> <span class="c2html-keyword">final</span>
  11.108 -<a class="c2html-lineno" name="l64" href="#l64"> 64 </a>        <span class="c2html-type">QName</span> <span class="c2html-type">TAG_GRAPH</span> = <span class="c2html-type">QName</span>.get(<span class="c2html-string">"graph"</span>, <span class="c2html-type">NAMESPACE</span>);
  11.109 -<a class="c2html-lineno" name="l65" href="#l65"> 65 </a>    <span class="c2html-keyword">private</span> <span class="c2html-keyword">static</span> <span class="c2html-keyword">final</span>
  11.110 -<a class="c2html-lineno" name="l66" href="#l66"> 66 </a>        <span class="c2html-type">QName</span> <span class="c2html-type">TAG_DIGRAPH</span> = <span class="c2html-type">QName</span>.get(<span class="c2html-string">"digraph"</span>, <span class="c2html-type">NAMESPACE</span>);
  11.111 -<a class="c2html-lineno" name="l67" href="#l67"> 67 </a>    <span class="c2html-keyword">private</span> <span class="c2html-keyword">static</span> <span class="c2html-keyword">final</span>
  11.112 -<a class="c2html-lineno" name="l68" href="#l68"> 68 </a>        <span class="c2html-type">QName</span> <span class="c2html-type">TAG_METADATA</span> = <span class="c2html-type">QName</span>.get(<span class="c2html-string">"metadata"</span>, <span class="c2html-type">NAMESPACE</span>);
  11.113 -<a class="c2html-lineno" name="l69" href="#l69"> 69 </a>    
  11.114 -<a class="c2html-lineno" name="l70" href="#l70"> 70 </a>    <span class="c2html-keyword">protected</span> <span class="c2html-keyword">final</span> <span class="c2html-type">T</span> graph;
  11.115 -<a class="c2html-lineno" name="l71" href="#l71"> 71 </a>    
  11.116 -<a class="c2html-lineno" name="l72" href="#l72"> 72 </a>    <span class="c2html-keyword">private</span> <span class="c2html-keyword">final</span> <span class="c2html-type">GraphDocumentMetadata</span> metadata;
  11.117 -<a class="c2html-lineno" name="l73" href="#l73"> 73 </a>    
  11.118 -<a class="c2html-lineno" name="l74" href="#l74"> 74 </a>    <span class="c2html-keyword">public</span> <span class="c2html-type">AbstractGraphDocument</span>(<span class="c2html-type">Class</span>&lt;<span class="c2html-type">T</span>&gt; graphType) {
  11.119 -<a class="c2html-lineno" name="l75" href="#l75"> 75 </a>        <span class="c2html-type">T</span> g;
  11.120 -<a class="c2html-lineno" name="l76" href="#l76"> 76 </a>        <span class="c2html-keyword">try</span> {
  11.121 -<a class="c2html-lineno" name="l77" href="#l77"> 77 </a>            g = graphType.newInstance();
  11.122 -<a class="c2html-lineno" name="l78" href="#l78"> 78 </a>        } <span class="c2html-keyword">catch</span> (<span class="c2html-type">ReflectiveOperationException</span> e) {
  11.123 -<a class="c2html-lineno" name="l79" href="#l79"> 79 </a>            <span class="c2html-keyword">assert</span> false;
  11.124 -<a class="c2html-lineno" name="l80" href="#l80"> 80 </a>            g = null; <span class="c2html-comment">// for the compiler</span>
  11.125 -<a class="c2html-lineno" name="l81" href="#l81"> 81 </a>        }
  11.126 -<a class="c2html-lineno" name="l82" href="#l82"> 82 </a>        graph = g;
  11.127 -<a class="c2html-lineno" name="l83" href="#l83"> 83 </a>        metadata = <span class="c2html-keyword">new</span> <span class="c2html-type">GraphDocumentMetadata</span>();
  11.128 -<a class="c2html-lineno" name="l84" href="#l84"> 84 </a>    }
  11.129 -<a class="c2html-lineno" name="l85" href="#l85"> 85 </a>
  11.130 -<a class="c2html-lineno" name="l86" href="#l86"> 86 </a>    <span class="c2html-keyword">public</span> <span class="c2html-type">T</span> getGraph() {
  11.131 -<a class="c2html-lineno" name="l87" href="#l87"> 87 </a>        <span class="c2html-keyword">return</span> graph;
  11.132 -<a class="c2html-lineno" name="l88" href="#l88"> 88 </a>    }
  11.133 -<a class="c2html-lineno" name="l89" href="#l89"> 89 </a>    
  11.134 -<a class="c2html-lineno" name="l90" href="#l90"> 90 </a>    <span class="c2html-keyword">public</span> <span class="c2html-type">GraphDocumentMetadata</span> getMetadata() {
  11.135 -<a class="c2html-lineno" name="l91" href="#l91"> 91 </a>        <span class="c2html-keyword">return</span> metadata;
  11.136 -<a class="c2html-lineno" name="l92" href="#l92"> 92 </a>    }
  11.137 -<a class="c2html-lineno" name="l93" href="#l93"> 93 </a>
  11.138 -<a class="c2html-lineno" name="l94" href="#l94"> 94 </a>    <span class="c2html-keyword">protected</span> <span class="c2html-keyword">abstract</span> <span class="c2html-keyword">void</span> writeGraph(<span class="c2html-type">Element</span> rootNode) <span class="c2html-keyword">throws</span> <span class="c2html-type">IOException</span>;
  11.139 -<a class="c2html-lineno" name="l95" href="#l95"> 95 </a>    <span class="c2html-keyword">protected</span> <span class="c2html-keyword">abstract</span> <span class="c2html-keyword">void</span> readGraph(<span class="c2html-type">Element</span> rootNode) <span class="c2html-keyword">throws</span> <span class="c2html-type">IOException</span>;
  11.140 -<a class="c2html-lineno" name="l96" href="#l96"> 96 </a>
  11.141 -<a class="c2html-lineno" name="l97" href="#l97"> 97 </a>    <span class="c2html-directive">@Override</span>
  11.142 -<a class="c2html-lineno" name="l98" href="#l98"> 98 </a>    <span class="c2html-keyword">public</span> <span class="c2html-keyword">void</span> writeTo(<span class="c2html-type">OutputStream</span> out) <span class="c2html-keyword">throws</span> <span class="c2html-type">IOException</span> {
  11.143 -<a class="c2html-lineno" name="l99" href="#l99"> 99 </a>        <span class="c2html-type">Document</span> doc = <span class="c2html-type">DocumentHelper</span>.createDocument();
  11.144 -<a class="c2html-lineno" name="l100" href="#l100">100 </a>
  11.145 -<a class="c2html-lineno" name="l101" href="#l101">101 </a>        <span class="c2html-type">Element</span> rootNode = doc.addElement(<span class="c2html-type">TAG_GRAPHDOC</span>);
  11.146 -<a class="c2html-lineno" name="l102" href="#l102">102 </a>
  11.147 -<a class="c2html-lineno" name="l103" href="#l103">103 </a>        <span class="c2html-type">Element</span> metadataNode = rootNode.addElement(<span class="c2html-type">TAG_METADATA</span>);
  11.148 -<a class="c2html-lineno" name="l104" href="#l104">104 </a>
  11.149 -<a class="c2html-lineno" name="l105" href="#l105">105 </a>        metadata.write(metadataNode);
  11.150 -<a class="c2html-lineno" name="l106" href="#l106">106 </a>
  11.151 -<a class="c2html-lineno" name="l107" href="#l107">107 </a>        <span class="c2html-keyword">if</span> (graph <span class="c2html-keyword">instanceof</span> <span class="c2html-type">Graph</span>) {
  11.152 -<a class="c2html-lineno" name="l108" href="#l108">108 </a>            writeGraph(rootNode.addElement(<span class="c2html-type">TAG_GRAPH</span>));
  11.153 -<a class="c2html-lineno" name="l109" href="#l109">109 </a>        } <span class="c2html-keyword">else</span> <span class="c2html-keyword">if</span> (graph <span class="c2html-keyword">instanceof</span> <span class="c2html-type">Digraph</span>) {
  11.154 -<a class="c2html-lineno" name="l110" href="#l110">110 </a>            writeGraph(rootNode.addElement(<span class="c2html-type">TAG_DIGRAPH</span>));
  11.155 -<a class="c2html-lineno" name="l111" href="#l111">111 </a>        } <span class="c2html-keyword">else</span> {
  11.156 -<a class="c2html-lineno" name="l112" href="#l112">112 </a>            <span class="c2html-keyword">throw</span> <span class="c2html-keyword">new</span> <span class="c2html-type">IOException</span>(<span class="c2html-string">"unsupported graph type"</span>);
  11.157 -<a class="c2html-lineno" name="l113" href="#l113">113 </a>        }
  11.158 -<a class="c2html-lineno" name="l114" href="#l114">114 </a>
  11.159 -<a class="c2html-lineno" name="l115" href="#l115">115 </a>        <span class="c2html-type">XMLWriter</span> writer = <span class="c2html-keyword">new</span> <span class="c2html-type">XMLWriter</span>(out, <span class="c2html-type">OutputFormat</span>.createPrettyPrint());
  11.160 -<a class="c2html-lineno" name="l116" href="#l116">116 </a>        writer.write(doc);
  11.161 -<a class="c2html-lineno" name="l117" href="#l117">117 </a>        writer.flush();
  11.162 -<a class="c2html-lineno" name="l118" href="#l118">118 </a>    }
  11.163 -<a class="c2html-lineno" name="l119" href="#l119">119 </a>
  11.164 -<a class="c2html-lineno" name="l120" href="#l120">120 </a>    <span class="c2html-directive">@Override</span>
  11.165 -<a class="c2html-lineno" name="l121" href="#l121">121 </a>    <span class="c2html-keyword">public</span> <span class="c2html-keyword">void</span> readFrom(<span class="c2html-type">InputStream</span> in) <span class="c2html-keyword">throws</span> <span class="c2html-type">IOException</span> {
  11.166 -<a class="c2html-lineno" name="l122" href="#l122">122 </a>        <span class="c2html-keyword">try</span> {
  11.167 -<a class="c2html-lineno" name="l123" href="#l123">123 </a>            <span class="c2html-type">SAXReader</span> reader = <span class="c2html-keyword">new</span> <span class="c2html-type">SAXReader</span>(true);
  11.168 -<a class="c2html-lineno" name="l124" href="#l124">124 </a>            reader.setStripWhitespaceText(true);
  11.169 -<a class="c2html-lineno" name="l125" href="#l125">125 </a>            
  11.170 -<a class="c2html-lineno" name="l126" href="#l126">126 </a>            reader.setFeature(<span class="c2html-type">Constants</span>.<span class="c2html-type">XERCES_FEATURE_PREFIX</span>+
  11.171 -<a class="c2html-lineno" name="l127" href="#l127">127 </a>                <span class="c2html-type">Constants</span>.<span class="c2html-type">SCHEMA_VALIDATION_FEATURE</span>, true);
  11.172 -<a class="c2html-lineno" name="l128" href="#l128">128 </a>            reader.setProperty(<span class="c2html-type">Constants</span>.<span class="c2html-type">XERCES_PROPERTY_PREFIX</span> +
  11.173 -<a class="c2html-lineno" name="l129" href="#l129">129 </a>                <span class="c2html-type">Constants</span>.<span class="c2html-type">SCHEMA_LOCATION</span>, <span class="c2html-type">String</span>.format(<span class="c2html-string">"%s %s"</span>,
  11.174 -<a class="c2html-lineno" name="l130" href="#l130">130 </a>                    <span class="c2html-type">NAMESPACE</span>.getURI(), <span class="c2html-type">Resources</span>.<span class="c2html-keyword">class</span>.getResource(
  11.175 -<a class="c2html-lineno" name="l131" href="#l131">131 </a>                        <span class="c2html-string">"graph-document.xsd"</span>).toExternalForm()));
  11.176 -<a class="c2html-lineno" name="l132" href="#l132">132 </a>            
  11.177 -<a class="c2html-lineno" name="l133" href="#l133">133 </a>            <span class="c2html-keyword">final</span> <span class="c2html-type">AtomicBoolean</span> passed = <span class="c2html-keyword">new</span> <span class="c2html-type">AtomicBoolean</span>(true);
  11.178 -<a class="c2html-lineno" name="l134" href="#l134">134 </a>            <span class="c2html-keyword">final</span> <span class="c2html-type">AtomicReference</span>&lt;<span class="c2html-type">SAXParseException</span>&gt; xmlerror = <span class="c2html-keyword">new</span> <span class="c2html-type">AtomicReference</span>&lt;&gt;();
  11.179 -<a class="c2html-lineno" name="l135" href="#l135">135 </a>            <span class="c2html-comment">// TODO: we should do more detailed error handling here</span>
  11.180 -<a class="c2html-lineno" name="l136" href="#l136">136 </a>            reader.setErrorHandler(<span class="c2html-keyword">new</span> <span class="c2html-type">ErrorHandler</span>() {
  11.181 -<a class="c2html-lineno" name="l137" href="#l137">137 </a>                <span class="c2html-directive">@Override</span>
  11.182 -<a class="c2html-lineno" name="l138" href="#l138">138 </a>                <span class="c2html-keyword">public</span> <span class="c2html-keyword">void</span> warning(<span class="c2html-type">SAXParseException</span> exception) <span class="c2html-keyword">throws</span> <span class="c2html-type">SAXException</span> {
  11.183 -<a class="c2html-lineno" name="l139" href="#l139">139 </a>                }
  11.184 -<a class="c2html-lineno" name="l140" href="#l140">140 </a>
  11.185 -<a class="c2html-lineno" name="l141" href="#l141">141 </a>                <span class="c2html-directive">@Override</span>
  11.186 -<a class="c2html-lineno" name="l142" href="#l142">142 </a>                <span class="c2html-keyword">public</span> <span class="c2html-keyword">void</span> error(<span class="c2html-type">SAXParseException</span> exception) <span class="c2html-keyword">throws</span> <span class="c2html-type">SAXException</span> {
  11.187 -<a class="c2html-lineno" name="l143" href="#l143">143 </a>                    xmlerror.set(exception);
  11.188 -<a class="c2html-lineno" name="l144" href="#l144">144 </a>                    passed.set(false);
  11.189 -<a class="c2html-lineno" name="l145" href="#l145">145 </a>                }
  11.190 -<a class="c2html-lineno" name="l146" href="#l146">146 </a>
  11.191 -<a class="c2html-lineno" name="l147" href="#l147">147 </a>                <span class="c2html-directive">@Override</span>
  11.192 -<a class="c2html-lineno" name="l148" href="#l148">148 </a>                <span class="c2html-keyword">public</span> <span class="c2html-keyword">void</span> fatalError(<span class="c2html-type">SAXParseException</span> exception) <span class="c2html-keyword">throws</span> <span class="c2html-type">SAXException</span> {
  11.193 -<a class="c2html-lineno" name="l149" href="#l149">149 </a>                    xmlerror.set(exception);
  11.194 -<a class="c2html-lineno" name="l150" href="#l150">150 </a>                    passed.set(false);
  11.195 -<a class="c2html-lineno" name="l151" href="#l151">151 </a>                }
  11.196 -<a class="c2html-lineno" name="l152" href="#l152">152 </a>                
  11.197 -<a class="c2html-lineno" name="l153" href="#l153">153 </a>            });
  11.198 -<a class="c2html-lineno" name="l154" href="#l154">154 </a>            <span class="c2html-type">Document</span> doc = reader.read(in);
  11.199 -<a class="c2html-lineno" name="l155" href="#l155">155 </a>            <span class="c2html-keyword">if</span> (!passed.get()) {
  11.200 -<a class="c2html-lineno" name="l156" href="#l156">156 </a>                <span class="c2html-comment">// TODO: provide details (maybe via separate error object?)</span>
  11.201 -<a class="c2html-lineno" name="l157" href="#l157">157 </a>                <span class="c2html-keyword">throw</span> xmlerror.get();
  11.202 -<a class="c2html-lineno" name="l158" href="#l158">158 </a>            }
  11.203 -<a class="c2html-lineno" name="l159" href="#l159">159 </a>            
  11.204 -<a class="c2html-lineno" name="l160" href="#l160">160 </a>            doc.normalize();
  11.205 -<a class="c2html-lineno" name="l161" href="#l161">161 </a>            
  11.206 -<a class="c2html-lineno" name="l162" href="#l162">162 </a>            <span class="c2html-type">Element</span> root = doc.getRootElement();
  11.207 -<a class="c2html-lineno" name="l163" href="#l163">163 </a>            metadata.read(root.element(<span class="c2html-type">TAG_METADATA</span>));
  11.208 -<a class="c2html-lineno" name="l164" href="#l164">164 </a>            
  11.209 -<a class="c2html-lineno" name="l165" href="#l165">165 </a>            <span class="c2html-keyword">if</span> (graph <span class="c2html-keyword">instanceof</span> <span class="c2html-type">Graph</span>) {
  11.210 -<a class="c2html-lineno" name="l166" href="#l166">166 </a>                readGraph(root.element(<span class="c2html-type">TAG_GRAPH</span>));
  11.211 -<a class="c2html-lineno" name="l167" href="#l167">167 </a>            } <span class="c2html-keyword">else</span> <span class="c2html-keyword">if</span> (graph <span class="c2html-keyword">instanceof</span> <span class="c2html-type">Digraph</span>) {
  11.212 -<a class="c2html-lineno" name="l168" href="#l168">168 </a>                readGraph(root.element(<span class="c2html-type">TAG_DIGRAPH</span>));
  11.213 -<a class="c2html-lineno" name="l169" href="#l169">169 </a>            } <span class="c2html-keyword">else</span> {
  11.214 -<a class="c2html-lineno" name="l170" href="#l170">170 </a>                <span class="c2html-keyword">throw</span> <span class="c2html-keyword">new</span> <span class="c2html-type">IOException</span>(<span class="c2html-string">"unsupported graph type"</span>);
  11.215 -<a class="c2html-lineno" name="l171" href="#l171">171 </a>            }
  11.216 -<a class="c2html-lineno" name="l172" href="#l172">172 </a>        } <span class="c2html-keyword">catch</span> (<span class="c2html-type">DocumentException</span> | <span class="c2html-type">SAXException</span> ex) {
  11.217 -<a class="c2html-lineno" name="l173" href="#l173">173 </a>            <span class="c2html-keyword">throw</span> <span class="c2html-keyword">new</span> <span class="c2html-type">IOException</span>(ex);
  11.218 -<a class="c2html-lineno" name="l174" href="#l174">174 </a>        }
  11.219 -<a class="c2html-lineno" name="l175" href="#l175">175 </a>    }
  11.220 -<a class="c2html-lineno" name="l176" href="#l176">176 </a>}
  11.221 -</pre>
  11.222 -  </body>
  11.223 -</html>
  11.224 -
    12.1 --- a/test/golden-master/plain.html	Mon Oct 03 12:56:28 2022 +0200
    12.2 +++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
    12.3 @@ -1,59 +0,0 @@
    12.4 -<!DOCTYPE html>
    12.5 -<html>
    12.6 -  <head>
    12.7 -    <title>c2html</title>
    12.8 -    <style type="text/css">
    12.9 -      a.c2html-lineno {
   12.10 -        /* as long as user-select isn't widely spread, we throw the bomb */
   12.11 -        -webkit-user-select: none;
   12.12 -        -moz-user-select: none;
   12.13 -        -ms-user-select: none;
   12.14 -        user-select: none;
   12.15 -        display: inline-block;
   12.16 -        font-style: italic;
   12.17 -        text-decoration: none;
   12.18 -        color: grey;
   12.19 -      }
   12.20 -      span.c2html-keyword {
   12.21 -        color: blue;
   12.22 -      }
   12.23 -      span.c2html-macroconst {
   12.24 -        color: cornflowerblue;
   12.25 -      }
   12.26 -      span.c2html-type {
   12.27 -        color: cornflowerblue;
   12.28 -      }
   12.29 -      span.c2html-directive {
   12.30 -        color: green;
   12.31 -      }
   12.32 -      span.c2html-string {
   12.33 -        color: darkorange;
   12.34 -      }
   12.35 -      span.c2html-comment {
   12.36 -        color: grey;
   12.37 -      }
   12.38 -      span.c2html-stdinclude {
   12.39 -        color: darkorange;
   12.40 -      }
   12.41 -      span.c2html-userinclude {
   12.42 -        color: darkorange;
   12.43 -      }
   12.44 -      a.c2html-userinclude {
   12.45 -        color: darkorange;
   12.46 -        text-decoration: underline;
   12.47 -      }
   12.48 -    </style>
   12.49 -  </head>
   12.50 -  <body>
   12.51 -
   12.52 -<pre>
   12.53 -<a class="c2html-lineno" name="l1" href="#l1">1 </a>&lt;/body&gt;
   12.54 -<a class="c2html-lineno" name="l2" href="#l2">2 </a>&lt;/html&gt;
   12.55 -<a class="c2html-lineno" name="l3" href="#l3">3 </a>&lt;!c
   12.56 -<a class="c2html-lineno" name="l4" href="#l4">4 </a>pblock_free(q);
   12.57 -<a class="c2html-lineno" name="l5" href="#l5">5 </a>!&gt;
   12.58 -<a class="c2html-lineno" name="l6" href="#l6">6 </a>
   12.59 -</pre>
   12.60 -  </body>
   12.61 -</html>
   12.62 -
    13.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
    13.2 +++ b/test/gs/bigtest.html	Mon Apr 24 21:01:41 2023 +0200
    13.3 @@ -0,0 +1,865 @@
    13.4 +<!DOCTYPE html>
    13.5 +<html>
    13.6 +  <head>
    13.7 +    <title>c2html</title>
    13.8 +    <style type="text/css">
    13.9 +      div.c2html-code {
   13.10 +        white-space: pre;
   13.11 +        font-family: monospace;
   13.12 +      }
   13.13 +      a.c2html-lineno {
   13.14 +        /* as long as user-select isn't widely spread, we throw the bomb */
   13.15 +        -webkit-user-select: none;
   13.16 +        -moz-user-select: none;
   13.17 +        -ms-user-select: none;
   13.18 +        user-select: none;
   13.19 +        display: inline-block;
   13.20 +        font-style: italic;
   13.21 +        text-decoration: none;
   13.22 +        color: grey;
   13.23 +      }
   13.24 +      span.c2html-keyword {
   13.25 +        color: blue;
   13.26 +      }
   13.27 +      span.c2html-macroconst {
   13.28 +        color: cornflowerblue;
   13.29 +      }
   13.30 +      span.c2html-type {
   13.31 +        color: cornflowerblue;
   13.32 +      }
   13.33 +      span.c2html-directive {
   13.34 +        color: green;
   13.35 +      }
   13.36 +      span.c2html-string {
   13.37 +        color: darkorange;
   13.38 +      }
   13.39 +      span.c2html-comment {
   13.40 +        color: grey;
   13.41 +      }
   13.42 +      span.c2html-stdinclude {
   13.43 +        color: darkorange;
   13.44 +      }
   13.45 +      span.c2html-userinclude {
   13.46 +        color: darkorange;
   13.47 +      }
   13.48 +      a.c2html-userinclude {
   13.49 +        color: darkorange;
   13.50 +        text-decoration: underline;
   13.51 +      }
   13.52 +    </style>
   13.53 +  </head>
   13.54 +  <body>
   13.55 +
   13.56 +<div class="c2html-code">
   13.57 +<a class="c2html-lineno" name="l1" href="#l1">  1 </a><span class="c2html-comment">/*</span>
   13.58 +<a class="c2html-lineno" name="l2" href="#l2">  2 </a><span class="c2html-comment"> * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.</span>
   13.59 +<a class="c2html-lineno" name="l3" href="#l3">  3 </a><span class="c2html-comment"> *</span>
   13.60 +<a class="c2html-lineno" name="l4" href="#l4">  4 </a><span class="c2html-comment"> * Copyright 2014 Mike Becker. All rights reserved.</span>
   13.61 +<a class="c2html-lineno" name="l5" href="#l5">  5 </a><span class="c2html-comment"> *</span>
   13.62 +<a class="c2html-lineno" name="l6" href="#l6">  6 </a><span class="c2html-comment"> * Redistribution and use in source and binary forms, with or without</span>
   13.63 +<a class="c2html-lineno" name="l7" href="#l7">  7 </a><span class="c2html-comment"> * modification, are permitted provided that the following conditions are met:</span>
   13.64 +<a class="c2html-lineno" name="l8" href="#l8">  8 </a><span class="c2html-comment"> *</span>
   13.65 +<a class="c2html-lineno" name="l9" href="#l9">  9 </a><span class="c2html-comment"> *   1. Redistributions of source code must retain the above copyright</span>
   13.66 +<a class="c2html-lineno" name="l10" href="#l10"> 10 </a><span class="c2html-comment"> *      notice, this list of conditions and the following disclaimer.</span>
   13.67 +<a class="c2html-lineno" name="l11" href="#l11"> 11 </a><span class="c2html-comment"> *</span>
   13.68 +<a class="c2html-lineno" name="l12" href="#l12"> 12 </a><span class="c2html-comment"> *   2. Redistributions in binary form must reproduce the above copyright</span>
   13.69 +<a class="c2html-lineno" name="l13" href="#l13"> 13 </a><span class="c2html-comment"> *      notice, this list of conditions and the following disclaimer in the</span>
   13.70 +<a class="c2html-lineno" name="l14" href="#l14"> 14 </a><span class="c2html-comment"> *      documentation and/or other materials provided with the distribution.</span>
   13.71 +<a class="c2html-lineno" name="l15" href="#l15"> 15 </a><span class="c2html-comment"> *</span>
   13.72 +<a class="c2html-lineno" name="l16" href="#l16"> 16 </a><span class="c2html-comment"> * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"</span>
   13.73 +<a class="c2html-lineno" name="l17" href="#l17"> 17 </a><span class="c2html-comment"> * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE</span>
   13.74 +<a class="c2html-lineno" name="l18" href="#l18"> 18 </a><span class="c2html-comment"> * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE</span>
   13.75 +<a class="c2html-lineno" name="l19" href="#l19"> 19 </a><span class="c2html-comment"> * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE</span>
   13.76 +<a class="c2html-lineno" name="l20" href="#l20"> 20 </a><span class="c2html-comment"> * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR</span>
   13.77 +<a class="c2html-lineno" name="l21" href="#l21"> 21 </a><span class="c2html-comment"> * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF</span>
   13.78 +<a class="c2html-lineno" name="l22" href="#l22"> 22 </a><span class="c2html-comment"> * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS</span>
   13.79 +<a class="c2html-lineno" name="l23" href="#l23"> 23 </a><span class="c2html-comment"> * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN</span>
   13.80 +<a class="c2html-lineno" name="l24" href="#l24"> 24 </a><span class="c2html-comment"> * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)</span>
   13.81 +<a class="c2html-lineno" name="l25" href="#l25"> 25 </a><span class="c2html-comment"> * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE</span>
   13.82 +<a class="c2html-lineno" name="l26" href="#l26"> 26 </a><span class="c2html-comment"> * POSSIBILITY OF SUCH DAMAGE.</span>
   13.83 +<a class="c2html-lineno" name="l27" href="#l27"> 27 </a><span class="c2html-comment"> *</span>
   13.84 +<a class="c2html-lineno" name="l28" href="#l28"> 28 </a><span class="c2html-comment"> */</span>
   13.85 +<a class="c2html-lineno" name="l29" href="#l29"> 29 </a>
   13.86 +<a class="c2html-lineno" name="l30" href="#l30"> 30 </a><span class="c2html-directive">#include</span> <a class="c2html-userinclude" href="rules.h">"rules.h"</a>
   13.87 +<a class="c2html-lineno" name="l31" href="#l31"> 31 </a><span class="c2html-directive">#include</span> <a class="c2html-userinclude" href="chess.h">"chess.h"</a>
   13.88 +<a class="c2html-lineno" name="l32" href="#l32"> 32 </a><span class="c2html-directive">#include</span> <span class="c2html-stdinclude">&lt;string.h&gt;</span>
   13.89 +<a class="c2html-lineno" name="l33" href="#l33"> 33 </a><span class="c2html-directive">#include</span> <span class="c2html-stdinclude">&lt;stdlib.h&gt;</span>
   13.90 +<a class="c2html-lineno" name="l34" href="#l34"> 34 </a><span class="c2html-directive">#include</span> <span class="c2html-stdinclude">&lt;sys/time.h&gt;</span>
   13.91 +<a class="c2html-lineno" name="l35" href="#l35"> 35 </a>
   13.92 +<a class="c2html-lineno" name="l36" href="#l36"> 36 </a><span class="c2html-keyword">static</span> GameState gamestate_copy_sim(GameState *gamestate) {
   13.93 +<a class="c2html-lineno" name="l37" href="#l37"> 37 </a>    GameState simulation = *gamestate;
   13.94 +<a class="c2html-lineno" name="l38" href="#l38"> 38 </a>    <span class="c2html-keyword">if</span> (simulation.lastmove) {
   13.95 +<a class="c2html-lineno" name="l39" href="#l39"> 39 </a>        MoveList *lastmovecopy = malloc(<span class="c2html-keyword">sizeof</span>(MoveList));
   13.96 +<a class="c2html-lineno" name="l40" href="#l40"> 40 </a>        *lastmovecopy = *(simulation.lastmove);
   13.97 +<a class="c2html-lineno" name="l41" href="#l41"> 41 </a>        simulation.movelist = simulation.lastmove = lastmovecopy;
   13.98 +<a class="c2html-lineno" name="l42" href="#l42"> 42 </a>    }
   13.99 +<a class="c2html-lineno" name="l43" href="#l43"> 43 </a>
  13.100 +<a class="c2html-lineno" name="l44" href="#l44"> 44 </a>    <span class="c2html-keyword">return</span> simulation;
  13.101 +<a class="c2html-lineno" name="l45" href="#l45"> 45 </a>}
  13.102 +<a class="c2html-lineno" name="l46" href="#l46"> 46 </a>
  13.103 +<a class="c2html-lineno" name="l47" href="#l47"> 47 </a><span class="c2html-keyword">void</span> gamestate_init(GameState *gamestate) {
  13.104 +<a class="c2html-lineno" name="l48" href="#l48"> 48 </a>    memset(gamestate, <span class="c2html-macroconst">0</span>, <span class="c2html-keyword">sizeof</span>(GameState));
  13.105 +<a class="c2html-lineno" name="l49" href="#l49"> 49 </a>    
  13.106 +<a class="c2html-lineno" name="l50" href="#l50"> 50 </a>    Board initboard = {
  13.107 +<a class="c2html-lineno" name="l51" href="#l51"> 51 </a>        {<span class="c2html-macroconst">WROOK</span>, <span class="c2html-macroconst">WKNIGHT</span>, <span class="c2html-macroconst">WBISHOP</span>, <span class="c2html-macroconst">WQUEEN</span>, <span class="c2html-macroconst">WKING</span>, <span class="c2html-macroconst">WBISHOP</span>, <span class="c2html-macroconst">WKNIGHT</span>, <span class="c2html-macroconst">WROOK</span>},
  13.108 +<a class="c2html-lineno" name="l52" href="#l52"> 52 </a>        {<span class="c2html-macroconst">WPAWN</span>, <span class="c2html-macroconst">WPAWN</span>,   <span class="c2html-macroconst">WPAWN</span>,   <span class="c2html-macroconst">WPAWN</span>,  <span class="c2html-macroconst">WPAWN</span>, <span class="c2html-macroconst">WPAWN</span>,   <span class="c2html-macroconst">WPAWN</span>,   <span class="c2html-macroconst">WPAWN</span>},
  13.109 +<a class="c2html-lineno" name="l53" href="#l53"> 53 </a>        {<span class="c2html-macroconst">0</span>,     <span class="c2html-macroconst">0</span>,       <span class="c2html-macroconst">0</span>,       <span class="c2html-macroconst">0</span>,      <span class="c2html-macroconst">0</span>,     <span class="c2html-macroconst">0</span>,       <span class="c2html-macroconst">0</span>,       <span class="c2html-macroconst">0</span>},
  13.110 +<a class="c2html-lineno" name="l54" href="#l54"> 54 </a>        {<span class="c2html-macroconst">0</span>,     <span class="c2html-macroconst">0</span>,       <span class="c2html-macroconst">0</span>,       <span class="c2html-macroconst">0</span>,      <span class="c2html-macroconst">0</span>,     <span class="c2html-macroconst">0</span>,       <span class="c2html-macroconst">0</span>,       <span class="c2html-macroconst">0</span>},
  13.111 +<a class="c2html-lineno" name="l55" href="#l55"> 55 </a>        {<span class="c2html-macroconst">0</span>,     <span class="c2html-macroconst">0</span>,       <span class="c2html-macroconst">0</span>,       <span class="c2html-macroconst">0</span>,      <span class="c2html-macroconst">0</span>,     <span class="c2html-macroconst">0</span>,       <span class="c2html-macroconst">0</span>,       <span class="c2html-macroconst">0</span>},
  13.112 +<a class="c2html-lineno" name="l56" href="#l56"> 56 </a>        {<span class="c2html-macroconst">0</span>,     <span class="c2html-macroconst">0</span>,       <span class="c2html-macroconst">0</span>,       <span class="c2html-macroconst">0</span>,      <span class="c2html-macroconst">0</span>,     <span class="c2html-macroconst">0</span>,       <span class="c2html-macroconst">0</span>,       <span class="c2html-macroconst">0</span>},
  13.113 +<a class="c2html-lineno" name="l57" href="#l57"> 57 </a>        {<span class="c2html-macroconst">BPAWN</span>, <span class="c2html-macroconst">BPAWN</span>,   <span class="c2html-macroconst">BPAWN</span>,   <span class="c2html-macroconst">BPAWN</span>,  <span class="c2html-macroconst">BPAWN</span>, <span class="c2html-macroconst">BPAWN</span>,   <span class="c2html-macroconst">BPAWN</span>,   <span class="c2html-macroconst">BPAWN</span>},
  13.114 +<a class="c2html-lineno" name="l58" href="#l58"> 58 </a>        {<span class="c2html-macroconst">BROOK</span>, <span class="c2html-macroconst">BKNIGHT</span>, <span class="c2html-macroconst">BBISHOP</span>, <span class="c2html-macroconst">BQUEEN</span>, <span class="c2html-macroconst">BKING</span>, <span class="c2html-macroconst">BBISHOP</span>, <span class="c2html-macroconst">BKNIGHT</span>, <span class="c2html-macroconst">BROOK</span>}
  13.115 +<a class="c2html-lineno" name="l59" href="#l59"> 59 </a>    };
  13.116 +<a class="c2html-lineno" name="l60" href="#l60"> 60 </a>    memcpy(gamestate-&gt;board, initboard, <span class="c2html-keyword">sizeof</span>(Board));
  13.117 +<a class="c2html-lineno" name="l61" href="#l61"> 61 </a>}
  13.118 +<a class="c2html-lineno" name="l62" href="#l62"> 62 </a>
  13.119 +<a class="c2html-lineno" name="l63" href="#l63"> 63 </a><span class="c2html-keyword">void</span> gamestate_cleanup(GameState *gamestate) {
  13.120 +<a class="c2html-lineno" name="l64" href="#l64"> 64 </a>    MoveList *elem;
  13.121 +<a class="c2html-lineno" name="l65" href="#l65"> 65 </a>    elem = gamestate-&gt;movelist;
  13.122 +<a class="c2html-lineno" name="l66" href="#l66"> 66 </a>    <span class="c2html-keyword">while</span> (elem) {
  13.123 +<a class="c2html-lineno" name="l67" href="#l67"> 67 </a>        MoveList *cur = elem;
  13.124 +<a class="c2html-lineno" name="l68" href="#l68"> 68 </a>        elem = elem-&gt;next;
  13.125 +<a class="c2html-lineno" name="l69" href="#l69"> 69 </a>        free(cur);
  13.126 +<a class="c2html-lineno" name="l70" href="#l70"> 70 </a>    };
  13.127 +<a class="c2html-lineno" name="l71" href="#l71"> 71 </a>}
  13.128 +<a class="c2html-lineno" name="l72" href="#l72"> 72 </a>
  13.129 +<a class="c2html-lineno" name="l73" href="#l73"> 73 </a><span class="c2html-comment">/* MUST be called IMMEDIATLY after applying a move to work correctly */</span>
  13.130 +<a class="c2html-lineno" name="l74" href="#l74"> 74 </a><span class="c2html-keyword">static</span> <span class="c2html-keyword">void</span> format_move(GameState *gamestate, Move *move) {
  13.131 +<a class="c2html-lineno" name="l75" href="#l75"> 75 </a>    <span class="c2html-keyword">char</span> *string = move-&gt;string;
  13.132 +<a class="c2html-lineno" name="l76" href="#l76"> 76 </a>    
  13.133 +<a class="c2html-lineno" name="l77" href="#l77"> 77 </a>    <span class="c2html-comment">/* at least 8 characters should be available, wipe them out */</span>
  13.134 +<a class="c2html-lineno" name="l78" href="#l78"> 78 </a>    memset(string, <span class="c2html-macroconst">0</span>, <span class="c2html-macroconst">8</span>);
  13.135 +<a class="c2html-lineno" name="l79" href="#l79"> 79 </a>    
  13.136 +<a class="c2html-lineno" name="l80" href="#l80"> 80 </a>    <span class="c2html-comment">/* special formats for castling */</span>
  13.137 +<a class="c2html-lineno" name="l81" href="#l81"> 81 </a>    <span class="c2html-keyword">if</span> ((move-&gt;piece&<span class="c2html-macroconst">PIECE_MASK</span>) == <span class="c2html-macroconst">KING</span> &&
  13.138 +<a class="c2html-lineno" name="l82" href="#l82"> 82 </a>            abs(move-&gt;tofile-move-&gt;fromfile) == <span class="c2html-macroconst">2</span>) {
  13.139 +<a class="c2html-lineno" name="l83" href="#l83"> 83 </a>        <span class="c2html-keyword">if</span> (move-&gt;tofile==fileidx(<span class="c2html-string">'c'</span>)) {
  13.140 +<a class="c2html-lineno" name="l84" href="#l84"> 84 </a>            memcpy(string, <span class="c2html-string">"O-O-O"</span>, <span class="c2html-macroconst">5</span>);
  13.141 +<a class="c2html-lineno" name="l85" href="#l85"> 85 </a>        } <span class="c2html-keyword">else</span> {
  13.142 +<a class="c2html-lineno" name="l86" href="#l86"> 86 </a>            memcpy(string, <span class="c2html-string">"O-O"</span>, <span class="c2html-macroconst">3</span>);
  13.143 +<a class="c2html-lineno" name="l87" href="#l87"> 87 </a>        }
  13.144 +<a class="c2html-lineno" name="l88" href="#l88"> 88 </a>    }
  13.145 +<a class="c2html-lineno" name="l89" href="#l89"> 89 </a>
  13.146 +<a class="c2html-lineno" name="l90" href="#l90"> 90 </a>    <span class="c2html-comment">/* start by notating the piece character */</span>
  13.147 +<a class="c2html-lineno" name="l91" href="#l91"> 91 </a>    string[<span class="c2html-macroconst">0</span>] = getpiecechr(move-&gt;piece);
  13.148 +<a class="c2html-lineno" name="l92" href="#l92"> 92 </a>    <span class="c2html-keyword">int</span> idx = string[<span class="c2html-macroconst">0</span>] ? <span class="c2html-macroconst">1</span> : <span class="c2html-macroconst">0</span>;
  13.149 +<a class="c2html-lineno" name="l93" href="#l93"> 93 </a>    
  13.150 +<a class="c2html-lineno" name="l94" href="#l94"> 94 </a>    <span class="c2html-comment">/* find out how many source information we do need */</span>
  13.151 +<a class="c2html-lineno" name="l95" href="#l95"> 95 </a>    <span class="c2html-type">uint8_t</span> piece = move-&gt;piece & <span class="c2html-macroconst">PIECE_MASK</span>;
  13.152 +<a class="c2html-lineno" name="l96" href="#l96"> 96 </a>    <span class="c2html-keyword">if</span> (piece == <span class="c2html-macroconst">PAWN</span>) {
  13.153 +<a class="c2html-lineno" name="l97" href="#l97"> 97 </a>        <span class="c2html-keyword">if</span> (move-&gt;capture) {
  13.154 +<a class="c2html-lineno" name="l98" href="#l98"> 98 </a>            string[idx++] = filechr(move-&gt;fromfile);
  13.155 +<a class="c2html-lineno" name="l99" href="#l99"> 99 </a>        }
  13.156 +<a class="c2html-lineno" name="l100" href="#l100">100 </a>    } <span class="c2html-keyword">else</span> <span class="c2html-keyword">if</span> (piece != <span class="c2html-macroconst">KING</span>) {
  13.157 +<a class="c2html-lineno" name="l101" href="#l101">101 </a>        Move threats[<span class="c2html-macroconst">16</span>];
  13.158 +<a class="c2html-lineno" name="l102" href="#l102">102 </a>        <span class="c2html-type">uint8_t</span> threatcount;
  13.159 +<a class="c2html-lineno" name="l103" href="#l103">103 </a>        get_real_threats(gamestate, move-&gt;torow, move-&gt;tofile,
  13.160 +<a class="c2html-lineno" name="l104" href="#l104">104 </a>            move-&gt;piece&<span class="c2html-macroconst">COLOR_MASK</span>, threats, &threatcount);
  13.161 +<a class="c2html-lineno" name="l105" href="#l105">105 </a>        <span class="c2html-keyword">if</span> (threatcount &gt; <span class="c2html-macroconst">1</span>) {
  13.162 +<a class="c2html-lineno" name="l106" href="#l106">106 </a>            <span class="c2html-keyword">int</span> ambrows = <span class="c2html-macroconst">0</span>, ambfiles = <span class="c2html-macroconst">0</span>;
  13.163 +<a class="c2html-lineno" name="l107" href="#l107">107 </a>            <span class="c2html-keyword">for</span> (<span class="c2html-type">uint8_t</span> i = <span class="c2html-macroconst">0</span> ; i &lt; threatcount ; i++) {
  13.164 +<a class="c2html-lineno" name="l108" href="#l108">108 </a>                <span class="c2html-keyword">if</span> (threats[i].fromrow == move-&gt;fromrow) {
  13.165 +<a class="c2html-lineno" name="l109" href="#l109">109 </a>                    ambrows++;
  13.166 +<a class="c2html-lineno" name="l110" href="#l110">110 </a>                }
  13.167 +<a class="c2html-lineno" name="l111" href="#l111">111 </a>                <span class="c2html-keyword">if</span> (threats[i].fromfile == move-&gt;fromfile) {
  13.168 +<a class="c2html-lineno" name="l112" href="#l112">112 </a>                    ambfiles++;
  13.169 +<a class="c2html-lineno" name="l113" href="#l113">113 </a>                }
  13.170 +<a class="c2html-lineno" name="l114" href="#l114">114 </a>            }
  13.171 +<a class="c2html-lineno" name="l115" href="#l115">115 </a>            <span class="c2html-comment">/* ambiguous row, name file */</span>
  13.172 +<a class="c2html-lineno" name="l116" href="#l116">116 </a>            <span class="c2html-keyword">if</span> (ambrows &gt; <span class="c2html-macroconst">1</span>) {
  13.173 +<a class="c2html-lineno" name="l117" href="#l117">117 </a>                string[idx++] = filechr(move-&gt;fromfile);
  13.174 +<a class="c2html-lineno" name="l118" href="#l118">118 </a>            }
  13.175 +<a class="c2html-lineno" name="l119" href="#l119">119 </a>            <span class="c2html-comment">/* ambiguous file, name row */</span>
  13.176 +<a class="c2html-lineno" name="l120" href="#l120">120 </a>            <span class="c2html-keyword">if</span> (ambfiles &gt; <span class="c2html-macroconst">1</span>) {
  13.177 +<a class="c2html-lineno" name="l121" href="#l121">121 </a>                string[idx++] = filechr(move-&gt;fromrow);
  13.178 +<a class="c2html-lineno" name="l122" href="#l122">122 </a>            }
  13.179 +<a class="c2html-lineno" name="l123" href="#l123">123 </a>        }
  13.180 +<a class="c2html-lineno" name="l124" href="#l124">124 </a>    }
  13.181 +<a class="c2html-lineno" name="l125" href="#l125">125 </a>    
  13.182 +<a class="c2html-lineno" name="l126" href="#l126">126 </a>    <span class="c2html-comment">/* capturing? */</span>
  13.183 +<a class="c2html-lineno" name="l127" href="#l127">127 </a>    <span class="c2html-keyword">if</span> (move-&gt;capture) {
  13.184 +<a class="c2html-lineno" name="l128" href="#l128">128 </a>        string[idx++] = <span class="c2html-string">'x'</span>;
  13.185 +<a class="c2html-lineno" name="l129" href="#l129">129 </a>    }
  13.186 +<a class="c2html-lineno" name="l130" href="#l130">130 </a>    
  13.187 +<a class="c2html-lineno" name="l131" href="#l131">131 </a>    <span class="c2html-comment">/* destination */</span>
  13.188 +<a class="c2html-lineno" name="l132" href="#l132">132 </a>    string[idx++] = filechr(move-&gt;tofile);
  13.189 +<a class="c2html-lineno" name="l133" href="#l133">133 </a>    string[idx++] = rowchr(move-&gt;torow);
  13.190 +<a class="c2html-lineno" name="l134" href="#l134">134 </a>    
  13.191 +<a class="c2html-lineno" name="l135" href="#l135">135 </a>    <span class="c2html-comment">/* promotion? */</span>
  13.192 +<a class="c2html-lineno" name="l136" href="#l136">136 </a>    <span class="c2html-keyword">if</span> (move-&gt;promotion) {
  13.193 +<a class="c2html-lineno" name="l137" href="#l137">137 </a>        string[idx++] = <span class="c2html-string">'='</span>;
  13.194 +<a class="c2html-lineno" name="l138" href="#l138">138 </a>        string[idx++] = getpiecechr(move-&gt;promotion);
  13.195 +<a class="c2html-lineno" name="l139" href="#l139">139 </a>    }
  13.196 +<a class="c2html-lineno" name="l140" href="#l140">140 </a>    
  13.197 +<a class="c2html-lineno" name="l141" href="#l141">141 </a>    <span class="c2html-comment">/* check? */</span>
  13.198 +<a class="c2html-lineno" name="l142" href="#l142">142 </a>    <span class="c2html-keyword">if</span> (move-&gt;check) {
  13.199 +<a class="c2html-lineno" name="l143" href="#l143">143 </a>        <span class="c2html-comment">/* works only, if this function is called when applying the move */</span>
  13.200 +<a class="c2html-lineno" name="l144" href="#l144">144 </a>        string[idx++] = gamestate-&gt;checkmate?<span class="c2html-string">'#'</span>:<span class="c2html-string">'+'</span>;
  13.201 +<a class="c2html-lineno" name="l145" href="#l145">145 </a>    }
  13.202 +<a class="c2html-lineno" name="l146" href="#l146">146 </a>}
  13.203 +<a class="c2html-lineno" name="l147" href="#l147">147 </a>
  13.204 +<a class="c2html-lineno" name="l148" href="#l148">148 </a><span class="c2html-keyword">static</span> <span class="c2html-keyword">void</span> addmove(GameState* gamestate, Move *move) {
  13.205 +<a class="c2html-lineno" name="l149" href="#l149">149 </a>    MoveList *elem = malloc(<span class="c2html-keyword">sizeof</span>(MoveList));
  13.206 +<a class="c2html-lineno" name="l150" href="#l150">150 </a>    elem-&gt;next = <span class="c2html-macroconst">NULL</span>;
  13.207 +<a class="c2html-lineno" name="l151" href="#l151">151 </a>    elem-&gt;move = *move;
  13.208 +<a class="c2html-lineno" name="l152" href="#l152">152 </a>    
  13.209 +<a class="c2html-lineno" name="l153" href="#l153">153 </a>    <span class="c2html-keyword">struct</span> timeval curtimestamp;
  13.210 +<a class="c2html-lineno" name="l154" href="#l154">154 </a>    gettimeofday(&curtimestamp, <span class="c2html-macroconst">NULL</span>);
  13.211 +<a class="c2html-lineno" name="l155" href="#l155">155 </a>    elem-&gt;move.timestamp.tv_sec = curtimestamp.tv_sec;
  13.212 +<a class="c2html-lineno" name="l156" href="#l156">156 </a>    elem-&gt;move.timestamp.tv_usec = curtimestamp.tv_usec;
  13.213 +<a class="c2html-lineno" name="l157" href="#l157">157 </a>    
  13.214 +<a class="c2html-lineno" name="l158" href="#l158">158 </a>    <span class="c2html-keyword">if</span> (gamestate-&gt;lastmove) {
  13.215 +<a class="c2html-lineno" name="l159" href="#l159">159 </a>        <span class="c2html-keyword">struct</span> movetimeval *lasttstamp = &(gamestate-&gt;lastmove-&gt;move.timestamp);
  13.216 +<a class="c2html-lineno" name="l160" href="#l160">160 </a>        <span class="c2html-type">uint64_t</span> sec = curtimestamp.tv_sec - lasttstamp-&gt;tv_sec;
  13.217 +<a class="c2html-lineno" name="l161" href="#l161">161 </a>        <span class="c2html-type">suseconds_t</span> micros;
  13.218 +<a class="c2html-lineno" name="l162" href="#l162">162 </a>        <span class="c2html-keyword">if</span> (curtimestamp.tv_usec &lt; lasttstamp-&gt;tv_usec) {
  13.219 +<a class="c2html-lineno" name="l163" href="#l163">163 </a>            micros = 1e6L-(lasttstamp-&gt;tv_usec - curtimestamp.tv_usec);
  13.220 +<a class="c2html-lineno" name="l164" href="#l164">164 </a>            sec--;
  13.221 +<a class="c2html-lineno" name="l165" href="#l165">165 </a>        } <span class="c2html-keyword">else</span> {
  13.222 +<a class="c2html-lineno" name="l166" href="#l166">166 </a>            micros = curtimestamp.tv_usec - lasttstamp-&gt;tv_usec;
  13.223 +<a class="c2html-lineno" name="l167" href="#l167">167 </a>        }
  13.224 +<a class="c2html-lineno" name="l168" href="#l168">168 </a>        
  13.225 +<a class="c2html-lineno" name="l169" href="#l169">169 </a>        elem-&gt;move.movetime.tv_sec = sec;
  13.226 +<a class="c2html-lineno" name="l170" href="#l170">170 </a>        elem-&gt;move.movetime.tv_usec = micros;
  13.227 +<a class="c2html-lineno" name="l171" href="#l171">171 </a>        
  13.228 +<a class="c2html-lineno" name="l172" href="#l172">172 </a>        gamestate-&gt;lastmove-&gt;next = elem;
  13.229 +<a class="c2html-lineno" name="l173" href="#l173">173 </a>        gamestate-&gt;lastmove = elem;
  13.230 +<a class="c2html-lineno" name="l174" href="#l174">174 </a>    } <span class="c2html-keyword">else</span> {
  13.231 +<a class="c2html-lineno" name="l175" href="#l175">175 </a>        elem-&gt;move.movetime.tv_usec = <span class="c2html-macroconst">0</span>;
  13.232 +<a class="c2html-lineno" name="l176" href="#l176">176 </a>        elem-&gt;move.movetime.tv_sec = <span class="c2html-macroconst">0</span>;
  13.233 +<a class="c2html-lineno" name="l177" href="#l177">177 </a>        gamestate-&gt;movelist = gamestate-&gt;lastmove = elem;
  13.234 +<a class="c2html-lineno" name="l178" href="#l178">178 </a>    }
  13.235 +<a class="c2html-lineno" name="l179" href="#l179">179 </a>}
  13.236 +<a class="c2html-lineno" name="l180" href="#l180">180 </a>
  13.237 +<a class="c2html-lineno" name="l181" href="#l181">181 </a><span class="c2html-keyword">char</span> getpiecechr(<span class="c2html-type">uint8_t</span> piece) {
  13.238 +<a class="c2html-lineno" name="l182" href="#l182">182 </a>    <span class="c2html-keyword">switch</span> (piece & <span class="c2html-macroconst">PIECE_MASK</span>) {
  13.239 +<a class="c2html-lineno" name="l183" href="#l183">183 </a>    <span class="c2html-keyword">case</span> <span class="c2html-macroconst">ROOK</span>: <span class="c2html-keyword">return</span> <span class="c2html-string">'R'</span>;
  13.240 +<a class="c2html-lineno" name="l184" href="#l184">184 </a>    <span class="c2html-keyword">case</span> <span class="c2html-macroconst">KNIGHT</span>: <span class="c2html-keyword">return</span> <span class="c2html-string">'N'</span>;
  13.241 +<a class="c2html-lineno" name="l185" href="#l185">185 </a>    <span class="c2html-keyword">case</span> <span class="c2html-macroconst">BISHOP</span>: <span class="c2html-keyword">return</span> <span class="c2html-string">'B'</span>;
  13.242 +<a class="c2html-lineno" name="l186" href="#l186">186 </a>    <span class="c2html-keyword">case</span> <span class="c2html-macroconst">QUEEN</span>: <span class="c2html-keyword">return</span> <span class="c2html-string">'Q'</span>;
  13.243 +<a class="c2html-lineno" name="l187" href="#l187">187 </a>    <span class="c2html-keyword">case</span> <span class="c2html-macroconst">KING</span>: <span class="c2html-keyword">return</span> <span class="c2html-string">'K'</span>;
  13.244 +<a class="c2html-lineno" name="l188" href="#l188">188 </a>    <span class="c2html-keyword">default</span>: <span class="c2html-keyword">return</span> <span class="c2html-string">'\0'</span>;
  13.245 +<a class="c2html-lineno" name="l189" href="#l189">189 </a>    }
  13.246 +<a class="c2html-lineno" name="l190" href="#l190">190 </a>}
  13.247 +<a class="c2html-lineno" name="l191" href="#l191">191 </a>
  13.248 +<a class="c2html-lineno" name="l192" href="#l192">192 </a><span class="c2html-type">uint8_t</span> getpiece(<span class="c2html-keyword">char</span> c) {
  13.249 +<a class="c2html-lineno" name="l193" href="#l193">193 </a>    <span class="c2html-keyword">switch</span> (c) {
  13.250 +<a class="c2html-lineno" name="l194" href="#l194">194 </a>        <span class="c2html-keyword">case</span> <span class="c2html-string">'R'</span>: <span class="c2html-keyword">return</span> <span class="c2html-macroconst">ROOK</span>;
  13.251 +<a class="c2html-lineno" name="l195" href="#l195">195 </a>        <span class="c2html-keyword">case</span> <span class="c2html-string">'N'</span>: <span class="c2html-keyword">return</span> <span class="c2html-macroconst">KNIGHT</span>;
  13.252 +<a class="c2html-lineno" name="l196" href="#l196">196 </a>        <span class="c2html-keyword">case</span> <span class="c2html-string">'B'</span>: <span class="c2html-keyword">return</span> <span class="c2html-macroconst">BISHOP</span>;
  13.253 +<a class="c2html-lineno" name="l197" href="#l197">197 </a>        <span class="c2html-keyword">case</span> <span class="c2html-string">'Q'</span>: <span class="c2html-keyword">return</span> <span class="c2html-macroconst">QUEEN</span>;
  13.254 +<a class="c2html-lineno" name="l198" href="#l198">198 </a>        <span class="c2html-keyword">case</span> <span class="c2html-string">'K'</span>: <span class="c2html-keyword">return</span> <span class="c2html-macroconst">KING</span>;
  13.255 +<a class="c2html-lineno" name="l199" href="#l199">199 </a>        <span class="c2html-keyword">default</span>: <span class="c2html-keyword">return</span> <span class="c2html-macroconst">0</span>;
  13.256 +<a class="c2html-lineno" name="l200" href="#l200">200 </a>    }
  13.257 +<a class="c2html-lineno" name="l201" href="#l201">201 </a>}
  13.258 +<a class="c2html-lineno" name="l202" href="#l202">202 </a>
  13.259 +<a class="c2html-lineno" name="l203" href="#l203">203 </a><span class="c2html-keyword">static</span> <span class="c2html-keyword">void</span> apply_move_impl(GameState *gamestate, Move *move, _Bool simulate) {
  13.260 +<a class="c2html-lineno" name="l204" href="#l204">204 </a>    <span class="c2html-type">uint8_t</span> piece = move-&gt;piece & <span class="c2html-macroconst">PIECE_MASK</span>;
  13.261 +<a class="c2html-lineno" name="l205" href="#l205">205 </a>    <span class="c2html-type">uint8_t</span> color = move-&gt;piece & <span class="c2html-macroconst">COLOR_MASK</span>;
  13.262 +<a class="c2html-lineno" name="l206" href="#l206">206 </a>    
  13.263 +<a class="c2html-lineno" name="l207" href="#l207">207 </a>    <span class="c2html-comment">/* en passant capture */</span>
  13.264 +<a class="c2html-lineno" name="l208" href="#l208">208 </a>    <span class="c2html-keyword">if</span> (move-&gt;capture && piece == <span class="c2html-macroconst">PAWN</span> &&
  13.265 +<a class="c2html-lineno" name="l209" href="#l209">209 </a>        mdst(gamestate-&gt;board, move) == <span class="c2html-macroconst">0</span>) {
  13.266 +<a class="c2html-lineno" name="l210" href="#l210">210 </a>        gamestate-&gt;board[move-&gt;fromrow][move-&gt;tofile] = <span class="c2html-macroconst">0</span>;
  13.267 +<a class="c2html-lineno" name="l211" href="#l211">211 </a>    }
  13.268 +<a class="c2html-lineno" name="l212" href="#l212">212 </a>    
  13.269 +<a class="c2html-lineno" name="l213" href="#l213">213 </a>    <span class="c2html-comment">/* remove old en passant threats */</span>
  13.270 +<a class="c2html-lineno" name="l214" href="#l214">214 </a>    <span class="c2html-keyword">for</span> (<span class="c2html-type">uint8_t</span> file = <span class="c2html-macroconst">0</span> ; file &lt; <span class="c2html-macroconst">8</span> ; file++) {
  13.271 +<a class="c2html-lineno" name="l215" href="#l215">215 </a>        gamestate-&gt;board[<span class="c2html-macroconst">3</span>][file] &= ~<span class="c2html-macroconst">ENPASSANT_THREAT</span>;
  13.272 +<a class="c2html-lineno" name="l216" href="#l216">216 </a>        gamestate-&gt;board[<span class="c2html-macroconst">4</span>][file] &= ~<span class="c2html-macroconst">ENPASSANT_THREAT</span>;
  13.273 +<a class="c2html-lineno" name="l217" href="#l217">217 </a>    }
  13.274 +<a class="c2html-lineno" name="l218" href="#l218">218 </a>    
  13.275 +<a class="c2html-lineno" name="l219" href="#l219">219 </a>    <span class="c2html-comment">/* add new en passant threat */</span>
  13.276 +<a class="c2html-lineno" name="l220" href="#l220">220 </a>    <span class="c2html-keyword">if</span> (piece == <span class="c2html-macroconst">PAWN</span> && (
  13.277 +<a class="c2html-lineno" name="l221" href="#l221">221 </a>        (move-&gt;fromrow == <span class="c2html-macroconst">1</span> && move-&gt;torow == <span class="c2html-macroconst">3</span>) ||
  13.278 +<a class="c2html-lineno" name="l222" href="#l222">222 </a>        (move-&gt;fromrow == <span class="c2html-macroconst">6</span> && move-&gt;torow == <span class="c2html-macroconst">4</span>))) {
  13.279 +<a class="c2html-lineno" name="l223" href="#l223">223 </a>        move-&gt;piece |= <span class="c2html-macroconst">ENPASSANT_THREAT</span>;
  13.280 +<a class="c2html-lineno" name="l224" href="#l224">224 </a>    }
  13.281 +<a class="c2html-lineno" name="l225" href="#l225">225 </a>    
  13.282 +<a class="c2html-lineno" name="l226" href="#l226">226 </a>    <span class="c2html-comment">/* move (and maybe capture or promote) */</span>
  13.283 +<a class="c2html-lineno" name="l227" href="#l227">227 </a>    msrc(gamestate-&gt;board, move) = <span class="c2html-macroconst">0</span>;
  13.284 +<a class="c2html-lineno" name="l228" href="#l228">228 </a>    <span class="c2html-keyword">if</span> (move-&gt;promotion) {
  13.285 +<a class="c2html-lineno" name="l229" href="#l229">229 </a>        mdst(gamestate-&gt;board, move) = move-&gt;promotion;
  13.286 +<a class="c2html-lineno" name="l230" href="#l230">230 </a>    } <span class="c2html-keyword">else</span> {
  13.287 +<a class="c2html-lineno" name="l231" href="#l231">231 </a>        mdst(gamestate-&gt;board, move) = move-&gt;piece;
  13.288 +<a class="c2html-lineno" name="l232" href="#l232">232 </a>    }
  13.289 +<a class="c2html-lineno" name="l233" href="#l233">233 </a>    
  13.290 +<a class="c2html-lineno" name="l234" href="#l234">234 </a>    <span class="c2html-comment">/* castling */</span>
  13.291 +<a class="c2html-lineno" name="l235" href="#l235">235 </a>    <span class="c2html-keyword">if</span> (piece == <span class="c2html-macroconst">KING</span> && move-&gt;fromfile == fileidx(<span class="c2html-string">'e'</span>)) {
  13.292 +<a class="c2html-lineno" name="l236" href="#l236">236 </a>        
  13.293 +<a class="c2html-lineno" name="l237" href="#l237">237 </a>        <span class="c2html-keyword">if</span> (move-&gt;tofile == fileidx(<span class="c2html-string">'g'</span>)) {
  13.294 +<a class="c2html-lineno" name="l238" href="#l238">238 </a>            gamestate-&gt;board[move-&gt;torow][fileidx(<span class="c2html-string">'h'</span>)] = <span class="c2html-macroconst">0</span>;
  13.295 +<a class="c2html-lineno" name="l239" href="#l239">239 </a>            gamestate-&gt;board[move-&gt;torow][fileidx(<span class="c2html-string">'f'</span>)] = color|<span class="c2html-macroconst">ROOK</span>;
  13.296 +<a class="c2html-lineno" name="l240" href="#l240">240 </a>        } <span class="c2html-keyword">else</span> <span class="c2html-keyword">if</span> (move-&gt;tofile == fileidx(<span class="c2html-string">'c'</span>)) {
  13.297 +<a class="c2html-lineno" name="l241" href="#l241">241 </a>            gamestate-&gt;board[move-&gt;torow][fileidx(<span class="c2html-string">'a'</span>)] = <span class="c2html-macroconst">0</span>;
  13.298 +<a class="c2html-lineno" name="l242" href="#l242">242 </a>            gamestate-&gt;board[move-&gt;torow][fileidx(<span class="c2html-string">'d'</span>)] = color|<span class="c2html-macroconst">ROOK</span>;
  13.299 +<a class="c2html-lineno" name="l243" href="#l243">243 </a>        }
  13.300 +<a class="c2html-lineno" name="l244" href="#l244">244 </a>    }
  13.301 +<a class="c2html-lineno" name="l245" href="#l245">245 </a>
  13.302 +<a class="c2html-lineno" name="l246" href="#l246">246 </a>    <span class="c2html-keyword">if</span> (!simulate) {
  13.303 +<a class="c2html-lineno" name="l247" href="#l247">247 </a>        <span class="c2html-keyword">if</span> (!move-&gt;string[<span class="c2html-macroconst">0</span>]) {
  13.304 +<a class="c2html-lineno" name="l248" href="#l248">248 </a>            format_move(gamestate, move);
  13.305 +<a class="c2html-lineno" name="l249" href="#l249">249 </a>        }
  13.306 +<a class="c2html-lineno" name="l250" href="#l250">250 </a>    }
  13.307 +<a class="c2html-lineno" name="l251" href="#l251">251 </a>    <span class="c2html-comment">/* add move, even in simulation (checkmate test needs it) */</span>
  13.308 +<a class="c2html-lineno" name="l252" href="#l252">252 </a>    addmove(gamestate, move);
  13.309 +<a class="c2html-lineno" name="l253" href="#l253">253 </a>}
  13.310 +<a class="c2html-lineno" name="l254" href="#l254">254 </a>
  13.311 +<a class="c2html-lineno" name="l255" href="#l255">255 </a><span class="c2html-keyword">void</span> apply_move(GameState *gamestate, Move *move) {
  13.312 +<a class="c2html-lineno" name="l256" href="#l256">256 </a>    apply_move_impl(gamestate, move, <span class="c2html-macroconst">0</span>);
  13.313 +<a class="c2html-lineno" name="l257" href="#l257">257 </a>}
  13.314 +<a class="c2html-lineno" name="l258" href="#l258">258 </a>
  13.315 +<a class="c2html-lineno" name="l259" href="#l259">259 </a><span class="c2html-keyword">static</span> <span class="c2html-keyword">int</span> validate_move_rules(GameState *gamestate, Move *move) {
  13.316 +<a class="c2html-lineno" name="l260" href="#l260">260 </a>    <span class="c2html-comment">/* validate indices (don't trust opponent) */</span>
  13.317 +<a class="c2html-lineno" name="l261" href="#l261">261 </a>    <span class="c2html-keyword">if</span> (!chkidx(move)) {
  13.318 +<a class="c2html-lineno" name="l262" href="#l262">262 </a>        <span class="c2html-keyword">return</span> <span class="c2html-macroconst">INVALID_POSITION</span>;
  13.319 +<a class="c2html-lineno" name="l263" href="#l263">263 </a>    }
  13.320 +<a class="c2html-lineno" name="l264" href="#l264">264 </a>    
  13.321 +<a class="c2html-lineno" name="l265" href="#l265">265 </a>    <span class="c2html-comment">/* must move */</span>
  13.322 +<a class="c2html-lineno" name="l266" href="#l266">266 </a>    <span class="c2html-keyword">if</span> (move-&gt;fromfile == move-&gt;tofile && move-&gt;fromrow == move-&gt;torow) {
  13.323 +<a class="c2html-lineno" name="l267" href="#l267">267 </a>        <span class="c2html-keyword">return</span> <span class="c2html-macroconst">INVALID_MOVE_SYNTAX</span>;
  13.324 +<a class="c2html-lineno" name="l268" href="#l268">268 </a>    }
  13.325 +<a class="c2html-lineno" name="l269" href="#l269">269 </a>    
  13.326 +<a class="c2html-lineno" name="l270" href="#l270">270 </a>    <span class="c2html-comment">/* does piece exist */</span>
  13.327 +<a class="c2html-lineno" name="l271" href="#l271">271 </a>    <span class="c2html-keyword">if</span> ((msrc(gamestate-&gt;board, move)&(<span class="c2html-macroconst">PIECE_MASK</span>|<span class="c2html-macroconst">COLOR_MASK</span>))
  13.328 +<a class="c2html-lineno" name="l272" href="#l272">272 </a>           != (move-&gt;piece&(<span class="c2html-macroconst">PIECE_MASK</span>|<span class="c2html-macroconst">COLOR_MASK</span>))) {
  13.329 +<a class="c2html-lineno" name="l273" href="#l273">273 </a>        <span class="c2html-keyword">return</span> <span class="c2html-macroconst">INVALID_POSITION</span>;
  13.330 +<a class="c2html-lineno" name="l274" href="#l274">274 </a>    }
  13.331 +<a class="c2html-lineno" name="l275" href="#l275">275 </a>    
  13.332 +<a class="c2html-lineno" name="l276" href="#l276">276 </a>    <span class="c2html-comment">/* can't capture own pieces */</span>
  13.333 +<a class="c2html-lineno" name="l277" href="#l277">277 </a>    <span class="c2html-keyword">if</span> ((mdst(gamestate-&gt;board, move) & <span class="c2html-macroconst">COLOR_MASK</span>)
  13.334 +<a class="c2html-lineno" name="l278" href="#l278">278 </a>            == (move-&gt;piece & <span class="c2html-macroconst">COLOR_MASK</span>)) {
  13.335 +<a class="c2html-lineno" name="l279" href="#l279">279 </a>        <span class="c2html-keyword">return</span> <span class="c2html-macroconst">RULES_VIOLATED</span>;
  13.336 +<a class="c2html-lineno" name="l280" href="#l280">280 </a>    }
  13.337 +<a class="c2html-lineno" name="l281" href="#l281">281 </a>    
  13.338 +<a class="c2html-lineno" name="l282" href="#l282">282 </a>    <span class="c2html-comment">/* must capture, if and only if destination is occupied */</span>
  13.339 +<a class="c2html-lineno" name="l283" href="#l283">283 </a>    <span class="c2html-keyword">if</span> ((mdst(gamestate-&gt;board, move) == <span class="c2html-macroconst">0</span> && move-&gt;capture) ||
  13.340 +<a class="c2html-lineno" name="l284" href="#l284">284 </a>            (mdst(gamestate-&gt;board, move) != <span class="c2html-macroconst">0</span> && !move-&gt;capture)) {
  13.341 +<a class="c2html-lineno" name="l285" href="#l285">285 </a>        <span class="c2html-keyword">return</span> <span class="c2html-macroconst">INVALID_MOVE_SYNTAX</span>;
  13.342 +<a class="c2html-lineno" name="l286" href="#l286">286 </a>    }
  13.343 +<a class="c2html-lineno" name="l287" href="#l287">287 </a>    
  13.344 +<a class="c2html-lineno" name="l288" href="#l288">288 </a>    <span class="c2html-comment">/* validate individual rules */</span>
  13.345 +<a class="c2html-lineno" name="l289" href="#l289">289 </a>    _Bool chkrules;
  13.346 +<a class="c2html-lineno" name="l290" href="#l290">290 </a>    <span class="c2html-keyword">switch</span> (move-&gt;piece & <span class="c2html-macroconst">PIECE_MASK</span>) {
  13.347 +<a class="c2html-lineno" name="l291" href="#l291">291 </a>    <span class="c2html-keyword">case</span> <span class="c2html-macroconst">PAWN</span>:
  13.348 +<a class="c2html-lineno" name="l292" href="#l292">292 </a>        chkrules = pawn_chkrules(gamestate, move) &&
  13.349 +<a class="c2html-lineno" name="l293" href="#l293">293 </a>            !pawn_isblocked(gamestate, move);
  13.350 +<a class="c2html-lineno" name="l294" href="#l294">294 </a>        <span class="c2html-keyword">break</span>;
  13.351 +<a class="c2html-lineno" name="l295" href="#l295">295 </a>    <span class="c2html-keyword">case</span> <span class="c2html-macroconst">ROOK</span>:
  13.352 +<a class="c2html-lineno" name="l296" href="#l296">296 </a>        chkrules = rook_chkrules(move) &&
  13.353 +<a class="c2html-lineno" name="l297" href="#l297">297 </a>            !rook_isblocked(gamestate, move);
  13.354 +<a class="c2html-lineno" name="l298" href="#l298">298 </a>        <span class="c2html-keyword">break</span>;
  13.355 +<a class="c2html-lineno" name="l299" href="#l299">299 </a>    <span class="c2html-keyword">case</span> <span class="c2html-macroconst">KNIGHT</span>:
  13.356 +<a class="c2html-lineno" name="l300" href="#l300">300 </a>        chkrules = knight_chkrules(move); <span class="c2html-comment">/* knight is never blocked */</span>
  13.357 +<a class="c2html-lineno" name="l301" href="#l301">301 </a>        <span class="c2html-keyword">break</span>;
  13.358 +<a class="c2html-lineno" name="l302" href="#l302">302 </a>    <span class="c2html-keyword">case</span> <span class="c2html-macroconst">BISHOP</span>:
  13.359 +<a class="c2html-lineno" name="l303" href="#l303">303 </a>        chkrules = bishop_chkrules(move) &&
  13.360 +<a class="c2html-lineno" name="l304" href="#l304">304 </a>            !bishop_isblocked(gamestate, move);
  13.361 +<a class="c2html-lineno" name="l305" href="#l305">305 </a>        <span class="c2html-keyword">break</span>;
  13.362 +<a class="c2html-lineno" name="l306" href="#l306">306 </a>    <span class="c2html-keyword">case</span> <span class="c2html-macroconst">QUEEN</span>:
  13.363 +<a class="c2html-lineno" name="l307" href="#l307">307 </a>        chkrules = queen_chkrules(move) &&
  13.364 +<a class="c2html-lineno" name="l308" href="#l308">308 </a>            !queen_isblocked(gamestate, move);
  13.365 +<a class="c2html-lineno" name="l309" href="#l309">309 </a>        <span class="c2html-keyword">break</span>;
  13.366 +<a class="c2html-lineno" name="l310" href="#l310">310 </a>    <span class="c2html-keyword">case</span> <span class="c2html-macroconst">KING</span>:
  13.367 +<a class="c2html-lineno" name="l311" href="#l311">311 </a>        chkrules = king_chkrules(gamestate, move) &&
  13.368 +<a class="c2html-lineno" name="l312" href="#l312">312 </a>            !king_isblocked(gamestate, move);
  13.369 +<a class="c2html-lineno" name="l313" href="#l313">313 </a>        <span class="c2html-keyword">break</span>;
  13.370 +<a class="c2html-lineno" name="l314" href="#l314">314 </a>    <span class="c2html-keyword">default</span>:
  13.371 +<a class="c2html-lineno" name="l315" href="#l315">315 </a>        <span class="c2html-keyword">return</span> <span class="c2html-macroconst">INVALID_MOVE_SYNTAX</span>;
  13.372 +<a class="c2html-lineno" name="l316" href="#l316">316 </a>    }
  13.373 +<a class="c2html-lineno" name="l317" href="#l317">317 </a>    
  13.374 +<a class="c2html-lineno" name="l318" href="#l318">318 </a>    <span class="c2html-keyword">return</span> chkrules ? <span class="c2html-macroconst">VALID_MOVE_SEMANTICS</span> : <span class="c2html-macroconst">RULES_VIOLATED</span>;
  13.375 +<a class="c2html-lineno" name="l319" href="#l319">319 </a>}
  13.376 +<a class="c2html-lineno" name="l320" href="#l320">320 </a>
  13.377 +<a class="c2html-lineno" name="l321" href="#l321">321 </a><span class="c2html-keyword">int</span> validate_move(GameState *gamestate, Move *move) {
  13.378 +<a class="c2html-lineno" name="l322" href="#l322">322 </a>    
  13.379 +<a class="c2html-lineno" name="l323" href="#l323">323 </a>    <span class="c2html-keyword">int</span> result = validate_move_rules(gamestate, move);
  13.380 +<a class="c2html-lineno" name="l324" href="#l324">324 </a>    
  13.381 +<a class="c2html-lineno" name="l325" href="#l325">325 </a>    <span class="c2html-comment">/* cancel processing to save resources */</span>
  13.382 +<a class="c2html-lineno" name="l326" href="#l326">326 </a>    <span class="c2html-keyword">if</span> (result != <span class="c2html-macroconst">VALID_MOVE_SEMANTICS</span>) {
  13.383 +<a class="c2html-lineno" name="l327" href="#l327">327 </a>        <span class="c2html-keyword">return</span> result;
  13.384 +<a class="c2html-lineno" name="l328" href="#l328">328 </a>    }
  13.385 +<a class="c2html-lineno" name="l329" href="#l329">329 </a>    
  13.386 +<a class="c2html-lineno" name="l330" href="#l330">330 </a>    <span class="c2html-comment">/* find kings for check validation */</span>
  13.387 +<a class="c2html-lineno" name="l331" href="#l331">331 </a>    <span class="c2html-type">uint8_t</span> piececolor = (move-&gt;piece & <span class="c2html-macroconst">COLOR_MASK</span>);
  13.388 +<a class="c2html-lineno" name="l332" href="#l332">332 </a>    
  13.389 +<a class="c2html-lineno" name="l333" href="#l333">333 </a>    <span class="c2html-type">uint8_t</span> mykingfile = <span class="c2html-macroconst">0</span>, mykingrow = <span class="c2html-macroconst">0</span>, opkingfile = <span class="c2html-macroconst">0</span>, opkingrow = <span class="c2html-macroconst">0</span>;
  13.390 +<a class="c2html-lineno" name="l334" href="#l334">334 </a>    <span class="c2html-keyword">for</span> (<span class="c2html-type">uint8_t</span> row = <span class="c2html-macroconst">0</span> ; row &lt; <span class="c2html-macroconst">8</span> ; row++) {
  13.391 +<a class="c2html-lineno" name="l335" href="#l335">335 </a>        <span class="c2html-keyword">for</span> (<span class="c2html-type">uint8_t</span> file = <span class="c2html-macroconst">0</span> ; file &lt; <span class="c2html-macroconst">8</span> ; file++) {
  13.392 +<a class="c2html-lineno" name="l336" href="#l336">336 </a>            <span class="c2html-keyword">if</span> (gamestate-&gt;board[row][file] ==
  13.393 +<a class="c2html-lineno" name="l337" href="#l337">337 </a>                    (piececolor == <span class="c2html-macroconst">WHITE</span>?<span class="c2html-macroconst">WKING</span>:<span class="c2html-macroconst">BKING</span>)) {
  13.394 +<a class="c2html-lineno" name="l338" href="#l338">338 </a>                mykingfile = file;
  13.395 +<a class="c2html-lineno" name="l339" href="#l339">339 </a>                mykingrow = row;
  13.396 +<a class="c2html-lineno" name="l340" href="#l340">340 </a>            } <span class="c2html-keyword">else</span> <span class="c2html-keyword">if</span> (gamestate-&gt;board[row][file] ==
  13.397 +<a class="c2html-lineno" name="l341" href="#l341">341 </a>                    (piececolor == <span class="c2html-macroconst">WHITE</span>?<span class="c2html-macroconst">BKING</span>:<span class="c2html-macroconst">WKING</span>)) {
  13.398 +<a class="c2html-lineno" name="l342" href="#l342">342 </a>                opkingfile = file;
  13.399 +<a class="c2html-lineno" name="l343" href="#l343">343 </a>                opkingrow = row;
  13.400 +<a class="c2html-lineno" name="l344" href="#l344">344 </a>            }
  13.401 +<a class="c2html-lineno" name="l345" href="#l345">345 </a>        }
  13.402 +<a class="c2html-lineno" name="l346" href="#l346">346 </a>    }
  13.403 +<a class="c2html-lineno" name="l347" href="#l347">347 </a>    
  13.404 +<a class="c2html-lineno" name="l348" href="#l348">348 </a>    <span class="c2html-comment">/* simulate move for check validation */</span>
  13.405 +<a class="c2html-lineno" name="l349" href="#l349">349 </a>    GameState simulation = gamestate_copy_sim(gamestate);
  13.406 +<a class="c2html-lineno" name="l350" href="#l350">350 </a>    Move simmove = *move;
  13.407 +<a class="c2html-lineno" name="l351" href="#l351">351 </a>    apply_move_impl(&simulation, &simmove, <span class="c2html-macroconst">1</span>);
  13.408 +<a class="c2html-lineno" name="l352" href="#l352">352 </a>    
  13.409 +<a class="c2html-lineno" name="l353" href="#l353">353 </a>    <span class="c2html-comment">/* don't move into or stay in check position */</span>
  13.410 +<a class="c2html-lineno" name="l354" href="#l354">354 </a>    <span class="c2html-keyword">if</span> (is_covered(&simulation, mykingrow, mykingfile,
  13.411 +<a class="c2html-lineno" name="l355" href="#l355">355 </a>        opponent_color(piececolor))) {
  13.412 +<a class="c2html-lineno" name="l356" href="#l356">356 </a>        
  13.413 +<a class="c2html-lineno" name="l357" href="#l357">357 </a>        gamestate_cleanup(&simulation);
  13.414 +<a class="c2html-lineno" name="l358" href="#l358">358 </a>        <span class="c2html-keyword">if</span> ((move-&gt;piece & <span class="c2html-macroconst">PIECE_MASK</span>) == <span class="c2html-macroconst">KING</span>) {
  13.415 +<a class="c2html-lineno" name="l359" href="#l359">359 </a>            <span class="c2html-keyword">return</span> <span class="c2html-macroconst">KING_MOVES_INTO_CHECK</span>;
  13.416 +<a class="c2html-lineno" name="l360" href="#l360">360 </a>        } <span class="c2html-keyword">else</span> {
  13.417 +<a class="c2html-lineno" name="l361" href="#l361">361 </a>            <span class="c2html-comment">/* last move is always not null in this case */</span>
  13.418 +<a class="c2html-lineno" name="l362" href="#l362">362 </a>            <span class="c2html-keyword">return</span> gamestate-&gt;lastmove-&gt;move.check ?
  13.419 +<a class="c2html-lineno" name="l363" href="#l363">363 </a>                <span class="c2html-macroconst">KING_IN_CHECK</span> : <span class="c2html-macroconst">PIECE_PINNED</span>;
  13.420 +<a class="c2html-lineno" name="l364" href="#l364">364 </a>        }
  13.421 +<a class="c2html-lineno" name="l365" href="#l365">365 </a>    }
  13.422 +<a class="c2html-lineno" name="l366" href="#l366">366 </a>    
  13.423 +<a class="c2html-lineno" name="l367" href="#l367">367 </a>    <span class="c2html-comment">/* correct check and checkmate flags (move is still valid) */</span>
  13.424 +<a class="c2html-lineno" name="l368" href="#l368">368 </a>    Move threats[<span class="c2html-macroconst">16</span>];
  13.425 +<a class="c2html-lineno" name="l369" href="#l369">369 </a>    <span class="c2html-type">uint8_t</span> threatcount;
  13.426 +<a class="c2html-lineno" name="l370" href="#l370">370 </a>    move-&gt;check = get_threats(&simulation, opkingrow, opkingfile,
  13.427 +<a class="c2html-lineno" name="l371" href="#l371">371 </a>        piececolor, threats, &threatcount);
  13.428 +<a class="c2html-lineno" name="l372" href="#l372">372 </a>    
  13.429 +<a class="c2html-lineno" name="l373" href="#l373">373 </a>    <span class="c2html-keyword">if</span> (move-&gt;check) {
  13.430 +<a class="c2html-lineno" name="l374" href="#l374">374 </a>        <span class="c2html-comment">/* determine possible escape fields */</span>
  13.431 +<a class="c2html-lineno" name="l375" href="#l375">375 </a>        _Bool canescape = <span class="c2html-macroconst">0</span>;
  13.432 +<a class="c2html-lineno" name="l376" href="#l376">376 </a>        <span class="c2html-keyword">for</span> (<span class="c2html-keyword">int</span> dr = -<span class="c2html-macroconst">1</span> ; dr &lt;= <span class="c2html-macroconst">1</span> && !canescape ; dr++) {
  13.433 +<a class="c2html-lineno" name="l377" href="#l377">377 </a>            <span class="c2html-keyword">for</span> (<span class="c2html-keyword">int</span> df = -<span class="c2html-macroconst">1</span> ; df &lt;= <span class="c2html-macroconst">1</span> && !canescape ; df++) {
  13.434 +<a class="c2html-lineno" name="l378" href="#l378">378 </a>                <span class="c2html-keyword">if</span> (!(dr == <span class="c2html-macroconst">0</span> && df == <span class="c2html-macroconst">0</span>)  &&
  13.435 +<a class="c2html-lineno" name="l379" href="#l379">379 </a>                        isidx(opkingrow + dr) && isidx(opkingfile + df)) {
  13.436 +<a class="c2html-lineno" name="l380" href="#l380">380 </a>                    
  13.437 +<a class="c2html-lineno" name="l381" href="#l381">381 </a>                    <span class="c2html-comment">/* escape field neither blocked nor covered */</span>
  13.438 +<a class="c2html-lineno" name="l382" href="#l382">382 </a>                    <span class="c2html-keyword">if</span> ((simulation.board[opkingrow + dr][opkingfile + df]
  13.439 +<a class="c2html-lineno" name="l383" href="#l383">383 </a>                            & <span class="c2html-macroconst">COLOR_MASK</span>) != opponent_color(piececolor)) {
  13.440 +<a class="c2html-lineno" name="l384" href="#l384">384 </a>                        canescape |= !is_covered(&simulation,
  13.441 +<a class="c2html-lineno" name="l385" href="#l385">385 </a>                            opkingrow + dr, opkingfile + df, piececolor);
  13.442 +<a class="c2html-lineno" name="l386" href="#l386">386 </a>                    }
  13.443 +<a class="c2html-lineno" name="l387" href="#l387">387 </a>                }
  13.444 +<a class="c2html-lineno" name="l388" href="#l388">388 </a>            }
  13.445 +<a class="c2html-lineno" name="l389" href="#l389">389 </a>        }
  13.446 +<a class="c2html-lineno" name="l390" href="#l390">390 </a>        <span class="c2html-comment">/* can't escape, can he capture? */</span>
  13.447 +<a class="c2html-lineno" name="l391" href="#l391">391 </a>        <span class="c2html-keyword">if</span> (!canescape && threatcount == <span class="c2html-macroconst">1</span>) {
  13.448 +<a class="c2html-lineno" name="l392" href="#l392">392 </a>            canescape = is_attacked(&simulation, threats[<span class="c2html-macroconst">0</span>].fromrow,
  13.449 +<a class="c2html-lineno" name="l393" href="#l393">393 </a>                threats[<span class="c2html-macroconst">0</span>].fromfile, opponent_color(piececolor));
  13.450 +<a class="c2html-lineno" name="l394" href="#l394">394 </a>        }
  13.451 +<a class="c2html-lineno" name="l395" href="#l395">395 </a>        
  13.452 +<a class="c2html-lineno" name="l396" href="#l396">396 </a>        <span class="c2html-comment">/* can't capture, can he block? */</span>
  13.453 +<a class="c2html-lineno" name="l397" href="#l397">397 </a>        <span class="c2html-keyword">if</span> (!canescape && threatcount == <span class="c2html-macroconst">1</span>) {
  13.454 +<a class="c2html-lineno" name="l398" href="#l398">398 </a>            Move *threat = &(threats[<span class="c2html-macroconst">0</span>]);
  13.455 +<a class="c2html-lineno" name="l399" href="#l399">399 </a>            <span class="c2html-type">uint8_t</span> threatpiece = threat-&gt;piece & <span class="c2html-macroconst">PIECE_MASK</span>;
  13.456 +<a class="c2html-lineno" name="l400" href="#l400">400 </a>            
  13.457 +<a class="c2html-lineno" name="l401" href="#l401">401 </a>            <span class="c2html-comment">/* knight, pawns and the king cannot be blocked */</span>
  13.458 +<a class="c2html-lineno" name="l402" href="#l402">402 </a>            <span class="c2html-keyword">if</span> (threatpiece == <span class="c2html-macroconst">BISHOP</span> || threatpiece == <span class="c2html-macroconst">ROOK</span>
  13.459 +<a class="c2html-lineno" name="l403" href="#l403">403 </a>                || threatpiece == <span class="c2html-macroconst">QUEEN</span>) {
  13.460 +<a class="c2html-lineno" name="l404" href="#l404">404 </a>                <span class="c2html-keyword">if</span> (threat-&gt;fromrow == threat-&gt;torow) {
  13.461 +<a class="c2html-lineno" name="l405" href="#l405">405 </a>                    <span class="c2html-comment">/* rook aspect (on row) */</span>
  13.462 +<a class="c2html-lineno" name="l406" href="#l406">406 </a>                    <span class="c2html-keyword">int</span> d = threat-&gt;tofile &gt; threat-&gt;fromfile ? <span class="c2html-macroconst">1</span> : -<span class="c2html-macroconst">1</span>;
  13.463 +<a class="c2html-lineno" name="l407" href="#l407">407 </a>                    <span class="c2html-type">uint8_t</span> file = threat-&gt;fromfile;
  13.464 +<a class="c2html-lineno" name="l408" href="#l408">408 </a>                    <span class="c2html-keyword">while</span> (!canescape && file != threat-&gt;tofile - d) {
  13.465 +<a class="c2html-lineno" name="l409" href="#l409">409 </a>                        file += d;
  13.466 +<a class="c2html-lineno" name="l410" href="#l410">410 </a>                        canescape |= is_protected(&simulation,
  13.467 +<a class="c2html-lineno" name="l411" href="#l411">411 </a>                            threat-&gt;torow, file, opponent_color(piececolor));
  13.468 +<a class="c2html-lineno" name="l412" href="#l412">412 </a>                    }
  13.469 +<a class="c2html-lineno" name="l413" href="#l413">413 </a>                } <span class="c2html-keyword">else</span> <span class="c2html-keyword">if</span> (threat-&gt;fromfile == threat-&gt;tofile) {
  13.470 +<a class="c2html-lineno" name="l414" href="#l414">414 </a>                    <span class="c2html-comment">/* rook aspect (on file) */</span>
  13.471 +<a class="c2html-lineno" name="l415" href="#l415">415 </a>                    <span class="c2html-keyword">int</span> d = threat-&gt;torow &gt; threat-&gt;fromrow ? <span class="c2html-macroconst">1</span> : -<span class="c2html-macroconst">1</span>;
  13.472 +<a class="c2html-lineno" name="l416" href="#l416">416 </a>                    <span class="c2html-type">uint8_t</span> row = threat-&gt;fromrow;
  13.473 +<a class="c2html-lineno" name="l417" href="#l417">417 </a>                    <span class="c2html-keyword">while</span> (!canescape && row != threat-&gt;torow - d) {
  13.474 +<a class="c2html-lineno" name="l418" href="#l418">418 </a>                        row += d;
  13.475 +<a class="c2html-lineno" name="l419" href="#l419">419 </a>                        canescape |= is_protected(&simulation,
  13.476 +<a class="c2html-lineno" name="l420" href="#l420">420 </a>                            row, threat-&gt;tofile, opponent_color(piececolor));
  13.477 +<a class="c2html-lineno" name="l421" href="#l421">421 </a>                    }
  13.478 +<a class="c2html-lineno" name="l422" href="#l422">422 </a>                } <span class="c2html-keyword">else</span> {
  13.479 +<a class="c2html-lineno" name="l423" href="#l423">423 </a>                    <span class="c2html-comment">/* bishop aspect */</span>
  13.480 +<a class="c2html-lineno" name="l424" href="#l424">424 </a>                    <span class="c2html-keyword">int</span> dr = threat-&gt;torow &gt; threat-&gt;fromrow ? <span class="c2html-macroconst">1</span> : -<span class="c2html-macroconst">1</span>;
  13.481 +<a class="c2html-lineno" name="l425" href="#l425">425 </a>                    <span class="c2html-keyword">int</span> df = threat-&gt;tofile &gt; threat-&gt;fromfile ? <span class="c2html-macroconst">1</span> : -<span class="c2html-macroconst">1</span>;
  13.482 +<a class="c2html-lineno" name="l426" href="#l426">426 </a>
  13.483 +<a class="c2html-lineno" name="l427" href="#l427">427 </a>                    <span class="c2html-type">uint8_t</span> row = threat-&gt;fromrow;
  13.484 +<a class="c2html-lineno" name="l428" href="#l428">428 </a>                    <span class="c2html-type">uint8_t</span> file = threat-&gt;fromfile;
  13.485 +<a class="c2html-lineno" name="l429" href="#l429">429 </a>                    <span class="c2html-keyword">while</span> (!canescape && file != threat-&gt;tofile - df
  13.486 +<a class="c2html-lineno" name="l430" href="#l430">430 </a>                        && row != threat-&gt;torow - dr) {
  13.487 +<a class="c2html-lineno" name="l431" href="#l431">431 </a>                        row += dr;
  13.488 +<a class="c2html-lineno" name="l432" href="#l432">432 </a>                        file += df;
  13.489 +<a class="c2html-lineno" name="l433" href="#l433">433 </a>                        canescape |= is_protected(&simulation, row, file,
  13.490 +<a class="c2html-lineno" name="l434" href="#l434">434 </a>                            opponent_color(piececolor));
  13.491 +<a class="c2html-lineno" name="l435" href="#l435">435 </a>                    }
  13.492 +<a class="c2html-lineno" name="l436" href="#l436">436 </a>                }
  13.493 +<a class="c2html-lineno" name="l437" href="#l437">437 </a>            }
  13.494 +<a class="c2html-lineno" name="l438" href="#l438">438 </a>        }
  13.495 +<a class="c2html-lineno" name="l439" href="#l439">439 </a>            
  13.496 +<a class="c2html-lineno" name="l440" href="#l440">440 </a>        <span class="c2html-keyword">if</span> (!canescape) {
  13.497 +<a class="c2html-lineno" name="l441" href="#l441">441 </a>            gamestate-&gt;checkmate = <span class="c2html-macroconst">1</span>;
  13.498 +<a class="c2html-lineno" name="l442" href="#l442">442 </a>        }
  13.499 +<a class="c2html-lineno" name="l443" href="#l443">443 </a>    }
  13.500 +<a class="c2html-lineno" name="l444" href="#l444">444 </a>    
  13.501 +<a class="c2html-lineno" name="l445" href="#l445">445 </a>    gamestate_cleanup(&simulation);
  13.502 +<a class="c2html-lineno" name="l446" href="#l446">446 </a>    
  13.503 +<a class="c2html-lineno" name="l447" href="#l447">447 </a>    <span class="c2html-keyword">return</span> <span class="c2html-macroconst">VALID_MOVE_SEMANTICS</span>;
  13.504 +<a class="c2html-lineno" name="l448" href="#l448">448 </a>}
  13.505 +<a class="c2html-lineno" name="l449" href="#l449">449 </a>
  13.506 +<a class="c2html-lineno" name="l450" href="#l450">450 </a>_Bool get_threats(GameState *gamestate, <span class="c2html-type">uint8_t</span> row, <span class="c2html-type">uint8_t</span> file,
  13.507 +<a class="c2html-lineno" name="l451" href="#l451">451 </a>        <span class="c2html-type">uint8_t</span> color, Move *threats, <span class="c2html-type">uint8_t</span> *threatcount) {
  13.508 +<a class="c2html-lineno" name="l452" href="#l452">452 </a>    Move candidates[<span class="c2html-macroconst">32</span>];
  13.509 +<a class="c2html-lineno" name="l453" href="#l453">453 </a>    <span class="c2html-keyword">int</span> candidatecount = <span class="c2html-macroconst">0</span>;
  13.510 +<a class="c2html-lineno" name="l454" href="#l454">454 </a>    <span class="c2html-keyword">for</span> (<span class="c2html-type">uint8_t</span> r = <span class="c2html-macroconst">0</span> ; r &lt; <span class="c2html-macroconst">8</span> ; r++) {
  13.511 +<a class="c2html-lineno" name="l455" href="#l455">455 </a>        <span class="c2html-keyword">for</span> (<span class="c2html-type">uint8_t</span> f = <span class="c2html-macroconst">0</span> ; f &lt; <span class="c2html-macroconst">8</span> ; f++) {
  13.512 +<a class="c2html-lineno" name="l456" href="#l456">456 </a>            <span class="c2html-keyword">if</span> ((gamestate-&gt;board[r][f] & <span class="c2html-macroconst">COLOR_MASK</span>) == color) {
  13.513 +<a class="c2html-lineno" name="l457" href="#l457">457 </a>                <span class="c2html-comment">// non-capturing move</span>
  13.514 +<a class="c2html-lineno" name="l458" href="#l458">458 </a>                memset(&(candidates[candidatecount]), <span class="c2html-macroconst">0</span>, <span class="c2html-keyword">sizeof</span>(Move));
  13.515 +<a class="c2html-lineno" name="l459" href="#l459">459 </a>                candidates[candidatecount].piece = gamestate-&gt;board[r][f];
  13.516 +<a class="c2html-lineno" name="l460" href="#l460">460 </a>                candidates[candidatecount].fromrow = r;
  13.517 +<a class="c2html-lineno" name="l461" href="#l461">461 </a>                candidates[candidatecount].fromfile = f;
  13.518 +<a class="c2html-lineno" name="l462" href="#l462">462 </a>                candidates[candidatecount].torow = row;
  13.519 +<a class="c2html-lineno" name="l463" href="#l463">463 </a>                candidates[candidatecount].tofile = file;
  13.520 +<a class="c2html-lineno" name="l464" href="#l464">464 </a>                candidatecount++;
  13.521 +<a class="c2html-lineno" name="l465" href="#l465">465 </a>
  13.522 +<a class="c2html-lineno" name="l466" href="#l466">466 </a>                <span class="c2html-comment">// capturing move</span>
  13.523 +<a class="c2html-lineno" name="l467" href="#l467">467 </a>                memcpy(&(candidates[candidatecount]),
  13.524 +<a class="c2html-lineno" name="l468" href="#l468">468 </a>                    &(candidates[candidatecount-<span class="c2html-macroconst">1</span>]), <span class="c2html-keyword">sizeof</span>(Move));
  13.525 +<a class="c2html-lineno" name="l469" href="#l469">469 </a>                candidates[candidatecount].capture = <span class="c2html-macroconst">1</span>;
  13.526 +<a class="c2html-lineno" name="l470" href="#l470">470 </a>                candidatecount++;
  13.527 +<a class="c2html-lineno" name="l471" href="#l471">471 </a>            }
  13.528 +<a class="c2html-lineno" name="l472" href="#l472">472 </a>        }
  13.529 +<a class="c2html-lineno" name="l473" href="#l473">473 </a>    }
  13.530 +<a class="c2html-lineno" name="l474" href="#l474">474 </a>
  13.531 +<a class="c2html-lineno" name="l475" href="#l475">475 </a>    <span class="c2html-keyword">if</span> (threatcount) {
  13.532 +<a class="c2html-lineno" name="l476" href="#l476">476 </a>        *threatcount = <span class="c2html-macroconst">0</span>;
  13.533 +<a class="c2html-lineno" name="l477" href="#l477">477 </a>    }
  13.534 +<a class="c2html-lineno" name="l478" href="#l478">478 </a>    
  13.535 +<a class="c2html-lineno" name="l479" href="#l479">479 </a>    
  13.536 +<a class="c2html-lineno" name="l480" href="#l480">480 </a>    _Bool result = <span class="c2html-macroconst">0</span>;
  13.537 +<a class="c2html-lineno" name="l481" href="#l481">481 </a>    
  13.538 +<a class="c2html-lineno" name="l482" href="#l482">482 </a>    <span class="c2html-keyword">for</span> (<span class="c2html-keyword">int</span> i = <span class="c2html-macroconst">0</span> ; i &lt; candidatecount ; i++) {
  13.539 +<a class="c2html-lineno" name="l483" href="#l483">483 </a>        <span class="c2html-keyword">if</span> (validate_move_rules(gamestate, &(candidates[i]))
  13.540 +<a class="c2html-lineno" name="l484" href="#l484">484 </a>                == <span class="c2html-macroconst">VALID_MOVE_SEMANTICS</span>) {
  13.541 +<a class="c2html-lineno" name="l485" href="#l485">485 </a>            result = <span class="c2html-macroconst">1</span>;
  13.542 +<a class="c2html-lineno" name="l486" href="#l486">486 </a>            <span class="c2html-keyword">if</span> (threats && threatcount) {
  13.543 +<a class="c2html-lineno" name="l487" href="#l487">487 </a>                threats[(*threatcount)++] = candidates[i];
  13.544 +<a class="c2html-lineno" name="l488" href="#l488">488 </a>            }
  13.545 +<a class="c2html-lineno" name="l489" href="#l489">489 </a>        }
  13.546 +<a class="c2html-lineno" name="l490" href="#l490">490 </a>    }
  13.547 +<a class="c2html-lineno" name="l491" href="#l491">491 </a>    
  13.548 +<a class="c2html-lineno" name="l492" href="#l492">492 </a>    <span class="c2html-keyword">return</span> result;
  13.549 +<a class="c2html-lineno" name="l493" href="#l493">493 </a>}
  13.550 +<a class="c2html-lineno" name="l494" href="#l494">494 </a>
  13.551 +<a class="c2html-lineno" name="l495" href="#l495">495 </a>_Bool is_pinned(GameState *gamestate, Move *move) {
  13.552 +<a class="c2html-lineno" name="l496" href="#l496">496 </a>    <span class="c2html-type">uint8_t</span> color = move-&gt;piece & <span class="c2html-macroconst">COLOR_MASK</span>;
  13.553 +<a class="c2html-lineno" name="l497" href="#l497">497 </a>
  13.554 +<a class="c2html-lineno" name="l498" href="#l498">498 </a>    <span class="c2html-type">uint8_t</span> kingfile = <span class="c2html-macroconst">0</span>, kingrow = <span class="c2html-macroconst">0</span>;
  13.555 +<a class="c2html-lineno" name="l499" href="#l499">499 </a>    <span class="c2html-keyword">for</span> (<span class="c2html-type">uint8_t</span> row = <span class="c2html-macroconst">0</span> ; row &lt; <span class="c2html-macroconst">8</span> ; row++) {
  13.556 +<a class="c2html-lineno" name="l500" href="#l500">500 </a>        <span class="c2html-keyword">for</span> (<span class="c2html-type">uint8_t</span> file = <span class="c2html-macroconst">0</span> ; file &lt; <span class="c2html-macroconst">8</span> ; file++) {
  13.557 +<a class="c2html-lineno" name="l501" href="#l501">501 </a>            <span class="c2html-keyword">if</span> (gamestate-&gt;board[row][file] == (color|<span class="c2html-macroconst">KING</span>)) {
  13.558 +<a class="c2html-lineno" name="l502" href="#l502">502 </a>                kingfile = file;
  13.559 +<a class="c2html-lineno" name="l503" href="#l503">503 </a>                kingrow = row;
  13.560 +<a class="c2html-lineno" name="l504" href="#l504">504 </a>            }
  13.561 +<a class="c2html-lineno" name="l505" href="#l505">505 </a>        }
  13.562 +<a class="c2html-lineno" name="l506" href="#l506">506 </a>    }
  13.563 +<a class="c2html-lineno" name="l507" href="#l507">507 </a>
  13.564 +<a class="c2html-lineno" name="l508" href="#l508">508 </a>    GameState simulation = gamestate_copy_sim(gamestate);
  13.565 +<a class="c2html-lineno" name="l509" href="#l509">509 </a>    Move simmove = *move;
  13.566 +<a class="c2html-lineno" name="l510" href="#l510">510 </a>    apply_move(&simulation, &simmove);
  13.567 +<a class="c2html-lineno" name="l511" href="#l511">511 </a>    _Bool covered = is_covered(&simulation,
  13.568 +<a class="c2html-lineno" name="l512" href="#l512">512 </a>        kingrow, kingfile, opponent_color(color));
  13.569 +<a class="c2html-lineno" name="l513" href="#l513">513 </a>    gamestate_cleanup(&simulation);
  13.570 +<a class="c2html-lineno" name="l514" href="#l514">514 </a>    
  13.571 +<a class="c2html-lineno" name="l515" href="#l515">515 </a>    <span class="c2html-keyword">return</span> covered;
  13.572 +<a class="c2html-lineno" name="l516" href="#l516">516 </a>}
  13.573 +<a class="c2html-lineno" name="l517" href="#l517">517 </a>
  13.574 +<a class="c2html-lineno" name="l518" href="#l518">518 </a>_Bool get_real_threats(GameState *gamestate, <span class="c2html-type">uint8_t</span> row, <span class="c2html-type">uint8_t</span> file,
  13.575 +<a class="c2html-lineno" name="l519" href="#l519">519 </a>        <span class="c2html-type">uint8_t</span> color, Move *threats, <span class="c2html-type">uint8_t</span> *threatcount) {
  13.576 +<a class="c2html-lineno" name="l520" href="#l520">520 </a>    
  13.577 +<a class="c2html-lineno" name="l521" href="#l521">521 </a>    <span class="c2html-keyword">if</span> (threatcount) {
  13.578 +<a class="c2html-lineno" name="l522" href="#l522">522 </a>        *threatcount = <span class="c2html-macroconst">0</span>;
  13.579 +<a class="c2html-lineno" name="l523" href="#l523">523 </a>    }
  13.580 +<a class="c2html-lineno" name="l524" href="#l524">524 </a>
  13.581 +<a class="c2html-lineno" name="l525" href="#l525">525 </a>    Move candidates[<span class="c2html-macroconst">16</span>];
  13.582 +<a class="c2html-lineno" name="l526" href="#l526">526 </a>    <span class="c2html-type">uint8_t</span> candidatecount;
  13.583 +<a class="c2html-lineno" name="l527" href="#l527">527 </a>    <span class="c2html-keyword">if</span> (get_threats(gamestate, row, file, color, candidates, &candidatecount)) {
  13.584 +<a class="c2html-lineno" name="l528" href="#l528">528 </a>        
  13.585 +<a class="c2html-lineno" name="l529" href="#l529">529 </a>        _Bool result = <span class="c2html-macroconst">0</span>;
  13.586 +<a class="c2html-lineno" name="l530" href="#l530">530 </a>        <span class="c2html-type">uint8_t</span> kingfile = <span class="c2html-macroconst">0</span>, kingrow = <span class="c2html-macroconst">0</span>;
  13.587 +<a class="c2html-lineno" name="l531" href="#l531">531 </a>        <span class="c2html-keyword">for</span> (<span class="c2html-type">uint8_t</span> row = <span class="c2html-macroconst">0</span> ; row &lt; <span class="c2html-macroconst">8</span> ; row++) {
  13.588 +<a class="c2html-lineno" name="l532" href="#l532">532 </a>            <span class="c2html-keyword">for</span> (<span class="c2html-type">uint8_t</span> file = <span class="c2html-macroconst">0</span> ; file &lt; <span class="c2html-macroconst">8</span> ; file++) {
  13.589 +<a class="c2html-lineno" name="l533" href="#l533">533 </a>                <span class="c2html-keyword">if</span> (gamestate-&gt;board[row][file] == (color|<span class="c2html-macroconst">KING</span>)) {
  13.590 +<a class="c2html-lineno" name="l534" href="#l534">534 </a>                    kingfile = file;
  13.591 +<a class="c2html-lineno" name="l535" href="#l535">535 </a>                    kingrow = row;
  13.592 +<a class="c2html-lineno" name="l536" href="#l536">536 </a>                }
  13.593 +<a class="c2html-lineno" name="l537" href="#l537">537 </a>            }
  13.594 +<a class="c2html-lineno" name="l538" href="#l538">538 </a>        }
  13.595 +<a class="c2html-lineno" name="l539" href="#l539">539 </a>
  13.596 +<a class="c2html-lineno" name="l540" href="#l540">540 </a>        <span class="c2html-keyword">for</span> (<span class="c2html-type">uint8_t</span> i = <span class="c2html-macroconst">0</span> ; i &lt; candidatecount ; i++) {
  13.597 +<a class="c2html-lineno" name="l541" href="#l541">541 </a>            GameState simulation = gamestate_copy_sim(gamestate);
  13.598 +<a class="c2html-lineno" name="l542" href="#l542">542 </a>            Move simmove = candidates[i];
  13.599 +<a class="c2html-lineno" name="l543" href="#l543">543 </a>            apply_move(&simulation, &simmove);
  13.600 +<a class="c2html-lineno" name="l544" href="#l544">544 </a>            <span class="c2html-keyword">if</span> (!is_covered(&simulation, kingrow, kingfile,
  13.601 +<a class="c2html-lineno" name="l545" href="#l545">545 </a>                    opponent_color(color))) {
  13.602 +<a class="c2html-lineno" name="l546" href="#l546">546 </a>                result = <span class="c2html-macroconst">1</span>;
  13.603 +<a class="c2html-lineno" name="l547" href="#l547">547 </a>                <span class="c2html-keyword">if</span> (threats && threatcount) {
  13.604 +<a class="c2html-lineno" name="l548" href="#l548">548 </a>                    threats[(*threatcount)++] = candidates[i];
  13.605 +<a class="c2html-lineno" name="l549" href="#l549">549 </a>                }
  13.606 +<a class="c2html-lineno" name="l550" href="#l550">550 </a>            }
  13.607 +<a class="c2html-lineno" name="l551" href="#l551">551 </a>        }
  13.608 +<a class="c2html-lineno" name="l552" href="#l552">552 </a>        
  13.609 +<a class="c2html-lineno" name="l553" href="#l553">553 </a>        <span class="c2html-keyword">return</span> result;
  13.610 +<a class="c2html-lineno" name="l554" href="#l554">554 </a>    } <span class="c2html-keyword">else</span> {
  13.611 +<a class="c2html-lineno" name="l555" href="#l555">555 </a>        <span class="c2html-keyword">return</span> <span class="c2html-macroconst">0</span>;
  13.612 +<a class="c2html-lineno" name="l556" href="#l556">556 </a>    }
  13.613 +<a class="c2html-lineno" name="l557" href="#l557">557 </a>}
  13.614 +<a class="c2html-lineno" name="l558" href="#l558">558 </a>
  13.615 +<a class="c2html-lineno" name="l559" href="#l559">559 </a><span class="c2html-keyword">static</span> <span class="c2html-keyword">int</span> getlocation(GameState *gamestate, Move *move) {   
  13.616 +<a class="c2html-lineno" name="l560" href="#l560">560 </a>
  13.617 +<a class="c2html-lineno" name="l561" href="#l561">561 </a>    <span class="c2html-type">uint8_t</span> color = move-&gt;piece & <span class="c2html-macroconst">COLOR_MASK</span>;
  13.618 +<a class="c2html-lineno" name="l562" href="#l562">562 </a>    _Bool incheck = gamestate-&gt;lastmove?gamestate-&gt;lastmove-&gt;move.check:<span class="c2html-macroconst">0</span>;
  13.619 +<a class="c2html-lineno" name="l563" href="#l563">563 </a>    
  13.620 +<a class="c2html-lineno" name="l564" href="#l564">564 </a>    Move threats[<span class="c2html-macroconst">16</span>], *threat = <span class="c2html-macroconst">NULL</span>;
  13.621 +<a class="c2html-lineno" name="l565" href="#l565">565 </a>    <span class="c2html-type">uint8_t</span> threatcount;
  13.622 +<a class="c2html-lineno" name="l566" href="#l566">566 </a>    
  13.623 +<a class="c2html-lineno" name="l567" href="#l567">567 </a>    <span class="c2html-keyword">if</span> (get_threats(gamestate, move-&gt;torow, move-&gt;tofile, color,
  13.624 +<a class="c2html-lineno" name="l568" href="#l568">568 </a>            threats, &threatcount)) {
  13.625 +<a class="c2html-lineno" name="l569" href="#l569">569 </a>        
  13.626 +<a class="c2html-lineno" name="l570" href="#l570">570 </a>        <span class="c2html-keyword">int</span> reason = <span class="c2html-macroconst">INVALID_POSITION</span>;
  13.627 +<a class="c2html-lineno" name="l571" href="#l571">571 </a>        
  13.628 +<a class="c2html-lineno" name="l572" href="#l572">572 </a>        <span class="c2html-comment">// find threats for the specified position</span>
  13.629 +<a class="c2html-lineno" name="l573" href="#l573">573 </a>        <span class="c2html-keyword">for</span> (<span class="c2html-type">uint8_t</span> i = <span class="c2html-macroconst">0</span> ; i &lt; threatcount ; i++) {
  13.630 +<a class="c2html-lineno" name="l574" href="#l574">574 </a>            <span class="c2html-keyword">if</span> ((threats[i].piece & (<span class="c2html-macroconst">PIECE_MASK</span> | <span class="c2html-macroconst">COLOR_MASK</span>))
  13.631 +<a class="c2html-lineno" name="l575" href="#l575">575 </a>                    == move-&gt;piece &&
  13.632 +<a class="c2html-lineno" name="l576" href="#l576">576 </a>                    (move-&gt;fromrow == <span class="c2html-macroconst">POS_UNSPECIFIED</span> ||
  13.633 +<a class="c2html-lineno" name="l577" href="#l577">577 </a>                    move-&gt;fromrow == threats[i].fromrow) &&
  13.634 +<a class="c2html-lineno" name="l578" href="#l578">578 </a>                    (move-&gt;fromfile == <span class="c2html-macroconst">POS_UNSPECIFIED</span> ||
  13.635 +<a class="c2html-lineno" name="l579" href="#l579">579 </a>                    move-&gt;fromfile == threats[i].fromfile)) {
  13.636 +<a class="c2html-lineno" name="l580" href="#l580">580 </a>
  13.637 +<a class="c2html-lineno" name="l581" href="#l581">581 </a>                <span class="c2html-keyword">if</span> (threat) {
  13.638 +<a class="c2html-lineno" name="l582" href="#l582">582 </a>                    <span class="c2html-keyword">return</span> <span class="c2html-macroconst">AMBIGUOUS_MOVE</span>;
  13.639 +<a class="c2html-lineno" name="l583" href="#l583">583 </a>                } <span class="c2html-keyword">else</span> {
  13.640 +<a class="c2html-lineno" name="l584" href="#l584">584 </a>                    <span class="c2html-comment">// found threat is no real threat</span>
  13.641 +<a class="c2html-lineno" name="l585" href="#l585">585 </a>                    <span class="c2html-keyword">if</span> (is_pinned(gamestate, &(threats[i]))) {
  13.642 +<a class="c2html-lineno" name="l586" href="#l586">586 </a>                        reason = incheck?<span class="c2html-macroconst">KING_IN_CHECK</span>:<span class="c2html-macroconst">PIECE_PINNED</span>;
  13.643 +<a class="c2html-lineno" name="l587" href="#l587">587 </a>                    } <span class="c2html-keyword">else</span> {
  13.644 +<a class="c2html-lineno" name="l588" href="#l588">588 </a>                        threat = &(threats[i]);
  13.645 +<a class="c2html-lineno" name="l589" href="#l589">589 </a>                    }
  13.646 +<a class="c2html-lineno" name="l590" href="#l590">590 </a>                }
  13.647 +<a class="c2html-lineno" name="l591" href="#l591">591 </a>            }
  13.648 +<a class="c2html-lineno" name="l592" href="#l592">592 </a>        }
  13.649 +<a class="c2html-lineno" name="l593" href="#l593">593 </a>        
  13.650 +<a class="c2html-lineno" name="l594" href="#l594">594 </a>        <span class="c2html-comment">// can't threaten specified position</span>
  13.651 +<a class="c2html-lineno" name="l595" href="#l595">595 </a>        <span class="c2html-keyword">if</span> (!threat) {
  13.652 +<a class="c2html-lineno" name="l596" href="#l596">596 </a>            <span class="c2html-keyword">return</span> reason;
  13.653 +<a class="c2html-lineno" name="l597" href="#l597">597 </a>        }
  13.654 +<a class="c2html-lineno" name="l598" href="#l598">598 </a>
  13.655 +<a class="c2html-lineno" name="l599" href="#l599">599 </a>        memcpy(move, threat, <span class="c2html-keyword">sizeof</span>(Move));
  13.656 +<a class="c2html-lineno" name="l600" href="#l600">600 </a>        <span class="c2html-keyword">return</span> <span class="c2html-macroconst">VALID_MOVE_SYNTAX</span>;
  13.657 +<a class="c2html-lineno" name="l601" href="#l601">601 </a>    } <span class="c2html-keyword">else</span> {
  13.658 +<a class="c2html-lineno" name="l602" href="#l602">602 </a>        <span class="c2html-keyword">return</span> <span class="c2html-macroconst">INVALID_POSITION</span>;
  13.659 +<a class="c2html-lineno" name="l603" href="#l603">603 </a>    }
  13.660 +<a class="c2html-lineno" name="l604" href="#l604">604 </a>}
  13.661 +<a class="c2html-lineno" name="l605" href="#l605">605 </a>
  13.662 +<a class="c2html-lineno" name="l606" href="#l606">606 </a><span class="c2html-keyword">int</span> eval_move(GameState *gamestate, <span class="c2html-keyword">char</span> *mstr, Move *move, <span class="c2html-type">uint8_t</span> color) {
  13.663 +<a class="c2html-lineno" name="l607" href="#l607">607 </a>    memset(move, <span class="c2html-macroconst">0</span>, <span class="c2html-keyword">sizeof</span>(Move));
  13.664 +<a class="c2html-lineno" name="l608" href="#l608">608 </a>    move-&gt;fromfile = <span class="c2html-macroconst">POS_UNSPECIFIED</span>;
  13.665 +<a class="c2html-lineno" name="l609" href="#l609">609 </a>    move-&gt;fromrow = <span class="c2html-macroconst">POS_UNSPECIFIED</span>;
  13.666 +<a class="c2html-lineno" name="l610" href="#l610">610 </a>
  13.667 +<a class="c2html-lineno" name="l611" href="#l611">611 </a>    <span class="c2html-type">size_t</span> len = strlen(mstr);
  13.668 +<a class="c2html-lineno" name="l612" href="#l612">612 </a>    <span class="c2html-keyword">if</span> (len &lt; <span class="c2html-macroconst">1</span> || len &gt; <span class="c2html-macroconst">6</span>) {
  13.669 +<a class="c2html-lineno" name="l613" href="#l613">613 </a>        <span class="c2html-keyword">return</span> <span class="c2html-macroconst">INVALID_MOVE_SYNTAX</span>;
  13.670 +<a class="c2html-lineno" name="l614" href="#l614">614 </a>    }
  13.671 +<a class="c2html-lineno" name="l615" href="#l615">615 </a>    
  13.672 +<a class="c2html-lineno" name="l616" href="#l616">616 </a>    <span class="c2html-comment">/* evaluate check/checkmate flags */</span>
  13.673 +<a class="c2html-lineno" name="l617" href="#l617">617 </a>    <span class="c2html-keyword">if</span> (mstr[len-<span class="c2html-macroconst">1</span>] == <span class="c2html-string">'+'</span>) {
  13.674 +<a class="c2html-lineno" name="l618" href="#l618">618 </a>        len--; mstr[len] = <span class="c2html-string">'\0'</span>;
  13.675 +<a class="c2html-lineno" name="l619" href="#l619">619 </a>        move-&gt;check = <span class="c2html-macroconst">1</span>;
  13.676 +<a class="c2html-lineno" name="l620" href="#l620">620 </a>    } <span class="c2html-keyword">else</span> <span class="c2html-keyword">if</span> (mstr[len-<span class="c2html-macroconst">1</span>] == <span class="c2html-string">'#'</span>) {
  13.677 +<a class="c2html-lineno" name="l621" href="#l621">621 </a>        len--; mstr[len] = <span class="c2html-string">'\0'</span>;
  13.678 +<a class="c2html-lineno" name="l622" href="#l622">622 </a>        <span class="c2html-comment">/* ignore - validation should set game state */</span>
  13.679 +<a class="c2html-lineno" name="l623" href="#l623">623 </a>    }
  13.680 +<a class="c2html-lineno" name="l624" href="#l624">624 </a>    
  13.681 +<a class="c2html-lineno" name="l625" href="#l625">625 </a>    <span class="c2html-comment">/* evaluate promotion */</span>
  13.682 +<a class="c2html-lineno" name="l626" href="#l626">626 </a>    <span class="c2html-keyword">if</span> (len &gt; <span class="c2html-macroconst">3</span> && mstr[len-<span class="c2html-macroconst">2</span>] == <span class="c2html-string">'='</span>) {
  13.683 +<a class="c2html-lineno" name="l627" href="#l627">627 </a>        move-&gt;promotion = getpiece(mstr[len-<span class="c2html-macroconst">1</span>]);
  13.684 +<a class="c2html-lineno" name="l628" href="#l628">628 </a>        <span class="c2html-keyword">if</span> (!move-&gt;promotion) {
  13.685 +<a class="c2html-lineno" name="l629" href="#l629">629 </a>            <span class="c2html-keyword">return</span> <span class="c2html-macroconst">INVALID_MOVE_SYNTAX</span>;
  13.686 +<a class="c2html-lineno" name="l630" href="#l630">630 </a>        } <span class="c2html-keyword">else</span> {
  13.687 +<a class="c2html-lineno" name="l631" href="#l631">631 </a>            move-&gt;promotion |= color;
  13.688 +<a class="c2html-lineno" name="l632" href="#l632">632 </a>            len -= <span class="c2html-macroconst">2</span>;
  13.689 +<a class="c2html-lineno" name="l633" href="#l633">633 </a>            mstr[len] = <span class="c2html-macroconst">0</span>;
  13.690 +<a class="c2html-lineno" name="l634" href="#l634">634 </a>        }
  13.691 +<a class="c2html-lineno" name="l635" href="#l635">635 </a>    }
  13.692 +<a class="c2html-lineno" name="l636" href="#l636">636 </a>    
  13.693 +<a class="c2html-lineno" name="l637" href="#l637">637 </a>    <span class="c2html-keyword">if</span> (len == <span class="c2html-macroconst">2</span>) {
  13.694 +<a class="c2html-lineno" name="l638" href="#l638">638 </a>        <span class="c2html-comment">/* pawn move (e.g. "e4") */</span>
  13.695 +<a class="c2html-lineno" name="l639" href="#l639">639 </a>        move-&gt;piece = <span class="c2html-macroconst">PAWN</span>;
  13.696 +<a class="c2html-lineno" name="l640" href="#l640">640 </a>        move-&gt;tofile = fileidx(mstr[<span class="c2html-macroconst">0</span>]);
  13.697 +<a class="c2html-lineno" name="l641" href="#l641">641 </a>        move-&gt;torow = rowidx(mstr[<span class="c2html-macroconst">1</span>]);
  13.698 +<a class="c2html-lineno" name="l642" href="#l642">642 </a>    } <span class="c2html-keyword">else</span> <span class="c2html-keyword">if</span> (len == <span class="c2html-macroconst">3</span>) {
  13.699 +<a class="c2html-lineno" name="l643" href="#l643">643 </a>        <span class="c2html-keyword">if</span> (strcmp(mstr, <span class="c2html-string">"O-O"</span>) == <span class="c2html-macroconst">0</span>) {
  13.700 +<a class="c2html-lineno" name="l644" href="#l644">644 </a>            <span class="c2html-comment">/* king side castling */</span>
  13.701 +<a class="c2html-lineno" name="l645" href="#l645">645 </a>            move-&gt;piece = <span class="c2html-macroconst">KING</span>;
  13.702 +<a class="c2html-lineno" name="l646" href="#l646">646 </a>            move-&gt;fromfile = fileidx(<span class="c2html-string">'e'</span>);
  13.703 +<a class="c2html-lineno" name="l647" href="#l647">647 </a>            move-&gt;tofile = fileidx(<span class="c2html-string">'g'</span>);
  13.704 +<a class="c2html-lineno" name="l648" href="#l648">648 </a>            move-&gt;fromrow = move-&gt;torow = color == <span class="c2html-macroconst">WHITE</span> ? <span class="c2html-macroconst">0</span> : <span class="c2html-macroconst">7</span>;
  13.705 +<a class="c2html-lineno" name="l649" href="#l649">649 </a>        } <span class="c2html-keyword">else</span> {
  13.706 +<a class="c2html-lineno" name="l650" href="#l650">650 </a>            <span class="c2html-comment">/* move (e.g. "Nf3") */</span>
  13.707 +<a class="c2html-lineno" name="l651" href="#l651">651 </a>            move-&gt;piece = getpiece(mstr[<span class="c2html-macroconst">0</span>]);
  13.708 +<a class="c2html-lineno" name="l652" href="#l652">652 </a>            move-&gt;tofile = fileidx(mstr[<span class="c2html-macroconst">1</span>]);
  13.709 +<a class="c2html-lineno" name="l653" href="#l653">653 </a>            move-&gt;torow = rowidx(mstr[<span class="c2html-macroconst">2</span>]);
  13.710 +<a class="c2html-lineno" name="l654" href="#l654">654 </a>        }
  13.711 +<a class="c2html-lineno" name="l655" href="#l655">655 </a>    } <span class="c2html-keyword">else</span> <span class="c2html-keyword">if</span> (len == <span class="c2html-macroconst">4</span>) {
  13.712 +<a class="c2html-lineno" name="l656" href="#l656">656 </a>        move-&gt;piece = getpiece(mstr[<span class="c2html-macroconst">0</span>]);
  13.713 +<a class="c2html-lineno" name="l657" href="#l657">657 </a>        <span class="c2html-keyword">if</span> (!move-&gt;piece) {
  13.714 +<a class="c2html-lineno" name="l658" href="#l658">658 </a>            move-&gt;piece = <span class="c2html-macroconst">PAWN</span>;
  13.715 +<a class="c2html-lineno" name="l659" href="#l659">659 </a>            move-&gt;fromfile = fileidx(mstr[<span class="c2html-macroconst">0</span>]);
  13.716 +<a class="c2html-lineno" name="l660" href="#l660">660 </a>        }
  13.717 +<a class="c2html-lineno" name="l661" href="#l661">661 </a>        <span class="c2html-keyword">if</span> (mstr[<span class="c2html-macroconst">1</span>] == <span class="c2html-string">'x'</span>) {
  13.718 +<a class="c2html-lineno" name="l662" href="#l662">662 </a>            <span class="c2html-comment">/* capture (e.g. "Nxf3", "dxe5") */</span>
  13.719 +<a class="c2html-lineno" name="l663" href="#l663">663 </a>            move-&gt;capture = <span class="c2html-macroconst">1</span>;
  13.720 +<a class="c2html-lineno" name="l664" href="#l664">664 </a>        } <span class="c2html-keyword">else</span> {
  13.721 +<a class="c2html-lineno" name="l665" href="#l665">665 </a>            <span class="c2html-comment">/* move (e.g. "Ndf3", "N2c3", "e2e4") */</span>
  13.722 +<a class="c2html-lineno" name="l666" href="#l666">666 </a>            <span class="c2html-keyword">if</span> (isfile(mstr[<span class="c2html-macroconst">1</span>])) {
  13.723 +<a class="c2html-lineno" name="l667" href="#l667">667 </a>                move-&gt;fromfile = fileidx(mstr[<span class="c2html-macroconst">1</span>]);
  13.724 +<a class="c2html-lineno" name="l668" href="#l668">668 </a>                <span class="c2html-keyword">if</span> (move-&gt;piece == <span class="c2html-macroconst">PAWN</span>) {
  13.725 +<a class="c2html-lineno" name="l669" href="#l669">669 </a>                    move-&gt;piece = <span class="c2html-macroconst">0</span>;
  13.726 +<a class="c2html-lineno" name="l670" href="#l670">670 </a>                }
  13.727 +<a class="c2html-lineno" name="l671" href="#l671">671 </a>            } <span class="c2html-keyword">else</span> {
  13.728 +<a class="c2html-lineno" name="l672" href="#l672">672 </a>                move-&gt;fromrow = rowidx(mstr[<span class="c2html-macroconst">1</span>]);
  13.729 +<a class="c2html-lineno" name="l673" href="#l673">673 </a>            }
  13.730 +<a class="c2html-lineno" name="l674" href="#l674">674 </a>        }
  13.731 +<a class="c2html-lineno" name="l675" href="#l675">675 </a>        move-&gt;tofile = fileidx(mstr[<span class="c2html-macroconst">2</span>]);
  13.732 +<a class="c2html-lineno" name="l676" href="#l676">676 </a>        move-&gt;torow = rowidx(mstr[<span class="c2html-macroconst">3</span>]);
  13.733 +<a class="c2html-lineno" name="l677" href="#l677">677 </a>    } <span class="c2html-keyword">else</span> <span class="c2html-keyword">if</span> (len == <span class="c2html-macroconst">5</span>) {
  13.734 +<a class="c2html-lineno" name="l678" href="#l678">678 </a>        <span class="c2html-keyword">if</span> (strcmp(mstr, <span class="c2html-string">"O-O-O"</span>) == <span class="c2html-macroconst">0</span>) {
  13.735 +<a class="c2html-lineno" name="l679" href="#l679">679 </a>            <span class="c2html-comment">/* queen side castling "O-O-O" */</span>
  13.736 +<a class="c2html-lineno" name="l680" href="#l680">680 </a>            move-&gt;piece = <span class="c2html-macroconst">KING</span>;
  13.737 +<a class="c2html-lineno" name="l681" href="#l681">681 </a>            move-&gt;fromfile = fileidx(<span class="c2html-string">'e'</span>);
  13.738 +<a class="c2html-lineno" name="l682" href="#l682">682 </a>            move-&gt;tofile = fileidx(<span class="c2html-string">'c'</span>);
  13.739 +<a class="c2html-lineno" name="l683" href="#l683">683 </a>            move-&gt;fromrow = move-&gt;torow = color == <span class="c2html-macroconst">WHITE</span> ? <span class="c2html-macroconst">0</span> : <span class="c2html-macroconst">7</span>;
  13.740 +<a class="c2html-lineno" name="l684" href="#l684">684 </a>        } <span class="c2html-keyword">else</span> {
  13.741 +<a class="c2html-lineno" name="l685" href="#l685">685 </a>            move-&gt;piece = getpiece(mstr[<span class="c2html-macroconst">0</span>]);
  13.742 +<a class="c2html-lineno" name="l686" href="#l686">686 </a>            <span class="c2html-keyword">if</span> (mstr[<span class="c2html-macroconst">2</span>] == <span class="c2html-string">'x'</span>) {
  13.743 +<a class="c2html-lineno" name="l687" href="#l687">687 </a>                move-&gt;capture = <span class="c2html-macroconst">1</span>;
  13.744 +<a class="c2html-lineno" name="l688" href="#l688">688 </a>                <span class="c2html-keyword">if</span> (move-&gt;piece) {
  13.745 +<a class="c2html-lineno" name="l689" href="#l689">689 </a>                    <span class="c2html-comment">/* capture (e.g. "Ndxf3") */</span>
  13.746 +<a class="c2html-lineno" name="l690" href="#l690">690 </a>                    move-&gt;fromfile = fileidx(mstr[<span class="c2html-macroconst">1</span>]);
  13.747 +<a class="c2html-lineno" name="l691" href="#l691">691 </a>                } <span class="c2html-keyword">else</span> {
  13.748 +<a class="c2html-lineno" name="l692" href="#l692">692 </a>                    <span class="c2html-comment">/* long notation capture (e.g. "e5xf6") */</span>
  13.749 +<a class="c2html-lineno" name="l693" href="#l693">693 </a>                    move-&gt;piece = <span class="c2html-macroconst">PAWN</span>;
  13.750 +<a class="c2html-lineno" name="l694" href="#l694">694 </a>                    move-&gt;fromfile = fileidx(mstr[<span class="c2html-macroconst">0</span>]);
  13.751 +<a class="c2html-lineno" name="l695" href="#l695">695 </a>                    move-&gt;fromrow = rowidx(mstr[<span class="c2html-macroconst">1</span>]);
  13.752 +<a class="c2html-lineno" name="l696" href="#l696">696 </a>                }
  13.753 +<a class="c2html-lineno" name="l697" href="#l697">697 </a>            } <span class="c2html-keyword">else</span> {
  13.754 +<a class="c2html-lineno" name="l698" href="#l698">698 </a>                <span class="c2html-comment">/* long notation move (e.g. "Nc5a4") */</span>
  13.755 +<a class="c2html-lineno" name="l699" href="#l699">699 </a>                move-&gt;fromfile = fileidx(mstr[<span class="c2html-macroconst">1</span>]);
  13.756 +<a class="c2html-lineno" name="l700" href="#l700">700 </a>                move-&gt;fromrow = rowidx(mstr[<span class="c2html-macroconst">2</span>]);
  13.757 +<a class="c2html-lineno" name="l701" href="#l701">701 </a>            }
  13.758 +<a class="c2html-lineno" name="l702" href="#l702">702 </a>            move-&gt;tofile = fileidx(mstr[<span class="c2html-macroconst">3</span>]);
  13.759 +<a class="c2html-lineno" name="l703" href="#l703">703 </a>            move-&gt;torow = rowidx(mstr[<span class="c2html-macroconst">4</span>]);
  13.760 +<a class="c2html-lineno" name="l704" href="#l704">704 </a>        }
  13.761 +<a class="c2html-lineno" name="l705" href="#l705">705 </a>    } <span class="c2html-keyword">else</span> <span class="c2html-keyword">if</span> (len == <span class="c2html-macroconst">6</span>) {
  13.762 +<a class="c2html-lineno" name="l706" href="#l706">706 </a>        <span class="c2html-comment">/* long notation capture (e.g. "Nc5xf3") */</span>
  13.763 +<a class="c2html-lineno" name="l707" href="#l707">707 </a>        <span class="c2html-keyword">if</span> (mstr[<span class="c2html-macroconst">3</span>] == <span class="c2html-string">'x'</span>) {
  13.764 +<a class="c2html-lineno" name="l708" href="#l708">708 </a>            move-&gt;capture = <span class="c2html-macroconst">1</span>;
  13.765 +<a class="c2html-lineno" name="l709" href="#l709">709 </a>            move-&gt;piece = getpiece(mstr[<span class="c2html-macroconst">0</span>]);
  13.766 +<a class="c2html-lineno" name="l710" href="#l710">710 </a>            move-&gt;fromfile = fileidx(mstr[<span class="c2html-macroconst">1</span>]);
  13.767 +<a class="c2html-lineno" name="l711" href="#l711">711 </a>            move-&gt;fromrow = rowidx(mstr[<span class="c2html-macroconst">2</span>]);
  13.768 +<a class="c2html-lineno" name="l712" href="#l712">712 </a>            move-&gt;tofile = fileidx(mstr[<span class="c2html-macroconst">4</span>]);
  13.769 +<a class="c2html-lineno" name="l713" href="#l713">713 </a>            move-&gt;torow = rowidx(mstr[<span class="c2html-macroconst">5</span>]);
  13.770 +<a class="c2html-lineno" name="l714" href="#l714">714 </a>        }
  13.771 +<a class="c2html-lineno" name="l715" href="#l715">715 </a>    }
  13.772 +<a class="c2html-lineno" name="l716" href="#l716">716 </a>
  13.773 +<a class="c2html-lineno" name="l717" href="#l717">717 </a>    
  13.774 +<a class="c2html-lineno" name="l718" href="#l718">718 </a>    <span class="c2html-keyword">if</span> (move-&gt;piece) {
  13.775 +<a class="c2html-lineno" name="l719" href="#l719">719 </a>        <span class="c2html-keyword">if</span> (move-&gt;piece == <span class="c2html-macroconst">PAWN</span>
  13.776 +<a class="c2html-lineno" name="l720" href="#l720">720 </a>            && move-&gt;torow == (color==<span class="c2html-macroconst">WHITE</span>?<span class="c2html-macroconst">7</span>:<span class="c2html-macroconst">0</span>)
  13.777 +<a class="c2html-lineno" name="l721" href="#l721">721 </a>            && !move-&gt;promotion) {
  13.778 +<a class="c2html-lineno" name="l722" href="#l722">722 </a>            <span class="c2html-keyword">return</span> <span class="c2html-macroconst">NEED_PROMOTION</span>;
  13.779 +<a class="c2html-lineno" name="l723" href="#l723">723 </a>        }
  13.780 +<a class="c2html-lineno" name="l724" href="#l724">724 </a>        
  13.781 +<a class="c2html-lineno" name="l725" href="#l725">725 </a>        move-&gt;piece |= color;
  13.782 +<a class="c2html-lineno" name="l726" href="#l726">726 </a>        <span class="c2html-keyword">if</span> (move-&gt;fromfile == <span class="c2html-macroconst">POS_UNSPECIFIED</span>
  13.783 +<a class="c2html-lineno" name="l727" href="#l727">727 </a>            || move-&gt;fromrow == <span class="c2html-macroconst">POS_UNSPECIFIED</span>) {
  13.784 +<a class="c2html-lineno" name="l728" href="#l728">728 </a>            <span class="c2html-keyword">return</span> getlocation(gamestate, move);
  13.785 +<a class="c2html-lineno" name="l729" href="#l729">729 </a>        } <span class="c2html-keyword">else</span> {
  13.786 +<a class="c2html-lineno" name="l730" href="#l730">730 </a>            <span class="c2html-keyword">return</span> chkidx(move) ? <span class="c2html-macroconst">VALID_MOVE_SYNTAX</span> : <span class="c2html-macroconst">INVALID_POSITION</span>;
  13.787 +<a class="c2html-lineno" name="l731" href="#l731">731 </a>        }
  13.788 +<a class="c2html-lineno" name="l732" href="#l732">732 </a>    } <span class="c2html-keyword">else</span> {
  13.789 +<a class="c2html-lineno" name="l733" href="#l733">733 </a>        <span class="c2html-keyword">return</span> <span class="c2html-macroconst">INVALID_MOVE_SYNTAX</span>;
  13.790 +<a class="c2html-lineno" name="l734" href="#l734">734 </a>    }
  13.791 +<a class="c2html-lineno" name="l735" href="#l735">735 </a>}
  13.792 +<a class="c2html-lineno" name="l736" href="#l736">736 </a>
  13.793 +<a class="c2html-lineno" name="l737" href="#l737">737 </a>_Bool is_protected(GameState *gamestate, <span class="c2html-type">uint8_t</span> row, <span class="c2html-type">uint8_t</span> file,
  13.794 +<a class="c2html-lineno" name="l738" href="#l738">738 </a>        <span class="c2html-type">uint8_t</span> color) {
  13.795 +<a class="c2html-lineno" name="l739" href="#l739">739 </a>    
  13.796 +<a class="c2html-lineno" name="l740" href="#l740">740 </a>    Move threats[<span class="c2html-macroconst">16</span>];
  13.797 +<a class="c2html-lineno" name="l741" href="#l741">741 </a>    <span class="c2html-type">uint8_t</span> threatcount;
  13.798 +<a class="c2html-lineno" name="l742" href="#l742">742 </a>    <span class="c2html-keyword">if</span> (get_real_threats(gamestate, row, file, color, threats, &threatcount)) {
  13.799 +<a class="c2html-lineno" name="l743" href="#l743">743 </a>        <span class="c2html-keyword">for</span> (<span class="c2html-keyword">int</span> i = <span class="c2html-macroconst">0</span> ; i &lt; threatcount ; i++) {
  13.800 +<a class="c2html-lineno" name="l744" href="#l744">744 </a>            <span class="c2html-keyword">if</span> (threats[i].piece != (color|<span class="c2html-macroconst">KING</span>)) {
  13.801 +<a class="c2html-lineno" name="l745" href="#l745">745 </a>                <span class="c2html-keyword">return</span> <span class="c2html-macroconst">1</span>;
  13.802 +<a class="c2html-lineno" name="l746" href="#l746">746 </a>            }
  13.803 +<a class="c2html-lineno" name="l747" href="#l747">747 </a>        }
  13.804 +<a class="c2html-lineno" name="l748" href="#l748">748 </a>        <span class="c2html-keyword">return</span> <span class="c2html-macroconst">0</span>;
  13.805 +<a class="c2html-lineno" name="l749" href="#l749">749 </a>    } <span class="c2html-keyword">else</span> {
  13.806 +<a class="c2html-lineno" name="l750" href="#l750">750 </a>        <span class="c2html-keyword">return</span> <span class="c2html-macroconst">0</span>;
  13.807 +<a class="c2html-lineno" name="l751" href="#l751">751 </a>    }
  13.808 +<a class="c2html-lineno" name="l752" href="#l752">752 </a>}
  13.809 +<a class="c2html-lineno" name="l753" href="#l753">753 </a>
  13.810 +<a class="c2html-lineno" name="l754" href="#l754">754 </a><span class="c2html-type">uint16_t</span> remaining_movetime(GameInfo *gameinfo, GameState *gamestate,
  13.811 +<a class="c2html-lineno" name="l755" href="#l755">755 </a>        <span class="c2html-type">uint8_t</span> color) {
  13.812 +<a class="c2html-lineno" name="l756" href="#l756">756 </a>    <span class="c2html-keyword">if</span> (!gameinfo-&gt;timecontrol) {
  13.813 +<a class="c2html-lineno" name="l757" href="#l757">757 </a>        <span class="c2html-keyword">return</span> <span class="c2html-macroconst">0</span>;
  13.814 +<a class="c2html-lineno" name="l758" href="#l758">758 </a>    }
  13.815 +<a class="c2html-lineno" name="l759" href="#l759">759 </a>    
  13.816 +<a class="c2html-lineno" name="l760" href="#l760">760 </a>    <span class="c2html-keyword">if</span> (gamestate-&gt;movelist) {
  13.817 +<a class="c2html-lineno" name="l761" href="#l761">761 </a>        <span class="c2html-type">uint16_t</span> time = gameinfo-&gt;time;
  13.818 +<a class="c2html-lineno" name="l762" href="#l762">762 </a>        <span class="c2html-type">suseconds_t</span> micros = <span class="c2html-macroconst">0</span>;
  13.819 +<a class="c2html-lineno" name="l763" href="#l763">763 </a>        
  13.820 +<a class="c2html-lineno" name="l764" href="#l764">764 </a>        MoveList *movelist = color == <span class="c2html-macroconst">WHITE</span> ?
  13.821 +<a class="c2html-lineno" name="l765" href="#l765">765 </a>            gamestate-&gt;movelist : gamestate-&gt;movelist-&gt;next;
  13.822 +<a class="c2html-lineno" name="l766" href="#l766">766 </a>        
  13.823 +<a class="c2html-lineno" name="l767" href="#l767">767 </a>        <span class="c2html-keyword">while</span> (movelist) {
  13.824 +<a class="c2html-lineno" name="l768" href="#l768">768 </a>            time += gameinfo-&gt;addtime;
  13.825 +<a class="c2html-lineno" name="l769" href="#l769">769 </a>            
  13.826 +<a class="c2html-lineno" name="l770" href="#l770">770 </a>            <span class="c2html-keyword">struct</span> movetimeval *movetime = &(movelist-&gt;move.movetime);
  13.827 +<a class="c2html-lineno" name="l771" href="#l771">771 </a>            <span class="c2html-keyword">if</span> (movetime-&gt;tv_sec &gt;= time) {
  13.828 +<a class="c2html-lineno" name="l772" href="#l772">772 </a>                <span class="c2html-keyword">return</span> <span class="c2html-macroconst">0</span>;
  13.829 +<a class="c2html-lineno" name="l773" href="#l773">773 </a>            }
  13.830 +<a class="c2html-lineno" name="l774" href="#l774">774 </a>            
  13.831 +<a class="c2html-lineno" name="l775" href="#l775">775 </a>            time -= movetime-&gt;tv_sec;
  13.832 +<a class="c2html-lineno" name="l776" href="#l776">776 </a>            micros += movetime-&gt;tv_usec;
  13.833 +<a class="c2html-lineno" name="l777" href="#l777">777 </a>            
  13.834 +<a class="c2html-lineno" name="l778" href="#l778">778 </a>            movelist = movelist-&gt;next ? movelist-&gt;next-&gt;next : <span class="c2html-macroconst">NULL</span>;
  13.835 +<a class="c2html-lineno" name="l779" href="#l779">779 </a>        }
  13.836 +<a class="c2html-lineno" name="l780" href="#l780">780 </a>        
  13.837 +<a class="c2html-lineno" name="l781" href="#l781">781 </a>        <span class="c2html-type">time_t</span> sec;
  13.838 +<a class="c2html-lineno" name="l782" href="#l782">782 </a>        movelist = gamestate-&gt;lastmove;
  13.839 +<a class="c2html-lineno" name="l783" href="#l783">783 </a>        <span class="c2html-keyword">if</span> ((movelist-&gt;move.piece & <span class="c2html-macroconst">COLOR_MASK</span>) != color) {
  13.840 +<a class="c2html-lineno" name="l784" href="#l784">784 </a>            <span class="c2html-keyword">struct</span> movetimeval *lastmovetstamp = &(movelist-&gt;move.timestamp);
  13.841 +<a class="c2html-lineno" name="l785" href="#l785">785 </a>            <span class="c2html-keyword">struct</span> timeval currenttstamp;
  13.842 +<a class="c2html-lineno" name="l786" href="#l786">786 </a>            gettimeofday(&currenttstamp, <span class="c2html-macroconst">NULL</span>);
  13.843 +<a class="c2html-lineno" name="l787" href="#l787">787 </a>            micros += currenttstamp.tv_usec - lastmovetstamp-&gt;tv_usec;
  13.844 +<a class="c2html-lineno" name="l788" href="#l788">788 </a>            sec = currenttstamp.tv_sec - lastmovetstamp-&gt;tv_sec;
  13.845 +<a class="c2html-lineno" name="l789" href="#l789">789 </a>            <span class="c2html-keyword">if</span> (sec &gt;= time) {
  13.846 +<a class="c2html-lineno" name="l790" href="#l790">790 </a>                <span class="c2html-keyword">return</span> <span class="c2html-macroconst">0</span>;
  13.847 +<a class="c2html-lineno" name="l791" href="#l791">791 </a>            }
  13.848 +<a class="c2html-lineno" name="l792" href="#l792">792 </a>            
  13.849 +<a class="c2html-lineno" name="l793" href="#l793">793 </a>            time -= sec;
  13.850 +<a class="c2html-lineno" name="l794" href="#l794">794 </a>        }
  13.851 +<a class="c2html-lineno" name="l795" href="#l795">795 </a>        
  13.852 +<a class="c2html-lineno" name="l796" href="#l796">796 </a>        sec = micros / 1e6L;
  13.853 +<a class="c2html-lineno" name="l797" href="#l797">797 </a>        
  13.854 +<a class="c2html-lineno" name="l798" href="#l798">798 </a>        <span class="c2html-keyword">if</span> (sec &gt;= time) {
  13.855 +<a class="c2html-lineno" name="l799" href="#l799">799 </a>            <span class="c2html-keyword">return</span> <span class="c2html-macroconst">0</span>;
  13.856 +<a class="c2html-lineno" name="l800" href="#l800">800 </a>        }
  13.857 +<a class="c2html-lineno" name="l801" href="#l801">801 </a>
  13.858 +<a class="c2html-lineno" name="l802" href="#l802">802 </a>        time -= sec;
  13.859 +<a class="c2html-lineno" name="l803" href="#l803">803 </a>        
  13.860 +<a class="c2html-lineno" name="l804" href="#l804">804 </a>        <span class="c2html-keyword">return</span> time;
  13.861 +<a class="c2html-lineno" name="l805" href="#l805">805 </a>    } <span class="c2html-keyword">else</span> {
  13.862 +<a class="c2html-lineno" name="l806" href="#l806">806 </a>        <span class="c2html-keyword">return</span> gameinfo-&gt;time;
  13.863 +<a class="c2html-lineno" name="l807" href="#l807">807 </a>    }
  13.864 +<a class="c2html-lineno" name="l808" href="#l808">808 </a>}
  13.865 +</div>
  13.866 +  </body>
  13.867 +</html>
  13.868 +
    14.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
    14.2 +++ b/test/gs/ctest.html	Mon Apr 24 21:01:41 2023 +0200
    14.3 @@ -0,0 +1,448 @@
    14.4 +<!DOCTYPE html>
    14.5 +<html>
    14.6 +  <head>
    14.7 +    <title>c2html</title>
    14.8 +    <style type="text/css">
    14.9 +      div.c2html-code {
   14.10 +        white-space: pre;
   14.11 +        font-family: monospace;
   14.12 +      }
   14.13 +      a.c2html-lineno {
   14.14 +        /* as long as user-select isn't widely spread, we throw the bomb */
   14.15 +        -webkit-user-select: none;
   14.16 +        -moz-user-select: none;
   14.17 +        -ms-user-select: none;
   14.18 +        user-select: none;
   14.19 +        display: inline-block;
   14.20 +        font-style: italic;
   14.21 +        text-decoration: none;
   14.22 +        color: grey;
   14.23 +      }
   14.24 +      span.c2html-keyword {
   14.25 +        color: blue;
   14.26 +      }
   14.27 +      span.c2html-macroconst {
   14.28 +        color: cornflowerblue;
   14.29 +      }
   14.30 +      span.c2html-type {
   14.31 +        color: cornflowerblue;
   14.32 +      }
   14.33 +      span.c2html-directive {
   14.34 +        color: green;
   14.35 +      }
   14.36 +      span.c2html-string {
   14.37 +        color: darkorange;
   14.38 +      }
   14.39 +      span.c2html-comment {
   14.40 +        color: grey;
   14.41 +      }
   14.42 +      span.c2html-stdinclude {
   14.43 +        color: darkorange;
   14.44 +      }
   14.45 +      span.c2html-userinclude {
   14.46 +        color: darkorange;
   14.47 +      }
   14.48 +      a.c2html-userinclude {
   14.49 +        color: darkorange;
   14.50 +        text-decoration: underline;
   14.51 +      }
   14.52 +    </style>
   14.53 +  </head>
   14.54 +  <body>
   14.55 +
   14.56 +<div class="c2html-code">
   14.57 +<a class="c2html-lineno" name="l1" href="#l1">  1 </a><span class="c2html-comment">/*</span>
   14.58 +<a class="c2html-lineno" name="l2" href="#l2">  2 </a><span class="c2html-comment"> * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.</span>
   14.59 +<a class="c2html-lineno" name="l3" href="#l3">  3 </a><span class="c2html-comment"> *</span>
   14.60 +<a class="c2html-lineno" name="l4" href="#l4">  4 </a><span class="c2html-comment"> * Copyright 2015 Olaf Wintermann. All rights reserved.</span>
   14.61 +<a class="c2html-lineno" name="l5" href="#l5">  5 </a><span class="c2html-comment"> *</span>
   14.62 +<a class="c2html-lineno" name="l6" href="#l6">  6 </a><span class="c2html-comment"> * Redistribution and use in source and binary forms, with or without</span>
   14.63 +<a class="c2html-lineno" name="l7" href="#l7">  7 </a><span class="c2html-comment"> * modification, are permitted provided that the following conditions are met:</span>
   14.64 +<a class="c2html-lineno" name="l8" href="#l8">  8 </a><span class="c2html-comment"> *</span>
   14.65 +<a class="c2html-lineno" name="l9" href="#l9">  9 </a><span class="c2html-comment"> *   1. Redistributions of source code must retain the above copyright</span>
   14.66 +<a class="c2html-lineno" name="l10" href="#l10"> 10 </a><span class="c2html-comment"> *      notice, this list of conditions and the following disclaimer.</span>
   14.67 +<a class="c2html-lineno" name="l11" href="#l11"> 11 </a><span class="c2html-comment"> *</span>
   14.68 +<a class="c2html-lineno" name="l12" href="#l12"> 12 </a><span class="c2html-comment"> *   2. Redistributions in binary form must reproduce the above copyright</span>
   14.69 +<a class="c2html-lineno" name="l13" href="#l13"> 13 </a><span class="c2html-comment"> *      notice, this list of conditions and the following disclaimer in the</span>
   14.70 +<a class="c2html-lineno" name="l14" href="#l14"> 14 </a><span class="c2html-comment"> *      documentation and/or other materials provided with the distribution.</span>
   14.71 +<a class="c2html-lineno" name="l15" href="#l15"> 15 </a><span class="c2html-comment"> *</span>
   14.72 +<a class="c2html-lineno" name="l16" href="#l16"> 16 </a><span class="c2html-comment"> * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"</span>
   14.73 +<a class="c2html-lineno" name="l17" href="#l17"> 17 </a><span class="c2html-comment"> * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE</span>
   14.74 +<a class="c2html-lineno" name="l18" href="#l18"> 18 </a><span class="c2html-comment"> * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE</span>
   14.75 +<a class="c2html-lineno" name="l19" href="#l19"> 19 </a><span class="c2html-comment"> * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE</span>
   14.76 +<a class="c2html-lineno" name="l20" href="#l20"> 20 </a><span class="c2html-comment"> * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR</span>
   14.77 +<a class="c2html-lineno" name="l21" href="#l21"> 21 </a><span class="c2html-comment"> * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF</span>
   14.78 +<a class="c2html-lineno" name="l22" href="#l22"> 22 </a><span class="c2html-comment"> * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS</span>
   14.79 +<a class="c2html-lineno" name="l23" href="#l23"> 23 </a><span class="c2html-comment"> * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN</span>
   14.80 +<a class="c2html-lineno" name="l24" href="#l24"> 24 </a><span class="c2html-comment"> * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)</span>
   14.81 +<a class="c2html-lineno" name="l25" href="#l25"> 25 </a><span class="c2html-comment"> * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE</span>
   14.82 +<a class="c2html-lineno" name="l26" href="#l26"> 26 </a><span class="c2html-comment"> * POSSIBILITY OF SUCH DAMAGE.</span>
   14.83 +<a class="c2html-lineno" name="l27" href="#l27"> 27 </a><span class="c2html-comment"> */</span>
   14.84 +<a class="c2html-lineno" name="l28" href="#l28"> 28 </a>
   14.85 +<a class="c2html-lineno" name="l29" href="#l29"> 29 </a><span class="c2html-directive">#include</span> <span class="c2html-stdinclude">&lt;time.h&gt;</span>
   14.86 +<a class="c2html-lineno" name="l30" href="#l30"> 30 </a><span class="c2html-directive">#include</span> <span class="c2html-stdinclude">&lt;stdio.h&gt;</span>
   14.87 +<a class="c2html-lineno" name="l31" href="#l31"> 31 </a><span class="c2html-directive">#include</span> <span class="c2html-stdinclude">&lt;stdlib.h&gt;</span>
   14.88 +<a class="c2html-lineno" name="l32" href="#l32"> 32 </a><span class="c2html-directive">#include</span> <span class="c2html-stdinclude">&lt;string.h&gt;</span>
   14.89 +<a class="c2html-lineno" name="l33" href="#l33"> 33 </a><span class="c2html-directive">#include</span> <span class="c2html-stdinclude">&lt;ucx/string.h&gt;</span>
   14.90 +<a class="c2html-lineno" name="l34" href="#l34"> 34 </a><span class="c2html-directive">#include</span> <span class="c2html-stdinclude">&lt;ucx/buffer.h&gt;</span>
   14.91 +<a class="c2html-lineno" name="l35" href="#l35"> 35 </a><span class="c2html-directive">#include</span> <span class="c2html-stdinclude">&lt;ucx/utils.h&gt;</span>
   14.92 +<a class="c2html-lineno" name="l36" href="#l36"> 36 </a><span class="c2html-directive">#include</span> <span class="c2html-stdinclude">&lt;libxml/tree.h&gt;</span>
   14.93 +<a class="c2html-lineno" name="l37" href="#l37"> 37 </a><span class="c2html-directive">#include</span> <span class="c2html-stdinclude">&lt;curl/curl.h&gt;</span>
   14.94 +<a class="c2html-lineno" name="l38" href="#l38"> 38 </a>
   14.95 +<a class="c2html-lineno" name="l39" href="#l39"> 39 </a><span class="c2html-directive">#include</span> <span class="c2html-stdinclude">&lt;openssl/sha.h&gt;</span>
   14.96 +<a class="c2html-lineno" name="l40" href="#l40"> 40 </a><span class="c2html-directive">#include</span> <span class="c2html-stdinclude">&lt;openssl/hmac.h&gt;</span>
   14.97 +<a class="c2html-lineno" name="l41" href="#l41"> 41 </a><span class="c2html-directive">#include</span> <span class="c2html-stdinclude">&lt;openssl/evp.h&gt;</span>
   14.98 +<a class="c2html-lineno" name="l42" href="#l42"> 42 </a><span class="c2html-directive">#include</span> <span class="c2html-stdinclude">&lt;openssl/bio.h&gt;</span>
   14.99 +<a class="c2html-lineno" name="l43" href="#l43"> 43 </a><span class="c2html-directive">#include</span> <span class="c2html-stdinclude">&lt;openssl/buffer.h&gt;</span>
  14.100 +<a class="c2html-lineno" name="l44" href="#l44"> 44 </a><span class="c2html-directive">#include</span> <span class="c2html-stdinclude">&lt;openssl/rand.h&gt;</span>
  14.101 +<a class="c2html-lineno" name="l45" href="#l45"> 45 </a>
  14.102 +<a class="c2html-lineno" name="l46" href="#l46"> 46 </a><span class="c2html-directive">#include</span> <a class="c2html-userinclude" href="utils.h">"utils.h"</a>
  14.103 +<a class="c2html-lineno" name="l47" href="#l47"> 47 </a><span class="c2html-directive">#include</span> <a class="c2html-userinclude" href="crypto.h">"crypto.h"</a>
  14.104 +<a class="c2html-lineno" name="l48" href="#l48"> 48 </a><span class="c2html-directive">#include</span> <a class="c2html-userinclude" href="webdav.h">"webdav.h"</a>
  14.105 +<a class="c2html-lineno" name="l49" href="#l49"> 49 </a>
  14.106 +<a class="c2html-lineno" name="l50" href="#l50"> 50 </a><span class="c2html-directive">#define</span> <span class="c2html-macroconst">MACRO1337</span> <span class="c2html-macroconst">1337L</span>
  14.107 +<a class="c2html-lineno" name="l51" href="#l51"> 51 </a>
  14.108 +<a class="c2html-lineno" name="l52" href="#l52"> 52 </a><span class="c2html-comment">/* -------------------- This is a testing file. -------------------------- */</span>
  14.109 +<a class="c2html-lineno" name="l53" href="#l53"> 53 </a><span class="c2html-comment">/*</span>
  14.110 +<a class="c2html-lineno" name="l54" href="#l54"> 54 </a><span class="c2html-comment">time_t util_parse_creationdate(char *str) {</span>
  14.111 +<a class="c2html-lineno" name="l55" href="#l55"> 55 </a><span class="c2html-comment">    // example: 2012-11-29T21:35:35Z</span>
  14.112 +<a class="c2html-lineno" name="l56" href="#l56"> 56 </a><span class="c2html-comment">    if(!str) {</span>
  14.113 +<a class="c2html-lineno" name="l57" href="#l57"> 57 </a><span class="c2html-comment">        return 0;</span>
  14.114 +<a class="c2html-lineno" name="l58" href="#l58"> 58 </a><span class="c2html-comment">    }</span>
  14.115 +<a class="c2html-lineno" name="l59" href="#l59"> 59 </a><span class="c2html-comment">    // TODO</span>
  14.116 +<a class="c2html-lineno" name="l60" href="#l60"> 60 </a><span class="c2html-comment">    return 0;</span>
  14.117 +<a class="c2html-lineno" name="l61" href="#l61"> 61 </a><span class="c2html-comment">}</span>
  14.118 +<a class="c2html-lineno" name="l62" href="#l62"> 62 </a><span class="c2html-comment">*/</span>
  14.119 +<a class="c2html-lineno" name="l63" href="#l63"> 63 </a><span class="c2html-type">time_t</span> util_parse_lastmodified(<span class="c2html-keyword">char</span> *str) {
  14.120 +<a class="c2html-lineno" name="l64" href="#l64"> 64 </a>    <span class="c2html-comment">// example: Thu, 29 Nov 2012 21:35:35 GMT</span>
  14.121 +<a class="c2html-lineno" name="l65" href="#l65"> 65 </a>    <span class="c2html-keyword">if</span>(!str) {
  14.122 +<a class="c2html-lineno" name="l66" href="#l66"> 66 </a>        <span class="c2html-keyword">return</span> <span class="c2html-macroconst">0</span>;
  14.123 +<a class="c2html-lineno" name="l67" href="#l67"> 67 </a>    } <span class="c2html-keyword">else</span> {
  14.124 +<a class="c2html-lineno" name="l68" href="#l68"> 68 </a>        <span class="c2html-keyword">return</span> curl_getdate(str, <span class="c2html-macroconst">NULL</span>);
  14.125 +<a class="c2html-lineno" name="l69" href="#l69"> 69 </a>    }
  14.126 +<a class="c2html-lineno" name="l70" href="#l70"> 70 </a>}
  14.127 +<a class="c2html-lineno" name="l71" href="#l71"> 71 </a>
  14.128 +<a class="c2html-lineno" name="l72" href="#l72"> 72 </a><span class="c2html-keyword">int</span> util_getboolean(<span class="c2html-keyword">char</span> *v) {
  14.129 +<a class="c2html-lineno" name="l73" href="#l73"> 73 </a>    <span class="c2html-keyword">if</span>(v[<span class="c2html-macroconst">0</span>] == <span class="c2html-string">'T'</span> || v[<span class="c2html-macroconst">0</span>] == <span class="c2html-string">'t'</span>) {
  14.130 +<a class="c2html-lineno" name="l74" href="#l74"> 74 </a>        <span class="c2html-keyword">return</span> <span class="c2html-macroconst">1</span>;
  14.131 +<a class="c2html-lineno" name="l75" href="#l75"> 75 </a>    }
  14.132 +<a class="c2html-lineno" name="l76" href="#l76"> 76 </a>    <span class="c2html-keyword">return</span> <span class="c2html-macroconst">0</span>;
  14.133 +<a class="c2html-lineno" name="l77" href="#l77"> 77 </a>}
  14.134 +<a class="c2html-lineno" name="l78" href="#l78"> 78 </a>
  14.135 +<a class="c2html-lineno" name="l79" href="#l79"> 79 </a><span class="c2html-keyword">int</span> util_strtoint(<span class="c2html-keyword">char</span> *str, <span class="c2html-type">int64_t</span> *value) {
  14.136 +<a class="c2html-lineno" name="l80" href="#l80"> 80 </a>    <span class="c2html-keyword">char</span> *end;
  14.137 +<a class="c2html-lineno" name="l81" href="#l81"> 81 </a>    <span class="c2html-type">int64_t</span> val = strtoll(str, &end, <span class="c2html-macroconst">0</span>);
  14.138 +<a class="c2html-lineno" name="l82" href="#l82"> 82 </a>    <span class="c2html-keyword">if</span>(strlen(end) == <span class="c2html-macroconst">0</span>) {
  14.139 +<a class="c2html-lineno" name="l83" href="#l83"> 83 </a>        *value = val;
  14.140 +<a class="c2html-lineno" name="l84" href="#l84"> 84 </a>        <span class="c2html-keyword">return</span> <span class="c2html-macroconst">1</span>;
  14.141 +<a class="c2html-lineno" name="l85" href="#l85"> 85 </a>    } <span class="c2html-keyword">else</span> {
  14.142 +<a class="c2html-lineno" name="l86" href="#l86"> 86 </a>        <span class="c2html-keyword">return</span> <span class="c2html-macroconst">0</span>;
  14.143 +<a class="c2html-lineno" name="l87" href="#l87"> 87 </a>    }
  14.144 +<a class="c2html-lineno" name="l88" href="#l88"> 88 </a>}
  14.145 +<a class="c2html-lineno" name="l89" href="#l89"> 89 </a>
  14.146 +<a class="c2html-lineno" name="l90" href="#l90"> 90 </a><span class="c2html-keyword">char</span>* util_url_path(<span class="c2html-keyword">char</span> *url) { 
  14.147 +<a class="c2html-lineno" name="l91" href="#l91"> 91 </a>    <span class="c2html-keyword">char</span> *path = <span class="c2html-macroconst">NULL</span>;
  14.148 +<a class="c2html-lineno" name="l92" href="#l92"> 92 </a>    <span class="c2html-type">size_t</span> len = strlen(url);
  14.149 +<a class="c2html-lineno" name="l93" href="#l93"> 93 </a>    <span class="c2html-keyword">int</span> slashcount = <span class="c2html-macroconst">0</span>;
  14.150 +<a class="c2html-lineno" name="l94" href="#l94"> 94 </a>    <span class="c2html-keyword">int</span> slmax;
  14.151 +<a class="c2html-lineno" name="l95" href="#l95"> 95 </a>    <span class="c2html-keyword">if</span>(len &gt; <span class="c2html-macroconst">7</span> && !strncasecmp(url, <span class="c2html-string">"http://"</span>, <span class="c2html-macroconst">7</span>)) {
  14.152 +<a class="c2html-lineno" name="l96" href="#l96"> 96 </a>        slmax = <span class="c2html-macroconst">3</span>;
  14.153 +<a class="c2html-lineno" name="l97" href="#l97"> 97 </a>    } <span class="c2html-keyword">else</span> <span class="c2html-keyword">if</span>(len &gt; <span class="c2html-macroconst">8</span> && !strncasecmp(url, <span class="c2html-string">"https://"</span>, <span class="c2html-macroconst">8</span>)) {
  14.154 +<a class="c2html-lineno" name="l98" href="#l98"> 98 </a>        slmax = <span class="c2html-macroconst">3</span>;
  14.155 +<a class="c2html-lineno" name="l99" href="#l99"> 99 </a>    } <span class="c2html-keyword">else</span> {
  14.156 +<a class="c2html-lineno" name="l100" href="#l100">100 </a>        slmax = <span class="c2html-macroconst">1</span>;
  14.157 +<a class="c2html-lineno" name="l101" href="#l101">101 </a>    }
  14.158 +<a class="c2html-lineno" name="l102" href="#l102">102 </a>    <span class="c2html-keyword">char</span> c;
  14.159 +<a class="c2html-lineno" name="l103" href="#l103">103 </a>    <span class="c2html-keyword">for</span>(<span class="c2html-keyword">int</span> i=<span class="c2html-macroconst">0</span>;i&lt;len;i++) {
  14.160 +<a class="c2html-lineno" name="l104" href="#l104">104 </a>        c = url[i];
  14.161 +<a class="c2html-lineno" name="l105" href="#l105">105 </a>        <span class="c2html-keyword">if</span>(c == <span class="c2html-string">'/'</span>) {
  14.162 +<a class="c2html-lineno" name="l106" href="#l106">106 </a>            slashcount++;
  14.163 +<a class="c2html-lineno" name="l107" href="#l107">107 </a>            <span class="c2html-keyword">if</span>(slashcount == slmax) {
  14.164 +<a class="c2html-lineno" name="l108" href="#l108">108 </a>                path = url + i;
  14.165 +<a class="c2html-lineno" name="l109" href="#l109">109 </a>                <span class="c2html-keyword">break</span>;
  14.166 +<a class="c2html-lineno" name="l110" href="#l110">110 </a>            }
  14.167 +<a class="c2html-lineno" name="l111" href="#l111">111 </a>        }
  14.168 +<a class="c2html-lineno" name="l112" href="#l112">112 </a>    } 
  14.169 +<a class="c2html-lineno" name="l113" href="#l113">113 </a>    <span class="c2html-keyword">return</span> path;
  14.170 +<a class="c2html-lineno" name="l114" href="#l114">114 </a>}
  14.171 +<a class="c2html-lineno" name="l115" href="#l115">115 </a>
  14.172 +<a class="c2html-lineno" name="l116" href="#l116">116 </a><span class="c2html-keyword">char</span>* util_url_decode(DavSession *sn, <span class="c2html-keyword">char</span> *url) {
  14.173 +<a class="c2html-lineno" name="l117" href="#l117">117 </a>    <span class="c2html-keyword">char</span> *unesc = curl_easy_unescape(sn-&gt;handle, url, strlen(url), <span class="c2html-macroconst">NULL</span>);
  14.174 +<a class="c2html-lineno" name="l118" href="#l118">118 </a>    <span class="c2html-keyword">char</span> *ret = strdup(unesc);
  14.175 +<a class="c2html-lineno" name="l119" href="#l119">119 </a>    curl_free(unesc);
  14.176 +<a class="c2html-lineno" name="l120" href="#l120">120 </a>    <span class="c2html-keyword">return</span> ret;
  14.177 +<a class="c2html-lineno" name="l121" href="#l121">121 </a>}
  14.178 +<a class="c2html-lineno" name="l122" href="#l122">122 </a>
  14.179 +<a class="c2html-lineno" name="l123" href="#l123">123 </a><span class="c2html-keyword">char</span>* util_resource_name(<span class="c2html-keyword">char</span> *url) {
  14.180 +<a class="c2html-lineno" name="l124" href="#l124">124 </a>    <span class="c2html-keyword">int</span> si = <span class="c2html-macroconst">0</span>;
  14.181 +<a class="c2html-lineno" name="l125" href="#l125">125 </a>    <span class="c2html-keyword">int</span> osi = <span class="c2html-macroconst">0</span>;
  14.182 +<a class="c2html-lineno" name="l126" href="#l126">126 </a>    <span class="c2html-keyword">int</span> i = <span class="c2html-macroconst">0</span>;
  14.183 +<a class="c2html-lineno" name="l127" href="#l127">127 </a>    <span class="c2html-keyword">int</span> p = <span class="c2html-macroconst">0</span>;
  14.184 +<a class="c2html-lineno" name="l128" href="#l128">128 </a>    <span class="c2html-keyword">char</span> c;
  14.185 +<a class="c2html-lineno" name="l129" href="#l129">129 </a>    <span class="c2html-keyword">while</span>((c = url[i]) != <span class="c2html-macroconst">0</span>) {
  14.186 +<a class="c2html-lineno" name="l130" href="#l130">130 </a>        <span class="c2html-keyword">if</span>(c == <span class="c2html-string">'/'</span>) {
  14.187 +<a class="c2html-lineno" name="l131" href="#l131">131 </a>            osi = si;
  14.188 +<a class="c2html-lineno" name="l132" href="#l132">132 </a>            si = i;
  14.189 +<a class="c2html-lineno" name="l133" href="#l133">133 </a>            p = <span class="c2html-macroconst">1</span>;
  14.190 +<a class="c2html-lineno" name="l134" href="#l134">134 </a>        }
  14.191 +<a class="c2html-lineno" name="l135" href="#l135">135 </a>        i++;
  14.192 +<a class="c2html-lineno" name="l136" href="#l136">136 </a>    }
  14.193 +<a class="c2html-lineno" name="l137" href="#l137">137 </a>    
  14.194 +<a class="c2html-lineno" name="l138" href="#l138">138 </a>    <span class="c2html-keyword">char</span> *name = url + si + p;
  14.195 +<a class="c2html-lineno" name="l139" href="#l139">139 </a>    <span class="c2html-keyword">if</span>(name[<span class="c2html-macroconst">0</span>] == <span class="c2html-macroconst">0</span>) {
  14.196 +<a class="c2html-lineno" name="l140" href="#l140">140 </a>        name = url + osi + p;
  14.197 +<a class="c2html-lineno" name="l141" href="#l141">141 </a>        <span class="c2html-keyword">if</span>(name[<span class="c2html-macroconst">0</span>] == <span class="c2html-macroconst">0</span>) {
  14.198 +<a class="c2html-lineno" name="l142" href="#l142">142 </a>            <span class="c2html-keyword">return</span> url;
  14.199 +<a class="c2html-lineno" name="l143" href="#l143">143 </a>        }
  14.200 +<a class="c2html-lineno" name="l144" href="#l144">144 </a>    }
  14.201 +<a class="c2html-lineno" name="l145" href="#l145">145 </a>    
  14.202 +<a class="c2html-lineno" name="l146" href="#l146">146 </a>    <span class="c2html-keyword">return</span> name;
  14.203 +<a class="c2html-lineno" name="l147" href="#l147">147 </a>}
  14.204 +<a class="c2html-lineno" name="l148" href="#l148">148 </a>
  14.205 +<a class="c2html-lineno" name="l149" href="#l149">149 </a><span class="c2html-keyword">int</span> util_mkdir(<span class="c2html-keyword">char</span> *path, <span class="c2html-type">mode_t</span> mode) {
  14.206 +<a class="c2html-lineno" name="l150" href="#l150">150 </a><span class="c2html-directive">#ifdef</span> <span class="c2html-macroconst">_WIN32</span>
  14.207 +<a class="c2html-lineno" name="l151" href="#l151">151 </a>    <span class="c2html-keyword">return</span> mkdir(path);
  14.208 +<a class="c2html-lineno" name="l152" href="#l152">152 </a><span class="c2html-directive">#else</span>
  14.209 +<a class="c2html-lineno" name="l153" href="#l153">153 </a>    <span class="c2html-keyword">return</span> mkdir(path, mode);
  14.210 +<a class="c2html-lineno" name="l154" href="#l154">154 </a><span class="c2html-directive">#endif</span>
  14.211 +<a class="c2html-lineno" name="l155" href="#l155">155 </a>}
  14.212 +<a class="c2html-lineno" name="l156" href="#l156">156 </a>
  14.213 +<a class="c2html-lineno" name="l157" href="#l157">157 </a><span class="c2html-keyword">char</span>* util_concat_path(<span class="c2html-keyword">char</span> *url_base, <span class="c2html-keyword">char</span> *p) {
  14.214 +<a class="c2html-lineno" name="l158" href="#l158">158 </a>    <span class="c2html-type">sstr_t</span> base = sstr(url_base);
  14.215 +<a class="c2html-lineno" name="l159" href="#l159">159 </a>    <span class="c2html-type">sstr_t</span> path;
  14.216 +<a class="c2html-lineno" name="l160" href="#l160">160 </a>    <span class="c2html-keyword">if</span>(p) {
  14.217 +<a class="c2html-lineno" name="l161" href="#l161">161 </a>        path = sstr(p);
  14.218 +<a class="c2html-lineno" name="l162" href="#l162">162 </a>    } <span class="c2html-keyword">else</span> {
  14.219 +<a class="c2html-lineno" name="l163" href="#l163">163 </a>        path = sstrn(<span class="c2html-string">""</span>, <span class="c2html-macroconst">0</span>);
  14.220 +<a class="c2html-lineno" name="l164" href="#l164">164 </a>    }
  14.221 +<a class="c2html-lineno" name="l165" href="#l165">165 </a>    
  14.222 +<a class="c2html-lineno" name="l166" href="#l166">166 </a>    <span class="c2html-keyword">int</span> add_separator = <span class="c2html-macroconst">0</span>;
  14.223 +<a class="c2html-lineno" name="l167" href="#l167">167 </a>    <span class="c2html-keyword">if</span>(base.ptr[base.length-<span class="c2html-macroconst">1</span>] == <span class="c2html-string">'/'</span>) {
  14.224 +<a class="c2html-lineno" name="l168" href="#l168">168 </a>        <span class="c2html-keyword">if</span>(path.ptr[<span class="c2html-macroconst">0</span>] == <span class="c2html-string">'/'</span>) {
  14.225 +<a class="c2html-lineno" name="l169" href="#l169">169 </a>            base.length--;
  14.226 +<a class="c2html-lineno" name="l170" href="#l170">170 </a>        }
  14.227 +<a class="c2html-lineno" name="l171" href="#l171">171 </a>    } <span class="c2html-keyword">else</span> {
  14.228 +<a class="c2html-lineno" name="l172" href="#l172">172 </a>        <span class="c2html-keyword">if</span>(path.length == <span class="c2html-macroconst">0</span> || path.ptr[<span class="c2html-macroconst">0</span>] != <span class="c2html-string">'/'</span>) {
  14.229 +<a class="c2html-lineno" name="l173" href="#l173">173 </a>            add_separator = <span class="c2html-macroconst">1</span>;
  14.230 +<a class="c2html-lineno" name="l174" href="#l174">174 </a>        }
  14.231 +<a class="c2html-lineno" name="l175" href="#l175">175 </a>    }
  14.232 +<a class="c2html-lineno" name="l176" href="#l176">176 </a>    
  14.233 +<a class="c2html-lineno" name="l177" href="#l177">177 </a>    <span class="c2html-type">sstr_t</span> url;
  14.234 +<a class="c2html-lineno" name="l178" href="#l178">178 </a>    <span class="c2html-keyword">if</span>(add_separator) {
  14.235 +<a class="c2html-lineno" name="l179" href="#l179">179 </a>        url = sstrcat(<span class="c2html-macroconst">3</span>, base, sstr(<span class="c2html-string">"/"</span>), path);
  14.236 +<a class="c2html-lineno" name="l180" href="#l180">180 </a>    } <span class="c2html-keyword">else</span> {
  14.237 +<a class="c2html-lineno" name="l181" href="#l181">181 </a>        url = sstrcat(<span class="c2html-macroconst">2</span>, base, path);
  14.238 +<a class="c2html-lineno" name="l182" href="#l182">182 </a>    }
  14.239 +<a class="c2html-lineno" name="l183" href="#l183">183 </a>    
  14.240 +<a class="c2html-lineno" name="l184" href="#l184">184 </a>    <span class="c2html-keyword">return</span> url.ptr;
  14.241 +<a class="c2html-lineno" name="l185" href="#l185">185 </a>}
  14.242 +<a class="c2html-lineno" name="l186" href="#l186">186 </a>
  14.243 +<a class="c2html-lineno" name="l187" href="#l187">187 </a><span class="c2html-keyword">void</span> util_set_url(DavSession *sn, <span class="c2html-keyword">char</span> *href) {
  14.244 +<a class="c2html-lineno" name="l188" href="#l188">188 </a>    <span class="c2html-type">sstr_t</span> base = sstr(sn-&gt;base_url);
  14.245 +<a class="c2html-lineno" name="l189" href="#l189">189 </a>    <span class="c2html-type">sstr_t</span> href_str = sstr(href);
  14.246 +<a class="c2html-lineno" name="l190" href="#l190">190 </a>    
  14.247 +<a class="c2html-lineno" name="l191" href="#l191">191 </a>    <span class="c2html-keyword">char</span> *base_path = util_url_path(sn-&gt;base_url);
  14.248 +<a class="c2html-lineno" name="l192" href="#l192">192 </a>    base.length -= strlen(base_path);
  14.249 +<a class="c2html-lineno" name="l193" href="#l193">193 </a>    
  14.250 +<a class="c2html-lineno" name="l194" href="#l194">194 </a>    <span class="c2html-type">sstr_t</span> url = sstrcat(<span class="c2html-macroconst">2</span>, base, href_str);
  14.251 +<a class="c2html-lineno" name="l195" href="#l195">195 </a>    
  14.252 +<a class="c2html-lineno" name="l196" href="#l196">196 </a>    curl_easy_setopt(sn-&gt;handle, <span class="c2html-macroconst">CURLOPT_URL</span>, url.ptr);
  14.253 +<a class="c2html-lineno" name="l197" href="#l197">197 </a>    free(url.ptr);
  14.254 +<a class="c2html-lineno" name="l198" href="#l198">198 </a>}
  14.255 +<a class="c2html-lineno" name="l199" href="#l199">199 </a>
  14.256 +<a class="c2html-lineno" name="l200" href="#l200">200 </a><span class="c2html-keyword">char</span>* util_path_to_url(DavSession *sn, <span class="c2html-keyword">char</span> *path) {
  14.257 +<a class="c2html-lineno" name="l201" href="#l201">201 </a>    <span class="c2html-keyword">char</span> *space = malloc(<span class="c2html-macroconst">256</span>);
  14.258 +<a class="c2html-lineno" name="l202" href="#l202">202 </a>    UcxBuffer *url = ucx_buffer_new(space, <span class="c2html-macroconst">256</span>, <span class="c2html-macroconst">CX_BUFFER_AUTO_EXTEND</span>);
  14.259 +<a class="c2html-lineno" name="l203" href="#l203">203 </a>    
  14.260 +<a class="c2html-lineno" name="l204" href="#l204">204 </a>    <span class="c2html-comment">// add base url</span>
  14.261 +<a class="c2html-lineno" name="l205" href="#l205">205 </a>    ucx_buffer_write(sn-&gt;base_url, <span class="c2html-macroconst">1</span>, strlen(sn-&gt;base_url), url);
  14.262 +<a class="c2html-lineno" name="l206" href="#l206">206 </a>    <span class="c2html-comment">// remove trailing slash</span>
  14.263 +<a class="c2html-lineno" name="l207" href="#l207">207 </a>    ucx_buffer_seek(url, -<span class="c2html-macroconst">1</span>, <span class="c2html-macroconst">SEEK_CUR</span>);
  14.264 +<a class="c2html-lineno" name="l208" href="#l208">208 </a>    
  14.265 +<a class="c2html-lineno" name="l209" href="#l209">209 </a>    <span class="c2html-type">sstr_t</span> p = sstr(path);
  14.266 +<a class="c2html-lineno" name="l210" href="#l210">210 </a>    <span class="c2html-type">ssize_t</span> ntk = <span class="c2html-macroconst">0</span>;
  14.267 +<a class="c2html-lineno" name="l211" href="#l211">211 </a>    <span class="c2html-type">sstr_t</span> *tks = sstrsplit(p, <span class="c2html-macroconst">S</span>(<span class="c2html-string">"/"</span>), &ntk);
  14.268 +<a class="c2html-lineno" name="l212" href="#l212">212 </a>    
  14.269 +<a class="c2html-lineno" name="l213" href="#l213">213 </a>    <span class="c2html-keyword">for</span>(<span class="c2html-keyword">int</span> i=<span class="c2html-macroconst">0</span>;i&lt;ntk;i++) {
  14.270 +<a class="c2html-lineno" name="l214" href="#l214">214 </a>        <span class="c2html-type">sstr_t</span> node = tks[i];
  14.271 +<a class="c2html-lineno" name="l215" href="#l215">215 </a>        <span class="c2html-keyword">if</span>(node.length &gt; <span class="c2html-macroconst">0</span>) {
  14.272 +<a class="c2html-lineno" name="l216" href="#l216">216 </a>            <span class="c2html-keyword">char</span> *esc = curl_easy_escape(sn-&gt;handle, node.ptr, node.length);
  14.273 +<a class="c2html-lineno" name="l217" href="#l217">217 </a>            ucx_buffer_putc(url, <span class="c2html-string">'/'</span>);
  14.274 +<a class="c2html-lineno" name="l218" href="#l218">218 </a>            ucx_buffer_write(esc, <span class="c2html-macroconst">1</span>, strlen(esc), url);
  14.275 +<a class="c2html-lineno" name="l219" href="#l219">219 </a>            curl_free(esc);
  14.276 +<a class="c2html-lineno" name="l220" href="#l220">220 </a>        }
  14.277 +<a class="c2html-lineno" name="l221" href="#l221">221 </a>        free(node.ptr);
  14.278 +<a class="c2html-lineno" name="l222" href="#l222">222 </a>    }
  14.279 +<a class="c2html-lineno" name="l223" href="#l223">223 </a>    free(tks);
  14.280 +<a class="c2html-lineno" name="l224" href="#l224">224 </a>    <span class="c2html-keyword">if</span>(path[p.length-<span class="c2html-macroconst">1</span>] == <span class="c2html-string">'/'</span>) {
  14.281 +<a class="c2html-lineno" name="l225" href="#l225">225 </a>        ucx_buffer_putc(url, <span class="c2html-string">'/'</span>);
  14.282 +<a class="c2html-lineno" name="l226" href="#l226">226 </a>    }
  14.283 +<a class="c2html-lineno" name="l227" href="#l227">227 </a>    ucx_buffer_putc(url, <span class="c2html-macroconst">0</span>);
  14.284 +<a class="c2html-lineno" name="l228" href="#l228">228 </a>    
  14.285 +<a class="c2html-lineno" name="l229" href="#l229">229 </a>    space = url-&gt;space;
  14.286 +<a class="c2html-lineno" name="l230" href="#l230">230 </a>    ucx_buffer_free(url);
  14.287 +<a class="c2html-lineno" name="l231" href="#l231">231 </a>    
  14.288 +<a class="c2html-lineno" name="l232" href="#l232">232 </a>    <span class="c2html-keyword">return</span> space;
  14.289 +<a class="c2html-lineno" name="l233" href="#l233">233 </a>}
  14.290 +<a class="c2html-lineno" name="l234" href="#l234">234 </a>
  14.291 +<a class="c2html-lineno" name="l235" href="#l235">235 </a><span class="c2html-keyword">char</span>* util_parent_path(<span class="c2html-keyword">char</span> *path) {
  14.292 +<a class="c2html-lineno" name="l236" href="#l236">236 </a>    <span class="c2html-keyword">char</span> *name = util_resource_name(path);
  14.293 +<a class="c2html-lineno" name="l237" href="#l237">237 </a>    <span class="c2html-type">size_t</span> namelen = strlen(name);
  14.294 +<a class="c2html-lineno" name="l238" href="#l238">238 </a>    <span class="c2html-type">size_t</span> pathlen = strlen(path);
  14.295 +<a class="c2html-lineno" name="l239" href="#l239">239 </a>    <span class="c2html-type">size_t</span> parentlen = pathlen - namelen;
  14.296 +<a class="c2html-lineno" name="l240" href="#l240">240 </a>    <span class="c2html-keyword">char</span> *parent = malloc(parentlen + <span class="c2html-macroconst">1</span>);
  14.297 +<a class="c2html-lineno" name="l241" href="#l241">241 </a>    memcpy(parent, path, parentlen);
  14.298 +<a class="c2html-lineno" name="l242" href="#l242">242 </a>    parent[parentlen] = <span class="c2html-string">'\0'</span>;
  14.299 +<a class="c2html-lineno" name="l243" href="#l243">243 </a>    <span class="c2html-keyword">return</span> parent;
  14.300 +<a class="c2html-lineno" name="l244" href="#l244">244 </a>}
  14.301 +<a class="c2html-lineno" name="l245" href="#l245">245 </a>
  14.302 +<a class="c2html-lineno" name="l246" href="#l246">246 </a>
  14.303 +<a class="c2html-lineno" name="l247" href="#l247">247 </a><span class="c2html-keyword">char</span>* util_xml_get_text(xmlNode *elm) {
  14.304 +<a class="c2html-lineno" name="l248" href="#l248">248 </a>    xmlNode *node = elm-&gt;children;
  14.305 +<a class="c2html-lineno" name="l249" href="#l249">249 </a>    <span class="c2html-keyword">while</span>(node) {
  14.306 +<a class="c2html-lineno" name="l250" href="#l250">250 </a>        <span class="c2html-keyword">if</span>(node-&gt;type == <span class="c2html-macroconst">XML_TEXT_NODE</span>) {
  14.307 +<a class="c2html-lineno" name="l251" href="#l251">251 </a>            <span class="c2html-keyword">return</span> (<span class="c2html-keyword">char</span>*)node-&gt;content;
  14.308 +<a class="c2html-lineno" name="l252" href="#l252">252 </a>        }
  14.309 +<a class="c2html-lineno" name="l253" href="#l253">253 </a>        node = node-&gt;next;
  14.310 +<a class="c2html-lineno" name="l254" href="#l254">254 </a>    }
  14.311 +<a class="c2html-lineno" name="l255" href="#l255">255 </a>    <span class="c2html-keyword">return</span> <span class="c2html-macroconst">NULL</span>;
  14.312 +<a class="c2html-lineno" name="l256" href="#l256">256 </a>}
  14.313 +<a class="c2html-lineno" name="l257" href="#l257">257 </a>
  14.314 +<a class="c2html-lineno" name="l258" href="#l258">258 </a>
  14.315 +<a class="c2html-lineno" name="l259" href="#l259">259 </a><span class="c2html-keyword">char</span>* util_base64decode(<span class="c2html-keyword">char</span> *in) {
  14.316 +<a class="c2html-lineno" name="l260" href="#l260">260 </a>    <span class="c2html-keyword">int</span> len = <span class="c2html-macroconst">0</span>;
  14.317 +<a class="c2html-lineno" name="l261" href="#l261">261 </a>    <span class="c2html-keyword">return</span> util_base64decode_len(in, &len);
  14.318 +<a class="c2html-lineno" name="l262" href="#l262">262 </a>}
  14.319 +<a class="c2html-lineno" name="l263" href="#l263">263 </a>
  14.320 +<a class="c2html-lineno" name="l264" href="#l264">264 </a><span class="c2html-keyword">char</span>* util_base64decode_len(<span class="c2html-keyword">char</span>* in, <span class="c2html-keyword">int</span> *outlen) {
  14.321 +<a class="c2html-lineno" name="l265" href="#l265">265 </a>    <span class="c2html-type">size_t</span> len = strlen(in);
  14.322 +<a class="c2html-lineno" name="l266" href="#l266">266 </a>    <span class="c2html-keyword">char</span> *out = calloc(<span class="c2html-macroconst">1</span>, len);
  14.323 +<a class="c2html-lineno" name="l267" href="#l267">267 </a>    
  14.324 +<a class="c2html-lineno" name="l268" href="#l268">268 </a>    <span class="c2html-macroconst">BIO</span>* b = BIO_new_mem_buf(in, len);
  14.325 +<a class="c2html-lineno" name="l269" href="#l269">269 </a>    <span class="c2html-macroconst">BIO</span> *d = BIO_new(BIO_f_base64());
  14.326 +<a class="c2html-lineno" name="l270" href="#l270">270 </a>    BIO_set_flags(d, <span class="c2html-macroconst">BIO_FLAGS_BASE64_NO_NL</span>);
  14.327 +<a class="c2html-lineno" name="l271" href="#l271">271 </a>    b = BIO_push(d, b);
  14.328 +<a class="c2html-lineno" name="l272" href="#l272">272 </a>
  14.329 +<a class="c2html-lineno" name="l273" href="#l273">273 </a>    *outlen = BIO_read(b, out, len);
  14.330 +<a class="c2html-lineno" name="l274" href="#l274">274 </a>    BIO_free_all(b);
  14.331 +<a class="c2html-lineno" name="l275" href="#l275">275 </a>    
  14.332 +<a class="c2html-lineno" name="l276" href="#l276">276 </a>    <span class="c2html-keyword">return</span> out;
  14.333 +<a class="c2html-lineno" name="l277" href="#l277">277 </a>}
  14.334 +<a class="c2html-lineno" name="l278" href="#l278">278 </a>
  14.335 +<a class="c2html-lineno" name="l279" href="#l279">279 </a><span class="c2html-keyword">char</span>* util_base64encode(<span class="c2html-keyword">char</span> *in, <span class="c2html-type">size_t</span> len) { 
  14.336 +<a class="c2html-lineno" name="l280" href="#l280">280 </a>    <span class="c2html-macroconst">BIO</span> *b;
  14.337 +<a class="c2html-lineno" name="l281" href="#l281">281 </a>    <span class="c2html-macroconst">BIO</span> *e;
  14.338 +<a class="c2html-lineno" name="l282" href="#l282">282 </a>    <span class="c2html-macroconst">BUF_MEM</span> *mem;
  14.339 +<a class="c2html-lineno" name="l283" href="#l283">283 </a>
  14.340 +<a class="c2html-lineno" name="l284" href="#l284">284 </a>    e = BIO_new(BIO_f_base64());
  14.341 +<a class="c2html-lineno" name="l285" href="#l285">285 </a>    b = BIO_new(BIO_s_mem());
  14.342 +<a class="c2html-lineno" name="l286" href="#l286">286 </a>    
  14.343 +<a class="c2html-lineno" name="l287" href="#l287">287 </a>    e = BIO_push(e, b);
  14.344 +<a class="c2html-lineno" name="l288" href="#l288">288 </a>    BIO_write(e, in, len);
  14.345 +<a class="c2html-lineno" name="l289" href="#l289">289 </a>    BIO_flush(e);
  14.346 +<a class="c2html-lineno" name="l290" href="#l290">290 </a>    
  14.347 +<a class="c2html-lineno" name="l291" href="#l291">291 </a>    BIO_get_mem_ptr(e, &mem);
  14.348 +<a class="c2html-lineno" name="l292" href="#l292">292 </a>    <span class="c2html-keyword">char</span> *out = malloc(mem-&gt;length);
  14.349 +<a class="c2html-lineno" name="l293" href="#l293">293 </a>    memcpy(out, mem-&gt;data, mem-&gt;length -<span class="c2html-macroconst">1</span>);
  14.350 +<a class="c2html-lineno" name="l294" href="#l294">294 </a>    out[mem-&gt;length - <span class="c2html-macroconst">1</span>] = <span class="c2html-string">'\0'</span>;
  14.351 +<a class="c2html-lineno" name="l295" href="#l295">295 </a>
  14.352 +<a class="c2html-lineno" name="l296" href="#l296">296 </a>    BIO_free_all(e);
  14.353 +<a class="c2html-lineno" name="l297" href="#l297">297 </a>
  14.354 +<a class="c2html-lineno" name="l298" href="#l298">298 </a>    <span class="c2html-keyword">return</span> out;
  14.355 +<a class="c2html-lineno" name="l299" href="#l299">299 </a>}
  14.356 +<a class="c2html-lineno" name="l300" href="#l300">300 </a>
  14.357 +<a class="c2html-lineno" name="l301" href="#l301">301 </a><span class="c2html-keyword">char</span>* util_encrypt_str(DavSession *sn, <span class="c2html-keyword">char</span> *str, <span class="c2html-keyword">char</span> *key) {
  14.358 +<a class="c2html-lineno" name="l302" href="#l302">302 </a>    DavKey *k = dav_context_get_key(sn-&gt;context, key);
  14.359 +<a class="c2html-lineno" name="l303" href="#l303">303 </a>    <span class="c2html-keyword">if</span>(!k) {
  14.360 +<a class="c2html-lineno" name="l304" href="#l304">304 </a>        <span class="c2html-comment">// TODO: session error</span>
  14.361 +<a class="c2html-lineno" name="l305" href="#l305">305 </a>        <span class="c2html-keyword">return</span> <span class="c2html-macroconst">NULL</span>;
  14.362 +<a class="c2html-lineno" name="l306" href="#l306">306 </a>    }
  14.363 +<a class="c2html-lineno" name="l307" href="#l307">307 </a>    
  14.364 +<a class="c2html-lineno" name="l308" href="#l308">308 </a>    <span class="c2html-keyword">char</span> *enc_str = aes_encrypt(str, k);
  14.365 +<a class="c2html-lineno" name="l309" href="#l309">309 </a>    <span class="c2html-keyword">char</span> *ret_str = dav_session_strdup(sn, enc_str);
  14.366 +<a class="c2html-lineno" name="l310" href="#l310">310 </a>    free(enc_str);
  14.367 +<a class="c2html-lineno" name="l311" href="#l311">311 </a>    <span class="c2html-keyword">return</span> ret_str;
  14.368 +<a class="c2html-lineno" name="l312" href="#l312">312 </a>}
  14.369 +<a class="c2html-lineno" name="l313" href="#l313">313 </a>
  14.370 +<a class="c2html-lineno" name="l314" href="#l314">314 </a><span class="c2html-comment">/* commented out for testing reasons */</span>
  14.371 +<a class="c2html-lineno" name="l315" href="#l315">315 </a><span class="c2html-comment">/*</span>
  14.372 +<a class="c2html-lineno" name="l316" href="#l316">316 </a><span class="c2html-comment">char* util_decrypt_str(DavSession *sn, char *str, char *key) {</span>
  14.373 +<a class="c2html-lineno" name="l317" href="#l317">317 </a><span class="c2html-comment">    DavKey *k = dav_context_get_key(sn-&gt;context, key);</span>
  14.374 +<a class="c2html-lineno" name="l318" href="#l318">318 </a><span class="c2html-comment">    if(!k) {</span>
  14.375 +<a class="c2html-lineno" name="l319" href="#l319">319 </a><span class="c2html-comment">        // TODO: session error</span>
  14.376 +<a class="c2html-lineno" name="l320" href="#l320">320 </a><span class="c2html-comment">        return NULL;</span>
  14.377 +<a class="c2html-lineno" name="l321" href="#l321">321 </a><span class="c2html-comment">    }</span>
  14.378 +<a class="c2html-lineno" name="l322" href="#l322">322 </a><span class="c2html-comment">    </span>
  14.379 +<a class="c2html-lineno" name="l323" href="#l323">323 </a><span class="c2html-comment">    char *dec_str = aes_decrypt(str, k);</span>
  14.380 +<a class="c2html-lineno" name="l324" href="#l324">324 </a><span class="c2html-comment">    char *ret_str = dav_session_strdup(sn, dec_str);</span>
  14.381 +<a class="c2html-lineno" name="l325" href="#l325">325 </a><span class="c2html-comment">    free(dec_str);</span>
  14.382 +<a class="c2html-lineno" name="l326" href="#l326">326 </a><span class="c2html-comment">    return ret_str;</span>
  14.383 +<a class="c2html-lineno" name="l327" href="#l327">327 </a><span class="c2html-comment">}</span>
  14.384 +<a class="c2html-lineno" name="l328" href="#l328">328 </a><span class="c2html-comment">*/</span>
  14.385 +<a class="c2html-lineno" name="l329" href="#l329">329 </a><span class="c2html-keyword">char</span>* util_random_str() {
  14.386 +<a class="c2html-lineno" name="l330" href="#l330">330 </a>    <span class="c2html-keyword">unsigned</span> <span class="c2html-keyword">char</span> *str = malloc(<span class="c2html-macroconst">25</span>);
  14.387 +<a class="c2html-lineno" name="l331" href="#l331">331 </a>    str[<span class="c2html-macroconst">24</span>] = <span class="c2html-string">'\0'</span>;
  14.388 +<a class="c2html-lineno" name="l332" href="#l332">332 </a>    
  14.389 +<a class="c2html-lineno" name="l333" href="#l333">333 </a>    <span class="c2html-type">sstr_t</span> t = <span class="c2html-macroconst">S</span>(
  14.390 +<a class="c2html-lineno" name="l334" href="#l334">334 </a>            <span class="c2html-string">"01234567890"</span>
  14.391 +<a class="c2html-lineno" name="l335" href="#l335">335 </a>            <span class="c2html-string">"abcdefghijklmnopqrstuvwxyz"</span>
  14.392 +<a class="c2html-lineno" name="l336" href="#l336">336 </a>            <span class="c2html-string">"ABCDEFGHIJKLMNOPQRSTUVWXYZ"</span>);
  14.393 +<a class="c2html-lineno" name="l337" href="#l337">337 </a>    <span class="c2html-keyword">const</span> <span class="c2html-keyword">unsigned</span> <span class="c2html-keyword">char</span> *table = (<span class="c2html-keyword">const</span> <span class="c2html-keyword">unsigned</span> <span class="c2html-keyword">char</span>*)t.ptr;
  14.394 +<a class="c2html-lineno" name="l338" href="#l338">338 </a>    
  14.395 +<a class="c2html-lineno" name="l339" href="#l339">339 </a>    RAND_pseudo_bytes(str, <span class="c2html-macroconst">24</span>);
  14.396 +<a class="c2html-lineno" name="l340" href="#l340">340 </a>    <span class="c2html-keyword">for</span>(<span class="c2html-keyword">int</span> i=<span class="c2html-macroconst">0</span>;i&lt;<span class="c2html-macroconst">24</span>;i++) {
  14.397 +<a class="c2html-lineno" name="l341" href="#l341">341 </a>        <span class="c2html-keyword">int</span> c = str[i] % t.length;
  14.398 +<a class="c2html-lineno" name="l342" href="#l342">342 </a>        str[i] = table[c];
  14.399 +<a class="c2html-lineno" name="l343" href="#l343">343 </a>    }
  14.400 +<a class="c2html-lineno" name="l344" href="#l344">344 </a>    
  14.401 +<a class="c2html-lineno" name="l345" href="#l345">345 </a>    <span class="c2html-keyword">return</span> (<span class="c2html-keyword">char</span>*)str;
  14.402 +<a class="c2html-lineno" name="l346" href="#l346">346 </a>}
  14.403 +<a class="c2html-lineno" name="l347" href="#l347">347 </a>
  14.404 +<a class="c2html-lineno" name="l348" href="#l348">348 </a><span class="c2html-comment">/*</span>
  14.405 +<a class="c2html-lineno" name="l349" href="#l349">349 </a><span class="c2html-comment"> * gets a substring from 0 to the appearance of the token</span>
  14.406 +<a class="c2html-lineno" name="l350" href="#l350">350 </a><span class="c2html-comment"> * tokens are separated by space</span>
  14.407 +<a class="c2html-lineno" name="l351" href="#l351">351 </a><span class="c2html-comment"> * sets sub to the substring and returns the remaining string</span>
  14.408 +<a class="c2html-lineno" name="l352" href="#l352">352 </a><span class="c2html-comment"> */</span>
  14.409 +<a class="c2html-lineno" name="l353" href="#l353">353 </a><span class="c2html-type">sstr_t</span> util_getsubstr_until_token(<span class="c2html-type">sstr_t</span> str, <span class="c2html-type">sstr_t</span> token, <span class="c2html-type">sstr_t</span> *sub) {  
  14.410 +<a class="c2html-lineno" name="l354" href="#l354">354 </a>    <span class="c2html-keyword">int</span> i;
  14.411 +<a class="c2html-lineno" name="l355" href="#l355">355 </a>    <span class="c2html-keyword">int</span> token_start = -<span class="c2html-macroconst">1</span>;
  14.412 +<a class="c2html-lineno" name="l356" href="#l356">356 </a>    <span class="c2html-keyword">int</span> token_end = -<span class="c2html-macroconst">1</span>;
  14.413 +<a class="c2html-lineno" name="l357" href="#l357">357 </a>    <span class="c2html-keyword">for</span>(i=<span class="c2html-macroconst">0</span>;i&lt;=str.length;i++) {
  14.414 +<a class="c2html-lineno" name="l358" href="#l358">358 </a>        <span class="c2html-keyword">int</span> c;
  14.415 +<a class="c2html-lineno" name="l359" href="#l359">359 </a>        <span class="c2html-keyword">if</span>(i == str.length) {
  14.416 +<a class="c2html-lineno" name="l360" href="#l360">360 </a>            c = <span class="c2html-string">' '</span>;
  14.417 +<a class="c2html-lineno" name="l361" href="#l361">361 </a>        } <span class="c2html-keyword">else</span> {
  14.418 +<a class="c2html-lineno" name="l362" href="#l362">362 </a>            c = str.ptr[i];
  14.419 +<a class="c2html-lineno" name="l363" href="#l363">363 </a>        }
  14.420 +<a class="c2html-lineno" name="l364" href="#l364">364 </a>        <span class="c2html-keyword">if</span>(c &lt; <span class="c2html-macroconst">33</span>) {
  14.421 +<a class="c2html-lineno" name="l365" href="#l365">365 </a>            <span class="c2html-keyword">if</span>(token_start != -<span class="c2html-macroconst">1</span>) {
  14.422 +<a class="c2html-lineno" name="l366" href="#l366">366 </a>                token_end = i;
  14.423 +<a class="c2html-lineno" name="l367" href="#l367">367 </a>                <span class="c2html-type">size_t</span> len = token_end - token_start;
  14.424 +<a class="c2html-lineno" name="l368" href="#l368">368 </a>                <span class="c2html-type">sstr_t</span> tk = sstrsubsl(str, token_start, len);
  14.425 +<a class="c2html-lineno" name="l369" href="#l369">369 </a>                <span class="c2html-comment">//printf("token: {%.*s}\n", token.length, token.ptr);</span>
  14.426 +<a class="c2html-lineno" name="l370" href="#l370">370 </a>                <span class="c2html-keyword">if</span>(!sstrcmp(tk, token)) {
  14.427 +<a class="c2html-lineno" name="l371" href="#l371">371 </a>                    *sub = sstrtrim(sstrsubsl(str, <span class="c2html-macroconst">0</span>, token_start));
  14.428 +<a class="c2html-lineno" name="l372" href="#l372">372 </a>                    <span class="c2html-keyword">break</span>;
  14.429 +<a class="c2html-lineno" name="l373" href="#l373">373 </a>                }
  14.430 +<a class="c2html-lineno" name="l374" href="#l374">374 </a>                token_start = -<span class="c2html-macroconst">1</span>;
  14.431 +<a class="c2html-lineno" name="l375" href="#l375">375 </a>                token_end = -<span class="c2html-macroconst">1</span>;
  14.432 +<a class="c2html-lineno" name="l376" href="#l376">376 </a>            }
  14.433 +<a class="c2html-lineno" name="l377" href="#l377">377 </a>        } <span class="c2html-keyword">else</span> {
  14.434 +<a class="c2html-lineno" name="l378" href="#l378">378 </a>            <span class="c2html-keyword">if</span>(token_start == -<span class="c2html-macroconst">1</span>) {
  14.435 +<a class="c2html-lineno" name="l379" href="#l379">379 </a>                token_start = i;
  14.436 +<a class="c2html-lineno" name="l380" href="#l380">380 </a>            }
  14.437 +<a class="c2html-lineno" name="l381" href="#l381">381 </a>        }
  14.438 +<a class="c2html-lineno" name="l382" href="#l382">382 </a>    }
  14.439 +<a class="c2html-lineno" name="l383" href="#l383">383 </a>    
  14.440 +<a class="c2html-lineno" name="l384" href="#l384">384 </a>    <span class="c2html-keyword">if</span>(i &lt; str.length) {
  14.441 +<a class="c2html-lineno" name="l385" href="#l385">385 </a>        <span class="c2html-keyword">return</span> sstrtrim(sstrsubs(str, i));
  14.442 +<a class="c2html-lineno" name="l386" href="#l386">386 </a>    } <span class="c2html-keyword">else</span> {
  14.443 +<a class="c2html-lineno" name="l387" href="#l387">387 </a>        str.ptr = <span class="c2html-macroconst">NULL</span>;
  14.444 +<a class="c2html-lineno" name="l388" href="#l388">388 </a>        str.length = <span class="c2html-macroconst">0</span>;
  14.445 +<a class="c2html-lineno" name="l389" href="#l389">389 </a>        <span class="c2html-keyword">return</span> str;
  14.446 +<a class="c2html-lineno" name="l390" href="#l390">390 </a>    }
  14.447 +<a class="c2html-lineno" name="l391" href="#l391">391 </a>}
  14.448 +</div>
  14.449 +  </body>
  14.450 +</html>
  14.451 +
    15.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
    15.2 +++ b/test/gs/javatest.html	Mon Apr 24 21:01:41 2023 +0200
    15.3 @@ -0,0 +1,225 @@
    15.4 +<!DOCTYPE html>
    15.5 +<html>
    15.6 +  <head>
    15.7 +    <title>c2html</title>
    15.8 +    <style type="text/css">
    15.9 +      div.c2html-code {
   15.10 +        white-space: pre;
   15.11 +        font-family: monospace;
   15.12 +      }
   15.13 +      a.c2html-lineno {
   15.14 +        /* as long as user-select isn't widely spread, we throw the bomb */
   15.15 +        -webkit-user-select: none;
   15.16 +        -moz-user-select: none;
   15.17 +        -ms-user-select: none;
   15.18 +        user-select: none;
   15.19 +        display: inline-block;
   15.20 +        font-style: italic;
   15.21 +        text-decoration: none;
   15.22 +        color: grey;
   15.23 +      }
   15.24 +      span.c2html-keyword {
   15.25 +        color: blue;
   15.26 +      }
   15.27 +      span.c2html-macroconst {
   15.28 +        color: cornflowerblue;
   15.29 +      }
   15.30 +      span.c2html-type {
   15.31 +        color: teal;
   15.32 +      }
   15.33 +      span.c2html-directive {
   15.34 +        color: silver;
   15.35 +      }
   15.36 +      span.c2html-string {
   15.37 +        color: darkorange;
   15.38 +      }
   15.39 +      span.c2html-comment {
   15.40 +        color: grey;
   15.41 +      }
   15.42 +      span.c2html-stdinclude, span.c2html-userinclude, a.c2html-userinclude {
   15.43 +      }
   15.44 +    </style>
   15.45 +  </head>
   15.46 +  <body>
   15.47 +
   15.48 +<div class="c2html-code">
   15.49 +<a class="c2html-lineno" name="l1" href="#l1">  1 </a><span class="c2html-comment">/*</span>
   15.50 +<a class="c2html-lineno" name="l2" href="#l2">  2 </a><span class="c2html-comment"> * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.</span>
   15.51 +<a class="c2html-lineno" name="l3" href="#l3">  3 </a><span class="c2html-comment"> *</span>
   15.52 +<a class="c2html-lineno" name="l4" href="#l4">  4 </a><span class="c2html-comment"> * Copyright 2014 Mike Becker. All rights reserved.</span>
   15.53 +<a class="c2html-lineno" name="l5" href="#l5">  5 </a><span class="c2html-comment"> *</span>
   15.54 +<a class="c2html-lineno" name="l6" href="#l6">  6 </a><span class="c2html-comment"> * Redistribution and use in source and binary forms, with or without</span>
   15.55 +<a class="c2html-lineno" name="l7" href="#l7">  7 </a><span class="c2html-comment"> * modification, are permitted provided that the following conditions are met:</span>
   15.56 +<a class="c2html-lineno" name="l8" href="#l8">  8 </a><span class="c2html-comment"> *</span>
   15.57 +<a class="c2html-lineno" name="l9" href="#l9">  9 </a><span class="c2html-comment"> *   1. Redistributions of source code must retain the above copyright</span>
   15.58 +<a class="c2html-lineno" name="l10" href="#l10"> 10 </a><span class="c2html-comment"> *      notice, this list of conditions and the following disclaimer.</span>
   15.59 +<a class="c2html-lineno" name="l11" href="#l11"> 11 </a><span class="c2html-comment"> *</span>
   15.60 +<a class="c2html-lineno" name="l12" href="#l12"> 12 </a><span class="c2html-comment"> *   2. Redistributions in binary form must reproduce the above copyright</span>
   15.61 +<a class="c2html-lineno" name="l13" href="#l13"> 13 </a><span class="c2html-comment"> *      notice, this list of conditions and the following disclaimer in the</span>
   15.62 +<a class="c2html-lineno" name="l14" href="#l14"> 14 </a><span class="c2html-comment"> *      documentation and/or other materials provided with the distribution.</span>
   15.63 +<a class="c2html-lineno" name="l15" href="#l15"> 15 </a><span class="c2html-comment"> *</span>
   15.64 +<a class="c2html-lineno" name="l16" href="#l16"> 16 </a><span class="c2html-comment"> * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"</span>
   15.65 +<a class="c2html-lineno" name="l17" href="#l17"> 17 </a><span class="c2html-comment"> * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE</span>
   15.66 +<a class="c2html-lineno" name="l18" href="#l18"> 18 </a><span class="c2html-comment"> * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE</span>
   15.67 +<a class="c2html-lineno" name="l19" href="#l19"> 19 </a><span class="c2html-comment"> * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE</span>
   15.68 +<a class="c2html-lineno" name="l20" href="#l20"> 20 </a><span class="c2html-comment"> * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR</span>
   15.69 +<a class="c2html-lineno" name="l21" href="#l21"> 21 </a><span class="c2html-comment"> * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF</span>
   15.70 +<a class="c2html-lineno" name="l22" href="#l22"> 22 </a><span class="c2html-comment"> * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS</span>
   15.71 +<a class="c2html-lineno" name="l23" href="#l23"> 23 </a><span class="c2html-comment"> * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN</span>
   15.72 +<a class="c2html-lineno" name="l24" href="#l24"> 24 </a><span class="c2html-comment"> * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)</span>
   15.73 +<a class="c2html-lineno" name="l25" href="#l25"> 25 </a><span class="c2html-comment"> * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE</span>
   15.74 +<a class="c2html-lineno" name="l26" href="#l26"> 26 </a><span class="c2html-comment"> * POSSIBILITY OF SUCH DAMAGE.</span>
   15.75 +<a class="c2html-lineno" name="l27" href="#l27"> 27 </a><span class="c2html-comment"> *</span>
   15.76 +<a class="c2html-lineno" name="l28" href="#l28"> 28 </a><span class="c2html-comment"> */</span>
   15.77 +<a class="c2html-lineno" name="l29" href="#l29"> 29 </a>
   15.78 +<a class="c2html-lineno" name="l30" href="#l30"> 30 </a><span class="c2html-keyword">package</span> de.uapcore.sigred.doc.base;
   15.79 +<a class="c2html-lineno" name="l31" href="#l31"> 31 </a>
   15.80 +<a class="c2html-lineno" name="l32" href="#l32"> 32 </a><span class="c2html-keyword">import</span> de.uapcore.sigred.doc.<span class="c2html-type">Resources</span>;
   15.81 +<a class="c2html-lineno" name="l33" href="#l33"> 33 </a><span class="c2html-keyword">import</span> de.uapcore.sigrapi.impl.<span class="c2html-type">Digraph</span>;
   15.82 +<a class="c2html-lineno" name="l34" href="#l34"> 34 </a><span class="c2html-keyword">import</span> de.uapcore.sigrapi.impl.<span class="c2html-type">Graph</span>;
   15.83 +<a class="c2html-lineno" name="l35" href="#l35"> 35 </a><span class="c2html-keyword">import</span> de.uapcore.sigrapi.<span class="c2html-type">IGraph</span>;
   15.84 +<a class="c2html-lineno" name="l36" href="#l36"> 36 </a><span class="c2html-keyword">import</span> java.io.<span class="c2html-type">IOException</span>;
   15.85 +<a class="c2html-lineno" name="l37" href="#l37"> 37 </a><span class="c2html-keyword">import</span> java.io.<span class="c2html-type">InputStream</span>;
   15.86 +<a class="c2html-lineno" name="l38" href="#l38"> 38 </a><span class="c2html-keyword">import</span> java.io.<span class="c2html-type">OutputStream</span>;
   15.87 +<a class="c2html-lineno" name="l39" href="#l39"> 39 </a><span class="c2html-keyword">import</span> java.util.concurrent.atomic.<span class="c2html-type">AtomicBoolean</span>;
   15.88 +<a class="c2html-lineno" name="l40" href="#l40"> 40 </a><span class="c2html-keyword">import</span> java.util.concurrent.atomic.<span class="c2html-type">AtomicReference</span>;
   15.89 +<a class="c2html-lineno" name="l41" href="#l41"> 41 </a><span class="c2html-keyword">import</span> org.apache.xerces.impl.<span class="c2html-type">Constants</span>;
   15.90 +<a class="c2html-lineno" name="l42" href="#l42"> 42 </a><span class="c2html-keyword">import</span> org.dom4j.<span class="c2html-type">Document</span>;
   15.91 +<a class="c2html-lineno" name="l43" href="#l43"> 43 </a><span class="c2html-keyword">import</span> org.dom4j.<span class="c2html-type">DocumentException</span>;
   15.92 +<a class="c2html-lineno" name="l44" href="#l44"> 44 </a><span class="c2html-keyword">import</span> org.dom4j.<span class="c2html-type">DocumentHelper</span>;
   15.93 +<a class="c2html-lineno" name="l45" href="#l45"> 45 </a><span class="c2html-keyword">import</span> org.dom4j.<span class="c2html-type">Element</span>;
   15.94 +<a class="c2html-lineno" name="l46" href="#l46"> 46 </a><span class="c2html-keyword">import</span> org.dom4j.<span class="c2html-type">Namespace</span>;
   15.95 +<a class="c2html-lineno" name="l47" href="#l47"> 47 </a><span class="c2html-keyword">import</span> org.dom4j.<span class="c2html-type">QName</span>;
   15.96 +<a class="c2html-lineno" name="l48" href="#l48"> 48 </a><span class="c2html-keyword">import</span> org.dom4j.io.<span class="c2html-type">OutputFormat</span>;
   15.97 +<a class="c2html-lineno" name="l49" href="#l49"> 49 </a><span class="c2html-keyword">import</span> org.dom4j.io.<span class="c2html-type">SAXReader</span>;
   15.98 +<a class="c2html-lineno" name="l50" href="#l50"> 50 </a><span class="c2html-keyword">import</span> org.dom4j.io.<span class="c2html-type">XMLWriter</span>;
   15.99 +<a class="c2html-lineno" name="l51" href="#l51"> 51 </a><span class="c2html-keyword">import</span> org.xml.sax.<span class="c2html-type">ErrorHandler</span>;
  15.100 +<a class="c2html-lineno" name="l52" href="#l52"> 52 </a><span class="c2html-keyword">import</span> org.xml.sax.<span class="c2html-type">SAXException</span>;
  15.101 +<a class="c2html-lineno" name="l53" href="#l53"> 53 </a><span class="c2html-keyword">import</span> org.xml.sax.<span class="c2html-type">SAXParseException</span>;
  15.102 +<a class="c2html-lineno" name="l54" href="#l54"> 54 </a>
  15.103 +<a class="c2html-lineno" name="l55" href="#l55"> 55 </a><span class="c2html-keyword">public</span> <span class="c2html-keyword">abstract</span> <span class="c2html-keyword">class</span> <span class="c2html-type">AbstractGraphDocument</span>&lt;<span class="c2html-type">T</span> <span class="c2html-keyword">extends</span> <span class="c2html-type">IGraph</span>&gt;
  15.104 +<a class="c2html-lineno" name="l56" href="#l56"> 56 </a>        <span class="c2html-keyword">extends</span> <span class="c2html-type">FileBackedDocument</span> {
  15.105 +<a class="c2html-lineno" name="l57" href="#l57"> 57 </a>    
  15.106 +<a class="c2html-lineno" name="l58" href="#l58"> 58 </a>    <span class="c2html-keyword">protected</span> <span class="c2html-keyword">static</span> <span class="c2html-keyword">final</span> <span class="c2html-type">Namespace</span> <span class="c2html-type">NAMESPACE</span> = <span class="c2html-type">Namespace</span>.get(<span class="c2html-string">"sigred"</span>,
  15.107 +<a class="c2html-lineno" name="l59" href="#l59"> 59 </a>        <span class="c2html-string">"http://develop.uap-core.de/sigred/"</span>);
  15.108 +<a class="c2html-lineno" name="l60" href="#l60"> 60 </a>    
  15.109 +<a class="c2html-lineno" name="l61" href="#l61"> 61 </a>    <span class="c2html-keyword">private</span> <span class="c2html-keyword">static</span> <span class="c2html-keyword">final</span>
  15.110 +<a class="c2html-lineno" name="l62" href="#l62"> 62 </a>        <span class="c2html-type">QName</span> <span class="c2html-type">TAG_GRAPHDOC</span> = <span class="c2html-type">QName</span>.get(<span class="c2html-string">"graph-document"</span>, <span class="c2html-type">NAMESPACE</span>);
  15.111 +<a class="c2html-lineno" name="l63" href="#l63"> 63 </a>    <span class="c2html-keyword">private</span> <span class="c2html-keyword">static</span> <span class="c2html-keyword">final</span>
  15.112 +<a class="c2html-lineno" name="l64" href="#l64"> 64 </a>        <span class="c2html-type">QName</span> <span class="c2html-type">TAG_GRAPH</span> = <span class="c2html-type">QName</span>.get(<span class="c2html-string">"graph"</span>, <span class="c2html-type">NAMESPACE</span>);
  15.113 +<a class="c2html-lineno" name="l65" href="#l65"> 65 </a>    <span class="c2html-keyword">private</span> <span class="c2html-keyword">static</span> <span class="c2html-keyword">final</span>
  15.114 +<a class="c2html-lineno" name="l66" href="#l66"> 66 </a>        <span class="c2html-type">QName</span> <span class="c2html-type">TAG_DIGRAPH</span> = <span class="c2html-type">QName</span>.get(<span class="c2html-string">"digraph"</span>, <span class="c2html-type">NAMESPACE</span>);
  15.115 +<a class="c2html-lineno" name="l67" href="#l67"> 67 </a>    <span class="c2html-keyword">private</span> <span class="c2html-keyword">static</span> <span class="c2html-keyword">final</span>
  15.116 +<a class="c2html-lineno" name="l68" href="#l68"> 68 </a>        <span class="c2html-type">QName</span> <span class="c2html-type">TAG_METADATA</span> = <span class="c2html-type">QName</span>.get(<span class="c2html-string">"metadata"</span>, <span class="c2html-type">NAMESPACE</span>);
  15.117 +<a class="c2html-lineno" name="l69" href="#l69"> 69 </a>    
  15.118 +<a class="c2html-lineno" name="l70" href="#l70"> 70 </a>    <span class="c2html-keyword">protected</span> <span class="c2html-keyword">final</span> <span class="c2html-type">T</span> graph;
  15.119 +<a class="c2html-lineno" name="l71" href="#l71"> 71 </a>    
  15.120 +<a class="c2html-lineno" name="l72" href="#l72"> 72 </a>    <span class="c2html-keyword">private</span> <span class="c2html-keyword">final</span> <span class="c2html-type">GraphDocumentMetadata</span> metadata;
  15.121 +<a class="c2html-lineno" name="l73" href="#l73"> 73 </a>    
  15.122 +<a class="c2html-lineno" name="l74" href="#l74"> 74 </a>    <span class="c2html-keyword">public</span> <span class="c2html-type">AbstractGraphDocument</span>(<span class="c2html-type">Class</span>&lt;<span class="c2html-type">T</span>&gt; graphType) {
  15.123 +<a class="c2html-lineno" name="l75" href="#l75"> 75 </a>        <span class="c2html-type">T</span> g;
  15.124 +<a class="c2html-lineno" name="l76" href="#l76"> 76 </a>        <span class="c2html-keyword">try</span> {
  15.125 +<a class="c2html-lineno" name="l77" href="#l77"> 77 </a>            g = graphType.newInstance();
  15.126 +<a class="c2html-lineno" name="l78" href="#l78"> 78 </a>        } <span class="c2html-keyword">catch</span> (<span class="c2html-type">ReflectiveOperationException</span> e) {
  15.127 +<a class="c2html-lineno" name="l79" href="#l79"> 79 </a>            <span class="c2html-keyword">assert</span> false;
  15.128 +<a class="c2html-lineno" name="l80" href="#l80"> 80 </a>            g = null; <span class="c2html-comment">// for the compiler</span>
  15.129 +<a class="c2html-lineno" name="l81" href="#l81"> 81 </a>        }
  15.130 +<a class="c2html-lineno" name="l82" href="#l82"> 82 </a>        graph = g;
  15.131 +<a class="c2html-lineno" name="l83" href="#l83"> 83 </a>        metadata = <span class="c2html-keyword">new</span> <span class="c2html-type">GraphDocumentMetadata</span>();
  15.132 +<a class="c2html-lineno" name="l84" href="#l84"> 84 </a>    }
  15.133 +<a class="c2html-lineno" name="l85" href="#l85"> 85 </a>
  15.134 +<a class="c2html-lineno" name="l86" href="#l86"> 86 </a>    <span class="c2html-keyword">public</span> <span class="c2html-type">T</span> getGraph() {
  15.135 +<a class="c2html-lineno" name="l87" href="#l87"> 87 </a>        <span class="c2html-keyword">return</span> graph;
  15.136 +<a class="c2html-lineno" name="l88" href="#l88"> 88 </a>    }
  15.137 +<a class="c2html-lineno" name="l89" href="#l89"> 89 </a>    
  15.138 +<a class="c2html-lineno" name="l90" href="#l90"> 90 </a>    <span class="c2html-keyword">public</span> <span class="c2html-type">GraphDocumentMetadata</span> getMetadata() {
  15.139 +<a class="c2html-lineno" name="l91" href="#l91"> 91 </a>        <span class="c2html-keyword">return</span> metadata;
  15.140 +<a class="c2html-lineno" name="l92" href="#l92"> 92 </a>    }
  15.141 +<a class="c2html-lineno" name="l93" href="#l93"> 93 </a>
  15.142 +<a class="c2html-lineno" name="l94" href="#l94"> 94 </a>    <span class="c2html-keyword">protected</span> <span class="c2html-keyword">abstract</span> <span class="c2html-keyword">void</span> writeGraph(<span class="c2html-type">Element</span> rootNode) <span class="c2html-keyword">throws</span> <span class="c2html-type">IOException</span>;
  15.143 +<a class="c2html-lineno" name="l95" href="#l95"> 95 </a>    <span class="c2html-keyword">protected</span> <span class="c2html-keyword">abstract</span> <span class="c2html-keyword">void</span> readGraph(<span class="c2html-type">Element</span> rootNode) <span class="c2html-keyword">throws</span> <span class="c2html-type">IOException</span>;
  15.144 +<a class="c2html-lineno" name="l96" href="#l96"> 96 </a>
  15.145 +<a class="c2html-lineno" name="l97" href="#l97"> 97 </a>    <span class="c2html-directive">@Override</span>
  15.146 +<a class="c2html-lineno" name="l98" href="#l98"> 98 </a>    <span class="c2html-keyword">public</span> <span class="c2html-keyword">void</span> writeTo(<span class="c2html-type">OutputStream</span> out) <span class="c2html-keyword">throws</span> <span class="c2html-type">IOException</span> {
  15.147 +<a class="c2html-lineno" name="l99" href="#l99"> 99 </a>        <span class="c2html-type">Document</span> doc = <span class="c2html-type">DocumentHelper</span>.createDocument();
  15.148 +<a class="c2html-lineno" name="l100" href="#l100">100 </a>
  15.149 +<a class="c2html-lineno" name="l101" href="#l101">101 </a>        <span class="c2html-type">Element</span> rootNode = doc.addElement(<span class="c2html-type">TAG_GRAPHDOC</span>);
  15.150 +<a class="c2html-lineno" name="l102" href="#l102">102 </a>
  15.151 +<a class="c2html-lineno" name="l103" href="#l103">103 </a>        <span class="c2html-type">Element</span> metadataNode = rootNode.addElement(<span class="c2html-type">TAG_METADATA</span>);
  15.152 +<a class="c2html-lineno" name="l104" href="#l104">104 </a>
  15.153 +<a class="c2html-lineno" name="l105" href="#l105">105 </a>        metadata.write(metadataNode);
  15.154 +<a class="c2html-lineno" name="l106" href="#l106">106 </a>
  15.155 +<a class="c2html-lineno" name="l107" href="#l107">107 </a>        <span class="c2html-keyword">if</span> (graph <span class="c2html-keyword">instanceof</span> <span class="c2html-type">Graph</span>) {
  15.156 +<a class="c2html-lineno" name="l108" href="#l108">108 </a>            writeGraph(rootNode.addElement(<span class="c2html-type">TAG_GRAPH</span>));
  15.157 +<a class="c2html-lineno" name="l109" href="#l109">109 </a>        } <span class="c2html-keyword">else</span> <span class="c2html-keyword">if</span> (graph <span class="c2html-keyword">instanceof</span> <span class="c2html-type">Digraph</span>) {
  15.158 +<a class="c2html-lineno" name="l110" href="#l110">110 </a>            writeGraph(rootNode.addElement(<span class="c2html-type">TAG_DIGRAPH</span>));
  15.159 +<a class="c2html-lineno" name="l111" href="#l111">111 </a>        } <span class="c2html-keyword">else</span> {
  15.160 +<a class="c2html-lineno" name="l112" href="#l112">112 </a>            <span class="c2html-keyword">throw</span> <span class="c2html-keyword">new</span> <span class="c2html-type">IOException</span>(<span class="c2html-string">"unsupported graph type"</span>);
  15.161 +<a class="c2html-lineno" name="l113" href="#l113">113 </a>        }
  15.162 +<a class="c2html-lineno" name="l114" href="#l114">114 </a>
  15.163 +<a class="c2html-lineno" name="l115" href="#l115">115 </a>        <span class="c2html-type">XMLWriter</span> writer = <span class="c2html-keyword">new</span> <span class="c2html-type">XMLWriter</span>(out, <span class="c2html-type">OutputFormat</span>.createPrettyPrint());
  15.164 +<a class="c2html-lineno" name="l116" href="#l116">116 </a>        writer.write(doc);
  15.165 +<a class="c2html-lineno" name="l117" href="#l117">117 </a>        writer.flush();
  15.166 +<a class="c2html-lineno" name="l118" href="#l118">118 </a>    }
  15.167 +<a class="c2html-lineno" name="l119" href="#l119">119 </a>
  15.168 +<a class="c2html-lineno" name="l120" href="#l120">120 </a>    <span class="c2html-directive">@Override</span>
  15.169 +<a class="c2html-lineno" name="l121" href="#l121">121 </a>    <span class="c2html-keyword">public</span> <span class="c2html-keyword">void</span> readFrom(<span class="c2html-type">InputStream</span> in) <span class="c2html-keyword">throws</span> <span class="c2html-type">IOException</span> {
  15.170 +<a class="c2html-lineno" name="l122" href="#l122">122 </a>        <span class="c2html-keyword">try</span> {
  15.171 +<a class="c2html-lineno" name="l123" href="#l123">123 </a>            <span class="c2html-type">SAXReader</span> reader = <span class="c2html-keyword">new</span> <span class="c2html-type">SAXReader</span>(true);
  15.172 +<a class="c2html-lineno" name="l124" href="#l124">124 </a>            reader.setStripWhitespaceText(true);
  15.173 +<a class="c2html-lineno" name="l125" href="#l125">125 </a>            
  15.174 +<a class="c2html-lineno" name="l126" href="#l126">126 </a>            reader.setFeature(<span class="c2html-type">Constants</span>.<span class="c2html-type">XERCES_FEATURE_PREFIX</span>+
  15.175 +<a class="c2html-lineno" name="l127" href="#l127">127 </a>                <span class="c2html-type">Constants</span>.<span class="c2html-type">SCHEMA_VALIDATION_FEATURE</span>, true);
  15.176 +<a class="c2html-lineno" name="l128" href="#l128">128 </a>            reader.setProperty(<span class="c2html-type">Constants</span>.<span class="c2html-type">XERCES_PROPERTY_PREFIX</span> +
  15.177 +<a class="c2html-lineno" name="l129" href="#l129">129 </a>                <span class="c2html-type">Constants</span>.<span class="c2html-type">SCHEMA_LOCATION</span>, <span class="c2html-type">String</span>.format(<span class="c2html-string">"%s %s"</span>,
  15.178 +<a class="c2html-lineno" name="l130" href="#l130">130 </a>                    <span class="c2html-type">NAMESPACE</span>.getURI(), <span class="c2html-type">Resources</span>.<span class="c2html-keyword">class</span>.getResource(
  15.179 +<a class="c2html-lineno" name="l131" href="#l131">131 </a>                        <span class="c2html-string">"graph-document.xsd"</span>).toExternalForm()));
  15.180 +<a class="c2html-lineno" name="l132" href="#l132">132 </a>            
  15.181 +<a class="c2html-lineno" name="l133" href="#l133">133 </a>            <span class="c2html-keyword">final</span> <span class="c2html-type">AtomicBoolean</span> passed = <span class="c2html-keyword">new</span> <span class="c2html-type">AtomicBoolean</span>(true);
  15.182 +<a class="c2html-lineno" name="l134" href="#l134">134 </a>            <span class="c2html-keyword">final</span> <span class="c2html-type">AtomicReference</span>&lt;<span class="c2html-type">SAXParseException</span>&gt; xmlerror = <span class="c2html-keyword">new</span> <span class="c2html-type">AtomicReference</span>&lt;&gt;();
  15.183 +<a class="c2html-lineno" name="l135" href="#l135">135 </a>            <span class="c2html-comment">// TODO: we should do more detailed error handling here</span>
  15.184 +<a class="c2html-lineno" name="l136" href="#l136">136 </a>            reader.setErrorHandler(<span class="c2html-keyword">new</span> <span class="c2html-type">ErrorHandler</span>() {
  15.185 +<a class="c2html-lineno" name="l137" href="#l137">137 </a>                <span class="c2html-directive">@Override</span>
  15.186 +<a class="c2html-lineno" name="l138" href="#l138">138 </a>                <span class="c2html-keyword">public</span> <span class="c2html-keyword">void</span> warning(<span class="c2html-type">SAXParseException</span> exception) <span class="c2html-keyword">throws</span> <span class="c2html-type">SAXException</span> {
  15.187 +<a class="c2html-lineno" name="l139" href="#l139">139 </a>                }
  15.188 +<a class="c2html-lineno" name="l140" href="#l140">140 </a>
  15.189 +<a class="c2html-lineno" name="l141" href="#l141">141 </a>                <span class="c2html-directive">@Override</span>
  15.190 +<a class="c2html-lineno" name="l142" href="#l142">142 </a>                <span class="c2html-keyword">public</span> <span class="c2html-keyword">void</span> error(<span class="c2html-type">SAXParseException</span> exception) <span class="c2html-keyword">throws</span> <span class="c2html-type">SAXException</span> {
  15.191 +<a class="c2html-lineno" name="l143" href="#l143">143 </a>                    xmlerror.set(exception);
  15.192 +<a class="c2html-lineno" name="l144" href="#l144">144 </a>                    passed.set(false);
  15.193 +<a class="c2html-lineno" name="l145" href="#l145">145 </a>                }
  15.194 +<a class="c2html-lineno" name="l146" href="#l146">146 </a>
  15.195 +<a class="c2html-lineno" name="l147" href="#l147">147 </a>                <span class="c2html-directive">@Override</span>
  15.196 +<a class="c2html-lineno" name="l148" href="#l148">148 </a>                <span class="c2html-keyword">public</span> <span class="c2html-keyword">void</span> fatalError(<span class="c2html-type">SAXParseException</span> exception) <span class="c2html-keyword">throws</span> <span class="c2html-type">SAXException</span> {
  15.197 +<a class="c2html-lineno" name="l149" href="#l149">149 </a>                    xmlerror.set(exception);
  15.198 +<a class="c2html-lineno" name="l150" href="#l150">150 </a>                    passed.set(false);
  15.199 +<a class="c2html-lineno" name="l151" href="#l151">151 </a>                }
  15.200 +<a class="c2html-lineno" name="l152" href="#l152">152 </a>                
  15.201 +<a class="c2html-lineno" name="l153" href="#l153">153 </a>            });
  15.202 +<a class="c2html-lineno" name="l154" href="#l154">154 </a>            <span class="c2html-type">Document</span> doc = reader.read(in);
  15.203 +<a class="c2html-lineno" name="l155" href="#l155">155 </a>            <span class="c2html-keyword">if</span> (!passed.get()) {
  15.204 +<a class="c2html-lineno" name="l156" href="#l156">156 </a>                <span class="c2html-comment">// TODO: provide details (maybe via separate error object?)</span>
  15.205 +<a class="c2html-lineno" name="l157" href="#l157">157 </a>                <span class="c2html-keyword">throw</span> xmlerror.get();
  15.206 +<a class="c2html-lineno" name="l158" href="#l158">158 </a>            }
  15.207 +<a class="c2html-lineno" name="l159" href="#l159">159 </a>            
  15.208 +<a class="c2html-lineno" name="l160" href="#l160">160 </a>            doc.normalize();
  15.209 +<a class="c2html-lineno" name="l161" href="#l161">161 </a>            
  15.210 +<a class="c2html-lineno" name="l162" href="#l162">162 </a>            <span class="c2html-type">Element</span> root = doc.getRootElement();
  15.211 +<a class="c2html-lineno" name="l163" href="#l163">163 </a>            metadata.read(root.element(<span class="c2html-type">TAG_METADATA</span>));
  15.212 +<a class="c2html-lineno" name="l164" href="#l164">164 </a>            
  15.213 +<a class="c2html-lineno" name="l165" href="#l165">165 </a>            <span class="c2html-keyword">if</span> (graph <span class="c2html-keyword">instanceof</span> <span class="c2html-type">Graph</span>) {
  15.214 +<a class="c2html-lineno" name="l166" href="#l166">166 </a>                readGraph(root.element(<span class="c2html-type">TAG_GRAPH</span>));
  15.215 +<a class="c2html-lineno" name="l167" href="#l167">167 </a>            } <span class="c2html-keyword">else</span> <span class="c2html-keyword">if</span> (graph <span class="c2html-keyword">instanceof</span> <span class="c2html-type">Digraph</span>) {
  15.216 +<a class="c2html-lineno" name="l168" href="#l168">168 </a>                readGraph(root.element(<span class="c2html-type">TAG_DIGRAPH</span>));
  15.217 +<a class="c2html-lineno" name="l169" href="#l169">169 </a>            } <span class="c2html-keyword">else</span> {
  15.218 +<a class="c2html-lineno" name="l170" href="#l170">170 </a>                <span class="c2html-keyword">throw</span> <span class="c2html-keyword">new</span> <span class="c2html-type">IOException</span>(<span class="c2html-string">"unsupported graph type"</span>);
  15.219 +<a class="c2html-lineno" name="l171" href="#l171">171 </a>            }
  15.220 +<a class="c2html-lineno" name="l172" href="#l172">172 </a>        } <span class="c2html-keyword">catch</span> (<span class="c2html-type">DocumentException</span> | <span class="c2html-type">SAXException</span> ex) {
  15.221 +<a class="c2html-lineno" name="l173" href="#l173">173 </a>            <span class="c2html-keyword">throw</span> <span class="c2html-keyword">new</span> <span class="c2html-type">IOException</span>(ex);
  15.222 +<a class="c2html-lineno" name="l174" href="#l174">174 </a>        }
  15.223 +<a class="c2html-lineno" name="l175" href="#l175">175 </a>    }
  15.224 +<a class="c2html-lineno" name="l176" href="#l176">176 </a>}
  15.225 +</div>
  15.226 +  </body>
  15.227 +</html>
  15.228 +
    16.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
    16.2 +++ b/test/gs/plain.html	Mon Apr 24 21:01:41 2023 +0200
    16.3 @@ -0,0 +1,63 @@
    16.4 +<!DOCTYPE html>
    16.5 +<html>
    16.6 +  <head>
    16.7 +    <title>c2html</title>
    16.8 +    <style type="text/css">
    16.9 +      div.c2html-code {
   16.10 +        white-space: pre;
   16.11 +        font-family: monospace;
   16.12 +      }
   16.13 +      a.c2html-lineno {
   16.14 +        /* as long as user-select isn't widely spread, we throw the bomb */
   16.15 +        -webkit-user-select: none;
   16.16 +        -moz-user-select: none;
   16.17 +        -ms-user-select: none;
   16.18 +        user-select: none;
   16.19 +        display: inline-block;
   16.20 +        font-style: italic;
   16.21 +        text-decoration: none;
   16.22 +        color: grey;
   16.23 +      }
   16.24 +      span.c2html-keyword {
   16.25 +        color: blue;
   16.26 +      }
   16.27 +      span.c2html-macroconst {
   16.28 +        color: cornflowerblue;
   16.29 +      }
   16.30 +      span.c2html-type {
   16.31 +        color: cornflowerblue;
   16.32 +      }
   16.33 +      span.c2html-directive {
   16.34 +        color: green;
   16.35 +      }
   16.36 +      span.c2html-string {
   16.37 +        color: darkorange;
   16.38 +      }
   16.39 +      span.c2html-comment {
   16.40 +        color: grey;
   16.41 +      }
   16.42 +      span.c2html-stdinclude {
   16.43 +        color: darkorange;
   16.44 +      }
   16.45 +      span.c2html-userinclude {
   16.46 +        color: darkorange;
   16.47 +      }
   16.48 +      a.c2html-userinclude {
   16.49 +        color: darkorange;
   16.50 +        text-decoration: underline;
   16.51 +      }
   16.52 +    </style>
   16.53 +  </head>
   16.54 +  <body>
   16.55 +
   16.56 +<div class="c2html-code">
   16.57 +<a class="c2html-lineno" name="l1" href="#l1">1 </a>&lt;/body&gt;
   16.58 +<a class="c2html-lineno" name="l2" href="#l2">2 </a>&lt;/html&gt;
   16.59 +<a class="c2html-lineno" name="l3" href="#l3">3 </a>&lt;!c
   16.60 +<a class="c2html-lineno" name="l4" href="#l4">4 </a>pblock_free(q);
   16.61 +<a class="c2html-lineno" name="l5" href="#l5">5 </a>!&gt;
   16.62 +<a class="c2html-lineno" name="l6" href="#l6">6 </a>
   16.63 +</div>
   16.64 +  </body>
   16.65 +</html>
   16.66 +
    17.1 --- a/test/header.html	Mon Oct 03 12:56:28 2022 +0200
    17.2 +++ b/test/header.html	Mon Apr 24 21:01:41 2023 +0200
    17.3 @@ -3,6 +3,10 @@
    17.4    <head>
    17.5      <title>c2html</title>
    17.6      <style type="text/css">
    17.7 +      div.c2html-code {
    17.8 +        white-space: pre;
    17.9 +        font-family: monospace;
   17.10 +      }
   17.11        a.c2html-lineno {
   17.12          /* as long as user-select isn't widely spread, we throw the bomb */
   17.13          -webkit-user-select: none;
    18.1 --- a/test/jheader.html	Mon Oct 03 12:56:28 2022 +0200
    18.2 +++ b/test/jheader.html	Mon Apr 24 21:01:41 2023 +0200
    18.3 @@ -3,6 +3,10 @@
    18.4    <head>
    18.5      <title>c2html</title>
    18.6      <style type="text/css">
    18.7 +      div.c2html-code {
    18.8 +        white-space: pre;
    18.9 +        font-family: monospace;
   18.10 +      }
   18.11        a.c2html-lineno {
   18.12          /* as long as user-select isn't widely spread, we throw the bomb */
   18.13          -webkit-user-select: none;

mercurial