-
Notifications
You must be signed in to change notification settings - Fork 48
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added passing the needed info through an environment variable in benc…
…ode serialization. Can be further extended, if we need more info.
- Loading branch information
Showing
11 changed files
with
304 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
/* | ||
* This is free and unencumbered software released into the public domain. | ||
* Anyone is free to copy, modify, publish, use, compile, sell, or distribute this software, either in source code form or as a compiled binary, for any purpose, commercial or non-commercial, and by any means. | ||
* In jurisdictions that recognize copyright laws, the author or authors of this software dedicate any and all copyright interest in the software to the public domain. We make this dedication for the benefit of the public at large and to the detriment of our heirs and successors. We intend this dedication to be an overt act of relinquishment in perpetuity of all present and future rights to this software under copyright law. | ||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. | ||
* For more information, please refer to <https://unlicense.org/> | ||
*/ | ||
|
||
#include <stdio.h> | ||
|
||
#include "bencode.h" | ||
|
||
size_t measure_benc_pascal_str(struct pascal_str *s){ | ||
return snprintf(NULL, 0, "%zu:", s->size) + s->size; | ||
} | ||
|
||
void serialize_benc_pascal_str(char **serPtr, struct pascal_str *s){ | ||
*serPtr += sprintf(*serPtr, "%zu:%s", s->size, s->ptr); | ||
} | ||
void serialize_benc_tag(char **serPtr, char tag){ | ||
*serPtr += sprintf(*serPtr, "1:%c", tag); | ||
} | ||
|
||
void serialize_benc_tag_value_pair(char **serPtr, char tag, struct pascal_str *s){ | ||
if(s->ptr){ | ||
serialize_benc_tag(serPtr, tag); | ||
serialize_benc_pascal_str(serPtr, s); | ||
} | ||
} | ||
|
||
|
||
size_t measure_benc_tag_value_pair(struct pascal_str *s){ | ||
if(s->ptr){ | ||
return measure_benc_pascal_str(s) + OUR_BENC_KEY_TAG_SIZE; | ||
} else { | ||
return 0; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
#pragma once | ||
|
||
/* | ||
* This is free and unencumbered software released into the public domain. | ||
* Anyone is free to copy, modify, publish, use, compile, sell, or distribute this software, either in source code form or as a compiled binary, for any purpose, commercial or non-commercial, and by any means. | ||
* In jurisdictions that recognize copyright laws, the author or authors of this software dedicate any and all copyright interest in the software to the public domain. We make this dedication for the benefit of the public at large and to the detriment of our heirs and successors. We intend this dedication to be an overt act of relinquishment in perpetuity of all present and future rights to this software under copyright law. | ||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. | ||
* For more information, please refer to <https://unlicense.org/> | ||
*/ | ||
|
||
#include "pascal_str.h" | ||
|
||
enum { | ||
OUR_BENC_KEY_TAG_SIZE = 3, | ||
BENC_START_END = 2, // l<...>e | ||
}; | ||
|
||
size_t measure_benc_pascal_str(struct pascal_str *s); | ||
void serialize_benc_pascal_str(char **serPtr, struct pascal_str *s); | ||
void serialize_benc_tag(char **serPtr, char tag); | ||
void serialize_benc_tag_value_pair(char **serPtr, char tag, struct pascal_str *s); | ||
size_t measure_benc_tag_value_pair(struct pascal_str *s); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
/* | ||
* This is free and unencumbered software released into the public domain. | ||
* Anyone is free to copy, modify, publish, use, compile, sell, or distribute this software, either in source code form or as a compiled binary, for any purpose, commercial or non-commercial, and by any means. | ||
* In jurisdictions that recognize copyright laws, the author or authors of this software dedicate any and all copyright interest in the software to the public domain. We make this dedication for the benefit of the public at large and to the detriment of our heirs and successors. We intend this dedication to be an overt act of relinquishment in perpetuity of all present and future rights to this software under copyright law. | ||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. | ||
* For more information, please refer to <https://unlicense.org/> | ||
*/ | ||
|
||
#include <string.h> | ||
#include <stdlib.h> | ||
|
||
#include "pascal_str.h" | ||
|
||
struct pascal_str init_pascal_str(const char * s){ | ||
struct pascal_str res; | ||
if(s){ | ||
res.size = strlen(s); | ||
res.ptr = malloc(res.size + 1); | ||
strcpy(res.ptr, s); | ||
}else{ | ||
res.size = 0; | ||
res.ptr = NULL; | ||
} | ||
return res; | ||
} | ||
|
||
struct pascal_str init_nonowning_pascal_str(const char * s){ | ||
struct pascal_str res; | ||
res.size = strlen(s); | ||
res.ptr = s; | ||
return res; | ||
} | ||
|
||
void free_pascal_string(struct pascal_str *str){ | ||
free(str->ptr); | ||
str->ptr = NULL; | ||
str->size = 0; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
#pragma once | ||
|
||
/* | ||
* This is free and unencumbered software released into the public domain. | ||
* Anyone is free to copy, modify, publish, use, compile, sell, or distribute this software, either in source code form or as a compiled binary, for any purpose, commercial or non-commercial, and by any means. | ||
* In jurisdictions that recognize copyright laws, the author or authors of this software dedicate any and all copyright interest in the software to the public domain. We make this dedication for the benefit of the public at large and to the detriment of our heirs and successors. We intend this dedication to be an overt act of relinquishment in perpetuity of all present and future rights to this software under copyright law. | ||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. | ||
* For more information, please refer to <https://unlicense.org/> | ||
*/ | ||
|
||
struct pascal_str { | ||
size_t size; | ||
char * ptr; | ||
}; | ||
|
||
struct pascal_str init_pascal_str(const char * s); | ||
|
||
struct pascal_str init_nonowning_pascal_str(const char * s); | ||
|
||
void free_pascal_string(struct pascal_str *str); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,114 @@ | ||
#include <stdlib.h> | ||
|
||
#include "pascal_str.h" | ||
#include "bencode.h" | ||
|
||
#include "triggerer_info_passthrough.h" | ||
#include <dpkg/ehandle.h> | ||
|
||
|
||
|
||
struct passed_through_package_info passed_through_package_info_init(void){ | ||
struct passed_through_package_info unpackedInfo; | ||
unpackedInfo.count = 0; | ||
unpackedInfo.records = NULL; | ||
return unpackedInfo; | ||
} | ||
|
||
|
||
void free_passed_through_package_info_record(struct passed_through_package_info_record *r){ | ||
free_pascal_string(&r->name); | ||
free_pascal_string(&r->arch); | ||
free_pascal_string(&r->version); | ||
} | ||
|
||
void passed_through_package_info_free(struct passed_through_package_info *unpackedInfo){ | ||
for(unsigned short int i = 0; i< unpackedInfo->count; ++i){ | ||
free_passed_through_package_info_record(&unpackedInfo->records[i]); | ||
} | ||
free(unpackedInfo->records); | ||
unpackedInfo->records = NULL; | ||
unpackedInfo->count = 0; | ||
} | ||
|
||
void passed_through_package_info_emplace_internal(struct passed_through_package_info_record * rec, struct pkginfo *pkg, struct pascal_str (*pascal_str_ctor)(char *)){ | ||
rec->name = pascal_str_ctor(pkg->set->name); | ||
rec->version = pascal_str_ctor(pkg->available.version.version); | ||
rec->arch = pascal_str_ctor(pkg->available.arch->name); | ||
rec->origin = pascal_str_ctor(pkg->available.origin); | ||
} | ||
|
||
void passed_through_package_info_emplace(struct passed_through_package_info_record * rec, struct pkginfo *pkg){ | ||
passed_through_package_info_emplace_internal(rec, pkg, init_pascal_str); | ||
} | ||
|
||
void passed_through_package_info_nonowning_emplace(struct passed_through_package_info_record * rec, struct pkginfo *pkg){ | ||
passed_through_package_info_emplace_internal(rec, pkg, init_nonowning_pascal_str); | ||
} | ||
|
||
void passed_through_package_info_append(struct passed_through_package_info *unpackedInfo, struct pkginfo *pkg){ | ||
if(!unpackedInfo) | ||
return; | ||
|
||
struct passed_through_package_info_record * rec; | ||
unsigned int prevCountAndIndex = unpackedInfo->count; | ||
unpackedInfo->records = realloc(unpackedInfo->records, sizeof(struct passed_through_package_info_record) * (++unpackedInfo->count)); | ||
if(!unpackedInfo->records){ | ||
ohshite("Unable to reallocate in %s", __func__); | ||
} | ||
rec = &(unpackedInfo->records[prevCountAndIndex]); | ||
passed_through_package_info_emplace(rec, pkg); | ||
} | ||
|
||
size_t precompute_the_bencode_serialized_size(struct passed_through_package_info *unpackedInfo){ | ||
size_t totalSize = BENC_START_END; | ||
unsigned int i; | ||
for(i=0;i<unpackedInfo->count;++i){ | ||
if(unpackedInfo->records[i].name.size){ | ||
totalSize += measure_benc_pascal_str(&unpackedInfo->records[i].name); | ||
totalSize += BENC_START_END; | ||
totalSize += measure_benc_tag_value_pair(&unpackedInfo->records[i].arch); | ||
totalSize += measure_benc_tag_value_pair(&unpackedInfo->records[i].version); | ||
totalSize += measure_benc_tag_value_pair(&unpackedInfo->records[i].origin); | ||
} | ||
} | ||
totalSize ++; // null termination | ||
return totalSize; | ||
} | ||
|
||
void serialize_the_info_about_triggerers_into_an_env_variable(struct passed_through_package_info *unpackedInfo){ | ||
unsigned int i; | ||
size_t totalSize = precompute_the_bencode_serialized_size(unpackedInfo); | ||
char buf4str[totalSize]; | ||
char * serPtr; | ||
|
||
memset(buf4str, ' ', totalSize); | ||
buf4str[totalSize - 1] = 0; | ||
serPtr = &buf4str[0]; | ||
*(serPtr++) = 'd'; | ||
for(i=0;i<unpackedInfo->count;++i){ | ||
serialize_benc_pascal_str(&serPtr, &unpackedInfo->records[i].name); | ||
*(serPtr++) = 'd'; | ||
serialize_benc_tag_value_pair(&serPtr, 'A', &unpackedInfo->records[i].arch); | ||
serialize_benc_tag_value_pair(&serPtr, 'V', &unpackedInfo->records[i].version); | ||
serialize_benc_tag_value_pair(&serPtr, 'O', &unpackedInfo->records[i].origin); | ||
*(serPtr++) = 'e'; | ||
} | ||
*(serPtr++) = 'e'; | ||
if((unsigned long)((char*) serPtr - (char*) buf4str) > totalSize){ | ||
ohshit("Buffer overflow %zu > %zu !", (char*) serPtr - (char*) buf4str, totalSize); | ||
} | ||
setenv("DPKG_TRIGGERER_PACKAGES_INFO", buf4str, 1); | ||
} | ||
|
||
void serialize_the_info_about_pkg_into_an_env_variable(struct pkginfo *pkg){ | ||
struct passed_through_package_info_record rec; // no need to free | ||
//passed_through_package_info_append(unpackedInfo, pkg); | ||
passed_through_package_info_emplace(&rec, pkg); | ||
struct passed_through_package_info i; | ||
i.records = &rec; | ||
i.count = 1; | ||
|
||
passed_through_package_info_nonowning_emplace(&rec, pkg); | ||
serialize_the_info_about_triggerers_into_an_env_variable(&i); | ||
} |
Oops, something went wrong.