Skip to content

Commit c1567ab

Browse files
committed
nqp::sha1(...)
1 parent 4762987 commit c1567ab

File tree

3 files changed

+35
-1
lines changed

3 files changed

+35
-1
lines changed

src/PAST/NQP.pir

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -645,6 +645,9 @@ entry to produce the node to be returned.
645645
maphash['box_s'] = 'repr_box_str__PsP'
646646
maphash['where'] = 'get_id__IP'
647647

648+
# serialization context related opcodes
649+
maphash['sha1'] = 'nqp_sha1__Ss'
650+
648651
# control opcodes
649652
$P0 = new ['Hash']
650653
$P0['pasttype'] = 'if'

src/ops/nqp.ops

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,9 @@ BEGIN_OPS_PREAMBLE
1414
#include "../6model/serialization_context.h"
1515
#include "pmc_sub.h"
1616

17+
/* SHA1 algorithm. */
18+
#include "../../3rdparty/sha1/sha1.h"
19+
1720
#if PARROT_HAS_ICU
1821
# include <unicode/uchar.h>
1922
#endif
@@ -1907,3 +1910,29 @@ inline op is_uprop(out INT, in STR, in STR, in INT) :base_core {
19071910
}
19081911

19091912

1913+
/*
1914+
1915+
=item nqp_sha1
1916+
1917+
Computes the SHA-1 hash of $2 and puts the result in $1.
1918+
1919+
=cut
1920+
1921+
*/
1922+
inline op nqp_sha1(out STR, in STR) :base_core {
1923+
/* Grab the Parrot string as a C string. */
1924+
char *cstr = Parrot_str_to_encoded_cstring(interp, $2, Parrot_utf8_encoding_ptr);
1925+
1926+
/* Compute its SHA-1 and encode it. */
1927+
SHA1_CTX context;
1928+
unsigned char digest[20];
1929+
char output[80];
1930+
SHA1_Init(&context);
1931+
SHA1_Update(&context, (unsigned char*)cstr, strlen(cstr));
1932+
SHA1_Final(&context, digest);
1933+
SHA1_DigestToHex(digest, output);
1934+
1935+
/* Free the C-string and put result into a new string. */
1936+
Parrot_str_free_cstring(cstr);
1937+
$1 = Parrot_str_new_init(interp, &output, 40, Parrot_utf8_encoding_ptr, 0);
1938+
}

tools/build/Makefile.in

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -491,6 +491,7 @@ CLEANUPS = \
491491
$(OPS_DIR)/*$(O) \
492492
$(OPS_DIR)/*$(LOAD_EXT) \
493493
3rdparty/libtommath/*$(O) \
494+
3rdparty/sha1/*$(O) \
494495
$(DYNEXT_DIR)/*$(LOAD_EXT) \
495496

496497
all: $(NQP_EXE) qregex
@@ -688,7 +689,8 @@ $(OPS_DIR)/$(OPS)$(LOAD_EXT): $(OPS_DIR)/$(OPS_SOURCE) $(DYNPMC)
688689
cd src/6model/reprs && $(CC) -c @cc_o_out@Uninstantiable$(O) -I../../../$(PMC_DIR) $(CINCLUDES) $(CFLAGS) Uninstantiable.c
689690
cd src/6model && $(CC) -c @cc_o_out@multi_dispatch$(O) -I../../$(PMC_DIR) $(CINCLUDES) $(CFLAGS) multi_dispatch.c
690691
cd src/6model && $(CC) -c @cc_o_out@serialization_context$(O) -I../../$(PMC_DIR) $(CINCLUDES) $(CFLAGS) serialization_context.c
691-
cd $(OPS_DIR) && $(LD) @ld_out@$(OPS)$(LOAD_EXT) $(OPS)$(O) $(METAMODEL_OBJS) $(LINKARGS)
692+
cd 3rdparty/sha1 && $(CC) -c @cc_o_out@sha1$(O) $(CINCLUDES) $(CFLAGS) sha1.c
693+
cd $(OPS_DIR) && $(LD) @ld_out@$(OPS)$(LOAD_EXT) $(OPS)$(O) $(METAMODEL_OBJS) ../../3rdparty/sha1/sha1$(O) $(LINKARGS)
692694

693695

694696
3rdparty/libtommath/bncore$(O): 3rdparty/libtommath/bncore.c $(LIBTOMMATH_H)

0 commit comments

Comments
 (0)