VirtualBox

source: vbox/trunk/include/iprt/bldprog-strtab.h@ 76417

Last change on this file since 76417 was 76346, checked in by vboxsync, 6 years ago

*: Preparing for iprt/string.h, iprt/json.h and iprt/serialport.h no longer including iprt/err.h and string.h no longer including latin1.h (it needs err.h). bugref:9344

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 4.6 KB
Line 
1/** @file
2 * IPRT - Build Program - String Table Generator, Accessors.
3 */
4
5/*
6 * Copyright (C) 2006-2017 Oracle Corporation
7 *
8 * This file is part of VirtualBox Open Source Edition (OSE), as
9 * available from http://www.virtualbox.org. This file is free software;
10 * you can redistribute it and/or modify it under the terms of the GNU
11 * General Public License (GPL) as published by the Free Software
12 * Foundation, in version 2 as it comes in the "COPYING" file of the
13 * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
14 * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
15 *
16 * The contents of this file may alternatively be used under the terms
17 * of the Common Development and Distribution License Version 1.0
18 * (CDDL) only, as it comes in the "COPYING.CDDL" file of the
19 * VirtualBox OSE distribution, in which case the provisions of the
20 * CDDL are applicable instead of those of the GPL.
21 *
22 * You may elect to license modified versions of this file under the
23 * terms and conditions of either the GPL or the CDDL or both.
24 */
25
26#ifndef ___iprt_bldprog_strtab_h
27#define ___iprt_bldprog_strtab_h
28
29#include <iprt/assert.h>
30#include <iprt/err.h>
31#include <iprt/string.h>
32
33
34/**
35 * The default build program string table reference.
36 */
37typedef struct RTBLDPROGSTRREF
38{
39 /** Offset of the string in the string table. */
40 uint32_t off : 22;
41 /** The length of the string. */
42 uint32_t cch : 10;
43} RTBLDPROGSTRREF;
44AssertCompileSize(RTBLDPROGSTRREF, sizeof(uint32_t));
45/** Pointer to a build program string table reference. */
46typedef RTBLDPROGSTRREF const *PCRTBLDPROGSTRREF;
47
48
49typedef struct RTBLDPROGSTRTAB
50{
51 const char *pchStrTab;
52 uint32_t cchStrTab;
53 uint8_t cCompDict;
54 PCRTBLDPROGSTRREF paCompDict;
55} RTBLDPROGSTRTAB;
56typedef const RTBLDPROGSTRTAB *PCRTBLDPROGSTRTAB;
57
58
59/**
60 * Retrieves the decompressed string.
61 *
62 * @returns The string size on success, IPRT status code on failure.
63 * @param pStrTab The string table.
64 * @param offString The offset of the string.
65 * @param cchString The length of the string.
66 * @param pszDst The return buffer.
67 * @param cbDst The size of the return buffer.
68 */
69DECLINLINE(ssize_t) RTBldProgStrTabQueryString(PCRTBLDPROGSTRTAB pStrTab, uint32_t offString, size_t cchString,
70 char *pszDst, size_t cbDst)
71{
72 AssertReturn(offString < pStrTab->cchStrTab, VERR_OUT_OF_RANGE);
73 AssertReturn(offString + cchString <= pStrTab->cchStrTab, VERR_OUT_OF_RANGE);
74
75 if (pStrTab->cCompDict)
76 {
77 /*
78 * Could be compressed, decompress it.
79 */
80 char * const pchDstStart = pszDst;
81 const char *pchSrc = &pStrTab->pchStrTab[offString];
82 while (cchString-- > 0)
83 {
84 unsigned char uch = *pchSrc++;
85 if (!(uch & 0x80))
86 {
87 /*
88 * Plain text.
89 */
90 AssertReturn(cbDst > 1, VERR_BUFFER_OVERFLOW);
91 *pszDst++ = (char)uch;
92 Assert(uch != 0);
93 }
94 else if (uch != 0xff)
95 {
96 /*
97 * Dictionary reference. (No UTF-8 unescaping necessary here.)
98 */
99 PCRTBLDPROGSTRREF pWord = &pStrTab->paCompDict[uch & 0x7f];
100 size_t const cchWord = pWord->cch;
101 AssertReturn((size_t)pWord->off + cchWord <= pStrTab->cchStrTab, VERR_INVALID_PARAMETER);
102 AssertReturn(cbDst > cchWord, VERR_BUFFER_OVERFLOW);
103
104 memcpy(pszDst, &pStrTab->pchStrTab[pWord->off], cchWord);
105 pszDst += cchWord;
106 cbDst -= cchWord;
107 }
108 else
109 {
110 /*
111 * UTF-8 encoded unicode codepoint.
112 */
113 size_t cchCp;
114 RTUNICP uc = ' ';
115 int rc = RTStrGetCpNEx(&pchSrc, &cchString, &uc);
116 AssertStmt(RT_SUCCESS(rc), (uc = '?', pchSrc++, cchString--));
117
118 cchCp = RTStrCpSize(uc);
119 AssertReturn(cbDst > cchCp, VERR_BUFFER_OVERFLOW);
120
121 RTStrPutCp(pszDst, uc);
122 pszDst += cchCp;
123 cbDst -= cchCp;
124 }
125 }
126 AssertReturn(cbDst > 0, VERR_BUFFER_OVERFLOW);
127 *pszDst = '\0';
128 return pszDst - pchDstStart;
129 }
130
131 /*
132 * Not compressed.
133 */
134 AssertReturn(cbDst > cchString, VERR_BUFFER_OVERFLOW);
135 memcpy(pszDst, &pStrTab->pchStrTab[offString], cchString);
136 pszDst[cchString] = '\0';
137 return (ssize_t)cchString;
138}
139
140
141#endif
142
Note: See TracBrowser for help on using the repository browser.

© 2024 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette