9 int staticerr(Tcl_Interp *ip, const char *m) {
10 Tcl_SetResult(ip, (char*)m, TCL_STATIC);
14 int posixerr(Tcl_Interp *ip, int errnoval, const char *m) {
19 em= Tcl_PosixError(ip);
20 Tcl_AppendResult(ip, m, ": ", em, (char*)0);
24 void objfreeir(Tcl_Obj *o) {
25 if (o->typePtr && o->typePtr->freeIntRepProc)
26 o->typePtr->freeIntRepProc(o);
30 int do_hbytes_rep_info(ClientData cd, Tcl_Interp *ip,
31 Tcl_Obj *obj, Tcl_Obj **result) {
36 if (obj->typePtr == &hbytes_type) {
37 HBytes_Value *v= OBJ_HBYTES(obj);
38 memset(nums,0,sizeof(nums));
39 nums[1]= hbytes_len(v);
41 if (HBYTES_ISEMPTY(v)) tn= "empty";
42 else if (HBYTES_ISSENTINEL(v)) tn= "sentinel!";
43 else if (HBYTES_ISSIMPLE(v)) tn= "simple";
45 HBytes_ComplexValue *cx= v->begin_complex;
47 nums[0]= cx->prespace;
48 nums[2]= cx->avail - cx->len;
56 objl[0]= Tcl_NewStringObj((char*)tn,-1);
57 for (i=0; i<lnl; i++) objl[i+1]= Tcl_NewIntObj(nums[i]);
58 *result= Tcl_NewListObj(lnl+1,objl);
63 static void hbytes_t_dup(Tcl_Obj *src, Tcl_Obj *dup) {
65 hbytes_array(OBJ_HBYTES(dup),
66 hbytes_data(OBJ_HBYTES(src)),
67 hbytes_len(OBJ_HBYTES(src)));
70 static void hbytes_t_free(Tcl_Obj *o) {
71 hbytes_free(OBJ_HBYTES(o));
74 void obj_updatestr_array(Tcl_Obj *o, const Byte *byte, int l) {
77 str= o->bytes= TALLOC(l*2+1);
80 sprintf(str,"%02x",*byte);
86 static void hbytes_t_ustr(Tcl_Obj *o) {
87 obj_updatestr_array(o,
88 hbytes_data(OBJ_HBYTES(o)),
89 hbytes_len(OBJ_HBYTES(o)));
92 static int hbytes_t_sfa(Tcl_Interp *ip, Tcl_Obj *o) {
94 Byte *startbytes, *bytes;
98 os= str= Tcl_GetStringFromObj(o,&l); assert(str);
101 if (l & 1) return staticerr(ip, "hbytes: conversion from hex:"
102 " odd length in hex");
104 startbytes= bytes= hbytes_arrayspace(OBJ_HBYTES(o), l/2);
110 *bytes++= strtoul(cbuf,&ep,16);
112 hbytes_free(OBJ_HBYTES(o));
113 return staticerr(ip, "hbytes: conversion from hex:"
119 o->typePtr = &hbytes_type;
123 Tcl_ObjType hbytes_type = {
125 hbytes_t_free, hbytes_t_dup, hbytes_t_ustr, hbytes_t_sfa
128 int do_hbytes_raw2h(ClientData cd, Tcl_Interp *ip,
129 Tcl_Obj *binary, HBytes_Value *result) {
133 str= Tcl_GetStringFromObj(binary,&l);
134 hbytes_array(result, str, l);
138 int do_hbytes_h2raw(ClientData cd, Tcl_Interp *ip,
139 HBytes_Value hex, Tcl_Obj **result) {
140 *result= Tcl_NewStringObj(hbytes_data(&hex), hbytes_len(&hex));
144 int do_hbytes_length(ClientData cd, Tcl_Interp *ip,
145 HBytes_Value v, int *result) {
146 *result= hbytes_len(&v);
150 int do__hbytes(ClientData cd, Tcl_Interp *ip,
151 const HBytes_SubCommand *subcmd,
152 int objc, Tcl_Obj *const *objv) {
153 return subcmd->func(0,ip,objc,objv);
156 #define URANDOM "/dev/urandom"
158 int get_urandom(Tcl_Interp *ip, Byte *buffer, int l) {
159 static FILE *urandom;
164 urandom= fopen(URANDOM,"rb");
165 if (!urandom) return posixerr(ip,errno,"open " URANDOM);
167 r= fread(buffer,1,l,urandom);
171 fclose(urandom); urandom=0;
173 if (ferror(urandom)) {
174 return posixerr(ip,errno,"read " URANDOM);
176 assert(feof(urandom));
177 return staticerr(ip, URANDOM " gave eof!");
181 int Hbytes_Init(Tcl_Interp *ip) {
182 Tcl_RegisterObjType(&hbytes_type);
183 Tcl_RegisterObjType(&blockcipherkey_type);
184 Tcl_RegisterObjType(&enum_nearlytype);
185 Tcl_RegisterObjType(&enum1_nearlytype);
186 Tcl_CreateObjCommand(ip,"hbytes", pa__hbytes,0,0);