|
| 1 | +#include <stdlib.h> |
| 2 | + |
| 3 | +#include "pascal_str.h" |
| 4 | +#include "bencode.h" |
| 5 | + |
| 6 | +#include "triggerer_info_passthrough.h" |
| 7 | +#include <dpkg/ehandle.h> |
| 8 | + |
| 9 | + |
| 10 | + |
| 11 | +struct passed_through_package_info passed_through_package_info_init(void){ |
| 12 | + struct passed_through_package_info unpackedInfo; |
| 13 | + unpackedInfo.count = 0; |
| 14 | + unpackedInfo.records = NULL; |
| 15 | + return unpackedInfo; |
| 16 | +} |
| 17 | + |
| 18 | + |
| 19 | +void free_passed_through_package_info_record(struct passed_through_package_info_record *r){ |
| 20 | + free_pascal_string(&r->name); |
| 21 | + free_pascal_string(&r->arch); |
| 22 | + free_pascal_string(&r->version); |
| 23 | +} |
| 24 | + |
| 25 | +void passed_through_package_info_free(struct passed_through_package_info *unpackedInfo){ |
| 26 | + for(unsigned short int i = 0; i< unpackedInfo->count; ++i){ |
| 27 | + free_passed_through_package_info_record(&unpackedInfo->records[i]); |
| 28 | + } |
| 29 | + free(unpackedInfo->records); |
| 30 | + unpackedInfo->records = NULL; |
| 31 | + unpackedInfo->count = 0; |
| 32 | +} |
| 33 | + |
| 34 | +void passed_through_package_info_emplace_internal(struct passed_through_package_info_record * rec, struct pkginfo *pkg, struct pascal_str (*pascal_str_ctor)(char *)){ |
| 35 | + rec->name = pascal_str_ctor(pkg->set->name); |
| 36 | + rec->version = pascal_str_ctor(pkg->available.version.version); |
| 37 | + rec->arch = pascal_str_ctor(pkg->available.arch->name); |
| 38 | + rec->origin = pascal_str_ctor(pkg->available.origin); |
| 39 | +} |
| 40 | + |
| 41 | +void passed_through_package_info_emplace(struct passed_through_package_info_record * rec, struct pkginfo *pkg){ |
| 42 | + passed_through_package_info_emplace_internal(rec, pkg, init_pascal_str); |
| 43 | +} |
| 44 | + |
| 45 | +void passed_through_package_info_nonowning_emplace(struct passed_through_package_info_record * rec, struct pkginfo *pkg){ |
| 46 | + passed_through_package_info_emplace_internal(rec, pkg, init_nonowning_pascal_str); |
| 47 | +} |
| 48 | + |
| 49 | +void passed_through_package_info_append(struct passed_through_package_info *unpackedInfo, struct pkginfo *pkg){ |
| 50 | + if(!unpackedInfo) |
| 51 | + return; |
| 52 | + |
| 53 | + struct passed_through_package_info_record * rec; |
| 54 | + unsigned int prevCountAndIndex = unpackedInfo->count; |
| 55 | + unpackedInfo->records = realloc(unpackedInfo->records, sizeof(struct passed_through_package_info_record) * (++unpackedInfo->count)); |
| 56 | + if(!unpackedInfo->records){ |
| 57 | + ohshite("Unable to reallocate in %s", __func__); |
| 58 | + } |
| 59 | + rec = &(unpackedInfo->records[prevCountAndIndex]); |
| 60 | + passed_through_package_info_emplace(rec, pkg); |
| 61 | +} |
| 62 | + |
| 63 | +size_t precompute_the_bencode_serialized_size(struct passed_through_package_info *unpackedInfo){ |
| 64 | + size_t totalSize = BENC_START_END; |
| 65 | + unsigned int i; |
| 66 | + for(i=0;i<unpackedInfo->count;++i){ |
| 67 | + if(unpackedInfo->records[i].name.size){ |
| 68 | + totalSize += measure_benc_pascal_str(&unpackedInfo->records[i].name); |
| 69 | + totalSize += BENC_START_END; |
| 70 | + totalSize += measure_benc_tag_value_pair(&unpackedInfo->records[i].arch); |
| 71 | + totalSize += measure_benc_tag_value_pair(&unpackedInfo->records[i].version); |
| 72 | + totalSize += measure_benc_tag_value_pair(&unpackedInfo->records[i].origin); |
| 73 | + } |
| 74 | + } |
| 75 | + totalSize ++; // null termination |
| 76 | + return totalSize; |
| 77 | +} |
| 78 | + |
| 79 | +void serialize_the_info_about_triggerers_into_an_env_variable(struct passed_through_package_info *unpackedInfo){ |
| 80 | + unsigned int i; |
| 81 | + size_t totalSize = precompute_the_bencode_serialized_size(unpackedInfo); |
| 82 | + char buf4str[totalSize]; |
| 83 | + char * serPtr; |
| 84 | + |
| 85 | + memset(buf4str, ' ', totalSize); |
| 86 | + buf4str[totalSize - 1] = 0; |
| 87 | + serPtr = &buf4str[0]; |
| 88 | + *(serPtr++) = 'd'; |
| 89 | + for(i=0;i<unpackedInfo->count;++i){ |
| 90 | + serialize_benc_pascal_str(&serPtr, &unpackedInfo->records[i].name); |
| 91 | + *(serPtr++) = 'd'; |
| 92 | + serialize_benc_tag_value_pair(&serPtr, 'A', &unpackedInfo->records[i].arch); |
| 93 | + serialize_benc_tag_value_pair(&serPtr, 'V', &unpackedInfo->records[i].version); |
| 94 | + serialize_benc_tag_value_pair(&serPtr, 'O', &unpackedInfo->records[i].origin); |
| 95 | + *(serPtr++) = 'e'; |
| 96 | + } |
| 97 | + *(serPtr++) = 'e'; |
| 98 | + if((unsigned long)((char*) serPtr - (char*) buf4str) > totalSize){ |
| 99 | + ohshit("Buffer overflow %zu > %zu !", (char*) serPtr - (char*) buf4str, totalSize); |
| 100 | + } |
| 101 | + setenv("DPKG_TRIGGERER_PACKAGES_INFO", buf4str, 1); |
| 102 | +} |
| 103 | + |
| 104 | +void serialize_the_info_about_pkg_into_an_env_variable(struct pkginfo *pkg){ |
| 105 | + struct passed_through_package_info_record rec; // no need to free |
| 106 | + //passed_through_package_info_append(unpackedInfo, pkg); |
| 107 | + passed_through_package_info_emplace(&rec, pkg); |
| 108 | + struct passed_through_package_info i; |
| 109 | + i.records = &rec; |
| 110 | + i.count = 1; |
| 111 | + |
| 112 | + passed_through_package_info_nonowning_emplace(&rec, pkg); |
| 113 | + serialize_the_info_about_triggerers_into_an_env_variable(&i); |
| 114 | +} |
0 commit comments