0000: 2f 2a 0a 20 2a 20 43 6f 70 79 72 69 67 68 74 20  /*. * Copyright 
0010: 28 43 29 20 31 39 39 37 2d 32 30 30 30 20 4d 61  (C) 1997-2000 Ma
0020: 74 74 20 4e 65 77 6d 61 6e 20 3c 6d 61 74 74 40  tt Newman <matt@
0030: 6e 6f 76 61 64 69 67 6d 2e 63 6f 6d 3e 0a 20 2a  novadigm.com>. *
0040: 0a 20 2a 20 24 48 65 61 64 65 72 3a 20 2f 68 6f  . * $Header: /ho
0050: 6d 65 2f 72 6b 65 65 6e 65 2f 74 6d 70 2f 63 76  me/rkeene/tmp/cv
0060: 73 32 66 6f 73 73 69 6c 2f 2e 2e 2f 74 63 6c 74  s2fossil/../tclt
0070: 6c 73 2f 74 6c 73 2f 74 6c 73 2f 74 6c 73 42 49  ls/tls/tls/tlsBI
0080: 4f 2e 63 2c 76 20 31 2e 32 2e 32 2e 33 20 32 30  O.c,v 1.2.2.3 20
0090: 30 30 2f 30 37 2f 32 31 20 30 35 3a 33 32 3a 35  00/07/21 05:32:5
00a0: 37 20 68 6f 62 62 73 20 45 78 70 20 24 0a 20 2a  7 hobbs Exp $. *
00b0: 0a 20 2a 20 50 72 6f 76 69 64 65 73 20 42 49 4f  . * Provides BIO
00c0: 20 6c 61 79 65 72 20 74 6f 20 69 6e 74 65 72 66   layer to interf
00d0: 61 63 65 20 6f 70 65 6e 73 73 6c 20 74 6f 20 54  ace openssl to T
00e0: 63 6c 2e 0a 20 2a 2f 0a 0a 23 69 6e 63 6c 75 64  cl.. */..#includ
00f0: 65 20 22 74 6c 73 49 6e 74 2e 68 22 0a 0a 2f 2a  e "tlsInt.h"../*
0100: 0a 20 2a 20 46 6f 72 77 61 72 64 20 64 65 63 6c  . * Forward decl
0110: 61 72 61 74 69 6f 6e 73 0a 20 2a 2f 0a 0a 73 74  arations. */..st
0120: 61 74 69 63 20 69 6e 74 20 42 69 6f 57 72 69 74  atic int BioWrit
0130: 65 09 5f 41 4e 53 49 5f 41 52 47 53 5f 20 28 28  e._ANSI_ARGS_ ((
0140: 42 49 4f 20 2a 68 2c 20 63 68 61 72 20 2a 62 75  BIO *h, char *bu
0150: 66 2c 20 69 6e 74 20 6e 75 6d 29 29 3b 0a 73 74  f, int num));.st
0160: 61 74 69 63 20 69 6e 74 20 42 69 6f 52 65 61 64  atic int BioRead
0170: 09 5f 41 4e 53 49 5f 41 52 47 53 5f 20 28 28 42  ._ANSI_ARGS_ ((B
0180: 49 4f 20 2a 68 2c 20 63 68 61 72 20 2a 62 75 66  IO *h, char *buf
0190: 2c 20 69 6e 74 20 6e 75 6d 29 29 3b 0a 73 74 61  , int num));.sta
01a0: 74 69 63 20 69 6e 74 20 42 69 6f 50 75 74 73 09  tic int BioPuts.
01b0: 5f 41 4e 53 49 5f 41 52 47 53 5f 20 28 28 42 49  _ANSI_ARGS_ ((BI
01c0: 4f 20 2a 68 2c 20 63 68 61 72 20 2a 73 74 72 29  O *h, char *str)
01d0: 29 3b 0a 73 74 61 74 69 63 20 6c 6f 6e 67 20 42  );.static long B
01e0: 69 6f 43 74 72 6c 09 5f 41 4e 53 49 5f 41 52 47  ioCtrl._ANSI_ARG
01f0: 53 5f 20 28 28 42 49 4f 20 2a 68 2c 20 69 6e 74  S_ ((BIO *h, int
0200: 20 63 6d 64 2c 20 6c 6f 6e 67 20 61 72 67 31 2c   cmd, long arg1,
0210: 20 63 68 61 72 20 2a 70 74 72 29 29 3b 0a 73 74   char *ptr));.st
0220: 61 74 69 63 20 69 6e 74 20 42 69 6f 4e 65 77 09  atic int BioNew.
0230: 5f 41 4e 53 49 5f 41 52 47 53 5f 20 28 28 42 49  _ANSI_ARGS_ ((BI
0240: 4f 20 2a 68 29 29 3b 0a 73 74 61 74 69 63 20 69  O *h));.static i
0250: 6e 74 20 42 69 6f 46 72 65 65 09 5f 41 4e 53 49  nt BioFree._ANSI
0260: 5f 41 52 47 53 5f 20 28 28 42 49 4f 20 2a 68 29  _ARGS_ ((BIO *h)
0270: 29 3b 0a 0a 0a 73 74 61 74 69 63 20 42 49 4f 5f  );...static BIO_
0280: 4d 45 54 48 4f 44 20 42 69 6f 4d 65 74 68 6f 64  METHOD BioMethod
0290: 73 20 3d 20 7b 0a 20 20 20 20 42 49 4f 5f 54 59  s = {.    BIO_TY
02a0: 50 45 5f 54 43 4c 2c 20 22 74 63 6c 22 2c 0a 20  PE_TCL, "tcl",. 
02b0: 20 20 20 42 69 6f 57 72 69 74 65 2c 0a 20 20 20     BioWrite,.   
02c0: 20 42 69 6f 52 65 61 64 2c 0a 20 20 20 20 42 69   BioRead,.    Bi
02d0: 6f 50 75 74 73 2c 0a 20 20 20 20 4e 55 4c 4c 2c  oPuts,.    NULL,
02e0: 09 2f 2a 20 42 69 6f 47 65 74 73 20 2a 2f 0a 20  ./* BioGets */. 
02f0: 20 20 20 42 69 6f 43 74 72 6c 2c 0a 20 20 20 20     BioCtrl,.    
0300: 42 69 6f 4e 65 77 2c 0a 20 20 20 20 42 69 6f 46  BioNew,.    BioF
0310: 72 65 65 2c 0a 7d 3b 0a 0a 42 49 4f 20 2a 0a 42  ree,.};..BIO *.B
0320: 49 4f 5f 6e 65 77 5f 74 63 6c 28 73 74 61 74 65  IO_new_tcl(state
0330: 50 74 72 2c 20 66 6c 61 67 73 29 0a 20 20 20 20  Ptr, flags).    
0340: 53 74 61 74 65 20 2a 73 74 61 74 65 50 74 72 3b  State *statePtr;
0350: 0a 20 20 20 20 69 6e 74 20 66 6c 61 67 73 3b 0a  .    int flags;.
0360: 7b 0a 20 20 20 20 42 49 4f 20 2a 62 69 6f 3b 0a  {.    BIO *bio;.
0370: 0a 20 20 20 20 62 69 6f 20 3d 20 42 49 4f 5f 6e  .    bio = BIO_n
0380: 65 77 28 26 42 69 6f 4d 65 74 68 6f 64 73 29 3b  ew(&BioMethods);
0390: 0a 20 20 20 20 62 69 6f 2d 3e 70 74 72 20 3d 20  .    bio->ptr = 
03a0: 28 63 68 61 72 2a 29 73 74 61 74 65 50 74 72 3b  (char*)statePtr;
03b0: 0a 20 20 20 20 62 69 6f 2d 3e 69 6e 69 74 20 3d  .    bio->init =
03c0: 20 31 3b 0a 20 20 20 20 62 69 6f 2d 3e 73 68 75   1;.    bio->shu
03d0: 74 64 6f 77 6e 20 3d 20 66 6c 61 67 73 3b 0a 0a  tdown = flags;..
03e0: 20 20 20 20 72 65 74 75 72 6e 20 62 69 6f 3b 0a      return bio;.
03f0: 7d 0a 0a 42 49 4f 5f 4d 45 54 48 4f 44 20 2a 0a  }..BIO_METHOD *.
0400: 42 49 4f 5f 73 5f 74 63 6c 28 29 0a 7b 0a 20 20  BIO_s_tcl().{.  
0410: 20 20 72 65 74 75 72 6e 20 26 42 69 6f 4d 65 74    return &BioMet
0420: 68 6f 64 73 3b 0a 7d 0a 0a 73 74 61 74 69 63 20  hods;.}..static 
0430: 69 6e 74 0a 42 69 6f 57 72 69 74 65 20 28 62 69  int.BioWrite (bi
0440: 6f 2c 20 62 75 66 2c 20 62 75 66 4c 65 6e 29 0a  o, buf, bufLen).
0450: 20 20 20 20 42 49 4f 20 2a 62 69 6f 3b 0a 20 20      BIO *bio;.  
0460: 20 20 63 68 61 72 20 2a 62 75 66 3b 0a 20 20 20    char *buf;.   
0470: 20 69 6e 74 20 62 75 66 4c 65 6e 3b 0a 7b 0a 20   int bufLen;.{. 
0480: 20 20 20 54 63 6c 5f 43 68 61 6e 6e 65 6c 20 63     Tcl_Channel c
0490: 68 61 6e 20 3d 20 54 6c 73 5f 47 65 74 50 61 72  han = Tls_GetPar
04a0: 65 6e 74 28 28 53 74 61 74 65 2a 29 62 69 6f 2d  ent((State*)bio-
04b0: 3e 70 74 72 29 3b 0a 20 20 20 20 69 6e 74 20 72  >ptr);.    int r
04c0: 65 74 3b 0a 0a 20 20 20 20 64 70 72 69 6e 74 66  et;..    dprintf
04d0: 28 73 74 64 65 72 72 2c 22 5c 6e 42 69 6f 57 72  (stderr,"\nBioWr
04e0: 69 74 65 28 30 78 25 78 2c 20 3c 62 75 66 3e 2c  ite(0x%x, <buf>,
04f0: 20 25 64 29 20 5b 30 78 25 78 5d 22 2c 20 62 69   %d) [0x%x]", bi
0500: 6f 2c 20 62 75 66 4c 65 6e 2c 20 63 68 61 6e 29  o, bufLen, chan)
0510: 3b 0a 0a 23 69 66 64 65 66 20 54 43 4c 5f 43 48  ;..#ifdef TCL_CH
0520: 41 4e 4e 45 4c 5f 56 45 52 53 49 4f 4e 5f 32 0a  ANNEL_VERSION_2.
0530: 20 20 20 20 72 65 74 20 3d 20 54 63 6c 5f 57 72      ret = Tcl_Wr
0540: 69 74 65 52 61 77 28 20 63 68 61 6e 2c 20 62 75  iteRaw( chan, bu
0550: 66 2c 20 62 75 66 4c 65 6e 29 3b 0a 23 65 6c 73  f, bufLen);.#els
0560: 65 0a 20 20 20 20 72 65 74 20 3d 20 54 63 6c 5f  e.    ret = Tcl_
0570: 57 72 69 74 65 28 20 63 68 61 6e 2c 20 62 75 66  Write( chan, buf
0580: 2c 20 62 75 66 4c 65 6e 29 3b 0a 23 65 6e 64 69  , bufLen);.#endi
0590: 66 0a 0a 20 20 20 20 64 70 72 69 6e 74 66 28 73  f..    dprintf(s
05a0: 74 64 65 72 72 2c 22 5c 6e 5b 30 78 25 78 5d 20  tderr,"\n[0x%x] 
05b0: 42 69 6f 57 72 69 74 65 28 25 64 29 20 2d 3e 20  BioWrite(%d) -> 
05c0: 25 64 20 5b 25 64 2e 25 64 5d 22 2c 20 63 68 61  %d [%d.%d]", cha
05d0: 6e 2c 20 62 75 66 4c 65 6e 2c 20 72 65 74 2c 0a  n, bufLen, ret,.
05e0: 09 09 54 63 6c 5f 45 6f 66 28 20 63 68 61 6e 29  ..Tcl_Eof( chan)
05f0: 2c 20 54 63 6c 5f 47 65 74 45 72 72 6e 6f 28 29  , Tcl_GetErrno()
0600: 29 3b 0a 0a 20 20 20 20 42 49 4f 5f 63 6c 65 61  );..    BIO_clea
0610: 72 5f 66 6c 61 67 73 28 62 69 6f 2c 20 42 49 4f  r_flags(bio, BIO
0620: 5f 46 4c 41 47 53 5f 57 52 49 54 45 7c 42 49 4f  _FLAGS_WRITE|BIO
0630: 5f 46 4c 41 47 53 5f 53 48 4f 55 4c 44 5f 52 45  _FLAGS_SHOULD_RE
0640: 54 52 59 29 3b 0a 0a 20 20 20 20 69 66 20 28 72  TRY);..    if (r
0650: 65 74 20 3d 3d 20 30 29 20 7b 0a 09 69 66 20 28  et == 0) {..if (
0660: 21 54 63 6c 5f 45 6f 66 28 20 63 68 61 6e 29 29  !Tcl_Eof( chan))
0670: 20 7b 0a 09 20 20 20 20 42 49 4f 5f 73 65 74 5f   {..    BIO_set_
0680: 72 65 74 72 79 5f 77 72 69 74 65 28 62 69 6f 29  retry_write(bio)
0690: 3b 0a 09 20 20 20 20 72 65 74 20 3d 20 2d 31 3b  ;..    ret = -1;
06a0: 0a 09 7d 0a 20 20 20 20 7d 0a 20 20 20 20 69 66  ..}.    }.    if
06b0: 20 28 42 49 4f 5f 73 68 6f 75 6c 64 5f 72 65 61   (BIO_should_rea
06c0: 64 28 62 69 6f 29 29 0a 09 42 49 4f 5f 73 65 74  d(bio))..BIO_set
06d0: 5f 72 65 74 72 79 5f 72 65 61 64 28 62 69 6f 29  _retry_read(bio)
06e0: 3b 0a 20 20 20 20 72 65 74 75 72 6e 20 72 65 74  ;.    return ret
06f0: 3b 0a 7d 0a 0a 73 74 61 74 69 63 20 69 6e 74 0a  ;.}..static int.
0700: 42 69 6f 52 65 61 64 20 28 62 69 6f 2c 20 62 75  BioRead (bio, bu
0710: 66 2c 20 62 75 66 4c 65 6e 29 0a 20 20 20 20 42  f, bufLen).    B
0720: 49 4f 20 2a 62 69 6f 3b 0a 20 20 20 20 63 68 61  IO *bio;.    cha
0730: 72 20 2a 62 75 66 3b 0a 20 20 20 20 69 6e 74 20  r *buf;.    int 
0740: 62 75 66 4c 65 6e 3b 0a 7b 0a 20 20 20 20 54 63  bufLen;.{.    Tc
0750: 6c 5f 43 68 61 6e 6e 65 6c 20 63 68 61 6e 20 3d  l_Channel chan =
0760: 20 54 6c 73 5f 47 65 74 50 61 72 65 6e 74 28 28   Tls_GetParent((
0770: 53 74 61 74 65 2a 29 62 69 6f 2d 3e 70 74 72 29  State*)bio->ptr)
0780: 3b 0a 20 20 20 20 69 6e 74 20 72 65 74 20 3d 20  ;.    int ret = 
0790: 30 3b 0a 0a 20 20 20 20 64 70 72 69 6e 74 66 28  0;..    dprintf(
07a0: 73 74 64 65 72 72 2c 22 5c 6e 42 69 6f 52 65 61  stderr,"\nBioRea
07b0: 64 28 30 78 25 78 2c 20 3c 62 75 66 3e 2c 20 25  d(0x%x, <buf>, %
07c0: 64 29 20 5b 30 78 25 78 5d 22 2c 20 62 69 6f 2c  d) [0x%x]", bio,
07d0: 20 62 75 66 4c 65 6e 2c 20 63 68 61 6e 29 3b 0a   bufLen, chan);.
07e0: 0a 20 20 20 20 69 66 20 28 62 75 66 20 3d 3d 20  .    if (buf == 
07f0: 4e 55 4c 4c 29 20 72 65 74 75 72 6e 20 30 3b 0a  NULL) return 0;.
0800: 0a 23 69 66 64 65 66 20 54 43 4c 5f 43 48 41 4e  .#ifdef TCL_CHAN
0810: 4e 45 4c 5f 56 45 52 53 49 4f 4e 5f 32 0a 20 20  NEL_VERSION_2.  
0820: 20 20 72 65 74 20 3d 20 54 63 6c 5f 52 65 61 64    ret = Tcl_Read
0830: 52 61 77 28 20 63 68 61 6e 2c 20 62 75 66 2c 20  Raw( chan, buf, 
0840: 62 75 66 4c 65 6e 29 3b 0a 23 65 6c 73 65 0a 20  bufLen);.#else. 
0850: 20 20 20 72 65 74 20 3d 20 54 63 6c 5f 52 65 61     ret = Tcl_Rea
0860: 64 28 20 63 68 61 6e 2c 20 62 75 66 2c 20 62 75  d( chan, buf, bu
0870: 66 4c 65 6e 29 3b 0a 23 65 6e 64 69 66 0a 0a 20  fLen);.#endif.. 
0880: 20 20 20 64 70 72 69 6e 74 66 28 73 74 64 65 72     dprintf(stder
0890: 72 2c 22 5c 6e 5b 30 78 25 78 5d 20 42 69 6f 52  r,"\n[0x%x] BioR
08a0: 65 61 64 28 25 64 29 20 2d 3e 20 25 64 20 5b 25  ead(%d) -> %d [%
08b0: 64 2e 25 64 5d 22 2c 20 63 68 61 6e 2c 20 62 75  d.%d]", chan, bu
08c0: 66 4c 65 6e 2c 20 72 65 74 2c 0a 09 54 63 6c 5f  fLen, ret,..Tcl_
08d0: 45 6f 66 28 63 68 61 6e 29 2c 20 54 63 6c 5f 47  Eof(chan), Tcl_G
08e0: 65 74 45 72 72 6e 6f 28 29 29 3b 0a 0a 20 20 20  etErrno());..   
08f0: 20 42 49 4f 5f 63 6c 65 61 72 5f 66 6c 61 67 73   BIO_clear_flags
0900: 28 62 69 6f 2c 20 42 49 4f 5f 46 4c 41 47 53 5f  (bio, BIO_FLAGS_
0910: 52 45 41 44 7c 42 49 4f 5f 46 4c 41 47 53 5f 53  READ|BIO_FLAGS_S
0920: 48 4f 55 4c 44 5f 52 45 54 52 59 29 3b 0a 0a 20  HOULD_RETRY);.. 
0930: 20 20 20 69 66 20 28 72 65 74 20 3d 3d 20 30 29     if (ret == 0)
0940: 20 7b 0a 09 69 66 20 28 21 54 63 6c 5f 45 6f 66   {..if (!Tcl_Eof
0950: 28 20 63 68 61 6e 29 29 20 7b 0a 09 20 20 20 20  ( chan)) {..    
0960: 42 49 4f 5f 73 65 74 5f 72 65 74 72 79 5f 72 65  BIO_set_retry_re
0970: 61 64 28 62 69 6f 29 3b 0a 09 20 20 20 20 72 65  ad(bio);..    re
0980: 74 20 3d 20 2d 31 3b 0a 09 7d 0a 20 20 20 20 7d  t = -1;..}.    }
0990: 0a 20 20 20 20 69 66 20 28 42 49 4f 5f 73 68 6f  .    if (BIO_sho
09a0: 75 6c 64 5f 77 72 69 74 65 28 62 69 6f 29 29 0a  uld_write(bio)).
09b0: 09 42 49 4f 5f 73 65 74 5f 72 65 74 72 79 5f 77  .BIO_set_retry_w
09c0: 72 69 74 65 28 62 69 6f 29 3b 0a 20 20 20 20 72  rite(bio);.    r
09d0: 65 74 75 72 6e 20 72 65 74 3b 0a 7d 0a 0a 73 74  eturn ret;.}..st
09e0: 61 74 69 63 20 69 6e 74 0a 42 69 6f 50 75 74 73  atic int.BioPuts
09f0: 09 28 62 69 6f 2c 20 73 74 72 29 0a 20 20 20 20  .(bio, str).    
0a00: 42 49 4f 20 2a 62 69 6f 3b 0a 20 20 20 20 63 68  BIO *bio;.    ch
0a10: 61 72 20 2a 73 74 72 3b 0a 7b 0a 20 20 20 20 72  ar *str;.{.    r
0a20: 65 74 75 72 6e 20 42 69 6f 57 72 69 74 65 28 20  eturn BioWrite( 
0a30: 62 69 6f 2c 20 73 74 72 2c 20 73 74 72 6c 65 6e  bio, str, strlen
0a40: 28 73 74 72 29 29 3b 0a 7d 0a 0a 73 74 61 74 69  (str));.}..stati
0a50: 63 20 6c 6f 6e 67 0a 42 69 6f 43 74 72 6c 09 28  c long.BioCtrl.(
0a60: 62 69 6f 2c 20 63 6d 64 2c 20 6e 75 6d 2c 20 70  bio, cmd, num, p
0a70: 74 72 29 0a 20 20 20 20 42 49 4f 20 2a 62 69 6f  tr).    BIO *bio
0a80: 3b 0a 20 20 20 20 69 6e 74 20 63 6d 64 3b 0a 20  ;.    int cmd;. 
0a90: 20 20 20 6c 6f 6e 67 20 6e 75 6d 3b 0a 20 20 20     long num;.   
0aa0: 20 63 68 61 72 20 2a 70 74 72 3b 0a 7b 0a 20 20   char *ptr;.{.  
0ab0: 20 20 54 63 6c 5f 43 68 61 6e 6e 65 6c 20 63 68    Tcl_Channel ch
0ac0: 61 6e 20 3d 20 54 6c 73 5f 47 65 74 50 61 72 65  an = Tls_GetPare
0ad0: 6e 74 28 28 53 74 61 74 65 2a 29 62 69 6f 2d 3e  nt((State*)bio->
0ae0: 70 74 72 29 3b 0a 20 20 20 20 6c 6f 6e 67 20 72  ptr);.    long r
0af0: 65 74 20 3d 20 31 3b 0a 20 20 20 20 69 6e 74 20  et = 1;.    int 
0b00: 2a 69 70 3b 0a 0a 20 20 20 20 64 70 72 69 6e 74  *ip;..    dprint
0b10: 66 28 73 74 64 65 72 72 2c 22 5c 6e 42 69 6f 43  f(stderr,"\nBioC
0b20: 74 72 6c 28 30 78 25 78 2c 20 30 78 25 78 2c 20  trl(0x%x, 0x%x, 
0b30: 30 78 25 78 2c 20 30 78 25 78 29 22 2c 20 62 69  0x%x, 0x%x)", bi
0b40: 6f 2c 20 63 6d 64 2c 20 6e 75 6d 2c 20 70 74 72  o, cmd, num, ptr
0b50: 29 3b 0a 0a 20 20 20 20 73 77 69 74 63 68 20 28  );..    switch (
0b60: 63 6d 64 29 20 7b 0a 20 20 20 20 63 61 73 65 20  cmd) {.    case 
0b70: 42 49 4f 5f 43 54 52 4c 5f 52 45 53 45 54 3a 0a  BIO_CTRL_RESET:.
0b80: 09 6e 75 6d 20 3d 20 30 3b 0a 20 20 20 20 63 61  .num = 0;.    ca
0b90: 73 65 20 42 49 4f 5f 43 5f 46 49 4c 45 5f 53 45  se BIO_C_FILE_SE
0ba0: 45 4b 3a 0a 20 20 20 20 63 61 73 65 20 42 49 4f  EK:.    case BIO
0bb0: 5f 43 5f 46 49 4c 45 5f 54 45 4c 4c 3a 0a 09 72  _C_FILE_TELL:..r
0bc0: 65 74 20 3d 20 30 3b 0a 09 62 72 65 61 6b 3b 0a  et = 0;..break;.
0bd0: 20 20 20 20 63 61 73 65 20 42 49 4f 5f 43 54 52      case BIO_CTR
0be0: 4c 5f 49 4e 46 4f 3a 0a 09 72 65 74 20 3d 20 31  L_INFO:..ret = 1
0bf0: 3b 0a 09 62 72 65 61 6b 3b 0a 20 20 20 20 63 61  ;..break;.    ca
0c00: 73 65 20 42 49 4f 5f 43 5f 53 45 54 5f 46 44 3a  se BIO_C_SET_FD:
0c10: 0a 09 42 69 6f 46 72 65 65 28 62 69 6f 29 3b 0a  ..BioFree(bio);.
0c20: 09 2f 2a 20 53 65 74 73 20 53 74 61 74 65 2a 20  ./* Sets State* 
0c30: 2a 2f 0a 09 62 69 6f 2d 3e 70 74 72 09 3d 20 2a  */..bio->ptr.= *
0c40: 28 28 63 68 61 72 20 2a 2a 29 70 74 72 29 3b 0a  ((char **)ptr);.
0c50: 09 62 69 6f 2d 3e 73 68 75 74 64 6f 77 6e 09 3d  .bio->shutdown.=
0c60: 20 28 69 6e 74 29 6e 75 6d 3b 0a 09 62 69 6f 2d   (int)num;..bio-
0c70: 3e 69 6e 69 74 09 3d 20 31 3b 0a 09 62 72 65 61  >init.= 1;..brea
0c80: 6b 3b 0a 20 20 20 20 63 61 73 65 20 42 49 4f 5f  k;.    case BIO_
0c90: 43 5f 47 45 54 5f 46 44 3a 0a 09 69 66 20 28 62  C_GET_FD:..if (b
0ca0: 69 6f 2d 3e 69 6e 69 74 29 20 7b 0a 09 20 20 20  io->init) {..   
0cb0: 20 69 70 20 3d 20 28 69 6e 74 20 2a 29 70 74 72   ip = (int *)ptr
0cc0: 3b 0a 09 20 20 20 20 69 66 20 28 69 70 20 21 3d  ;..    if (ip !=
0cd0: 20 4e 55 4c 4c 29 20 7b 0a 09 09 2a 69 70 20 3d   NULL) {...*ip =
0ce0: 20 62 69 6f 2d 3e 6e 75 6d 3b 0a 09 20 20 20 20   bio->num;..    
0cf0: 7d 0a 09 20 20 20 20 72 65 74 20 3d 20 62 69 6f  }..    ret = bio
0d00: 2d 3e 6e 75 6d 3b 0a 09 7d 20 65 6c 73 65 20 7b  ->num;..} else {
0d10: 0a 09 20 20 20 20 72 65 74 20 3d 20 2d 31 3b 0a  ..    ret = -1;.
0d20: 09 7d 0a 09 62 72 65 61 6b 3b 0a 20 20 20 20 63  .}..break;.    c
0d30: 61 73 65 20 42 49 4f 5f 43 54 52 4c 5f 47 45 54  ase BIO_CTRL_GET
0d40: 5f 43 4c 4f 53 45 3a 0a 09 72 65 74 20 3d 20 62  _CLOSE:..ret = b
0d50: 69 6f 2d 3e 73 68 75 74 64 6f 77 6e 3b 0a 09 62  io->shutdown;..b
0d60: 72 65 61 6b 3b 0a 20 20 20 20 63 61 73 65 20 42  reak;.    case B
0d70: 49 4f 5f 43 54 52 4c 5f 53 45 54 5f 43 4c 4f 53  IO_CTRL_SET_CLOS
0d80: 45 3a 0a 09 62 69 6f 2d 3e 73 68 75 74 64 6f 77  E:..bio->shutdow
0d90: 6e 20 3d 20 28 69 6e 74 29 6e 75 6d 3b 0a 09 62  n = (int)num;..b
0da0: 72 65 61 6b 3b 0a 20 20 20 20 63 61 73 65 20 42  reak;.    case B
0db0: 49 4f 5f 43 54 52 4c 5f 45 4f 46 3a 0a 09 64 70  IO_CTRL_EOF:..dp
0dc0: 72 69 6e 74 66 28 73 74 64 65 72 72 2c 20 22 42  rintf(stderr, "B
0dd0: 49 4f 5f 43 54 52 4c 5f 45 4f 46 5c 6e 22 29 3b  IO_CTRL_EOF\n");
0de0: 0a 09 72 65 74 20 3d 20 54 63 6c 5f 45 6f 66 28  ..ret = Tcl_Eof(
0df0: 20 63 68 61 6e 29 3b 0a 09 62 72 65 61 6b 3b 0a   chan);..break;.
0e00: 20 20 20 20 63 61 73 65 20 42 49 4f 5f 43 54 52      case BIO_CTR
0e10: 4c 5f 50 45 4e 44 49 4e 47 3a 0a 09 72 65 74 20  L_PENDING:..ret 
0e20: 3d 20 28 54 63 6c 5f 49 6e 70 75 74 42 75 66 66  = (Tcl_InputBuff
0e30: 65 72 65 64 28 63 68 61 6e 29 20 3f 20 31 20 3a  ered(chan) ? 1 :
0e40: 20 30 29 3b 0a 09 64 70 72 69 6e 74 66 28 73 74   0);..dprintf(st
0e50: 64 65 72 72 2c 20 22 42 49 4f 5f 43 54 52 4c 5f  derr, "BIO_CTRL_
0e60: 50 45 4e 44 49 4e 47 28 25 64 29 5c 6e 22 2c 20  PENDING(%d)\n", 
0e70: 72 65 74 29 3b 0a 09 62 72 65 61 6b 3b 0a 20 20  ret);..break;.  
0e80: 20 20 63 61 73 65 20 42 49 4f 5f 43 54 52 4c 5f    case BIO_CTRL_
0e90: 57 50 45 4e 44 49 4e 47 3a 0a 09 72 65 74 20 3d  WPENDING:..ret =
0ea0: 20 30 3b 0a 09 62 72 65 61 6b 3b 0a 20 20 20 20   0;..break;.    
0eb0: 63 61 73 65 20 42 49 4f 5f 43 54 52 4c 5f 44 55  case BIO_CTRL_DU
0ec0: 50 3a 0a 09 62 72 65 61 6b 3b 0a 20 20 20 20 63  P:..break;.    c
0ed0: 61 73 65 20 42 49 4f 5f 43 54 52 4c 5f 46 4c 55  ase BIO_CTRL_FLU
0ee0: 53 48 3a 0a 09 64 70 72 69 6e 74 66 28 73 74 64  SH:..dprintf(std
0ef0: 65 72 72 2c 20 22 42 49 4f 5f 43 54 52 4c 5f 46  err, "BIO_CTRL_F
0f00: 4c 55 53 48 5c 6e 22 29 3b 0a 09 69 66 20 28 0a  LUSH\n");..if (.
0f10: 23 69 66 64 65 66 20 54 43 4c 5f 43 48 41 4e 4e  #ifdef TCL_CHANN
0f20: 45 4c 5f 56 45 52 53 49 4f 4e 5f 32 0a 09 20 20  EL_VERSION_2..  
0f30: 20 20 54 63 6c 5f 57 72 69 74 65 52 61 77 28 63    Tcl_WriteRaw(c
0f40: 68 61 6e 2c 20 22 22 2c 20 30 29 20 3e 3d 20 30  han, "", 0) >= 0
0f50: 0a 23 65 6c 73 65 0a 09 20 20 20 20 54 63 6c 5f  .#else..    Tcl_
0f60: 46 6c 75 73 68 28 20 63 68 61 6e 29 20 3d 3d 20  Flush( chan) == 
0f70: 54 43 4c 5f 4f 4b 0a 23 65 6e 64 69 66 0a 09 20  TCL_OK.#endif.. 
0f80: 20 20 20 29 20 7b 0a 09 20 20 20 20 72 65 74 20     ) {..    ret 
0f90: 3d 20 31 3b 0a 09 7d 20 65 6c 73 65 20 7b 0a 09  = 1;..} else {..
0fa0: 20 20 20 20 72 65 74 20 3d 20 2d 31 3b 0a 09 7d      ret = -1;..}
0fb0: 0a 09 62 72 65 61 6b 3b 0a 20 20 20 20 64 65 66  ..break;.    def
0fc0: 61 75 6c 74 3a 0a 09 72 65 74 20 3d 20 30 3b 0a  ault:..ret = 0;.
0fd0: 09 62 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 20 20  .break;.    }.  
0fe0: 20 20 72 65 74 75 72 6e 28 72 65 74 29 3b 0a 7d    return(ret);.}
0ff0: 0a 0a 73 74 61 74 69 63 20 69 6e 74 0a 42 69 6f  ..static int.Bio
1000: 4e 65 77 09 28 62 69 6f 29 0a 20 20 20 20 42 49  New.(bio).    BI
1010: 4f 20 2a 62 69 6f 3b 0a 7b 0a 20 20 20 20 62 69  O *bio;.{.    bi
1020: 6f 2d 3e 69 6e 69 74 09 3d 20 30 3b 0a 20 20 20  o->init.= 0;.   
1030: 20 62 69 6f 2d 3e 6e 75 6d 09 3d 20 30 3b 0a 20   bio->num.= 0;. 
1040: 20 20 20 62 69 6f 2d 3e 70 74 72 09 3d 20 4e 55     bio->ptr.= NU
1050: 4c 4c 3b 0a 20 20 20 20 62 69 6f 2d 3e 66 6c 61  LL;.    bio->fla
1060: 67 73 09 3d 20 30 3b 0a 0a 20 20 20 20 72 65 74  gs.= 0;..    ret
1070: 75 72 6e 20 31 3b 0a 7d 0a 0a 73 74 61 74 69 63  urn 1;.}..static
1080: 20 69 6e 74 0a 42 69 6f 46 72 65 65 09 28 62 69   int.BioFree.(bi
1090: 6f 29 0a 20 20 20 20 42 49 4f 20 2a 62 69 6f 3b  o).    BIO *bio;
10a0: 0a 7b 0a 20 20 20 20 69 66 20 28 62 69 6f 20 3d  .{.    if (bio =
10b0: 3d 20 4e 55 4c 4c 29 20 7b 0a 09 72 65 74 75 72  = NULL) {..retur
10c0: 6e 20 30 3b 0a 20 20 20 20 7d 0a 0a 20 20 20 20  n 0;.    }..    
10d0: 69 66 20 28 62 69 6f 2d 3e 73 68 75 74 64 6f 77  if (bio->shutdow
10e0: 6e 29 20 7b 0a 09 69 66 20 28 62 69 6f 2d 3e 69  n) {..if (bio->i
10f0: 6e 69 74 29 20 7b 0a 09 20 20 20 20 2f 2a 73 68  nit) {..    /*sh
1100: 75 74 64 6f 77 6e 28 62 69 6f 2d 3e 6e 75 6d 2c  utdown(bio->num,
1110: 20 32 29 20 2a 2f 0a 09 20 20 20 20 2f 2a 63 6c   2) */..    /*cl
1120: 6f 73 65 73 6f 63 6b 65 74 28 62 69 6f 2d 3e 6e  osesocket(bio->n
1130: 75 6d 29 20 2a 2f 0a 09 7d 0a 09 62 69 6f 2d 3e  um) */..}..bio->
1140: 69 6e 69 74 09 3d 20 30 3b 0a 09 62 69 6f 2d 3e  init.= 0;..bio->
1150: 66 6c 61 67 73 09 3d 20 30 3b 0a 09 62 69 6f 2d  flags.= 0;..bio-
1160: 3e 6e 75 6d 09 3d 20 30 3b 0a 20 20 20 20 7d 0a  >num.= 0;.    }.
1170: 20 20 20 20 72 65 74 75 72 6e 20 31 3b 0a 7d 0a      return 1;.}.