void hbytes_sentinel(HBytes_Value *returns);
void hbytes_array(HBytes_Value *returns, const Byte *array, int l);
Byte *hbytes_arrayspace(HBytes_Value *returns, int l);
-void hbytes_free(HBytes_Value *frees);
+void hbytes_free(const HBytes_Value *frees);
/* _empty, _sentinel and _array do not free or read the old value;
* _free it first if needed. _free leaves it garbage, so you
* have to call _empty to reuse it. _arrayspace doesn't fill
0, enum_nt_dup, enum_nt_ustr, enum_nt_sfa
};
+static void report_bad(Tcl_Interp *ip, const char *what, const char *supplied,
+ const void *first, size_t each,
+ int (*isvalid)(const void *entry),
+ void (*appres)(Tcl_Interp *ip, const void *entry)) {
+ int count, i;
+ const Byte *entry;
+
+ for (entry=first; isvalid(entry); entry+=each);
+ count= (entry - (const Byte*)first) / each;
+
+ Tcl_ResetResult(ip);
+ Tcl_AppendResult(ip, "bad ",what," \"",supplied,"\": must be",(char*)0);
+
+ for (i=0, entry=first; i<count; i++, entry+=each) {
+ Tcl_AppendResult(ip,
+ (char*)(i==0 ? " " :
+ i+1==count ? ", or " :
+ ", "),
+ (char*)0);
+ appres(ip,entry);
+ }
+}
+
+static const char *enum_str(const void *p) { return *(const char*const*)p; }
+static int isvalid_enum(const void *p) { return !!enum_str(p); }
+static void appres_enum(Tcl_Interp *ip, const void *p) {
+ Tcl_AppendResult(ip, enum_str(p), (char*)0);
+}
+
const void *enum_lookup_cached_func(Tcl_Interp *ip, Tcl_Obj *o,
const void *firstentry, size_t entrysize,
const char *what) {
return ep;
}
- Tcl_ResetResult(ip);
- Tcl_AppendResult(ip, "invalid ",what," - must be one of:",(char*)0);
- for (ep= firstentry;
- (found= *(const char*const*)ep);
- ep += entrysize)
- Tcl_AppendResult(ip, " ",found,(char*)0);
+ report_bad(ip,what,supplied, firstentry,entrysize, isvalid_enum,appres_enum);
return 0;
}
+static int isvalid_enum1(const void *p) { return !!*(const char*)p; }
+static void appres_enum1(Tcl_Interp *ip, const void *p) {
+ char buf[2];
+ buf[0]= *(const char*)p;
+ buf[1]= 0;
+ Tcl_AppendResult(ip, buf, (char*)0);
+}
+
int enum1_lookup_cached_func(Tcl_Interp *ip, Tcl_Obj *o,
const char *opts, const char *what) {
const char *supplied, *fp;
if (!(strlen(supplied) == 1 &&
(fp= strchr(opts, supplied[0])))) {
- Tcl_ResetResult(ip);
- Tcl_AppendResult(ip, "invalid ",what,
- " - must be one character from: ", opts);
+ report_bad(ip,what,supplied, opts,1, isvalid_enum1,appres_enum1);
return -1;
}
return TCL_OK;
}
+int do_hbytes_random(ClientData cd, Tcl_Interp *ip,
+ int length, HBytes_Value *result) {
+ Byte *space;
+ int rc;
+
+ space= hbytes_arrayspace(result, length);
+ rc= get_urandom(ip, space, length);
+ if (rc) { hbytes_free(result); return rc; }
+ return TCL_OK;
+}
+
+int do_hbytes_zeroes(ClientData cd, Tcl_Interp *ip,
+ int length, HBytes_Value *result) {
+ Byte *space;
+ space= hbytes_arrayspace(result, length);
+ memset(space,0,length);
+ return TCL_OK;
+}
+
int do__hbytes(ClientData cd, Tcl_Interp *ip,
const HBytes_SubCommand *subcmd,
int objc, Tcl_Obj *const *objv) {
key obj
?maclen obj
=> hb
+ zeroes
+ length int
+ => hb
+ random
+ length int
+ => hb
+
+#Table udp UDP_SubCommand
+# create
+#
$any_mand= 0;
$any_optl= 0;
$any_eerr= 0;
+ $any_eargc= 0;
+ $pa_hint= '';
+ $pa_hint .= "$c_table " if length $c_table;
+ $pa_hint.= $c_entry;
foreach $arg (@{ $r_entry->{A} }) {
$n= $arg->{N};
$t= $arg->{T};
push @do_al, make_decl($n, $t, $arg->{A});
$pa_vars .= make_decl_init("a_$n", $t, $a, \$pa_init);
if ($arg->{O}) {
+ $pa_hint .= " ?$n?";
if ($any_mand) {
- $pa_argc .= " if (objc < $any_mand) {".
- " e=\"too few args\"; goto e_err; }\n";
- $pa_body .= " objc -= $any_mand;\n";
$any_mand= 0;
$any_eerr= 1;
}
$pa_body .= " if (!objc--) goto end_optional;\n";
$any_optl= 1;
} else {
- die if $any_optl;
+ $pa_hint .= " $n";
+ $pa_body .= " if (!objc--) goto wrong_count_args;\n";
$any_mand++;
+ $any_eargc= 1;
+ die if $any_optl;
}
$paarg= "&a_$n";
$pafin= '';
push @do_aa, "a_$n";
}
if (exists $r_entry->{V}) {
- if ($any_mand) {
- $pa_body .= " objc -= $any_mand;\n";
- }
+ $pa_hint .= " ...";
$va= $r_entry->{V};
push @do_al, subst_in_decl("${va}c", 'int @');
push @do_al, subst_in_decl("${va}v", 'Tcl_Obj *const *@');
push @do_aa, "objc+1", "objv-1";
} else {
if (!$any_optl) {
- $pa_argc .= " if (objc != $any_mand) {".
- " e=\"wrong number of args\"; goto e_err; }\n";
- $any_eerr= 1;
+ $pa_body .= " if (objc) goto wrong_count_args;\n";
+ $any_eargc= 1;
}
}
if ($any_optl) {
$pa_rslt .= "rc_err:\n";
$pa_fini .= " return rc;\n";
+ if ($any_eargc) {
+ $pa_fini .= "\nwrong_count_args:\n";
+ $pa_fini .= " e=\"wrong # args: should be \\\"$pa_hint\\\"\";\n";
+ $pa_fini .= " goto e_err;";
+ $any_eerr= 1;
+ }
if ($any_eerr) {
$pa_vars .= " const char *e;\n";
$pa_fini .= "\n";
%.o: %.c $(HDRS)
$(CC) $(CFLAGS) $(CPPFLAGS) -o $@ -c $<
+serpent.o: serpent.c serpent.h serpentsboxes.h
+ $(CC) $(CFLAGS) $(CPPFLAGS) -O3 -o $@ -c $<
+
clean:
rm -f $(OBJS) $(TARGETS) *~ ./#*#
/* destructor */
-void hbytes_free(HBytes_Value *frees) {
+void hbytes_free(const HBytes_Value *frees) {
if (HBYTES_ISCOMPLEX(frees)) {
HBytes_ComplexValue *cx= COMPLEX(frees);
TFREE(cx->dstart - cx->prespace);
void hbytes_sentinel(HBytes_Value *returns);
void hbytes_array(HBytes_Value *returns, const Byte *array, int l);
Byte *hbytes_arrayspace(HBytes_Value *returns, int l);
-void hbytes_free(HBytes_Value *frees);
+void hbytes_free(const HBytes_Value *frees);
/* _empty, _sentinel and _array do not free or read the old value;
* _free it first if needed. _free leaves it garbage, so you
* have to call _empty to reuse it. _arrayspace doesn't fill
return TCL_OK;
}
+int do_hbytes_random(ClientData cd, Tcl_Interp *ip,
+ int length, HBytes_Value *result) {
+ Byte *space;
+ int rc;
+
+ space= hbytes_arrayspace(result, length);
+ rc= get_urandom(ip, space, length);
+ if (rc) { hbytes_free(result); return rc; }
+ return TCL_OK;
+}
+
+int do_hbytes_zeroes(ClientData cd, Tcl_Interp *ip,
+ int length, HBytes_Value *result) {
+ Byte *space;
+ space= hbytes_arrayspace(result, length);
+ memset(space,0,length);
+ return TCL_OK;
+}
+
int do__hbytes(ClientData cd, Tcl_Interp *ip,
const HBytes_SubCommand *subcmd,
int objc, Tcl_Obj *const *objv) {