8 int staticerr(Tcl_Interp *ip, const char *m) {
9 Tcl_SetResult(ip, (char*)m, TCL_STATIC);
13 void hbytes_set(HBytes_Value *upd, const Byte *array, int l) {
16 upd->start= np= l ? TALLOC(l) : 0;
21 static void hbytes_setintern(Tcl_Obj *o, const Byte *array, int l) {
22 hbytes_set(&HBYTES(o), array, l);
23 o->typePtr = &hbytes_type;
26 static void hbytes_t_dup(Tcl_Obj *src, Tcl_Obj *dup) {
27 hbytes_setintern(src, HBYTES(src).start, HBYTES_LEN(HBYTES(src)));
30 Tcl_Obj *hbytes_set_obj(Tcl_Obj *overwrite, const Byte *array, int l) {
31 if (!overwrite) overwrite= Tcl_NewObj();
33 Tcl_InvalidateStringRep(overwrite);
34 hbytes_setintern(overwrite, array, l);
38 static void hbytes_t_free(Tcl_Obj *o) {
39 TFREE(HBYTES(o).start);
42 static void hbytes_t_ustr(Tcl_Obj *o) {
47 l= HBYTES_LEN(HBYTES(o));
48 byte= HBYTES(o).start;
49 str= o->bytes= TALLOC(l*2+1);
52 sprintf(str,"%02x",*byte);
57 void objfreeir(Tcl_Obj *o) {
58 if (o->typePtr && o->typePtr->freeIntRepProc)
59 o->typePtr->freeIntRepProc(o);
62 static int hbytes_t_sfa(Tcl_Interp *ip, Tcl_Obj *o) {
64 Byte *startbytes, *bytes;
68 os= str= Tcl_GetStringFromObj(o,&l); assert(str);
69 if (l & 1) return staticerr(ip, "hbytes: conversion from hex:"
70 " odd length in hex");
72 startbytes= bytes= l ? TALLOC(l*2) : 0;
77 *bytes++= strtoul(cbuf,&ep,16);
80 fprintf(stderr,">%d|%s|%s<\n",l,os,cbuf);
81 return staticerr(ip, "hbytes: conversion from hex:"
88 HBYTES(o).start= startbytes;
90 o->typePtr = &hbytes_type;
94 Tcl_ObjType hbytes_type = {
96 hbytes_t_free, hbytes_t_dup, hbytes_t_ustr, hbytes_t_sfa
99 int do_hbytes_raw2h(ClientData cd, Tcl_Interp *ip,
100 Tcl_Obj *binary, HBytes_Value *result) {
104 str= Tcl_GetStringFromObj(binary,&l);
105 hbytes_set(result, str, l);
109 int do_hbytes_h2raw(ClientData cd, Tcl_Interp *ip,
110 HBytes_Value hex, Tcl_Obj **result) {
111 *result= Tcl_NewStringObj(hex.start, HBYTES_LEN(hex));
117 static const PadKindInfo padkindinfos[0]= {
128 const PadKindInfo *pk;
129 const BlockCipherInfo *bc;
131 HC_ARG_ENUM(pk, padkindinfos);
133 if (!pk->algname) HC_ARG_INTRANGE(blocksize, 1,255);
134 else { HC_ARG_ENUM(bc, blockciphers); blocksize= bc->blocksize; }
143 int do__hbytes(ClientData cd, Tcl_Interp *ip,
144 const HBytes_SubCommand *subcmd,
145 int objc, Tcl_Obj *const *objv) {
146 return subcmd->func(0,ip,objc,objv);
149 int Hbytes_Init(Tcl_Interp *ip) {
150 Tcl_RegisterObjType(&hbytes_type);
151 Tcl_RegisterObjType(&enum_nearlytype);
152 Tcl_RegisterObjType(&enum1_nearlytype);
153 Tcl_CreateObjCommand(ip,"hbytes", pa__hbytes,0,0);