7 int staticerr(Tcl_Interp *ip, const char *m) {
8 Tcl_SetResult(ip, (char*)m, TCL_STATIC);
12 void objfreeir(Tcl_Obj *o) {
13 if (o->typePtr && o->typePtr->freeIntRepProc)
14 o->typePtr->freeIntRepProc(o);
18 int do_hbytes_rep_info(ClientData cd, Tcl_Interp *ip,
19 HBytes_Value v, Tcl_Obj **result) {
24 memset(nums,0,sizeof(nums));
25 nums[1]= hbytes_len(&v);
27 if (HBYTES_ISEMPTY(&v)) tn= "empty";
28 else if (HBYTES_ISSENTINEL(&v)) tn= "sentinel!";
29 else if (HBYTES_ISSIMPLE(&v)) tn= "simple";
31 HBytes_ComplexValue *cx= v.begin_complex;
33 nums[0]= cx->prespace;
34 nums[2]= cx->avail - cx->len;
37 objl[0]= Tcl_NewStringObj((char*)tn,-1);
38 for (i=0; i<3; i++) objl[i+1]= Tcl_NewIntObj(nums[i]);
39 *result= Tcl_NewListObj(4,objl);
43 static void hbytes_t_dup(Tcl_Obj *src, Tcl_Obj *dup) {
45 hbytes_array(OBJ_HBYTES(dup),
46 hbytes_data(OBJ_HBYTES(src)),
47 hbytes_len(OBJ_HBYTES(src)));
50 static void hbytes_t_free(Tcl_Obj *o) {
51 hbytes_free(OBJ_HBYTES(o));
54 static void hbytes_t_ustr(Tcl_Obj *o) {
59 byte= hbytes_data(OBJ_HBYTES(o));
60 l= hbytes_len(OBJ_HBYTES(o));
61 str= o->bytes= TALLOC(l*2+1);
64 sprintf(str,"%02x",*byte);
70 static int hbytes_t_sfa(Tcl_Interp *ip, Tcl_Obj *o) {
72 Byte *startbytes, *bytes;
76 os= str= Tcl_GetStringFromObj(o,&l); assert(str);
79 if (l & 1) return staticerr(ip, "hbytes: conversion from hex:"
80 " odd length in hex");
82 startbytes= bytes= hbytes_arrayspace(OBJ_HBYTES(o), l/2);
88 *bytes++= strtoul(cbuf,&ep,16);
90 hbytes_free(OBJ_HBYTES(o));
91 return staticerr(ip, "hbytes: conversion from hex:"
97 o->typePtr = &hbytes_type;
101 Tcl_ObjType hbytes_type = {
103 hbytes_t_free, hbytes_t_dup, hbytes_t_ustr, hbytes_t_sfa
106 int do_hbytes_raw2h(ClientData cd, Tcl_Interp *ip,
107 Tcl_Obj *binary, HBytes_Value *result) {
111 str= Tcl_GetStringFromObj(binary,&l);
112 hbytes_array(result, str, l);
116 int do_hbytes_h2raw(ClientData cd, Tcl_Interp *ip,
117 HBytes_Value hex, Tcl_Obj **result) {
118 *result= Tcl_NewStringObj(hbytes_data(&hex), hbytes_len(&hex));
122 int do_hbytes_length(ClientData cd, Tcl_Interp *ip,
123 HBytes_Value v, int *result) {
124 *result= hbytes_len(&v);
128 int do__hbytes(ClientData cd, Tcl_Interp *ip,
129 const HBytes_SubCommand *subcmd,
130 int objc, Tcl_Obj *const *objv) {
131 return subcmd->func(0,ip,objc,objv);
134 int Hbytes_Init(Tcl_Interp *ip) {
135 Tcl_RegisterObjType(&hbytes_type);
136 Tcl_RegisterObjType(&enum_nearlytype);
137 Tcl_RegisterObjType(&enum1_nearlytype);
138 Tcl_CreateObjCommand(ip,"hbytes", pa__hbytes,0,0);