dedit
a tool for Teeworlds demo file shenanigens
git clone https://jhhm.no/repos/dedit.git
dedit/src/pack.c
get raw file: pack.c
1/* This file is part of dedit - a tool for editing Teeworlds/DDNet demo files 2 Copyright (C) 2024 JHHM 3 4 Dedit is free software: you can redistribute it and/or modify 5 it under the terms of the GNU General Public License as published by 6 the Free Software Foundation, either version 3 of the License, or 7 (at your option) any later version. 8 9 Dedit is distributed in the hope that it will be useful, 10 but WITHOUT ANY WARRANTY; without even the implied warranty of 11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 GNU General Public License for more details. 13 14 You should have received a copy of the GNU General Public License 15 along with dedit. If not, see <https://www.gnu.org/licenses/>. */ 16 17#define _POSIX_C_SOURCE 200112L 18 19#include <stdio.h> 20 21int frombigendian(const unsigned char in[4]) 22{ 23 return (in[0] << 24) | (in[1] << 16) | (in[2] << 8) | in[3]; 24} 25 26void tobigendian(const int in, unsigned char out[4]) 27{ 28 out[0] = (in >> 24) & 0xff; 29 out[1] = (in >> 16) & 0xff; 30 out[2] = (in >> 8) & 0xff; 31 out[3] = in & 0xff; 32} 33 34/* 35 Function is derived from libtw2 code (MIT/Apache-2.0 License). 36 See NOTICES file for full license details. 37 38 https://github.com/heinrich5991/libtw2/blob/b9286674da94d3d45b9c10ffce517af394e2d58c/packer/src/lib.rs#L62 39*/ 40int readint(char **cp) 41{ 42 int result = 0; 43 int len = 1; 44 45 unsigned char src = **cp; 46 (*cp)++; 47 48 int sign = ((src >> 6) & 1); 49 result |= (src & 0x3f); 50 51 for (int i = 0; i < 4; i++) 52 { 53 // printf("(%d, ", src); 54 if ((src & 0x80) == 0) 55 break; 56 57 src = **cp; 58 (*cp)++; 59 60 len++; 61 62 if (i == 3 && (src & 0xf0) != 0) 63 printf("[ WARNING ] Non zero int padding!\n"); 64 65 result |= (src & 0x7f) << (6 + 7 * i); 66 } 67 if (len > 1 && src == 0x00) 68 printf("[ WARNING ] Overlong int encoding!\n"); 69 70 result ^= -sign; 71 72 return result; 73} 74 75/* 76 Function is derived from libtw2 code (MIT/Apache-2.0 License). 77 See NOTICES file for full license details. 78 79 https://github.com/heinrich5991/libtw2/blob/b9286674da94d3d45b9c10ffce517af394e2d58c/packer/src/lib.rs#L105 80*/ 81void writeint(int i, char **cp) 82{ 83 int sign = i < 0; 84 unsigned int in = i; 85 in = (in ^ -sign); 86 char next = (in & 0x3f); 87 in >>= 6; 88 89 unsigned char head = 0; 90 if (in != 0) 91 head |= 0x80; 92 if (sign != 0) 93 head |= 0x40; 94 95 **cp = head | next; 96 (*cp)++; 97 98 while (in != 0) 99 { 100 next = (in & 0x7f); 101 in >>= 7; 102 103 if (in != 0) 104 head = 0x80; 105 else 106 head = 0; 107 108 **cp = head | next; 109 (*cp)++; 110 } 111} 112 113/* 114 Function is derived from libtw2 code (MIT/Apache-2.0 License). 115 See NOTICES file for full license details. 116 117 https://github.com/teeworlds/teeworlds/blob/a1911c8f7d8458fb4076ef8e7651e8ef5e91ab3e/src/game/gamecore.h#L68 118*/ 119void intstostr(const int *ints, int num, char *str) 120{ 121 while (num) 122 { 123 str[0] = (((*ints) >> 24) & 0xff) - 128; 124 str[1] = (((*ints) >> 16) & 0xff) - 128; 125 str[2] = (((*ints) >> 8) & 0xff) - 128; 126 str[3] = ((*ints) & 0xff) - 128; 127 str += 4; 128 ints++; 129 num--; 130 } 131 str[-1] = 0; 132} 133 134void strtoint(const char *str, int num, int *ints) 135{ 136 int intloc = 3; 137 *ints = 0; 138 while (num) 139 { 140 if (intloc < 0) 141 { 142 ints++; 143 *ints = 0; 144 intloc = 3; 145 } 146 *ints |= (*str + 128) << (8 * intloc); 147 str++; 148 intloc--; 149 num--; 150 } 151}