Unnamed Fossil Project

Check-in [039942ee9d]
Login

Many hyperlinks are disabled.
Use anonymous login to enable hyperlinks.

Overview
Comment:updated
Timelines: family | ancestors | descendants | both | trunk
Files: files | file ages | folders
SHA1:039942ee9d015efca4d645c914617e6abd5b51d7
User & Date: alexey@sudachen.name 2012-03-20 03:31:39
Context
2012-03-25
21:39
updated check-in: c84f04fc57 user: alexey@sudachen.name tags: trunk
2012-03-20
03:31
updated check-in: 039942ee9d user: alexey@sudachen.name tags: trunk
2012-03-04
15:35
updated check-in: c5449f1ab4 user: alexey@sudachen.name tags: trunk
Changes
Hide Diffs Unified Diffs Ignore Whitespace Patch

Changes to aes.hc.

3
4
5
6
7
8
9
10
11
12
13
14
15
16
17

//
// The AES block cipher was designed by Vincent Rijmen and Joan Daemen.
//   http://csrc.nist.gov/encryption/aes/rijndael/Rijndael.pdf
//   http://csrc.nist.gov/publications/fips/fips197/fips-197.pdf
//

Copyright © 2010-2011, Alexéy Sudáchen, alexey@sudachen.name, Chile

In USA, UK, Japan and other countries allowing software patents:

    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.







|







3
4
5
6
7
8
9
10
11
12
13
14
15
16
17

//
// The AES block cipher was designed by Vincent Rijmen and Joan Daemen.
//   http://csrc.nist.gov/encryption/aes/rijndael/Rijndael.pdf
//   http://csrc.nist.gov/publications/fips/fips197/fips-197.pdf
//

Copyright © 2010-2011, Alexéy Sudachén, alexey@sudachen.name, Chile

In USA, UK, Japan and other countries allowing software patents:

    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

Changes to array.hc.

1
2
3
4
5
6
7
8
9
10
11

/*

Copyright © 2010-2011, Alexéy Sudáchen, alexey@sudachen.name, Chile

In USA, UK, Japan and other countries allowing software patents:

    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.



|







1
2
3
4
5
6
7
8
9
10
11

/*

Copyright © 2010-2011, Alexéy Sudachén, alexey@sudachen.name, Chile

In USA, UK, Japan and other countries allowing software patents:

    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

Changes to asio.hc.

1
2
3
4
5
6
7
8
9
10
11
..
47
48
49
50
51
52
53

54
55
56
57
58
59
60
61
62
63
..
65
66
67
68
69
70
71
72
73
74
75
76
77

78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117


118
119
120



121
122
123
124
125
126
127
128
129
130
131
132

133
134
135
136
137
138
139
...
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
...
266
267
268
269
270
271
272

273


274
275

276
277
278
279
280
281
282
283
284

285
286
287
288
289
290
291
292
293
294
295



296
297
298
299
300

301
302
303








304
305
306
307
308
309
310
311
312
313
314
315

316
317
318
319
320
321
322
323
324
325
326
327
328

329
330
331
332
333
334
335
336
337
338

339
340
341
342
343
344
345
346
347
348

349
350
351
352


















353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
...
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
...
400
401
402
403
404
405
406

407
408
409
410
411
412

/*

Copyright © 2010-2012, Alexéy Sudáchen, alexey@sudachen.name, Chile

In USA, UK, Japan and other countries allowing software patents:

    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.
................................................................................
#define C_once_5F140392_CCAA_4FE1_88CB_DC84EE3B25A0

#ifdef _LIBYOYO
#define _YOYO_ASIO_BUILTIN
#endif

#include "yoyo.hc"


#ifdef __windoze
  #include <winsock.h>
  typedef SOCKET socket_t;
#else
  #include <errno.h>
  #include <sys/types.h>
  #include <sys/socket.h>
  #include <netdb.h>
  #include <netinet/in.h>
................................................................................
  typedef int socket_t;
#endif

enum  
  {
    ASIO_COMPLETED    = 0x00000001,
    ASIO_CLOSED       = 0x00000002,
    ASIO_SYNCHRONOUSE = 0x00200000,
    ASIO_TIMER        = 0x00100000,
    ASIO_FAILED       = 0x8e000000,
    ASIO_INTERRAPTED  = 0x8f000000,
    ASIO_PENGING      = 0,
    ASIO_CONTINUE     = 0,

  
    ASIO_ST_SEND      = 1,
    ASIO_ST_RECV      = 2,
    ASIO_ST_NOTIFY    = 3,
    ASIO_ST_ACCEPT    = 4,
    
    ASIO_MAP_BASE     = 101,
  };
  
#define ASIO_SUCCEEDED(State) (!((State)&0xff000000))

int (asio_recv_callback_t)(void *obj,int status,int count);
int (asio_send_callback_t)(void *obj,int status,int count);
int (asio_accept_callback_t)(void *obj,int status,socket_t fd,struct sockaddr *addr);
int (asio_notify_callback_t)(void *obj,int status);
int (asio_any_callback_t)(void *obj,int status,...);

typedef struct _YOYO_ASIO_STATE
  {
    socket_t fd;
    longptr_t accum;
    int fdsno,count,mincount;
    struct sockaddr addr;
    void *obj, *dta;
    asio_any_callback_t cbk;
    int (*perform)(struct _YOYO_ASIO_STATE *st);
    YOYO_ASIO_STATE *next;
  } YOYO_ASIO_STATE;

#ifdef _YOYO_ASIO_BUILTIN
static int Asio_St_Count = 0;
static YOYO_ASIO_STATE *Asio_St_Map[ASIO_MAP_BASE] = {0};
static YOYO_ASIO_STATE *Asio_St_Pool = 0;
static int Asio_Is_Init = 0;
#endif
  ;
  
YOYO_ASIO_STATE **Asio_Map_Fd(socket_t fd)
#ifdef _YOYO_ASIO_BUILTIN
  {


    YOYO_ASIO_STATE **st = Asio_St_Map[fd/ASIO_MAP_BASE];
    while ( *st && (*st)->fd != fd ) st = &(*st)->next;
    return st;



  }
#endif
  ;
  
YOYO_ASIO_STATE *Asio_Pool_Get_State()
#ifdef _YOYO_ASIO_BUILTIN
  {
    YOYO_ASIO_STATE *st = Asio_St_Pool;
    if ( !Asio_St_Pool )
      {
        Asio_St_Pool = __Malloc_Npl(sizeof(YOYO_ASIO_STATE));
        st = Asio_St_Pool;

      }
    Asio_St_Pool = Asio_St_Pool->next;
    memset(st,0,sizeof(YOYO_ASIO_STATE));
    return st;
  }
#endif
  ;
................................................................................
#endif
  ;
  
int Asio_Do_Accept(YOYO_ASIO_STATE *st)
#ifdef _YOYO_ASIO_BUILTIN
  {
    int len = sizeof(st->addr);
    int e = accept(st->fd,&st->addr,&len);
    if ( e < 0 ) 
      return Asio_Status_Repeat_Or_Die()
    st->accum = e;
    return ASIO_COMPLETED;
  }
#endif  
  ;
  
int Asio_Do_Recv(YOYO_ASIO_STATE *st)
#ifdef _YOYO_ASIO_BUILTIN
  {
    int e = recv(st->fd,(char*)st->dta+st->accum,st->count-st->accum,0);
    if ( e < 0 || (!e && st->accum < st->mincount) )
      return Asio_Status_Repeat_Or_Die()
    if ( !e && st->count )
      return ASIO_CLOSED;
    st->accum += e;
    return st->accum >= st->mincount ? ASIO_COMPLETED : ASIO_PENGING;  
  }
#endif
  ;
  
int Asio_Do_Send(YOYO_ASIO_STATE *st)
#ifdef _YOYO_ASIO_BUILTIN
  {
    int e = send(st->fd,(char*)st->dta+st->accum,st->count-st->accum,0);
    if ( e <= 0 ) 
      return Asio_Status_Repeat_Or_Die()
    st->accum += e;
    return st->accum >= st->mincount ? ASIO_COMPLETED : ASIO_PENGING;  
  }
#endif
  ;
  
void Asio_Perform_Interrupt()
#ifdef _YOYO_ASIO_BUILTIN
  {
    int i;
    for ( i = 0; i < ASIO_MAP_BASE; ++i )
      {
        YOYO_ASIO_STATE *pst = &Asio_St_Map[i];
        while ( *pst )
          {
            __Auto_Release
              {
                YOYO_ASIO_STATE st = **pst;
                void *obj = __Pool_Ptr(st.obj,Yo_Unrefe);
                Asio_Release_State(pst);
                
                if ( st.cbk ) 
                  st.cbk(obj,status,st.accum,&st.addr);
              }
          }
      }
  }
#endif
  ;
  
void Asio_At_Exit()
#ifdef _YOYO_ASIO_BUILTIN
  {
    Asio_Perform_Interrupt();
    while ( Asio_St_Pool ) 
      {
        YOYO_ASIO_STATE * st = Asio_St_Pool;
        Asio_St_Pool = st->next;
................................................................................
          st->perform = Asio_Do_Accept; 
          break;
        case ASIO_ST_SEND:    
          st->fdsno = 1; 
          st->perform = Asio_Do_Send; 
          break;
        case ASIO_ST_NOTIFY:  

          break;


      }
      

    ++Asio_St_Count;
    return st;
  }
#endif
  ;
  
void Asio_Complete(socket_t fd, int failed)
#ifdef _YOYO_ASIO_BUILTIN
  {

    __Auto_Release
      {
        YOYO_ASIO_STATE **pst = Asio_Map_Fd(fd);
        if ( *pst )
          {
            int status = failed ? ASIO_FAILED 
                         : (*pst)->perform ? (*pst)->perform(*pst) 
                         : ASIO_COMPLETED;
            
            if ( status != ASIO_PENGING )
              {



                YOYO_ASIO_STATE st = **pst;
                void *obj     = __Pool_Ptr(st.obj,Yo_Unrefe);
                Asio_Release_State(pst);
                if ( st.cbk ) st.cbk(obj,status,st.accum);
              }

          }
      } 
  }








#endif
  ;
  
int Asio_Recv(socket_t fd, void *dta, int count, int mincount, void *obj, asio_recv_callback_t callback)
#ifdef _YOYO_ASIO_BUILTIN
  {
    YOYO_ASIO_STATE *st = Asio_Alloc_State(fd,ASIO_ST_RECV);
    st->obj      = __Refe(obj);
    st->cbk      = (asio_any_callback_t)callback;
    st->count    = count;
    st->mincount = mincount;
    st->dta      = dta;

  }
#endif
  ;
    
int Asio_Send(socket_t fd, void *dta, int count, void *obj, asio_send_callback_t callback)
#ifdef _YOYO_ASIO_BUILTIN
  {
    YOYO_ASIO_STATE *st = Asio_Alloc_State(fd,ASIO_ST_SEND);
    st->obj      = __Refe(obj);
    st->cbk      = (asio_any_callback_t)callback;
    st->count    = count;
    st->mincount = count;
    st->dta      = dta;

  }
#endif
  ;
    
int Asio_Notify(socket_t fd, void *obj, asio_notify_callback_t callback)
#ifdef _YOYO_ASIO_BUILTIN
  {
    YOYO_ASIO_STATE *st = Asio_Alloc_State(fd,ASIO_ST_NOTIFY);
    st->obj      = __Refe(obj);
    st->cbk      = (asio_any_callback_t)callback;

  }
#endif
  ;
  
int Asio_Accept(socket_t fd, char *host, int port, void *obj, asio_accept_callback_t callback)
#ifdef _YOYO_ASIO_BUILTIN
  {
    YOYO_ASIO_STATE *st = Asio_Alloc_State(fd,ASIO_ST_ACCEPT);
    st->obj      = __Refe(obj);
    st->cbk      = (asio_any_callback_t)callback;

  }
#endif
  ;
  


















int Asio_Perform_IO(int ms, int maxms)
#ifdef _YOYO_ASIO_BUILTIN
  {
    int i,j;
    timeval timeout = { ms/1000, ms*1000 };
    quad_t start    = maxms?System_Millis():0;

  #ifdef __windoze

    fd_set fds[3] = { {0, {-1}}, {0, {-1}}, {0, {-1}} };
    for ( i = 0; i < ASIO_MAP_BASE; ++i )
      {
        YOYO_ASIO_STATE *st = Asio_St_Map[i];
        while ( st )
          {
            fd_set *q = fds[st->fdsno];
            if ( fds[2].fd_count < FD_SETSIZE )
              {
                q->fd_array[q->fd_count] = st->fd;
                ++q->fd_count; 
                fds[2].fd_array[fds[2].fd_count] = st->fd;
                ++fds[2].fd_count; 
              }
................................................................................
            st = st->next;
          }
      }
    
    i = select(Yo_MIN(FD_SETSIZE,Asio_St_Count),&fds[0],&fds[1],&fds[2],&timeout);

    if ( i < 0 )
      __Raise_Format(YOYO_ERROR_IO,(__yoTa("ASIO select failed: %d",0),i));

    for ( i = 2; i >= 0; --i )
      for ( j = 0; j < fds[i].fd_count; ++j )
        {
          Asio_Complete(fds[i].fd_array[j],i==2);
          if ( maxms && ( maxms < (System_Millis() - start) ) )
            goto l;
        }

  #else
  #error "not implemented!"
  #endif
................................................................................
  }
#endif
  ;
  
int Asio_Perform_Timer(int interval)
#ifdef _YOYO_ASIO_BUILTIN
  {

  }
#endif
  ;
  
#endif /* C_once_5F140392_CCAA_4FE1_88CB_DC84EE3B25A0 */




|







 







>


|







 







|





>











|
|
|
|
|










|













>
>
|
|
|
>
>
>












>







 







|
|
|
|









|
|



|









|

|










|









|







|







 







>

>
>


>





|



>


<


|





>
>
>
|
|
|
|
|
>



>
>
>
>
>
>
>
>












>













>










>




|





>




>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>




|










|







 







|


|

|







 







>






1
2
3
4
5
6
7
8
9
10
11
..
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
..
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
...
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
...
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299

300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
...
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
...
446
447
448
449
450
451
452
453
454
455
456
457
458
459

/*

Copyright © 2010-2012, Alexéy Sudachén, alexey@sudachen.name, Chile

In USA, UK, Japan and other countries allowing software patents:

    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.
................................................................................
#define C_once_5F140392_CCAA_4FE1_88CB_DC84EE3B25A0

#ifdef _LIBYOYO
#define _YOYO_ASIO_BUILTIN
#endif

#include "yoyo.hc"
#include "datetime.hc"

#ifdef __windoze
  #include <winsock2.h>
  typedef SOCKET socket_t;
#else
  #include <errno.h>
  #include <sys/types.h>
  #include <sys/socket.h>
  #include <netdb.h>
  #include <netinet/in.h>
................................................................................
  typedef int socket_t;
#endif

enum  
  {
    ASIO_COMPLETED    = 0x00000001,
    ASIO_CLOSED       = 0x00000002,
    ASIO_SYNCHRONOUS = 0x00200000,
    ASIO_TIMER        = 0x00100000,
    ASIO_FAILED       = 0x8e000000,
    ASIO_INTERRAPTED  = 0x8f000000,
    ASIO_PENGING      = 0,
    ASIO_CONTINUE     = 0,
    ASIO_INTERRUPT    = 1,
  
    ASIO_ST_SEND      = 1,
    ASIO_ST_RECV      = 2,
    ASIO_ST_NOTIFY    = 3,
    ASIO_ST_ACCEPT    = 4,
    
    ASIO_MAP_BASE     = 101,
  };
  
#define ASIO_SUCCEEDED(State) (!((State)&0xff000000))

typedef int (*asio_recv_callback_t)(void *obj,int status,int count);
typedef int (*asio_send_callback_t)(void *obj,int status);
typedef int (*asio_accept_callback_t)(void *obj,int status,socket_t fd,struct sockaddr *addr);
typedef int (*asio_notify_callback_t)(void *obj,int status);
typedef void (*asio_any_callback_t)(void *obj,int status,...);

typedef struct _YOYO_ASIO_STATE
  {
    socket_t fd;
    longptr_t accum;
    int fdsno,count,mincount;
    struct sockaddr addr;
    void *obj, *dta;
    asio_any_callback_t cbk;
    int (*perform)(struct _YOYO_ASIO_STATE *st);
    struct _YOYO_ASIO_STATE *next;
  } YOYO_ASIO_STATE;

#ifdef _YOYO_ASIO_BUILTIN
static int Asio_St_Count = 0;
static YOYO_ASIO_STATE *Asio_St_Map[ASIO_MAP_BASE] = {0};
static YOYO_ASIO_STATE *Asio_St_Pool = 0;
static int Asio_Is_Init = 0;
#endif
  ;
  
YOYO_ASIO_STATE **Asio_Map_Fd(socket_t fd)
#ifdef _YOYO_ASIO_BUILTIN
  {
    if ( fd != INVALID_SOCKET )
      {
        YOYO_ASIO_STATE **st = &Asio_St_Map[(longptr_t)fd/ASIO_MAP_BASE];
        while ( *st && (*st)->fd != fd ) st = &(*st)->next;
        return st;
      }
    else
      __Raise(YOYO_ERROR_INVALID_PARAM,__yoTa("ASIO couldn´t handle INVALID_SOCKET",0));
  }
#endif
  ;
  
YOYO_ASIO_STATE *Asio_Pool_Get_State()
#ifdef _YOYO_ASIO_BUILTIN
  {
    YOYO_ASIO_STATE *st = Asio_St_Pool;
    if ( !Asio_St_Pool )
      {
        Asio_St_Pool = __Malloc_Npl(sizeof(YOYO_ASIO_STATE));
        st = Asio_St_Pool;
        st->next = 0;
      }
    Asio_St_Pool = Asio_St_Pool->next;
    memset(st,0,sizeof(YOYO_ASIO_STATE));
    return st;
  }
#endif
  ;
................................................................................
#endif
  ;
  
int Asio_Do_Accept(YOYO_ASIO_STATE *st)
#ifdef _YOYO_ASIO_BUILTIN
  {
    int len = sizeof(st->addr);
    socket_t e = accept(st->fd,&st->addr,&len);
    if ( e == INVALID_SOCKET ) 
      return Asio_Status_Repeat_Or_Die();
    st->accum = (longptr_t)e;
    return ASIO_COMPLETED;
  }
#endif  
  ;
  
int Asio_Do_Recv(YOYO_ASIO_STATE *st)
#ifdef _YOYO_ASIO_BUILTIN
  {
    int e = recv(st->fd,(char*)st->dta+st->accum,st->count-st->accum,0);
    if ( e < 0 || (!e && (int)st->accum < st->mincount) )
      return Asio_Status_Repeat_Or_Die();
    if ( !e && st->count )
      return ASIO_CLOSED;
    st->accum += e;
    return (int)st->accum >= st->mincount ? ASIO_COMPLETED : ASIO_PENGING;  
  }
#endif
  ;
  
int Asio_Do_Send(YOYO_ASIO_STATE *st)
#ifdef _YOYO_ASIO_BUILTIN
  {
    int e = send(st->fd,(char*)st->dta+st->accum,st->count-st->accum,0);
    if ( e <= 0 ) 
      return Asio_Status_Repeat_Or_Die();
    st->accum += e;
    return (int)st->accum >= st->mincount ? ASIO_COMPLETED : ASIO_PENGING;  
  }
#endif
  ;
  
void Asio_Perform_Interrupt()
#ifdef _YOYO_ASIO_BUILTIN
  {
    int i;
    for ( i = 0; i < ASIO_MAP_BASE; ++i )
      {
        YOYO_ASIO_STATE **pst = &Asio_St_Map[i];
        while ( *pst )
          {
            __Auto_Release
              {
                YOYO_ASIO_STATE st = **pst;
                void *obj = __Pool_Ptr(st.obj,Yo_Unrefe);
                Asio_Release_State(pst);
                
                if ( st.cbk ) 
                  st.cbk(obj,ASIO_INTERRAPTED,st.accum,&st.addr);
              }
          }
      }
  }
#endif
  ;
  
void Asio_At_Exit(void)
#ifdef _YOYO_ASIO_BUILTIN
  {
    Asio_Perform_Interrupt();
    while ( Asio_St_Pool ) 
      {
        YOYO_ASIO_STATE * st = Asio_St_Pool;
        Asio_St_Pool = st->next;
................................................................................
          st->perform = Asio_Do_Accept; 
          break;
        case ASIO_ST_SEND:    
          st->fdsno = 1; 
          st->perform = Asio_Do_Send; 
          break;
        case ASIO_ST_NOTIFY:  
          st->fdsno = 1; 
          break;
        default:
          __Raise_Format(YOYO_ERROR_UNEXPECTED,(__yoTa("ASIO unknown tag: %d",0),tag));
      }
      
    st->fd = fd;  
    ++Asio_St_Count;
    return st;
  }
#endif
  ;

void Asio_Complete(socket_t fd, int failed)
#ifdef _YOYO_ASIO_BUILTIN
  {
    YOYO_ASIO_STATE **pst = Asio_Map_Fd(fd);
    __Auto_Release
      {

        if ( *pst )
          {
            int status = failed ? failed 
                         : (*pst)->perform ? (*pst)->perform(*pst) 
                         : ASIO_COMPLETED;
            
            if ( status != ASIO_PENGING )
              {
                pst = Asio_Map_Fd(fd);
                if ( *pst )
                  {
                    YOYO_ASIO_STATE st = **pst;
                    __Pool_Ptr(st.obj,Yo_Unrefe);
                    Asio_Release_State(pst);
                    if ( st.cbk ) st.cbk(st.obj,status,st.accum,&st.addr);
                  }
              } 
          }
      } 
  }
#endif
  ;

void Asio_Interrupt(socket_t fd)
#ifdef _YOYO_ASIO_BUILTIN
  {
    Asio_Complete(fd,ASIO_INTERRUPT);
  }
#endif
  ;
  
int Asio_Recv(socket_t fd, void *dta, int count, int mincount, void *obj, asio_recv_callback_t callback)
#ifdef _YOYO_ASIO_BUILTIN
  {
    YOYO_ASIO_STATE *st = Asio_Alloc_State(fd,ASIO_ST_RECV);
    st->obj      = __Refe(obj);
    st->cbk      = (asio_any_callback_t)callback;
    st->count    = count;
    st->mincount = mincount;
    st->dta      = dta;
    return 0;
  }
#endif
  ;
    
int Asio_Send(socket_t fd, void *dta, int count, void *obj, asio_send_callback_t callback)
#ifdef _YOYO_ASIO_BUILTIN
  {
    YOYO_ASIO_STATE *st = Asio_Alloc_State(fd,ASIO_ST_SEND);
    st->obj      = __Refe(obj);
    st->cbk      = (asio_any_callback_t)callback;
    st->count    = count;
    st->mincount = count;
    st->dta      = dta;
    return 0;
  }
#endif
  ;
    
int Asio_Notify(socket_t fd, void *obj, asio_notify_callback_t callback)
#ifdef _YOYO_ASIO_BUILTIN
  {
    YOYO_ASIO_STATE *st = Asio_Alloc_State(fd,ASIO_ST_NOTIFY);
    st->obj      = __Refe(obj);
    st->cbk      = (asio_any_callback_t)callback;
    return 0;
  }
#endif
  ;
  
int Asio_Accept(socket_t fd, void *obj, asio_accept_callback_t callback)
#ifdef _YOYO_ASIO_BUILTIN
  {
    YOYO_ASIO_STATE *st = Asio_Alloc_State(fd,ASIO_ST_ACCEPT);
    st->obj      = __Refe(obj);
    st->cbk      = (asio_any_callback_t)callback;
    return 0;
  }
#endif
  ;
  
char *Format_Network_Error()
#ifdef _YOYO_ASIO_BUILTIN
  {
  #ifdef __windoze
    int err = WSAGetLastError();
    char *msg = __Malloc(1024);
    FormatMessageA(FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS |
                  FORMAT_MESSAGE_MAX_WIDTH_MASK, NULL, err,
                  MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
                  (LPSTR)msg, 1024,0);
    return msg;  
  #else
    return strerror(errno);
  #endif
  }
#endif
  ;

int Asio_Perform_IO(int ms, int maxms)
#ifdef _YOYO_ASIO_BUILTIN
  {
    int i,j;
    struct timeval timeout = { ms/1000, ms*1000 };
    quad_t start    = maxms?System_Millis():0;

  #ifdef __windoze

    fd_set fds[3] = { {0, {-1}}, {0, {-1}}, {0, {-1}} };
    for ( i = 0; i < ASIO_MAP_BASE; ++i )
      {
        YOYO_ASIO_STATE *st = Asio_St_Map[i];
        while ( st )
          {
            fd_set *q = &fds[st->fdsno];
            if ( fds[2].fd_count < FD_SETSIZE )
              {
                q->fd_array[q->fd_count] = st->fd;
                ++q->fd_count; 
                fds[2].fd_array[fds[2].fd_count] = st->fd;
                ++fds[2].fd_count; 
              }
................................................................................
            st = st->next;
          }
      }
    
    i = select(Yo_MIN(FD_SETSIZE,Asio_St_Count),&fds[0],&fds[1],&fds[2],&timeout);

    if ( i < 0 )
      __Raise_Format(YOYO_ERROR_IO,(__yoTa("ASIO select failed: %s",0),Format_Network_Error()));

    for ( i = 2; i >= 0; --i )
      for ( j = 0; j < (int)fds[i].fd_count; ++j )
        {
          Asio_Complete(fds[i].fd_array[j],i==2?ASIO_FAILED:0);
          if ( maxms && ( maxms < (System_Millis() - start) ) )
            goto l;
        }

  #else
  #error "not implemented!"
  #endif
................................................................................
  }
#endif
  ;
  
int Asio_Perform_Timer(int interval)
#ifdef _YOYO_ASIO_BUILTIN
  {
    return Asio_St_Count;
  }
#endif
  ;
  
#endif /* C_once_5F140392_CCAA_4FE1_88CB_DC84EE3B25A0 */

Changes to bigint.hc.

1
2
3
4
5
6
7
8
9
10
11

/*

Copyright © 2010-2011, Alexéy Sudáchen, alexey@sudachen.name, Chile

In USA, UK, Japan and other countries allowing software patents:

    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.



|







1
2
3
4
5
6
7
8
9
10
11

/*

Copyright © 2010-2011, Alexéy Sudachén, alexey@sudachen.name, Chile

In USA, UK, Japan and other countries allowing software patents:

    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

Changes to bio.hc.

1
2
3
4
5
6
7
8
9
10
11

/*

Copyright © 2010-2012, Alexéy Sudáchen, alexey@sudachen.name, Chile

In USA, UK, Japan and other countries allowing software patents:

    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.



|







1
2
3
4
5
6
7
8
9
10
11

/*

Copyright © 2010-2012, Alexéy Sudachén, alexey@sudachen.name, Chile

In USA, UK, Japan and other countries allowing software patents:

    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

Changes to blowfish.hc.

2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
/*

//
// Blowfish Cipher 
//   Designed in 1993 by Bruce Schneier 
//

Copyright © 2010-2011, Alexéy Sudáchen, alexey@sudachen.name, Chile

In USA, UK, Japan and other countries allowing software patents:

    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.







|







2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
/*

//
// Blowfish Cipher 
//   Designed in 1993 by Bruce Schneier 
//

Copyright © 2010-2011, Alexéy Sudachén, alexey@sudachen.name, Chile

In USA, UK, Japan and other countries allowing software patents:

    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

Changes to buffer.hc.

1
2
3
4
5
6
7
8
9
10
11

/*

Copyright © 2010-2011, Alexéy Sudáchen, alexey@sudachen.name, Chile

In USA, UK, Japan and other countries allowing software patents:

    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.



|







1
2
3
4
5
6
7
8
9
10
11

/*

Copyright © 2010-2011, Alexéy Sudachén, alexey@sudachen.name, Chile

In USA, UK, Japan and other countries allowing software patents:

    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

Changes to cipher.hc.

1
2
3
4
5
6
7
8
9
10
11

/*

Copyright © 2010-2011, Alexéy Sudáchen, alexey@sudachen.name, Chile

In USA, UK, Japan and other countries allowing software patents:

    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.



|







1
2
3
4
5
6
7
8
9
10
11

/*

Copyright © 2010-2011, Alexéy Sudachén, alexey@sudachen.name, Chile

In USA, UK, Japan and other countries allowing software patents:

    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

Changes to crc.hc.

1
2
3
4
5
6
7
8
9
10
11

/*

Copyright © 2010-2011, Alexéy Sudáchen, alexey@sudachen.name, Chile

In USA, UK, Japan and other countries allowing software patents:

    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.



|







1
2
3
4
5
6
7
8
9
10
11

/*

Copyright © 2010-2011, Alexéy Sudachén, alexey@sudachen.name, Chile

In USA, UK, Japan and other countries allowing software patents:

    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

Changes to datetime.hc.

1
2
3
4
5
6
7
8
9
10
11

/*

Copyright © 2010-2011, Alexéy Sudáchen, alexey@sudachen.name, Chile

In USA, UK, Japan and other countries allowing software patents:

    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.



|







1
2
3
4
5
6
7
8
9
10
11

/*

Copyright © 2010-2011, Alexéy Sudachén, alexey@sudachen.name, Chile

In USA, UK, Japan and other countries allowing software patents:

    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

Changes to defpars.hc.

1
2
3
4
5
6
7
8
9
10
11

/*

Copyright © 2010-2011, Alexéy Sudáchen, alexey@sudachen.name, Chile

In USA, UK, Japan and other countries allowing software patents:

    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.



|







1
2
3
4
5
6
7
8
9
10
11

/*

Copyright © 2010-2011, Alexéy Sudachén, alexey@sudachen.name, Chile

In USA, UK, Japan and other countries allowing software patents:

    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

Changes to dicto.hc.

1
2
3
4
5
6
7
8
9
10
11
...
363
364
365
366
367
368
369



















370
371
372
373
374

/*

Copyright © 2010-2011, Alexéy Sudáchen, alexey@sudachen.name, Chile

In USA, UK, Japan and other countries allowing software patents:

    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.
................................................................................

void *Dicto_Init(void)
#ifdef _YOYO_DICTO_BUILTIN  
  {
    YOYO_DICTO *dicto = Yo_Object_Dtor(sizeof(YOYO_DICTO),Dicto_Destruct);
    return dicto;
  }



















#endif
  ;

#endif /* C_once_38B1FFE7_1462_42EB_BABE_AA8E0BE62203 */




|







 







>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>





1
2
3
4
5
6
7
8
9
10
11
...
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393

/*

Copyright © 2010-2011, Alexéy Sudachén, alexey@sudachen.name, Chile

In USA, UK, Japan and other countries allowing software patents:

    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.
................................................................................

void *Dicto_Init(void)
#ifdef _YOYO_DICTO_BUILTIN  
  {
    YOYO_DICTO *dicto = Yo_Object_Dtor(sizeof(YOYO_DICTO),Dicto_Destruct);
    return dicto;
  }
#endif
  ;

void Dicto_Apply(YOYO_DICTO *o,void (*filter)(char *,void *,void *),void *state)  
#ifdef _YOYO_DICTO_BUILTIN  
  {
    int i;
    YOYO_DICTO_REC *nrec;
    if ( o && o->table ) 
      for ( i = 0; i < o->width; ++i )
        {      
          nrec = o->table[i];
          while ( nrec )
            {
              filter(nrec->key,nrec->ptr,state);
              nrec = nrec->next;
            }  
        }
  }
#endif
  ;

#endif /* C_once_38B1FFE7_1462_42EB_BABE_AA8E0BE62203 */

Changes to file.hc.

1
2
3
4
5
6
7
8
9
10
11
..
55
56
57
58
59
60
61






62
63
64
65
66
67
68
69
70
71






72
73
74
75
76
77
78
...
177
178
179
180
181
182
183






























184
185
186
187
188
189
190
....
1432
1433
1434
1435
1436
1437
1438


1439




1440
1441
1442
1443
1444
1445
1446
....
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597

/*

Copyright © 2010-2011, Alexéy Sudáchen, alexey@sudachen.name, Chile

In USA, UK, Japan and other countries allowing software patents:

    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.
................................................................................
#include "buffer.hc"
#include "random.hc"

#ifndef __windoze
# include <dirent.h>
# include <sys/file.h>
#endif







enum { YOYO_FILE_COPY_BUFFER_SIZE = 4096, };

#ifdef _YOYO_FILE_BUILTIN
# define _YOYO_FILE_BUILTIN_CODE(Code) Code
# define _YOYO_FILE_EXTERN
#else
# define _YOYO_FILE_BUILTIN_CODE(Code)
# define _YOYO_FILE_EXTERN extern
#endif







void File_Check_Error(char *op, FILE *f, char *fname, int look_to_errno)
#ifdef _YOYO_FILE_BUILTIN
  {
    int err = 0;
    char *errS = 0;
    
................................................................................
        if ( err == - 1 )
          Raise_If_File_Error("chdir",dirname);
      }
  }
#endif
  ;
  






























char *Path_Unique_Name(char *dirname,char *pfx, char *sfx)
#ifdef _YOYO_FILE_BUILTIN
  {
  #ifdef __windoze
    char DELIMITER[] = "\\";
  #else
    char DELIMITER[] = "/";
................................................................................
  ;

#define Lseek(Fd,Pos) Fd_Lseek(Fd,Pos,0)
#define Lseek_Raise(Fd,Pos) Fd_Lseek(Fd,Pos,1)
int Fd_Lseek(int fd, quad_t pos, int do_raise)
#ifdef _YOYO_FILE_BUILTIN
  {


    if ( lseek(fd,pos,(pos<0?SEEK_END:SEEK_SET)) < 0 )




      {
        int err = errno;
        if ( do_raise )
          __Raise_Format(YOYO_ERROR_IO,(__yoTa("failed to seek file: %s",0),strerror(err)));
        return err;
      }
    return 0;
................................................................................
  ;

quad_t Buffer_File_Seek(YOYO_BUFFER_FILE *f, quad_t pos, int whence)
#ifdef _YOYO_FILE_BUILTIN
  {
    quad_t old = f->offs;
    if ( whence == SEEK_SET )
      f->offs = pos;
    else if ( whence == SEEK_CUR )
      f->offs += pos;
    else if ( whence == SEEK_END )
      f->offs = f->bf->count + pos;
    if ( f->offs < 0 ) 
      {
        f->offs = 0;
        __Raise(YOYO_ERROR_IO,"negative file offset");
      }
    return old;
  }



|







 







>
>
>
>
>
>










>
>
>
>
>
>







 







>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>







 







>
>
|
>
>
>
>







 







|

|

|







1
2
3
4
5
6
7
8
9
10
11
..
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
...
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
....
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
....
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645

/*

Copyright © 2010-2011, Alexéy Sudachén, alexey@sudachen.name, Chile

In USA, UK, Japan and other countries allowing software patents:

    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.
................................................................................
#include "buffer.hc"
#include "random.hc"

#ifndef __windoze
# include <dirent.h>
# include <sys/file.h>
#endif

#ifdef _FILEi32
  typedef long file_offset_t;
#else
  typedef quad_t file_offset_t;
#endif

enum { YOYO_FILE_COPY_BUFFER_SIZE = 4096, };

#ifdef _YOYO_FILE_BUILTIN
# define _YOYO_FILE_BUILTIN_CODE(Code) Code
# define _YOYO_FILE_EXTERN
#else
# define _YOYO_FILE_BUILTIN_CODE(Code)
# define _YOYO_FILE_EXTERN extern
#endif

#ifdef __windoze
enum { YOYO_PATH_SEPARATOR = '\\' };
#else
enum { YOYO_PATH_SEPARATOR = '/' };
#endif

void File_Check_Error(char *op, FILE *f, char *fname, int look_to_errno)
#ifdef _YOYO_FILE_BUILTIN
  {
    int err = 0;
    char *errS = 0;
    
................................................................................
        if ( err == - 1 )
          Raise_If_File_Error("chdir",dirname);
      }
  }
#endif
  ;
  
char *Path_Normilize_Npl_(char *path, int sep)
#ifdef _YOYO_FILE_BUILTIN
  {
    int i = 0, ln = strlen(path);
    char *S = __Malloc_Npl(ln+1);
    *S = 0;
    
    while ( *path )
      {
        if ( *path != '/' && *path != '\\' )
          S[i++] = *path;
        else
          if ( !i || S[i-1] != sep ) 
            S[i++] = sep;
        ++path;
      }
    
    S[i] = 0;
    if ( i && S[i-1] == sep ) S[--i] = 0;
    
    return S;
  }
#endif
  ;

#define Path_Normilize_Npl(S) Path_Normilize_Npl_(S,YOYO_PATH_SEPARATOR)
#define Path_Normilize(S) __Pool(Path_Normilize_Npl(S))
#define Path_Normposix_Npl(S) Path_Normilize_Npl_(S,'/')
#define Path_Normposix(S) __Pool(Path_Normilize_Npl(S))

char *Path_Unique_Name(char *dirname,char *pfx, char *sfx)
#ifdef _YOYO_FILE_BUILTIN
  {
  #ifdef __windoze
    char DELIMITER[] = "\\";
  #else
    char DELIMITER[] = "/";
................................................................................
  ;

#define Lseek(Fd,Pos) Fd_Lseek(Fd,Pos,0)
#define Lseek_Raise(Fd,Pos) Fd_Lseek(Fd,Pos,1)
int Fd_Lseek(int fd, quad_t pos, int do_raise)
#ifdef _YOYO_FILE_BUILTIN
  {
    if ( 0 >  
  #ifdef _FILEi32
    lseek(fd,(long)pos,(pos<0?SEEK_END:SEEK_SET))
  #else
    _WINPOSIX(_lseeki64,lseek64)(fd,pos,(pos<0?SEEK_END:SEEK_SET))
  #endif
    )
      {
        int err = errno;
        if ( do_raise )
          __Raise_Format(YOYO_ERROR_IO,(__yoTa("failed to seek file: %s",0),strerror(err)));
        return err;
      }
    return 0;
................................................................................
  ;

quad_t Buffer_File_Seek(YOYO_BUFFER_FILE *f, quad_t pos, int whence)
#ifdef _YOYO_FILE_BUILTIN
  {
    quad_t old = f->offs;
    if ( whence == SEEK_SET )
      f->offs = (int)pos;
    else if ( whence == SEEK_CUR )
      f->offs += (int)pos;
    else if ( whence == SEEK_END )
      f->offs = f->bf->count + (int)pos;
    if ( f->offs < 0 ) 
      {
        f->offs = 0;
        __Raise(YOYO_ERROR_IO,"negative file offset");
      }
    return old;
  }

Changes to httpx.hc.

1
2
3
4
5
6
7
8
9
10
11
..
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
..
79
80
81
82
83
84
85

86
87
88
89

90
91
92
93
94
95
96
97
98
99

100
101















102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122

123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144

145
146
147
148
149
150
151
152
153



































































































































































































































































































































































































































































































































































































154
155
156
157















158
159
160
161
162
163
164
165
166
167
168
...
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759

/*

Copyright © 2010-2012, Alexéy Sudáchen, alexey@sudachen.name, Chile

In USA, UK, Japan and other countries allowing software patents:

    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.
................................................................................
#include "dicto.hc"
#include "file.hc"
#include "tcpip.hc"
#include "url.hc"
#include "asio.hc"

#ifdef _LIBYOYO
#define _YOYO_HTTPX_BUILTIN
#endif

/*
добавить отсылку на пост
добваить разбор хидеров ответа
добавить вызов колбек хендлера на финише
в колбек хендлер на терминирование и завершение добавить закрытие соеденения (тольео если не завершение с кипэлайвом)
*/

typedef int (*httpx_collback_t)(void *obj, struct _YOYO_HTTPX *httpx, int status);

typedef struct _YOYO_HTTPX
  {
    void *netchnl;
    YOYO_URL *url;
    YOYO_URL *proxy_url;
    
................................................................................
    int  left;
    
    void *outstrm;
    void *poststrm;
    void *mon_obj;
    httpx_collback_t mon_callback;


    YOYO_BUFFER *bf;
    
    YOYO_DICTO  *hdrs;
    YOYO_BUFFER *content;

    int   streamed;
    int   status;
    char *status_text;
    
    int async:      1;
    int keepalive:  1;
    int legacy10:   1;
    int ssl:        1;
    int proxy:      1;
    int nline:      1;

        
  } YOYO_HTTPX;
















enum 
  {
    HTTPX_ASYNC       = 1,
    HTTPX_KEEPALIVE   = 2,
    HTTPX_LEGACY_10   = 4,
    
    HTTPX_GET         = __FOUR_CHARS('G','E','T','_'),
    HTTPX_PUT         = __FOUR_CHARS('P','U','T','_'),
    HTTPX_POST        = __FOUR_CHARS('P','O','S','T'),
    HTTPX_HEAD        = __FOUR_CHARS('H','E','A','D'),
  
    HTTPX_BROKEN                  = 0x8e000000,
    HTTPX_FAILED                  = 0x8f000000,
    HTTPX_NOTIFY                  = 0x00001000,
    
    /* HTTPX_RESOLVED                = 0x00000f01, */
    HTTPX_PROXY_CONNECTED         = 0x00000f01,
    HTTPX_PROXY_AUTHORIZED        = 0x00000f02,
    HTTPX_SSL_CONNECTED           = 0x00000f03,
    HTTPX_CONNECTED               = 0x00000f10,

    HTTPX_REQUESTED               = 0x00000f20,
    HTTPX_GETTING_HEADERS         = 0x00000f30,
    HTTPX_RESPONDED               = 0x00000f41,
    HTTPX_GETTING_CONTENT         = 0x00000f42,
    HTTPX_FINISHED                = 0x00000f43,
    HTTPX_SEND                    = 0x00000e01,
    HTTPX_RECV                    = 0x00000e02,

    HTTPX_INVALID_RESPONSE        = 0x00000d01,
    HTTPX_INVALID_RESPONSE_CODE   = 0x00000d02,

    HTTPX_PENDING = ASIO_PENDING /* 0 */,
  };

#define HTTPX_SUCCEEDED(Status) (((Status)&0x8f000000) == 0)
#define HTTPX_STATUS(Status)    ((Status)&0x0fff)

int Httpx_Asio_Recv(YOYO_HTTPX *httpx, void *dta, int count, int mincount, asio_callback_t callback)
#ifdef _YOYO_HTTPX_BUILTIN
  {
    if ( !httpx->ssl )
      return Tcp_Asio_Recv(httpx->netchnl,dta,count,mincount,httpx,callback);

  }
#endif
  ;
  
int Httpx_Asio_Send(YOYO_HTTPX *httpx, void *dta, int count, asio_callback_t callback)
#ifdef _YOYO_HTTPX_BUILTIN
  {
    if ( !httpx->ssl )
      return Tcp_Asio_Send(httpx->netchnl,dta,count,httpx,callback);



































































































































































































































































































































































































































































































































































































  }
#endif
  ;
  















YOYO_HTTPX *Httpx_Client(char *url,int flags,void *mon_obj,httpx_collback_t mon_callback)
#ifdef _YOYO_HTTPX_BUILTIN
  {
    YOYO_HTTPX *httpx = __Object(sizeof(YOYO_HTTPX),funcs);
    httpx->url = __Retain(Url_Parse(url));
    
    if ( flags & HTTPX_ASYNC )
      httpx->async = 1;
    if ( flags & HTTPX_KEEPALIVE )
      httpx->keepalive = 1;
    if ( flags & HTTPX_LEGACY_10 )
................................................................................
        httpx->url->port = 80;
        
    return httpx;
  }
#endif
  ;
  
int Httpx_Do_Callback(YOYO_HTTPX *httpx, int status)
#ifdef _YOYO_HTTPX_BUILTIN
  {
    if ( httpx->mon_callback )
      return httpx->mon_callback(httpx->mon_obj,httpx,status);
    return status;
  }
#endif
  ;
  
int Httpx_Update_Until_EOH(YOYO_HTTPX *httpx, int count)
#ifdef _YOYO_HTTPX_BUILTIN
  {
    YOYO_BUFFER *bf = httpx->bf;
    int i;
    char *q = bf->at + bf->count;
    bf->count += count;

    for ( i = 0; i < count; ++i )
      {
        if ( q[i] == '\r' && q[i+1] == '\n' )
          { if ( httpx->nline ) return bf->count-(count-i+2); httpx->nline = 1; ++i; } 
        else if ( q[i] == '\n' )
          { if ( httpx->nline ) return bf->count-(count-i+1);  httpx->nline = 1; } 
        ++i;
      }
      
    return 0;
  }      
#endif
  ;
  
void Httpx_Analyze_Headers(YOYO_HTTPX *httpx)
#ifdef _YOYO_HTTPX_BUILTIN
  {
    int i;
    char *q, *Q, *S = httpx->bf->at;
    while (*S!='\n') ++S; /* skip HTTP/1.x line */
    for (;;)
      {
        q = ++S;
        while ( *q != '\n' ) ++q;
        *q = 0;
        for ( i = 1; q-i > S && Isspace(q[-i]); ++i ) q[-i] = 0;
        while (Isspace(*S)) ++S;
        if ( !*S ) break; /* empty line => end of headers*/ 
        Q = S;
        while ( *Q && *Q != ':' ) ++Q;
        if ( *Q == ':' )
          {
            *Q = 0;
            for ( i = 1; Q-i > S && Isspace(Q[-i]); ++i ) Q[-i] = 0;
            ++Q;
            while ( Isspace(*Q) ) ++Q;
            Dicto_Put(httpx->hdrs,S,(Q=Str_Copy_Npl(q,-1)));
          }
      }
  }
#endif
  ;
  
int Cbk_Httpx_Getting_Content(void *netchnl, void *_httpx, int status, byte_t *bytes, int count, int left)
#ifdef _YOYO_HTTPX_BUILTIN
  {
    int L;
    YOYO_HTTPX *httpx = _httpx;
    YOYO_BUFFER *bf = httpx->bf;
    
    if ( !ASIO_SUCCEEDED(status) )
      return Httpx_Do_Callback(httpx,HTTPX_BROKEN|HTTPX_GETTING_HEADERS);

    L = Yo_MIN(httpx->left,count);
    httpx->left -= L;
    httpx->streamed += L;
    Oj_Write(httpx->outstrm,bf->at,L,-1);

    if ( !httpx->left )
      return HTTPX_FINISHED;

    if ( status&ASIO_SYNCHRONOUSE )
      return HTTPX_CONTINUE;
    
    return Httpx_Asio_Recv(netchnl,bf->at,bf->capacity,Yo_MIN(httpx->left,bf->capacity)
                        ,httpx
                        ,Cbk_Httpx_Getting_Content);
  }
#endif
  ;
    
int Cbk_Httpx_Getting_Headers(void *netchnl, void *_httpx, int status, byte_t *bytes, int count, int left)
#ifdef _YOYO_HTTPX_BUILTIN
  {
    YOYO_HTTPX *httpx = _httpx;
    YOYO_BUFFER *bf = httpx->bf;
    int iterate = 0;
    
    if ( !ASIO_SUCCEEDED(status) )
      return Httpx_Do_Callback(httpx,HTTPX_BROKEN|HTTPX_GETTING_HEADERS);
    
    if ( httpx->bf->count == 0 )
      {
        if ( !strcmp_I(bf->at,"HTTP/1.") )
          {
            char *q = bf->at + 8, *Q;
            bf->at[count] = 0;
            while ( *q && !Isspace(*q) ) ++q;
            while ( Isspace(*q) ) ++q;
            httpx->status = strtol(q,&q,10);
            if ( httpx->status < 100 || httpx->status >= 510 )
              return Httpx_Do_Callback(httpx,HTTPX_FAILED|HTTPX_INVALID_RESPONSE_CODE);    
            while ( *q != '\n' && *q != '\r' && Isspace(*q) ) ++q;
            Q = q;
            while ( *q && *q != '\n' && *q != '\r' && !Isspace(*q) ) ++q;
            httpx->status_text = Str_Range_Npl(Q,q);
            bf->count = (q - bf->at);         
            count -= bf->count; 
            httpx->nline = 0;
          }
        else
          return Httpx_Do_Callback(httpx,HTTPX_FAILED|HTTPX_INVALID_RESPONSE);
      }
    else
      iterate = status&ASIO_SYNCHRONOUSE;
      
    for(;;) 
      {
        int eoh = Httpx_Update_Until_EOH(httpx,count);

        if ( eoh )
          {
            char *foo;
            Httpx_Analyze_Headers(httpx);
            httpx->left = 0;
            
            if ( foo = Dicto_Get(httpx->hdrs,"Content-Length",0) )
              httpx->left = Yo_Minu(Str_To_Int(foo),httpx->maxcount);
              
            if ( httpx->outstrm && httpx->left != 0 )
              {
                int L = bf->count-eoh;
                L = Yo_MIN(httpx->left,L);
                httpx->left -= L;
                Oj_Write(httpx->outstrm,bf->at+eoh,L,-1);
                httpx->streamed += L;
                bf->count = 0;
                while ( httpx->left )
                  {
                    status = Httpx_Asio_Recv(netchnl,bf->at,bf->capacity,Yo_MIN(httpx->left,bf->capacity)
                                          ,httpx
                                          ,Cbk_Httpx_Getting_Content);
                    if ( status != HTTPX_CONTINUE )
                      return status;
                  }
              }
              
            return HTTPX_FINISHED;
          }
        
        if ( iterate )
           return HTTPX_CONTINUE;
           
        Buffer_Grow_Reserve(bf,bf->count + 512);
        status = Httpx_Asio_Recv(netchnl,bf->at,512,1,
                          ,httpx
                          ,Cbk_Httpx_Getting_Headers);    

        if ( status != HTTPX_CONTINUE )
          return status;
      }
  }
#endif
  ;
  
int Cbk_Httpx_Requested(void *netchnl, void *_httpx, int status)
#ifdef _YOYO_HTTPX_BUILTIN
  {
    YOYO_HTTPX *httpx = _httpx;
    if ( !ASIO_SUCCEEDED(status) )
      return Httpx_Do_Callback(httpx,HTTPX_BROKEN|HTTPX_REQUESTED);
    httpx->state = HTTPX_REQUESTED;
    Httpx_Do_Callback(httpx,HTTPX_REQUESTED);
    Buffer_Grow_Reserve(httpx->bf,1023);
    return Httpx_Asio_Recv(netchnl, httpx->bf->at, 1023, 12 /* lengthof HTTP/1.1 200 */
                        ,httpx
                        ,Cbk_Httpx_Getting_Headers);    
  }
#endif
  ;
  
int Cbk_Httpx_Connected(void *netchnl,void *_httpx,int status)
#ifdef _YOYO_HTTPX_BUILTIN
  {
    YOYO_HTTPX *httpx = _httpx;
    if ( !ASIO_SUCCEEDED(status) )
      return Httpx_Do_Callback(httpx,HTTPX_BROKEN|HTTPX_CONNECTED);
    httpx->state = HTTPX_CONNECTED;
    Httpx_Do_Callback(httpx,HTTPX_CONNECTED);
    /* httpx->bf contains prepared request */
    return Httpx_Asio_Send(netchnl,httpx->bf->at,httpx->bf->count,httpx,Cbk_Httpx_Requested);    
  }
#endif
  ;
  
int Cbk_Httpx_Proxy_Connected(void *netchnl,void *_httpx,int status)
#ifdef _YOYO_HTTPX_BUILTIN
  {
    YOYO_HTTPX *httpx = _httpx;
    if ( !ASIO_SUCCEEDED(status) )
      return Httpx_Do_Callback(httpx,HTTPX_BROKEN|HTTPX_PROXY_CONNECTED);
    httpx->state = HTTPX_PROXY_CONNECTED;
    Httpx_Do_Callback(httpx,HTTPX_PROXY_CONNECTED);
    
    /// auth / ssl connect
    return Cbk_Httpx_Connected(netchnl,_httpx,status);
  }
#endif
  ;
  
int Httpx_Asio_Connect(YOYO_HTTPX *httpx)
#ifdef _YOYO_HTTPX_BUILTIN
  {
    __Unrefe(httpx->netchnl);
    httpx->netchnl = Tcp_Socket(httpx->async?TCPSOK_ASYNC:0);
    
    if ( httpx->proxy_url )
      return Tcp_Asio_Connect(httpx->netchnl,httpx->proxy_url->host,httpx->proxy_url->port,httpx,Cbk_Httpx_Proxy_Connected);
      
    return Tcp_Asio_Connect(httpx->netchnl,httpx->url->host,httpx->url->port,httpx,Cbk_Httpx_Connected);
  }
#endif
  ;

void Build_Http_Request(YOYO_HTTPX *httpx, int method, char *uri, YOYO_DICTO *hdrs)
#ifdef _YOYO_HTTPX_BUILTIN
  {
  }
#endif
  ;
  
int Httpx_Query(YOYO_HTTPX *httpx, int method, char *uri, YOYO_DICTO *hdrs, void *poststrm, void *contstrm, int maxcount)
#ifdef _YOYO_HTTPX_BUILTIN
  {
    if ( httpx->status_text ) { free(httpx->status_text); httpx->status_text = 0; }
    httpx->status = 0;
    httpx->method = method;
    httpx->maxcount = maxcount;
    httpx->streamed = 0;
    Build_Http_Request(httpx,method,uri,hdrs);
    return Httpx_Asio_Connect(httpx->netchnl,httpx,Cbk_Httpx_Connected);
  }
#endif
  ;
  
int Httpx_Head(YOYO_HTTPX *httpx, char *uri)
#ifdef _YOYO_HTTPX_BUILTIN
  {
    return Httpx_Query(httpx,HTTPX_GET,uri,0,0,0,0);
  }
#endif
  ;
  
int Httpx_Get(YOYO_HTTPX *httpx, char *uri, YOYO_DICTO *params, void *contstrm, int maxcount)
#ifdef _YOYO_HTTPX_BUILTIN
  {
    uri = Url_Compose(uri,params,0);
    return Httpx_Query(httpx,HTTPX_GET,uri,0,0,contstrm,maxcount);
  }
#endif
  ;
  
int Httpx_Post(YOYO_HTTPX *httpx, char *uri, YOYO_DICTO *params, void *contstrm, int maxcount)
#ifdef _YOYO_HTTPX_BUILTIN
  {
    void *poststrm = 0;
    if ( params ) 
      {
        YOYO_BUFFER *bf = Buffer_Init(0);
        Url_Xform_Encode(params,Buffer_Init(0));
        poststrm = Buffer_As_File(bf);
      } 
    return Httpx_Query(httpx,HTTPX_GET,uri,0,poststrm,contstrm,maxcount);
  }
#endif
  ;
  




enum
  {
    YOYO_HTTPX_DOING_NOTHING      = 0,
    YOYO_HTTPX_PREPARING       = 1,
    YOYO_HTTPX_RESOLVING       = 2,
    YOYO_HTTPX_CONNECTING      = 3,
    YOYO_HTTPX_QUERYING        = 4,
    YOYO_HTTPX_GETTING_STATUS  = 5,
    YOYO_HTTPX_GETTING_HEADERS = 6,
    YOYO_HTTPX_GETTING_CONTENT = 7,
    YOYO_HTTPX_FINISHED        = 8,
    YOYO_HTTPX_FAILED          = 9,
    YOYO_HTTPX_RESOLVING_ERROR    = 0x1001,
    YOYO_HTTPX_URLPARSING_ERROR   = 0x1002,
    YOYO_HTTPX_GETTING_ERROR      = 0x1003,
    
    YOYO_HTTPX_GET                = __FOUR_CHARS('G','E','T','_'),
    YOYO_HTTPX_PUT                = __FOUR_CHARS('P','U','T','_'),
    YOYO_HTTPX_POST               = __FOUR_CHARS('P','O','S','T'),
    YOYO_HTTPX_HEAD               = __FOUR_CHARS('H','E','A','D'),
  };

enum 
  {
    YOYO_URL_UNKNOWN  = 0,
    YOYO_URL_HTTP     = 80,
    YOYO_URL_HTTPS    = 443,
    YOYO_URL_FILE     = -1,
  };

typedef struct _YOYO_HTTPX_NTFY
  {
    void (*progress)(struct _YOYO_HTTPX_NTFY *, int st, int count, int total);
    void (*error)(struct _YOYO_HTTPX_NTFY *, int st, char *msg);
  } YOYO_HTTPX_NTFY;


typedef struct _YOYO_HTTPX
  {
    void *strm;
    YOYO_BIO *bio_in;
    YOYO_BIO *bio_out;
    
    YOYO_DICTO *hdrs;
    int  code;
    char *text;
    int  content_length;
    char *content_type; /* handles by hdrs */
    
    int http10: 1;
    int heepalive: 1;
  } YOYO_HTTPX;

YOYO_HTTPX_Destruct(YOYO_HTTPX *httpx)
#ifdef _YOYO_HTTPX_BUILTIN
  {
    Oj_Close(httpx->strm);
    __Unrefe(httpx->bio_in);
    __Unrefe(httpx->bio_out);
    __Unrefe(httpx->strm);
    __Unrefe(httpx->hdrs);
    free(http->text);
    __Destruct(httpx);
  }
#endif
  ;
  
YOYO_HTTPX *Httpx_Connect(int proto, char *host, int port)
#ifdef _YOYO_HTTPX_BUILTIN
  {
    YOYO_HTTPX *httpx = 0;
    __Auto_Ptr(cnt) 
      {
        YOYO_TCPSOK *sok;
        
        sok = Tcp_Open(host,port);
        
        httpx = __Object_Dtor(sizeof(YOYO_HTTPX),YOYO_HTTPX_Destruct);
        httpx->strm    = __Refe(sok);
        httpx->bio_in  = __Refe(Bio_Input(httpx->strm));
        httpx->bio_out = __Refe(Bio_Output(httpx->strm));
      }
    return cnt;
  }
#endif

void Httpx_Send_Header_Line(YOYO_BIO *bio, char *name, char *value)
#ifdef _YOYO_HTTPX_BUILTIN
  {
    Bio_Write(bio,name,-1);
    Bio_Write(bio,": ",2);
    Bio_Write(bio,value,-1);
    Bio_Write(bio,"\r\n",2);
  }
#endif
  ;
  
void Httpx_Send_Header_Line_Filtered(YOYO_BIO *bio, char *name, char *value)
#ifdef _YOYO_HTTPX_BUILTIN
  {
    if ( !strcmp_I(name,"Host")
       ||!strcmp_I(name,"User-Agent")
       ||!strcmp_I(name,"Accept-Charset")
       ||!strcmp_I(name,"Accept")
       ||!strcmp_I(name,"Content-Type")
       ||!strcmp_I(name,"Connection")
       ||!strcmp_I(name,"Content-Length") )
      return;
      
    Httpx_Send_Header_Line(bio,name,value); 
  }
#endif  
  ;
    
void Httpx_Send_Common_Header_Line(YOYO_DICTO *hdrs,char *name, char *dflt,YOYO_BIO *bio)
#ifdef _YOYO_HTTPX_BUILTIN
  {
    char *val = hdrs?Dicto_Get(hdrs,name,dlft):dflt;
    Httpx_Send_Header_Line(bio,name,val); 
  }
#endif
  ;
    
void Httpx_Send_Request_Line(YOYO_BIO *bio, int method, char *uri, int http10)
#ifdef _YOYO_HTTPX_BUILTIN
  {
    static char s_GET[3]  = "GET";
    static char s_PUT[3]  = "PUT";
    static char s_POST[4] = "POST";
    static char s_HEAD[4] = "HEAD";
    
    switch ( method )
      {
        case YOYO_HTTPX_GET:  Bio_Write(bio,s_GET,sizeof(s_GET)); break;
        case YOYO_HTTPX_PUT:  Bio_Write(bio,s_PUT,sizeof(s_PUT)); break;
        case YOYO_HTTPX_POST: Bio_Write(bio,s_POST,sizeof(s_POST)); break;
        case YOYO_HTTPX_HEAD: Bio_Write(bio,s_HEAD,sizeof(s_HEAD)); break;
        default:
          __Raise_Format(YOYO_ERROR_ILLFORMED,(__yoTa("invalid HTTPX request method: %d",0),method);
      }
      
    Bio_Write(bio," ",1);
    Bio_Write(bio,uri,-1);
    
    if ( http10 )
      Bio_Write(bio,"HTTP/1.0\r\n",10);
    else
      Bio_Write(bio,"HTTP/1.1\r\n",10);
  }
#endif
  ;
  
void Httpx_Send_Request(YOYO_HTTPX *httpx, YOYO_URL *url, int method, YOYO_DICTO *hdrs, void *strm)
#ifdef _YOYO_HTTPX_BUILTIN
  {
    YOYO_BIO *bio = httpx->bio_out;
    Bio_Reset(&httpx->bio_out);    
    Httpx_Send_Request_Line(bio,method,url->uri,httpx->http10);
    Httpx_Send_Common_Header_Line(hdrs,"Host",url->host,bio);
    Httpx_Send_Common_Header_Line(hdrs,"User-Agent","httpx",bio);
    Httpx_Send_Common_Header_Line(hdrs,"Accept","*/*",bio);
    Httpx_Send_Common_Header_Line(hdrs,"Accept-Charset","utf-8",bio);
    if ( hdrs ) 
      Dicto_Apply(hdrs,Httpx_Send_Header_Line_Filtered,bio);
    if ( strm && ( method == YOYO_HTTPX_POST || method == YOYO_HTTPX_PUT ) ) 
      {
        quad_t length = Oj_Available(strm);
        char len[13];
        snprintf(len,"%d",sizeof(len),(int)length);
        if ( method == YOYO_HTTPX_POST )
          Httpx_Send_Common_Header_Line(hdrs,"Content-Type","application/x-www-form-urlencoded",bio);
        else  
          Httpx_Send_Common_Header_Line(hdrs,"Content-Type","application/octet-stream",bio);
        Httpx_Send_Header_Line("Content-Length",len);
        Bio_Write(bio,"\r\n",2);
        Bio_Copy_Into(bio,strm);
      }
    else
      Bio_Write(bio,"\r\n",2);
    Bio_Flush(bio);
  }
#endif
  ;
  
void Httpx_Recv_Response(YOYO_HTTPX *httpx)
#ifdef _YOYO_HTTPX_BUILTIN
  {
    char *S = 0;
    YOYO_BUFFER *bf = Buffer_Init(0);
    YOYO_BIO *bio = httpx->bio_in;
    Bio_Reset(bio);
    S = Bio_Read_Line(bio,bf);
    httpx->content_length = 0;
    httpx->content_type = 0;
    
    if ( strcmp_I(S,"http/1.") )
      {
        while ( *S && !Isspace(*S) ) ++S;
        while ( Isspace(*S) ) ++S;
        httpx->code = strtol(S,&S,10);
        while ( Isspace(*S) ) ++S;
        httpx->text = Str_Copy(S,-1);
      }
    else
      __Raise_Format(YOYO_ERROR_ILLFORMED,(__yoTa("illformed http response: %.10s",0),S));
        
    for (;;)
      {
        char *q, *Q;
        S = Bio_Read_Line(bio,bf);
        while (Isspace(*S)) ++S;
        if ( !*S ) break; 
        q = S;
        while ( *q && *q != ':' ) ++q;
        if ( *q == ':' )
          {
            *q = 0;
            ++q;
            while ( Isspace(*q) ) ++q;
            Dicto_Put(httpx->hdrs,S,(Q=Str_Copy_Npl(q,-1)));
            if ( !strcmp_I(S,"Content-Length") )
              httpx->content_length = Str_To_Int(q);
            else if ( !strcmp_I(S,"Content-Type") )
              httpx->content_type = Q;
          }
      }
  }
#endif
  ;
  
int Httpx_Request(YOYO_HTTPX *httpx, char *uri, int method, YOYO_DICTO *hdrs, void *strm)
#ifdef _YOYO_HTTPX_BUILTIN
  {
    __Auto_Release
      {
        Httpx_Send_Request(httpx,uri,method,hdrs,strm);
        Httpx_Recv_Response(httpx);
      }
      
    return httpx->code;
  }
#endif
  ;
  
#define Httpx_Clear_Cookies(Hdrs) Httpx_Set_Cookie(Hdrs,0,0,0)   
void Httpx_Set_Cookie(YOYO_DICTO *hdrs, char *name, char *value, quad_t lifetime)
#ifdef _YOYO_HTTPX_BUILTIN
  {
  }
#endif
  ;
    
char *Httpx_Get_Cookie(YOYO_DICTO *hdrs, char *name, quad_t *lifetime)
#ifdef _YOYO_HTTPX_BUILTIN
  {
    return 0;
  }
#endif
  ;
  
#define Httpx_Read_All(Httpx,Out) Httpx_Read(Httpx,Out,-1)
YOYO_BUFFER *Httpx_Read(YOYO_HTTPX *httpx, YOYO_BUFFER *out, int count)
#ifdef _YOYO_HTTPX_BUILTIN
  {
    if ( !out )
      out = Buffer_Init(0);

    __Auto_Release
      {
        void *f = Buffer_As_File(out);
        Bio_Copy_From(httpx->bio_in,f,count);
      }
      
    return out;
  }
#endif
  ;
  
void Httpx_Close(YOYO_HTTPX *httpx)
#ifdef _YOYO_HTTPX_BUILTIN
  {
    Oj_Close(httpx->strm);
  }
#endif
  ;

#endif /* C_once_DAA1D391_392F_467C_B5A8_6C8D0D3A9DCC */




|







 







|


<
<
<
<
<
<
<
|







 







>




>









|
>


>
>
>
>
>
>
>
>
>
>
>
>
>
>
>












|
<
|

|




>
|

|
|
|






|





|
|



>




|
|



>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>




>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
|
|

|







 







<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<


1
2
3
4
5
6
7
8
9
10
11
..
52
53
54
55
56
57
58
59
60
61







62
63
64
65
66
67
68
69
..
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125

126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
...
782
783
784
785
786
787
788































































































































































































































































































































































































































































































































































































789
790

/*

Copyright © 2010-2012, Alexéy Sudachén, alexey@sudachen.name, Chile

In USA, UK, Japan and other countries allowing software patents:

    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.
................................................................................
#include "dicto.hc"
#include "file.hc"
#include "tcpip.hc"
#include "url.hc"
#include "asio.hc"

#ifdef _LIBYOYO
#define _HTTPX_BUILTIN
#endif








typedef void (*httpx_collback_t)(void *obj, struct _YOYO_HTTPX *httpx, int status);

typedef struct _YOYO_HTTPX
  {
    void *netchnl;
    YOYO_URL *url;
    YOYO_URL *proxy_url;
    
................................................................................
    int  left;
    
    void *outstrm;
    void *poststrm;
    void *mon_obj;
    httpx_collback_t mon_callback;

    int nline;
    YOYO_BUFFER *bf;
    
    YOYO_DICTO  *hdrs;
    YOYO_BUFFER *content;
    int   state;
    int   streamed;
    int   status;
    char *status_text;
    
    int async:      1;
    int keepalive:  1;
    int legacy10:   1;
    int ssl:        1;
    int proxy:      1;
    int interrupted:1;
    int chunked:    1;
        
  } YOYO_HTTPX;

void YOYO_HTTPX_Destruct(YOYO_HTTPX *httpx)
  {
    __Unrefe(httpx->netchnl);
    __Unrefe(httpx->url);
    __Unrefe(httpx->proxy_url);
    __Unrefe(httpx->outstrm);
    __Unrefe(httpx->poststrm);
    __Unrefe(httpx->mon_obj);
    __Unrefe(httpx->bf);
    __Unrefe(httpx->hdrs);
    __Unrefe(httpx->content);
    free(httpx->status_text);
    __Destruct(httpx);
  }

enum 
  {
    HTTPX_ASYNC       = 1,
    HTTPX_KEEPALIVE   = 2,
    HTTPX_LEGACY_10   = 4,
    
    HTTPX_GET         = __FOUR_CHARS('G','E','T','_'),
    HTTPX_PUT         = __FOUR_CHARS('P','U','T','_'),
    HTTPX_POST        = __FOUR_CHARS('P','O','S','T'),
    HTTPX_HEAD        = __FOUR_CHARS('H','E','A','D'),
  
    HTTPX_BROKEN                  = 0x80000000,

    HTTPX_TIMER                   = 0x00001000,
    
    HTTPX_INITILIZED              = 0x00000f00,
    HTTPX_PROXY_CONNECTED         = 0x00000f01,
    HTTPX_PROXY_AUTHORIZED        = 0x00000f02,
    HTTPX_SSL_CONNECTED           = 0x00000f03,
    HTTPX_CONNECTED               = 0x00000f10,
    HTTPX_SENDING_POSTDATA        = 0x00000f20,
    HTTPX_REQUESTED               = 0x00000f21,
    HTTPX_GETTING_HEADERS         = 0x00000f30,
    HTTPX_RESPONDED               = 0x00000f31,
    HTTPX_GETTING_CONTENT         = 0x00000f40,
    HTTPX_FINISHED                = 0x00000f41,
    HTTPX_SEND                    = 0x00000e01,
    HTTPX_RECV                    = 0x00000e02,

    HTTPX_INVALID_RESPONSE        = 0x00000d01,
    HTTPX_INVALID_RESPONSE_CODE   = 0x00000d02,

    HTTPX_CONTINUE                = -1,
  };

#define HTTPX_SUCCEEDED(Status) (((Status)&0x8f000000) == 0)
#define HTTPX_STATUS(Status)    ((Status)&0x0fff)

int Httpx_Asio_Recv(YOYO_HTTPX *httpx, void *dta, int count, int mincount, asio_recv_callback_t callback)
#ifdef _HTTPX_BUILTIN
  {
    if ( !httpx->ssl )
      return Tcp_Asio_Recv(httpx->netchnl,dta,count,mincount,httpx,callback);
    return 0;
  }
#endif
  ;
  
int Httpx_Asio_Send(YOYO_HTTPX *httpx, void *dta, int count, asio_send_callback_t callback)
#ifdef _HTTPX_BUILTIN
  {
    if ( !httpx->ssl )
      return Tcp_Asio_Send(httpx->netchnl,dta,count,httpx,callback);
    return 0;
  }
#endif
  ;
  
void Httpx_Append_Header_Line(YOYO_BUFFER *bf, char *name, char *value)
#ifdef _HTTPX_BUILTIN
  {
    Buffer_Append(bf,name,-1);
    Buffer_Append(bf,": ",2);
    Buffer_Append(bf,value,-1);
    Buffer_Append(bf,"\r\n",2);
  }
#endif
  ;

void Httpx_Append_Header_Line_Filtered(char *name, void *value, void *bf)
#ifdef _HTTPX_BUILTIN
  {
    if ( !strcmp_I(name,"Host")
       ||!strcmp_I(name,"User-Agent")
       ||!strcmp_I(name,"Accept-Charset")
       ||!strcmp_I(name,"Accept")
       ||!strcmp_I(name,"Content-Type")
       ||!strcmp_I(name,"Connection")
       ||!strcmp_I(name,"Content-Length") )
      return;
      
    Httpx_Append_Header_Line(bf,name,value); 
  }
#endif  
  ;
  
void Httpx_Append_Common_Header_Line(YOYO_BUFFER *bf, YOYO_DICTO *hdrs,char *name, char *dflt)
#ifdef _HTTPX_BUILTIN
  {
    char *val = hdrs?Dicto_Get(hdrs,name,dflt):dflt;
    Httpx_Append_Header_Line(bf,name,val); 
  }
#endif
  ;
    
void Httpx_Append_Request_Line(YOYO_BUFFER *bf, int method, char *uri, int http10)
#ifdef _HTTPX_BUILTIN
  {
    static char s_GET[3]  = "GET";
    static char s_PUT[3]  = "PUT";
    static char s_POST[4] = "POST";
    static char s_HEAD[4] = "HEAD";
    
    switch ( method )
      {
        case HTTPX_GET:  Buffer_Append(bf,s_GET,sizeof(s_GET)); break;
        case HTTPX_PUT:  Buffer_Append(bf,s_PUT,sizeof(s_PUT)); break;
        case HTTPX_POST: Buffer_Append(bf,s_POST,sizeof(s_POST)); break;
        case HTTPX_HEAD: Buffer_Append(bf,s_HEAD,sizeof(s_HEAD)); break;
        default:
          __Raise_Format(YOYO_ERROR_ILLFORMED,(__yoTa("invalid HTTPX request method: %d",0),method));
      }
      
    Buffer_Append(bf," ",1);
    Buffer_Append(bf,uri,-1);
    
    if ( http10 )
      Buffer_Append(bf," HTTP/1.0\r\n",11);
    else
      Buffer_Append(bf," HTTP/1.1\r\n",11);
  }
#endif
  ;
  
void Build_Http_Request(YOYO_HTTPX *httpx, int method, char *uri, YOYO_DICTO *hdrs)
#ifdef _HTTPX_BUILTIN
  {
    YOYO_BUFFER *bf = httpx->bf;
    bf->count = 0;
    Httpx_Append_Request_Line(bf,method,uri,httpx->legacy10);
    Httpx_Append_Common_Header_Line(bf,hdrs,"Host",httpx->url->host);
    Httpx_Append_Common_Header_Line(bf,hdrs,"User-Agent","httpx");
    Httpx_Append_Common_Header_Line(bf,hdrs,"Accept","*/*");
    Httpx_Append_Common_Header_Line(bf,hdrs,"Accept-Charset","utf-8");
    if ( hdrs ) 
      Dicto_Apply(hdrs,Httpx_Append_Header_Line_Filtered,bf);
    if ( method == HTTPX_POST || method == HTTPX_PUT )
      {
        char len[13];
        quad_t qlen = httpx->poststrm?Oj_Available(httpx->poststrm):0;
        sprintf(len,"%u",(unsigned int)qlen);
        if ( method == HTTPX_POST )
          Httpx_Append_Common_Header_Line(bf,hdrs,"Content-Type","application/x-www-form-urlencoded");
        else
          Httpx_Append_Common_Header_Line(bf,hdrs,"Content-Type","application/octet-stream");
        Httpx_Append_Header_Line(bf,"Content-Length",len);
      }
    Buffer_Append(bf,"\r\n",2);      
  }
#endif
  ;

void Httpx_Interrupt(YOYO_HTTPX *httpx)
  {
    if ( !httpx->ssl )
      Tcp_Asio_Interrupt(httpx->netchnl);
    STRICT_REQUIRE( httpx->interrupted == 1 );
  }
  
void Httpx_Do_Callback(YOYO_HTTPX *httpx, int status)
#ifdef _HTTPX_BUILTIN
  {
    if ( httpx->mon_callback )
      httpx->mon_callback(httpx->mon_obj,httpx,httpx->state|status);

    if ( status & HTTPX_BROKEN ) /* Asio interrupt callback fall here */
      httpx->interrupted = 1;
         
    if ( ( httpx->interrupted )
      || ( httpx->state == HTTPX_FINISHED && !httpx->keepalive ) )
      {
        Oj_Close(httpx->netchnl); 
      }
  }
#endif
  ;
  
void Httpx_Timer_Callback(YOYO_HTTPX *httpx)
#ifdef _HTTPX_BUILTIN
  {
    if ( httpx->mon_callback )
      httpx->mon_callback(httpx->mon_obj,httpx,httpx->state|HTTPX_TIMER);    
  }
#endif
  ;

int Httpx_Update_Until_EOH(YOYO_HTTPX *httpx, int count)
#ifdef _HTTPX_BUILTIN
  {
    YOYO_BUFFER *bf = httpx->bf;
    int i = bf->count;
    bf->count += count;

    for ( ; i < bf->count; ++i )
      {
        if ( bf->at[i] == '\r' && bf->at[i+1] == '\n' )
          { if ( httpx->nline ) return i+2; httpx->nline = 1; ++i; } 
        else if ( bf->at[i] == '\n' )
          { if ( httpx->nline ) return i+1;  httpx->nline = 1; } 
        else
          httpx->nline = 0; 
        ++i;
      }
      
    return 0;
  }      
#endif
  ;
  
void Httpx_Analyze_Headers(YOYO_HTTPX *httpx)
#ifdef _HTTPX_BUILTIN
  {
    int i;
    char *q, *Q, *S = httpx->bf->at;
    while (*S!='\n') ++S; /* skip HTTP/1.x line */
    q = ++S;
    for (;;)
      {
        while ( *q != '\n' ) ++q;
        STRICT_REQUIRE(q >= httpx->bf->at && q < httpx->bf->at + httpx->bf->capacity);
        *q = 0;
        for ( i = 1; q-i > S && Isspace(q[-i]); ++i ) q[-i] = 0;
        while (Isspace(*S)) ++S;
        if ( !*S ) break; /* empty line => end of headers*/ 
        Q = S;
        puts(S);
        while ( *Q && *Q != ':' ) ++Q;
        STRICT_REQUIRE(Q >= httpx->bf->at && Q < httpx->bf->at + httpx->bf->capacity);
        if ( *Q == ':' )
          {
            *Q = 0;
            for ( i = 1; Q-i > S && Isspace(Q[-i]); ++i ) Q[-i] = 0;
            ++Q;
            while ( Isspace(*Q) ) ++Q;
            Dicto_Put(httpx->hdrs,S,(Q=Str_Copy_Npl(Q,-1)));
          }
        S = ++q;
      }
  }
#endif
  ;
  
int Cbk_Httpx_Getting_Content(void *_httpx, int status, int count)
#ifdef _HTTPX_BUILTIN
  {
    int L;
    YOYO_HTTPX *httpx = _httpx;
    YOYO_BUFFER *bf = httpx->bf;
    
    if ( status == ASIO_TIMER )
      return Httpx_Timer_Callback(httpx),0;

    if ( !ASIO_SUCCEEDED(status) )
      return Httpx_Do_Callback(httpx,HTTPX_BROKEN),
             httpx->state;

    L = Yo_MIN(httpx->left,count);
    httpx->left -= L;
    httpx->streamed += L;
    Oj_Write(httpx->outstrm,bf->at,L,-1);

    if ( !httpx->left )
      return httpx->state = HTTPX_FINISHED,
             Httpx_Do_Callback(httpx,0),
             httpx->state;

    if ( status&ASIO_SYNCHRONOUS )
      return HTTPX_CONTINUE;
    
    return Httpx_Asio_Recv(httpx, bf->at, bf->capacity, Yo_MIN(httpx->left,bf->capacity)
                          ,Cbk_Httpx_Getting_Content);
  }
#endif
  ;

int Cbk_Http_Getting_Chunked(void *_httpx, int status, int count)
#ifdef _HTTPX_BUILTIN
  {
    YOYO_HTTPX *httpx = _httpx;
    YOYO_BUFFER *bf = httpx->bf;
    int iterate = 0;
    
    if ( status == ASIO_TIMER )
      return Httpx_Timer_Callback(httpx),0;

    httpx->state = HTTPX_GETTING_CONTENT;
    if ( !ASIO_SUCCEEDED(status) )
      return Httpx_Do_Callback(httpx,HTTPX_BROKEN),
             httpx->state;
  
    bf->count += count;
  
    if ( status & ASIO_SYNCHRONOUS )
      return HTTPX_CONTINUE;
  
    if ( httpx->outstrm ) for (;;)
      {
        if ( httpx->left )
          {
            int L = Yo_Minu(httpx->left,bf->count-httpx->nline);
            Oj_Write(httpx->outstrm,bf->at+httpx->nline,L,L);
            httpx->left -= L;
            if ( httpx->left )
              {
                bf->count = 0;
                httpx->nline = 0;
                if ( (status = Httpx_Asio_Recv(httpx,bf->at,bf->capacity,Yo_Minu(httpx->left,bf->capacity)
                                            ,Cbk_Http_Getting_Chunked)) 
                             != HTTPX_CONTINUE )
                    return status;
              }
            else
              {
                httpx->nline += L;
              }
          }
        else
          {
            int i = httpx->nline;
            byte_t *q = bf->at;
            while ( i < bf->count && q[i] != '\n' ) ++i;
            if ( q[i] == '\n' )
              {
                char *e = 0;
                ++i;
                if ( Isspace(*(q+httpx->nline)) ) 
                  {
                    httpx->nline = i;
                  }
                else
                  {
                    httpx->left = strtol(q+httpx->nline,&e,16);
                    if ( !httpx->left && *(q+httpx->nline) == '0' )
                      {
                        /* fixme */
                        if ( q[i] != '\n' && !(q[i] == '\r' && q[i+1] == '\n' ) )
                          {
                            Buffer_Grow_Reserve(bf,bf->count+2);
                            if ( (status = Httpx_Asio_Recv(httpx,bf->at+bf->count,2,1
                                                         ,Cbk_Http_Getting_Chunked)) 
                                         != HTTPX_CONTINUE )
                            continue;
                          }
                        break; // finish
                      }
                    httpx->nline = i;
                  }
              }
            else  
              {
                Buffer_Grow_Reserve(bf,bf->count+128);
                if ( (status = Httpx_Asio_Recv(httpx,bf->at+bf->count,128,1
                                               ,Cbk_Http_Getting_Chunked)) 
                               != HTTPX_CONTINUE )
                  return status;
              } 
          }
      }

    return httpx->state = HTTPX_FINISHED,
           Httpx_Do_Callback(httpx,0),
           httpx->state;
  }
#endif
  ;
    
int Cbk_Httpx_Getting_Headers(void *_httpx, int status, int count)
#ifdef _HTTPX_BUILTIN
  {
    YOYO_HTTPX *httpx = _httpx;
    YOYO_BUFFER *bf = httpx->bf;
    int iterate = 0;
    
    if ( status == ASIO_TIMER )
      return Httpx_Timer_Callback(httpx),0;

    httpx->state = HTTPX_GETTING_HEADERS;
    if ( !ASIO_SUCCEEDED(status) )
      return Httpx_Do_Callback(httpx,HTTPX_BROKEN),
             httpx->state;
    
    if ( httpx->bf->count == 0 )
      {
        if ( !strncmp_I(bf->at,"HTTP/1.",7) )
          {
            char *q = bf->at + 8, *Q;
            bf->at[count] = 0;
            while ( *q && !Isspace(*q) ) ++q;
            while ( Isspace(*q) ) ++q;
            httpx->status = strtol(q,&q,10);
            
            if ( httpx->status < 100 || httpx->status >= 510 )
              return  httpx->state = HTTPX_INVALID_RESPONSE_CODE,
                      Httpx_Do_Callback(httpx,HTTPX_BROKEN),
                      httpx->state;
                          
            while ( *q != '\n' && *q != '\r' && Isspace(*q) ) ++q;
            Q = q;
            while ( *q && *q != '\n' && *q != '\r' && !Isspace(*q) ) ++q;
            httpx->status_text = Str_Range_Npl(Q,q);
            bf->count = (q - bf->at);         
            count -= bf->count; 
            httpx->nline = 0;
            __Unrefe(httpx->hdrs);
            httpx->hdrs = __Refe(Dicto_Ptrs());
          }
        else
          return  httpx->state = HTTPX_INVALID_RESPONSE,
                  Httpx_Do_Callback(httpx,HTTPX_BROKEN),
                  httpx->state;
      }
    else
      iterate = status&ASIO_SYNCHRONOUS;
      
    for(;;) 
      {
        int eoh = Httpx_Update_Until_EOH(httpx,count);

        if ( eoh )
          {
            char *foo;
            Httpx_Analyze_Headers(httpx);
            httpx->left = 0;
            
            if ( foo = Dicto_Get(httpx->hdrs,"Content-Length",0) )
              {
                httpx->left = Yo_Minu(Str_To_Int(foo),httpx->maxcount);
              }
            else if ( strcmp_I(Dicto_Get(httpx->hdrs,"Transfer-Encoding",""),"chunked") == 0 )
              {
                httpx->chunked = 1;  
                httpx->nline = 0;
              }
              
            if ( httpx->outstrm && httpx->left != 0 )
              {
                int L = bf->count-eoh;
                L = Yo_MIN(httpx->left,L);
                httpx->left -= L;
                Oj_Write(httpx->outstrm,bf->at+eoh,L,-1);
                httpx->streamed += L;
                bf->count = 0;
                while ( httpx->left )
                  {
                    status = Httpx_Asio_Recv(httpx,bf->at,bf->capacity,Yo_MIN(httpx->left,bf->capacity)
                                            ,Cbk_Httpx_Getting_Content);
                    if ( status != HTTPX_CONTINUE )
                      return status;
                  }
              }
            else if ( httpx->chunked )
              {
                httpx->nline = eoh;
                httpx->left = 0;
                return Cbk_Http_Getting_Chunked(httpx,0,0);
              }
            
            return httpx->state = HTTPX_FINISHED,
                   Httpx_Do_Callback(httpx,0),
                   httpx->state;
          }
        
        if ( iterate )
           return HTTPX_CONTINUE;
           
        Buffer_Grow_Reserve(bf,bf->count + 512);
        
        status = Httpx_Asio_Recv(httpx, bf->at+bf->count, 512, 1
                                ,Cbk_Httpx_Getting_Headers);    

        if ( status != HTTPX_CONTINUE )
          return status;
      }
  }
#endif
  ;
  
int Cbk_Httpx_Requested(void *_httpx, int status)
#ifdef _HTTPX_BUILTIN
  {
    YOYO_HTTPX *httpx = _httpx;

    if ( status == ASIO_TIMER )
      return Httpx_Timer_Callback(httpx),0;

    httpx->state = HTTPX_REQUESTED;
    if ( !ASIO_SUCCEEDED(status) )
      return Httpx_Do_Callback(httpx,HTTPX_BROKEN),
             httpx->state;

    Httpx_Do_Callback(httpx,HTTPX_REQUESTED);
    Buffer_Grow_Reserve(httpx->bf,1023);
    httpx->bf->count = 0;
    
    return Httpx_Asio_Recv(httpx, httpx->bf->at, 1023, 12 /* lengthof HTTP/1.1 200 */
                          ,Cbk_Httpx_Getting_Headers);    
  }
#endif
  ;
  
int Cbk_Httpx_Post(void *_httpx, int status)
#ifdef _HTTPX_BUILTIN
  {
    YOYO_HTTPX *httpx = _httpx;
    YOYO_BUFFER *bf = httpx->bf;
    int i;

    if ( status == ASIO_TIMER )
      return Httpx_Timer_Callback(httpx),0;

    if ( httpx->state == HTTPX_SENDING_POSTDATA && (status & ASIO_SYNCHRONOUS) )
      return HTTPX_CONTINUE;

    if ( httpx->state != HTTPX_SENDING_POSTDATA )
      {
        bf->count = 0;
        httpx->left = httpx->poststrm?Oj_Available(httpx->poststrm):0;
      }

    httpx->state = HTTPX_SENDING_POSTDATA;
    if ( !ASIO_SUCCEEDED(status) )
      return Httpx_Do_Callback(httpx,HTTPX_BROKEN),
             httpx->state;
    
    for ( ;; )
      {
        bf->count = 0;
        i = !httpx->left ? 0 : Oj_Read(httpx->poststrm,bf->at,Yo_MIN(httpx->left,bf->capacity),-1);
        httpx->left -= i;
        if ( !Httpx_Asio_Send(httpx, bf->at, i,
                              httpx->left ? Cbk_Httpx_Post : Cbk_Httpx_Requested) )
        return 0;
      }
  }
#endif
  ;
    
int Cbk_Httpx_Connected(void *_httpx,int status)
#ifdef _HTTPX_BUILTIN
  {
    YOYO_HTTPX *httpx = _httpx;

    if ( status == ASIO_TIMER )
      return Httpx_Timer_Callback(httpx),0;

    httpx->state = HTTPX_CONNECTED;
    if ( !ASIO_SUCCEEDED(status) )
      return Httpx_Do_Callback(httpx,HTTPX_BROKEN),
             httpx->state;

    Httpx_Do_Callback(httpx,HTTPX_CONNECTED);
    
    /* httpx->bf contains prepared request */
    return Httpx_Asio_Send(httpx, httpx->bf->at, httpx->bf->count,
                           (httpx->method == HTTPX_POST || httpx->method == HTTPX_PUT ) 
                                ?Cbk_Httpx_Post
                                :Cbk_Httpx_Requested);    
  }
#endif
  ;
  
int Cbk_Httpx_Proxy_Connected(void *_httpx,int status)
#ifdef _HTTPX_BUILTIN
  {
    YOYO_HTTPX *httpx = _httpx;

    if ( status == ASIO_TIMER )
      return Httpx_Timer_Callback(httpx),0;
      
    httpx->state = HTTPX_PROXY_CONNECTED;
    if ( !ASIO_SUCCEEDED(status) )
      return Httpx_Do_Callback(httpx,HTTPX_BROKEN),
             httpx->state;

    Httpx_Do_Callback(httpx,HTTPX_PROXY_CONNECTED);
    
    /// auth / ssl connect
    return Cbk_Httpx_Connected(_httpx,status);
  }
#endif
  ;
  
int Httpx_Asio_Connect(YOYO_HTTPX *httpx)
#ifdef _HTTPX_BUILTIN
  {
    __Unrefe(httpx->netchnl);
    httpx->netchnl = __Refe(Tcp_Socket(httpx->async?TCPSOK_ASYNC:0));
    
    if ( httpx->proxy_url )
      return Tcp_Asio_Connect(httpx->netchnl,httpx->proxy_url->host,httpx->proxy_url->port,httpx,Cbk_Httpx_Proxy_Connected);
      
    return Tcp_Asio_Connect(httpx->netchnl,httpx->url->host,httpx->url->port,httpx,Cbk_Httpx_Connected);
  }
#endif
  ;
  
int Httpx_Query(YOYO_HTTPX *httpx, int method, char *uri, YOYO_DICTO *hdrs, void *poststrm, void *contstrm, int maxcount)
#ifdef _HTTPX_BUILTIN
  {
    if ( httpx->status_text ) { free(httpx->status_text); httpx->status_text = 0; }
    httpx->status = 0;
    httpx->method = method;
    httpx->maxcount = maxcount;
    httpx->streamed = 0;
    httpx->state = HTTPX_INITILIZED;
    httpx->outstrm = __Refe(contstrm);
    httpx->poststrm = __Refe(poststrm);
    if ( httpx->bf ) httpx->bf->count = 0;
    else httpx->bf = __Refe(Buffer_Init(0));
    Buffer_Reserve(httpx->bf,4*1024-1);
    Build_Http_Request(httpx,method,uri,hdrs);
    if ( 0 == Httpx_Asio_Connect(httpx) )
      return 1;
    else
      return httpx->state == HTTPX_FINISHED; 
  }
#endif
  ;
  
int Httpx_Head(YOYO_HTTPX *httpx, char *uri)
#ifdef _HTTPX_BUILTIN
  {
    return Httpx_Query(httpx,HTTPX_GET,uri,0,0,0,0);
  }
#endif
  ;
  
int Httpx_Get(YOYO_HTTPX *httpx, char *uri, YOYO_DICTO *params, void *contstrm, int maxcount)
#ifdef _HTTPX_BUILTIN
  {
    uri = Url_Compose(uri,params);
    return Httpx_Query(httpx,HTTPX_GET,uri,0,0,contstrm,maxcount);
  }
#endif
  ;
  
int Httpx_Post(YOYO_HTTPX *httpx, char *uri, YOYO_DICTO *params, void *contstrm, int maxcount)
#ifdef _HTTPX_BUILTIN
  {
    void *poststrm = 0;
    if ( params ) 
      {
        char *encoded = Url_Xform_Encode(params);
        if ( encoded )
          poststrm = Memory_As_File(encoded,strlen(encoded));
      }
    return Httpx_Query(httpx,HTTPX_GET,uri,0,poststrm,contstrm,maxcount);
  }
#endif
  ;

YOYO_HTTPX *Httpx_Client(char *url,int flags)
#ifdef _HTTPX_BUILTIN
  {
    YOYO_HTTPX *httpx = __Object_Dtor(sizeof(YOYO_HTTPX),YOYO_HTTPX_Destruct);
    httpx->url = __Retain(Url_Parse(url));
    
    if ( flags & HTTPX_ASYNC )
      httpx->async = 1;
    if ( flags & HTTPX_KEEPALIVE )
      httpx->keepalive = 1;
    if ( flags & HTTPX_LEGACY_10 )
................................................................................
        httpx->url->port = 80;
        
    return httpx;
  }
#endif
  ;
  































































































































































































































































































































































































































































































































































































#endif /* C_once_DAA1D391_392F_467C_B5A8_6C8D0D3A9DCC */

Changes to libcrypt.hc.

1
2
3
4
5
6
7
8
9
10
11

/*

Copyright © 2010-2011, Alexéy Sudáchen, alexey@sudachen.name, Chile

In USA, UK, Japan and other countries allowing software patents:

    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.



|







1
2
3
4
5
6
7
8
9
10
11

/*

Copyright © 2010-2011, Alexéy Sudachén, alexey@sudachen.name, Chile

In USA, UK, Japan and other countries allowing software patents:

    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

Changes to libyoyo.hc.

1
2
3
4
5
6
7
8
9
10
11

/*

Copyright © 2010-2011, Alexéy Sudáchen, alexey@sudachen.name, Chile

In USA, UK, Japan and other countries allowing software patents:

    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.



|







1
2
3
4
5
6
7
8
9
10
11

/*

Copyright © 2010-2011, Alexéy Sudachén, alexey@sudachen.name, Chile

In USA, UK, Japan and other countries allowing software patents:

    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

Changes to logout.hc.

1
2
3
4
5
6
7
8
9
10
11

/*

Copyright © 2010-2011, Alexéy Sudáchen, alexey@sudachen.name, Chile

In USA, UK, Japan and other countries allowing software patents:

    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.



|







1
2
3
4
5
6
7
8
9
10
11

/*

Copyright © 2010-2011, Alexéy Sudachén, alexey@sudachen.name, Chile

In USA, UK, Japan and other countries allowing software patents:

    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

Changes to lzss.hc.

1
2
3
4
5
6
7
8
9
10
11

/*

Copyright © 2005-2011, Alexéy Sudáchen, alexey@sudachen.name, Chile

In USA, UK, Japan and other countries allowing software patents:

    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.



|







1
2
3
4
5
6
7
8
9
10
11

/*

Copyright © 2005-2011, Alexéy Sudachén, alexey@sudachen.name, Chile

In USA, UK, Japan and other countries allowing software patents:

    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

Changes to md5.hc.

1
2
3
4
5
6
7
8
9
10
11
12
13
14

/*

// The MD5 algorithm was designed by Ron Rivest in 1991.
// http://www.ietf.org/rfc/rfc1321.txt

Copyright © 2010-2011, Alexéy Sudáchen, alexey@sudachen.name, Chile

In USA, UK, Japan and other countries allowing software patents:

    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.






|







1
2
3
4
5
6
7
8
9
10
11
12
13
14

/*

// The MD5 algorithm was designed by Ron Rivest in 1991.
// http://www.ietf.org/rfc/rfc1321.txt

Copyright © 2010-2011, Alexéy Sudachén, alexey@sudachen.name, Chile

In USA, UK, Japan and other countries allowing software patents:

    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

Changes to newdes96.hc.

3
4
5
6
7
8
9
10
11
12
13
14
15
16
17

//
// NEWDES96 
// Released to the public domain by Robert Scott
// -  Originally published in Cryptologia, Jan. 1985
//

Copyright © 2010-2011, Alexéy Sudáchen, alexey@sudachen.name, Chile

In USA, UK, Japan and other countries allowing software patents:

    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.







|







3
4
5
6
7
8
9
10
11
12
13
14
15
16
17

//
// NEWDES96 
// Released to the public domain by Robert Scott
// -  Originally published in Cryptologia, Jan. 1985
//

Copyright © 2010-2011, Alexéy Sudachén, alexey@sudachen.name, Chile

In USA, UK, Japan and other countries allowing software patents:

    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

Changes to pefile.hc.

1
2
3
4
5
6
7
8
9
10
11

/*

Copyright © 2010-2011, Alexéy Sudáchen, alexey@sudachen.name, Chile

In USA, UK, Japan and other countries allowing software patents:

    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.



|







1
2
3
4
5
6
7
8
9
10
11

/*

Copyright © 2010-2011, Alexéy Sudachén, alexey@sudachen.name, Chile

In USA, UK, Japan and other countries allowing software patents:

    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

Changes to pictbmp.hc.

1
2
3
4
5
6
7
8
9
10
11

/*

Copyright © 2010-2011, Alexéy Sudáchen, alexey@sudachen.name, Chile

In USA, UK, Japan and other countries allowing software patents:

    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.



|







1
2
3
4
5
6
7
8
9
10
11

/*

Copyright © 2010-2011, Alexéy Sudachén, alexey@sudachen.name, Chile

In USA, UK, Japan and other countries allowing software patents:

    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

Changes to pictpng.hc.

1
2
3
4
5
6
7
8
9
10
11

/*

Copyright © 2010-2011, Alexéy Sudáchen, alexey@sudachen.name, Chile

In USA, UK, Japan and other countries allowing software patents:

    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.



|







1
2
3
4
5
6
7
8
9
10
11

/*

Copyright © 2010-2011, Alexéy Sudachén, alexey@sudachen.name, Chile

In USA, UK, Japan and other countries allowing software patents:

    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

Changes to picture.hc.

1
2
3
4
5
6
7
8
9
10
11

/*

Copyright © 2010-2011, Alexéy Sudáchen, alexey@sudachen.name, Chile

In USA, UK, Japan and other countries allowing software patents:

    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.



|







1
2
3
4
5
6
7
8
9
10
11

/*

Copyright © 2010-2011, Alexéy Sudachén, alexey@sudachen.name, Chile

In USA, UK, Japan and other countries allowing software patents:

    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

Changes to prog.hc.

1
2
3
4
5
6
7
8
9
10
11

/*

Copyright © 2010-2011, Alexéy Sudáchen, alexey@sudachen.name, Chile

In USA, UK, Japan and other countries allowing software patents:

    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.



|







1
2
3
4
5
6
7
8
9
10
11

/*

Copyright © 2010-2011, Alexéy Sudachén, alexey@sudachen.name, Chile

In USA, UK, Japan and other countries allowing software patents:

    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

Changes to random.hc.

1
2
3
4
5
6
7
8
9
10
11

/*

Copyright © 2010-2011, Alexéy Sudáchen, alexey@sudachen.name, Chile

In USA, UK, Japan and other countries allowing software patents:

    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.



|







1
2
3
4
5
6
7
8
9
10
11

/*

Copyright © 2010-2011, Alexéy Sudachén, alexey@sudachen.name, Chile

In USA, UK, Japan and other countries allowing software patents:

    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

Changes to sha1.hc.

2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
/*

//
// The SHA-1 standard was published by NIST in 1993.
//   http://www.itl.nist.gov/fipspubs/fip180-1.htm
//

Copyright © 2010-2011, Alexéy Sudáchen, alexey@sudachen.name, Chile

In USA, UK, Japan and other countries allowing software patents:

    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.







|







2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
/*

//
// The SHA-1 standard was published by NIST in 1993.
//   http://www.itl.nist.gov/fipspubs/fip180-1.htm
//

Copyright © 2010-2011, Alexéy Sudachén, alexey@sudachen.name, Chile

In USA, UK, Japan and other countries allowing software patents:

    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

Changes to sha2.hc.

2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
/*

//
// The SHA-256 Secure Hash Standard was published by NIST in 2002.
//   http://csrc.nist.gov/publications/fips/fips180-2/fips180-2.pdf
//

Copyright © 2010-2011, Alexéy Sudáchen, alexey@sudachen.name, Chile

In USA, UK, Japan and other countries allowing software patents:

    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.







|







2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
/*

//
// The SHA-256 Secure Hash Standard was published by NIST in 2002.
//   http://csrc.nist.gov/publications/fips/fips180-2/fips180-2.pdf
//

Copyright © 2010-2011, Alexéy Sudachén, alexey@sudachen.name, Chile

In USA, UK, Japan and other countries allowing software patents:

    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

Changes to string.hc.

1
2
3
4
5
6
7
8
9
10
11
..
42
43
44
45
46
47
48



49
50
51
52
53
54
55
56
57
58
59
60
61
62

/*

Copyright © 2010-2011, Alexéy Sudáchen, alexey@sudachen.name, Chile

In USA, UK, Japan and other countries allowing software patents:

    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.
................................................................................
in this Software without prior written authorization of the copyright holder.

*/

#ifndef C_once_0ED387CD_668B_44C3_9D91_A6336A2F5F48
#define C_once_0ED387CD_668B_44C3_9D91_A6336A2F5F48




#ifdef _LIBYOYO
#define _YOYO_STRING_BUILTIN
#endif

#include "yoyo.hc"
#include "array.hc"

#ifdef _YOYO_STRING_BUILTIN
#define _YOYO_STRING_EXTERN
#else
#define _YOYO_STRING_EXTERN extern
#endif

/* caseinsensitive strcmp, returns 0 if equal */



|







 







>
>
>




<
<
<







1
2
3
4
5
6
7
8
9
10
11
..
42
43
44
45
46
47
48
49
50
51
52
53
54
55



56
57
58
59
60
61
62

/*

Copyright © 2010-2011, Alexéy Sudachén, alexey@sudachen.name, Chile

In USA, UK, Japan and other countries allowing software patents:

    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.
................................................................................
in this Software without prior written authorization of the copyright holder.

*/

#ifndef C_once_0ED387CD_668B_44C3_9D91_A6336A2F5F48
#define C_once_0ED387CD_668B_44C3_9D91_A6336A2F5F48

#include "yoyo.hc"
#include "array.hc"

#ifdef _LIBYOYO
#define _YOYO_STRING_BUILTIN
#endif




#ifdef _YOYO_STRING_BUILTIN
#define _YOYO_STRING_EXTERN
#else
#define _YOYO_STRING_EXTERN extern
#endif

/* caseinsensitive strcmp, returns 0 if equal */

Changes to tcpip.hc.

1
2
3
4
5
6
7
8
9
10
11
..
55
56
57
58
59
60
61


62
63
64
65
66
67
68
69
70
71









72
73
74
75
76
77
78
79
80
81

82

83
84

85
86
87

88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
...
132
133
134
135
136
137
138
139
140



141

142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
...
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
...
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
...
246
247
248
249
250
251
252
253












254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274

275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329


















































































































330
331

/*

Copyright © 2010-2011, Alexéy Sudáchen, alexey@sudachen.name, Chile

In USA, UK, Japan and other countries allowing software patents:

    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.
................................................................................
#define _YOYO_TCPIP_BUILTIN
#endif

enum  
  {
    TCPSOK_ASYNC = 1,
  };



typedef struct _YOYO_TCPSOK
  {
    in_addr_t ip;
    int       skt;
    int       port;
    int       async: 1;
  } YOYO_TCPSOK;

#ifdef __windoze









void _WSA_Init()
#ifdef _YOYO_TCPIP_BUILTIN
  {
    static int wsa_status = -1;
    static WSADATA wsa_data = {0};
    if ( wsa_status != 0 )
      {
        if ( 0 != WSAStartup(MAKEWORD(2, 2), &wsa_data) )
          __Raise(YOYO_ERROR_SUBSYSTEM_INIT,"failed to initialize WSA subsystem");
        else

          wsa_status = 0;

      }
    return true;

  }
#endif
  ;

#else
#define _WSA_Init()
#endif


#define Ipv4_Format(Ip) __Pool(Ipv4_Format_Npl(Ip))
char *Ipv4_Format_Npl(in_addr_t ip)
#ifdef _YOYO_TCPIP_BUILTIN
  {
    return __Format_Npl("%d.%d.%d.%d"
              ,(ip&0x0ff)
              ,((ip>>8)&0x0ff)
              ,((ip>>16)&0x0ff)
              ,((ip>>24)&0x0ff));
  }
#endif
  ;
  
in_addr_t Dns_Resolve(char *host)
#ifdef _YOYO_TCPIP_BUILTIN
  {
    in_addr_t ip;
    hostent *hstn = 0;
    
    _WSA_Init();
    if ( !strcmp_I(host,"localhost") ) return 0x0100007f;
    ip = inet_addr(host);
    if ( ip != 0x0ffffffff ) return ip;
  l:
    if ( hstn = gethostbyname(host) )
................................................................................
  }        
#endif
  ;
  
void Tcp_Close(YOYO_TCPSOK *sok)
#ifdef _YOYO_TCPIP_BUILTIN
  {
    if ( sok->skt >= 0 ) 
      {



        close(sok->skt);

        sok->skt = -1;
      }
  }
#endif
  ;
  
void YOYO_TCPSOK_Destruct(YOYO_TCPSOK *sok)
#ifdef _YOYO_TCPIP_BUILTIN
  {
    Tcp_Close(sok);
    free(sok->host);
    __Destruct(sok);
  }
#endif
  ;
  
int Tcp_Read(YOYO_TCPSOK *sok, void *out, int count, int mincount)
#ifdef _YOYO_TCPIP_BUILTIN  
................................................................................
      {
        int q = recv(sok->skt,b,cc,0);
        if ( q < 0 )
          __Raise_Format(YOYO_ERROR_IO,("tcp recv failed with error %s",strerror(errno)));
        STRICT_REQUIRE( q <= cc );
        cc -= q;
        b += q;
        if ( q == 0 && count-cc >= mincount )
          break;
      }
    return count-cc;
  }
#endif
  ;

int Tcp_Asio_Recv(YOYO_TCPSOK *sok,void *out, int count, int mincount, void *obj, asio_send_callback_t callback)
#ifdef _YOYO_TCPIP_BUILTIN
  {
    if ( sok->async )
      return Asio_Recv(sok->skt,out,count,mincount,obj,callback);
    else
      {
        int cc = Tcp_Read(sok,out,count,mincount);
        if ( callback )
          return callback(obj,ASIO_COMPLETED|ASIO_SYNCHRONOUSE,cc);
      }
    return 0;
  }
#endif
  ;

int Tcp_Write(YOYO_TCPSOK *sok, void *out, int count, int mincount)
................................................................................
  {
    if ( sok->async )
      return Asio_Send(sok->skt,out,count,obj,callback);
    else
      {
        int cc = Tcp_Write(sok,out,count,count);
        if ( callback )
          return callback(obj,ASIO_COMPLETED|ASIO_SYNCHRONOUSE,cc);
      }
    return 0;
  }
#endif
  ;
  
YOYO_TCPSOK *Tcp_Socket(int flags)
................................................................................
    YOYO_TCPSOK *sok = __Object(sizeof(YOYO_TCPSOK),funcs);
    sok->skt = INVALID_SOCKET;
    if ( flags & TCPSOK_ASYNC ) sok->async = 1;
    return sok;
  }
#endif
  ;
  












#define Tcp_IPv4_Connect(Sok,Ip,Port) Tcp_Asio_IPv4_Connet(Sok,Ip,Port,0,0)
int Tcp_Asio_IPv4_Connect(YOYO_TCPSOK *sok, in_addr_t ip, int port, void *obj, asio_notify_callback_t *callback)
#ifdef _YOYO_TCPIP_BUILTIN
  {
    sockaddr_in addr = {0}; 
    socket_t skt;
    int conerr;
    
    _WSA_Init();
    
    addr.sin_family = AF_INET;
    addr.sin_port   = htons(port);
    addr.sin_addr.s_addr = ip;
    
    sok->port = port;
    sok->ip = ip;
    
    skt = socket(PF_INET,SOCK_STREAM,IPPROTO_TCP);
    sok->skt = skt;

    if ( skt != INVALID_SOCKET && sok->async )

      {
      #ifdef __windoze
        ulong_t nonblock = 1;
        ioctlsocket(skt, FIONBIO, &nonblock);
      #else       
        int arg = fcntl(skt,F_GETFL,0);
        arg |= O_NONBLOCK; 
        fcntl(skt,F_SETFL,arg); 
      #endif
      }
          
    conerr = (skt != INVALID_SOCKET ) ? connect(skt,(sockaddr*)&addr,sizeof(addr)) : -1;
    
    if ( conerr < 0 )
      {
        if ( skt != INVALID_SOCKET && sok->async && Asio_Status_Repeat_Or_Die() == ASIO_PENGING && callback )
          {
            return Asio_Notify(skt,obj,callback);
          }
        else
          __Raise_Format(YOYO_ERROR_IO,
                          (__yoTa("tcp connection failed: sok %d, point %s:%d, error %d",0),
                          ,skt
                          ,Ipv4_Fomat(ip)
                          ,port
                          ,conerr));
      }
      
    if ( callback ) 
      return callback(obj,ASIO_COMPLETED|(sok->async?ASIO_SYNCHRONOUSE:0));
    return 0;
  }
#endif
  ;

#define Tcp_Connect(Sok,Host,Port) Tcp_Asio_Connect(Sok,Host,Port,0,0)
int Tcp_Asio_Connect(YOYO_TCPSOK *sok,char *host,int port,void *obj,asio_notify_callback_t *callback)
#ifdef _YOYO_TCPIP_BUILTIN
  {
    in_addr_t ip = Dns_Resolve(host);
    return Tcp_Asio_IPv4_Connect(sok,ip,port,obj,callback);    
  }
#endif
  ;

YOYO_TCPSOK *Tcp_Open(char *host, int port)
#ifdef _YOYO_TCPIP_BUILTIN
  {
    YOYO_TCPSOK *sok = Tcp_Socket(0);
    Tcp_Connect(sok,host,port);
    return sok;
  }
#endif
  ;



















































































































#endif /* C_once_F8F16072_F92E_49E7_A983_54F60965F4C9 */




|







 







>
>



|






>
>
>
>
>
>
>
>
>










>
|
>
|
<
>



>






|











|
|
|
|
|







 







|
|
>
>
>
|
>










<







 







|







|








|







 







|







 







|
>
>
>
>
>
>
>
>
>
>
>
>
|
|
|
|
|







|








>
|
<
<
<
<
<
<
<
<
<
<
|









|
|
|





|






|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
|
|
1
2
3
4
5
6
7
8
9
10
11
..
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96

97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
...
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169

170
171
172
173
174
175
176
...
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
...
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
...
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305










306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465

/*

Copyright © 2010-2011, Alexéy Sudachén, alexey@sudachen.name, Chile

In USA, UK, Japan and other countries allowing software patents:

    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.
................................................................................
#define _YOYO_TCPIP_BUILTIN
#endif

enum  
  {
    TCPSOK_ASYNC = 1,
  };

typedef uquad_t ipaddr_t; 

typedef struct _YOYO_TCPSOK
  {
    ipaddr_t  ip;
    int       skt;
    int       port;
    int       async: 1;
  } YOYO_TCPSOK;

#ifdef __windoze

void _WSA_Term(void)
#ifdef _YOYO_TCPIP_BUILTIN
  {
    WSACleanup();
  }
#endif
  ;
  
void _WSA_Init()
#ifdef _YOYO_TCPIP_BUILTIN
  {
    static int wsa_status = -1;
    static WSADATA wsa_data = {0};
    if ( wsa_status != 0 )
      {
        if ( 0 != WSAStartup(MAKEWORD(2, 2), &wsa_data) )
          __Raise(YOYO_ERROR_SUBSYSTEM_INIT,"failed to initialize WSA subsystem");
        else
          {
            wsa_status = 0;
            atexit(_WSA_Term);
          }

      }
  }
#endif
  ;

#else
#define _WSA_Init()
#endif


#define Ipv4_Format(Ip) __Pool(Ipv4_Format_Npl(Ip))
char *Ipv4_Format_Npl(ipaddr_t ip)
#ifdef _YOYO_TCPIP_BUILTIN
  {
    return __Format_Npl("%d.%d.%d.%d"
              ,(ip&0x0ff)
              ,((ip>>8)&0x0ff)
              ,((ip>>16)&0x0ff)
              ,((ip>>24)&0x0ff));
  }
#endif
  ;
  
ipaddr_t Dns_Resolve(char *host)
#ifdef _YOYO_TCPIP_BUILTIN
  {
    ipaddr_t ip = 0;
    struct hostent *hstn = 0;
    
    _WSA_Init();
    if ( !strcmp_I(host,"localhost") ) return 0x0100007f;
    ip = inet_addr(host);
    if ( ip != 0x0ffffffff ) return ip;
  l:
    if ( hstn = gethostbyname(host) )
................................................................................
  }        
#endif
  ;
  
void Tcp_Close(YOYO_TCPSOK *sok)
#ifdef _YOYO_TCPIP_BUILTIN
  {
    if ( sok->skt != INVALID_SOCKET ) 
      {
#ifdef __windoze
        closesocket(sok->skt);
#else      
        close(sok->skt);
#endif
        sok->skt = -1;
      }
  }
#endif
  ;
  
void YOYO_TCPSOK_Destruct(YOYO_TCPSOK *sok)
#ifdef _YOYO_TCPIP_BUILTIN
  {
    Tcp_Close(sok);

    __Destruct(sok);
  }
#endif
  ;
  
int Tcp_Read(YOYO_TCPSOK *sok, void *out, int count, int mincount)
#ifdef _YOYO_TCPIP_BUILTIN  
................................................................................
      {
        int q = recv(sok->skt,b,cc,0);
        if ( q < 0 )
          __Raise_Format(YOYO_ERROR_IO,("tcp recv failed with error %s",strerror(errno)));
        STRICT_REQUIRE( q <= cc );
        cc -= q;
        b += q;
        if ( count-cc >= mincount )
          break;
      }
    return count-cc;
  }
#endif
  ;

int Tcp_Asio_Recv(YOYO_TCPSOK *sok,void *out, int count, int mincount, void *obj, asio_recv_callback_t callback)
#ifdef _YOYO_TCPIP_BUILTIN
  {
    if ( sok->async )
      return Asio_Recv(sok->skt,out,count,mincount,obj,callback);
    else
      {
        int cc = Tcp_Read(sok,out,count,mincount);
        if ( callback )
          return callback(obj,ASIO_COMPLETED|ASIO_SYNCHRONOUS,cc);
      }
    return 0;
  }
#endif
  ;

int Tcp_Write(YOYO_TCPSOK *sok, void *out, int count, int mincount)
................................................................................
  {
    if ( sok->async )
      return Asio_Send(sok->skt,out,count,obj,callback);
    else
      {
        int cc = Tcp_Write(sok,out,count,count);
        if ( callback )
          return callback(obj,ASIO_COMPLETED|ASIO_SYNCHRONOUS);
      }
    return 0;
  }
#endif
  ;
  
YOYO_TCPSOK *Tcp_Socket(int flags)
................................................................................
    YOYO_TCPSOK *sok = __Object(sizeof(YOYO_TCPSOK),funcs);
    sok->skt = INVALID_SOCKET;
    if ( flags & TCPSOK_ASYNC ) sok->async = 1;
    return sok;
  }
#endif
  ;
  
void Tcp_Set_Async(socket_t skt)
  {
  #ifdef __windoze
    ulong_t nonblock = 1;
    ioctlsocket(skt, FIONBIO, &nonblock);
  #else       
    int arg = fcntl(skt,F_GETFL,0);
    arg |= O_NONBLOCK; 
    fcntl(skt,F_SETFL,arg); 
  #endif
  }
  
#define Tcp_IPv4_Connect(Sok,Ip,Port) Tcp_Asio_IPv4_Connet(Sok,Ip,Port,0,0)
int Tcp_Asio_IPv4_Connect(YOYO_TCPSOK *sok, ipaddr_t ip, int port, void *obj, asio_notify_callback_t callback)
#ifdef _YOYO_TCPIP_BUILTIN
  {
    struct sockaddr_in addr = {0}; 
    socket_t skt;
    int conerr;
    
    _WSA_Init();
    
    addr.sin_family = AF_INET;
    addr.sin_port   = htons(port);
    addr.sin_addr.s_addr = (ulong_t)ip;
    
    sok->port = port;
    sok->ip = ip;
    
    skt = socket(PF_INET,SOCK_STREAM,IPPROTO_TCP);
    sok->skt = skt;

    if ( skt != INVALID_SOCKET && sok->async )
      Tcp_Set_Async(skt);
          










    conerr = (skt != INVALID_SOCKET ) ? connect(skt,(struct sockaddr*)&addr,sizeof(addr)) : -1;
    
    if ( conerr < 0 )
      {
        if ( skt != INVALID_SOCKET && sok->async && Asio_Status_Repeat_Or_Die() == ASIO_PENGING && callback )
          {
            return Asio_Notify(skt,obj,callback);
          }
        else
          __Raise_Format(YOYO_ERROR_IO,
                          (__yoTa("tcp connection failed: sok %d, point %s:%d, error %d",0)
                          ,skt
                          ,Ipv4_Format(ip)
                          ,port
                          ,conerr));
      }
      
    if ( callback ) 
      return callback(obj,ASIO_COMPLETED|(sok->async?ASIO_SYNCHRONOUS:0));
    return 0;
  }
#endif
  ;

#define Tcp_Connect(Sok,Host,Port) Tcp_Asio_Connect(Sok,Host,Port,0,0)
int Tcp_Asio_Connect(YOYO_TCPSOK *sok,char *host,int port,void *obj,asio_notify_callback_t callback)
#ifdef _YOYO_TCPIP_BUILTIN
  {
    ipaddr_t ip = Dns_Resolve(host);
    return Tcp_Asio_IPv4_Connect(sok,ip,port,obj,callback);    
  }
#endif
  ;

YOYO_TCPSOK *Tcp_Open(char *host, int port)
#ifdef _YOYO_TCPIP_BUILTIN
  {
    YOYO_TCPSOK *sok = Tcp_Socket(0);
    Tcp_Connect(sok,host,port);
    return sok;
  }
#endif
  ;

void Tcp_Asio_Interrupt(YOYO_TCPSOK *sok)
#ifdef _YOYO_TCPIP_BUILTIN
  {
    Asio_Interrupt(sok->skt);
  }
#endif
  ;

YOYO_TCPSOK *Tcp_Listen(int flags, char *host,int port,int listlen)
#ifdef _YOYO_TCPIP_BUILTIN
  {
    YOYO_TCPSOK *sok;
    struct sockaddr_in addr = {0}; 
    ipaddr_t ip;
    
    _WSA_Init();
    
    ip = host?Dns_Resolve(host):0;

    addr.sin_family = AF_INET;
    addr.sin_port   = htons(port);
    addr.sin_addr.s_addr = (ulong_t)ip;
    
    sok = __Refe(Tcp_Socket(0));
    sok->port = port;
    sok->ip = ip;
    
    if ( INVALID_SOCKET == (sok->skt = socket(PF_INET,SOCK_STREAM,IPPROTO_TCP)) )  goto sok_error;
    if ( -1 == bind(sok->skt,(struct sockaddr*)&addr,sizeof(addr)) ) goto sok_error;
    if ( -1 == listen(sok->skt,(listlen?listlen:SOMAXCONN)) ) goto sok_error;

    if ( sok->skt == INVALID_SOCKET )
      {       
    sok_error: 
        __Raise_Format(YOYO_ERROR_IO,
                        (__yoTa("tcp bind/listen failed: sok %d, point %s:%d, error %s",0)
                        ,sok->skt
                        ,Ipv4_Format(ip)
                        ,port
                        ,Format_Network_Error()));
      }
    else if ( flags & TCPSOK_ASYNC )
      Tcp_Set_Async(sok->skt);
      
    return sok;
  }
#endif
  ;

typedef int (*tcp_accept_callback_t)(void *obj,int status,YOYO_TCPSOK *sok);

#ifdef _YOYO_TCPIP_BUILTIN
typedef struct _YOYO_TCPSOK_ACCEPT
  {
    void *obj;
    tcp_accept_callback_t callback;
  } YOYO_TCPSOK_ACCEPT;

void YOYO_TCPSOK_ACCEPT_Destruct(YOYO_TCPSOK_ACCEPT *acbk)
  {
    __Unrefe(acbk->obj);
    __Destruct(acbk);
  }

YOYO_TCPSOK_ACCEPT *Tcp_Accept_Callback(void *obj,tcp_accept_callback_t callback)
  {
    YOYO_TCPSOK_ACCEPT *acbk = __Object_Dtor(sizeof(YOYO_TCPSOK_ACCEPT),YOYO_TCPSOK_ACCEPT_Destruct);
    acbk->callback = callback;
    acbk->obj = __Refe(obj);
    return acbk;
  }

int Cbk_Tcp_Asio_Accept(void *obj, int status, socket_t skt, struct sockaddr *_addr)
  {
    struct sockaddr_in *addr = ((struct sockaddr_in*)_addr);
    YOYO_TCPSOK *sok = Tcp_Socket( (status&ASIO_SYNCHRONOUS)?0:TCPSOK_ASYNC );
    sok->ip = addr->sin_addr.s_addr;
    sok->port = addr->sin_port;
    sok->skt = skt;
    if ( sok->async )
      Tcp_Set_Async(sok->skt);
    return ((YOYO_TCPSOK_ACCEPT*)obj)->callback(((YOYO_TCPSOK_ACCEPT*)obj)->obj,status,sok);
  }
#endif

int Tcp_Asio_Accept(YOYO_TCPSOK *sok, void *obj, tcp_accept_callback_t callback)
#ifdef _YOYO_TCPIP_BUILTIN
  {
    return Asio_Accept(sok->skt,Tcp_Accept_Callback(obj,callback),Cbk_Tcp_Asio_Accept);
  }
#endif
  ;

void Tcp_Asio_Shutdown(YOYO_TCPSOK *sok)
#ifdef _YOYO_TCPIP_BUILTIN
  {
    if ( sok->async )
      {
        socket_t skt = sok->skt;
        if ( skt != INVALID_SOCKET )
          {
            sok->skt = -1;
            /*fixme*/ //Asio_Shutdown(skt);
          }
      }
    else
      {
        shutdown(sok->skt,SB_BOTH);
        Tcp_Close(sok);
      }
  }
#endif
  ;

#endif /* C_once_F8F16072_F92E_49E7_A983_54F60965F4C9 */

Changes to uname.hc.

1
2
3
4
5
6
7
8
9
10
11

/*

Copyright © 2005-2011, Alexéy Sudáchen, alexey@sudachen.name, Chile

In USA, UK, Japan and other countries allowing software patents:

    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.



|







1
2
3
4
5
6
7
8
9
10
11

/*

Copyright © 2005-2011, Alexéy Sudachén, alexey@sudachen.name, Chile

In USA, UK, Japan and other countries allowing software patents:

    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

Changes to url.hc.

1


















































2
3
4
5
6
7
8
.
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29

30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45


46
47
48
49
50
51
52
53
..
58
59
60
61
62
63
64

65
66




67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114

115
116
117
118
119
120
121
...
138
139
140
141
142
143
144
145

146


















147
148
149
150





































































































































#ifdef _LIBYOYO
#define _YOYO_URL_BUILTIN
#endif

enum 
  {
    URLX_UNKNOWN  = 0,
................................................................................
    URLX_HTTP     = 80,
    URLX_HTTPS    = 443,
    URLX_FILE     = -1,
  };

typedef struct _YOYO_URL 
  {
    char *host, *user, *passw, *query, *args, *anchor, *uri;
    int   port, proto;
  } YOYO_URL;

void YOYO_URL_Destruct(YOYO_URL *url)
#ifdef _YOYO_URL_BUILTIN
  {
    free(url->host);
    free(url->user);
    free(url->passw);
    free(url->query);
    free(url->args);
    free(url->anchor);
    free(url->uri);

    __Destruct(url);
  }
#endif
  ;
  
int Url_Proto(char *S)
#ifdef _YOYO_URL_BUILTIN
  {
    if ( !strcmp_I(S,"http")  )  return URLX_HTTP;
    if ( !strcmp_I(S,"https") )  return URLX_HTTPS;
    if ( !strcmp_I(S,"file")  )  return URLX_FILE;
    return YOYO_URL_UNKNOWN;
  }
#endif
  ;
  


YOYO_URL *Url_Parse(char *url)
#ifdef _YOYO_URL_BUILTIN
  {
    YOYO_URL *urlout = 0;
    __Auto_Ptr(urlout)
      {
        /* proto://user:passwd@host:port/query#anchor?args */
      
................................................................................
        char *host  = 0;
        char *user  = 0;
        char *passw = 0;
        char *uri   = 0;
        char *args  = 0;
        char *query = 0;
        char *anchor= 0;

        int   port  = 0;
        




        p = pS;
        while ( *p && Isalpha(*p) ) ++p;
        
        if ( *p && *p == ':' && p[1] && p[1] == '/' && p[2] && p[2] == '/' )
          {
            proto = Str_Range(pS,p);
            pS = p+3;
          }
          
        p = pS;
        while ( *p && (Isalnum(*p) || *p == '.' || *p == '-' || *p == ':' ) ) ++p;
        
        if ( *p == '@' ) // user/password
          {
            char *q = pS;
            while ( *q != '@' && *q != ':' ) ++q;
            if ( *q == ':' ) 
              {
                user = Str_Range(pS,q);
                passw = Str_Range(q+1,p);
              }
            else
              user = Str_Range(pS,p);
            pS = p+1;
          }
          
        p = pS;
        while ( *p && (Isalnum(*p) || *p == '.' || *p == '-') ) ++p;
        
        if ( *p == ':' )
          {
            host = Str_Range(pS,p);
            pS = p+1; ++p;
            while ( *p && Isdigit(*p) ) ++p;
            if ( *p == '/' || !*p )
              { 
                port = strtol(pS,0,10); 
              }
            else
              __Raise(YOYO_ERROR_ILLFORMED,__yoTa("invalid port value",0));
            pS = p;
          }
        else if ( !*p || *p == '/' )
          {
            host = Str_Range(pS,p);
            pS = p;
          }
        

        uri = Str_Copy(pS,-1);  
        
        p = pS;
        while ( *p && *p != '?' && *p != '#' ) ++p;
        query = Str_Range(pS,p);

        if ( *p == '#' )
................................................................................
        urlout->query = __Retain(query);
        urlout->uri   = __Retain(uri);
        urlout->host  = __Retain(host);
        urlout->passw = __Retain(passw);
        urlout->user  = __Retain(user);
        urlout->port  = port;
        urlout->proto = Url_Proto(proto);
      }

      


















    return urlout;
  }
#endif
  ;



















































































>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>







 







|













>











|




>
>
|







 







>


>
>
>
>
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
>







 







|
>
|
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>




>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
..
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
...
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
...
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310

/*

Copyright © 2010-2012, Alexéy Sudachén, alexey@sudachen.name, Chile

In USA, UK, Japan and other countries allowing software patents:

    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    http://www.gnu.org/licenses/

Otherwise:

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.

Except as contained in this notice, the name of a copyright holder shall not
be used in advertising or otherwise to promote the sale, use or other dealings
in this Software without prior written authorization of the copyright holder.

*/

#ifndef C_once_36F09FA7_8AEC_4584_91B3_D25C37490B80
#define C_once_36F09FA7_8AEC_4584_91B3_D25C37490B80

#include "yoyo.hc"
#include "string.hc"

#ifdef _LIBYOYO
#define _YOYO_URL_BUILTIN
#endif

enum 
  {
    URLX_UNKNOWN  = 0,
................................................................................
    URLX_HTTP     = 80,
    URLX_HTTPS    = 443,
    URLX_FILE     = -1,
  };

typedef struct _YOYO_URL 
  {
    char *host, *user, *passw, *query, *args, *anchor, *uri, *endpoint;
    int   port, proto;
  } YOYO_URL;

void YOYO_URL_Destruct(YOYO_URL *url)
#ifdef _YOYO_URL_BUILTIN
  {
    free(url->host);
    free(url->user);
    free(url->passw);
    free(url->query);
    free(url->args);
    free(url->anchor);
    free(url->uri);
    free(url->endpoint);
    __Destruct(url);
  }
#endif
  ;
  
int Url_Proto(char *S)
#ifdef _YOYO_URL_BUILTIN
  {
    if ( !strcmp_I(S,"http")  )  return URLX_HTTP;
    if ( !strcmp_I(S,"https") )  return URLX_HTTPS;
    if ( !strcmp_I(S,"file")  )  return URLX_FILE;
    return URLX_UNKNOWN;
  }
#endif
  ;
  
#define Url_Parse(Url) Url_Parse_(Url,0)
#define Url_Parse_Uri(Url) Url_Parse_(Url,1)
YOYO_URL *Url_Parse_(char *url,int uri_only)
#ifdef _YOYO_URL_BUILTIN
  {
    YOYO_URL *urlout = 0;
    __Auto_Ptr(urlout)
      {
        /* proto://user:passwd@host:port/query#anchor?args */
      
................................................................................
        char *host  = 0;
        char *user  = 0;
        char *passw = 0;
        char *uri   = 0;
        char *args  = 0;
        char *query = 0;
        char *anchor= 0;
        char *endpoint = 0;
        int   port  = 0;
        
        while ( *pS && Isspace(*pS) ) ++pS;
        
        if ( !uri_only )
          {
            p = pS;
            while ( *p && Isalpha(*p) ) ++p;
            
            if ( *p && *p == ':' && p[1] && p[1] == '/' && p[2] && p[2] == '/' )
              {
                proto = Str_Range(pS,p);
                pS = p+3;
              }
              
            p = pS;
            while ( *p && (Isalnum(*p) || *p == '.' || *p == '-' || *p == ':' ) ) ++p;
            
            if ( *p == '@' ) // user/password
              {
                char *q = pS;
                while ( *q != '@' && *q != ':' ) ++q;
                if ( *q == ':' ) 
                  {
                    user = Str_Range(pS,q);
                    passw = Str_Range(q+1,p);
                  }
                else
                  user = Str_Range(pS,p);
                pS = p+1;
              }
              
            p = pS;
            while ( *p && (Isalnum(*p) || *p == '.' || *p == '-') ) ++p;
            
            if ( *p == ':' )
              {
                host = Str_Range(pS,p);
                pS = p+1; ++p;
                while ( *p && Isdigit(*p) ) ++p;
                if ( *p == '/' || !*p )
                  { 
                    port = strtol(pS,0,10); 
                  }
                else
                  __Raise(YOYO_ERROR_ILLFORMED,__yoTa("invalid port value",0));
                pS = p;
              }
            else if ( !*p || *p == '/' )
              {
                host = Str_Range(pS,p);
                pS = p;
              }
          }
              
        uri = Str_Copy(pS,-1);  
        
        p = pS;
        while ( *p && *p != '?' && *p != '#' ) ++p;
        query = Str_Range(pS,p);

        if ( *p == '#' )
................................................................................
        urlout->query = __Retain(query);
        urlout->uri   = __Retain(uri);
        urlout->host  = __Retain(host);
        urlout->passw = __Retain(passw);
        urlout->user  = __Retain(user);
        urlout->port  = port;
        urlout->proto = Url_Proto(proto);

        if ( proto )        
          {
            Str_Cat(&endpoint,proto,-1);
            Str_Cat(&endpoint,"://",3);
          }
        
        if ( host )
          {
            Str_Cat(&endpoint,host,-1);
            if ( port )
              {
                char S[13];
                sprintf(S,":%u",port);
                Str_Cat(&endpoint,S,-1);
              }
          }
          
        urlout->endpoint = __Retain(endpoint);
      }
      
    return urlout;
  }
#endif
  ;

typedef struct _YOYO_URL_COMPOSER
  {
    char *S;
    int  capacity;
  } YOYO_URL_COMPOSER;

void Url_Compose_Dicto_Fitler(char *name, void *val, void *o)
  {
    char **inout  = &((YOYO_URL_COMPOSER*)o)->S;
    int *capacity = &((YOYO_URL_COMPOSER*)o)->capacity;
    int count     = *inout?strlen(*inout):0;
    count += __Elm_Append(inout,count,"&",1,1,capacity);
    count += __Elm_Append(inout,count,name,strlen(name),1,capacity);
    if ( val )
      {
        int i, iE = strlen(val);
        char C[4];
        count += __Elm_Append(inout,count,"=",1,1,capacity);
        for ( i = 0; i < iE; ++i )
          {
            byte_t b = ((char*)val)[i];
            if ( Isalnum(b) || b == '-' || b == '.' || b == '_' || b == '%' )
              count += __Elm_Append(inout,count,&b,1,1,capacity);
            else
              {
                Str_Hex_Byte(b,'%',C);
                count += __Elm_Append(inout,count,C,3,1,capacity);
              }
          }
      }
  }

char *Url_Compose(char *url, YOYO_DICTO *params)
#ifdef _YOYO_URL_BUILTIN
  {
    YOYO_URL_COMPOSER cmps = { 0, 0 };
    int i, q = __Elm_Append(&cmps.S,0,url,strlen(url),1,&cmps.capacity);
    
    for ( i = 0; i < q ; ++i )
      if ( cmps.S[i] == '?' ) break;
    if ( i == q ) i = -1;
    
    if ( params )
      {
        Dicto_Apply(params,Url_Compose_Dicto_Fitler,&cmps);
        
        if ( !i && cmps.S[q] ) 
          {
            STRICT_REQUIRE(cmps.S[q] == '&');
            cmps.S[q] = '?';
          }
      }
        
    return cmps.S;
  }
#endif
  ;
  
char *Url_Xform_Encode(YOYO_DICTO *params)
#ifdef _YOYO_URL_BUILTIN
  {
    if ( params )
      {
        YOYO_URL_COMPOSER cmps = { 0, 0 };
        Dicto_Apply(params,Url_Compose_Dicto_Fitler,&cmps);
        
        if ( cmps.S && cmps.S[0] ) 
          {
            STRICT_REQUIRE(cmps.S[0] == '&');
            cmps.S[0] = '?';
          }
          
        return cmps.S;
      }
    return 0;
  }
#endif
  ;

#endif /*C_once_36F09FA7_8AEC_4584_91B3_D25C37490B80*/

Changes to winlpc.hc.

1
2
3
4
5
6
7
8
9
10
11
..
48
49
50
51
52
53
54




55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
..
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100

/*

Copyright © 2010-2011, Alexéy Sudáchen, alexey@sudachen.name, Chile

In USA, UK, Japan and other countries allowing software patents:

    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.
................................................................................

#ifndef C_once_37ADA665_BE0A_456F_9491_3A81B3EB8980
#define C_once_37ADA665_BE0A_456F_9491_3A81B3EB8980

#include "yoyo.hc"
#ifdef __windoze





#ifdef _LIBYOYO
#define _YOYO_WINLPC_BUILTIN
#endif

#include "string.hc"
#include "xntdef.hc"
#include "winlpc.inc"

enum { WINLPC_MAX_DATA_LENGTH = 256 };

typedef struct _YOYO_LPCPORT
  {
    HANDLE handle;
    void  *tag;
    int    waitable;
  } YOYO_LPCPORT;
................................................................................
    Str_Utf8_To_Unicode_Convert(name,wname,256);
    RtlInitUnicodeString(&us_name, wname);
    InitializeObjectAttributes(&oa, &us_name, 0, 0, 0);
    
    port->waitable = waitable;
    
    if ( waitable )
      ntst = NtCreateWaitablePort(&port->handle, &oa, WINLPC_MAX_DATA_LENGTH, WINLPC_MAX_DATA_LENGTH, 0);
    else
      ntst = NtCreatePort(&port->handle, &oa, WINLPC_MAX_DATA_LENGTH, WINLPC_MAX_DATA_LENGTH, 0);

    return ntst;
  }
#endif
  ;

long Lpc_Connect_Port(



|







 







>
>
>
>




|
|
|
|
|







 







|
|
|







1
2
3
4
5
6
7
8
9
10
11
..
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
..
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104

/*

Copyright © 2010-2011, Alexéy Sudachén, alexey@sudachen.name, Chile

In USA, UK, Japan and other countries allowing software patents:

    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.
................................................................................

#ifndef C_once_37ADA665_BE0A_456F_9491_3A81B3EB8980
#define C_once_37ADA665_BE0A_456F_9491_3A81B3EB8980

#include "yoyo.hc"
#ifdef __windoze

#include "string.hc"
#include "xntdef.hc"
#include "winlpc.inc"

#ifdef _LIBYOYO
#define _YOYO_WINLPC_BUILTIN
#endif

enum 
  { 
    WINLPC_MAX_CONINFO_LENGTH = 260, 
    WINLPC_MAX_DATA_LENGTH = 328, 
  };

typedef struct _YOYO_LPCPORT
  {
    HANDLE handle;
    void  *tag;
    int    waitable;
  } YOYO_LPCPORT;
................................................................................
    Str_Utf8_To_Unicode_Convert(name,wname,256);
    RtlInitUnicodeString(&us_name, wname);
    InitializeObjectAttributes(&oa, &us_name, 0, 0, 0);
    
    port->waitable = waitable;
    
    if ( waitable )
      ntst = NtCreateWaitablePort(&port->handle, &oa, WINLPC_MAX_CONINFO_LENGTH, WINLPC_MAX_DATA_LENGTH, 0);
    else
      ntst = NtCreatePort(&port->handle, &oa, WINLPC_MAX_CONINFO_LENGTH, WINLPC_MAX_DATA_LENGTH, 0);

    return ntst;
  }
#endif
  ;

long Lpc_Connect_Port(

Changes to winlpc.inc.

1
2
3
4
5
6
7
8
9
10
11

/*

Copyright © 2010-2011, Alexéy Sudáchen, alexey@sudachen.name, Chile

In USA, UK, Japan and other countries allowing software patents:

    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.



|







1
2
3
4
5
6
7
8
9
10
11

/*

Copyright © 2010-2011, Alexéy Sudachén, alexey@sudachen.name, Chile

In USA, UK, Japan and other countries allowing software patents:

    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

Changes to winreg.hc.

1
2
3
4
5
6
7
8
9
10
11

/*

Copyright © 2010-2011, Alexéy Sudáchen, alexey@sudachen.name, Chile

In USA, UK, Japan and other countries allowing software patents:

    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.



|







1
2
3
4
5
6
7
8
9
10
11

/*

Copyright © 2010-2011, Alexéy Sudachén, alexey@sudachen.name, Chile

In USA, UK, Japan and other countries allowing software patents:

    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

Changes to x86dis.hc.

1
2
3
4
5
6
7
8
9
10
11

/*

Copyright © 2005-2011, Alexéy Sudáchen, alexey@sudachen.name, Chile

In USA, UK, Japan and other countries allowing software patents:

    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.



|







1
2
3
4
5
6
7
8
9
10
11

/*

Copyright © 2005-2011, Alexéy Sudachén, alexey@sudachen.name, Chile

In USA, UK, Japan and other countries allowing software patents:

    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

Changes to xdata.hc.

1
2
3
4
5
6
7
8
9
10
11

/*

Copyright © 2010-2011, Alexéy Sudáchen, alexey@sudachen.name, Chile

In USA, UK, Japan and other countries allowing software patents:

    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.



|







1
2
3
4
5
6
7
8
9
10
11

/*

Copyright © 2010-2011, Alexéy Sudachén, alexey@sudachen.name, Chile

In USA, UK, Japan and other countries allowing software patents:

    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

Changes to xntdef.hc.

1
2
3
4
5
6
7
8
9
10
11

/*

Copyright © 2010-2011, Alexéy Sudáchen, alexey@sudachen.name, Chile

In USA, UK, Japan and other countries allowing software patents:

    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.



|







1
2
3
4
5
6
7
8
9
10
11

/*

Copyright © 2010-2011, Alexéy Sudachén, alexey@sudachen.name, Chile

In USA, UK, Japan and other countries allowing software patents:

    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

Changes to yoyo.hc.

1
2
3
4
5
6
7
8
9
10
11
...
128
129
130
131
132
133
134

135
136
137
138
139
140
141
...
241
242
243
244
245
246
247






248
249
250
251
252
253
254
...
832
833
834
835
836
837
838

839
840
841
842
843
844
845
846


847
848
849
850
851
852
853

/*

Copyright © 2010-2011, Alexéy Sudáchen, alexey@sudachen.name, Chile

In USA, UK, Japan and other countries allowing software patents:

    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.
................................................................................
#   define _X86_ 1
#  endif
# endif
# if !defined WINVER
#  define WINVER 0x500
# endif
# include <windef.h>

# include <winbase.h>
# include <excpt.h>
# include <objbase.h>
# include <io.h>
# include <process.h>
# include <malloc.h> /* alloca */
#else
................................................................................
/* compatibility with legacy TeggoSoft/MoleStudio code */
typedef uhalf_t  u16_t;
typedef udword_t u32_t;
typedef uquad_t  u64_t;
typedef half_t   i16_t;
typedef dword_t  i32_t;
typedef quad_t   i64_t;







#ifndef _NO__FILE__
# define __Yo_FILE__ __FILE__
# define __Yo_Expr__(Expr) #Expr
# define Yo_Raise(Error,Msg,File,Line) _Yo_Raise(Error,Msg,File,Line)
# define Yo_Fatal(Error,Msg,File,Line) _Yo_Fatal(Error,Msg,File,Line)
#else
................................................................................
  }
#endif
  ;
  
#define Yo_Push_Scope() Yo_Pool_Ptr(0,Yo_Pool_Marker_Tag)
#define Yo_Pool(Ptr) Yo_Pool_Ptr(Ptr,0)


void *Yo_Pool_Ptr(void *ptr,void *cleanup)
#ifdef _YOYO_CORE_BUILTIN
  {
    if ( ptr || cleanup == Yo_Pool_Marker_Tag )
      {
        YOYO_C_SUPPORT_INFO *nfo = Yo_Tls_Get(Yo_Csup_Nfo_Tls);
        if ( !nfo ) nfo = Yo_Acquire_Csup_Nfo();
        STRICT_REQUIRE( (cleanup == Yo_Pool_Marker_Tag) || !Yo_Find_Ptr_In_Pool(nfo,ptr) );



        ++nfo->auto_top;
        STRICT_REQUIRE(nfo->auto_top <= nfo->auto_count);
        
        if ( nfo->auto_top == nfo->auto_count )
          Yo_Extend_Csup_Autopool();
        nfo->auto_pool[nfo->auto_top].ptr = ptr;



|







 







>







 







>
>
>
>
>
>







 







>







|
>
>







1
2
3
4
5
6
7
8
9
10
11
...
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
...
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
...
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863

/*

Copyright © 2010-2011, Alexéy Sudachén, alexey@sudachen.name, Chile

In USA, UK, Japan and other countries allowing software patents:

    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.
................................................................................
#   define _X86_ 1
#  endif
# endif
# if !defined WINVER
#  define WINVER 0x500
# endif
# include <windef.h>
# include <winsock2.h>
# include <winbase.h>
# include <excpt.h>
# include <objbase.h>
# include <io.h>
# include <process.h>
# include <malloc.h> /* alloca */
#else
................................................................................
/* compatibility with legacy TeggoSoft/MoleStudio code */
typedef uhalf_t  u16_t;
typedef udword_t u32_t;
typedef uquad_t  u64_t;
typedef half_t   i16_t;
typedef dword_t  i32_t;
typedef quad_t   i64_t;

#ifdef __windoze
  #define _WINPOSIX(W,F) W
#else
  #define _WINPOSIX(W,F) F
#endif

#ifndef _NO__FILE__
# define __Yo_FILE__ __FILE__
# define __Yo_Expr__(Expr) #Expr
# define Yo_Raise(Error,Msg,File,Line) _Yo_Raise(Error,Msg,File,Line)
# define Yo_Fatal(Error,Msg,File,Line) _Yo_Fatal(Error,Msg,File,Line)
#else
................................................................................
  }
#endif
  ;
  
#define Yo_Push_Scope() Yo_Pool_Ptr(0,Yo_Pool_Marker_Tag)
#define Yo_Pool(Ptr) Yo_Pool_Ptr(Ptr,0)

void *Yo_Unrefe(void *p);
void *Yo_Pool_Ptr(void *ptr,void *cleanup)
#ifdef _YOYO_CORE_BUILTIN
  {
    if ( ptr || cleanup == Yo_Pool_Marker_Tag )
      {
        YOYO_C_SUPPORT_INFO *nfo = Yo_Tls_Get(Yo_Csup_Nfo_Tls);
        if ( !nfo ) nfo = Yo_Acquire_Csup_Nfo();
        STRICT_REQUIRE( (cleanup == Yo_Pool_Marker_Tag)
                      ||(cleanup == Yo_Unrefe) 
                      ||!Yo_Find_Ptr_In_Pool(nfo,ptr) );

        ++nfo->auto_top;
        STRICT_REQUIRE(nfo->auto_top <= nfo->auto_count);
        
        if ( nfo->auto_top == nfo->auto_count )
          Yo_Extend_Csup_Autopool();
        nfo->auto_pool[nfo->auto_top].ptr = ptr;