tag.con commit verify-tag: factor out signature detection (ac58c4c)
   1#include "cache.h"
   2#include "tag.h"
   3#include "commit.h"
   4#include "tree.h"
   5#include "blob.h"
   6
   7#define PGP_SIGNATURE "-----BEGIN PGP SIGNATURE-----"
   8
   9const char *tag_type = "tag";
  10
  11struct object *deref_tag(struct object *o, const char *warn, int warnlen)
  12{
  13        while (o && o->type == OBJ_TAG)
  14                if (((struct tag *)o)->tagged)
  15                        o = parse_object(((struct tag *)o)->tagged->sha1);
  16                else
  17                        o = NULL;
  18        if (!o && warn) {
  19                if (!warnlen)
  20                        warnlen = strlen(warn);
  21                error("missing object referenced by '%.*s'", warnlen, warn);
  22        }
  23        return o;
  24}
  25
  26struct tag *lookup_tag(const unsigned char *sha1)
  27{
  28        struct object *obj = lookup_object(sha1);
  29        if (!obj)
  30                return create_object(sha1, OBJ_TAG, alloc_tag_node());
  31        if (!obj->type)
  32                obj->type = OBJ_TAG;
  33        if (obj->type != OBJ_TAG) {
  34                error("Object %s is a %s, not a tag",
  35                      sha1_to_hex(sha1), typename(obj->type));
  36                return NULL;
  37        }
  38        return (struct tag *) obj;
  39}
  40
  41static unsigned long parse_tag_date(const char *buf, const char *tail)
  42{
  43        const char *dateptr;
  44
  45        while (buf < tail && *buf++ != '>')
  46                /* nada */;
  47        if (buf >= tail)
  48                return 0;
  49        dateptr = buf;
  50        while (buf < tail && *buf++ != '\n')
  51                /* nada */;
  52        if (buf >= tail)
  53                return 0;
  54        /* dateptr < buf && buf[-1] == '\n', so strtoul will stop at buf-1 */
  55        return strtoul(dateptr, NULL, 10);
  56}
  57
  58int parse_tag_buffer(struct tag *item, void *data, unsigned long size)
  59{
  60        unsigned char sha1[20];
  61        char type[20];
  62        const char *bufptr = data;
  63        const char *tail = bufptr + size;
  64        const char *nl;
  65
  66        if (item->object.parsed)
  67                return 0;
  68        item->object.parsed = 1;
  69
  70        if (size < 64)
  71                return -1;
  72        if (memcmp("object ", bufptr, 7) || get_sha1_hex(bufptr + 7, sha1) || bufptr[47] != '\n')
  73                return -1;
  74        bufptr += 48; /* "object " + sha1 + "\n" */
  75
  76        if (prefixcmp(bufptr, "type "))
  77                return -1;
  78        bufptr += 5;
  79        nl = memchr(bufptr, '\n', tail - bufptr);
  80        if (!nl || sizeof(type) <= (nl - bufptr))
  81                return -1;
  82        strncpy(type, bufptr, nl - bufptr);
  83        type[nl - bufptr] = '\0';
  84        bufptr = nl + 1;
  85
  86        if (!strcmp(type, blob_type)) {
  87                item->tagged = &lookup_blob(sha1)->object;
  88        } else if (!strcmp(type, tree_type)) {
  89                item->tagged = &lookup_tree(sha1)->object;
  90        } else if (!strcmp(type, commit_type)) {
  91                item->tagged = &lookup_commit(sha1)->object;
  92        } else if (!strcmp(type, tag_type)) {
  93                item->tagged = &lookup_tag(sha1)->object;
  94        } else {
  95                error("Unknown type %s", type);
  96                item->tagged = NULL;
  97        }
  98
  99        if (prefixcmp(bufptr, "tag "))
 100                return -1;
 101        bufptr += 4;
 102        nl = memchr(bufptr, '\n', tail - bufptr);
 103        if (!nl)
 104                return -1;
 105        item->tag = xmemdupz(bufptr, nl - bufptr);
 106        bufptr = nl + 1;
 107
 108        if (!prefixcmp(bufptr, "tagger "))
 109                item->date = parse_tag_date(bufptr, tail);
 110        else
 111                item->date = 0;
 112
 113        return 0;
 114}
 115
 116int parse_tag(struct tag *item)
 117{
 118        enum object_type type;
 119        void *data;
 120        unsigned long size;
 121        int ret;
 122
 123        if (item->object.parsed)
 124                return 0;
 125        data = read_sha1_file(item->object.sha1, &type, &size);
 126        if (!data)
 127                return error("Could not read %s",
 128                             sha1_to_hex(item->object.sha1));
 129        if (type != OBJ_TAG) {
 130                free(data);
 131                return error("Object %s not a tag",
 132                             sha1_to_hex(item->object.sha1));
 133        }
 134        ret = parse_tag_buffer(item, data, size);
 135        free(data);
 136        return ret;
 137}
 138
 139size_t parse_signature(const char *buf, unsigned long size)
 140{
 141        char *eol;
 142        size_t len = 0;
 143        while (len < size && prefixcmp(buf + len, PGP_SIGNATURE)) {
 144                eol = memchr(buf + len, '\n', size - len);
 145                len += eol ? eol - (buf + len) + 1 : size - len;
 146        }
 147        return len;
 148}