commit 01986ce9001e81cf428f546081a6888c518a54cc from: Omar Polo via: Thomas Adam date: Tue Feb 07 20:14:55 2023 UTC introduce got_object_id_hex to replace some got_sha1_digest_to_str() It's an analogous to got_object_id_str but writes to the given buffer. ok + improvements by stsp@ commit - 8cb46987fe75bb80841acf551340ae2587d35d4a commit + 01986ce9001e81cf428f546081a6888c518a54cc blob - 9d11c8f4f518f5f911295671d3aafcb494a7ec10 blob + 6a161bc20df5aaa7789395593ab2ffc6fbe4b1fb --- include/got_object.h +++ include/got_object.h @@ -13,6 +13,8 @@ * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. */ + +#define GOT_OBJECT_ID_HEX_MAXLEN SHA1_DIGEST_STRING_LENGTH struct got_object_id { u_int8_t sha1[SHA1_DIGEST_LENGTH]; blob - 60f065829263cd6efb7509bf77e6120c3ff8aa62 blob + 6f2dfcfbe30bcc4ee982d27c6380d19f5ac22b3a --- lib/diff.c +++ lib/diff.c @@ -137,8 +137,8 @@ diff_blobs(struct got_diff_line **lines, size_t *nline enum got_diff_algorithm diff_algo) { const struct got_error *err = NULL, *free_err; - char hex1[SHA1_DIGEST_STRING_LENGTH]; - char hex2[SHA1_DIGEST_STRING_LENGTH]; + char hex1[GOT_OBJECT_ID_HEX_MAXLEN]; + char hex2[GOT_OBJECT_ID_HEX_MAXLEN]; const char *idstr1 = NULL, *idstr2 = NULL; char *modestr1 = NULL, *modestr2 = NULL; off_t size1, size2; @@ -345,7 +345,7 @@ diff_blob_file(struct got_diffreg_result **resultp, int force_text_diff, struct got_diffstat_cb_arg *diffstat, FILE *outfile) { const struct got_error *err = NULL, *free_err; - char hex1[SHA1_DIGEST_STRING_LENGTH]; + char hex1[GOT_OBJECT_ID_HEX_MAXLEN]; const char *idstr1 = NULL; struct got_diffreg_result *result = NULL; blob - d12852060882bfaa4f9b35bbb149605727ce2f0a blob + 31a8aca5dcebc80f345a950a87d260a721e8e729 --- lib/error.c +++ lib/error.c @@ -32,7 +32,7 @@ #include "got_lib_delta.h" #include "got_lib_inflate.h" #include "got_lib_object.h" -#include "got_lib_sha1.h" +#include "got_lib_object_parse.h" #ifndef nitems #define nitems(_a) (sizeof(_a) / sizeof((_a)[0])) @@ -376,11 +376,11 @@ got_ferror(FILE *f, int code) const struct got_error * got_error_no_obj(struct got_object_id *id) { - char msg[sizeof("object not found") + SHA1_DIGEST_STRING_LENGTH]; - char id_str[SHA1_DIGEST_STRING_LENGTH]; + char id_str[GOT_OBJECT_ID_HEX_MAXLEN]; + char msg[sizeof("object not found") + sizeof(id_str)]; int ret; - if (!got_sha1_digest_to_str(id->sha1, id_str, sizeof(id_str))) + if (!got_object_id_hex(id, id_str, sizeof(id_str))) return got_error(GOT_ERR_NO_OBJ); ret = snprintf(msg, sizeof(msg), "object %s not found", id_str); blob - 37def8a54e88bcdfbc49e4cc2f7a41fd8175f3b5 blob + 4b96e21f407d84ac399adbccb5d7e21cf86dad1f --- lib/got_lib_object_parse.h +++ lib/got_lib_object_parse.h @@ -16,6 +16,13 @@ struct got_pathlist_head; +/* + * Write the string representation fo an object ID in the given buffer. + * This buffer must be at least GOT_OBJECT_ID_HEX_MAXLEN bytes in size. + * The output depneds on the hash function used by the repository format. + */ +char *got_object_id_hex(struct got_object_id *, char *, size_t); + const struct got_error *got_object_qid_alloc_partial(struct got_object_qid **); struct got_commit_object *got_object_commit_alloc_partial(void); struct got_tree_entry *got_alloc_tree_entry_partial(void); blob - 358d74b8bcbd7a84add024a4336f30c10be59dd0 blob + 076a2ad67006b36618c1b5b08b7f512c5f69d3dc --- lib/object.c +++ lib/object.c @@ -374,7 +374,7 @@ got_object_blob_rewind(struct got_blob_object *blob) char * got_object_blob_id_str(struct got_blob_object *blob, char *buf, size_t size) { - return got_sha1_digest_to_str(blob->id.sha1, buf, size); + return got_object_id_hex(&blob->id, buf, size); } size_t blob - cfb1f98b880d584709f8c09b1952ca89aed51bae blob + 6fc646eb18e40a9ccd26d08703df008856322e4e --- lib/object_parse.c +++ lib/object_parse.c @@ -87,19 +87,25 @@ got_object_qid_alloc_partial(struct got_object_qid **q const struct got_error * got_object_id_str(char **outbuf, struct got_object_id *id) { - static const size_t len = SHA1_DIGEST_STRING_LENGTH; + static const size_t len = GOT_OBJECT_ID_HEX_MAXLEN; *outbuf = malloc(len); if (*outbuf == NULL) return got_error_from_errno("malloc"); - if (got_sha1_digest_to_str(id->sha1, *outbuf, len) == NULL) { + if (got_object_id_hex(id, *outbuf, len) == NULL) { free(*outbuf); *outbuf = NULL; return got_error(GOT_ERR_BAD_OBJ_ID_STR); } return NULL; +} + +char * +got_object_id_hex(struct got_object_id *id, char *buf, size_t len) +{ + return got_sha1_digest_to_str(id->sha1, buf, len); } void